From c126ee72f59ee844032dcb8831fbb3fd763d4fef Mon Sep 17 00:00:00 2001 From: "Nichols, Kieran" Date: Tue, 22 Oct 2024 10:49:06 -0400 Subject: [PATCH] fix(readoptStyles): only attempt to readopt constructed stylesheets if the element instance has an `ownerDocument.defaultView` value set --- src/custom-elements/component-utils.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/custom-elements/component-utils.ts b/src/custom-elements/component-utils.ts index b90a792..eb256e0 100644 --- a/src/custom-elements/component-utils.ts +++ b/src/custom-elements/component-utils.ts @@ -218,22 +218,21 @@ export function setShadowStyles(componentInstance: T, sty } /** - * Reapplies styles to the shadow root of the provided element instance. This function was + * Re-applies styles to the shadow root of the provided element instance. This function is * intended to be called after an element has been adopted by a new document to reconstruct the - * adopted stylesheet instances within the context of the new document. + * adopted stylesheet instances within the context (view) of the new document. * * @param componentInstance The component instance to reapply styles to. */ export function readoptStyles(componentInstance: T): void { if (!supportsConstructableStyleSheets || !componentInstance.shadowRoot || - !componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY]) { + !componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY] || + !componentInstance.ownerDocument.defaultView) { return; } - const cssText = componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY]; - const context = componentInstance.ownerDocument.defaultView ?? window; - const sheet = new context.CSSStyleSheet(); - sheet.replaceSync(cssText); + const sheet = new componentInstance.ownerDocument.defaultView.CSSStyleSheet(); + sheet.replaceSync(componentInstance.constructor[CUSTOM_ELEMENT_CSS_PROPERTY]); componentInstance.shadowRoot.adoptedStyleSheets = [sheet]; }