Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!doctype html>
<meta charset="utf-8">
<title>Fragment parser context handling for streamHTML variants</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<div id="container"></div>
<script>
const methods = [
"streamHTML", "streamAppendHTML", "streamPrependHTML",
"streamHTMLUnsafe", "streamAppendHTMLUnsafe", "streamPrependHTMLUnsafe"
];
const positionalMethods = [
"streamBeforeHTML", "streamAfterHTML", "streamReplaceWithHTML",
"streamBeforeHTMLUnsafe", "streamAfterHTMLUnsafe", "streamReplaceWithHTMLUnsafe"
];
// 1. Table family context elements
for (const method of methods) {
promise_test(async (t) => {
const container = document.getElementById("container");
t.add_cleanup(() => container.replaceChildren());
const table = document.createElement("table");
container.appendChild(table);
const writer = table[method]().getWriter();
await writer.write("<tr><td>Row 1</td></tr><tr><td>Row 2</td></tr>");
await writer.close();
const tbody = table.querySelector("tbody");
assert_not_equals(tbody, null, "<table> fragment context automatically creates <tbody> for <tr>");
assert_equals(tbody.querySelectorAll("tr").length, 2, "Both rows present in tbody");
assert_equals(table.rows.length, 2);
}, `${method} on <table> creates <tbody> and rows`);
promise_test(async (t) => {
const container = document.getElementById("container");
t.add_cleanup(() => container.replaceChildren());
const table = document.createElement("table");
const tbody = document.createElement("tbody");
table.appendChild(tbody);
container.appendChild(table);
const writer = tbody[method]().getWriter();
await writer.write("<tr><td>Cell A</td><td>Cell B</td></tr>");
await writer.close();
assert_equals(tbody.children.length, 1, "tbody contains direct <tr> child");
assert_equals(tbody.firstElementChild.tagName, "TR");
assert_equals(tbody.firstElementChild.children.length, 2);
}, `${method} on <tbody> parses <tr> children directly`);
promise_test(async (t) => {
const container = document.getElementById("container");
t.add_cleanup(() => container.replaceChildren());
const table = document.createElement("table");
const tbody = document.createElement("tbody");
const tr = document.createElement("tr");
tbody.appendChild(tr);
table.appendChild(tbody);
container.appendChild(table);
const writer = tr[method]().getWriter();
await writer.write("<td>Data 1</td><th>Header 2</th>");
await writer.close();
assert_equals(tr.children.length, 2, "tr contains <td> and <th> directly");
assert_equals(tr.children[0].tagName, "TD");
assert_equals(tr.children[1].tagName, "TH");
}, `${method} on <tr> parses <td> and <th> children directly`);
}
// 2. Select family context elements
for (const method of methods) {
const isUnsafe = method.includes("Unsafe");
const options = isUnsafe ? {} : { sanitizer: { elements: ["option", "optgroup", "hr"] } };
promise_test(async (t) => {
const container = document.getElementById("container");
t.add_cleanup(() => container.replaceChildren());
const select = document.createElement("select");
container.appendChild(select);
const writer = select[method](options).getWriter();
await writer.write('<option value="1">One</option><optgroup label="G"><option value="2">Two</option></optgroup>');
await writer.close();
assert_equals(select.options.length, 2, "select contains two options");
assert_not_equals(select.querySelector("optgroup"), null, "select contains optgroup");
assert_equals(select.querySelector("optgroup").label, "G");
}, `${method} on <select> parses <option> and <optgroup>`);
promise_test(async (t) => {
const container = document.getElementById("container");
t.add_cleanup(() => container.replaceChildren());
const option = document.createElement("option");
container.appendChild(option);
const writer = option[method]().getWriter();
await writer.write("Option Text &amp; &lt;Label&gt;");
await writer.close();
assert_equals(option.textContent, "Option Text & <Label>", "option parses text and expands character references");
}, `${method} on <option> parses text and character references`);
}
// 3. SVG foreign content context
for (const method of methods) {
promise_test(async (t) => {
const container = document.getElementById("container");
t.add_cleanup(() => container.replaceChildren());
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
container.appendChild(svg);
const writer = svg[method]().getWriter();
await writer.write('<rect x="10" y="20" width="100" height="50"/><circle cx="30" cy="30" r="20"/><foreignObject width="100" height="100"><div>HTML Content</div></foreignObject>');
await writer.close();
const rect = svg.querySelector("rect");
assert_not_equals(rect, null, "<rect> created in <svg>");
assert_equals(rect.namespaceURI, "http://www.w3.org/2000/svg", "<rect> is in SVG namespace");
const circle = svg.querySelector("circle");
assert_not_equals(circle, null, "<circle> created in <svg>");
assert_equals(circle.namespaceURI, "http://www.w3.org/2000/svg", "<circle> is in SVG namespace");
const foreignObject = svg.querySelector("foreignObject");
assert_not_equals(foreignObject, null, "<foreignObject> created in <svg>");
assert_equals(foreignObject.namespaceURI, "http://www.w3.org/2000/svg", "<foreignObject> is in SVG namespace");
const div = foreignObject.querySelector("div");
assert_not_equals(div, null, "<div> created inside <foreignObject>");
assert_equals(div.namespaceURI, "http://www.w3.org/1999/xhtml", "<div> inside foreignObject is in HTML namespace");
}, `${method} on <svg> parses foreign content with correct SVG/HTML namespaces`);
}
// 4. MathML foreign content context
for (const method of methods) {
promise_test(async (t) => {
const container = document.getElementById("container");
t.add_cleanup(() => container.replaceChildren());
const math = document.createElementNS("http://www.w3.org/1998/Math/MathML", "math");
container.appendChild(math);
const writer = math[method]().getWriter();
await writer.write("<mi>x</mi><mo>+</mo><mn>1</mn>");
await writer.close();
const mi = math.querySelector("mi");
assert_not_equals(mi, null, "<mi> created in <math>");
assert_equals(mi.namespaceURI, "http://www.w3.org/1998/Math/MathML", "<mi> is in MathML namespace");
const mo = math.querySelector("mo");
assert_not_equals(mo, null, "<mo> created in <math>");
assert_equals(mo.namespaceURI, "http://www.w3.org/1998/Math/MathML", "<mo> is in MathML namespace");
}, `${method} on <math> parses MathML content with MathML namespace`);
}
// 5. Positional methods on child nodes in special contexts
for (const method of positionalMethods) {
promise_test(async (t) => {
const container = document.getElementById("container");
t.add_cleanup(() => container.replaceChildren());
const table = document.createElement("table");
const tbody = document.createElement("tbody");
const tr = document.createElement("tr");
const initialTd = document.createElement("td");
initialTd.textContent = "Initial";
tr.appendChild(initialTd);
tbody.appendChild(tr);
table.appendChild(tbody);
container.appendChild(table);
const writer = initialTd[method]().getWriter();
await writer.write("<td>NewCell</td>");
await writer.close();
const expectedTrHTML = method.startsWith("streamBefore")
? "<td>NewCell</td><td>Initial</td>"
: method.startsWith("streamAfter")
? "<td>Initial</td><td>NewCell</td>"
: "<td>NewCell</td>";
assert_equals(tr.innerHTML, expectedTrHTML, "Positional update in <tr> succeeds");
}, `${method} targeting child in <tr> context`);
promise_test(async (t) => {
const container = document.getElementById("container");
t.add_cleanup(() => container.replaceChildren());
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
const initialRect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
svg.appendChild(initialRect);
container.appendChild(svg);
const writer = initialRect[method]().getWriter();
await writer.write('<circle cx="10" cy="10" r="5"/>');
await writer.close();
const circle = svg.querySelector("circle");
assert_not_equals(circle, null, "<circle> inserted by positional method in <svg>");
assert_equals(circle.namespaceURI, "http://www.w3.org/2000/svg", "Inserted element has SVG namespace");
}, `${method} targeting child in <svg> context`);
}
// 6. C1: Chunk boundary is not EOF (elements and attributes split across chunks)
for (const method of ["streamHTML", "streamHTMLUnsafe", "streamAppendHTML", "streamAppendHTMLUnsafe"]) {
promise_test(async (t) => {
const div = document.createElement("div");
const writer = div[method]().getWriter();
await writer.write("<p>a");
await writer.write("b</p>");
await writer.write("<a hr");
await writer.write('ef="https://example.com/">link</a>');
await writer.close();
const ps = div.querySelectorAll("p");
assert_equals(ps.length, 1, "Only one <p> element should be created across chunks");
assert_equals(ps[0].textContent, "ab");
assert_equals(ps[0].childNodes.length, 1, "Text inside <p> across chunks is a single Text node");
const a = div.querySelector("a");
assert_not_equals(a, null);
assert_equals(a.getAttribute("href"), "https://example.com/");
assert_equals(a.textContent, "link");
}, `${method}: chunk boundary is not treated as EOF for elements or attributes`);
}
// 7. C2: Foster parenting across chunks
for (const method of ["streamHTML", "streamHTMLUnsafe", "streamAppendHTML", "streamAppendHTMLUnsafe"]) {
promise_test(async (t) => {
const div = document.createElement("div");
const writer = div[method]().getWriter();
await writer.write("<table><tr>");
await writer.write("x<td>y");
await writer.close();
assert_equals(div.innerHTML, "x<table><tbody><tr><td>y</td></tr></tbody></table>");
}, `${method}: foster parenting across chunk boundaries`);
}
// 8. C3: Text merges into an existing adjacent Text node
for (const method of ["streamAppendHTML", "streamAppendHTMLUnsafe"]) {
promise_test(async (t) => {
const div = document.createElement("div");
const existingText = document.createTextNode("x");
div.appendChild(existingText);
const writer = div[method]().getWriter();
await writer.write("y");
await writer.close();
assert_equals(div.childNodes.length, 1, "Streamed text merges into existing Text node");
assert_equals(div.firstChild, existingText);
assert_equals(existingText.data, "xy");
}, `${method}: streamed text merges into existing trailing Text node`);
}
for (const method of ["streamAfterHTML", "streamAfterHTMLUnsafe"]) {
promise_test(async (t) => {
const div = document.createElement("div");
const existingText = document.createTextNode("x");
div.appendChild(existingText);
const writer = existingText[method]().getWriter();
await writer.write("y");
await writer.close();
assert_equals(div.childNodes.length, 1, "Streamed text after Text node merges into it");
assert_equals(div.firstChild, existingText);
assert_equals(existingText.data, "xy");
}, `${method}: streamed text merges into preceding Text node`);
}
// 9. C5: Quirks-mode context document vs no-quirks context document
promise_test(async (t) => {
const quirksIframe = document.createElement("iframe");
t.add_cleanup(() => quirksIframe.remove());
document.body.appendChild(quirksIframe);
quirksIframe.contentDocument.open();
quirksIframe.contentDocument.write("<html><body><div></div></body></html>");
quirksIframe.contentDocument.close();
assert_equals(quirksIframe.contentDocument.compatMode, "BackCompat");
const quirksDiv = quirksIframe.contentDocument.querySelector("div");
const quirksWriter = quirksDiv.streamHTMLUnsafe().getWriter();
await quirksWriter.write("<p><table><tr><td>cell</td></tr></table>");
await quirksWriter.close();
assert_not_equals(
quirksDiv.querySelector("p > table"),
null,
"In quirks mode, <table> does not close an open <p>",
);
const noQuirksIframe = document.createElement("iframe");
t.add_cleanup(() => noQuirksIframe.remove());
noQuirksIframe.srcdoc = "<!doctype html><html><body><div></div></body></html>";
await new Promise(resolve => {
noQuirksIframe.addEventListener("load", resolve, { once: true });
document.body.appendChild(noQuirksIframe);
});
assert_equals(noQuirksIframe.contentDocument.compatMode, "CSS1Compat");
const noQuirksDiv = noQuirksIframe.contentDocument.querySelector("div");
const noQuirksWriter = noQuirksDiv.streamHTMLUnsafe().getWriter();
await noQuirksWriter.write("<p><table><tr><td>cell</td></tr></table>");
await noQuirksWriter.close();
assert_equals(
noQuirksDiv.querySelector("p > table"),
null,
"In no-quirks mode, <table> closes an open <p>",
);
assert_not_equals(noQuirksDiv.querySelector("p + table"), null);
}, "streamHTMLUnsafe respects quirks mode vs no-quirks mode of context document");
// 10. C6: Open elements are popped at close()
for (const method of ["streamHTML", "streamHTMLUnsafe", "streamAppendHTML", "streamAppendHTMLUnsafe"]) {
promise_test(async (t) => {
const div = document.createElement("div");
const options = method.includes("Unsafe") ? {} : { sanitizer: { elements: ["textarea"] } };
const writer = div[method](options).getWriter();
await writer.write("<textarea>abc");
await writer.close();
const textarea = div.querySelector("textarea");
assert_not_equals(textarea, null);
assert_equals(textarea.defaultValue, "abc");
assert_equals(textarea.value, "abc");
}, `${method}: unclosed <textarea> has expected defaultValue after close()`);
promise_test(async (t) => {
const container = document.getElementById("container");
t.add_cleanup(() => container.replaceChildren());
const div = container.appendChild(document.createElement("div"));
const options = method.includes("Unsafe") ? {} : { sanitizer: { elements: ["style"] } };
const writer = div[method](options).getWriter();
await writer.write("<style>#container { --popped: 1; }");
// A style element's style sheet is only updated once it's popped off the stack of open elements.
const style = div.querySelector("style");
assert_not_equals(style, null);
assert_equals(style.sheet, null, "the style element is still open");
await writer.close();
assert_not_equals(style.sheet, null, "the style element is popped on close()");
}, `${method}: unclosed <style> is popped on close()`);
}
// 11. The HTML parser is used regardless of the type of document.
for (const method of methods) {
promise_test(async (t) => {
const doc = new DOMParser().parseFromString(
'<html xmlns="http://www.w3.org/1999/xhtml"><body><div id="target"></div></body></html>',
"application/xhtml+xml");
const target = doc.getElementById("target");
const writer = target[method]().getWriter();
await writer.write("<P>a<p>b");
await writer.close();
const paragraphs = Array.from(target.children);
assert_equals(paragraphs.length, 2, "the second <p> implicitly closes the first");
for (const p of paragraphs) {
assert_equals(p.localName, "p");
assert_equals(p.namespaceURI, "http://www.w3.org/1999/xhtml");
assert_equals(p.ownerDocument, doc);
}
assert_equals(target.textContent, "ab");
}, `${method} into an element in an XML document uses the HTML parser`);
}
</script>
</body>