Skip to content

Add positional HTML insertion methods - #12753

Merged
zcorpan merged 1 commit into
mainfrom
noamr/positional-html
Oct 12, 2026
Merged

zcorpan merged 1 commit into
mainfrom
noamr/positional-html

Conversation

@noamr

@noamr noamr commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Add appendHTML(), prependHTML(), beforeHTML(), afterHTML(), and replaceWithHTML(), alongside their *Unsafe() variants.

NonDocumentTypeChildNode methods fail silently when the child has no parent, like their equivalent DOM methods, and fail with a "HierarchyRequestError" DOMException for an invalid parent (Document, hostless DocumentFragment, template element, or html element). appendHTML() and prependHTML() (and their *Unsafe() variants) also throw a "HierarchyRequestError" DOMException when called on an html element, as parsing a fragment with an html context element always produces both a head and a body element.

Note that the new NonDocumentTypeChildNode methods are also exposed on CharacterData.

Renamed "set and filter HTML" to "filter and set HTML" to be more consistent with the order of operations and with the newly added helper methods.

Closes #10122


/dynamic-markup-insertion.html ( diff )
/index.html ( diff )
/infrastructure.html ( diff )
/parsing.html ( diff )

@noamr noamr closed this Aug 4, 2026
@noamr noamr reopened this Aug 4, 2026
@noamr noamr mentioned this pull request Aug 5, 2026
5 of 6 tasks
@noamr
noamr force-pushed the noamr/positional-html branch 2 times, most recently from ee46eb1 to b48a945 Compare August 6, 2026 10:40
Comment thread source Resolved
Comment thread source Resolved
@noamr
noamr force-pushed the noamr/positional-html branch from f9a87e5 to d256495 Compare August 12, 2026 13:25
@zcorpan zcorpan closed this Aug 13, 2026
@zcorpan zcorpan reopened this Aug 13, 2026

@zcorpan zcorpan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM % typo

Comment thread source Resolved
@noamr

noamr commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Fixes from Opus review:

Blockers

  • Trusted Types sink names: Updated sink names to use the declaring interface ("ChildNode beforeHTMLUnsafe", "ChildNode afterHTMLUnsafe", and "ChildNode replaceWithHTMLUnsafe" instead of "Node ...").

Suggestions

  • DOM divergence note: Added an explicit non-normative <p class="note"> under parent for HTML insertion explaining the intentional divergence from DOM before()/after()/replaceWith() regarding template and Document parents.
  • domintro error descriptions: Updated the domintro entries for all six positional methods to explicitly mention throwing HierarchyRequestError if the parent is a Document or template element.
  • Helper invocation phrasing: Standardized algorithm step invocations across all call sites to use "the parent for HTML insertion given this" and "the HTML insertion target given target".
  • Method alignment: Aligned the step structure of replaceWithHTML and replaceWithHTMLUnsafe (binds parent, returns early if null, then invokes filter and replace with HTML).
  • [Unscopable]: Omission of [Unscopable] for the new ChildNode methods follows standard Web IDL practice (retained only for legacy identifiers for compat).

Nits

  • Helper parameter signatures: Standardized to a string <var>html</var> and data-x="tt-trustedparseroptions" across filter and set HTML, filter and pre-insert HTML, and filter and replace with HTML.
  • Formatting & Whitespace: Fixed indentation (3 spaces) and line wrapping in parent for HTML insertion, closed missing </p> tags on method steps are:</p>, removed double spacing in algorithm signatures, and removed the stray extra blank line before the Document.parseHTML domintro.

@noamr
noamr force-pushed the noamr/positional-html branch 3 times, most recently from dec53b2 to 73e73db Compare August 24, 2026 12:41
@noamr
noamr force-pushed the noamr/positional-html branch from 73e73db to 0c5b590 Compare August 25, 2026 08:18
@noamr
noamr force-pushed the noamr/positional-html branch from 0c5b590 to b21b5d3 Compare August 25, 2026 14:38
@noamr
noamr force-pushed the noamr/positional-html branch 2 times, most recently from d7c9a5e to f23f3cf Compare August 26, 2026 15:13
@noamr
noamr force-pushed the noamr/positional-html branch from f23f3cf to ee138bf Compare August 26, 2026 19:48
beckysiegel pushed a commit to chromium/chromium that referenced this pull request Oct 2, 2026
…usted Types sinks

This aligns positional HTML insertion and streaming methods with
whatwg/html#12753:

Bug: 491743369
Change-Id: Ie0b6f8175b7de75b9842bf90cccd168dbced1275
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8494796
Reviewed-by: Mason Freed <[email protected]>
Commit-Queue: Noam Rosenthal <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1710333}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Oct 3, 2026
…usted Types sinks

This aligns positional HTML insertion and streaming methods with
whatwg/html#12753:

