@tbela99/css-parser 1.4.8 → 1.4.10

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 (56) hide show
  1. package/CHANGELOG.md +32 -3
  2. package/README.md +1 -1
  3. package/dist/index-umd-web.js +12950 -13310
  4. package/dist/index.cjs +16447 -16809
  5. package/dist/index.d.ts +2481 -1754
  6. package/dist/lib/ast/features/calc.js +3 -1
  7. package/dist/lib/ast/features/if.js +2 -96
  8. package/dist/lib/ast/features/prefix.js +7 -2
  9. package/dist/lib/ast/features/shorthand.js +1 -1
  10. package/dist/lib/ast/features/transform.js +1 -1
  11. package/dist/lib/ast/find.js +6 -6
  12. package/dist/lib/ast/math/expression.js +30 -12
  13. package/dist/lib/ast/math/math.js +2 -1
  14. package/dist/lib/ast/minify.js +34 -5
  15. package/dist/lib/ast/transform/compute.js +2 -1
  16. package/dist/lib/ast/transform/perspective.js +1 -1
  17. package/dist/lib/ast/transform/utils.js +29 -21
  18. package/dist/lib/ast/types.js +9 -66
  19. package/dist/lib/fs/resolve.js +1 -16
  20. package/dist/lib/parser/declaration/list.js +6 -1
  21. package/dist/lib/parser/{linemap.js → linesmap.js} +25 -9
  22. package/dist/lib/parser/parse.js +1264 -151
  23. package/dist/lib/parser/source.js +48 -17
  24. package/dist/lib/parser/tokenize.js +112 -133
  25. package/dist/lib/parser/utils/at-rule-container.js +12 -44
  26. package/dist/lib/parser/utils/at-rule-generic.js +12 -24
  27. package/dist/lib/parser/utils/at-rule-import.js +4 -172
  28. package/dist/lib/parser/utils/at-rule-media.js +14 -251
  29. package/dist/lib/parser/utils/at-rule-support.js +8 -258
  30. package/dist/lib/parser/utils/at-rule-when-else.js +4 -214
  31. package/dist/lib/parser/utils/declaration.js +5 -95
  32. package/dist/lib/parser/utils/hash.js +54 -1
  33. package/dist/lib/parser/utils/selector.js +1 -1
  34. package/dist/lib/parser/utils/token.js +1 -0
  35. package/dist/lib/renderer/render.js +79 -87
  36. package/dist/lib/renderer/sourcemap/sourcemap.js +47 -43
  37. package/dist/lib/syntax/color/cmyk.js +3 -3
  38. package/dist/lib/syntax/color/color-mix.js +7 -7
  39. package/dist/lib/syntax/color/color.js +9 -27
  40. package/dist/lib/syntax/color/hsl.js +6 -6
  41. package/dist/lib/syntax/color/hwb.js +6 -6
  42. package/dist/lib/syntax/color/lab.js +5 -5
  43. package/dist/lib/syntax/color/lch.js +6 -6
  44. package/dist/lib/syntax/color/oklab.js +5 -5
  45. package/dist/lib/syntax/color/oklch.js +5 -5
  46. package/dist/lib/syntax/color/relative-color.js +8 -1
  47. package/dist/lib/syntax/color/utils/distance.js +2 -1
  48. package/dist/lib/syntax/constants.js +2 -1
  49. package/dist/lib/syntax/syntax.js +96 -9
  50. package/dist/lib/validation/config.json.js +27 -6
  51. package/dist/lib/validation/match.js +42 -845
  52. package/dist/node.js +134 -9
  53. package/dist/web.js +133 -6
  54. package/llms.txt +73 -0
  55. package/package.json +13 -16
  56. package/dist/lib/parser/arena.js +0 -206
@@ -1,5 +1,5 @@
1
1
  import { EnumToken, ColorType } from '../../ast/types.js';
2
- import { minmax, getNumber, toPrecisionValue, toPrecisionAngle } from './color.js';
2
+ import { minmax, getNumber } from './color.js';
3
3
  import { srgbvalues, srgb2lsrgbvalues } from './srgb.js';
4
4
  import { srgb2lch, xyz2lchvalues } from './lch.js';
5
5
  import { srgb2rgb } from './rgb.js';
@@ -345,7 +345,7 @@ function colorMix(...args) {
345
345
  chi: values.map((v) => {
346
346
  return {
347
347
  typ: EnumToken.NumberTokenType,
348
- val: toPrecisionValue(v),
348
+ val: v,
349
349
  };
350
350
  }),
351
351
  kin: ColorType.LCH,
@@ -366,7 +366,7 @@ function colorMix(...args) {
366
366
  values.map((v) => {
367
367
  return {
368
368
  typ: EnumToken.NumberTokenType,
369
- val: toPrecisionValue(v),
369
+ val: v,
370
370
  };
371
371
  })),
372
372
  kin: ColorType.COLOR,
@@ -404,18 +404,18 @@ function colorMix(...args) {
404
404
  chi: values.map((v) => {
405
405
  return {
406
406
  typ: EnumToken.NumberTokenType,
407
- val: toPrecisionValue(v),
407
+ val: v,
408
408
  };
409
409
  }),
410
410
  kin: ColorType[colorSpace.toUpperCase().replaceAll("-", "_")],
411
411
  };
412
412
  if (colorSpace == "hsl" || colorSpace == "hwb") {
413
413
  // @ts-ignore
414
- result.chi[0] = { typ: EnumToken.AngleTokenType, val: toPrecisionAngle(result.chi[0].val * 360) };
414
+ result.chi[0] = { typ: EnumToken.AngleTokenType, val: result.chi[0].val * 360 };
415
415
  // @ts-ignore
416
- result.chi[1] = { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(result.chi[1].val) * 100 };
416
+ result.chi[1] = { typ: EnumToken.PercentageTokenType, val: result.chi[1].val * 100 };
417
417
  // @ts-ignore
418
- result.chi[2] = { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(result.chi[2].val) * 100 };
418
+ result.chi[2] = { typ: EnumToken.PercentageTokenType, val: result.chi[2].val * 100 };
419
419
  }
420
420
  return result;
421
421
  }
@@ -11,7 +11,7 @@ import { oklch2srgbvalues, lch2srgbvalues, oklab2srgbvalues, lab2srgbvalues, cmy
11
11
  import { prophotorgb2srgbvalues, srgb2prophotorgbvalues } from './prophotorgb.js';
12
12
  import { rec20202srgb, srgb2rec2020values } from './rec2020.js';
13
13
  import { srgb2xyz_d65, srgb2xyz } from './xyz.js';
14
- import { lp32srgbvalues, p32srgbvalues, srgb2p3values } from './p3.js';
14
+ import { lp32srgbvalues, p32srgbvalues, srgb2lp3values, srgb2p3values } from './p3.js';
15
15
  import { xyzd502srgb } from './xyzd50.js';
16
16
  import { colorMix } from './color-mix.js';
17
17
  import { reduceHexValue, rgb2HexToken, color2HexToken, lch2HexToken, lab2HexToken, oklch2HexToken, oklab2HexToken, cmyk2HexToken, hwb2HexToken, hsl2HexToken } from './hex.js';
@@ -19,7 +19,7 @@ import { parseRelativeColorComponents } from './relative-color.js';
19
19
  import { isIdentColor } from '../syntax.js';
20
20
  import { color2cmykToken, lch2cmykToken, lab2cmykToken, oklch2cmykToken, oklab2cmyk, hwb2cmykToken, hsl2cmykToken, rgb2cmykToken } from './cmyk.js';
21
21
  import { a98rgb2srgbvalues, srgb2a98values } from './a98rgb.js';
22
- import { LOC, colorPrecision, colorFuncColorSpace, epsilon, anglePrecision } from '../constants.js';
22
+ import { LOC, colorFuncColorSpace } from '../constants.js';
23
23
  import { trimArray } from '../../validation/match.js';
24
24
  import { alpha } from './alpha.js';
25
25
  import { equalsIgnoreCase } from '../../parser/utils/text.js';
@@ -102,7 +102,7 @@ function convertColor(token, to) {
102
102
  if (colorSpace.val == ColorType[to].toLowerCase().replaceAll("_", "-")) {
103
103
  for (const chi of token.chi) {
104
104
  if (chi.typ == EnumToken.NumberTokenType && typeof chi.val == "number") {
105
- chi.val = toPrecisionValue(getNumber(chi));
105
+ chi.val = getNumber(chi);
106
106
  }
107
107
  }
108
108
  return token;
@@ -524,21 +524,21 @@ function color2srgbvalues(token) {
524
524
  break;
525
525
  }
526
526
  if (values.length == 4) {
527
- values[3] = toPrecisionValue(values[3], 2);
527
+ values[3] = values[3];
528
528
  }
529
529
  return values;
530
530
  }
531
531
  function values2colortoken(values, to) {
532
532
  values = srgb2srgbcolorspace(values, to);
533
533
  const chi = [
534
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
535
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
536
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[2]) },
534
+ { typ: EnumToken.NumberTokenType, val: values[0] },
535
+ { typ: EnumToken.NumberTokenType, val: values[1] },
536
+ { typ: EnumToken.NumberTokenType, val: values[2] },
537
537
  ];
538
538
  if (values.length == 4) {
539
539
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
540
540
  typ: EnumToken.PercentageTokenType,
541
- val: toPrecisionValue(values[3], 2) * 100,
541
+ val: values[3] * 100,
542
542
  });
543
543
  }
544
544
  const colorSpace = ColorType[to].toLowerCase().replaceAll("_", "-");
@@ -602,23 +602,5 @@ function getAngle(token) {
602
602
  // @ts-ignore
603
603
  return token.val / 360;
604
604
  }
605
- function toPrecisionValue(value, precision = colorPrecision) {
606
- const div = Math.pow(10, precision);
607
- value = Math.round(value * div) / div;
608
- return Math.abs(value) < epsilon ? 0 : value;
609
- }
610
- function toPrecisionAngle(angle, precision = colorPrecision, correctValue = true) {
611
- angle = toPrecisionValue(angle, precision);
612
- if (correctValue && Math.abs(angle) >= 360) {
613
- angle %= 360;
614
- }
615
- if (Math.abs(angle) < anglePrecision) {
616
- angle = 0;
617
- }
618
- if (correctValue && angle < 0) {
619
- angle += 360;
620
- }
621
- return angle;
622
- }
623
605
 
624
- export { cmyk2colorToken, color2colorToken, color2srgbvalues, convertColor, getAngle, getNumber, hex2colorToken, hsl2colorToken, hwb2colorToken, lab2colorToken, lch2colorToken, minmax, oklab2colorToken, oklch2colorToken, rgb2colorToken, toPrecisionAngle, toPrecisionValue };
606
+ export { cmyk2colorToken, color2colorToken, color2srgbvalues, convertColor, getAngle, getNumber, hex2colorToken, hsl2colorToken, hwb2colorToken, lab2colorToken, lch2colorToken, minmax, oklab2colorToken, oklch2colorToken, rgb2colorToken };
@@ -1,5 +1,5 @@
1
1
  import { hwb2hsv } from './hsv.js';
2
- import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getNumber } from './color.js';
2
+ import { color2srgbvalues, getNumber } from './color.js';
3
3
  import { lch2rgbvalues, lab2rgbvalues, cmyk2rgbvalues } from './rgb.js';
4
4
  import { getColorComponents } from './utils/components.js';
5
5
  import { hex2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hslvalues } from './srgb.js';
@@ -67,16 +67,16 @@ function color2HslToken(token) {
67
67
  return hslToken(srgb2hslvalues(...values));
68
68
  }
69
69
  function hslToken(values) {
70
- values[0] = toPrecisionAngle(values[0] * 360);
70
+ values[0] = values[0] * 360;
71
71
  const chi = [
72
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
73
- { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[1]) * 100 },
74
- { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[2]) * 100 },
72
+ { typ: EnumToken.NumberTokenType, val: values[0] },
73
+ { typ: EnumToken.PercentageTokenType, val: values[1] * 100 },
74
+ { typ: EnumToken.PercentageTokenType, val: values[2] * 100 },
75
75
  ];
76
76
  if (values.length == 4 && values[3] != 1) {
77
77
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
78
78
  typ: EnumToken.PercentageTokenType,
79
- val: toPrecisionValue(values[3], 2) * 100,
79
+ val: values[3] * 100,
80
80
  });
81
81
  }
82
82
  return {
@@ -1,6 +1,6 @@
1
1
  import { hsl2hsv } from './hsv.js';
2
2
  import { getColorComponents } from './utils/components.js';
3
- import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getAngle, getNumber } from './color.js';
3
+ import { color2srgbvalues, getAngle, getNumber } from './color.js';
4
4
  import { EnumToken, ColorType } from '../../ast/types.js';
5
5
  import { cmyk2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues } from './srgb.js';
6
6
 
@@ -61,16 +61,16 @@ function color2hwbToken(token) {
61
61
  return hwbToken(values);
62
62
  }
63
63
  function hwbToken(values) {
64
- values[0] = toPrecisionAngle(values[0] * 360);
64
+ values[0] = values[0] * 360;
65
65
  const chi = [
66
- { typ: EnumToken.NumberTokenType, val: toPrecisionAngle(values[0]) },
67
- { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[1] * 100) },
68
- { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[2] * 100) },
66
+ { typ: EnumToken.NumberTokenType, val: values[0] },
67
+ { typ: EnumToken.PercentageTokenType, val: values[1] * 100 },
68
+ { typ: EnumToken.PercentageTokenType, val: values[2] * 100 },
69
69
  ];
70
70
  if (values.length == 4) {
71
71
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
72
72
  typ: EnumToken.PercentageTokenType,
73
- val: toPrecisionValue(values[3], 2) * 100
73
+ val: values[3] * 100
74
74
  });
75
75
  }
