<html>
    <head>
      <base href="https://bugs.webkit.org/" />
    </head>
    <body>
      <p>
        <div>
            <b><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Implement iterator for traversing composed DOM"
   href="https://bugs.webkit.org/show_bug.cgi?id=149997#c9">Comment # 9</a>
              on <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Implement iterator for traversing composed DOM"
   href="https://bugs.webkit.org/show_bug.cgi?id=149997">bug 149997</a>
              from <span class="vcard"><a class="email" href="mailto:koivisto&#64;iki.fi" title="Antti Koivisto &lt;koivisto&#64;iki.fi&gt;"> <span class="fn">Antti Koivisto</span></a>
</span></b>
        <pre><span class="quote">&gt; &gt; Not sure what you mean. In general case there is no way to know if there is
&gt; &gt; a trip to shadow tree between a node and its ancestor without walking the
&gt; &gt; parent chain.
&gt; 
&gt; You do. If you only move into a slot if the slot is the next node, or if the
&gt; next node is assigned into a slot. Since a node can only be assigned into a
&gt; slot inside shadow DOM of its parent node, you just need to do:
&gt; if (auto parent = node-&gt;parentElement()) {
&gt;   if (auto shadowRoot = parent-&gt;shadowRoot()) {
&gt;     slot = shadowRoot-&gt;findAssignedSlot(this)
&gt;   }
&gt; }</span >

Right. And if you have a node and an ancestor you need to walk the parent chain between them to see if this condition happens to know if there are trip to a shadow tree there. That's what I meant.

<span class="quote">&gt; For moving out of a slot, you find the assigned slot in O(1) as shown above,
&gt; and check whether you're the last one in the assigned nodes list or not.  If</span >

Yeah. That's what the patch does.

<span class="quote">&gt; you're the last assigned node, then just move to the next node from the
&gt; slot. If the slot from which you're moving out is assigned to another slot,
&gt; then you into that slot's next assigned node. If we're the last slot</span >

I'm maintaining the shadow stack to cache the assigned node index within the current slot. This allows moving to the slot's next assigned node in O(1).

A way to sort-of do this stacklessly (though I'm not sure if that is what you are arguing for) would be to walk the real siblings of the current slot assignee and find the next one in the same slot. This is much slower since we are doing hash lookups vs indexing to array. It also has bad worst case if nodes are assigned to mixed slots (like ABCABCABC.

If we are maintaining a stack and want to support arbitrary starting state for the iterator then we need to have function to synthesize the stack without traversing the tree. That's what initializeShadowStack() is about.

Note that it doesn't get called at all if you use shadowTreeDescendants() and is almost never called for shadowTreeChildren() (since there is a fast path that avoids it).

<span class="quote">&gt; assigned to another slot, then we recursively move out of slot as long as
&gt; the slot we just moved out is the last assigned node in the containing slot.
&gt; 
&gt; Does that make sense?</span ></pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>