@tbela99/css-parser 2.0.1 → 2.2.0
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/CHANGELOG.md +86 -1
- package/README.md +15 -7
- package/architecture.png +0 -0
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +4848 -3750
- package/dist/index-umd-web.js +18234 -9498
- package/dist/index.d.ts +1251 -373
- package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
- package/dist/{src/lib → lib}/ast/arena.js +1034 -655
- package/dist/{src/lib → lib}/ast/cache.js +36 -31
- package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
- package/dist/{src/lib → lib}/ast/features/if.js +24 -19
- package/dist/{src/lib → lib}/ast/features/index.js +3 -2
- package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
- package/dist/lib/ast/features/mixin.js +953 -0
- package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
- package/dist/lib/ast/features/shorthand.js +129 -0
- package/dist/lib/ast/features/transform.js +188 -0
- package/dist/{src/lib → lib}/ast/features/type.js +2 -2
- package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
- package/dist/{src/lib → lib}/ast/math/math.js +8 -8
- package/dist/{src/lib → lib}/ast/minify.js +932 -354
- package/dist/lib/ast/mixins/custom-function.js +393 -0
- package/dist/lib/ast/mixins/utils.js +53 -0
- package/dist/lib/ast/mixins/variables.js +678 -0
- package/dist/lib/ast/strings.js +906 -0
- package/dist/lib/ast/transform/compute.js +560 -0
- package/dist/lib/ast/transform/matrix.js +67 -0
- package/dist/lib/ast/transform/minify.js +325 -0
- package/dist/lib/ast/transform/perspective.js +24 -0
- package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
- package/dist/lib/ast/transform/scale.js +43 -0
- package/dist/lib/ast/transform/skew.js +38 -0
- package/dist/lib/ast/transform/translate.js +56 -0
- package/dist/lib/ast/transform/utils.js +390 -0
- package/dist/{src/lib → lib}/ast/types.js +7 -15
- package/dist/{src/lib → lib}/ast/walk.js +19 -19
- package/dist/{src/lib → lib}/fs/resolve.js +15 -15
- package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
- package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
- package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
- package/dist/lib/parser/input.js +85 -0
- package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
- package/dist/{src/lib → lib}/parser/parse.js +852 -619
- package/dist/lib/parser/source.js +71 -0
- package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
- package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
- package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
- package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
- package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
- package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
- package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
- package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
- package/dist/lib/parser/utils/cache.js +53 -0
- package/dist/lib/parser/utils/canonical.js +78 -0
- package/dist/lib/parser/utils/codes.js +93 -0
- package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
- package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
- package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +720 -0
- package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
- package/dist/{src/lib → lib}/printer/render.js +536 -243
- package/dist/lib/printer/result.js +148 -0
- package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
- package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
- package/dist/lib/printer/writer.js +252 -0
- package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
- package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
- package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
- package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
- package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
- package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
- package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
- package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
- package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
- package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
- package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
- package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
- package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
- package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
- package/dist/{src/lib → lib}/syntax/constants.js +30 -3
- package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
- package/dist/lib/validation/config.js +173 -0
- package/dist/lib/validation/first.js +571 -0
- package/dist/{src/lib → lib}/validation/match.js +1256 -579
- package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
- package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
- package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
- package/dist/{src/node.js → node.js} +92 -80
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +13 -8
- package/dist/version.d.ts +4 -0
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +80 -75
- package/package.json +13 -13
- package/dist/package.json.js +0 -10
- package/dist/src/lib/ast/features/shorthand.js +0 -87
- package/dist/src/lib/ast/features/transform.js +0 -85
- package/dist/src/lib/ast/transform/compute.js +0 -358
- package/dist/src/lib/ast/transform/matrix.js +0 -88
- package/dist/src/lib/ast/transform/minify.js +0 -267
- package/dist/src/lib/ast/transform/perspective.js +0 -16
- package/dist/src/lib/ast/transform/scale.js +0 -39
- package/dist/src/lib/ast/transform/skew.js +0 -30
- package/dist/src/lib/ast/transform/translate.js +0 -39
- package/dist/src/lib/ast/transform/utils.js +0 -287
- package/dist/src/lib/parser/source.js +0 -127
- package/dist/src/lib/parser/utils/cache.js +0 -54
- package/dist/src/lib/parser/utils/selector.js +0 -440
- package/dist/src/lib/validation/config.js +0 -120
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 2.2.0
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
8
|
+
import { EnumToken, EnumAstNodeStatus } from '../types.js';
|
|
9
|
+
import { renderValue } from '../../printer/render.js';
|
|
10
|
+
import { parseDeclarations } from '../../parser/parse.js';
|
|
11
|
+
import { parseSyntax } from '../../validation/parser/parse.js';
|
|
12
|
+
import { matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
|
|
13
|
+
import { SourceFile } from '../../parser/source.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* variables of a style rule: the custom properties declared by the rule
|
|
17
|
+
*/
|
|
18
|
+
class RuleScope {
|
|
19
|
+
properties;
|
|
20
|
+
functions;
|
|
21
|
+
dynamic;
|
|
22
|
+
/**
|
|
23
|
+
* @param properties custom properties of the rule
|
|
24
|
+
* @param functions
|
|
25
|
+
* @param dynamic the variables visible from the custom functions called from the rule, see
|
|
26
|
+
* {@link VariableScope.dynamic}
|
|
27
|
+
*/
|
|
28
|
+
constructor(properties, functions = null, dynamic = null) {
|
|
29
|
+
this.properties = properties;
|
|
30
|
+
this.functions = functions;
|
|
31
|
+
this.dynamic = dynamic;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* the arguments see the variables of the call site
|
|
35
|
+
*/
|
|
36
|
+
get arguments() {
|
|
37
|
+
return this.dynamic;
|
|
38
|
+
}
|
|
39
|
+
lookup(name) {
|
|
40
|
+
return this.properties.get(name) ?? null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* at-rules that may contain mixin and custom function definitions: the conditional group rules and the cascade layers
|
|
45
|
+
*/
|
|
46
|
+
const scopeRules = ["media", "supports", "container", "layer", "when", "else"];
|
|
47
|
+
/**
|
|
48
|
+
* source of the validation errors of the parameter types
|
|
49
|
+
*/
|
|
50
|
+
const validationSource = new SourceFile("", [], "");
|
|
51
|
+
/**
|
|
52
|
+
* parsed syntaxes of the parameter types
|
|
53
|
+
*/
|
|
54
|
+
const syntaxes = new Map();
|
|
55
|
+
/**
|
|
56
|
+
* render options of the tokens: the values are parsed again, the colors are not converted
|
|
57
|
+
*/
|
|
58
|
+
const renderOptions = { convertColor: false };
|
|
59
|
+
/**
|
|
60
|
+
* returned by {@link VariableScope.lookup} when the variable is a private variable that is not visible (or not
|
|
61
|
+
* defined):
|
|
62
|
+
* the fallback of var() is used, otherwise the value is not valid: it is left as is
|
|
63
|
+
*/
|
|
64
|
+
const UNDEFINED = Symbol("undefined");
|
|
65
|
+
/**
|
|
66
|
+
* true if the token is a whitespace or a comment
|
|
67
|
+
* @param arena
|
|
68
|
+
* @param token
|
|
69
|
+
*/
|
|
70
|
+
function isBlank(arena, token) {
|
|
71
|
+
const typ = arena.dataTypes[token];
|
|
72
|
+
return typ === EnumToken.WhitespaceTokenType || typ === EnumToken.CommentTokenType;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* true if the token is a comment
|
|
76
|
+
* @param arena
|
|
77
|
+
* @param token
|
|
78
|
+
*/
|
|
79
|
+
function isComment(arena, token) {
|
|
80
|
+
return arena.dataTypes[token] === EnumToken.CommentTokenType;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* remove the leading and trailing whitespace and comments
|
|
84
|
+
* @param arena
|
|
85
|
+
* @param tokens
|
|
86
|
+
*/
|
|
87
|
+
function trim(arena, tokens) {
|
|
88
|
+
let start = 0;
|
|
89
|
+
let end = tokens.length;
|
|
90
|
+
while (start < end && isBlank(arena, tokens[start])) {
|
|
91
|
+
start++;
|
|
92
|
+
}
|
|
93
|
+
while (end > start && isBlank(arena, tokens[end - 1])) {
|
|
94
|
+
end--;
|
|
95
|
+
}
|
|
96
|
+
return start === 0 && end === tokens.length ? tokens : tokens.slice(start, end);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* remove the curly braces that wrap a comma containing value: `{a, b}` -> `a, b`
|
|
100
|
+
* @param arena
|
|
101
|
+
* @param tokens trimmed tokens
|
|
102
|
+
*/
|
|
103
|
+
function unwrap(arena, tokens) {
|
|
104
|
+
// the curly braces of a declaration value: `--fn({a, b})`
|
|
105
|
+
if (tokens.length === 1 && arena.dataTypes[tokens[0]] === EnumToken.WrappedValuesTokenType) {
|
|
106
|
+
return trim(arena, arena.tokens(tokens[0]));
|
|
107
|
+
}
|
|
108
|
+
if (tokens.length < 2 ||
|
|
109
|
+
arena.dataTypes[tokens[0]] !== EnumToken.BlockStartTokenType ||
|
|
110
|
+
arena.dataTypes[tokens[tokens.length - 1]] !== EnumToken.BlockEndTokenType) {
|
|
111
|
+
return tokens;
|
|
112
|
+
}
|
|
113
|
+
let depth = 0;
|
|
114
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
115
|
+
if (arena.dataTypes[tokens[i]] === EnumToken.BlockStartTokenType) {
|
|
116
|
+
depth++;
|
|
117
|
+
}
|
|
118
|
+
else if (arena.dataTypes[tokens[i]] === EnumToken.BlockEndTokenType) {
|
|
119
|
+
depth--;
|
|
120
|
+
// the first brace is closed before the end: `{a} b {c}`
|
|
121
|
+
if (depth === 0 && i < tokens.length - 1) {
|
|
122
|
+
return tokens;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return trim(arena, tokens.slice(1, -1));
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* split a list of tokens on the commas that are not wrapped in curly braces
|
|
130
|
+
* @param arena
|
|
131
|
+
* @param tokens
|
|
132
|
+
*/
|
|
133
|
+
function splitArguments(arena, tokens) {
|
|
134
|
+
const result = [[]];
|
|
135
|
+
let depth = 0;
|
|
136
|
+
for (const token of tokens) {
|
|
137
|
+
const typ = arena.dataTypes[token];
|
|
138
|
+
// the comments are ignored
|
|
139
|
+
if (isComment(arena, token)) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (typ === EnumToken.BlockStartTokenType) {
|
|
143
|
+
depth++;
|
|
144
|
+
}
|
|
145
|
+
else if (typ === EnumToken.BlockEndTokenType && depth > 0) {
|
|
146
|
+
depth--;
|
|
147
|
+
}
|
|
148
|
+
else if (typ === EnumToken.CommaTokenType && depth === 0) {
|
|
149
|
+
result.push([]);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
result[result.length - 1].push(token);
|
|
153
|
+
}
|
|
154
|
+
return result.map((part) => trim(arena, part));
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* first token that is not a whitespace or a comment
|
|
158
|
+
* @param arena
|
|
159
|
+
* @param tokens
|
|
160
|
+
*/
|
|
161
|
+
function firstToken(arena, tokens) {
|
|
162
|
+
for (const token of tokens) {
|
|
163
|
+
if (!isBlank(arena, token)) {
|
|
164
|
+
return token;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* true if the token is a var() function
|
|
171
|
+
* @param arena
|
|
172
|
+
* @param token
|
|
173
|
+
*/
|
|
174
|
+
function isVar(arena, token) {
|
|
175
|
+
return (arena.dataTypes[token] === EnumToken.WildCardFunctionTokenType &&
|
|
176
|
+
arena.stringEqualsIgnoreCase(arena.stringValue[token], "var"));
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* true if a token or one of its descendants is a var() function, or a custom function call (`--fn()`)
|
|
180
|
+
* @param arena
|
|
181
|
+
* @param token
|
|
182
|
+
* @param functions the custom function calls are evaluated
|
|
183
|
+
*/
|
|
184
|
+
function hasReference(arena, token, functions = false) {
|
|
185
|
+
if (isVar(arena, token) || (functions && arena.dataTypes[token] === EnumToken.CustomFunctionTokenType)) {
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
if (arena.firstToken[token] === 0 && arena.firstChild[token] === 0) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
for (const child of arena.subTokens(token)) {
|
|
192
|
+
if (hasReference(arena, child, functions)) {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* render tokens
|
|
200
|
+
* @param arena
|
|
201
|
+
* @param tokens
|
|
202
|
+
*/
|
|
203
|
+
function render(arena, tokens) {
|
|
204
|
+
let result = "";
|
|
205
|
+
for (const token of tokens) {
|
|
206
|
+
result += renderValue(arena, token, renderOptions);
|
|
207
|
+
}
|
|
208
|
+
return result;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* render tokens, the var() functions that reference a private variable are replaced by a copy of the variable value,
|
|
212
|
+
* the custom function calls are replaced by their result when the scope evaluates them.
|
|
213
|
+
* null is returned when a variable is invalid and var() has no fallback: the value is not valid, it is left as is
|
|
214
|
+
* @param arena
|
|
215
|
+
* @param tokens
|
|
216
|
+
* @param scope
|
|
217
|
+
* @param descendant the tokens are used by a descendant of the element the mixin is applied to
|
|
218
|
+
*/
|
|
219
|
+
function substitute(arena, tokens, scope, descendant) {
|
|
220
|
+
const functions = scope.functions != null;
|
|
221
|
+
if (!tokens.some((token) => hasReference(arena, token, functions))) {
|
|
222
|
+
return render(arena, tokens);
|
|
223
|
+
}
|
|
224
|
+
// the copies of the tokens are modified: the var() functions are replaced by their value
|
|
225
|
+
const list = arena.createList(EnumToken.ListTokenType, tokens.map((token) => arena.clone(token, true)));
|
|
226
|
+
if (!replaceVariables(arena, list, scope, descendant)) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
return render(arena, arena.tokens(list));
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* replace the var() functions that reference a private variable with literal tokens holding the value
|
|
233
|
+
* @param arena
|
|
234
|
+
* @param parent
|
|
235
|
+
* @param scope
|
|
236
|
+
* @param descendant
|
|
237
|
+
* @returns false when the value is not valid
|
|
238
|
+
*/
|
|
239
|
+
function replaceVariables(arena, parent, scope, descendant) {
|
|
240
|
+
for (const token of arena.subTokens(parent)) {
|
|
241
|
+
if (isVar(arena, token)) {
|
|
242
|
+
const value = substituteVariable(arena, token, scope, descendant);
|
|
243
|
+
if (value === null) {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
if (value !== undefined) {
|
|
247
|
+
arena.replaceToken(token, arena.createToken(EnumToken.LiteralTokenType, value));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
else if (scope.functions != null && arena.dataTypes[token] === EnumToken.CustomFunctionTokenType) {
|
|
251
|
+
// custom function call: `--fn(1px)`
|
|
252
|
+
const value = scope.functions.call(arena, token, scope, descendant);
|
|
253
|
+
if (value === null) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
if (value !== undefined) {
|
|
257
|
+
arena.replaceToken(token, arena.createToken(EnumToken.LiteralTokenType, value));
|
|
258
|
+
}
|
|
259
|
+
else if (!replaceVariables(arena, token, scope, descendant)) {
|
|
260
|
+
// the call is not evaluated: its arguments may reference variables
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
else if ((arena.firstToken[token] !== 0 || arena.firstChild[token] !== 0) &&
|
|
265
|
+
!replaceVariables(arena, token, scope, descendant)) {
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* value of a var() function
|
|
273
|
+
* - a string: the value that replaces the var() function
|
|
274
|
+
* - undefined: the var() function is not modified, it does not reference a private variable
|
|
275
|
+
* - null: the variable is invalid and var() has no fallback
|
|
276
|
+
* @param arena
|
|
277
|
+
* @param token
|
|
278
|
+
* @param scope
|
|
279
|
+
* @param descendant
|
|
280
|
+
*/
|
|
281
|
+
function substituteVariable(arena, token, scope, descendant) {
|
|
282
|
+
const children = arena.tokens(token);
|
|
283
|
+
const nameToken = firstToken(arena, children);
|
|
284
|
+
if (nameToken == null || arena.dataTypes[nameToken] !== EnumToken.DashedIdenTokenType) {
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
const name = arena.resolveString(arena.stringValue[nameToken]);
|
|
288
|
+
const comma = children.findIndex((child) => arena.dataTypes[child] === EnumToken.CommaTokenType);
|
|
289
|
+
const fallback = comma === -1 ? null : trim(arena, children.slice(comma + 1));
|
|
290
|
+
const found = scope.lookup(name, descendant);
|
|
291
|
+
if (found === null) {
|
|
292
|
+
// not a private variable: the fallback may reference private variables `var(--color, var(--private))`
|
|
293
|
+
if (fallback != null &&
|
|
294
|
+
fallback.some((child) => hasReference(arena, child, scope.functions != null))) {
|
|
295
|
+
const value = substitute(arena, fallback, scope, descendant);
|
|
296
|
+
// an invalid fallback is equivalent to no fallback
|
|
297
|
+
return value === null ? `var(${name})` : `var(${name},${value})`;
|
|
298
|
+
}
|
|
299
|
+
return undefined;
|
|
300
|
+
}
|
|
301
|
+
if (found !== UNDEFINED) {
|
|
302
|
+
const value = resolve(arena, found, descendant);
|
|
303
|
+
if (value !== null) {
|
|
304
|
+
return value;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
// the variable is not defined or is invalid
|
|
308
|
+
return fallback == null ? null : substitute(arena, fallback, scope, descendant);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* resolved value of a variable. null when the value is invalid
|
|
312
|
+
* @param arena
|
|
313
|
+
* @param entry
|
|
314
|
+
* @param descendant the variable is used by a descendant of the element the mixin is applied to: the typed arguments
|
|
315
|
+
* it references are the values computed on the element, see {@link TypedArgument}
|
|
316
|
+
*/
|
|
317
|
+
function resolve(arena, entry, descendant) {
|
|
318
|
+
// the value is already resolved: the custom properties of the rule, the arguments
|
|
319
|
+
if (entry.tokens == null || entry.scope == null) {
|
|
320
|
+
return entry.text ?? null;
|
|
321
|
+
}
|
|
322
|
+
const key = descendant ? "descendantText" : "text";
|
|
323
|
+
if (entry[key] !== undefined) {
|
|
324
|
+
return entry[key];
|
|
325
|
+
}
|
|
326
|
+
// dependency cycle
|
|
327
|
+
if (entry.resolving) {
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
entry.resolving = true;
|
|
331
|
+
entry[key] = substitute(arena, entry.tokens, entry.scope, descendant);
|
|
332
|
+
entry.resolving = false;
|
|
333
|
+
return entry[key];
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* value tokens of a declaration, without `!important`
|
|
337
|
+
* @param arena
|
|
338
|
+
* @param declaration
|
|
339
|
+
*/
|
|
340
|
+
function declarationValue(arena, declaration) {
|
|
341
|
+
const tokens = trim(arena, arena.tokens(declaration));
|
|
342
|
+
if (tokens.length > 0 && arena.dataTypes[tokens[tokens.length - 1]] === EnumToken.ImportantTokenType) {
|
|
343
|
+
return trim(arena, tokens.slice(0, -1));
|
|
344
|
+
}
|
|
345
|
+
return tokens;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* true if the node is an at-rule with the given name
|
|
349
|
+
* @param arena
|
|
350
|
+
* @param node
|
|
351
|
+
* @param name
|
|
352
|
+
*/
|
|
353
|
+
function isAtRule(arena, node, name) {
|
|
354
|
+
return arena.dataTypes[node] === EnumToken.AtRuleNodeType && arena.stringEquals(arena.name[node], name);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* true if the node is an at-rule with a block that may contain mixin definitions, see {@link scopeRules}. the name of
|
|
358
|
+
* the at-rule is not created
|
|
359
|
+
* @param arena
|
|
360
|
+
* @param node
|
|
361
|
+
*/
|
|
362
|
+
function isScopeRule(arena, node) {
|
|
363
|
+
if (arena.dataTypes[node] !== EnumToken.AtRuleNodeType || !(arena.flags[node] & EnumAstNodeStatus.HasChildren)) {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
for (const name of scopeRules) {
|
|
367
|
+
if (arena.stringEquals(arena.name[node], name)) {
|
|
368
|
+
return true;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* true if the node is a custom property declaration
|
|
375
|
+
* @param arena
|
|
376
|
+
* @param node
|
|
377
|
+
*/
|
|
378
|
+
function isCustomProperty(arena, node) {
|
|
379
|
+
return arena.dataTypes[node] === EnumToken.DeclarationNodeType && arena.stringStartsWith(arena.name[node], "--");
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* custom properties declared by the children of a node
|
|
383
|
+
* - the `!important` declarations override the other declarations
|
|
384
|
+
* - the last declaration wins
|
|
385
|
+
* @param arena
|
|
386
|
+
* @param node
|
|
387
|
+
* @param result
|
|
388
|
+
*/
|
|
389
|
+
function collectProperties(arena, node, result) {
|
|
390
|
+
const important = new Set();
|
|
391
|
+
const properties = new Map();
|
|
392
|
+
for (let child = arena.firstChild[node]; child !== 0; child = arena.nextSibling[child]) {
|
|
393
|
+
if (!isCustomProperty(arena, child)) {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
const name = arena.resolveString(arena.name[child]);
|
|
397
|
+
const isImportant = (arena.flags[child] & EnumAstNodeStatus.ImportantDeclaration) !== 0;
|
|
398
|
+
if (important.has(name) && !isImportant) {
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
if (isImportant) {
|
|
402
|
+
important.add(name);
|
|
403
|
+
}
|
|
404
|
+
properties.set(name, { tokens: null, text: render(arena, declarationValue(arena, child)), scope: null });
|
|
405
|
+
}
|
|
406
|
+
for (const [name, entry] of properties) {
|
|
407
|
+
// the declarations of the inner nodes override the declarations of the outer nodes
|
|
408
|
+
if (!result.has(name)) {
|
|
409
|
+
result.set(name, entry);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* syntax of a parameter type: `<color>` -> `<color>`, `type(<length> | auto)` -> `<length> | auto`.
|
|
415
|
+
* null when the parameter is not typed or accepts any value
|
|
416
|
+
* @param arena
|
|
417
|
+
* @param tokens tokens between the parameter name and the default value
|
|
418
|
+
*/
|
|
419
|
+
function parameterSyntax(arena, tokens) {
|
|
420
|
+
const type = trim(arena, tokens.filter((token) => !isComment(arena, token)));
|
|
421
|
+
if (type.length === 0) {
|
|
422
|
+
return null;
|
|
423
|
+
}
|
|
424
|
+
let syntax;
|
|
425
|
+
if (type.length === 1 &&
|
|
426
|
+
arena.dataTypes[type[0]] === EnumToken.FunctionTokenType &&
|
|
427
|
+
arena.stringEqualsIgnoreCase(arena.stringValue[type[0]], "type")) {
|
|
428
|
+
syntax = render(arena, trim(arena, arena.tokens(type[0]))).trim();
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
syntax = render(arena, type).trim();
|
|
432
|
+
}
|
|
433
|
+
return syntax === "" || syntax === "*" ? null : syntax;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* the values that are resolved by the browser: the variables, the environment variables, the conditions and the math
|
|
437
|
+
* functions (their type depends on the values they compute)
|
|
438
|
+
*/
|
|
439
|
+
const unresolvedValue = new RegExp("(^|[^\\w-])(" +
|
|
440
|
+
[
|
|
441
|
+
"var", "env", "attr", "if",
|
|
442
|
+
"calc", "min", "max", "clamp", "round", "mod", "rem", "sin", "cos", "tan", "asin", "acos", "atan", "atan2",
|
|
443
|
+
"pow", "sqrt", "hypot", "log", "exp", "abs", "sign", "calc-size", "progress", "random",
|
|
444
|
+
].join("|") +
|
|
445
|
+
")\\(", "i");
|
|
446
|
+
/**
|
|
447
|
+
* true if a value matches the type of a parameter. a value that is resolved by the browser is not checked: a variable
|
|
448
|
+
* (`var()`, `env()`, ...) or a math function (`calc()`, `min()`, ...)
|
|
449
|
+
* @param arena
|
|
450
|
+
* @param syntax
|
|
451
|
+
* @param value
|
|
452
|
+
* @param options
|
|
453
|
+
*/
|
|
454
|
+
function matchesType(arena, syntax, value, options) {
|
|
455
|
+
if (syntax == null || unresolvedValue.test(value)) {
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
if (!syntaxes.has(syntax)) {
|
|
459
|
+
let parsed = null;
|
|
460
|
+
try {
|
|
461
|
+
parsed = parseSyntax(syntax);
|
|
462
|
+
}
|
|
463
|
+
catch (e) {
|
|
464
|
+
// the type is not supported: the values are not checked
|
|
465
|
+
}
|
|
466
|
+
syntaxes.set(syntax, parsed == null || parsed.length === 0 ? null : parsed);
|
|
467
|
+
}
|
|
468
|
+
const parsed = syntaxes.get(syntax);
|
|
469
|
+
if (parsed == null) {
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
// the value is parsed as a declaration value: the math functions are parsed into expressions
|
|
473
|
+
const declaration = parseDeclarations("--value:" + value, arena).nodes.find((node) => arena.dataTypes[node] === EnumToken.DeclarationNodeType);
|
|
474
|
+
if (declaration == null) {
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
const tokens = arena.tokens(declaration);
|
|
478
|
+
return matchAllSyntaxes(arena, parsed, createValidationContext(arena, tokens), {
|
|
479
|
+
...options,
|
|
480
|
+
source: validationSource,
|
|
481
|
+
}).success;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* custom properties of the rule the `@apply` rule belongs to: the declarations of the nearest style rule and of the
|
|
485
|
+
* at-rules between them. null when the `@apply` rule is not inside a style rule
|
|
486
|
+
* @param arena
|
|
487
|
+
* @param node
|
|
488
|
+
*/
|
|
489
|
+
function ruleProperties(arena, node) {
|
|
490
|
+
const result = new Map();
|
|
491
|
+
let parent = arena.parents[node];
|
|
492
|
+
while (parent !== 0) {
|
|
493
|
+
if (arena.dataTypes[parent] === EnumToken.AtRuleNodeType) {
|
|
494
|
+
collectProperties(arena, parent, result);
|
|
495
|
+
}
|
|
496
|
+
else if (arena.dataTypes[parent] === EnumToken.RuleNodeType) {
|
|
497
|
+
collectProperties(arena, parent, result);
|
|
498
|
+
return result;
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
return null;
|
|
502
|
+
}
|
|
503
|
+
parent = arena.parents[parent];
|
|
504
|
+
}
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* parse the parameters of a mixin or a custom function: `--name(--a, --b <color>: red)`. null when the parameters are
|
|
509
|
+
* not valid. the comments are ignored
|
|
510
|
+
* @param arena
|
|
511
|
+
* @param reference function token
|
|
512
|
+
*/
|
|
513
|
+
function parseParameters(arena, reference) {
|
|
514
|
+
const parameters = [];
|
|
515
|
+
const children = trim(arena, arena.tokens(reference));
|
|
516
|
+
if (children.every((token) => isBlank(arena, token))) {
|
|
517
|
+
return parameters;
|
|
518
|
+
}
|
|
519
|
+
const list = splitArguments(arena, children);
|
|
520
|
+
// trailing comma: `@mixin --name(--a, --b,)`
|
|
521
|
+
if (list.length > 1 && list[list.length - 1].length === 0) {
|
|
522
|
+
list.pop();
|
|
523
|
+
}
|
|
524
|
+
for (const parameter of list) {
|
|
525
|
+
if (parameter.length === 0 || arena.dataTypes[parameter[0]] !== EnumToken.DashedIdenTokenType) {
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
// <dashed-ident> <css-type>? [ : <default-value> ]?
|
|
529
|
+
const colon = parameter.findIndex((token) => arena.dataTypes[token] === EnumToken.ColonTokenType);
|
|
530
|
+
const name = arena.resolveString(arena.stringValue[parameter[0]]);
|
|
531
|
+
// the parameters are unique
|
|
532
|
+
if (parameters.some((parameter) => parameter.name === name)) {
|
|
533
|
+
return null;
|
|
534
|
+
}
|
|
535
|
+
parameters.push({
|
|
536
|
+
name,
|
|
537
|
+
syntax: parameterSyntax(arena, parameter.slice(1, colon === -1 ? parameter.length : colon)),
|
|
538
|
+
defaultValue: colon === -1 ? null : unwrap(arena, trim(arena, parameter.slice(colon + 1))),
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
return parameters;
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* arguments of a mixin or a custom function call: the comments and the whitespace are ignored, the curly braces are
|
|
545
|
+
* removed. `--name(/* comment *\/)` has no argument
|
|
546
|
+
* @param arena
|
|
547
|
+
* @param reference function token
|
|
548
|
+
*/
|
|
549
|
+
function parseArguments(arena, reference) {
|
|
550
|
+
const children = arena.tokens(reference);
|
|
551
|
+
return children.every((token) => isBlank(arena, token))
|
|
552
|
+
? []
|
|
553
|
+
: splitArguments(arena, children).map((arg) => unwrap(arena, arg));
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* the definitions visible from a reference (`@apply`, custom function call): a definition is visible in its parent
|
|
557
|
+
* (the stylesheet, a rule or an at-rule) and in the descendants of its parent. the definitions of the innermost scope
|
|
558
|
+
* that defines the name are returned: they hide the definitions of the outer scopes
|
|
559
|
+
* @param arena
|
|
560
|
+
* @param definitions the definitions of the name
|
|
561
|
+
* @param site the reference
|
|
562
|
+
*/
|
|
563
|
+
function resolveDefinitions(arena, definitions, site) {
|
|
564
|
+
if (definitions == null || definitions.length === 0) {
|
|
565
|
+
return [];
|
|
566
|
+
}
|
|
567
|
+
for (let scope = arena.parents[site]; scope !== 0; scope = arena.parents[scope]) {
|
|
568
|
+
const result = definitions.filter((definition) => arena.parents[definition.node] === scope);
|
|
569
|
+
if (result.length > 0) {
|
|
570
|
+
return result;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return [];
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* name of a mixin or a custom function definition: `@mixin --name(...)`, `@function --name(...)`
|
|
577
|
+
* @param arena
|
|
578
|
+
* @param node
|
|
579
|
+
*/
|
|
580
|
+
function definitionName(arena, node) {
|
|
581
|
+
const reference = firstToken(arena, arena.tokens(node));
|
|
582
|
+
return reference == null ? null : arena.resolveString(arena.stringValue[reference]);
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* collect the mixin or custom function definitions of a stylesheet, a conditional group rule or a cascade layer
|
|
586
|
+
* @param arena
|
|
587
|
+
* @param node
|
|
588
|
+
* @param name at-rule name: `mixin`, `function`
|
|
589
|
+
* @param parse parse a definition. null when the definition is not valid
|
|
590
|
+
* @param definitions valid definitions by name, in document order
|
|
591
|
+
* @param invalid the definitions that are not valid: they are ignored
|
|
592
|
+
*/
|
|
593
|
+
function collectDefinitions(arena, node, name, parse, definitions, invalid) {
|
|
594
|
+
for (let child = arena.firstChild[node]; child !== 0; child = arena.nextSibling[child]) {
|
|
595
|
+
if (isAtRule(arena, child, name)) {
|
|
596
|
+
const key = definitionName(arena, child);
|
|
597
|
+
const definition = key == null ? null : parse(arena, child);
|
|
598
|
+
if (definition == null) {
|
|
599
|
+
invalid.push(child);
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
if (!definitions.has(key)) {
|
|
603
|
+
definitions.set(key, []);
|
|
604
|
+
}
|
|
605
|
+
definitions.get(key).push(definition);
|
|
606
|
+
}
|
|
607
|
+
else if (isScopeRule(arena, child)) {
|
|
608
|
+
collectDefinitions(arena, child, name, parse, definitions, invalid);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return definitions;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* the mixin or custom function at-rules of a stylesheet, with the scope they are defined in
|
|
615
|
+
* @param arena
|
|
616
|
+
* @param node
|
|
617
|
+
* @param name at-rule name: `mixin`, `function`
|
|
618
|
+
* @param result
|
|
619
|
+
*/
|
|
620
|
+
function collectAtRules(arena, node, name, result = []) {
|
|
621
|
+
for (let child = arena.firstChild[node]; child !== 0; child = arena.nextSibling[child]) {
|
|
622
|
+
if (isAtRule(arena, child, name)) {
|
|
623
|
+
result.push(child);
|
|
624
|
+
}
|
|
625
|
+
else if (isScopeRule(arena, child)) {
|
|
626
|
+
collectAtRules(arena, child, name, result);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
return result;
|
|
630
|
+
}
|
|
631
|
+
/**
|
|
632
|
+
* finalize the mixin or custom function definitions once they are evaluated
|
|
633
|
+
* - the evaluated definitions are dropped and replaced with a comment, unless `keep` is set
|
|
634
|
+
* - the definitions that are kept are reduced to their last definition in the same scope
|
|
635
|
+
* - the definitions that are not valid are left as is
|
|
636
|
+
* @param arena
|
|
637
|
+
* @param ast
|
|
638
|
+
* @param name at-rule name: `mixin`, `function`
|
|
639
|
+
* @param kept the definitions that are kept
|
|
640
|
+
* @param keep keep the evaluated definitions
|
|
641
|
+
* @param invalid the definitions that are not valid
|
|
642
|
+
*/
|
|
643
|
+
function finalizeDefinitions(arena, ast, name, kept, keep, invalid) {
|
|
644
|
+
// the last definition in the same scope
|
|
645
|
+
const last = new Map();
|
|
646
|
+
for (const node of collectAtRules(arena, ast, name)) {
|
|
647
|
+
const key = definitionName(arena, node);
|
|
648
|
+
if (key == null || invalid.includes(node)) {
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
if (!kept.has(node)) {
|
|
652
|
+
if (!keep) {
|
|
653
|
+
// the comments of the prelude are removed: the comments are not nested
|
|
654
|
+
const comment = arena.createComment(EnumToken.CommentTokenType, `/* @${name} ${arena
|
|
655
|
+
.resolveString(arena.stringValue[node])
|
|
656
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
657
|
+
.replace(/\(\s+/g, "(")
|
|
658
|
+
.replace(/\s+([),])/g, "$1")
|
|
659
|
+
.trim()} */`);
|
|
660
|
+
arena.copyLocation(comment, node);
|
|
661
|
+
arena.replaceChild(node, comment);
|
|
662
|
+
}
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
const parent = arena.parents[node];
|
|
666
|
+
if (!last.has(parent)) {
|
|
667
|
+
last.set(parent, new Map());
|
|
668
|
+
}
|
|
669
|
+
const previous = last.get(parent).get(key);
|
|
670
|
+
// the definition is defined only once in the same scope: the last definition wins
|
|
671
|
+
if (previous !== undefined) {
|
|
672
|
+
arena.detach(previous);
|
|
673
|
+
}
|
|
674
|
+
last.get(parent).set(key, node);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
export { RuleScope, UNDEFINED, collectAtRules, collectDefinitions, collectProperties, declarationValue, definitionName, finalizeDefinitions, firstToken, hasReference, isAtRule, isBlank, isComment, isCustomProperty, isScopeRule, isVar, matchesType, parameterSyntax, parseArguments, parseParameters, render, replaceVariables, resolve, resolveDefinitions, ruleProperties, scopeRules, splitArguments, substitute, substituteVariable, trim, unwrap };
|