@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.
Files changed (128) hide show
  1. package/CHANGELOG.md +86 -1
  2. package/README.md +15 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/data → data}/properties.json.js +86 -2
  5. package/dist/{src/data → data}/validation.json.js +4848 -3750
  6. package/dist/index-umd-web.js +18234 -9498
  7. package/dist/index.d.ts +1251 -373
  8. package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
  9. package/dist/{src/lib → lib}/ast/arena.js +1034 -655
  10. package/dist/{src/lib → lib}/ast/cache.js +36 -31
  11. package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
  12. package/dist/{src/lib → lib}/ast/features/if.js +24 -19
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
  17. package/dist/lib/ast/features/shorthand.js +129 -0
  18. package/dist/lib/ast/features/transform.js +188 -0
  19. package/dist/{src/lib → lib}/ast/features/type.js +2 -2
  20. package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
  21. package/dist/{src/lib → lib}/ast/math/math.js +8 -8
  22. package/dist/{src/lib → lib}/ast/minify.js +932 -354
  23. package/dist/lib/ast/mixins/custom-function.js +393 -0
  24. package/dist/lib/ast/mixins/utils.js +53 -0
  25. package/dist/lib/ast/mixins/variables.js +678 -0
  26. package/dist/lib/ast/strings.js +906 -0
  27. package/dist/lib/ast/transform/compute.js +560 -0
  28. package/dist/lib/ast/transform/matrix.js +67 -0
  29. package/dist/lib/ast/transform/minify.js +325 -0
  30. package/dist/lib/ast/transform/perspective.js +24 -0
  31. package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
  32. package/dist/lib/ast/transform/scale.js +43 -0
  33. package/dist/lib/ast/transform/skew.js +38 -0
  34. package/dist/lib/ast/transform/translate.js +56 -0
  35. package/dist/lib/ast/transform/utils.js +390 -0
  36. package/dist/{src/lib → lib}/ast/types.js +7 -15
  37. package/dist/{src/lib → lib}/ast/walk.js +19 -19
  38. package/dist/{src/lib → lib}/fs/resolve.js +15 -15
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
  42. package/dist/lib/parser/input.js +85 -0
  43. package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
  44. package/dist/{src/lib → lib}/parser/parse.js +852 -619
  45. package/dist/lib/parser/source.js +71 -0
  46. package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
  49. package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
  50. package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
  55. package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
  56. package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
  57. package/dist/lib/parser/utils/cache.js +53 -0
  58. package/dist/lib/parser/utils/canonical.js +78 -0
  59. package/dist/lib/parser/utils/codes.js +93 -0
  60. package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
  61. package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
  64. package/dist/lib/parser/utils/selector.js +720 -0
  65. package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
  66. package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
  67. package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
  68. package/dist/{src/lib → lib}/printer/render.js +536 -243
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
  77. package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
  94. package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
  95. package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
  99. package/dist/{src/lib → lib}/syntax/constants.js +30 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
  101. package/dist/lib/validation/config.js +173 -0
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +1256 -579
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +92 -80
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +13 -8
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +80 -75
  113. package/package.json +13 -13
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -87
  116. package/dist/src/lib/ast/features/transform.js +0 -85
  117. package/dist/src/lib/ast/transform/compute.js +0 -358
  118. package/dist/src/lib/ast/transform/matrix.js +0 -88
  119. package/dist/src/lib/ast/transform/minify.js +0 -267
  120. package/dist/src/lib/ast/transform/perspective.js +0 -16
  121. package/dist/src/lib/ast/transform/scale.js +0 -39
  122. package/dist/src/lib/ast/transform/skew.js +0 -30
  123. package/dist/src/lib/ast/transform/translate.js +0 -39
  124. package/dist/src/lib/ast/transform/utils.js +0 -287
  125. package/dist/src/lib/parser/source.js +0 -127
  126. package/dist/src/lib/parser/utils/cache.js +0 -54
  127. package/dist/src/lib/parser/utils/selector.js +0 -440
  128. package/dist/src/lib/validation/config.js +0 -120
