Skip to content

Commit

Permalink
gaurd CSSStyleSheet constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Jan 18, 2023
1 parent 65f1b08 commit 9191fc8
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/scriptlets/inject-css-in-shadow-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9191fc8

Please sign in to comment.