@tbela99/css-parser 1.4.3 → 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 (166) hide show
  1. package/CHANGELOG.md +192 -2
  2. package/README.md +69 -1120
  3. package/dist/config.json.js +43 -2
  4. package/dist/index-umd-web.js +17226 -15037
  5. package/dist/index.cjs +16889 -14705
  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 +22 -16
  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 +528 -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
@@ -4,8 +4,10 @@ import { EnumToken, minifyNumber, ColorType } from '../ast/types.js';
4
4
  import { expand } from '../ast/expand.js';
5
5
  import { SourceMap } from './sourcemap/sourcemap.js';
6
6
  import { colorsFunc, urlTokenMatcher, tokensfuncSet } from '../syntax/constants.js';
7
- import { isColor, pseudoElements, parseColor } from '../syntax/syntax.js';
7
+ import { isColor, pseudoElements, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor } from '../syntax/syntax.js';
8
8
  import { move } from '../parser/tokenize.js';
9
+ import { equalsIgnoreCase } from '../parser/utils/text.js';
10
+ import { toDegrees } from '../parser/utils/angle.js';
9
11
 
10
12
  /**
11
13
  * render ast
@@ -88,7 +90,7 @@ function doRender(data, options = {}, mapping) {
88
90
  }
89
91
  return acc + curr.val;
90
92
  }
91
- return acc + renderToken(curr, options, cache, reducer, errors);
93
+ return acc + renderValue(curr, options, cache, reducer, errors);
92
94
  }, cache),
93
95
  errors,
94
96
  stats: {
@@ -131,7 +133,7 @@ function updateSourceMap(node, options, cache, sourcemap, position, str) {
131
133
  { src, sta: { ...position } }, {
132
134
  ...node.loc,
133
135
  // @ts-ignore
134
- src
136
+ src,
135
137
  });
136
138
  }
137
139
  move(position, str);
@@ -264,9 +266,9 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
264
266
  .trim()};`;
265
267
  case EnumToken.CssVariableDeclarationMapTokenType:
266
268
  return `@value ${filterValues(data.vars)
267
- .reduce((acc, curr) => acc + renderToken(curr), "")
269
+ .reduce((acc, curr) => acc + renderValue(curr), "")
268
270
  .trim()} from ${filterValues(data.from)
269
- .reduce((acc, curr) => acc + renderToken(curr), "")
271
+ .reduce((acc, curr) => acc + renderValue(curr), "")
270
272
  .trim()};`;
271
273
  case EnumToken.InvalidDeclarationNodeType:
272
274
  case EnumToken.InvalidRuleNodeType:
@@ -281,7 +283,12 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
281
283
  * @param options
282
284
  * @private
283
285
  */
284
- function renderToken(token, options = {}, cache = Object.create(null), reducer, errors) {
286
+ function renderValue(token, options = {}, cache = Object.create(null), reducer, errors) {
287
+ if (token.typ === EnumToken.WhenElseFunctionTokenType &&
288
+ equalsIgnoreCase(token.val, "supports")) {
289
+ options = { ...options, minify: false, convertColor: false };
290
+ reducer = null;
291
+ }
285
292
  if (reducer == null) {
286
293
  reducer = function (acc, curr) {
287
294
  if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
@@ -290,7 +297,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
290
297
  }
291
298
  return acc + curr.val;
292
299
  }
293
- return acc + renderToken(curr, options, cache, reducer, errors);
300
+ return acc + renderValue(curr, options, cache, reducer, errors);
294
301
  };
295
302
  }
296
303
  if (token.typ == EnumToken.FunctionTokenType && colorsFunc.includes(token.val)) {
@@ -348,33 +355,33 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
348
355
  case EnumToken.WildCardFunctionTokenDefType:
349
356
  return token.val + "(";
350
357
  case EnumToken.ListToken:
351
- return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), "");
358
+ return token.chi.reduce((acc, curr) => acc + renderValue(curr, options, cache), "");
352
359
  case EnumToken.BinaryExpressionTokenType:
