chocola 1.3.10 → 1.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,18 +5,63 @@ import chalk from "chalk";
|
|
|
5
5
|
import beautify from "js-beautify";
|
|
6
6
|
|
|
7
7
|
function scopeCss(cssString, cssId) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
function findMatchingBrace(s, openIndex) {
|
|
9
|
+
let depth = 0;
|
|
10
|
+
for (let i = openIndex; i < s.length; i++) {
|
|
11
|
+
const ch = s[i];
|
|
12
|
+
if (ch === '{') depth++;
|
|
13
|
+
else if (ch === '}') {
|
|
14
|
+
depth--;
|
|
15
|
+
if (depth === 0) return i;
|
|
16
|
+
}
|
|
11
17
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
return s.length - 1;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function processBlock(str) {
|
|
22
|
+
str = str.replaceAll(":root", `.${cssId}`);
|
|
23
|
+
|
|
24
|
+
let out = "";
|
|
25
|
+
let i = 0;
|
|
26
|
+
while (i < str.length) {
|
|
27
|
+
const braceIndex = str.indexOf('{', i);
|
|
28
|
+
if (braceIndex === -1) {
|
|
29
|
+
out += str.slice(i);
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
const header = str.substring(i, braceIndex);
|
|
33
|
+
const endBrace = findMatchingBrace(str, braceIndex);
|
|
34
|
+
const inner = str.substring(braceIndex + 1, endBrace);
|
|
35
|
+
const innerScoped = processBlock(inner);
|
|
36
|
+
|
|
37
|
+
if (header.trim().startsWith("@")) {
|
|
38
|
+
out += header + "{" + innerScoped + "}";
|
|
39
|
+
} else {
|
|
40
|
+
const selectors = header
|
|
41
|
+
.split(",")
|
|
42
|
+
.map(s => s.trim())
|
|
43
|
+
.filter(Boolean);
|
|
44
|
+
const scopedHeader = selectors.length > 0
|
|
45
|
+
? selectors.map(sel => {
|
|
46
|
+
const s = sel.trim();
|
|
47
|
+
if (s.startsWith("." + cssId)) {
|
|
48
|
+
return s;
|
|
49
|
+
}
|
|
50
|
+
return `.${cssId} ${s}`;
|
|
51
|
+
}).join(", ")
|
|
52
|
+
: header;
|
|
53
|
+
out += scopedHeader + "{" + innerScoped + "}";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
i = endBrace + 1;
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return processBlock(cssString);
|
|
18
62
|
}
|
|
19
63
|
|
|
64
|
+
|
|
20
65
|
function interpolateNode(node, ctxProxy) {
|
|
21
66
|
if (node.nodeType === 3) {
|
|
22
67
|
node.textContent = node.textContent.replace(/\{([^}]+)\}/g, (_, expr) => {
|
|
@@ -150,6 +195,8 @@ export function processComponentElement(
|
|
|
150
195
|
if (!cssId) {
|
|
151
196
|
cssId = genRandomId(cssScopes, 8, true);
|
|
152
197
|
cssScopesMap.set(compName, cssId);
|
|
198
|
+
}
|
|
199
|
+
if (fragment.firstChild && fragment.firstChild.nodeType === 1) {
|
|
153
200
|
fragment.firstChild.classList.add(cssId);
|
|
154
201
|
}
|
|
155
202
|
style = scopeCss(style, cssId);
|