Re: [WICG/webcomponents] Selection APIs for Shadow DOM (#79)

> What is a concrete use case for doing this?

I guess the most basic one: click a button that highlights whatever the user has selected with some color. To do that, you'd need to provide all shadow roots on the page, otherwise you'll get it wrong. I.e. if a user has selected half-way through some text inside a shadow root, you'll change the color for the entire shadow root contents, unless you pre-provide that shadow root in the call to `getComposedRange()`.

What's a concrete use case that would be "confused" by receiving `Node`s that live in any (open) shadow root?


> The issue isn't so much about leaks in the case of open shadow roots but more about ergonomics & separations of concerns. If the only way to get the range of selection when its end points are in a shadow root is to get selection end points in any shadow root, then you're effectively back to not having shadow root encapsulation at all. You'd have to constantly check against end points to see if it's in your shadow root or not.

I suppose the counterpoint is that for pages that want to make extensive use of shadow dom, this API shape gets really cumbersome. E.g.:

```html
<body>
<my-app></my-app>
</body>
```

In this case, assuming `<my-app>` uses shadow DOM, `getComposedSelection(allRoots)` just doesn't work at all unless you pre-walk the tree and provide everything in `allRoots`.

I'm curious why you think shadow dom is special here. The user is **already able** to select anything on the page, including inside deeply nested shadow DOM. The existing `getRangeAt()` already returns **any `Node`** on the page, not just inside some sub-tree. This new API is meant to extend that current behavior to `Node`s inside shadow trees. If you're writing some code that doesn't understand Shadow DOM, great, just keep using `getRangeAt()`.

> The question is: would this use case be possible to achieve by using proposed getComposedRange(rootDocument)?
Please correct me if I'm wrong, but the answer seems to be "yes". So I'm fine with what @rniwa suggests.

Sorry, I'm a bit unclear on the specific use case you're asking about here. You should be able to use **either version** of `getComposedRange()` to get the composed range. The difference is what arguments you need to pre-provide.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/79#issuecomment-941674794

Received on Tuesday, 12 October 2021 22:28:34 UTC