Skip to content

Clarify when a parser-inserted element is dropped on the floor - #12708

Merged
annevk merged 2 commits into
mainfrom
clarify-drop-element-into-document
Aug 20, 2026
Merged

Clarify when a parser-inserted element is dropped on the floor#12708
annevk merged 2 commits into
mainfrom
clarify-drop-element-into-document

Conversation

@annevk

@annevk annevk commented Jul 19, 2026

Copy link
Copy Markdown
Member

Clarify when a parser-inserted element is dropped on the floor

"Insert an element at the adjusted insertion location" previously aborted when
it was "not possible to insert" the element, without defining what that meant.
Replace it with an explicit condition — the adjusted insertion location is in a
Document that already has an element child — mirroring the analogous check in
"insert a character", and assert that the insertion then satisfies the DOM's
"ensure pre-insert validity".

Tests: web-platform-tests/wpt#61396

See #1706.

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


/infrastructure.html ( diff )
/parsing.html ( diff )

@noamr noamr closed this Aug 17, 2026
@noamr noamr reopened this Aug 17, 2026
@noamr

noamr commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

It's quite complex but I believe this doesn't cover all the cases. I think that there could be cases of "If it is not possible to insert..." where you have a pagehide script that would be triggered in the middle of AAA and would make it so that insertion is circular or has an a refnode that's no longer a child of the intended parent?

The following creates a circular failure:

<div id="common-ancestor">
  <b id="formatting-element">
    <p id="furthest-block">
      
      <iframe id="trap-frame"></iframe>

      <script>
        let frame = document.getElementById('trap-frame');
        frame.contentWindow.onpagehide = () => {
          const ca = document.getElementById('common-ancestor');
          const fb = document.getElementById('furthest-block');
          fb.remove(); 
          fb.appendChild(ca); 
        };
      </script>

    </b>
    </p>

https://software.hixie.ch/utilities/js/live-dom-viewer/?saved=14827

@noamr noamr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approved since sync pagehide is non-standard (see #10194). Though it would be good to cover that in WPTs nontheless.

annevk added a commit to annevk/WebKit that referenced this pull request Aug 18, 2026
https://bugs.webkit.org/show_bug.cgi?id=319780

Reviewed by NOBODY (OOPS!).

The parser inserts nodes with low-level operations that skip pre-insertion validity
checks, so script that moves a node on the stack of open elements during parsing
could make us build a tree the DOM does not allow.

Use Document::canAcceptChild() when inserting into a Document, which drops Text
node children, a second element child, and a second or misordered DocumentType
child on the floor. This is reachable by foster parenting into a table that script
has made another Document's document element, and by document.open() leaving
children in the Document before the DOCTYPE token or the "html" start tag.

The adoption agency algorithm checked for cycles with Node::contains(), which only
considers the node's own tree, so it missed a parent script had moved into a shadow
root (crashing) or into a template element's content (silently creating an invalid
tree).

Move the nextChild check out of executeInsertAlreadyParsedChildTask() and into
insert(), where nextChild is used. Script can run before a queued insertion takes
place: in the case from bug 169222, parserRemoveChild() destroys an iframe
element's frame and its unload handler removes nextChild from the parent, which is
covered by fast/parser/scriptexec-during-parserInsertBefore.html. Nothing about
that is specific to the adoption agency, though no input is known to reach the
check through a plain insertion.

Because the template element branch runs first and clears nextChild, a stale
nextChild no longer prevents an insertion into a template element's contents. That
is what appropriate place for inserting a node calls for, as it redirects into the
contents after the last child, discarding the reference node.

Spec changes: whatwg/html#12708
              whatwg/html#12709

Tests: imported/w3c/web-platform-tests/html/syntax/parsing/foster-parenting-into-document-with-element-child.html
       imported/w3c/web-platform-tests/html/syntax/parsing/insert-into-nonempty-document.html
	   imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_url.html?file=scripted_foster01
	   imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_write.html?file=scripted_foster01
	   imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_write_single.html?file=scripted_foster01
	   imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-document-target.html
	   imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-shadow-cycle.html
	   imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-template-cycle.html
annevk added a commit to annevk/WebKit that referenced this pull request Aug 19, 2026
https://bugs.webkit.org/show_bug.cgi?id=319780
rdar://183256491

Reviewed by NOBODY (OOPS!).

The parser inserts nodes with low-level operations that skip pre-insertion validity
checks, so script that moves a node on the stack of open elements during parsing
could make us build a tree the DOM does not allow.

Use Document::canAcceptChild() when inserting into a Document, which drops Text
node children, a second element child, and a second or misordered DocumentType
child on the floor. This is reachable by foster parenting into a table that script
has made another Document's document element, and by document.open() leaving
children in the Document before the DOCTYPE token or the "html" start tag.

