@tbela99/css-parser 1.3.2 → 1.3.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 (59) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +59 -20
  3. package/dist/index-umd-web.js +1846 -1075
  4. package/dist/index.cjs +1941 -1202
  5. package/dist/index.d.ts +914 -181
  6. package/dist/lib/ast/expand.js +5 -10
  7. package/dist/lib/ast/features/calc.js +8 -8
  8. package/dist/lib/ast/features/inlinecssvariables.js +9 -8
  9. package/dist/lib/ast/features/prefix.js +5 -15
  10. package/dist/lib/ast/features/shorthand.js +5 -6
  11. package/dist/lib/ast/features/transform.js +18 -25
  12. package/dist/lib/ast/features/type.js +4 -2
  13. package/dist/lib/ast/minify.js +56 -112
  14. package/dist/lib/ast/transform/compute.js +2 -4
  15. package/dist/lib/ast/transform/matrix.js +20 -20
  16. package/dist/lib/ast/transform/minify.js +105 -12
  17. package/dist/lib/ast/transform/rotate.js +11 -11
  18. package/dist/lib/ast/transform/scale.js +6 -6
  19. package/dist/lib/ast/transform/skew.js +4 -4
  20. package/dist/lib/ast/transform/translate.js +3 -3
  21. package/dist/lib/ast/transform/utils.js +30 -37
  22. package/dist/lib/ast/types.js +16 -4
  23. package/dist/lib/ast/walk.js +172 -70
  24. package/dist/lib/fs/resolve.js +12 -7
  25. package/dist/lib/parser/declaration/list.js +3 -1
  26. package/dist/lib/parser/parse.js +441 -161
  27. package/dist/lib/parser/tokenize.js +12 -14
  28. package/dist/lib/renderer/render.js +7 -7
  29. package/dist/lib/syntax/color/cmyk.js +6 -3
  30. package/dist/lib/syntax/color/color-mix.js +2 -3
  31. package/dist/lib/syntax/color/color.js +28 -6
  32. package/dist/lib/syntax/color/hex.js +3 -0
  33. package/dist/lib/syntax/color/hsl.js +18 -7
  34. package/dist/lib/syntax/color/hwb.js +3 -3
  35. package/dist/lib/syntax/color/lab.js +4 -4
  36. package/dist/lib/syntax/color/lch.js +7 -4
  37. package/dist/lib/syntax/color/oklab.js +4 -4
  38. package/dist/lib/syntax/color/oklch.js +18 -6
  39. package/dist/lib/syntax/color/relativecolor.js +9 -56
  40. package/dist/lib/syntax/color/srgb.js +1 -1
  41. package/dist/lib/syntax/syntax.js +36 -18
  42. package/dist/lib/validation/at-rules/container.js +11 -0
  43. package/dist/lib/validation/at-rules/counter-style.js +11 -0
  44. package/dist/lib/validation/at-rules/font-feature-values.js +11 -0
  45. package/dist/lib/validation/at-rules/keyframes.js +11 -0
  46. package/dist/lib/validation/at-rules/layer.js +11 -0
  47. package/dist/lib/validation/at-rules/media.js +11 -0
  48. package/dist/lib/validation/at-rules/page-margin-box.js +11 -0
  49. package/dist/lib/validation/at-rules/page.js +11 -0
  50. package/dist/lib/validation/at-rules/supports.js +11 -0
  51. package/dist/lib/validation/at-rules/when.js +11 -0
  52. package/dist/lib/validation/config.js +0 -2
  53. package/dist/lib/validation/config.json.js +21 -9
  54. package/dist/lib/validation/parser/parse.js +53 -2
  55. package/dist/lib/validation/syntax.js +199 -36
  56. package/dist/node.js +63 -36
  57. package/dist/web.js +84 -25
  58. package/package.json +7 -5
  59. package/dist/lib/validation/parser/types.js +0 -54
@@ -182,13 +182,15 @@ function* tokenize(parseInfo, yieldEOFToken = true) {
182
182
  yield pushToken(buffer, parseInfo);
183
183
  buffer = '';
184
184
  }
185
+ buffer += value;
185
186
  while (value = next(parseInfo)) {
186
187
  charCode = value.charCodeAt(0);
187
188
  if (!isWhiteSpace(charCode)) {
188
189
  break;
189
190
  }
191
+ buffer += value;
190
192
  }
191
- yield pushToken('', parseInfo, EnumToken.WhitespaceTokenType);
193
+ yield pushToken(buffer, parseInfo, EnumToken.WhitespaceTokenType);
192
194
  buffer = '';
