forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss-rules.ts
More file actions
115 lines (100 loc) · 3.73 KB
/
Copy pathcss-rules.ts
File metadata and controls
115 lines (100 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {forEach} from '../../utils/array';
import {parseURL, getAbsoluteURL} from './url';
import {logWarn} from '../utils/log';
export function iterateCSSRules(rules: CSSRuleList, iterate: (rule: CSSStyleRule) => void) {
forEach(rules, (rule) => {
if (rule instanceof CSSMediaRule) {
const media = Array.from(rule.media);
if (media.includes('screen') || media.includes('all') || !(media.includes('print') || media.includes('speech'))) {
iterateCSSRules(rule.cssRules, iterate);
}
} else if (rule instanceof CSSStyleRule) {
iterate(rule);
} else if (rule instanceof CSSImportRule) {
try {
iterateCSSRules(rule.styleSheet.cssRules, iterate);
} catch (err) {
logWarn(err);
}
} else {
logWarn(`CSSRule type not supported`, rule);
}
});
}
export function iterateCSSDeclarations(style: CSSStyleDeclaration, iterate: (property: string, value: string) => void) {
forEach(style, (property) => {
const value = style.getPropertyValue(property).trim();
if (!value) {
return;
}
iterate(property, value);
});
}
function isCSSVariable(property: string) {
return property.startsWith('--') && !property.startsWith('--darkreader');
}
export function getCSSVariables(rules: CSSRuleList) {
const variables = new Map<string, string>();
rules && iterateCSSRules(rules, (rule) => {
rule.style && iterateCSSDeclarations(rule.style, (property, value) => {
if (isCSSVariable(property)) {
variables.set(property, value);
}
});
});
return variables;
}
export function getElementCSSVariables(element: HTMLElement) {
const variables = new Map<string, string>();
iterateCSSDeclarations(element.style, (property, value) => {
if (isCSSVariable(property)) {
variables.set(property, value);
}
});
return variables;
}
export const cssURLRegex = /url\((('.+?')|(".+?")|([^\)]*?))\)/g;
export const cssImportRegex = /@import (url\()?(('.+?')|(".+?")|([^\)]*?))\)?;?/g;
export function getCSSURLValue(cssURL: string) {
return cssURL.replace(/^url\((.*)\)$/, '$1').replace(/^"(.*)"$/, '$1').replace(/^'(.*)'$/, '$1');
}
export function getCSSBaseBath(url: string) {
const cssURL = parseURL(url);
return `${cssURL.protocol}//${cssURL.host}${cssURL.pathname.replace(/\?.*$/, '').replace(/(\/)([^\/]+)$/i, '$1')}`;
}
export function replaceCSSRelativeURLsWithAbsolute($css: string, cssBasePath: string) {
return $css.replace(cssURLRegex, (match) => {
const pathValue = getCSSURLValue(match);
return `url("${getAbsoluteURL(cssBasePath, pathValue)}")`;
});
}
const cssCommentsRegex = /\/\*[\s\S]*?\*\//g;
export function removeCSSComments($css: string) {
return $css.replace(cssCommentsRegex, '');
}
const fontFaceRegex = /@font-face\s*{[^}]*}/g;
export function replaceCSSFontFace($css: string) {
return $css.replace(fontFaceRegex, '');
}
const varRegex = /var\((--[^\s,]+),?\s*([^\(\)]*(\([^\(\)]*\)[^\(\)]*)*\s*)\)/g;
export function replaceCSSVariables(value: string, variables: Map<string, string>) {
let missing = false;
const result = value.replace(varRegex, (match, name, fallback) => {
if (variables.has(name)) {
return variables.get(name);
} else if (fallback) {
return fallback;
} else {
logWarn(`Variable ${name} not found`);
missing = true;
}
return match;
});
if (missing) {
return result;
}
if (result.match(varRegex)) {
return replaceCSSVariables(result, variables);
}
return result;
}