@tbela99/css-parser 1.4.2 → 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 (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -1,16 +1,22 @@
1
1
  import { convertColor, getNumber } from './color.js';
2
- import { ColorType, EnumToken } from '../../ast/types.js';
3
- import '../../ast/minify.js';
2
+ import { EnumToken, ColorType } from '../../ast/types.js';
4
3
  import { walkValues } from '../../ast/walk.js';
5
- import '../../parser/parse.js';
6
- import '../../parser/tokenize.js';
7
- import '../../parser/utils/config.js';
8
- import { mathFuncs } from '../syntax.js';
9
- import { colorRange } from './utils/constants.js';
10
4
  import { evaluateFunc, evaluate } from '../../ast/math/expression.js';
11
- import '../../renderer/sourcemap/lib/encode.js';
5
+ import { colorsFunc, colorFuncColorSpace, colorRange, mathFuncs } from '../constants.js';
6
+ import { equalsIgnoreCase } from '../../parser/utils/text.js';
7
+ import { getColorComponents } from './utils/components.js';
12
8
 
13
- function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
9
+ /**
10
+ * Parse relative color components
11
+ * @param relativeKeys
12
+ * @param original
13
+ * @param rExp
14
+ * @param gExp
15
+ * @param bExp
16
+ * @param aExp
17
+ * @returns
18
+ */
19
+ function parseRelativeColorComponents(relativeKeys, original, rExp, gExp, bExp, aExp) {
14
20
  let r;
15
21
  let g;
16
22
  let b;
@@ -18,88 +24,194 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
18
24
  let keys = {};
19
25
  let values = {};
20
26
  // colorFuncColorSpace x,y,z or r,g,b
21
- const names = relativeKeys.startsWith('xyz') ? 'xyz' : ['srgb', 'srgb-linear', 'display-p3', 'a98-rgb', 'prophoto-rgb', 'rec2020', 'rgb'].includes(relativeKeys.toLowerCase()) ? 'rgb' : relativeKeys.slice(-3);
22
- const converted = convertColor(original, ColorType[relativeKeys.toUpperCase().replaceAll('-', '_')]);
27
+ const names = relativeKeys.startsWith("xyz")
28
+ ? "xyz"
29
+ : ["srgb", "srgb-linear", "display-p3", "a98-rgb", "prophoto-rgb", "rec2020", "rgb"].some((t) => equalsIgnoreCase(t, relativeKeys))
30
+ ? "rgb"
31
+ : relativeKeys.slice(-3);
32
+ const allComponents = [rExp, gExp, bExp, aExp];
33
+ const components = getColorComponents(original);
34
+ const validKeys = names.split("");
35
+ let val = "";
36
+ if (components != null) {
37
+ allComponents.push(...components);
38
+ }
39
+ // ensure all components are valid for the color space
40
+ for (const component of allComponents) {
41
+ if (component == null) {
42
+ continue;
43
+ }
44
+ if (component.typ == EnumToken.IdenTokenType) {
45
+ val = component.val.toLowerCase();
46
+ if (
47
+ // @ts-expect-error
48
+ typeof Math[val.toUpperCase()] !== "number" &&
49
+ val != "in" &&
50
+ val != "hue" &&
51
+ val != "from" &&
52
+ val != "alpha" &&
53
+ val != "none" &&
54
+ val != "shorter" &&
55
+ val != "longer" &&
56
+ val != "increasing" &&
57
+ val != "decreasing" &&
58
+ !colorsFunc.includes(val) &&
59
+ !colorFuncColorSpace.includes(val) &&
60
+ !validKeys.includes(val)) {
61
+ return null;
62
+ }
63
+ continue;
64
+ }
65
+ if (component.typ === EnumToken.MathFunctionTokenType &&
66
+ equalsIgnoreCase("calc", component.val)) {
67
+ for (const { value } of walkValues(component.chi)) {
68
+ if (value.typ == EnumToken.IdenTokenType) {
69
+ val = value.val.toLowerCase();
70
+ if (
71
+ // @ts-expect-error
72
+ typeof Math[val.toUpperCase()] !== "number" &&
73
+ val != "in" &&
74
+ val != "hue" &&
75
+ val != "from" &&
76
+ val != "alpha" &&
77
+ val != "none" &&
78
+ val != "shorter" &&
79
+ val != "longer" &&
80
+ val != "increasing" &&
81
+ val != "decreasing" &&
82
+ !colorsFunc.includes(val) &&
83
+ !colorFuncColorSpace.includes(val) &&
84
+ !validKeys.includes(val)) {
85
+ return null;
86
+ }
87
+ }
88
+ }
89
+ }
90
+ }
91
+ const converted = (convertColor(original, ColorType[relativeKeys.toUpperCase().replaceAll("-", "_")]));
23
92
  if (converted == null) {
24
93
  return null;
25
94
  }
26
- const children = converted.chi.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType, EnumToken.CommentTokenType].includes(t.typ));
95
+ const children = converted.chi.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType, EnumToken.CommentTokenType].includes(t.typ));
27
96
  [r, g, b, alpha] = converted.kin == ColorType.COLOR ? children.slice(1) : children;