353
360
  if (EnumToken.Mul === token.op ||
354
361
  EnumToken.Div === token.op) {
355
362
  let result = "";
356
363
  if (token.l.typ == EnumToken.BinaryExpressionTokenType &&
357
364
  [EnumToken.Add, EnumToken.Sub].includes(token.l.op)) {
358
- result = "(" + renderToken(token.l, options, cache) + ")";
365
+ result = "(" + renderValue(token.l, options, cache) + ")";
359
366
  }
360
367
  else {
361
- result = renderToken(token.l, options, cache);
368
+ result = renderValue(token.l, options, cache);
362
369
  }
363
370
  result += token.op == EnumToken.Mul ? "*" : "/";
364
371
  if (token.r.typ == EnumToken.BinaryExpressionTokenType &&
365
372
  [EnumToken.Add, EnumToken.Sub].includes(token.r.op)) {
366
- result += "(" + renderToken(token.r, options, cache) + ")";
373
+ result += "(" + renderValue(token.r, options, cache) + ")";
367
374
  }
368
375
  else {
369
- result += renderToken(token.r, options, cache);
376
+ result += renderValue(token.r, options, cache);
370
377
  }
371
378
  return result;
372
379
  }
373
- return (renderToken(token.l, options, cache) +
380
+ return (renderValue(token.l, options, cache) +
374
381
  (token.op == EnumToken.Add ? " + " : " - ") +
375
- renderToken(token.r, options, cache));
382
+ renderValue(token.r, options, cache));
376
383
  case EnumToken.FractionTokenType:
377
- const fraction = renderToken(token.l) + "/" + renderToken(token.r);
384
+ const fraction = renderValue(token.l) + "/" + renderValue(token.r);
378
385
  if (+token.r.val != 0) {
379
386
  const value = minifyNumber(+token.l.val / +token.r.val);
380
387
  if (value.length <= fraction.length) {
@@ -401,12 +408,12 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
401
408
  if (curr.typ === EnumToken.IdenTokenType) {
402
409
  parseColor(curr);
403
410
  }
404
- return acc + renderToken(curr, options, cache);
411
+ return acc + renderValue(curr, options, cache);
405
412
  }, "") +
406
413
  ")");
407
414
  }
408
415
  if (options.convertColor !== false) {
409
- const value = convertColor(token, typeof options.convertColor == "boolean"
416
+ const value = convertColor(token, typeof options.convertColor === "boolean"
410
417
  ? ColorType.HEX
411
418
  : (ColorType[ColorType[options.convertColor ?? "HEX"]
412
419
  ?.toUpperCase?.()
@@ -427,13 +434,22 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
427
434
  return token.val;
428
435
  }
429
436
  if (Array.isArray(token.chi)) {
437
+ const fnName = token.val.toLowerCase();
430
438
  const isLegacy = ["rgb", "rgba", "hsl", "hsla"].includes(token.val.toLowerCase());
431
- const useAlpha = (["rgb", "rgba", "hsl", "hsla", "hwb", "oklab", "oklch", "lab", "lch"].includes(token.val.toLowerCase()) &&
432
- token.chi.length == 4) ||
433
- ("color" == token.val.toLowerCase() && token.chi.length == 5);
434
- return ((token.val.endsWith("a")
435
- ? token.val.slice(0, -1)
436
- : token.val) +
439
+ const hasAlpha = [
440
+ "rgb",
441
+ "rgba",
442
+ "hsl",
443
+ "hsla",
444
+ "hwb",
445
+ "oklab",
446
+ "oklch",
447
+ "lab",
448
+ "lch",
449
+ ].includes(fnName);
450
+ const useAlpha = (hasAlpha && token.chi.length == (isLegacy ? 7 : 4)) ||
451
+ ("color" == token.val && token.chi.length == 5);
452
+ return ((hasAlpha && token.val.endsWith("a") ? fnName.slice(0, -1) : fnName) +
437
453
  "(" +
438
454
  token
439
455
  .chi.reduce((acc, curr, index, array) => {
@@ -441,7 +457,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
441
457
  if (curr.typ == EnumToken.WhitespaceTokenType) {
442
458
  return acc.trimEnd();
443
459
  }
444
- return acc.trimStart() + renderToken(curr, options, cache);
460
+ return acc.trimStart() + renderValue(curr, options, cache);
445
461
  }
446
462
  if (isLegacy && curr.typ == EnumToken.CommaTokenType) {
447
463
  return acc.trimEnd() + " ";
@@ -455,7 +471,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
455
471
  }
456
472
  return (acc.trimEnd() +
457
473
  (useAlpha && index == array.length - 1 ? "/" : " ") +
458
- renderToken(curr, options, cache));
474
+ renderValue(curr, options, cache));
459
475
  }, "")
460
476
  .trimStart() +
461
477
  ")");
@@ -479,6 +495,431 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
479
495
  case EnumToken.FunctionTokenType:
480
496
  case EnumToken.MathFunctionTokenType:
481
497
  case EnumToken.ImageFunctionTokenType:
498
+ if (options.minify && token.typ === EnumToken.ImageFunctionTokenType) {
499
+ const slice = token.chi.slice();
500
+ switch (token.val) {
501
+ case "linear-gradient":
502
+ case "repeating-linear-gradient":
503
+ {
504
+ let i;
505
+ let k;
506
+ let j;
507
+ let key;
508
+ for (i = 0; i < slice.length; i++) {
509
+ if (slice[i].typ == EnumToken.ColorTokenType ||
510
+ slice[i].typ === EnumToken.CommaTokenType) {
511
+ break;
512
+ }
513
+ if (slice[i].typ == EnumToken.IdenTokenType &&
514
+ equalsIgnoreCase(slice[i].val, "to")) {
515
+ k = i + 1;
516
+ while (k < slice.length &&
517
+ (slice[k].typ === EnumToken.WhitespaceTokenType ||
518
+ slice[k].typ === EnumToken.CommentTokenType)) {
519
+ k++;
520
+ }
521
+ if (k < slice.length && slice[k].typ === EnumToken.IdenTokenType) {
522
+ // convert keyword to angle
523
+ key = slice[k].val.toLowerCase();
524
+ switch (key) {
525
+ case "right":
526
+ slice.splice(i, k - i + 1, {
527
+ typ: EnumToken.AngleTokenType,
528
+ val: 90,
529
+ });
530
+ break;
531
+ case "left":
532
+ slice.splice(i, k - i + 1, {
533
+ typ: EnumToken.AngleTokenType,
534
+ val: 270,
535
+ });
536
+ break;
537
+ case "top":
538
+ case "bottom":
539
+ j = k + 1;
540
+ while (j < slice.length &&
541
+ (slice[j].typ === EnumToken.WhitespaceTokenType ||
542
+ slice[j].typ === EnumToken.CommentTokenType)) {
543
+ j++;
544
+ }
545
+ if (j < slice.length && slice[j].typ !== EnumToken.CommaTokenType) {
546
+ i = j - 1;
547
+ break;
548
+ }
549
+ // https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/gradient/linear-gradient#:~:text=If%20unspecified%2C%20it%20defaults%20to%20to%20bottom
550
+ if (key === "bottom") {
551
+ // to bottom -> 180deg is the default angle and will be removed
552
+ while (k < slice.length &&
553
+ slice[k].typ != EnumToken.CommaTokenType) {
554
+ k++;
555
+ }
556
+ slice.splice(i, k - i + 1);
557
+ i--;
558
+ continue;
559
+ }
560
+ slice.splice(i, k - i + 1, {
561
+ typ: EnumToken.AngleTokenType,
562
+ val: key === "top" ? 0 : 180,
563
+ });
564
+ i--;
565
+ break;
566
+ }
567
+ }
568
+ }
569
+ else if (slice[i].typ == EnumToken.AngleTokenType) {
570
+ if ((180 - toDegrees(slice[i]).val) % 360 === 0) {
571
+ k = i + 1;
572
+ while (k < slice.length && slice[k].typ !== EnumToken.CommaTokenType) {
573
+ k++;
574
+ }
575
+ slice.splice(i, k - i + 1);
576
+ }
577
+ }
578
+ }
579
+ if (slice[i]?.typ === EnumToken.CommaTokenType) {
580
+ i++;
581
+ while (i < slice.length &&
582
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
583
+ slice[i].typ === EnumToken.CommentTokenType)) {
584
+ i++;
585
+ }
586
+ }
587
+ if (slice[i]?.typ === EnumToken.ColorTokenType) {
588
+ slice.push(...reduceColorStops(slice.splice(i, slice.length - i)));
589
+ }
590
+ }
591
+ break;
592
+ case "radial-gradient":
593
+ case "repeating-radial-gradient":
594
+ {
595
+ let i = 0;
596
+ while (i < slice.length &&
597
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
598
+ slice[i].typ === EnumToken.CommentTokenType)) {
599
+ i++;
600
+ }
601
+ const positions = [];
602
+ const form = [];
603
+ const size = [];
604
+ const colorSpaceDef = [];
605
+ if (slice[i]?.typ === EnumToken.IdenTokenType) {
606
+ if (equalsIgnoreCase(slice[i].val, "farthest-corner") ||
607
+ equalsIgnoreCase(slice[i].val, "ellipse")) {
608
+ i++;
609
+ }
610
+ else if (equalsIgnoreCase(slice[i].val, "closest-side") ||
611
+ equalsIgnoreCase(slice[i].val, "closest-corner") ||
612
+ equalsIgnoreCase(slice[i].val, "farthest-side")) {
613
+ size.push(slice[i++]);
614
+ }
615
+ else if (equalsIgnoreCase(slice[i].val, "circle")) {
616
+ form.push(slice[i++]);
617
+ }
618
+ while (slice[i]?.typ === EnumToken.WhitespaceTokenType ||
619
+ slice[i]?.typ === EnumToken.CommentTokenType) {
620
+ i++;
621
+ }
622
+ }
623
+ if (slice[i]?.typ === EnumToken.IdenTokenType) {
624
+ if (equalsIgnoreCase(slice[i].val, "farthest-corner") ||
625
+ equalsIgnoreCase(slice[i].val, "ellipse")) {
626
+ i++;
627
+ }
628
+ else if (equalsIgnoreCase(slice[i].val, "closest-side") ||
629
+ equalsIgnoreCase(slice[i].val, "closest-corner") ||
630
+ equalsIgnoreCase(slice[i].val, "farthest-side")) {
631
+ size.push(slice[i++]);
632
+ }
633
+ else if (equalsIgnoreCase(slice[i].val, "circle")) {
634
+ form.push(slice[i++]);
635
+ }
636
+ while (slice[i]?.typ === EnumToken.WhitespaceTokenType ||
637
+ slice[i]?.typ === EnumToken.CommentTokenType) {
638
+ i++;
639
+ }
640
+ }
641
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
642
+ equalsIgnoreCase(slice[i].val, "at")) {
643
+ i++;
644
+ }
645
+ while (slice[i]?.typ === EnumToken.WhitespaceTokenType ||
646
+ slice[i]?.typ === EnumToken.CommentTokenType) {
647
+ i++;
648
+ }
649
+ if (slice[i]?.typ === EnumToken.PercentageTokenType ||
650
+ (slice[i]?.typ === EnumToken.NumberTokenType && 0 === slice[i].val) ||
651
+ (slice[i]?.typ === EnumToken.IdenTokenType &&
652
+ (equalsIgnoreCase(slice[i].val, "center") ||
653
+ equalsIgnoreCase(slice[i].val, "top") ||
654
+ equalsIgnoreCase(slice[i].val, "bottom") ||
655
+ equalsIgnoreCase(slice[i].val, "left") ||
656
+ equalsIgnoreCase(slice[i].val, "right")))) {
657
+ do {
658
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
659
+ "in" === slice[i].val) {
660
+ break;
661
+ }
662
+ positions.push(slice[i]);
663
+ i++;
664
+ } while (slice[i]?.typ !== EnumToken.CommaTokenType);
665
+ }
666
+ while (slice[i]?.typ !== EnumToken.CommaTokenType) {
667
+ colorSpaceDef.push(slice[i++]);
668
+ }
669
+ if (slice[i]?.typ === EnumToken.CommaTokenType) {
670
+ i++;
671
+ }
672
+ if (positions.length > 0) {
673
+ let k;
674
+ let position1 = "";
675
+ let position2 = "";
676
+ for (k = 0; k < positions.length; k++) {
677
+ if (positions[k].typ === EnumToken.IdenTokenType) {
678
+ if (position1.length === 0) {
679
+ position1 = positions[k].val;
680
+ }
681
+ else {
682
+ position2 = positions[k].val;
683
+ }
684
+ }
685
+ else if (positions[k].typ === EnumToken.PercentageTokenType ||
686
+ positions[k].typ === EnumToken.NumberTokenType) {
687
+ if (position1.length === 0) {
688
+ position1 = positions[k].val + "%";
689
+ }
690
+ else {
691
+ position2 = positions[k].val + "%";
692
+ }
693
+ }
694
+ if (position2.length > 0) {
695
+ break;
696
+ }
697
+ }
698
+ if (position1.length > 0) {
699
+ let key = `${position1} ${position2}`.trim();
700
+ if (key === "50% 50%" ||
701
+ key === "center center" ||
702
+ key === "50%" ||
703
+ key === "center") {
704
+ positions.length = 0;
705
+ }
706
+ else if (key === "0% 0%" ||
707
+ key === "0 0" ||
708
+ key === "top left" ||
709
+ key === "left top") {
710
+ positions.splice(0, positions.length, {
711
+ typ: EnumToken.PercentageTokenType,
712
+ val: 0,
713
+ });
714
+ }
715
+ else if (key === "50% 0%" ||
716
+ key === "50% 0" ||
717
+ key === "center top" ||
718
+ key === "top center") {
719
+ positions.splice(0, positions.length, {
720
+ typ: EnumToken.IdenTokenType,
721
+ val: "top",
722
+ });
723
+ }
724
+ else if (key === "top right" || key === "right top") {
725
+ positions.splice(0, positions.length, {
726
+ typ: EnumToken.PercentageTokenType,
727
+ val: 100,
728
+ }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.PercentageTokenType, val: 0 });
729
+ }
730
+ else if (key === "0 50%" ||
731
+ key === "0% 50%" ||
732
+ key === "left center" ||
733
+ key === "center left") {
734
+ positions.splice(0, positions.length, {
735
+ typ: EnumToken.IdenTokenType,
736
+ val: "left",
737
+ });
738
+ }
739
+ else if (key === "100% 50%" || key === "right center" || key === "center right") {
740
+ positions.splice(0, positions.length, {
741
+ typ: EnumToken.IdenTokenType,
742
+ val: "right",
743
+ });
744
+ }
745
+ else if (key === "0 100%" ||
746
+ key === "0% 100%" ||
747
+ key === "left bottom" ||
748
+ key === "bottom left") {
749
+ positions.splice(0, positions.length, {
750
+ typ: EnumToken.IdenTokenType,
751
+ val: "bottom",
752
+ });
753
+ }
754
+ else if (key === "50% 100%" ||
755
+ key === "center bottom" ||
756
+ key === "bottom center") {
757
+ positions.splice(0, positions.length, {
758
+ typ: EnumToken.IdenTokenType,
759
+ val: "bottom",
760
+ });
761
+ }
762
+ else if (key === "100% 100%" ||
763
+ key === "right bottom" ||
764
+ key === "bottom right") {
765
+ positions.splice(0, positions.length, {
766
+ typ: EnumToken.PercentageTokenType,
767
+ val: 100,
768
+ });
769
+ }
770
+ else if (position1 === position2) {
771
+ positions.splice(0, positions.length, positions[k]);
772
+ }
773
+ }
774
+ }
775
+ const result = [];
776
+ if (form.length > 0) {
777
+ result.push(...form);
778
+ }
779
+ if (size.length > 0) {
780
+ if (result.length > 0) {
781
+ result.push({ typ: EnumToken.WhitespaceTokenType });
782
+ }
783
+ result.push(...size);
784
+ }
785
+ if (positions.length > 0) {
786
+ if (result.length > 0) {
787
+ result.push({ typ: EnumToken.WhitespaceTokenType });
788
+ }
789
+ result.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }, ...positions);
790
+ }
791
+ if (colorSpaceDef.length > 0) {
792
+ if (result.length > 0) {
793
+ result.push({ typ: EnumToken.WhitespaceTokenType });
794
+ }
795
+ result.push(...colorSpaceDef);
796
+ }
797
+ if (result.length > 0) {
798
+ result.push({ typ: EnumToken.CommaTokenType });
799
+ }
800
+ result.push(...reduceColorStops(slice.slice(i)));
801
+ slice.length = 0;
802
+ slice.push(...result);
803
+ }
804
+ break;
805
+ case "conic-gradient":
806
+ case "repeating-conic-gradient":
807
+ {
808
+ let i = 0;
809
+ const angles = [];
810
+ const positions = [];
811
+ const colorSpaceDef = [];
812
+ while (i < slice.length &&
813
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
814
+ slice[i].typ === EnumToken.CommentTokenType)) {
815
+ i++;
816
+ }
817
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
818
+ equalsIgnoreCase(slice[i].val, "from")) {
819
+ angles.push(slice[i++]);
820
+ while (i < slice.length &&
821
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
822
+ slice[i].typ === EnumToken.CommentTokenType)) {
823
+ angles.push(slice[i++]);
824
+ }
825
+ if ((slice[i]?.typ === EnumToken.NumberTokenType ||
826
+ slice[i]?.typ === EnumToken.AngleTokenType) &&
827
+ 0 === toDegrees(slice[i]).val) {
828
+ angles.length = 0;
829
+ i++;
830
+ }
831
+ else if (slice[i]?.typ !== EnumToken.CommaTokenType &&
832
+ slice[i].typ != EnumToken.IdenTokenType) {
833
+ angles.push(slice[i++]);
834
+ }
835
+ while (i < slice.length &&
836
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
837
+ slice[i].typ === EnumToken.CommentTokenType)) {
838
+ i++;
839
+ }
840
+ }
841
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
842
+ equalsIgnoreCase(slice[i].val, "at")) {
843
+ i++;
844
+ while (i < slice.length &&
845
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
846
+ slice[i].typ === EnumToken.CommentTokenType)) {
847
+ i++;
848
+ }
849
+ let position1 = "";
850
+ let position2 = "";
851
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
852
+ !equalsIgnoreCase("in", slice[i].val)) {
853
+ position1 = slice[i].val;
854
+ positions.push(slice[i++]);
855
+ }
856
+ else if (slice[i]?.typ === EnumToken.PercentageTokenType ||
857
+ slice[i]?.typ === EnumToken.NumberTokenType) {
858
+ position1 = slice[i].val + "%";
859
+ positions.push(slice[i++]);
860
+ }
861
+ while (i < slice.length &&
862
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
863
+ slice[i].typ === EnumToken.CommentTokenType)) {
864
+ positions.push(slice[i++]);
865
+ }
866
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
867
+ !equalsIgnoreCase("in", slice[i].val)) {
868
+ position2 = slice[i].val;
869
+ positions.push(slice[i++]);
870
+ }
871
+ else if (slice[i]?.typ === EnumToken.PercentageTokenType ||
872
+ slice[i]?.typ === EnumToken.NumberTokenType) {
873
+ position2 = slice[i].val + "%";
874
+ positions.push(slice[i++]);
875
+ }
876
+ while (i < slice.length &&
877
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
878
+ slice[i].typ === EnumToken.CommentTokenType)) {
879
+ i++;
880
+ }
881
+ if (position1.length > 0) {
882
+ reducegradientBackgroundPosition(positions, `${position1} ${position2}`.trim());
883
+ }
884
+ }
885
+ while (i < slice.length &&
886
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
887
+ slice[i].typ === EnumToken.CommentTokenType)) {
888
+ i++;
889
+ }
890
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
891
+ equalsIgnoreCase("in", slice[i].val)) {
892
+ while (i < slice.length && slice[i].typ !== EnumToken.CommaTokenType) {
893
+ colorSpaceDef.push(slice[i++]);
894
+ }
895
+ }
896
+ if (slice[i]?.typ === EnumToken.CommaTokenType) {
897
+ i++;
898
+ }
899
+ const result = [];
900
+ if (angles.length > 0) {
901
+ result.push(...angles, { typ: EnumToken.CommaTokenType });
902
+ }
903
+ if (positions.length > 0 || colorSpaceDef.length > 0) {
904
+ if (positions.length > 0) {
905
+ result.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }, ...positions);
906
+ }
907
+ if (colorSpaceDef.length > 0) {
908
+ if (result.length > 0) {
909
+ result.push({ typ: EnumToken.WhitespaceTokenType });
910
+ }
911
+ result.push(...colorSpaceDef);
912
+ }
913
+ result.push({ typ: EnumToken.CommaTokenType });
914
+ }
915
+ result.push(...reduceConicColorStops(slice.slice(i)));
916
+ slice.length = 0;
917
+ slice.push(...result);
918
+ }
919
+ break;
920
+ }
921
+ return token.val + "(" + slice.reduce(reducer, "") + ")";
922
+ }
482
923
  case EnumToken.TimingFunctionTokenType:
483
924
  case EnumToken.PseudoClassFuncTokenType:
484
925
  case EnumToken.WhenElseFunctionTokenType:
@@ -499,7 +940,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
499
940
  return (token.val +
500
941
  "(" +
501
942
  token.chi.reduce((acc, curr) => acc +
502
- renderToken(curr, token.typ == EnumToken.FunctionTokenType ? { minify: false } : options, cache, reducer), "") +
943
+ renderValue(curr, token.typ == EnumToken.FunctionTokenType ? { minify: false } : options, cache, reducer), "") +
503
944
  ")");
504
945
  }
505
946
  return (
@@ -508,22 +949,22 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
508
949
  token.chi.reduce(reducer, "") +
509
950
  ")");
510
951
  case EnumToken.MatchExpressionTokenType:
511
- return (renderToken(token.l, options, cache, reducer, errors) +
512
- renderToken(token.op, options, cache, reducer, errors) +
513
- renderToken(token.r, options, cache, reducer, errors) +
952
+ return (renderValue(token.l, options, cache, reducer, errors) +
953
+ renderValue(token.op, options, cache, reducer, errors) +
954
+ renderValue(token.r, options, cache, reducer, errors) +
514
955
  (token.attr ? " " + token.attr : ""));
515
956
  case EnumToken.NameSpaceAttributeTokenType:
516
957
  return ((token.l == null
517
958
  ? ""
518
- : renderToken(token.l, options, cache, reducer, errors)) +
959
+ : renderValue(token.l, options, cache, reducer, errors)) +
519
960
  "|" +
520
- renderToken(token.r, options, cache, reducer, errors));
961
+ renderValue(token.r, options, cache, reducer, errors));
521
962
  case EnumToken.ComposesSelectorNodeType:
