From 9191fc8953c036a855b45df6b14a4b1662ff2c93 Mon Sep 17 00:00:00 2001 From: Stanislav A Date: Wed, 18 Jan 2023 17:09:27 +0300 Subject: [PATCH] gaurd CSSStyleSheet constructor --- src/scriptlets/inject-css-in-shadow-dom.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/scriptlets/inject-css-in-shadow-dom.js b/src/scriptlets/inject-css-in-shadow-dom.js index b927f2f26..c9f8ca81b 100644 --- a/src/scriptlets/inject-css-in-shadow-dom.js +++ b/src/scriptlets/inject-css-in-shadow-dom.js @@ -46,20 +46,19 @@ export function injectCssInShadowDom(source, cssRule, hostSelector = '') { } const callback = (shadowRoot) => { - const stylesheet = new CSSStyleSheet(); try { - stylesheet.insertRule(cssRule); - } catch { - logMessage(source, `Failed to parse the rule: ${cssRule}`); - return; - } - - // attach stylesheet to shadow root so the whole subtree would be affected - if (shadowRoot.adoptedStyleSheets) { - // adoptedStyleSheets is not yet supported by Safari + // adoptedStyleSheets and CSSStyleSheet constructor are not yet supported by Safari // https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets + // https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/CSSStyleSheet + const stylesheet = new CSSStyleSheet(); + try { + stylesheet.insertRule(cssRule); + } catch { + logMessage(source, `Failed to parse the rule: ${cssRule}`); + return; + } shadowRoot.adoptedStyleSheets = [...shadowRoot.adoptedStyleSheets, stylesheet]; - } else { + } catch { const styleTag = document.createElement('style'); styleTag.innerText = cssRule; shadowRoot.appendChild(styleTag);