w3c / w3c/DOM-Parsing

Provide an API to serialize with the "require well-formed" parameter set to true

Open
#84 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
HTML
Stars
28
Forks
14
Avg merge
3h 10m
Merged PRs (30d)
1

Description

The serializeToString static method of XMLSerializer is specified to "produce an XML serialization of root passing a value of false for the require well-formed parameter, and return the result." It's a little bit confusing that something called XMLSerializer might return something that isn't actually valid XML, but I understand that this can't be changed for backwards compatibility. Still, it would be useful to have a mechanism that sets the "require well-formed" parameter to be true, i.e., that throws if the node cannot be serialized to XML.

Background: I'm trying to use the technique in this blog post to render HTML to an image by creating an SVG with a <foreignObject> containing the HTML. As noted on the page, because SVG is XML, you need the contents of <foreignObject> to be valid XML. Doing this with serializeToString, which the post suggests, works for most documents, but not certain less-than-well-formed HTML documents that successfully parse in the browser. The specific case I ran into was an attribute that unescaped quotation marks in the value:

<meta property="og:description" content="I forgot to "escape" this value">

which gets parsed as

<meta property="og:description" content="I forgot to " escape"="" this="" value"="">

i.e., it picks up some attributes whose names have a quotation mark in them. (You can see this by setting an element's innerHTML to the first string and then reading innerHTML again.) This can't be represented in XML, but serializeToString successfully returns an "XML" document with this syntax, which the browser cannot deserialize as XML (e.g., in an <img> with SVG source, or with new DOMParser().parseFromString(xml, "text/xml")).

I can try to see if DOMParser succeeds and throw away the parse if successful, or catch the error event from the <img>, but it would be cleanest if I could just get serializeToString to fail in the first place. Is it possible to add an optional boolean parameter serializeToString(document, requireWellFormed) that defaults to false, or a property of the XMLSerializer, or something?

(Originally reported as https://bugzilla.mozilla.org/1914813 because I didn't realize the spec requires this, but it does, and the behavior is the same in Firefox, Safari, and Chrome. See also mdn/content#35585.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DOM-Parsing XMLSerializer interface and its serializeToString specification, especially the require well-formed parameter linked in the issue. Review the compatibility concerns and related MDN issue, then define the proposed API behavior and its handling of nodes that cannot be represented as well-formed XML.

Written by the indexing model from the issue text.

Assessment

Tech stack
html, xml
Domain
api, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.