@tbela99/css-parser 0.7.1 → 0.9.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 (90) hide show
  1. package/.editorconfig +484 -0
  2. package/README.md +140 -84
  3. package/dist/index-umd-web.js +8461 -51655
  4. package/dist/index.cjs +8437 -51636
  5. package/dist/index.d.ts +220 -68
  6. package/dist/lib/ast/expand.js +46 -9
  7. package/dist/lib/ast/features/calc.js +76 -12
  8. package/dist/lib/ast/features/inlinecssvariables.js +6 -1
  9. package/dist/lib/ast/features/prefix.js +17 -9
  10. package/dist/lib/ast/features/shorthand.js +1 -0
  11. package/dist/lib/ast/math/expression.js +299 -11
  12. package/dist/lib/ast/math/math.js +7 -1
  13. package/dist/lib/ast/minify.js +30 -16
  14. package/dist/lib/ast/types.js +59 -49
  15. package/dist/lib/ast/walk.js +92 -18
  16. package/dist/lib/parser/declaration/list.js +1 -0
  17. package/dist/lib/parser/declaration/map.js +60 -52
  18. package/dist/lib/parser/declaration/set.js +1 -12
  19. package/dist/lib/parser/parse.js +371 -119
  20. package/dist/lib/parser/tokenize.js +31 -6
  21. package/dist/lib/parser/utils/declaration.js +2 -2
  22. package/dist/lib/parser/utils/type.js +6 -6
  23. package/dist/lib/renderer/color/a98rgb.js +1 -0
  24. package/dist/lib/renderer/color/color.js +1 -0
  25. package/dist/lib/renderer/color/colormix.js +1 -0
  26. package/dist/lib/renderer/color/hex.js +2 -1
  27. package/dist/lib/renderer/color/hsl.js +2 -1
  28. package/dist/lib/renderer/color/hwb.js +3 -2
  29. package/dist/lib/renderer/color/lab.js +2 -1
  30. package/dist/lib/renderer/color/lch.js +2 -1
  31. package/dist/lib/renderer/color/oklab.js +3 -2
  32. package/dist/lib/renderer/color/oklch.js +2 -1
  33. package/dist/lib/renderer/color/p3.js +2 -1
  34. package/dist/lib/renderer/color/prophotoRgb.js +56 -0
  35. package/dist/lib/renderer/color/prophotorgb.js +1 -1
  36. package/dist/lib/renderer/color/rec2020.js +1 -0
  37. package/dist/lib/renderer/color/relativecolor.js +52 -28
  38. package/dist/lib/renderer/color/rgb.js +2 -1
  39. package/dist/lib/renderer/color/srgb.js +3 -2
  40. package/dist/lib/renderer/color/utils/components.js +1 -0
  41. package/dist/lib/renderer/color/utils/constants.js +2 -1
  42. package/dist/lib/renderer/color/xyz.js +2 -1
  43. package/dist/lib/renderer/color/xyzd50.js +1 -0
  44. package/dist/lib/renderer/render.js +62 -12
  45. package/dist/lib/syntax/syntax.js +362 -4
  46. package/dist/lib/validation/at-rules/container.js +353 -0
  47. package/dist/lib/validation/at-rules/counter-style.js +78 -0
  48. package/dist/lib/validation/at-rules/custom-media.js +52 -0
  49. package/dist/lib/validation/at-rules/document.js +114 -0
  50. package/dist/lib/validation/at-rules/else.js +5 -0
  51. package/dist/lib/validation/at-rules/font-feature-values.js +52 -0
  52. package/dist/lib/validation/at-rules/import.js +199 -0
  53. package/dist/lib/validation/at-rules/keyframes.js +70 -0
  54. package/dist/lib/validation/at-rules/layer.js +30 -0
  55. package/dist/lib/validation/at-rules/media.js +254 -0
  56. package/dist/lib/validation/at-rules/namespace.js +85 -0
  57. package/dist/lib/validation/at-rules/page-margin-box.js +56 -0
  58. package/dist/lib/validation/at-rules/page.js +88 -0
  59. package/dist/lib/validation/at-rules/supports.js +262 -0
  60. package/dist/lib/validation/at-rules/when.js +178 -0
  61. package/dist/lib/validation/atrule.js +187 -0
  62. package/dist/lib/validation/config.js +35 -2
  63. package/dist/lib/validation/config.json.js +1683 -50905
  64. package/dist/lib/validation/declaration.js +102 -0
  65. package/dist/lib/validation/parser/parse.js +1137 -7
  66. package/dist/lib/validation/parser/types.js +28 -12
  67. package/dist/lib/validation/selector.js +26 -444
  68. package/dist/lib/validation/syntax.js +1475 -0
  69. package/dist/lib/validation/syntaxes/complex-selector-list.js +45 -0
  70. package/dist/lib/validation/syntaxes/complex-selector.js +53 -0
  71. package/dist/lib/validation/syntaxes/compound-selector.js +226 -0
  72. package/dist/lib/validation/syntaxes/family-name.js +91 -0
  73. package/dist/lib/validation/syntaxes/image.js +29 -0
  74. package/dist/lib/validation/syntaxes/keyframe-block-list.js +27 -0
  75. package/dist/lib/validation/syntaxes/keyframe-selector.js +137 -0
  76. package/dist/lib/validation/syntaxes/layer-name.js +67 -0
  77. package/dist/lib/validation/syntaxes/relative-selector-list.js +57 -0
  78. package/dist/lib/validation/syntaxes/relative-selector.js +36 -0
  79. package/dist/lib/validation/syntaxes/selector-list.js +5 -0
  80. package/dist/lib/validation/syntaxes/selector.js +5 -0
  81. package/dist/lib/validation/syntaxes/url.js +75 -0
  82. package/dist/lib/validation/utils/list.js +24 -0
  83. package/dist/lib/validation/utils/whitespace.js +22 -0
  84. package/dist/node/index.js +5 -5
  85. package/dist/web/index.js +5 -1
  86. package/dist/web/load.js +1 -0
  87. package/package.json +16 -14
  88. package/dist/lib/ast/utils/minifyfeature.js +0 -9
  89. package/dist/lib/iterable/weakset.js +0 -58
  90. package/dist/lib/parser/utils/syntax.js +0 -450
@@ -1,5 +1,6 @@
1
1
  import { EnumToken } from '../ast/types.js';
2
2
  import '../ast/minify.js';
3
+ import '../ast/walk.js';
3
4
  import './parse.js';
4
5
  import { isWhiteSpace, isNewLine, isDigit, isNonPrintable } from '../syntax/syntax.js';
5
6
  import './utils/config.js';
@@ -15,7 +16,13 @@ function consumeWhiteSpace(parseInfo) {
15
16
  return count;
16
17
  }
17
18
  function pushToken(token, parseInfo, hint) {
18
- const result = { token, hint, position: { ...parseInfo.position }, bytesIn: parseInfo.currentPosition.ind + 1 };
19
+ const result = {
20
+ token,
21
+ len: parseInfo.currentPosition.ind - parseInfo.position.ind,
22
+ hint,
23
+ position: { ...parseInfo.position },
24
+ bytesIn: parseInfo.currentPosition.ind + 1
25
+ };
19
26
  parseInfo.position.ind = parseInfo.currentPosition.ind;
20
27
  parseInfo.position.lin = parseInfo.currentPosition.lin;
21
28
  parseInfo.position.col = Math.max(parseInfo.currentPosition.col, 1);
@@ -135,6 +142,10 @@ function next(parseInfo, count = 1) {
135
142
  }
136
143
  return char;
137
144
  }
145
+ /**
146
+ * tokenize css string
147
+ * @param stream
148
+ */
138
149
  function* tokenize(stream) {
139
150
  const parseInfo = {
140
151
  stream,
@@ -183,8 +194,10 @@ function* tokenize(stream) {
183
194
  buffer += value;
184
195
  }
185
196
  }
186
- yield pushToken(buffer, parseInfo, EnumToken.BadCommentTokenType);
187
- buffer = '';
197
+ if (buffer.length > 0) {
198
+ yield pushToken(buffer, parseInfo, EnumToken.BadCommentTokenType);
199
+ buffer = '';
200
+ }
188
201
  }
189
202
  break;
190
203
  case '&':
@@ -222,6 +235,13 @@ function* tokenize(stream) {
222
235
  buffer = '';
223
236
  }
224
237
  break;
238
+ case '#':
239
+ if (buffer.length > 0) {
240
+ yield pushToken(buffer, parseInfo);
241
+ buffer = '';
242
+ }
243
+ buffer += value;
244
+ break;
225
245
  case '\\':
226
246
  // EOF
227
247
  if (!(value = next(parseInfo))) {
@@ -326,7 +346,7 @@ function* tokenize(stream) {
326
346
  case ':':
327
347
  case ',':
328
348
  case '=':
329
- if (buffer.length > 0) {
349
+ if (buffer.length > 0 && buffer != ':') {
330
350
  yield pushToken(buffer, parseInfo);
331
351
  buffer = '';
332
352
  }
@@ -336,8 +356,13 @@ function* tokenize(stream) {
336
356
  yield pushToken(value + val, parseInfo, EnumToken.ContainMatchTokenType);
337
357
  break;
338
358
  }
339
- if (value == ':' && ':' == val) {
340
- buffer += value + next(parseInfo);
359
+ if (value == ':') {
360
+ if (isWhiteSpace(val.codePointAt(0))) {
361
+ yield pushToken(value, parseInfo, EnumToken.ColonTokenType);
362
+ buffer = '';
363
+ break;
364
+ }
365
+ buffer += value;
341
366
  break;
342
367
  }
343
368
  yield pushToken(value, parseInfo);
@@ -7,7 +7,7 @@ import './config.js';
7
7
  import '../../renderer/color/utils/constants.js';
8
8
  import '../../renderer/sourcemap/lib/encode.js';
9
9
 
10
- function parseDeclaration(node, errors, src, position) {
10
+ function parseDeclarationNode(node, errors, src, position) {
11
11
  while (node.val[0]?.typ == EnumToken.WhitespaceTokenType) {
12
12
  node.val.shift();
13
13
  }
@@ -65,4 +65,4 @@ function parseGridTemplate(template) {
65
65
  return buffer.length > 0 ? result + buffer : result;
66
66
  }
67
67
 
68
- export { parseDeclaration };
68
+ export { parseDeclarationNode };
@@ -1,12 +1,12 @@
1
1
  import { EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
+ import '../../ast/walk.js';
3
4
  import '../parse.js';
5
+ import { mathFuncs } from '../../syntax/syntax.js';
6
+ import './config.js';
4
7
  import '../../renderer/color/utils/constants.js';
5
8
  import '../../renderer/sourcemap/lib/encode.js';
6
- import './config.js';
7
9
 
8
- // https://www.w3.org/TR/css-values-4/#math-function
9
- const funcList = ['clamp', 'calc'];
10
10
  function matchType(val, properties) {
11
11
  if (val.typ == EnumToken.IdenTokenType && properties.keywords.includes(val.val) ||
12
12
  // @ts-ignore
@@ -22,8 +22,8 @@ function matchType(val, properties) {
22
22
  });
23
23
  }
24
24
  if (val.typ == EnumToken.FunctionTokenType) {
25
- if (funcList.includes(val.val)) {
26
- return val.chi.every(((t) => [EnumToken.LiteralTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.StartParensTokenType, EnumToken.EndParensTokenType].includes(t.typ) || matchType(t, properties)));
25
+ if (mathFuncs.includes(val.val)) {
26
+ return val.chi.every(((t) => [EnumToken.Add, EnumToken.Mul, EnumToken.Div, EnumToken.Sub, EnumToken.LiteralTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType, EnumToken.DimensionTokenType, EnumToken.NumberTokenType, EnumToken.LengthTokenType, EnumToken.AngleTokenType, EnumToken.PercentageTokenType, EnumToken.ResolutionTokenType, EnumToken.TimeTokenType, EnumToken.BinaryExpressionTokenType].includes(t.typ) || matchType(t, properties)));
27
27
  }
28
28
  // match type defined like function 'symbols()', 'url()', 'attr()' etc.
29
29
  // return properties.types.includes((<FunctionToken>val).val + '()')
@@ -31,4 +31,4 @@ function matchType(val, properties) {
31
31
  return false;
32
32
  }
33
33
 
34
- export { funcList, matchType };
34
+ export { matchType };
@@ -3,6 +3,7 @@ import { multiplyMatrices } from './utils/matrix.js';
3
3
  import './utils/constants.js';
4
4
  import '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
+ import '../../ast/walk.js';
6
7
  import '../../parser/parse.js';
7
8
  import { srgb2xyz } from './xyz.js';
8
9
  import '../sourcemap/lib/encode.js';
@@ -1,5 +1,6 @@
1
1
  import { EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
+ import '../../ast/walk.js';
3
4
  import '../../parser/parse.js';
4
5
  import { srgb2rgb, lch2rgb, lab2rgb, oklch2rgb, oklab2rgb, hwb2rgb, hsl2rgb, hex2rgb } from './rgb.js';
5
6
  import { srgb2hsl, lch2hsl, lab2hsl, oklch2hsl, oklab2hsl, hwb2hsl, hex2hsl, rgb2hsl } from './hsl.js';
@@ -1,5 +1,6 @@
1
1
  import { EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
+ import '../../ast/walk.js';
3
4
  import '../../parser/parse.js';
4
5
  import { isRectangularOrthogonalColorspace, isPolarColorspace } from '../../syntax/syntax.js';
5
6
  import '../../parser/utils/config.js';
@@ -1,7 +1,8 @@
1
1
  import { EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
+ import '../../ast/walk.js';
3
4
  import '../../parser/parse.js';
4
- import { getNumber, minmax } from './color.js';
5
+ import { minmax, getNumber } from './color.js';
5
6
  import { hsl2rgb, hwb2rgb, cmyk2rgb, oklab2rgb, oklch2rgb, lab2rgb, lch2rgb } from './rgb.js';
6
7
  import { NAMES_COLORS } from './utils/constants.js';
7
8
  import { getComponents } from './utils/components.js';
@@ -1,10 +1,11 @@
1
1
  import { hwb2hsv } from './hsv.js';
2
2
  import { getNumber } from './color.js';
3
- import { hex2rgb, lab2rgb, lch2rgb, oklab2rgb, oklch2rgb } from './rgb.js';
3
+ import { lch2rgb, lab2rgb, oklch2rgb, oklab2rgb, hex2rgb } from './rgb.js';
4
4
  import './utils/constants.js';
5
5
  import { getComponents } from './utils/components.js';
6
6
  import { EnumToken } from '../../ast/types.js';
7
7
  import '../../ast/minify.js';
8
+ import '../../ast/walk.js';
8
9
  import '../../parser/parse.js';
9
10
  import { hslvalues } from './srgb.js';
10
11
  import '../sourcemap/lib/encode.js';
@@ -1,11 +1,12 @@
1
1
  import { hsl2hsv } from './hsv.js';
2
2
  import './utils/constants.js';
3
3
  import { getComponents } from './utils/components.js';
4
- import { getNumber, getAngle } from './color.js';
4
+ import { getAngle, getNumber } from './color.js';
5
5
  import { EnumToken } from '../../ast/types.js';
6
6
  import '../../ast/minify.js';
7
+ import '../../ast/walk.js';
7
8
  import '../../parser/parse.js';
8
- import { lab2srgb, lch2srgb, oklab2srgb, oklch2srgb } from './srgb.js';
9
+ import { lch2srgb, lab2srgb, oklch2srgb, oklab2srgb } from './srgb.js';
9
10
  import '../sourcemap/lib/encode.js';
10
11
  import '../../parser/utils/config.js';
11
12
 
@@ -1,12 +1,13 @@
1
1
  import { e, k, D50 } from './utils/constants.js';
2
2
  import { getComponents } from './utils/components.js';
3
3
  import { srgb2xyz, xyzd502srgb } from './xyz.js';
4
- import { hex2srgb, rgb2srgb, hsl2srgb, hwb2srgb, oklch2srgb } from './srgb.js';
4
+ import { oklch2srgb, hwb2srgb, hsl2srgb, rgb2srgb, hex2srgb } from './srgb.js';
5
5
  import { getLCHComponents } from './lch.js';
6
6
  import { OKLab_to_XYZ, getOKLABComponents } from './oklab.js';
7
7
  import { getNumber } from './color.js';
8
8
  import { EnumToken } from '../../ast/types.js';
9
9
  import '../../ast/minify.js';
10
+ import '../../ast/walk.js';
10
11
  import '../../parser/parse.js';
11
12
  import '../sourcemap/lib/encode.js';
12
13
  import '../../parser/utils/config.js';
@@ -3,8 +3,9 @@ import { getComponents } from './utils/components.js';
3
3
  import { getNumber, getAngle } from './color.js';
4
4
  import { EnumToken } from '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
+ import '../../ast/walk.js';
6
7
  import '../../parser/parse.js';
7
- import { srgb2lab, xyz2lab, hex2lab, rgb2lab, hsl2lab, hwb2lab, getLABComponents, oklab2lab, oklch2lab } from './lab.js';
8
+ import { srgb2lab, xyz2lab, oklch2lab, oklab2lab, getLABComponents, hwb2lab, hsl2lab, rgb2lab, hex2lab } from './lab.js';
8
9
  import '../sourcemap/lib/encode.js';
9
10
  import '../../parser/utils/config.js';
10
11
 
@@ -1,10 +1,11 @@
1
1
  import { multiplyMatrices } from './utils/matrix.js';
2
2
  import './utils/constants.js';
3
3
  import { getComponents } from './utils/components.js';
4
- import { srgb2lsrgbvalues, hex2srgb, rgb2srgb, hsl2srgb, hwb2srgb, lab2srgb, lch2srgb, lsrgb2srgbvalues } from './srgb.js';
4
+ import { srgb2lsrgbvalues, lch2srgb, lab2srgb, hwb2srgb, hsl2srgb, rgb2srgb, hex2srgb, lsrgb2srgbvalues } from './srgb.js';
5
5
  import { getNumber } from './color.js';
6
6
  import { EnumToken } from '../../ast/types.js';
7
7
  import '../../ast/minify.js';
8
+ import '../../ast/walk.js';
8
9
  import '../../parser/parse.js';
9
10
  import { lch2labvalues } from './lab.js';
10
11
  import { getOKLCHComponents } from './oklch.js';
@@ -105,7 +106,7 @@ function OKLab_to_sRGB(l, a, b) {
105
106
  1.2914855378640917399 * b, 3);
106
107
  return lsrgb2srgbvalues(
107
108
  /* r: */
108
- +4.076741661347994 * L -
109
+ 4.076741661347994 * L -
109
110
  3.307711590408193 * M +
110
111
  0.230969928729428 * S,
111
112
  /* g: */
@@ -3,9 +3,10 @@ import { getComponents } from './utils/components.js';
3
3
  import { getNumber, getAngle } from './color.js';
4
4
  import { EnumToken } from '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
+ import '../../ast/walk.js';
6
7
  import '../../parser/parse.js';
7
8
  import { lab2lchvalues } from './lch.js';
8
- import { srgb2oklab, hex2oklab, rgb2oklab, hsl2oklab, hwb2oklab, lab2oklab, lch2oklab, getOKLABComponents } from './oklab.js';
9
+ import { srgb2oklab, lch2oklab, getOKLABComponents, lab2oklab, hwb2oklab, hsl2oklab, rgb2oklab, hex2oklab } from './oklab.js';
9
10
  import '../sourcemap/lib/encode.js';
10
11
  import '../../parser/utils/config.js';
11
12
 
@@ -1,8 +1,9 @@
1
- import { xyz2srgb, srgb2lsrgbvalues, lsrgb2srgbvalues } from './srgb.js';
1
+ import { xyz2srgb, lsrgb2srgbvalues, srgb2lsrgbvalues } from './srgb.js';
2
2
  import { multiplyMatrices } from './utils/matrix.js';
3
3
  import './utils/constants.js';
4
4
  import '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
+ import '../../ast/walk.js';
6
7
  import '../../parser/parse.js';
7
8
  import { srgb2xyz } from './xyz.js';
8
9
  import '../sourcemap/lib/encode.js';
@@ -0,0 +1,56 @@
1
+ import { srgb2xyz, xyzd502srgb } from './xyz.js';
2
+ import { XYZ_D65_to_D50 } from './xyzd50.js';
3
+
4
+ function prophotorgb2srgbvalues(r, g, b, a = null) {
5
+ // @ts-ignore
6
+ return xyzd502srgb(...prophotorgb2xyz50(r, g, b, a));
7
+ }
8
+ function srgb2prophotorgbvalues(r, g, b, a) {
9
+ // @ts-ignore
10
+ return xyz50_to_prophotorgb(...XYZ_D65_to_D50(...srgb2xyz(r, g, b, a)));
11
+ }
12
+ function prophotorgb2lin_ProPhoto(r, g, b, a = null) {
13
+ return [r, g, b].map(v => {
14
+ let abs = Math.abs(v);
15
+ if (abs >= 16 / 512) {
16
+ return Math.sign(v) * Math.pow(abs, 1.8);
17
+ }
18
+ return v / 16;
19
+ }).concat(a == null || a == 1 ? [] : [a]);
20
+ }
21
+ function prophotorgb2xyz50(r, g, b, a = null) {
22
+ [r, g, b, a] = prophotorgb2lin_ProPhoto(r, g, b, a);
23
+ const xyz = [
24
+ 0.7977666449006423 * r +
25
+ 0.1351812974005331 * g +
26
+ 0.0313477341283922 * b,
27
+ 0.2880748288194013 * r +
28
+ 0.7118352342418731 * g +
29
+ 0.0000899369387256 * b,
30
+ 0.8251046025104602 * b
31
+ ];
32
+ return xyz.concat(a == null || a == 1 ? [] : [a]);
33
+ }
34
+ function xyz50_to_prophotorgb(x, y, z, a) {
35
+ // @ts-ignore
36
+ return gam_prophotorgb(...[
37
+ x * 1.3457868816471585 -
38
+ y * 0.2555720873797946 -
39
+ 0.0511018649755453 * z,
40
+ x * -0.5446307051249019 +
41
+ y * 1.5082477428451466 +
42
+ 0.0205274474364214 * z,
43
+ 1.2119675456389452 * z
44
+ ].concat(a == null || a == 1 ? [] : [a]));
45
+ }
46
+ function gam_prophotorgb(r, g, b, a) {
47
+ return [r, g, b].map(v => {
48
+ let abs = Math.abs(v);
49
+ if (abs >= 1 / 512) {
50
+ return Math.sign(v) * Math.pow(abs, 1 / 1.8);
51
+ }
52
+ return 16 * v;
53
+ }).concat(a == null || a == 1 ? [] : [a]);
54
+ }
55
+
56
+ export { prophotorgb2srgbvalues, srgb2prophotorgbvalues };
@@ -1,4 +1,4 @@
1
- import { xyzd502srgb, srgb2xyz } from './xyz.js';
1
+ import { srgb2xyz, xyzd502srgb } from './xyz.js';
2
2
  import { XYZ_D65_to_D50 } from './xyzd50.js';
3
3
 
4
4
  function prophotorgb2srgbvalues(r, g, b, a = null) {
@@ -3,6 +3,7 @@ import { multiplyMatrices } from './utils/matrix.js';
3
3
  import './utils/constants.js';
4
4
  import '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
+ import '../../ast/walk.js';
6
7
  import '../../parser/parse.js';
7
8
  import { srgb2xyz } from './xyz.js';
8
9
  import '../sourcemap/lib/encode.js';
@@ -3,10 +3,11 @@ import { EnumToken } from '../../ast/types.js';
3
3
  import '../../ast/minify.js';
4
4
  import { walkValues } from '../../ast/walk.js';
5
5
  import '../../parser/parse.js';
6
+ import { mathFuncs } from '../../syntax/syntax.js';
7
+ import '../../parser/utils/config.js';
6
8
  import { reduceNumber } from '../render.js';
9
+ import { evaluateFunc, evaluate } from '../../ast/math/expression.js';
7
10
  import { colorRange } from './utils/constants.js';
8
- import { evaluate } from '../../ast/math/expression.js';
9
- import '../../parser/utils/config.js';
10
11
 
11
12
  function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
12
13
  let r;
@@ -47,7 +48,7 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
47
48
  val: '1'
48
49
  } : aExp)
49
50
  };
50
- return computeComponentValue(keys, values);
51
+ return computeComponentValue(keys, converted, values);
51
52
  }
52
53
  function getValue(t, converted, component) {
53
54
  if (t == null) {
@@ -66,7 +67,7 @@ function getValue(t, converted, component) {
66
67
  }
67
68
  return t;
68
69
  }
69
- function computeComponentValue(expr, values) {
70
+ function computeComponentValue(expr, converted, values) {
70
71
  for (const object of [values, expr]) {
71
72
  if ('h' in object) {
72
73
  // normalize hue
@@ -107,34 +108,29 @@ function computeComponentValue(expr, values) {
107
108
  expr[key] = values[exp.val];
108
109
  }
109
110
  }
110
- else if (exp.typ == EnumToken.FunctionTokenType && exp.val == 'calc') {
111
- for (let { value, parent } of walkValues(exp.chi)) {
112
- if (value.typ == EnumToken.IdenTokenType) {
113
- if (!(value.val in values)) {
111
+ else if (exp.typ == EnumToken.FunctionTokenType && mathFuncs.includes(exp.val)) {
112
+ for (let { value, parent } of walkValues(exp.chi, exp)) {
113
+ if (parent == null) {
114
+ parent = exp;
115
+ }
116
+ if (value.typ == EnumToken.PercentageTokenType) {
117
+ replaceValue(parent, value, getValue(value, converted, key));
118
+ }
119
+ else if (value.typ == EnumToken.IdenTokenType) {
120
+ // @ts-ignore
121
+ if (!(value.val in values || typeof Math[value.val.toUpperCase()] == 'number')) {
114
122
  return null;
115
123
  }
116
- if (parent == null) {
117
- parent = exp;
118
- }
119
- if (parent.typ == EnumToken.BinaryExpressionTokenType) {
120
- if (parent.l == value) {
121
- parent.l = values[value.val];
122
- }
123
- else {
124
- parent.r = values[value.val];
125
- }
126
- }
127
- else {
128
- for (let i = 0; i < parent.chi.length; i++) {
129
- if (parent.chi[i] == value) {
130
- parent.chi.splice(i, 1, values[value.val]);
131
- break;
132
- }
133
- }
134
- }
124
+ // @ts-ignore
125
+ replaceValue(parent, value, values[value.val] ?? {
126
+ typ: EnumToken.NumberTokenType,
127
+ // @ts-ignore
128
+ val: '' + Math[value.val.toUpperCase()]
129
+ // @ts-ignore
130
+ });
135
131
  }
136
132
  }
137
- const result = evaluate(exp.chi);
133
+ const result = exp.typ == EnumToken.FunctionTokenType && mathFuncs.includes(exp.val) && exp.val != 'calc' ? evaluateFunc(exp) : evaluate(exp.chi);
138
134
  if (result.length == 1 && result[0].typ != EnumToken.BinaryExpressionTokenType) {
139
135
  expr[key] = result[0];
140
136
  }
@@ -145,5 +141,33 @@ function computeComponentValue(expr, values) {
145
141
  }
146
142
  return expr;
147
143
  }
144
+ function replaceValue(parent, value, newValue) {
145
+ if (parent.typ == EnumToken.BinaryExpressionTokenType) {
146
+ if (parent.l == value) {
147
+ parent.l = newValue;
148
+ }
149
+ else {
150
+ parent.r = newValue;
151
+ }
152
+ }
153
+ else {
154
+ for (let i = 0; i < parent.chi.length; i++) {
155
+ if (parent.chi[i] == value) {
156
+ parent.chi.splice(i, 1, newValue);
157
+ break;
158
+ }
159
+ if (parent.chi[i].typ == EnumToken.BinaryExpressionTokenType) {
160
+ if (parent.chi[i].l == value) {
161
+ parent.chi[i].l = newValue;
162
+ break;
163
+ }
164
+ else if (parent.chi[i].r == value) {
165
+ parent.chi[i].r = newValue;
166
+ break;
167
+ }
168
+ }
169
+ }
170
+ }
171
+ }
148
172
 
149
173
  export { parseRelativeColor };
@@ -2,9 +2,10 @@ import { minmax } from './color.js';
2
2
  import { COLORS_NAMES } from './utils/constants.js';
3
3
  import '../../ast/types.js';
4
4
  import '../../ast/minify.js';
5
+ import '../../ast/walk.js';
5
6
  import '../../parser/parse.js';
6
7
  import { expandHexValue } from './hex.js';
7
- import { hwb2srgb, hslvalues, hsl2srgbvalues, cmyk2srgb, oklab2srgb, oklch2srgb, lab2srgb, lch2srgb } from './srgb.js';
8
+ import { hslvalues, hsl2srgbvalues, hwb2srgb, cmyk2srgb, oklab2srgb, oklch2srgb, lab2srgb, lch2srgb } from './srgb.js';
8
9
  import '../sourcemap/lib/encode.js';
9
10
  import '../../parser/utils/config.js';
10
11
 
@@ -3,10 +3,11 @@ import { getComponents } from './utils/components.js';
3
3
  import { color2srgbvalues, getNumber, getAngle } from './color.js';
4
4
  import { EnumToken } from '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
+ import '../../ast/walk.js';
6
7
  import '../../parser/parse.js';
7
8
  import { expandHexValue } from './hex.js';
8
- import { lch2labvalues, getLABComponents, Lab_to_sRGB } from './lab.js';
9
- import { getOKLABComponents, OKLab_to_sRGB } from './oklab.js';
9
+ import { lch2labvalues, Lab_to_sRGB, getLABComponents } from './lab.js';
10
+ import { OKLab_to_sRGB, getOKLABComponents } from './oklab.js';
10
11
  import { getLCHComponents } from './lch.js';
11
12
  import { getOKLCHComponents } from './oklch.js';
12
13
  import { XYZ_to_lin_sRGB } from './xyz.js';
@@ -1,5 +1,6 @@
1
1
  import { EnumToken } from '../../../ast/types.js';
2
2
  import '../../../ast/minify.js';
3
+ import '../../../ast/walk.js';
3
4
  import '../../../parser/parse.js';
4
5
  import { COLORS_NAMES } from './constants.js';
5
6
  import { expandHexValue } from '../hex.js';
@@ -1,5 +1,6 @@
1
1
  import { EnumToken } from '../../../ast/types.js';
2
2
  import '../../../ast/minify.js';
3
+ import '../../../ast/walk.js';
3
4
  import '../../../parser/parse.js';
4
5
  import '../../sourcemap/lib/encode.js';
5
6
  import '../../../parser/utils/config.js';
@@ -27,7 +28,7 @@ const colorRange = {
27
28
  }
28
29
  };
29
30
  const colorFuncColorSpace = ['srgb', 'srgb-linear', 'display-p3', 'prophoto-rgb', 'a98-rgb', 'rec2020', 'xyz', 'xyz-d65', 'xyz-d50'];
30
- ({ typ: EnumToken.IdenTokenType, val: 'none' });
31
+ ({ typ: EnumToken.IdenTokenType});
31
32
  const D50 = [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585];
32
33
  const k = Math.pow(29, 3) / Math.pow(3, 3);
33
34
  const e = Math.pow(6, 3) / Math.pow(29, 3);
@@ -2,8 +2,9 @@ import { multiplyMatrices } from './utils/matrix.js';
2
2
  import './utils/constants.js';
3
3
  import '../../ast/types.js';
4
4
  import '../../ast/minify.js';
5
+ import '../../ast/walk.js';
5
6
  import '../../parser/parse.js';
6
- import { lsrgb2srgbvalues, srgb2lsrgbvalues } from './srgb.js';
7
+ import { srgb2lsrgbvalues, lsrgb2srgbvalues } from './srgb.js';
7
8
  import '../sourcemap/lib/encode.js';
8
9
  import '../../parser/utils/config.js';
9
10
 
@@ -2,6 +2,7 @@ import { multiplyMatrices } from './utils/matrix.js';
2
2
  import './utils/constants.js';
3
3
  import '../../ast/types.js';
4
4
  import '../../ast/minify.js';
5
+ import '../../ast/walk.js';
5
6
  import '../../parser/parse.js';
6
7
  import { xyz2lab } from './lab.js';
7
8
  import { lab2lchvalues } from './lch.js';