Bug: 491743369
Change-Id: Ie0b6f8175b7de75b9842bf90cccd168dbced1275
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8494796
Reviewed-by: Mason Freed <[email protected]>
Commit-Queue: Noam Rosenthal <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1710333}
Base automatically changed from noamr/cpo to main October 7, 2026 10:52
@noamr
noamr force-pushed the noamr/positional-html branch 2 times, most recently from 031a042 to 3cae80f Compare October 7, 2026 14:57
@zcorpan

zcorpan commented Oct 7, 2026

Copy link
Copy Markdown
Member
AI review

Reviewed at #12753 031a042 and web-platform-tests/wpt#60555 0d5e8d9.

Suggestions

  • source:126217: Renaming the heading changes its auto-generated anchor from #html-parsing-methods to #html-parsing-and-insertion-methods. That breaks existing links, including the rel=help in the new positional-methods.html and positional-html-into-script-safe.html. Consider <h4 id="html-parsing-methods">.
  • source:126357-126521: The long Trusted Types paragraph is copied into all six new *HTMLUnsafe domintro entries. With the existing two, that's eight copies to keep in sync. Consider having several <dt>s share one <dd>, or sending readers to the setHTMLUnsafe() entry for the TT behavior.
  • Commit message: It talks about "ChildNode methods" that are "also exposed on CharacterData and DocumentType". The IDL actually uses NonDocumentTypeChildNode, so the methods are not on DocumentType, and positional-methods.html checks exactly that. Consider fixing the wording.
  • wpt: No test checks that positional methods parse using the parent (or the host, for shadow roots and template contents) as context. Only the Safe script no-op covers this, indirectly. Consider tests like a template.content child calling .afterHTMLUnsafe("<td>x</td>") and checking the td is kept (context is template), and the same for a child of <tr>.

Nits

  • source:142916: The line is 105 characters; rewrap it.
  • source:127111-127126: Two notes in a row cover the same divergence from the legacy methods; consider merging them.
  • source:126466 (and the same text in the other two positional *HTMLUnsafe entries): "Otherwise, throws a HierarchyRequestError" follows the TT sentence, so it reads as if it only applies when TT isn't enforced. Consider "Then throws …", or putting it first like the Safe entries do.
  • wpt PR description: It links HTML insertion positional methods #12528, which is closed; the spec PR is Add positional HTML insertion methods #12753.
  • wpt positional-methods.html:946-964: The "on disconnected Element/Comment/…" tests are really about nodes with no parent. A disconnected node that has a parent wouldn't no-op. Consider "without a parent".
  • wpt stream-html-with-trusted-types-createParserOptions-order.html: When the stream test was moved here, it lost the old recorded_type/recorded_sink assertions.
  • wpt positional-methods-with-parser-options.html:7 and positional-methods-with-trusted-types.html:7: These files are no longer tentative but still have rel=help pointing to WICG/declarative-partial-updates; point them at the HTML spec.

@noamr
noamr force-pushed the noamr/positional-html branch from 3cae80f to 9ad27b2 Compare October 7, 2026 17:32
@noamr

noamr commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

AI review

All addressed (here and in the WPT PR, including commit messages).

@noamr
noamr force-pushed the noamr/positional-html branch from 9ad27b2 to 7944b13 Compare October 7, 2026 20:41
lando-worker Bot pushed a commit to mozilla-firefox/firefox that referenced this pull request Oct 8, 2026
…entTypeChildNode and align Trusted Types sinks, a=testonly

Automatic update from web-platform-tests
Move positional HTML methods to NonDocumentTypeChildNode and align Trusted Types sinks

This aligns positional HTML insertion and streaming methods with
whatwg/html#12753:

Bug: 491743369
Change-Id: Ie0b6f8175b7de75b9842bf90cccd168dbced1275
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8494796
Reviewed-by: Mason Freed <[email protected]>
Commit-Queue: Noam Rosenthal <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1710333}

--

wpt-commits: c3a310677da334e3d08e4704a4c7788ff8f5fdb5
wpt-pr: 63224
@annevk

annevk commented Oct 9, 2026

Copy link
Copy Markdown
Member
AI feedback

Reviewed at 7944b13. Lint and the full build pass. Checked and fine: the IDL and method steps line up. Nothing can run author script between computing the parent or reference child and inserting, because Trusted Types runs first, so the pre-insert and replace calls can't throw.

