@tbela99/css-parser 1.6.1 → 1.6.3

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 (39) hide show
  1. package/CHANGELOG.md +119 -2
  2. package/README.md +1 -0
  3. package/dist/index-umd-web.js +1214 -247
  4. package/dist/index.cjs +1214 -247
  5. package/dist/index.d.ts +71 -6
  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/math/expression.js +156 -1
  9. package/dist/lib/ast/transform/compute.js +4 -8
  10. package/dist/lib/ast/types.js +12 -0
  11. package/dist/lib/parser/declaration/list.js +8 -10
  12. package/dist/lib/parser/parse.js +2 -9
  13. package/dist/lib/parser/tokenize.js +50 -18
  14. package/dist/lib/parser/utils/at-rule-font-feature-values.js +1 -1
  15. package/dist/lib/parser/utils/at-rule-media.js +66 -0
  16. package/dist/lib/parser/utils/at-rule-when-else.js +2 -2
  17. package/dist/lib/parser/utils/base64.js +14 -0
  18. package/dist/lib/parser/utils/declaration.js +7 -14
  19. package/dist/lib/parser/utils/eq.js +6 -0
  20. package/dist/lib/parser/utils/hash.js +1 -0
  21. package/dist/lib/parser/utils/selector.js +1 -1
  22. package/dist/lib/parser/utils/text.js +16 -0
  23. package/dist/lib/renderer/render.js +18 -4
  24. package/dist/lib/renderer/sourcemap/lib/codec.js +5 -0
  25. package/dist/lib/renderer/sourcemap/sourcemap.js +35 -5
  26. package/dist/lib/syntax/color/alpha.js +57 -22
  27. package/dist/lib/syntax/color/color-mix.js +18 -18
  28. package/dist/lib/syntax/color/srgb.js +10 -5
  29. package/dist/lib/syntax/color/utils/components.js +1 -1
  30. package/dist/lib/syntax/color/utils/distance.js +2 -2
  31. package/dist/lib/syntax/color/utils/matrix.js +1 -0
  32. package/dist/lib/syntax/color/xyz.js +5 -0
  33. package/dist/lib/syntax/color/xyzd50.js +17 -0
  34. package/dist/lib/syntax/syntax.js +549 -13
  35. package/dist/lib/validation/config.js +26 -0
  36. package/dist/lib/validation/match.js +16 -4
  37. package/dist/node.js +8 -14
  38. package/dist/web.js +8 -14
  39. package/package.json +1 -1
@@ -5,6 +5,7 @@ import { tokensfuncDefMap, mediaTypes, LOCSTA, LOCEND, mFLT, mFGT, LOCSRCID } fr
5
5
  import { trimArray, matchAllSyntaxes, createValidationContext, getMFInfo, isMFValue } from '../../validation/match.js';
6
6
  import { ValidationSyntaxGroupEnum, MediaFeatureType } from '../../validation/parser/typedef.js';
7
7
  import { getParsedSyntax } from '../../validation/config.js';
8
+ import { equalsIgnoreCase } from './text.js';
8
9
 
9
10
  // https://drafts.csswg.org/mediaqueries/#media-descriptor-table:~:text=It%20is%20invalid%20to%20mix%20and%20and%20or%20and%20not%20at%20the%20same%20%E2%80%9Clevel%E2%80%9D%20of%20a%20media%20query%2E
10
11
  // 'or' is not allowed at the same level as 'and' and 'not'
@@ -285,6 +286,16 @@ function parseMediaqueryList(stream, options) {
285
286
  val.splice(0, val.length, ...filteredValues);
286
287
  }
287
288
  }
289
+ if (stack.at(-1)?.typ === EnumToken.ColonTokenType) {
290
+ if (name.startsWith("min-")) {
291
+ filteredNames[0].val = name.substring(4);
292
+ stack.at(-1).typ = EnumToken.GteTokenType;
293
+ }
294
+ else if (name.startsWith("max-")) {
295
+ filteredNames[0].val = name.substring(4);
296
+ stack.at(-1).typ = EnumToken.LteTokenType;
297
+ }
298
+ }
288
299
  // @ts-expect-error
