@@ -282,3 +282,36 @@ export function sanitizeCSS(css) {
282282export function addScopeToCss ( css , scopeSelector ) {
283283 return scopeSelector + ' {' + css + '\n}' ;
284284}
285+
286+ /**
287+ * Older browser versions do not support nested css and cannot use the normal `addScopeToCss`.
288+ * All major web browsers should be fine but Anki is still distributing Chromium 112 on some platforms as of Anki version 24.11.
289+ * Chromium 120+ is required for full support.
290+ * @param {string } css
291+ * @param {string } scopeSelector
292+ * @returns {string }
293+ */
294+ export function addScopeToCssLegacy ( css , scopeSelector ) {
295+ const stylesheet = new CSSStyleSheet ( ) ;
296+ // nodejs must fall back to the normal version of the function
297+ if ( typeof stylesheet . replaceSync === 'undefined' ) {
298+ return addScopeToCss ( css , scopeSelector ) ;
299+ }
300+ stylesheet . replaceSync ( css ) ;
301+ const newCSSRules = [ ] ;
302+ for ( const cssRule of stylesheet . cssRules ) {
303+ // ignore non-style rules
304+ if ( ! ( cssRule instanceof CSSStyleRule ) ) {
305+ continue ;
306+ }
307+
308+ const newSelectors = [ ] ;
309+ for ( const selector of cssRule . selectorText . split ( ',' ) ) {
310+ newSelectors . push ( scopeSelector + ' ' + selector ) ;
311+ }
312+ const newRule = cssRule . cssText . replace ( cssRule . selectorText , newSelectors . join ( ', ' ) ) ;
313+ newCSSRules . push ( newRule ) ;
314+ }
315+ stylesheet . replaceSync ( newCSSRules . join ( '\n' ) ) ;
316+ return [ ...stylesheet . cssRules ] . map ( ( rule ) => rule . cssText || '' ) . join ( '\n' ) ;
317+ }
0 commit comments