whatwg / dom

DOM Standard
https://dom.spec.whatwg.org/
Other
1.58k stars 295 forks source link

Extension only API to query across shadow trees #1321

Open rniwa opened 3 weeks ago

rniwa commented 3 weeks ago

What problem are you trying to solve?

Certain browser extensions need to query elements across shadow trees in order to implement features such as autofill.

What solutions exist today?

Chrome exposes chrome.dom.openOrClosedShadowRoot. Firefox exposes element.openOrClosedShadowRoot. Safari has collectMatchingElementsInFlatTree defined in window for Safari's autofill implementation.

How would you solve it?

Ideally, we'd come up with an interoperable API for browser extensions that work across browsers.

Anything else?

No response

oliverdunk commented 3 weeks ago

To add some more context here, we recently had some discussions about this in the WebExtensions Community Group. While we could add an API in the extension namespace (e.g, browser.dom.querySelector) there was alignment across the extension teams from Chrome, Firefox and Safari that we would prefer to put this closer to the web platform.

Based on previous discussions about other capabilities, I could imagine this being a new API or an { includeShadowRoots: true } option on an existing method (e.g, querySelector).

That doesn't mean this has to be exposed to all sites - we could still allow the user agent to prevent anything other than an extension from calling it / setting the new flag.

rniwa commented 3 weeks ago

That doesn't mean this has to be exposed to all sites - we could still allow the user agent to prevent anything other than an extension from calling it / setting the new flag.

We most certainly need to do that. We don't want to expose this capability to regular scripts.

xeenon commented 3 weeks ago

Yes, this should only be exposed to isolated worlds of extensions, not the main world for normal page scripts.

Joejoshara commented 2 weeks ago
annevk commented 2 weeks ago

Perhaps we could add something like [Exposed=Extensions] to IDL and have a corresponding check available on environment settings objects (when we cannot rely on IDL alone)?

That still leaves the question of what exact API shape we want here, but would at least address the exposure question and present a path for other APIs of similar nature.

(It would definitely increase the scope of what specifications have covered to date, but it's probably reasonable given the focus on interoperable extensions code.)

rniwa commented 2 weeks ago

So in terms of the actual API shape, I'm thinking that we want some kind of a way to query across shadow boundaries because iterating over each element and checking the existence of a shadow root is rather expensive. Maybe this could be a new option to querySelector & querySelectorAll? e.g. querySelector(':host', {composed: true}).

alice commented 2 weeks ago

It might be good to check in with folks writing accessibility auditing tools who have been asking for this type of API for years, too. querySelector[All] might be sufficient, but it might be nice to have an option for TreeWalker as well?

sorvell commented 1 week ago

@rniwa

So in terms of the actual API shape, I'm thinking that we want some kind of a way to query across shadow boundaries because iterating over each element and checking the existence of a shadow root is rather expensive

It would be great if this API was not limited to extensions (although maybe they could have exclusive access to closed roots?).

It's very useful for testing or any operations that need to act deeply on shadow roots like styling.

Some related issues:

rniwa commented 1 week ago

Please file a separate issue to track a proposal for non-extension use cases. This issue is specifically about adding an API for extensions.