522
- return (token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache), "") +
963
+ return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
523
964
  (token.r == null
524
965
  ? ""
525
966
  : " from " +
526
- renderToken(token.r, options, cache, reducer, errors)));
967
+ renderValue(token.r, options, cache, reducer, errors)));
527
968
  case EnumToken.BlockStartTokenType:
528
969
  return "{";
529
970
  case EnumToken.BlockEndTokenType:
@@ -591,7 +1032,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
591
1032
  case EnumToken.FrequencyTokenType:
592
1033
  case EnumToken.ResolutionTokenType:
593
1034
  let val = token.val.typ == EnumToken.FractionTokenType
594
- ? renderToken(token.val, options, cache)
1035
+ ? renderValue(token.val, options, cache)
595
1036
  : minifyNumber(token.val);
596
1037
  let unit = token.unit;
597
1038
  if (token.typ == EnumToken.AngleTokenType && !val.includes("/")) {
@@ -670,12 +1111,12 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
670
1111
  case EnumToken.PercentageTokenType:
671
1112
  const uni = token.typ == EnumToken.PercentageTokenType ? "%" : "fr";
672
1113
  const perc = token.val.typ == EnumToken.FractionTokenType
673
- ? renderToken(token.val, options, cache)
1114
+ ? renderValue(token.val, options, cache)
674
1115
  : minifyNumber(token.val);
675
1116
  return options.minify && perc == "0" ? "0" : perc.includes("/") ? perc.replace("/", uni + "/") : perc + uni;
676
1117
  case EnumToken.NumberTokenType:
677
1118
  return token.val.typ == EnumToken.FractionTokenType
678
- ? renderToken(token.val, options, cache)
1119
+ ? renderValue(token.val, options, cache)
679
1120
  : minifyNumber(token.val);
680
1121
  case EnumToken.AtRuleTokenType:
681
1122
  return "@" + token.nam;
@@ -705,36 +1146,37 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
705
1146
  return "&";
706
1147
  case EnumToken.InvalidAttrTokenType:
707
1148
  return ("[" +
708
- token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), ""));
1149
+ token.chi.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
709
1150
  case EnumToken.InvalidClassSelectorTokenType:
