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

Function modifySheet

src/inject/dynamic-theme/stylesheet-modifier.ts:70–375  ·  view source on GitHub ↗
(options: ModifySheetOptions)

Source from the content-addressed store, hash-verified

68 }
69
70 function modifySheet(options: ModifySheetOptions) {
71 const rules = options.sourceCSSRules;
72 const {theme, ignoreImageAnalysis, force, prepareSheet, isAsyncCancelled} = options;
73
74 let rulesChanged = (rulesModCache.size === 0);
75 const notFoundCacheKeys = new Set(rulesModCache.keys());
76 const themeKey = getThemeKey(theme);
77 const themeChanged = (themeKey !== prevFilterKey);
78
79 if (hasNonLoadedLink) {
80 wasRebuilt = true;
81 }
82
83 const modRules: ModifiableCSSRule[] = [];
84 iterateCSSRules(rules, (rule) => {
85 const hash = getStyleRuleHash(rule);
86 let textDiffersFromPrev = false;
87
88 notFoundCacheKeys.delete(hash);
89 if (!rulesTextCache.has(hash)) {
90 rulesTextCache.add(hash);
91 textDiffersFromPrev = true;
92 }
93
94 if (textDiffersFromPrev) {
95 rulesChanged = true;
96 } else {
97 modRules.push(rulesModCache.get(hash)!);
98 return;
99 }
100
101 // A very specific case to skip. This causes a lot of calls to `getModifiableCSSDeclaration`
102 // and currently contributes nothing in real-world case.
103 // TODO: Allow `setRule` to throw a exception when we're modifying SVGs namespace styles.
104 if (rule.style.all === 'revert') {
105 return;
106 }
107
108 const modDecs: ModifiableCSSDeclaration[] = [];
109 rule.style && iterateCSSDeclarations(rule.style, (property, value) => {
110 const mod = getModifiableCSSDeclaration(property, value, rule, variablesStore, ignoreImageAnalysis, isAsyncCancelled);
111 if (mod) {
112 modDecs.push(mod);
113 }
114 });
115
116 let modRule: ModifiableCSSRule | null = null;
117 if (modDecs.length > 0) {
118 const parentRule = rule.parentRule!;
119 modRule = {selector: rule.selectorText, declarations: modDecs, parentRule};
120 modRules.push(modRule);
121 }
122 rulesModCache.set(hash, modRule!);
123 }, () => {
124 hasNonLoadedLink = true;
125 });
126
127 notFoundCacheKeys.forEach((key) => {

Callers

nothing calls this directly

Calls 15

iterateCSSRulesFunction · 0.90
iterateCSSDeclarationsFunction · 0.90
getThemeKeyFunction · 0.85
getStyleRuleHashFunction · 0.85
clearFunction · 0.85
getGroupFunction · 0.85
handleAsyncDeclarationFunction · 0.85
handleVarDeclarationsFunction · 0.85
prepareSheetFunction · 0.85
buildStyleSheetFunction · 0.85
addMethod · 0.80

Tested by

no test coverage detected