@tbela99/css-parser 1.4.11 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +18 -11
  3. package/dist/index-umd-web.js +6558 -5310
  4. package/dist/index.cjs +6576 -5313
  5. package/dist/index.d.ts +805 -279
  6. package/dist/lib/ast/clone.js +13 -8
  7. package/dist/lib/ast/expand.js +65 -15
  8. package/dist/lib/ast/features/calc.js +19 -56
  9. package/dist/lib/ast/features/if.js +28 -4
  10. package/dist/lib/ast/features/inlinecssvariables.js +6 -5
  11. package/dist/lib/ast/features/prefix.js +52 -73
  12. package/dist/lib/ast/features/shorthand.js +11 -6
  13. package/dist/lib/ast/features/transform.js +6 -3
  14. package/dist/lib/ast/find.js +7 -5
  15. package/dist/lib/ast/math/expression.js +190 -32
  16. package/dist/lib/ast/minify.js +117 -49
  17. package/dist/lib/ast/node.js +52 -0
  18. package/dist/lib/ast/transform/compute.js +63 -4
  19. package/dist/lib/ast/transform/minify.js +6 -6
  20. package/dist/lib/ast/transform/perspective.js +1 -1
  21. package/dist/lib/ast/transform/rotate.js +1 -1
  22. package/dist/lib/ast/transform/scale.js +1 -1
  23. package/dist/lib/ast/transform/skew.js +1 -1
  24. package/dist/lib/ast/transform/translate.js +1 -1
  25. package/dist/lib/ast/transform/utils.js +46 -18
  26. package/dist/lib/ast/types.js +1 -1
  27. package/dist/lib/ast/walk.js +55 -11
  28. package/dist/lib/fs/resolve.js +68 -27
  29. package/dist/lib/parser/declaration/list.js +22 -7
  30. package/dist/lib/parser/declaration/map.js +40 -13
  31. package/dist/lib/parser/declaration/set.js +3 -1
  32. package/dist/lib/parser/linesmap.js +2 -12
  33. package/dist/lib/parser/parse.js +843 -912
  34. package/dist/lib/parser/source.js +16 -2
  35. package/dist/lib/parser/tokenize.js +1489 -725
  36. package/dist/lib/parser/utils/at-rule-container.js +29 -196
  37. package/dist/lib/parser/utils/at-rule-generic.js +6 -7
  38. package/dist/lib/parser/utils/at-rule-import.js +17 -13
  39. package/dist/lib/parser/utils/at-rule-media.js +36 -19
  40. package/dist/lib/parser/utils/at-rule-support.js +19 -9
  41. package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
  42. package/dist/lib/parser/utils/at-rule.js +0 -18
  43. package/dist/lib/parser/utils/declaration.js +30 -95
  44. package/dist/lib/parser/utils/hash.js +4 -4
  45. package/dist/lib/parser/utils/selector.js +164 -141
  46. package/dist/lib/parser/utils/text.js +4 -2
  47. package/dist/lib/renderer/render.js +230 -125
  48. package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
  49. package/dist/lib/renderer/sourcemap/sourcemap.js +187 -34
  50. package/dist/lib/syntax/color/a98rgb.js +14 -4
  51. package/dist/lib/syntax/color/cmyk.js +10 -18
  52. package/dist/lib/syntax/color/color-mix.js +21 -34
  53. package/dist/lib/syntax/color/color.js +32 -49
  54. package/dist/lib/syntax/color/hsl.js +15 -15
  55. package/dist/lib/syntax/color/hwb.js +18 -17
  56. package/dist/lib/syntax/color/lab.js +17 -17
  57. package/dist/lib/syntax/color/lch.js +13 -13
  58. package/dist/lib/syntax/color/oklab.js +10 -11
  59. package/dist/lib/syntax/color/oklch.js +15 -18
  60. package/dist/lib/syntax/color/p3.js +18 -12
  61. package/dist/lib/syntax/color/prophotorgb.js +26 -28
  62. package/dist/lib/syntax/color/rec2020.js +8 -4
  63. package/dist/lib/syntax/color/relative-color.js +26 -10
  64. package/dist/lib/syntax/color/srgb.js +5 -4
  65. package/dist/lib/syntax/color/utils/distance.js +1 -1
  66. package/dist/lib/syntax/color/xyz.js +2 -2
  67. package/dist/lib/syntax/color/xyzd50.js +2 -2
  68. package/dist/lib/syntax/constants.js +12 -2
  69. package/dist/lib/syntax/syntax.js +45 -258
  70. package/dist/lib/validation/config.json.js +3 -0
  71. package/dist/lib/validation/match.js +110 -27
  72. package/dist/node.js +55 -33
  73. package/dist/types.d.ts +5 -1
  74. package/dist/types.js +4 -0
  75. package/dist/utils/sync.d.ts +16 -0
  76. package/dist/utils/sync.js +73 -0
  77. package/dist/web.js +37 -30
  78. package/package.json +1 -1
  79. package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
  80. package/llms.txt +0 -73
