@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
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
/**
|
|
@@ -40,7 +40,7 @@ const bits = new Int32Array(f64.buffer);
|
|
|
40
40
|
* @param value
|
|
41
41
|
*/
|
|
42
42
|
function normalize(value) {
|
|
43
|
-
return value !== value ? NaN : value
|
|
43
|
+
return value !== value ? NaN : value;
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* Memoize values by token structure. Lookups return a slot, the value of a slot is `undefined` until it is set.
|
|
@@ -73,6 +73,14 @@ class StructuralMemo {
|
|
|
73
73
|
* @internal
|
|
74
74
|
*/
|
|
75
75
|
values = [];
|
|
76
|
+
/**
|
|
77
|
+
* number of distinct structures (lookups that inserted a new entry)
|
|
78
|
+
*/
|
|
79
|
+
size = 0;
|
|
80
|
+
/**
|
|
81
|
+
* number of lookups that found an existing entry
|
|
82
|
+
*/
|
|
83
|
+
hits = 0;
|
|
76
84
|
/**
|
|
77
85
|
* key hash, key offset in the pool and key length of each slot (3 values per slot)
|
|
78
86
|
*/
|
|
@@ -81,6 +89,10 @@ class StructuralMemo {
|
|
|
81
89
|
* serialized keys. the key being looked up is serialized after the last key: it is kept when it is inserted
|
|
82
90
|
*/
|
|
83
91
|
pool = new Int32Array(1024);
|
|
92
|
+
/**
|
|
93
|
+
* length of the pool: the length of a typed array is a getter
|
|
94
|
+
*/
|
|
95
|
+
poolLength = 1024;
|
|
84
96
|
/**
|
|
85
97
|
* used length of the pool
|
|
86
98
|
*/
|
|
@@ -93,14 +105,6 @@ class StructuralMemo {
|
|
|
93
105
|
* end of the key being looked up in the pool
|
|
94
106
|
*/
|
|
95
107
|
used = 0;
|
|
96
|
-
/**
|
|
97
|
-
* number of distinct structures (lookups that inserted a new entry)
|
|
98
|
-
*/
|
|
99
|
-
size = 0;
|
|
100
|
-
/**
|
|
101
|
-
* number of lookups that found an existing entry
|
|
102
|
-
*/
|
|
103
|
-
hits = 0;
|
|
104
108
|
/**
|
|
105
109
|
* @param numericEquality compare the numeric values with `==` semantics (`0` and `-0` are equal) and consider
|
|
106
110
|
* all the `NaN` values equal. the raw bits are compared otherwise
|
|
@@ -122,11 +126,11 @@ class StructuralMemo {
|
|
|
122
126
|
*/
|
|
123
127
|
list(arena, first, prefix = 0) {
|
|
124
128
|
let used = this.poolSize;
|
|
125
|
-
if (used + 1 > this.
|
|
129
|
+
if (used + 1 > this.poolLength) {
|
|
126
130
|
this.growPool(used + 1);
|
|
127
131
|
}
|
|
128
132
|
this.pool[used++] = prefix;
|
|
129
|
-
for (let token = first; token
|
|
133
|
+
for (let token = first; token !== 0; token = arena.nextSibling[token]) {
|
|
130
134
|
used = this.serialize(arena, token, used);
|
|
131
135
|
}
|
|
132
136
|
this.used = used;
|
|
@@ -177,7 +181,7 @@ class StructuralMemo {
|
|
|
177
181
|
*/
|
|
178
182
|
serialize(arena, token, used) {
|
|
179
183
|
// header, 5 fields, end marker
|
|
180
|
-
let buffer = used + 7 > this.
|
|
184
|
+
let buffer = used + 7 > this.poolLength ? this.growPool(used + 7) : this.pool;
|
|
181
185
|
const name = arena.name[token];
|
|
182
186
|
const str = arena.stringValue[token];
|
|
183
187
|
const unit = arena.unit[token];
|
|
@@ -187,15 +191,15 @@ class StructuralMemo {
|
|
|
187
191
|
// type and color type are 8 bits values
|
|
188
192
|
let header = arena.dataTypes[token] | (arena.colorType[token] << 8);
|
|
189
193
|
const index = used++;
|
|
190
|
-
if (name
|
|
194
|
+
if (name !== 0) {
|
|
191
195
|
header |= 65536 /* Field.Name */;
|
|
192
196
|
buffer[used++] = name;
|
|
193
197
|
}
|
|
194
|
-
if (str
|
|
198
|
+
if (str !== 0) {
|
|
195
199
|
header |= 131072 /* Field.String */;
|
|
196
200
|
buffer[used++] = str;
|
|
197
201
|
}
|
|
198
|
-
if (unit
|
|
202
|
+
if (unit !== 0) {
|
|
199
203
|
header |= 262144 /* Field.Unit */;
|
|
200
204
|
buffer[used++] = unit;
|
|
201
205
|
}
|
|
@@ -215,25 +219,25 @@ class StructuralMemo {
|
|
|
215
219
|
buffer[used++] = bits[1];
|
|
216
220
|
}
|
|
217
221
|
}
|
|
218
|
-
if (firstToken
|
|
222
|
+
if (firstToken !== 0) {
|
|
219
223
|
header |= 2097152 /* Field.Tokens */;
|
|
220
224
|
}
|
|
221
|
-
if (firstChild
|
|
225
|
+
if (firstChild !== 0) {
|
|
222
226
|
header |= 4194304 /* Field.Children */;
|
|
223
227
|
}
|
|
224
228
|
buffer[index] = header;
|
|
225
|
-
if (firstToken
|
|
226
|
-
for (let child = firstToken; child
|
|
229
|
+
if (firstToken !== 0) {
|
|
230
|
+
for (let child = firstToken; child !== 0; child = arena.nextSibling[child]) {
|
|
227
231
|
used = this.serialize(arena, child, used);
|
|
228
232
|
}
|
|
229
|
-
buffer = used + 1 > this.
|
|
233
|
+
buffer = used + 1 > this.poolLength ? this.growPool(used + 1) : this.pool;
|
|
230
234
|
buffer[used++] = End;
|
|
231
235
|
}
|
|
232
|
-
if (firstChild
|
|
233
|
-
for (let child = firstChild; child
|
|
236
|
+
if (firstChild !== 0) {
|
|
237
|
+
for (let child = firstChild; child !== 0; child = arena.nextSibling[child]) {
|
|
234
238
|
used = this.serialize(arena, child, used);
|
|
235
239
|
}
|
|
236
|
-
buffer = used + 1 > this.
|
|
240
|
+
buffer = used + 1 > this.poolLength ? this.growPool(used + 1) : this.pool;
|
|
237
241
|
buffer[used++] = End;
|
|
238
242
|
}
|
|
239
243
|
return used;
|
|
@@ -244,6 +248,7 @@ class StructuralMemo {
|
|
|
244
248
|
*/
|
|
245
249
|
growPool(size) {
|
|
246
250
|
this.pool = grow(this.pool, size);
|
|
251
|
+
this.poolLength = this.pool.length;
|
|
247
252
|
return this.pool;
|
|
248
253
|
}
|
|
249
254
|
/**
|
|
@@ -268,16 +273,16 @@ class StructuralMemo {
|
|
|
268
273
|
const mask = table.length - 1;
|
|
269
274
|
let index = hash & mask;
|
|
270
275
|
let slot;
|
|
271
|
-
while ((slot = table[index])
|
|
276
|
+
while ((slot = table[index]) !== 0) {
|
|
272
277
|
slot--;
|
|
273
278
|
const meta = slot * 3;
|
|
274
|
-
if (this.slots[meta]
|
|
279
|
+
if (this.slots[meta] === hash && this.slots[meta + 2] === length) {
|
|
275
280
|
const offset = this.slots[meta + 1];
|
|
276
281
|
let i = 0;
|
|
277
|
-
while (i < length && pool[offset + i]
|
|
282
|
+
while (i < length && pool[offset + i] === pool[base + i]) {
|
|
278
283
|
i++;
|
|
279
284
|
}
|
|
280
|
-
if (i
|
|
285
|
+
if (i === length) {
|
|
281
286
|
this.hits++;
|
|
282
287
|
return slot;
|
|
283
288
|
}
|
|
@@ -286,7 +291,7 @@ class StructuralMemo {
|
|
|
286
291
|
}
|
|
287
292
|
// insert: the serialized key is kept in the pool
|
|
288
293
|
slot = this.size++;
|
|
289
|
-
if (slot * 3
|
|
294
|
+
if (slot * 3 === this.slots.length) {
|
|
290
295
|
this.slots = grow(this.slots, slot * 3 + 3);
|
|
291
296
|
}
|
|
292
297
|
this.slots[slot * 3] = hash;
|
|
@@ -307,7 +312,7 @@ class StructuralMemo {
|
|
|
307
312
|
const mask = table.length - 1;
|
|
308
313
|
for (let slot = 0; slot < this.size; slot++) {
|
|
309
314
|
let index = this.slots[slot * 3] & mask;
|
|
310
|
-
while (table[index]
|
|
315
|
+
while (table[index] !== 0) {
|
|
311
316
|
index = (index + 1) & mask;
|
|
312
317
|
}
|
|
313
318
|
table[index] = slot + 1;
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { TokenField, numericTokenTypes } from '../arena.js';
|
|
9
9
|
import { EnumToken, EnumAstNodeStatus } from '../types.js';
|
|
10
10
|
import { evaluate } from '../math/expression.js';
|
|
11
11
|
import { FeatureWalkMode } from './type.js';
|
|
12
|
-
import {
|
|
12
|
+
import { isFunctionType, mathFuncs } from '../../syntax/constants.js';
|
|
13
13
|
import { ValueWalker } from '../walk.js';
|
|
14
|
+
import { isInTemplate } from '../mixins/utils.js';
|
|
14
15
|
|
|
15
16
|
class ComputeCalcExpressionFeature {
|
|
16
17
|
accept = new Set([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType]);
|
|
@@ -26,12 +27,18 @@ class ComputeCalcExpressionFeature {
|
|
|
26
27
|
options.features.push(new ComputeCalcExpressionFeature());
|
|
27
28
|
}
|
|
28
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
* @param arena
|
|
33
|
+
* @param ast
|
|
34
|
+
*/
|
|
29
35
|
run(arena, ast) {
|
|
30
|
-
|
|
36
|
+
// the mixin and custom function bodies are templates: the expressions are computed once they are used
|
|
37
|
+
if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren) || isInTemplate(arena, ast)) {
|
|
31
38
|
return null;
|
|
32
39
|
}
|
|
33
|
-
for (let node = arena.firstChild[ast]; node
|
|
34
|
-
if (arena.dataTypes[node]
|
|
40
|
+
for (let node = arena.firstChild[ast]; node !== 0; node = arena.nextSibling[node]) {
|
|
41
|
+
if (arena.dataTypes[node] !== EnumToken.DeclarationNodeType) {
|
|
35
42
|
continue;
|
|
36
43
|
}
|
|
37
44
|
if (!hasExpression(arena, node)) {
|
|
@@ -43,11 +50,11 @@ class ComputeCalcExpressionFeature {
|
|
|
43
50
|
for (const value of new ValueWalker(arena, node)) {
|
|
44
51
|
// the parent of a detached token is the declaration
|
|
45
52
|
const parent = arena.getValueParent(value) || node;
|
|
46
|
-
if (parent != null && arena.dataTypes[parent]
|
|
53
|
+
if (parent != null && arena.dataTypes[parent] === EnumToken.BinaryExpressionTokenType) {
|
|
47
54
|
continue;
|
|
48
55
|
}
|
|
49
56
|
const typ = arena.dataTypes[value];
|
|
50
|
-
if (typ
|
|
57
|
+
if (typ === EnumToken.BinaryExpressionTokenType) {
|
|
51
58
|
const result = evaluate(arena, [value]);
|
|
52
59
|
try {
|
|
53
60
|
if (result.length > 0) {
|
|
@@ -57,44 +64,47 @@ class ComputeCalcExpressionFeature {
|
|
|
57
64
|
catch (e) { }
|
|
58
65
|
continue;
|
|
59
66
|
}
|
|
60
|
-
if (
|
|
67
|
+
if (isFunctionType(typ)) {
|
|
61
68
|
set ??= new Set();
|
|
62
69
|
if (!set.has(value)) {
|
|
63
70
|
set.add(value);
|
|
64
71
|
if (parent != null) {
|
|
65
|
-
const name = arena.
|
|
72
|
+
const name = arena.resolveString(arena.stringValue[value]);
|
|
66
73
|
const shouldEvaluate = typ === EnumToken.MathFunctionTokenType ||
|
|
67
|
-
(typ
|
|
74
|
+
(typ === EnumToken.FunctionTokenType && mathFuncs.includes(name));
|
|
68
75
|
if (shouldEvaluate) {
|
|
69
|
-
const cp = name
|
|
76
|
+
const cp = name !== "calc" ? [value] : arena.tokens(value);
|
|
70
77
|
const values = evaluate(arena, cp);
|
|
71
78
|
// fix a + -b into a - b
|
|
72
79
|
// inlined: arena.walkValues(values)
|
|
73
80
|
for (const value of new ValueWalker(arena, values)) {
|
|
74
81
|
if (arena.dataTypes[value] === EnumToken.BinaryExpressionTokenType) {
|
|
75
82
|
// inlined: arena.getRight(value)
|
|
76
|
-
const r = (arena.getChildAt(value, TokenField.R) == null
|
|
83
|
+
const r = (arena.getChildAt(value, TokenField.R) == null
|
|
84
|
+
? null
|
|
85
|
+
: arena.fieldTokens(value, TokenField.R))[0];
|
|
77
86
|
if (arena.numericValue[value] === EnumToken.Add &&
|
|
78
87
|
// inlined: arena.isNumeric(r)
|
|
79
88
|
numericTokenTypes.has(arena.dataTypes[r]) &&
|
|
80
|
-
Math.sign(arena.numericValue[r])
|
|
89
|
+
Math.sign(arena.numericValue[r]) === -1) {
|
|
81
90
|
arena.numericValue[value] = EnumToken.Sub;
|
|
82
91
|
arena.numericValue[r] *= -1;
|
|
83
92
|
}
|
|
84
93
|
}
|
|
85
94
|
}
|
|
86
95
|
// the value is replaced by the computed tokens or wrapped into a calc() function
|
|
87
|
-
if (values.length
|
|
88
|
-
arena.dataTypes[values[0]]
|
|
96
|
+
if (values.length === 1 &&
|
|
97
|
+
arena.dataTypes[values[0]] !== EnumToken.BinaryExpressionTokenType) {
|
|
89
98
|
const parentType = arena.dataTypes[parent];
|
|
90
99
|
const wrap = !(parentType === EnumToken.MathFunctionTokenType ||
|
|
91
|
-
(parentType
|
|
92
|
-
arena.
|
|
100
|
+
(parentType === EnumToken.FunctionTokenType &&
|
|
101
|
+
arena.resolveString(arena.stringValue[parent]) === "calc")) &&
|
|
93
102
|
// inlined: arena.isNumeric(values[0])
|
|
94
|
-
(!numericTokenTypes.has(arena.dataTypes[values[0]]) ||
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
103
|
+
(!numericTokenTypes.has(arena.dataTypes[values[0]]) ||
|
|
104
|
+
arena.hasFraction(values[0])) &&
|
|
105
|
+
!(arena.dataTypes[values[0]] === EnumToken.MathFunctionTokenType ||
|
|
106
|
+
(arena.dataTypes[values[0]] === EnumToken.FunctionTokenType &&
|
|
107
|
+
mathFuncs.includes(arena.resolveString(arena.stringValue[values[0]]))));
|
|
98
108
|
arena.replaceToken(value, wrap ? createCalc(arena, value, values) : values[0]);
|
|
99
109
|
}
|
|
100
110
|
else {
|
|
@@ -127,11 +137,11 @@ function createCalc(arena, source, values) {
|
|
|
127
137
|
* @param node
|
|
128
138
|
*/
|
|
129
139
|
function hasExpression(arena, node) {
|
|
130
|
-
for (let token = arena.firstToken[node]; token
|
|
131
|
-
if (arena.dataTypes[token]
|
|
132
|
-
|
|
133
|
-
arena.firstToken[token]
|
|
134
|
-
arena.firstChild[token]
|
|
140
|
+
for (let token = arena.firstToken[node]; token !== 0; token = arena.nextSibling[token]) {
|
|
141
|
+
if (arena.dataTypes[token] === EnumToken.BinaryExpressionTokenType ||
|
|
142
|
+
isFunctionType(arena.dataTypes[token]) ||
|
|
143
|
+
arena.firstToken[token] !== 0 ||
|
|
144
|
+
arena.firstChild[token] !== 0) {
|
|
135
145
|
return true;
|
|
136
146
|
}
|
|
137
147
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken } from '../types.js';
|
|
@@ -21,7 +21,7 @@ import { TokenField } from '../arena.js';
|
|
|
21
21
|
function isIfToken(arena, value) {
|
|
22
22
|
return (arena.dataTypes[value] === EnumToken.IfConditionTokenType ||
|
|
23
23
|
(arena.dataTypes[value] === EnumToken.WildCardFunctionTokenType &&
|
|
24
|
-
equalsIgnoreCase("if", arena.
|
|
24
|
+
equalsIgnoreCase("if", arena.resolveString(arena.stringValue[value]))));
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* deep clone a declaration. the clone map is filled with the cloned value tokens
|
|
@@ -44,10 +44,10 @@ function cloneDeclaration(arena, declaration, nodeMap) {
|
|
|
44
44
|
*/
|
|
45
45
|
function createAtRule(arena, declaration, parent, nam, tokens, options = {}) {
|
|
46
46
|
const atRule = arena.createAtRule(EnumToken.AtRuleNodeType, nam, tokens.reduce((acc, curr) => acc + renderValue(arena, curr, options), ""), true, tokens, null, null);
|
|
47
|
-
if (arena.parents[declaration]
|
|
47
|
+
if (arena.parents[declaration] !== 0) {
|
|
48
48
|
parent = arena.parents[declaration];
|
|
49
49
|
}
|
|
50
|
-
if (parent
|
|
50
|
+
if (parent !== 0) {
|
|
51
51
|
arena.copyLocation(atRule, parent);
|
|
52
52
|
}
|
|
53
53
|
return atRule;
|
|
@@ -60,8 +60,8 @@ function createAtRule(arena, declaration, parent, nam, tokens, options = {}) {
|
|
|
60
60
|
function conditionLeft(arena, condition) {
|
|
61
61
|
return (arena
|
|
62
62
|
.fieldTokens(condition, TokenField.L)
|
|
63
|
-
.find((t) => arena.dataTypes[t]
|
|
64
|
-
arena.dataTypes[t]
|
|
63
|
+
.find((t) => arena.dataTypes[t] !== EnumToken.CommentTokenType &&
|
|
64
|
+
arena.dataTypes[t] !== EnumToken.WhitespaceTokenType) ?? null);
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
67
67
|
* value tokens of an if condition (right side). the trailing semicolon is removed
|
|
@@ -83,7 +83,7 @@ function substituteIfElseNode(arena, declaration, node, wrapper, parentWrapper,
|
|
|
83
83
|
let targetParentWrapper = parentWrapper;
|
|
84
84
|
let targetWrapper = wrapper;
|
|
85
85
|
// map a token or the declaration itself to its clone
|
|
86
|
-
const mapped = (target) => target
|
|
86
|
+
const mapped = (target) => target === declaration ? clonedDeclaration : nodeMap.get(target);
|
|
87
87
|
if (arena.dataTypes[node] === EnumToken.IfElseConditionTokenType) {
|
|
88
88
|
//
|
|
89
89
|
clonedDeclaration = cloneDeclaration(arena, declaration, nodeMap);
|
|
@@ -94,12 +94,12 @@ function substituteIfElseNode(arena, declaration, node, wrapper, parentWrapper,
|
|
|
94
94
|
//
|
|
95
95
|
if (target != null &&
|
|
96
96
|
arena.dataTypes[target] === EnumToken.IdenTokenType &&
|
|
97
|
-
equalsIgnoreCase("else", arena.
|
|
97
|
+
equalsIgnoreCase("else", arena.resolveString(arena.stringValue[target]))) {
|
|
98
98
|
arena.replaceToken(mapped(targetWrapper), ...conditionValue(arena, elseCondition, nodeMap));
|
|
99
|
-
if (targetParentWrapper
|
|
99
|
+
if (targetParentWrapper !== declaration) {
|
|
100
100
|
const siblings = arena.tokens(targetParentWrapper);
|
|
101
101
|
let index = siblings.indexOf(targetWrapper);
|
|
102
|
-
if (index
|
|
102
|
+
if (index !== -1) {
|
|
103
103
|
let i;
|
|
104
104
|
let siblingWrapper;
|
|
105
105
|
let left = conditionLeft(arena, arena.field(node, TokenField.L));
|
|
@@ -132,13 +132,13 @@ function substituteIfElseNode(arena, declaration, node, wrapper, parentWrapper,
|
|
|
132
132
|
return result;
|
|
133
133
|
}
|
|
134
134
|
const leftType = arena.dataTypes[left];
|
|
135
|
-
if (leftType === EnumToken.IdenTokenType && equalsIgnoreCase("else", arena.
|
|
135
|
+
if (leftType === EnumToken.IdenTokenType && equalsIgnoreCase("else", arena.resolveString(arena.stringValue[left]))) {
|
|
136
136
|
clonedDeclaration = cloneDeclaration(arena, declaration, nodeMap);
|
|
137
|
-
arena.replaceToken(mapped(targetWrapper
|
|
137
|
+
arena.replaceToken(mapped(targetWrapper === declaration ? node : targetWrapper), ...conditionValue(arena, node, nodeMap));
|
|
138
138
|
result.push(clonedDeclaration);
|
|
139
139
|
}
|
|
140
140
|
else if (leftType === EnumToken.WhenElseFunctionTokenType) {
|
|
141
|
-
const name = arena.
|
|
141
|
+
const name = arena.resolveString(arena.stringValue[left]);
|
|
142
142
|
const minify = name !== "supports";
|
|
143
143
|
const atRule = createAtRule(arena, declaration, parent, name, [
|
|
144
144
|
arena.createList(EnumToken.ParensTokenType, arena.tokens(left).map((t) => arena.clone(t, true))),
|
|
@@ -156,7 +156,7 @@ function substituteIfElseNode(arena, declaration, node, wrapper, parentWrapper,
|
|
|
156
156
|
else if (leftType === EnumToken.ContainerFunctionTokenType) {
|
|
157
157
|
const atRule = createAtRule(arena, declaration, parent, "container", [arena.clone(left, true)]);
|
|
158
158
|
clonedDeclaration = cloneDeclaration(arena, declaration, nodeMap);
|
|
159
|
-
const isWildCard = targetWrapper
|
|
159
|
+
const isWildCard = targetWrapper !== declaration && arena.dataTypes[targetWrapper] === EnumToken.WildCardFunctionTokenType;
|
|
160
160
|
arena.replaceToken(mapped(isWildCard ? targetWrapper : node), ...conditionValue(arena, node, nodeMap));
|
|
161
161
|
// inlined: arena.appendChildren(atRule, clonedDeclaration)
|
|
162
162
|
arena.appendChildList(atRule, [clonedDeclaration]);
|
|
@@ -180,7 +180,7 @@ function processNode(arena, declarationNode, cache, parent = 0) {
|
|
|
180
180
|
const result = [];
|
|
181
181
|
const stack = [declarationNode];
|
|
182
182
|
// the generated nodes are detached: remember the parent for the location of the generated at-rules
|
|
183
|
-
if (arena.parents[declarationNode]
|
|
183
|
+
if (arena.parents[declarationNode] !== 0) {
|
|
184
184
|
parent = arena.parents[declarationNode];
|
|
185
185
|
}
|
|
186
186
|
while (++k < stack.length) {
|
|
@@ -194,7 +194,7 @@ function processNode(arena, declarationNode, cache, parent = 0) {
|
|
|
194
194
|
const value = found.value;
|
|
195
195
|
// the parent of a detached token is the declaration
|
|
196
196
|
const parentWrapper = arena.getValueParent(value) || declaration;
|
|
197
|
-
const grandParent = parentWrapper
|
|
197
|
+
const grandParent = parentWrapper === declaration ? declaration : arena.getValueParent(parentWrapper) || declaration;
|
|
198
198
|
if (arena.dataTypes[value] === EnumToken.WildCardFunctionTokenType) {
|
|
199
199
|
const conditions = arena.tokens(value);
|
|
200
200
|
for (i = 0; i < conditions.length; i++) {
|
|
@@ -205,11 +205,11 @@ function processNode(arena, declarationNode, cache, parent = 0) {
|
|
|
205
205
|
stack.push(...substituteIfElseNode(arena, declaration, value, parentWrapper, grandParent, cache, parent));
|
|
206
206
|
}
|
|
207
207
|
// the node is substituted: release it unless it is the node being replaced
|
|
208
|
-
if (astNode
|
|
208
|
+
if (astNode !== declarationNode) {
|
|
209
209
|
arena.detach(astNode);
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
|
-
if (result.length > 0 && arena.parents[declarationNode]
|
|
212
|
+
if (result.length > 0 && arena.parents[declarationNode] !== 0) {
|
|
213
213
|
arena.replaceChild(declarationNode, ...result);
|
|
214
214
|
if (!result.includes(declarationNode)) {
|
|
215
215
|
arena.detach(declarationNode);
|
|
@@ -232,6 +232,11 @@ class ExpandIfFeature {
|
|
|
232
232
|
options.features.push(new ExpandIfFeature());
|
|
233
233
|
}
|
|
234
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
*
|
|
237
|
+
* @param arena
|
|
238
|
+
* @param declaration
|
|
239
|
+
*/
|
|
235
240
|
run(arena, declaration) {
|
|
236
241
|
return processNode(arena, declaration, new Set());
|
|
237
242
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
export { ComputePrefixFeature } from './prefix.js';
|
|
9
9
|
export { InlineCssVariablesFeature } from './inlinecssvariables.js';
|
|
10
|
+
export { EvaluateMixinFeature } from './mixin.js';
|
|
10
11
|
export { ComputeShorthandFeature } from './shorthand.js';
|
|
11
12
|
export { ComputeCalcExpressionFeature } from './calc.js';
|
|
12
13
|
export { TransformCssFeature } from './transform.js';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken, EnumAstNodeStatus } from '../types.js';
|
|
@@ -12,6 +12,7 @@ import { FeatureWalkMode } from './type.js';
|
|
|
12
12
|
import { mathFuncs } from '../../syntax/constants.js';
|
|
13
13
|
import { TokenField } from '../arena.js';
|
|
14
14
|
import { ValueWalker } from '../walk.js';
|
|
15
|
+
import { isInTemplate } from '../mixins/utils.js';
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* inline a binary expression into a parens token: (l op r)
|
|
@@ -20,7 +21,7 @@ import { ValueWalker } from '../walk.js';
|
|
|
20
21
|
*/
|
|
21
22
|
function inlineExpression(arena, token) {
|
|
22
23
|
const result = [];
|
|
23
|
-
if (arena.dataTypes[token]
|
|
24
|
+
if (arena.dataTypes[token] === EnumToken.BinaryExpressionTokenType) {
|
|
24
25
|
const chi = inlineExpression(arena, arena.field(token, TokenField.L));
|
|
25
26
|
chi.push(arena.createToken(arena.numericValue[token]));
|
|
26
27
|
for (const child of inlineExpression(arena, arena.field(token, TokenField.R))) {
|
|
@@ -38,24 +39,26 @@ function replace(arena, node, variableScope) {
|
|
|
38
39
|
for (const value of new ValueWalker(arena, node)) {
|
|
39
40
|
// the parent of a detached token is the declaration
|
|
40
41
|
const parentValue = arena.getValueParent(value) || node;
|
|
41
|
-
if (arena.dataTypes[value]
|
|
42
|
+
if (arena.dataTypes[value] === EnumToken.BinaryExpressionTokenType &&
|
|
42
43
|
parentValue != null &&
|
|
43
|
-
parentValue
|
|
44
|
+
parentValue !== node &&
|
|
44
45
|
// the token belongs to the parent token list (function arguments)
|
|
45
|
-
arena.parents[value]
|
|
46
|
+
arena.parents[value] === parentValue) {
|
|
46
47
|
arena.replaceToken(value, ...inlineExpression(arena, value));
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
50
|
// inlined: arena.walkValues(node)
|
|
50
51
|
for (const value of new ValueWalker(arena, node)) {
|
|
51
52
|
const typ = arena.dataTypes[value];
|
|
52
|
-
if (typ
|
|
53
|
-
(typ
|
|
53
|
+
if (typ === EnumToken.MathFunctionTokenType ||
|
|
54
|
+
(typ === EnumToken.WildCardFunctionTokenType && arena.resolveString(arena.stringValue[value]) === "var")) {
|
|
54
55
|
const first = arena.firstToken[value];
|
|
55
|
-
if (first
|
|
56
|
-
|
|
56
|
+
if (first !== 0 &&
|
|
57
|
+
arena.nextSibling[first] === 0 &&
|
|
58
|
+
arena.dataTypes[first] === EnumToken.DashedIdenTokenType) {
|
|
59
|
+
const info = (variableScope.get(arena.resolveString(arena.stringValue[first])));
|
|
57
60
|
// the variable declaration may have been released
|
|
58
|
-
if (info?.replaceable && arena.name[info.node]
|
|
61
|
+
if (info?.replaceable && arena.name[info.node] !== 0 && arena.parents[value] !== 0) {
|
|
59
62
|
arena.replaceToken(value, ...arena.tokens(info.node).map((t) => arena.clone(t, true)));
|
|
60
63
|
}
|
|
61
64
|
}
|
|
@@ -76,23 +79,32 @@ class InlineCssVariablesFeature {
|
|
|
76
79
|
options.features.push(new InlineCssVariablesFeature());
|
|
77
80
|
}
|
|
78
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
*
|
|
84
|
+
* @param arena
|
|
85
|
+
* @param ast
|
|
86
|
+
* @param _options
|
|
87
|
+
* @param parent
|
|
88
|
+
* @param context
|
|
89
|
+
*/
|
|
79
90
|
run(arena, ast, _options = {}, parent, context) {
|
|
80
|
-
|
|
91
|
+
// the variables of a mixin or a custom function body are private variables
|
|
92
|
+
if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren) || isInTemplate(arena, ast)) {
|
|
81
93
|
return null;
|
|
82
94
|
}
|
|
83
95
|
if (!("variableScope" in context)) {
|
|
84
96
|
context.variableScope = new Map();
|
|
85
97
|
}
|
|
86
98
|
// [':root', 'html']
|
|
87
|
-
const isRoot = arena.dataTypes[parent]
|
|
88
|
-
arena.dataTypes[ast]
|
|
89
|
-
(arena.raw
|
|
99
|
+
const isRoot = arena.dataTypes[parent] === EnumToken.StyleSheetNodeType &&
|
|
100
|
+
arena.dataTypes[ast] === EnumToken.RuleNodeType &&
|
|
101
|
+
(arena.raw.get(ast) ?? splitRule(arena.resolveString(arena.name[ast]))).some((segment) => segment.some((s) => s === ":root" || s === "html"));
|
|
90
102
|
const variableScope = context.variableScope;
|
|
91
|
-
for (let node = arena.firstChild[ast]; node
|
|
103
|
+
for (let node = arena.firstChild[ast]; node !== 0; node = arena.nextSibling[node]) {
|
|
92
104
|
if (arena.dataTypes[node] !== EnumToken.DeclarationNodeType) {
|
|
93
105
|
continue;
|
|
94
106
|
}
|
|
95
|
-
const name = arena.
|
|
107
|
+
const name = arena.resolveString(arena.name[node]);
|
|
96
108
|
// css variable
|
|
97
109
|
if (name.startsWith("--")) {
|
|
98
110
|
// the last declaration wins
|
|
@@ -110,10 +122,10 @@ class InlineCssVariablesFeature {
|
|
|
110
122
|
let recursive = false;
|
|
111
123
|
// inlined: arena.walkValues(node)
|
|
112
124
|
for (const value of new ValueWalker(arena, node)) {
|
|
113
|
-
if (arena.dataTypes[value]
|
|
114
|
-
(arena.dataTypes[value]
|
|
115
|
-
(mathFuncs.includes(arena.
|
|
116
|
-
arena.
|
|
125
|
+
if (arena.dataTypes[value] === EnumToken.MathFunctionTokenDefType ||
|
|
126
|
+
(arena.dataTypes[value] === EnumToken.WildCardFunctionTokenType &&
|
|
127
|
+
(mathFuncs.includes(arena.resolveString(arena.stringValue[value])) ||
|
|
128
|
+
arena.resolveString(arena.stringValue[value]) === "var"))) {
|
|
117
129
|
recursive = true;
|
|
118
130
|
break;
|
|
119
131
|
}
|
|
@@ -128,6 +140,13 @@ class InlineCssVariablesFeature {
|
|
|
128
140
|
}
|
|
129
141
|
return null;
|
|
130
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
*
|
|
145
|
+
* @param arena
|
|
146
|
+
* @param _ast
|
|
147
|
+
* @param _options
|
|
148
|
+
* @param context
|
|
149
|
+
*/
|
|
131
150
|
cleanup(arena, _ast, _options = {}, context) {
|
|
132
151
|
const variableScope = context.variableScope;
|
|
133
152
|
if (variableScope == null) {
|
|
@@ -137,8 +156,8 @@ class InlineCssVariablesFeature {
|
|
|
137
156
|
if (info.replaceable) {
|
|
138
157
|
// drop declarations from :root{}
|
|
139
158
|
for (const parent of info.parent) {
|
|
140
|
-
if (arena.parents[info.node]
|
|
141
|
-
const comment = arena.createComment(EnumToken.CommentTokenType, `/* ${arena.
|
|
159
|
+
if (arena.parents[info.node] === parent) {
|
|
160
|
+
const comment = arena.createComment(EnumToken.CommentTokenType, `/* ${arena.resolveString(arena.name[info.node])}: ${info.values.reduce((acc, curr) => acc + renderValue(arena, curr, { convertColor: false }), "")} */`);
|
|
142
161
|
arena.replaceChild(info.node, comment);
|
|
143
162
|
arena.detach(info.node);
|
|
144
163
|
}
|