Get selection range of shadow DOM tree

26 views Asked by At

If we have two nodes outer and inner, each one of them has shadowRoot

      const inner = document.createElement("span");
      inner.attachShadow({ mode: "open" }).innerHTML = "inner";

      const outer = document.createElement("span");
      outer.attachShadow({ mode: "open" }).append("outer ", inner);

      document.body.append(outer);

enter image description here

Then we select a text exists in both

enter image description here

If we try to get selected text

window.getSelection().toString()

We get 'er ' which is incorrect! Even if try to get selection on the shadowRoot of inner span it not correct.

  • How to get selection is this case?
0

There are 0 answers