You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[cssom-view] HTMLElement.prototype.offsetParent leaks a node inside a shadow tree #159
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.
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().
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:
@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.
@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.
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:
Activity
tabatkins commentedon Jun 1, 2016
I support @rniwa's answer - walking up until you find a valid (non-leaky)
offsetParent, then making sure thatoffsetLeftandoffsetTopare 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.
rniwa commentedon Jun 6, 2016
I think it's okay for shadow tree to access to elements outside the shadow tree since that's already possible via
ShadowRoot.prototype.hostas well asHTMLSlotElement.prototype.assignedNodes().tabatkins commentedon Jun 6, 2016
In that case, 👍 to the idea.
hayatoito commentedon Jun 7, 2016
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
A quick fix can be:
Let me send a PR.
[css-omview] Fix w3c#159. Do not leak a closed node via offsetParent
[css-omview] Fix w3c#159. Do not leak a closed node via offsetParent
[css-omview] Fix w3c#159. Do not leak a closed node via offsetParent
[cssom-view] Fix #159. Do not leak a closed node via offsetParent (#169)
platosha commentedon Jun 14, 2016
Now with the fix merged, what
offsetParentwould return for a node that has a container block withposition: fixedinside 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?
rniwa commentedon Jun 15, 2016
Node C's
offsetParentwill beoffsetParentof B.platosha commentedon Jun 15, 2016
@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,
offsetParenthas another use case: for an absolutely positioned node it points to the containing block. This is also broken here. In my example, if I assignposition: absoluteto the node C, its containing block will be the node B, butoffsetParentwill be null.rniwa commentedon Jun 15, 2016
No,
offsetParentwould point to B's containing block instead andoffsetLeftandoffsetTopwould be adjusted accordingly.platosha commentedon Jun 16, 2016
@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,
offsetParentis null. So, according to your answer, it will be null. Though to my understanding of the spec, it should bedocument.body.Anyways, it doesn't really matter for this case if
offsetParentof C isdocument.bodyornull. 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.44 remaining items
rniwa commentedon Nov 25, 2019
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
tkent-google commentedon Nov 26, 2019
@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
Update offsetParent behavior in shadow trees
Update offsetParent behavior in shadow trees
Update offsetParent behavior in shadow trees
Update offsetParent behavior in shadow trees
Update offsetParent behavior in shadow trees
Bug 1700509 [wpt PR 28201] - Update offsetParent behavior in shadow t…
Polyfill legacy offsetParent behavior
fix: apply offsetParent polyfill for Chrome 109+ (#6520)
mfreed7 commentedon Apr 20, 2023
The Chromium bug has been fixed. Seems like this issue can be closed now?
Bug 1514074 - Retarget results of offset* DOM APIs. r=smaug
Bug 1700509 [wpt PR 28201] - Update offsetParent behavior in shadow t…
trusktr commentedon Dec 3, 2025
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 makeoffsetParentbenull, while keeping numerical properties likeoffsetTopproducing 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