289
300
  tokens.splice(index3 + 1, tokens.length - index3 - 2, {
290
301
  typ: EnumToken.MediaQueryConditionTokenType,
@@ -340,6 +351,61 @@ function parseMediaqueryList(stream, options) {
340
351
  [LOCEND]: right.at(-1)[LOCEND],
341
352
  };
342
353
  tokens.length = l + 1;
354
+ // media range query
355
+ if (options.minify &&
356
+ tokens[l].op.typ === EnumToken.AndTokenType) {
357
+ if (left.length === 1 &&
358
+ left[0].typ == EnumToken.ParensTokenType &&
359
+ left[0].chi.length == 1 &&
360
+ left[0].chi[0].typ == EnumToken.MediaQueryConditionTokenType &&
361
+ left[0].chi[0].l.length == 1 &&
362
+ left[0].chi[0].l[0].typ ==
363
+ EnumToken.IdenTokenType &&
364
+ (left[0].chi[0].op.typ ==
365
+ EnumToken.GtTokenType ||
366
+ left[0].chi[0].op.typ ==
367
+ EnumToken.GteTokenType) &&
368
+ right.length === 1 &&
369
+ left[0].typ == EnumToken.ParensTokenType &&
370
+ right[0].chi.length == 1 &&
371
+ right[0].chi[0].typ ==
372
+ EnumToken.MediaQueryConditionTokenType &&
373
+ right[0].chi[0].l.length == 1 &&
374
+ right[0].chi[0].l[0].typ ==
375
+ EnumToken.IdenTokenType &&
376
+ (right[0].chi[0].op.typ ==
377
+ EnumToken.LtTokenType ||
378
+ right[0].chi[0].op.typ ==
379
+ EnumToken.LteTokenType) &&
380
+ equalsIgnoreCase(left[0].chi[0]
381
+ .l[0].val, right[0].chi[0]
382
+ .l[0].val)) {
383
+ tokens[l] = {
384
+ typ: EnumToken.ParensTokenType,
385
+ chi: [
386
+ {
387
+ typ: EnumToken.MediaRangeQueryTokenType,
388
+ op1: Object.assign(left[0].chi[0]
389
+ .op, {
390
+ typ: left[0]
391
+ .chi[0].op.typ == EnumToken.GteTokenType
392
+ ? EnumToken.LteTokenType
393
+ : EnumToken.LtTokenType,
394
+ }),
395
+ op2: right[0].chi[0]
396
+ .op,
397
+ l: left[0].chi[0].r,
398
+ val: left[0].chi[0]
399
+ .l,
400
+ r: right[0].chi[0].r,
401
+ [LOCSRCID]: left[0][LOCSRCID],
402
+ [LOCSTA]: left[0][LOCSTA],
403
+ [LOCEND]: right.at(-1)[LOCEND],
404
+ },
405
+ ],
406
+ };
407
+ }
408
+ }
343
409
  expectAndOrComma = true;
344
410
  }
345
411
  }
