Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose functions to force the polyfill and make not exception with WC polyfill #124

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 48 additions & 11 deletions src/element-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,49 @@ export function isElementInternalsSupported(): boolean {
].every(prop => prop in featureDetectionElement.internals);
}

if (!isElementInternalsSupported()) {
let hasElementInternalsPolyfillBeenApplied = false;
let hasCustomStateSetPolyfillBeenApplied = false;

/**
* Forcibly applies the polyfill for CustomStateSet.
*
* https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet
*/
export function forceCustomStateSetPolyfill(attachInternals?: HTMLElement['attachInternals']) {
if (hasCustomStateSetPolyfillBeenApplied) {
return;
}

hasCustomStateSetPolyfillBeenApplied = true;
window.CustomStateSet = CustomStateSet;

if (attachInternals) {
HTMLElement.prototype.attachInternals = function(...args) {
const internals = attachInternals.call(this, args);
internals.states = new CustomStateSet(this);
return internals;
}
}
}

/**
* Forcibly applies the polyfill for ElementInternals. Useful for situations
* like Chrome extensions where Chrome supports ElementInternals, but the
* CustomElements polyfill is required.
*
* https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals
*
* @param forceCustomStateSet Optional: when true, forces the
* [CustomStateSet](https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet)
* polyfill as well.
*/
export function forceElementInternalsPolyfill(forceCustomStateSet = true) {
if (hasElementInternalsPolyfillBeenApplied) {
return;
}

hasElementInternalsPolyfillBeenApplied = true;

if (typeof window !== 'undefined') {
/** @ts-expect-error: we need to replace the default ElementInternals */
window.ElementInternals = ElementInternals;
Expand Down Expand Up @@ -391,15 +433,10 @@ if (!isElementInternalsSupported()) {
patchFormPrototype();
}

if (typeof window !== 'undefined' && !window.CustomStateSet) {
window.CustomStateSet = CustomStateSet;
}
} else if (typeof window !== 'undefined' && !window.CustomStateSet) {
window.CustomStateSet = CustomStateSet;
const attachInternals = HTMLElement.prototype.attachInternals;
HTMLElement.prototype.attachInternals = function(...args) {
const internals = attachInternals.call(this, args);
internals.states = new CustomStateSet(this);
return internals;
if (
forceCustomStateSet ||
(typeof window !== "undefined" && !window.CustomStateSet)
) {
forceCustomStateSetPolyfill();
}
}
28 changes: 27 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { ElementInternals } from './element-internals.js';
import {
ElementInternals,
forceCustomStateSetPolyfill,
forceElementInternalsPolyfill,
isElementInternalsSupported,
} from './element-internals.js';
import { CustomStateSet } from './CustomStateSet.js';
import './element-internals.js';
import { IElementInternals } from './types.js';
export * from './types.js';
export {
forceCustomStateSetPolyfill,
forceElementInternalsPolyfill,
} from './element-internals.js';

declare global {
interface Window {
Expand All @@ -18,3 +27,20 @@ declare global {
attachInternals(): ElementInternals&IElementInternals;
}
}

// Deteermine whether the webcomponents polyfill has been applied.
const isCePolyfill = !!(
customElements as unknown as {
polyfillWrapFlushCallback: () => void;
}
).polyfillWrapFlushCallback;

// custom elements polyfill is on. Do not auto-apply. User should determine
// whether to force or not.
if (!isCePolyfill) {
if (!isElementInternalsSupported()) {
forceElementInternalsPolyfill(false);
} else if (typeof window !== "undefined" && !window.CustomStateSet) {
forceCustomStateSetPolyfill(HTMLElement.prototype.attachInternals);
}
}
13 changes: 11 additions & 2 deletions src/mutation-observers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,19 @@ export function fragmentObserverCallback(mutationList: MutationRecord[]): void {
* @param fragment {DocumentFragment}
*/
export const deferUpgrade = (fragment: DocumentFragment) => {
const observer = new MutationObserver(fragmentObserverCallback)
const observer = new MutationObserver(fragmentObserverCallback);
// is this using shady DOM and is not actually a DocumentFragment?
if (
window?.ShadyDOM?.inUse &&
(fragment as unknown as { mode: string }).mode &&
(fragment as unknown as { host: HTMLElement | null }).host
) {
// using shady DOM polyfill. Best to just observe the host.
fragment = (fragment as ShadowRoot).host as unknown as DocumentFragment;
}
observer.observe?.(fragment, { childList: true });
documentFragmentMap.set(fragment, observer);
};
};

export const observer = mutationObserverExists() ? new MutationObserver(observerCallback) : {} as MutationObserver;
export const observerConfig: MutationObserverInit = {
Expand Down