Blockers

  • html element as parent: when the parent is an html element, it becomes the fragment parser's context. This happens for document.body.beforeHTML(), document.head.afterHTML(), and document.body.replaceWithHTML(). It also happens for appendHTML() and prependHTML() on document.documentElement, where the html element is the target. Reset the insertion mode appropriately then picks "before head", so the fragment always comes out as a head plus a body. For example, document.body.beforeHTML("<p>x</p>") inserts <head></head><body><p>x</p></body> before the existing body. The document ends up with two heads and two bodies, and document.body returns the new one.

    insertAdjacentHTML() and createContextualFragment() avoid this by parsing with a new body element as context. That doesn't carry over well here. With a body context, document.body.replaceWithHTML("<body>…</body>") drops the body start tag and leaves the document without a body.

    Throwing a "HierarchyRequestError" DOMException, as for a Document parent, may be the simplest fix. setHTML() on the html element is fine as is, since there a head plus a body is what you want. So the check would go in filter and pre-insert HTML and filter and replace with HTML. Whichever way this goes, the note under parent for HTML insertion, the domintro, and the tests need to cover it. Chromium's implementation (FragmentParserConfig::ForContainer) currently matches the spec here, so it would need to change too.

  • Safe-mode script check: this moved into the exported fragment parsing algorithm steps, so that algorithm can return null again. The PR then adds Asserts to outerHTML, insertAdjacentHTML(), and createContextualFragment() to cope. Integrate fragment parser options with trusted types #12583 moved this check out of the exported algorithm before it landed, so on main it lives in set and filter HTML.

    Consider a non-exported wrapper instead (e.g., "parse HTML for insertion"). It would do the check and otherwise return the result of fragment parsing algorithm steps, and the three filter and … HTML helpers would call it. The exported algorithm then always returns a fragment, and the three Asserts and the "or null" in its intro go away.

Nits

  • ShadowRoot's setHTMLUnsafe() entry says "in the same way as for element's setHTMLUnsafe()". The eight new entries, e.g. appendHTMLUnsafe(), say "for setHTMLUnsafe()". Pick one.
  • The six positional domintro entries, e.g. beforeHTML(), link "template contents" but not "shadow root". Consider <span>shadow root</span>.
  • Fragment scripting mode: "(and the other HTML insertion methods)" includes the Safe methods, which never run scripts. Consider "the other Unsafe HTML insertion methods".
  • fragment parsing algorithm steps intro: "are as follows. They return either a DocumentFragment or null:" doesn't match the "…are the following steps. They return …" pattern used elsewhere. This goes away with the wrapper above.
  • (optional) Renaming set and filter HTML drops the #set-and-filter-html anchor, which mozilla/explainers and a couple of DOM implementations link to. <dfn id="set-and-filter-html"> keeps those links working, as done elsewhere.
  • (optional) The mXSS paragraph says "setHTML() uses the current element". beforeHTML(), afterHTML(), and replaceWithHTML() use the parent (or its host) instead.
  • The PR title says "parsing", but the commit says "insertion". The squash commit takes the PR title.
  • Commit message: "a direct child of a template element is considered an invalid parent" reads as if the child is the invalid parent, but it's the template element. Also, "Renamed" → "Rename", to match "Add" and "Use".
  • (optional) Checklist: "Chromium: Implemented" has no link, and the MDN box is ticked without an issue.

@noamr
noamr force-pushed the noamr/positional-html branch from 7944b13 to cdd3145 Compare October 9, 2026 15:20
@noamr

noamr commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

AI feedback

Throwing an exception when using those methods with an html parent. Also addressed the rest of the nits.

@noamr noamr changed the title Add positional HTML parsing methods Add positional HTML insertion methods Oct 9, 2026
@noamr
noamr force-pushed the noamr/positional-html branch 2 times, most recently from 2af3135 to b450495 Compare October 12, 2026 09:16
Add `appendHTML`, `prependHTML`, `beforeHTML`, `afterHTML`, and `replaceWithHTML`, alongside their `*Unsafe` variants.

Add helper algorithms for HTML insertion (filter and pre-insert/replace with/set HTML, parse HTML for insertion, and the target and parent for HTML insertion), and call those from the appropriate web-exposed methods.

`NonDocumentTypeChildNode` methods fail silently when the child has no parent, like their equivalent DOM methods, and fail with a `HierarchyRequestError` for an invalid parent. Note that a `template` element is considered an invalid parent, as setting HTML on `template` elements affects their template contents rather than their children, and an `html` element is disallowed as the parent/target for pre-inserting or replacing HTML because parsing with an `html` context element always produces both a `head` and a `body` element.

Note that the new `NonDocumentTypeChildNode` methods are also exposed on `CharacterData`.

Rename "set and filter HTML" to "filter and set HTML" to be more consistent with the order of operations and with the newly added helper methods.
@noamr
noamr force-pushed the noamr/positional-html branch from b450495 to 49e5663 Compare October 12, 2026 09:37
@zcorpan
zcorpan merged commit e88f8aa into main Oct 12, 2026
2 checks passed
@zcorpan
zcorpan deleted the noamr/positional-html branch October 12, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Improve API of insertAdjacent*() methods

4 participants