@tbela99/css-parser 1.4.2 → 1.4.4

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.
Files changed (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -1,48 +1,69 @@
1
- import { EnumToken, SyntaxValidationResult } from '../types.js';
2
- import { getSyntaxConfig } from '../../validation/config.js';
3
- import '../../validation/parser/parse.js';
4
- import { splitRule } from '../minify.js';
1
+ import { EnumToken } from '../types.js';
5
2
  import { walkValues } from '../walk.js';
6
- import '../../parser/parse.js';
7
- import '../../parser/tokenize.js';
8
- import '../../parser/utils/config.js';
9
3
  import { pseudoAliasMap } from '../../syntax/syntax.js';
10
- import { renderToken } from '../../renderer/render.js';
11
- import '../../renderer/sourcemap/lib/encode.js';
12
- import '../../validation/syntaxes/complex-selector.js';
13
- import { evaluateSyntax } from '../../validation/syntax.js';
14
- import { funcLike } from '../../syntax/color/utils/constants.js';
4
+ import { splitRule } from '../minify.js';
5
+ import { renderValue } from '../../renderer/render.js';
6
+ import { funcLike, regMatchLinearGradient, regMatchRadialGradient } from '../../syntax/constants.js';
15
7
  import { FeatureWalkMode } from './type.js';
8
+ import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
9
+ import { getSyntaxConfig } from '../../validation/config.js';
10
+ import { splitTokenList } from '../../validation/utils/list.js';
11
+ import { equalsIgnoreCase } from '../../parser/utils/text.js';
12
+ import { toDegrees } from '../../parser/utils/angle.js';
16
13
 
17
14
  const config = getSyntaxConfig();
18
15
  function replacePseudo(tokens) {
19
- return tokens.map((raw) => raw.map(r => {
20
- if (r.includes('(')) {
21
- const index = r.indexOf('(');
22
- const name = r.slice(0, index) + '()';
16
+ return tokens.map((raw) => raw.map((r) => {
17
+ if (r.includes("(")) {
18
+ const index = r.indexOf("(");
19
+ const name = r.slice(0, index) + "()";
23
20
  if (name in pseudoAliasMap) {
24
21
  return pseudoAliasMap[name] + r.slice(index);
25
22
  }
26
23
  return r;
27
24
  }
28
- return r in pseudoAliasMap && pseudoAliasMap[r] in config["selectors" /* ValidationSyntaxGroupEnum.Selectors */] ? pseudoAliasMap[r] : r;
25
+ return r in pseudoAliasMap && pseudoAliasMap[r] in config[ValidationSyntaxGroupEnum.Selectors]
26
+ ? pseudoAliasMap[r]
27
+ : r;
29
28
  }));
30
29
  }
31
30
  function replaceAstNodes(tokens, root) {
32
31
  let result = false;
33
32
  for (const { value, parent } of walkValues(tokens, root)) {
34
- if (value.typ == EnumToken.IdenTokenType || value.typ == EnumToken.PseudoClassFuncTokenType || value.typ == EnumToken.PseudoClassTokenType || value.typ == EnumToken.PseudoElementTokenType) {
35
- let key = value.val + (value.typ == EnumToken.PseudoClassFuncTokenType ? '()' : '');
33
+ if (value.typ == EnumToken.MediaQueryConditionTokenType) {
34
+ const token = value.l.find((t) => t.typ == EnumToken.IdenTokenType);
35
+ if (token != null) {
36
+ if (token.val in pseudoAliasMap) {
37
+ token.val = pseudoAliasMap[token.val];
38
+ if ((equalsIgnoreCase(token.val, "min-resolution") ||
39
+ equalsIgnoreCase(token.val, "max-resolution")) &&
40
+ // ["min-resolution", "max-resolution"].includes((token as IdentToken).val) &&
41
+ value.r?.[0]?.typ == EnumToken.NumberTokenType) {
42
+ Object.assign(value.r?.[0], {
43
+ typ: EnumToken.ResolutionTokenType,
44
+ unit: "x",
45
+ });
46
+ result = true;
47
+ }
48
+ }
49
+ }
50
+ }
51
+ else if (value.typ == EnumToken.IdenTokenType ||
52
+ value.typ == EnumToken.PseudoClassFuncTokenType ||
53
+ value.typ == EnumToken.PseudoClassTokenType ||
54
+ value.typ == EnumToken.PseudoElementTokenType) {
55
+ let key = value.val +
56
+ (value.typ == EnumToken.PseudoClassFuncTokenType ? "()" : "");
36
57
  if (key in pseudoAliasMap) {
37
- const isPseudClass = pseudoAliasMap[key].startsWith('::');
58
+ const isPseudClass = pseudoAliasMap[key].startsWith("::");
38
59
  value.val = pseudoAliasMap[key];
39
60
  if (value.typ == EnumToken.IdenTokenType &&
40
- ['min-resolution', 'max-resolution'].includes(value.val) &&
61
+ ["min-resolution", "max-resolution"].includes(value.val) &&
41
62
  parent?.typ == EnumToken.MediaQueryConditionTokenType &&
42
63
  parent.r?.[0]?.typ == EnumToken.NumberTokenType) {
43
64
  Object.assign(parent.r?.[0], {
44
65
  typ: EnumToken.ResolutionTokenType,
45
- unit: 'x',
66
+ unit: "x",
46
67
  });
47
68
  }
48
69
  else if (isPseudClass && value.typ == EnumToken.PseudoElementTokenType) {
@@ -53,6 +74,24 @@ function replaceAstNodes(tokens, root) {
53
74
  }
54
75
  }
55
76
  }
77
+ if (tokens.find((t) => t.typ == EnumToken.CommaTokenType) != null) {
78
+ const set = new Set();
79
+ const split = splitTokenList(tokens, [EnumToken.CommaTokenType]);
80
+ tokens.length = 0;
81
+ tokens.push(...split.reduce((acc, curr) => {
82
+ const str = curr.reduce((acc, curr) => acc + renderValue(curr), "");
83
+ if (set.has(str)) {
84
+ return acc;
85
+ }
86
+ set.add(str);
87
+ if (acc.length > 0) {
88
+ acc.push({
89
+ typ: EnumToken.CommaTokenType,
90
+ });
91
+ }
92
+ return acc.concat(curr);
93
+ }, []));
94
+ }
56
95
  return result;
57
96
  }
58
97
  class ComputePrefixFeature {
@@ -70,22 +109,13 @@ class ComputePrefixFeature {
70
109
  }
71
110
  run(node) {
72
111
  if (node.typ == EnumToken.RuleNodeType) {
73
- node.sel = replacePseudo(splitRule(node.sel)).reduce((acc, curr, index) => acc + (index > 0 ? ',' : '') + curr.join(''), '');
74
- // if ((node as AstRule).raw != null) {
75
- //
76
- // (node as AstRule).raw = replacePseudo((node as AstRule).raw as string[][]);
77
- // }
78
- //
79
- // if ((node as AstRule).optimized != null) {
80
- //
81
- // (node as AstRule).optimized!.selector = replacePseudo((node as AstRule).optimized!.selector as string[][]);
82
- // }
112
+ node.sel = replacePseudo(splitRule(node.sel)).reduce((acc, curr, index) => acc + (index > 0 ? "," : "") + curr.join(""), "");
83
113
  if (node.tokens != null) {
84
114
  replaceAstNodes(node.tokens);
85
115
  }
86
116
  }
87
117
  else if (node.typ == EnumToken.DeclarationNodeType) {
88
- if (node.nam.charAt(0) == '-') {
118
+ if (node.nam.charAt(0) == "-") {
89
119
  const match = node.nam.match(/^-([^-]+)-(.+)$/);
90
120
  if (match != null) {
91
121
  let nam = match[2];
@@ -101,8 +131,19 @@ class ComputePrefixFeature {
101
131
  }
102
132
  let hasPrefix = false;
103
133
  for (const { value } of walkValues(node.val)) {
104
- if ((value.typ == EnumToken.IdenTokenType || (value.typ != EnumToken.ParensTokenType && funcLike.includes(value.typ))) && value.val.match(/^-([^-]+)-(.+)$/) != null) {
105
- if (value.val.endsWith('-gradient')) {
134
+ if ((value.typ == EnumToken.IdenTokenType ||
135
+ (value.typ != EnumToken.ParensTokenType && funcLike.includes(value.typ))) &&
136
+ value.val.match(/^-([^-]+)-(.+)$/) != null) {
137
+ if (value.val.endsWith("-gradient")) {
138
+ if (regMatchLinearGradient.test(value.val)) {
139
+ this.webkitLinearToLinearGradient(value);
140
+ }
141
+ else if (regMatchRadialGradient.test(value.val)) {
142
+ this.webkitRadialToRadialGradient(value);
143
+ }
144
+ else if (equalsIgnoreCase(value.val, "-webkit-gradient")) {
145
+ this.webkitGradientToGradient(value);
146
+ }
106
147
  // not supported yet
107
148
  break;
108
149
  }
@@ -113,34 +154,355 @@ class ComputePrefixFeature {
113
154
  if (hasPrefix) {
114
155
  const nodes = structuredClone(node.val);
115
156
  for (const { value } of walkValues(nodes)) {
116
- if ((value.typ == EnumToken.IdenTokenType || funcLike.includes(value.typ))) {
157
+ if (value.typ == EnumToken.IdenTokenType || funcLike.includes(value.typ)) {
117
158
  const match = value.val.match(/^-([^-]+)-(.+)$/);
118
159
  if (match != null) {
119
160
  value.val = match[2];
120
161
  }
121
162
  }
122
163
  }
123
- // @ts-ignore
124
- if (SyntaxValidationResult.Valid == evaluateSyntax({ ...node, val: nodes }, {}).valid) {
125
- node.val = nodes;
126
- }
164
+ node.val = nodes;
127
165
  }
128
166
  }
129
167
  else if (node.typ == EnumToken.AtRuleNodeType || node.typ == EnumToken.KeyframesAtRuleNodeType) {
130
- if (node.nam.startsWith('-')) {
168
+ if (node.nam.startsWith("-")) {
131
169
  const match = node.nam.match(/^-([^-]+)-(.+)$/);
132
- if (match != null && '@' + match[2] in config.atRules) {
170
+ if (match != null && "@" + match[2] in config.atRules) {
133
171
  node.nam = match[2];
134
172
  }
135
173
  }
136
- if (node.typ == EnumToken.AtRuleNodeType && node.val !== '') {
174
+ if (node.typ == EnumToken.AtRuleNodeType && node.val !== "") {
137
175
  if (replaceAstNodes(node.tokens)) {
138
- node.val = node.tokens.reduce((acc, curr) => acc + renderToken(curr), '');
176
+ node.val = node.tokens.reduce((acc, curr) => acc + renderValue(curr), "");
139
177
  }
140
178
  }
141
179
  }
142
180
  return node;
143
181
  }
182
+ /**
183
+ * Convert -webkit-linear-gradient to linear-gradient syntax
184
+ * @param tokens
185
+ * @returns
186
+ */
187
+ webkitLinearToLinearGradient(token) {
188
+ let i;
189
+ let k;
190
+ let to;
191
+ let val;
192
+ let key;
193
+ const tokens = token.chi;
194
+ // conversion rules
195
+ // translation:
196
+ // - left -> to right
197
+ // - right -> to left
198
+ // - top -> to bottom
199
+ // - top right -> to bottom left
200
+ // - top left -> to bottom right
201
+ // - bottom -> to top
202
+ // - bottom right -> to top left
203
+ // - bottom left -> to top right
204
+ // https://drafts.csswg.org/css-images-3/#linear-gradients
205
+ // final angle:
206
+ // - to top -> 0deg
207
+ // - to right -> 90deg
208
+ // - to bottom -> 180deg
209
+ // - to left -> 270deg
210
+ for (i = 0; i < tokens.length; i++) {
211
+ if (tokens[i].typ == EnumToken.AngleTokenType ||
212
+ (tokens[i].typ == EnumToken.NumberTokenType && tokens[i].val == 0)) {
213
+ tokens[i].val =
214
+ (90 -
215
+ // @ts-expect-error
216
+ (tokens[i].typ == EnumToken.NumberTokenType
217
+ ? tokens[i]
218
+ : toDegrees(tokens[i]).val)) %
219
+ 360;
220
+ }
221
+ else if (tokens[i].typ == EnumToken.IdenTokenType) {
222
+ key = tokens[i].val.toLowerCase();
223
+ switch (key) {
224
+ case "right":
225
+ case "left":
226
+ tokens.splice(i, 1, { typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, {
227
+ typ: EnumToken.IdenTokenType,
228
+ val: key == "right" ? "left" : "right",
229
+ });
230
+ i += 2;
231
+ break;
232
+ case "top":
233
+ case "bottom":
234
+ k = i + 1;
235
+ to = key == "top" ? "bottom" : "top";
236
+ while (k < tokens.length &&
237
+ (tokens[k].typ === EnumToken.WhitespaceTokenType ||
238
+ tokens[k].typ === EnumToken.CommentTokenType)) {
239
+ k++;
240
+ }
241
+ if (tokens[k]?.typ === EnumToken.IdenTokenType) {
242
+ val = "";
243
+ if (equalsIgnoreCase(tokens[k].val, "left")) {
244
+ val = "right";
245
+ }
246
+ if (equalsIgnoreCase(tokens[k].val, "right")) {
247
+ val = "left";
248
+ }
249
+ if (val !== "") {
250
+ tokens.splice(i, k - i + 1, { typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: to }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val });
251
+ i += 4;
252
+ break;
253
+ }
254
+ }
255
+ tokens.splice(i, 1, { typ: EnumToken.IdenTokenType, val: "to" }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: to });
256
+ i += 2;
257
+ break;
258
+ }
259
+ }
260
+ }
261
+ token.val = equalsIgnoreCase(token.val, "-webkit-repeating-linear-gradient")
262
+ ? "repeating-linear-gradient"
263
+ : "linear-gradient";
264
+ }
265
+ /**
266
+ * Convert -webkit-gradient(linear) to linear-gradient or linear-gradient syntax
267
+ * @param token
268
+ * @returns
269
+ */
270
+ webkitGradientToGradient(token) {
271
+ let i = 0;
272
+ let k;
273
+ let key = "";
274
+ let commaCount;
275
+ let type = "";
276
+ let tokens = token.chi.slice();
277
+ while (i < tokens.length &&
278
+ (tokens[i].typ === EnumToken.WhitespaceTokenType || tokens[i].typ === EnumToken.CommentTokenType)) {
279
+ i++;
280
+ }
281
+ if (i >= tokens.length || tokens[i].typ !== EnumToken.IdenTokenType) {
282
+ return;
283
+ }
284
+ // linear or radial
285
+ if (equalsIgnoreCase(tokens[i].val, "linear")) {
286
+ type = "linear-gradient";
287
+ i++;
288
+ }
289
+ else {
290
+ return;
291
+ }
292
+ while (i < tokens.length &&
293
+ (tokens[i].typ === EnumToken.WhitespaceTokenType || tokens[i].typ === EnumToken.CommentTokenType)) {
294
+ i++;
295
+ }
296
+ if (tokens[i].typ !== EnumToken.CommaTokenType) {
297
+ return;
298
+ }
299
+ tokens.splice(0, i + 1);
300
+ commaCount = 0;
301
+ for (i = 0; i < tokens.length; i++) {
302
+ if (tokens[i].typ === EnumToken.CommaTokenType) {
303
+ commaCount++;
304
+ if (commaCount > 1) {
305
+ break;
306
+ }
307
+ }
308
+ if (tokens[i].typ === EnumToken.IdenTokenType) {
309
+ key += (key.length > 0 ? " " : "") + tokens[i].val;
310
+ }
311
+ }
312
+ // mapping keywords
313
+ // left top → left bottom to bottom
314
+ // left bottom → left top to top
315
+ // left top → right top to right
316
+ // right top → left top to left
317
+ // left top → right bottom to bottom right
318
+ // right top → left bottom to bottom left
319
+ // left bottom → right top to top right
320
+ // right bottom → left top to top left
321
+ const replacements = [];
322
+ if (key === "left top left bottom") {
323
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
324
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
325
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "bottom" });
326
+ }
327
+ else if (key === "left bottom left top") {
328
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
329
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
330
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "top" });
331
+ }
332
+ else if (key === "left top right top") {
333
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
334
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
335
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "right" });
336
+ }
337
+ else if (key === "right top left top") {
338
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
339
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
340
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "left" });
341
+ }
342
+ else if (key === "left top right bottom") {
343
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
344
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
345
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "bottom" });
346
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
347
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "right" });
348
+ }
349
+ else if (key === "right top left bottom") {
350
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
351
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
352
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "bottom" });
353
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
354
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "left" });
355
+ }
356
+ else if (key === "left bottom right top") {
357
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
358
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
359
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "top" });
360
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
361
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "right" });
362
+ }
363
+ else if (key === "right bottom left top") {
364
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "to" });
365
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
366
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "top" });
367
+ replacements.push({ typ: EnumToken.WhitespaceTokenType });
368
+ replacements.push({ typ: EnumToken.IdenTokenType, val: "left" });
369
+ }
370
+ tokens.splice(0, i, ...replacements);
371
+ let checkStop = true;
372
+ let checkStopIndex = replacements.length + 1;
373
+ let colorStop = [];
374
+ for (i = replacements.length + 1; i < tokens.length; i++) {
375
+ if (tokens[i].typ === EnumToken.CommaTokenType && checkStop) {
376
+ checkStopIndex = i;
377
+ continue;
378
+ }
379
+ if (tokens[i].typ === EnumToken.FunctionTokenType) {
380
+ if (equalsIgnoreCase(tokens[i].val, "to")) {
381
+ colorStop.push(tokens[checkStopIndex], ...tokens[i].chi);
382
+ tokens.splice(checkStopIndex, i - checkStopIndex + 1);
383
+ i = checkStopIndex;
384
+ checkStop = false;
385
+ }
386
+ else if (equalsIgnoreCase(tokens[i].val, "from")) {
387
+ k = tokens[i].chi.length + 1;
388
+ tokens.splice(i, 1, ...tokens[i].chi, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.PercentageTokenType, val: 0 });
389
+ i += k;
390
+ }
391
+ else if (equalsIgnoreCase(tokens[i].val, "color-stop")) {
392
+ k = tokens[i].chi.length - 1;
393
+ tokens.splice(i, 1,
394
+ // @ts-expect-error
395
+ ...tokens[i].chi.reverse().map((t) => t.typ === EnumToken.CommaTokenType
396
+ ? { typ: EnumToken.WhitespaceTokenType }
397
+ : t.typ === EnumToken.NumberTokenType
398
+ ? {
399
+ typ: EnumToken.PercentageTokenType,
400
+ // @ts-expect-error
401
+ val: t.val * 100,
402
+ }
403
+ : t));
404
+ i += k;
405
+ }
406
+ }
407
+ }
408
+ if (colorStop.length > 0) {
409
+ tokens.push(...colorStop);
410
+ }
411
+ if (type !== "") {
412
+ token.val = type;
413
+ token.chi.length = 0;
414
+ token.chi.push(...tokens);
415
+ }
416
+ }
417
+ /**
418
+ * Convert -webkit-radial-gradient to radial-gradient syntax
419
+ * @param tokens
420
+ * @returns
421
+ */
422
+ webkitRadialToRadialGradient(token) {
423
+ let i = 0;
424
+ const tokens = token.chi;
425
+ while (i < tokens.length &&
426
+ (tokens[i].typ === EnumToken.WhitespaceTokenType || tokens[i].typ === EnumToken.CommentTokenType)) {
427
+ i++;
428
+ }
429
+ const positions = [];
430
+ const form = [];
431
+ const size = [];
432
+ const colorStops = [];
433
+ if (tokens[i]?.typ === EnumToken.PercentageTokenType ||
434
+ (tokens[i]?.typ === EnumToken.NumberTokenType && 0 === tokens[i].val) ||
435
+ (tokens[i]?.typ === EnumToken.IdenTokenType &&
436
+ (equalsIgnoreCase(tokens[i].val, "center") ||
437
+ equalsIgnoreCase(tokens[i].val, "top") ||
438
+ equalsIgnoreCase(tokens[i].val, "bottom") ||
439
+ equalsIgnoreCase(tokens[i].val, "left") ||
440
+ equalsIgnoreCase(tokens[i].val, "right")))) {
441
+ do {
442
+ positions.push(tokens[i]);
443
+ i++;
444
+ } while (tokens[i]?.typ !== EnumToken.CommaTokenType);
445
+ if (tokens[i]?.typ === EnumToken.CommaTokenType) {
446
+ i++;
447
+ }
448
+ }
449
+ if (tokens[i]?.typ === EnumToken.IdenTokenType) {
450
+ if (equalsIgnoreCase(tokens[i].val, "closest-side") ||
451
+ equalsIgnoreCase(tokens[i].val, "closest-corner") ||
452
+ equalsIgnoreCase(tokens[i].val, "farthest-side") ||
453
+ equalsIgnoreCase(tokens[i].val, "farthest-corner")) {
454
+ size.push(tokens[i++]);
455
+ }
456
+ else if (equalsIgnoreCase(tokens[i].val, "circle") ||
457
+ equalsIgnoreCase(tokens[i].val, "ellipse")) {
458
+ form.push(tokens[i++]);
459
+ }
460
+ while (tokens[i]?.typ === EnumToken.WhitespaceTokenType || tokens[i]?.typ === EnumToken.CommentTokenType) {
461
+ i++;
462
+ }
463
+ if (tokens[i]?.typ === EnumToken.CommaTokenType) {
464
+ i++;
465
+ }
466
+ }
467
+ if (tokens[i]?.typ === EnumToken.IdenTokenType) {
468
+ if (equalsIgnoreCase(tokens[i].val, "closest-side") ||
469
+ equalsIgnoreCase(tokens[i].val, "closest-corner") ||
470
+ equalsIgnoreCase(tokens[i].val, "farthest-side") ||
471
+ equalsIgnoreCase(tokens[i].val, "farthest-corner")) {
472
+ size.push(tokens[i++]);
473
+ }
474
+ else if (equalsIgnoreCase(tokens[i].val, "circle") ||
475
+ equalsIgnoreCase(tokens[i].val, "ellipse")) {
476
+ form.push(tokens[i++]);
477
+ }
478
+ while (tokens[i]?.typ === EnumToken.WhitespaceTokenType || tokens[i]?.typ === EnumToken.CommentTokenType) {
479
+ i++;
480
+ }
481
+ if (tokens[i]?.typ === EnumToken.CommaTokenType) {
482
+ i++;
483
+ }
484
+ }
485
+ colorStops.push(...tokens.slice(i));
486
+ tokens.length = 0;
487
+ if (form.length > 0 || size.length > 0) {
488
+ if (form.length === 0) {
489
+ form.push({ typ: EnumToken.IdenTokenType, val: "ellipse" });
490
+ }
491
+ if (size.length > 0) {
492
+ form.push({ typ: EnumToken.WhitespaceTokenType });
493
+ form.push(...size);
494
+ }
495
+ if (positions.length > 0) {
496
+ form.push({ typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }, ...positions);
497
+ }
498
+ tokens.push(...form, { typ: EnumToken.CommaTokenType });
499
+ }
500
+ token.val = equalsIgnoreCase(token.val, "-webkit-repeating-radial-gradient")
501
+ ? "repeating-radial-gradient"
502
+ : "radial-gradient";
503
+ tokens.push(...colorStops);
504
+ return tokens;
505
+ }
144
506
  }