193
195
  }
194
196
  switch (charCode) {
@@ -236,8 +238,7 @@ function* tokenize(parseInfo, yieldEOFToken = true) {
236
238
  buffer = '';
237
239
  }
238
240
  if (match(parseInfo, '=')) {
239
- yield pushToken('', parseInfo, EnumToken.LteTokenType);
240
- next(parseInfo);
241
+ yield pushToken(value + next(parseInfo), parseInfo, EnumToken.LteTokenType);
241
242
  break;
242
243
  }
243
244
  buffer += value;
@@ -292,8 +293,7 @@ function* tokenize(parseInfo, yieldEOFToken = true) {
292
293
  }
293
294
  if (charCode == 124 /* TokenMap.PIPE */) {
294
295
  if (match(parseInfo, '|')) {
295
- next(parseInfo);
296
- yield pushToken('', parseInfo, EnumToken.ColumnCombinatorTokenType);
296
+ yield pushToken(value + next(parseInfo), parseInfo, EnumToken.ColumnCombinatorTokenType);
297
297
  }
298
298
  else if (match(parseInfo, '=')) {
299
299
  buffer += next(parseInfo);
@@ -347,11 +347,10 @@ function* tokenize(parseInfo, yieldEOFToken = true) {
347
347
  buffer = '';
348
348
  }
349
349
  if (match(parseInfo, '=')) {
350
- yield pushToken('', parseInfo, EnumToken.GteTokenType);
351
- next(parseInfo);
350
+ yield pushToken(value + next(parseInfo), parseInfo, EnumToken.GteTokenType);
352
351
  }
353
352
  else {
354
- yield pushToken('', parseInfo, EnumToken.GtTokenType);
353
+ yield pushToken(value, parseInfo, EnumToken.GtTokenType);
355
354
  }
356
355
  consumeWhiteSpace(parseInfo);
357
356
  break;
@@ -402,7 +401,7 @@ function* tokenize(parseInfo, yieldEOFToken = true) {
402
401
  yield pushToken(buffer, parseInfo);
403
402
  buffer = '';
404
403
  }
405
- yield pushToken('', parseInfo, EnumToken.EndParensTokenType);
404
+ yield pushToken(value, parseInfo, EnumToken.EndParensTokenType);
406
405
  break;
407
406
  case 40 /* TokenMap.OPEN_PAREN */:
408
407
  if (buffer.length == 0) {
@@ -468,7 +467,7 @@ function* tokenize(parseInfo, yieldEOFToken = true) {
468
467
  // ')'
469
468
  if (charCode == 0x29) {
470
469
  yield pushToken(buffer, parseInfo, hasNewLine ? EnumToken.BadStringTokenType : EnumToken.StringTokenType);
471
- yield pushToken('', parseInfo, EnumToken.EndParensTokenType);
470
+ yield pushToken(value, parseInfo, EnumToken.EndParensTokenType);
472
471
  buffer = '';
473
472
  break;
474
473
  }
@@ -492,7 +491,7 @@ function* tokenize(parseInfo, yieldEOFToken = true) {
492
491
  charCode = value.charCodeAt(0);
493
492
  if (charCode == 0x29) { // ')'
494
493
  yield pushToken(buffer, parseInfo, EnumToken.UrlTokenTokenType);
495
- yield pushToken('', parseInfo, EnumToken.EndParensTokenType);
494
+ yield pushToken(value, parseInfo, EnumToken.EndParensTokenType);
496
495
  buffer = '';
497
496
  break;
498
497
  }
@@ -526,8 +525,7 @@ function* tokenize(parseInfo, yieldEOFToken = true) {
526
525
  buffer = '';
527
526
  }
528
527
  if (match(parseInfo, 'important')) {
529
- yield pushToken('', parseInfo, EnumToken.ImportantTokenType);
530
- next(parseInfo, 9);
528
+ yield pushToken(value + next(parseInfo, 9), parseInfo, EnumToken.ImportantTokenType);
531
529
  buffer = '';
532
530
  break;
533
531
  }
@@ -579,7 +577,7 @@ async function* tokenizeStream(input) {
579
577
  const reader = input.getReader();
580
578
  while (true) {
581
579
  const { done, value } = await reader.read();
582
- parseInfo.stream += decoder.decode(value, { stream: true });
580
+ parseInfo.stream += ArrayBuffer.isView(value) ? decoder.decode(value, { stream: true }) : value;
583
581
  yield* tokenize(parseInfo, done);
584
582
  if (done) {
585
583
  break;
@@ -69,7 +69,7 @@ function doRender(data, options = {}) {
69
69
  const sourcemap = options.sourcemap ? new SourceMap : null;
70
70
  const cache = Object.create(null);
71
71
  const result = {
72
- code: renderAstNode(options.expandNestingRules ? expand(data) : data, options, sourcemap, {
72
+ code: renderAstNode(options.expandNestingRules && [EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(data.typ) && 'chi' in data ? expand(data) : data, options, sourcemap, {
73
73
  ind: 0,
74
74
  lin: 1,
75
75
  col: 1
@@ -111,7 +111,7 @@ function doRender(data, options = {}) {
111
111
  function updateSourceMap(node, options, cache, sourcemap, position, str) {
112
112
  if ([
113
113
  EnumToken.RuleNodeType, EnumToken.AtRuleNodeType,
114
- EnumToken.KeyFrameRuleNodeType, EnumToken.KeyframeAtRuleNodeType
114
+ EnumToken.KeyFramesRuleNodeType, EnumToken.KeyframesAtRuleNodeType
115
115
  ].includes(node.typ)) {
116
116
  let src = node.loc?.src ?? '';
117
117
  let output = options.output ?? '';
@@ -185,9 +185,9 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
185
185
  }, '');
186
186
  case EnumToken.AtRuleNodeType:
187
187
  case EnumToken.RuleNodeType:
188
- case EnumToken.KeyFrameRuleNodeType:
189
- case EnumToken.KeyframeAtRuleNodeType:
190
- if ([EnumToken.AtRuleNodeType, EnumToken.KeyframeAtRuleNodeType].includes(data.typ) && !('chi' in data)) {
188
+ case EnumToken.KeyFramesRuleNodeType:
189
+ case EnumToken.KeyframesAtRuleNodeType:
190
+ if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !('chi' in data)) {
191
191
  return `${indent}@${data.nam}${data.val === '' ? '' : options.indent || ' '}${data.val};`;
192
192
  }
193
193
  // @ts-ignore
@@ -209,7 +209,7 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
209
209
  str = `${node.nam}:${options.indent}${(options.minify ? filterValues(node.val) : node.val).reduce(reducer, '').trimEnd()};`;
210
210
  }
211
211
  else if (node.typ == EnumToken.AtRuleNodeType && !('chi' in node)) {
212
- str = `${data.val === '' ? '' : options.indent || ' '}${data.val};`;
212
+ str = `${node.val === '' ? '' : options.indent || ' '}${node.val};`;
213
213
  }
214
214
  else {
215
215
  str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level + 1, indents);
@@ -228,7 +228,7 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
228
228
  if (children.endsWith(';')) {
229
229
  children = children.slice(0, -1);
230
230
  }
231
- if ([EnumToken.AtRuleNodeType, EnumToken.KeyframeAtRuleNodeType].includes(data.typ)) {
231
+ if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ)) {
232
232
  return `@${data.nam}${data.val === '' ? '' : options.indent || ' '}${data.val}${options.indent}{${options.newLine}` + (children === '' ? '' : indentSub + children + options.newLine) + indent + `}`;
233
233
  }
234
234
  return data.sel + `${options.indent}{${options.newLine}` + (children === '' ? '' : indentSub + children + options.newLine) + indent + `}`;
@@ -4,7 +4,7 @@ import '../../ast/walk.js';
4
4
  import '../../parser/parse.js';
5
5
  import '../../parser/tokenize.js';
6
6
  import '../../parser/utils/config.js';
7
- import { color2srgbvalues } from './color.js';
7
+ import { color2srgbvalues, toPrecisionValue } from './color.js';
8
8
  import { hsl2srgbvalues } from './rgb.js';
9
9
  import './utils/constants.js';
10
10
  import { lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hwb2srgbvalues, rgb2srgbvalues } from './srgb.js';
@@ -92,11 +92,14 @@ function cmyktoken(values) {
92
92
  chi: values.reduce((acc, curr, index) => index < 4 ? [...acc, {
93
93
  typ: EnumToken.PercentageTokenType,
94
94
  // @ts-ignore
95
- val: curr * 100
95
+ val: toPrecisionValue(curr) * 100
96
96
  }] : [...acc, {
97
97
  typ: EnumToken.LiteralTokenType,
98
98
  val: '/'
99
- }, { typ: EnumToken.PercentageTokenType, val: curr * 100 }], []),
99
+ }, {
100
+ typ: EnumToken.PercentageTokenType,
101
+ val: toPrecisionValue(curr) * 100
102
+ }], []),
100
103
  kin: ColorType.DEVICE_CMYK
101
104
  };
102
105
  }
@@ -129,11 +129,10 @@ function colorMix(colorSpace, hueInterpolationMethod, color1, percentage1, color
129
129
  // @ts-ignore
130
130
  const calculate = () => [colorSpace].concat(values1.map((v1, i) => {
131
131
  return {
132
- // @ts-ignore
133
- typ: EnumToken.NumberTokenType, val: String((mul1 * v1 * p1 + mul2 * values2[i] * p2) / mul)
132
+ typ: EnumToken.NumberTokenType, val: (mul1 * v1 * p1 + mul2 * values2[i] * p2) / mul
134
133
  };
135
134
  }).concat(mul == 1 ? [] : [{
136
- typ: EnumToken.NumberTokenType, val: String(mul)
135
+ typ: EnumToken.NumberTokenType, val: mul
137
136
  }]));
138
137
  switch (colorSpace.val) {
139
138
  case 'srgb':
@@ -25,6 +25,7 @@ import { reduceHexValue, rgb2HexToken, color2HexToken, lch2HexToken, lab2HexToke
25
25
  import { parseRelativeColor } from './relativecolor.js';
26
26
  import { color2cmykToken, lch2cmykToken, lab2cmykToken, oklch2cmykToken, oklab2cmyk, hwb2cmykToken, hsl2cmykToken, rgb2cmykToken } from './cmyk.js';
27
27
  import { a98rgb2srgbvalues, srgb2a98values } from './a98rgb.js';
28
+ import { epsilon } from '../../ast/transform/utils.js';
28
29
  import '../../renderer/sourcemap/lib/encode.js';
29
30
 
30
31
  /**
@@ -32,6 +33,8 @@ import '../../renderer/sourcemap/lib/encode.js';
32
33
  * @param token
33
34
  * @param to
34
35
  *
36
+ * @private
37
+ *
35
38
  * <code>
36
39
  *
37
40
  * const token = {typ: EnumToken.ColorTokenType, kin: ColorType.HEX, val: '#F00'}
@@ -73,7 +76,8 @@ function convertColor(token, to) {
73
76
  if (components != null) {
74
77
  token = {
75
78
  ...token,
76
- chi: [...(token.val == 'color' ? [chi[offset]] : []), ...Object.values(components)]
79
+ chi: [...(token.val == 'color' ? [chi[offset]] : []), ...Object.values(components)],
80
+ kin: ColorType[token.val.toUpperCase().replaceAll('-', '_')]
77
81
  };
78
82
  delete token.cal;
79
83
  }
@@ -89,6 +93,11 @@ function convertColor(token, to) {
89
93
  if (token.kin == ColorType.COLOR) {
90
94
  const colorSpace = token.chi.find(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
91
95
  if (colorSpace.val == ColorType[to].toLowerCase().replaceAll('_', '-')) {
96
+ for (const chi of token.chi) {
97
+ if (chi.typ == EnumToken.NumberTokenType && typeof chi.val == 'number') {
98
+ chi.val = toPrecisionValue(getNumber(chi));
99
+ }
100
+ }
92
101
  return token;
93
102
  }
94
103
  }
@@ -512,9 +521,9 @@ function color2srgbvalues(token) {
512
521
  function values2colortoken(values, to) {
513
522
  values = srgb2srgbcolorspace(values, to);
514
523
  const chi = [
515
- { typ: EnumToken.NumberTokenType, val: values[0] },
516
- { typ: EnumToken.NumberTokenType, val: values[1] },
517
- { typ: EnumToken.NumberTokenType, val: values[2] },
524
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
525
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
526
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[2]) },
518
527
  ];
519
528
  if (values.length == 4) {
520
529
  chi.push({ typ: EnumToken.LiteralTokenType, val: "/" }, {
@@ -539,8 +548,17 @@ function getNumber(token) {
539
548
  if (token.typ == EnumToken.IdenTokenType && token.val == 'none') {
540
549
  return 0;
541
550
  }
551
+ let val;
542
552
  // @ts-ignore
543
- return token.typ == EnumToken.PercentageTokenType ? token.val / 100 : token.val;
553
+ if (typeof token.val != 'number' && token.val?.typ == EnumToken.FractionTokenType) {
554
+ // @ts-ignore
555
+ val = token.val.l.val / token.val.r.val;
556
+ }
557
+ else {
558
+ val = token.val;
559
+ }
560
+ // @ts-ignore
561
+ return token.typ == EnumToken.PercentageTokenType ? val / 100 : val;
544
562
  }
545
563
  /**
546
564
  * convert angle to turn
@@ -571,6 +589,10 @@ function getAngle(token) {
571
589
  // @ts-ignore
572
590
  return token.val / 360;
573
591
  }
592
+ function toPrecisionValue(value) {
593
+ value = +value.toFixed(colorPrecision);
594
+ return Math.abs(value) < epsilon ? 0 : value;
595
+ }
574
596
  function toPrecisionAngle(angle) {
575
597
  angle = +angle.toPrecision(colorPrecision);
576
598
  if (Math.abs(angle) >= 360) {
@@ -585,4 +607,4 @@ function toPrecisionAngle(angle) {
585
607
  return angle;
586
608
  }
587
609
 
588
- export { cmyk2colorToken, color2colorToken, color2srgbvalues, convertColor, getAngle, getNumber, hex2colorToken, hsl2colorToken, hwb2colorToken, lab2colorToken, lch2colorToken, minmax, oklab2colorToken, oklch2colorToken, rgb2colorToken, toPrecisionAngle };
610
+ export { cmyk2colorToken, color2colorToken, color2srgbvalues, convertColor, getAngle, getNumber, hex2colorToken, hsl2colorToken, hwb2colorToken, lab2colorToken, lch2colorToken, minmax, oklab2colorToken, oklch2colorToken, rgb2colorToken, toPrecisionAngle, toPrecisionValue };
@@ -33,6 +33,9 @@ function reduceHexValue(value) {
33
33
  value[7] == value[8]) {
34
34
  value = `#${value[1]}${value[3]}${value[5]}${value[7] == 'f' ? '' : value[7]}`;
35
35
  }
36
+ if (value.endsWith('ff')) {
37
+ value = value.slice(0, -2);
38
+ }
36
39
  }
37
40
  return named_color != null && named_color.length <= value.length ? named_color : value;
38
41
  }
@@ -1,5 +1,5 @@
1
1
  import { hwb2hsv } from './hsv.js';
2
- import { color2srgbvalues, toPrecisionAngle, getNumber } from './color.js';
2
+ import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getNumber } from './color.js';
3
3
  import { lch2rgbvalues, lab2rgbvalues, cmyk2rgbvalues } from './rgb.js';
4
4
  import './utils/constants.js';
5
5
  import { getComponents } from './utils/components.js';
@@ -76,12 +76,15 @@ function color2HslToken(token) {
76
76
  function hslToken(values) {
77
77
  values[0] = toPrecisionAngle(values[0] * 360);
78
78
  const chi = [
79
- { typ: EnumToken.NumberTokenType, val: values[0] },
80
- { typ: EnumToken.PercentageTokenType, val: values[1] * 100 },
81
- { typ: EnumToken.PercentageTokenType, val: values[2] * 100 },
79
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
80
+ { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[1]) * 100 },
81
+ { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[2]) * 100 },
82
82
  ];
83
83
  if (values.length == 4 && values[3] != 1) {
84
- chi.push({ typ: EnumToken.LiteralTokenType, val: '/' }, { typ: EnumToken.PercentageTokenType, val: values[3] * 100 });
84
+ chi.push({ typ: EnumToken.LiteralTokenType, val: '/' }, {
85
+ typ: EnumToken.PercentageTokenType,
86
+ val: values[3] * 100
87
+ });
85
88
  }
86
89
  return {
87
90
  typ: EnumToken.ColorTokenType,
@@ -147,12 +150,20 @@ function hwb2hslvalues(token) {
147
150
  return hsv2hsl(...hwb2hsv(...Object.values(hslvalues(token))));
148
151
  }
149
152
  function lab2hslvalues(token) {
153
+ const values = lab2rgbvalues(token);
154
+ if (values == null) {
155
+ return null;
156
+ }
150
157
  // @ts-ignore
151
- return rgbvalues2hslvalues(...lab2rgbvalues(token));
158
+ return rgbvalues2hslvalues(...values);
152
159
  }
153
160
  function lch2hslvalues(token) {
161
+ const values = lch2rgbvalues(token);
162
+ if (values == null) {
163
+ return null;
164
+ }
154
165
  // @ts-ignore
155
- return rgbvalues2hslvalues(...lch2rgbvalues(token));
166
+ return rgbvalues2hslvalues(...values);
156
167
  }
157
168
  function oklab2hslvalues(token) {
158
169
  const t = oklab2srgbvalues(token);
@@ -1,7 +1,7 @@
1
1
  import { hsl2hsv } from './hsv.js';
2
2
  import './utils/constants.js';
3
3
  import { getComponents } from './utils/components.js';
4
- import { color2srgbvalues, toPrecisionAngle, getAngle, getNumber } from './color.js';
4
+ import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getAngle, getNumber } from './color.js';
5
5
  import { cmyk2srgbvalues, lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues } from './srgb.js';
6
6
  import { EnumToken, ColorType } from '../../ast/types.js';
7
7
  import '../../ast/minify.js';
@@ -71,8 +71,8 @@ function hwbToken(values) {
71
71
  values[0] = toPrecisionAngle(values[0] * 360);
72
72
  const chi = [
73
73
  { typ: EnumToken.NumberTokenType, val: values[0] },
74
- { typ: EnumToken.PercentageTokenType, val: values[1] * 100 },
75
- { typ: EnumToken.PercentageTokenType, val: values[2] * 100 },
74
+ { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[1]) * 100 },
75
+ { typ: EnumToken.PercentageTokenType, val: toPrecisionValue(values[2]) * 100 },
76
76
  ];
77
77
  if (values.length == 4) {
78
78
  chi.push({ typ: EnumToken.LiteralTokenType, val: '/' }, {
@@ -1,6 +1,6 @@
1
1
  import { e, k, D50 } from './utils/constants.js';
2
2
  import { getComponents } from './utils/components.js';
3
- import { color2srgbvalues, getNumber } from './color.js';
3
+ import { color2srgbvalues, toPrecisionValue, getNumber } from './color.js';
4
4
  import { getOKLABComponents, OKLab_to_XYZ } from './oklab.js';
5
5
  import { EnumToken, ColorType } from '../../ast/types.js';
6
6
  import '../../ast/minify.js';
@@ -76,9 +76,9 @@ function color2labToken(token) {
76
76
  }
77
77
  function labToken(values) {
78
78
  const chi = [
79
- { typ: EnumToken.NumberTokenType, val: values[0] },
80
- { typ: EnumToken.NumberTokenType, val: values[1] },
81
- { typ: EnumToken.NumberTokenType, val: values[2] },
79
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
80
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
81
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[2]) },
82
82
  ];
83
83
  if (values.length == 4) {
84
84
  chi.push({ typ: EnumToken.LiteralTokenType, val: '/' }, {
@@ -1,6 +1,6 @@
1
1
  import './utils/constants.js';
2
2
  import { getComponents } from './utils/components.js';
3
- import { color2srgbvalues, toPrecisionAngle, getNumber, getAngle } from './color.js';
3
+ import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getNumber, getAngle } from './color.js';
4
4
  import { cmyk2srgbvalues } from './srgb.js';
5
5
  import { EnumToken, ColorType } from '../../ast/types.js';
6
6
  import '../../ast/minify.js';
@@ -77,12 +77,15 @@ function color2lchToken(token) {
77
77
  function lchToken(values) {
78
78
  values[2] = toPrecisionAngle(values[2]);
79
79
  const chi = [
80
- { typ: EnumToken.NumberTokenType, val: values[0] },
81
- { typ: EnumToken.NumberTokenType, val: values[1] },
80
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
81
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
82
82
  { typ: EnumToken.NumberTokenType, val: values[2] },
83
83
  ];
84
84
  if (values.length == 4) {
85
- chi.push({ typ: EnumToken.LiteralTokenType, val: '/' }, { typ: EnumToken.PercentageTokenType, val: values[3] * 100 });
85
+ chi.push({ typ: EnumToken.LiteralTokenType, val: '/' }, {
86
+ typ: EnumToken.PercentageTokenType,
87
+ val: values[3] * 100
88
+ });
86
89
  }
87
90
  return {
88
91
  typ: EnumToken.ColorTokenType,
@@ -1,7 +1,7 @@
1
1
  import { multiplyMatrices } from './utils/matrix.js';
2
2
  import './utils/constants.js';
3
3
  import { getComponents } from './utils/components.js';
4
- import { color2srgbvalues, getNumber } from './color.js';
4
+ import { color2srgbvalues, toPrecisionValue, getNumber } from './color.js';
5
5
  import { srgb2lsrgbvalues, lch2srgbvalues, lab2srgbvalues, cmyk2srgbvalues, hwb2srgbvalues, hsl2srgb, rgb2srgb, hex2srgbvalues, lsrgb2srgbvalues } from './srgb.js';
6
6
  import { EnumToken, ColorType } from '../../ast/types.js';
7
7
  import '../../ast/minify.js';
@@ -78,9 +78,9 @@ function color2oklabToken(token) {
78
78
  }
79
79
  function oklabToken(values) {
80
80
  const chi = [
81
- { typ: EnumToken.NumberTokenType, val: values[0] },
82
- { typ: EnumToken.NumberTokenType, val: values[1] },
83
- { typ: EnumToken.NumberTokenType, val: values[2] },
81
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
82
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
83
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[2]) },
84
84
  ];
85
85
  if (values.length == 4) {
86
86
  chi.push({ typ: EnumToken.LiteralTokenType, val: '/' }, {
@@ -1,6 +1,6 @@
1
1
  import './utils/constants.js';
2
2
  import { getComponents } from './utils/components.js';
3
- import { color2srgbvalues, toPrecisionAngle, getNumber, getAngle } from './color.js';
3
+ import { color2srgbvalues, toPrecisionAngle, toPrecisionValue, getNumber, getAngle } from './color.js';
4
4
  import { srgb2oklab, lch2oklabvalues, getOKLABComponents, lab2oklabvalues, hwb2oklabvalues, hsl2oklabvalues, rgb2oklabvalues, hex2oklabvalues } from './oklab.js';
5
5
  import { EnumToken, ColorType } from '../../ast/types.js';
6
6
  import '../../ast/minify.js';
@@ -76,8 +76,8 @@ function color2oklchToken(token) {
76
76
  function oklchToken(values) {
77
77
  values[2] = toPrecisionAngle(values[2]);
78
78
  const chi = [
79
- { typ: EnumToken.NumberTokenType, val: values[0] },
80
- { typ: EnumToken.NumberTokenType, val: values[1] },
79
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[0]) },
80
+ { typ: EnumToken.NumberTokenType, val: toPrecisionValue(values[1]) },
81
81
  { typ: EnumToken.NumberTokenType, val: values[2] },
82
82
  ];
83
83
  if (values.length == 4) {
@@ -119,16 +119,28 @@ function cmyk2oklchvalues(token) {
119
119
  return values == null ? null : srgb2oklch(...values);
120
120
  }
121
121
  function lab2oklchvalues(token) {
122
+ const values = lab2oklabvalues(token);
123
+ if (values == null) {
124
+ return null;
125
+ }
122
126
  // @ts-ignore
123
- return labvalues2lchvalues(...lab2oklabvalues(token));
127
+ return labvalues2lchvalues(...values);
124
128
  }
125
129
  function lch2oklchvalues(token) {
130
+ const values = lch2oklabvalues(token);
131
+ if (values == null) {
132
+ return null;
133
+ }
126
134
  // @ts-ignore
127
- return labvalues2lchvalues(...lch2oklabvalues(token));
135
+ return labvalues2lchvalues(...values);
128
136
  }
129
137
  function oklab2oklchvalues(token) {
138
+ const values = getOKLABComponents(token);
139
+ if (values == null) {
140
+ return null;
141
+ }
130
142
  // @ts-ignore
131
- return labvalues2lchvalues(...getOKLABComponents(token));
143
+ return labvalues2lchvalues(...values);
132
144
  }
133
145
  function srgb2oklch(r, g, blue, alpha) {
134
146
  // @ts-ignore
@@ -18,7 +18,7 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
18
18
  let keys = {};
19
19
  let values = {};
20
20
  // colorFuncColorSpace x,y,z or r,g,b
21
- const names = relativeKeys.startsWith('xyz') ? 'xyz' : relativeKeys.slice(-3);
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
22
  const converted = convertColor(original, ColorType[relativeKeys.toUpperCase().replaceAll('-', '_')]);
23
23
  if (converted == null) {
24
24
  return null;
@@ -32,13 +32,13 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
32
32
  // @ts-ignore
33
33
  alpha: alpha == null ? {
34
34
  typ: EnumToken.NumberTokenType,
35
- val: '1'
35
+ val: 1
36
36
  } : (alpha.typ == EnumToken.IdenTokenType && alpha.val == 'none') ? {
37
37
  typ: EnumToken.NumberTokenType,
38
- val: '0'
38
+ val: 0
39
39
  } : (alpha.typ == EnumToken.PercentageTokenType ? {
40
40
  typ: EnumToken.NumberTokenType,
41
- val: String(getNumber(alpha))
41
+ val: getNumber(alpha)
42
42
  } : alpha)
43
43
  };
44
44
  keys = {
@@ -48,19 +48,15 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
48
48
  // @ts-ignore
49
49
  alpha: getValue(aExp == null ? {
50
50
  typ: EnumToken.NumberTokenType,
51
- val: '1'
51
+ val: 1
52
52
  } : (aExp.typ == EnumToken.IdenTokenType && aExp.val == 'none') ? {
53
53
  typ: EnumToken.NumberTokenType,
54
- val: '0'
54
+ val: 0
55
55
  } : aExp)
56
56
  };
57
57
  return computeComponentValue(keys, converted, values);
58
58
  }
59
59
  function getValue(t, converted, component) {
60
- // if (t == null) {
61
- //
62
- // return t;
63
- // }
64
60
  if (t.typ == EnumToken.PercentageTokenType) {
65
61
  let value = getNumber(t);
66
62
  let colorSpace = ColorType[converted.kin].toLowerCase().replaceAll('-', '_');
@@ -90,55 +86,13 @@ function computeComponentValue(expr, converted, values) {
90
86
  }
91
87
  }
92
88
  for (const [key, exp] of Object.entries(expr)) {
93
- /*
94
- if (exp == null) {
95
-
96
- if (key in values) {
97
-
98
- if (typeof values[<RelativeColorTypes>key] == 'number') {
99
-
100
- expr[<RelativeColorTypes>key] = {
101
- typ: EnumToken.NumberTokenType,
102
- val: reduceNumber(<number>values[<RelativeColorTypes>key])
103
- };
104
- } else {
105
-
106
- expr[<RelativeColorTypes>key] = <Token>values[<RelativeColorTypes>key];
107
- }
108
- }
109
-
110
- } else
111
- */
112
89
  if ([EnumToken.NumberTokenType, EnumToken.PercentageTokenType, EnumToken.AngleTokenType, EnumToken.LengthTokenType].includes(exp.typ)) ;
113
90
  else if (exp.typ == EnumToken.IdenTokenType && exp.val in values) {
114
- // if (typeof values[<RelativeColorTypes>exp.val] == 'number') {
115
- //
116
- // expr[<RelativeColorTypes>key] = {
117
- // typ: EnumToken.NumberTokenType,
118
- // val: reduceNumber(<number>values[<RelativeColorTypes>exp.val])
119
- // };
120
- // } else {
121
91
  expr[key] = values[exp.val];
122
- // }
123
92
  }
124
93
  else if (exp.typ == EnumToken.FunctionTokenType && mathFuncs.includes(exp.val)) {
125
94
  for (let { value, parent } of walkValues(exp.chi, exp)) {
126
- // if (parent == null) {
127
- //
128
- // parent = exp;
129
- // }
130
- /*
131
- if (value.typ == EnumToken.PercentageTokenType) {
132
-
133
- replaceValue(parent as BinaryExpressionToken | FunctionToken | ParensToken, value, getValue(value, converted, <RelativeColorTypes>key));
134
- } else
135
- */
136
95
  if (value.typ == EnumToken.IdenTokenType) {
137
- // @ts-ignore
138
- // if (!(value.val in values || typeof Math[(value as IdentToken).val.toUpperCase()] == 'number')) {
139
- //
140
- // return null;
141
- // }
142
96
  // @ts-ignore
143
97
  replaceValue(parent, value, values[value.val] ?? {
144
98
  typ: EnumToken.NumberTokenType,
@@ -152,10 +106,6 @@ function computeComponentValue(expr, converted, values) {
152
106
  if (result.length == 1 && result[0].typ != EnumToken.BinaryExpressionTokenType) {
153
107
  expr[key] = result[0];
154
108
  }
155
- // else {
156
- //
157
- // return null;
158
- // }
159
109
  }
160
110
  }
161
111
  return expr;
@@ -166,13 +116,16 @@ function replaceValue(parent, value, newValue) {
166
116
  if (pr.typ == EnumToken.BinaryExpressionTokenType) {
167
117
  if (pr.l == val) {
168
118
  pr.l = newValue;
119
+ return;
169
120
  }
170
121
  else {
171
122
  pr.r = newValue;
123
+ return;
172
124
  }
173
125
  }
174
126
  else {
175
127
  pr.chi.splice(pr.chi.indexOf(val), 1, newValue);
128
+ return;
176
129
  }
177
130
  }
178
131
  }
@@ -235,7 +235,7 @@ function lab2srgbvalues(token) {
235
235
  return null;
236
236
  }
237
237
  const rgb = Lab_to_sRGB(l, a, b);
238
- if (alpha != null && alpha != 1) {
238
+ if (alpha != null && alpha < 1) {
239
239
  rgb.push(alpha);
240
240
  }
241
241
  return rgb;