Skip to content

Commit b414768

Browse files
committed
Fix aborted <template for> patch expectation and test abort checks in the end and the adoption agency
1 parent 237a8db commit b414768

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

‎domparsing/stream-html-defer-async-script.html‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,39 @@
231231
assert_false(readystatechange_fired);
232232
}, `element.${method} should not modify Document readyState`);
233233
}
234+
235+
promise_test(async (t) => {
236+
window.events = [];
237+
const placeholder = document.getElementById("placeholder");
238+
const container = document.createElement("div");
239+
const ref = document.createElement("span");
240+
container.append(ref);
241+
placeholder.append(container);
242+
t.add_cleanup(() => container.remove());
243+
244+
const writer = container.streamPrependHTMLUnsafe({ runScripts: true }).getWriter();
245+
// The trailing "<" is only emitted, together with EOF, when the stream is closed.
246+
await writer.write(
247+
`<script defer src="data:text/javascript,window.events.push('defer');{let cache_buster=${Math.random()}}"></` + `script><`
248+
);
249+
ref.remove();
250+
// Inserting the "<" before the removed reference child errors the stream and aborts the
251+
// parser, but EOF is still processed.
252+
await promise_rejects_dom(t, "HierarchyRequestError", writer.close());
253+
assert_equals(container.textContent, "", "the trailing '<' is not inserted");
254+
255+
// Control: a defer script streamed and closed now runs within a few tasks, so the defer script
256+
// of the aborted parser would have run by then too.
257+
window.control_ran = Promise.withResolvers();
258+
t.add_cleanup(() => { delete window.control_ran; });
259+
const control = document.createElement("div");
260+
container.append(control);
261+
const controlWriter = control.streamAppendHTMLUnsafe({ runScripts: true }).getWriter();
262+
await controlWriter.write(
263+
`<script defer src="data:text/javascript,window.events.push('control-defer');window.control_ran.resolve();{let cache_buster=${Math.random()}}"></` + `script>`
264+
);
265+
await controlWriter.close();
266+
await failOnTimeout(window.control_ran.promise);
267+
assert_array_equals(window.events, ["control-defer"]);
268+
}, "element.streamPrependHTMLUnsafe: defer scripts do not run if the reference child is removed before a trailing '<' is flushed by close()");
234269
</script>

‎domparsing/stream-html-reentrancy.html‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,15 @@
401401
.map((n) => `${n.target}:${n.data}`);
402402
assert_array_equals(markers, ['start:name="m"', "end:"],
403403
"</template> must not remove patch markers after abort in table text");
404-
assert_equals(target.querySelector("#patched").textContent, "Old");
404+
// The old content was removed when the <template for> start tag was processed. The
405+
// content streamed before the abort stays; the foster-parented text after it doesn't.
406+
assert_array_equals(
407+
Array.from(target.querySelector("#patched").childNodes, (n) => n.nodeName),
408+
["start", "B", "A", "TABLE", "end"]);
409+
assert_equals(target.querySelector("#patched").textContent, "1");
405410
await failOnTimeout(abortPromise);
406411
await promise_rejects_exactly(t, "aborted-in-table-text", writer.closed);
407-
}, `${method}: abort() from a foster-parented custom element reaction in table text prevents reprocessing </template>`);
412+
}, `${method}: abort() from a foster-parented custom element reaction in table text keeps patch markers at </template>`);
408413

409414
promise_test(async (t) => {
410415
const target = document.getElementById("target");

‎domparsing/stream-html-sanitizer.html‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,4 +495,27 @@
495495
}, `element.${method}: a <form> in removeElements is removed along with its descendants and does not become the form owner`);
496496
}
497497
}
498+
499+
for (const method of ["streamHTML", "streamHTMLUnsafe"]) {
500+
promise_test(async (t) => {
501+
const doc = document.implementation.createHTMLDocument("");
502+
const placeholder = document.createElement("div");
503+
const writer = placeholder[method]({
504+
sanitizer: { replaceWithChildrenElements: ["section"] },
505+
}).getWriter();
506+
// <b> is foster parented before the <table>, and <section>'s children are redirected into
507+
// <b>.
508+
await writer.write("<table><b><section>");
509+
const table = placeholder.querySelector("table");
510+
assert_equals(placeholder.querySelector("b")?.nextSibling, table, "sanity check: <b> is foster parented");
511+
doc.replaceChild(table, doc.documentElement);
512+
// The adoption agency algorithm foster parents the clone of <b> before the <table>, which is
513+
// now the document element of doc. It must not be inserted there.
514+
await writer.write("</b>");
515+
await writer.close();
516+
assert_array_equals(Array.from(doc.childNodes, (n) => n.nodeName), ["html", "TABLE"]);
517+
assert_equals(doc.documentElement, table);
518+
assert_equals(table.childNodes.length, 0);
519+
}, `element.${method}: the adoption agency algorithm does not insert into a document that has a document element`);
520+
}
498521
</script>

0 commit comments

Comments
 (0)