Skip to content

Cover a few fragment parsing edge cases - #61042

Merged
nt1m merged 3 commits into
masterfrom
dsd-xhr-doc-open
Jul 13, 2026
Merged

nt1m merged 3 commits into
masterfrom
dsd-xhr-doc-open

Conversation

@noamr

@noamr noamr commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

See whatwg/html#12624

  • A few test cases of the "allow declarative shadow roots" opt-in (survives clones and document.open)
  • "after body" parsing mode when fragment-parsing
  • Custom element registry is taken from the template in template.innerHTML

See whatwg/html#12624
This clarifies a scenario where the allow-DSD flag has to be sticky
on the document rather than just a parser flag.
- Check insertion logic "after body" for template.innerHTML
- Check that template.innerHTML takes the custom element registry from the template
@noamr
noamr requested a review from a team as a code owner July 7, 2026 10:09
@wpt-pr-bot
wpt-pr-bot requested review from ChrisParis, jdm and snuggs July 7, 2026 10:09
@noamr noamr changed the title Add test-case for DSD when calling document.open() on an XHR response Cover a few fragment parsing edge cases Jul 7, 2026
annevk added a commit to whatwg/html that referenced this pull request Jul 9, 2026
This makes a few key changes to the fragment parsing workflow, to better match existing implementations and tests.

- Instead of inserting elements to a fake root element of a connected inert document and then appending them to a `DocumentFragment`, we let the parser insert the root's children straight into that `DocumentFragment`. This introduces an "root insertion target", which is a `DocumentFragment` to insert into if the parser wants to insert nodes into the root. 
- In addition to keeping "allow declarative shadow roots" directly on the document, it becomes a parser flag. This removes some confusion that makes some subtle cases use the wrong setting. It is still kept on the document so that it sticks for the "document opening steps" (`document.open()` of a document created elsewhere).
- The custom element registry is taken from the target of the fragment parse rather than from the context document. Together with the other changes, this essential for picking the correct registry, e.g., when calling `shadowRoot.innerHTML`. Note that this changes the specified behavior of `template.innerHTML` to have a null registry - this aligns with current implementations and can be changed in a follow-up together with implementation/test changes.
- The fragment parsing algorithms take in the target of the parse and infer the context element from that (i.e., it's either the target itself, or its fragment host if it's a `DocumentFragment`).
- The "adjusted insertion location" algorithm is separate from the "appropriate place from inserting a node", as the parser internals make some decisions based on the "intended parent" which should remain unchanged by this PR.
- The adjusted insertion location" is also an enabler that can be used later on for HTML streaming and sanitize-while-parsing, as it can be the context element instead of the intermediate fragment.
- The intermediate inert document is still around. It is needed to avoid creation-time side effects like image loading and custom element constructors from occurring before sanitization.

The above changes are a lot closer to how Chromium implements fragment parsing (and given the passing tests, likely WebKit and Gecko as well).

Existing tests are aligned with implementations already. Additional tests: web-platform-tests/wpt#61042.

Closes #11023 and closes #12661.

Co-authored-by: Anne van Kesteren <[email protected]>
@nt1m
nt1m merged commit 35be3b4 into master Jul 13, 2026
28 checks passed
@nt1m
nt1m deleted the dsd-xhr-doc-open branch July 13, 2026 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants