You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the function _applyStyles in apiCustomElements.ts styles are still prepended as a style element in the shadowRoot. Modern browsers support shadowRoot.adoptedStyleSheets with the advantage, that it doesn't need the Content Security Policy style-src 'unsafe-inline' or nonce (see issue #6530).
I would propose to check if the user agent supports adoptedStyleSheets and use that method then, and the style element only as a fallback for legacy browsers, like this:
if (styles) {
styles.forEach((css) => {
if (this.shadowRoot.adoptedStyleSheets) {
const sheet = new CSSStyleSheet();
sheet.replaceSync(css);
this.shadowRoot.adoptedStyleSheets = [...this.shadowRoot.adoptedStyleSheets, sheet];
} else {
const s = document.createElement('style')
if (nonce) s.setAttribute('nonce', nonce)
s.textContent = css;
this.shadowRoot!.prepend(s);
...
}
});
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
In the function
_applyStyles
inapiCustomElements.ts
styles are still prepended as astyle
element in theshadowRoot
. Modern browsers supportshadowRoot.adoptedStyleSheets
with the advantage, that it doesn't need the Content Security Policystyle-src 'unsafe-inline'
ornonce
(see issue #6530).I would propose to check if the user agent supports adoptedStyleSheets and use that method then, and the
style
element only as a fallback for legacy browsers, like this:Beta Was this translation helpful? Give feedback.
All reactions