@@ -10,7 +10,7 @@ function matchAtRuleWhenElseSyntax(stream, context, options = {}) {
10
10
  let i = 0;
11
11
  let success = true;
12
12
  let expectAndOr = false;
13
- let scope = new Set();
13
+ // let scope: Set<EnumToken> = new Set();
14
14
  const errors = [];
15
15
  // const scopes: Array<Set<EnumToken>> = [scope];
16
16
  for (; i < stream.length; i++) {
@@ -32,7 +32,7 @@ function matchAtRuleWhenElseSyntax(stream, context, options = {}) {
32
32
  Object.assign(stream[i], {
33
33
  typ: "or" === val ? EnumToken.OrTokenType : EnumToken.AndTokenType,
34
34
  });
35
- scope.add(stream[i].typ);
35
+ // scope.add(stream[i].typ);
36
36
  stack.push(stream[i]);
37
37
  // break;
38
38
  }
@@ -0,0 +1,14 @@
1
+ /**
2
+ *
3
+ * @param value
4
+ * @returns
5
+ */
6
+ function toBase64(value) {
7
+ let result = '';
8
+ for (const c of new TextEncoder().encode(value)) {
9
+ result += String.fromCharCode(c);
10
+ }
11
+ return btoa(result);
12
+ }
13
+
14
+ export { toBase64 };
@@ -1,4 +1,4 @@
1
- import { EnumToken, EnumAstNodeStatus, ColorType, ValidationLevel } from '../../ast/types.js';
1
+ import { EnumToken, EnumAstNodeStatus, ColorType } from '../../ast/types.js';
2
2
  import { LOCEND, STATE, ERRORS, LOCSTA, tokensfuncDefMap, COLORS_NAMES, nonStandardColors, systemColors, deprecatedSystemColors, tokensMap, trimTokenSpace, LOCSRCID } from '../../syntax/constants.js';
3
3
  import { renamedStandardProperties, isColor, parseColor, isWhiteSpace } from '../../syntax/syntax.js';
4
4
  import { getSyntaxRule, getParsedSyntax } from '../../validation/config.js';
@@ -164,7 +164,7 @@ function parseDeclaration(tokens, parent, options, errors) {
164
164
  let token;
165
165
  let index;
166
166
  if (syntaxRules != null) {
167
- const doNotValidate = options.validation === false || options.validation === ValidationLevel.None;
167
+ const doNotValidate = options.validation === false;
168
168
  result = doNotValidate ? null : matchAllSyntaxes(syntaxRules, createValidationContext(tokens), options);
169
169
  if (doNotValidate || result != null) {
170
170
  success = doNotValidate || result?.success;
@@ -195,17 +195,11 @@ function parseDeclaration(tokens, parent, options, errors) {
195
195
  stack.push(token);
196
196
  continue;
197
197
  }
198
+ if (token.typ === EnumToken.ColonTokenType && stack.length == 0) {
199
+ stack.push(token);
200
+ continue;
201
+ }
198
202
  switch (token.typ) {
199
- // case EnumToken.IdenTokenType:
200
- // if (tokens[i + 1]?.typ == EnumToken.StartParensTokenType) {
201
- // Object.assign(token, {
202
- // typ: EnumToken.FunctionTokenDefType,
203
- // });
204
- // token[LOCEND] = tokens[i + 1][LOCEND];
205
- // tokens.splice(i + 1, 1);
206
- // stack.push(token);
207
- // }
208
- // break;
209
203
  case EnumToken.Literal:
210
204
  if (token.val === "/" && stack.at(-1)?.typ == EnumToken.MathFunctionTokenDefType) {
211
205
  Object.assign(token, {
@@ -447,12 +441,11 @@ function parseDeclaration(tokens, parent, options, errors) {
447
441
  name[STATE] = EnumAstNodeStatus.Unknown;
448
442
  name[ERRORS] = result?.errors ?? [];
449
443
  // @ts-expect-error
450
- const node = Object.assign(name, {
444
+ return Object.assign(name, {
451
445
  typ: EnumToken.DeclarationNodeType,
452
446
  nam: name.val,
453
447
  val: tokens,
454
448
  });
455
- return node;
456
449
  }
457
450
  if (equalsIgnoreCase("composes", name.val)) {
458
451
  let index = -1;
@@ -1,3 +1,9 @@
1
+ /**
2
+ *
3
+ * @param a
4
+ * @param b
5
+ * @returns
6
+ */
1
7
  function eq(a, b) {
2
8
  if (a == null || b == null) {
3
9
  return a == b;
@@ -65,6 +65,7 @@ function objectHash(object) {
65
65
  /**
66
66
  * convert input to hex
67
67
  * @param input
68
+ * @param length
68
69
  */
69
70
  function toHex(input, length) {
70
71
  let result = "";
@@ -232,7 +232,7 @@ function parseSelector(tokens, context, options, errors) {
232
232
  }
233
233
  tokens.length = write;
234
234
  }
235
- const result = matchSelectorSyntax(tokens, errors, options, nested === true);
235
+ const result = matchSelectorSyntax(tokens, errors, options, nested);
236
236
  trimArray(tokens);
237
237
  if (result.success) {
238
238
  for (let i = 0; i < tokens.length; i++) {
@@ -1,9 +1,25 @@
1
+ /**
2
+ *
3
+ * @param value
4
+ * @returns
5
+ */
1
6
  function dasherize(value) {
2
7
  return value.replace(/([A-Z])/g, (all, one) => `-${one.toLowerCase()}`);
3
8
  }
9
+ /**
10
+ *
11
+ * @param value
12
+ * @returns
13
+ */
4
14
  function camelize(value) {
5
15
  return value.replace(/-([a-z])/g, (all, one) => one.toUpperCase());
6
16
  }
17
+ /**
18
+ *
19
+ * @param a
20
+ * @param b
21
+ * @returns
22
+ */
7
23
  function equalsIgnoreCase(a, b) {
8
24
  if (a.length !== b.length)
9
25
  return false;
@@ -10,6 +10,7 @@ import { toDegrees } from '../parser/utils/angle.js';
10
10
  import { LineMap } from '../parser/linesmap.js';
11
11
  import { dirname } from '../fs/resolve.js';
12
12
  import { SourceFile } from '../parser/source.js';
13
+ import { cloneNode } from '../ast/clone.js';
13
14
 
14
15
  /**
15
16
  * render ast
@@ -48,11 +49,13 @@ function doRender(data, options = {}, mapping) {
48
49
  ...options,
49
50
  };
50
51
  if (options.withParents) {
51
- // @ts-ignore
52
- let parent = data[PARENT];
52
+ let parent;
53
53
  while (data[PARENT] != null) {
54
54
  // @ts-ignore
55
- parent = { ...data[PARENT], chi: [{ ...data }] };
55
+ // parent = { ...data[PARENT], chi: [data] };
56
+ parent = cloneNode(data[PARENT]);
57
+ // @ts-ignore
58
+ parent.chi = [data];
56
59
  // @ts-ignore
57
60
  parent[PARENT] = data[PARENT][PARENT];
58
61
  // @ts-ignore
@@ -222,6 +225,8 @@ function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, lines
222
225
  * @param sourceLocation
223
226
  * @param linesMap
224
227
  * @param str
228
+ * @param start
229
+ * @param end
225
230
  */
226
231
  function move(sourceLocation, linesMap, str, start, end) {
227
232
  let i = start ?? 0;
@@ -414,8 +419,8 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
414
419
  };
415
420
  }
416
421
  switch (token.typ) {
417
- case EnumToken.FunctionTokenDefType:
418
422
  case EnumToken.UrlFunctionTokenDefType:
423
+ case EnumToken.FunctionTokenDefType:
419
424
  case EnumToken.MathFunctionTokenDefType:
420
425
  case EnumToken.ImageFunctionTokenDefType:
421
426
  case EnumToken.ColorFunctionTokenDefType:
@@ -559,6 +564,9 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
559
564
  }
560
565
  case EnumToken.UrlFunctionTokenType:
561
566
  if (options.minify && token.typ === EnumToken.UrlFunctionTokenType) {
567
+ if (token.chi[0]?.typ === EnumToken.BadUrlTokenType) {
568
+ return "url()";
569
+ }
562
570
  for (const child of token.chi) {
563
571
  if (child.typ === EnumToken.StringTokenType) {
564
572
  if (child.val.slice(1, 5) !== "data:" &&
@@ -1246,6 +1254,12 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1246
1254
  return token.val.typ == EnumToken.FractionTokenType
1247
1255
  ? renderValue(token.val, options, cache)
1248
1256
  : minifyNumber(token.val);
1257
+ case EnumToken.InfinityTokenType:
1258
+ return "1/0";
1259
+ case EnumToken.NegativeInfinityTokenType:
1260
+ return "-1/0";
1261
+ case EnumToken.NaNTokenType:
1262
+ return "NaN";
1249
1263
  case EnumToken.AtRuleTokenType:
1250
1264
  return "@" + token.nam;
1251
1265
  case EnumToken.CommentTokenType:
@@ -56,6 +56,11 @@ function encode(value) {
56
56
  }
57
57
  return result;
58
58
  }
59
+ /**
60
+ *
61
+ * @param num
62
+ * @returns
63
+ */
59
64
  function encode_integer(num) {
60
65
  let result = '';
61
66
  if (num < 0) {
@@ -1,7 +1,37 @@
1
+ import { toBase64 } from '../../parser/utils/base64.js';
1
2
  import { decode, encode } from './lib/codec.js';
2
3
 
3
4
  /**
4
5
  * Generate and parse source map
6
+ *
7
+ * ```ts
8
+ * const sourcemap = new SourceMap();
9
+ *
10
+ * // you can also pass an input sourcemap
11
+ * // const sourcemap = new SourceMap(sourcemapObjectOrJSONString);
12
+ *
13
+ * sourcemap.addSourceContent(0, '/css/styles.css', null);
14
+ * sourcemap.addSourceContent(1, '/css/typography.css', null);
15
+ *
16
+ * const maps = [];
17
+ *
18
+ * maps.add([
19
+ 1, 1, 0, 1, 1]);
20
+ * maps.add([
21
+ 2, 1, 1, 1, 1
22
+ ]);
23
+
24
+ sourcemap.add(maps);
25
+
26
+ // convert to inline sourcemap
27
+ sourcemap.toUrl();
28
+
29
+ // convert to JSON
30
+ sourcemap.toJSON();
31
+
32
+ // find the original file, line and column
33
+ sourcemap.find(2, 1);
34
+ ```
5
35
  */
6
36
  class SourceMap {
7
37
  /**
@@ -52,7 +82,7 @@ class SourceMap {
52
82
  line = -1;
53
83
  /**
54
84
  *
55
- * @param sourcemaps
85
+ * @param sourcemaps input sourcemap
56
86
  */
57
87
  constructor(sourcemaps) {
58
88
  if (typeof sourcemaps === "string") {
@@ -93,9 +123,9 @@ class SourceMap {
93
123
  }
94
124
  /**
95
125
  * add source
96
- * @param id
97
- * @param fileName
98
- * @param content
126
+ * @param id source id
127
+ * @param fileName source file
128
+ * @param content source content
99
129
  * @returns
100
130
  */
101
131
  addSourceContent(id, fileName, content) {
@@ -231,7 +261,7 @@ class SourceMap {
231
261
  */
232
262
  toUrl() {
233
263
  // /*# sourceMappingURL = ${url} */
234
- return `data:application/json;charset=utf-8;base64,${btoa(JSON.stringify(this.toJSON()))}`;
264
+ return `data:application/json;charset=utf-8;base64,${toBase64(JSON.stringify(this.toJSON()))}`;
235
265
  }
236
266
  /**
237
267
  * Convert to JSON object
@@ -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);
@@ -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 = [];
@@ -117,11 +126,7 @@ function cmyk2srgbvalues(token) {
117
126
  // @ts-ignore
118
127
  const k = getNumber(t);
119
128
  const mul = 1 - k;
120
- const rgb = [
121
- 1 - Math.min(1, c * mul + k),
122
- 1 - Math.min(1, m * mul + k),
123
- 1 - Math.min(1, y * mul + k),
124
- ];
129
+ const rgb = [1 - Math.min(1, c * mul + k), 1 - Math.min(1, m * mul + k), 1 - Math.min(1, y * mul + k)];
125
130
  if (components.length == 5) {
126
131
  rgb.push(getNumber(components[4]));
127
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) {