@tbela99/css-parser 1.4.3 → 1.4.5

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 (166) hide show
  1. package/CHANGELOG.md +196 -2
  2. package/README.md +68 -1120
  3. package/dist/config.json.js +43 -2
  4. package/dist/index-umd-web.js +18118 -15886
  5. package/dist/index.cjs +16838 -14611
  6. package/dist/index.d.ts +1804 -1684
  7. package/dist/lib/ast/clone.js +4 -3
  8. package/dist/lib/ast/expand.js +2 -2
  9. package/dist/lib/ast/features/calc.js +59 -54
  10. package/dist/lib/ast/features/if.js +64 -17
  11. package/dist/lib/ast/features/inlinecssvariables.js +2 -2
  12. package/dist/lib/ast/features/prefix.js +343 -6
  13. package/dist/lib/ast/features/transform.js +7 -4
  14. package/dist/lib/ast/find.js +5 -5
  15. package/dist/lib/ast/math/expression.js +12 -8
  16. package/dist/lib/ast/minify.js +28 -33
  17. package/dist/lib/ast/transform/minify.js +135 -131
  18. package/dist/lib/ast/transform/utils.js +3 -2
  19. package/dist/lib/ast/types.js +69 -8
  20. package/dist/lib/ast/walk.js +4 -5
  21. package/dist/lib/fs/resolve.js +71 -57
  22. package/dist/lib/parser/declaration/list.js +92 -3
  23. package/dist/lib/parser/declaration/map.js +5 -5
  24. package/dist/lib/parser/parse.js +530 -466
  25. package/dist/lib/parser/tokenize.js +110 -4
  26. package/dist/lib/parser/utils/angle.js +29 -0
  27. package/dist/lib/parser/utils/at-rule-container.js +2 -2
  28. package/dist/lib/parser/utils/at-rule-font-feature-values.js +2 -2
  29. package/dist/lib/parser/utils/at-rule-generic.js +1 -1
  30. package/dist/lib/parser/utils/at-rule-media.js +2 -2
  31. package/dist/lib/parser/utils/at-rule-support.js +73 -8
  32. package/dist/lib/parser/utils/at-rule-when-else.js +2 -2
  33. package/dist/lib/parser/utils/at-rule.js +2 -8
  34. package/dist/lib/parser/utils/declaration.js +179 -76
  35. package/dist/lib/parser/utils/selector.js +53 -11
  36. package/dist/lib/parser/utils/token.js +13 -11
  37. package/dist/lib/renderer/render.js +501 -59
  38. package/dist/lib/syntax/color/alpha.js +64 -0
  39. package/dist/lib/syntax/color/cmyk.js +2 -2
  40. package/dist/lib/syntax/color/color-mix.js +283 -220
  41. package/dist/lib/syntax/color/color.js +37 -28
  42. package/dist/lib/syntax/color/hex.js +2 -2
  43. package/dist/lib/syntax/color/hsl.js +3 -3
  44. package/dist/lib/syntax/color/hwb.js +7 -7
  45. package/dist/lib/syntax/color/lab.js +5 -5
  46. package/dist/lib/syntax/color/lch.js +4 -4
  47. package/dist/lib/syntax/color/oklab.js +3 -3
  48. package/dist/lib/syntax/color/oklch.js +3 -3
  49. package/dist/lib/syntax/color/relativecolor.js +97 -12
  50. package/dist/lib/syntax/color/srgb.js +5 -5
  51. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  52. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  53. package/dist/lib/syntax/color/utils/components.js +7 -3
  54. package/dist/lib/syntax/color/utils/distance.js +21 -4
  55. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  56. package/dist/lib/syntax/color/xyz.js +2 -2
  57. package/dist/lib/syntax/constants.js +29 -1
  58. package/dist/lib/syntax/syntax.js +307 -39
  59. package/dist/lib/validation/config.js +2 -2
  60. package/dist/lib/validation/config.json.js +32 -17
  61. package/dist/lib/validation/match.js +239 -104
  62. package/dist/lib/validation/parser/parse.js +0 -1
  63. package/dist/lib/validation/parser/typedef.js +6 -0
  64. package/dist/node.js +64 -22
  65. package/dist/web.js +65 -18
  66. package/package.json +26 -15
  67. package/.nyc_output/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +0 -1
  68. package/.nyc_output/6193bc4c-6f5f-4898-8950-c628825e6342.json +0 -1
  69. package/.nyc_output/processinfo/4874b20e-6f53-4d7a-be5a-cf68316925f2.json +0 -1
  70. package/.nyc_output/processinfo/6193bc4c-6f5f-4898-8950-c628825e6342.json +0 -1
  71. package/.repl_history +0 -4
  72. package/badges/coverage.svg +0 -20
  73. package/deno.lock +0 -2861
  74. package/dist/lib/ast/clone.d.ts +0 -10
  75. package/dist/lib/ast/expand.d.ts +0 -14
  76. package/dist/lib/ast/features/calc.d.ts +0 -10
  77. package/dist/lib/ast/features/if.d.ts +0 -10
  78. package/dist/lib/ast/features/index.d.ts +0 -6
  79. package/dist/lib/ast/features/inlinecssvariables.d.ts +0 -15
  80. package/dist/lib/ast/features/prefix.d.ts +0 -8
  81. package/dist/lib/ast/features/shorthand.d.ts +0 -12
  82. package/dist/lib/ast/features/transform.d.ts +0 -10
  83. package/dist/lib/ast/features/type.d.ts +0 -15
  84. package/dist/lib/ast/find.d.ts +0 -165
  85. package/dist/lib/ast/math/expression.d.ts +0 -18
  86. package/dist/lib/ast/math/math.d.ts +0 -6
  87. package/dist/lib/ast/minify.d.ts +0 -19
  88. package/dist/lib/ast/transform/compute.d.ts +0 -8
  89. package/dist/lib/ast/transform/matrix.d.ts +0 -22
  90. package/dist/lib/ast/transform/minify.d.ts +0 -5
  91. package/dist/lib/ast/transform/perspective.d.ts +0 -3
  92. package/dist/lib/ast/transform/rotate.d.ts +0 -12
  93. package/dist/lib/ast/transform/scale.d.ts +0 -6
  94. package/dist/lib/ast/transform/skew.d.ts +0 -4
  95. package/dist/lib/ast/transform/translate.d.ts +0 -6
  96. package/dist/lib/ast/transform/utils.d.ts +0 -9
  97. package/dist/lib/ast/types.d.ts +0 -903
  98. package/dist/lib/ast/walk.d.ts +0 -162
  99. package/dist/lib/fs/resolve.d.ts +0 -20
  100. package/dist/lib/parser/declaration/list.d.ts +0 -16
  101. package/dist/lib/parser/declaration/map.d.ts +0 -15
  102. package/dist/lib/parser/declaration/set.d.ts +0 -9
  103. package/dist/lib/parser/node.d.ts +0 -7
  104. package/dist/lib/parser/parse.d.ts +0 -107
  105. package/dist/lib/parser/tokenize.d.ts +0 -57
  106. package/dist/lib/parser/utils/at-rule-container.d.ts +0 -5
  107. package/dist/lib/parser/utils/at-rule-font-feature-values.d.ts +0 -5
  108. package/dist/lib/parser/utils/at-rule-generic.d.ts +0 -5
  109. package/dist/lib/parser/utils/at-rule-import.d.ts +0 -5
  110. package/dist/lib/parser/utils/at-rule-media.d.ts +0 -5
  111. package/dist/lib/parser/utils/at-rule-page.d.ts +0 -5
  112. package/dist/lib/parser/utils/at-rule-page.js +0 -28
  113. package/dist/lib/parser/utils/at-rule-support.d.ts +0 -5
  114. package/dist/lib/parser/utils/at-rule-token.d.ts +0 -1
  115. package/dist/lib/parser/utils/at-rule-when-else.d.ts +0 -5
  116. package/dist/lib/parser/utils/at-rule.d.ts +0 -13
  117. package/dist/lib/parser/utils/cache.d.ts +0 -6
  118. package/dist/lib/parser/utils/config.d.ts +0 -2
  119. package/dist/lib/parser/utils/declaration-list.d.ts +0 -5
  120. package/dist/lib/parser/utils/declaration.d.ts +0 -18
  121. package/dist/lib/parser/utils/eq.d.ts +0 -1
  122. package/dist/lib/parser/utils/hash.d.ts +0 -21
  123. package/dist/lib/parser/utils/selector.d.ts +0 -5
  124. package/dist/lib/parser/utils/text.d.ts +0 -3
  125. package/dist/lib/parser/utils/token.d.ts +0 -14
  126. package/dist/lib/parser/utils/type.d.ts +0 -2
  127. package/dist/lib/renderer/render.d.ts +0 -28
  128. package/dist/lib/renderer/sourcemap/lib/encode.d.ts +0 -1
  129. package/dist/lib/renderer/sourcemap/sourcemap.d.ts +0 -26
  130. package/dist/lib/syntax/color/a98rgb.d.ts +0 -2
  131. package/dist/lib/syntax/color/cmyk.d.ts +0 -10
  132. package/dist/lib/syntax/color/color-mix.d.ts +0 -2
  133. package/dist/lib/syntax/color/color.d.ts +0 -42
  134. package/dist/lib/syntax/color/hex.d.ts +0 -16
  135. package/dist/lib/syntax/color/hsl.d.ts +0 -20
  136. package/dist/lib/syntax/color/hsv.d.ts +0 -2
  137. package/dist/lib/syntax/color/hwb.d.ts +0 -21
  138. package/dist/lib/syntax/color/lab.d.ts +0 -25
  139. package/dist/lib/syntax/color/lch.d.ts +0 -23
  140. package/dist/lib/syntax/color/oklab.d.ts +0 -22
  141. package/dist/lib/syntax/color/oklch.d.ts +0 -20
  142. package/dist/lib/syntax/color/p3.d.ts +0 -6
  143. package/dist/lib/syntax/color/prophotorgb.d.ts +0 -2
  144. package/dist/lib/syntax/color/rec2020.d.ts +0 -2
  145. package/dist/lib/syntax/color/relativecolor.d.ts +0 -13
  146. package/dist/lib/syntax/color/rgb.d.ts +0 -20
  147. package/dist/lib/syntax/color/srgb.d.ts +0 -23
  148. package/dist/lib/syntax/color/utils/components.d.ts +0 -2
  149. package/dist/lib/syntax/color/utils/distance.d.ts +0 -18
  150. package/dist/lib/syntax/color/utils/matrix.d.ts +0 -6
  151. package/dist/lib/syntax/color/xyz.d.ts +0 -5
  152. package/dist/lib/syntax/color/xyzd50.d.ts +0 -4
  153. package/dist/lib/syntax/constants.d.ts +0 -67
  154. package/dist/lib/syntax/syntax.d.ts +0 -38
  155. package/dist/lib/validation/config.d.ts +0 -14
  156. package/dist/lib/validation/json.d.ts +0 -2
  157. package/dist/lib/validation/match.d.ts +0 -38
  158. package/dist/lib/validation/parser/parse.d.ts +0 -8
  159. package/dist/lib/validation/parser/typedef.d.ts +0 -95
  160. package/dist/lib/validation/utils/list.d.ts +0 -4
  161. package/dist/lib/validation/utils/whitespace.d.ts +0 -2
  162. package/dist/node.d.ts +0 -207
  163. package/dist/web.d.ts +0 -169
  164. package/playground/index.html +0 -1328
  165. package/playground/sw.js +0 -55
  166. package/playground/tree.js +0 -176