710
1151
  return token.val;
711
1152
  case EnumToken.SupportsQueryUnaryConditionTokenType:
712
1153
  case EnumToken.WhenElseUnaryConditionTokenType:
713
- return (renderToken(token.l, options, cache, reducer, errors) +
1154
+ return (renderValue(token.l, options, cache, reducer, errors) +
714
1155
  " " +
715
- token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), ""));
1156
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), ""));
716
1157
  case EnumToken.SupportsQueryConditionTokenType:
717
1158
  case EnumToken.WhenElseQueryConditionTokenType:
718
- return (token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), "") +
1159
+ return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), "") +
719
1160
  " " +
720
- renderToken(token.op, options, cache, reducer, errors) +
1161
+ renderValue(token.op, options, cache, reducer, errors) +
721
1162
  " " +
722
- token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), ""));
1163
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), ""));
723
1164
  case EnumToken.IfConditionTokenType:
724
- return token.l.length == 0 ? '' : (token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), "") +
725
- ':' +
726
- token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), ""));
1165
+ return token.l.length == 0
1166
+ ? ""
1167
+ : token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), "") +
1168
+ ":" +
1169
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), "");
727
1170
  case EnumToken.IfElseConditionTokenType:
728
- return (renderToken(token.l) +
729
- renderToken(token.r));
1171
+ return renderValue(token.l) + renderValue(token.r);
730
1172
  case EnumToken.DeclarationNodeType:
