MCPcopy Index your code
hub / github.com/darkreader/darkreader / iterateCSSRules

Function iterateCSSRules

src/inject/dynamic-theme/css-rules.ts:7–39  ·  view source on GitHub ↗
(rules: CSSRuleList | CSSRule[] | Set<CSSRule>, iterate: (rule: CSSStyleRule) => void, onImportError?: () => void)

Source from the content-addressed store, hash-verified

5import {logInfo, logWarn} from '../utils/log';
6
7export function iterateCSSRules(rules: CSSRuleList | CSSRule[] | Set<CSSRule>, iterate: (rule: CSSStyleRule) => void, onImportError?: () => void): void {
8 forEach(rules, (rule) => {
9 if (isStyleRule(rule)) {
10 iterate(rule);
11 if (rule.cssRules?.length > 0) {
12 iterateCSSRules(rule.cssRules, iterate);
13 }
14 } else if (isImportRule(rule)) {
15 try {
16 iterateCSSRules(rule.styleSheet!.cssRules, iterate, onImportError);
17 } catch (err) {
18 logInfo(`Found a non-loaded link.`);
19 onImportError?.();
20 }
21 } else if (isMediaRule(rule)) {
22 const media = Array.from(rule.media);
23 const isScreenOrAllOrQuery = media.some((m) => m.startsWith('screen') || m.startsWith('all') || m.startsWith('('));
24 const isNotScreen = !isScreenOrAllOrQuery && media.some((m) => ignoredMedia.some((i) => m.startsWith(i)));
25
26 if (isScreenOrAllOrQuery || !isNotScreen) {
27 iterateCSSRules(rule.cssRules, iterate, onImportError);
28 }
29 } else if (isSupportsRule(rule)) {
30 if (CSS.supports(rule.conditionText)) {
31 iterateCSSRules(rule.cssRules, iterate, onImportError);
32 }
33 } else if (isLayerRule(rule)) {
34 iterateCSSRules(rule.cssRules, iterate, onImportError);
35 } else {
36 logWarn(`CSSRule type not supported`, rule);
37 }
38 });
39}
40
41export const ignoredMedia = [
42 'aural',

Callers 3

modifySheetFunction · 0.90
renderFunction · 0.90

Calls 8

forEachFunction · 0.90
logInfoFunction · 0.90
logWarnFunction · 0.90
isStyleRuleFunction · 0.85
isImportRuleFunction · 0.85
isMediaRuleFunction · 0.85
isSupportsRuleFunction · 0.85
isLayerRuleFunction · 0.85

Tested by

no test coverage detected