28
97
  values = {
29
98
  [names[0]]: getValue(r, converted, names[0]),
30
99
  [names[1]]: getValue(g, converted, names[1]), // string,
31
100
  [names[2]]: getValue(b, converted, names[2]),
32
101
  // @ts-ignore
33
- alpha: alpha == null ? {
34
- typ: EnumToken.NumberTokenType,
35
- val: 1
36
- } : (alpha.typ == EnumToken.IdenTokenType && alpha.val == 'none') ? {
37
- typ: EnumToken.NumberTokenType,
38
- val: 0
39
- } : (alpha.typ == EnumToken.PercentageTokenType ? {
40
- typ: EnumToken.NumberTokenType,
41
- val: getNumber(alpha)
42
- } : alpha)
102
+ alpha: alpha == null
103
+ ? {
104
+ typ: EnumToken.NumberTokenType,
105
+ val: 1,
106
+ }
107
+ : alpha.typ == EnumToken.IdenTokenType && alpha.val == "none"
108
+ ? {
109
+ typ: EnumToken.NumberTokenType,
110
+ val: 0,
111
+ }
112
+ : alpha.typ == EnumToken.PercentageTokenType
113
+ ? {
114
+ typ: EnumToken.NumberTokenType,
115
+ val: getNumber(alpha),
116
+ }
117
+ : alpha,
43
118
  };
44
119
  keys = {
45
120
  [names[0]]: getValue(rExp, converted, names[0]),
46
121
  [names[1]]: getValue(gExp, converted, names[1]),
47
122
  [names[2]]: getValue(bExp, converted, names[2]),
48
123
  // @ts-ignore
49
- alpha: getValue(aExp == null ? {
50
- typ: EnumToken.NumberTokenType,
51
- val: 1
52
- } : (aExp.typ == EnumToken.IdenTokenType && aExp.val == 'none') ? {
53
- typ: EnumToken.NumberTokenType,
54
- val: 0
55
- } : aExp)
124
+ alpha: getValue(aExp == null
125
+ ? {
126
+ typ: EnumToken.NumberTokenType,
127
+ val: 1,
128
+ }
129
+ : aExp.typ == EnumToken.IdenTokenType && aExp.val == "none"
130
+ ? {
131
+ typ: EnumToken.NumberTokenType,
132
+ val: 0,
133
+ }
134
+ : aExp),
56
135
  };
57
- return computeComponentValue(keys, converted, values);
136
+ const result = computeComponentValue(keys, values);
137
+ if (result?.alpha?.typ == EnumToken.NumberTokenType && result.alpha.val === 1) {
138
+ const { alpha, ...components } = result;
139
+ return components;
140
+ }
141
+ return result;
58
142
  }
