Skip to content

HTMLElement.prototype.offsetParent leaks a node inside a shadow tree #497

Open

Description

@rniwa

We need to patch offsetParent so that when a node's container block is inside a shadow tree, we don't leak a node inside a shadow tree.

Chrome seems to return null in such cases. It's somewhat undesirable in that this makes the feature useless (since you can't determine where your containing block is) but I don't have a strong opinion about this as long as we don't leak a node inside a shadow tree.

Activity

  1. rniwa commented on May 7, 2016

    @rniwa
    CollaboratorAuthor
  2. rniwa commented on May 7, 2016

    @rniwa
    CollaboratorAuthor

    It looks like Chrome returns null whenever the offset parent is inside a shadow tree. I think a better change would be to return null if the offset parent is not an unclosed node of the context object.

  3. rniwa commented on May 7, 2016

    @rniwa
    CollaboratorAuthor

    An alternative solution is to walk up offsetParent enough to find the first offset parent that is unclosed node to the context object.

    This is probably a better fix since authors won't be able to determine the location of an element which is assigned to a slot otherwise.

  4. kojiishi commented on May 8, 2016

    @kojiishi

    Should offsetLeft and offsetTop be adjusted accordingly?

  5. rniwa commented on May 8, 2016

    @rniwa
    CollaboratorAuthor

    Right. Since offsetTop and offsetLeft are defined to use offsetParent already, that's kind of automatic.

  6. hayatoito commented on Jun 1, 2016

    @hayatoito
    Contributor

    I have filed an issue for csswg: w3c/csswg-drafts#159

  7. hayatoito commented on Jun 20, 2016

    @hayatoito
    Contributor

    PR is merged. Let me close this issue.

  8. rniwa commented on Dec 10, 2016

    @rniwa
    CollaboratorAuthor

    Sorry, it looks like I missed an crucial point in w3c/csswg-drafts#159

    We’re exposing any node that’s not closed-shadow-hidden here, which means we’d return an element inside shadow tree as an offset parent. I don’t think we want that even in an open shadow tree.

    Let’s imagine you’re trying to traverse over ancestor offsetParents. Such a code would now walk over elements inside a shadow tree. I don’t think this is desirable just as much as finding nodes via querySelector or getElementsByTagName inside shadow trees are not desirable.

  9. reopened this on Dec 10, 2016
  10. rniwa commented on Oct 26, 2018

    @rniwa
    CollaboratorAuthor

    Rough consensus at TPAC F2F: Use the retargeting algorithm to find the offset parent, and the compute left and top from that node.

  11. rniwa commented on Apr 25, 2019

    @rniwa
    CollaboratorAuthor

    We need to resolve CSS OM View spec change in w3c/csswg-drafts#159

  12. added a commit that references this issue on Apr 24, 2021
  13. eKoopmans commented on Dec 7, 2022

    @eKoopmans

    Note: This behaviour appears to have now been updated in Chrome 109 (and earlier versions of Firefox/Webkit) so that it no longer leaks elements within a shadow DOM.

    If you need the original behaviour, see this comment from October 2022 and the linked polyfill:
    https://github.com/josepharhar/offsetparent-polyfills

  14. added a commit that references this issue on Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions