Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions packages/blockly/core/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
*/

// Former goog.module ID: Blockly.Css
/** Has CSS already been injected? */
const injectionSites = new WeakSet<Document | ShadowRoot>();
const registeredStyleSheets: Array<CSSStyleSheet> = [];
const registeredCss: Array<string> = [];
import * as userAgent from './utils/useragent.js';

/**
Expand All @@ -17,11 +16,7 @@ import * as userAgent from './utils/useragent.js';
* @param cssContent Multiline CSS string or an array of single lines of CSS.
*/
export function register(cssContent: string) {
if (typeof window === 'undefined' || !window.CSSStyleSheet) return;

const sheet = new CSSStyleSheet();
sheet.replace(cssContent);
registeredStyleSheets.push(sheet);
registeredCss.push(cssContent);
}

/**
Expand All @@ -41,24 +36,26 @@ export function inject(
hasCss: boolean,
pathToMedia: string,
) {
if (!hasCss || typeof window === 'undefined' || !window.CSSStyleSheet) {
return;
}
if (!hasCss || typeof window === 'undefined') return;

const root = container.getRootNode() as Document | ShadowRoot;
// Only inject the CSS once.
if (injectionSites.has(root)) return;
injectionSites.add(root);

// Strip off any trailing slash (either Unix or Windows).
const mediaPath = pathToMedia.replace(/[\\/]$/, '');
const cssContent = content.replace(/<<<PATH>>>/g, mediaPath);

const sheet = new CSSStyleSheet();
sheet.replace(cssContent);
root.adoptedStyleSheets.push(sheet);

registeredStyleSheets.forEach((sheet) => root.adoptedStyleSheets.push(sheet));
const cssText = [content, ...registeredCss]
.join('\n')
.replace(/<<<PATH>>>/g, mediaPath);

const styleEl = document.createElement('style');
styleEl.id = 'blockly-common-style';
styleEl.textContent = cssText;
// Prepend so Blockly's rules sit at the start of the cascade; any user
// stylesheet declared later wins by document order. Style elements appended
// to the light DOM don't apply inside shadow roots, so for the shadow DOM
// case we prepend the style element to the shadow root itself.
(root instanceof ShadowRoot ? root : document.head).prepend(styleEl);
}

/**
Expand Down
20 changes: 9 additions & 11 deletions packages/blockly/core/renderers/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1126,17 +1126,15 @@ export class ConstantProvider {
* @param selector The CSS selector to interpolate into the stylesheet.
*/
protected injectCSS_(root: Document | ShadowRoot, selector: string) {
if (
typeof window === 'undefined' ||
!window.CSSStyleSheet ||
injectionSites.get(selector)?.has(root)
) {
return;
}

const sheet = new CSSStyleSheet();
sheet.replace(this.getCSS_(selector).join('\n'));
root.adoptedStyleSheets.push(sheet);
if (typeof window === 'undefined') return;
if (injectionSites.get(selector)?.has(root)) return;

const styleEl = document.createElement('style');
styleEl.className = 'blockly-renderer-style';
styleEl.textContent = this.getCSS_(selector).join('\n');
// See css.ts inject() for the rationale on prepending and shadow root
// handling.
(root instanceof ShadowRoot ? root : document.head).prepend(styleEl);

const sitesForSelector =
injectionSites.get(selector) ?? new WeakSet<Document | ShadowRoot>();
Expand Down
Loading