Repository navigation
Add streaming HTML insertion methods - #12758
Conversation
7b105e3 to
e24c23b
Compare
df24ea2 to
e416732
Compare
e24c23b to
aae1d67
Compare
e416732 to
5106e9d
Compare
aae1d67 to
4ff3705
Compare
deb55bb to
e97c374
Compare
4ff3705 to
8551681
Compare
d5a18a7 to
b19b385
Compare
d87ab8d to
4880d38
Compare
c374a0b to
e8c7cba
Compare
2de75ec to
ce9f49b
Compare
See whatwg/html#12758 Note that tests that assert how this works with <template for> are still tentative as that PR is still not landed.
421aa3f to
043dcdf
Compare
2e1181e to
1d9e935
Compare
043dcdf to
286c499
Compare
1d9e935 to
4453c2a
Compare
286c499 to
146266b
Compare
4453c2a to
931141c
Compare
146266b to
6e5c251
Compare
AI feedbackReviewed at Blockers
Suggestions
Nits
|
Changed the behavior of mid-chunk reference child displacement to error the stream. Applied all nits and suggestions. |
AI feedbackReviewed at Blockers
Suggestions
Nits
|
I think we shouldn't skip it for fragment parsers; speculative fetches there should be allowed. |
AI reviewReviewed at 1f7ee1a, together with web-platform-tests/wpt#61748 at 8c22eae. This covers the whole spec diff and all 53 test files. Each finding below was checked against the spec text and the test source. The spec diff has no new blockers. The test PR hasn't been updated since before the last three spec commits, and four of its tests are broken. Blockers
Suggestions
Nits
|
Thanks! Addressed in here in the WPT. Blockers
|
AI review and fixesReviewed at 60f91db and again at b9dec97, together with web-platform-tests/wpt#61748 at 5b249c3 and 49e13af All findings from the previous review are addressed. I've pushed fixes for the new findings below to both branches, one commit each, and self-reviewed them: Blockers
Suggestions
Nits
|
AI review and fixesReviewed at b9dec97, together with web-platform-tests/wpt#61748 at 38f96d8 This was a fresh full pass: separate reviews of the spec's API layer, its tree construction changes, and the tests. I also ran all test files in Chrome Canary with experimental features, and in Firefox Nightly, which doesn't implement streaming, to catch tests that pass anyway. Line numbers below are at the reviewed commits. I pushed a fix for each finding, one commit each, except where noted, and self-reviewed them: Open questions
Blockers
Suggestions
Nits
|
I discussed this with @noamr. We think dropping in this case is best. I'll add a test. Edit: web-platform-tests/wpt@f50b348 |
AI review and fixesReviewed at 604b4c7, together with web-platform-tests/wpt#61748 at f50b348 All findings from my previous review are addressed, and f50b348 covers the decision to drop children of a replaced element after its table moves. I've pushed a fix for each new finding below to both branches, one commit each, and self-reviewed them: Open questions
Blockers
Suggestions
Nits
|
|
Pushed a rebase onto Normative fixes
Editorial & markup fixes
WPT updates (web-platform-tests/wpt#61748)
|
Review rounds 3–4: findings and fixesI reviewed again from the current heads and fixed what I found. A second, independent pass over the result found nothing further in the spec, and only the three small test items listed at the end. Those are fixed too, so this has converged. Note on one of my own earlier changes: web-platform-tests/wpt@9610b68516 from round 2 had a bug, fixed in web-platform-tests/wpt@42c8629746 (see below). Spec (#12758)
Tests (web-platform-tests/wpt#61748)
Verification
|
Introduce safe and unsafe streaming HTML parsing and insertion methods returning a `WritableStream`: * `Element` & `ShadowRoot`: `streamHTML()`, `streamHTMLUnsafe()`, `streamAppendHTML()`, `streamAppendHTMLUnsafe()`, `streamPrependHTML()`, and `streamPrependHTMLUnsafe()` * `NonDocumentTypeChildNode`: `streamBeforeHTML()`, `streamBeforeHTMLUnsafe()`, `streamAfterHTML()`, `streamAfterHTMLUnsafe()`, `streamReplaceWithHTML()`, and `streamReplaceWithHTMLUnsafe()` Key behaviors and parser updates: * **Stream operations**: `writeAlgorithm` and `closeAlgorithm` convert chunks to `DOMString`, append chunks (or an explicit `"EOF"` character on close) to the parser's input stream, and run the fragment parser. `closeAlgorithm` waits for any pending parser-blocking scripts via `fragment parser close promise`. `abortAlgorithm` aborts the fragment parser (deferring via `fragment parser abort requested` if currently running), marks open `script` elements on the stack as already started, and unblocks rendering on pending scripts. * **Displaced reference child**: If `referenceChild` is no longer a child of `target` when a node would be inserted before it or when another chunk is written, `invalid reference child steps` error the stream (and reject `fragment parser close promise` if closing) with a `"HierarchyRequestError"` `DOMException` and abort the fragment parser. Elements that would be inserted before a displaced `referenceChild` are created in the `appropriate template contents owner document` with a null custom element registry and dropped, and the adoption agency algorithm skips removing `lastNode`. * **Validation, Trusted Types & DOM mutations**: Target validation (throwing `"NotSupportedError"` for HTML/SVG `script` context elements, or `"HierarchyRequestError"` for `html` targets or invalid `parent for HTML streaming`), `HTMLTemplateElement` redirection to `template contents`, and Trusted Types / `SanitizerConfig` validation occur synchronously before returning the stream or mutating the DOM. * **Scripts, custom elements & fragment parsing**: - Extracts `initialize an HTML fragment parser`. When `runScripts` is true, `script target document` is `context`'s node document; otherwise it is the temporary inert document. - Parser-blocking, `defer`, `async`, and module scripts follow normal HTML parser pausing and execution order. - Custom element upgrades/callbacks when inserting into a connected target run before any subsequent `script` via a dedicated element queue; other reactions when streaming HTML use the backup element queue. - The speculative HTML parser is not started in the fragment case. Speculative fetches from the fragment parser are still allowed, except when sanitizing or for elements that will be dropped. *The end* (`stops parsing`) returns early for fragment parsers. - Elements whose intended parent is the fragment root `html` element are created directly in the target's (or fragment's) node document. - SVG `script` elements are only processed when the parser's `script target document` is not the parser's `Document` and the `script` element is connected (which also applies to document parsing when an ancestor of an SVG `script` was disconnected before `</script>`). - Elements removed or replaced with their children by the sanitizer are created in the `appropriate template contents owner document` with a null custom element registry and not inserted into the DOM. Note: Pre-existing adoption agency algorithm edge cases (#13093) are out of scope for this PR. Closes #2142. Tests: web-platform-tests/wpt#61748.
…hunk is parsed When the fragment parser runs from a task after a parser-blocking script, a streamed script that writes to the stream invokes writeAlgorithm synchronously; run a fragment parser returns early because the parser is already running, so the write promise resolves before the chunk is parsed. Qualify the intro's claim and extend the writeAlgorithm note.
With streaming, both the parser's Document and the target's document can have a pending parsing-blocking script; say which one is meant, matching the other references the PR added.
AI reviewRound 5 (after squashing both PRs at the author's request; the WPT PR was also rebased onto current Follow-ups / for the author
Findings and fixesHTML (whatwg/html, on top of the squashed commit)
WPT (web-platform-tests/wpt)
Verification
|
AI feedbackReviewed at 55eedc8. Lint and the full build (including the developer edition) pass, and the findings from my October 20 comments are addressed. The first two points are about the abort checks this PR adds to tree construction. None of the checks produces a wrong tree today, but several are unreachable or duplicate each other, and one has a reachable case that its placement doesn't account for. I also commented on web-platform-tests/wpt#61748 about a test that contradicts the spec (third point). Suggestions
Nits
|
Thanks, addressed in a62174f and web-platform-tests/wpt@b4147689cd:
|
Introduce safe and unsafe streaming HTML parsing and insertion methods returning a
WritableStream:Element&ShadowRoot:streamHTML(),streamHTMLUnsafe(),streamAppendHTML(),streamAppendHTMLUnsafe(),streamPrependHTML(), andstreamPrependHTMLUnsafe()NonDocumentTypeChildNode:streamBeforeHTML(),streamBeforeHTMLUnsafe(),streamAfterHTML(),streamAfterHTMLUnsafe(),streamReplaceWithHTML(), andstreamReplaceWithHTMLUnsafe()Key behaviors and parser updates:
writeAlgorithmandcloseAlgorithmconvert chunks toDOMString, append chunks (or an explicit"EOF"character on close) to the parser's input stream, and run the fragment parser.closeAlgorithmwaits for any pending parser-blocking scripts viafragment parser close promise.abortAlgorithmaborts the fragment parser (deferring viafragment parser abort requestedif currently running), marks openscriptelements on the stack as already started, and unblocks rendering on pending scripts.referenceChildis no longer a child oftargetwhen a node would be inserted before it or when another chunk is written,invalid reference child stepserror the stream (and rejectfragment parser close promiseif closing) with a"HierarchyRequestError"DOMExceptionand abort the fragment parser. Elements that would be inserted before a displacedreferenceChildare created in theappropriate template contents owner documentwith a null custom element registry and dropped, and the adoption agency algorithm skips removinglastNode."NotSupportedError"for HTML/SVGscriptcontext elements, or"HierarchyRequestError"forhtmltargets or invalidparent for HTML streaming),HTMLTemplateElementredirection totemplate contents, and Trusted Types /SanitizerConfigvalidation occur synchronously before returning the stream or mutating the DOM.initialize an HTML fragment parser. WhenrunScriptsis true,script target documentiscontext's node document; otherwise it is the temporary inert document.defer,async, and module scripts follow normal HTML parser pausing and execution order.scriptvia a dedicated element queue; other reactions when streaming HTML use the backup element queue.stops parsing) returns early for fragment parsers.htmlelement are created directly in the target's (or fragment's) node document.scriptelements are only processed when the parser'sscript target documentis not the parser'sDocumentand thescriptelement is connected (which also applies to document parsing when an ancestor of an SVGscriptwas disconnected before</script>).appropriate template contents owner documentwith a null custom element registry and not inserted into the DOM. Their descendants look up a null registry from them too. This also applies to the non-streaming methods that use the HTML fragment parsing algorithm with a sanitizer (setHTML(),setHTMLUnsafe(), etc.), where previously the whole fragment was parsed (and custom elements upgraded) before sanitization.Note: Pre-existing adoption agency algorithm edge cases (#13093) are out of scope for this PR.
Closes #2142
streamHTMLtests web-platform-tests/wpt#61748(See WHATWG Working Mode: Changes for more details.)
/dynamic-markup-insertion.html ( diff )
/index.html ( diff )
/infrastructure.html ( diff )
/parsing.html ( diff )
/references.html ( diff )
/scripting.html ( diff )
/xhtml.html ( diff )