@tbela99/css-parser 1.6.0 → 1.6.2

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 (48) hide show
  1. package/CHANGELOG.md +118 -2
  2. package/README.md +4 -3
  3. package/dist/index-umd-web.js +1629 -467
  4. package/dist/index.cjs +1629 -467
  5. package/dist/index.d.ts +72 -7
  6. package/dist/lib/ast/features/prefix.js +9 -1
  7. package/dist/lib/ast/features/shorthand.js +1 -1
  8. package/dist/lib/ast/features/transform.js +1 -1
  9. package/dist/lib/ast/math/expression.js +175 -5
  10. package/dist/lib/ast/minify.js +84 -10
  11. package/dist/lib/ast/transform/compute.js +24 -55
  12. package/dist/lib/ast/transform/minify.js +1 -1
  13. package/dist/lib/ast/types.js +12 -0
  14. package/dist/{config.json.js → lib/data/properties.json.js} +106 -0
  15. package/dist/lib/{validation/config.json.js → data/validation.json.js} +11 -8
  16. package/dist/lib/parser/declaration/list.js +8 -10
  17. package/dist/lib/parser/parse.js +8 -14
  18. package/dist/lib/parser/tokenize.js +89 -58
  19. package/dist/lib/parser/utils/at-rule-font-feature-values.js +1 -1
  20. package/dist/lib/parser/utils/at-rule-media.js +66 -0
  21. package/dist/lib/parser/utils/at-rule-when-else.js +2 -2
  22. package/dist/lib/parser/utils/base64.js +14 -0
  23. package/dist/lib/parser/utils/config.js +1 -1
  24. package/dist/lib/parser/utils/declaration.js +18 -17
  25. package/dist/lib/parser/utils/eq.js +6 -0
  26. package/dist/lib/parser/utils/hash.js +1 -0
  27. package/dist/lib/parser/utils/selector.js +99 -89
  28. package/dist/lib/parser/utils/text.js +16 -0
  29. package/dist/lib/renderer/render.js +20 -6
  30. package/dist/lib/renderer/sourcemap/lib/codec.js +8 -3
  31. package/dist/lib/renderer/sourcemap/sourcemap.js +35 -5
  32. package/dist/lib/syntax/color/alpha.js +57 -22
  33. package/dist/lib/syntax/color/cmyk.js +4 -3
  34. package/dist/lib/syntax/color/color-mix.js +18 -18
  35. package/dist/lib/syntax/color/srgb.js +11 -5
  36. package/dist/lib/syntax/color/utils/components.js +1 -1
  37. package/dist/lib/syntax/color/utils/distance.js +2 -2
  38. package/dist/lib/syntax/color/utils/matrix.js +1 -0
  39. package/dist/lib/syntax/color/xyz.js +5 -0
  40. package/dist/lib/syntax/color/xyzd50.js +17 -0
  41. package/dist/lib/syntax/constants.js +1 -1
  42. package/dist/lib/syntax/syntax.js +549 -13
  43. package/dist/lib/validation/config.js +27 -1
  44. package/dist/lib/validation/match.js +16 -4
  45. package/dist/lib/validation/parser/parse.js +7 -3
  46. package/dist/node.js +8 -14
  47. package/dist/web.js +8 -14
  48. package/package.json +4 -4
@@ -6,7 +6,6 @@ import { getColorType } from './utils/color-type.js';
6
6
  import { equalsIgnoreCase } from '../../parser/utils/text.js';
7
7
  import { cloneNode } from '../../ast/clone.js';
8
8
  import { walkValues } from '../../ast/walk.js';
9
- import { replaceNodeOrValue } from '../../parser/utils/token.js';
10
9
  import { evaluate } from '../../ast/math/expression.js';
11
10
 
