Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@onsenui/custom-elements
Advanced tools
A polyfill for the custom elements v1 spec.
Include custom-elements.min.js
at the beginning of your page, before any code that
manipulates the DOM:
<script src="custom-elements.min.js"></script>
npm install
npm run build
(Or, npm i && gulp
, if gulp is installed globally.)
npm run test
(Or, wct
, if installed
globally.)
API which might trigger custom element reactions in the DOM
and HTML specifications are marked with the
CEReactions
extended attribute.
adoptedCallback
is not supported.CEReactions
extended attribute.
Element
for id
, className
, and slot
.DOMTokenList
(element.classList
)NamedNodeMap
(element.attributes
)Attr
(element.attributes.getNamedItem('attr-name')
)new
operator on a custom element constructor. This
means there is no way to know when a call to a constructor has begun or
finished.ParentNode
and ChildNode
interfaces do not support
DocumentFragment
s as arguments.constructor
which is that constructor.
F
, F.prototype.constructor === F
.
If you replace the prototype of your constructor F
, you must make sure
that F.prototype.constructor === F
remains true. Otherwise, the polyfill
will not be able to create or upgrade your custom elements.The custom elements v1 spec is not compatible with ES5 style classes. This means
ES2015 code compiled to ES5 will not work with a native implementation of Custom
Elements.[0] While it's possible to force the custom elements polyfill to be
used to workaround this issue (by setting (customElements.forcePolyfill = true;
before loading the polyfill), you will not be using the UA's native
implementation in that case.
Since this is not ideal, we've provided an alternative: native-shim.js. Loading this shim minimally augments the native implementation to be compatible with ES5 code. We are also working on some future refinements to this approach that will improve the implementation and automatically detect if it's needed.
[0] The spec requires that an element call the HTMLElement
constructor.
Typically an ES5 style class would do something like HTMLElement.call(this)
to
emulate super()
. However, HTMLElement
must be called as a constructor and
not as a plain function, i.e. with Reflect.construct(HTMLElement, [], MyCEConstructor)
,
or it will throw.
By default, the polyfill uses a MutationObserver
to learn about and upgrade
elements in the main document as they are parsed. This MutationObserver
is
attached to document
synchronously when the script is run.
MutationObserver
earlier before loading the polyfill, that
mutation observer will not see upgraded custom elements.Note: Using polyfillWrapFlushCallback
disconnects this MutationObserver
.
customElements.polyfillWrapFlushCallback
tl;dr: The polyfill gets slower as the size of your page and number of custom
element definitons increases. You can use polyfillWrapFlushCallback
to prevent
redundant work.
To avoid a potential memory leak, the polyfill does not maintain a list of upgrade
candidates. This means that calling customElements.define
causes a synchronous,
full-document walk to search for elements with localName
s matching the new
definition. Given that this operation is potentially expensive and, if your page
loads many custom element definitions before using any of them, highly redundant,
an extra method is added to the CustomElementRegistry
prototype -
polyfillWrapFlushCallback
.
polyfillWrapFlushCallback
allows you to block the synchronous, full-document
upgrade attempts made when calling define
and perform them later. Call
polyfillWrapFlushCallback
with a function; the next time customElements.define
is called and a full-document upgrade would happen, your function will be called
instead. The only argument to your function is another function which, when
called, will run the full-document upgrade attempt.
For example, if you wanted to delay upgrades until the document's ready state
was 'complete'
, you could use the following:
customElements.polyfillWrapFlushCallback(function(flush) {
if (document.readyState === 'complete') {
// If the document is already complete, flush synchronously.
flush();
} else {
// Otherwise, wait until it is complete.
document.addEventListener('readystatechange', function() {
if (document.readyState === 'complete') {
flush();
}
});
}
});
Once your wrapper function is called (because the polyfill wants to upgrade the document), it will not be called again until you have triggered the full-document upgrade attempt. If multiple definitions are registered before you trigger upgrades, all of those definitions will apply when you trigger upgrades - don't call the provided function multiple times.
Promises returned by customElements.whenDefined
will not resolve until a
full-document upgrade attempt has been performed after the given local name
has been defined.
let flush;
customElements.polyfillWrapFlushCallback(f => flush = f);
const p = customElements.whenDefined('c-e', () => console.log('c-e defined'));
customElements.define('c-e', class extends HTMLElement {});
// `p` is not yet resolved; `flush` is now a function.
flush(); // Resolves `p`.
You can't remove a callback given to polyfillWrapFlushCallback
. If the
condition your callback was intended to wait on is no longer important, your
callback should call the given function synchronously. (See the
document.readyState
example above.)
Calling polyfillWrapFlushCallback
disconnects the MutationObserver
watching
the main document. This means that you must delay until at least
document.readyState !== 'loading'
to be sure that all elements in the main
document are found (subject to exceptions mentioned in the section above).
You can call polyfillWrapFlushCallback
multiple times, each function given
will automatically wrap and delay any previous wrappers:
customElements.polyfillWrapFlushCallback(function(flush) {
console.log('added first');
flush();
});
customElements.polyfillWrapFlushCallback(function(flush) {
console.log('added second');
setTimeout(() => flush(), 1000);
});
customElements.define('c-e', class extends HTMLElement {});
// 'added second'
// ~1s delay
// 'added first'
// The document is walked to attempt upgrades.
FAQs
HTML Custom Elements Polyfill
We found that @onsenui/custom-elements demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.