@@ -2,12 +2,14 @@ import { EnumToken } from '../types.js';
2
2
  import { walkValues } from '../walk.js';
3
3
  import { pseudoAliasMap } from '../../syntax/syntax.js';
4
4
  import { splitRule } from '../minify.js';
5
- import { renderToken } from '../../renderer/render.js';
6
- import { funcLike } from '../../syntax/constants.js';
5
+ import { renderValue } from '../../renderer/render.js';
6
+ import { funcLike, regMatchLinearGradient, regMatchRadialGradient } from '../../syntax/constants.js';
7
7
  import { FeatureWalkMode } from './type.js';
8
8
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
9
9
  import { getSyntaxConfig } from '../../validation/config.js';
10
10
  import { splitTokenList } from '../../validation/utils/list.js';
11
+ import { equalsIgnoreCase } from '../../parser/utils/text.js';
12
+ import { toDegrees } from '../../parser/utils/angle.js';
11
13
 
12
14
  const config = getSyntaxConfig();
13
15
  function replacePseudo(tokens) {
@@ -33,7 +35,9 @@ function replaceAstNodes(tokens, root) {
33
35
  if (token != null) {
34
36
  if (token.val in pseudoAliasMap) {
35
37
  token.val = pseudoAliasMap[token.val];
36
- if (["min-resolution", "max-resolution"].includes(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) &&
37
41
  value.r?.[0]?.typ == EnumToken.NumberTokenType) {
38
42
  Object.assign(value.r?.[0], {
39
43
  typ: EnumToken.ResolutionTokenType,
@@ -75,13 +79,13 @@ function replaceAstNodes(tokens, root) {
75
79
  const split = splitTokenList(tokens, [EnumToken.CommaTokenType]);
76
80
  tokens.length = 0;
77
81
  tokens.push(...split.reduce((acc, curr) => {
78
- const str = curr.reduce((acc, curr) => acc + renderToken(curr), "");
82
+ const str = curr.reduce((acc, curr) => acc + renderValue(curr), "");
79
83
  if (set.has(str)) {
80
84
  return acc;
81
85
  }
82
86
  set.add(str);
83
87
  if (acc.length > 0) {
84
- tokens.push({
88
+ acc.push({
85
89
  typ: EnumToken.CommaTokenType,
86
90
  });
87
91
  }
@@ -131,6 +135,15 @@ class ComputePrefixFeature {
131
135
  (value.typ != EnumToken.ParensTokenType && funcLike.includes(value.typ))) &&
132
136
  value.val.match(/^-([^-]+)-(.+)$/) != null) {
133
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
+ }
134
147
  // not supported yet
135
148
  break;
136
149
  }
@@ -160,12 +173,336 @@ class ComputePrefixFeature {
160
173
  }
161
174
  if (node.typ == EnumToken.AtRuleNodeType && node.val !== "") {
162
175
  if (replaceAstNodes(node.tokens)) {
163
- node.val = node.tokens.reduce((acc, curr) => acc + renderToken(curr), "");
176
+ node.val = node.tokens.reduce((acc, curr) => acc + renderValue(curr), "");
164
177
  }
165
178
  }
166
179
  }
167
180
  return node;
168
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
+ }
169
506
  }
170
507
 
171
508
  export { ComputePrefixFeature };
@@ -1,7 +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
3
  import { compute } from '../transform/compute.js';
4
- import { filterValues, renderToken } from '../../renderer/render.js';
4
+ import { filterValues, renderValue } from '../../renderer/render.js';
5
5
  import { minifyTransformFunctions, eqMatrix } from '../transform/minify.js';
6
6
  import { FeatureWalkMode } from './type.js';
7
7
 
@@ -30,6 +30,9 @@ class TransformCssFeature {
30
30
  for (; i < ast.chi.length; i++) {
31
31
  // @ts-ignore
32
32
  node = ast.chi[i];
33
+ if (node.state == EnumAstNodeStatus.Invalid || node.state == EnumAstNodeStatus.ValidationFailed) {
34
+ continue;
35
+ }
33
36
  if (node.typ != EnumToken.DeclarationNodeType ||
34
37
  !node.nam.match(/^(-[a-z]+-)?transform$/)) {
35
38
  continue;
@@ -57,9 +60,9 @@ class TransformCssFeature {
57
60
  if (eqMatrix(matrix, minified)) {
58
61
  r.push(minified);
59
62
  }
60
- const l = renderToken(matrix).length;
63
+ const l = renderValue(matrix).length;
61
64
  node.val = r.reduce((acc, curr) => {
62
- if (curr.reduce((acc, t) => acc + renderToken(t), "").length < l) {
65
+ if (curr.reduce((acc, t) => acc + renderValue(t), "").length < l) {
63
66
  return curr;
64
67
  }
65
68
  return acc;
@@ -2,7 +2,7 @@ import { EnumToken } from './types.js';
2
2
  import { walk, walkValues } from './walk.js';
3
3
 
4
4
  /**
5
- * search the ast tree and return the first match
5
+ * Search the ast tree and return the first match
6
6
  *
7
7
  * ```ts
8
8
  * // find the first ast declaration node which name is 'aspect-ratio'
@@ -41,7 +41,7 @@ function find(ast, matcher) {
41
41
  return null;
42
42
  }
43
43
  /**
44
- * search the ast tree by checking each node's value and return the first match
44
+ * Search the ast tree by checking each node's value token and return the first match
45
45
  *
46
46
  * ```ts
47
47
  * // find the first ast node which contains the length token '30px'
@@ -56,7 +56,7 @@ button {
56
56
  }
57
57
  `;
58
58
 
59
- // find declaration which contain a '30px'
59
+ // find declaration which contain the length token '30px'
60
60
  const nodeMatcher = (value: Token) =>
61
61
  return value.typ == EnumToken.LengthTokenType && (value as LengthToken).val == 30 && (value as LengthToken).unit == 'px' ;
62
62
 
@@ -93,7 +93,7 @@ function findByValue(ast, matcher) {
93
93
  return null;
94
94
  }
95
95
  /**
96
- * search the ast tree and return all matches
96
+ * Search the ast tree and return all matches
97
97
  *
98
98
  * ```ts
99
99
  * // find the first ast declaration node which name is 'aspect-ratio'
@@ -133,7 +133,7 @@ function findAll(ast, matcher) {
133
133
  return result;
134
134
  }
135
135
  /**
136
- * search the ast tree and return the last match
136
+ * Search the ast tree and return the last match.
137
137
  *
138
138
  * ```ts
139
139
  * // find the first ast declaration node which name is 'aspect-ratio'
@@ -134,7 +134,7 @@ function doEvaluate(l, r, op) {
134
134
  return defaultReturn;
135
135
  }
136
136
  }
137
- let typ = l.typ == EnumToken.NumberTokenType
137
+ let typ = l.typ == EnumToken.NumberTokenType || l.typ === EnumToken.IdenTokenType
138
138
  ? r.typ
139
139
  : r.typ == EnumToken.NumberTokenType
140
140
  ? l.typ
@@ -143,9 +143,10 @@ function doEvaluate(l, r, op) {
143
143
  : l.typ;
144
144
  // @ts-expect-error
145
145
  let v1 = l.val?.typ == EnumToken.FractionTokenType ? l.val : getValue(l);
146
- let v2 =
147
- // @ts-expect-error
148
- r.val?.typ == EnumToken.FractionTokenType ? r.val : getValue(r);
146
+ let v2 = r.val?.typ == EnumToken.FractionTokenType
147
+ ? // @ts-expect-error
148
+ r.val
149
+ : getValue(r);
149
150
  if (op == EnumToken.Mul) {
150
151
  if (l.typ != EnumToken.NumberTokenType && r.typ != EnumToken.NumberTokenType) {
151
152
  if (typeof v1 == "number" && l.typ == EnumToken.PercentageTokenType) {
@@ -167,7 +168,7 @@ function doEvaluate(l, r, op) {
167
168
  // @ts-ignore
168
169
  const val = compute(v1, v2, op);
169
170
  const token = {
170
- ...(l.typ == EnumToken.NumberTokenType ? r : l),
171
+ ...(l.typ === EnumToken.NumberTokenType || l.typ === EnumToken.IdenTokenType ? r : l),
171
172
  typ,
172
173
  val /* : typeof val == 'number' ? minifyNumber(val) : val */,
173
174
  };
@@ -202,8 +203,8 @@ function evaluateFunc(token) {
202
203
  // @ts-ignore
203
204
  let val = value[0].typ == EnumToken.NumberTokenType
204
205
  ? +value[0].val
205
- // @ts-expect-error
206
- : value[0].l.val / value[0].r.val;
206
+ : // @ts-expect-error
207
+ value[0].l.val / value[0].r.val;
207
208
  return [
208
209
  {
209
210
  typ: EnumToken.NumberTokenType,
@@ -451,12 +452,15 @@ function factor(tokens, ops) {
451
452
  }
452
453
  isOp = opList.includes(tokens[i].typ === EnumToken.Plus ? EnumToken.Add : tokens[i].typ);
453
454
  if (isOp ||
455
+ tokens[i].typ === EnumToken.Star ||
454
456
  // @ts-ignore
455
457
  (tokens[i].typ == EnumToken.LiteralTokenType && ops.includes(tokens[i].val))) {
456
458
  tokens.splice(i - 1, 3, {
457
459
  typ: EnumToken.BinaryExpressionTokenType,
458
460
  op: isOp
459
- ? (tokens[i].typ === EnumToken.Plus ? EnumToken.Add : tokens[i].typ)
461
+ ? tokens[i].typ === EnumToken.Plus
462
+ ? EnumToken.Add
463
+ : tokens[i].typ
460
464
  : getArithmeticOperation(tokens[i].val),
461
465
  l: factorToken(tokens[i - 1]),
462
466
  r: factorToken(tokens[i + 1]),