12
11
  /**
@@ -19,40 +18,76 @@ function alpha(color, alpha) {
19
18
  if (alpha == null) {
20
19
  return color;
21
20
  }
21
+ // https://www.w3.org/TR/css-color-5/#:~:text=There%20is%20no%20relative%20device%2Dcmyk%28%29%20syntax
22
22
  if (color.kin === ColorType.DEVICE_CMYK) {
23
23
  return null;
24
24
  }
25
+ let components = getColorComponents(color);
26
+ if (alpha.typ === EnumToken.MathFunctionTokenType) {
27
+ const originalAlpha = cloneNode(alpha, true);
28
+ for (const { value } of walkValues(alpha.chi, alpha)) {
29
+ if (value.typ === EnumToken.IdenTokenType) {
30
+ if (equalsIgnoreCase(value.val, "alpha")) {
31
+ Object.assign(value, components?.[3]
32
+ ? cloneNode(components[3], true)
33
+ : {
34
+ typ: EnumToken.NumberTokenType,
35
+ val: 1,
36
+ });
37
+ // continue;
38
+ }
39
+ else if (equalsIgnoreCase(value.val, "none")) {
40
+ Object.assign(value, {
41
+ typ: EnumToken.NumberTokenType,
42
+ val: 0,
43
+ });
44
+ }
45
+ }
46
+ }
47
+ const result = evaluate([alpha]);
48
+ if (result.length == 1) {
49
+ alpha = result[0];
50
+ }
51
+ else {
52
+ // @ts-expect-error
53
+ alpha = originalAlpha;
54
+ }
55
+ }
56
+ // console.error({ alpha });
57
+ if (alpha.typ !== EnumToken.IdenTokenType &&
58
+ alpha.typ !== EnumToken.NumberTokenType &&
59
+ alpha.typ !== EnumToken.PercentageTokenType) {
60
+ return null;
61
+ }
62
+ if (alpha.typ === EnumToken.IdenTokenType) {
63
+ if (equalsIgnoreCase(alpha.val, "NaN") ||
64
+ equalsIgnoreCase(alpha.val, "Infinity") ||
65
+ equalsIgnoreCase(alpha.val, "-Infinity")) {
66
+ return null;
67
+ }
68
+ }
25
69
  if (color.kin === ColorType.COLOR_MIX || color.cal === "rel") {
26
70
  color = convertColor(color, getColorType(color));
27
71
  if (color == null) {
28
72
  return null;
29
73
  }
74
+ components = getColorComponents(color);
30
75
  }
31
- const components = getColorComponents(color);
32
76
  if (components == null) {
33
77
  return null;
34
78
  }
35
- if (alpha?.typ === EnumToken.IdenTokenType && equalsIgnoreCase(alpha.val, "alpha")) {
36
- alpha = components[3] ?? {
37
- typ: EnumToken.NumberTokenType,
38
- val: 1,
39
- };
40
- }
41
- else if (alpha.typ === EnumToken.MathFunctionTokenType &&
42
- equalsIgnoreCase(alpha.val, "calc")) {
43
- alpha = cloneNode(alpha, true);
44
- const alphaValue = components[3] ?? {
45
- typ: EnumToken.NumberTokenType,
46
- val: 1,
47
- };
48
- for (const { value, parent } of walkValues(alpha.chi, alpha)) {
49
- if (value.typ === EnumToken.IdenTokenType && equalsIgnoreCase(value.val, "alpha")) {
50
- replaceNodeOrValue(parent, value, alphaValue);
51
- }
79
+ if (alpha?.typ === EnumToken.IdenTokenType) {
80
+ if (equalsIgnoreCase(alpha.val, "alpha")) {
81
+ alpha = components[3] ?? {
82
+ typ: EnumToken.NumberTokenType,
83
+ val: 1,
84
+ };
52
85
  }
53
- const result = evaluate([alpha]);
54
- if (result.length == 1) {
55
- alpha = result[0];
86
+ else if (equalsIgnoreCase(alpha.val, "node")) {
87
+ alpha = {
88
+ typ: EnumToken.NumberTokenType,
89
+ val: 0,
90
+ };
56
91
  }
57
92
  }
58
93
  return makeColor(color.kin, components, alpha);
@@ -61,9 +61,10 @@ function color2cmykToken(token) {
61
61
  }
62
62
  function srgb2cmykvalues(r, g, b, a = null) {
63
63
  const k = 1 - Math.max(r, g, b);
64
- const c = k == 1 ? 0 : (1 - r - k) / (1 - k);
65
- const m = k == 1 ? 0 : (1 - g - k) / (1 - k);
66
- const y = k == 1 ? 0 : (1 - b - k) / (1 - k);
64
+ const div = 1 - k;
65
+ const c = k == 1 ? 0 : (1 - r - k) / div;
66
+ const m = k == 1 ? 0 : (1 - g - k) / div;
67
+ const y = k == 1 ? 0 : (1 - b - k) / div;
67
68
  const result = [c, m, y, k];
68
69
  if (a != null && a < 1) {
69
70
  result.push(a);
@@ -68,7 +68,7 @@ function colorMix(...args) {
68
68
  let colorSpace = "oklab";
69
69
  let hueInterpolationMethod = "shorter";
70
70
  let values = null;
71
- const colors = [];
71
+ // const colors: ColorToken[] = [];
72
72
  const percentages = [];
73
73
  const srgbComponentValues = [];
74
74
  const colorComponents = [];
@@ -177,7 +177,8 @@ function colorMix(...args) {
177
177
  return null;
178
178
  }
179
179
  srgbComponentValues.push(values);
180
- colors.push(args[i++]);
180
+ // colors.push(args[i++] as ColorToken);
181
+ i++;
181
182
  if (i >= args.length) {
182
183
  missingPercentageCount++;
183
184
  percentages.push(null);
@@ -219,9 +220,16 @@ function colorMix(...args) {
219
220
  for (i = 0; i < percentages.length; i++) {
220
221
  percentages[i] = percentages[i] / perc;
221
222
  }
222
- totalPercentage = 1;
223
223
  }
224
- i = colors.length;
224
+ const stack = [];
225
+ const lchSpaces = ["lch", "oklch"];
226
+ i = srgbComponentValues.length;
227
+ while (i--) {
228
+ stack.push({
229
+ color: srgbComponentValues[i],
230
+ alpha: percentages[i],
231
+ });
232
+ }
225
233
  let currentIndex = 0;
226
234
  let r1;
227
235
  let r2;
@@ -232,18 +240,11 @@ function colorMix(...args) {
232
240
  let premult1;
233
241
  let premult2;
234
242
  let mixedPremult;
235
- let colorSpace1;
236
- const stack = [];
237
- const lchSpaces = ["lch", "oklch"];
238
- i = srgbComponentValues.length;
239
- while (i--) {
240
- stack.push({
241
- color: srgbComponentValues[i],
242
- alpha: percentages[i],
243
- });
244
- }
243
+ let colorSpace1 = ColorType[
244
+ // @ts-expect-error
245
+ colorComponents.at(-1).kin
245
246
  // @ts-expect-error
246
- colorSpace1 = ColorType[colorComponents.at(-1).kin]?.toLowerCase?.();
247
+ ]?.toLowerCase?.();
247
248
  if (colorComponents[0][3] != null &&
248
249
  colorComponents[0][3].typ == EnumToken.IdenTokenType &&
249
250
  colorComponents[0][3].val == "none" &&
@@ -325,14 +326,13 @@ function colorMix(...args) {
325
326
  else {
326
327
  values = xyz2lchvalues(values[0], values[1], values[2], values[3]);
327
328
  }
328
- // @ts-ignore
329
329
  return {
330
330
  typ: EnumToken.ColorTokenType,
331
331
  val: "lch",
332
- chi: values.map((v) => {
332
+ chi: values.map((val) => {
333
333
  return {
334
334
  typ: EnumToken.NumberTokenType,
335
- val: v,
335
+ val,
336
336
  };
337
337
  }),
338
338
  kin: ColorType.LCH,
@@ -56,6 +56,15 @@ function rgb2srgbvalues(token) {
56
56
  ? getNumber(t)
57
57
  : getNumber(t) / 255) ?? null);
58
58
  }
59
+ // export function rgbvalues2srgbvalues(r: number, g: number, b: number, a: number | null = null): number[] | null {
60
+ // const result = [r / 255, g / 255, b / 255];
61
+ //
62
+ // if (a != null && a != 1) {
63
+ // result.push(a);
64
+ // }
65
+ //
66
+ // return result;
67
+ // }
59
68
  function hex2srgbvalues(token) {
60
69
  const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
61
70
  const rgb = [];
@@ -116,11 +125,8 @@ function cmyk2srgbvalues(token) {
116
125
  t = components[3];
117
126
  // @ts-ignore
118
127
  const k = getNumber(t);
119
- const rgb = [
120
- 1 - Math.min(1, c * (1 - k) + k),
121
- 1 - Math.min(1, m * (1 - k) + k),
122
- 1 - Math.min(1, y * (1 - k) + k),
123
- ];
128
+ const mul = 1 - k;
129
+ const rgb = [1 - Math.min(1, c * mul + k), 1 - Math.min(1, m * mul + k), 1 - Math.min(1, y * mul + k)];
124
130
  if (components.length == 5) {
125
131
  rgb.push(getNumber(components[4]));
126
132
  }
@@ -12,7 +12,7 @@ function getColorComponents(token) {
12
12
  // }
13
13
  // }
14
14
  if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
15
- if (equalsIgnoreCase('currentcolor', token.val)) {
15
+ if (equalsIgnoreCase("currentcolor", token.val)) {
16
16
  return null;
17
17
  }
18
18
  const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
@@ -6,11 +6,11 @@ import { toPrecisionValue } from '../../syntax.js';
6
6
 
7
7
  /**
8
8
  * Calculate the distance between two okLab colors.
9
- * @param okLab1
10
- * @param okLab2
11
9
  *
12
10
  * @private
13
11
  * {@link https://drafts.csswg.org/css-color-4/#comparing-color-values}
12
+ * @param color1
13
+ * @param color2
14
14
  */
