This repo aims to provide a polyfill for Shadow DOM v1 that is as spec-complete and correct as possible.
What you will get if you use it right now:
- Shadow DOM polyfill
- Polyfills Shadow DOM APIs like
Element.prototype.attachShadow
andEvent.prototype.composedPath()
- Polyfills Event and CustomEvent constructors to allow for
composed
events - Augments DOM querying/traversal/manipulation APIs to account for Shadow DOM
- Augments
MutationObserver
to account for Shadow DOM
- Polyfills Shadow DOM APIs like
- Custom Elements polyfill
- Built in/around the Shadow DOM polyfill
- Shims HTMLElement and kin for browsers that have native support so transpiled and ES5-style classes
can work (i.e. via
var self = HTMLElement.call(this)
)
Please see the Caveats section below for details about certain APIs that are not polyfilled.
To help with testing for cross-browser support, this project is receiving tremendous help from BrowserStack.
Browser | Version | Notes |
---|---|---|
Chrome | 43 |
|
Safari | 9 |
|
Firefox | 34 | |
Edge | 13 |
|
Internet Explorer | 11 |
|
- Include
dist/shadow-dom.js
ordist/shadow-dom.min.js
before any other scripts. It wouldn't be wise to use theasync
attribute for the<script>
. Use thedefer
attribute with caution. - If you want to force the Shadow DOM polyfill to be used in browsers with native support for some reason,
set
window.forceShadowDomPolyfill = true
before the script is included. - If you want to force the Custom Elements polyfill to be used in browsers with native support for some reason,
set
window.forceCustomElementsPolyfill = true
before the script is included.
-
Benchmarks
There are no benchmarks yet. Correctness comes first, which means benchmarks come after test-verified functionality.
-
MutationObserver and garbage collection
MutationObserver is polyfilled to account for shadow boundaries, but Javascript does not provide any sort of weak reference, so you must be sure to
disconnect()
your MutationObserver instances whenever it is appropriate to do so. -
<slot>
and thedisplay
style propertyBrowsers with native Shadow DOM treat
<slot>
elements asdisplay: contents
by default which simply cannot be properly polyfilled (thinkdisplay: flex
,display: table
, etc.) With that in mind, you may want to adopt a practice of explicitly specifyingdisplay
properties for<slot>
elements. -
[CEReactions]
and reflected content attributesObject properties that are said to reflect a content attribute will not cause
attributeChangedCallback
to be invoked (for example, settingclassName
from script will not invoke theattributeChangedCallback
for theclass
attribute.) This is because the[CEReactions]
behavior for setters cannot be reasonably polyfilled for in Safari 9 and this project aims to provide a consistent level of support for its supported browsers. -
CSS Selectors
No attempt is made to polyfill any selectors in CSS, like
::slotted
,:host
, or:host-context
. These are also not polyfilled for APIs likematches
,querySelector
, and so forth. -
Obscure or low-priority DOM APIs
APIs like
Range
,NodeIterator
, andTreeWalker
are not addressed. They may be in the future but they are currently very low-priority.
This project is licensed under the MIT license. See the LICENSE file.