Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To what degree can the Shadow DOM be emulated using a polyfill?

Tags:

Can the Shadow DOM W3C draft be polyfilled in JavaScript by providing custom functions for searching and traversing the DOM? Has this been done? The attempts I've found have been rather meek shims and appear not to make much effort to emulate the spec.

I appreciate that this is not an easy task, but surely someone has given it a thorough consideration?

like image 304
Patricia Brothers Avatar asked Mar 20 '13 00:03

Patricia Brothers


People also ask

How does the shadow DOM work?

Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree — this shadow DOM tree starts with a shadow root, underneath which you can attach any element, in the same way as the normal DOM.

Does EDGE support shadow DOM?

ShadowDOM will be supported in IE and Edge(I don't have other older browsers so only tested in IE and Edge).

How do you find shadow DOM?

To identify Shadow DOM:Open Developer tools (press the shortcut keys Fn+F12). On the Elements tab, expand the <body> element and the first element inside the <body> element and notice the #shadow-root line.

Is shadow DOM supported?

Shadow DOM (V1) on Chrome is fully supported on 53-106, partially supported on None of the versions, and not supported on 4-52 Chrome versions.


1 Answers

I've been working on this exact problem for the last few months.

Bottom line there is a polyfill that works on evergreen browsers here https://github.com/webcomponents/polyfills/tree/master/packages/webcomponentsjs

^ polyfilling of CSS features like @host are not in there yet, coming soon

So, yes, it's a hard thing to polyfill, specifically because we have to invent secondary DOM trees. We tried to make it as user-friendly as possible, which required the use of a rather invasive wrapper technique.

In other words, if you div = document.createElement('div'), you get a thing that looks like a DIV and works like a DIV, but is actually a Wrapper object. The ultimate goal of course, is for your code to look the same whether it's running under the polyfill or under a native implementation.

It's not 100% bulletproof. In particular it's not possible for us to wrap document for you, so you have to do that yourself, somewhat like this:

wrap(document).querySelector(...)

Other than the document issue, the wrappers are intended to work transparently. This is all brand new, so I apologize for the lack of docs. We are working on that as we speak.

Please file issues if you have questions or problems, we love to get feedback. There is also an email channel for discussing this polyfill (and the other polyfills in that org) at [email protected].

I doubt this stuff is widely implemented

True, but it's in Chrome at least.

like image 135
Scott Miles Avatar answered Sep 27 '22 19:09

Scott Miles