Skip to content

[cssom-view] HTMLElement.prototype.offsetParent leaks a node inside a shadow tree #159

Closed

Description

@hayatoito

See the context: WICG/webcomponents#497

@rniwa, do you have any preference to this issue? I do not have a strong opinion yet.

Activity

  1. tabatkins commented on Jun 1, 2016

    @tabatkins
    Member

    I support @rniwa's answer - walking up until you find a valid (non-leaky) offsetParent, then making sure that offsetLeft and offsetTop are both relative to that, should work great.

    This will still leave us with cases where an element is in a shadow tree and the only possible offset parents are outside the shadow. That leaks in the opposite way - info leaking into the shadow. Is that ok? I know this happens with CSS inheritance and such, but I dunno how strict we are about element refs leaking in.

  2. rniwa commented on Jun 6, 2016

    @rniwa

    I think it's okay for shadow tree to access to elements outside the shadow tree since that's already possible via ShadowRoot.prototype.host as well as HTMLSlotElement.prototype.assignedNodes().

  3. tabatkins commented on Jun 6, 2016

    @tabatkins
    Member

    In that case, 👍 to the idea.

  4. hayatoito commented on Jun 7, 2016

    @hayatoito
    MemberAuthor

    Okay. We can fix this by using "unclosed node" (https://dom.spec.whatwg.org/#concept-unclosed-node)

    https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetparent

    Return the nearest ancestor element of the element for which at least one of the following is true and terminate this algorithm if such an ancestor is found:

    A quick fix can be:

    Return the nearest ancestor unclosed element of the element for which at least one of the following is true and terminate this algorithm if such an ancestor is found:

    Let me send a PR.

  5. added 3 commits that reference this issue on Jun 7, 2016
    97781e3
    ec0bb47
    6da67c3
  6. added a commit that references this issue on Jun 14, 2016
    06fb597
  7. platosha commented on Jun 14, 2016

    @platosha

    Now with the fix merged, what offsetParent would return for a node that has a container block with position: fixed inside a closed shadow root?

    Considering the following code, what would be in the log output? Is it still possible for the node C to discover that it is inside a fixed container?

    var A = document.createElement('node-a');
    document.body.appendChild(A);
    
    var shadowRoot = A.attachShadow({mode: 'closed'});
    
    var B = document.createElement('node-b');
    shadowRoot.appendChild(B);
    B.style.position = 'fixed';
    
    var slot = document.createElement('slot');
    B.appendChild(slot);
    
    var C = document.createElement('node-c');
    A.appendChild(C);
    
    console.log(C.offsetParent.localName);
  8. rniwa commented on Jun 15, 2016

    @rniwa

    Node C's offsetParent will be offsetParent of B.

  9. platosha commented on Jun 15, 2016

    @platosha

    @rniwa then it looks like it's now completely impossible to discover for the node C that it's inside a fixed container.

    We have a component that should behave differently inside a fixed container. So we use a fixed container detection. Previously, it was safe to walk through .offsetParents chain and check their computed styles. Now the detection is broken.

    Aside from that, offsetParent has another use case: for an absolutely positioned node it points to the containing block. This is also broken here. In my example, if I assign position: absolute to the node C, its containing block will be the node B, but offsetParent will be null.

  10. rniwa commented on Jun 15, 2016

    @rniwa

    No, offsetParent would point to B's containing block instead and offsetLeft and offsetTop would be adjusted accordingly.

  11. platosha commented on Jun 16, 2016

    @platosha

    @rniwa node B’s containing block is the viewport, since it is fixed. I assume, that in case when the node’s containing block is the viewport, offsetParent is null. So, according to your answer, it will be null. Though to my understanding of the spec, it should be document.body.

    Anyways, it doesn't really matter for this case if offsetParent of C is document.body or null. Neither of these variants allow to find out that C's positioning is actually affected by B. Neither of these variants allow to find out that the C is inside a fixed container.

  12. 44 remaining items

  13. rniwa commented on Nov 25, 2019

    @rniwa

    Hm... looks like Blink still hasn't implemented this behavior?

    @rakina, @tkent-google: Can someone from Blink side follow up on this? We're getting bug reports like https://bugs.webkit.org/show_bug.cgi?id=204195

  14. tkent-google commented on Nov 26, 2019

    @tkent-google
    Contributor

    @rniwa I contacted the current owner of Shadow DOM in Chromium project about this issue.

    Here is a tracking issue: https://bugs.chromium.org/p/chromium/issues/detail?id=920069

  15. added a commit that references this issue on Apr 24, 2021
  16. mfreed7 commented on Apr 20, 2023

    @mfreed7
    Contributor

    The Chromium bug has been fixed. Seems like this issue can be closed now?

  17. added a commit that references this issue on Sep 16, 2025
  18. trusktr commented on Dec 3, 2025

    @trusktr
    Member

    I hadn't ran into this before, but now that I've realized it (after a decade), I realize that offset values in the presence of a ShadowRoot are effectively useless because of this.

    Typically offset values are used to calculate positioning relative to a CSS layout, but this destroys that ability in the presence of a ShadowRoot when that layout is inside the ShadowRoot, especially for slotted children that the consumer of a custom element controls.

    Much like Element.shadowRoot, it would have been a ton better to only make offsetParent be null, while keeping numerical properties like offsetTop producing the same values, if we really thought the "encapsulation" was actually helpful (ten years later, and the best practice is to avoid closed shadow roots to begin with, and they're not actually secure without going through very great lengths).

    Here's an example without Shadow DOM that shows expected offset values:

    https://jsbin.com/piyisidace/edit?html,console,output

    Here's an example with ShadowDOM showing values that are hard to make sense of:

    https://jsbin.com/fecuresaze/edit?html,console,output

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