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

Since my comment above just re-links back to the updated explainer, I thought it might be helpful to quickly summarize the [current state of the proposal](https://github.com/mfreed7/shadow-dom-selection#proposal) in a comment. I'm proposing that we:

1. Add `getComposedRange()` to `Selection`:

    ```javascript
      window.getSelection().getComposedRange({
          shadowRoots: [shadowRoot1, shadowRoot2, ...], // (Optionally) include selections that start/end within any of the provided `shadowRoot`s.
          selectionRoot: element, // (Optionally) scope the returned selection to live within the provided root node
      });
    ```

    In the above call, the `shadowRoots` argument allows the selection to be "revealed" within any of the provided `shadowRoot`s. For example, if the user selection starts or ends within a custom element that uses Shadow DOM, and the developer would like to retrieve that selection endpoint (within the shadow root), this parameter can be used to "unlock" it. If the selection starts/ends within a **non-provided** shadow root, the return value will make it appear that the **entire** shadow host is selected.

    The `selectionRoot` argument "scopes" the selection, so that the returned selection endpoints are **always** descendants of `element`. This is meant to be used, for example, by a rich text editor custom element that wants to manage selections within it, but doesn't want to know about or have to deal with selections outside the component. This **also** implicitly adds any containing `shadowRoot`s to the `shadowRoots` parameter, so that selections are always available, even when the custom element is contained within an ancestor shadow root.

2. Update the existing `Selection` APIs:

   The rest of the `Selection` object will stay the same, except that cross-shadow-root selections will now be allowed. For example, when calling `window.getSelection().setBaseAndExtent(node1,offset1,node2,offset2)`, the `node1` and `node2` endpoints will now be able to live in different shadow trees. Previously, this was not supported behavior.


Again, see [the full proposal](https://github.com/mfreed7/shadow-dom-selection#proposal) for all the details. But I hope this summary might help the conversation and feedback.


-- 
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-965900820

Received on Thursday, 11 November 2021 01:06:55 UTC