[Webkit-unassigned] [Bug 149997] Implement iterator for traversing composed DOM

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Mon Oct 12 15:03:42 PDT 2015


https://bugs.webkit.org/show_bug.cgi?id=149997

--- Comment #8 from Ryosuke Niwa <rniwa at webkit.org> ---
(In reply to comment #7)
> > Walking up the tree each time seems unnecessary / inefficient
> > since we may never get out of the current shadow DOM.
> 
> Stack setup code is there mostly for completeness sake (that is, it allows
> starting from any node within any root). In normal use the stack build as
> you traverse.
> 
> > It's probably better to figure out the counting shadow root instead
> > since node->treeScope->root() will just get you that in O(1).
> 
> Not sure what you mean. In general case there is no way to know if there is
> a trip to shadow tree between a node and its ancestor without walking the
> parent chain.

You do. If you only move into a slot if the slot is the next node, or if the next node is assigned into a slot. Since a node can only be assigned into a slot inside shadow DOM of its parent node, you just need to do:
if (auto parent = node->parentElement()) {
  if (auto shadowRoot = parent->shadowRoot()) {
    slot = shadowRoot->findAssignedSlot(this)
  }
}
to find the slot into which we're moving into. This is O(1). Once you found the slot, you can figure out whether we're moving in ("this" is the first assigned node), still in the middle of the slot, or we're moving out of the slot ("this" is the last assigned node).

If "this" is a slot element, we just need to recursively find the first assigned node as long as the current assigned node is also a slot.

For moving out of a slot, you find the assigned slot in O(1) as shown above, and check whether you're the last one in the assigned nodes list or not.  If you're the last assigned node, then just move to the next node from the slot. If the slot from which you're moving out is assigned to another slot, then you into that slot's next assigned node. If we're the last slot assigned to another slot, then we recursively move out of slot as long as the slot we just moved out is the last assigned node in the containing slot.

Does that make sense?

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.webkit.org/pipermail/webkit-unassigned/attachments/20151012/62730b75/attachment-0001.html>


More information about the webkit-unassigned mailing list