Description
There are requests from web developers who need faster access to all trees under a document, e.g. to find out specific elements that matches some criteria. Now you have to run querySelector()
recursively for each document and its descendant shadow roots. You don't have any faster way than iterating all elements to see if their .shadowRoot
is null or not.
A popular use case that I have heard is for testing, and one possibility is to add a new interface to webdriver. But developers also demand some fast solution usable on the web, even if it's applicable to open shadow roots. As adding a new combinator to CSS selector was strongly opposed, it is not an option at least for now.
Other possibilities so far:
- A new pseudo class (e.g.
:has-shadow-root
) that matches a shadow host - A new flag or filter for TreeWalker API for matching a shadow host or a shadow root.
- A new API that can accept limited set of selector (e.g. a simple selector) against all elements in all trees
First two options may achieve the same goal, though no one yet has submitted a concrete spec proposal. The last option was from Apple, but no standardization effort so far.
I once ran an experiment of performance, see WICG/webcomponents#78 (comment). From the results, any of the above once implemented native, would have significant speedup.
Background history: In the Shadow DOM V0 era Google proposed /deep/
combinator (later renamed to >>>
) in CSS selector and found that the usage in stylesheet was problematic for performance, and restricted the usage only in querySelector()
(aka snapshot or static profile). But it did not reach agreement with other vendors (see WICG/webcomponents#78). The rationale we had in selector was that it gives both performance and flexibility of CSS selectors, at the cost of implementation complexity.
References:
https://bugs.chromium.org/p/chromium/issues/detail?id=829713
WICG/webcomponents#78
w3c/csswg-drafts#640
https://www.chromestatus.com/feature/5045542597951488