Skip to content

Adoption agency: invoke custom element reactions once the algorithm finishes - #13215

Open
noamr wants to merge 1 commit into
noamr/aaa-fixesfrom
noamr/aaa-reactions
Open

noamr wants to merge 1 commit into
noamr/aaa-fixesfrom
noamr/aaa-reactions

Conversation

@noamr

@noamr noamr commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

The adoption agency algorithm removes and inserts nodes directly without an element queue on the custom element reactions stack. During normal parsing (when the JavaScript execution context stack is empty), disconnectedCallback and connectedCallback reactions from reparenting the furthest block and its children are enqueued on the backup element queue and only run at the next microtask checkpoint (such as at a following </script> end tag, after any intervening tokens—or the new element for an <a> or <nobr> start tag—have already been parsed). When the parser is driven from script (e.g., via document.write()), document.write()'s [CEReactions] extended attribute makes its element queue the current element queue, so those reactions are enqueued there and only run when document.write() returns, after a following <script> has already executed.

Push a new element queue before running the inner adoption agency algorithm and pop and invoke it once the algorithm finishes, matching how "insert an element at the adjusted insertion location" handles reactions.

Wrapping the algorithm as a whole rather than each DOM operation inside it ensures that:

  • No custom element callback reactions caused by the algorithm's tree mutations run while the tree is in an intermediate state of the algorithm (for example, while a node has been moved into a clone created by the inner loop that has not yet been inserted into the common ancestor).
  • Each custom element's callbacks observe the final tree produced by the algorithm, grouped per element in the order the elements were first enqueued.

Tests: web-platform-tests/wpt#64911.

Fixes #13072.


(See WHATWG Working Mode: Changes for more details.)


/parsing.html ( diff )

@noamr noamr changed the title Adoption agency: invoke custom element reactions after each reparenting step Invoke custom element reactions after each adoption agency reparenting step Nov 8, 2026
noamr added a commit to web-platform-tests/wpt that referenced this pull request Nov 8, 2026
Rewrite the expectations so that connectedCallback and disconnectedCallback
run after the adoption agency algorithm has finished rather than after each
reparenting step. Every callback therefore observes the final tree: the
reparented element is reported as connected, and reactions queued by the
intermediate removals and insertions are invoked back-to-back in order.

See whatwg/html#13215.
@noamr noamr changed the title Invoke custom element reactions after each adoption agency reparenting step Adoption agency: invoke custom element reactions once the algorithm finishes Nov 8, 2026
@noamr
noamr marked this pull request as ready for review November 8, 2026 11:57
@noamr noamr added the topic: parser The HTML parser and serializer, including fragment parsing and character references label Nov 8, 2026
@noamr

noamr commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

@whatwg/html-parser

@annevk

annevk commented Nov 9, 2026

Copy link
Copy Markdown
Member
AI feedback

Reviewed at f40f15e. The new algorithm's cross-references resolve, and the #adoptionAgency anchor stays on the wrapper. WebKit results below are from tip-of-tree WebKitTestRunner.

Blockers

Suggestions

  • Consider correcting the commit message's account of the current behavior. During normal parsing, the script end tag performs a microtask checkpoint, which runs the backup element queue before the script executes. So today the reactions already run before a following script element; only document.write() (and streaming with script on the stack) defers them past it. WebKit does exactly this and passes the first three subtests, so "it is not what engines do" doesn't hold for WebKit. What changes for normal parsing is that the callbacks run right after the algorithm, before later tokens are processed, rather than at the next microtask checkpoint.
  • Consider narrowing three more claims in the commit message:
    • "No script runs while the tree is in an intermediate state of the algorithm": creating a customized built-in clone or new element runs its constructor mid-algorithm (after a microtask checkpoint), and reinserting an iframe runs the iframe load event steps synchronously. "No custom element callback reactions caused by the algorithm run…" would hold.
    • "The callbacks run in the order the reactions were enqueued": invoking an element queue runs all of an element's reactions the first time it is dequeued, so with two custom elements in the furthest block, all of the first one's callbacks run before any of the second one's.
    • "That matches this PR for normal parsing": only for what the tests observe. In WebKit, the callbacks for <b><p><x-aaa></x-aaa></b><span id=after></span> see #after, and those for <a><p><x-aaa></x-aaa><a id=a2> see #a2.
  • For the a and nobr start tags, more steps of the same token follow the adoption agency algorithm, so these reactions run mid-token, before the token's new element is inserted. They run without the throw-on-dynamic-markup-insertion counter, so a document.write() call from a callback during document.write()-driven parsing re-enters the tokenizer mid-token, which create an element for the token prevents for its own reactions. Consider incrementing the counter while invoking these reactions, or raising this together with insert an element at the adjusted insertion location, which has the same exposure. (WebKit already throws an InvalidStateError for document.write() in the connectedCallback of a parser-created custom element.)
  • Consider "inner adoption agency algorithm" as the new algorithm's name, as with "inner consider speculative loads steps" here and "inner invoke" in DOM. "Adoption agency algorithm steps" reads like the steps of the adoption agency algorithm itself.
  • Possibly as a follow-up: the "in body" frameset start tag also removes already-parsed nodes ("Remove the second element on the stack of open elements from its parent node") without an element queue. Custom elements can be among them, since neither an implicit body nor "any other start tag" sets the frameset-ok flag to "not ok", e.g., <x-a></x-a><frameset> with x-a defined by an earlier script.

Nits

  • The line ending in and <span>invoke custom is 101 columns.
  • The wrapper keeps "which takes as its only argument a token token for which the algorithm is being run, consists of the following steps"; it could match the new algorithm's "given a token token".

@noamr

noamr commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed all comments:

@noamr
noamr added this pull request to stack #13222 November 9, 2026 19:42
@annevk

annevk commented Nov 10, 2026

Copy link
Copy Markdown
Member
AI feedback

Reviewed at 1c0d36b.

Suggestions

  • Two earlier suggestions are still open. Consider addressing them here or in follow-up issues:
    • For the a and nobr start tags, the reactions invoked by the new wrapper run mid-token, without the throw-on-dynamic-markup-insertion counter. So a callback's document.write() during document.write()-driven parsing re-enters the tokenizer mid-token. Insert an element at the adjusted insertion location has the same exposure.
    • The "in body" frameset start tag removes already-parsed nodes, which can include custom elements (e.g., <x-a></x-a><frameset> with an implicit body), without an element queue.
  • In the commit message, "the </script> microtask checkpoint is skipped because the JavaScript execution context stack is not empty, deferring those reactions onto document.write()'s element queue" gets the cause slightly wrong. The reactions are enqueued on document.write()'s element queue because document.write() is [CEReactions], which makes that queue the current element queue. The checkpoint only runs the backup element queue, so skipping it doesn't change where they go.

@noamr

noamr commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Updated the PR description to clarify that document.write()'s [CEReactions] extended attribute makes its element queue the current element queue (rather than attributing that to skipping the </script> microtask checkpoint).

For the other two points (throw-on-dynamic-markup-insertion counter while invoking reactions—which insert an element at the adjusted insertion location also shares—and the "in body" frameset start tag removing body without an element queue), let's track those in follow-up issues so this PR stays scoped to #13072.

@noamr
noamr force-pushed the noamr/aaa-reactions branch from 1c0d36b to 237051a Compare November 25, 2026 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: parser The HTML parser and serializer, including fragment parsing and character references

Development

Successfully merging this pull request may close these issues.

Timing of CE reactions with adoption agency

2 participants