@umber-style/core 0.0.0 → 0.1.0-alpha.1
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/LICENSE +21 -0
- package/README.md +13 -1
- package/dist/index.cjs +3741 -0
- package/dist/index.d.cts +1144 -0
- package/dist/index.d.mts +1144 -0
- package/dist/index.mjs +3670 -0
- package/package.json +66 -5
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,3670 @@
|
|
|
1
|
+
// src/standIn.ts
|
|
2
|
+
const BRAND$3 = Symbol.for("umber-style.stand-in");
|
|
3
|
+
/**
|
|
4
|
+
* A stand-in for the theme at `path` (a dotted path, '' for the theme itself): any key read from it is
|
|
5
|
+
* a stand-in one level down, a call is a stand-in for `path(arguments)` (a helper of the theme such as
|
|
6
|
+
* `breakpoints.up('md')`), a symbol reads as undefined, listing its keys throws, calling it with a function throws, and as text it is its path. `keyframes` and
|
|
7
|
+
* `globalStyles` name the styles of a function of the theme with it, so that the name does not depend on
|
|
8
|
+
* the theme's values.
|
|
9
|
+
*/
|
|
10
|
+
function createStandIn(path) {
|
|
11
|
+
const call = (...values) => {
|
|
12
|
+
if (values.some((value) => typeof value === "function" && !isStandIn(value))) throw new TypeError(`A function of the theme given to keyframes() or globalStyles() cannot pass a function to "${path}": its name is made before the theme is known. Read each token by its key.`);
|
|
13
|
+
return createStandIn(`${path}(${values.map(String).join(",")})`);
|
|
14
|
+
};
|
|
15
|
+
return new Proxy(call, {
|
|
16
|
+
get: (_target, key) => {
|
|
17
|
+
if (key === Symbol.toPrimitive) return () => path;
|
|
18
|
+
if (typeof key === "symbol") return void 0;
|
|
19
|
+
return createStandIn(path === "" ? key : `${path}.${key}`);
|
|
20
|
+
},
|
|
21
|
+
has: (_target, key) => key === BRAND$3,
|
|
22
|
+
ownKeys: () => {
|
|
23
|
+
throw new TypeError(`A function of the theme given to keyframes() or globalStyles() cannot list the keys of "${path}" (Object.keys, a spread, for...in): its name is made before the theme is known. Read each token by its key.`);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/** Whether `value` was made by `createStandIn`. */
|
|
28
|
+
function isStandIn(value) {
|
|
29
|
+
return typeof value === "function" && BRAND$3 in value;
|
|
30
|
+
}
|
|
31
|
+
/** `value` as its path when it is a stand-in, else `value` itself: a helper that computes with a token takes a stand-in as the text of a reference. */
|
|
32
|
+
function readStandIn(value) {
|
|
33
|
+
return isStandIn(value) ? String(value) : value;
|
|
34
|
+
}
|
|
35
|
+
// src/alpha.ts
|
|
36
|
+
const BRAND$2 = Symbol.for("umber-style.alpha-color");
|
|
37
|
+
const NUMBER$1 = String.raw`[+-]?(?:\d+\.?\d*|\.\d+)`;
|
|
38
|
+
const HEX_PATTERN = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
39
|
+
const FUNCTION_PATTERNS = [new RegExp(String.raw`^rgba?\(\s*(${NUMBER$1})\s*,\s*(${NUMBER$1})\s*,\s*(${NUMBER$1})\s*(?:,\s*(${NUMBER$1}%?)\s*)?\)$`, "i"), new RegExp(String.raw`^rgba?\(\s*(${NUMBER$1})\s+(${NUMBER$1})\s+(${NUMBER$1})\s*(?:/\s*(${NUMBER$1}%?)\s*)?\)$`, "i")];
|
|
40
|
+
/** Whether `value` was made by `alpha()` as a mix: a plain string is a color, not a fallback pair. */
|
|
41
|
+
function isAlphaColor(value) {
|
|
42
|
+
return typeof value === "object" && value !== null && BRAND$2 in value;
|
|
43
|
+
}
|
|
44
|
+
/** `value` limited to `low` through `high`. */
|
|
45
|
+
function clamp(value, low, high) {
|
|
46
|
+
return Math.min(high, Math.max(low, value));
|
|
47
|
+
}
|
|
48
|
+
/** `value` with four decimals at most, so that float noise (0.1 * 3) does not reach the CSS. */
|
|
49
|
+
function round(value) {
|
|
50
|
+
return Math.round(value * 1e4) / 1e4;
|
|
51
|
+
}
|
|
52
|
+
/** The channels of `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`, or undefined for another string. */
|
|
53
|
+
function readHex(color) {
|
|
54
|
+
if (!HEX_PATTERN.test(color)) return void 0;
|
|
55
|
+
const digits = color.slice(1);
|
|
56
|
+
const full = digits.length <= 4 ? [...digits].map((digit) => digit + digit).join("") : digits;
|
|
57
|
+
const [red, green, blue, opacity = 255] = [
|
|
58
|
+
0,
|
|
59
|
+
2,
|
|
60
|
+
4,
|
|
61
|
+
6
|
|
62
|
+
].slice(0, full.length / 2).map((start) => Number.parseInt(full.slice(start, start + 2), 16));
|
|
63
|
+
return {
|
|
64
|
+
red,
|
|
65
|
+
green,
|
|
66
|
+
blue,
|
|
67
|
+
opacity: opacity / 255
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** The channels of `rgb()` or `rgba()` with numbers (comma or space syntax), or undefined for another string. */
|
|
71
|
+
function readFunction(color) {
|
|
72
|
+
for (const pattern of FUNCTION_PATTERNS) {
|
|
73
|
+
const match = pattern.exec(color);
|
|
74
|
+
if (match === null) continue;
|
|
75
|
+
const [red, green, blue] = [
|
|
76
|
+
1,
|
|
77
|
+
2,
|
|
78
|
+
3
|
|
79
|
+
].map((index) => Math.round(clamp(Number.parseFloat(match[index]), 0, 255)));
|
|
80
|
+
const given = match[4] ?? "1";
|
|
81
|
+
return {
|
|
82
|
+
red,
|
|
83
|
+
green,
|
|
84
|
+
blue,
|
|
85
|
+
opacity: clamp(given.endsWith("%") ? Number.parseFloat(given) / 100 : Number.parseFloat(given), 0, 1)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* `color` at `opacity` as `rgba(r, g, b, a)`, when `color` is a literal that can be read (`#rgb`, `#rgba`,
|
|
91
|
+
* `#rrggbb`, `#rrggbbaa`, `rgb()` or `rgba()` with numbers; its own alpha is multiplied), else undefined.
|
|
92
|
+
*/
|
|
93
|
+
function toRgba(color, opacity) {
|
|
94
|
+
const channels = readHex(color.trim()) ?? readFunction(color.trim());
|
|
95
|
+
if (channels === void 0) return void 0;
|
|
96
|
+
const { red, green, blue } = channels;
|
|
97
|
+
return `rgba(${red}, ${green}, ${blue}, ${round(channels.opacity * opacity)})`;
|
|
98
|
+
}
|
|
99
|
+
/** Throws what `alpha()` throws: a `TypeError` for a color that is not a string or an opacity that is not a number, a `RangeError` for an opacity outside 0 to 1 or NaN. */
|
|
100
|
+
function assertAlphaInput(color, opacity) {
|
|
101
|
+
if (typeof color !== "string") throw new TypeError(`alpha(): the color must be a string, got ${typeof color}`);
|
|
102
|
+
if (typeof opacity !== "number") throw new TypeError(`alpha(): the opacity must be a number, got ${typeof opacity}`);
|
|
103
|
+
if (!(opacity >= 0 && opacity <= 1)) throw new RangeError(`alpha(): the opacity must be from 0 to 1, got ${opacity}`);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* `color` at `opacity` (0 to 1). Between 0 and 1 the result is an `AlphaColor`: written as a fallback
|
|
107
|
+
* (`rgba()` for a literal colour, else the colour itself) followed by `color-mix(in srgb, <color> <n>%,
|
|
108
|
+
* transparent)`, which works with var(--x) colors. 1 returns `color` and 0 returns `transparent`. A
|
|
109
|
+
* fallback is only written when the result is the whole value of a property; in a longer string
|
|
110
|
+
* (`1px solid ${alpha(c, 0.5)}`) it is the color-mix() form alone. Throws a `TypeError` for a color
|
|
111
|
+
* that is not a string or an opacity that is not a number, a `RangeError` for an opacity outside 0 to 1.
|
|
112
|
+
*/
|
|
113
|
+
function alpha(color, opacity) {
|
|
114
|
+
const text = readStandIn(color);
|
|
115
|
+
assertAlphaInput(text, opacity);
|
|
116
|
+
if (opacity === 1) return text;
|
|
117
|
+
if (opacity === 0) return "transparent";
|
|
118
|
+
const value = `color-mix(in srgb, ${text} ${Math.round(opacity * 1e4) / 100}%, transparent)`;
|
|
119
|
+
const mix = {};
|
|
120
|
+
Object.defineProperties(mix, {
|
|
121
|
+
fallback: {
|
|
122
|
+
value: toRgba(text, opacity) ?? text,
|
|
123
|
+
enumerable: true
|
|
124
|
+
},
|
|
125
|
+
value: {
|
|
126
|
+
value,
|
|
127
|
+
enumerable: true
|
|
128
|
+
},
|
|
129
|
+
toString: { value: () => value },
|
|
130
|
+
[BRAND$2]: { value: true }
|
|
131
|
+
});
|
|
132
|
+
return Object.freeze(mix);
|
|
133
|
+
}
|
|
134
|
+
// src/at.ts
|
|
135
|
+
/**
|
|
136
|
+
* `{ [key]: style }` typed so that it can be spread into a web style. A key that is computed in an
|
|
137
|
+
* object literal has the type `string`, and the web style type rejects it (its custom property
|
|
138
|
+
* signature takes no style); the key here keeps its template literal type, and `style` is checked as a
|
|
139
|
+
* style of its own: `{ ...at(`@media (width >= ${breakpoint})`, { gap: 16 }) }`.
|
|
140
|
+
*/
|
|
141
|
+
function at(key, style) {
|
|
142
|
+
return { [key]: style };
|
|
143
|
+
}
|
|
144
|
+
// src/cacheRegistry.ts
|
|
145
|
+
const registry$1 = /* @__PURE__ */ new WeakMap();
|
|
146
|
+
/** Records `cache` as the cache of its prefix in `page`, unless a cache of that prefix was recorded there before. */
|
|
147
|
+
function registerCache(page, cache) {
|
|
148
|
+
let caches = registry$1.get(page);
|
|
149
|
+
if (caches === void 0) {
|
|
150
|
+
caches = /* @__PURE__ */ new Map();
|
|
151
|
+
registry$1.set(page, caches);
|
|
152
|
+
}
|
|
153
|
+
if (!caches.has(cache.prefix)) caches.set(cache.prefix, cache);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The cache of `prefix` in `page`: the first cache of that prefix made with a sheet in a style element of
|
|
157
|
+
* that document (`createCache` records it), or undefined where none was made. It is how code that has no
|
|
158
|
+
* engine of its own (a component that a server component renders, in the browser) reaches the cache of the
|
|
159
|
+
* page, for example to release a global rule that a server sent.
|
|
160
|
+
*/
|
|
161
|
+
function findCache(prefix, page) {
|
|
162
|
+
return registry$1.get(page)?.get(prefix);
|
|
163
|
+
}
|
|
164
|
+
// src/hash.ts
|
|
165
|
+
const MULTIPLIER = 0x5bd1e995;
|
|
166
|
+
/** Multiplies `value` by the hash constant, kept to 32 bits. */
|
|
167
|
+
function multiply$1(value) {
|
|
168
|
+
return (value & 65535) * MULTIPLIER + ((value >>> 16) * 59797 << 16);
|
|
169
|
+
}
|
|
170
|
+
/** A short, stable hash of `text`, in base 36. */
|
|
171
|
+
function hashString(text) {
|
|
172
|
+
let hash = 0;
|
|
173
|
+
let index = 0;
|
|
174
|
+
let remaining = text.length;
|
|
175
|
+
const codeAt = (position) => text.codePointAt(position) & 255;
|
|
176
|
+
for (; remaining >= 4; index += 4, remaining -= 4) {
|
|
177
|
+
let chunk = codeAt(index) | codeAt(index + 1) << 8 | codeAt(index + 2) << 16 | codeAt(index + 3) << 24;
|
|
178
|
+
chunk = multiply$1(chunk);
|
|
179
|
+
chunk ^= chunk >>> 24;
|
|
180
|
+
hash = multiply$1(chunk) ^ multiply$1(hash);
|
|
181
|
+
}
|
|
182
|
+
if (remaining === 3) hash ^= codeAt(index + 2) << 16;
|
|
183
|
+
if (remaining >= 2) hash ^= codeAt(index + 1) << 8;
|
|
184
|
+
if (remaining >= 1) hash = multiply$1(hash ^ codeAt(index));
|
|
185
|
+
hash ^= hash >>> 13;
|
|
186
|
+
hash = multiply$1(hash);
|
|
187
|
+
return ((hash ^ hash >>> 15) >>> 0).toString(36);
|
|
188
|
+
}
|
|
189
|
+
// src/findMissingStyles.ts
|
|
190
|
+
const STATE_PATTERN = /:(hover|focus|active|focus-visible|focus-within)\b/;
|
|
191
|
+
const NAME_CODES = /[a-z0-9_-]/;
|
|
192
|
+
/** A selector that names the root element or the body: `html`, `body` or `:root` as a whole word. */
|
|
193
|
+
const ROOT_PATTERN = /(?:^|[\s>+~(,])(?:html|body)(?![\w-])|:root(?![\w-])/;
|
|
194
|
+
/** The prefix named by the first style element of an engine in `page`, or undefined. */
|
|
195
|
+
function findPrefix(page) {
|
|
196
|
+
const element = page.querySelector("style[data-style], style[data-precedence]");
|
|
197
|
+
return element?.dataset.style ?? element?.dataset.precedence;
|
|
198
|
+
}
|
|
199
|
+
/** Whether `rule` is a style rule. A rule of another document (an iframe's) is not an instance of this window's `CSSStyleRule`, so the name of its class is read instead. */
|
|
200
|
+
function isStyleRule(rule) {
|
|
201
|
+
return rule.constructor.name === "CSSStyleRule";
|
|
202
|
+
}
|
|
203
|
+
/** The style rules under `list`, groups included. */
|
|
204
|
+
function collectStyleRules(list, found) {
|
|
205
|
+
for (const rule of list) if (isStyleRule(rule)) found.push(rule);
|
|
206
|
+
else if ("cssRules" in rule) collectStyleRules(rule.cssRules, found);
|
|
207
|
+
return found;
|
|
208
|
+
}
|
|
209
|
+
/** The classes that start with `start` in `selector`. */
|
|
210
|
+
function readClassNames(selector, start) {
|
|
211
|
+
const names = [];
|
|
212
|
+
let from = selector.indexOf(`.${start}`);
|
|
213
|
+
while (from !== -1) {
|
|
214
|
+
let end = from + 1 + start.length;
|
|
215
|
+
while (end < selector.length && NAME_CODES.test(selector[end])) end++;
|
|
216
|
+
names.push(selector.slice(from + 1, end));
|
|
217
|
+
from = selector.indexOf(`.${start}`, end);
|
|
218
|
+
}
|
|
219
|
+
return names;
|
|
220
|
+
}
|
|
221
|
+
/** The variables that `text` refers to without a fallback: `var(--prefix-x)`. */
|
|
222
|
+
function readBareVariables(text, start) {
|
|
223
|
+
const names = [];
|
|
224
|
+
let from = text.indexOf("var(");
|
|
225
|
+
while (from !== -1) {
|
|
226
|
+
const begin = from + 4;
|
|
227
|
+
let end = begin + start.length;
|
|
228
|
+
if (text.startsWith(start, begin)) {
|
|
229
|
+
while (end < text.length && NAME_CODES.test(text[end])) end++;
|
|
230
|
+
if (text[end] === ")") names.push(text.slice(begin, end));
|
|
231
|
+
}
|
|
232
|
+
from = text.indexOf("var(", begin);
|
|
233
|
+
}
|
|
234
|
+
return names;
|
|
235
|
+
}
|
|
236
|
+
/** A short name of `element` for a message. */
|
|
237
|
+
function describe(element) {
|
|
238
|
+
const label = element.attributes.getNamedItem("data-testid")?.value ?? element.id;
|
|
239
|
+
return `${element.tagName.toLowerCase()}[${label}]`;
|
|
240
|
+
}
|
|
241
|
+
/** Whether `element` matches the selector of `rule`; a selector the browser cannot parse matches nothing. */
|
|
242
|
+
function isMatched(element, rule) {
|
|
243
|
+
try {
|
|
244
|
+
return element.matches(rule.selectorText);
|
|
245
|
+
} catch {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/** The rules of the engine's style elements in `page` (those of `data-umber` too), each with the variables it uses without a fallback. */
|
|
250
|
+
function collectRules(page, prefix) {
|
|
251
|
+
const selector = `style[data-style="${prefix}"], style[data-precedence="${prefix}"], style[data-umber~="${prefix}"], style[data-umber~="${prefix}-global"]`;
|
|
252
|
+
const rules = [];
|
|
253
|
+
for (const element of page.querySelectorAll(selector)) if (element.sheet) collectStyleRules(element.sheet.cssRules, rules);
|
|
254
|
+
return rules.map((rule) => ({
|
|
255
|
+
rule,
|
|
256
|
+
variables: readBareVariables(rule.cssText, `--${prefix}-`),
|
|
257
|
+
isState: STATE_PATTERN.test(rule.selectorText)
|
|
258
|
+
}));
|
|
259
|
+
}
|
|
260
|
+
/** The classes of the engine on the element called `label` that no rule names. */
|
|
261
|
+
function findClassProblems(label, classes, named, problems) {
|
|
262
|
+
for (const name of classes) {
|
|
263
|
+
if (named.has(name)) continue;
|
|
264
|
+
const message = `${label} has the class ${name}, which no rule names`;
|
|
265
|
+
problems.push({
|
|
266
|
+
kind: "class-without-rule",
|
|
267
|
+
name,
|
|
268
|
+
element: label,
|
|
269
|
+
message,
|
|
270
|
+
key: `class:${name}`
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
/** The variables used by a rule that matches `element` and that have no value on it. */
|
|
275
|
+
function findVariableProblems(element, label, rules, problems) {
|
|
276
|
+
const computed = getComputedStyle(element);
|
|
277
|
+
for (const { rule, variables, isState } of rules) {
|
|
278
|
+
if (variables.length === 0 || isState || !isMatched(element, rule)) continue;
|
|
279
|
+
for (const name of variables) {
|
|
280
|
+
if (computed.getPropertyValue(name).trim() !== "") continue;
|
|
281
|
+
const message = `${label} has no value for ${name}, which ${rule.selectorText} uses`;
|
|
282
|
+
const key = `variable:${name}:${rule.selectorText}`;
|
|
283
|
+
problems.push({
|
|
284
|
+
kind: "variable-without-value",
|
|
285
|
+
name,
|
|
286
|
+
element: label,
|
|
287
|
+
message,
|
|
288
|
+
key
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* The global rules (rules that name no class of the engine) for `html`, `body` or `:root` that match
|
|
295
|
+
* `element`, `<html>` or `<body>` without a class of the engine, and use variables that have no value
|
|
296
|
+
* on it: one problem for each rule, with all of those variables. A variable is declared on the element
|
|
297
|
+
* that carries a theme class and below it, so a rule for `html` or `body` sees the variables of a theme
|
|
298
|
+
* only when they are declared on `:root` (what the outermost provider does) or the theme class is on
|
|
299
|
+
* `<html>`; then there is no problem.
|
|
300
|
+
*/
|
|
301
|
+
function findRootProblems(element, label, rules, start, problems) {
|
|
302
|
+
const computed = getComputedStyle(element);
|
|
303
|
+
for (const { rule, variables, isState } of rules) {
|
|
304
|
+
const selector = rule.selectorText;
|
|
305
|
+
const isGlobal = readClassNames(selector, start).length === 0 && ROOT_PATTERN.test(selector);
|
|
306
|
+
if (variables.length === 0 || isState || !isGlobal || !isMatched(element, rule)) continue;
|
|
307
|
+
const missing = variables.filter((name) => computed.getPropertyValue(name).trim() === "");
|
|
308
|
+
if (missing.length === 0) continue;
|
|
309
|
+
const message = `${label} has no value for ${missing.join(", ")}, which the global rule ${selector} uses: a rule for html or body sees the variables of a theme declared on :root (the outermost provider) or on <html> (its theme class)`;
|
|
310
|
+
problems.push({
|
|
311
|
+
kind: "root-without-theme",
|
|
312
|
+
name: missing[0],
|
|
313
|
+
element: label,
|
|
314
|
+
message,
|
|
315
|
+
key: `root:${selector}`
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Looks at a page that an engine styled, and lists what is missing. For each element under `root`
|
|
321
|
+
* (the document by default; the style elements are read in the document of `root`): every class of the engine on it is named by at least one rule of the
|
|
322
|
+
* engine's style elements (its own, and the ones React hoists), and every variable of the engine that
|
|
323
|
+
* a rule matching the element uses, without a fallback, has a value on the element. The classes that
|
|
324
|
+
* are markers (`<prefix>-boundary`, `<prefix>-scheme-light` and `-dark`) have no rule of their own and
|
|
325
|
+
* are left out, and so is `<prefix>-theme-0`, the theme class of a theme that declares nothing, which has
|
|
326
|
+
* no rule by design; and so are the rules for `:hover`, `:focus` and the like. `<html>` and `<body>` without
|
|
327
|
+
* a class of the engine are checked for the global rules that name them (see `root-without-theme`). An
|
|
328
|
+
* empty list means nothing is missing. Where there is no document, or no style element of an engine,
|
|
329
|
+
* the list is empty.
|
|
330
|
+
*/
|
|
331
|
+
function findMissingStyles(root, options = {}) {
|
|
332
|
+
if (typeof document === "undefined") return [];
|
|
333
|
+
const scope = root ?? document;
|
|
334
|
+
const page = scope.ownerDocument ?? scope;
|
|
335
|
+
const prefix = options.prefix ?? findPrefix(page);
|
|
336
|
+
if (prefix === void 0) return [];
|
|
337
|
+
const rules = collectRules(page, prefix);
|
|
338
|
+
const named = new Set(rules.flatMap(({ rule }) => readClassNames(rule.selectorText, `${prefix}-`)));
|
|
339
|
+
const markers = /* @__PURE__ */ new Set([
|
|
340
|
+
`${prefix}-boundary`,
|
|
341
|
+
`${prefix}-scheme-light`,
|
|
342
|
+
`${prefix}-scheme-dark`,
|
|
343
|
+
`${prefix}-theme-0`
|
|
344
|
+
]);
|
|
345
|
+
const problems = [];
|
|
346
|
+
const elements = [...scope.nodeType === 1 ? [scope] : [], ...scope.querySelectorAll("*")];
|
|
347
|
+
for (const element of elements) {
|
|
348
|
+
const classes = [...element.classList].filter((name) => name.startsWith(`${prefix}-`) && !markers.has(name));
|
|
349
|
+
if (classes.length === 0) {
|
|
350
|
+
if (element === page.documentElement || element === page.body) findRootProblems(element, describe(element), rules, `${prefix}-`, problems);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
const label = describe(element);
|
|
354
|
+
findClassProblems(label, classes, named, problems);
|
|
355
|
+
findVariableProblems(element, label, rules, problems);
|
|
356
|
+
}
|
|
357
|
+
return problems;
|
|
358
|
+
}
|
|
359
|
+
// src/findUnsupportedDeclarations.ts
|
|
360
|
+
const KEYFRAMES_AT_RULE$1 = "@keyframes";
|
|
361
|
+
const IMPORTANT = "important";
|
|
362
|
+
/** A value that starts with a vendor prefix: a keyword or a function written for another browser. */
|
|
363
|
+
const VENDOR_VALUE = /^-(?:webkit|moz|ms|o)-/i;
|
|
364
|
+
/**
|
|
365
|
+
* `CSS.supports` of the browser, or undefined where there is none to trust: no `CSS` (a server), one
|
|
366
|
+
* that throws or answers two plain questions wrong, or jsdom, which answers from a table of its own and
|
|
367
|
+
* not from what a browser renders. The checker of a cache asks once, when it is made.
|
|
368
|
+
*/
|
|
369
|
+
function findBrowserSupports() {
|
|
370
|
+
try {
|
|
371
|
+
return !navigator.userAgent.includes("jsdom") && CSS.supports("color", "red") && !CSS.supports("color", "not-a-color") ? (property, value) => CSS.supports(property, value) : void 0;
|
|
372
|
+
} catch {
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/** The index after the string that starts at `index`, which is a quote. */
|
|
377
|
+
function skipString$1(text, index) {
|
|
378
|
+
const quote = text[index];
|
|
379
|
+
let position = index + 1;
|
|
380
|
+
while (position < text.length && text[position] !== quote) position += text[position] === "\\" ? 2 : 1;
|
|
381
|
+
return position + 1;
|
|
382
|
+
}
|
|
383
|
+
/** Opens a block at `index`, a `{`: its prelude is the text since `boundary`. */
|
|
384
|
+
function openBlock$2(stack, text, boundary, index) {
|
|
385
|
+
const parent = stack.at(-1);
|
|
386
|
+
if (parent) parent.hasChild = true;
|
|
387
|
+
stack.push({
|
|
388
|
+
prelude: text.slice(boundary, index).trim(),
|
|
389
|
+
bodyStart: index + 1,
|
|
390
|
+
hasChild: false
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
/** Closes the innermost block at `index`, a `}`: a block without a block of its own that is not an at-rule is a rule to add to `rules`. */
|
|
394
|
+
function closeBlock$2(stack, text, index, rules) {
|
|
395
|
+
const block = stack.pop();
|
|
396
|
+
if (!block || block.hasChild || block.prelude.startsWith("@")) return;
|
|
397
|
+
const parent = stack.at(-1)?.prelude ?? "";
|
|
398
|
+
rules.push({
|
|
399
|
+
selector: block.prelude,
|
|
400
|
+
parent,
|
|
401
|
+
body: text.slice(block.bodyStart, index)
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
/** The innermost blocks of `text` that are not at-rules: the rules that hold declarations. */
|
|
405
|
+
function readStyleRules(text) {
|
|
406
|
+
const rules = [];
|
|
407
|
+
const stack = [];
|
|
408
|
+
let boundary = 0;
|
|
409
|
+
let index = 0;
|
|
410
|
+
while (index < text.length) {
|
|
411
|
+
const char = text[index];
|
|
412
|
+
if (char === "\"" || char === "'") {
|
|
413
|
+
index = skipString$1(text, index);
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
if (char === ";") boundary = index + 1;
|
|
417
|
+
else if (char === "{") {
|
|
418
|
+
openBlock$2(stack, text, boundary, index);
|
|
419
|
+
boundary = index + 1;
|
|
420
|
+
} else if (char === "}") {
|
|
421
|
+
closeBlock$2(stack, text, index, rules);
|
|
422
|
+
boundary = index + 1;
|
|
423
|
+
}
|
|
424
|
+
index++;
|
|
425
|
+
}
|
|
426
|
+
return rules;
|
|
427
|
+
}
|
|
428
|
+
/** Adds the declaration `declaration` (`property:value`) to `declarations`, unless it has no ':'. */
|
|
429
|
+
function addDeclaration(declarations, declaration) {
|
|
430
|
+
const colon = declaration.indexOf(":");
|
|
431
|
+
if (colon === -1) return;
|
|
432
|
+
declarations.set(declaration.slice(0, colon).trim(), declaration.slice(colon + 1).trim());
|
|
433
|
+
}
|
|
434
|
+
/** The declarations of `body` by property, the last one of each: the items before it in a fallback list are meant to be dropped. */
|
|
435
|
+
function readLastDeclarations(body) {
|
|
436
|
+
const declarations = /* @__PURE__ */ new Map();
|
|
437
|
+
let depth = 0;
|
|
438
|
+
let start = 0;
|
|
439
|
+
let index = 0;
|
|
440
|
+
while (index <= body.length) {
|
|
441
|
+
const char = body[index];
|
|
442
|
+
if (char === "\"" || char === "'") {
|
|
443
|
+
index = skipString$1(body, index);
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
if (char === "(") depth++;
|
|
447
|
+
else if (char === ")") depth--;
|
|
448
|
+
else if (index === body.length || char === ";" && depth === 0) {
|
|
449
|
+
addDeclaration(declarations, body.slice(start, index));
|
|
450
|
+
start = index + 1;
|
|
451
|
+
}
|
|
452
|
+
index++;
|
|
453
|
+
}
|
|
454
|
+
return declarations;
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* `value` without a `!important` at its end (in any case, with whitespace around the `!`), and without
|
|
458
|
+
* the whitespace before it. A scan from the end: nothing is tried twice, so it takes linear time.
|
|
459
|
+
*/
|
|
460
|
+
function stripImportant(value) {
|
|
461
|
+
const trimmed = value.trimEnd();
|
|
462
|
+
if (trimmed.slice(-9).toLowerCase() !== IMPORTANT) return value;
|
|
463
|
+
const before = trimmed.slice(0, -9).trimEnd();
|
|
464
|
+
return before.endsWith("!") ? before.slice(0, -1).trimEnd() : value;
|
|
465
|
+
}
|
|
466
|
+
/** What a message names a rule by: its `@keyframes` rule, the last class of the engine in its selector, or its selector for a global rule. */
|
|
467
|
+
function describeRule({ selector, parent }, classPattern) {
|
|
468
|
+
if (parent.startsWith(KEYFRAMES_AT_RULE$1)) return `the keyframes ${parent.slice(10).trim()}`;
|
|
469
|
+
const name = [...selector.matchAll(classPattern)].at(-1)?.[1];
|
|
470
|
+
return name === void 0 ? `the global rule ${selector}` : `the class ${name}`;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Asks the browser about each declaration of `texts`, rules that an engine wrote, and returns a message
|
|
474
|
+
* for each one that it does not support. Only the last declaration of a property in a rule is asked
|
|
475
|
+
* about. A property that starts with a hyphen is left out: a custom property takes any value, and one
|
|
476
|
+
* with a vendor prefix is written for another browser, as is a value that starts with a vendor prefix
|
|
477
|
+
* (`-moz-available`, `-webkit-linear-gradient(...)`). For a value with `var(`, which is valid until it
|
|
478
|
+
* is used, the browser is asked whether it knows the property at all. `!important` is not part of the
|
|
479
|
+
* question. At-rules with descriptors (`@property`, `@font-face`) are not asked about.
|
|
480
|
+
*/
|
|
481
|
+
function findUnsupportedDeclarations(texts, prefix, supports) {
|
|
482
|
+
const classPattern = new RegExp(String.raw`\.(${prefix}-[a-z0-9_-]*)`, "g");
|
|
483
|
+
const messages = [];
|
|
484
|
+
for (const text of texts) for (const rule of readStyleRules(text)) addRuleMessages(rule, classPattern, supports, messages);
|
|
485
|
+
return messages;
|
|
486
|
+
}
|
|
487
|
+
/** Adds to `messages` a message for each last declaration of `rule` that the browser does not support (see `findUnsupportedDeclarations`). */
|
|
488
|
+
function addRuleMessages(rule, classPattern, supports, messages) {
|
|
489
|
+
for (const [name, value] of readLastDeclarations(rule.body)) {
|
|
490
|
+
const plain = stripImportant(value);
|
|
491
|
+
if (name.startsWith("-") || VENDOR_VALUE.test(plain)) continue;
|
|
492
|
+
if (supports(name, plain.includes("var(") ? "inherit" : plain)) continue;
|
|
493
|
+
const owner = describeRule(rule, classPattern);
|
|
494
|
+
messages.push(`${owner} declares ${name}:${value}, which this browser does not support`);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
// src/devChecks.ts
|
|
498
|
+
/** How long a check waits after the last insertion, in milliseconds: rules arrive in bursts while a page renders. */
|
|
499
|
+
const CHECK_DELAY = 250;
|
|
500
|
+
/** Whether this is a development build: `process.env.NODE_ENV` is `development`. Where `process` does not exist (a browser, no bundler) it is not. */
|
|
501
|
+
function isDevelopment() {
|
|
502
|
+
try {
|
|
503
|
+
return process.env.NODE_ENV === "development";
|
|
504
|
+
} catch {
|
|
505
|
+
return false;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* The function that asks for a check of `page` (the document by default): it runs `findMissingStyles` once, after the calls
|
|
510
|
+
* have stopped for a short while, and warns once for each problem (never again for the same one). The
|
|
511
|
+
* rules given to it are asked about in the same run: a declaration that the browser does not support
|
|
512
|
+
* (`findUnsupportedDeclarations`) is warned about once for each message. Where the browser cannot be
|
|
513
|
+
* asked (no `CSS.supports`, or jsdom) the rules are not kept.
|
|
514
|
+
*/
|
|
515
|
+
function createDevChecker(prefix, delay = CHECK_DELAY, page = document) {
|
|
516
|
+
const warned = /* @__PURE__ */ new Set();
|
|
517
|
+
const supports = findBrowserSupports();
|
|
518
|
+
const written = [];
|
|
519
|
+
let timer;
|
|
520
|
+
const warn = (key, message) => {
|
|
521
|
+
if (warned.has(key)) return;
|
|
522
|
+
warned.add(key);
|
|
523
|
+
console.warn(`[${prefix}] ${message}`);
|
|
524
|
+
};
|
|
525
|
+
const run = () => {
|
|
526
|
+
timer = void 0;
|
|
527
|
+
for (const problem of findMissingStyles(page, { prefix })) warn(problem.key, problem.message);
|
|
528
|
+
if (!supports) return;
|
|
529
|
+
for (const message of findUnsupportedDeclarations(written.splice(0), prefix, supports)) warn(`declaration:${message}`, message);
|
|
530
|
+
};
|
|
531
|
+
return (rules) => {
|
|
532
|
+
if (supports && rules) written.push(...rules);
|
|
533
|
+
timer ??= setTimeout(run, delay);
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
// src/fingerprint.ts
|
|
537
|
+
/**
|
|
538
|
+
* The short code each option has in the stamp, in the order they are written. An option added later has
|
|
539
|
+
* a third entry, the value it is left out with: the stamp of a page that does not turn it on stays the
|
|
540
|
+
* same text as before, and a stamp that lacks it reads as that value.
|
|
541
|
+
*/
|
|
542
|
+
const CODES = [
|
|
543
|
+
["p", "prefix"],
|
|
544
|
+
["x", "suffix"],
|
|
545
|
+
["l", "hasLabels"],
|
|
546
|
+
["y", "hasLayers"],
|
|
547
|
+
["r", "hasPruning"],
|
|
548
|
+
["b", "hasBaseClass"],
|
|
549
|
+
["s", "hasScope"],
|
|
550
|
+
["f", "hasColorFallbacks"],
|
|
551
|
+
[
|
|
552
|
+
"v",
|
|
553
|
+
"hasValueRepair",
|
|
554
|
+
"0"
|
|
555
|
+
],
|
|
556
|
+
["t", "tokens"]
|
|
557
|
+
];
|
|
558
|
+
/** The text of one option in the stamp: a flag is 1 or 0, a string is encoded. */
|
|
559
|
+
function toStampValue(value) {
|
|
560
|
+
if (typeof value !== "boolean") return encodeURIComponent(value);
|
|
561
|
+
return value ? "1" : "0";
|
|
562
|
+
}
|
|
563
|
+
/** The stamp of `options`, short enough for an attribute: `p=umber;x=;l=1;y=1;...`. */
|
|
564
|
+
function createFingerprint(options) {
|
|
565
|
+
return CODES.flatMap(([code, name, omitted]) => {
|
|
566
|
+
const value = toStampValue(options[name]);
|
|
567
|
+
return value === omitted ? [] : [`${code}=${value}`];
|
|
568
|
+
}).join(";");
|
|
569
|
+
}
|
|
570
|
+
/** The values of a stamp, by short code. */
|
|
571
|
+
function parseFingerprint(stamp) {
|
|
572
|
+
return new Map(stamp.split(";").map((part) => {
|
|
573
|
+
const at = part.indexOf("=");
|
|
574
|
+
return [part.slice(0, at), part.slice(at + 1)];
|
|
575
|
+
}));
|
|
576
|
+
}
|
|
577
|
+
/** The message that says which options differ between the stamp of the server and that of the browser, or '' when none. */
|
|
578
|
+
function describeFingerprintDifferences(server, browser) {
|
|
579
|
+
const theirs = parseFingerprint(server);
|
|
580
|
+
const ours = parseFingerprint(browser);
|
|
581
|
+
const differences = CODES.filter(([code, , omitted]) => (theirs.get(code) ?? omitted) !== (ours.get(code) ?? omitted)).map(([code, name, omitted]) => `${name} (server: ${decodeURIComponent(theirs.get(code) ?? omitted ?? "")}, browser: ${decodeURIComponent(ours.get(code) ?? omitted)})`);
|
|
582
|
+
if (differences.length === 0) return "";
|
|
583
|
+
return `UmberStyle: the server rendered this page with other options than the browser uses. Differing options: ${differences.join("; ")}. Create the style with the same options on both sides.`;
|
|
584
|
+
}
|
|
585
|
+
// src/calc.ts
|
|
586
|
+
const LENGTH_PATTERN$1 = /^(-?(?:\d+(?:\.\d*)?|\.\d+))([a-z]+|%)$/i;
|
|
587
|
+
const CALC_PREFIX = "calc(";
|
|
588
|
+
/** `value` rounded so that float noise (0.1 * 3) does not reach the CSS; `-0` is `0`. */
|
|
589
|
+
function tidy(value) {
|
|
590
|
+
return Math.round(value * 1e6) / 1e6 + 0;
|
|
591
|
+
}
|
|
592
|
+
/** Throws unless `value` is a finite number; `what` names the argument in the message. */
|
|
593
|
+
function assertFinite(name, what, value) {
|
|
594
|
+
if (typeof value !== "number") throw new TypeError(`${name}(): ${what} must be a number, got ${typeof value}`);
|
|
595
|
+
if (!Number.isFinite(value)) throw new RangeError(`${name}(): ${what} must be finite, got ${value}`);
|
|
596
|
+
}
|
|
597
|
+
/** Throws unless `value` is a string or a finite number. */
|
|
598
|
+
function assertOperand(name, value) {
|
|
599
|
+
if (isAlphaColor(value)) throw new TypeError(`${name}(): an alpha() color is not a length or a number, so it cannot be an operand`);
|
|
600
|
+
if (typeof value !== "string") assertFinite(name, "a value", value);
|
|
601
|
+
}
|
|
602
|
+
/** The inside of `value` when the whole of it is one `calc(...)`, else undefined. */
|
|
603
|
+
function unwrapCalc(value) {
|
|
604
|
+
if (!value.startsWith(CALC_PREFIX) || !value.endsWith(")")) return void 0;
|
|
605
|
+
let depth = 0;
|
|
606
|
+
for (let index = 4; index < value.length; index += 1) {
|
|
607
|
+
if (value[index] === "(") depth += 1;
|
|
608
|
+
if (value[index] === ")") depth -= 1;
|
|
609
|
+
if (depth === 0 && index < value.length - 1) return void 0;
|
|
610
|
+
}
|
|
611
|
+
return value.slice(5, -1);
|
|
612
|
+
}
|
|
613
|
+
/** `operand` as a part of a calc expression: a number is a length in px, an inner calc gets parentheses. */
|
|
614
|
+
function toTerm(operand) {
|
|
615
|
+
if (typeof operand === "number") return `${tidy(operand)}px`;
|
|
616
|
+
const inner = unwrapCalc(operand);
|
|
617
|
+
return inner === void 0 ? operand : `(${inner})`;
|
|
618
|
+
}
|
|
619
|
+
/** `operand` as a literal length (a number is in px), or undefined when it is not one. */
|
|
620
|
+
function toLength(operand) {
|
|
621
|
+
if (typeof operand === "number") return {
|
|
622
|
+
value: operand,
|
|
623
|
+
unit: "px"
|
|
624
|
+
};
|
|
625
|
+
const match = LENGTH_PATTERN$1.exec(operand.trim());
|
|
626
|
+
return match ? {
|
|
627
|
+
value: Number.parseFloat(match[1]),
|
|
628
|
+
unit: match[2].toLowerCase()
|
|
629
|
+
} : void 0;
|
|
630
|
+
}
|
|
631
|
+
const OPERATIONS = {
|
|
632
|
+
"*": (value, factor) => value * factor,
|
|
633
|
+
"/": (value, factor) => value / factor
|
|
634
|
+
};
|
|
635
|
+
/** `value` (a stand-in of `keyframes` as its path) multiplied or divided by `factor`, as a number, a literal length or a calc. */
|
|
636
|
+
function scale(name, given, factor, operator) {
|
|
637
|
+
const value = readStandIn(given);
|
|
638
|
+
assertOperand(name, value);
|
|
639
|
+
assertFinite(name, operator === "*" ? "the factor" : "the divisor", factor);
|
|
640
|
+
if (factor === 0) {
|
|
641
|
+
if (operator === "/") throw new RangeError(`${name}(): the divisor must not be 0`);
|
|
642
|
+
return 0;
|
|
643
|
+
}
|
|
644
|
+
if (factor === 1) return value;
|
|
645
|
+
if (typeof value === "number") return tidy(OPERATIONS[operator](value, factor));
|
|
646
|
+
const length = toLength(value);
|
|
647
|
+
return length === void 0 ? `${CALC_PREFIX}${toTerm(value)} ${operator} ${factor})` : `${tidy(OPERATIONS[operator](length.value, factor))}${length.unit}`;
|
|
648
|
+
}
|
|
649
|
+
/** The sum of `terms` when they are all numbers, else undefined. */
|
|
650
|
+
function sumNumbers(terms) {
|
|
651
|
+
let total = 0;
|
|
652
|
+
for (const { operand, sign } of terms) {
|
|
653
|
+
if (typeof operand !== "number") return void 0;
|
|
654
|
+
total += sign * operand;
|
|
655
|
+
}
|
|
656
|
+
return tidy(total);
|
|
657
|
+
}
|
|
658
|
+
/** The sum of `terms` when they are literal lengths of one unit, else undefined. */
|
|
659
|
+
function sumLengths(terms) {
|
|
660
|
+
const lengths = terms.map(({ operand }) => toLength(operand));
|
|
661
|
+
const unit = lengths[0]?.unit;
|
|
662
|
+
let total = 0;
|
|
663
|
+
for (const [index, length] of lengths.entries()) {
|
|
664
|
+
if (length === void 0 || length.unit !== unit) return void 0;
|
|
665
|
+
total += terms[index].sign * length.value;
|
|
666
|
+
}
|
|
667
|
+
return `${tidy(total)}${unit}`;
|
|
668
|
+
}
|
|
669
|
+
/** `terms` as one calc: the first term, then each with its sign. */
|
|
670
|
+
function toCalc(terms) {
|
|
671
|
+
const body = terms.map(({ operand, sign }, index) => {
|
|
672
|
+
const term = toTerm(operand);
|
|
673
|
+
if (index === 0) return term;
|
|
674
|
+
return `${sign === 1 ? "+" : "-"} ${term}`;
|
|
675
|
+
}).join(" ");
|
|
676
|
+
return `${CALC_PREFIX}${body})`;
|
|
677
|
+
}
|
|
678
|
+
/** `terms` added and subtracted, as a number, a literal length or one calc. */
|
|
679
|
+
function sum(terms) {
|
|
680
|
+
if (terms.length === 1) return terms[0].operand;
|
|
681
|
+
return sumNumbers(terms) ?? sumLengths(terms) ?? toCalc(terms);
|
|
682
|
+
}
|
|
683
|
+
/** The terms of `values` with `sign`, without the zeros that change nothing; each is checked, a stand-in as its path. */
|
|
684
|
+
function toTerms(name, given, sign) {
|
|
685
|
+
const values = given.map((value) => readStandIn(value));
|
|
686
|
+
for (const value of values) assertOperand(name, value);
|
|
687
|
+
return values.filter((value) => value !== 0).map((operand) => ({
|
|
688
|
+
operand,
|
|
689
|
+
sign
|
|
690
|
+
}));
|
|
691
|
+
}
|
|
692
|
+
function multiply(value, factor) {
|
|
693
|
+
return scale("multiply", value, factor, "*");
|
|
694
|
+
}
|
|
695
|
+
function divide(value, divisor) {
|
|
696
|
+
return scale("divide", value, divisor, "/");
|
|
697
|
+
}
|
|
698
|
+
function add(...values) {
|
|
699
|
+
const terms = toTerms("add", values, 1);
|
|
700
|
+
return terms.length === 0 ? 0 : sum(terms);
|
|
701
|
+
}
|
|
702
|
+
function subtract(value, ...values) {
|
|
703
|
+
const first = readStandIn(value);
|
|
704
|
+
assertOperand("subtract", first);
|
|
705
|
+
return sum([{
|
|
706
|
+
operand: first,
|
|
707
|
+
sign: 1
|
|
708
|
+
}, ...toTerms("subtract", values, -1)]);
|
|
709
|
+
}
|
|
710
|
+
function negate(value) {
|
|
711
|
+
return scale("negate", value, -1, "*");
|
|
712
|
+
}
|
|
713
|
+
// src/codeAt.ts
|
|
714
|
+
/** The code point at `index` of `text`; NaN past the end, so that every comparison with a code is false. */
|
|
715
|
+
function codeAt(text, index) {
|
|
716
|
+
return text.codePointAt(index) ?? NaN;
|
|
717
|
+
}
|
|
718
|
+
// src/scope.ts
|
|
719
|
+
/** The scoping of a cache that gave none: the default prefix, with `@scope`. */
|
|
720
|
+
const DEFAULT_SCOPING = {
|
|
721
|
+
prefix: "css",
|
|
722
|
+
hasScope: true
|
|
723
|
+
};
|
|
724
|
+
const THEME_KEY = "@theme ";
|
|
725
|
+
const SCHEME_KEY = "@scheme ";
|
|
726
|
+
/** The key for "the nearest theme is the one of this theme class". */
|
|
727
|
+
function createThemeKey(themeClass) {
|
|
728
|
+
return `${THEME_KEY}${themeClass}`;
|
|
729
|
+
}
|
|
730
|
+
/** The key for "the nearest color scheme is this one". */
|
|
731
|
+
function createSchemeKey(scheme) {
|
|
732
|
+
return `${SCHEME_KEY}${scheme}`;
|
|
733
|
+
}
|
|
734
|
+
/** The condition a style key stands for, or undefined for any other key. */
|
|
735
|
+
function readCondition(key) {
|
|
736
|
+
if (key.startsWith(THEME_KEY)) return {
|
|
737
|
+
kind: "theme",
|
|
738
|
+
id: key.slice(7)
|
|
739
|
+
};
|
|
740
|
+
if (key.startsWith(SCHEME_KEY)) {
|
|
741
|
+
const scheme = key.slice(8);
|
|
742
|
+
if (scheme === "light" || scheme === "dark") return {
|
|
743
|
+
kind: "scheme",
|
|
744
|
+
scheme
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
/** The class that every element with a theme carries, so that it can be a scope limit. */
|
|
749
|
+
function getBoundaryClass(prefix) {
|
|
750
|
+
return `${prefix}-boundary`;
|
|
751
|
+
}
|
|
752
|
+
/** The class of an element whose theme has a color scheme of its own. */
|
|
753
|
+
function getSchemeClass(prefix, scheme) {
|
|
754
|
+
return `${prefix}-scheme-${scheme}`;
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* The class string of a theme: its theme class, the boundary marker, and, when its color scheme is
|
|
758
|
+
* `light` or `dark`, the scheme marker. `light dark` and no scheme have no scheme marker.
|
|
759
|
+
*/
|
|
760
|
+
function createThemeClassName(prefix, id, colorScheme) {
|
|
761
|
+
const scheme = colorScheme === "light" || colorScheme === "dark" ? colorScheme : void 0;
|
|
762
|
+
return [
|
|
763
|
+
id,
|
|
764
|
+
getBoundaryClass(prefix),
|
|
765
|
+
scheme && getSchemeClass(prefix, scheme)
|
|
766
|
+
].filter(Boolean).join(" ");
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* `rules` for the elements below the elements that match `start`, down to but not into the elements
|
|
770
|
+
* that match `limits`: `@scope (start) to (limits){rules}`. A limit is relative to the scope root, so
|
|
771
|
+
* the root is never cut off by its own marker (checked in a browser: `to (.x)` and `to (:scope .x)` agree).
|
|
772
|
+
*/
|
|
773
|
+
function createScopeRule(start, limits, rules) {
|
|
774
|
+
return `@scope (${start}) to (${limits}){${rules.join("")}}`;
|
|
775
|
+
}
|
|
776
|
+
/** The selector that matches an element with no scheme marker on it or above it. Its specificity is 0. */
|
|
777
|
+
function createNoSchemeGuard(prefix) {
|
|
778
|
+
const markers = [getSchemeClass(prefix, "light"), getSchemeClass(prefix, "dark")].map((name) => `.${name}`);
|
|
779
|
+
const nested = markers.map((marker) => `${marker} *`);
|
|
780
|
+
return `:where(:not(${[...markers, ...nested].join(",")}))`;
|
|
781
|
+
}
|
|
782
|
+
// src/serialize.ts
|
|
783
|
+
const UNITLESS = /* @__PURE__ */ new Set([
|
|
784
|
+
"animationIterationCount",
|
|
785
|
+
"aspectRatio",
|
|
786
|
+
"columnCount",
|
|
787
|
+
"columns",
|
|
788
|
+
"flex",
|
|
789
|
+
"flexGrow",
|
|
790
|
+
"flexShrink",
|
|
791
|
+
"fontWeight",
|
|
792
|
+
"gridArea",
|
|
793
|
+
"gridColumn",
|
|
794
|
+
"gridColumnEnd",
|
|
795
|
+
"gridColumnStart",
|
|
796
|
+
"gridRow",
|
|
797
|
+
"gridRowEnd",
|
|
798
|
+
"gridRowStart",
|
|
799
|
+
"lineHeight",
|
|
800
|
+
"opacity",
|
|
801
|
+
"order",
|
|
802
|
+
"orphans",
|
|
803
|
+
"scale",
|
|
804
|
+
"tabSize",
|
|
805
|
+
"widows",
|
|
806
|
+
"zIndex",
|
|
807
|
+
"zoom",
|
|
808
|
+
"fillOpacity",
|
|
809
|
+
"floodOpacity",
|
|
810
|
+
"stopOpacity",
|
|
811
|
+
"strokeDasharray",
|
|
812
|
+
"strokeDashoffset",
|
|
813
|
+
"strokeMiterlimit",
|
|
814
|
+
"strokeOpacity",
|
|
815
|
+
"strokeWidth"
|
|
816
|
+
]);
|
|
817
|
+
/** The properties whose value is a time: a number is in milliseconds. */
|
|
818
|
+
const TIMES = /* @__PURE__ */ new Set([
|
|
819
|
+
"animationDelay",
|
|
820
|
+
"animationDuration",
|
|
821
|
+
"transitionDelay",
|
|
822
|
+
"transitionDuration"
|
|
823
|
+
]);
|
|
824
|
+
const propertyNames = /* @__PURE__ */ new Map();
|
|
825
|
+
/** Whether `value` is a nested style object, not an array, an `alpha()` result or a plain value. */
|
|
826
|
+
function isStyleObject(value) {
|
|
827
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && !isAlphaColor(value);
|
|
828
|
+
}
|
|
829
|
+
/** The CSS name of a style key: kebab-case, custom properties as written. */
|
|
830
|
+
function toPropertyName(key) {
|
|
831
|
+
if (key.startsWith("--")) return key;
|
|
832
|
+
let name = propertyNames.get(key);
|
|
833
|
+
if (name === void 0) {
|
|
834
|
+
name = key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`).replace(/^ms-/, "-ms-");
|
|
835
|
+
propertyNames.set(key, name);
|
|
836
|
+
}
|
|
837
|
+
return name;
|
|
838
|
+
}
|
|
839
|
+
const REFERENCE = "var(";
|
|
840
|
+
const HEX_PAIR = /^[0-9a-f]{2}$/i;
|
|
841
|
+
/** The units that make a reference a time: `var(--x)ms` is `calc(var(--x) * 1ms)`. */
|
|
842
|
+
const TIME_UNITS = ["ms", "s"];
|
|
843
|
+
/** Whether the character code `code` can be part of an identifier: a letter, a digit, `-`, `_` or any non-ASCII character. NaN (past either end of the text) cannot. */
|
|
844
|
+
function isNameCode$2(code) {
|
|
845
|
+
return code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122 || code === 45 || code === 95 || code >= 128;
|
|
846
|
+
}
|
|
847
|
+
/** Whether the character at `at` is a minus that stands alone before a reference: at the start of the value, or after a space, a comma or `(`. */
|
|
848
|
+
function isLoneMinus(value, at) {
|
|
849
|
+
return value[at] === "-" && (at === 0 || " ,(".includes(value[at - 1]));
|
|
850
|
+
}
|
|
851
|
+
/** The index just after the parenthesis that closes the one before `start`, or -1 when none does. */
|
|
852
|
+
function findClose(value, start) {
|
|
853
|
+
let depth = 1;
|
|
854
|
+
for (let index = start; index < value.length; index++) if (value[index] === "(") depth++;
|
|
855
|
+
else if (value[index] === ")" && --depth === 0) return index + 1;
|
|
856
|
+
return -1;
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* `reference` with what follows it at `at` taken in, and where the rest of the value starts: two hex
|
|
860
|
+
* digits of alpha are what `alpha()` writes for that opacity, a time unit makes a `calc()` of a time.
|
|
861
|
+
* The digits or the unit must end there: `var(--x)1Fa` is left alone.
|
|
862
|
+
*/
|
|
863
|
+
function repairSuffix(value, at, reference) {
|
|
864
|
+
const pair = value.slice(at, at + 2);
|
|
865
|
+
if (HEX_PAIR.test(pair) && !isNameCode$2(codeAt(value, at + 2))) return [String(alpha(reference, Number.parseInt(pair, 16) / 255)), at + 2];
|
|
866
|
+
for (const unit of TIME_UNITS) {
|
|
867
|
+
const end = at + unit.length;
|
|
868
|
+
if (value.slice(at, end).toLowerCase() === unit && !isNameCode$2(codeAt(value, end))) return [`calc(${reference} * 1${unit})`, end];
|
|
869
|
+
}
|
|
870
|
+
return [reference, at];
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
873
|
+
* `value` with the string forms of token maths that are never valid CSS rewritten: a reference followed
|
|
874
|
+
* by two hex digits (`var(--x)1F`) as `alpha()` writes it, a minus before a reference (`-var(--x)`) as
|
|
875
|
+
* `negate()` writes it, and a reference followed by `ms` or `s` as a `calc()` of a time. A reference may
|
|
876
|
+
* have a fallback, which is repaired too. Any other text is kept as it is.
|
|
877
|
+
*/
|
|
878
|
+
function repairValue(value) {
|
|
879
|
+
let repaired = "";
|
|
880
|
+
let from = 0;
|
|
881
|
+
let index = value.indexOf(REFERENCE);
|
|
882
|
+
while (index !== -1) {
|
|
883
|
+
const hasMinus = isLoneMinus(value, index - 1);
|
|
884
|
+
if (isNameCode$2(codeAt(value, index - 1)) && !hasMinus) {
|
|
885
|
+
index = value.indexOf(REFERENCE, index + 4);
|
|
886
|
+
continue;
|
|
887
|
+
}
|
|
888
|
+
const close = findClose(value, index + 4);
|
|
889
|
+
if (close === -1) break;
|
|
890
|
+
const inner = value.slice(index + 4, close - 1);
|
|
891
|
+
const [text, end] = repairSuffix(value, close, `${REFERENCE}${repairValue(inner)})`);
|
|
892
|
+
const start = hasMinus ? index - 1 : index;
|
|
893
|
+
repaired += value.slice(from, start) + (hasMinus ? String(negate(text)) : text);
|
|
894
|
+
from = end;
|
|
895
|
+
index = value.indexOf(REFERENCE, end);
|
|
896
|
+
}
|
|
897
|
+
return repaired + value.slice(from);
|
|
898
|
+
}
|
|
899
|
+
/**
|
|
900
|
+
* The CSS text of one declared value: a number is a time in `ms` (0 too: a time needs its unit), or a
|
|
901
|
+
* length in `px` unless it is 0 or the property takes a plain number. With `hasValueRepair` a string
|
|
902
|
+
* that holds a reference, outside a custom property, has its string forms of token maths rewritten.
|
|
903
|
+
*/
|
|
904
|
+
function toValue(key, value, hasValueRepair) {
|
|
905
|
+
if (typeof value === "string") {
|
|
906
|
+
if (!hasValueRepair || !value.includes(REFERENCE) || key.startsWith("--")) return value;
|
|
907
|
+
return repairValue(value);
|
|
908
|
+
}
|
|
909
|
+
if (TIMES.has(key)) return `${value}ms`;
|
|
910
|
+
return value !== 0 && !key.startsWith("--") && !UNITLESS.has(key) ? `${value}px` : String(value);
|
|
911
|
+
}
|
|
912
|
+
/** Splits `selector` on the commas that are not inside brackets or parentheses. */
|
|
913
|
+
function splitSelectors(selector) {
|
|
914
|
+
const parts = [];
|
|
915
|
+
let depth = 0;
|
|
916
|
+
let start = 0;
|
|
917
|
+
for (let index = 0; index < selector.length; index++) {
|
|
918
|
+
const char = selector[index];
|
|
919
|
+
if (char === "(" || char === "[") depth++;
|
|
920
|
+
else if (char === ")" || char === "]") depth--;
|
|
921
|
+
else if (char === "," && depth === 0) {
|
|
922
|
+
parts.push(selector.slice(start, index).trim());
|
|
923
|
+
start = index + 1;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
parts.push(selector.slice(start).trim());
|
|
927
|
+
return parts;
|
|
928
|
+
}
|
|
929
|
+
/** The selector of one part of a key inside one parent: `&` is the parent, a leading colon is the parent itself, anything else is a descendant. */
|
|
930
|
+
function joinSelector(one, part) {
|
|
931
|
+
if (part.includes("&")) return part.replaceAll("&", one);
|
|
932
|
+
return part.startsWith(":") ? one + part : `${one} ${part}`;
|
|
933
|
+
}
|
|
934
|
+
/** The selector a nested key makes inside `parent`, with `&` standing for the parent. */
|
|
935
|
+
function resolveSelector(parent, key) {
|
|
936
|
+
const parents = splitSelectors(parent);
|
|
937
|
+
return splitSelectors(key).flatMap((part) => parents.map((one) => joinSelector(one, part))).join(",");
|
|
938
|
+
}
|
|
939
|
+
/**
|
|
940
|
+
* `style` for the web: a style written for both platforms has a `web` part, which is merged over the
|
|
941
|
+
* rest, and a `native` part, which is dropped.
|
|
942
|
+
*/
|
|
943
|
+
function resolveWebParts(style) {
|
|
944
|
+
if (!("web" in style) && !("native" in style)) return style;
|
|
945
|
+
const { web, native: _native, ...rest } = style;
|
|
946
|
+
return isStyleObject(web) ? mergeStyles([rest, web]) : rest;
|
|
947
|
+
}
|
|
948
|
+
/** Merges style objects left to right; a later key wins and moves to the end; nested objects merge the same way. */
|
|
949
|
+
function mergeStyles(styles) {
|
|
950
|
+
const merged = {};
|
|
951
|
+
for (const style of styles) for (const [key, value] of Object.entries(resolveWebParts(style))) {
|
|
952
|
+
const earlier = merged[key];
|
|
953
|
+
delete merged[key];
|
|
954
|
+
merged[key] = isStyleObject(earlier) && isStyleObject(value) ? mergeStyles([earlier, value]) : value;
|
|
955
|
+
}
|
|
956
|
+
return merged;
|
|
957
|
+
}
|
|
958
|
+
/** `selector`, a list or one, with `text` in front of each of its parts. */
|
|
959
|
+
function prefixSelector(selector, text) {
|
|
960
|
+
return splitSelectors(selector).map((part) => text + part).join(",");
|
|
961
|
+
}
|
|
962
|
+
/** `selector`, a list or one, with `text` at the end of each of its parts. */
|
|
963
|
+
function suffixSelector(selector, text) {
|
|
964
|
+
return splitSelectors(selector).map((part) => part + text).join(",");
|
|
965
|
+
}
|
|
966
|
+
/**
|
|
967
|
+
* The rules of `value` for the elements below `start`, down to the elements that match `limits`: one
|
|
968
|
+
* `@scope` rule, or, without `hasScope`, the plain descendant selector (which does not stop at a limit).
|
|
969
|
+
*/
|
|
970
|
+
function compileScoped(selector, value, context, start, limits) {
|
|
971
|
+
if (!context.scoping.hasScope) return compileStyle(prefixSelector(selector, `:where(${start}) `), value, context);
|
|
972
|
+
const rules = compileStyle(selector, value, context);
|
|
973
|
+
return rules.length > 0 ? [createScopeRule(start, limits, rules)] : [];
|
|
974
|
+
}
|
|
975
|
+
/** The rules of a condition key: the style applies where the nearest theme, or the nearest color scheme, is the one named. */
|
|
976
|
+
function compileCondition(selector, condition, value, context) {
|
|
977
|
+
const { prefix } = context.scoping;
|
|
978
|
+
if (condition.kind === "theme") {
|
|
979
|
+
const limit = `.${getBoundaryClass(prefix)}`;
|
|
980
|
+
return compileScoped(selector, value, context, `.${condition.id}`, limit);
|
|
981
|
+
}
|
|
982
|
+
const limits = ["light", "dark"].map((name) => `.${getSchemeClass(prefix, name)}`);
|
|
983
|
+
const scoped = compileScoped(selector, value, context, `.${getSchemeClass(prefix, condition.scheme)}`, limits.join(","));
|
|
984
|
+
const guarded = compileStyle(suffixSelector(selector, createNoSchemeGuard(prefix)), value, context);
|
|
985
|
+
const query = `@media (prefers-color-scheme: ${condition.scheme}){${guarded.join("")}}`;
|
|
986
|
+
return guarded.length > 0 ? [...scoped, query] : scoped;
|
|
987
|
+
}
|
|
988
|
+
/** The rules of the nested style `value` under `key`: a condition scopes them, an at-rule wraps them, any other key is a selector inside `selector`. */
|
|
989
|
+
function compileNested(selector, key, value, context) {
|
|
990
|
+
const condition = readCondition(key);
|
|
991
|
+
if (condition) return compileCondition(selector, condition, value, context);
|
|
992
|
+
if (!key.startsWith("@")) return compileStyle(resolveSelector(selector, key), value, context);
|
|
993
|
+
return compileStyle(selector, value, context).map((rule) => `${key}{${rule}}`);
|
|
994
|
+
}
|
|
995
|
+
/** The declarations of one item: a custom property takes any text, so a fallback would always win there, and is not written. */
|
|
996
|
+
function toItemDeclarations(key, name, item, context) {
|
|
997
|
+
if (!isAlphaColor(item)) return `${name}:${toValue(key, item, context.hasValueRepair)};`;
|
|
998
|
+
return `${context.hasColorFallbacks && !key.startsWith("--") ? `${name}:${item.fallback};` : ""}${name}:${item.value};`;
|
|
999
|
+
}
|
|
1000
|
+
/** The declarations of one property: one for a value, one for each item of a fallback list. */
|
|
1001
|
+
function toDeclarations(key, value, context) {
|
|
1002
|
+
const name = toPropertyName(key);
|
|
1003
|
+
return (Array.isArray(value) ? value : [value]).map((item) => toItemDeclarations(key, name, item, context)).join("");
|
|
1004
|
+
}
|
|
1005
|
+
/** The rules of `style` for `selector`, with the options of `context`. */
|
|
1006
|
+
function compileStyle(selector, style, context) {
|
|
1007
|
+
let declarations = "";
|
|
1008
|
+
const nested = [];
|
|
1009
|
+
for (const [key, value] of Object.entries(style)) {
|
|
1010
|
+
if (key === "label" || value === null || value === void 0 || value === false) continue;
|
|
1011
|
+
if (isStyleObject(value)) nested.push(...compileNested(selector, key, value, context));
|
|
1012
|
+
else declarations += toDeclarations(key, value, context);
|
|
1013
|
+
}
|
|
1014
|
+
return declarations ? [`${selector}{${declarations}}`, ...nested] : nested;
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* The flat CSS rules of `style` for `selector`; without `hasColorFallbacks` an `alpha()` result is
|
|
1018
|
+
* written as its mix alone. A `when()` or `whenScheme()` key becomes a scoped rule, written as
|
|
1019
|
+
* `scoping` says. With `hasValueRepair` the string forms of token maths that are never valid CSS
|
|
1020
|
+
* (`var(--x)1F`, `-var(--x)`, `var(--x)ms`) are rewritten in each value.
|
|
1021
|
+
*/
|
|
1022
|
+
function compileRules(selector, style, hasColorFallbacks = true, scoping = DEFAULT_SCOPING, hasValueRepair = false) {
|
|
1023
|
+
return compileStyle(selector, style, {
|
|
1024
|
+
hasColorFallbacks,
|
|
1025
|
+
hasValueRepair,
|
|
1026
|
+
scoping
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
/** The label of `style` (its `label` key), or ''. */
|
|
1030
|
+
function readLabel(style) {
|
|
1031
|
+
const { label } = style;
|
|
1032
|
+
return typeof label === "string" ? label : "";
|
|
1033
|
+
}
|
|
1034
|
+
// src/sheetEntry.ts
|
|
1035
|
+
const BRAND$1 = Symbol.for("umber-style.sheet-entry");
|
|
1036
|
+
const SOURCE = Symbol.for("umber-style.sheet-entry-source");
|
|
1037
|
+
const ENTRY_KEY_PREFIX = "--umber-entry-";
|
|
1038
|
+
/**
|
|
1039
|
+
* Makes the entry of a class name: a frozen object that `isSheetEntry` knows and that prints as the
|
|
1040
|
+
* class name. Its one enumerable key is a custom property named after the class, with the value `null`,
|
|
1041
|
+
* so that a framework that merges the style objects of an array (Vue does) keeps every entry, and
|
|
1042
|
+
* `flattenStyle` finds them by the key. Both frameworks skip a `null` style value on a plain element, on
|
|
1043
|
+
* the client and on the server, so an entry given to a plain element renders no style attribute. With a
|
|
1044
|
+
* `source` (a cache made it) the class name is read through it, so that the rule is written when the entry
|
|
1045
|
+
* is used, not when it is made; without one the entry is just the name.
|
|
1046
|
+
*/
|
|
1047
|
+
function createSheetEntry(className, source) {
|
|
1048
|
+
const entry = {
|
|
1049
|
+
[`${ENTRY_KEY_PREFIX}${className}`]: null,
|
|
1050
|
+
[BRAND$1]: true
|
|
1051
|
+
};
|
|
1052
|
+
const read = () => source?.use("components") ?? className;
|
|
1053
|
+
Object.defineProperties(entry, {
|
|
1054
|
+
className: { get: read },
|
|
1055
|
+
toString: { value: read },
|
|
1056
|
+
...source ? { [SOURCE]: { value: source } } : {}
|
|
1057
|
+
});
|
|
1058
|
+
return Object.freeze(entry);
|
|
1059
|
+
}
|
|
1060
|
+
/** Whether `value` was made by `createSheetEntry`: a class name is a plain string, a style object has no brand. */
|
|
1061
|
+
function isSheetEntry(value) {
|
|
1062
|
+
return typeof value === "object" && value !== null && BRAND$1 in value;
|
|
1063
|
+
}
|
|
1064
|
+
/** What a cache gave `entry` to make its classes, or undefined for an entry that is only a name. */
|
|
1065
|
+
function readEntrySource(entry) {
|
|
1066
|
+
return entry[SOURCE];
|
|
1067
|
+
}
|
|
1068
|
+
/** The cache that made the first entry among `values` that a cache made, or undefined when there is none. */
|
|
1069
|
+
function findEntryCache(values) {
|
|
1070
|
+
for (const value of values) {
|
|
1071
|
+
const source = isSheetEntry(value) ? readEntrySource(value) : void 0;
|
|
1072
|
+
if (source) return source.cache;
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* The style of `value` when it is a sheet entry, else `value` as it is: for a place that takes a style
|
|
1077
|
+
* object and so also an entry. Throws a `TypeError`, that names `caller`, for an entry that is only a class
|
|
1078
|
+
* name (one that no cache made), whose style is not known.
|
|
1079
|
+
*/
|
|
1080
|
+
function readEntryStyle(value, caller) {
|
|
1081
|
+
if (!isSheetEntry(value)) return value;
|
|
1082
|
+
const source = readEntrySource(value);
|
|
1083
|
+
if (!source) throw new TypeError(`${caller}(): the sheet entry "${value.className}" is only a class name, so its style cannot be merged.`);
|
|
1084
|
+
return source.style;
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* What a `style` prop holds, as a flat list in order, for the cache to merge into one class: sheet
|
|
1088
|
+
* entries and plain style objects, with arrays flattened and what is empty dropped. An object in which a
|
|
1089
|
+
* framework merged entries and plain keys (Vue does) is split back into its entries, then the rest; an
|
|
1090
|
+
* entry is looked up by its class name with `findEntry` (a cache's `findEntry`), else it is only the name.
|
|
1091
|
+
* A string is an error: a text style cannot be merged into a class.
|
|
1092
|
+
*/
|
|
1093
|
+
function flattenStyle(style, findEntry) {
|
|
1094
|
+
const flat = [];
|
|
1095
|
+
const visit = (value) => {
|
|
1096
|
+
if (Array.isArray(value)) value.forEach(visit);
|
|
1097
|
+
else if (typeof value === "string" && value) throw new Error("A style given as a string cannot be merged into a class: use a sheet entry or an object.");
|
|
1098
|
+
else if (isSheetEntry(value) && readEntrySource(value)) flat.push(value);
|
|
1099
|
+
else if (typeof value === "object" && value !== null) {
|
|
1100
|
+
const rest = {};
|
|
1101
|
+
for (const [key, item] of Object.entries(value)) if (key.startsWith(ENTRY_KEY_PREFIX)) {
|
|
1102
|
+
const name = key.slice(14);
|
|
1103
|
+
flat.push(findEntry?.(name) ?? createSheetEntry(name));
|
|
1104
|
+
} else rest[key] = item;
|
|
1105
|
+
if (Object.keys(rest).length > 0) flat.push(rest);
|
|
1106
|
+
}
|
|
1107
|
+
};
|
|
1108
|
+
visit(style);
|
|
1109
|
+
return flat;
|
|
1110
|
+
}
|
|
1111
|
+
// src/templateStyle.ts
|
|
1112
|
+
/** The character that stands for an interpolation in the joined text of a template: CSS never holds it. */
|
|
1113
|
+
const HOLE = "\0";
|
|
1114
|
+
/**
|
|
1115
|
+
* After an interpolation that starts a statement: a newline before anything but spaces and a line
|
|
1116
|
+
* comment, then a character that does not go on with a selector or a property name. The interpolation is
|
|
1117
|
+
* then a statement of its own, as `${mixin}` alone on its line is.
|
|
1118
|
+
*/
|
|
1119
|
+
const OWN_LINE = /[^\S\n]*(?:\/\/.*)?\n\s*[^\s{,:>+~]/y;
|
|
1120
|
+
const templates = /* @__PURE__ */ new WeakMap();
|
|
1121
|
+
/** Throws the `TypeError` of a template that cannot be read: `problem`, with the line and the text around `at`. */
|
|
1122
|
+
function fail(text, at, problem) {
|
|
1123
|
+
const line = text.slice(0, at).split("\n").length;
|
|
1124
|
+
const around = text.slice(Math.max(0, at - 30), at + 10).replaceAll(HOLE, `\${}`).replace(/\s+/g, " ").trim();
|
|
1125
|
+
throw new TypeError(`parseStyleTemplate(): ${problem} at line ${line}: "${around}"`);
|
|
1126
|
+
}
|
|
1127
|
+
/** The style key of a CSS property name: camelCase (`-webkit-x` is `WebkitX`, written back as it was); a custom property as it is. */
|
|
1128
|
+
function toKey(name) {
|
|
1129
|
+
if (name.startsWith("--")) return name;
|
|
1130
|
+
return name.toLowerCase().replace(/-([a-z])/g, (_match, letter) => letter.toUpperCase());
|
|
1131
|
+
}
|
|
1132
|
+
/** `pieces` as one text: adjacent static text joined, trimmed at both ends; a string when no interpolation is left. */
|
|
1133
|
+
function join(pieces) {
|
|
1134
|
+
const joined = [];
|
|
1135
|
+
let text = "";
|
|
1136
|
+
for (const piece of pieces) if (typeof piece === "string") text += piece;
|
|
1137
|
+
else {
|
|
1138
|
+
joined.push(text, piece);
|
|
1139
|
+
text = "";
|
|
1140
|
+
}
|
|
1141
|
+
joined.push(text);
|
|
1142
|
+
joined[0] = joined[0].trimStart();
|
|
1143
|
+
joined[joined.length - 1] = joined.at(-1).trimEnd();
|
|
1144
|
+
return joined.length === 1 ? joined[0] : joined.filter((piece) => piece !== "");
|
|
1145
|
+
}
|
|
1146
|
+
/** Ends the text not yet in the pieces of the statement: it becomes a piece unless it is only whitespace at the start. */
|
|
1147
|
+
function flush(state) {
|
|
1148
|
+
if (state.pieces.length > 0 || state.chunk.trim()) state.pieces.push(state.chunk);
|
|
1149
|
+
state.chunk = "";
|
|
1150
|
+
}
|
|
1151
|
+
/** Adds a space to the statement, unless it already ends with one. */
|
|
1152
|
+
function space(state) {
|
|
1153
|
+
if (!state.chunk.endsWith(" ")) state.chunk += " ";
|
|
1154
|
+
}
|
|
1155
|
+
/** Counts the holes from `from` to `to` as read: they are in a comment, and stand for nothing. */
|
|
1156
|
+
function skip(state, from, to) {
|
|
1157
|
+
state.hole += state.text.slice(from, to).split(HOLE).length - 1;
|
|
1158
|
+
}
|
|
1159
|
+
/** Adds the next interpolation to the pieces of the statement. */
|
|
1160
|
+
function addHole(state) {
|
|
1161
|
+
flush(state);
|
|
1162
|
+
state.pieces.push(state.hole++);
|
|
1163
|
+
}
|
|
1164
|
+
/** Adds the declaration that was read to the block: the pieces before its first ':' are its key, the rest its value. */
|
|
1165
|
+
function endDeclaration(state, at) {
|
|
1166
|
+
const key = join(state.pieces.slice(0, state.split));
|
|
1167
|
+
if (key === "") fail(state.text, at, "a declaration without a property");
|
|
1168
|
+
state.body.push({
|
|
1169
|
+
key: typeof key === "string" ? toKey(key) : key,
|
|
1170
|
+
value: join(state.pieces.slice(state.split + 1))
|
|
1171
|
+
});
|
|
1172
|
+
}
|
|
1173
|
+
/** Adds the interpolations that were read without a ':' to the block, each a statement of its own; any other text is a declaration without one. */
|
|
1174
|
+
function endInterpolations(state, at) {
|
|
1175
|
+
for (const piece of state.pieces) if (typeof piece === "number") state.body.push(piece);
|
|
1176
|
+
else if (piece.trim()) fail(state.text, at, "a declaration without \":\"");
|
|
1177
|
+
}
|
|
1178
|
+
/** Ends the statement being read at `at`, and starts the next one. */
|
|
1179
|
+
function end(state, at) {
|
|
1180
|
+
flush(state);
|
|
1181
|
+
if (state.split >= 0) endDeclaration(state, at);
|
|
1182
|
+
else endInterpolations(state, at);
|
|
1183
|
+
state.pieces = [];
|
|
1184
|
+
state.split = -1;
|
|
1185
|
+
}
|
|
1186
|
+
/** Reads the interpolation at `index`; one that starts a statement and stands on its own line ends it. Returns the index after it. */
|
|
1187
|
+
function readHole(state, index) {
|
|
1188
|
+
addHole(state);
|
|
1189
|
+
if (state.pieces.length === 1) {
|
|
1190
|
+
OWN_LINE.lastIndex = index + 1;
|
|
1191
|
+
if (OWN_LINE.test(state.text)) end(state, index);
|
|
1192
|
+
}
|
|
1193
|
+
return index + 1;
|
|
1194
|
+
}
|
|
1195
|
+
/** Copies the character at `index` into the statement, and the next one when it is an escape: `HOLE` and past the end are not above it, so a hole is not kept. Returns the index after them. */
|
|
1196
|
+
function readEscaped(state, index) {
|
|
1197
|
+
const { text } = state;
|
|
1198
|
+
const length = text[index] === "\\" && text[index + 1] > HOLE ? 2 : 1;
|
|
1199
|
+
state.chunk += text.slice(index, index + length);
|
|
1200
|
+
return index + length;
|
|
1201
|
+
}
|
|
1202
|
+
/** Reads the string that starts at `index`, with the interpolations in it. Returns the index after its closing quote. */
|
|
1203
|
+
function readString(state, index) {
|
|
1204
|
+
const { text } = state;
|
|
1205
|
+
const quote = text[index];
|
|
1206
|
+
state.chunk += quote;
|
|
1207
|
+
let at = index + 1;
|
|
1208
|
+
while (text[at] !== quote) {
|
|
1209
|
+
if (at >= text.length) fail(text, index, "an unclosed string");
|
|
1210
|
+
if (text[at] === HOLE) {
|
|
1211
|
+
addHole(state);
|
|
1212
|
+
at++;
|
|
1213
|
+
} else at = readEscaped(state, at);
|
|
1214
|
+
}
|
|
1215
|
+
state.chunk += quote;
|
|
1216
|
+
return at + 1;
|
|
1217
|
+
}
|
|
1218
|
+
/** Reads the comment that starts at `index`: it is a space. Returns the index after its `*` and `/`. */
|
|
1219
|
+
function readComment$1(state, index) {
|
|
1220
|
+
const close = state.text.indexOf("*/", index + 2);
|
|
1221
|
+
if (close < 0) fail(state.text, index, "an unclosed comment");
|
|
1222
|
+
skip(state, index, close);
|
|
1223
|
+
space(state);
|
|
1224
|
+
return close + 2;
|
|
1225
|
+
}
|
|
1226
|
+
/** Reads the line comment that starts at `index`. Returns the index of the newline that ends it, or the end of the text. */
|
|
1227
|
+
function readLineComment(state, index) {
|
|
1228
|
+
const stop = state.text.indexOf("\n", index);
|
|
1229
|
+
const close = stop < 0 ? state.text.length : stop;
|
|
1230
|
+
skip(state, index, close);
|
|
1231
|
+
return close;
|
|
1232
|
+
}
|
|
1233
|
+
/** Reads the `/` at `index`: the start of a comment, or a character of the statement. Returns the index after what it read. */
|
|
1234
|
+
function readSlash(state, index) {
|
|
1235
|
+
const next = state.text[index + 1];
|
|
1236
|
+
if (next === "*") return readComment$1(state, index);
|
|
1237
|
+
if (next === "/" && state.depth === 0) return readLineComment(state, index);
|
|
1238
|
+
state.chunk += "/";
|
|
1239
|
+
return index + 1;
|
|
1240
|
+
}
|
|
1241
|
+
/** Reads `char` at `index` inside parentheses, or the `(` that opens them: it is a character of the statement. */
|
|
1242
|
+
function readParenthesized(state, char, index) {
|
|
1243
|
+
if (char === "(" && state.depth++ === 0) state.parenAt = index;
|
|
1244
|
+
else if (char === ")") state.depth--;
|
|
1245
|
+
state.chunk += char;
|
|
1246
|
+
}
|
|
1247
|
+
/** Reads the first ':' of a statement: what is before it is the key of a declaration. */
|
|
1248
|
+
function readColon(state) {
|
|
1249
|
+
flush(state);
|
|
1250
|
+
state.split = state.pieces.length;
|
|
1251
|
+
state.pieces.push(":");
|
|
1252
|
+
}
|
|
1253
|
+
/** Reads the `{` at `index`: the statement read so far is the prelude of a block, which is open until its `}`. */
|
|
1254
|
+
function openBlock$1(state, index) {
|
|
1255
|
+
flush(state);
|
|
1256
|
+
const prelude = join(state.pieces);
|
|
1257
|
+
if (prelude === "") fail(state.text, index, "a block without a selector");
|
|
1258
|
+
const block = {
|
|
1259
|
+
prelude,
|
|
1260
|
+
body: []
|
|
1261
|
+
};
|
|
1262
|
+
state.body.push(block);
|
|
1263
|
+
state.open.push([state.body, index]);
|
|
1264
|
+
state.body = block.body;
|
|
1265
|
+
state.pieces = [];
|
|
1266
|
+
state.split = -1;
|
|
1267
|
+
}
|
|
1268
|
+
/** Reads the `}` at `index`: it ends the statement being read and the innermost open block. */
|
|
1269
|
+
function closeBlock$1(state, index) {
|
|
1270
|
+
end(state, index);
|
|
1271
|
+
const parent = state.open.pop();
|
|
1272
|
+
if (!parent) fail(state.text, index, "a \"}\" without a \"{\"");
|
|
1273
|
+
state.body = parent[0];
|
|
1274
|
+
}
|
|
1275
|
+
/** Reads `char` at `index`, outside strings, comments and parentheses. */
|
|
1276
|
+
function readStatementChar(state, char, index) {
|
|
1277
|
+
switch (char) {
|
|
1278
|
+
case ":":
|
|
1279
|
+
if (state.split < 0) readColon(state);
|
|
1280
|
+
else state.chunk += char;
|
|
1281
|
+
break;
|
|
1282
|
+
case ";":
|
|
1283
|
+
end(state, index);
|
|
1284
|
+
break;
|
|
1285
|
+
case "{":
|
|
1286
|
+
openBlock$1(state, index);
|
|
1287
|
+
break;
|
|
1288
|
+
case "}":
|
|
1289
|
+
closeBlock$1(state, index);
|
|
1290
|
+
break;
|
|
1291
|
+
default: state.chunk += char;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
/** Reads `char` at `index`, which starts nothing longer: whitespace, a character in parentheses, or one of a statement. */
|
|
1295
|
+
function readChar(state, char, index) {
|
|
1296
|
+
if (char <= " ") space(state);
|
|
1297
|
+
else if (state.depth > 0 || char === "(") readParenthesized(state, char, index);
|
|
1298
|
+
else readStatementChar(state, char, index);
|
|
1299
|
+
}
|
|
1300
|
+
/** Reads what starts at `index`: an interpolation, a string, an escape, a comment, or one character. Returns the index after it. */
|
|
1301
|
+
function readNext(state, index) {
|
|
1302
|
+
const char = state.text[index];
|
|
1303
|
+
switch (char) {
|
|
1304
|
+
case HOLE: return readHole(state, index);
|
|
1305
|
+
case "\"":
|
|
1306
|
+
case "'": return readString(state, index);
|
|
1307
|
+
case "\\": return readEscaped(state, index);
|
|
1308
|
+
case "/": return readSlash(state, index);
|
|
1309
|
+
default:
|
|
1310
|
+
readChar(state, char, index);
|
|
1311
|
+
return index + 1;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
/**
|
|
1315
|
+
* The statements of `text`, a template's strings joined with `HOLE`, in one pass. Outside strings and
|
|
1316
|
+
* comments a run of whitespace is one space; a comment is a space; the holes in a comment are skipped.
|
|
1317
|
+
*/
|
|
1318
|
+
function parse(text) {
|
|
1319
|
+
const root = [];
|
|
1320
|
+
const state = {
|
|
1321
|
+
text,
|
|
1322
|
+
open: [],
|
|
1323
|
+
body: root,
|
|
1324
|
+
pieces: [],
|
|
1325
|
+
chunk: "",
|
|
1326
|
+
split: -1,
|
|
1327
|
+
hole: 0,
|
|
1328
|
+
depth: 0,
|
|
1329
|
+
parenAt: 0
|
|
1330
|
+
};
|
|
1331
|
+
let index = 0;
|
|
1332
|
+
while (index < text.length) index = readNext(state, index);
|
|
1333
|
+
if (state.depth > 0) fail(text, state.parenAt, "an unclosed \"(\"");
|
|
1334
|
+
end(state, text.length);
|
|
1335
|
+
const unclosed = state.open.pop();
|
|
1336
|
+
if (unclosed) fail(text, unclosed[1], "an unclosed block");
|
|
1337
|
+
return root;
|
|
1338
|
+
}
|
|
1339
|
+
/** Whether `value` is a function of the props: not one that prints as text of its own (a stand-in for the theme, a component that is a selector). */
|
|
1340
|
+
function isCallable(value) {
|
|
1341
|
+
return typeof value === "function" && value.toString === Function.prototype.toString;
|
|
1342
|
+
}
|
|
1343
|
+
/** Whether `value`, or an item of it, is a function of the props. */
|
|
1344
|
+
function isDynamic(value) {
|
|
1345
|
+
return isCallable(value) || Array.isArray(value) && value.some(isDynamic);
|
|
1346
|
+
}
|
|
1347
|
+
/** `value` with each function called with `props`, until it is not a function. */
|
|
1348
|
+
function resolve(value, props) {
|
|
1349
|
+
let resolved = value;
|
|
1350
|
+
while (isCallable(resolved)) resolved = resolved(props);
|
|
1351
|
+
return resolved;
|
|
1352
|
+
}
|
|
1353
|
+
/** Whether `value` is a plain object, not one with a text of its own (a sheet entry, an `alpha()` result). */
|
|
1354
|
+
function isPlainObject$2(value) {
|
|
1355
|
+
return typeof value === "object" && value !== null && value.toString === Object.prototype.toString;
|
|
1356
|
+
}
|
|
1357
|
+
/** An interpolation as text in a value, a selector or a property name. */
|
|
1358
|
+
function toText$1(value, props) {
|
|
1359
|
+
const resolved = resolve(value, props);
|
|
1360
|
+
if (Array.isArray(resolved)) return resolved.map((item) => toText$1(item, props)).join("");
|
|
1361
|
+
if (resolved === null || resolved === void 0 || typeof resolved === "boolean") return "";
|
|
1362
|
+
if (isPlainObject$2(resolved)) throw new TypeError("parseStyleTemplate(): a style object can stand only between declarations, not in a value, a selector or a property name.");
|
|
1363
|
+
return String(resolved);
|
|
1364
|
+
}
|
|
1365
|
+
/** `text` with its interpolations filled in, trimmed. */
|
|
1366
|
+
function fill(text, values, props) {
|
|
1367
|
+
if (typeof text === "string") return text;
|
|
1368
|
+
let filled = "";
|
|
1369
|
+
for (const piece of text) filled += typeof piece === "string" ? piece : toText$1(values[piece], props);
|
|
1370
|
+
return filled.trim();
|
|
1371
|
+
}
|
|
1372
|
+
/** A declared value: an `alpha()` result that is the whole value is kept, so that its fallback is written; anything else is text. */
|
|
1373
|
+
function fillValue(text, values, props) {
|
|
1374
|
+
if (typeof text === "string" || text.length > 1) return fill(text, values, props);
|
|
1375
|
+
const value = resolve(values[text[0]], props);
|
|
1376
|
+
return isAlphaColor(value) ? value : toText$1(value, props).trim();
|
|
1377
|
+
}
|
|
1378
|
+
/** Whether `value` is a nested style, not a declared value. */
|
|
1379
|
+
function isNested(value) {
|
|
1380
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && !isAlphaColor(value);
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
* Sets `key` of `style` to `value`, at the end: a nested style is merged into the one before it; a value
|
|
1384
|
+
* declared again is added to the one before it as a fallback list, as CSS writes both.
|
|
1385
|
+
*/
|
|
1386
|
+
function put(style, key, value) {
|
|
1387
|
+
const earlier = style[key];
|
|
1388
|
+
delete style[key];
|
|
1389
|
+
if (isNested(value)) style[key] = isNested(earlier) ? mergeStyles([earlier, value]) : value;
|
|
1390
|
+
else if (earlier === void 0 || earlier === null || earlier === false || isNested(earlier)) style[key] = value;
|
|
1391
|
+
else style[key] = [earlier, value].flat();
|
|
1392
|
+
}
|
|
1393
|
+
/** `style` with the interpolation `value` that stands between declarations merged in. */
|
|
1394
|
+
function include(style, value, props) {
|
|
1395
|
+
const resolved = resolve(value, props);
|
|
1396
|
+
if (Array.isArray(resolved)) return resolved.reduce((merged, item) => include(merged, item, props), style);
|
|
1397
|
+
if (resolved === null || resolved === void 0 || typeof resolved === "boolean") return style;
|
|
1398
|
+
if (isSheetEntry(resolved)) {
|
|
1399
|
+
const source = readEntrySource(resolved);
|
|
1400
|
+
if (!source) throw new TypeError(`parseStyleTemplate(): the sheet entry "${resolved.className}" is only a class name, so its style cannot be merged.`);
|
|
1401
|
+
return mergeStyles([style, source.style]);
|
|
1402
|
+
}
|
|
1403
|
+
if (isPlainObject$2(resolved)) return mergeStyles([style, resolved]);
|
|
1404
|
+
return build(parse(String(resolved)), [], props, style);
|
|
1405
|
+
}
|
|
1406
|
+
/** The style of `nodes`, with `values` for the interpolations and `props` for the functions among them, added to `style`. */
|
|
1407
|
+
function build(nodes, values, props, style = {}) {
|
|
1408
|
+
let built = style;
|
|
1409
|
+
for (const node of nodes) if (typeof node === "number") built = include(built, values[node], props);
|
|
1410
|
+
else if ("body" in node) put(built, fill(node.prelude, values, props), build(node.body, values, props));
|
|
1411
|
+
else {
|
|
1412
|
+
const key = typeof node.key === "string" ? node.key : toKey(fill(node.key, values, props));
|
|
1413
|
+
put(built, key, fillValue(node.value, values, props));
|
|
1414
|
+
}
|
|
1415
|
+
return built;
|
|
1416
|
+
}
|
|
1417
|
+
/** Whether `value` is the strings of a tagged template: an array with `raw`, which a call with styles never is. */
|
|
1418
|
+
function isTemplateStrings(value) {
|
|
1419
|
+
return Array.isArray(value) && Object.hasOwn(value, "raw");
|
|
1420
|
+
}
|
|
1421
|
+
/**
|
|
1422
|
+
* The style of a tagged template, `strings` and `values` as a tag is given them: CSS text with
|
|
1423
|
+
* declarations, nested rules (`&:hover { }`, `> p { }`), at-rules (`@media (...) { }`) and comments.
|
|
1424
|
+
* A value is kept as text (`padding: 8px` is `'8px'`); a property is a camelCase key; a property declared
|
|
1425
|
+
* twice in a block is a fallback list. An interpolation in a value, a selector or a property name is
|
|
1426
|
+
* text; one alone between declarations is merged in: a style object, a sheet entry, CSS text, or a list
|
|
1427
|
+
* of them. `null`, `undefined`, `true` and `false` are nothing. When an interpolation is a function, the
|
|
1428
|
+
* result is a function of the props that calls it with them (what it returns is an interpolation too);
|
|
1429
|
+
* else it is the style. The text is split once for each `strings` array. Throws a `TypeError` with the
|
|
1430
|
+
* text around the place for a template that cannot be read.
|
|
1431
|
+
*/
|
|
1432
|
+
function parseStyleTemplate(strings, values) {
|
|
1433
|
+
const nodes = templates.get(strings) ?? parse(strings.join(HOLE));
|
|
1434
|
+
templates.set(strings, nodes);
|
|
1435
|
+
return values.some(isDynamic) ? (props) => build(nodes, values, props) : build(nodes, values, void 0);
|
|
1436
|
+
}
|
|
1437
|
+
/**
|
|
1438
|
+
* The steps of an animation from a tagged template: `from { ... } 50% { ... } to { ... }`, read as
|
|
1439
|
+
* `parseStyleTemplate` reads a style. When an interpolation is a function, the result is a function of
|
|
1440
|
+
* the theme, and the function is called with `{ theme }`, as a function in a style is.
|
|
1441
|
+
*/
|
|
1442
|
+
function parseKeyframesTemplate(strings, values) {
|
|
1443
|
+
const steps = parseStyleTemplate(strings, values);
|
|
1444
|
+
return typeof steps === "function" ? (theme) => steps({ theme }) : steps;
|
|
1445
|
+
}
|
|
1446
|
+
// src/globalStyles.ts
|
|
1447
|
+
const BRAND = Symbol.for("umber-style.global-styles");
|
|
1448
|
+
/** The at-rules that hold rules (selectors as keys); every other at-rule holds declarations. */
|
|
1449
|
+
const GROUP_AT_RULE = /^@(?:media|supports|container|layer|scope|starting-style|document)(?![\w-])/;
|
|
1450
|
+
/** `value` for the id of a handle: a stand-in as its path, everything else as it is. */
|
|
1451
|
+
function toText(_key, value) {
|
|
1452
|
+
return readStandIn(value);
|
|
1453
|
+
}
|
|
1454
|
+
function globalStyles(first, ...values) {
|
|
1455
|
+
if (isTemplateStrings(first)) {
|
|
1456
|
+
const parsed = parseStyleTemplate(first, values);
|
|
1457
|
+
return globalStyles(typeof parsed === "function" ? (theme) => parsed({ theme }) : parsed);
|
|
1458
|
+
}
|
|
1459
|
+
const styles = first;
|
|
1460
|
+
if (typeof styles === "function") {
|
|
1461
|
+
const hash = hashString(`theme:${JSON.stringify(styles(createStandIn("")), toText)}`);
|
|
1462
|
+
return Object.freeze({
|
|
1463
|
+
[BRAND]: true,
|
|
1464
|
+
id: `global-${hash}`,
|
|
1465
|
+
resolve: (readTheme) => styles(readTheme())
|
|
1466
|
+
});
|
|
1467
|
+
}
|
|
1468
|
+
if (typeof styles !== "object" || styles === null || Array.isArray(styles)) throw new TypeError("globalStyles(): the styles must be an object, or a function that returns one.");
|
|
1469
|
+
return Object.freeze({
|
|
1470
|
+
[BRAND]: true,
|
|
1471
|
+
id: `global-${hashString(JSON.stringify(styles))}`,
|
|
1472
|
+
resolve: () => styles
|
|
1473
|
+
});
|
|
1474
|
+
}
|
|
1475
|
+
/** Whether `value` is a handle that `globalStyles` made. */
|
|
1476
|
+
function isGlobalStyles(value) {
|
|
1477
|
+
return typeof value === "object" && value !== null && BRAND in value;
|
|
1478
|
+
}
|
|
1479
|
+
/** The styles of one item of a `GlobalInput`: a handle resolves itself, a function is called with the theme that `readTheme` gives. */
|
|
1480
|
+
function readGlobalStyles(item, readTheme) {
|
|
1481
|
+
if (isGlobalStyles(item)) return item.resolve(readTheme);
|
|
1482
|
+
return typeof item === "function" ? item(readTheme()) : item;
|
|
1483
|
+
}
|
|
1484
|
+
/** The style of one item under `key` (a sheet entry is read as its style); a `TypeError` for anything that is not a style object. */
|
|
1485
|
+
function readGlobalItem(key, each) {
|
|
1486
|
+
const item = readEntryStyle(each, "globalStyles");
|
|
1487
|
+
if (typeof item !== "object" || item === null || Array.isArray(item)) throw new TypeError(`globalStyles(): "${key}" must hold a style object, or a list of them.`);
|
|
1488
|
+
return item;
|
|
1489
|
+
}
|
|
1490
|
+
/** Adds to `rules` the rules of the style `item` under `key`: an at-rule that holds rules around the rules of its keys, any other key compiled as a class's style is. */
|
|
1491
|
+
function addGlobalItemRules(rules, key, item, hasColorFallbacks, scoping, hasValueRepair) {
|
|
1492
|
+
if (!GROUP_AT_RULE.test(key)) {
|
|
1493
|
+
rules.push(...compileRules(key, item, hasColorFallbacks, scoping, hasValueRepair));
|
|
1494
|
+
return;
|
|
1495
|
+
}
|
|
1496
|
+
const inner = compileGlobalRules(item, hasColorFallbacks, scoping, hasValueRepair);
|
|
1497
|
+
if (inner.length > 0) rules.push(`${key}{${inner.join("")}}`);
|
|
1498
|
+
}
|
|
1499
|
+
/**
|
|
1500
|
+
* The flat CSS rules of global styles, in the order of their keys: a selector's style compiled as a
|
|
1501
|
+
* class's is, an at-rule that holds rules as one rule around the rules of its keys, any other at-rule as
|
|
1502
|
+
* a rule of declarations (one for each item of a list). Throws a `TypeError` for styles that are not an
|
|
1503
|
+
* object, and for a key that holds no style.
|
|
1504
|
+
*/
|
|
1505
|
+
function compileGlobalRules(styles, hasColorFallbacks, scoping, hasValueRepair) {
|
|
1506
|
+
if (typeof styles !== "object" || styles === null || Array.isArray(styles)) throw new TypeError("globalStyles(): the styles must be an object, or a function that returns one.");
|
|
1507
|
+
const rules = [];
|
|
1508
|
+
for (const [key, value] of Object.entries(styles)) {
|
|
1509
|
+
if (value === null || value === void 0 || value === false) continue;
|
|
1510
|
+
for (const each of Array.isArray(value) ? value : [value]) addGlobalItemRules(rules, key, readGlobalItem(key, each), hasColorFallbacks, scoping, hasValueRepair);
|
|
1511
|
+
}
|
|
1512
|
+
return rules;
|
|
1513
|
+
}
|
|
1514
|
+
// src/keyframes.ts
|
|
1515
|
+
const DEFAULT_LABEL = "keyframes";
|
|
1516
|
+
const LABEL_PATTERN = /^[A-Za-z_][\w-]*$/;
|
|
1517
|
+
const OFFSET_PATTERN = /^(?:\d+(?:\.\d+)?|\.\d+)%$/;
|
|
1518
|
+
const ANIMATION = "animation";
|
|
1519
|
+
const NAME_SUFFIX = "-name:";
|
|
1520
|
+
const NONE = [];
|
|
1521
|
+
const registry = /* @__PURE__ */ new Map();
|
|
1522
|
+
/** The value of `key` in a step, with a stand-in (alone or in a list) turned into its path; a nested style is a `TypeError`. */
|
|
1523
|
+
function readStepValue(offset, key, value) {
|
|
1524
|
+
if (Array.isArray(value)) return value.map((item) => readStandIn(item));
|
|
1525
|
+
const read = readStandIn(value);
|
|
1526
|
+
if (typeof read !== "object" || read === null || isAlphaColor(read)) return read;
|
|
1527
|
+
throw new TypeError(`keyframes(): the step "${offset}" holds "${key}", a nested style (a selector or an at-rule). A step is flat: it holds declarations only.`);
|
|
1528
|
+
}
|
|
1529
|
+
/** The offset of one part of a step key, from 0 to 100; a `TypeError` for anything else. */
|
|
1530
|
+
function readOffset(part, key) {
|
|
1531
|
+
if (part === "from") return 0;
|
|
1532
|
+
if (part === "to") return 100;
|
|
1533
|
+
const offset = OFFSET_PATTERN.test(part) ? Number.parseFloat(part) : NaN;
|
|
1534
|
+
if (Number.isNaN(offset) || offset > 100) throw new TypeError(`keyframes(): "${key}" is not a step: use from, to, a percentage from 0% to 100%, or a comma list of them.`);
|
|
1535
|
+
return offset;
|
|
1536
|
+
}
|
|
1537
|
+
/** The steps by offset, sorted: comma lists expanded, `from` and `to` as 0% and 100%, steps of one offset merged with the later one winning. */
|
|
1538
|
+
function normalizeSteps(steps) {
|
|
1539
|
+
if (typeof steps !== "object" || steps === null) throw new TypeError("keyframes(): the steps must be an object, or a function that returns one.");
|
|
1540
|
+
const merged = /* @__PURE__ */ new Map();
|
|
1541
|
+
for (const [key, step] of Object.entries(steps)) {
|
|
1542
|
+
if (typeof step !== "object" || step === null) throw new TypeError(`keyframes(): the step "${key}" must be an object of declarations.`);
|
|
1543
|
+
const flat = {};
|
|
1544
|
+
for (const [name, value] of Object.entries(step)) flat[name] = readStepValue(key, name, value);
|
|
1545
|
+
for (const part of key.split(",")) {
|
|
1546
|
+
const offset = readOffset(part.trim(), key);
|
|
1547
|
+
const earlier = merged.get(offset);
|
|
1548
|
+
merged.set(offset, earlier ? mergeStyles([earlier, flat]) : flat);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
return [...merged].sort(([first], [second]) => first - second);
|
|
1552
|
+
}
|
|
1553
|
+
/** The body of a `@keyframes` rule: each step that declares something, sorted, written as the rule of a class. */
|
|
1554
|
+
function compileSteps(steps, hasColorFallbacks, hasValueRepair) {
|
|
1555
|
+
return normalizeSteps(steps).flatMap(([offset, step]) => compileRules(`${offset}%`, step, hasColorFallbacks, DEFAULT_SCOPING, hasValueRepair)).join("");
|
|
1556
|
+
}
|
|
1557
|
+
function keyframes(first, ...rest) {
|
|
1558
|
+
if (isTemplateStrings(first)) return keyframes(parseKeyframesTemplate(first, rest));
|
|
1559
|
+
const steps = first;
|
|
1560
|
+
const { label = DEFAULT_LABEL } = rest[0] ?? {};
|
|
1561
|
+
if (!LABEL_PATTERN.test(label)) throw new TypeError(`keyframes(): the label "${label}" is not an identifier: use letters, digits, '-' and '_', not starting with a digit or '-'.`);
|
|
1562
|
+
const isThemed = typeof steps === "function";
|
|
1563
|
+
const resolved = isThemed ? steps(createStandIn("")) : steps;
|
|
1564
|
+
const name = `${label}-${hashString((isThemed ? "theme:" : "") + compileSteps(resolved, true, false))}`;
|
|
1565
|
+
if (!registry.has(name)) registry.set(name, steps);
|
|
1566
|
+
return name;
|
|
1567
|
+
}
|
|
1568
|
+
/** Whether `name` is a name that `keyframes` returned. */
|
|
1569
|
+
function isKeyframesName(name) {
|
|
1570
|
+
return registry.has(name);
|
|
1571
|
+
}
|
|
1572
|
+
/**
|
|
1573
|
+
* The `@keyframes` rule of the registered `name`: steps given as a function are resolved against the
|
|
1574
|
+
* theme that `readTheme` gives, which is read only then.
|
|
1575
|
+
*/
|
|
1576
|
+
function createKeyframesRule(name, readTheme, hasColorFallbacks, hasValueRepair) {
|
|
1577
|
+
const steps = registry.get(name);
|
|
1578
|
+
return `@keyframes ${name}{${compileSteps(typeof steps === "function" ? steps(readTheme()) : steps, hasColorFallbacks, hasValueRepair)}}`;
|
|
1579
|
+
}
|
|
1580
|
+
/** Adds to `names` each identifier of `value` that is a registered name. */
|
|
1581
|
+
function addRegistered(value, names) {
|
|
1582
|
+
for (const word of value.split(/[^\w-]+/)) if (registry.has(word) && !names.includes(word)) names.push(word);
|
|
1583
|
+
}
|
|
1584
|
+
/** Where the value starts when the `animation` at `index` of `rule` begins an `animation` or `animation-name` declaration, else -1. */
|
|
1585
|
+
function findValueStart(rule, index) {
|
|
1586
|
+
const before = rule[index - 1];
|
|
1587
|
+
if (before !== "{" && before !== ";") return -1;
|
|
1588
|
+
const after = index + 9;
|
|
1589
|
+
if (rule[after] === ":") return after + 1;
|
|
1590
|
+
return rule.startsWith(NAME_SUFFIX, after) ? after + 6 : -1;
|
|
1591
|
+
}
|
|
1592
|
+
/**
|
|
1593
|
+
* The registered names that the `animation` and `animation-name` declarations of `rules` (compiled rules,
|
|
1594
|
+
* whose declarations each end with `;`) use, in nested selectors and at-rules too, matched as whole
|
|
1595
|
+
* identifiers. Nothing is read while no name is registered.
|
|
1596
|
+
*/
|
|
1597
|
+
function findAnimationNames(rules) {
|
|
1598
|
+
if (registry.size === 0) return NONE;
|
|
1599
|
+
const names = [];
|
|
1600
|
+
for (const rule of rules) {
|
|
1601
|
+
let index = rule.indexOf(ANIMATION);
|
|
1602
|
+
while (index !== -1) {
|
|
1603
|
+
const start = findValueStart(rule, index);
|
|
1604
|
+
if (start !== -1) {
|
|
1605
|
+
index = rule.indexOf(";", start);
|
|
1606
|
+
addRegistered(rule.slice(start, index), names);
|
|
1607
|
+
}
|
|
1608
|
+
index = rule.indexOf(ANIMATION, index + 1);
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
return names;
|
|
1612
|
+
}
|
|
1613
|
+
// src/variables.ts
|
|
1614
|
+
const VARIABLE_OPEN = "var(";
|
|
1615
|
+
/** Whether `code` is a character of a variable name: a lowercase letter, a digit, '-' or '_'. */
|
|
1616
|
+
function isNameCode$1(code) {
|
|
1617
|
+
return code >= 97 && code <= 122 || code >= 48 && code <= 57 || code === 45 || code === 95;
|
|
1618
|
+
}
|
|
1619
|
+
/** The index of the first character at or after `index` that is not whitespace. */
|
|
1620
|
+
function skipSpaces$1(text, index) {
|
|
1621
|
+
let position = index;
|
|
1622
|
+
while (codeAt(text, position) <= 32) position++;
|
|
1623
|
+
return position;
|
|
1624
|
+
}
|
|
1625
|
+
/**
|
|
1626
|
+
* The names of the variables that `css` refers to and that start with `start` (`--<prefix>-`), in order
|
|
1627
|
+
* of appearance, with repeats. A reference may have a fallback, which may refer to others. One pass.
|
|
1628
|
+
*/
|
|
1629
|
+
function scanVariables(css, start) {
|
|
1630
|
+
const names = [];
|
|
1631
|
+
let from = css.indexOf(VARIABLE_OPEN);
|
|
1632
|
+
while (from !== -1) {
|
|
1633
|
+
const begin = skipSpaces$1(css, from + 4);
|
|
1634
|
+
let end = begin + start.length;
|
|
1635
|
+
if (css.startsWith(start, begin)) {
|
|
1636
|
+
while (isNameCode$1(codeAt(css, end))) end++;
|
|
1637
|
+
names.push(css.slice(begin, end));
|
|
1638
|
+
}
|
|
1639
|
+
from = css.indexOf(VARIABLE_OPEN, begin);
|
|
1640
|
+
}
|
|
1641
|
+
return names;
|
|
1642
|
+
}
|
|
1643
|
+
// src/scanStyleText.ts
|
|
1644
|
+
/** Makes findings that hold nothing. */
|
|
1645
|
+
function createEmptyFindings() {
|
|
1646
|
+
return {
|
|
1647
|
+
hasLayerOrder: false,
|
|
1648
|
+
classes: /* @__PURE__ */ new Set(),
|
|
1649
|
+
themes: /* @__PURE__ */ new Map(),
|
|
1650
|
+
properties: /* @__PURE__ */ new Map(),
|
|
1651
|
+
keyframes: /* @__PURE__ */ new Set(),
|
|
1652
|
+
variables: /* @__PURE__ */ new Set(),
|
|
1653
|
+
ruleCount: 0,
|
|
1654
|
+
globals: [],
|
|
1655
|
+
roots: []
|
|
1656
|
+
};
|
|
1657
|
+
}
|
|
1658
|
+
const LAYER_AT_RULE = "@layer";
|
|
1659
|
+
const ROOT_SELECTOR$1 = ":where(:root)";
|
|
1660
|
+
const PROPERTY_AT_RULE = "@property";
|
|
1661
|
+
const KEYFRAMES_AT_RULE = "@keyframes";
|
|
1662
|
+
/** Whether `code` is a character of a class name: a lowercase letter, a digit, '-' or '_'. */
|
|
1663
|
+
function isNameCode(code) {
|
|
1664
|
+
return code >= 97 && code <= 122 || code >= 48 && code <= 57 || code === 45 || code === 95;
|
|
1665
|
+
}
|
|
1666
|
+
/** The classes in `selector` whose name starts with `start` (`<prefix>-`), without the dot. */
|
|
1667
|
+
function readClasses(selector, start) {
|
|
1668
|
+
const names = [];
|
|
1669
|
+
let from = selector.indexOf(`.${start}`);
|
|
1670
|
+
while (from !== -1) {
|
|
1671
|
+
let end = from + 1 + start.length;
|
|
1672
|
+
while (isNameCode(codeAt(selector, end))) end++;
|
|
1673
|
+
names.push(selector.slice(from + 1, end));
|
|
1674
|
+
from = selector.indexOf(`.${start}`, end);
|
|
1675
|
+
}
|
|
1676
|
+
return names;
|
|
1677
|
+
}
|
|
1678
|
+
/** The names of the variables that `body`, the declarations of a rule, declares. */
|
|
1679
|
+
function readDeclared(body) {
|
|
1680
|
+
const names = /* @__PURE__ */ new Set();
|
|
1681
|
+
for (const part of body.split(";")) {
|
|
1682
|
+
const text = part.trimStart();
|
|
1683
|
+
if (text.startsWith("--")) names.add(text.slice(0, text.indexOf(":")));
|
|
1684
|
+
}
|
|
1685
|
+
return names;
|
|
1686
|
+
}
|
|
1687
|
+
/** The index after the string that starts at `index`, which is a quote. */
|
|
1688
|
+
function skipString(text, index) {
|
|
1689
|
+
const quote = text[index];
|
|
1690
|
+
let position = index + 1;
|
|
1691
|
+
while (position < text.length && text[position] !== quote) position += text[position] === "\\" ? 2 : 1;
|
|
1692
|
+
return position + 1;
|
|
1693
|
+
}
|
|
1694
|
+
/** Records the innermost rule `prelude{body}` of a style element in `found`. */
|
|
1695
|
+
function record(found, prefix, prelude, body) {
|
|
1696
|
+
if (prelude.startsWith(PROPERTY_AT_RULE)) {
|
|
1697
|
+
found.properties.set(prelude.slice(9).trim(), `${prelude}{${body}}`);
|
|
1698
|
+
return;
|
|
1699
|
+
}
|
|
1700
|
+
if (prelude.startsWith("@") || prelude === ROOT_SELECTOR$1) return;
|
|
1701
|
+
const themeStart = `${prefix}-theme-`;
|
|
1702
|
+
const classes = readClasses(prelude, `${prefix}-`);
|
|
1703
|
+
const themeId = classes.length === 1 && prelude === `.${classes[0]}` ? classes[0] : void 0;
|
|
1704
|
+
if (themeId?.startsWith(themeStart)) {
|
|
1705
|
+
const declared = found.themes.get(themeId) ?? /* @__PURE__ */ new Set();
|
|
1706
|
+
for (const name of readDeclared(body)) declared.add(name);
|
|
1707
|
+
found.themes.set(themeId, declared);
|
|
1708
|
+
return;
|
|
1709
|
+
}
|
|
1710
|
+
const markers = /* @__PURE__ */ new Set([
|
|
1711
|
+
`${prefix}-boundary`,
|
|
1712
|
+
`${prefix}-scheme-light`,
|
|
1713
|
+
`${prefix}-scheme-dark`
|
|
1714
|
+
]);
|
|
1715
|
+
for (const name of classes) if (!name.startsWith(themeStart) && !markers.has(name)) found.classes.add(name);
|
|
1716
|
+
for (const name of scanVariables(body, `--${prefix}-`)) found.variables.add(name);
|
|
1717
|
+
}
|
|
1718
|
+
/** Opens a block at `index`, a `{`: its prelude is the text since `boundary`. */
|
|
1719
|
+
function openBlock(stack, text, boundary, index) {
|
|
1720
|
+
const parent = stack.at(-1);
|
|
1721
|
+
if (parent) parent.hasChild = true;
|
|
1722
|
+
stack.push({
|
|
1723
|
+
prelude: text.slice(boundary, index).trim(),
|
|
1724
|
+
bodyStart: index + 1,
|
|
1725
|
+
hasChild: false
|
|
1726
|
+
});
|
|
1727
|
+
}
|
|
1728
|
+
/** Closes the innermost block at `index`, a `}`, and returns it: a `@keyframes` rule records its name, and a block without a block of its own is a rule to record. */
|
|
1729
|
+
function closeBlock(stack, found, prefix, text, index) {
|
|
1730
|
+
const block = stack.pop();
|
|
1731
|
+
if (block?.prelude.startsWith(KEYFRAMES_AT_RULE)) found.keyframes.add(block.prelude.slice(10).trim());
|
|
1732
|
+
else if (block && !block.hasChild) record(found, prefix, block.prelude, text.slice(block.bodyStart, index));
|
|
1733
|
+
return block;
|
|
1734
|
+
}
|
|
1735
|
+
/** Whether a top-level rule with `prelude` may be a global rule: in the global layer, or in none and not a rule that the engine writes for something else. */
|
|
1736
|
+
function isGlobalPrelude(prefix, prelude) {
|
|
1737
|
+
if (prelude.startsWith(LAYER_AT_RULE)) return prelude === `${LAYER_AT_RULE} ${prefix}-global`;
|
|
1738
|
+
return !prelude.startsWith(PROPERTY_AT_RULE) && !prelude.startsWith(KEYFRAMES_AT_RULE) && !prelude.startsWith(`.${prefix}-`);
|
|
1739
|
+
}
|
|
1740
|
+
/** Whether the top-level rule `text` is the rule of a root theme, in the theme layer or in none. */
|
|
1741
|
+
function isRootRule(prefix, text) {
|
|
1742
|
+
return text.startsWith(`${ROOT_SELECTOR$1}{`) || text.startsWith(`${LAYER_AT_RULE} ${prefix}-theme{${ROOT_SELECTOR$1}{`);
|
|
1743
|
+
}
|
|
1744
|
+
/** Closes the innermost block at `index`, a `}`; when it is a top-level rule (which started at `ruleStart`), records its place in `found`, and whether it is the rule of a root theme or may be a global rule. */
|
|
1745
|
+
function closeRule(stack, found, prefix, text, ruleStart, index) {
|
|
1746
|
+
const block = closeBlock(stack, found, prefix, text, index);
|
|
1747
|
+
if (!block || stack.length > 0) return;
|
|
1748
|
+
const place = found.ruleCount++;
|
|
1749
|
+
const rule = text.slice(ruleStart, index + 1).trimStart();
|
|
1750
|
+
if (isRootRule(prefix, rule)) found.roots.push(place);
|
|
1751
|
+
else if (isGlobalPrelude(prefix, block.prelude)) found.globals.push([place, rule]);
|
|
1752
|
+
}
|
|
1753
|
+
/** Records the top-level statement `statement` (the text before a `;`, trimmed) in `found`: a rule unless it is empty, and the layer order statement when it is one. */
|
|
1754
|
+
function recordStatement(found, statement) {
|
|
1755
|
+
if (statement.startsWith(LAYER_AT_RULE)) found.hasLayerOrder = true;
|
|
1756
|
+
if (statement !== "") found.ruleCount++;
|
|
1757
|
+
}
|
|
1758
|
+
/**
|
|
1759
|
+
* Reads the CSS that an engine wrote (compact, no comments) in one pass and adds what it holds to
|
|
1760
|
+
* `found`: the layer order statement, the classes with a rule, each theme class with the variables it
|
|
1761
|
+
* declares, the `@property` rules, the names of the `@keyframes` rules, the variables that rules use, how
|
|
1762
|
+
* many rules are at the top level, the top-level rules that may be global rules, and the places of the
|
|
1763
|
+
* rules of a root theme.
|
|
1764
|
+
*/
|
|
1765
|
+
function scanStyleText(prefix, text, found = createEmptyFindings()) {
|
|
1766
|
+
const stack = [];
|
|
1767
|
+
let boundary = 0;
|
|
1768
|
+
let ruleStart = 0;
|
|
1769
|
+
let index = 0;
|
|
1770
|
+
while (index < text.length) {
|
|
1771
|
+
const char = text[index];
|
|
1772
|
+
if (char === "\"" || char === "'") {
|
|
1773
|
+
index = skipString(text, index);
|
|
1774
|
+
continue;
|
|
1775
|
+
}
|
|
1776
|
+
if (char === "{") {
|
|
1777
|
+
if (stack.length === 0) ruleStart = boundary;
|
|
1778
|
+
openBlock(stack, text, boundary, index);
|
|
1779
|
+
boundary = index + 1;
|
|
1780
|
+
} else if (char === "}") {
|
|
1781
|
+
closeRule(stack, found, prefix, text, ruleStart, index);
|
|
1782
|
+
boundary = index + 1;
|
|
1783
|
+
} else if (char === ";" && stack.length === 0) {
|
|
1784
|
+
recordStatement(found, text.slice(boundary, index).trim());
|
|
1785
|
+
boundary = index + 1;
|
|
1786
|
+
}
|
|
1787
|
+
index++;
|
|
1788
|
+
}
|
|
1789
|
+
return found;
|
|
1790
|
+
}
|
|
1791
|
+
// src/theme.ts
|
|
1792
|
+
/** The selector of the rule of the root theme: the document root, with no specificity. */
|
|
1793
|
+
const ROOT_SELECTOR = ":where(:root)";
|
|
1794
|
+
/** The rule `selector{body}` of a theme, inside the theme layer of `prefix` when there are layers. */
|
|
1795
|
+
function createThemeRule(prefix, hasLayers, selector, body) {
|
|
1796
|
+
const rule = `${selector}{${body}}`;
|
|
1797
|
+
return hasLayers ? `@layer ${prefix}-theme{${rule}}` : rule;
|
|
1798
|
+
}
|
|
1799
|
+
/** The CSS declarations `name:value;` of `entries`. */
|
|
1800
|
+
function toDeclarationText(entries) {
|
|
1801
|
+
let text = "";
|
|
1802
|
+
for (const [name, value] of entries) text += `${name}:${value};`;
|
|
1803
|
+
return text;
|
|
1804
|
+
}
|
|
1805
|
+
/** The hash of all the declarations of a theme, as text, and of its color-scheme: what names its class or root rule. */
|
|
1806
|
+
function hashTheme(variables, colorScheme) {
|
|
1807
|
+
return hashString(colorScheme ? `${variables}color-scheme:${colorScheme};` : variables);
|
|
1808
|
+
}
|
|
1809
|
+
/**
|
|
1810
|
+
* The theme class of the declarations `variables` (the text of `toDeclarationText`, as `readThemeText`
|
|
1811
|
+
* gives it) and of `colorScheme`, `<prefix>-theme-<hash>`, without registering it: the name that
|
|
1812
|
+
* `createThemeClass` gives the same declarations.
|
|
1813
|
+
*/
|
|
1814
|
+
function createThemeId(prefix, variables, colorScheme) {
|
|
1815
|
+
return `${prefix}-theme-${hashTheme(variables, colorScheme)}`;
|
|
1816
|
+
}
|
|
1817
|
+
/**
|
|
1818
|
+
* Registers the theme class of `declarations` and returns it: the scope of a theme. Its name is the
|
|
1819
|
+
* hash of all the declarations, whatever the cache writes of them (see `hasPruning`).
|
|
1820
|
+
*/
|
|
1821
|
+
function createThemeClass(cache, declarations, options = {}) {
|
|
1822
|
+
const { colorScheme } = options;
|
|
1823
|
+
const variables = toDeclarationText(Object.entries(declarations));
|
|
1824
|
+
const className = createThemeId(cache.prefix, variables, colorScheme);
|
|
1825
|
+
cache.insertTheme(className, declarations, colorScheme);
|
|
1826
|
+
return className;
|
|
1827
|
+
}
|
|
1828
|
+
/**
|
|
1829
|
+
* Makes `declarations` the root theme, the one declared on `:root` (see `Cache.insertRoot`), and returns
|
|
1830
|
+
* its id, `<prefix>-root-<hash>`: like a theme class, named by the hash of all its declarations.
|
|
1831
|
+
*/
|
|
1832
|
+
function declareRootTheme(cache, declarations, options = {}) {
|
|
1833
|
+
const { colorScheme } = options;
|
|
1834
|
+
const variables = toDeclarationText(Object.entries(declarations));
|
|
1835
|
+
const id = `${cache.prefix}-root-${hashTheme(variables, colorScheme)}`;
|
|
1836
|
+
cache.insertRoot(id, declarations, colorScheme);
|
|
1837
|
+
return id;
|
|
1838
|
+
}
|
|
1839
|
+
// src/cache.ts
|
|
1840
|
+
/** The cascade layers of a cache, from the lowest priority to the highest. */
|
|
1841
|
+
const RULE_LAYERS = [
|
|
1842
|
+
"theme",
|
|
1843
|
+
"global",
|
|
1844
|
+
"components",
|
|
1845
|
+
"overrides"
|
|
1846
|
+
];
|
|
1847
|
+
/** The start of every variable name, and of the ids that mark a used variable. */
|
|
1848
|
+
const VARIABLE_START = "--";
|
|
1849
|
+
const PREFIX_PATTERN = /^[a-z][a-z0-9-]*$/;
|
|
1850
|
+
/** A sheet that keeps its rules in memory, for a server render or a test. */
|
|
1851
|
+
function createMemorySheet() {
|
|
1852
|
+
const rules = [];
|
|
1853
|
+
return {
|
|
1854
|
+
rules,
|
|
1855
|
+
insert(rule) {
|
|
1856
|
+
rules.push(rule);
|
|
1857
|
+
},
|
|
1858
|
+
replace(index, rule) {
|
|
1859
|
+
rules[index] = rule;
|
|
1860
|
+
},
|
|
1861
|
+
remove(index) {
|
|
1862
|
+
rules.splice(index, 1);
|
|
1863
|
+
}
|
|
1864
|
+
};
|
|
1865
|
+
}
|
|
1866
|
+
/** Deletes `rule` from the style sheet that holds it, found by its place now; a rule that is held by none any more is left. */
|
|
1867
|
+
function deleteCssRule(rule) {
|
|
1868
|
+
const parent = rule.parentStyleSheet;
|
|
1869
|
+
if (parent) parent.deleteRule(Array.prototype.indexOf.call(parent.cssRules, rule));
|
|
1870
|
+
}
|
|
1871
|
+
/**
|
|
1872
|
+
* A sheet in the style element marked `data-style="<key>"` of the document of `container`, which it
|
|
1873
|
+
* creates in `container` when that document has none. It keeps the browser's object of each rule it
|
|
1874
|
+
* inserts, and removes a rule by finding where that object is at the time, so rules that others insert
|
|
1875
|
+
* into or delete from the element do not confuse it. Its `document` is that document: a cache over it
|
|
1876
|
+
* reads that one (an iframe's, for a container in the iframe).
|
|
1877
|
+
*/
|
|
1878
|
+
function createDomSheet(key, container = document.head) {
|
|
1879
|
+
const owner = container.ownerDocument;
|
|
1880
|
+
let element = owner.querySelector(`style[data-style="${key}"]`);
|
|
1881
|
+
if (!element) {
|
|
1882
|
+
element = owner.createElement("style");
|
|
1883
|
+
element.dataset.style = key;
|
|
1884
|
+
container.appendChild(element);
|
|
1885
|
+
}
|
|
1886
|
+
const sheet = element.sheet;
|
|
1887
|
+
const rules = [];
|
|
1888
|
+
const objects = [];
|
|
1889
|
+
return {
|
|
1890
|
+
rules,
|
|
1891
|
+
document: owner,
|
|
1892
|
+
insert(rule) {
|
|
1893
|
+
try {
|
|
1894
|
+
const index = sheet.insertRule(rule, sheet.cssRules.length);
|
|
1895
|
+
objects.push(sheet.cssRules[index]);
|
|
1896
|
+
rules.push(rule);
|
|
1897
|
+
} catch {}
|
|
1898
|
+
},
|
|
1899
|
+
remove(index) {
|
|
1900
|
+
rules.splice(index, 1);
|
|
1901
|
+
deleteCssRule(objects.splice(index, 1)[0]);
|
|
1902
|
+
}
|
|
1903
|
+
};
|
|
1904
|
+
}
|
|
1905
|
+
/**
|
|
1906
|
+
* The selector of the style elements that hold engine rules: the cache's own, the ones React hoists for
|
|
1907
|
+
* the same prefix, and the `data-umber="<prefix> <ids>"` and `data-umber="<prefix>-global <ids>"` elements
|
|
1908
|
+
* of `@umber-style/server` and of an app's own registry.
|
|
1909
|
+
*/
|
|
1910
|
+
function createStyleSelector(prefix) {
|
|
1911
|
+
return `style[data-style="${prefix}"], style[data-precedence="${prefix}"], style[data-umber~="${prefix}"], style[data-umber~="${prefix}-global"]`;
|
|
1912
|
+
}
|
|
1913
|
+
/** The stamp of the options that the server wrote in `page`: on a style element, or on the `<meta>` that a server component renders. */
|
|
1914
|
+
function readServerStamp(page, prefix) {
|
|
1915
|
+
const element = page.querySelector(`style[data-style="${prefix}"][data-options], meta[name="${prefix}-options"]`);
|
|
1916
|
+
return element?.dataset.options ?? element?.getAttribute("content") ?? void 0;
|
|
1917
|
+
}
|
|
1918
|
+
/** The class names in `value`, a class string or a sheet entry. */
|
|
1919
|
+
function splitClassNames(value) {
|
|
1920
|
+
return (typeof value === "string" ? value : value.className).split(/\s+/).filter(Boolean);
|
|
1921
|
+
}
|
|
1922
|
+
/** Sorts the class names of a class string or sheet entry: a registered class is a style to merge, the rest pass through. */
|
|
1923
|
+
function sortClassNames(value, registered, styles, rawClasses) {
|
|
1924
|
+
for (const name of splitClassNames(value)) {
|
|
1925
|
+
const style = registered.get(name);
|
|
1926
|
+
if (style) styles.push(style);
|
|
1927
|
+
else rawClasses.push(name);
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
/** Orders distinct strings by UTF-16 code unit, what a sort without a compare function does. */
|
|
1931
|
+
function compareCodeUnits(first, second) {
|
|
1932
|
+
return first < second ? -1 : 1;
|
|
1933
|
+
}
|
|
1934
|
+
/** Adds to `names` every variable that the theme values of `themeIds` bring in for a name already in it. */
|
|
1935
|
+
function addThemeReferences(names, themeIds, readDeclarations) {
|
|
1936
|
+
const queue = [...names];
|
|
1937
|
+
for (const queued of queue) for (const id of themeIds) {
|
|
1938
|
+
const value = readDeclarations(id)?.[queued];
|
|
1939
|
+
if (value === void 0) continue;
|
|
1940
|
+
for (const name of scanVariables(value, VARIABLE_START)) {
|
|
1941
|
+
if (!names.has(name)) queue.push(name);
|
|
1942
|
+
names.add(name);
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
/**
|
|
1947
|
+
* Splits `values` into the styles to merge (the style of an entry a cache made, registered classes
|
|
1948
|
+
* resolved back) and the class names to pass through. `sole` is the entry when the one style is its own;
|
|
1949
|
+
* an entry of another cache is first made this cache's own with `adopt`.
|
|
1950
|
+
*/
|
|
1951
|
+
function sortClassValues(values, registered, adopt) {
|
|
1952
|
+
const styles = [];
|
|
1953
|
+
const rawClasses = [];
|
|
1954
|
+
let entry;
|
|
1955
|
+
for (const value of values) {
|
|
1956
|
+
if (!value) continue;
|
|
1957
|
+
if (typeof value !== "string" && !isSheetEntry(value)) {
|
|
1958
|
+
styles.push(value);
|
|
1959
|
+
continue;
|
|
1960
|
+
}
|
|
1961
|
+
const source = typeof value === "string" ? void 0 : readEntrySource(adopt(value));
|
|
1962
|
+
if (source) {
|
|
1963
|
+
styles.push(source.style);
|
|
1964
|
+
entry = source;
|
|
1965
|
+
continue;
|
|
1966
|
+
}
|
|
1967
|
+
sortClassNames(value, registered, styles, rawClasses);
|
|
1968
|
+
}
|
|
1969
|
+
return {
|
|
1970
|
+
styles,
|
|
1971
|
+
rawClasses,
|
|
1972
|
+
sole: styles.length === 1 ? entry : void 0
|
|
1973
|
+
};
|
|
1974
|
+
}
|
|
1975
|
+
/** Makes a cache: it turns styles into classes and inserts each class's rules once. */
|
|
1976
|
+
function createCache(options = {}) {
|
|
1977
|
+
const { prefix = "css", suffix = "", hasLabels = true, hasLayers = true, hasPruning = true, hasColorFallbacks = true, hasValueRepair = false, hasScope = true, hasBaseClass = true } = options;
|
|
1978
|
+
if (!PREFIX_PATTERN.test(prefix)) throw new Error(`Invalid cache prefix "${prefix}": use lowercase letters, digits and hyphens.`);
|
|
1979
|
+
const fingerprint = createFingerprint({
|
|
1980
|
+
prefix,
|
|
1981
|
+
suffix,
|
|
1982
|
+
hasLabels,
|
|
1983
|
+
hasLayers,
|
|
1984
|
+
hasPruning,
|
|
1985
|
+
hasBaseClass,
|
|
1986
|
+
hasScope,
|
|
1987
|
+
hasColorFallbacks,
|
|
1988
|
+
hasValueRepair,
|
|
1989
|
+
tokens: options.tokenSignature ?? ""
|
|
1990
|
+
});
|
|
1991
|
+
const page = options.sheet?.document ?? (typeof document === "undefined" ? void 0 : document);
|
|
1992
|
+
if (page) {
|
|
1993
|
+
const stamp = readServerStamp(page, prefix);
|
|
1994
|
+
const difference = stamp === void 0 ? "" : describeFingerprintDifferences(stamp, fingerprint);
|
|
1995
|
+
if (difference !== "") throw new Error(difference);
|
|
1996
|
+
}
|
|
1997
|
+
const { hasDevChecks = page !== void 0 && isDevelopment() } = options;
|
|
1998
|
+
const checkLater = hasDevChecks ? createDevChecker(prefix, void 0, page) : void 0;
|
|
1999
|
+
const insertedIds = /* @__PURE__ */ new Set();
|
|
2000
|
+
const used = /* @__PURE__ */ new Set();
|
|
2001
|
+
const pinned = /* @__PURE__ */ new Set();
|
|
2002
|
+
const pageThemes = /* @__PURE__ */ new Map();
|
|
2003
|
+
const afters = /* @__PURE__ */ new Map();
|
|
2004
|
+
const sheet = options.sheet ?? (page ? createDomSheet(prefix, page.head) : createMemorySheet());
|
|
2005
|
+
const registered = /* @__PURE__ */ new Map();
|
|
2006
|
+
const entries = /* @__PURE__ */ new Map();
|
|
2007
|
+
const scoping = {
|
|
2008
|
+
prefix,
|
|
2009
|
+
hasScope
|
|
2010
|
+
};
|
|
2011
|
+
const insertedRules = /* @__PURE__ */ new Map();
|
|
2012
|
+
let propertyRules = {};
|
|
2013
|
+
const themeNames = /* @__PURE__ */ new Map();
|
|
2014
|
+
const layerNames = RULE_LAYERS.map((layer) => `${prefix}-${layer}`).join(",");
|
|
2015
|
+
const layerOrder = hasLayers ? `@layer ${layerNames};` : "";
|
|
2016
|
+
const layerOrderId = `${prefix}-layers`;
|
|
2017
|
+
const wrapInLayer = (rules, layer) => hasLayers && layer ? rules.map((rule) => `@layer ${prefix}-${layer}{${rule}}`) : rules;
|
|
2018
|
+
const { onInsert } = options;
|
|
2019
|
+
const insertLayerOrder = () => {
|
|
2020
|
+
if (hasLayers && !insertedIds.has(layerOrderId)) {
|
|
2021
|
+
insertedIds.add(layerOrderId);
|
|
2022
|
+
sheet.insert(layerOrder);
|
|
2023
|
+
onInsert?.(layerOrderId, layerOrder);
|
|
2024
|
+
}
|
|
2025
|
+
};
|
|
2026
|
+
/** Writes `layered` after the layer order statement, and reports them under `name`; with `indexes`, the place of each rule that the sheet took is added to it. */
|
|
2027
|
+
const insertLayered = (name, layered, indexes) => {
|
|
2028
|
+
if (layered.length === 0) return;
|
|
2029
|
+
insertLayerOrder();
|
|
2030
|
+
if (indexes) for (const rule of layered) {
|
|
2031
|
+
const index = sheet.rules.length;
|
|
2032
|
+
sheet.insert(rule);
|
|
2033
|
+
if (sheet.rules.length > index) indexes.push(index);
|
|
2034
|
+
}
|
|
2035
|
+
else for (const rule of layered) sheet.insert(rule);
|
|
2036
|
+
onInsert?.(name, layered.join(""));
|
|
2037
|
+
checkLater?.(layered);
|
|
2038
|
+
};
|
|
2039
|
+
const themes = /* @__PURE__ */ new Map();
|
|
2040
|
+
const roots = /* @__PURE__ */ new Map();
|
|
2041
|
+
let root;
|
|
2042
|
+
let pageRoots = [];
|
|
2043
|
+
/** The rule of a root theme with the variables `names`, and its color-scheme; none when that is nothing. */
|
|
2044
|
+
const createRootRules = (theme, names) => {
|
|
2045
|
+
const body = toDeclarationText([...names].map((name) => [name, theme.declarations[name]])) + theme.schemeText;
|
|
2046
|
+
return body === "" ? [] : [createThemeRule(prefix, hasLayers, ROOT_SELECTOR, body)];
|
|
2047
|
+
};
|
|
2048
|
+
/** Writes the rule of the root theme anew with every variable written so far, after removing the one it had where the sheet can; a sheet that cannot keeps it, and the new rule, later, wins. */
|
|
2049
|
+
const writeRoot = (entry) => {
|
|
2050
|
+
const rules = createRootRules(roots.get(entry.id), entry.written);
|
|
2051
|
+
insertedRules.set(entry.id, rules);
|
|
2052
|
+
const { remove } = sheet;
|
|
2053
|
+
if (entry.index !== void 0 && remove) removeAt(remove, entry.index);
|
|
2054
|
+
entry.index = void 0;
|
|
2055
|
+
const before = sheet.rules.length;
|
|
2056
|
+
insertLayered(`${entry.id}-${hashString(rules.join(""))}`, rules);
|
|
2057
|
+
if (sheet.rules.length > before) entry.index = sheet.rules.length - 1;
|
|
2058
|
+
};
|
|
2059
|
+
/** Writes `names` for a theme class: a rule of its own, or in place of the rule it has when the sheet can replace. */
|
|
2060
|
+
const writeTheme = (className, entry, names) => {
|
|
2061
|
+
const added = toDeclarationText(names.map((name) => [name, entry.declarations[name]]));
|
|
2062
|
+
const ownScheme = entry.isSent ? "" : entry.schemeText;
|
|
2063
|
+
const first = entry.isStarted ? "" : ownScheme;
|
|
2064
|
+
if (added + first === "") return;
|
|
2065
|
+
const part = wrapInLayer([`.${className}{${added + first}}`], "theme");
|
|
2066
|
+
const reported = entry.isStarted ? `${className}-${hashString(added)}` : className;
|
|
2067
|
+
entry.isStarted = true;
|
|
2068
|
+
for (const name of names) entry.written.add(name);
|
|
2069
|
+
entry.variables += added;
|
|
2070
|
+
const { index } = entry;
|
|
2071
|
+
if (sheet.replace && index !== void 0) {
|
|
2072
|
+
const rules = wrapInLayer([`.${className}{${entry.variables + ownScheme}}`], "theme");
|
|
2073
|
+
sheet.replace(index, rules[0]);
|
|
2074
|
+
entry.rules = rules;
|
|
2075
|
+
onInsert?.(reported, part[0]);
|
|
2076
|
+
} else {
|
|
2077
|
+
const before = sheet.rules.length;
|
|
2078
|
+
insertLayered(reported, part);
|
|
2079
|
+
if (sheet.replace && sheet.rules.length > before) entry.index = sheet.rules.length - 1;
|
|
2080
|
+
entry.rules = [...entry.rules, ...part];
|
|
2081
|
+
}
|
|
2082
|
+
insertedRules.set(className, entry.rules);
|
|
2083
|
+
};
|
|
2084
|
+
/** Writes the `@property` rule of the variable `name`, once; the rule of the same text is never written twice, a changed one is written after it and wins. */
|
|
2085
|
+
const writeProperty = (name) => {
|
|
2086
|
+
const rule = propertyRules[name];
|
|
2087
|
+
if (rule === void 0) return;
|
|
2088
|
+
if (insertOnce(`@property:${name}:${hashString(rule)}`, [rule])) insertedRules.set(name, [rule]);
|
|
2089
|
+
};
|
|
2090
|
+
const registerProperties = (rules) => {
|
|
2091
|
+
propertyRules = rules;
|
|
2092
|
+
for (const name of Object.keys(rules)) if (!hasPruning || used.has(name)) writeProperty(name);
|
|
2093
|
+
};
|
|
2094
|
+
const animations = /* @__PURE__ */ new Map();
|
|
2095
|
+
let readKeyframesTheme = () => {
|
|
2096
|
+
throw new Error("keyframes(): these steps are a function of the theme, and this cache has no theme: make the cache through an engine.");
|
|
2097
|
+
};
|
|
2098
|
+
/** The `@keyframes` rule of `name`: the one written, else made now. */
|
|
2099
|
+
const readKeyframesRule = (name) => insertedRules.get(name)?.[0] ?? createKeyframesRule(name, readKeyframesTheme, hasColorFallbacks, hasValueRepair);
|
|
2100
|
+
/** Writes the `@keyframes` rule of the registered `name` once, outside every layer, unless the page has it. */
|
|
2101
|
+
const writeKeyframes = (name) => {
|
|
2102
|
+
const id = `@keyframes:${name}`;
|
|
2103
|
+
if (insertedIds.has(id)) return;
|
|
2104
|
+
const rule = readKeyframesRule(name);
|
|
2105
|
+
insertOnce(id, [rule]);
|
|
2106
|
+
insertedRules.set(name, [rule]);
|
|
2107
|
+
};
|
|
2108
|
+
/** Inserts the rules of a class once, after the `@keyframes` rules of the animations they use; with `indexes`, the places of its rules are added to it. */
|
|
2109
|
+
const insertClass = (className, rules, layer, indexes) => {
|
|
2110
|
+
if (insertedIds.has(className)) return;
|
|
2111
|
+
const names = findAnimationNames(rules);
|
|
2112
|
+
if (names.length > 0) {
|
|
2113
|
+
animations.set(className, names);
|
|
2114
|
+
names.forEach(writeKeyframes);
|
|
2115
|
+
}
|
|
2116
|
+
insertOnce(className, rules, layer, indexes);
|
|
2117
|
+
};
|
|
2118
|
+
const insertKeyframes = (name) => {
|
|
2119
|
+
if (!isKeyframesName(name)) throw new TypeError(`insertKeyframes(): "${name}" is not a name that keyframes() returned, so there is no rule to write.`);
|
|
2120
|
+
writeKeyframes(name);
|
|
2121
|
+
};
|
|
2122
|
+
const markUsed = (names) => {
|
|
2123
|
+
if (!hasPruning) return;
|
|
2124
|
+
const queue = [...names];
|
|
2125
|
+
const fresh = [];
|
|
2126
|
+
for (const name of queue) {
|
|
2127
|
+
if (used.has(name)) continue;
|
|
2128
|
+
used.add(name);
|
|
2129
|
+
fresh.push(name);
|
|
2130
|
+
for (const { declarations } of [...themes.values(), ...roots.values()]) {
|
|
2131
|
+
const value = declarations[name];
|
|
2132
|
+
if (value !== void 0) queue.push(...scanVariables(value, VARIABLE_START));
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
if (fresh.length === 0) return;
|
|
2136
|
+
fresh.forEach(writeProperty);
|
|
2137
|
+
for (const [className, entry] of themes) writeTheme(className, entry, fresh.filter((name) => entry.declarations[name] !== void 0 && !entry.written.has(name)));
|
|
2138
|
+
if (root === void 0) return;
|
|
2139
|
+
const { declarations } = roots.get(root.id);
|
|
2140
|
+
const added = fresh.filter((name) => declarations[name] !== void 0);
|
|
2141
|
+
if (added.length === 0) return;
|
|
2142
|
+
for (const name of added) root.written.add(name);
|
|
2143
|
+
writeRoot(root);
|
|
2144
|
+
};
|
|
2145
|
+
const insertRoot = (id, declarations, colorScheme) => {
|
|
2146
|
+
if (!roots.has(id)) {
|
|
2147
|
+
roots.set(id, {
|
|
2148
|
+
declarations,
|
|
2149
|
+
schemeText: colorScheme ? `color-scheme:${colorScheme};` : ""
|
|
2150
|
+
});
|
|
2151
|
+
themeNames.set(id, Object.keys(declarations));
|
|
2152
|
+
}
|
|
2153
|
+
if (root?.id === id) return;
|
|
2154
|
+
for (const rule of pageRoots) if (rule) deleteCssRule(rule);
|
|
2155
|
+
pageRoots = [];
|
|
2156
|
+
if (root) {
|
|
2157
|
+
insertedIds.delete(root.id);
|
|
2158
|
+
insertedRules.delete(root.id);
|
|
2159
|
+
}
|
|
2160
|
+
insertedIds.add(id);
|
|
2161
|
+
const names = Object.keys(declarations);
|
|
2162
|
+
const isUsed = (name) => !hasPruning || used.has(name);
|
|
2163
|
+
const entry = {
|
|
2164
|
+
id,
|
|
2165
|
+
written: new Set(names.filter(isUsed)),
|
|
2166
|
+
index: root?.index
|
|
2167
|
+
};
|
|
2168
|
+
root = entry;
|
|
2169
|
+
writeRoot(entry);
|
|
2170
|
+
markUsed([...entry.written].flatMap((name) => scanVariables(declarations[name], VARIABLE_START)));
|
|
2171
|
+
};
|
|
2172
|
+
const insertTheme = (className, declarations, colorScheme) => {
|
|
2173
|
+
const schemeText = colorScheme ? `color-scheme:${colorScheme};` : "";
|
|
2174
|
+
themeNames.set(className, Object.keys(declarations));
|
|
2175
|
+
const body = toDeclarationText(Object.entries(declarations)) + schemeText;
|
|
2176
|
+
if (!hasPruning) {
|
|
2177
|
+
insertOnce(className, body ? [`.${className}{${body}}`] : [], "theme");
|
|
2178
|
+
return;
|
|
2179
|
+
}
|
|
2180
|
+
if (themes.has(className)) return;
|
|
2181
|
+
const sent = pageThemes.get(className);
|
|
2182
|
+
const isSent = sent !== void 0;
|
|
2183
|
+
insertedIds.add(className);
|
|
2184
|
+
const names = Object.keys(declarations);
|
|
2185
|
+
const entry = {
|
|
2186
|
+
declarations,
|
|
2187
|
+
schemeText,
|
|
2188
|
+
isSent,
|
|
2189
|
+
isStarted: false,
|
|
2190
|
+
written: new Set(isSent ? names.filter((name) => sent.has(name)) : []),
|
|
2191
|
+
variables: "",
|
|
2192
|
+
index: void 0,
|
|
2193
|
+
rules: []
|
|
2194
|
+
};
|
|
2195
|
+
themes.set(className, entry);
|
|
2196
|
+
insertedRules.set(className, []);
|
|
2197
|
+
markUsed(names.filter((name) => used.has(name)).flatMap((name) => scanVariables(declarations[name], VARIABLE_START)));
|
|
2198
|
+
writeTheme(className, entry, names.filter((name) => used.has(name) && !entry.written.has(name)));
|
|
2199
|
+
};
|
|
2200
|
+
function insertOnce(id, rules, layer, indexes) {
|
|
2201
|
+
if (insertedIds.has(id)) return false;
|
|
2202
|
+
insertedIds.add(id);
|
|
2203
|
+
const layered = wrapInLayer(rules, layer);
|
|
2204
|
+
insertedRules.set(id, layered);
|
|
2205
|
+
insertLayered(id, layered, indexes);
|
|
2206
|
+
if (hasPruning) markUsed(rules.flatMap((rule) => scanVariables(rule, VARIABLE_START)));
|
|
2207
|
+
return true;
|
|
2208
|
+
}
|
|
2209
|
+
const globals = /* @__PURE__ */ new Map();
|
|
2210
|
+
const pageGlobals = /* @__PURE__ */ new Map();
|
|
2211
|
+
let readArrived = () => void 0;
|
|
2212
|
+
const held = /* @__PURE__ */ new Map();
|
|
2213
|
+
const releasing = /* @__PURE__ */ new Set();
|
|
2214
|
+
const compileGlobal = (styles) => {
|
|
2215
|
+
const flat = compileGlobalRules(styles, hasColorFallbacks, scoping, hasValueRepair);
|
|
2216
|
+
const rules = wrapInLayer(flat, "global");
|
|
2217
|
+
return {
|
|
2218
|
+
id: `${prefix}-global-${hashString(rules.join(""))}`,
|
|
2219
|
+
rules,
|
|
2220
|
+
animations: findAnimationNames(flat)
|
|
2221
|
+
};
|
|
2222
|
+
};
|
|
2223
|
+
/** Whether the page has every one of `rules`, not taken by another set. */
|
|
2224
|
+
const hasPageRules = (rules) => rules.every((rule) => pageGlobals.get(rule)?.length);
|
|
2225
|
+
/** The rules of the page for `rules`, taken so that no other set takes them, when the page has every one of them; else undefined. */
|
|
2226
|
+
const takePageRules = (rules) => {
|
|
2227
|
+
if (!hasPageRules(rules)) return void 0;
|
|
2228
|
+
return rules.map((rule) => pageGlobals.get(rule).shift());
|
|
2229
|
+
};
|
|
2230
|
+
/** Writes the rules of `global`, or takes them over from the page, after the `@keyframes` rules they use. */
|
|
2231
|
+
const writeGlobal = ({ id, rules, animations: names }) => {
|
|
2232
|
+
names.forEach(writeKeyframes);
|
|
2233
|
+
if (!hasPageRules(rules)) readArrived();
|
|
2234
|
+
const pageRules = takePageRules(rules);
|
|
2235
|
+
const indexes = [];
|
|
2236
|
+
if (pageRules === void 0) {
|
|
2237
|
+
insertLayerOrder();
|
|
2238
|
+
for (const rule of rules) {
|
|
2239
|
+
const index = sheet.rules.length;
|
|
2240
|
+
sheet.insert(rule);
|
|
2241
|
+
if (sheet.rules.length > index) indexes.push(index);
|
|
2242
|
+
}
|
|
2243
|
+
onInsert?.(id, rules.join(""));
|
|
2244
|
+
checkLater?.(rules);
|
|
2245
|
+
}
|
|
2246
|
+
markUsed(rules.flatMap((rule) => scanVariables(rule, VARIABLE_START)));
|
|
2247
|
+
const entry = {
|
|
2248
|
+
count: 0,
|
|
2249
|
+
appCount: 0,
|
|
2250
|
+
rules,
|
|
2251
|
+
animations: names,
|
|
2252
|
+
indexes,
|
|
2253
|
+
pageRules
|
|
2254
|
+
};
|
|
2255
|
+
globals.set(id, entry);
|
|
2256
|
+
return entry;
|
|
2257
|
+
};
|
|
2258
|
+
/** Removes the rule at `index` of the sheet, and moves down every place the cache keeps that is after it. */
|
|
2259
|
+
const removeAt = (remove, index) => {
|
|
2260
|
+
remove(index);
|
|
2261
|
+
for (const entry of [...globals.values(), ...held.values()]) entry.indexes = entry.indexes.map((at) => at > index ? at - 1 : at);
|
|
2262
|
+
for (const entry of themes.values()) if (entry.index !== void 0 && entry.index > index) entry.index--;
|
|
2263
|
+
if (root?.index !== void 0 && root.index > index) root.index--;
|
|
2264
|
+
};
|
|
2265
|
+
/** Removes the rules of a set that nothing holds any more, unless they cannot be removed: then they stay, and count as written. */
|
|
2266
|
+
const removeGlobal = (id, entry) => {
|
|
2267
|
+
const { pageRules, indexes } = entry;
|
|
2268
|
+
const { remove } = sheet;
|
|
2269
|
+
if (pageRules ? pageRules.includes(void 0) : remove === void 0) return;
|
|
2270
|
+
globals.delete(id);
|
|
2271
|
+
pageRules?.forEach((rule) => deleteCssRule(rule));
|
|
2272
|
+
for (let at = indexes.length - 1; at >= 0; at--) removeAt(remove, indexes[at]);
|
|
2273
|
+
};
|
|
2274
|
+
const insertGlobal = (global, { isRendered = false } = {}) => {
|
|
2275
|
+
const entry = globals.get(global.id) ?? writeGlobal(global);
|
|
2276
|
+
const forApp = isRendered ? 0 : 1;
|
|
2277
|
+
entry.count++;
|
|
2278
|
+
entry.appCount += forApp;
|
|
2279
|
+
let isReleased = false;
|
|
2280
|
+
return () => {
|
|
2281
|
+
if (isReleased) return;
|
|
2282
|
+
isReleased = true;
|
|
2283
|
+
entry.count--;
|
|
2284
|
+
entry.appCount -= forApp;
|
|
2285
|
+
if (entry.count === 0) removeGlobal(global.id, entry);
|
|
2286
|
+
};
|
|
2287
|
+
};
|
|
2288
|
+
const getFullRules = (id) => {
|
|
2289
|
+
const entry = themes.get(id);
|
|
2290
|
+
const properties = (themeNames.get(id) ?? []).flatMap((name) => propertyRules[name] ?? []);
|
|
2291
|
+
const rootTheme = roots.get(id);
|
|
2292
|
+
if (rootTheme) return [...properties, ...createRootRules(rootTheme, Object.keys(rootTheme.declarations))];
|
|
2293
|
+
if (entry === void 0) return [...properties, ...insertedRules.get(id) ?? []];
|
|
2294
|
+
const body = toDeclarationText(Object.entries(entry.declarations)) + entry.schemeText;
|
|
2295
|
+
return [...properties, ...body ? [createThemeRule(prefix, hasLayers, `.${id}`, body)] : []];
|
|
2296
|
+
};
|
|
2297
|
+
/** The name of a class: the layer and `after` are part of the hash, so that the same styles in two layers, or after two classes, are two classes. */
|
|
2298
|
+
const nameClass = (layer, merged, rules, after) => {
|
|
2299
|
+
const label = hasLabels ? readLabel(merged) : "";
|
|
2300
|
+
const hash = hashString((hasLayers && layer === "overrides" ? layer : "") + after + rules.join(""));
|
|
2301
|
+
return [
|
|
2302
|
+
prefix,
|
|
2303
|
+
label,
|
|
2304
|
+
hash
|
|
2305
|
+
].filter(Boolean).join("-") + suffix;
|
|
2306
|
+
};
|
|
2307
|
+
/** The merged style of `styles` (the label last) and the name of its class; undefined when it has no rule. */
|
|
2308
|
+
const prepareClass = (layer, label, after, styles) => {
|
|
2309
|
+
if (styles.length === 0) return void 0;
|
|
2310
|
+
const merged = mergeStyles(label ? [...styles, { label }] : styles);
|
|
2311
|
+
const rules = compileRules("&", merged, hasColorFallbacks, scoping, hasValueRepair);
|
|
2312
|
+
if (rules.length === 0) return void 0;
|
|
2313
|
+
return {
|
|
2314
|
+
merged,
|
|
2315
|
+
className: nameClass(layer, merged, rules, after)
|
|
2316
|
+
};
|
|
2317
|
+
};
|
|
2318
|
+
/** The rules of the class `className` with the style `merged`. */
|
|
2319
|
+
const compileClass = (className, merged) => compileRules(`.${className}`, merged, hasColorFallbacks, scoping, hasValueRepair);
|
|
2320
|
+
const cssWith = (options, ...values) => {
|
|
2321
|
+
const { layer = "components", label, after = "" } = options;
|
|
2322
|
+
const { styles, rawClasses, sole } = sortClassValues(values, registered, adoptEntry);
|
|
2323
|
+
if (sole && after === "") return [...rawClasses, sole.use(layer)].filter(Boolean).join(" ");
|
|
2324
|
+
const prepared = prepareClass(layer, label, after, styles);
|
|
2325
|
+
if (prepared === void 0) return rawClasses.join(" ");
|
|
2326
|
+
const { merged, className } = prepared;
|
|
2327
|
+
if (held.size > 0) held.delete(className);
|
|
2328
|
+
registered.set(className, merged);
|
|
2329
|
+
if (after !== "") afters.set(className, after);
|
|
2330
|
+
insertClass(className, compileClass(className, merged), layer);
|
|
2331
|
+
return [...rawClasses, className].join(" ");
|
|
2332
|
+
};
|
|
2333
|
+
/**
|
|
2334
|
+
* Registers and writes the held class `className`, or takes it over from the page when the page has
|
|
2335
|
+
* it. A class written by this cache is removed after the delay unless it is held by then.
|
|
2336
|
+
*/
|
|
2337
|
+
const writeHeld = (className, merged, layer) => {
|
|
2338
|
+
const rules = compileClass(className, merged);
|
|
2339
|
+
const isPage = insertedIds.has(className);
|
|
2340
|
+
const entry = {
|
|
2341
|
+
count: 0,
|
|
2342
|
+
indexes: [],
|
|
2343
|
+
pageRules: isPage ? rules.length : void 0,
|
|
2344
|
+
hold: () => {
|
|
2345
|
+
let holder = held.get(className);
|
|
2346
|
+
if (holder === void 0) {
|
|
2347
|
+
if (registered.has(className)) return () => void 0;
|
|
2348
|
+
holder = writeHeld(className, merged, layer);
|
|
2349
|
+
}
|
|
2350
|
+
const current = holder;
|
|
2351
|
+
current.count++;
|
|
2352
|
+
let isReleased = false;
|
|
2353
|
+
return () => {
|
|
2354
|
+
if (isReleased) return;
|
|
2355
|
+
isReleased = true;
|
|
2356
|
+
current.count--;
|
|
2357
|
+
if (current.count === 0) scheduleRelease(className);
|
|
2358
|
+
};
|
|
2359
|
+
}
|
|
2360
|
+
};
|
|
2361
|
+
held.set(className, entry);
|
|
2362
|
+
registered.set(className, merged);
|
|
2363
|
+
insertClass(className, rules, layer, entry.indexes);
|
|
2364
|
+
if (!isPage) scheduleRelease(className);
|
|
2365
|
+
return entry;
|
|
2366
|
+
};
|
|
2367
|
+
/** The rules of the page (the style elements of the engine) that name the class `className`, when there are `count` of them; else undefined. */
|
|
2368
|
+
const findPageRules = (className, count) => {
|
|
2369
|
+
const escaped = className.replaceAll(/[^\w-]/g, String.raw`\$&`);
|
|
2370
|
+
const pattern = new RegExp(String.raw`\.${escaped}(?![\w-])`);
|
|
2371
|
+
const found = [];
|
|
2372
|
+
for (const element of page.querySelectorAll(createStyleSelector(prefix))) for (const rule of element.sheet?.cssRules ?? []) if (pattern.test(rule.cssText)) found.push(rule);
|
|
2373
|
+
return found.length === count ? found : void 0;
|
|
2374
|
+
};
|
|
2375
|
+
/** Removes a held class that nothing holds: its rules from the sheet, or from the page when they are the page's and can all be found; then the class is forgotten. Rules that cannot be found stay, and the class is permanent. */
|
|
2376
|
+
const removeHeld = (className, entry) => {
|
|
2377
|
+
held.delete(className);
|
|
2378
|
+
if (entry.pageRules === void 0) for (let at = entry.indexes.length - 1; at >= 0; at--) removeAt(sheet.remove, entry.indexes[at]);
|
|
2379
|
+
else {
|
|
2380
|
+
const rules = findPageRules(className, entry.pageRules);
|
|
2381
|
+
if (rules === void 0) return;
|
|
2382
|
+
rules.forEach(deleteCssRule);
|
|
2383
|
+
}
|
|
2384
|
+
registered.delete(className);
|
|
2385
|
+
insertedIds.delete(className);
|
|
2386
|
+
insertedRules.delete(className);
|
|
2387
|
+
animations.delete(className);
|
|
2388
|
+
};
|
|
2389
|
+
/** Removes each held class that waits for its removal and that nothing holds any more. */
|
|
2390
|
+
const flushReleases = () => {
|
|
2391
|
+
for (const className of releasing) {
|
|
2392
|
+
const entry = held.get(className);
|
|
2393
|
+
if (entry?.count === 0) removeHeld(className, entry);
|
|
2394
|
+
}
|
|
2395
|
+
releasing.clear();
|
|
2396
|
+
};
|
|
2397
|
+
/** Marks `className` for removal after the delay: one macrotask, so that a class let go and held again in the same task (a commit, a value that comes straight back) stays. */
|
|
2398
|
+
const scheduleRelease = (className) => {
|
|
2399
|
+
releasing.add(className);
|
|
2400
|
+
if (releasing.size === 1) setTimeout(flushReleases, 0);
|
|
2401
|
+
};
|
|
2402
|
+
const cssHeld = (options, ...values) => {
|
|
2403
|
+
const { layer = "components", label } = options;
|
|
2404
|
+
const { styles, rawClasses, sole } = sortClassValues(values, registered, adoptEntry);
|
|
2405
|
+
if (sole || sheet.remove === void 0) return {
|
|
2406
|
+
className: cssWith(options, ...values),
|
|
2407
|
+
hold: void 0
|
|
2408
|
+
};
|
|
2409
|
+
const prepared = prepareClass(layer, label, "", styles);
|
|
2410
|
+
if (prepared === void 0) return {
|
|
2411
|
+
className: rawClasses.join(" "),
|
|
2412
|
+
hold: void 0
|
|
2413
|
+
};
|
|
2414
|
+
const { merged, className: name } = prepared;
|
|
2415
|
+
const className = [...rawClasses, name].join(" ");
|
|
2416
|
+
let entry = held.get(name);
|
|
2417
|
+
if (entry === void 0) {
|
|
2418
|
+
if (registered.has(name)) return {
|
|
2419
|
+
className,
|
|
2420
|
+
hold: void 0
|
|
2421
|
+
};
|
|
2422
|
+
entry = writeHeld(name, merged, layer);
|
|
2423
|
+
}
|
|
2424
|
+
return {
|
|
2425
|
+
className,
|
|
2426
|
+
hold: entry.hold
|
|
2427
|
+
};
|
|
2428
|
+
};
|
|
2429
|
+
const cssIn = (layer, ...values) => cssWith({ layer }, ...values);
|
|
2430
|
+
const createEntry = (style) => {
|
|
2431
|
+
const merged = mergeStyles([style]);
|
|
2432
|
+
const rules = compileRules("&", merged, hasColorFallbacks, scoping, hasValueRepair);
|
|
2433
|
+
const names = {};
|
|
2434
|
+
const nameFor = (role) => {
|
|
2435
|
+
names[role] ??= rules.length === 0 ? "" : nameClass(role, merged, rules, "");
|
|
2436
|
+
return names[role];
|
|
2437
|
+
};
|
|
2438
|
+
const source = {
|
|
2439
|
+
style: merged,
|
|
2440
|
+
cache: self,
|
|
2441
|
+
use(role) {
|
|
2442
|
+
const name = nameFor(role);
|
|
2443
|
+
if (held.size > 0) held.delete(name);
|
|
2444
|
+
if (name !== "" && !registered.has(name)) {
|
|
2445
|
+
registered.set(name, merged);
|
|
2446
|
+
insertClass(name, compileClass(name, merged), role);
|
|
2447
|
+
}
|
|
2448
|
+
return name;
|
|
2449
|
+
}
|
|
2450
|
+
};
|
|
2451
|
+
const entry = createSheetEntry(nameFor("components"), source);
|
|
2452
|
+
entries.set(nameFor("components"), entry);
|
|
2453
|
+
return entry;
|
|
2454
|
+
};
|
|
2455
|
+
const adopted = /* @__PURE__ */ new WeakMap();
|
|
2456
|
+
const adoptEntry = (entry) => {
|
|
2457
|
+
const source = readEntrySource(entry);
|
|
2458
|
+
if (source === void 0 || source.cache === self) return entry;
|
|
2459
|
+
let own = adopted.get(source);
|
|
2460
|
+
if (own === void 0) {
|
|
2461
|
+
own = createEntry(source.style);
|
|
2462
|
+
adopted.set(source, own);
|
|
2463
|
+
}
|
|
2464
|
+
return own;
|
|
2465
|
+
};
|
|
2466
|
+
/** Takes in what rules on the page say: the classes, the layer order, the `@property` and `@keyframes` rules and the themes that are written, and the variables the rules use. */
|
|
2467
|
+
const absorb = (found) => {
|
|
2468
|
+
if (found.hasLayerOrder) insertedIds.add(layerOrderId);
|
|
2469
|
+
for (const name of found.classes) insertedIds.add(name);
|
|
2470
|
+
for (const name of found.keyframes) insertedIds.add(`@keyframes:${name}`);
|
|
2471
|
+
for (const [name, rule] of found.properties) insertedIds.add(`@property:${name}:${hashString(rule)}`);
|
|
2472
|
+
for (const [className, names] of found.themes) {
|
|
2473
|
+
insertedIds.add(className);
|
|
2474
|
+
const known = pageThemes.get(className) ?? /* @__PURE__ */ new Set();
|
|
2475
|
+
for (const name of names) known.add(name);
|
|
2476
|
+
pageThemes.set(className, known);
|
|
2477
|
+
const entry = themes.get(className);
|
|
2478
|
+
if (entry === void 0) continue;
|
|
2479
|
+
entry.isSent = true;
|
|
2480
|
+
for (const name of names) entry.written.add(name);
|
|
2481
|
+
}
|
|
2482
|
+
markUsed(found.variables);
|
|
2483
|
+
};
|
|
2484
|
+
if (page) {
|
|
2485
|
+
const selector = createStyleSelector(prefix);
|
|
2486
|
+
const scanned = /* @__PURE__ */ new WeakSet();
|
|
2487
|
+
const scan = (element) => {
|
|
2488
|
+
if (scanned.has(element) || element.getAttribute("media") === "not all") return;
|
|
2489
|
+
scanned.add(element);
|
|
2490
|
+
const found = scanStyleText(prefix, element.textContent);
|
|
2491
|
+
absorb(found);
|
|
2492
|
+
const list = element.sheet?.cssRules;
|
|
2493
|
+
const objects = list?.length === found.ruleCount ? list : void 0;
|
|
2494
|
+
for (const [index, text] of found.globals) {
|
|
2495
|
+
const known = pageGlobals.get(text) ?? [];
|
|
2496
|
+
known.push(objects?.[index]);
|
|
2497
|
+
pageGlobals.set(text, known);
|
|
2498
|
+
}
|
|
2499
|
+
for (const index of found.roots) pageRoots.push(objects?.[index]);
|
|
2500
|
+
};
|
|
2501
|
+
for (const element of page.querySelectorAll(selector)) scan(element);
|
|
2502
|
+
const readRecords = (records) => {
|
|
2503
|
+
for (const { addedNodes } of records) for (const node of addedNodes) if (node === page.body) observer.observe(node, { childList: true });
|
|
2504
|
+
else if (node.nodeType === 1 && node.matches(selector)) scan(node);
|
|
2505
|
+
};
|
|
2506
|
+
const observer = new MutationObserver((records) => {
|
|
2507
|
+
readRecords(records);
|
|
2508
|
+
checkLater?.();
|
|
2509
|
+
});
|
|
2510
|
+
observer.observe(page.head, { childList: true });
|
|
2511
|
+
observer.observe(page.body ?? page.documentElement, { childList: true });
|
|
2512
|
+
readArrived = () => readRecords(observer.takeRecords());
|
|
2513
|
+
checkLater?.();
|
|
2514
|
+
}
|
|
2515
|
+
/** What orders the classes of a page: a props class is right after its base class (its `after`), the rest by name. */
|
|
2516
|
+
const sortKey = (id) => `${afters.get(id) ?? id}|${afters.has(id) ? "1" : "0"}${id}`;
|
|
2517
|
+
/** The variables that the theme class `id` declares. */
|
|
2518
|
+
const readNames = (id) => themeNames.get(id);
|
|
2519
|
+
/** The rules that the cache wrote under `id`, a class or a theme class it made. */
|
|
2520
|
+
const readRules = (id) => insertedRules.get(id);
|
|
2521
|
+
/** The variables a page needs declared: those its rules (of classes, and `@keyframes` and global rules) refer to, those marked from outside, and what the values of the themes in play refer to. */
|
|
2522
|
+
const collectPageVariables = (classIds, themeIds, otherRules) => {
|
|
2523
|
+
const names = new Set(pinned);
|
|
2524
|
+
const addVariables = (rule) => {
|
|
2525
|
+
for (const name of scanVariables(rule, VARIABLE_START)) names.add(name);
|
|
2526
|
+
};
|
|
2527
|
+
for (const id of classIds) readRules(id).forEach(addVariables);
|
|
2528
|
+
otherRules.forEach(addVariables);
|
|
2529
|
+
addThemeReferences(names, themeIds, (id) => themes.get(id)?.declarations ?? roots.get(id)?.declarations);
|
|
2530
|
+
return names;
|
|
2531
|
+
};
|
|
2532
|
+
/** The rule of a theme class for a page: with pruning, only the variables in `names`, and its color-scheme. */
|
|
2533
|
+
const createPageThemeRules = (id, names) => {
|
|
2534
|
+
const entry = themes.get(id);
|
|
2535
|
+
if (entry === void 0) return readRules(id);
|
|
2536
|
+
const body = toDeclarationText(Object.keys(entry.declarations).filter((name) => names.has(name)).map((name) => [name, entry.declarations[name]])) + entry.schemeText;
|
|
2537
|
+
return body === "" ? [] : wrapInLayer([`.${id}{${body}}`], "theme");
|
|
2538
|
+
};
|
|
2539
|
+
const extractPageParts = (classNames, pageOptions = {}) => {
|
|
2540
|
+
const { themes: offered = [], animations: named = [], carried = /* @__PURE__ */ new Set(), rendered = /* @__PURE__ */ new Set() } = pageOptions;
|
|
2541
|
+
const wanted = /* @__PURE__ */ new Set([...classNames, ...offered]);
|
|
2542
|
+
const themeIds = [...wanted].filter((name) => themeNames.has(name)).sort(compareCodeUnits);
|
|
2543
|
+
const classIds = [...wanted].filter((name) => name.startsWith(`${prefix}-`) && !themeNames.has(name)).filter((name) => insertedRules.has(name)).sort((first, second) => sortKey(first) < sortKey(second) ? -1 : 1);
|
|
2544
|
+
const held = [...globals].filter(([id, entry]) => entry.appCount > 0 || rendered.has(id) || carried.has(id));
|
|
2545
|
+
const pageAnimations = [...classIds.flatMap((id) => animations.get(id) ?? []), ...held.flatMap(([, entry]) => entry.animations)];
|
|
2546
|
+
const keyframesRules = [.../* @__PURE__ */ new Set([...pageAnimations, ...named])].filter((name) => isKeyframesName(name) && !carried.has(name)).sort(compareCodeUnits).map(readKeyframesRule);
|
|
2547
|
+
const allGlobalRules = held.flatMap(([, entry]) => entry.rules);
|
|
2548
|
+
const rootIds = root === void 0 ? [] : [root.id];
|
|
2549
|
+
const allThemeIds = [...rootIds, ...themeIds];
|
|
2550
|
+
const names = hasPruning ? collectPageVariables(classIds, allThemeIds, [...keyframesRules, ...allGlobalRules]) : /* @__PURE__ */ new Set();
|
|
2551
|
+
const covered = new Set(allThemeIds.filter((id) => carried.has(id)).flatMap(readNames));
|
|
2552
|
+
const declared = hasPruning ? names : allThemeIds.flatMap(readNames);
|
|
2553
|
+
const properties = [...new Set(declared)].filter((name) => propertyRules[name] !== void 0 && !covered.has(name)).sort(compareCodeUnits);
|
|
2554
|
+
const newRoots = rootIds.filter((id) => !carried.has(id));
|
|
2555
|
+
const newThemes = themeIds.filter((id) => !carried.has(id));
|
|
2556
|
+
const newGlobals = held.filter(([id]) => !carried.has(id));
|
|
2557
|
+
const newClasses = classIds.filter((id) => !carried.has(id));
|
|
2558
|
+
const themeRules = [...newRoots.flatMap((id) => {
|
|
2559
|
+
const theme = roots.get(id);
|
|
2560
|
+
const all = Object.keys(theme.declarations);
|
|
2561
|
+
return createRootRules(theme, hasPruning ? all.filter((name) => names.has(name)) : all);
|
|
2562
|
+
}), ...newThemes.flatMap((id) => createPageThemeRules(id, names))];
|
|
2563
|
+
const globalRules = newGlobals.flatMap(([, entry]) => entry.rules);
|
|
2564
|
+
const classRules = newClasses.flatMap(readRules);
|
|
2565
|
+
const hasLayered = themeRules.length + globalRules.length + classRules.length > 0;
|
|
2566
|
+
return {
|
|
2567
|
+
global: {
|
|
2568
|
+
css: [
|
|
2569
|
+
hasLayers && hasLayered ? layerOrder : "",
|
|
2570
|
+
...properties.map((name) => propertyRules[name]),
|
|
2571
|
+
...keyframesRules,
|
|
2572
|
+
...themeRules,
|
|
2573
|
+
...globalRules
|
|
2574
|
+
].join(""),
|
|
2575
|
+
ids: [
|
|
2576
|
+
...newRoots,
|
|
2577
|
+
...newThemes,
|
|
2578
|
+
...newGlobals.map(([id]) => id)
|
|
2579
|
+
]
|
|
2580
|
+
},
|
|
2581
|
+
classes: {
|
|
2582
|
+
css: classRules.join(""),
|
|
2583
|
+
ids: newClasses
|
|
2584
|
+
}
|
|
2585
|
+
};
|
|
2586
|
+
};
|
|
2587
|
+
const extractPage = (classNames, pageOptions) => {
|
|
2588
|
+
const { global, classes } = extractPageParts(classNames, pageOptions);
|
|
2589
|
+
return {
|
|
2590
|
+
css: global.css + classes.css,
|
|
2591
|
+
ids: [...global.ids, ...classes.ids]
|
|
2592
|
+
};
|
|
2593
|
+
};
|
|
2594
|
+
const self = {
|
|
2595
|
+
prefix,
|
|
2596
|
+
hasLayers,
|
|
2597
|
+
layerOrder,
|
|
2598
|
+
registered,
|
|
2599
|
+
css: (...values) => cssIn("components", ...values),
|
|
2600
|
+
cssIn,
|
|
2601
|
+
cssWith,
|
|
2602
|
+
cssHeld,
|
|
2603
|
+
createEntry,
|
|
2604
|
+
findEntry: (className) => entries.get(className),
|
|
2605
|
+
adoptEntry,
|
|
2606
|
+
hasBaseClass,
|
|
2607
|
+
hasScope,
|
|
2608
|
+
hasPruning,
|
|
2609
|
+
usedVariables: used,
|
|
2610
|
+
markUsed: (names) => {
|
|
2611
|
+
const list = [...names];
|
|
2612
|
+
for (const name of list) pinned.add(name);
|
|
2613
|
+
markUsed(list);
|
|
2614
|
+
},
|
|
2615
|
+
insertTheme,
|
|
2616
|
+
insertRoot,
|
|
2617
|
+
registerProperties,
|
|
2618
|
+
setKeyframesTheme: (read) => {
|
|
2619
|
+
readKeyframesTheme = read;
|
|
2620
|
+
},
|
|
2621
|
+
insertKeyframes,
|
|
2622
|
+
getAnimations: (className) => animations.get(className) ?? [],
|
|
2623
|
+
compileGlobal,
|
|
2624
|
+
insertGlobal,
|
|
2625
|
+
insertOnce,
|
|
2626
|
+
getRules: (id) => insertedRules.get(id) ?? [],
|
|
2627
|
+
getFullRules,
|
|
2628
|
+
extract: () => ({
|
|
2629
|
+
css: sheet.rules.join(""),
|
|
2630
|
+
ids: [
|
|
2631
|
+
...insertedIds,
|
|
2632
|
+
...globals.keys(),
|
|
2633
|
+
...used
|
|
2634
|
+
]
|
|
2635
|
+
}),
|
|
2636
|
+
extractPage,
|
|
2637
|
+
extractPageParts,
|
|
2638
|
+
fingerprint
|
|
2639
|
+
};
|
|
2640
|
+
if (sheet.document) registerCache(sheet.document, self);
|
|
2641
|
+
return self;
|
|
2642
|
+
}
|
|
2643
|
+
// src/lazyRecord.ts
|
|
2644
|
+
/**
|
|
2645
|
+
* An object with one key for each of `keys`, whose value is made by `make` the first time the key is
|
|
2646
|
+
* read, and kept. `Object.keys` and a spread list the keys (a spread reads every one); nothing is made
|
|
2647
|
+
* until then. The object is frozen.
|
|
2648
|
+
*/
|
|
2649
|
+
function createLazyRecord(keys, make) {
|
|
2650
|
+
const record = {};
|
|
2651
|
+
for (const key of keys) {
|
|
2652
|
+
let value;
|
|
2653
|
+
let isMade = false;
|
|
2654
|
+
Object.defineProperty(record, key, {
|
|
2655
|
+
enumerable: true,
|
|
2656
|
+
get() {
|
|
2657
|
+
if (!isMade) {
|
|
2658
|
+
value = make(key);
|
|
2659
|
+
isMade = true;
|
|
2660
|
+
}
|
|
2661
|
+
return value;
|
|
2662
|
+
}
|
|
2663
|
+
});
|
|
2664
|
+
}
|
|
2665
|
+
return Object.freeze(record);
|
|
2666
|
+
}
|
|
2667
|
+
/**
|
|
2668
|
+
* `createLazyRecord` for the keys of `source`, with every value taken from the record that `build` makes,
|
|
2669
|
+
* which is called once, when the first key is read: for a sheet whose engine must not be made, or whose
|
|
2670
|
+
* platform call must not run, before it is used.
|
|
2671
|
+
*/
|
|
2672
|
+
function createDeferredRecord(source, build) {
|
|
2673
|
+
let built;
|
|
2674
|
+
return createLazyRecord(Object.keys(source), (key) => {
|
|
2675
|
+
built ??= build();
|
|
2676
|
+
return built[key];
|
|
2677
|
+
});
|
|
2678
|
+
}
|
|
2679
|
+
/**
|
|
2680
|
+
* An object that stands for the one `make` returns, which is made the first time the object is used in
|
|
2681
|
+
* any way (a property read, `in`, a listing of its keys, a check of whether it is frozen): from then on it
|
|
2682
|
+
* has the same properties, and is frozen as that one is. When `make` throws, the next use tries again. For a value that a module makes before the
|
|
2683
|
+
* engine it needs may exist, such as a `css()` result of a default style before `configure` is called.
|
|
2684
|
+
*/
|
|
2685
|
+
function createDeferredObject(make) {
|
|
2686
|
+
const target = {};
|
|
2687
|
+
let isMade = false;
|
|
2688
|
+
const ensure = () => {
|
|
2689
|
+
if (!isMade) {
|
|
2690
|
+
const made = make();
|
|
2691
|
+
isMade = true;
|
|
2692
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(made));
|
|
2693
|
+
Object.setPrototypeOf(target, Object.getPrototypeOf(made));
|
|
2694
|
+
if (Object.isFrozen(made)) Object.freeze(target);
|
|
2695
|
+
}
|
|
2696
|
+
return target;
|
|
2697
|
+
};
|
|
2698
|
+
return new Proxy(target, {
|
|
2699
|
+
get: (_target, key) => Reflect.get(ensure(), key),
|
|
2700
|
+
has: (_target, key) => Reflect.has(ensure(), key),
|
|
2701
|
+
ownKeys: () => Reflect.ownKeys(ensure()),
|
|
2702
|
+
getOwnPropertyDescriptor: (_target, key) => Reflect.getOwnPropertyDescriptor(ensure(), key),
|
|
2703
|
+
getPrototypeOf: () => Reflect.getPrototypeOf(ensure()),
|
|
2704
|
+
isExtensible: () => Reflect.isExtensible(ensure())
|
|
2705
|
+
});
|
|
2706
|
+
}
|
|
2707
|
+
// src/styleBase.ts
|
|
2708
|
+
/** Whether `style` is a function style. */
|
|
2709
|
+
function isStyleFunction(style) {
|
|
2710
|
+
return typeof style === "function";
|
|
2711
|
+
}
|
|
2712
|
+
/** Whether a style result adds no declaration of its own: nothing, or an object with no key but `label`. */
|
|
2713
|
+
function isEmptyResult(result) {
|
|
2714
|
+
return !result || Object.keys(result).every((key) => key === "label");
|
|
2715
|
+
}
|
|
2716
|
+
/**
|
|
2717
|
+
* Calls `style` with the props and the theme view, the props wrapped so that every read of a key other
|
|
2718
|
+
* than `theme` is noted, and so is every way of listing them (a spread, `Object.keys`, `in`): such a
|
|
2719
|
+
* function may depend on the props. The view is passed as it is, never wrapped. What the function reads
|
|
2720
|
+
* is the real props, so what it does (a throw included) is unchanged.
|
|
2721
|
+
*/
|
|
2722
|
+
function callTracked(style, props, view) {
|
|
2723
|
+
let isDependent = false;
|
|
2724
|
+
const note = (key) => {
|
|
2725
|
+
if (key !== "theme") isDependent = true;
|
|
2726
|
+
};
|
|
2727
|
+
return {
|
|
2728
|
+
result: style(new Proxy({
|
|
2729
|
+
...props,
|
|
2730
|
+
theme: view
|
|
2731
|
+
}, {
|
|
2732
|
+
get(target, key, receiver) {
|
|
2733
|
+
note(key);
|
|
2734
|
+
return Reflect.get(target, key, receiver);
|
|
2735
|
+
},
|
|
2736
|
+
has(target, key) {
|
|
2737
|
+
note(key);
|
|
2738
|
+
return Reflect.has(target, key);
|
|
2739
|
+
},
|
|
2740
|
+
ownKeys(target) {
|
|
2741
|
+
isDependent = true;
|
|
2742
|
+
return Reflect.ownKeys(target);
|
|
2743
|
+
},
|
|
2744
|
+
getOwnPropertyDescriptor(target, key) {
|
|
2745
|
+
note(key);
|
|
2746
|
+
return Reflect.getOwnPropertyDescriptor(target, key);
|
|
2747
|
+
}
|
|
2748
|
+
})),
|
|
2749
|
+
isDependent
|
|
2750
|
+
};
|
|
2751
|
+
}
|
|
2752
|
+
/**
|
|
2753
|
+
* The base of a style list, for one theme view: the longest run from the start of items that do not
|
|
2754
|
+
* depend on props, which are plain objects and functions that read nothing but `theme` (each is called
|
|
2755
|
+
* once here, with the props of this call, and its result is what goes into the base). `propsFrom` is the
|
|
2756
|
+
* index of the first item that depends on props, or the length of the list when none does. When there is
|
|
2757
|
+
* a props part, the empty results at the end of the base (the label of the component, which stands right
|
|
2758
|
+
* before it) are left out, so that the base is the same one for a component and for one that wraps it.
|
|
2759
|
+
*/
|
|
2760
|
+
function splitLeading(list, props, view) {
|
|
2761
|
+
const base = [];
|
|
2762
|
+
let propsFrom = 0;
|
|
2763
|
+
for (; propsFrom < list.length; propsFrom++) {
|
|
2764
|
+
const style = list[propsFrom];
|
|
2765
|
+
if (!isStyleFunction(style)) {
|
|
2766
|
+
base.push(style);
|
|
2767
|
+
continue;
|
|
2768
|
+
}
|
|
2769
|
+
const { result, isDependent } = callTracked(style, props, view);
|
|
2770
|
+
if (isDependent) break;
|
|
2771
|
+
base.push(result);
|
|
2772
|
+
}
|
|
2773
|
+
if (propsFrom < list.length) while (base.length > 0 && isEmptyResult(base.at(-1))) base.pop();
|
|
2774
|
+
return {
|
|
2775
|
+
base,
|
|
2776
|
+
propsFrom
|
|
2777
|
+
};
|
|
2778
|
+
}
|
|
2779
|
+
// src/sameView.ts
|
|
2780
|
+
/** Whether `value` is an array, or an object made by a literal. */
|
|
2781
|
+
function isContainer(value) {
|
|
2782
|
+
if (typeof value !== "object" || value === null) return false;
|
|
2783
|
+
const prototype = Object.getPrototypeOf(value);
|
|
2784
|
+
return prototype === Object.prototype || prototype === null || Array.isArray(value);
|
|
2785
|
+
}
|
|
2786
|
+
/** Whether two descriptors hold the same: equal values, or the same accessors. */
|
|
2787
|
+
function isSameDescriptor(first, second) {
|
|
2788
|
+
if ("value" in first) return "value" in second && isSameView(first.value, second.value);
|
|
2789
|
+
return first.get === second.get && first.set === second.set;
|
|
2790
|
+
}
|
|
2791
|
+
/**
|
|
2792
|
+
* Whether two theme views hold the same thing: arrays and plain objects with the same own members
|
|
2793
|
+
* (enumerable or not), each the same, and anything else the same value (a function, a class instance,
|
|
2794
|
+
* by identity). Two views of themes that differ only in the values of their variables are the same.
|
|
2795
|
+
*/
|
|
2796
|
+
function isSameView(first, second) {
|
|
2797
|
+
if (first === second) return true;
|
|
2798
|
+
if (!isContainer(first) || !isContainer(second)) return false;
|
|
2799
|
+
if (Object.getPrototypeOf(first) !== Object.getPrototypeOf(second)) return false;
|
|
2800
|
+
const keys = Reflect.ownKeys(first);
|
|
2801
|
+
if (keys.length !== Reflect.ownKeys(second).length) return false;
|
|
2802
|
+
return keys.every((key) => {
|
|
2803
|
+
const other = Object.getOwnPropertyDescriptor(second, key);
|
|
2804
|
+
const own = Object.getOwnPropertyDescriptor(first, key);
|
|
2805
|
+
return other !== void 0 && isSameDescriptor(own, other);
|
|
2806
|
+
});
|
|
2807
|
+
}
|
|
2808
|
+
// src/registered.ts
|
|
2809
|
+
const SYNTAXES = [
|
|
2810
|
+
"<color>",
|
|
2811
|
+
"<length>",
|
|
2812
|
+
"<number>",
|
|
2813
|
+
"<percentage>",
|
|
2814
|
+
"<length-percentage>",
|
|
2815
|
+
"<angle>",
|
|
2816
|
+
"<time>",
|
|
2817
|
+
"*"
|
|
2818
|
+
];
|
|
2819
|
+
const NUMBER = String.raw`[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?`;
|
|
2820
|
+
/** The units that depend on the element or the viewport: a value in them cannot be an initial value. */
|
|
2821
|
+
const RELATIVE_UNIT = "r?em|r?ex|r?cap|r?ch|r?ic|r?lh|[sld]?v(?:w|h|i|b|min|max)|cq(?:w|h|i|b|min|max)";
|
|
2822
|
+
const LENGTH_UNIT = `px|cm|mm|q|in|pt|pc|${RELATIVE_UNIT}`;
|
|
2823
|
+
const NUMBER_PATTERN = new RegExp(`^${NUMBER}$`, "i");
|
|
2824
|
+
const LENGTH_PATTERN = new RegExp(`^${NUMBER}(?:${LENGTH_UNIT})$`, "i");
|
|
2825
|
+
const PERCENTAGE_PATTERN = new RegExp(`^${NUMBER}%$`, "i");
|
|
2826
|
+
const ANGLE_PATTERN = new RegExp(`^${NUMBER}(?:deg|grad|rad|turn)$`, "i");
|
|
2827
|
+
const TIME_PATTERN = new RegExp(`^${NUMBER}(?:ms|s)$`, "i");
|
|
2828
|
+
const HEX_COLOR_PATTERN = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
2829
|
+
const COLOR_FUNCTION_PATTERN = /^(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color(?:-mix)?)\(.*\)$/i;
|
|
2830
|
+
const TRANSPARENT_PATTERN = /^transparent$/i;
|
|
2831
|
+
const KEYWORD_PATTERN = /^[a-z]+$/i;
|
|
2832
|
+
const MATH_PATTERN = /^(?:calc|min|max|clamp)\(.*\)$/i;
|
|
2833
|
+
const RELATIVE_PATTERN = new RegExp(String.raw`[\d.](?:${RELATIVE_UNIT})(?![a-z])`, "i");
|
|
2834
|
+
const DEPENDENT_PATTERN = /\b(?:var|env|attr)\(|currentcolor/i;
|
|
2835
|
+
/** Whether `value` is a color literal: a hex color, a color function or `transparent`. */
|
|
2836
|
+
const isColor = (value) => HEX_COLOR_PATTERN.test(value) || COLOR_FUNCTION_PATTERN.test(value) || TRANSPARENT_PATTERN.test(value);
|
|
2837
|
+
/** Whether `value` is a length: 0, or a number with a length unit. */
|
|
2838
|
+
const isLength = (value) => value === "0" || LENGTH_PATTERN.test(value);
|
|
2839
|
+
/** Whether `value` is a length or a percentage. */
|
|
2840
|
+
const isLengthPercentage = (value) => isLength(value) || PERCENTAGE_PATTERN.test(value);
|
|
2841
|
+
/** The test of each syntax but `*`; a keyword counts as a color. */
|
|
2842
|
+
const MATCHERS = {
|
|
2843
|
+
"<color>": (value) => isColor(value) || KEYWORD_PATTERN.test(value),
|
|
2844
|
+
"<length>": isLength,
|
|
2845
|
+
"<number>": (value) => NUMBER_PATTERN.test(value),
|
|
2846
|
+
"<percentage>": (value) => PERCENTAGE_PATTERN.test(value),
|
|
2847
|
+
"<length-percentage>": isLengthPercentage,
|
|
2848
|
+
"<angle>": (value) => ANGLE_PATTERN.test(value),
|
|
2849
|
+
"<time>": (value) => TIME_PATTERN.test(value)
|
|
2850
|
+
};
|
|
2851
|
+
/** Whether `value` is a literal of the syntax; a math function (calc, min, max, clamp) is taken on trust for a number, a length, a percentage, an angle or a time. */
|
|
2852
|
+
function isOfSyntax(syntax, value) {
|
|
2853
|
+
return syntax !== "<color>" && MATH_PATTERN.test(value) || MATCHERS[syntax](value);
|
|
2854
|
+
}
|
|
2855
|
+
/** The syntax that a base theme value says: a length, a percentage, a time, an angle, a color or a number; undefined for anything else. */
|
|
2856
|
+
function inferFromText(value) {
|
|
2857
|
+
if (LENGTH_PATTERN.test(value)) return "<length>";
|
|
2858
|
+
if (PERCENTAGE_PATTERN.test(value)) return "<percentage>";
|
|
2859
|
+
if (TIME_PATTERN.test(value)) return "<time>";
|
|
2860
|
+
if (ANGLE_PATTERN.test(value)) return "<angle>";
|
|
2861
|
+
if (isColor(value)) return "<color>";
|
|
2862
|
+
return NUMBER_PATTERN.test(value) ? "<number>" : void 0;
|
|
2863
|
+
}
|
|
2864
|
+
/** The syntax that the base value of a token says: a number is a length under a length key; undefined for text that says none. */
|
|
2865
|
+
function inferFromSource(source, isLength) {
|
|
2866
|
+
if (typeof source === "string") return inferFromText(source.trim());
|
|
2867
|
+
return isLength ? "<length>" : "<number>";
|
|
2868
|
+
}
|
|
2869
|
+
/** The syntax for a token registered with `true`: the one that its base value says, or a `TypeError` that asks for one. */
|
|
2870
|
+
function inferSyntax({ path, source, isLength }) {
|
|
2871
|
+
const inferred = inferFromSource(source, isLength);
|
|
2872
|
+
if (inferred === void 0) throw new TypeError(`registered: the syntax of "${path}" cannot be inferred from ${JSON.stringify(source)}: give an explicit syntax ('<color>', '<length>', '<number>', '<percentage>', '<length-percentage>', '<angle>', '<time>') or '*'.`);
|
|
2873
|
+
return inferred;
|
|
2874
|
+
}
|
|
2875
|
+
/** Throws a `TypeError` unless `declared` can be the initial value of a property of `syntax`. */
|
|
2876
|
+
function assertInitialValue(token, syntax) {
|
|
2877
|
+
const { path, value: declared } = token;
|
|
2878
|
+
if (DEPENDENT_PATTERN.test(declared) || RELATIVE_PATTERN.test(declared)) throw new TypeError(`registered: "${path}" is ${JSON.stringify(declared)}, which cannot be the initial value of a registered property: it depends on the element (var(), em, rem, vw, currentcolor and the like). Give it a value that does not, or register it as '*'.`);
|
|
2879
|
+
if (!isOfSyntax(syntax, declared)) throw new TypeError(`registered: "${path}" is ${JSON.stringify(declared)}, which is not a ${syntax} value. Register it with the syntax it has, or as '*'.`);
|
|
2880
|
+
}
|
|
2881
|
+
/**
|
|
2882
|
+
* The `@property` rule of a registered token. The initial value is its base value; `inherits` is true,
|
|
2883
|
+
* which a variable set on a theme wrapper needs to reach what is inside. The universal syntax `*` needs
|
|
2884
|
+
* no initial value and gets none. Throws a `TypeError` for a syntax that is unknown, or cannot be
|
|
2885
|
+
* inferred, or a value that cannot be the initial value.
|
|
2886
|
+
*/
|
|
2887
|
+
function createPropertyRule(token, syntax) {
|
|
2888
|
+
if (syntax !== true && !SYNTAXES.includes(syntax)) throw new TypeError(`registered: "${token.path}" has the unknown syntax ${JSON.stringify(syntax)}: use one of ${SYNTAXES.join(", ")}.`);
|
|
2889
|
+
const resolved = syntax === true ? inferSyntax(token) : syntax;
|
|
2890
|
+
if (resolved === "*") return `@property ${token.name}{syntax:"*";inherits:true;}`;
|
|
2891
|
+
assertInitialValue(token, resolved);
|
|
2892
|
+
return `@property ${token.name}{syntax:"${resolved}";inherits:true;initial-value:${token.value};}`;
|
|
2893
|
+
}
|
|
2894
|
+
// src/tokens.ts
|
|
2895
|
+
const DEFAULT_RAW_KEYS = ["breakpoints", "components"];
|
|
2896
|
+
const DEFAULT_LENGTH_KEYS = [
|
|
2897
|
+
"space",
|
|
2898
|
+
"sizes",
|
|
2899
|
+
"radii",
|
|
2900
|
+
"fontSizes",
|
|
2901
|
+
"borderWidths"
|
|
2902
|
+
];
|
|
2903
|
+
/** Whether `value` is an object made by a literal, not a class instance. */
|
|
2904
|
+
function isPlainObject$1(value) {
|
|
2905
|
+
if (typeof value !== "object" || value === null) return false;
|
|
2906
|
+
const prototype = Object.getPrototypeOf(value);
|
|
2907
|
+
return prototype === Object.prototype || prototype === null;
|
|
2908
|
+
}
|
|
2909
|
+
/** Whether `value` is a string or a number, the values that get a variable. */
|
|
2910
|
+
function isLeaf(value) {
|
|
2911
|
+
return typeof value === "string" || typeof value === "number";
|
|
2912
|
+
}
|
|
2913
|
+
/** A theme key as one part of a variable name. */
|
|
2914
|
+
function toSegment(key) {
|
|
2915
|
+
return key.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase().replace(/[^a-z0-9_-]/g, "-");
|
|
2916
|
+
}
|
|
2917
|
+
/** The value of the longest dotted path in `entries` that `path` starts with, or undefined. */
|
|
2918
|
+
function findByPath(entries, path) {
|
|
2919
|
+
for (let length = path.length; length > 0; length--) {
|
|
2920
|
+
const value = entries.get(path.slice(0, length).join("."));
|
|
2921
|
+
if (value !== void 0) return value;
|
|
2922
|
+
}
|
|
2923
|
+
}
|
|
2924
|
+
/** The dotted paths above each key of `modes`: 'a.b.c' gives 'a' and 'a.b'. */
|
|
2925
|
+
function collectParents(modes) {
|
|
2926
|
+
const parents = /* @__PURE__ */ new Set();
|
|
2927
|
+
for (const key of modes.keys()) {
|
|
2928
|
+
let index = key.indexOf(".");
|
|
2929
|
+
while (index !== -1) {
|
|
2930
|
+
parents.add(key.slice(0, index));
|
|
2931
|
+
index = key.indexOf(".", index + 1);
|
|
2932
|
+
}
|
|
2933
|
+
}
|
|
2934
|
+
return parents;
|
|
2935
|
+
}
|
|
2936
|
+
/** Turns a theme into variable references and the declarations that give them values. */
|
|
2937
|
+
function createTokens(theme, options = {}) {
|
|
2938
|
+
const { prefix = "", rawKeys = DEFAULT_RAW_KEYS, lengthKeys = DEFAULT_LENGTH_KEYS } = options;
|
|
2939
|
+
const modes = new Map(Object.entries(options.modes ?? {}));
|
|
2940
|
+
const registered = new Map(Object.entries(options.registered ?? {}));
|
|
2941
|
+
const parents = collectParents(modes);
|
|
2942
|
+
const toName = (path) => `--${[prefix, ...path.map(toSegment)].filter(Boolean).join("-")}`;
|
|
2943
|
+
/** The mode of the token at `path`: the longest path in `modes`, else `rawKeys`, else variable. */
|
|
2944
|
+
const readMode = (path) => {
|
|
2945
|
+
const mode = findByPath(modes, path);
|
|
2946
|
+
if (mode !== void 0) return mode;
|
|
2947
|
+
return rawKeys.includes(path[0]) ? "raw" : "variable";
|
|
2948
|
+
};
|
|
2949
|
+
/** Whether everything under `path` keeps its real value: one mode that writes no variable, and no entry deeper down. */
|
|
2950
|
+
const isKept = (path) => {
|
|
2951
|
+
const mode = readMode(path);
|
|
2952
|
+
return (mode === "raw" || mode === "inline") && !parents.has(path.join("."));
|
|
2953
|
+
};
|
|
2954
|
+
/** Whether `value` is a number under a length key: it is a length, written in px unless it is 0. */
|
|
2955
|
+
const isLengthValue = (path, value) => typeof value === "number" && lengthKeys.includes(path[0]);
|
|
2956
|
+
/** The value a variable at `path` holds: a number under a length key is in px. */
|
|
2957
|
+
const toDeclared = (path, value) => isLengthValue(path, value) && value !== 0 ? `${value}px` : String(value);
|
|
2958
|
+
const collect = (node, path, found) => {
|
|
2959
|
+
if (path.length > 0 && isKept(path)) return;
|
|
2960
|
+
if (Array.isArray(node)) node.forEach((item, index) => collect(item, [...path, String(index)], found));
|
|
2961
|
+
else if (isPlainObject$1(node)) for (const [key, value] of Object.entries(node)) collect(value, [...path, key], found);
|
|
2962
|
+
else if (isLeaf(node)) {
|
|
2963
|
+
const mode = readMode(path);
|
|
2964
|
+
if (mode === "variable" || mode === "reference") found.push({
|
|
2965
|
+
name: toName(path),
|
|
2966
|
+
path: path.join("."),
|
|
2967
|
+
value: toDeclared(path, node),
|
|
2968
|
+
source: node,
|
|
2969
|
+
isLength: isLengthValue(path, node)
|
|
2970
|
+
});
|
|
2971
|
+
}
|
|
2972
|
+
};
|
|
2973
|
+
const list = (partial) => {
|
|
2974
|
+
const found = [];
|
|
2975
|
+
collect(partial, [], found);
|
|
2976
|
+
return found;
|
|
2977
|
+
};
|
|
2978
|
+
const declare = (partial) => Object.fromEntries(list(partial).map(({ name, value }) => [name, value]));
|
|
2979
|
+
const baseTokens = list(theme);
|
|
2980
|
+
const baseDeclarations = Object.fromEntries(baseTokens.map(({ name, value }) => [name, value]));
|
|
2981
|
+
const registrations = {};
|
|
2982
|
+
for (const token of baseTokens) {
|
|
2983
|
+
const syntax = findByPath(registered, token.path.split("."));
|
|
2984
|
+
if (syntax === void 0) continue;
|
|
2985
|
+
registrations[token.name] = createPropertyRule(token, syntax);
|
|
2986
|
+
}
|
|
2987
|
+
const toRef = (path, node) => {
|
|
2988
|
+
const mode = readMode(path);
|
|
2989
|
+
const name = toName(path);
|
|
2990
|
+
if (mode === "variable") return `var(${name})`;
|
|
2991
|
+
if (mode !== "reference") return node;
|
|
2992
|
+
const fallback = baseDeclarations[name];
|
|
2993
|
+
return fallback === void 0 ? `var(${name})` : `var(${name}, ${fallback})`;
|
|
2994
|
+
};
|
|
2995
|
+
const toRefs = (node, path) => {
|
|
2996
|
+
if (path.length > 0 && isKept(path)) return node;
|
|
2997
|
+
if (Array.isArray(node)) return node.map((item, index) => toRefs(item, [...path, String(index)]));
|
|
2998
|
+
if (isPlainObject$1(node)) return Object.fromEntries(Object.entries(node).map(([key, value]) => [key, toRefs(value, [...path, key])]));
|
|
2999
|
+
return isLeaf(node) ? toRef(path, node) : node;
|
|
3000
|
+
};
|
|
3001
|
+
return {
|
|
3002
|
+
refs: toRefs(theme, []),
|
|
3003
|
+
declare,
|
|
3004
|
+
registrations,
|
|
3005
|
+
createView: (realTheme) => toRefs(realTheme, []),
|
|
3006
|
+
findNames: (wanted) => baseTokens.filter(({ path }) => wanted.some((one) => path === one || path.startsWith(`${one}.`))).map(({ name }) => name)
|
|
3007
|
+
};
|
|
3008
|
+
}
|
|
3009
|
+
// src/createEngine.ts
|
|
3010
|
+
/** Whether `sheet` is a sheet made from a function of the theme, which `resolve` turns into entries; an object sheet is its entries, none of which is a function. */
|
|
3011
|
+
function isThemedSheet(sheet) {
|
|
3012
|
+
return typeof sheet.resolve === "function";
|
|
3013
|
+
}
|
|
3014
|
+
/** How many recent views a new view is compared with. */
|
|
3015
|
+
const RECENT_VIEWS = 8;
|
|
3016
|
+
/** What is thrown when something needs the shape of the theme before any theme is known. */
|
|
3017
|
+
const NO_THEME_MESSAGE = "There is no theme yet: render a ThemeProvider with the full theme first, or call setTheme (or configure({ theme })) before a function style, a sheet or useTheme is used.";
|
|
3018
|
+
/** `text` in kebab-case: ButtonBase gives button-base. */
|
|
3019
|
+
function toKebabCase(text) {
|
|
3020
|
+
return text.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
3021
|
+
}
|
|
3022
|
+
/** Whether `value`, given to a styled component from outside, is a plain style object: not a sheet entry, not a class name. */
|
|
3023
|
+
function isPlainStyle(value) {
|
|
3024
|
+
return typeof value === "object" && value !== null && !isSheetEntry(value);
|
|
3025
|
+
}
|
|
3026
|
+
/** The label of a styled component: `name-slot`, or '' without a name. */
|
|
3027
|
+
function createLabel({ name, slot = "root" }) {
|
|
3028
|
+
return name ? `${toKebabCase(name)}-${toKebabCase(slot)}` : "";
|
|
3029
|
+
}
|
|
3030
|
+
/** A short text for the token options of an engine, or '' when it has none of its own: they are part of what the server and the browser must agree on. */
|
|
3031
|
+
function createTokenSignature(values) {
|
|
3032
|
+
return values.every((value) => value === void 0 || Array.isArray(value) && value.length === 0) ? "" : hashString(JSON.stringify(values));
|
|
3033
|
+
}
|
|
3034
|
+
/** Makes an engine that has no theme yet: it takes one from `setTheme`, or from the first `themeClass`. It needs no framework. */
|
|
3035
|
+
function createLazyEngine(options = {}) {
|
|
3036
|
+
return createFamilyEngine(options, {
|
|
3037
|
+
theme: void 0,
|
|
3038
|
+
engines: /* @__PURE__ */ new WeakMap()
|
|
3039
|
+
});
|
|
3040
|
+
}
|
|
3041
|
+
/** Makes an engine of `family`: its cache is the one in `options` or a new one; its base theme is the family's. */
|
|
3042
|
+
function createFamilyEngine(options, family) {
|
|
3043
|
+
const { cache: given, suffix, hasLabels, hasLayers, hasPruning, hasColorFallbacks } = options;
|
|
3044
|
+
const { hasScope, hasBaseClass, hasDevChecks, hasValueRepair } = options;
|
|
3045
|
+
const { alwaysDeclare = [] } = options;
|
|
3046
|
+
const { rawKeys, lengthKeys, modes, registered } = options;
|
|
3047
|
+
if (given && options.prefix !== void 0 && options.prefix !== given.prefix) throw new TypeError(`the engine options give the prefix "${options.prefix}", which differs from the prefix "${given.prefix}" of the given cache; variables and classes would use two prefixes. Leave out \`prefix\`, or give the same one.`);
|
|
3048
|
+
const prefix = given?.prefix ?? options.prefix ?? "css";
|
|
3049
|
+
const cache = given ?? createCache({
|
|
3050
|
+
prefix,
|
|
3051
|
+
suffix,
|
|
3052
|
+
hasLabels,
|
|
3053
|
+
hasLayers,
|
|
3054
|
+
hasPruning,
|
|
3055
|
+
hasColorFallbacks,
|
|
3056
|
+
hasValueRepair,
|
|
3057
|
+
hasScope,
|
|
3058
|
+
hasBaseClass,
|
|
3059
|
+
hasDevChecks,
|
|
3060
|
+
tokenSignature: createTokenSignature([
|
|
3061
|
+
rawKeys,
|
|
3062
|
+
lengthKeys,
|
|
3063
|
+
modes,
|
|
3064
|
+
registered,
|
|
3065
|
+
alwaysDeclare
|
|
3066
|
+
])
|
|
3067
|
+
});
|
|
3068
|
+
const tokenOptions = {
|
|
3069
|
+
prefix,
|
|
3070
|
+
rawKeys,
|
|
3071
|
+
lengthKeys,
|
|
3072
|
+
modes,
|
|
3073
|
+
registered
|
|
3074
|
+
};
|
|
3075
|
+
let tokensTheme;
|
|
3076
|
+
let tokens;
|
|
3077
|
+
let version = 0;
|
|
3078
|
+
/** Makes the tokens of the base theme of the family, when they were made from another one or not yet. */
|
|
3079
|
+
const sync = () => {
|
|
3080
|
+
const theme = family.theme;
|
|
3081
|
+
if (theme === tokensTheme) return;
|
|
3082
|
+
tokensTheme = theme;
|
|
3083
|
+
tokens = createTokens(theme, tokenOptions);
|
|
3084
|
+
views = /* @__PURE__ */ new WeakMap();
|
|
3085
|
+
recentViews = [];
|
|
3086
|
+
cache.registerProperties(tokens.registrations);
|
|
3087
|
+
cache.markUsed(tokens.findNames(alwaysDeclare));
|
|
3088
|
+
version++;
|
|
3089
|
+
};
|
|
3090
|
+
const readTokens = () => {
|
|
3091
|
+
sync();
|
|
3092
|
+
if (!tokens) throw new Error(NO_THEME_MESSAGE);
|
|
3093
|
+
return tokens;
|
|
3094
|
+
};
|
|
3095
|
+
cache.setKeyframesTheme(() => readTokens().refs);
|
|
3096
|
+
const setTheme = (theme) => {
|
|
3097
|
+
family.theme = theme;
|
|
3098
|
+
sync();
|
|
3099
|
+
};
|
|
3100
|
+
let views = /* @__PURE__ */ new WeakMap();
|
|
3101
|
+
let recentViews = [];
|
|
3102
|
+
const createThemeView = (realTheme) => {
|
|
3103
|
+
sync();
|
|
3104
|
+
let view = views.get(realTheme);
|
|
3105
|
+
if (view === void 0) {
|
|
3106
|
+
const made = (tokens ?? createTokens(realTheme, tokenOptions)).createView(realTheme);
|
|
3107
|
+
view = recentViews.find((recent) => isSameView(recent, made));
|
|
3108
|
+
if (view === void 0) {
|
|
3109
|
+
view = made;
|
|
3110
|
+
recentViews = [made, ...recentViews].slice(0, RECENT_VIEWS);
|
|
3111
|
+
}
|
|
3112
|
+
views.set(realTheme, view);
|
|
3113
|
+
}
|
|
3114
|
+
return view;
|
|
3115
|
+
};
|
|
3116
|
+
const themeClasses = /* @__PURE__ */ new WeakMap();
|
|
3117
|
+
const themeId = (partial, colorScheme) => {
|
|
3118
|
+
if (family.theme === void 0) setTheme(partial);
|
|
3119
|
+
const key = colorScheme ?? "";
|
|
3120
|
+
const known = themeClasses.get(partial) ?? /* @__PURE__ */ new Map();
|
|
3121
|
+
themeClasses.set(partial, known);
|
|
3122
|
+
let className = known.get(key);
|
|
3123
|
+
if (className === void 0) {
|
|
3124
|
+
className = createThemeClass(cache, readTokens().declare(partial), { colorScheme });
|
|
3125
|
+
known.set(key, className);
|
|
3126
|
+
}
|
|
3127
|
+
return className;
|
|
3128
|
+
};
|
|
3129
|
+
const roots = /* @__PURE__ */ new WeakMap();
|
|
3130
|
+
const declareRoot = (theme, colorScheme) => {
|
|
3131
|
+
if (family.theme === void 0) setTheme(theme);
|
|
3132
|
+
const key = colorScheme ?? "";
|
|
3133
|
+
const known = roots.get(theme) ?? /* @__PURE__ */ new Map();
|
|
3134
|
+
roots.set(theme, known);
|
|
3135
|
+
let made = known.get(key);
|
|
3136
|
+
if (made === void 0) {
|
|
3137
|
+
const declarations = readTokens().declare(theme);
|
|
3138
|
+
made = [declareRootTheme(cache, declarations, { colorScheme }), declarations];
|
|
3139
|
+
known.set(key, made);
|
|
3140
|
+
}
|
|
3141
|
+
const [id, declarations] = made;
|
|
3142
|
+
cache.insertRoot(id, declarations, colorScheme);
|
|
3143
|
+
return id;
|
|
3144
|
+
};
|
|
3145
|
+
const themeTexts = /* @__PURE__ */ new WeakMap();
|
|
3146
|
+
const readThemeText = (partial) => {
|
|
3147
|
+
sync();
|
|
3148
|
+
let text = themeTexts.get(partial);
|
|
3149
|
+
if (text === void 0) {
|
|
3150
|
+
const { declare, registrations } = tokens ?? createTokens(partial, tokenOptions);
|
|
3151
|
+
const declarations = declare(partial);
|
|
3152
|
+
const names = Object.keys(declarations);
|
|
3153
|
+
text = {
|
|
3154
|
+
declarations: toDeclarationText(Object.entries(declarations)),
|
|
3155
|
+
properties: names.map((name) => registrations[name] ?? "").join(""),
|
|
3156
|
+
names
|
|
3157
|
+
};
|
|
3158
|
+
if (tokens) themeTexts.set(partial, text);
|
|
3159
|
+
}
|
|
3160
|
+
return text;
|
|
3161
|
+
};
|
|
3162
|
+
const findThemeIds = (partial) => [...themeClasses.get(partial)?.values() ?? []];
|
|
3163
|
+
const themeClass = (partial, colorScheme) => createThemeClassName(cache.prefix, themeId(partial, colorScheme), colorScheme);
|
|
3164
|
+
const when = (partial, colorScheme) => createThemeKey(themeId(partial, colorScheme));
|
|
3165
|
+
const scopeName = `${cache.prefix}-scope`;
|
|
3166
|
+
const scopeClass = () => {
|
|
3167
|
+
cache.insertOnce(scopeName, [`.${scopeName}{display:contents;}`], "theme");
|
|
3168
|
+
return scopeName;
|
|
3169
|
+
};
|
|
3170
|
+
/** The entries of `source`, each made on first access; nothing is written until an entry is used. */
|
|
3171
|
+
const createEntries = (source) => createLazyRecord(Object.keys(source), (key) => cache.createEntry(source[key]));
|
|
3172
|
+
const createThemedSheet = (styles) => {
|
|
3173
|
+
let baseEntries;
|
|
3174
|
+
let madeAt = 0;
|
|
3175
|
+
const viewEntries = /* @__PURE__ */ new WeakMap();
|
|
3176
|
+
return { resolve(theme) {
|
|
3177
|
+
if (theme !== void 0) {
|
|
3178
|
+
let entries = viewEntries.get(theme);
|
|
3179
|
+
if (entries === void 0) {
|
|
3180
|
+
entries = createEntries(styles(theme));
|
|
3181
|
+
viewEntries.set(theme, entries);
|
|
3182
|
+
}
|
|
3183
|
+
return entries;
|
|
3184
|
+
}
|
|
3185
|
+
const { refs } = readTokens();
|
|
3186
|
+
if (baseEntries === void 0 || madeAt !== version) {
|
|
3187
|
+
baseEntries = createEntries(styles(refs));
|
|
3188
|
+
madeAt = version;
|
|
3189
|
+
}
|
|
3190
|
+
return baseEntries;
|
|
3191
|
+
} };
|
|
3192
|
+
};
|
|
3193
|
+
function createSheet(styles) {
|
|
3194
|
+
return typeof styles === "function" ? createThemedSheet(styles) : createEntries(styles);
|
|
3195
|
+
}
|
|
3196
|
+
const createStyler = (stylerOptions, styles, inherited = []) => {
|
|
3197
|
+
const label = createLabel(stylerOptions);
|
|
3198
|
+
const list = [
|
|
3199
|
+
...inherited,
|
|
3200
|
+
...label ? [{ label }] : [],
|
|
3201
|
+
...styles
|
|
3202
|
+
];
|
|
3203
|
+
const hasFunction = list.some(isStyleFunction);
|
|
3204
|
+
const resolveStyle = (style, props, theme) => typeof style === "function" ? style({
|
|
3205
|
+
...props,
|
|
3206
|
+
theme: theme ?? readTokens().refs
|
|
3207
|
+
}) : style;
|
|
3208
|
+
const resolveAll = (source, props, theme) => source.map((style) => resolveStyle(style, props, theme));
|
|
3209
|
+
let fixed;
|
|
3210
|
+
const getFixed = () => fixed ??= hasFunction ? {
|
|
3211
|
+
baseClass: "",
|
|
3212
|
+
propsStyles: list
|
|
3213
|
+
} : {
|
|
3214
|
+
baseClass: cache.css(...list),
|
|
3215
|
+
propsStyles: []
|
|
3216
|
+
};
|
|
3217
|
+
const plans = /* @__PURE__ */ new WeakMap();
|
|
3218
|
+
const makePlan = (props, view) => {
|
|
3219
|
+
const { base, propsFrom } = splitLeading(list, props, view);
|
|
3220
|
+
const hasProps = propsFrom < list.length;
|
|
3221
|
+
const lead = label ? [{ label }] : [];
|
|
3222
|
+
return {
|
|
3223
|
+
baseClass: base.some(Boolean) ? cache.css(...base) : "",
|
|
3224
|
+
propsStyles: hasProps ? [...lead, ...list.slice(propsFrom)] : []
|
|
3225
|
+
};
|
|
3226
|
+
};
|
|
3227
|
+
const getPlan = (props, theme) => {
|
|
3228
|
+
if (!cache.hasBaseClass || !hasFunction) return getFixed();
|
|
3229
|
+
const view = theme ?? readTokens().refs;
|
|
3230
|
+
let plan = plans.get(view);
|
|
3231
|
+
if (plan === void 0) {
|
|
3232
|
+
plan = makePlan(props, view);
|
|
3233
|
+
plans.set(view, plan);
|
|
3234
|
+
}
|
|
3235
|
+
return plan;
|
|
3236
|
+
};
|
|
3237
|
+
/** The classes of the styles: the base, then the props class that is made after it. */
|
|
3238
|
+
const getOwn = (plan, props, theme) => {
|
|
3239
|
+
if (plan.propsStyles.length === 0) return plan.baseClass;
|
|
3240
|
+
const resolved = resolveAll(plan.propsStyles, props, theme);
|
|
3241
|
+
const second = cache.cssWith({ after: plan.baseClass }, ...resolved);
|
|
3242
|
+
return [plan.baseClass, second].filter(Boolean).join(" ");
|
|
3243
|
+
};
|
|
3244
|
+
/** Without layers there is no override class and no order between classes to rely on: what comes in is merged with all of the styles, into the one class. */
|
|
3245
|
+
const getMerged = (props, theme, extras) => cache.css(...resolveAll(list, props, theme), ...extras);
|
|
3246
|
+
const getClassName = (props, extra, theme) => {
|
|
3247
|
+
const extras = Array.isArray(extra) ? extra : [extra];
|
|
3248
|
+
const hasExtras = extras.some(Boolean);
|
|
3249
|
+
if (hasExtras && !cache.hasLayers) return getMerged(props, theme, extras);
|
|
3250
|
+
const own = getOwn(getPlan(props, theme), props, theme);
|
|
3251
|
+
if (!hasExtras) return own;
|
|
3252
|
+
return [own, cache.cssWith({
|
|
3253
|
+
layer: "overrides",
|
|
3254
|
+
label: label || void 0
|
|
3255
|
+
}, ...extras)].filter(Boolean).join(" ");
|
|
3256
|
+
};
|
|
3257
|
+
return {
|
|
3258
|
+
styles: list,
|
|
3259
|
+
isDynamic: hasFunction,
|
|
3260
|
+
getClassName,
|
|
3261
|
+
getHeldClass(props, extra, theme) {
|
|
3262
|
+
const extras = Array.isArray(extra) ? extra : [extra];
|
|
3263
|
+
if (!extras.some(isPlainStyle)) return {
|
|
3264
|
+
className: getClassName(props, extras, theme),
|
|
3265
|
+
hold: void 0
|
|
3266
|
+
};
|
|
3267
|
+
if (!cache.hasLayers) return cache.cssHeld({}, ...resolveAll(list, props, theme), ...extras);
|
|
3268
|
+
const own = getOwn(getPlan(props, theme), props, theme);
|
|
3269
|
+
const overrides = cache.cssHeld({
|
|
3270
|
+
layer: "overrides",
|
|
3271
|
+
label: label || void 0
|
|
3272
|
+
}, ...extras);
|
|
3273
|
+
return {
|
|
3274
|
+
className: [own, overrides.className].filter(Boolean).join(" "),
|
|
3275
|
+
hold: overrides.hold
|
|
3276
|
+
};
|
|
3277
|
+
}
|
|
3278
|
+
};
|
|
3279
|
+
};
|
|
3280
|
+
const resolveGlobal = (input, theme) => {
|
|
3281
|
+
const readTheme = () => theme ?? readTokens().refs;
|
|
3282
|
+
return (Array.isArray(input) ? input : [input]).map((item) => cache.compileGlobal(readGlobalStyles(item, readTheme)));
|
|
3283
|
+
};
|
|
3284
|
+
const insertGlobal = (input, theme) => {
|
|
3285
|
+
const releases = resolveGlobal(input, theme).map((set) => cache.insertGlobal(set));
|
|
3286
|
+
return () => {
|
|
3287
|
+
for (const release of releases) release();
|
|
3288
|
+
};
|
|
3289
|
+
};
|
|
3290
|
+
const adoptedSheets = /* @__PURE__ */ new WeakMap();
|
|
3291
|
+
const adoptSheet = (entries) => {
|
|
3292
|
+
let adopted = adoptedSheets.get(entries);
|
|
3293
|
+
if (adopted === void 0) {
|
|
3294
|
+
adopted = createLazyRecord(Object.keys(entries), (key) => cache.adoptEntry(entries[key]));
|
|
3295
|
+
adoptedSheets.set(entries, adopted);
|
|
3296
|
+
}
|
|
3297
|
+
return adopted;
|
|
3298
|
+
};
|
|
3299
|
+
const engine = {
|
|
3300
|
+
cache,
|
|
3301
|
+
css: cache.css,
|
|
3302
|
+
resolveGlobal,
|
|
3303
|
+
insertGlobal,
|
|
3304
|
+
get hasTheme() {
|
|
3305
|
+
return family.theme !== void 0;
|
|
3306
|
+
},
|
|
3307
|
+
get baseTheme() {
|
|
3308
|
+
return family.theme;
|
|
3309
|
+
},
|
|
3310
|
+
get refs() {
|
|
3311
|
+
return readTokens().refs;
|
|
3312
|
+
},
|
|
3313
|
+
tokenOptions,
|
|
3314
|
+
scopeClass,
|
|
3315
|
+
keyframes,
|
|
3316
|
+
insertKeyframes: cache.insertKeyframes,
|
|
3317
|
+
setTheme,
|
|
3318
|
+
createThemeView,
|
|
3319
|
+
themeClass,
|
|
3320
|
+
themeId,
|
|
3321
|
+
findThemeIds,
|
|
3322
|
+
declareRoot,
|
|
3323
|
+
readThemeText,
|
|
3324
|
+
when,
|
|
3325
|
+
whenScheme: createSchemeKey,
|
|
3326
|
+
createSheet,
|
|
3327
|
+
createStyler,
|
|
3328
|
+
withCache: (other) => family.engines.get(other) ?? createFamilyEngine({
|
|
3329
|
+
...options,
|
|
3330
|
+
prefix: void 0,
|
|
3331
|
+
cache: other
|
|
3332
|
+
}, family),
|
|
3333
|
+
adoptSheet
|
|
3334
|
+
};
|
|
3335
|
+
family.engines.set(cache, engine);
|
|
3336
|
+
return engine;
|
|
3337
|
+
}
|
|
3338
|
+
/** Makes the engine of one theme shape: a cache, the theme as variables, and what the bindings build on. It needs no framework. */
|
|
3339
|
+
function createEngine(theme, options = {}) {
|
|
3340
|
+
const engine = createLazyEngine(options);
|
|
3341
|
+
engine.setTheme(theme);
|
|
3342
|
+
return engine;
|
|
3343
|
+
}
|
|
3344
|
+
// src/css.ts
|
|
3345
|
+
/** Whether `value`, or an item of a list in it, is a function. */
|
|
3346
|
+
function hasFunction(value) {
|
|
3347
|
+
return typeof value === "function" || Array.isArray(value) && value.some(hasFunction);
|
|
3348
|
+
}
|
|
3349
|
+
/**
|
|
3350
|
+
* The style of `args` when they are a tagged template (`caller` was used as a tag), read as CSS text;
|
|
3351
|
+
* undefined when they are not. `caller` has no props, so a function among the interpolations is a
|
|
3352
|
+
* `TypeError` that names it.
|
|
3353
|
+
*/
|
|
3354
|
+
function readStaticTemplate(args, caller) {
|
|
3355
|
+
const [first, ...values] = args;
|
|
3356
|
+
if (!isTemplateStrings(first)) return void 0;
|
|
3357
|
+
const parsed = parseStyleTemplate(first, values);
|
|
3358
|
+
if (typeof parsed === "function") throw new TypeError(`${caller}(): an interpolation of this template is a function, which needs props, and ${caller}() has none. Use the template with styled, or call the function yourself and interpolate what it returns.`);
|
|
3359
|
+
return parsed;
|
|
3360
|
+
}
|
|
3361
|
+
/**
|
|
3362
|
+
* Throws a `TypeError` that names `caller` when an argument, or an item of a list among them, is a
|
|
3363
|
+
* function: `caller` has no props to call it with.
|
|
3364
|
+
*/
|
|
3365
|
+
function rejectFunctions(args, caller) {
|
|
3366
|
+
if (args.some(hasFunction)) throw new TypeError(`${caller}(): a function is not a style here, because ${caller}() has no props. Give it to styled, or call it yourself and pass what it returns.`);
|
|
3367
|
+
}
|
|
3368
|
+
/**
|
|
3369
|
+
* The style of the arguments of `css()`: a tagged template read as CSS text, or style objects, `css()`
|
|
3370
|
+
* results and sheet entries, with lists flattened and `false`, `null` and `undefined` dropped, merged in
|
|
3371
|
+
* order, the last winning. An entry that a framework merged into an object is found with `findEntry`.
|
|
3372
|
+
* Throws a `TypeError` for a function, for a string, and for an entry that is only a class name.
|
|
3373
|
+
*/
|
|
3374
|
+
function readCssStyle(args, findEntry) {
|
|
3375
|
+
const template = readStaticTemplate(args, "css");
|
|
3376
|
+
if (template) return template;
|
|
3377
|
+
rejectFunctions(args, "css");
|
|
3378
|
+
return mergeStyles(flattenStyle(args, findEntry).map((item) => readEntryStyle(item, "css")));
|
|
3379
|
+
}
|
|
3380
|
+
/**
|
|
3381
|
+
* `css()` of a web binding over `cache`: one sheet entry of the merged style (see `readCssStyle`). Its
|
|
3382
|
+
* class name is worked out now and its rule is written when the entry is first used, as an entry of a
|
|
3383
|
+
* sheet; equal styles give the same class.
|
|
3384
|
+
*/
|
|
3385
|
+
function createCssEntry(cache, args) {
|
|
3386
|
+
return cache.createEntry(readCssStyle(args, cache.findEntry));
|
|
3387
|
+
}
|
|
3388
|
+
/**
|
|
3389
|
+
* The styles of a call of the function that `styled(tag)` returns: as a tag, the template is one style
|
|
3390
|
+
* (an object, or a function of the props when an interpolation is a function); else the styles given,
|
|
3391
|
+
* with lists flattened.
|
|
3392
|
+
*/
|
|
3393
|
+
function readStyleArguments(args) {
|
|
3394
|
+
const [first, ...values] = args;
|
|
3395
|
+
if (isTemplateStrings(first)) return [parseStyleTemplate(first, values)];
|
|
3396
|
+
return args.flat(1e400);
|
|
3397
|
+
}
|
|
3398
|
+
/**
|
|
3399
|
+
* The `toString` of a styled component, which makes it a selector in a template or a key
|
|
3400
|
+
* (`${Button}:hover &`): `.` and a class that every element of the component has. That is the
|
|
3401
|
+
* component's own `target` when it was given one (each class of it, when it holds several); else, when none of its styles is a function, its class
|
|
3402
|
+
* (the base class, the same for every element); else there is none, and it throws a `TypeError` that says
|
|
3403
|
+
* to give the component a `target`.
|
|
3404
|
+
*/
|
|
3405
|
+
function createComponentSelector(target, styler) {
|
|
3406
|
+
return () => {
|
|
3407
|
+
if (target) return `.${target.trim().replaceAll(/\s+/g, ".")}`;
|
|
3408
|
+
const className = styler.isDynamic ? "" : styler.getClassName({});
|
|
3409
|
+
if (className === "") throw new TypeError("A styled component used as a selector needs a class that every element of it has: give it a `target` (styled(tag, { target: \"my-button\" })), or write its styles without functions.");
|
|
3410
|
+
return `.${className}`;
|
|
3411
|
+
};
|
|
3412
|
+
}
|
|
3413
|
+
// src/scanHtml.ts
|
|
3414
|
+
/** The end of the name of a marker of global rules: `<prefix>-global`. */
|
|
3415
|
+
const MARKER_END = "-global";
|
|
3416
|
+
/**
|
|
3417
|
+
* The marker by which a `Global` that renders no style element names its sets in the markup of a server
|
|
3418
|
+
* render, so that `extractPageStyles` puts them in that page only: the sets that have rules, by id. None
|
|
3419
|
+
* when no set has a rule. A binding renders it as `<meta name="<name>" content="<ids>">` (React) or as a
|
|
3420
|
+
* comment `<!--<name> <ids>-->` (Vue).
|
|
3421
|
+
*/
|
|
3422
|
+
function createGlobalMarker(prefix, sets) {
|
|
3423
|
+
const ids = sets.filter(({ rules }) => rules.length > 0).map(({ id }) => id);
|
|
3424
|
+
return ids.length === 0 ? void 0 : {
|
|
3425
|
+
name: `${prefix}${MARKER_END}`,
|
|
3426
|
+
ids: ids.join(" ")
|
|
3427
|
+
};
|
|
3428
|
+
}
|
|
3429
|
+
/** The elements whose content is text, not markup: nothing in them is an attribute. */
|
|
3430
|
+
const RAW_TEXT_ELEMENTS = /* @__PURE__ */ new Set([
|
|
3431
|
+
"script",
|
|
3432
|
+
"style",
|
|
3433
|
+
"textarea",
|
|
3434
|
+
"title"
|
|
3435
|
+
]);
|
|
3436
|
+
/** Whether `code` is whitespace in HTML: tab, line feed, vertical tab, form feed, carriage return, space. */
|
|
3437
|
+
function isSpace(code) {
|
|
3438
|
+
return code === 32 || code >= 9 && code <= 13;
|
|
3439
|
+
}
|
|
3440
|
+
/** Whether `code` ends an attribute name: whitespace, '=', '>' or '/'. */
|
|
3441
|
+
function isNameEnd(code) {
|
|
3442
|
+
return isSpace(code) || code === 61 || code === 62 || code === 47;
|
|
3443
|
+
}
|
|
3444
|
+
/** Whether `code` is an ASCII letter. */
|
|
3445
|
+
function isLetter(code) {
|
|
3446
|
+
return code >= 97 && code <= 122 || code >= 65 && code <= 90;
|
|
3447
|
+
}
|
|
3448
|
+
/** The index of the first character at or after `index` that is not whitespace. */
|
|
3449
|
+
function skipSpaces(html, index) {
|
|
3450
|
+
let position = index;
|
|
3451
|
+
while (position < html.length && isSpace(codeAt(html, position))) position++;
|
|
3452
|
+
return position;
|
|
3453
|
+
}
|
|
3454
|
+
/** Adds the whitespace-separated tokens of `value` to `target`. */
|
|
3455
|
+
function addTokens(target, value) {
|
|
3456
|
+
for (const token of value.split(/\s+/)) if (token !== "") target.add(token);
|
|
3457
|
+
}
|
|
3458
|
+
/** The value that starts at `index` (quoted with ' or ", or bare) and the index after it. */
|
|
3459
|
+
function readValue(html, index) {
|
|
3460
|
+
const quote = html[index];
|
|
3461
|
+
if (quote === "\"" || quote === "'") {
|
|
3462
|
+
const close = html.indexOf(quote, index + 1);
|
|
3463
|
+
const end = close === -1 ? html.length : close;
|
|
3464
|
+
return {
|
|
3465
|
+
value: html.slice(index + 1, end),
|
|
3466
|
+
end: end + 1
|
|
3467
|
+
};
|
|
3468
|
+
}
|
|
3469
|
+
let end = index;
|
|
3470
|
+
while (end < html.length && !isSpace(codeAt(html, end)) && html[end] !== ">") end++;
|
|
3471
|
+
return {
|
|
3472
|
+
value: html.slice(index, end),
|
|
3473
|
+
end
|
|
3474
|
+
};
|
|
3475
|
+
}
|
|
3476
|
+
/** Adds the ids that a marker names to `found`: the tokens of `content` when `name` is that of a marker. */
|
|
3477
|
+
function addMarked(found, name, content) {
|
|
3478
|
+
if (name.endsWith(MARKER_END)) addTokens(found.rendered, content);
|
|
3479
|
+
}
|
|
3480
|
+
/** Adds what the attribute `name` with `value` says to `found`, or keeps it in `meta`. */
|
|
3481
|
+
function addAttribute(found, meta, name, value) {
|
|
3482
|
+
if (name === "class" || name === "classname") addTokens(found.classes, value);
|
|
3483
|
+
else if (name === "data-href") addTokens(found.carried, value);
|
|
3484
|
+
else if (name === "name") meta.name = value;
|
|
3485
|
+
else if (name === "content") meta.content = value;
|
|
3486
|
+
}
|
|
3487
|
+
/** Reads the attribute that starts at `index`, adds what matters to `found` or `meta`, and returns the index after it. */
|
|
3488
|
+
function readAttribute(html, index, found, meta) {
|
|
3489
|
+
let position = readNameEnd(html, index);
|
|
3490
|
+
if (position === index) position++;
|
|
3491
|
+
const name = html.slice(index, position).toLowerCase();
|
|
3492
|
+
position = skipSpaces(html, position);
|
|
3493
|
+
if (html[position] !== "=") return position;
|
|
3494
|
+
const { value, end } = readValue(html, skipSpaces(html, position + 1));
|
|
3495
|
+
addAttribute(found, meta, name, value);
|
|
3496
|
+
return end;
|
|
3497
|
+
}
|
|
3498
|
+
/** Reads the attributes of the tag whose name ended at `index`, adds what matters to `found`, and returns the index after the `>`. A `<meta>` (`isMeta`) is read for the marker of global rules too. */
|
|
3499
|
+
function readAttributes(html, index, found, isMeta) {
|
|
3500
|
+
const meta = {
|
|
3501
|
+
name: "",
|
|
3502
|
+
content: ""
|
|
3503
|
+
};
|
|
3504
|
+
let position = index;
|
|
3505
|
+
while (position < html.length) {
|
|
3506
|
+
const code = codeAt(html, position);
|
|
3507
|
+
if (code === 62) {
|
|
3508
|
+
if (isMeta) addMarked(found, meta.name, meta.content);
|
|
3509
|
+
return position + 1;
|
|
3510
|
+
}
|
|
3511
|
+
position = isSpace(code) || code === 47 ? position + 1 : readAttribute(html, position, found, meta);
|
|
3512
|
+
}
|
|
3513
|
+
return html.length;
|
|
3514
|
+
}
|
|
3515
|
+
/** Reads the comment whose text starts at `start` and ends at `end`: a marker of global rules adds the ids it names to `found`. */
|
|
3516
|
+
function readComment(html, start, end, found) {
|
|
3517
|
+
if (!isLetter(codeAt(html, start))) return;
|
|
3518
|
+
const text = html.slice(start, end);
|
|
3519
|
+
const space = text.indexOf(" ");
|
|
3520
|
+
if (space !== -1) addMarked(found, text.slice(0, space), text.slice(space + 1));
|
|
3521
|
+
}
|
|
3522
|
+
/** The index of the `</name` that closes a raw text element, or the end of the document. */
|
|
3523
|
+
function findClosingTag(html, index, name) {
|
|
3524
|
+
let at = html.indexOf("</", index);
|
|
3525
|
+
while (at !== -1 && html.slice(at + 2, at + 2 + name.length).toLowerCase() !== name) at = html.indexOf("</", at + 2);
|
|
3526
|
+
return at === -1 ? html.length : at;
|
|
3527
|
+
}
|
|
3528
|
+
/** The index of the end of the name (of a tag or an attribute) that starts at `index`. */
|
|
3529
|
+
function readNameEnd(html, index) {
|
|
3530
|
+
let end = index;
|
|
3531
|
+
while (end < html.length && !isNameEnd(codeAt(html, end))) end++;
|
|
3532
|
+
return end;
|
|
3533
|
+
}
|
|
3534
|
+
/**
|
|
3535
|
+
* The class tokens of `html`: the values of the `class` and `className` attributes, in one pass over
|
|
3536
|
+
* the markup, with the keys of the style elements it carries (`data-href`) and the ids that the markers
|
|
3537
|
+
* of `Global` name. Comments other than a marker and the content of `script`, `style`, `textarea` and
|
|
3538
|
+
* `title` are skipped, so a class name inside a script or a text is not taken for a class. Quoted and
|
|
3539
|
+
* bare values are read; a class name in any other attribute is not looked at. The pass moves forward
|
|
3540
|
+
* only: its time is linear in the length of the document, and there is no pattern to backtrack.
|
|
3541
|
+
*/
|
|
3542
|
+
function scanHtml(html) {
|
|
3543
|
+
const found = {
|
|
3544
|
+
classes: /* @__PURE__ */ new Set(),
|
|
3545
|
+
carried: /* @__PURE__ */ new Set(),
|
|
3546
|
+
rendered: /* @__PURE__ */ new Set()
|
|
3547
|
+
};
|
|
3548
|
+
let at = html.indexOf("<");
|
|
3549
|
+
while (at !== -1) {
|
|
3550
|
+
if (html.startsWith("<!--", at)) {
|
|
3551
|
+
const close = html.indexOf("-->", at + 4);
|
|
3552
|
+
const end = close === -1 ? html.length : close;
|
|
3553
|
+
readComment(html, at + 4, end, found);
|
|
3554
|
+
at = end + 3;
|
|
3555
|
+
} else if (isLetter(codeAt(html, at + 1))) {
|
|
3556
|
+
const nameEnd = readNameEnd(html, at + 1);
|
|
3557
|
+
const name = html.slice(at + 1, nameEnd).toLowerCase();
|
|
3558
|
+
at = readAttributes(html, nameEnd, found, name === "meta");
|
|
3559
|
+
if (RAW_TEXT_ELEMENTS.has(name)) at = findClosingTag(html, at, name);
|
|
3560
|
+
} else at++;
|
|
3561
|
+
at = html.indexOf("<", at);
|
|
3562
|
+
}
|
|
3563
|
+
return found;
|
|
3564
|
+
}
|
|
3565
|
+
// src/extractStyles.ts
|
|
3566
|
+
/** The style element of `css`: the cache's key, and the stamp of its options for the browser to compare. */
|
|
3567
|
+
function createTag(cache, css) {
|
|
3568
|
+
return `<style data-style="${cache.prefix}" data-options="${cache.fingerprint}">${css}</style>`;
|
|
3569
|
+
}
|
|
3570
|
+
/**
|
|
3571
|
+
* The style element of a static page or a server render: everything `cache` inserted, whatever page
|
|
3572
|
+
* inserted it. A cache that renders more than one page (a server process) should use `extractPageStyles`,
|
|
3573
|
+
* which carries only what one page uses. The element is stamped with the options of the cache
|
|
3574
|
+
* (`data-options`), which a cache made in the browser compares with its own; it holds no list of ids,
|
|
3575
|
+
* because the browser reads the rules themselves.
|
|
3576
|
+
*/
|
|
3577
|
+
function createStyleTag(cache) {
|
|
3578
|
+
return createTag(cache, cache.extract().css);
|
|
3579
|
+
}
|
|
3580
|
+
/**
|
|
3581
|
+
* The CSS of one page, decided from the page: the finished `html` is scanned for the class names it
|
|
3582
|
+
* holds (the values of its `class` and `className` attributes only; scripts, styles, comments and other
|
|
3583
|
+
* attributes are not looked at), and only those classes are written, with the `@keyframes` rules they
|
|
3584
|
+
* use and the theme classes trimmed to the variables that the page uses. Nothing depends on what else the cache holds, on the
|
|
3585
|
+
* order pages were rendered in, or on pages being rendered at the same time, and the cache is not changed.
|
|
3586
|
+
* The `<style href precedence>` elements that `html` carries already (React's server render of the
|
|
3587
|
+
* client build emits them) are not written again. The global rules are those inserted for the app
|
|
3588
|
+
* (`engine.insertGlobal`), which are in every page, and those of the `Global` components that `html`
|
|
3589
|
+
* rendered: a style element it carries, or a marker that names the set (a `<meta name="<prefix>-global">`
|
|
3590
|
+
* of React, a comment `<!--<prefix>-global ...-->` of Vue). A set that another page rendered is not.
|
|
3591
|
+
*/
|
|
3592
|
+
function extractPageStyles(cache, html, options = {}) {
|
|
3593
|
+
const { css, ids } = cache.extractPage(...readPage(html, options));
|
|
3594
|
+
return {
|
|
3595
|
+
css,
|
|
3596
|
+
ids,
|
|
3597
|
+
options: cache.fingerprint,
|
|
3598
|
+
tag: createTag(cache, css)
|
|
3599
|
+
};
|
|
3600
|
+
}
|
|
3601
|
+
/** The class names of `html`, and what `extractPage` is told besides them. */
|
|
3602
|
+
function readPage(html, options) {
|
|
3603
|
+
const { classes, carried, rendered } = scanHtml(html);
|
|
3604
|
+
return [classes, {
|
|
3605
|
+
themes: (options.themes ?? []).flatMap((value) => value.split(/\s+/)),
|
|
3606
|
+
animations: options.animations,
|
|
3607
|
+
carried,
|
|
3608
|
+
rendered
|
|
3609
|
+
}];
|
|
3610
|
+
}
|
|
3611
|
+
/**
|
|
3612
|
+
* `extractPageStyles` in two parts, decided from `html` the same way: the rules that no class stands for
|
|
3613
|
+
* (the layer order, the `@property` and `@keyframes` rules, the theme classes, the global rules) and the
|
|
3614
|
+
* rules of the classes. The two CSS texts joined in this order are the CSS of `extractPageStyles`.
|
|
3615
|
+
*/
|
|
3616
|
+
function extractPageStyleParts(cache, html, options = {}) {
|
|
3617
|
+
return cache.extractPageParts(...readPage(html, options));
|
|
3618
|
+
}
|
|
3619
|
+
// src/mergeDeep.ts
|
|
3620
|
+
/** Whether `value` is an object made by a literal, not a class instance. */
|
|
3621
|
+
function isPlainObject(value) {
|
|
3622
|
+
if (typeof value !== "object" || value === null) return false;
|
|
3623
|
+
const prototype = Object.getPrototypeOf(value);
|
|
3624
|
+
return prototype === Object.prototype || prototype === null;
|
|
3625
|
+
}
|
|
3626
|
+
/**
|
|
3627
|
+
* `override` over `base`: plain objects merge key by key, everything else is replaced, undefined keeps
|
|
3628
|
+
* `base`. Every own member is taken with its descriptor, so a member that is not enumerable (a helper
|
|
3629
|
+
* function on `breakpoints`) is kept, from `override` when it has one, else from `base`.
|
|
3630
|
+
*/
|
|
3631
|
+
function mergeDeep(base, override) {
|
|
3632
|
+
if (override === void 0) return base;
|
|
3633
|
+
if (!isPlainObject(base) || !isPlainObject(override)) return override;
|
|
3634
|
+
const descriptors = Object.getOwnPropertyDescriptors(base);
|
|
3635
|
+
const given = Object.getOwnPropertyDescriptors(override);
|
|
3636
|
+
for (const key of Reflect.ownKeys(given)) {
|
|
3637
|
+
const descriptor = given[key];
|
|
3638
|
+
descriptors[key] = "value" in descriptor ? {
|
|
3639
|
+
...descriptor,
|
|
3640
|
+
value: mergeDeep(base[key], descriptor.value)
|
|
3641
|
+
} : descriptor;
|
|
3642
|
+
}
|
|
3643
|
+
return Object.create(Object.getPrototypeOf(base), descriptors);
|
|
3644
|
+
}
|
|
3645
|
+
// src/tagShorthands.ts
|
|
3646
|
+
/** What a shorthand can be: a tag name, a lowercase letter then letters and digits (`div`, `h1`, `foreignObject`). */
|
|
3647
|
+
const TAG_PATTERN = /^[a-z][a-zA-Z0-9]*$/;
|
|
3648
|
+
/**
|
|
3649
|
+
* `styled` with a shorthand for every tag: `styled.div(...styles)` is `styled('div')(...styles)`. Nothing
|
|
3650
|
+
* is made up front: a member that the function does not have and that looks like a tag name is made when
|
|
3651
|
+
* it is read. `then` is not a tag, so that `styled` is never taken for a promise.
|
|
3652
|
+
*/
|
|
3653
|
+
function withTagShorthands(styled) {
|
|
3654
|
+
return new Proxy(styled, { get(target, key, receiver) {
|
|
3655
|
+
if (typeof key !== "string" || key in target || key === "then" || !TAG_PATTERN.test(key)) return Reflect.get(target, key, receiver);
|
|
3656
|
+
return target(key);
|
|
3657
|
+
} });
|
|
3658
|
+
}
|
|
3659
|
+
// src/themeInput.ts
|
|
3660
|
+
/** The theme where there is no provider and no base theme: an object with nothing in it, never changed. */
|
|
3661
|
+
const EMPTY_THEME = Object.freeze({});
|
|
3662
|
+
/** The theme that a provider given `input` adds below `outer` (the theme above it, or `EMPTY_THEME`): the result of a function, or the object as it is. */
|
|
3663
|
+
function resolveThemeInput(input, outer) {
|
|
3664
|
+
return typeof input === "function" ? input(outer) : input;
|
|
3665
|
+
}
|
|
3666
|
+
/** The theme below a provider: `given` merged over the theme above it, or `given` as it is where there is none above and no base theme. */
|
|
3667
|
+
function mergeTheme(outer, given) {
|
|
3668
|
+
return outer === void 0 ? given : mergeDeep(outer, given);
|
|
3669
|
+
}
|
|
3670
|
+
export { DEFAULT_SCOPING, EMPTY_THEME, NO_THEME_MESSAGE, ROOT_SELECTOR, RULE_LAYERS, add, alpha, assertAlphaInput, at, compileRules, createCache, createComponentSelector, createCssEntry, createDeferredObject, createDeferredRecord, createDomSheet, createEngine, createGlobalMarker, createLazyEngine, createLazyRecord, createMemorySheet, createNoSchemeGuard, createSchemeKey, createScopeRule, createSheetEntry, createStyleSelector, createStyleTag, createThemeClass, createThemeClassName, createThemeId, createThemeKey, createThemeRule, createTokens, declareRootTheme, divide, extractPageStyleParts, extractPageStyles, findCache, findEntryCache, findMissingStyles, flattenStyle, getBoundaryClass, getSchemeClass, globalStyles, hashString, isAlphaColor, isDevelopment, isSheetEntry, isTemplateStrings, isThemedSheet, keyframes, mergeDeep, mergeStyles, mergeTheme, multiply, negate, parseKeyframesTemplate, parseStyleTemplate, readCondition, readCssStyle, readEntrySource, readEntryStyle, readLabel, readStaticTemplate, readStyleArguments, rejectFunctions, resolveThemeInput, subtract, toDeclarationText, toRgba, withTagShorthands };
|