731
1173
  return (token.nam +
732
1174
  ":" +
733
- (options.minify ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderToken(curr, options, cache), ""));
1175
+ (options.minify ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
734
1176
  case EnumToken.MediaQueryUnaryFeatureTokenType:
735
- return (renderToken(token.l, options, cache, reducer, errors) +
1177
+ return (renderValue(token.l, options, cache, reducer, errors) +
736
1178
  " " +
737
- token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache), ""));
1179
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
738
1180
  case EnumToken.MediaQueryConditionTokenType: {
739
1181
  const indent = token.op.typ == EnumToken.LtTokenType ||
740
1182
  token.op.typ == EnumToken.GtTokenType ||
@@ -744,18 +1186,18 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
744
1186
  token.op.typ == EnumToken.GteTokenType
745
1187
  ? ""
746
1188
  : " ";
747
- return (token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), "") +
1189
+ return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), "") +
748
1190
  indent +
749
- renderToken(token.op, options, cache, reducer, errors) +
1191
+ renderValue(token.op, options, cache, reducer, errors) +
750
1192
  indent +
751
- token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache), ""));
1193
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
752
1194
  }
753
1195
  case EnumToken.MediaRangeQueryTokenType:
754
- return (token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache), "") +
755
- renderToken(token.op1) +
756
- token.val.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer, errors), "") +
757
- renderToken(token.op2) +
758
- token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache), ""));
1196
+ return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
1197
+ renderValue(token.op1) +
1198
+ token.val.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), "") +
1199
+ renderValue(token.op2) +
1200
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
759
1201
  case EnumToken.MediaFeatureTokenType:
760
1202
  return token.val;
761
1203
  case EnumToken.NotTokenType:
@@ -803,4 +1245,4 @@ function filterValues(values) {
803
1245
  return values;
804
1246
  }
805
1247
 
806
- export { doRender, filterValues, renderToken };
1248
+ export { doRender, filterValues, renderValue };