dom icon indicating copy to clipboard operation
dom copied to clipboard

Proposal: new API to get text content across shadowRoot

Open woody-li opened this issue 7 months ago • 6 comments

What problem are you trying to solve?

Currently, we already have HTML-related API: getHTML, setHTML, etc. But there's no way to get text content across shadowRoot.

In some cases, we need to get real text content across element shadowRoots. Such as a customized tooltip, check content in the auto-test frame, etc.

What solutions exist today?

No response

How would you solve it?

How about adding a new API, like getText or getTextContent? Add an option like checkVisibility to deal with hidden elements. (The difference between innerText and textContent)

Anything else?

No response

woody-li avatar Jul 28 '25 03:07 woody-li

How would this be different from the textContent getter?

annevk avatar Jul 28 '25 06:07 annevk

@annevk You can not use testContent to get real text content across shadowRoot.

woody-li avatar Jul 29 '25 01:07 woody-li

You'll have to elaborate as this proposal is very much unclear to me.

annevk avatar Jul 29 '25 05:07 annevk

Perhaps you mean a new API like Node.prototype.getTextContent which takes an optional argument of shadow roots like getHTML?

rniwa avatar Jul 29 '25 06:07 rniwa

@rniwa Yeah, it is.

This proposal is meant to resolve how to get text content across shadowRoot. Both innerText and textContent cannot get text across shadow DOM.

For example:

<div id="box">
  // some other elements.
  <my-element>
    #shadow-fragment
        <span>Some test content</span>
  </my-element>
</div>

In some cases, I want to get the real text content of the box element. I have to query every element, check whether it's a custom element, and collect its inner elements again and again. Then, use all elements to map a list of text content.


This proposal is meant to create a new API like getTextContent, using it to get the real text content. And I think it should contain an options object argument. One option for deciding whether to get text across shadowRoot. One option is to get innerText or textContent.


This is my first proposal here. Please let me know if there's any problem. Thanks.

woody-li avatar Jul 29 '25 09:07 woody-li

Such as a customized tooltip, check content in the auto-test frame, etc.

Can you elaborate on the use cases, please? It's difficult to evaluate an API proposal without understanding what problem is being solved.

zcorpan avatar Aug 25 '25 13:08 zcorpan