← Back to release summary

Fix Selection isCollapsed in Shadow DOM

Category
DOM
Type
No developer-visible change
Status
Enabled by default (Chrome Enabled by default)
Intent stage
None

Summary

Selection isCollapsed should return true if and only if the anchor and focus are the same. This should be true whether the selection starts/ends inside a light or a shadow tree. Currently, the Chrome implementation returns true if selection's anchor node is in a shadow tree, even if the selection itself is not collapsed. We fix this by removing the erroneous shadow tree check.

Motivation

The Chromium issue was opened in 2015 and has 13 stars. Many web developers have mentioned this bug being a pain point and having to work around it. For example, [1] and [2]. While Shadow DOM and Selection across shadow DOM is still being defined, the spec is clear that a selection should return isCollapsed true if and only if the anchor and focus nodes are the same. [1] https://github.com/ankitects/anki/blob/b1bf1c3141863585989d5d541a73edb0265f866a/ts/lib/tslib/cross-browser.ts#L33-L40 [2] https://stackoverflow.com/questions/68882638/why-is-selection-iscollapsed-always-true-in-a-shadow-dom

Standards & signals

Samples: https://codepen.io/Di-Zhang/pen/jOjdeoX

View on chromestatus.com