@@ -0,0 +1,393 @@
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 { parseDeclarations } from '../../parser/parse.js';
10
+ import { resolveDefinitions, parseArguments, substitute, resolve, matchesType, isAtRule, UNDEFINED, firstToken, parseParameters, parameterSyntax, isCustomProperty, declarationValue, hasReference, RuleScope, ruleProperties } from './variables.js';
11
+
12
+ /**
13
+ * maximum number of nested custom function calls
14
+ */
15
+ const MAX_DEPTH = 32;
16
+ /**
17
+ * variables visible from the body of a custom function
18
+ */
19
+ class FunctionScope {
20
+ locals;
21
+ args;
22
+ parent;
23
+ functions;
24
+ /**
25
+ * @param locals local variables
26
+ * @param args arguments and default values
27
+ * @param parent scope of the call: the calling function, or the element
28
+ * @param functions
29
+ */
30
+ constructor(locals, args, parent, functions) {
31
+ this.locals = locals;
32
+ this.args = args;
33
+ this.parent = parent;
34
+ this.functions = functions;
35
+ }
36
+ lookup(name, descendant) {
37
+ return this.locals.get(name) ?? this.args.get(name) ?? this.parent.lookup(name, descendant);
38
+ }
39
+ }
40
+ /**
41
+ * parse a custom function definition. null if the prelude is not valid
42
+ *
43
+ * ```css
44
+ * @function --name(--a <length>: 1px) returns <length> { result: calc(2 * var(--a)) }
45
+ * ```
46
+ * @param arena
47
+ * @param node
48
+ */
49
+ function parseFunction(arena, node) {
50
+ const tokens = arena.tokens(node);
51
+ const reference = firstToken(arena, tokens);
52
+ if (reference == null || arena.dataTypes[reference] !== EnumToken.CustomFunctionTokenType) {
53
+ return null;
54
+ }
55
+ const parameters = parseParameters(arena, reference);
56
+ if (parameters == null) {
57
+ return null;
58
+ }
59
+ // [ returns <css-type> ]?
60
+ let returns = null;
61
+ const index = tokens.findIndex((token) => arena.dataTypes[token] === EnumToken.IdenTokenType &&
62
+ arena.stringEqualsIgnoreCase(arena.stringValue[token], "returns"));
63
+ if (index !== -1) {
64
+ returns = parameterSyntax(arena, tokens.slice(index + 1));
65
+ }
66
+ const definition = {
67
+ node,
68
+ name: arena.resolveString(arena.stringValue[reference]),
69
+ parameters,
70
+ returns,
71
+ conditionalBody: false,
72
+ locals: new Map(),
73
+ result: null,
74
+ };
75
+ for (let child = arena.firstChild[node]; child !== 0; child = arena.nextSibling[child]) {
76
+ if (arena.dataTypes[child] === EnumToken.AtRuleNodeType) {
77
+ definition.conditionalBody = true;
78
+ }
79
+ else if (isCustomProperty(arena, child)) {
80
+ definition.locals.set(arena.resolveString(arena.name[child]), declarationValue(arena, child));
81
+ }
82
+ else if (arena.dataTypes[child] === EnumToken.DeclarationNodeType &&
83
+ arena.stringEquals(arena.name[child], "result")) {
84
+ definition.result = declarationValue(arena, child);
85
+ }
86
+ }
87
+ return definition;
88
+ }
89
+ /**
90
+ * local variable of a custom function. the css-wide keywords of the local variables:
91
+ * - `initial`: the value of the parameter with the same name (the argument or the default value), otherwise the value
92
+ * is not valid
93
+ * - `inherit`, `unset`: the value of the variable with the same name in the calling context (the calling function or
94
+ * the custom properties of the call site). otherwise the var() function is evaluated by the browser
95
+ * @param arena
96
+ * @param name
97
+ * @param tokens
98
+ * @param values arguments and default values
99
+ * @param context calling context
100
+ * @param scope scope of the function body
101
+ * @param descendant
102
+ */
103
+ function localVariable(arena, name, tokens, values, context, scope, descendant) {
104
+ if (tokens.length === 1 && arena.dataTypes[tokens[0]] === EnumToken.IdenTokenType) {
105
+ if (arena.stringEqualsIgnoreCase(arena.stringValue[tokens[0]], "initial")) {
106
+ return values.get(name) ?? { tokens: null, text: null, scope: null };
107
+ }
108
+ if (arena.stringEqualsIgnoreCase(arena.stringValue[tokens[0]], "inherit") ||
109
+ arena.stringEqualsIgnoreCase(arena.stringValue[tokens[0]], "unset")) {
110
+ const found = context.lookup(name, descendant);
111
+ if (found === UNDEFINED) {
112
+ return { tokens: null, text: null, scope: null };
113
+ }
114
+ return found ?? { tokens: null, text: `var(${name})`, scope: null };
115
+ }
116
+ }
117
+ return { tokens, scope };
118
+ }
119
+ /**
120
+ * evaluate the custom function calls
121
+ */
122
+ class FunctionRegistry {
123
+ definitions;
124
+ options;
125
+ /**
126
+ * the function definitions that have been evaluated
127
+ */
128
+ evaluated = new Set();
129
+ /**
130
+ * the function definitions referenced by the calls that have not been evaluated: they are kept
131
+ */
132
+ pending = new Set();
133
+ /**
134
+ * number of evaluated calls
135
+ */
136
+ count = 0;
137
+ /**
138
+ * number of calls that are not evaluated: the calls of the functions that are defined but cannot be evaluated.
139
+ * the `@apply` rules, the functions and the declarations that contain them are not evaluated
140
+ */
141
+ failed = 0;
142
+ /**
143
+ * the calls are located in a custom property declaration
144
+ */
145
+ customProperty = false;
146
+ /**
147
+ * location of the calls: the declaration or the `@apply` rule that contains them. the functions visible from it
148
+ * are evaluated, see {@link resolveDefinitions}
149
+ */
150
+ site = 0;
151
+ /**
152
+ * functions being evaluated
153
+ */
154
+ stack = [];
155
+ /**
156
+ * the functions that call themselves, directly or through other functions
157
+ */
158
+ recursive = new Map();
159
+ /**
160
+ * @param definitions function definitions by name, in document order. the definitions that are not valid are null
161
+ * @param options
162
+ */
163
+ constructor(definitions, options) {
164
+ this.definitions = definitions;
165
+ this.options = options;
166
+ }
167
+ call(arena, token, caller, descendant) {
168
+ const name = arena.resolveString(arena.stringValue[token]);
169
+ const all = this.definitions.get(name);
170
+ // the function is not defined in the stylesheet
171
+ if (all == null) {
172
+ return undefined;
173
+ }
174
+ // the functions visible from the call: a function defined in a rule or an at-rule is not visible outside of it.
175
+ // the function is defined only once in the innermost scope that defines it
176
+ const definitions = resolveDefinitions(arena, all, this.site);
177
+ const definition = definitions.length === 1 ? definitions[0] : null;
178
+ if (definition == null ||
179
+ definition.conditionalBody ||
180
+ this.stack.length >= MAX_DEPTH ||
181
+ this.isRecursive(arena, name) ||
182
+ (this.customProperty &&
183
+ (definition.returns != null ||
184
+ definition.parameters.some((parameter) => parameter.syntax != null)))) {
185
+ // the visible definitions are kept
186
+ for (const visible of definitions) {
187
+ this.pending.add(visible.node);
188
+ }
189
+ this.failed++;
190
+ return undefined;
191
+ }
192
+ const failed = this.failed;
193
+ let result = this.evaluate(arena, definition, token, caller, descendant);
194
+ // a call of the function body is not evaluated: the function is not evaluated
195
+ if (result !== undefined && this.failed !== failed) {
196
+ result = undefined;
197
+ }
198
+ if (result === undefined) {
199
+ this.pending.add(definition.node);
200
+ this.failed++;
201
+ }
202
+ else {
203
+ this.evaluated.add(definition.node);
204
+ this.count++;
205
+ }
206
+ return result;
207
+ }
208
+ /**
209
+ * evaluate a call
210
+ * @param arena
211
+ * @param definition
212
+ * @param token call token
213
+ * @param caller
214
+ * @param descendant
215
+ * @returns the result, undefined when the arguments do not match the parameters or the result is not valid
216
+ */
217
+ evaluate(arena, definition, token, caller, descendant) {
218
+ const args = parseArguments(arena, token);
219
+ if (args.length > definition.parameters.length) {
220
+ return undefined;
221
+ }
222
+ const values = new Map();
223
+ const locals = new Map();
224
+ // the variables of the call site: the custom properties of the rule, or the variables of the calling function
225
+ const context = caller.dynamic ?? caller;
226
+ const argumentScope = caller.arguments ?? caller;
227
+ const scope = new FunctionScope(locals, values, context, this);
228
+ for (let index = 0; index < definition.parameters.length; index++) {
229
+ const parameter = definition.parameters[index];
230
+ if (index < args.length) {
231
+ // the arguments are evaluated in the scope of the call
232
+ const text = substitute(arena, args[index], argumentScope, descendant);
233
+ if (text === null) {
234
+ return undefined;
235
+ }
236
+ values.set(parameter.name, { tokens: null, text, scope: null });
237
+ }
238
+ else if (parameter.defaultValue != null) {
239
+ values.set(parameter.name, { tokens: parameter.defaultValue, scope });
240
+ }
241
+ else {
242
+ return undefined;
243
+ }
244
+ }
245
+ for (const [name, tokens] of definition.locals) {
246
+ locals.set(name, localVariable(arena, name, tokens, values, context, scope, descendant));
247
+ }
248
+ this.stack.push(definition.name);
249
+ try {
250
+ for (const parameter of definition.parameters) {
251
+ const value = resolve(arena, values.get(parameter.name), descendant);
252
+ if (value === null || !matchesType(arena, parameter.syntax, value, this.options)) {
253
+ return undefined;
254
+ }
255
+ }
256
+ // no result: guaranteed-invalid value. the call is left as is
257
+ if (definition.result == null) {
258
+ return undefined;
259
+ }
260
+ const result = substitute(arena, definition.result, scope, descendant);
261
+ // an invalid result or a result that does not match the return type: the call is left as is
262
+ if (result === null || !matchesType(arena, definition.returns, result, this.options)) {
263
+ return undefined;
264
+ }
265
+ return result;
266
+ }
267
+ finally {
268
+ this.stack.pop();
269
+ }
270
+ }
271
+ /**
272
+ * true if a function calls itself, directly or through the functions it calls: it cannot be evaluated
273
+ * @param arena
274
+ * @param name
275
+ * @param stack functions being checked
276
+ */
277
+ isRecursive(arena, name, stack = []) {
278
+ if (stack.includes(name)) {
279
+ return true;
280
+ }
281
+ if (this.recursive.has(name)) {
282
+ return this.recursive.get(name);
283
+ }
284
+ let result = false;
285
+ stack.push(name);
286
+ for (const definition of this.definitions.get(name) ?? []) {
287
+ const values = [...definition.locals.values()];
288
+ if (definition.result != null) {
289
+ values.push(definition.result);
290
+ }
291
+ for (const parameter of definition.parameters) {
292
+ if (parameter.defaultValue != null) {
293
+ values.push(parameter.defaultValue);
294
+ }
295
+ }
296
+ for (const tokens of values) {
297
+ for (const call of findCalls(arena, tokens)) {
298
+ if (this.definitions.has(arena.resolveString(arena.stringValue[call])) &&
299
+ this.isRecursive(arena, arena.resolveString(arena.stringValue[call]), stack)) {
300
+ result = true;
301
+ break;
302
+ }
303
+ }
304
+ if (result) {
305
+ break;
306
+ }
307
+ }
308
+ if (result) {
309
+ break;
310
+ }
311
+ }
312
+ stack.pop();
313
+ // a function that reaches one of the functions being checked is part of a cycle
314
+ this.recursive.set(name, result);
315
+ return result;
316
+ }
317
+ }
318
+ /**
319
+ * the custom function calls of a list of tokens and their descendants
320
+ * @param arena
321
+ * @param tokens
322
+ * @param result
323
+ */
324
+ function findCalls(arena, tokens, result = []) {
325
+ for (const token of tokens) {
326
+ if (arena.dataTypes[token] === EnumToken.CustomFunctionTokenType) {
327
+ result.push(token);
328
+ }
329
+ if (arena.firstToken[token] !== 0 || arena.firstChild[token] !== 0) {
330
+ findCalls(arena, arena.subTokens(token), result);
331
+ }
332
+ }
333
+ return result;
334
+ }
335
+ /**
336
+ * replace the custom function calls of the declarations of a stylesheet with their result. the bodies of the mixins
337
+ * and of the custom functions are not modified: they are evaluated when they are used
338
+ * @param arena
339
+ * @param ast
340
+ * @param registry
341
+ */
342
+ function evaluateFunctions(arena, ast, registry) {
343
+ const stack = [];
344
+ for (let child = arena.lastChild[ast]; child !== 0; child = arena.previousSibling[child]) {
345
+ stack.push(child);
346
+ }
347
+ while (stack.length > 0) {
348
+ const node = stack.pop();
349
+ if (isAtRule(arena, node, "mixin") || isAtRule(arena, node, "function")) {
350
+ continue;
351
+ }
352
+ if (arena.dataTypes[node] === EnumToken.DeclarationNodeType) {
353
+ evaluateDeclaration(arena, node, registry);
354
+ continue;
355
+ }
356
+ if (arena.flags[node] & EnumAstNodeStatus.HasChildren) {
357
+ for (let child = arena.lastChild[node]; child !== 0; child = arena.previousSibling[child]) {
358
+ stack.push(child);
359
+ }
360
+ }
361
+ }
362
+ }
363
+ /**
364
+ * replace the custom function calls of a declaration with their result
365
+ * @param arena
366
+ * @param declaration
367
+ * @param registry
368
+ */
369
+ function evaluateDeclaration(arena, declaration, registry) {
370
+ const tokens = arena.tokens(declaration);
371
+ if (!tokens.some((token) => hasReference(arena, token, true))) {
372
+ return;
373
+ }
374
+ // the var() functions of the declaration are not modified. the custom functions see the custom properties of
375
+ // the rule: the call site
376
+ const scope = new RuleScope(new Map(), registry, new RuleScope(ruleProperties(arena, declaration) ?? new Map(), registry));
377
+ const count = registry.count;
378
+ const failed = registry.failed;
379
+ registry.customProperty = isCustomProperty(arena, declaration);
380
+ registry.site = declaration;
381
+ const value = substitute(arena, tokens, scope, false);
382
+ // no call has been evaluated, a call is not evaluated or the value is not valid: the declaration is left as is
383
+ if (registry.count === count || registry.failed !== failed || value === null) {
384
+ return;
385
+ }
386
+ const nodes = parseDeclarations(arena.resolveString(arena.name[declaration]) + ":" + value, arena).nodes.filter((node) => arena.dataTypes[node] === EnumToken.DeclarationNodeType);
387
+ if (nodes.length === 1) {
388
+ arena.copyLocation(nodes[0], declaration);
389
+ arena.replaceChild(declaration, nodes[0]);
390
+ }
391
+ }
392
+
393
+ export { FunctionRegistry, evaluateFunctions, findCalls, parseFunction };
@@ -0,0 +1,53 @@
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 } from '../types.js';
9
+
10
+ /**
11
+ * true if the node is a `@mixin` or a `@function` rule, or is located inside one of them.
12
+ * the body of a mixin or a custom function is a template: it is evaluated when the mixin is applied or the function is
13
+ * called, its variables are private variables
14
+ * @param arena
15
+ * @param node
16
+ */
17
+ function isInTemplate(arena, node) {
18
+ while (node !== 0) {
19
+ if (arena.dataTypes[node] === EnumToken.AtRuleNodeType &&
20
+ (arena.stringEquals(arena.name[node], "mixin") || arena.stringEquals(arena.name[node], "function"))) {
21
+ return true;
22
+ }
23
+ node = arena.parents[node];
24
+ }
25
+ return false;
26
+ }
27
+ /**
28
+ * move the last `result` descriptor of a custom function after its conditional rules: there is no early return.
29
+ * `result: 16px; @media (...) { result: 20px }` -> `@media (...) { result: 20px } result: 16px`.
30
+ * the conditional rules are never removed
31
+ * @param arena
32
+ * @param node `@function` rule
33
+ */
34
+ function moveResult(arena, node) {
35
+ let last = arena.lastChild[node];
36
+ // the last result
37
+ while (last !== 0 &&
38
+ !(arena.dataTypes[last] === EnumToken.DeclarationNodeType && arena.stringEquals(arena.name[last], "result"))) {
39
+ last = arena.previousSibling[last];
40
+ }
41
+ if (last === 0) {
42
+ return;
43
+ }
44
+ for (let next = arena.nextSibling[last]; next !== 0; next = arena.nextSibling[next]) {
45
+ if (arena.dataTypes[next] === EnumToken.AtRuleNodeType) {
46
+ arena.detach(last);
47
+ arena.appendChild(node, last);
48
+ return;
49
+ }
50
+ }
51
+ }
52
+
53
+ export { isInTemplate, moveResult };