CDATA section in SVG title is handled differently in browsers
Test case http://software.hixie.ch/utilities/js/live-dom-viewer/saved/6207
Edge and Firefox support the CDATA section. Safari and Chrome insert a comment.
I think Edge and Firefox match the spec,
https://html.spec.whatwg.org/multipage/parsing.html#markup-declaration-open-state
"If there is an adjusted current node and it is not an element in the HTML namespace, then switch to the CDATA section state."
FYI @whatwg/html-parser
I think the spec can probably stay as-is, but we should make sure there is a test and file bugs.
I'd agree the spec is right here.
~~Hmm it seems like CDATA sections are just not at all supported in Safari and Chrome.~~
data:text/html,<svg><!|CDATA[ ><p>FAIL
Edit: made a typo.
https://bugs.chromium.org/p/chromium/issues/detail?id=881983 https://bugs.webkit.org/show_bug.cgi?id=189431
https://github.com/html5lib/html5lib-tests/blob/515dc09aaa25d48a72f0d4c1a890507022b36e09/tree-construction/tests21.dat has tests for CDATA in svg/math, could add more tests to make sure it's supported in SVG title, but not in HTML span in SVG title.
Not a browser expert here, but chrome and opera show:
- #comment: [CDATA[x]]
Edge shows:
#text: x
I don't have mac access for safari
Blisk:
- #comment: [CDATA[x]]
Steve
From: Simon Pieters [email protected] Sent: Friday, September 7, 2018 8:04:48 PM To: whatwg/html Cc: Subscribed Subject: [whatwg/html] CDATA section in SVG title is handled differently in browsers (#4016)
Test case http://software.hixie.ch/utilities/js/live-dom-viewer/saved/6207
Edge and Firefox support the CDATA section. Safari and Chrome insert a comment.
I think Edge and Firefox match the spec,
https://html.spec.whatwg.org/multipage/parsing.html#markup-declaration-open-state
"If there is an adjusted current node and it is not an element in the HTML namespace, then switch to the CDATA section state."
FYI @whatwg/html-parserhttps://github.com/orgs/whatwg/teams/html-parser
I think the spec can probably stay as-is, but we should make sure there is a test and file bugs.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/whatwg/html/issues/4016, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ACKXS5Phk0Ie32fkS08SIf4hyz1nAtYfks5uYtFggaJpZM4WfZy-.
Browsers are inconsistent and reveal confusion between HTML integration points and elements in the HTML namespace.
The spec is clear on how to tokenize CDATA sections, to paraphrase:
- if the adjusted current node is an HTML element: handle as bogus comment
- otherwise: handle as CDATA
CDATA sections are handled during tokenization, before tree construction, and emit character tokens. Integration points affect how those tokens are handled during tree construction, but have no impact on CDATA section tokenization.
… If the adjusted current node is a MathML text integration point and the token is a character token … If the adjusted current node is an HTML integration point and the token is a start tag If the adjusted current node is an HTML integration point and the token is a character token
- [use the] current insertion mode in HTML content.
Otherwise
- [use the rules] for parsing tokens in foreign content.
The non-conforming browsers treat integration points as HTML elements when tokenizing CDATA sections.
<svg>␀(null becomes U+FFFD in foreign content)
<title><![CDATA[this should be text]]></title>
<desc><![CDATA[this should be text]]></desc>
<foreignObject><![CDATA[this should be text]]></foreignObject>
</svg>
<math>
<annotation-xml encoding="text/html"><![CDATA[this should be text]]></annotation-xml>
<mtext>␀(null character token is ignored in HTML insertion modes)<![CDATA[this should be text]]></mtext>
</math>
(The above example includes some null bytes, shown as ␀ for clarity.)
All tested browsers handle character tokens correctly: U+0000 NULL is ignored at integration points and becomes U+FFFD (�) in foreign content. The issue is limited to CDATA section tokenization.
In my testing of Chrome 150, Safari 26.5, Edge 150, and Firefox 152, only Firefox adheres to the spec.
| Chrome, Safari, Edge | Firefox |
└─HTML
├─HEAD
└─BODY
├─svg:svg
│ ├─#text �(null becomes U+FFFD in foreign content)
│ ├─svg:title
│ │ └─#comment [CDATA[this should be text]]
│ ├─svg:desc
│ │ └─#comment [CDATA[this should be text]]
│ └─svg:foreignObject
│ └─#comment [CDATA[this should be text]]
└─math:math
├─math:annotation-xml encoding="text/html"
│ └─#comment [CDATA[this should be text]]
└─math:mtext
├─#text (null character token is ignored in HTML insertion modes)
└─#comment [CDATA[this should be text]] |
└─HTML
├─HEAD
└─BODY
├─svg:svg
│ ├─#text �(null becomes U+FFFD in foreign content)
│ ├─svg:title
│ │ └─#text this should be text
│ ├─svg:desc
│ │ └─#text this should be text
│ └─svg:foreignObject
│ └─#text this should be text
└─math:math
├─math:annotation-xml encoding="text/html"
│ └─#text this should be text
└─math:mtext
└─#text (null character token is ignored in HTML insertion modes)this should be text
|
|---|
I think the tokenizer should treat integration points like HTML elements for the purposes of CDATA sections. This aligns with way the character tokens are handled by during tree construction. The markup declaration open state could be modified as follows:
"[CDATA["
Consume those characters. If there is an adjusted current node
and it is not an element in the HTML namespace,
+ it is not an HTML integration point,
+ and it is not a MathML text integration point,
then switch to the CDATA section state.
Otherwise, this is a cdata-in-html-content parse error.
Create a comment token whose data is "[CDATA[". Switch to the bogus comment state.
This describes the current behavior of Chrome, Safari, and Edge (but makes Firefox's behavior incorrect).
One more point of confusion: the explanation of the insertion mode includes this line (bold mine):
The insertion mode affects how tokens are processed and whether CDATA sections are supported.
This is inaccurate. CDATA section handling depends on the adjusted current node, not the insertion mode.
https://github.com/whatwg/html/issues/8558 is a related issue.
https://github.com/web-platform-tests/wpt/pull/61253 adds tests enforcing the behavior as currently specified.
https://github.com/web-platform-tests/wpt/pull/58991 introduced assertions that CDATA should be handled as bogus comments at integration points. That conflicts with the specification as it is now.
@hsivonen provided relevant insight about the current behavior https://github.com/whatwg/html/issues/8558#issuecomment-1331189029:
The reason why the CDATA stuff exists in the first place is to support copying and pasting SVG source from an XML-based .svg file inline into HTML. Since SVG title, etc., can contain a text node in SVG and text nodes in SVG can be serialized as CDATA sections, it makes sense to support CDATA sections in that context.
OTOH, for nested HTML, we prioritize HTMLness over an SVG ancestor.
web-platform-tests/wpt#58991 introduced assertions that CDATA should be handled as bogus comments at integration points. That conflicts with the specification as it is now.
I followed up in https://github.com/web-platform-tests/wpt/pull/61303 to fix those tests to match the specification. I've also updated WebKit to match the specification in https://github.com/WebKit/WebKit/pull/69464
Okay, so Gecko, WebKit, and tests match the standard. It seems @zcorpan already filed a Chromium bug.
I'm going to close this therefore.
(#4015 covers a tiny remaining case.)
It seems @zcorpan already filed a Chromium bug.
I'd appreciate if anyone has a link to the Chromium issue. I've only found what seem to be automated WPT reports: https://issues.chromium.org/issues/536943787, https://issues.chromium.org/issues/536952792.
https://issues.chromium.org/issues/41412594