@@ -3,12 +3,16 @@ import { multiplyMatrices } from './utils/matrix.js';
3
3
  import { srgb2xyz } from './xyz.js';
4
4
 
5
5
  function rec20202srgb(r, g, b, a) {
6
+ let values = rec20202lrec2020(r, g, b);
7
+ values = lrec20202xyz(values[0], values[1], values[2]);
6
8
  // @ts-ignore
7
- return xyz2srgb(...lrec20202xyz(...rec20202lrec2020(r, g, b)), a);
9
+ return xyz2srgb(values[0], values[1], values[2], a);
8
10
  }
9
11
  function srgb2rec2020values(r, g, b, a) {
12
+ let values = srgb2xyz(r, g, b);
13
+ values = xyz2lrec2020(values[0], values[1], values[2]);
10
14
  // @ts-ignore
11
- return lrec20202rec2020(...xyz2lrec2020(...srgb2xyz(r, g, b)), a);
15
+ return lrec20202rec2020(values[0], values[1], values[2], a);
12
16
  }
13
17
  function rec20202lrec2020(r, g, b, a) {
14
18
  // convert an array of rec2020 RGB values in the range 0.0 - 1.0
@@ -54,7 +58,7 @@ function lrec20202xyz(r, g, b, a) {
54
58
  [0, 19567812 / 697040785, 295819943 / 278816314],
55
59
  ];
56
60
  // 0 is actually calculated as 4.994106574466076e-17
57
- return multiplyMatrices(M, [r, g, b]).concat(a == null || a == 1 ? [] : [a]);
61
+ return multiplyMatrices(M, [r, g, b]).concat([] );
58
62
  }
59
63
  function xyz2lrec2020(x, y, z, a) {
60
64
  // convert XYZ to linear-light rec2020
@@ -63,7 +67,7 @@ function xyz2lrec2020(x, y, z, a) {
63
67
  [-19765991 / 29648200, 47925759 / 29648200, 467509 / 29648200],
64
68
  [792561 / 44930125, -1921689 / 44930125, 42328811 / 44930125],
65
69
  ];
66
- return multiplyMatrices(M, [x, y, z]).concat(a == null || a == 1 ? [] : [a]);
70
+ return multiplyMatrices(M, [x, y, z]).concat([] );
67
71
  }
68
72
 
69
73
  export { rec20202srgb, srgb2rec2020values };
@@ -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, LOC, colorRange, mathFuncs } from '../constants.js';
5
+ import { colorsFunc, colorFuncColorSpace, LOCEND, LOCSTA, LOCSRCID, colorRange, mathFuncs } from '../constants.js';
6
6
  import { equalsIgnoreCase } from '../../parser/utils/text.js';
7
7
  import { getColorComponents } from './utils/components.js';
8
8
 
@@ -34,7 +34,9 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
34
34
  const validKeys = names.split("");
35
35
  let val = "";
36
36
  if (components != null) {
37
- allComponents.push(...components);
37
+ for (const component of components) {
38
+ allComponents.push(component);
39
+ }
38
40
  }
39
41
  // ensure all components are valid for the color space
40
42
  for (const component of allComponents) {
@@ -103,19 +105,25 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
103
105
  ? {
104
106
  typ: EnumToken.NumberTokenType,
105
107
  val: 1,
106
- [LOC]: b[LOC],
108
+ [LOCSRCID]: b[LOCSRCID],
109
+ [LOCSTA]: b[LOCSTA],
110
+ [LOCEND]: b[LOCEND],
107
111
  }
108
112
  : alpha.typ == EnumToken.IdenTokenType && alpha.val == "none"
109
113
  ? {
110
114
  typ: EnumToken.NumberTokenType,
111
115
  val: 0,
112
- [LOC]: alpha[LOC],
116
+ [LOCSRCID]: alpha[LOCSRCID],
117
+ [LOCSTA]: alpha[LOCSTA],
118
+ [LOCEND]: alpha[LOCEND],
113
119
  }
114
120
  : alpha.typ == EnumToken.PercentageTokenType
115
121
  ? {
116
122
  typ: EnumToken.NumberTokenType,
117
123
  val: getNumber(alpha),
118
- [LOC]: alpha[LOC],
124
+ [LOCSRCID]: alpha[LOCSRCID],
125
+ [LOCSTA]: alpha[LOCSTA],
126
+ [LOCEND]: alpha[LOCEND],
119
127
  }
120
128
  : alpha,
121
129
  };
