@sentropic/track 0.98.11-rc.13 → 0.98.11-rc.15
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.
- package/dist/focus-vendor/cli/index.d.ts.map +1 -1
- package/dist/focus-vendor/cli/index.js +38 -3
- package/dist/focus-vendor/cli/index.js.map +1 -1
- package/dist/focus-vendor/index.d.ts +1 -0
- package/dist/focus-vendor/index.js +3 -0
- package/dist/focus-vendor/index.js.map +1 -1
- package/dist/focus-vendor/registry/entries.v1.json +1 -1
- package/dist/focus-vendor/render/html.d.ts +1 -1
- package/dist/focus-vendor/render/html.d.ts.map +1 -1
- package/dist/focus-vendor/render/html.js +8 -2
- package/dist/focus-vendor/render/html.js.map +1 -1
- package/dist/focus-vendor/render/theme.d.ts +17 -0
- package/dist/focus-vendor/render/theme.d.ts.map +1 -0
- package/dist/focus-vendor/render/theme.js +654 -0
- package/dist/focus-vendor/render/theme.js.map +1 -0
- package/dist/focus-vendor/validation/readability.d.ts.map +1 -1
- package/dist/focus-vendor/validation/readability.js +15 -20
- package/dist/focus-vendor/validation/readability.js.map +1 -1
- package/package.json +1 -1
- package/skills/present-decision/SKILL.md +17 -18
- package/skills/present-decision/assets/self-audit.md +8 -7
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Theme CSS validation and root custom-property discovery for portable Focus HTML surfaces.
|
|
3
|
+
*
|
|
4
|
+
* Security & portability contract:
|
|
5
|
+
* - Bounded size (256 KiB max) to keep offline portable dossiers self-contained and fast.
|
|
6
|
+
* - Rejects `@import`, external/non-`data:` `url(...)` references, restricted image functions
|
|
7
|
+
* (`image-set(`, `-webkit-image-set(`, `image(`, `cross-fade(`, `src(`) unless every argument
|
|
8
|
+
* supplies a `data:` `url(...)` or `"data:..."` string without `var(`, `env(`, or `attr(`,
|
|
9
|
+
* `</style` breakouts, and NUL bytes.
|
|
10
|
+
* - Does not relax the host CSP (`style-src 'unsafe-inline'` already permits inline `<style>`).
|
|
11
|
+
*/
|
|
12
|
+
/** Maximum allowed size (256 KiB) for an inlined theme CSS file. */
|
|
13
|
+
export const FOCUS_THEME_MAX_CSS_BYTES = 256 * 1024;
|
|
14
|
+
const RESTRICTED_IMAGE_FUNCTIONS = new Set([
|
|
15
|
+
"image-set",
|
|
16
|
+
"-webkit-image-set",
|
|
17
|
+
"image",
|
|
18
|
+
"cross-fade",
|
|
19
|
+
"src",
|
|
20
|
+
]);
|
|
21
|
+
const FORBIDDEN_SUBSTITUTION_FUNCTIONS = new Set(["var", "env", "attr"]);
|
|
22
|
+
const THEME_ID_RE = /^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,63}$/;
|
|
23
|
+
const CONDITIONAL_AT_RULES = new Set([
|
|
24
|
+
"media",
|
|
25
|
+
"supports",
|
|
26
|
+
"layer",
|
|
27
|
+
"container",
|
|
28
|
+
"scope",
|
|
29
|
+
"document",
|
|
30
|
+
]);
|
|
31
|
+
const isWhitespace = (ch) => ch === " " || ch === "\t" || ch === "\n" || ch === "\r" || ch === "\f";
|
|
32
|
+
const isIdentStart = (ch) => (ch >= "a" && ch <= "z") ||
|
|
33
|
+
(ch >= "A" && ch <= "Z") ||
|
|
34
|
+
(ch >= "0" && ch <= "9") ||
|
|
35
|
+
ch === "_" ||
|
|
36
|
+
ch === "-" ||
|
|
37
|
+
ch.charCodeAt(0) >= 0x80;
|
|
38
|
+
const isHexDigit = (ch) => (ch >= "0" && ch <= "9") || (ch >= "a" && ch <= "f") || (ch >= "A" && ch <= "F");
|
|
39
|
+
const decodeCssEscape = (src, backslashIndex) => {
|
|
40
|
+
const next = backslashIndex + 1;
|
|
41
|
+
if (next >= src.length)
|
|
42
|
+
return { char: "\uFFFD", next };
|
|
43
|
+
const first = src[next];
|
|
44
|
+
if (first === "\n" || first === "\r" || first === "\f") {
|
|
45
|
+
return { char: "", next: first === "\r" && src[next + 1] === "\n" ? next + 2 : next + 1 };
|
|
46
|
+
}
|
|
47
|
+
if (isHexDigit(first)) {
|
|
48
|
+
let end = next;
|
|
49
|
+
while (end < src.length && end - next < 6 && isHexDigit(src[end]))
|
|
50
|
+
end += 1;
|
|
51
|
+
const codePoint = Number.parseInt(src.slice(next, end), 16);
|
|
52
|
+
if (end < src.length && isWhitespace(src[end])) {
|
|
53
|
+
end += src[end] === "\r" && src[end + 1] === "\n" ? 2 : 1;
|
|
54
|
+
}
|
|
55
|
+
const safe = codePoint === 0 || codePoint > 0x10ffff || (codePoint >= 0xd800 && codePoint <= 0xdfff)
|
|
56
|
+
? "\uFFFD"
|
|
57
|
+
: String.fromCodePoint(codePoint);
|
|
58
|
+
return { char: safe, next: end };
|
|
59
|
+
}
|
|
60
|
+
return { char: first, next: next + 1 };
|
|
61
|
+
};
|
|
62
|
+
const readCssIdent = (src, start) => {
|
|
63
|
+
let i = start;
|
|
64
|
+
let value = "";
|
|
65
|
+
while (i < src.length) {
|
|
66
|
+
const ch = src[i];
|
|
67
|
+
if (ch === "\\") {
|
|
68
|
+
const decoded = decodeCssEscape(src, i);
|
|
69
|
+
value += decoded.char;
|
|
70
|
+
i = decoded.next;
|
|
71
|
+
}
|
|
72
|
+
else if (isIdentStart(ch)) {
|
|
73
|
+
value += ch;
|
|
74
|
+
i += 1;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return { value, end: i };
|
|
81
|
+
};
|
|
82
|
+
const readCssStringEnd = (src, openIndex) => {
|
|
83
|
+
const quote = src[openIndex];
|
|
84
|
+
let i = openIndex + 1;
|
|
85
|
+
let value = "";
|
|
86
|
+
while (i < src.length) {
|
|
87
|
+
const ch = src[i];
|
|
88
|
+
if (ch === quote)
|
|
89
|
+
return { value, end: i + 1 };
|
|
90
|
+
if (ch === "\\") {
|
|
91
|
+
const decoded = decodeCssEscape(src, i);
|
|
92
|
+
value += decoded.char;
|
|
93
|
+
i = decoded.next;
|
|
94
|
+
}
|
|
95
|
+
else if (ch === "\n" || ch === "\r" || ch === "\f") {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
value += ch;
|
|
100
|
+
i += 1;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
};
|
|
105
|
+
const readUrlTarget = (src, openParenIndex) => {
|
|
106
|
+
let i = openParenIndex + 1;
|
|
107
|
+
while (i < src.length && isWhitespace(src[i]))
|
|
108
|
+
i += 1;
|
|
109
|
+
if (i >= src.length)
|
|
110
|
+
return null;
|
|
111
|
+
const first = src[i];
|
|
112
|
+
if (first === '"' || first === "'") {
|
|
113
|
+
const str = readCssStringEnd(src, i);
|
|
114
|
+
if (!str)
|
|
115
|
+
return null;
|
|
116
|
+
let j = str.end;
|
|
117
|
+
while (j < src.length && isWhitespace(src[j]))
|
|
118
|
+
j += 1;
|
|
119
|
+
if (src[j] !== ")")
|
|
120
|
+
return null;
|
|
121
|
+
return { target: str.value.trim(), end: j + 1 };
|
|
122
|
+
}
|
|
123
|
+
let value = "";
|
|
124
|
+
while (i < src.length) {
|
|
125
|
+
const ch = src[i];
|
|
126
|
+
if (ch === ")")
|
|
127
|
+
return { target: value.trim(), end: i + 1 };
|
|
128
|
+
if (ch === "\\") {
|
|
129
|
+
const decoded = decodeCssEscape(src, i);
|
|
130
|
+
value += decoded.char;
|
|
131
|
+
i = decoded.next;
|
|
132
|
+
}
|
|
133
|
+
else if (ch === '"' || ch === "'" || ch === "(") {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
value += ch;
|
|
138
|
+
i += 1;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
};
|
|
143
|
+
/** Validate a theme identifier placed in `data-theme="<id>"`. */
|
|
144
|
+
export const validateThemeId = (themeId) => {
|
|
145
|
+
if (typeof themeId !== "string" || !THEME_ID_RE.test(themeId)) {
|
|
146
|
+
throw new Error(`Invalid theme id "${String(themeId)}": expected 1–64 chars matching ${THEME_ID_RE.source}`);
|
|
147
|
+
}
|
|
148
|
+
return themeId;
|
|
149
|
+
};
|
|
150
|
+
const matchUrlCall = (src, index) => {
|
|
151
|
+
const ch = src[index];
|
|
152
|
+
if (ch !== "\\" && !isIdentStart(ch))
|
|
153
|
+
return null;
|
|
154
|
+
const prev = index > 0 ? src[index - 1] : "";
|
|
155
|
+
if (isIdentStart(prev))
|
|
156
|
+
return null;
|
|
157
|
+
const ident = readCssIdent(src, index);
|
|
158
|
+
if (ident.value.toLowerCase() !== "url")
|
|
159
|
+
return null;
|
|
160
|
+
let parenIndex = ident.end;
|
|
161
|
+
while (parenIndex < src.length && isWhitespace(src[parenIndex]))
|
|
162
|
+
parenIndex += 1;
|
|
163
|
+
if (src[parenIndex] !== "(")
|
|
164
|
+
return null;
|
|
165
|
+
const parsed = readUrlTarget(src, parenIndex);
|
|
166
|
+
if (!parsed)
|
|
167
|
+
return { target: "", end: -1 };
|
|
168
|
+
return parsed;
|
|
169
|
+
};
|
|
170
|
+
const findMatchingParen = (src, openParenIndex) => {
|
|
171
|
+
let depth = 1;
|
|
172
|
+
let i = openParenIndex + 1;
|
|
173
|
+
while (i < src.length) {
|
|
174
|
+
const ch = src[i];
|
|
175
|
+
if (ch === '"' || ch === "'") {
|
|
176
|
+
const str = readCssStringEnd(src, i);
|
|
177
|
+
if (!str)
|
|
178
|
+
return -1;
|
|
179
|
+
i = str.end;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
if (ch === "/" && src[i + 1] === "*") {
|
|
183
|
+
const close = src.indexOf("*/", i + 2);
|
|
184
|
+
if (close === -1)
|
|
185
|
+
return -1;
|
|
186
|
+
i = close + 2;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (ch === "\\") {
|
|
190
|
+
i = decodeCssEscape(src, i).next;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (ch === "(")
|
|
194
|
+
depth += 1;
|
|
195
|
+
else if (ch === ")") {
|
|
196
|
+
depth -= 1;
|
|
197
|
+
if (depth === 0)
|
|
198
|
+
return i;
|
|
199
|
+
}
|
|
200
|
+
i += 1;
|
|
201
|
+
}
|
|
202
|
+
return -1;
|
|
203
|
+
};
|
|
204
|
+
const splitCssFunctionArgs = (argsText) => {
|
|
205
|
+
const args = [];
|
|
206
|
+
let start = 0;
|
|
207
|
+
let parenDepth = 0;
|
|
208
|
+
let bracketDepth = 0;
|
|
209
|
+
let i = 0;
|
|
210
|
+
while (i < argsText.length) {
|
|
211
|
+
const ch = argsText[i];
|
|
212
|
+
if (ch === '"' || ch === "'") {
|
|
213
|
+
const str = readCssStringEnd(argsText, i);
|
|
214
|
+
if (!str)
|
|
215
|
+
return null;
|
|
216
|
+
i = str.end;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (ch === "/" && argsText[i + 1] === "*") {
|
|
220
|
+
const close = argsText.indexOf("*/", i + 2);
|
|
221
|
+
if (close === -1)
|
|
222
|
+
return null;
|
|
223
|
+
i = close + 2;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (ch === "\\") {
|
|
227
|
+
i = decodeCssEscape(argsText, i).next;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (ch === "(")
|
|
231
|
+
parenDepth += 1;
|
|
232
|
+
else if (ch === ")") {
|
|
233
|
+
if (parenDepth === 0)
|
|
234
|
+
return null;
|
|
235
|
+
parenDepth -= 1;
|
|
236
|
+
}
|
|
237
|
+
else if (ch === "[")
|
|
238
|
+
bracketDepth += 1;
|
|
239
|
+
else if (ch === "]" && bracketDepth > 0)
|
|
240
|
+
bracketDepth -= 1;
|
|
241
|
+
else if (ch === "," && parenDepth === 0 && bracketDepth === 0) {
|
|
242
|
+
args.push(argsText.slice(start, i));
|
|
243
|
+
start = i + 1;
|
|
244
|
+
}
|
|
245
|
+
i += 1;
|
|
246
|
+
}
|
|
247
|
+
args.push(argsText.slice(start));
|
|
248
|
+
return args;
|
|
249
|
+
};
|
|
250
|
+
const isSafeImageSetTypeCall = (inner) => {
|
|
251
|
+
let i = 0;
|
|
252
|
+
let seenString = false;
|
|
253
|
+
while (i < inner.length) {
|
|
254
|
+
const ch = inner[i];
|
|
255
|
+
if (isWhitespace(ch)) {
|
|
256
|
+
i += 1;
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (ch === "/" && inner[i + 1] === "*") {
|
|
260
|
+
const close = inner.indexOf("*/", i + 2);
|
|
261
|
+
if (close === -1)
|
|
262
|
+
return false;
|
|
263
|
+
i = close + 2;
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if ((ch === '"' || ch === "'") && !seenString) {
|
|
267
|
+
const str = readCssStringEnd(inner, i);
|
|
268
|
+
if (!str)
|
|
269
|
+
return false;
|
|
270
|
+
seenString = true;
|
|
271
|
+
i = str.end;
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
return seenString;
|
|
277
|
+
};
|
|
278
|
+
const validateRestrictedImageCall = (fnName, argsText) => {
|
|
279
|
+
const args = splitCssFunctionArgs(argsText);
|
|
280
|
+
if (!args || args.length === 0) {
|
|
281
|
+
throw new Error(`Theme CSS ${fnName}(...) call is malformed.`);
|
|
282
|
+
}
|
|
283
|
+
for (const arg of args) {
|
|
284
|
+
let hasDataSource = false;
|
|
285
|
+
let k = 0;
|
|
286
|
+
while (k < arg.length) {
|
|
287
|
+
const ch = arg[k];
|
|
288
|
+
if (isWhitespace(ch)) {
|
|
289
|
+
k += 1;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (ch === "/" && arg[k + 1] === "*") {
|
|
293
|
+
const close = arg.indexOf("*/", k + 2);
|
|
294
|
+
if (close === -1) {
|
|
295
|
+
throw new Error(`Theme CSS ${fnName}(...) contains an unclosed comment.`);
|
|
296
|
+
}
|
|
297
|
+
k = close + 2;
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (ch === '"' || ch === "'") {
|
|
301
|
+
const str = readCssStringEnd(arg, k);
|
|
302
|
+
if (!str) {
|
|
303
|
+
throw new Error(`Theme CSS ${fnName}(...) contains an unclosed string.`);
|
|
304
|
+
}
|
|
305
|
+
const target = str.value.trim();
|
|
306
|
+
if (!/^data:/i.test(target)) {
|
|
307
|
+
throw new Error(`Theme CSS ${fnName}(...) must only reference inline data: URLs (rejected string: "${target.slice(0, 80)}").`);
|
|
308
|
+
}
|
|
309
|
+
hasDataSource = true;
|
|
310
|
+
k = str.end;
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (ch === "\\" || isIdentStart(ch)) {
|
|
314
|
+
const ident = readCssIdent(arg, k);
|
|
315
|
+
const lower = ident.value.toLowerCase();
|
|
316
|
+
let parenIdx = ident.end;
|
|
317
|
+
while (parenIdx < arg.length && isWhitespace(arg[parenIdx]))
|
|
318
|
+
parenIdx += 1;
|
|
319
|
+
if (arg[parenIdx] === "(") {
|
|
320
|
+
if (FORBIDDEN_SUBSTITUTION_FUNCTIONS.has(lower)) {
|
|
321
|
+
throw new Error(`Theme CSS ${fnName}(...) must not contain ${lower}(...).`);
|
|
322
|
+
}
|
|
323
|
+
if (lower === "url") {
|
|
324
|
+
const parsed = readUrlTarget(arg, parenIdx);
|
|
325
|
+
if (!parsed) {
|
|
326
|
+
throw new Error(`Theme CSS ${fnName}(...) contains a malformed or unclosed url(...) reference.`);
|
|
327
|
+
}
|
|
328
|
+
if (!/^data:/i.test(parsed.target)) {
|
|
329
|
+
throw new Error(`Theme CSS ${fnName}(...) url(...) must only reference inline data: URLs (rejected: "${parsed.target.slice(0, 80)}").`);
|
|
330
|
+
}
|
|
331
|
+
hasDataSource = true;
|
|
332
|
+
k = parsed.end;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
const closeIdx = findMatchingParen(arg, parenIdx);
|
|
336
|
+
if (closeIdx === -1) {
|
|
337
|
+
throw new Error(`Theme CSS ${fnName}(...) contains an unclosed ${lower}(...) call.`);
|
|
338
|
+
}
|
|
339
|
+
const nestedBody = arg.slice(parenIdx + 1, closeIdx);
|
|
340
|
+
if ((fnName === "image-set" || fnName === "-webkit-image-set") &&
|
|
341
|
+
lower === "type") {
|
|
342
|
+
if (!isSafeImageSetTypeCall(nestedBody)) {
|
|
343
|
+
throw new Error(`Theme CSS ${fnName}(...) contains an invalid type(...) modifier.`);
|
|
344
|
+
}
|
|
345
|
+
k = closeIdx + 1;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
if (RESTRICTED_IMAGE_FUNCTIONS.has(lower)) {
|
|
349
|
+
validateRestrictedImageCall(lower, nestedBody);
|
|
350
|
+
hasDataSource = true;
|
|
351
|
+
k = closeIdx + 1;
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
throw new Error(`Theme CSS ${fnName}(...) does not allow nested ${lower}(...) calls.`);
|
|
355
|
+
}
|
|
356
|
+
k = Math.max(ident.end, k + 1);
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
if (ch === "(" || ch === ")" || ch === "[" || ch === "]" || ch === "{" || ch === "}" || ch === ";") {
|
|
360
|
+
throw new Error(`Theme CSS ${fnName}(...) contains unsupported syntax.`);
|
|
361
|
+
}
|
|
362
|
+
k += 1;
|
|
363
|
+
}
|
|
364
|
+
if (!hasDataSource) {
|
|
365
|
+
throw new Error(`Theme CSS ${fnName}(...) requires each argument to provide a data: url(...) or "data:..." string.`);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
/**
|
|
370
|
+
* Validate theme CSS for strict offline CSP portability.
|
|
371
|
+
* Returns the CSS string unchanged if valid; throws a descriptive `Error` otherwise.
|
|
372
|
+
*/
|
|
373
|
+
export const validateThemeCss = (css) => {
|
|
374
|
+
if (typeof css !== "string") {
|
|
375
|
+
throw new Error("Theme CSS must be a string.");
|
|
376
|
+
}
|
|
377
|
+
if (css.includes("\0")) {
|
|
378
|
+
throw new Error("Theme CSS must not contain NUL bytes.");
|
|
379
|
+
}
|
|
380
|
+
const byteLength = new TextEncoder().encode(css).length;
|
|
381
|
+
if (byteLength > FOCUS_THEME_MAX_CSS_BYTES) {
|
|
382
|
+
throw new Error(`Theme CSS exceeds maximum size of ${FOCUS_THEME_MAX_CSS_BYTES} bytes (256 KiB): got ${byteLength} bytes.`);
|
|
383
|
+
}
|
|
384
|
+
// Check raw CSS before stripping comments: HTML closes `<style>` even inside `/* ... */`.
|
|
385
|
+
if (/<\/style/i.test(css)) {
|
|
386
|
+
throw new Error('Theme CSS must not contain the "</style" sequence.');
|
|
387
|
+
}
|
|
388
|
+
let i = 0;
|
|
389
|
+
while (i < css.length) {
|
|
390
|
+
const ch = css[i];
|
|
391
|
+
if (ch === '"' || ch === "'") {
|
|
392
|
+
const str = readCssStringEnd(css, i);
|
|
393
|
+
i = str ? str.end : i + 1;
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
if (ch === "/" && css[i + 1] === "*") {
|
|
397
|
+
const close = css.indexOf("*/", i + 2);
|
|
398
|
+
i = close === -1 ? css.length : close + 2;
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
if (ch === "@") {
|
|
402
|
+
const ident = readCssIdent(css, i + 1);
|
|
403
|
+
if (ident.value.toLowerCase() === "import") {
|
|
404
|
+
throw new Error('Theme CSS must not contain "@import" rules.');
|
|
405
|
+
}
|
|
406
|
+
i = Math.max(ident.end, i + 1);
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (ch === "\\" || isIdentStart(ch)) {
|
|
410
|
+
const ident = readCssIdent(css, i);
|
|
411
|
+
const lower = ident.value.toLowerCase();
|
|
412
|
+
let parenIndex = ident.end;
|
|
413
|
+
while (parenIndex < css.length && isWhitespace(css[parenIndex]))
|
|
414
|
+
parenIndex += 1;
|
|
415
|
+
if (css[parenIndex] === "(") {
|
|
416
|
+
if (lower === "url") {
|
|
417
|
+
const parsed = readUrlTarget(css, parenIndex);
|
|
418
|
+
if (!parsed) {
|
|
419
|
+
throw new Error("Theme CSS contains a malformed or unclosed url(...) reference.");
|
|
420
|
+
}
|
|
421
|
+
if (!/^data:/i.test(parsed.target)) {
|
|
422
|
+
throw new Error(`Theme CSS url(...) must only reference inline data: URLs (rejected: "${parsed.target.slice(0, 80)}").`);
|
|
423
|
+
}
|
|
424
|
+
i = parsed.end;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (RESTRICTED_IMAGE_FUNCTIONS.has(lower)) {
|
|
428
|
+
const closeIndex = findMatchingParen(css, parenIndex);
|
|
429
|
+
if (closeIndex === -1) {
|
|
430
|
+
throw new Error(`Theme CSS contains an unclosed ${lower}(...) call.`);
|
|
431
|
+
}
|
|
432
|
+
validateRestrictedImageCall(lower, css.slice(parenIndex + 1, closeIndex));
|
|
433
|
+
i = closeIndex + 1;
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
i = Math.max(ident.end, i + 1);
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
i += 1;
|
|
441
|
+
}
|
|
442
|
+
return css;
|
|
443
|
+
};
|
|
444
|
+
/** Strip CSS comments outside strings and `url(...)` tokens. */
|
|
445
|
+
const stripCssComments = (css) => {
|
|
446
|
+
let out = "";
|
|
447
|
+
let i = 0;
|
|
448
|
+
while (i < css.length) {
|
|
449
|
+
const ch = css[i];
|
|
450
|
+
if (ch === '"' || ch === "'") {
|
|
451
|
+
const str = readCssStringEnd(css, i);
|
|
452
|
+
const end = str ? str.end : css.length;
|
|
453
|
+
out += css.slice(i, end);
|
|
454
|
+
i = end;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if (ch === "/" && css[i + 1] === "*") {
|
|
458
|
+
const close = css.indexOf("*/", i + 2);
|
|
459
|
+
out += " ";
|
|
460
|
+
i = close === -1 ? css.length : close + 2;
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
const urlMatch = matchUrlCall(css, i);
|
|
464
|
+
if (urlMatch !== null && urlMatch.end !== -1) {
|
|
465
|
+
out += css.slice(i, urlMatch.end);
|
|
466
|
+
i = urlMatch.end;
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
if (ch === "\\" || isIdentStart(ch)) {
|
|
470
|
+
const end = Math.max(readCssIdent(css, i).end, i + 1);
|
|
471
|
+
out += css.slice(i, end);
|
|
472
|
+
i = end;
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
out += ch;
|
|
476
|
+
i += 1;
|
|
477
|
+
}
|
|
478
|
+
return out;
|
|
479
|
+
};
|
|
480
|
+
const splitTopLevel = (text, sep) => {
|
|
481
|
+
const parts = [];
|
|
482
|
+
let start = 0;
|
|
483
|
+
let parenDepth = 0;
|
|
484
|
+
let bracketDepth = 0;
|
|
485
|
+
let i = 0;
|
|
486
|
+
while (i < text.length) {
|
|
487
|
+
const ch = text[i];
|
|
488
|
+
if (ch === '"' || ch === "'") {
|
|
489
|
+
const str = readCssStringEnd(text, i);
|
|
490
|
+
i = str ? str.end : text.length;
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
if (ch === "(")
|
|
494
|
+
parenDepth += 1;
|
|
495
|
+
else if (ch === ")" && parenDepth > 0)
|
|
496
|
+
parenDepth -= 1;
|
|
497
|
+
else if (ch === "[")
|
|
498
|
+
bracketDepth += 1;
|
|
499
|
+
else if (ch === "]" && bracketDepth > 0)
|
|
500
|
+
bracketDepth -= 1;
|
|
501
|
+
else if (ch === sep && parenDepth === 0 && bracketDepth === 0) {
|
|
502
|
+
parts.push(text.slice(start, i));
|
|
503
|
+
start = i + 1;
|
|
504
|
+
}
|
|
505
|
+
i += 1;
|
|
506
|
+
}
|
|
507
|
+
parts.push(text.slice(start));
|
|
508
|
+
return parts;
|
|
509
|
+
};
|
|
510
|
+
const extractSubjectSelector = (selector) => {
|
|
511
|
+
let lastBoundary = -1;
|
|
512
|
+
let parenDepth = 0;
|
|
513
|
+
let bracketDepth = 0;
|
|
514
|
+
let i = 0;
|
|
515
|
+
while (i < selector.length) {
|
|
516
|
+
const ch = selector[i];
|
|
517
|
+
if (ch === '"' || ch === "'") {
|
|
518
|
+
const str = readCssStringEnd(selector, i);
|
|
519
|
+
i = str ? str.end : selector.length;
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
if (ch === "(")
|
|
523
|
+
parenDepth += 1;
|
|
524
|
+
else if (ch === ")" && parenDepth > 0)
|
|
525
|
+
parenDepth -= 1;
|
|
526
|
+
else if (ch === "[")
|
|
527
|
+
bracketDepth += 1;
|
|
528
|
+
else if (ch === "]" && bracketDepth > 0)
|
|
529
|
+
bracketDepth -= 1;
|
|
530
|
+
else if (parenDepth === 0 &&
|
|
531
|
+
bracketDepth === 0 &&
|
|
532
|
+
(isWhitespace(ch) || ch === ">" || ch === "+" || ch === "~")) {
|
|
533
|
+
lastBoundary = i;
|
|
534
|
+
}
|
|
535
|
+
i += 1;
|
|
536
|
+
}
|
|
537
|
+
return selector.slice(lastBoundary + 1).trim();
|
|
538
|
+
};
|
|
539
|
+
const subjectTargetsRoot = (subject, themeId) => {
|
|
540
|
+
if (subject === "")
|
|
541
|
+
return false;
|
|
542
|
+
const wrapped = /^:(?:is|where)\(([\s\S]+)\)$/i.exec(subject);
|
|
543
|
+
if (wrapped) {
|
|
544
|
+
return splitTopLevel(wrapped[1], ",").some((inner) => subjectTargetsRoot(extractSubjectSelector(inner.trim()), themeId));
|
|
545
|
+
}
|
|
546
|
+
const dataThemeMatches = [
|
|
547
|
+
...subject.matchAll(/\[\s*data-theme(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s\]]+)))?\s*\]/gi),
|
|
548
|
+
];
|
|
549
|
+
for (const match of dataThemeMatches) {
|
|
550
|
+
const attrVal = match[1] ?? match[2] ?? match[3];
|
|
551
|
+
if (themeId === undefined)
|
|
552
|
+
return false;
|
|
553
|
+
if (attrVal !== undefined && attrVal !== themeId)
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
if (themeId !== undefined && dataThemeMatches.length > 0) {
|
|
557
|
+
return true;
|
|
558
|
+
}
|
|
559
|
+
const stripped = subject.replace(/\[[^\]]*\]/g, "");
|
|
560
|
+
return /(?:^|(?<![a-zA-Z0-9_-]))(?::root|html)(?![a-zA-Z0-9_-])/i.test(stripped);
|
|
561
|
+
};
|
|
562
|
+
const selectorListTargetsRoot = (prelude, themeId) => splitTopLevel(prelude, ",").some((raw) => {
|
|
563
|
+
const trimmed = raw.trim();
|
|
564
|
+
if (trimmed === "")
|
|
565
|
+
return false;
|
|
566
|
+
return subjectTargetsRoot(extractSubjectSelector(trimmed), themeId);
|
|
567
|
+
});
|
|
568
|
+
const collectStDeclarations = (blockBody, out) => {
|
|
569
|
+
let depth = 0;
|
|
570
|
+
let i = 0;
|
|
571
|
+
let topLevel = "";
|
|
572
|
+
while (i < blockBody.length) {
|
|
573
|
+
const ch = blockBody[i];
|
|
574
|
+
if (ch === '"' || ch === "'") {
|
|
575
|
+
const str = readCssStringEnd(blockBody, i);
|
|
576
|
+
const end = str ? str.end : blockBody.length;
|
|
577
|
+
if (depth === 0)
|
|
578
|
+
topLevel += blockBody.slice(i, end);
|
|
579
|
+
i = end;
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
if (ch === "{")
|
|
583
|
+
depth += 1;
|
|
584
|
+
else if (ch === "}" && depth > 0)
|
|
585
|
+
depth -= 1;
|
|
586
|
+
else if (depth === 0)
|
|
587
|
+
topLevel += ch;
|
|
588
|
+
i += 1;
|
|
589
|
+
}
|
|
590
|
+
for (const match of topLevel.matchAll(/(?:^|[;\s])(--st-[a-zA-Z0-9_-]+)\s*:/g)) {
|
|
591
|
+
out.add(match[1]);
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
const walkCssBlocks = (src, themeId, out) => {
|
|
595
|
+
let i = 0;
|
|
596
|
+
let preludeStart = 0;
|
|
597
|
+
while (i < src.length) {
|
|
598
|
+
const ch = src[i];
|
|
599
|
+
if (ch === '"' || ch === "'") {
|
|
600
|
+
const str = readCssStringEnd(src, i);
|
|
601
|
+
i = str ? str.end : src.length;
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
if (ch === ";") {
|
|
605
|
+
preludeStart = i + 1;
|
|
606
|
+
i += 1;
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
if (ch === "{") {
|
|
610
|
+
const prelude = src.slice(preludeStart, i).trim();
|
|
611
|
+
let depth = 1;
|
|
612
|
+
let j = i + 1;
|
|
613
|
+
while (j < src.length && depth > 0) {
|
|
614
|
+
const inner = src[j];
|
|
615
|
+
if (inner === '"' || inner === "'") {
|
|
616
|
+
const str = readCssStringEnd(src, j);
|
|
617
|
+
j = str ? str.end : src.length;
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
if (inner === "{")
|
|
621
|
+
depth += 1;
|
|
622
|
+
else if (inner === "}")
|
|
623
|
+
depth -= 1;
|
|
624
|
+
j += 1;
|
|
625
|
+
}
|
|
626
|
+
const body = src.slice(i + 1, depth === 0 ? j - 1 : j);
|
|
627
|
+
if (prelude.startsWith("@")) {
|
|
628
|
+
const atName = /^@([a-zA-Z-]+)/.exec(prelude)?.[1]?.toLowerCase() ?? "";
|
|
629
|
+
if (CONDITIONAL_AT_RULES.has(atName)) {
|
|
630
|
+
walkCssBlocks(body, themeId, out);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
else if (selectorListTargetsRoot(prelude, themeId)) {
|
|
634
|
+
collectStDeclarations(body, out);
|
|
635
|
+
}
|
|
636
|
+
preludeStart = j;
|
|
637
|
+
i = j;
|
|
638
|
+
continue;
|
|
639
|
+
}
|
|
640
|
+
i += 1;
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
/**
|
|
644
|
+
* @internal
|
|
645
|
+
* Extract `--st-*` custom properties declared for the page root (`:root`, `html`, or
|
|
646
|
+
* `[data-theme="<themeId>"]` when `themeId` is provided). Used by portable DS hosts so
|
|
647
|
+
* root theme tokens inherit cleanly through `[data-st-theme]` wrappers.
|
|
648
|
+
*/
|
|
649
|
+
export const extractRootThemeCustomProperties = (css, themeId) => {
|
|
650
|
+
const out = new Set();
|
|
651
|
+
walkCssBlocks(stripCssComments(css), themeId, out);
|
|
652
|
+
return [...out];
|
|
653
|
+
};
|
|
654
|
+
//# sourceMappingURL=theme.js.map
|