15
15
  function okLabDistance(color1, color2) {
16
16
  color1 = convertColor(color1, ColorType.OKLAB);
@@ -6,6 +6,7 @@
6
6
  */
7
7
  // A is m x n. B is n x p. product is m x p.
8
8
  function multiplyMatrices(A, B) {
9
+ // let m: number = A.length;
9
10
  // if (!Array.isArray(A[0])) {
10
11
  // // A is vector, convert to [[a, b, c, ...]]
11
12
  // A = <number[][]>[A];
@@ -2,6 +2,11 @@ import { multiplyMatrices } from './utils/matrix.js';
2
2
  import { srgb2lsrgbvalues } from './srgb.js';
3
3
  import { XYZ_D65_to_D50 } from './xyzd50.js';
4
4
 
5
+ // export function lab2xyz(l: number, a: number, b: number, alpha?: number): number[] {
6
+ // const [x, y, z] = Lab_to_XYZ(l, a, b);
7
+ //
8
+ // return alpha == null || alpha == 1 ? [x, y, z] : [x, y, z, alpha];
9
+ // }
5
10
  function XYZ_to_lin_sRGB(x, y, z, alpha = null) {
6
11
  // convert XYZ to linear-light sRGB
7
12
  const M = [
@@ -4,6 +4,23 @@ import { xyz2lab } from './lab.js';
4
4
  import { XYZ_D50_to_D65 } from './xyz.js';
5
5
  import { labvalues2lchvalues } from './lch.js';
6
6
 
7
+ // export function srgb2xyzd50values(r: number, g: number, b: number, alpha: number | null = null): number[] {
8
+ // [r, g, b] = srgb2lsrgbvalues(r, g, b);
9
+ //
10
+ // const rgb: number[] = [
11
+ // 0.436065742824811 * r + 0.3851514688337912 * g + 0.14307845442264197 * b,
12
+ //
13
+ // 0.22249319175623702 * r + 0.7168870538238823 * g + 0.06061979053616537 * b,
14
+ //
15
+ // 0.013923904500943465 * r + 0.09708128566574634 * g + 0.7140993584005155 * b,
16
+ // ];
17
+ //
18
+ // if (alpha != null && alpha != 1) {
19
+ // rgb.push(alpha);
20
+ // }
21
+ //
22
+ // return rgb;
23
+ // }
7
24
  /*
8
25
  */
9
26
  function xyzd502lch(x, y, z, alpha) {
@@ -1,5 +1,5 @@
1
1
  import { EnumToken } from '../ast/types.js';
2
- import config from '../validation/config.json.js';
2
+ import config from '../data/validation.json.js';
3
3
 
4
4
  /**
5
5
  * Location source id