@@ -128,13 +136,17 @@ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp,
128
136
  ? {
129
137
  typ: EnumToken.NumberTokenType,
130
138
  val: 1,
131
- [LOC]: bExp[LOC],
139
+ [LOCSRCID]: bExp[LOCSRCID],
140
+ [LOCSTA]: bExp[LOCSTA],
141
+ [LOCEND]: bExp[LOCEND],
132
142
  }
133
143
  : aExp.typ == EnumToken.IdenTokenType && aExp.val == "none"
134
144
  ? {
135
145
  typ: EnumToken.NumberTokenType,
136
146
  val: 0,
137
- [LOC]: aExp[LOC],
147
+ [LOCSRCID]: aExp[LOCSRCID],
148
+ [LOCSTA]: aExp[LOCSTA],
149
+ [LOCEND]: aExp[LOCEND],
138
150
  }
139
151
  : aExp),
140
152
  };
@@ -165,7 +177,9 @@ function getValue(t, converted, component) {
165
177
  return {
166
178
  typ: EnumToken.NumberTokenType,
167
179
  val: value,
168
- [LOC]: t[LOC],
180
+ [LOCSRCID]: t[LOCSRCID],
181
+ [LOCSTA]: t[LOCSTA],
182
+ [LOCEND]: t[LOCEND],
169
183
  };
170
184
  }
171
185
  return t;
@@ -208,8 +222,10 @@ function computeComponentValue(expr, values) {
208
222
  {
209
223
  typ: EnumToken.NumberTokenType,
210
224
  // @ts-ignore
211
- val: "" + Math[value.val.toUpperCase()],
212
- [LOC]: value[LOC],
225
+ val: Math[value.val.toUpperCase()],
226
+ [LOCSRCID]: value[LOCSRCID],
227
+ [LOCSTA]: value[LOCSTA],
228
+ [LOCEND]: value[LOCEND],
213
229
  // @ts-ignore
214
230
  });
215
231
  }
@@ -69,8 +69,9 @@ function hex2srgbvalues(token) {
69
69
  }
70
70
  // xyz d65 input
71
71
  function xyz2srgb(x, y, z, alpha = null) {
72
+ let values = XYZ_to_lin_sRGB(x, y, z);
72
73
  // @ts-ignore
73
- return lsrgb2srgbvalues(...XYZ_to_lin_sRGB(x, y, z, alpha));
74
+ return lsrgb2srgbvalues(values[0], values[1], values[2], alpha);
74
75
  }
75
76
  function hwb2srgbvalues(token) {
76
77
  const { h: hue, s: white, l: black, a: alpha } = hslvalues(token) ?? {};
@@ -141,8 +142,8 @@ function oklch2srgbvalues(token) {
141
142
  if (l == null || c == null || h == null) {
142
143
  return null;
143
144
  }
144
- // @ts-ignore
145
- const rgb = OKLab_to_sRGB(...lchvalues2labvalues(l, c, h));
145
+ const values = lchvalues2labvalues(l, c, h);
146
+ const rgb = OKLab_to_sRGB(values[0], values[1], values[2]);
146
147
  if (alpha != 1) {
147
148
  rgb.push(alpha);
148
149
  }
@@ -243,7 +244,7 @@ function lch2srgbvalues(token) {
243
244
  return null;
244
245
  }
245
246
  // @ts-ignore
246
- const [l, a, b, alpha] = lchvalues2labvalues(...components);
247
+ const [l, a, b, alpha] = lchvalues2labvalues(components[0], components[1], components[2], components[3]);
247
248
  if (l == null || a == null || b == null) {
248
249
  return null;
249
250
  }
@@ -28,7 +28,7 @@ function okLabDistance(color1, color2) {
28
28
  if (okLab1[3] != null || okLab2[3] != null) {
29
29
  diff.push((okLab1[3] ?? 1) - (okLab2[3] ?? 1));
30
30
  }
31
- return toPrecisionValue(Math.hypot(...diff));
31
+ return toPrecisionValue(Math.hypot(diff[0], diff[1], diff[2], diff[3] ?? 0));
32
32
  }
33
33
  /**
34
34
  * Check if two colors are close in okLab space.
@@ -41,8 +41,8 @@ function srgb2xyz(r, g, b, alpha) {
41
41
  // xyz d50
42
42
  function srgb2xyz_d65(r, g, b, alpha) {
43
43
  // xyx d65
44
- // @ts-ignore
45
- let rgb = XYZ_D65_to_D50(...srgb2xyz(r, g, b));
44
+ let values = srgb2xyz(r, g, b);
45
+ let rgb = XYZ_D65_to_D50(values[0], values[1], values[2]);
46
46
  if (alpha != null && alpha != 1) {
47
47
  rgb.push(alpha);
48
48
  }
@@ -7,8 +7,8 @@ import { labvalues2lchvalues } from './lch.js';
7
7
  /*
8
8
  */
9
9
  function xyzd502lch(x, y, z, alpha) {
10
- // @ts-ignore
11
- const [l, a, b] = xyz2lab(...XYZ_D50_to_D65(x, y, z));
10
+ const values = XYZ_D50_to_D65(x, y, z);
11
+ const [l, a, b] = xyz2lab(values[0], values[1], values[2]);
12
12
  // L in range [0,100]. For use in CSS, add a percent
13
13
  return labvalues2lchvalues(l, a, b, alpha);
14
14
  }
@@ -1,6 +1,15 @@
1
1
  import { EnumToken } from '../ast/types.js';
2
2
  import config from '../validation/config.json.js';
3
3
 
4
+ /**
5
+ * Location source id
6
+ */
7
+ const LOCSRCID = Symbol.for("locSrcId");
8
+ const LOCSTA = Symbol.for("locSta");
9
+ const LOCEND = Symbol.for("locEnd");
10
+ /**
11
+ * Used by the validation parser
12
+ */
4
13
  const LOC = Symbol.for("loc");
5
14
  const RAW = Symbol.for("raw");
6
15
  const STATE = Symbol.for("state");
@@ -53,7 +62,7 @@ const colorPrecision = 6;
53
62
  /**
54
63
  * Angle precision
55
64
  */
56
- const anglePrecision = 0.001;
65
+ const anglePrecision = 3;
57
66
  /**
58
67
  * Color range definitions
59
68
  */
@@ -109,6 +118,7 @@ const mathFuncs = [
109
118
  "acos",
110
119
  "atan",
111
120
  "atan2",
121
+ "tan",
112
122
  "pow",
113
123
  "sqrt",
114
124
  "hypot",
@@ -484,4 +494,4 @@ const trimTokenSpace = new Set([
484
494
  ]);
485
495
  const combinators = ["+", ">", "~", "||", "|"];
486
496
 
487
- export { COLORS_NAMES, D50, ERRORS, LOC, NAMES_COLORS, OPTIMIZED, PARENT, PROPERTYNAME, RAW, ROOT, STATE, TOKENS, anglePrecision, colorDistancePrecision, colorFuncColorSpace, colorPrecision, colorRange, colorsFunc, combinators, containerFunc, deprecatedSystemColors, e, epsilon, funcLike, gridTemplateFunc, imageFunc, k, mFGT, mFLT, mathFuncs, mediaTypes, nonStandardColors, pageMarginBoxType, pseudoElements, regMatchLinearGradient, regMatchRadialGradient, supportFunc, systemColors, timelineFunc, timingFunc, tokensMap, tokensfuncDefMap, tokensfuncSet, transformFunctions, trimTokenSpace, urlFunc, urlTokenMatcher, whenElseFunc, wildCardFuncs };
497
+ export { COLORS_NAMES, D50, ERRORS, LOC, LOCEND, LOCSRCID, LOCSTA, NAMES_COLORS, OPTIMIZED, PARENT, PROPERTYNAME, RAW, ROOT, STATE, TOKENS, anglePrecision, colorDistancePrecision, colorFuncColorSpace, colorPrecision, colorRange, colorsFunc, combinators, containerFunc, deprecatedSystemColors, e, epsilon, funcLike, gridTemplateFunc, imageFunc, k, mFGT, mFLT, mathFuncs, mediaTypes, nonStandardColors, pageMarginBoxType, pseudoElements, regMatchLinearGradient, regMatchRadialGradient, supportFunc, systemColors, timelineFunc, timingFunc, tokensMap, tokensfuncDefMap, tokensfuncSet, transformFunctions, trimTokenSpace, urlFunc, urlTokenMatcher, whenElseFunc, wildCardFuncs };