The adoption agency algorithm checked for cycles with Node::contains(), which only
considers the node's own tree, so it missed a parent script had moved into a shadow
root (crashing) or into a template element's content (silently creating an invalid
tree).

Move the nextChild check out of executeInsertAlreadyParsedChildTask() and into
insert(), where nextChild is used. Script can run before a queued insertion takes
place: in the case from bug 169222, parserRemoveChild() destroys an iframe
element's frame and its unload handler removes nextChild from the parent, which is
covered by fast/parser/scriptexec-during-parserInsertBefore.html. Nothing about
that is specific to the adoption agency, though no input is known to reach the
check through a plain insertion.

Because the template element branch runs first and clears nextChild, a stale
nextChild no longer prevents an insertion into a template element's contents. That
is what appropriate place for inserting a node calls for, as it redirects into the
contents after the last child, discarding the reference node.

Spec changes: whatwg/html#12708
              whatwg/html#12709

Tests: imported/w3c/web-platform-tests/html/syntax/parsing/foster-parenting-into-document-with-element-child.html
       imported/w3c/web-platform-tests/html/syntax/parsing/insert-into-nonempty-document.html
       imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_url.html?file=scripted_foster01
       imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_write.html?file=scripted_foster01
       imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_write_single.html?file=scripted_foster01
       imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-document-target.html
       imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-shadow-cycle.html
       imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-template-cycle.html
annevk added a commit to web-platform-tests/wpt that referenced this pull request Aug 19, 2026
document.open() empties the Document and returns the parser to the "initial"
insertion mode, so script can add children before the next document.write(). The
DOCTYPE token and the "html" start tag then append directly to the Document, which
the DOM does not allow when it already has a doctype child or an element child, so
those nodes have to be dropped on the floor.

See whatwg/html#12708 for context.
webkit-commit-queue pushed a commit to annevk/WebKit that referenced this pull request Aug 19, 2026
https://bugs.webkit.org/show_bug.cgi?id=319780
rdar://183256491

Reviewed by Ryosuke Niwa.

The parser inserts nodes with low-level operations that skip pre-insertion validity
checks, so script that moves a node on the stack of open elements during parsing
could make us build a tree the DOM does not allow.

Use Document::canAcceptChild() when inserting into a Document, which drops Text
node children, a second element child, and a second or misordered DocumentType
child on the floor. This is reachable by foster parenting into a table that script
has made another Document's document element, and by document.open() leaving
children in the Document before the DOCTYPE token or the "html" start tag.

The adoption agency algorithm checked for cycles with Node::contains(), which only
considers the node's own tree, so it missed a parent script had moved into a shadow
root (crashing) or into a template element's content (silently creating an invalid
tree).

Move the nextChild check out of executeInsertAlreadyParsedChildTask() and into
insert(), where nextChild is used. Script can run before a queued insertion takes
place: in the case from bug 169222, parserRemoveChild() destroys an iframe
element's frame and its unload handler removes nextChild from the parent, which is
covered by fast/parser/scriptexec-during-parserInsertBefore.html. Nothing about
that is specific to the adoption agency, though no input is known to reach the
check through a plain insertion.

Because the template element branch runs first and clears nextChild, a stale
nextChild no longer prevents an insertion into a template element's contents. That
is what appropriate place for inserting a node calls for, as it redirects into the
contents after the last child, discarding the reference node.

Spec changes: whatwg/html#12708
              whatwg/html#12709

Tests: imported/w3c/web-platform-tests/html/syntax/parsing/foster-parenting-into-document-with-element-child.html
       imported/w3c/web-platform-tests/html/syntax/parsing/insert-into-nonempty-document.html
       imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_url.html?file=scripted_foster01
       imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_write.html?file=scripted_foster01
       imported/w3c/web-platform-tests/html/syntax/parsing/html5lib_write_single.html?file=scripted_foster01
       imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-document-target.html
       imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-shadow-cycle.html
       imported/w3c/web-platform-tests/html/syntax/parsing/adoption-agency-reparenting-template-cycle.html

Canonical link: https://commits.webkit.org/319453@main
annevk added 2 commits August 20, 2026 10:20
"Insert an element at the adjusted insertion location" previously aborted when
it was "not possible to insert" the element, without defining what that meant.
Replace it with an explicit condition — the adjusted insertion location is in a
Document that already has an element child — mirroring the analogous check in
"insert a character", and assert that the insertion then satisfies the DOM's
"ensure pre-insert validity".

Tests: web-platform-tests/wpt#61396

See #1706.
@annevk
annevk force-pushed the clarify-drop-element-into-document branch from 4b5307d to ac66b72 Compare August 20, 2026 08:23
@annevk
annevk merged commit 8d14932 into main Aug 20, 2026
2 checks passed
@annevk
annevk deleted the clarify-drop-element-into-document branch August 20, 2026 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants