包详细信息

@webcomponents/shadydom

webcomponents35.8kBSD-3-Clause1.11.0

Shadow DOM polyfill

shady-dom, shadydom, shadow-dom, shadowdom

自述文件

ShadyDOM

ShadyDOM provides a shim for ShadowDOM V1. It is less correct but less intrusive and faster than the ShadowDOM Polyfill.

Usage

Usage of the shim is transparent when attachShadow is unavailable. Elements are patched as needed to report ShadowDOM correct dom information. Only dom tree accessors and mutation api is maintained. Some dom api (for example MutationObservers) is not shimmed.

To force ShadyDOM to be used even when native ShadowDOM is available, set the ShadyDOM = {force: true} in a script prior to loading the polyfill.

Example

<div id="host"></div>
<script>
  host.attachShadow({mode: 'open'});
  host.shadowRoot.appendChild(document.createElement('a'));
</script>

Building and Testing

For building and testing, first run

npm install
npm run build

To test, run npm test

Limitations

ShadyDOM distribution is asynchronous for performance reasons. This means that the composed dom will be available 1 microtask after the dom mutation occurs. For testing, ShadyDOM.flush may be called to force syncronous composition.

ShadowDOM compatible styling is not provided with the ShadyDOM shim. To shim ShadowDOM styling, use the shadycss shim.

On very old versions of Chrome and Safari, in rare cases ShadyDOM.patch(node) must be called to properly set textContent or innerHTML. On these browsers, element property accessors like firstChild and textContent cannot be modified on the element prototype and instances must be patched. This is automated but there are some cases that are not handled. This should only be an issue when setting textContent or innerHTML on a node inside a shadowRoot adds or removes a <slot> element. In this case, the shadowRoot will not render correctly. To workaround this, call ShadyDOM.patch(node) prior to setting textContent or innerHTML.

The APIs Document.querySelectorAll, Element.querySelectorAll, Node.childNodes, and ParentNode.children each return an Array instead of a NodeList or HTMLCollection.

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[1.11.0] - 2023-03-30

  • Add support for Element.toggleAttribute() (#541)
  • Update dependencies (#542)

[1.10.0] - 2022-10-20

  • Fix focus/blur events to be usable without wrapped addEventListener in noPatch mode. (#502)
  • Add type annotations for JSCompiler to eventPhase property descriptor. (#473)
  • Allow event listener options to be specified using a function in addition to an object. (#469)
  • The eventPhase property of events is now properly set to Event.AT_TARGET when events are re-targeted to hosts of shadowRoots. (#469)
  • Adds an opt-out for polyfilling element(s)FromPoint on document via setting ShadyDOM.useNativeDocumentEFP to true. (#472)
  • In browsers where EventTarget doesn't exist, XMLHttpRequest.prototype's EventTarget-like properties are now patched. In browsers where events' currentTarget is an own value property and Shady DOM needs to manually dispatch an event, Shady DOM will instead dispatch a new object having the original event as its prototype so that it can update currentTarget without modifying the built-in property descriptor. (#519)
  • Add ShadyDOM.querySelectorImplementation setting. (#517)
  • Fix Closure types of querySelector{,All} patches for internal conformance checks. (#526)

[1.9.0] - 2021-08-02

  • Add @this annotation to new elementFromPoint wrappers. (#464)
  • Adds element(s)FromPoint to document and shadowRoot (#463)
  • Add TS externs. (#457)
  • When EventTarget.prototype exists but is not in the prototype chain of window, polyfill installation breaks. (#416)

[1.8.0] - 2020-10-21

  • Implicitly coerce values passed to createTextNode to a string. (#401)
  • Add support for ChildNode APIs. (#390)
  • Add support for select ParentNode APIs. (#389)

[1.7.4] - 2020-07-20

  • Manually dispatch events for connected, unassigned targets, if preferPerformance is not enabled. (#332)
  • Remove outdated references to customElements.nativeHTMLElement (#234)
  • Add README warning about polyfill ordering when using noPatch (#179)

[1.7.3] - 2020-03-16

  • Maintenance release (no user-facing changes)

[1.7.2] - 2020-02-26

  • Correctly detect slot.insertBefore as requiring shadowRoot distribution (#267)
  • Gather event properties from the correct prototypes (#263)

[1.7.1] - 2020-01-08

  • Fix case where user creates a global customElements object for the sake of passing flags to the polyfill by only modify customElements at load if it's a CustomElementRegistry (#235)

[1.7.0] - 2019-11-12

  • Add on-demand patching mode (#189)
  • Avoid IE11 bug that breaks Closure's WeakMap polyfill (#228)