Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query elements within shadow DOM from outside in Dart?

How can I select nodes within shadow DOM? Consider the following example:

structure of "unshadowed" DOM

<app-element>   #shadow-root     <h2></h2>     <content>       #outside shadow       <h2></h2>     </content>     <ui-button>       #shadow-root         <h2></h2>   </ui-button> </app-element> 

index.html

<body> <app-element>   <!-- OK: querySelect('app-element').querySelect('h2') -->   <!-- OK: querySelect('app-element h2') -->   <!-- There is no problem to select it -->   <h2>app-element > content > h2</h2> </app-element> </body> 

templates.html

<polymer-element name="ui-button" noscript>   <template>     <!-- FAIL: querySelect('app-element::shadow ui-button::shadow h2') -->     <h2>app-element > ui-button > h2</h2>   </template> </polymer-element>  <polymer-element name="app-element" noscript>   <template>     <!-- FAIL: querySelect('app-element::shadow').querySelect('h2') -->     <!-- FAIL: querySelect('app-element::shadow h2') -->     <!-- FAIL: querySelect('app-element').shadowRoot.querySelect('h2') -->     <h2>app-element > h2</h2>     <content></content>     <ui-button></ui-button>   </template> </polymer-element> 

In comments like "OK: querySelect()" I show selectors I've tried to run from outside any shadowed DOM.

I've already read the following article: http://www.html5rocks.com/en/tutorials/webcomponents/shadowdom-201/?redirect_from_locale=ru and based on the fact that it was said in the article, query like: document.querySelector('app-element::shadow h2'); in JS should work as expected. However in Dart it doesn't work.

What do I wrong?

like image 513
Timur Fayzrakhmanov Avatar asked Apr 14 '15 14:04

Timur Fayzrakhmanov


People also ask

How do I access shadow DOM elements?

You won't be able to access the shadow DOM from the outside, If you attach a shadow root to a custom element with mode: closed set. We can only access the shadow DOM by the reference returned by attachShadow and it is probably hidden inside a class. Browser-native shadow trees are closed. There's no way to access them.

Can we access the element which is inside closed shadow DOM?

Any code is able to access the shadow tree of elem . "closed" – elem. shadowRoot is always null . We can only access the shadow DOM by the reference returned by attachShadow (and probably hidden inside a class).

What selector works for shadow DOM elements?

The :host selector allows to select the shadow host (the element containing the shadow tree).

How do you test shadow DOM?

Create a new project. Go to File > New > Project. Here, we name the project Shadow DOM Testing. In the demo website, navigate to the search bar, right-click > Inspect.


1 Answers

Pseudo selector ::shadow and combinator /deep/ doesn't work on firefox.

Use .shadowRoot

var shadowroot = app-element.shadowRoot; shadowroot.querySelector('h2'); 
like image 103
Aniruddha Avatar answered Sep 19 '22 16:09

Aniruddha