@tbela99/css-parser 1.4.11 → 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 +39 -0
- package/README.md +18 -11
- package/dist/index-umd-web.js +6558 -5310
- package/dist/index.cjs +6576 -5313
- package/dist/index.d.ts +805 -279
- package/dist/lib/ast/clone.js +13 -8
- package/dist/lib/ast/expand.js +65 -15
- package/dist/lib/ast/features/calc.js +19 -56
- package/dist/lib/ast/features/if.js +28 -4
- package/dist/lib/ast/features/inlinecssvariables.js +6 -5
- package/dist/lib/ast/features/prefix.js +52 -73
- package/dist/lib/ast/features/shorthand.js +11 -6
- package/dist/lib/ast/features/transform.js +6 -3
- package/dist/lib/ast/find.js +7 -5
- package/dist/lib/ast/math/expression.js +190 -32
- package/dist/lib/ast/minify.js +117 -49
- package/dist/lib/ast/node.js +52 -0
- 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/types.js +1 -1
- package/dist/lib/ast/walk.js +55 -11
- package/dist/lib/fs/resolve.js +68 -27
- package/dist/lib/parser/declaration/list.js +22 -7
- package/dist/lib/parser/declaration/map.js +40 -13
- package/dist/lib/parser/declaration/set.js +3 -1
- package/dist/lib/parser/linesmap.js +2 -12
- package/dist/lib/parser/parse.js +843 -912
- package/dist/lib/parser/source.js +16 -2
- package/dist/lib/parser/tokenize.js +1489 -725
- package/dist/lib/parser/utils/at-rule-container.js +29 -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 +164 -141
- package/dist/lib/parser/utils/text.js +4 -2
- package/dist/lib/renderer/render.js +230 -125
- package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +187 -34
- 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 +45 -258
- package/dist/lib/validation/config.json.js +3 -0
- package/dist/lib/validation/match.js +110 -27
- package/dist/node.js +55 -33
- package/dist/types.d.ts +5 -1
- package/dist/types.js +4 -0
- package/dist/utils/sync.d.ts +16 -0
- package/dist/utils/sync.js +73 -0
- package/dist/web.js +37 -30
- package/package.json +1 -1
- package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
- package/llms.txt +0 -73
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);
|
|
@@ -17,7 +16,7 @@ const rules = [
|
|
|
17
16
|
EnumToken.AtRuleNodeType,
|
|
18
17
|
EnumToken.RuleNodeType,
|
|
19
18
|
EnumToken.AtRuleTokenType,
|
|
20
|
-
EnumToken.
|
|
19
|
+
EnumToken.KeyframesRuleNodeType,
|
|
21
20
|
];
|
|
22
21
|
// @ts-ignore
|
|
23
22
|
const features = Object.values(index).sort((a, b) => a.ordering - b.ordering);
|
|
@@ -29,6 +28,7 @@ const features = Object.values(index).sort((a, b) => a.ordering - b.ordering);
|
|
|
29
28
|
* @param errors
|
|
30
29
|
* @param nestingContent
|
|
31
30
|
*
|
|
31
|
+
* @param context
|
|
32
32
|
* @private
|
|
33
33
|
*/
|
|
34
34
|
function minify(ast, options = {}, recursive = false, errors, nestingContent, context = {}) {
|
|
@@ -36,22 +36,22 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
36
36
|
let postprocess = false;
|
|
37
37
|
let parents;
|
|
38
38
|
let replacement;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
let { sourcemap, module, ...options2 } = options;
|
|
40
|
+
if (!(options2.features != null)) {
|
|
41
|
+
options2 = {
|
|
42
42
|
removeDuplicateDeclarations: true,
|
|
43
43
|
computeShorthand: true,
|
|
44
44
|
computeCalcExpression: true,
|
|
45
45
|
removePrefix: false,
|
|
46
46
|
features: [],
|
|
47
|
-
...
|
|
47
|
+
...options2,
|
|
48
48
|
};
|
|
49
49
|
for (const feature of features) {
|
|
50
|
-
feature.register(
|
|
50
|
+
feature.register(options2);
|
|
51
51
|
}
|
|
52
|
-
|
|
52
|
+
options2.features.sort((a, b) => a.ordering - b.ordering);
|
|
53
53
|
}
|
|
54
|
-
for (const feature of
|
|
54
|
+
for (const feature of options2.features) {
|
|
55
55
|
if (feature.processMode & FeatureWalkMode.Pre) {
|
|
56
56
|
preprocess = true;
|
|
57
57
|
}
|
|
@@ -66,17 +66,20 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
66
66
|
continue;
|
|
67
67
|
}
|
|
68
68
|
replacement = parent;
|
|
69
|
-
for (const feature of
|
|
69
|
+
for (const feature of options2.features) {
|
|
70
70
|
if ((feature.processMode & FeatureWalkMode.Pre) === 0 ||
|
|
71
71
|
(feature.accept != null && !feature.accept.has(parent.typ))) {
|
|
72
72
|
continue;
|
|
73
73
|
}
|
|
74
74
|
if (rules.includes(replacement.typ) && !Array.isArray(replacement[TOKENS])) {
|
|
75
|
-
replacement[TOKENS] = parseString(replacement.typ == EnumToken.RuleNodeType || replacement.typ === EnumToken.
|
|
75
|
+
replacement[TOKENS] = parseString(replacement.typ == EnumToken.RuleNodeType || replacement.typ === EnumToken.KeyframesRuleNodeType
|
|
76
76
|
? replacement.sel
|
|
77
|
-
:
|
|
77
|
+
: // @ts-ignore
|
|
78
|
+
replacement.nam);
|
|
78
79
|
}
|
|
79
|
-
const result = feature.run(replacement,
|
|
80
|
+
const result = feature.run(replacement, options2,
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
parent[PARENT] ?? ast, context, FeatureWalkMode.Pre);
|
|
80
83
|
if (result != null) {
|
|
81
84
|
replacement = result;
|
|
82
85
|
}
|
|
@@ -85,24 +88,26 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
85
88
|
(!Array.isArray(replacement) || replacement.length > 0) &&
|
|
86
89
|
replacement != parent &&
|
|
87
90
|
parent[PARENT] != null) {
|
|
91
|
+
// @ts-ignore
|
|
88
92
|
replaceNodeOrValue(parent[PARENT], parent, replacement);
|
|
89
93
|
}
|
|
90
|
-
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
if (replacement.chi != null) {
|
|
91
96
|
// @ts-ignore
|
|
92
97
|
for (const node of replacement.chi) {
|
|
93
|
-
|
|
98
|
+
node[PARENT] = replacement;
|
|
94
99
|
parents.add(node);
|
|
95
100
|
}
|
|
96
101
|
}
|
|
97
102
|
}
|
|
98
|
-
for (const feature of
|
|
103
|
+
for (const feature of options2.features) {
|
|
99
104
|
if (feature.processMode & FeatureWalkMode.Pre && "cleanup" in feature) {
|
|
100
105
|
// @ts-ignore
|
|
101
|
-
feature.cleanup(ast,
|
|
106
|
+
feature.cleanup(ast, options2, context, FeatureWalkMode.Pre);
|
|
102
107
|
}
|
|
103
108
|
}
|
|
104
109
|
}
|
|
105
|
-
doMinify(ast,
|
|
110
|
+
doMinify(ast, options2, recursive, errors, nestingContent, context);
|
|
106
111
|
parents = new Set([ast]);
|
|
107
112
|
for (const parent of parents) {
|
|
108
113
|
if (parent.typ == EnumToken.CommentTokenType || parent.typ == EnumToken.CDOCOMMTokenType) {
|
|
@@ -110,12 +115,14 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
110
115
|
}
|
|
111
116
|
replacement = parent;
|
|
112
117
|
if (postprocess) {
|
|
113
|
-
for (const feature of
|
|
118
|
+
for (const feature of options2.features) {
|
|
114
119
|
if ((feature.processMode & FeatureWalkMode.Post) === 0 ||
|
|
115
120
|
(feature.accept != null && !feature.accept.has(parent.typ))) {
|
|
116
121
|
continue;
|
|
117
122
|
}
|
|
118
|
-
const result = feature.run(replacement,
|
|
123
|
+
const result = feature.run(replacement, options2,
|
|
124
|
+
// @ts-ignore
|
|
125
|
+
parent[PARENT] ?? ast, context, FeatureWalkMode.Post);
|
|
119
126
|
if (result != null) {
|
|
120
127
|
replacement = result;
|
|
121
128
|
}
|
|
@@ -128,18 +135,20 @@ function minify(ast, options = {}, recursive = false, errors, nestingContent, co
|
|
|
128
135
|
// @ts-ignore
|
|
129
136
|
replaceNodeOrValue(parent[PARENT], parent, replacement);
|
|
130
137
|
}
|
|
131
|
-
|
|
138
|
+
// @ts-ignore
|
|
139
|
+
if (replacement.chi != null) {
|
|
140
|
+
// @ts-ignore
|
|
132
141
|
for (const node of replacement.chi) {
|
|
133
|
-
|
|
142
|
+
node[PARENT] = replacement;
|
|
134
143
|
parents.add(node);
|
|
135
144
|
}
|
|
136
145
|
}
|
|
137
146
|
}
|
|
138
147
|
if (postprocess) {
|
|
139
|
-
for (const feature of
|
|
148
|
+
for (const feature of options2.features) {
|
|
140
149
|
if (feature.processMode & FeatureWalkMode.Post && "cleanup" in feature) {
|
|
141
150
|
// @ts-ignore
|
|
142
|
-
feature.cleanup(ast,
|
|
151
|
+
feature.cleanup(ast, options2, context, FeatureWalkMode.Post);
|
|
143
152
|
}
|
|
144
153
|
}
|
|
145
154
|
}
|
|
@@ -159,6 +168,7 @@ function transformAtRuleMediaPrelude(values) {
|
|
|
159
168
|
values[values.indexOf(value)] = value.l;
|
|
160
169
|
}
|
|
161
170
|
else {
|
|
171
|
+
// @ts-ignore
|
|
162
172
|
replaceNodeOrValue(parent, value, value.l);
|
|
163
173
|
// @ts-ignore
|
|
164
174
|
value = value.l;
|
|
@@ -208,16 +218,20 @@ function transformAtRuleMediaPrelude(values) {
|
|
|
208
218
|
},
|
|
209
219
|
l: val1,
|
|
210
220
|
r: val2,
|
|
211
|
-
[
|
|
221
|
+
[LOCSRCID]: value[LOCSRCID],
|
|
222
|
+
[LOCSTA]: value[LOCSTA],
|
|
223
|
+
[LOCEND]: value[LOCEND],
|
|
212
224
|
},
|
|
213
225
|
],
|
|
214
226
|
};
|
|
215
227
|
// @ts-expect-error
|
|
216
228
|
const p = parents?.[parents?.indexOf?.(parent) + 1];
|
|
217
229
|
if (p != null) {
|
|
230
|
+
// @ts-ignore
|
|
218
231
|
replaceNodeOrValue(p, parent, replacement);
|
|
219
232
|
}
|
|
220
233
|
else {
|
|
234
|
+
// @ts-ignore
|
|
221
235
|
values.splice(values.indexOf(parent), 1, replacement);
|
|
222
236
|
}
|
|
223
237
|
hasUpdates = true;
|
|
@@ -232,9 +246,9 @@ function transformAtRuleMediaPrelude(values) {
|
|
|
232
246
|
* Minify at-rule media
|
|
233
247
|
* - remove redundant tokens
|
|
234
248
|
* - generate range queries
|
|
235
|
-
* @param ast
|
|
236
249
|
*
|
|
237
250
|
* @private
|
|
251
|
+
* @param tokens
|
|
238
252
|
*/
|
|
239
253
|
function minifyAtRuleMedia(tokens) {
|
|
240
254
|
let hasUpdates = false;
|
|
@@ -273,7 +287,9 @@ function minifyAtRuleMedia(tokens) {
|
|
|
273
287
|
typ: EnumToken.CommaTokenType,
|
|
274
288
|
});
|
|
275
289
|
}
|
|
276
|
-
|
|
290
|
+
for (const token of t) {
|
|
291
|
+
acc.push(token);
|
|
292
|
+
}
|
|
277
293
|
return acc;
|
|
278
294
|
}, []));
|
|
279
295
|
}
|
|
@@ -331,8 +347,8 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
331
347
|
continue;
|
|
332
348
|
}
|
|
333
349
|
while (previous?.typ === EnumToken.CommentNodeType) {
|
|
350
|
+
// @ts-ignore
|
|
334
351
|
previous = ast.chi[--nodeIndex];
|
|
335
|
-
continue;
|
|
336
352
|
}
|
|
337
353
|
node = ast.chi[i];
|
|
338
354
|
if (node.typ === EnumToken.AtRuleNodeType && node.nam === "font-face") {
|
|
@@ -348,12 +364,15 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
348
364
|
continue;
|
|
349
365
|
}
|
|
350
366
|
}
|
|
351
|
-
else if (node.typ === EnumToken.
|
|
352
|
-
if (previous?.typ === EnumToken.
|
|
367
|
+
else if (node.typ === EnumToken.KeyframesRuleNodeType) {
|
|
368
|
+
if (previous?.typ === EnumToken.KeyframesRuleNodeType &&
|
|
353
369
|
node.sel === previous.sel) {
|
|
354
370
|
// do not merge keyframes
|
|
355
371
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@keyframes#resolving_duplicates
|
|
356
|
-
|
|
372
|
+
for (const child of node.chi) {
|
|
373
|
+
previous.chi.push(child);
|
|
374
|
+
}
|
|
375
|
+
// @ts-ignore
|
|
357
376
|
ast.chi.splice(i, 1);
|
|
358
377
|
previous = ast?.chi?.[nodeIndex] ?? null;
|
|
359
378
|
i = nodeIndex;
|
|
@@ -384,7 +403,9 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
384
403
|
minifyAtRuleMedia(slice);
|
|
385
404
|
if (slice.length !== node[TOKENS].length) {
|
|
386
405
|
node[TOKENS].length = 0;
|
|
387
|
-
|
|
406
|
+
for (const token of slice) {
|
|
407
|
+
node[TOKENS].push(token);
|
|
408
|
+
}
|
|
388
409
|
node.val = slice.reduce((acc, curr, index, arr) => acc +
|
|
389
410
|
(curr.typ === EnumToken.CommentTokenType ||
|
|
390
411
|
(curr.typ === EnumToken.WhitespaceTokenType &&
|
|
@@ -426,6 +447,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
426
447
|
else if (ast.typ === node.typ &&
|
|
427
448
|
ast.nam === node.nam &&
|
|
428
449
|
ast.val === node.val) {
|
|
450
|
+
// @ts-ignore
|
|
429
451
|
replaceNodeOrValue(ast, node, node.chi);
|
|
430
452
|
i--;
|
|
431
453
|
continue;
|
|
@@ -435,8 +457,9 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
435
457
|
previous.nam === node.nam &&
|
|
436
458
|
previous.val === node.val) {
|
|
437
459
|
if ("chi" in node) {
|
|
438
|
-
|
|
439
|
-
|
|
460
|
+
for (const child of node.chi) {
|
|
461
|
+
previous.chi.push(child);
|
|
462
|
+
}
|
|
440
463
|
if (!hasDeclaration(previous)) {
|
|
441
464
|
context.nodes.delete(previous);
|
|
442
465
|
doMinify(previous, options, recursive, errors, nestingContent, context);
|
|
@@ -519,7 +542,9 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
519
542
|
":is(" +
|
|
520
543
|
node[OPTIMIZED].selector.reduce(reducer, []).join(",") +
|
|
521
544
|
")";
|
|
522
|
-
const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) =>
|
|
545
|
+
const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) =>
|
|
546
|
+
// @ts-ignore
|
|
547
|
+
(acc.length > 0 ? acc + "," : "") + node[OPTIMIZED].optimized[0] + curr.join(""), "");
|
|
523
548
|
node.sel = sel1.length < sel2.length ? sel1 : sel2;
|
|
524
549
|
node[TOKENS] = null;
|
|
525
550
|
}
|
|
@@ -599,7 +624,9 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
599
624
|
":is(" +
|
|
600
625
|
node[OPTIMIZED].selector.reduce(reducer, []).join(",") +
|
|
601
626
|
")";
|
|
602
|
-
const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) =>
|
|
627
|
+
const sel2 = node[OPTIMIZED].selector.reduce((acc, curr) =>
|
|
628
|
+
// @ts-ignore
|
|
629
|
+
(acc.length > 0 ? acc + "," : "") + node[OPTIMIZED].optimized[0] + curr.join(""), "");
|
|
603
630
|
node.sel = sel1.length < sel2.length ? sel1 : sel2;
|
|
604
631
|
node[TOKENS] = null;
|
|
605
632
|
}
|
|
@@ -610,11 +637,14 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
610
637
|
curr.join(""), "");
|
|
611
638
|
node[TOKENS] = null;
|
|
612
639
|
}
|
|
640
|
+
// @ts-ignore
|
|
613
641
|
}
|
|
614
642
|
else if (node[OPTIMIZED]?.optimized.length > 0) {
|
|
643
|
+
// @ts-ignore
|
|
615
644
|
const sel = node[OPTIMIZED].optimized.join("");
|
|
616
645
|
if (sel.length < node.sel.length) {
|
|
617
646
|
node.sel = sel;
|
|
647
|
+
// @ts-ignore
|
|
618
648
|
node[RAW] = [node[OPTIMIZED].optimized.slice()];
|
|
619
649
|
node[TOKENS] = null;
|
|
620
650
|
}
|
|
@@ -637,12 +667,22 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
637
667
|
}
|
|
638
668
|
if (shouldMerge) {
|
|
639
669
|
if (((node.typ === EnumToken.RuleNodeType ||
|
|
640
|
-
node.typ === EnumToken.
|
|
670
|
+
node.typ === EnumToken.KeyframesRuleNodeType) &&
|
|
641
671
|
node.sel === previous.sel) ||
|
|
672
|
+
// @ts-ignore
|
|
642
673
|
(node.typ == EnumToken.AtRuleNodeType &&
|
|
643
674
|
node.nam !== "font-face" &&
|
|
675
|
+
// @ts-ignore
|
|
644
676
|
node.nam === previous.nam)) {
|
|
645
|
-
|
|
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
|
+
}
|
|
684
|
+
// @ts-ignore
|
|
685
|
+
node.chi = array;
|
|
646
686
|
doMinify(node, options, recursive, errors, nestingContent, context);
|
|
647
687
|
ast.chi.splice(nodeIndex, 1);
|
|
648
688
|
previous = ast.chi[--i];
|
|
@@ -650,7 +690,7 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent,
|
|
|
650
690
|
continue;
|
|
651
691
|
}
|
|
652
692
|
else if (node.typ == previous?.typ &&
|
|
653
|
-
[EnumToken.
|
|
693
|
+
[EnumToken.KeyframesRuleNodeType, EnumToken.RuleNodeType].includes(node.typ)) {
|
|
654
694
|
const intersect = diff(previous, node, options);
|
|
655
695
|
if (intersect != null) {
|
|
656
696
|
if (intersect.node1.chi.length == 0) {
|
|
@@ -803,7 +843,9 @@ function optimizeSelector(selector) {
|
|
|
803
843
|
}
|
|
804
844
|
break;
|
|
805
845
|
}
|
|
806
|
-
|
|
846
|
+
for (let i1 = 0; i1 < selector.length; i1++) {
|
|
847
|
+
selector[i1].splice(0, optimized.length);
|
|
848
|
+
}
|
|
807
849
|
let reducible = optimized.length == 1;
|
|
808
850
|
if (optimized[0] == "&") {
|
|
809
851
|
if (optimized[1] == " ") {
|
|
@@ -989,7 +1031,9 @@ function reduceSelector(acc, curr) {
|
|
|
989
1031
|
if (acc.length > 0) {
|
|
990
1032
|
acc.push(",");
|
|
991
1033
|
}
|
|
992
|
-
|
|
1034
|
+
for (const c of curr) {
|
|
1035
|
+
acc.push(c);
|
|
1036
|
+
}
|
|
993
1037
|
return acc;
|
|
994
1038
|
}, []);
|
|
995
1039
|
}
|
|
@@ -1117,7 +1161,7 @@ function matchSelectors(selector1, selector2) {
|
|
|
1117
1161
|
*/
|
|
1118
1162
|
function fixSelector(node) {
|
|
1119
1163
|
if (node.sel.includes("&")) {
|
|
1120
|
-
const attributes =
|
|
1164
|
+
const attributes = parseString(node.sel);
|
|
1121
1165
|
for (const attr of walkValues(attributes)) {
|
|
1122
1166
|
if (attr.value.typ == EnumToken.PseudoClassFuncTokenType &&
|
|
1123
1167
|
attr.value.val == ":is") {
|
|
@@ -1158,9 +1202,13 @@ function wrapNodes(previous, node, match, ast, reducer, i, nodeIndex) {
|
|
|
1158
1202
|
[RAW]: match.match.map((t) => t.slice()),
|
|
1159
1203
|
};
|
|
1160
1204
|
if (pSel == "&" || pSel === "") {
|
|
1161
|
-
|
|
1205
|
+
for (const child of previous.chi) {
|
|
1206
|
+
wrapper.chi.push(child);
|
|
1207
|
+
}
|
|
1162
1208
|
if (nSel == "&" || nSel === "") {
|
|
1163
|
-
|
|
1209
|
+
for (const child of node.chi) {
|
|
1210
|
+
wrapper.chi.push(child);
|
|
1211
|
+
}
|
|
1164
1212
|
}
|
|
1165
1213
|
else {
|
|
1166
1214
|
wrapper.chi.push(node);
|
|
@@ -1185,7 +1233,6 @@ function wrapNodes(previous, node, match, ast, reducer, i, nodeIndex) {
|
|
|
1185
1233
|
* Diff nodes
|
|
1186
1234
|
* @param n1
|
|
1187
1235
|
* @param n2
|
|
1188
|
-
* @param reducer
|
|
1189
1236
|
* @param options
|
|
1190
1237
|
*
|
|
1191
1238
|
* @private
|
|
@@ -1304,17 +1351,36 @@ function diff(n1, n2, options = {}) {
|
|
|
1304
1351
|
// @ts-ignore
|
|
1305
1352
|
chi: intersect.reverse(),
|
|
1306
1353
|
};
|
|
1354
|
+
let op = { level: 0, ...options };
|
|
1307
1355
|
if (result == null ||
|
|
1308
1356
|
[n1, n2].reduce((acc, curr) => {
|
|
1309
1357
|
let css = options.cache.get(curr);
|
|
1310
1358
|
if (css == null) {
|
|
1311
|
-
|
|
1359
|
+
let level = 0;
|
|
1360
|
+
let parent = curr[PARENT];
|
|
1361
|
+
while (parent != null && parent.typ != EnumToken.StyleSheetNodeType) {
|
|
1362
|
+
level++;
|
|
1363
|
+
parent = parent[PARENT];
|
|
1364
|
+
}
|
|
1365
|
+
op.level = level;
|
|
1366
|
+
css = doRender(curr, op).code;
|
|
1312
1367
|
options.cache.set(curr, css);
|
|
1313
1368
|
}
|
|
1314
1369
|
return curr.chi.length == 0 ? acc : acc + css.length;
|
|
1315
1370
|
}, 0) <=
|
|
1316
1371
|
[node1, node2, result].reduce((acc, curr) => {
|
|
1317
|
-
|
|
1372
|
+
let css = options.cache.get(curr);
|
|
1373
|
+
if (css != null) {
|
|
1374
|
+
return curr.chi.length == 0 ? acc : acc + css.length;
|
|
1375
|
+
}
|
|
1376
|
+
let level = 0;
|
|
1377
|
+
let parent = curr[PARENT];
|
|
1378
|
+
while (parent != null && parent.typ != EnumToken.StyleSheetNodeType) {
|
|
1379
|
+
level++;
|
|
1380
|
+
parent = parent[PARENT];
|
|
1381
|
+
}
|
|
1382
|
+
op.level = level;
|
|
1383
|
+
css = doRender(curr, op).code;
|
|
1318
1384
|
return curr.chi.length == 0 ? acc : acc + css.length;
|
|
1319
1385
|
}, 0)) {
|
|
1320
1386
|
if (node1.chi.length != 0 && node2.chi.length != 0) {
|
|
@@ -1381,7 +1447,9 @@ function reduceRuleSelector(node) {
|
|
|
1381
1447
|
acc.push(",");
|
|
1382
1448
|
}
|
|
1383
1449
|
unique.add(sig);
|
|
1384
|
-
|
|
1450
|
+
for (const c of curr) {
|
|
1451
|
+
acc.push(c);
|
|
1452
|
+
}
|
|
1385
1453
|
}
|
|
1386
1454
|
return acc;
|
|
1387
1455
|
}, []);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { TOKENS, ERRORS, STATE, LOCSRCID, LOCSTA, LOCEND, PARENT } from '../syntax/constants.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param node
|
|
6
|
+
* @param key
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
function getNodeProperty(node, key) {
|
|
10
|
+
switch (key) {
|
|
11
|
+
case "parent":
|
|
12
|
+
return node[PARENT];
|
|
13
|
+
case "location":
|
|
14
|
+
return node[LOCSRCID] == null && node[LOCSTA] == null && node[LOCEND] == null ? null : { srcId: node[LOCSRCID], sta: node[LOCSTA], end: node[LOCEND] };
|
|
15
|
+
case "state":
|
|
16
|
+
return node[STATE];
|
|
17
|
+
case "errors":
|
|
18
|
+
return node[ERRORS];
|
|
19
|
+
case "tokens":
|
|
20
|
+
return node[TOKENS];
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* @param node
|
|
27
|
+
* @param key
|
|
28
|
+
* @param value
|
|
29
|
+
*/
|
|
30
|
+
function setNodeProperty(node, key, value) {
|
|
31
|
+
switch (key) {
|
|
32
|
+
case "parent":
|
|
33
|
+
node[PARENT] = value;
|
|
34
|
+
break;
|
|
35
|
+
case "location":
|
|
36
|
+
node[LOCSRCID] = value.srcId;
|
|
37
|
+
node[LOCSTA] = value.sta;
|
|
38
|
+
node[LOCEND] = value.end;
|
|
39
|
+
break;
|
|
40
|
+
case "state":
|
|
41
|
+
node[STATE] = value;
|
|
42
|
+
break;
|
|
43
|
+
case "errors":
|
|
44
|
+
node[ERRORS] = value;
|
|
45
|
+
break;
|
|
46
|
+
case "tokens":
|
|
47
|
+
node[TOKENS] = value;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { getNodeProperty, setNodeProperty };
|
|
@@ -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") {
|