Skip to content

anchorNode & focusNode can never be in shadow tree? #361

Open

Description

@yezhizhen

anchor and focus of selection need not to be in the document tree. It could be in a shadow tree of the same document.

However, it also says:
anchorNode
The attribute must return the anchor node of this, or null if the anchor is null or anchor is not in the document tree.

So anchorNode can never be in shadow tree. This is also contradictory to tests:
https://github.com/servo/servo/blob/e83f3117045171e59f230c3b36ebd721826d3754/tests/wpt/tests/selection/shadow-dom/tentative/Selection-isCollapsed.html#L48

Activity

  1. added
    Agenda+
    on Aug 26, 2026
  2. dandclark commented on Sep 10, 2026

    @dandclark

    Discussed in September meeting:

    The spec is probably wrong here. Chromium and Firefox both pass the test, Safari fails. But the test is not directly focused on all the cases we might care about here, it's more focused on selection collapsing. In particular, there is a a question of whether anchorNode/focusNode should be retarget their result into the light DOM before returning (which is probably more correct), or whether they should just return a shadow node directly (which is what Chromium and Firefox seem to do).

    The next step would be to write a WPT focused on these scenarios:

    • When selection is in a shadow DOM, including cases with nested shadow DOMs, what do focusNode/anchorNode return? The node itself, null, or the node retargeted into the light DOM?
    • When selection is on a node that's <slot>ted into a shadow DOM, what gets returned for focusNode/anchorNode? The slotted node, the slot, the shadow host, null?
  3. annevk commented on Sep 11, 2026

    @annevk
    Member

    Selection is not allowed to bypass encapsulation so if the selection is returned from window.getSelection() you should not be able to get shadow root nodes from it.

  4. smaug---- commented on Oct 8, 2026

    @smaug----

    I agree with that, but a question here is that would changing the behavior break lots of sites. I guess we need some telemetry.
    And if we do retargeting, what kind of retargeting? selection is a weird beast, where APIs tend to deal with DOM tree, but visually it is a flat tree (and because of slots and such, ordering in those trees might be different).

  5. dandclark commented on Oct 29, 2026

    @dandclark

    From TPAC 2026 Editing WG meeting:

    RESOLVED: @smaug---- will write tentative WPTs, including more interesting slotting cases etc. Chromium will also add telemetry to see how often these focus getters are called for selections in shadow roots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Agenda+

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions