@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
|
import { EnumAstNodeStatus, EnumToken } from '../types.js';
|
|
@@ -40,12 +40,12 @@ function replaceAstNodes(arena, tokens) {
|
|
|
40
40
|
// inlined: arena.walkValues(tokens)
|
|
41
41
|
for (const value of new ValueWalker(arena, tokens)) {
|
|
42
42
|
const typ = arena.dataTypes[value];
|
|
43
|
-
if (typ
|
|
43
|
+
if (typ === EnumToken.MediaQueryConditionTokenType) {
|
|
44
44
|
const token = arena
|
|
45
45
|
.fieldTokens(value, TokenField.L)
|
|
46
|
-
.find((t) => arena.dataTypes[t]
|
|
46
|
+
.find((t) => arena.dataTypes[t] === EnumToken.IdenTokenType);
|
|
47
47
|
if (token != null) {
|
|
48
|
-
let val = arena.
|
|
48
|
+
let val = arena.resolveString(arena.stringValue[token]);
|
|
49
49
|
if (val in pseudoAliasMap) {
|
|
50
50
|
val = pseudoAliasMap[val];
|
|
51
51
|
// inlined: arena.setStr(token, val)
|
|
@@ -53,7 +53,7 @@ function replaceAstNodes(arena, tokens) {
|
|
|
53
53
|
const r = arena.fieldTokens(value, TokenField.R);
|
|
54
54
|
if ((equalsIgnoreCase(val, "min-resolution") || equalsIgnoreCase(val, "max-resolution")) &&
|
|
55
55
|
r.length > 0 &&
|
|
56
|
-
arena.dataTypes[r[0]]
|
|
56
|
+
arena.dataTypes[r[0]] === EnumToken.NumberTokenType) {
|
|
57
57
|
arena.dataTypes[r[0]] = EnumToken.ResolutionTokenType;
|
|
58
58
|
// inlined: arena.setUnitString(r[0], "x")
|
|
59
59
|
arena.unit[r[0]] = arena.internString("x");
|
|
@@ -71,16 +71,16 @@ function replaceAstNodes(arena, tokens) {
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
else if (typ
|
|
75
|
-
typ
|
|
76
|
-
typ
|
|
77
|
-
typ
|
|
78
|
-
let key = arena.
|
|
74
|
+
else if (typ === EnumToken.IdenTokenType ||
|
|
75
|
+
typ === EnumToken.PseudoClassFuncTokenType ||
|
|
76
|
+
typ === EnumToken.PseudoClassTokenType ||
|
|
77
|
+
typ === EnumToken.PseudoElementTokenType) {
|
|
78
|
+
let key = arena.resolveString(arena.stringValue[value]) + (typ === EnumToken.PseudoClassFuncTokenType ? "()" : "");
|
|
79
79
|
if (key in pseudoAliasMap) {
|
|
80
80
|
const isPseudClass = pseudoAliasMap[key].startsWith("::");
|
|
81
81
|
// inlined: arena.setStr(value, pseudoAliasMap[key])
|
|
82
82
|
arena.stringValue[value] = arena.internString(pseudoAliasMap[key]);
|
|
83
|
-
if (isPseudClass && typ
|
|
83
|
+
if (isPseudClass && typ === EnumToken.PseudoElementTokenType) {
|
|
84
84
|
arena.dataTypes[value] = EnumToken.PseudoClassTokenType;
|
|
85
85
|
}
|
|
86
86
|
result = true;
|
|
@@ -88,7 +88,7 @@ function replaceAstNodes(arena, tokens) {
|
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
// remove duplicate tokens
|
|
91
|
-
if (tokens.find((t) => arena.dataTypes[t]
|
|
91
|
+
if (tokens.find((t) => arena.dataTypes[t] === EnumToken.CommaTokenType) != null) {
|
|
92
92
|
const set = new Set();
|
|
93
93
|
const split = splitTokenList(arena, tokens, [EnumToken.CommaTokenType]);
|
|
94
94
|
tokens.length = 0;
|
|
@@ -121,24 +121,29 @@ class ComputePrefixFeature {
|
|
|
121
121
|
options.features.push(new ComputePrefixFeature(options));
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
*
|
|
126
|
+
* @param arena
|
|
127
|
+
* @param node
|
|
128
|
+
*/
|
|
124
129
|
run(arena, node) {
|
|
125
130
|
if (arena.flags[node] &
|
|
126
131
|
(EnumAstNodeStatus.Disallowed | EnumAstNodeStatus.Invalid | EnumAstNodeStatus.Malformed)) {
|
|
127
132
|
return null;
|
|
128
133
|
}
|
|
129
134
|
const typ = arena.dataTypes[node];
|
|
130
|
-
if (typ
|
|
131
|
-
// inlined: arena.setSelector(node, replacePseudo(splitRule(arena.
|
|
132
|
-
arena.name[node] = arena.internString(replacePseudo(splitRule(arena.
|
|
133
|
-
if (arena.firstToken[node]
|
|
135
|
+
if (typ === EnumToken.RuleNodeType) {
|
|
136
|
+
// inlined: arena.setSelector(node, replacePseudo(splitRule(arena.resolveString(arena.name[node]))).reduce((acc, curr, index) => acc + (index > 0 ? "," : "") + curr.join(""), ""))
|
|
137
|
+
arena.name[node] = arena.internString(replacePseudo(splitRule(arena.resolveString(arena.name[node]))).reduce((acc, curr, index) => acc + (index > 0 ? "," : "") + curr.join(""), ""));
|
|
138
|
+
if (arena.firstToken[node] !== 0) {
|
|
134
139
|
const tokens = arena.tokens(node);
|
|
135
140
|
replaceAstNodes(arena, tokens);
|
|
136
141
|
arena.setTokenList(node, tokens);
|
|
137
142
|
}
|
|
138
143
|
}
|
|
139
|
-
else if (typ
|
|
140
|
-
const name = arena.
|
|
141
|
-
if (name.charAt(0)
|
|
144
|
+
else if (typ === EnumToken.DeclarationNodeType) {
|
|
145
|
+
const name = arena.resolveString(arena.name[node]);
|
|
146
|
+
if (name.charAt(0) === "-") {
|
|
142
147
|
const match = name.match(/^-([^-]+)-(.+)$/);
|
|
143
148
|
if (match != null) {
|
|
144
149
|
let nam = match[2];
|
|
@@ -156,8 +161,9 @@ class ComputePrefixFeature {
|
|
|
156
161
|
// inlined: arena.walkValues(node)
|
|
157
162
|
for (const value of new ValueWalker(arena, node)) {
|
|
158
163
|
const typ = arena.dataTypes[value];
|
|
159
|
-
const val = arena.
|
|
160
|
-
if ((typ
|
|
164
|
+
const val = arena.resolveString(arena.stringValue[value]);
|
|
165
|
+
if ((typ === EnumToken.IdenTokenType ||
|
|
166
|
+
(typ !== EnumToken.ParensTokenType && funcLike.includes(typ))) &&
|
|
161
167
|
val.match(/^-([^-]+)-(.+)$/) != null) {
|
|
162
168
|
if (val.endsWith("-gradient")) {
|
|
163
169
|
if (regMatchLinearGradient.test(val)) {
|
|
@@ -179,8 +185,9 @@ class ComputePrefixFeature {
|
|
|
179
185
|
if (hasPrefix) {
|
|
180
186
|
// inlined: arena.walkValues(node)
|
|
181
187
|
for (const value of new ValueWalker(arena, node)) {
|
|
182
|
-
if (arena.dataTypes[value]
|
|
183
|
-
|
|
188
|
+
if (arena.dataTypes[value] === EnumToken.IdenTokenType ||
|
|
189
|
+
funcLike.includes(arena.dataTypes[value])) {
|
|
190
|
+
const match = arena.resolveString(arena.stringValue[value]).match(/^-([^-]+)-(.+)$/);
|
|
184
191
|
if (match != null) {
|
|
185
192
|
// inlined: arena.setStr(value, match[2])
|
|
186
193
|
arena.stringValue[value] = arena.internString(match[2]);
|
|
@@ -189,15 +196,15 @@ class ComputePrefixFeature {
|
|
|
189
196
|
}
|
|
190
197
|
}
|
|
191
198
|
}
|
|
192
|
-
else if (typ
|
|
193
|
-
const name = arena.
|
|
199
|
+
else if (typ === EnumToken.AtRuleNodeType || typ === EnumToken.KeyframesAtRuleNodeType) {
|
|
200
|
+
const name = arena.resolveString(arena.name[node]);
|
|
194
201
|
if (name.startsWith("-")) {
|
|
195
202
|
const match = name.match(/^-([^-]+)-(.+)$/);
|
|
196
203
|
if (match != null && "@" + match[2] in config.atRules) {
|
|
197
204
|
arena.name[node] = arena.internString(match[2]);
|
|
198
205
|
}
|
|
199
206
|
}
|
|
200
|
-
if (typ
|
|
207
|
+
if (typ === EnumToken.AtRuleNodeType && arena.stringValue[node] !== 0) {
|
|
201
208
|
const tokens = arena.tokens(node);
|
|
202
209
|
const result = replaceAstNodes(arena, tokens);
|
|
203
210
|
arena.setTokenList(node, tokens);
|
|
@@ -210,8 +217,9 @@ class ComputePrefixFeature {
|
|
|
210
217
|
}
|
|
211
218
|
/**
|
|
212
219
|
* Convert -webkit-linear-gradient to linear-gradient syntax
|
|
213
|
-
* @param tokens
|
|
214
220
|
* @returns
|
|
221
|
+
* @param arena
|
|
222
|
+
* @param token
|
|
215
223
|
*/
|
|
216
224
|
webkitLinearToLinearGradient(arena, token) {
|
|
217
225
|
let i;
|
|
@@ -237,27 +245,27 @@ class ComputePrefixFeature {
|
|
|
237
245
|
// - to bottom -> 180deg
|
|
238
246
|
// - to left -> 270deg
|
|
239
247
|
for (i = 0; i < tokens.length; i++) {
|
|
240
|
-
if (arena.dataTypes[tokens[i]]
|
|
241
|
-
(arena.dataTypes[tokens[i]]
|
|
248
|
+
if (arena.dataTypes[tokens[i]] === EnumToken.AngleTokenType ||
|
|
249
|
+
(arena.dataTypes[tokens[i]] === EnumToken.NumberTokenType && arena.numericValue[tokens[i]] === 0)) {
|
|
242
250
|
arena.numericValue[tokens[i]] =
|
|
243
251
|
(90 -
|
|
244
|
-
(arena.dataTypes[tokens[i]]
|
|
252
|
+
(arena.dataTypes[tokens[i]] === EnumToken.NumberTokenType
|
|
245
253
|
? arena.numericValue[tokens[i]]
|
|
246
254
|
: arena.numericValue[toDegrees(arena, tokens[i])])) %
|
|
247
255
|
360;
|
|
248
256
|
}
|
|
249
|
-
else if (arena.dataTypes[tokens[i]]
|
|
250
|
-
key = arena.
|
|
257
|
+
else if (arena.dataTypes[tokens[i]] === EnumToken.IdenTokenType) {
|
|
258
|
+
key = arena.resolveString(arena.stringValue[tokens[i]]).toLowerCase();
|
|
251
259
|
switch (key) {
|
|
252
260
|
case "right":
|
|
253
261
|
case "left":
|
|
254
|
-
tokens.splice(i, 1, arena.createToken(EnumToken.IdenTokenType, "to"), arena.createToken(EnumToken.WhitespaceTokenType), arena.createToken(EnumToken.IdenTokenType, key
|
|
262
|
+
tokens.splice(i, 1, arena.createToken(EnumToken.IdenTokenType, "to"), arena.createToken(EnumToken.WhitespaceTokenType), arena.createToken(EnumToken.IdenTokenType, key === "right" ? "left" : "right"));
|
|
255
263
|
i += 2;
|
|
256
264
|
break;
|
|
257
265
|
case "top":
|
|
258
266
|
case "bottom":
|
|
259
267
|
k = i + 1;
|
|
260
|
-
to = key
|
|
268
|
+
to = key === "top" ? "bottom" : "top";
|
|
261
269
|
while (k < tokens.length &&
|
|
262
270
|
(arena.dataTypes[tokens[k]] === EnumToken.WhitespaceTokenType ||
|
|
263
271
|
arena.dataTypes[tokens[k]] === EnumToken.CommentTokenType)) {
|
|
@@ -265,10 +273,10 @@ class ComputePrefixFeature {
|
|
|
265
273
|
}
|
|
266
274
|
if (arena.dataTypes[tokens[k]] === EnumToken.IdenTokenType) {
|
|
267
275
|
val = "";
|
|
268
|
-
if (equalsIgnoreCase(arena.
|
|
276
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[k]]), "left")) {
|
|
269
277
|
val = "right";
|
|
270
278
|
}
|
|
271
|
-
if (equalsIgnoreCase(arena.
|
|
279
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[k]]), "right")) {
|
|
272
280
|
val = "left";
|
|
273
281
|
}
|
|
274
282
|
if (val !== "") {
|
|
@@ -283,14 +291,15 @@ class ComputePrefixFeature {
|
|
|
283
291
|
}
|
|
284
292
|
}
|
|
285
293
|
}
|
|
286
|
-
// inlined: arena.setStr(token, equalsIgnoreCase(arena.
|
|
287
|
-
arena.stringValue[token] = arena.internString(
|
|
294
|
+
// inlined: arena.setStr(token, equalsIgnoreCase(arena.resolveString(arena.stringValue[token]), "-webkit-repeating-linear-gradient") ? "repeating-linear-gradient" : "linear-gradient")
|
|
295
|
+
arena.stringValue[token] = arena.internString(equalsIgnoreCase(arena.resolveString(arena.stringValue[token]), "-webkit-repeating-linear-gradient")
|
|
288
296
|
? "repeating-linear-gradient"
|
|
289
|
-
: "linear-gradient")
|
|
297
|
+
: "linear-gradient");
|
|
290
298
|
arena.setTokenList(token, tokens);
|
|
291
299
|
}
|
|
292
300
|
/**
|
|
293
301
|
* Convert -webkit-gradient(linear) to linear-gradient or linear-gradient syntax
|
|
302
|
+
* @param arena
|
|
294
303
|
* @param token
|
|
295
304
|
* @returns
|
|
296
305
|
*/
|
|
@@ -302,7 +311,7 @@ class ComputePrefixFeature {
|
|
|
302
311
|
let type = "";
|
|
303
312
|
let tokens = arena.tokens(token);
|
|
304
313
|
// linear or radial
|
|
305
|
-
if (equalsIgnoreCase(arena.
|
|
314
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "linear")) {
|
|
306
315
|
type = "linear-gradient";
|
|
307
316
|
i++;
|
|
308
317
|
}
|
|
@@ -316,7 +325,7 @@ class ComputePrefixFeature {
|
|
|
316
325
|
}
|
|
317
326
|
}
|
|
318
327
|
if (arena.dataTypes[tokens[i]] === EnumToken.IdenTokenType) {
|
|
319
|
-
key += (key.length > 0 ? " " : "") + arena.
|
|
328
|
+
key += (key.length > 0 ? " " : "") + arena.resolveString(arena.stringValue[tokens[i]]);
|
|
320
329
|
}
|
|
321
330
|
}
|
|
322
331
|
// mapping keywords
|
|
@@ -363,7 +372,7 @@ class ComputePrefixFeature {
|
|
|
363
372
|
continue;
|
|
364
373
|
}
|
|
365
374
|
if (arena.dataTypes[tokens[i]] === EnumToken.FunctionTokenType) {
|
|
366
|
-
if (equalsIgnoreCase(arena.
|
|
375
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "to")) {
|
|
367
376
|
colorStop.push(tokens[checkStopIndex]);
|
|
368
377
|
for (const token of arena.tokens(tokens[i])) {
|
|
369
378
|
colorStop.push(token);
|
|
@@ -372,12 +381,12 @@ class ComputePrefixFeature {
|
|
|
372
381
|
i = checkStopIndex;
|
|
373
382
|
checkStop = false;
|
|
374
383
|
}
|
|
375
|
-
else if (equalsIgnoreCase(arena.
|
|
384
|
+
else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "from")) {
|
|
376
385
|
k = arena.tokens(tokens[i]).length + 1;
|
|
377
386
|
tokens.splice(i, 1, ...arena.tokens(tokens[i]), arena.createToken(EnumToken.WhitespaceTokenType), arena.createToken(EnumToken.PercentageTokenType, 0));
|
|
378
387
|
i += k;
|
|
379
388
|
}
|
|
380
|
-
else if (equalsIgnoreCase(arena.
|
|
389
|
+
else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "color-stop")) {
|
|
381
390
|
k = arena.tokens(tokens[i]).length - 1;
|
|
382
391
|
tokens.splice(i, 1, ...arena
|
|
383
392
|
.tokens(tokens[i])
|
|
@@ -404,14 +413,16 @@ class ComputePrefixFeature {
|
|
|
404
413
|
}
|
|
405
414
|
/**
|
|
406
415
|
* Convert -webkit-radial-gradient to radial-gradient syntax
|
|
407
|
-
* @param tokens
|
|
408
416
|
* @returns
|
|
417
|
+
* @param arena
|
|
418
|
+
* @param token
|
|
409
419
|
*/
|
|
410
420
|
webkitRadialToRadialGradient(arena, token) {
|
|
411
421
|
let i = 0;
|
|
412
422
|
const tokens = arena.tokens(token);
|
|
413
423
|
while (i < tokens.length &&
|
|
414
|
-
(arena.dataTypes[tokens[i]] === EnumToken.WhitespaceTokenType ||
|
|
424
|
+
(arena.dataTypes[tokens[i]] === EnumToken.WhitespaceTokenType ||
|
|
425
|
+
arena.dataTypes[tokens[i]] === EnumToken.CommentTokenType)) {
|
|
415
426
|
i++;
|
|
416
427
|
}
|
|
417
428
|
const positions = [];
|
|
@@ -421,11 +432,11 @@ class ComputePrefixFeature {
|
|
|
421
432
|
if (arena.dataTypes[tokens[i]] === EnumToken.PercentageTokenType ||
|
|
422
433
|
(arena.dataTypes[tokens[i]] === EnumToken.NumberTokenType && 0 === arena.numericValue[tokens[i]]) ||
|
|
423
434
|
(arena.dataTypes[tokens[i]] === EnumToken.IdenTokenType &&
|
|
424
|
-
(equalsIgnoreCase(arena.
|
|
425
|
-
equalsIgnoreCase(arena.
|
|
426
|
-
equalsIgnoreCase(arena.
|
|
427
|
-
equalsIgnoreCase(arena.
|
|
428
|
-
equalsIgnoreCase(arena.
|
|
435
|
+
(equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "center") ||
|
|
436
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "top") ||
|
|
437
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "bottom") ||
|
|
438
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "left") ||
|
|
439
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "right")))) {
|
|
429
440
|
do {
|
|
430
441
|
positions.push(tokens[i]);
|
|
431
442
|
i++;
|
|
@@ -435,17 +446,18 @@ class ComputePrefixFeature {
|
|
|
435
446
|
}
|
|
436
447
|
}
|
|
437
448
|
if (arena.dataTypes[tokens[i]] === EnumToken.IdenTokenType) {
|
|
438
|
-
if (equalsIgnoreCase(arena.
|
|
439
|
-
equalsIgnoreCase(arena.
|
|
440
|
-
equalsIgnoreCase(arena.
|
|
441
|
-
equalsIgnoreCase(arena.
|
|
449
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "closest-side") ||
|
|
450
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "closest-corner") ||
|
|
451
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "farthest-side") ||
|
|
452
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "farthest-corner")) {
|
|
442
453
|
size.push(tokens[i++]);
|
|
443
454
|
}
|
|
444
|
-
else if (equalsIgnoreCase(arena.
|
|
445
|
-
equalsIgnoreCase(arena.
|
|
455
|
+
else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "circle") ||
|
|
456
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "ellipse")) {
|
|
446
457
|
form.push(tokens[i++]);
|
|
447
458
|
}
|
|
448
|
-
while (arena.dataTypes[tokens[i]] === EnumToken.WhitespaceTokenType ||
|
|
459
|
+
while (arena.dataTypes[tokens[i]] === EnumToken.WhitespaceTokenType ||
|
|
460
|
+
arena.dataTypes[tokens[i]] === EnumToken.CommentTokenType) {
|
|
449
461
|
i++;
|
|
450
462
|
}
|
|
451
463
|
if (arena.dataTypes[tokens[i]] === EnumToken.CommaTokenType) {
|
|
@@ -453,17 +465,18 @@ class ComputePrefixFeature {
|
|
|
453
465
|
}
|
|
454
466
|
}
|
|
455
467
|
if (arena.dataTypes[tokens[i]] === EnumToken.IdenTokenType) {
|
|
456
|
-
if (equalsIgnoreCase(arena.
|
|
457
|
-
equalsIgnoreCase(arena.
|
|
458
|
-
equalsIgnoreCase(arena.
|
|
459
|
-
equalsIgnoreCase(arena.
|
|
468
|
+
if (equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "closest-side") ||
|
|
469
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "closest-corner") ||
|
|
470
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "farthest-side") ||
|
|
471
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "farthest-corner")) {
|
|
460
472
|
size.push(tokens[i++]);
|
|
461
473
|
}
|
|
462
|
-
else if (equalsIgnoreCase(arena.
|
|
463
|
-
equalsIgnoreCase(arena.
|
|
474
|
+
else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "circle") ||
|
|
475
|
+
equalsIgnoreCase(arena.resolveString(arena.stringValue[tokens[i]]), "ellipse")) {
|
|
464
476
|
form.push(tokens[i++]);
|
|
465
477
|
}
|
|
466
|
-
while (arena.dataTypes[tokens[i]] === EnumToken.WhitespaceTokenType ||
|
|
478
|
+
while (arena.dataTypes[tokens[i]] === EnumToken.WhitespaceTokenType ||
|
|
479
|
+
arena.dataTypes[tokens[i]] === EnumToken.CommentTokenType) {
|
|
467
480
|
i++;
|
|
468
481
|
}
|
|
469
482
|
if (arena.dataTypes[tokens[i]] === EnumToken.CommaTokenType) {
|
|
@@ -495,10 +508,10 @@ class ComputePrefixFeature {
|
|
|
495
508
|
}
|
|
496
509
|
tokens.push(arena.createToken(EnumToken.CommaTokenType));
|
|
497
510
|
}
|
|
498
|
-
// inlined: arena.setStr(token, equalsIgnoreCase(arena.
|
|
499
|
-
arena.stringValue[token] = arena.internString(
|
|
511
|
+
// inlined: arena.setStr(token, equalsIgnoreCase(arena.resolveString(arena.stringValue[token]), "-webkit-repeating-radial-gradient") ? "repeating-radial-gradient" : "radial-gradient")
|
|
512
|
+
arena.stringValue[token] = arena.internString(equalsIgnoreCase(arena.resolveString(arena.stringValue[token]), "-webkit-repeating-radial-gradient")
|
|
500
513
|
? "repeating-radial-gradient"
|
|
501
|
-
: "radial-gradient")
|
|
514
|
+
: "radial-gradient");
|
|
502
515
|
for (const colorStop of colorStops) {
|
|
503
516
|
tokens.push(colorStop);
|
|
504
517
|
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 2.2.0
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
8
|
+
import { DeclarationMemo, PropertyList } from '../../parser/declaration/list.js';
|
|
9
|
+
import { EnumToken, EnumAstNodeStatus } from '../types.js';
|
|
10
|
+
import { FeatureWalkMode } from './type.js';
|
|
11
|
+
import { moveResult } from '../mixins/utils.js';
|
|
12
|
+
import { getSyntaxRule } from '../../validation/config.js';
|
|
13
|
+
import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
|
|
14
|
+
|
|
15
|
+
class ComputeShorthandFeature {
|
|
16
|
+
accept = new Set([
|
|
17
|
+
EnumToken.RuleNodeType,
|
|
18
|
+
EnumToken.AtRuleNodeType,
|
|
19
|
+
EnumToken.KeyframesRuleNodeType,
|
|
20
|
+
]);
|
|
21
|
+
get ordering() {
|
|
22
|
+
return 5;
|
|
23
|
+
}
|
|
24
|
+
get processMode() {
|
|
25
|
+
return FeatureWalkMode.Post;
|
|
26
|
+
}
|
|
27
|
+
static register(options) {
|
|
28
|
+
if (options.computeShorthand) {
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
options.features.push(new ComputeShorthandFeature(options));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param arena
|
|
36
|
+
* @param ast
|
|
37
|
+
* @param options
|
|
38
|
+
* @param _parent
|
|
39
|
+
* @param context
|
|
40
|
+
*/
|
|
41
|
+
run(arena, ast, options, _parent, context) {
|
|
42
|
+
if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren) || arena.firstChild[ast] === 0) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
// the keys of the declarations are shared by the rules of the minification
|
|
46
|
+
const memo = context == null ? null : (context.declarations ??= new DeclarationMemo());
|
|
47
|
+
let doNotSort = false;
|
|
48
|
+
// the descriptors of a custom function: the `result` descriptors are deduplicated when the declarations are
|
|
49
|
+
// not validated by the parser
|
|
50
|
+
let descriptors = null;
|
|
51
|
+
let isFunction = false;
|
|
52
|
+
if (arena.dataTypes[ast] === EnumToken.AtRuleNodeType) {
|
|
53
|
+
switch (arena.resolveString(arena.name[ast])) {
|
|
54
|
+
case "function":
|
|
55
|
+
doNotSort = true;
|
|
56
|
+
isFunction = true;
|
|
57
|
+
descriptors =
|
|
58
|
+
getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@function")?.getPropertyDescriptors() ?? null;
|
|
59
|
+
break;
|
|
60
|
+
case "mixin":
|
|
61
|
+
doNotSort = true;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// the children are processed by segments: an at-rule without a block or an `@apply` rule inserts declarations
|
|
66
|
+
// at its position, the declarations are not moved across it
|
|
67
|
+
let child = arena.firstChild[ast];
|
|
68
|
+
while (child !== 0) {
|
|
69
|
+
const list = new PropertyList(arena, options, memo, descriptors);
|
|
70
|
+
let hasDeclarations = false;
|
|
71
|
+
// first node of the segment that is not moved into the property list
|
|
72
|
+
let first = 0;
|
|
73
|
+
// at-rule without a block that ends the segment
|
|
74
|
+
let boundary = 0;
|
|
75
|
+
// the declarations (with their leading comments) are moved into the property list,
|
|
76
|
+
// the rules (with their leading comments) stay in place
|
|
77
|
+
while (child !== 0) {
|
|
78
|
+
let last = child;
|
|
79
|
+
if (!doNotSort) {
|
|
80
|
+
// capture comments with the next node
|
|
81
|
+
while (arena.nextSibling[last] !== 0 && arena.dataTypes[last] === EnumToken.CommentNodeType) {
|
|
82
|
+
last = arena.nextSibling[last];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const next = arena.nextSibling[last];
|
|
86
|
+
if (arena.dataTypes[last] === EnumToken.AtRuleNodeType &&
|
|
87
|
+
(!(arena.flags[last] & EnumAstNodeStatus.HasChildren) ||
|
|
88
|
+
arena.stringEquals(arena.name[last], "apply"))) {
|
|
89
|
+
boundary = child;
|
|
90
|
+
child = next;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
// https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example
|
|
94
|
+
if (doNotSort || arena.dataTypes[last] === EnumToken.DeclarationNodeType) {
|
|
95
|
+
hasDeclarations = true;
|
|
96
|
+
for (let node = child; node !== next;) {
|
|
97
|
+
const nextNode = arena.nextSibling[node];
|
|
98
|
+
arena.detach(node);
|
|
99
|
+
list.addDeclaration(node);
|
|
100
|
+
node = nextNode;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else if (first === 0) {
|
|
104
|
+
first = child;
|
|
105
|
+
}
|
|
106
|
+
child = next;
|
|
107
|
+
}
|
|
108
|
+
if (hasDeclarations) {
|
|
109
|
+
// the declarations are inserted before the rules of the segment
|
|
110
|
+
const reference = first !== 0 ? first : boundary;
|
|
111
|
+
for (const node of list) {
|
|
112
|
+
if (reference === 0) {
|
|
113
|
+
arena.appendChild(ast, node);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
arena.insertBefore(reference, node);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// the deduplicated result keeps the position of the first result: it is moved after the conditional rules
|
|
122
|
+
if (isFunction) {
|
|
123
|
+
moveResult(arena, ast);
|
|
124
|
+
}
|
|
125
|
+
return ast;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export { ComputeShorthandFeature };
|