-
Notifications
You must be signed in to change notification settings - Fork 169
/
style.ts
150 lines (140 loc) · 4.79 KB
/
style.ts
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// deno-lint-ignore-file ban-ts-comment
export function applyCSS(url: string, css: string) {
const { document } = globalThis;
if (document) {
const ssrEl = Array.from<Element>(document.head.children).find((el: Element) =>
el.getAttribute("data-module-id") === url &&
el.hasAttribute("ssr")
);
if (ssrEl) {
// apply the css at next time
ssrEl.removeAttribute("ssr");
} else {
const prevEls = Array.from(document.head.children).filter((el: Element) => {
return el.getAttribute("data-module-id") === url;
});
const el = document.createElement("style");
el.setAttribute("data-module-id", url);
el.appendChild(document.createTextNode(css));
document.head.appendChild(el);
if (prevEls.length > 0) {
prevEls.forEach((el) => document.head.removeChild(el));
}
}
}
}
export function applyUnoCSS(url: string, css: string) {
let unocssSheet: CSSStyleSheet | null = null;
if (globalThis.document?.styleSheets) {
for (const sheet of document.styleSheets) {
if (sheet.ownerNode && (sheet.ownerNode as HTMLStyleElement).hasAttribute("data-unocss")) {
unocssSheet = sheet;
break;
}
}
}
if (unocssSheet) {
const sheet = createCSSStyleSheet(css);
if (sheet) {
const tokens = new Set<string>();
const medias = new Map<string, CSSRuleList>();
for (const rule of unocssSheet.cssRules) {
// @ts-ignore
const { cssText, media, cssRules: mediaCSSRules } = rule;
if ((media || cssText.startsWith("@media")) && mediaCSSRules) {
const cond: string = getConditionText(rule);
medias.set(cond, mediaCSSRules);
}
tokens.add(getSelectorText(rule));
}
for (const rule of sheet.cssRules) {
// @ts-ignore
const { cssText, media, cssRules: mediaCSSRules } = rule;
if ((media || cssText.startsWith("@media")) && mediaCSSRules) {
const cond = getConditionText(rule);
if (medias.has(cond)) {
const tokens = new Set<string>();
const mediaCSSLines: string[] = [];
for (const rule of medias.get(cond)!) {
tokens.add(getSelectorText(rule));
mediaCSSLines.push(fixCSSText(rule.cssText));
}
let hasNewRule = false;
for (const rule of mediaCSSRules) {
if (!tokens.has(getSelectorText(rule))) {
mediaCSSLines.push(fixCSSText(rule.cssText));
hasNewRule = true;
}
}
if (hasNewRule) {
insertMediaRule(unocssSheet, cond, `@media ${cond} {${mediaCSSLines.join("\n")}}`);
}
} else {
insertMediaRule(unocssSheet, cond, cssText);
}
} else if (!tokens.has(getSelectorText(rule))) {
unocssSheet.insertRule(fixCSSText(cssText), unocssSheet.cssRules.length);
}
}
return;
}
}
// fallback to create a new style element
applyCSS(url, css);
}
function insertMediaRule(sheet: CSSStyleSheet, conditionText: string, cssText: string) {
// delete the old media rule
for (let i = 0; i < sheet.cssRules.length; i++) {
const rule = sheet.cssRules[i];
// @ts-ignore
if (rule.media || rule.cssText.startsWith("@media")) {
const cond: string = getConditionText(rule);
if (cond === conditionText) {
sheet.deleteRule(i);
break;
}
}
}
// insert the new media rule at the end
sheet.insertRule(cssText, sheet.cssRules.length);
}
function getSelectorText(rule: CSSRule): string {
// @ts-ignore
return rule.selectorText || rule.cssText.split("{")[0].trim();
}
function getConditionText(rule: CSSRule): string {
// @ts-ignore
return rule.conditionText || rule.cssText.substring(6, cssText.indexOf("{")).trim();
}
const isChorme = globalThis.document && /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
function fixCSSText(cssText: string): string {
// fix chrome dropping `mask` webkit prefix
if (isChorme && cssText.startsWith(".i-")) {
return cssText.replace(/(mask:[^;]+;)/, "$1-webkit-$1");
}
return cssText;
}
function createCSSStyleSheet(css: string): CSSStyleSheet | null {
try {
const sheet = new CSSStyleSheet();
// @ts-ignore
sheet.replaceSync(css);
return sheet;
} catch (_e) {
const el = document.createElement("style");
el.appendChild(document.createTextNode(css));
el.media = "(max-width: 1px)";
document.head.appendChild(el);
setTimeout(() => {
document.head.removeChild(el);
}, 0);
for (const sheet of document.styleSheets) {
if (sheet.ownerNode === el) {
sheet.disabled = false;
return sheet;
}
}
}
console.warn("The browser does not support CSSStyleSheet!");
return null;
}