143
+ /**
144
+ * Get token numeric value
145
+ * @param t
146
+ * @param converted
147
+ * @param component
148
+ * @returns
149
+ */
59
150
  function getValue(t, converted, component) {
60
151
  if (t.typ == EnumToken.PercentageTokenType) {
61
152
  let value = getNumber(t);
62
- let colorSpace = ColorType[converted.kin].toLowerCase().replaceAll('-', '_');
63
- if (colorSpace in colorRange) {
64
- // @ts-ignore
65
- value *= colorRange[colorSpace][component].at(-1);
153
+ if (converted != null) {
154
+ let colorSpace = ColorType[converted.kin].toLowerCase().replaceAll("-", "_");
155
+ if (colorSpace in colorRange) {
156
+ // @ts-ignore
157
+ value *= colorRange[colorSpace][component].at(-1);
158
+ }
66
159
  }
67
160
  return {
68
161
  typ: EnumToken.NumberTokenType,
69
- val: value
162
+ val: value,
70
163
  };
71
164
  }
72
165
  return t;
73
166
  }
74
- function computeComponentValue(expr, converted, values) {
167
+ /**
168
+ * Compute component value
169
+ * @param expr
170
+ * @param values
171
+ * @returns
172
+ */
173
+ function computeComponentValue(expr, values) {
75
174
  for (const object of [values, expr]) {
76
- if ('h' in object) {
175
+ if ("h" in object) {
77
176
  // normalize hue
78
177
  for (const k of walkValues([object.h])) {
79
- if (k.value.typ == EnumToken.AngleTokenType && k.value.unit == 'deg') {
178
+ if (k.value.typ == EnumToken.AngleTokenType && k.value.unit == "deg") {
80
179
  k.value.typ = EnumToken.NumberTokenType;
81
180
  }
82
181
  }
83
182
  }
84
183
  }
85
184
  for (const [key, exp] of Object.entries(expr)) {
86
- if ([EnumToken.NumberTokenType, EnumToken.PercentageTokenType, EnumToken.AngleTokenType, EnumToken.LengthTokenType].includes(exp.typ)) ;
185
+ if ([
186
+ EnumToken.NumberTokenType,
187
+ EnumToken.PercentageTokenType,
188
+ EnumToken.AngleTokenType,
189
+ EnumToken.LengthTokenType,
190
+ ].includes(exp.typ)) ;
87
191
  else if (exp.typ == EnumToken.IdenTokenType && exp.val in values) {
88
192
  expr[key] = values[exp.val];
89
193
  }
90
- else if (exp.typ == EnumToken.FunctionTokenType && mathFuncs.includes(exp.val)) {
194
+ else if (exp.typ == EnumToken.MathFunctionTokenType ||
195
+ (exp.typ == EnumToken.FunctionTokenType && mathFuncs.includes(exp.val))) {
91
196
  for (let { value, parent } of walkValues(exp.chi, exp)) {
92
197
  if (value.typ == EnumToken.IdenTokenType) {
93
198
  // @ts-ignore
94
- replaceValue(parent, value, values[value.val] ?? {
95
- typ: EnumToken.NumberTokenType,
96
- // @ts-ignore
97
- val: '' + Math[value.val.toUpperCase()]
98
- // @ts-ignore
99
- });
199
+ replaceValue(parent, value,
200
+ // @ts-expect-error
201
+ values[value.val] ??
202
+ {
203
+ typ: EnumToken.NumberTokenType,
204
+ // @ts-ignore
205
+ val: "" + Math[value.val.toUpperCase()],
206
+ // @ts-ignore
207
+ });
100
208
  }
101
209
  }
102
- const result = exp.typ == EnumToken.FunctionTokenType && mathFuncs.includes(exp.val) && exp.val != 'calc' ? evaluateFunc(exp) : evaluate(exp.chi);
210
+ const result = (exp.typ === EnumToken.MathFunctionTokenType ||
211
+ (exp.typ == EnumToken.FunctionTokenType && mathFuncs.includes(exp.val))) &&
212
+ exp.val !== "calc"
213
+ ? evaluateFunc(exp)
214
+ : evaluate(exp.chi);
103
215
  if (result.length == 1 && result[0].typ != EnumToken.BinaryExpressionTokenType) {
104
216
  expr[key] = result[0];
105
217
  }
@@ -128,4 +240,4 @@ function replaceValue(parent, value, newValue) {
128
240
  }
129
241
  }
130
242
 
131
- export { parseRelativeColor, replaceValue };
243
+ export { parseRelativeColorComponents, replaceValue };
@@ -1,14 +1,8 @@
1
1
  import { color2srgbvalues, minmax } from './color.js';
2
- import { COLORS_NAMES } from './utils/constants.js';
3
- import { EnumToken, ColorType } from '../../ast/types.js';
4
- import '../../ast/minify.js';
5
- import '../../ast/walk.js';
6
- import '../../parser/parse.js';
7
- import '../../parser/tokenize.js';
8
- import '../../parser/utils/config.js';
9
2
  import { expandHexValue } from './hex.js';
10
3
  import { hslvalues, hslvalues2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, cmyk2srgbvalues, hwb2srgbvalues } from './srgb.js';
11
- import '../../renderer/sourcemap/lib/encode.js';
4
+ import { EnumToken, ColorType } from '../../ast/types.js';
5
+ import { COLORS_NAMES } from '../constants.js';
12
6
 
13
7
  function srgb2rgb(value) {
14
8
  return minmax(Math.round(value * 255), 0, 255);
@@ -70,7 +64,7 @@ function color2RgbToken(token) {
70
64
  if (values == null) {
71
65
  return null;
72
66
  }
73
- return rgb2RgbToken(values.map((t, index) => index == 3 ? t : srgb2rgb(t)));
67
+ return rgb2RgbToken(values.map((t, index) => (index == 3 ? t : srgb2rgb(t))));
74
68
  }
75
69
  function rgb2RgbToken(values) {
76
70
  const chi = [
@@ -83,9 +77,9 @@ function rgb2RgbToken(values) {
83
77
  }
84
78
  return {
85
79
  typ: EnumToken.ColorTokenType,
86
- val: 'rgb',
80
+ val: "rgb",
87
81
  chi,
88
- kin: ColorType.RGB
82
+ kin: ColorType.RGB,
89
83
  };
90
84
  }
91
85
  function hex2rgbvalues(token) {
@@ -105,14 +99,16 @@ function hex2rgbvalues(token) {
105
99
  return rgb;
106
100
  }
107
101
  function hwb2rgbvalues(token) {
108
- return hwb2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
102
+ return hwb2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
109
103
  }
110
104
  function hsl2rgbvalues(token) {
111
105
  let { h, s, l, a } = hslvalues(token) ?? {};
112
106
  if (h == null || s == null || l == null) {
113
107
  return null;
114
108
  }
115
- return hslvalues2srgbvalues(h, s, l).map((t) => minmax(Math.round(t * 255), 0, 255)).concat(a == 1 || a == null ? [] : [a]);
109
+ return hslvalues2srgbvalues(h, s, l)
110
+ .map((t) => minmax(Math.round(t * 255), 0, 255))
111
+ .concat(a == 1 || a == null ? [] : [a]);
116
112
  }
117
113
  function hsl2srgbvalues(token) {
118
114
  let { h, s, l, a } = hslvalues(token) ?? {};
@@ -122,19 +118,19 @@ function hsl2srgbvalues(token) {
122
118
  return hslvalues2srgbvalues(h, s, l).concat(a == 1 || a == null ? [] : [a]);
123
119
  }
124
120
  function cmyk2rgbvalues(token) {
125
- return cmyk2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
121
+ return cmyk2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
126
122
  }
127
123
  function oklab2rgbvalues(token) {
128
- return oklab2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
124
+ return oklab2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
129
125
  }
130
126
  function oklch2rgbvalues(token) {
131
- return oklch2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
127
+ return oklch2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
132
128
  }
133
129
  function lab2rgbvalues(token) {
134
- return lab2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
130
+ return lab2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
135
131
  }
136
132
  function lch2rgbvalues(token) {
137
- return lch2srgbvalues(token)?.map?.((t, index) => index == 3 ? t : srgb2rgb(t)) ?? null;
133
+ return lch2srgbvalues(token)?.map?.((t, index) => (index == 3 ? t : srgb2rgb(t))) ?? null;
138
134
  }
139
135
 
140
136
  export { cmyk2RgbToken, cmyk2rgbvalues, color2RgbToken, hex2RgbToken, hex2rgbvalues, hsl2RgbToken, hsl2rgbvalues, hsl2srgbvalues, hwb2RgbToken, hwb2rgbvalues, lab2RgbToken, lab2rgbvalues, lch2RgbToken, lch2rgbvalues, oklab2RgbToken, oklab2rgbvalues, oklch2RgbToken, oklch2rgbvalues, srgb2rgb };
@@ -1,24 +1,22 @@
1
- import { COLORS_NAMES } from './utils/constants.js';
2
- import { getComponents } from './utils/components.js';
1
+ import { getColorComponents } from './utils/components.js';
3
2
  import { color2srgbvalues, getNumber, getAngle } from './color.js';
4
- import { OKLab_to_sRGB, getOKLABComponents } from './oklab.js';
5
- import { ColorType, EnumToken } from '../../ast/types.js';
6
- import '../../ast/minify.js';
7
- import '../../ast/walk.js';
8
- import '../../parser/parse.js';
9
- import '../../parser/tokenize.js';
10
- import '../../parser/utils/config.js';
3
+ import { EnumToken, ColorType } from '../../ast/types.js';
4
+ import { Lab_to_sRGB, getLABComponents, lchvalues2labvalues } from './lab.js';
11
5
  import { expandHexValue } from './hex.js';
12
- import { lchvalues2labvalues, Lab_to_sRGB, getLABComponents } from './lab.js';
6
+ import { OKLab_to_sRGB, getOKLABComponents } from './oklab.js';
13
7
  import { getLCHComponents } from './lch.js';
14
8
  import { getOKLCHComponents } from './oklch.js';
15
9
  import { XYZ_to_lin_sRGB } from './xyz.js';
16
- import '../../renderer/sourcemap/lib/encode.js';
10
+ import { COLORS_NAMES } from '../constants.js';
11
+ import { parseColor } from '../syntax.js';
17
12
 
18
13
  // from https://www.w3.org/TR/css-color-4/#color-conversion-code
19
14
  // srgb-linear -> srgb
20
15
  // 0 <= r, g, b <= 1
21
16
  function srgbvalues(token) {
17
+ if (token.typ === EnumToken.IdenTokenType) {
18
+ token = parseColor(token);
19
+ }
22
20
  switch (token.kin) {
23
21
  case ColorType.LIT:
24
22
  case ColorType.HEX:
@@ -45,10 +43,18 @@ function srgbvalues(token) {
45
43
  return null;
46
44
  }
47
45
  function rgb2srgb(token) {
48
- return getComponents(token)?.map?.((t, index) => index == 3 ? ((t.typ == EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t)) : (t.typ == EnumToken.PercentageTokenType ? 255 : 1) * getNumber(t) / 255) ?? null;
46
+ return (getColorComponents(token)?.map?.((t, index) => index == 3
47
+ ? t.typ == EnumToken.IdenTokenType && t.val == "none"
48
+ ? 1
49
+ : getNumber(t)
50
+ : ((t.typ == EnumToken.PercentageTokenType ? 255 : 1) *
51
+ getNumber(t)) /
52
+ 255) ?? null);
49
53
  }
50
54
  function rgb2srgbvalues(token) {
51
- return getComponents(token)?.map?.((t, index) => index == 3 ? getNumber(t) : getNumber(t) / 255) ?? null;
55
+ return (getColorComponents(token)?.map?.((t, index) => index == 3
56
+ ? getNumber(t)
57
+ : getNumber(t) / 255) ?? null);
52
58
  }
53
59
  function hex2srgbvalues(token) {
54
60
  const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
@@ -71,9 +77,9 @@ function hwb2srgbvalues(token) {
71
77
  if (hue == null || white == null || black == null) {
72
78
  return [];
73
79
  }
74
- const rgb = hslvalues2srgbvalues(hue, 1, .5);
80
+ const rgb = hslvalues2srgbvalues(hue, 1, 0.5);
75
81
  for (let i = 0; i < 3; i++) {
76
- rgb[i] *= (1 - white - black);
82
+ rgb[i] *= 1 - white - black;
77
83
  rgb[i] = rgb[i] + white;
78
84
  }
79
85
  if (alpha != null && alpha != 1) {
@@ -89,7 +95,7 @@ function hsl2srgb(token) {
89
95
  return hslvalues2srgbvalues(h, s, l, a);
90
96
  }
91
97
  function cmyk2srgbvalues(token) {
92
- const components = getComponents(token);
98
+ const components = getColorComponents(token);
93
99
  if (components == null) {
94
100
  return null;
95
101
  }
@@ -112,20 +118,11 @@ function cmyk2srgbvalues(token) {
112
118
  const rgb = [
113
119
  1 - Math.min(1, c * (1 - k) + k),
114
120
  1 - Math.min(1, m * (1 - k) + k),
115
- 1 - Math.min(1, y * (1 - k) + k)
121
+ 1 - Math.min(1, y * (1 - k) + k),
116
122
  ];
117
123
  if (components.length == 5) {
118
124
  rgb.push(getNumber(components[4]));
119
125
  }
120
- // @ts-ignore
121
- // if (token.chi.length >= 9) {
122
- //
123
- // // @ts-ignore
124
- // t = <NumberToken | PercentageToken>token.chi[8];
125
- //
126
- // // @ts-ignore
127
- // rgb.push(getNumber(t));
128
- // }
129
126
  return rgb;
130
127
  }
131
128
  function oklab2srgbvalues(token) {
@@ -152,7 +149,7 @@ function oklch2srgbvalues(token) {
152
149
  return rgb;
153
150
  }
154
151
  function hslvalues(token) {
155
- const components = getComponents(token);
152
+ const components = getColorComponents(token);
156
153
  if (components == null) {
157
154
  return null;
158
155
  }
@@ -177,7 +174,7 @@ function hslvalues(token) {
177
174
  return a == null ? { h, s, l } : { h, s, l, a };
178
175
  }
179
176
  function hslvalues2srgbvalues(h, s, l, a = null) {
180
- let v = l <= .5 ? l * (1.0 + s) : l + s - l * s;
177
+ let v = l <= 0.5 ? l * (1.0 + s) : l + s - l * s;
181
178
  let r = l;
182
179
  let g = l;
183
180
  let b = l;
@@ -241,8 +238,12 @@ function lab2srgbvalues(token) {
241
238
  return rgb;
242
239
  }
243
240
  function lch2srgbvalues(token) {
241
+ const components = getLCHComponents(token);
242
+ if (components == null) {
243
+ return null;
244
+ }
244
245
  // @ts-ignore
245
- const [l, a, b, alpha] = lchvalues2labvalues(...getLCHComponents(token));
246
+ const [l, a, b, alpha] = lchvalues2labvalues(...components);
246
247
  if (l == null || a == null || b == null) {
247
248
  return null;
248
249
  }
@@ -0,0 +1,18 @@
1
+ import { ColorType } from '../../../ast/types.js';
2
+
3
+ /**
4
+ * Return the color type of the color components
5
+ */
6
+ function getColorType(color) {
7
+ if (color.kin === ColorType.COLOR_MIX || color.cal === "rel") {
8
+ return color.kin === ColorType.COLOR_MIX
9
+ ? // @ts-expect-error
10
+ ColorType[
11
+ // @ts-expect-error
12
+ color.chi[2]?.val?.toUpperCase?.()?.replace?.("-", "_")]
13
+ : getColorType(color.chi[2]);
14
+ }
15
+ return color.kin;
16
+ }
17
+
18
+ export { getColorType };
@@ -0,0 +1,46 @@
1
+ import { equalsIgnoreCase } from '../../../parser/utils/text.js';
2
+ import { getColorComponents } from './components.js';
3
+
4
+ /**
5
+ * Return the color space of the color
6
+ * @param color
7
+ * @returns
8
+ */
9
+ function getColorSpace(color) {
10
+ if (!("chi" in color)) {
11
+ return "rgb";
12
+ }
13
+ let name;
14
+ const components = getColorComponents(color);
15
+ if (components == null || components.length < 3) {
16
+ return null;
17
+ }
18
+ if (equalsIgnoreCase("color", color.val)) {
19
+ if (equalsIgnoreCase("from", components[0].val)) {
20
+ name = components[2].val;
21
+ }
22
+ else {
23
+ name = components[0].val;
24
+ }
25
+ }
26
+ else if (equalsIgnoreCase("color-mix", color.val)) {
27
+ if (equalsIgnoreCase("in", components[0].val)) {
28
+ name = components[1].val;
29
+ }
30
+ else {
31
+ return "oklab";
32
+ }
33
+ }
34
+ else {
35
+ name = color.val;
36
+ }
37
+ return name == "device-cmyk"
38
+ ? "cmyk"
39
+ : name.startsWith("xyz")
40
+ ? "xyz"
41
+ : ["srgb", "srgb-linear", "display-p3", "a98-rgb", "prophoto-rgb", "rec2020", "rgb"].some((t) => equalsIgnoreCase(t, name))
42
+ ? "rgb"
43
+ : name.slice(-3);
44
+ }
45
+
46
+ export { getColorSpace };
@@ -1,41 +1,61 @@
1
- import { ColorType, EnumToken } from '../../../ast/types.js';
2
- import '../../../ast/minify.js';
1
+ import { EnumToken, ColorType } from '../../../ast/types.js';
3
2
  import { walkValues } from '../../../ast/walk.js';
4
- import '../../../parser/parse.js';
5
- import '../../../parser/tokenize.js';
6
- import '../../../parser/utils/config.js';
7
- import { COLORS_NAMES } from './constants.js';
3
+ import { COLORS_NAMES } from '../../constants.js';
8
4
  import { expandHexValue } from '../hex.js';
9
- import '../../../renderer/sourcemap/lib/encode.js';
5
+ import { isColor, parseColor } from '../../syntax.js';
6
+ import { equalsIgnoreCase } from '../../../parser/utils/text.js';
10
7
 
11
- function getComponents(token) {
8
+ function getColorComponents(token) {
9
+ if (token.typ === EnumToken.IdenTokenType) {
10
+ if (isColor(token)) {
11
+ parseColor(token);
12
+ }
13
+ else {
14
+ return null;
15
+ }
16
+ }
12
17
  if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
18
+ if (equalsIgnoreCase('currentcolor', token.val)) {
19
+ return null;
20
+ }
13
21
  const value = expandHexValue(token.kin == ColorType.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
14
22
  // @ts-ignore
15
- return value.slice(1).match(/([a-fA-F0-9]{2})/g).map((t, index) => {
23
+ return value
24
+ .slice(1)
25
+ .match(/([a-fA-F0-9]{2})/g)
26
+ .map((t, index) => {
16
27
  return { typ: EnumToken.Number, val: index < 3 ? parseInt(t, 16) : parseInt(t, 16) / 255 };
17
28
  });
18
29
  }
19
30
  const result = [];
20
- for (const child of (token.chi)) {
31
+ for (const child of token.chi) {
21
32
  if ([
22
- EnumToken.LiteralTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType
33
+ EnumToken.LiteralTokenType,
34
+ EnumToken.CommentTokenType,
35
+ EnumToken.CommaTokenType,
36
+ EnumToken.WhitespaceTokenType,
23
37
  ].includes(child.typ)) {
24
38
  continue;
25
39
  }
26
- if (child.typ == EnumToken.FunctionTokenType) {
27
- if ('var' == child.val.toLowerCase()) {
40
+ if (child.typ === EnumToken.IdenTokenType && isColor(child)) {
41
+ parseColor(child);
42
+ }
43
+ if (child.typ === EnumToken.FunctionTokenType ||
44
+ child.typ === EnumToken.WildCardFunctionTokenType ||
45
+ child.typ === EnumToken.MathFunctionTokenType) {
46
+ if ("var" == child.val.toLowerCase()) {
28
47
  return null;
29
48
  }
30
49
  else {
31
50
  for (const { value } of walkValues(child.chi)) {
32
- if (value.typ == EnumToken.FunctionTokenType && 'var' === value.val.toLowerCase()) {
51
+ if (value.typ == EnumToken.WildCardFunctionTokenDefType &&
52
+ "var" === value.val.toLowerCase()) {
33
53
  return null;
34
54
  }
35
55
  }
36
56
  }
37
57
  }
38
- if (child.typ == EnumToken.ColorTokenType && 'currentcolor' === child.val.toLowerCase()) {
58
+ if (child.typ == EnumToken.ColorTokenType && equalsIgnoreCase("currentcolor", child.val)) {
39
59
  return null;
40
60
  }
41
61
  result.push(child);
@@ -43,4 +63,4 @@ function getComponents(token) {
43
63
  return result;
44
64
  }
45
65
 
46
- export { getComponents };
66
+ export { getColorComponents };