145
507
 
146
508
  export { ComputePrefixFeature };
@@ -1,16 +1,13 @@
1
1
  import { PropertyList } from '../../parser/declaration/list.js';
2
2
  import { EnumToken } from '../types.js';
3
- import '../minify.js';
4
- import '../walk.js';
5
- import '../../parser/parse.js';
6
- import '../../parser/tokenize.js';
7
- import '../../parser/utils/config.js';
8
- import '../../syntax/color/utils/constants.js';
9
- import '../../renderer/sourcemap/lib/encode.js';
10
3
  import { FeatureWalkMode } from './type.js';
11
4
 
12
5
  class ComputeShorthandFeature {
13
- accept = new Set([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType, EnumToken.KeyFramesRuleNodeType]);
6
+ accept = new Set([
7
+ EnumToken.RuleNodeType,
8
+ EnumToken.AtRuleNodeType,
9
+ EnumToken.KeyFramesRuleNodeType,
10
+ ]);
14
11
  get ordering() {
15
12
  return 3;
16
13
  }
@@ -24,7 +21,7 @@ class ComputeShorthandFeature {
24
21
  }
25
22
  }
26
23
  run(ast, options = {}, parent, context) {
27
- if (!('chi' in ast)) {
24
+ if (!("chi" in ast)) {
28
25
  return null;
29
26
  }
30
27
  // @ts-ignore
@@ -1,14 +1,7 @@
1
- import { EnumToken } from '../types.js';
1
+ import { EnumToken, EnumAstNodeStatus } from '../types.js';
2
2
  import { consumeWhitespace } from '../../validation/utils/whitespace.js';
3
- import '../minify.js';
4
- import '../walk.js';
5
- import '../../parser/parse.js';
6
- import '../../parser/tokenize.js';
7
- import '../../parser/utils/config.js';
8
- import '../../syntax/color/utils/constants.js';
9
- import { filterValues, renderToken } from '../../renderer/render.js';
10
- import '../../renderer/sourcemap/lib/encode.js';
11
3
  import { compute } from '../transform/compute.js';
4
+ import { filterValues, renderValue } from '../../renderer/render.js';
12
5
  import { minifyTransformFunctions, eqMatrix } from '../transform/minify.js';
13
6
  import { FeatureWalkMode } from './type.js';
14
7
 
@@ -28,7 +21,7 @@ class TransformCssFeature {
28
21
  }
29
22
  }
30
23
  run(ast) {
31
- if (!('chi' in ast)) {
24
+ if (!("chi" in ast)) {
32
25
  return null;
33
26
  }
34
27
  let i = 0;
@@ -37,18 +30,24 @@ class TransformCssFeature {
37
30
  for (; i < ast.chi.length; i++) {
38
31
  // @ts-ignore
39
32
  node = ast.chi[i];
40
- if (node.typ != EnumToken.DeclarationNodeType || !node.nam.match(/^(-[a-z]+-)?transform$/)) {
33
+ if (node.state == EnumAstNodeStatus.Invalid || node.state == EnumAstNodeStatus.ValidationFailed) {
34
+ continue;
35
+ }
36
+ if (node.typ != EnumToken.DeclarationNodeType ||
37
+ !node.nam.match(/^(-[a-z]+-)?transform$/)) {
41
38
  continue;
42
39
  }
43
40
  const children = [];
44
41
  for (const child of node.val) {
45
- children.push(child.typ == EnumToken.FunctionTokenType ? minifyTransformFunctions(child) : child);
42
+ children.push(child.typ == EnumToken.TransformFunctionTokenType
43
+ ? minifyTransformFunctions(child)
44
+ : child);
46
45
  }
47
46
  consumeWhitespace(children);
48
47
  let { matrix, cumulative, minified } = compute(children) ?? {
49
48
  matrix: null,
50
49
  cumulative: null,
51
- minified: null
50
+ minified: null,
52
51
  };
53
52
  if (matrix == null || cumulative == null || minified == null) {
54
53
  node.val = children;
@@ -61,9 +60,9 @@ class TransformCssFeature {
61
60
  if (eqMatrix(matrix, minified)) {
62
61
  r.push(minified);
63
62
  }
64
- const l = renderToken(matrix).length;
63
+ const l = renderValue(matrix).length;
65
64
  node.val = r.reduce((acc, curr) => {
66
- if (curr.reduce((acc, t) => acc + renderToken(t), '').length < l) {
65
+ if (curr.reduce((acc, t) => acc + renderValue(t), "").length < l) {
67
66
  return curr;
68
67
  }
69
68
  return acc;