<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>325616</bug_id>
          
          <creation_ts>2026-09-29 01:02:52 -0700</creation_ts>
          <short_desc>moveBefore() updates id and name maps of shadow trees inside the moved subtree against the wrong tree scope</short_desc>
          <delta_ts>2026-09-29 23:22:15 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>DOM</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://github.com/web-platform-tests/wpt/pull/63051</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          <blocked>325123</blocked>
    
    <blocked>314067</blocked>
    
    <blocked>325644</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="Anne van Kesteren">annevk</reporter>
          <assigned_to name="Anne van Kesteren">annevk</assigned_to>
          <cc>ntim</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2255924</commentid>
    <comment_count>0</comment_count>
    <who name="Anne van Kesteren">annevk</who>
    <bug_when>2026-09-29 01:02:52 -0700</bug_when>
    <thetext>Element::movingSteps() re-registers ids and names for every shadow-including
inclusive descendant of the moved node, and always uses the old parent&apos;s tree
scope as the scope to remove from. For nodes in a shadow tree inside the moved
subtree that is wrong: their tree scope is their own shadow root, which the move
does not change. They get removed from the old parent&apos;s maps, where they were
never registered, and then registered a second time in their shadow root&apos;s maps.

This is reached by moving a &lt;select&gt; or &lt;details&gt;, or an ancestor of one,
because their user agent shadow trees contain a named &lt;slot&gt;. It is also reached
by moving any shadow host whose shadow tree has an element with an id or name.

Steps to reproduce:

&lt;div id=host1&gt;&lt;/div&gt;
&lt;div id=inner&gt;&lt;/div&gt;
&lt;img name=x&gt;
&lt;div id=from&gt;&lt;select&gt;&lt;/select&gt;&lt;details&gt;&lt;/details&gt;&lt;/div&gt;
&lt;div id=to&gt;&lt;/div&gt;
&lt;script&gt;
host1.attachShadow({mode: &quot;open&quot;}).innerHTML = &apos;&lt;div id=inner&gt;&lt;/div&gt;&apos;;
to.moveBefore(host1, null);
to.moveBefore(from, null);
&lt;/script&gt;

In a Debug build this asserts in TreeScopeOrderedMap: registeredElements.remove()
fails in remove() when the id is moved, and
!entry.registeredElements.contains(&amp;element) fails in add() via
TreeScope::addElementByName() when the named slot of &lt;select&gt; or &lt;details&gt; is
moved.

From reading the code, release builds are affected too:
- If the document&apos;s name map lacks the slot&apos;s name, the removal hits
  RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(it != m_map.end()).
- If the document has an element with the same id as one in the moved shadow
  tree, the removal either hits the RELEASE_ASSERT in remove() or silently drops
  the document&apos;s registration, and document.getElementById() stops finding it.

Bug 324715 has the same root cause for a move inside a disconnected tree, where
the old parent&apos;s tree scope is the document.

The insertion and removal steps already avoid this: they pass
TreeScopeChange::DidNotChange for shadow roots, and only update the maps when the
tree scope changed. The moving steps can do the same, and they should only
remove from the old tree scope when the old parent was in one. A patch with
web-platform-tests is ready.

imported/w3c/web-platform-tests/html/semantics/forms/the-select-element/customizable-select/selectedcontent-subtree-mutations.html
is skipped because of this.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2256000</commentid>
    <comment_count>1</comment_count>
    <who name="Anne van Kesteren">annevk</who>
    <bug_when>2026-09-29 04:27:37 -0700</bug_when>
    <thetext>I have a fix for this.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2256005</commentid>
    <comment_count>2</comment_count>
    <who name="Anne van Kesteren">annevk</who>
    <bug_when>2026-09-29 04:52:10 -0700</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/75264</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2256006</commentid>
    <comment_count>3</comment_count>
    <who name="Anne van Kesteren">annevk</who>
    <bug_when>2026-09-29 04:54:55 -0700</bug_when>
    <thetext>Submitted web-platform-tests pull request: https://github.com/web-platform-tests/wpt/pull/63051</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2256010</commentid>
    <comment_count>4</comment_count>
    <who name="Anne van Kesteren">annevk</who>
    <bug_when>2026-09-29 04:58:35 -0700</bug_when>
    <thetext>*** Bug 324715 has been marked as a duplicate of this bug. ***</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2256025</commentid>
    <comment_count>5</comment_count>
    <who name="Anne van Kesteren">annevk</who>
    <bug_when>2026-09-29 06:01:17 -0700</bug_when>
    <thetext>Submitted web-platform-tests pull request: https://github.com/web-platform-tests/wpt/pull/63051</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2256521</commentid>
    <comment_count>6</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2026-09-29 23:21:20 -0700</bug_when>
    <thetext>Committed 322249@main (537fe4583eb1): &lt;https://commits.webkit.org/322249@main&gt;

Reviewed commits have been landed. Closing PR #75264 and removing active labels.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2256522</commentid>
    <comment_count>7</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2026-09-29 23:22:15 -0700</bug_when>
    <thetext>&lt;rdar://problem/188785171&gt;</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>