76
76
  return {
@@ -4,7 +4,7 @@ import { srgb2xyz_d65, XYZ_D50_to_D65 } from './xyz.js';
4
4
  import { oklch2srgbvalues, cmyk2srgbvalues, hwb2srgbvalues, hsl2srgb, rgb2srgb, hex2srgbvalues, xyz2srgb } from './srgb.js';
5
5
  import { getLCHComponents } from './lch.js';
6
6
  import { getOKLABComponents, OKLab_to_XYZ } from './oklab.js';
7
- import { color2srgbvalues, toPrecisionValue, getNumber } from './color.js';
7
+ import { color2srgbvalues, getNumber } from './color.js';
8
8
  import { EnumToken, ColorType } from '../../ast/types.js';
9
9
  import { XYZ_D65_to_D50 } from './xyzd50.js';
10
10
 
@@ -70,14 +70,14 @@ function color2labToken(token) {
70
70
  }
71
71
  function labToken(values) {
72
72
  const chi = [
73
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
74
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
75
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[2]) },
73
+ { typ: EnumToken.NumberTokenType, val: values[0] },
74
+ { typ: EnumToken.NumberTokenType, val: values[1] },
75
+ { typ: EnumToken.NumberTokenType, val: values[2] },
76
76
  ];
77
77
  if (values.length == 4) {
78
78
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
79
79
  typ: EnumToken.PercentageTokenType,
80
- val: toPrecisionValue(values[3], 2) * 100,
80
+ val: values[3] * 100,
81
81
  });
82
82
  }
83
83
  return {
@@ -1,5 +1,5 @@
1
1
  import { getColorComponents } from './utils/components.js';
2
- import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getNumber, getAngle } from './color.js';
2
+ import { color2srgbvalues, getNumber, getAngle } from './color.js';
3
3
  import { EnumToken, ColorType } from '../../ast/types.js';
4
4
  import { srgb2labvalues, xyz2lab, oklch2labvalues, oklab2labvalues, getLABComponents, hwb2labvalues, hsl2labvalues, rgb2labvalues, hex2labvalues } from './lab.js';
5
5
  import { cmyk2srgbvalues } from './srgb.js';
@@ -68,16 +68,16 @@ function color2lchToken(token) {
68
68
  return lchToken(values);
69
69
  }
70
70
  function lchToken(values) {
71
- values[2] = toPrecisionAngle(values[2]);
71
+ values[2] = values[2];
72
72
  const chi = [
73
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
74
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
75
- { typ: EnumToken.NumberTokenType, val: toPrecisionAngle(values[2]) },
73
+ { typ: EnumToken.NumberTokenType, val: values[0] },
74
+ { typ: EnumToken.NumberTokenType, val: values[1] },
75
+ { typ: EnumToken.NumberTokenType, val: values[2] },
76
76
  ];
77
77
  if (values.length == 4) {
78
78
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
79
79
  typ: EnumToken.PercentageTokenType,
80
- val: toPrecisionValue(values[3], 2) * 100,
80
+ val: values[3] * 100,
81
81
  });
82
82
  }
83
83
  return {
@@ -1,7 +1,7 @@
1
1
  import { getColorComponents } from './utils/components.js';
2
2
  import { multiplyMatrices } from './utils/matrix.js';
3
3
  import { srgb2lsrgbvalues, lch2srgbvalues, lab2srgbvalues, cmyk2srgbvalues, hwb2srgbvalues, hsl2srgb, rgb2srgb, hex2srgbvalues, lsrgb2srgbvalues } from './srgb.js';
4
- import { color2srgbvalues, toPrecisionValue, getNumber } from './color.js';
4
+ import { getNumber, color2srgbvalues } from './color.js';
5
5
  import { EnumToken, ColorType } from '../../ast/types.js';
6
6
  import { getOKLCHComponents } from './oklch.js';
7
7
  import { lchvalues2labvalues } from './lab.js';
@@ -71,14 +71,14 @@ function color2oklabToken(token) {
71
71
  }
72
72
  function oklabToken(values) {
73
73
  const chi = [
74
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
75
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
76
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[2]) },
74
+ { typ: EnumToken.NumberTokenType, val: values[0] },
75
+ { typ: EnumToken.NumberTokenType, val: values[1] },
76
+ { typ: EnumToken.NumberTokenType, val: values[2] },
77
77
  ];
78
78
  if (values.length == 4) {
79
79
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
80
80
  typ: EnumToken.PercentageTokenType,
81
- val: toPrecisionValue(values[3], 2) * 100,
81
+ val: values[3] * 100,
82
82
  });
83
83
  }
84
84
  return {
@@ -1,5 +1,5 @@
1
1
  import { getColorComponents } from './utils/components.js';
2
- import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getNumber, getAngle } from './color.js';
2
+ import { color2srgbvalues, getNumber, getAngle } from './color.js';
3
3
  import { EnumToken, ColorType } from '../../ast/types.js';
4
4
  import { labvalues2lchvalues } from './lch.js';
5
5
  import { srgb2oklab, lch2oklabvalues, getOKLABComponents, lab2oklabvalues, hwb2oklabvalues, hsl2oklabvalues, rgb2oklabvalues, hex2oklabvalues } from './oklab.js';
@@ -67,16 +67,16 @@ function color2oklchToken(token) {
67
67
  return oklchToken(srgb2oklch(...values));
68
68
  }
69
69
  function oklchToken(values) {
70
- values[2] = toPrecisionAngle(values[2]);
70
+ values[2] = values[2];
71
71
  const chi = [
72
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
73
- { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
72
+ { typ: EnumToken.NumberTokenType, val: values[0] },
73
+ { typ: EnumToken.NumberTokenType, val: values[1] },
74
74
  { typ: EnumToken.NumberTokenType, val: values[2] },
75
75
  ];
76
76
  if (values.length == 4) {
77
77
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
78
78
  typ: EnumToken.PercentageTokenType,
79
- val: toPrecisionValue(values[3], 2) * 100,
79
+ val: values[3] * 100,
80
80
  });
81
81
  }
82
82
  return {
@@ -2,7 +2,7 @@ import { convertColor, getNumber } from './color.js';
2
2
  import { EnumToken, ColorType } from '../../ast/types.js';
3
3
  import { walkValues } from '../../ast/walk.js';
4
4
  import { evaluateFunc, evaluate } from '../../ast/math/expression.js';
5
- import { colorsFunc, colorFuncColorSpace, colorRange, mathFuncs } from '../constants.js';
5
+ import { colorsFunc, colorFuncColorSpace, LOC, colorRange, mathFuncs } from '../constants.js';
6
6
  import { equalsIgnoreCase } from '../../parser/utils/text.js';
7
7
  import { getColorComponents } from './utils/components.js';
8
8
 
@@ -103,16 +103,19 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
103
103
  ? {
104
104
  typ: EnumToken.NumberTokenType,
105
105
  val: 1,
106
+ [LOC]: b[LOC],
106
107
  }
107
108
  : alpha.typ == EnumToken.IdenTokenType && alpha.val == "none"
108
109
  ? {
109
110
  typ: EnumToken.NumberTokenType,
110
111
  val: 0,
112
+ [LOC]: alpha[LOC],
111
113
  }
112
114
  : alpha.typ == EnumToken.PercentageTokenType
113
115
  ? {
114
116
  typ: EnumToken.NumberTokenType,
115
117
  val: getNumber(alpha),
118
+ [LOC]: alpha[LOC],
116
119
  }
117
120
  : alpha,
118
121
  };
@@ -125,11 +128,13 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
125
128
  ? {
126
129
  typ: EnumToken.NumberTokenType,
127
130
  val: 1,
131
+ [LOC]: bExp[LOC],
128
132
  }
129
133
  : aExp.typ == EnumToken.IdenTokenType && aExp.val == "none"
130
134
  ? {
131
135
  typ: EnumToken.NumberTokenType,
132
136
  val: 0,
137
+ [LOC]: aExp[LOC],
133
138
  }
134
139
  : aExp),
135
140
  };
@@ -160,6 +165,7 @@ function getValue(t, converted, component) {
160
165
  return {
161
166
  typ: EnumToken.NumberTokenType,
162
167
  val: value,
168
+ [LOC]: t[LOC],
163
169
  };
164
170
  }
165
171
  return t;
@@ -203,6 +209,7 @@ function computeComponentValue(expr, values) {
203
209
  typ: EnumToken.NumberTokenType,
204
210
  // @ts-ignore
205
211
  val: "" + Math[value.val.toUpperCase()],
212
+ [LOC]: value[LOC],
206
213
  // @ts-ignore
207
214
  });
208
215
  }
@@ -2,6 +2,7 @@ import { convertColor } from '../color.js';
2
2
  import { getOKLABComponents } from '../oklab.js';
3
3
  import { ColorType } from '../../../ast/types.js';
4
4
  import { colorDistancePrecision } from '../../constants.js';
5
+ import { toPrecisionValue } from '../../syntax.js';
5
6
 
6
7
  /**
7
8
  * Calculate the distance between two okLab colors.
@@ -27,7 +28,7 @@ function okLabDistance(color1, color2) {
27
28
  if (okLab1[3] != null || okLab2[3] != null) {
28
29
  diff.push((okLab1[3] ?? 1) - (okLab2[3] ?? 1));
29
30
  }
30
- return Math.hypot(...diff);
31
+ return toPrecisionValue(Math.hypot(...diff));
31
32
  }
32
33
  /**
33
34
  * Check if two colors are close in okLab space.
@@ -185,9 +185,10 @@ const colorsFunc = [
185
185
  "lab",
186
186
  "oklch",
187
187
  "lch",
188
+ "alpha",
188
189
  "light-dark",
190
+ "color-layers",
189
191
  "contrast-color",
190
- "alpha",
191
192
  ];
192
193
  const imageFunc = [
193
194
  "linear-gradient",
@@ -1,3 +1,4 @@
1
+ import { isOkLabClose } from './color/utils/distance.js';
1
2
  import { EnumToken, ColorType } from '../ast/types.js';
2
3
  import { walkValues, WalkerOptionEnum } from '../ast/walk.js';
3
4
  import { toDegrees } from '../parser/utils/angle.js';
@@ -7,8 +8,8 @@ import { trimArray } from '../validation/match.js';
7
8
  import { splitTokenList } from '../validation/utils/list.js';
8
9
  import { getColorSpace } from './color/utils/colorspace.js';
9
10
  import { getColorComponents } from './color/utils/components.js';
10
- import { nonStandardColors, systemColors, deprecatedSystemColors, COLORS_NAMES, colorsFunc, colorFuncColorSpace, LOC } from './constants.js';
11
- import { isOkLabClose } from './color/utils/distance.js';
11
+ import { nonStandardColors, systemColors, deprecatedSystemColors, COLORS_NAMES, colorsFunc, colorFuncColorSpace, colorPrecision, epsilon, anglePrecision } from './constants.js';
12
+ import { getSyntaxConfig } from '../validation/config.js';
12
13
 
13
14
  // https://www.w3.org/TR/CSS21/syndata.html#syntax
14
15
  // https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/#typedef-ident-token
@@ -482,7 +483,7 @@ function isColor(token, errors) {
482
483
  action: "drop",
483
484
  message: `Unexpected constant '${val}'`,
484
485
  node: value,
485
- location: value[LOC],
486
+ // location: options.source!.getSourLocation(value[LOC]!.sta),
486
487
  });
487
488
  return false;
488
489
  }
@@ -512,7 +513,7 @@ function isColor(token, errors) {
512
513
  action: "drop",
513
514
  message: `Unexpected constant '${val}'`,
514
515
  node: v.value,
515
- location: v.value[LOC],
516
+ // location: options.source!.getSourLocation(v.value[LOC]!.sta),
516
517
  });
517
518
  return false;
518
519
  }
@@ -560,7 +561,7 @@ function isColor(token, errors) {
560
561
  action: "drop",
561
562
  message: "adding percentage and number is not allowed",
562
563
  node: token,
563
- location: token[LOC],
564
+ // location: options.source!.getSourLocation(token[LOC]!.sta),
564
565
  });
565
566
  return false;
566
567
  }
@@ -588,9 +589,9 @@ function isColor(token, errors) {
588
589
  if (children.length <= offset + 1) {
589
590
  errors?.push({
590
591
  action: "drop",
591
- message: `Invalid color at ${token[LOC]?.src}:${token[LOC]?.sta.lin}:${token[LOC]?.sta.col}`,
592
+ message: `Invalid color`,
592
593
  node: token,
593
- location: token[LOC],
594
+ // location: options.source!.getSourLocation(token[LOC]!.sta),
594
595
  });
595
596
  return false;
596
597
  }
@@ -1061,10 +1062,12 @@ function parseDimension(name) {
1061
1062
  if (index < 0) {
1062
1063
  return null;
1063
1064
  }
1065
+ const unit = name.slice(index);
1064
1066
  const dimension = {
1065
1067
  typ: EnumToken.DimensionTokenType,
1066
1068
  val: +name.slice(0, index),
1067
- unit: name.slice(index),
1069
+ // @ts-ignore
1070
+ unit: getSyntaxConfig().units.find((u) => equalsIgnoreCase(u, unit)) || unit.toLowerCase(),
1068
1071
  };
1069
1072
  if (Number.isNaN(dimension.val)) {
1070
1073
  return null;
@@ -1137,5 +1140,89 @@ function isWhiteSpace(codepoint) {
1137
1140
  codepoint == 0x2028 ||
1138
1141
  codepoint == 0x2029);
1139
1142
  }
1143
+ // https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Values_and_units#absolute_length_units
1144
+ /**
1145
+ * Convert length to px
1146
+ * @param value
1147
+ * @returns
1148
+ */
1149
+ function length2Px(value) {
1150
+ let result = null;
1151
+ if (value.typ == EnumToken.NumberTokenType) {
1152
+ result = +value.val;
1153
+ }
1154
+ else {
1155
+ switch (value.unit) {
1156
+ case "cm":
1157
+ // @ts-ignore
1158
+ result = value.val * 37.8;
1159
+ break;
1160
+ case "mm":
1161
+ // @ts-ignore
1162
+ result = value.val * 3.78;
1163
+ break;
1164
+ case "Q":
1165
+ // @ts-ignore
1166
+ result = (value.val * 37.8) / 40;
1167
+ break;
1168
+ case "in":
1169
+ // @ts-ignore
1170
+ result = value.val / 96;
1171
+ break;
1172
+ case "pc":
1173
+ // @ts-ignore
1174
+ result = value.val / 16;
1175
+ break;
1176
+ case "pt":
1177
+ // @ts-ignore
1178
+ result = (value.val * 4) / 3;
1179
+ break;
1180
+ case "px":
1181
+ result = +value.val;
1182
+ break;
1183
+ }
1184
+ }
1185
+ return isNaN(result) ? null : result;
1186
+ }
1187
+ /**
1188
+ * minify number
1189
+ * @param val
1190
+ */
1191
+ function minifyNumber(val) {
1192
+ val = String(toPrecisionValue(val));
1193
+ if (val === "0") {
1194
+ return "0";
1195
+ }
1196
+ const chr = val.charAt(0);
1197
+ if (chr == "-") {
1198
+ const slice = val.slice(0, 2);
1199
+ if (slice == "-0") {
1200
+ return val.length == 2 ? "0" : "-" + val.slice(2);
1201
+ }
1202
+ }
1203
+ if (chr == "0") {
1204
+ return val.slice(1);
1205
+ }
1206
+ return val;
1207
+ }
1208
+ function toPrecisionValue(value, precision = colorPrecision) {
1209
+ const div = Math.pow(10, precision);
1210
+ // @ts-ignore
1211
+ value = Math.round(value * div) / div;
1212
+ return Math.abs(value) < epsilon ? 0 : value;
1213
+ }
1214
+ function toPrecisionAngle(angle, precision = colorPrecision, correctValue = true) {
1215
+ angle = toPrecisionValue(angle, precision);
1216
+ if (correctValue && Math.abs(angle) >= 360) {
1217
+ angle %= 360;
1218
+ }
1219
+ if (Math.abs(angle) < anglePrecision) {
1220
+ angle = 0;
1221
+ }
1222
+ if (correctValue && angle < 0) {
1223
+ angle += 360;
1224
+ }
1225
+ return angle;
1226
+ }
1140
1227
 
1141
- export { dimensionUnits, isAngle, isColor, isDigit, isFlex, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isLetter, isNewLine, isNumber, isPercentage, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, parseColor, parseDimension, pseudoAliasMap, reduceColorStops, reduceConicColorStops, reducegradientBackgroundPosition, renamedStandardProperties };
1228
+ export { dimensionUnits, isAngle, isColor, isDigit, isFlex, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isLetter, isNewLine, isNumber, isPercentage, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, length2Px, minifyNumber, parseColor, parseDimension, pseudoAliasMap, reduceColorStops, reduceConicColorStops, reducegradientBackgroundPosition, renamedStandardProperties, toPrecisionAngle, toPrecisionValue };
@@ -914,11 +914,14 @@ var declarations = {
914
914
  "flex-grow": {
915
915
  syntax: "<number>"
916
916
  },
917
+ "flex-line-count": {
918
+ syntax: "<integer [1,∞]>"
919
+ },
917
920
  "flex-shrink": {
918
921
  syntax: "<number>"
919
922
  },
920
923
  "flex-wrap": {
921
- syntax: "nowrap | wrap | wrap-reverse"
924
+ syntax: "nowrap | [ wrap | wrap-reverse ] || balance"
922
925
  },
923
926
  float: {
924
927
  syntax: "left | right | none | inline-start | inline-end"
@@ -1184,6 +1187,9 @@ var declarations = {
1184
1187
  "line-height-step": {
1185
1188
  syntax: "<length>"
1186
1189
  },
1190
+ "link-parameters": {
1191
+ syntax: "none | <param()>#"
1192
+ },
1187
1193
  "list-style": {
1188
1194
  syntax: "<'list-style-type'> || <'list-style-position'> || <'list-style-image'>"
1189
1195
  },
@@ -1481,6 +1487,9 @@ var declarations = {
1481
1487
  "paint-order": {
1482
1488
  syntax: "normal | [ fill || stroke || markers ]"
1483
1489
  },
1490
+ "path-length": {
1491
+ syntax: "none | <length> [0,∞]"
1492
+ },
1484
1493
  perspective: {
1485
1494
  syntax: "none | <length>"
1486
1495
  },
@@ -1776,7 +1785,7 @@ var declarations = {
1776
1785
  syntax: "<color>"
1777
1786
  },
1778
1787
  "text-decoration-inset": {
1779
- syntax: "<length>{1,2} | auto"
1788
+ syntax: "<length-percentage>{1,2} | auto"
1780
1789
  },
1781
1790
  "text-decoration-line": {
1782
1791
  syntax: "none | [ underline || overline || line-through || blink ] | spelling-error | grammar-error"
@@ -2338,6 +2347,9 @@ var functions = {
2338
2347
  "palette-mix": {
2339
2348
  syntax: "palette-mix(<color-interpolation-method> , [ [normal | light | dark | <palette-identifier> | <palette-mix()> ] && <percentage [0,100]>? ]#{2})"
2340
2349
  },
2350
+ param: {
2351
+ syntax: "mod( <dashed-ident>, <declaration-value>? )"
2352
+ },
2341
2353
  path: {
2342
2354
  syntax: "path( <'fill-rule'>? , <string> )"
2343
2355
  },
@@ -2929,7 +2941,7 @@ var syntaxes = {
2929
2941
  syntax: "[ historical-ligatures | no-historical-ligatures ]"
2930
2942
  },
2931
2943
  "hsl()": {
2932
- syntax: "hsl( <hue>, <percentage>, <percentage>, <alpha-value>? ) | hsl( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? ) | hsl(from <color> [ <hue> | none | h | s | l ] <relative-hsl-component>{2} [ / [ <alpha-value> | none] ]? )"
2944
+ syntax: "hsl( <hue>, <percentage>, <percentage>, <alpha-value>? ) | hsl( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? ) | hsl(from <color> [ <hue> | none | h | s | l ] <relative-hsl-component>{2} [ / [ <alpha-value> | none] ]? )"
2933
2945
  },
2934
2946
  "hsla()": {
2935
2947
  syntax: "hsla( <hue>, <percentage>, <percentage>, <alpha-value>? ) | hsla( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? ) | hsla(from <color> < [ <hue> | none | h | s | l ] <relative-hsl-component>{2} [ / [ <alpha-value> | none ] ]? )"
@@ -2944,7 +2956,7 @@ var syntaxes = {
2944
2956
  syntax: "hue-rotate( [ <angle> | <zero> ]? )"
2945
2957
  },
2946
2958
  "hwb()": {
2947
- syntax: "hwb( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? ) | hwb(from <color> [ <hue> | none | h | w | b ] <relative-hwb-component>{2} [ / [ <alpha-value> | none] ]? )"
2959
+ syntax: "hwb( [ <hue> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? ) | hwb(from <color> [ <hue> | none | h | w | b ] <relative-hwb-component>{2} [ / [ <alpha-value> | none] ]? )"
2948
2960
  },
2949
2961
  "hypot()": {
2950
2962
  syntax: "hypot( <calc-sum># )"
@@ -3319,7 +3331,7 @@ var syntaxes = {
3319
3331
  syntax: "reversed( <counter-name> )"
3320
3332
  },
3321
3333
  "rgb()": {
3322
- syntax: "rgb( <percentage>#{3} , <alpha-value>? ) | rgb( <number>#{3} , <alpha-value>? ) | rgb( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ) | rgb(from <color> <relative-rgb-component>{3} [ / [ <alpha-value> | none] ]? )"
3334
+ syntax: "rgb( <percentage>#{3} , <alpha-value>? ) | rgb( <number>#{3} , <alpha-value>? ) | rgb( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ) | rgb(from <color> <relative-rgb-component>{3} [ / [ <alpha-value> | none] ]? )"
3323
3335
  },
3324
3336
  "rgba()": {
3325
3337
  syntax: "rgba( <percentage>#{3} , <alpha-value>? ) | rgba( <number>#{3} , <alpha-value>? ) | rgba( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ) | rgba(from <color> <relative-rgb-component>{3} [ / [ <alpha-value> | none ] ]? )"
@@ -3888,8 +3900,17 @@ var syntaxes = {
3888
3900
  "composes-selector": {
3889
3901
  syntax: "<ident>+ [from [global&&<string>]]?"
3890
3902
  },
3903
+ wcag2: {
3904
+ syntax: "wcag2 | wcag2([<number> | [ aa | aaa ] && large? ])"
3905
+ },
3906
+ "target-contrast": {
3907
+ syntax: "<wcag2>"
3908
+ },
3891
3909
  "contrast-color()": {
3892
- syntax: "contrast-color(<color> )"
3910
+ syntax: "contrast-color( [ [ <color> && [ tbd-fg | tbd-bg ] && <target-contrast>? ] | [ <color> && [ tbd-fg | tbd-bg ] && <target-contrast>, <color># ] ] )"
3911
+ },
3912
+ "color-layers()": {
3913
+ syntax: "color-layers([ <blend-mode>, ]? <color># )"
3893
3914
  },
3894
3915
  "font-feature-custom-ident": {
3895
3916
  syntax: "<integer>"