- styles.ts: adoptStyles(root, css) adopts a constructed stylesheet into a shadow root or document (not governed by a page's CSP, parsed once, stays with the root when its host is moved); <style> fallback where constructable sheets are missing or refused. - UI kit: the kit's shadow root and each dialog's `css` use it (dialog CSS goes with the dialog). - Plugin API: api.adoptCSS(root, css) and scope.adoptCSS (removed on dispose); injectCSS keeps its <style> and, when a CSP leaves it without a sheet, applies the same rules as an adopted sheet. - tests/styles.test.ts.
83 lines
3.4 KiB
TypeScript
83 lines
3.4 KiB
TypeScript
// Styles for roots the runtime (or a plugin) creates: a constructable stylesheet adopted by the
|
|
// shadow root (or document) instead of a `<style>` element.
|
|
//
|
|
// Why: a site's Content-Security-Policy governs `<style>` elements (a `style-src` with a nonce or
|
|
// hash and no 'unsafe-inline' refuses them, in shadow roots too), but not constructed sheets
|
|
// (`new CSSStyleSheet()` + `replaceSync`), which are also parsed once instead of per element and
|
|
// stay adopted when their host is moved to another parent in the same document (a modal
|
|
// re-rendered by the site, a card lifted into the top layer). Engines without constructable
|
|
// sheets (or a root from another realm that refuses the sheet) get the `<style>` element.
|
|
|
|
export type RootStyles = {
|
|
/** The live sheet (insertRule/deleteRule work on it), or null when nothing could be parsed. */
|
|
readonly sheet: CSSStyleSheet | null;
|
|
/** True when the sheet is adopted (CSP-proof); false for the `<style>` fallback. */
|
|
readonly adopted: boolean;
|
|
/** Replaces the rules. */
|
|
update(css: string): void;
|
|
/** Takes the rules off the root (idempotent). */
|
|
remove(): void;
|
|
};
|
|
|
|
type StyleRoot = (ShadowRoot | Document) & { adoptedStyleSheets?: CSSStyleSheet[] };
|
|
|
|
const DOCUMENT_NODE = 9;
|
|
const isDocument = (root: StyleRoot): root is Document & StyleRoot => root.nodeType === DOCUMENT_NODE;
|
|
const documentOf = (root: StyleRoot): Document => (isDocument(root) ? root : (root as ShadowRoot).ownerDocument);
|
|
|
|
/** A sheet built in `root`'s own realm (a sheet from another document cannot be adopted). */
|
|
function construct(root: StyleRoot, css: string): CSSStyleSheet | null {
|
|
if (!root.adoptedStyleSheets) return null;
|
|
const Sheet = (documentOf(root).defaultView as (Window & typeof globalThis) | null)?.CSSStyleSheet;
|
|
if (typeof Sheet !== 'function' || typeof Sheet.prototype.replaceSync !== 'function') return null;
|
|
try {
|
|
const sheet = new Sheet();
|
|
sheet.replaceSync(css);
|
|
return sheet;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Styles `root` with `css`. Adopted sheets come after the root's `<style>` elements in the cascade
|
|
* (like a `<style>` appended last), and several calls stack in call order.
|
|
*/
|
|
export function adoptStyles(root: ShadowRoot | Document, css: string): RootStyles {
|
|
const target = root as StyleRoot;
|
|
const sheet = construct(target, css);
|
|
if (sheet) {
|
|
let on = false;
|
|
const attach = () => {
|
|
try {
|
|
target.adoptedStyleSheets = [...(target.adoptedStyleSheets ?? []), sheet];
|
|
on = true;
|
|
} catch { on = false; }
|
|
};
|
|
attach();
|
|
if (on) {
|
|
return {
|
|
sheet,
|
|
adopted: true,
|
|
update(next) { try { sheet.replaceSync(next); } catch { /* Keeps the last good rules. */ } },
|
|
remove() {
|
|
if (!on) return;
|
|
on = false;
|
|
try { target.adoptedStyleSheets = (target.adoptedStyleSheets ?? []).filter(item => item !== sheet); } catch { /* Root gone. */ }
|
|
},
|
|
};
|
|
}
|
|
}
|
|
// Fallback: a `<style>` element as the root's last child (a document: its <head>).
|
|
const doc = documentOf(target);
|
|
const style = doc.createElement('style');
|
|
style.textContent = css;
|
|
const parent: Node = isDocument(target) ? (target.head ?? target.documentElement) : target;
|
|
parent.appendChild(style);
|
|
return {
|
|
get sheet() { return style.sheet; },
|
|
adopted: false,
|
|
update(next) { style.textContent = next; },
|
|
remove() { style.remove(); },
|
|
};
|
|
}
|