Skip to content

Sanitizer: test the adoption agency algorithm with removed formatting elements - #64909

Open
noamr wants to merge 6 commits into
masterfrom
noamr/aaa-fixes
Open

noamr wants to merge 6 commits into
masterfrom
noamr/aaa-fixes

Conversation

@noamr

@noamr noamr commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Tests for whatwg/html#13214, which fixes how the adoption agency algorithm creates and reparents elements when the parser's sanitizer configuration removes a formatting element (whatwg/html#13093).

The new sanitizer-api/sanitizer-adoption-agency-removed-elements.html pins down, when furthestBlock is moved out of a removed formatting element:

  • that a scoped custom element registry on the setHTMLUnsafe() target applies to furthestBlock, to its deeper descendants, and to elements parsed into it afterwards, just as it does when the formatting element is kept (tests 1–3);
  • that furthestBlock's children never become connected: a customized built-in element in there gets no connectedCallback, and an img sends no request, while the adopted furthestBlock itself still works (test 4);
  • that inside a template, the reparented elements and inner-loop clones keep a null registry and the template contents' document, and that a clone whose token has an is attribute survives with its children (tests 5–6).

Chrome Canary and Firefox Nightly fail tests 2 and 3 (neither propagates the scoped registry); Chrome Canary also fails test 6 because it drops the inner-loop clone of a formatting element with an is attribute, independently of the sanitizer.

… elements

When the sanitizer removes a formatting element, the adoption agency
algorithm can reparent furthestBlock (and the formatting elements cloned
above it) out of the removed element's inert subtree into the live tree.

These tests pin down what happens then, per whatwg/html#13093:

- elements that are moved into a target with a scoped custom element
  registry pick up that registry, so custom elements parsed into them
  afterwards are created with it;
- children of furthestBlock that end up below a removed clone of the
  formatting element never become connected, so no custom element
  reactions run for them and their resources are not fetched;
- inner-loop clones created in template contents have a null registry
  like everything else in there, and are not upgraded.
@noamr

noamr commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Review-and-fix pass over the current head (63815b4), together with whatwg/html#13214.

Questions / follow-ups

  • Test 4 (Children of furthestBlock that end up below a removed formatting element never become connected) awaits a control image load (both onload and onerror settle the promise, so it cannot hang) and then two record-header.py probes, so it depends on /fetch/metadata/resources/. Fine by me, just noting the cross-directory dependency in case you would rather use a local resources/ endpoint.
  • Results on this head: Chrome Canary fails tests 2, 3, and 6; Firefox Nightly fails tests 2 and 3; no harness errors or timeouts. Test 6's failure in Chrome reproduces without a sanitizer (<b><i is="x"><p></b> loses the cloned i), so it is a pre-existing parser bug rather than a sanitizer one; happy to file it.

Findings

No test changes were needed. I traced each of the six tests through the algorithm on the spec PR branch and the expected values follow from it: tests 2 and 3 need the registry propagation step before lastNode is inserted, test 4 needs furthestBlock's children to be moved into the removed newElement before anything is inserted (and the trailing x lands in the now-empty p), and tests 5 and 6 need the inner-loop clone to take node's document and registry rather than commonAncestor's. ./wpt lint is clean, the inline script has no literal </script>, and the file has no flaky waits. On the spec side I pushed two non-normative wording/markup commits; see the comment on whatwg/html#13214.

@annevk

annevk commented Nov 10, 2026

Copy link
Copy Markdown
Member
AI feedback

Reviewed at 575dc99.

Suggestions

  • Consider going back to ["constructed", "connected"] in the removed intermediate element subtest. setHTMLUnsafe() parses into a DocumentFragment in the inert document, so nothing is connected while the adoption agency algorithm removes and inserts nodes, and none of that causes callbacks, whatever the order. The custom element is first connected when the fragment replaces the target's children, and is then upgraded, logging "constructed" and "connected", as in the first three subtests.

@noamr

noamr commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in df85cc86b4: reverted the expected reaction log in "furthestBlock and its children reparented out of a removed intermediate element get the target's scoped registry" back to ["constructed", "connected"], since setHTMLUnsafe() parses into a DocumentFragment and only connects/upgrades the custom element when replacing the target's children.

@otherdaniel otherdaniel 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.

Thanks! This all looks good to me, but admittedly I'm having a hard time thinking through the edge cases here. :|

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