@tbela99/css-parser 2.1.0 → 2.3.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 +66 -1
- package/README.md +14 -7
- package/architecture.png +0 -0
- package/dist/{src/lib/ast → ast}/arena-node.js +10 -10
- package/dist/{src/lib/ast → ast}/arena.js +304 -139
- package/dist/{src/lib/ast → ast}/cache.js +40 -24
- package/dist/{src/lib/ast → ast}/features/calc.js +11 -9
- package/dist/{src/lib/ast → ast}/features/if.js +6 -6
- package/dist/ast/features/index.d.ts +7 -0
- package/dist/{src/lib/ast → ast}/features/index.js +3 -2
- package/dist/{src/lib/ast → ast}/features/inlinecssvariables.js +12 -10
- package/dist/ast/features/mixin.js +953 -0
- package/dist/{src/lib/ast → ast}/features/prefix.js +39 -39
- package/dist/ast/features/shorthand.js +129 -0
- package/dist/ast/features/transform.js +188 -0
- package/dist/{src/lib/ast → ast}/features/type.js +2 -2
- package/dist/{src/lib/ast → ast}/math/expression.js +24 -24
- package/dist/{src/lib/ast → ast}/math/math.js +2 -2
- package/dist/{src/lib/ast → ast}/minify.js +745 -201
- package/dist/ast/mixins/custom-function.js +393 -0
- package/dist/ast/mixins/utils.js +53 -0
- package/dist/ast/mixins/variables.js +678 -0
- package/dist/ast/strings.js +910 -0
- package/dist/ast/transform/compute.js +560 -0
- package/dist/ast/transform/matrix.js +67 -0
- package/dist/ast/transform/minify.js +325 -0
- package/dist/ast/transform/perspective.js +24 -0
- package/dist/{src/lib/ast → ast}/transform/rotate.js +16 -7
- package/dist/ast/transform/scale.js +43 -0
- package/dist/ast/transform/skew.js +38 -0
- package/dist/ast/transform/translate.js +56 -0
- package/dist/ast/transform/utils.js +390 -0
- package/dist/{src/lib/ast → ast}/types.js +2 -2
- package/dist/{src/lib/ast → ast}/walk.js +2 -2
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +30 -21
- package/dist/{src/lib/fs → fs}/resolve.js +2 -2
- package/dist/index-umd-web.js +19727 -10719
- package/dist/index.d.ts +738 -100
- package/dist/{src/node.js → node.js} +70 -52
- package/dist/{src/lib/parser → parser}/declaration/list.js +25 -8
- package/dist/{src/lib/parser → parser}/declaration/map.js +15 -15
- package/dist/{src/lib/parser → parser}/declaration/set.js +5 -5
- package/dist/{src/lib/parser → parser}/input.js +18 -3
- package/dist/{src/lib/parser → parser}/linesmap.js +2 -2
- package/dist/{src/lib/parser → parser}/parse.js +444 -278
- package/dist/{src/lib/parser → parser}/source.js +3 -3
- package/dist/{src/lib/parser → parser}/tokenize.js +480 -312
- package/dist/{src/lib/parser → parser}/utils/angle.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/at-rule-container.js +12 -12
- package/dist/{src/lib/parser → parser}/utils/at-rule-font-feature-values.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/at-rule-generic.js +7 -7
- package/dist/{src/lib/parser → parser}/utils/at-rule-import.js +10 -10
- package/dist/{src/lib/parser → parser}/utils/at-rule-media.js +14 -14
- package/dist/{src/lib/parser → parser}/utils/at-rule-support.js +9 -14
- package/dist/{src/lib/parser → parser}/utils/at-rule-when-else.js +6 -6
- package/dist/{src/lib/parser → parser}/utils/at-rule.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/base64.js +2 -2
- package/dist/parser/utils/cache.js +53 -0
- package/dist/parser/utils/canonical.js +78 -0
- package/dist/parser/utils/codes.js +93 -0
- package/dist/{src/lib/parser → parser}/utils/config.js +3 -3
- package/dist/{src/lib/parser → parser}/utils/declaration.js +31 -26
- package/dist/{src/lib/parser → parser}/utils/eq.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/hash.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/selector.js +339 -87
- package/dist/{src/lib/parser → parser}/utils/text.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/token.js +2 -2
- package/dist/{src/lib/parser → parser}/utils/type.js +9 -7
- package/dist/{src/lib/printer/sourcemap → printer}/lib/codec.js +2 -2
- package/dist/{src/lib/printer → printer}/render.js +582 -153
- package/dist/printer/result.js +148 -0
- package/dist/{src/lib/printer/sourcemap → printer}/sourcemap.js +4 -3
- package/dist/printer/writer.js +252 -0
- package/dist/{src/lib/syntax → syntax}/color/a98rgb.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/alpha.js +10 -10
- package/dist/{src/lib/syntax → syntax}/color/cmyk.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/color-mix.js +18 -18
- package/dist/{src/lib/syntax → syntax}/color/color.js +18 -18
- package/dist/{src/lib/syntax → syntax}/color/hex.js +3 -3
- package/dist/{src/lib/syntax → syntax}/color/hsl.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/hwb.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/lab.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/lch.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/oklab.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/oklch.js +3 -3
- package/dist/{src/lib/syntax → syntax}/color/p3.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/prophotorgb.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/rec2020.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/relative-color.js +14 -14
- package/dist/{src/lib/syntax → syntax}/color/rgb.js +4 -4
- package/dist/{src/lib/syntax → syntax}/color/srgb.js +5 -5
- package/dist/{src/lib/syntax → syntax}/color/utils/color-type.js +4 -4
- package/dist/{src/lib/syntax → syntax}/color/utils/colorspace.js +10 -10
- package/dist/{src/lib/syntax → syntax}/color/utils/components.js +5 -5
- package/dist/{src/lib/syntax → syntax}/color/utils/distance.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/utils/make-color.js +3 -3
- package/dist/{src/lib/syntax → syntax}/color/utils/matrix.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/xyz.js +2 -2
- package/dist/{src/lib/syntax → syntax}/color/xyzd50.js +2 -2
- package/dist/{src/lib/syntax → syntax}/constants.js +27 -4
- package/dist/{src/lib/syntax → syntax}/syntax.js +37 -29
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +12 -7
- package/dist/validation/compiled/builtins.js +32 -0
- package/dist/validation/compiled/runtime.js +705 -0
- package/dist/validation/compiled/syntaxes.js +1620 -0
- package/dist/{src/lib/validation → validation}/config.js +63 -16
- package/dist/validation/first.js +574 -0
- package/dist/{src/lib/validation → validation}/match.js +376 -161
- package/dist/{src/lib/validation → validation}/parser/parse.js +2 -2
- package/dist/{src/lib/validation → validation}/parser/typedef.js +2 -2
- package/dist/{src/lib/validation → validation}/utils/list.js +3 -19
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +68 -55
- package/package.json +15 -13
- package/dist/package.json.js +0 -10
- package/dist/src/lib/ast/features/shorthand.js +0 -95
- package/dist/src/lib/ast/features/transform.js +0 -90
- package/dist/src/lib/ast/transform/compute.js +0 -362
- 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/utils/cache.js +0 -54
- package/dist/types.d.ts +0 -21
- package/dist/utils/sync.d.ts +0 -16
|
@@ -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.3.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.3.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 };
|