@tbela99/css-parser 1.5.0 → 1.6.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 +33 -0
- package/README.md +7 -7
- package/dist/index-umd-web.js +5562 -4731
- package/dist/index.cjs +5565 -4736
- package/dist/index.d.ts +51 -37
- package/dist/lib/ast/expand.js +21 -7
- package/dist/lib/ast/features/calc.js +17 -55
- package/dist/lib/ast/features/if.js +7 -3
- package/dist/lib/ast/features/inlinecssvariables.js +6 -5
- package/dist/lib/ast/features/prefix.js +41 -44
- package/dist/lib/ast/features/shorthand.js +10 -5
- package/dist/lib/ast/features/transform.js +1 -1
- package/dist/lib/ast/math/expression.js +190 -32
- package/dist/lib/ast/minify.js +37 -14
- package/dist/lib/ast/node.js +5 -3
- package/dist/lib/ast/transform/compute.js +63 -4
- package/dist/lib/ast/transform/minify.js +6 -6
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.js +46 -18
- package/dist/lib/ast/walk.js +28 -4
- package/dist/lib/fs/resolve.js +11 -2
- package/dist/lib/parser/declaration/list.js +22 -7
- package/dist/lib/parser/declaration/map.js +26 -8
- package/dist/lib/parser/declaration/set.js +3 -1
- package/dist/lib/parser/linesmap.js +2 -4
- package/dist/lib/parser/parse.js +388 -227
- package/dist/lib/parser/tokenize.js +1478 -818
- package/dist/lib/parser/utils/at-rule-container.js +28 -196
- package/dist/lib/parser/utils/at-rule-generic.js +6 -7
- package/dist/lib/parser/utils/at-rule-import.js +17 -13
- package/dist/lib/parser/utils/at-rule-media.js +36 -19
- package/dist/lib/parser/utils/at-rule-support.js +19 -9
- package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
- package/dist/lib/parser/utils/at-rule.js +0 -18
- package/dist/lib/parser/utils/declaration.js +30 -95
- package/dist/lib/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +89 -144
- package/dist/lib/parser/utils/text.js +4 -2
- package/dist/lib/renderer/render.js +102 -72
- package/dist/lib/renderer/sourcemap/sourcemap.js +2 -5
- package/dist/lib/syntax/color/a98rgb.js +14 -4
- package/dist/lib/syntax/color/cmyk.js +10 -18
- package/dist/lib/syntax/color/color-mix.js +21 -34
- package/dist/lib/syntax/color/color.js +32 -49
- package/dist/lib/syntax/color/hsl.js +15 -15
- package/dist/lib/syntax/color/hwb.js +18 -17
- package/dist/lib/syntax/color/lab.js +17 -17
- package/dist/lib/syntax/color/lch.js +13 -13
- package/dist/lib/syntax/color/oklab.js +10 -11
- package/dist/lib/syntax/color/oklch.js +15 -18
- package/dist/lib/syntax/color/p3.js +18 -12
- package/dist/lib/syntax/color/prophotorgb.js +26 -28
- package/dist/lib/syntax/color/rec2020.js +8 -4
- package/dist/lib/syntax/color/relative-color.js +26 -10
- package/dist/lib/syntax/color/srgb.js +5 -4
- package/dist/lib/syntax/color/utils/distance.js +1 -1
- package/dist/lib/syntax/color/xyz.js +2 -2
- package/dist/lib/syntax/color/xyzd50.js +2 -2
- package/dist/lib/syntax/constants.js +12 -2
- package/dist/lib/syntax/syntax.js +32 -108
- package/dist/lib/validation/config.json.js +3 -0
- package/dist/lib/validation/match.js +29 -26
- package/dist/node.js +16 -9
- package/dist/types.d.ts +5 -1
- package/dist/types.js +4 -0
- package/dist/utils/sync.js +22 -1
- package/dist/web.js +13 -4
- package/package.json +1 -1
- package/llms.txt +0 -80
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mathFuncs,
|
|
1
|
+
import { mathFuncs, LOCEND, LOCSTA, LOCSRCID } from '../../syntax/constants.js';
|
|
2
2
|
import { EnumToken } from '../types.js';
|
|
3
3
|
import { rem, compute } from './math.js';
|
|
4
4
|
|
|
@@ -28,7 +28,9 @@ function evaluate(tokens) {
|
|
|
28
28
|
if (acc.length > 0) {
|
|
29
29
|
acc.push({ typ: EnumToken.CommaTokenType });
|
|
30
30
|
}
|
|
31
|
-
|
|
31
|
+
for (const token of t) {
|
|
32
|
+
acc.push(token);
|
|
33
|
+
}
|
|
32
34
|
return acc;
|
|
33
35
|
});
|
|
34
36
|
const result = evaluateFunc(tokens[0]);
|
|
@@ -58,7 +60,9 @@ function evaluate(tokens) {
|
|
|
58
60
|
// @ts-ignore
|
|
59
61
|
val: Math[nodes[0].val.toUpperCase()],
|
|
60
62
|
typ: EnumToken.NumberTokenType,
|
|
61
|
-
[
|
|
63
|
+
[LOCSRCID]: nodes[0][LOCSRCID],
|
|
64
|
+
[LOCSTA]: nodes[0][LOCSTA],
|
|
65
|
+
[LOCEND]: nodes[0][LOCEND],
|
|
62
66
|
},
|
|
63
67
|
];
|
|
64
68
|
}
|
|
@@ -78,11 +82,19 @@ function evaluate(tokens) {
|
|
|
78
82
|
token = {
|
|
79
83
|
typ: EnumToken.ListToken,
|
|
80
84
|
chi: [nodes[i], nodes[i + 1]],
|
|
81
|
-
[
|
|
85
|
+
[LOCSRCID]: nodes[i][LOCSRCID],
|
|
86
|
+
[LOCSTA]: nodes[i][LOCSTA],
|
|
87
|
+
[LOCEND]: nodes[i + 1][LOCEND],
|
|
82
88
|
};
|
|
83
89
|
}
|
|
84
90
|
else {
|
|
85
|
-
token = doEvaluate(nodes[i + 1], {
|
|
91
|
+
token = doEvaluate(nodes[i + 1], {
|
|
92
|
+
typ: EnumToken.NumberTokenType,
|
|
93
|
+
val: -1,
|
|
94
|
+
[LOCSRCID]: nodes[i + 1][LOCSRCID],
|
|
95
|
+
[LOCSTA]: nodes[i + 1][LOCSTA],
|
|
96
|
+
[LOCEND]: nodes[i + 1][LOCEND],
|
|
97
|
+
}, EnumToken.Mul);
|
|
86
98
|
}
|
|
87
99
|
i++;
|
|
88
100
|
}
|
|
@@ -97,16 +109,28 @@ function evaluate(tokens) {
|
|
|
97
109
|
const token = curr[1].reduce((acc, curr) => doEvaluate(acc, curr, EnumToken.Add));
|
|
98
110
|
if (token.typ != EnumToken.BinaryExpressionTokenType) {
|
|
99
111
|
if ("val" in token && +token.val < 0) {
|
|
100
|
-
acc.push({
|
|
112
|
+
acc.push({
|
|
113
|
+
typ: EnumToken.Sub,
|
|
114
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
115
|
+
[LOCSTA]: token[LOCSTA],
|
|
116
|
+
[LOCEND]: token[LOCEND],
|
|
117
|
+
}, {
|
|
101
118
|
...token,
|
|
102
119
|
val: -token.val,
|
|
103
|
-
[
|
|
120
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
121
|
+
[LOCSTA]: token[LOCSTA],
|
|
122
|
+
[LOCEND]: token[LOCEND],
|
|
104
123
|
});
|
|
105
124
|
return acc;
|
|
106
125
|
}
|
|
107
126
|
}
|
|
108
127
|
if (acc.length > 0 && curr[0] != EnumToken.ListToken) {
|
|
109
|
-
acc.push({
|
|
128
|
+
acc.push({
|
|
129
|
+
typ: EnumToken.Add,
|
|
130
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
131
|
+
[LOCSTA]: token[LOCSTA],
|
|
132
|
+
[LOCEND]: token[LOCEND],
|
|
133
|
+
});
|
|
110
134
|
}
|
|
111
135
|
acc.push(token);
|
|
112
136
|
return acc;
|
|
@@ -124,7 +148,9 @@ function doEvaluate(l, r, op) {
|
|
|
124
148
|
op,
|
|
125
149
|
l,
|
|
126
150
|
r,
|
|
127
|
-
[
|
|
151
|
+
[LOCSRCID]: l[LOCSRCID],
|
|
152
|
+
[LOCSTA]: l[LOCSTA],
|
|
153
|
+
[LOCEND]: r?.[LOCEND] ?? l[LOCEND],
|
|
128
154
|
};
|
|
129
155
|
if (!isScalarToken(l) || !isScalarToken(r) || (l.typ == r.typ && "unit" in l && "unit" in r && l.unit != r.unit)) {
|
|
130
156
|
return defaultReturn;
|
|
@@ -162,15 +188,39 @@ function doEvaluate(l, r, op) {
|
|
|
162
188
|
if (typeof v1 == "number" && l.typ == EnumToken.PercentageTokenType) {
|
|
163
189
|
v1 = {
|
|
164
190
|
typ: EnumToken.FractionTokenType,
|
|
165
|
-
l: {
|
|
166
|
-
|
|
191
|
+
l: {
|
|
192
|
+
typ: EnumToken.NumberTokenType,
|
|
193
|
+
val: v1,
|
|
194
|
+
[LOCSRCID]: l[LOCSRCID],
|
|
195
|
+
[LOCSTA]: l[LOCSTA],
|
|
196
|
+
[LOCEND]: l[LOCEND],
|
|
197
|
+
},
|
|
198
|
+
r: {
|
|
199
|
+
typ: EnumToken.NumberTokenType,
|
|
200
|
+
val: 100,
|
|
201
|
+
[LOCSRCID]: r[LOCSRCID],
|
|
202
|
+
[LOCSTA]: r[LOCSTA],
|
|
203
|
+
[LOCEND]: r[LOCEND],
|
|
204
|
+
},
|
|
167
205
|
};
|
|
168
206
|
}
|
|
169
207
|
else if (typeof v2 == "number" && r.typ == EnumToken.PercentageTokenType) {
|
|
170
208
|
v2 = {
|
|
171
209
|
typ: EnumToken.FractionTokenType,
|
|
172
|
-
l: {
|
|
173
|
-
|
|
210
|
+
l: {
|
|
211
|
+
typ: EnumToken.NumberTokenType,
|
|
212
|
+
val: v2,
|
|
213
|
+
[LOCSRCID]: l[LOCSRCID],
|
|
214
|
+
[LOCSTA]: l[LOCSTA],
|
|
215
|
+
[LOCEND]: l[LOCEND],
|
|
216
|
+
},
|
|
217
|
+
r: {
|
|
218
|
+
typ: EnumToken.NumberTokenType,
|
|
219
|
+
val: 100,
|
|
220
|
+
[LOCSRCID]: r[LOCSRCID],
|
|
221
|
+
[LOCSTA]: r[LOCSTA],
|
|
222
|
+
[LOCEND]: r[LOCEND],
|
|
223
|
+
},
|
|
174
224
|
};
|
|
175
225
|
}
|
|
176
226
|
}
|
|
@@ -181,7 +231,9 @@ function doEvaluate(l, r, op) {
|
|
|
181
231
|
...(l.typ === EnumToken.NumberTokenType || l.typ === EnumToken.IdenTokenType ? r : l),
|
|
182
232
|
typ,
|
|
183
233
|
val /* : typeof val == 'number' ? minifyNumber(val) : val */,
|
|
184
|
-
[
|
|
234
|
+
[LOCSRCID]: l[LOCSRCID],
|
|
235
|
+
[LOCSTA]: l[LOCSTA],
|
|
236
|
+
[LOCEND]: r?.[LOCEND] ?? l[LOCEND],
|
|
185
237
|
};
|
|
186
238
|
if (token.typ == EnumToken.IdenTokenType) {
|
|
187
239
|
// @ts-ignore
|
|
@@ -210,25 +262,64 @@ function evaluateFunc(token) {
|
|
|
210
262
|
case "sign":
|
|
211
263
|
case "sqrt":
|
|
212
264
|
case "exp": {
|
|
265
|
+
if (token.val == "tan" || token.val == "atan") {
|
|
266
|
+
for (let i = 0; i < values.length; i++) {
|
|
267
|
+
if (values[i].typ == EnumToken.NumberTokenType) {
|
|
268
|
+
values[i] = Object.assign(values[i], { typ: EnumToken.AngleTokenType, unit: "rad" });
|
|
269
|
+
}
|
|
270
|
+
else if (values[i].typ == EnumToken.AngleTokenType && values[i].unit != "rad") {
|
|
271
|
+
switch (values[i].unit) {
|
|
272
|
+
case "deg":
|
|
273
|
+
Object.assign(values[i], {
|
|
274
|
+
unit: "rad",
|
|
275
|
+
val: values[i].val * (Math.PI / 180),
|
|
276
|
+
});
|
|
277
|
+
break;
|
|
278
|
+
case "grad":
|
|
279
|
+
Object.assign(values[i], {
|
|
280
|
+
unit: "rad",
|
|
281
|
+
val: values[i].val * (Math.PI / 200),
|
|
282
|
+
});
|
|
283
|
+
break;
|
|
284
|
+
case "turn":
|
|
285
|
+
Object.assign(values[i], {
|
|
286
|
+
unit: "rad",
|
|
287
|
+
val: values[i].val * (2 * Math.PI),
|
|
288
|
+
});
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
213
294
|
const value = evaluate(values);
|
|
214
295
|
// @ts-ignore
|
|
215
|
-
let val = value[0].typ == EnumToken.NumberTokenType
|
|
296
|
+
let val = value[0].typ == EnumToken.NumberTokenType || value[0].typ == EnumToken.AngleTokenType
|
|
216
297
|
? +value[0].val
|
|
217
298
|
: // @ts-expect-error
|
|
218
299
|
value[0].l.val / value[0].r.val;
|
|
219
300
|
return [
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
301
|
+
token.val == "tan" || token.val == "atan"
|
|
302
|
+
? {
|
|
303
|
+
typ: EnumToken.AngleTokenType,
|
|
304
|
+
val: Math[token.val](val),
|
|
305
|
+
unit: "rad",
|
|
306
|
+
[LOCSRCID]: value[0][LOCSRCID],
|
|
307
|
+
[LOCSTA]: value[0][LOCSTA],
|
|
308
|
+
[LOCEND]: value[0][LOCEND],
|
|
309
|
+
}
|
|
310
|
+
: {
|
|
311
|
+
typ: EnumToken.NumberTokenType,
|
|
312
|
+
val: Math[token.val](val),
|
|
313
|
+
[LOCSRCID]: value[0][LOCSRCID],
|
|
314
|
+
[LOCSTA]: value[0][LOCSTA],
|
|
315
|
+
[LOCEND]: value[0][LOCEND],
|
|
316
|
+
},
|
|
225
317
|
];
|
|
226
318
|
}
|
|
227
319
|
case "hypot": {
|
|
228
320
|
const chi = values.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(t.typ));
|
|
229
321
|
let all = [];
|
|
230
322
|
let ref = chi[0];
|
|
231
|
-
let value = 0;
|
|
232
323
|
for (let i = 0; i < chi.length; i++) {
|
|
233
324
|
// @ts-ignore
|
|
234
325
|
const val = getValue(chi[i]);
|
|
@@ -236,13 +327,14 @@ function evaluateFunc(token) {
|
|
|
236
327
|
return null;
|
|
237
328
|
}
|
|
238
329
|
all.push(val);
|
|
239
|
-
value += val * val;
|
|
240
330
|
}
|
|
241
331
|
return [
|
|
242
332
|
{
|
|
243
333
|
...ref,
|
|
244
|
-
val:
|
|
245
|
-
[
|
|
334
|
+
val: Math.hypot(...all),
|
|
335
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
336
|
+
[LOCSTA]: token[LOCSTA],
|
|
337
|
+
[LOCEND]: token[LOCEND],
|
|
246
338
|
},
|
|
247
339
|
];
|
|
248
340
|
}
|
|
@@ -251,6 +343,35 @@ function evaluateFunc(token) {
|
|
|
251
343
|
case "rem":
|
|
252
344
|
case "mod": {
|
|
253
345
|
const chi = values.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
|
|
346
|
+
if (token.val == "atan2") {
|
|
347
|
+
for (let i = 0; i < chi.length; i++) {
|
|
348
|
+
if (chi[i].typ == EnumToken.NumberTokenType) {
|
|
349
|
+
chi[i] = Object.assign(chi[i], { typ: EnumToken.AngleTokenType, unit: "rad" });
|
|
350
|
+
}
|
|
351
|
+
else if (chi[i].typ == EnumToken.AngleTokenType && chi[i].unit != "rad") {
|
|
352
|
+
switch (chi[i].unit) {
|
|
353
|
+
case "deg":
|
|
354
|
+
Object.assign(chi[i], {
|
|
355
|
+
unit: "rad",
|
|
356
|
+
val: chi[i].val * (Math.PI / 180),
|
|
357
|
+
});
|
|
358
|
+
break;
|
|
359
|
+
case "grad":
|
|
360
|
+
Object.assign(chi[i], {
|
|
361
|
+
unit: "rad",
|
|
362
|
+
val: chi[i].val * (Math.PI / 200),
|
|
363
|
+
});
|
|
364
|
+
break;
|
|
365
|
+
case "turn":
|
|
366
|
+
Object.assign(chi[i], {
|
|
367
|
+
unit: "rad",
|
|
368
|
+
val: chi[i].val * (2 * Math.PI),
|
|
369
|
+
});
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
254
375
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/mod
|
|
255
376
|
const v1 = evaluate([chi[0]]);
|
|
256
377
|
const v2 = evaluate([chi[2]]);
|
|
@@ -271,7 +392,9 @@ function evaluateFunc(token) {
|
|
|
271
392
|
{
|
|
272
393
|
...v1[0],
|
|
273
394
|
val: Math.pow(val1, val2),
|
|
274
|
-
[
|
|
395
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
396
|
+
[LOCSTA]: token[LOCSTA],
|
|
397
|
+
[LOCEND]: token[LOCEND],
|
|
275
398
|
},
|
|
276
399
|
];
|
|
277
400
|
}
|
|
@@ -280,8 +403,12 @@ function evaluateFunc(token) {
|
|
|
280
403
|
{
|
|
281
404
|
...{},
|
|
282
405
|
...v1[0],
|
|
406
|
+
typ: EnumToken.AngleTokenType,
|
|
407
|
+
unit: "rad",
|
|
283
408
|
val: Math.atan2(val1, val2),
|
|
284
|
-
[
|
|
409
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
410
|
+
[LOCSTA]: token[LOCSTA],
|
|
411
|
+
[LOCEND]: token[LOCEND],
|
|
285
412
|
},
|
|
286
413
|
];
|
|
287
414
|
}
|
|
@@ -289,7 +416,9 @@ function evaluateFunc(token) {
|
|
|
289
416
|
{
|
|
290
417
|
...v1[0],
|
|
291
418
|
val: val2 == 0 ? val1 : val1 - Math.floor(val1 / val2) * val2,
|
|
292
|
-
[
|
|
419
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
420
|
+
[LOCSTA]: token[LOCSTA],
|
|
421
|
+
[LOCEND]: token[LOCEND],
|
|
293
422
|
},
|
|
294
423
|
];
|
|
295
424
|
}
|
|
@@ -326,7 +455,9 @@ function evaluateFunc(token) {
|
|
|
326
455
|
{
|
|
327
456
|
...values[0],
|
|
328
457
|
val: Math.log(val1) / Math.log(val2),
|
|
329
|
-
[
|
|
458
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
459
|
+
[LOCSTA]: token[LOCSTA],
|
|
460
|
+
[LOCEND]: token[LOCEND],
|
|
330
461
|
},
|
|
331
462
|
];
|
|
332
463
|
}
|
|
@@ -362,7 +493,15 @@ function evaluateFunc(token) {
|
|
|
362
493
|
: Math.ceil(val / val2) * val2;
|
|
363
494
|
}
|
|
364
495
|
// @ts-ignore
|
|
365
|
-
return [
|
|
496
|
+
return [
|
|
497
|
+
{
|
|
498
|
+
...values[0],
|
|
499
|
+
val,
|
|
500
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
501
|
+
[LOCSTA]: token[LOCSTA],
|
|
502
|
+
[LOCEND]: token[LOCEND],
|
|
503
|
+
},
|
|
504
|
+
];
|
|
366
505
|
}
|
|
367
506
|
}
|
|
368
507
|
}
|
|
@@ -380,7 +519,18 @@ function inlineExpression(token) {
|
|
|
380
519
|
result.push(token);
|
|
381
520
|
}
|
|
382
521
|
else {
|
|
383
|
-
|
|
522
|
+
for (const child of inlineExpression(token.l)) {
|
|
523
|
+
result.push(child);
|
|
524
|
+
}
|
|
525
|
+
result.push({
|
|
526
|
+
typ: token.op,
|
|
527
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
528
|
+
[LOCSTA]: token[LOCSTA],
|
|
529
|
+
[LOCEND]: token[LOCEND],
|
|
530
|
+
});
|
|
531
|
+
for (const child of inlineExpression(token.r)) {
|
|
532
|
+
result.push(child);
|
|
533
|
+
}
|
|
384
534
|
}
|
|
385
535
|
}
|
|
386
536
|
else {
|
|
@@ -443,7 +593,13 @@ function factorToken(token) {
|
|
|
443
593
|
token.val == "calc")) {
|
|
444
594
|
if ((token.typ == EnumToken.MathFunctionTokenType || token.typ == EnumToken.FunctionTokenType) &&
|
|
445
595
|
token.val == "calc") {
|
|
446
|
-
token = {
|
|
596
|
+
token = {
|
|
597
|
+
...token,
|
|
598
|
+
typ: EnumToken.ParensTokenType,
|
|
599
|
+
[LOCSRCID]: token[LOCSRCID],
|
|
600
|
+
[LOCSTA]: token[LOCSTA],
|
|
601
|
+
[LOCEND]: token[LOCEND],
|
|
602
|
+
};
|
|
447
603
|
// @ts-ignore
|
|
448
604
|
delete token.val;
|
|
449
605
|
}
|
|
@@ -481,7 +637,9 @@ function factor(tokens, ops) {
|
|
|
481
637
|
: getArithmeticOperation(tokens[i].val),
|
|
482
638
|
l: factorToken(tokens[i - 1]),
|
|
483
639
|
r: factorToken(tokens[i + 1]),
|
|
484
|
-
[
|
|
640
|
+
[LOCSRCID]: tokens[i - 1][LOCSRCID],
|
|
641
|
+
[LOCSTA]: tokens[i - 1][LOCSTA],
|
|
642
|
+
[LOCEND]: tokens[i + 1][LOCEND],
|
|
485
643
|
});
|
|
486
644
|
i--;
|
|
487
645
|
}
|
package/dist/lib/ast/minify.js
CHANGED
|
@@ -6,10 +6,9 @@ import { EnumToken } from './types.js';
|
|
|
6
6
|
import { isWhiteSpace, isIdent, isFunction, isIdentStart } from '../syntax/syntax.js';
|
|
7
7
|
import { FeatureWalkMode } from './features/type.js';
|
|
8
8
|
import { trimArray } from '../validation/match.js';
|
|
9
|
-
import { TOKENS, PARENT, OPTIMIZED, RAW, combinators,
|
|
9
|
+
import { TOKENS, PARENT, OPTIMIZED, RAW, combinators, LOCEND, LOCSTA, LOCSRCID } from '../syntax/constants.js';
|
|
10
10
|
import { replaceNodeOrValue } from '../parser/utils/token.js';
|
|
11
11
|
import { parseString } from '../parser/parse.js';
|
|
12
|
-
import { tokenize } from '../parser/tokenize.js';
|
|
13
12
|
import { replaceCompound } from './expand.js';
|
|
14
13
|
|
|
15
14
|
const notEndingWith = ["(", "["].concat(combinators);
|
|
@@ -219,7 +218,9 @@ function transformAtRuleMediaPrelude(values) {
|
|
|
219
218
|
},
|
|
220
219
|
l: val1,
|
|
221
220
|
r: val2,
|
|
222
|
-
[
|
|
221
|
+
[LOCSRCID]: value[LOCSRCID],
|
|
222
|
+
[LOCSTA]: value[LOCSTA],
|
|
223
|
+
[LOCEND]: value[LOCEND],
|
|
223
224
|
},
|
|
224
225
|
],
|
|
225
226
|
};
|
|
@@ -286,7 +287,9 @@ function minifyAtRuleMedia(tokens) {
|
|
|
286
287
|
typ: EnumToken.CommaTokenType,
|
|
287
288
|
});
|
|
288
289
|
}
|
|
289
|
-
|
|
290
|
+
for (const token of t) {
|
|
291
|
+
acc.push(token);
|
|
292
|
+
}
|
|
290
293
|
return acc;
|
|
291
294
|
}, []));
|
|
292
295
|
}
|
|
@@ -366,7 +369,9 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
366
369
|
node.sel === previous.sel) {
|
|
367
370
|
// do not merge keyframes
|
|
368
371
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@keyframes#resolving_duplicates
|
|
369
|
-
|
|
372
|
+
for (const child of node.chi) {
|
|
373
|
+
previous.chi.push(child);
|
|
374
|
+
}
|
|
370
375
|
// @ts-ignore
|
|
371
376
|
ast.chi.splice(i, 1);
|
|
372
377
|
previous = ast?.chi?.[nodeIndex] ?? null;
|
|
@@ -398,7 +403,9 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
398
403
|
minifyAtRuleMedia(slice);
|
|
399
404
|
if (slice.length !== node[TOKENS].length) {
|
|
400
405
|
node[TOKENS].length = 0;
|
|
401
|
-
|
|
406
|
+
for (const token of slice) {
|
|
407
|
+
node[TOKENS].push(token);
|
|
408
|
+
}
|
|
402
409
|
node.val = slice.reduce((acc, curr, index, arr) => acc +
|
|
403
410
|
(curr.typ === EnumToken.CommentTokenType ||
|
|
404
411
|
(curr.typ === EnumToken.WhitespaceTokenType &&
|
|
@@ -450,8 +457,9 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
450
457
|
previous.nam === node.nam &&
|
|
451
458
|
previous.val === node.val) {
|
|
452
459
|
if ("chi" in node) {
|
|
453
|
-
|
|
454
|
-
|
|
460
|
+
for (const child of node.chi) {
|
|
461
|
+
previous.chi.push(child);
|
|
462
|
+
}
|
|
455
463
|
if (!hasDeclaration(previous)) {
|
|
456
464
|
context.nodes.delete(previous);
|
|
457
465
|
doMinify(previous, options, recursive, errors, nestingContent, context);
|
|
@@ -666,8 +674,15 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
666
674
|
node.nam !== "font-face" &&
|
|
667
675
|
// @ts-ignore
|
|
668
676
|
node.nam === previous.nam)) {
|
|
677
|
+
const array = [];
|
|
678
|
+
for (let i = 0; i < previous.chi.length; i++) {
|
|
679
|
+
array.push(previous.chi[i]);
|
|
680
|
+
}
|
|
681
|
+
for (let i = 0; i < node.chi.length; i++) {
|
|
682
|
+
array.push(node.chi[i]);
|
|
683
|
+
}
|
|
669
684
|
// @ts-ignore
|
|
670
|
-
node.chi
|
|
685
|
+
node.chi = array;
|
|
671
686
|
doMinify(node, options, recursive, errors, nestingContent, context);
|
|
672
687
|
ast.chi.splice(nodeIndex, 1);
|
|
673
688
|
previous = ast.chi[--i];
|
|
@@ -1016,7 +1031,9 @@ function reduceSelector(acc, curr) {
|
|
|
1016
1031
|
if (acc.length > 0) {
|
|
1017
1032
|
acc.push(",");
|
|
1018
1033
|
}
|
|
1019
|
-
|
|
1034
|
+
for (const c of curr) {
|
|
1035
|
+
acc.push(c);
|
|
1036
|
+
}
|
|
1020
1037
|
return acc;
|
|
1021
1038
|
}, []);
|
|
1022
1039
|
}
|
|
@@ -1144,7 +1161,7 @@ function matchSelectors(selector1, selector2) {
|
|
|
1144
1161
|
*/
|
|
1145
1162
|
function fixSelector(node) {
|
|
1146
1163
|
if (node.sel.includes("&")) {
|
|
1147
|
-
const attributes =
|
|
1164
|
+
const attributes = parseString(node.sel);
|
|
1148
1165
|
for (const attr of walkValues(attributes)) {
|
|
1149
1166
|
if (attr.value.typ == EnumToken.PseudoClassFuncTokenType &&
|
|
1150
1167
|
attr.value.val == ":is") {
|
|
@@ -1185,9 +1202,13 @@ function wrapNodes(previous, node, match, ast, reducer, i, nodeIndex) {
|
|
|
1185
1202
|
[RAW]: match.match.map((t) => t.slice()),
|
|
1186
1203
|
};
|
|
1187
1204
|
if (pSel == "&" || pSel === "") {
|
|
1188
|
-
|
|
1205
|
+
for (const child of previous.chi) {
|
|
1206
|
+
wrapper.chi.push(child);
|
|
1207
|
+
}
|
|
1189
1208
|
if (nSel == "&" || nSel === "") {
|
|
1190
|
-
|
|
1209
|
+
for (const child of node.chi) {
|
|
1210
|
+
wrapper.chi.push(child);
|
|
1211
|
+
}
|
|
1191
1212
|
}
|
|
1192
1213
|
else {
|
|
1193
1214
|
wrapper.chi.push(node);
|
|
@@ -1426,7 +1447,9 @@ function reduceRuleSelector(node) {
|
|
|
1426
1447
|
acc.push(",");
|
|
1427
1448
|
}
|
|
1428
1449
|
unique.add(sig);
|
|
1429
|
-
|
|
1450
|
+
for (const c of curr) {
|
|
1451
|
+
acc.push(c);
|
|
1452
|
+
}
|
|
1430
1453
|
}
|
|
1431
1454
|
return acc;
|
|
1432
1455
|
}, []);
|
package/dist/lib/ast/node.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TOKENS, ERRORS, STATE,
|
|
1
|
+
import { TOKENS, ERRORS, STATE, LOCSRCID, LOCSTA, LOCEND, PARENT } from '../syntax/constants.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
*
|
|
@@ -11,7 +11,7 @@ function getNodeProperty(node, key) {
|
|
|
11
11
|
case "parent":
|
|
12
12
|
return node[PARENT];
|
|
13
13
|
case "location":
|
|
14
|
-
return node[
|
|
14
|
+
return node[LOCSRCID] == null && node[LOCSTA] == null && node[LOCEND] == null ? null : { srcId: node[LOCSRCID], sta: node[LOCSTA], end: node[LOCEND] };
|
|
15
15
|
case "state":
|
|
16
16
|
return node[STATE];
|
|
17
17
|
case "errors":
|
|
@@ -33,7 +33,9 @@ function setNodeProperty(node, key, value) {
|
|
|
33
33
|
node[PARENT] = value;
|
|
34
34
|
break;
|
|
35
35
|
case "location":
|
|
36
|
-
node[
|
|
36
|
+
node[LOCSRCID] = value.srcId;
|
|
37
|
+
node[LOCSTA] = value.sta;
|
|
38
|
+
node[LOCEND] = value.end;
|
|
37
39
|
break;
|
|
38
40
|
case "state":
|
|
39
41
|
node[STATE] = value;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { multiply, toZero
|
|
1
|
+
import { identity, multiply, toZero } from './utils.js';
|
|
2
2
|
import { EnumToken } from '../types.js';
|
|
3
3
|
import { stripCommaToken } from '../../validation/utils/list.js';
|
|
4
4
|
import { translateX, translateY, translateZ, translate, translate3d } from './translate.js';
|
|
@@ -17,6 +17,7 @@ function compute(transformLists) {
|
|
|
17
17
|
stripCommaToken(transformLists);
|
|
18
18
|
let matrix = identity();
|
|
19
19
|
let mat;
|
|
20
|
+
let transforms;
|
|
20
21
|
const cumulative = [];
|
|
21
22
|
for (const transformList of splitTransformList(transformLists)) {
|
|
22
23
|
mat = computeMatrix(transformList, identity());
|
|
@@ -24,7 +25,10 @@ function compute(transformLists) {
|
|
|
24
25
|
return null;
|
|
25
26
|
}
|
|
26
27
|
matrix = multiply(matrix, mat);
|
|
27
|
-
|
|
28
|
+
transforms = minify(mat) ?? transformList;
|
|
29
|
+
for (let i = 0; i < transforms.length; i++) {
|
|
30
|
+
cumulative.push(transforms[i]);
|
|
31
|
+
}
|
|
28
32
|
}
|
|
29
33
|
const serialized = serialize(matrix);
|
|
30
34
|
if (cumulative.length > 0) {
|
|
@@ -40,11 +44,66 @@ function compute(transformLists) {
|
|
|
40
44
|
});
|
|
41
45
|
}
|
|
42
46
|
}
|
|
43
|
-
|
|
47
|
+
const result = {
|
|
44
48
|
matrix: serialize(toZero(matrix)),
|
|
45
49
|
cumulative,
|
|
46
50
|
minified: minify(matrix) ?? [serialized],
|
|
47
51
|
};
|
|
52
|
+
// valid identity matrix
|
|
53
|
+
if ((result.minified.length == 1 &&
|
|
54
|
+
result.minified[0].typ == EnumToken.IdenTokenType &&
|
|
55
|
+
result.minified[0].val == "none") ||
|
|
56
|
+
(result.cumulative.length == 1 &&
|
|
57
|
+
result.cumulative[0].typ == EnumToken.IdenTokenType &&
|
|
58
|
+
result.cumulative[0].val == "none") ||
|
|
59
|
+
(result.matrix?.typ == EnumToken.IdenTokenType && result.matrix.val == "none")) {
|
|
60
|
+
// all transform function arguments must be 0 or scale(1)
|
|
61
|
+
for (const transform of transformLists) {
|
|
62
|
+
switch (transform.val) {
|
|
63
|
+
case "translate":
|
|
64
|
+
case "translateX":
|
|
65
|
+
case "translateY":
|
|
66
|
+
case "translateZ":
|
|
67
|
+
case "translate3d":
|
|
68
|
+
case "rotate":
|
|
69
|
+
case "rotateX":
|
|
70
|
+
case "rotateY":
|
|
71
|
+
case "rotateZ":
|
|
72
|
+
case "rotate3d":
|
|
73
|
+
case "skew":
|
|
74
|
+
case "skewX":
|
|
75
|
+
case "skewY":
|
|
76
|
+
for (const child of transform.chi) {
|
|
77
|
+
if (child.typ == EnumToken.WhitespaceTokenType || child.typ == EnumToken.CommaTokenType) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if ((child.typ != EnumToken.AngleTokenType &&
|
|
81
|
+
child.typ != EnumToken.NumberTokenType &&
|
|
82
|
+
child.typ != EnumToken.PercentageTokenType) ||
|
|
83
|
+
getNumber(child) != 0) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
case "scale":
|
|
89
|
+
case "scaleX":
|
|
90
|
+
case "scaleY":
|
|
91
|
+
case "scaleZ":
|
|
92
|
+
case "scale3d":
|
|
93
|
+
for (const child of transform.chi) {
|
|
94
|
+
if (child.typ == EnumToken.WhitespaceTokenType || child.typ == EnumToken.CommaTokenType) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if ((child.typ != EnumToken.NumberTokenType && child.typ != EnumToken.PercentageTokenType) ||
|
|
98
|
+
getNumber(child) != 1) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
48
107
|
}
|
|
49
108
|
function computeMatrix(transformList, matrixVar) {
|
|
50
109
|
let values = [];
|
|
@@ -166,7 +225,7 @@ function computeMatrix(transformList, matrixVar) {
|
|
|
166
225
|
if (values.length != 3) {
|
|
167
226
|
return null;
|
|
168
227
|
}
|
|
169
|
-
matrixVar = scale3d(
|
|
228
|
+
matrixVar = scale3d(values[0], values[1], values[2], matrixVar);
|
|
170
229
|
break;
|
|
171
230
|
}
|
|
172
231
|
if (transformList[i].val == "scale") {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { multiply, decompose, round, toZero
|
|
1
|
+
import { identity, multiply, decompose, round, toZero } from './utils.js';
|
|
2
2
|
import { epsilon } from '../../syntax/constants.js';
|
|
3
3
|
import { EnumToken } from '../types.js';
|
|
4
4
|
import { computeMatrix } from './compute.js';
|
|
@@ -245,7 +245,7 @@ function minify(matrix) {
|
|
|
245
245
|
function eqMatrix(a, b) {
|
|
246
246
|
let mat = identity();
|
|
247
247
|
let tmp = identity();
|
|
248
|
-
const data = (Array.isArray(a) ? a : parseMatrix(a));
|
|
248
|
+
const data = (Array.isArray(a) || ArrayBuffer.isView(a) ? a : parseMatrix(a));
|
|
249
249
|
for (const transform of b) {
|
|
250
250
|
tmp = computeMatrix([transform], identity());
|
|
251
251
|
if (tmp == null) {
|
|
@@ -267,7 +267,7 @@ function eqMatrix(a, b) {
|
|
|
267
267
|
}
|
|
268
268
|
function minifyTransformFunctions(transform) {
|
|
269
269
|
const name = transform.val.toLowerCase();
|
|
270
|
-
if ("
|
|
270
|
+
if ("skewX" == name) {
|
|
271
271
|
transform.val = "skew";
|
|
272
272
|
return transform;
|
|
273
273
|
}
|
|
@@ -307,10 +307,10 @@ function minifyTransformFunctions(transform) {
|
|
|
307
307
|
}
|
|
308
308
|
const ignoredValue = name.startsWith("scale") ? 1 : 0;
|
|
309
309
|
const t = new Set(["x", "y", "z"]);
|
|
310
|
-
let i = 3;
|
|
311
|
-
|
|
310
|
+
for (let i = 0; i < 3; i++) {
|
|
311
|
+
const axis = i == 0 ? "x" : i == 1 ? "y" : "z";
|
|
312
312
|
if (values.length <= i || values[i].val == ignoredValue) {
|
|
313
|
-
t.delete(
|
|
313
|
+
t.delete(axis);
|
|
314
314
|
}
|
|
315
315
|
}
|
|
316
316
|
if (name == "translate3d" || name == "translate") {
|