Skip to content

Custom elements: expect adoption agency reactions once the algorithm finishes - #64911

Open
noamr wants to merge 4 commits into
masterfrom
noamr/aaa-reactions
Open

noamr wants to merge 4 commits into
masterfrom
noamr/aaa-reactions

Conversation

@noamr

@noamr noamr commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Spec PR: whatwg/html#13215 (for whatwg/html#13072).

The adoption agency algorithm reparents nodes, and when they are or contain custom elements, that queues connectedCallback() and disconnectedCallback() reactions. Without an element queue around the adoption agency algorithm, those reactions go to the backup element queue during normal parsing (running at the next microtask checkpoint, such as at a </script> end tag after any intervening tokens have been parsed) or to document.write()'s element queue when parsing from script (running only when document.write() returns, after a written <script> has already executed). The spec PR pushes an element queue around the whole algorithm, so the reactions run once it finishes, before later tokens are processed, and every callback observes the final tree produced by the algorithm.

The test uses an x-aaa element that logs each callback together with isConnected, its parent at that moment, and optional element ID or target-presence checks:

  • <b><p><x-aaa></x-aaa></b>: moving <p> to the common ancestor and moving its children into the new <b> each produce a disconnected/connected pair; all four callbacks run before the script and see <x-aaa> connected inside the new <b>.
  • <b><i><p><x-aaa></x-aaa></b>: the inner loop moves <p> into a clone of <i> that is not yet inserted; the callbacks do not observe that intermediate state and log the same as above.
  • A custom element before the furthest block, inside a formatting element that gets cloned, stays where it is and gets no reactions.
  • <b><p><x-aaa data-check-id="after"></x-aaa></b><span id="after"></span>: callbacks run right after the adoption agency algorithm finishes, before #after is parsed.
  • <a><p><x-aaa data-check-id="a2"></x-aaa><a id="a2"></a>: callbacks run during the second <a> start tag before #a2 is inserted.
  • <b><p><x-aaa id="first"></x-aaa><x-aaa id="second"></x-aaa></b>: reactions for multiple custom elements in the furthest block are invoked grouped per element (all four of #first's callbacks before #second's).
  • document.write('<b><p><x-aaa></x-aaa></b><script>...</script>'): reactions run before the written <script>, with nothing left over when document.write() returns.

Test that the custom element reactions caused by the adoption agency
algorithm moving the furthest block, moving it into a clone created by
the inner loop, and moving its children into the new formatting element
are invoked before the following script element runs, both when parsing
the document and from document.write(), and that each reaction observes
the tree as it is right after the step that caused it.

For whatwg/html#13072.
@noamr

noamr commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Review-and-fix pass over the current head (b9d9cd8), together with whatwg/html#13215.

Questions / follow-ups

  • Once engines parse innerHTML/setHTMLUnsafe() directly into the context element, the same reactions become observable mid-parse there too; a fragment-case test could be added then (see the question on the spec PR).

Findings

No changes needed. All four tests trace correctly through the spec change, and each can fail against the previous text: without the per-step queues the reactions would run from a microtask or, for document.write(), from its [CEReactions] queue after the written script, so the recorded isConnected/parent values and the writtenLog contents would differ. The written script's closing tag is escaped, lint is clean, and there are no waits or shared state across tests beyond the log, which each test drains.

Results: Chrome Canary passes all four. Firefox passes the simple and document.write() tests and fails the two inner-loop tests with disconnected:true:p instead of disconnected:false:p, matching the implementation gap described in the spec PR. No harness errors or timeouts.

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 Custom elements: test reaction timing in the adoption agency algorithm Custom elements: expect adoption agency reactions once the algorithm finishes Nov 8, 2026
@annevk

annevk commented Nov 9, 2026

Copy link
Copy Markdown
Member
AI feedback

Reviewed at 48544ae. In tip-of-tree WebKitTestRunner, the first three subtests pass and the document.write() subtest fails with only ["connected:true:p"] logged before the written script, as described.

Suggestions

  • Consider holding the expectations until the operation order in Adoption agency: fix element creation and reparenting with a sanitizer whatwg/html#13214 is settled. That PR now moves the furthest block's children into the new element and inserts that element before the furthest block itself is moved, also without a sanitizer. In the second and third subtests, the inner loop has already moved p into the clone of i, which isn't inserted yet, so <x-aaa> gets one disconnected/connected pair instead of the two in expectedReactions. Those two subtests then don't match the spec at Adoption agency: invoke custom element reactions once the algorithm finishes whatwg/html#13215's head.
  • Consider adding subtests that tell the change apart from the current spec during normal parsing. Today the script end tag's microtask checkpoint already runs the backup element queue before the script executes, so the first three subtests pass without the change (WebKit implements the current spec). For example:
    • <b><p><x-aaa></x-aaa></b><span id=after></span>, with the callbacks recording whether #after exists yet. With the change it doesn't; in WebKit it does.
    • <a><p><x-aaa></x-aaa><a id=a2>, where the second a start tag runs the adoption agency algorithm and the reactions run before that a is inserted, so the callbacks shouldn't see #a2. In WebKit they do.
    • Two custom elements in the furthest block, which pins that the reactions are grouped per element (all of the first one's callbacks before the second one's), unlike invoking them after each DOM operation.

@noamr

noamr commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Addressed all comments:

@annevk

annevk commented Nov 10, 2026

Copy link
Copy Markdown
Member
AI feedback

Reviewed at 53651b2.

Suggestions

  • Consider expecting #after inside p in the before-next-token serialization: <b></b><p><b><x-aaa data-check-id="after"></x-aaa></b><span id="after"></span></p>. The p element is still open after </b>, so <span id="after"> is inserted into it, which is also where WebKit puts it. As written, an implementation of the spec change passes the reactions assertion and then fails this one.

@noamr

noamr commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in e24e1116f3: updated the expected #before-next-token serialization to '<b></b><p><b><x-aaa data-check-id="after"></x-aaa></b><span id="after"></span></p>'.

This branch has not been deployed

No deployments
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.

5 participants