@tbela99/css-parser 1.4.6 → 1.4.8

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 (55) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +1 -0
  3. package/dist/index-umd-web.js +6578 -6969
  4. package/dist/index.cjs +7808 -8197
  5. package/dist/index.d.ts +923 -51
  6. package/dist/lib/ast/clone.js +16 -23
  7. package/dist/lib/ast/expand.js +2 -2
  8. package/dist/lib/ast/features/if.js +88 -87
  9. package/dist/lib/ast/features/inlinecssvariables.js +2 -2
  10. package/dist/lib/ast/features/prefix.js +26 -22
  11. package/dist/lib/ast/features/transform.js +3 -3
  12. package/dist/lib/ast/find.js +17 -14
  13. package/dist/lib/ast/minify.js +97 -94
  14. package/dist/lib/ast/types.js +10 -2
  15. package/dist/lib/ast/walk.js +1 -1
  16. package/dist/lib/fs/resolve.js +47 -45
  17. package/dist/lib/parser/arena.js +206 -0
  18. package/dist/lib/parser/declaration/list.js +9 -10
  19. package/dist/lib/parser/declaration/map.js +13 -18
  20. package/dist/lib/parser/linemap.js +62 -0
  21. package/dist/lib/parser/parse.js +331 -504
  22. package/dist/lib/parser/source.js +72 -0
  23. package/dist/lib/parser/tokenize.js +39 -33
  24. package/dist/lib/parser/utils/at-rule-container.js +244 -247
  25. package/dist/lib/parser/utils/at-rule-generic.js +25 -25
  26. package/dist/lib/parser/utils/at-rule-import.js +194 -234
  27. package/dist/lib/parser/utils/at-rule-media.js +284 -283
  28. package/dist/lib/parser/utils/at-rule-support.js +279 -251
  29. package/dist/lib/parser/utils/at-rule-when-else.js +256 -259
  30. package/dist/lib/parser/utils/at-rule.js +19 -16
  31. package/dist/lib/parser/utils/cache.js +33 -5
  32. package/dist/lib/parser/utils/declaration.js +260 -298
  33. package/dist/lib/parser/utils/hash.js +15 -15
  34. package/dist/lib/parser/utils/selector.js +182 -222
  35. package/dist/lib/parser/utils/token.js +4 -7
  36. package/dist/lib/renderer/render.js +152 -121
  37. package/dist/lib/syntax/color/alpha.js +1 -4
  38. package/dist/lib/syntax/color/color-mix.js +8 -1
  39. package/dist/lib/syntax/color/color.js +17 -9
  40. package/dist/lib/syntax/color/hex.js +1 -1
  41. package/dist/lib/syntax/color/p3.js +9 -1
  42. package/dist/lib/syntax/color/utils/colorspace.js +3 -3
  43. package/dist/lib/syntax/color/utils/components.js +21 -22
  44. package/dist/lib/syntax/color/utils/distance.js +6 -1
  45. package/dist/lib/syntax/color/utils/matrix.js +12 -11
  46. package/dist/lib/syntax/constants.js +25 -3
  47. package/dist/lib/syntax/syntax.js +240 -311
  48. package/dist/lib/validation/config.js +16 -17
  49. package/dist/lib/validation/match.js +1118 -1131
  50. package/dist/lib/validation/parser/parse.js +130 -286
  51. package/dist/node.js +18 -14
  52. package/dist/web.js +12 -10
  53. package/package.json +4 -4
  54. package/dist/lib/validation/utils/whitespace.js +0 -17
  55. /package/dist/lib/syntax/color/{relativecolor.js → relative-color.js} +0 -0
@@ -1,5 +1,5 @@
1
1
  import { EnumToken } from '../../ast/types.js';
2
- import { definedPropertySettings, tokensfuncSet } from '../../syntax/constants.js';
2
+ import { PARENT, tokensfuncSet } from '../../syntax/constants.js';
3
3
 
4
4
  const trimTokenSpace = new Set([
5
5
  EnumToken.CommaTokenType,
@@ -28,12 +28,9 @@ function replaceNodeOrValue(parent, node, replacement) {
28
28
  if (replacement == null || (Array.isArray(replacement) && replacement.length === 0)) {
29
29
  throw new TypeError(`replacement is null`);
30
30
  }
31
- for (const node of Array.isArray(replacement) ? replacement : [replacement]) {
32
- if ("parent" in node && node.parent != node.parent) {
33
- Object.defineProperty(node, "parent", {
34
- ...definedPropertySettings,
35
- value: node.parent,
36
- });
31
+ for (const newNode of Array.isArray(replacement) ? replacement : [replacement]) {
32
+ if (newNode[PARENT] != node[PARENT]) {
33
+ newNode[PARENT] = node[PARENT];
37
34
  }
38
35
  }
39
36
  if (parent.typ == EnumToken.BinaryExpressionTokenType) {
@@ -1,10 +1,10 @@
1
- import { convertColor, getAngle } from '../syntax/color/color.js';
1
+ import { convertColor, toPrecisionAngle, toPrecisionValue, getAngle } from '../syntax/color/color.js';
2
2
  import { reduceHexValue } from '../syntax/color/hex.js';
3
3
  import { EnumToken, minifyNumber, ColorType } from '../ast/types.js';
4
4
  import { expand } from '../ast/expand.js';
5
5
  import { SourceMap } from './sourcemap/sourcemap.js';
6
- import { colorsFunc, urlTokenMatcher, tokensfuncSet } from '../syntax/constants.js';
7
- import { isColor, pseudoElements, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor } from '../syntax/syntax.js';
6
+ import { pseudoElements, urlTokenMatcher, PARENT, tokensfuncSet, LOC, colorPrecision } from '../syntax/constants.js';
7
+ import { reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor } from '../syntax/syntax.js';
8
8
  import { move } from '../parser/tokenize.js';
9
9
  import { equalsIgnoreCase } from '../parser/utils/text.js';
10
10
  import { toDegrees } from '../parser/utils/angle.js';
@@ -47,13 +47,12 @@ function doRender(data, options = {}, mapping) {
47
47
  };
48
48
  if (options.withParents) {
49
49
  // @ts-ignore
50
- let parent = data.parent;
51
- // @ts-ignore
52
- while (data.parent != null) {
50
+ let parent = data[PARENT];
51
+ while (data[PARENT] != null) {
53
52
  // @ts-ignore
54
- parent = { ...data.parent, chi: [{ ...data }] };
53
+ parent = { ...data[PARENT], chi: [{ ...data }] };
55
54
  // @ts-ignore
56
- parent.parent = data.parent.parent;
55
+ parent[PARENT] = data[PARENT][PARENT];
57
56
  // @ts-ignore
58
57
  data = parent;
59
58
  }
@@ -127,11 +126,12 @@ function updateSourceMap(node, options, cache, sourcemap, position, str) {
127
126
  EnumToken.KeyFramesRuleNodeType,
128
127
  EnumToken.KeyframesAtRuleNodeType,
129
128
  ].includes(node.typ)) {
130
- let src = node.loc?.src ?? "";
129
+ let src = node[LOC]?.src ?? "";
130
+ // console.debug(src);
131
131
  sourcemap.add(
132
132
  // @ts-ignore
133
133
  { src, sta: { ...position } }, {
134
- ...node.loc,
134
+ ...node[LOC],
135
135
  // @ts-ignore
136
136
  src,
137
137
  });
@@ -182,12 +182,12 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
182
182
  return css;
183
183
  }
184
184
  if (css === "") {
185
- if (sourcemap != null && node.loc != null) {
185
+ if (sourcemap != null && node[LOC] != null) {
186
186
  updateSourceMap(node, options, cache, sourcemap, position, str);
187
187
  }
188
188
  return str;
189
189
  }
190
- if (sourcemap != null && node.loc != null) {
190
+ if (sourcemap != null && node[LOC] != null) {
191
191
  move(position, options.newLine);
192
192
  updateSourceMap(node, options, cache, sourcemap, position, str);
193
193
  }
@@ -211,24 +211,24 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
211
211
  : node.val;
212
212
  }
213
213
  else if (node.typ == EnumToken.DeclarationNodeType) {
214
- if (!node.nam.startsWith("--") && node.val.length === 0) {
215
- // @ts-ignore
216
- errors.push({
217
- action: "ignore",
218
- message: `render: invalid declaration ${JSON.stringify(node)}`,
219
- location: node.loc,
220
- });
221
- return "";
222
- }
214
+ // if (!(<AstDeclaration>node).nam.startsWith("--") && (<AstDeclaration>node).val.length === 0) {
215
+ // // @ts-ignore
216
+ // errors.push(<ErrorDescription>{
217
+ // action: "ignore",
218
+ // message: `render: invalid declaration ${JSON.stringify(node)}`,
219
+ // location: node[LOC],
220
+ // });
221
+ // return "";
222
+ // }
223
223
  str = `${node.nam}:${options.indent}${(options.minify
224
224
  ? filterValues(node.val)
225
225
  : node.val)
226
226
  .reduce(reducer, "")
227
227
  .trimEnd()};`;
228
228
  }
229
- else if (node.typ == EnumToken.AtRuleNodeType && !("chi" in node)) {
230
- str = `${node.val === "" ? "" : options.indent || " "}${node.val};`;
231
- }
229
+ // else if (node.typ == EnumToken.AtRuleNodeType && !("chi" in node)) {
230
+ // str = `${(<AstAtRule>node).val === "" ? "" : options.indent || " "}${(<AstAtRule>node).val};`;
231
+ // }
232
232
  else {
233
233
  str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level + 1, indents);
234
234
  }
@@ -257,22 +257,24 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
257
257
  (children === "" ? "" : indentSub + children + options.newLine) +
258
258
  indent +
259
259
  `}`);
260
- case EnumToken.CssVariableTokenType:
261
- case EnumToken.CssVariableImportTokenType:
262
- return `@value ${data.val}:${options.indent}${filterValues(options.minify
263
- ? data.val
264
- : data.val)
265
- .reduce(reducer, "")
266
- .trim()};`;
267
- case EnumToken.CssVariableDeclarationMapTokenType:
268
- return `@value ${filterValues(data.vars)
269
- .reduce((acc, curr) => acc + renderValue(curr), "")
270
- .trim()} from ${filterValues(data.from)
271
- .reduce((acc, curr) => acc + renderValue(curr), "")
272
- .trim()};`;
273
- case EnumToken.InvalidDeclarationNodeType:
274
- case EnumToken.InvalidRuleNodeType:
275
- case EnumToken.InvalidAtRuleNodeType:
260
+ // case EnumToken.CssVariableTokenType:
261
+ // case EnumToken.CssVariableImportTokenType:
262
+ // return `@value ${(<CssVariableToken | CssVariableImportTokenType>data).val}:${options.indent}${filterValues(
263
+ // options.minify
264
+ // ? (<CssVariableToken | CssVariableImportTokenType>data).val
265
+ // : (<CssVariableToken>data).val,
266
+ // )
267
+ // .reduce(reducer, "")
268
+ // .trim()};`;
269
+ // case EnumToken.CssVariableDeclarationMapTokenType:
270
+ // return `@value ${filterValues((data as CssVariableMapTokenType).vars)
271
+ // .reduce((acc, curr) => acc + renderValue(curr), "")
272
+ // .trim()} from ${filterValues((data as CssVariableMapTokenType).from)
273
+ // .reduce((acc, curr) => acc + renderValue(curr), "")
274
+ // .trim()};`;
275
+ // case EnumToken.InvalidDeclarationNodeType:
276
+ // case EnumToken.InvalidRuleNodeType:
277
+ // case EnumToken.InvalidAtRuleNodeType:
276
278
  default:
277
279
  return "";
278
280
  }
@@ -300,42 +302,46 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
300
302
  return acc + renderValue(curr, options, cache, reducer, errors);
301
303
  };
302
304
  }
303
- if (token.typ == EnumToken.FunctionTokenType && colorsFunc.includes(token.val)) {
304
- if (isColor(token)) {
305
- // @ts-ignore
306
- token.typ = EnumToken.ColorTokenType;
307
- if (
308
- // @ts-ignore
309
- token.chi[0].typ == EnumToken.IdenTokenType &&
310
- // @ts-ignore
311
- token.chi[0].val == "from") {
312
- // @ts-ignore
313
- token.cal = "rel";
314
- }
315
- else {
316
- // @ts-ignore
317
- if (token.val == "color-mix" &&
318
- token.chi?.[0]?.typ == EnumToken.IdenTokenType &&
319
- (token.chi?.[0]).val == "in") {
320
- // @ts-ignore
321
- token.cal = "mix";
322
- }
323
- else {
324
- // @ts-ignore
325
- if (token.val == "color") {
326
- // @ts-ignore
327
- token.cal = "col";
328
- }
329
- // @ts-ignore
330
- token.chi = token.chi.filter((t) => ![
331
- EnumToken.WhitespaceTokenType,
332
- EnumToken.CommaTokenType,
333
- EnumToken.CommentTokenType,
334
- ].includes(t.typ));
335
- }
336
- }
337
- }
338
- }
305
+ // if (token.typ == EnumToken.FunctionTokenType && colorsFunc.includes((token as FunctionToken).val)) {
306
+ // if (isColor(token)) {
307
+ // // @ts-ignore
308
+ // token.typ = EnumToken.ColorTokenType;
309
+ // if (
310
+ // // @ts-ignore
311
+ // (token as ColorToken)!.chi[0]!.typ == EnumToken.IdenTokenType &&
312
+ // // @ts-ignore
313
+ // ((token as ColorToken)!.chi[0] as IdentToken).val == "from"
314
+ // ) {
315
+ // // @ts-ignore
316
+ // (<ColorToken>token).cal = "rel";
317
+ // } else {
318
+ // // @ts-ignore
319
+ // if (
320
+ // (token as ColorToken).val == "color-mix" &&
321
+ // (token as ColorToken).chi?.[0]?.typ == EnumToken.IdenTokenType &&
322
+ // ((token as ColorToken).chi?.[0] as IdentToken).val == "in"
323
+ // ) {
324
+ // // @ts-ignore
325
+ // (<ColorToken>token).cal = "mix";
326
+ // } else {
327
+ // // @ts-ignore
328
+ // if ((token as ColorToken).val == "color") {
329
+ // // @ts-ignore
330
+ // token.cal = "col";
331
+ // }
332
+ // // @ts-ignore
333
+ // (token as ColorToken).chi = (token as ColorToken).chi!.filter(
334
+ // (t: Token) =>
335
+ // ![
336
+ // EnumToken.WhitespaceTokenType,
337
+ // EnumToken.CommaTokenType,
338
+ // EnumToken.CommentTokenType,
339
+ // ].includes(t.typ),
340
+ // );
341
+ // }
342
+ // }
343
+ // }
344
+ // }
339
345
  switch (token.typ) {
340
346
  case EnumToken.FunctionTokenDefType:
341
347
  case EnumToken.UrlFunctionTokenDefType:
@@ -399,6 +405,10 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
399
405
  return "*";
400
406
  case EnumToken.Div:
401
407
  return "/";
408
+ case EnumToken.WrappedValuesTokenType:
409
+ return ("{" +
410
+ token.chi.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
411
+ "}");
402
412
  case EnumToken.ColorTokenType:
403
413
  if (token.kin == ColorType.LIGHT_DARK ||
404
414
  ("chi" in token && options.convertColor === false && token.chi.length == 2)) {
@@ -569,20 +579,22 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
569
579
  else if (slice[i].typ == EnumToken.AngleTokenType) {
570
580
  if ((180 - toDegrees(slice[i]).val) % 360 === 0) {
571
581
  k = i + 1;
572
- while (k < slice.length && slice[k].typ !== EnumToken.CommaTokenType) {
573
- k++;
574
- }
582
+ // while (k < slice.length && slice[k].typ !== EnumToken.CommaTokenType) {
583
+ // k++;
584
+ // }
575
585
  slice.splice(i, k - i + 1);
576
586
  }
577
587
  }
578
588
  }
579
589
  if (slice[i]?.typ === EnumToken.CommaTokenType) {
580
590
  i++;
581
- while (i < slice.length &&
582
- (slice[i].typ === EnumToken.WhitespaceTokenType ||
583
- slice[i].typ === EnumToken.CommentTokenType)) {
584
- i++;
585
- }
591
+ // while (
592
+ // i < slice.length &&
593
+ // (slice[i].typ === EnumToken.WhitespaceTokenType ||
594
+ // slice[i].typ === EnumToken.CommentTokenType)
595
+ // ) {
596
+ // i++;
597
+ // }
586
598
  }
587
599
  if (slice[i]?.typ === EnumToken.ColorTokenType) {
588
600
  slice.push(...reduceColorStops(slice.splice(i, slice.length - i)));
@@ -593,11 +605,13 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
593
605
  case "repeating-radial-gradient":
594
606
  {
595
607
  let i = 0;
596
- while (i < slice.length &&
597
- (slice[i].typ === EnumToken.WhitespaceTokenType ||
598
- slice[i].typ === EnumToken.CommentTokenType)) {
599
- i++;
600
- }
608
+ // while (
609
+ // i < slice.length &&
610
+ // (slice[i].typ === EnumToken.WhitespaceTokenType ||
611
+ // slice[i].typ === EnumToken.CommentTokenType)
612
+ // ) {
613
+ // i++;
614
+ // }
601
615
  const positions = [];
602
616
  const form = [];
603
617
  const size = [];
@@ -809,11 +823,13 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
809
823
  const angles = [];
810
824
  const positions = [];
811
825
  const colorSpaceDef = [];
812
- while (i < slice.length &&
813
- (slice[i].typ === EnumToken.WhitespaceTokenType ||
814
- slice[i].typ === EnumToken.CommentTokenType)) {
815
- i++;
816
- }
826
+ // while (
827
+ // i < slice.length &&
828
+ // (slice[i].typ === EnumToken.WhitespaceTokenType ||
829
+ // slice[i].typ === EnumToken.CommentTokenType)
830
+ // ) {
831
+ // i++;
832
+ // }
817
833
  if (slice[i]?.typ === EnumToken.IdenTokenType &&
818
834
  equalsIgnoreCase(slice[i].val, "from")) {
819
835
  angles.push(slice[i++]);
@@ -897,13 +913,18 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
897
913
  i++;
898
914
  }
899
915
  const result = [];
916
+ if (positions.length > 0) {
917
+ if (positions.length > 0) {
918
+ if (angles.length > 0) {
919
+ angles.push({ typ: EnumToken.WhitespaceTokenType });
920
+ }
921
+ angles.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }, ...positions);
922
+ }
923
+ }
900
924
  if (angles.length > 0) {
901
925
  result.push(...angles, { typ: EnumToken.CommaTokenType });
902
926
  }
903
- if (positions.length > 0 || colorSpaceDef.length > 0) {
904
- if (positions.length > 0) {
905
- result.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }, ...positions);
906
- }
927
+ if (colorSpaceDef.length > 0) {
907
928
  if (colorSpaceDef.length > 0) {
908
929
  if (result.length > 0) {
909
930
  result.push({ typ: EnumToken.WhitespaceTokenType });
@@ -948,23 +969,32 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
948
969
  "(" +
949
970
  token.chi.reduce(reducer, "") +
950
971
  ")");
951
- case EnumToken.MatchExpressionTokenType:
952
- return (renderValue(token.l, options, cache, reducer, errors) +
953
- renderValue(token.op, options, cache, reducer, errors) +
954
- renderValue(token.r, options, cache, reducer, errors) +
955
- (token.attr ? " " + token.attr : ""));
956
- case EnumToken.NameSpaceAttributeTokenType:
957
- return ((token.l == null
958
- ? ""
959
- : renderValue(token.l, options, cache, reducer, errors)) +
960
- "|" +
961
- renderValue(token.r, options, cache, reducer, errors));
962
- case EnumToken.ComposesSelectorNodeType:
963
- return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
964
- (token.r == null
965
- ? ""
966
- : " from " +
967
- renderValue(token.r, options, cache, reducer, errors)));
972
+ // case EnumToken.MatchExpressionTokenType:
973
+ // return (
974
+ // renderValue((token as MatchExpressionToken).l as Token, options, cache, reducer, errors) +
975
+ // renderValue((token as MatchExpressionToken).op, options, cache, reducer, errors) +
976
+ // renderValue((token as MatchExpressionToken).r, options, cache, reducer, errors) +
977
+ // ((token as MatchExpressionToken).attr ? " " + (token as MatchExpressionToken).attr : "")
978
+ // );
979
+ // case EnumToken.NameSpaceAttributeTokenType:
980
+ // return (
981
+ // ((token as NameSpaceAttributeToken).l == null
982
+ // ? ""
983
+ // : renderValue((token as NameSpaceAttributeToken).l as Token, options, cache, reducer, errors)) +
984
+ // "|" +
985
+ // renderValue((token as NameSpaceAttributeToken).r, options, cache, reducer, errors)
986
+ // );
987
+ // case EnumToken.ComposesSelectorNodeType:
988
+ // return (
989
+ // (token as ComposesSelectorToken).l.reduce(
990
+ // (acc: string, curr: Token) => acc + renderValue(curr, options, cache),
991
+ // "",
992
+ // ) +
993
+ // ((token as ComposesSelectorToken).r == null
994
+ // ? ""
995
+ // : " from " +
996
+ // renderValue((token as ComposesSelectorToken).r as Token, options, cache, reducer, errors))
997
+ // );
968
998
  case EnumToken.BlockStartTokenType:
969
999
  return "{";
970
1000
  case EnumToken.BlockEndTokenType:
@@ -1045,7 +1075,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1045
1075
  }
1046
1076
  switch (u) {
1047
1077
  case "turn":
1048
- v = minifyNumber(angle);
1078
+ v = minifyNumber(toPrecisionAngle(angle, colorPrecision, false));
1049
1079
  if (v.length + 4 < value.length) {
1050
1080
  val = v;
1051
1081
  unit = u;
@@ -1053,7 +1083,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1053
1083
  }
1054
1084
  break;
1055
1085
  case "deg":
1056
- v = minifyNumber(angle * 360);
1086
+ v = minifyNumber(toPrecisionAngle(angle * 360, colorPrecision, false));
1057
1087
  if (v.length + 3 < value.length) {
1058
1088
  val = v;
1059
1089
  unit = u;
@@ -1061,7 +1091,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1061
1091
  }
1062
1092
  break;
1063
1093
  case "rad":
1064
- v = minifyNumber(angle * (2 * Math.PI));
1094
+ v = minifyNumber(toPrecisionAngle(angle * (2 * Math.PI), colorPrecision, false));
1065
1095
  if (v.length + 3 < value.length) {
1066
1096
  val = v;
1067
1097
  unit = u;
@@ -1069,7 +1099,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1069
1099
  }
1070
1100
  break;
1071
1101
  case "grad":
1072
- v = minifyNumber(angle * 400);
1102
+ v = minifyNumber(toPrecisionAngle(angle * 400, colorPrecision, false));
1073
1103
  if (v.length + 4 < value.length) {
1074
1104
  val = v;
1075
1105
  unit = u;
@@ -1106,7 +1136,8 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1106
1136
  if (token.typ == EnumToken.ResolutionTokenType && unit == "dppx") {
1107
1137
  unit = "x";
1108
1138
  }
1109
- return val.includes("/") ? val.replace("/", unit + "/") : val + unit;
1139
+ // @ts-ignore
1140
+ return val.includes("/") ? val.replace("/", unit + "/") : minifyNumber(toPrecisionValue(val)) + unit;
1110
1141
  case EnumToken.FlexTokenType:
1111
1142
  case EnumToken.PercentageTokenType:
1112
1143
  const uni = token.typ == EnumToken.PercentageTokenType ? "%" : "fr";
@@ -1209,7 +1240,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1209
1240
  case EnumToken.OrTokenType:
1210
1241
  return "or";
1211
1242
  case EnumToken.InvalidMediaQueryTokenType:
1212
- case EnumToken.InvalidDeclarationNodeType:
1243
+ // case EnumToken.InvalidDeclarationNodeType:
1213
1244
  case EnumToken.InvalidCommentTokenType:
1214
1245
  case EnumToken.BadCommentTokenType:
1215
1246
  case EnumToken.BadCdoTokenType:
@@ -22,9 +22,6 @@ function alpha(color, alpha) {
22
22
  if (color.kin === ColorType.DEVICE_CMYK) {
23
23
  return null;
24
24
  }
25
- if (alpha == null) {
26
- return color;
27
- }
28
25
  if (color.kin === ColorType.COLOR_MIX || color.cal === "rel") {
29
26
  color = convertColor(color, getColorType(color));
30
27
  if (color == null) {
@@ -35,7 +32,7 @@ function alpha(color, alpha) {
35
32
  if (components == null) {
36
33
  return null;
37
34
  }
38
- if (alpha.typ === EnumToken.IdenTokenType && equalsIgnoreCase(alpha.val, "alpha")) {
35
+ if (alpha?.typ === EnumToken.IdenTokenType && equalsIgnoreCase(alpha.val, "alpha")) {
39
36
  alpha = components[3] ?? {
40
37
  typ: EnumToken.NumberTokenType,
41
38
  val: 1,
@@ -6,7 +6,7 @@ import { srgb2rgb } from './rgb.js';
6
6
  import { srgb2hslvalues } from './hsl.js';
7
7
  import { srgb2hwb } from './hwb.js';
8
8
  import { srgb2labvalues } from './lab.js';
9
- import { srgb2p3values } from './p3.js';
9
+ import { srgb2lp3values, srgb2p3values } from './p3.js';
10
10
  import { getColorComponents } from './utils/components.js';
11
11
  import { srgb2oklch } from './oklch.js';
12
12
  import { srgb2oklab } from './oklab.js';
@@ -127,6 +127,10 @@ function colorMix(...args) {
127
127
  // @ts-ignore
128
128
  values = srgb2p3values(...values);
129
129
  break;
130
+ case "display-p3-linear":
131
+ // @ts-ignore
132
+ values = srgb2lp3values(...values);
133
+ break;
130
134
  case "a98-rgb":
131
135
  // @ts-ignore
132
136
  values = srgb2a98values(...values);
@@ -350,6 +354,9 @@ function colorMix(...args) {
350
354
  case "srgb-linear":
351
355
  case "a98-rgb":
352
356
  case "rec2020":
357
+ case "display-p3":
358
+ case "display-p3-linear":
359
+ case "prophoto-rgb":
353
360
  // @ts-ignore
354
361
  return {
355
362
  typ: EnumToken.ColorTokenType,
@@ -11,15 +11,15 @@ import { oklch2srgbvalues, lch2srgbvalues, oklab2srgbvalues, lab2srgbvalues, cmy
11
11
  import { prophotorgb2srgbvalues, srgb2prophotorgbvalues } from './prophotorgb.js';
12
12
  import { rec20202srgb, srgb2rec2020values } from './rec2020.js';
13
13
  import { srgb2xyz_d65, srgb2xyz } from './xyz.js';
14
- import { p32srgbvalues, srgb2p3values } from './p3.js';
14
+ import { lp32srgbvalues, p32srgbvalues, srgb2p3values } from './p3.js';
15
15
  import { xyzd502srgb } from './xyzd50.js';
16
16
  import { colorMix } from './color-mix.js';
17
17
  import { reduceHexValue, rgb2HexToken, color2HexToken, lch2HexToken, lab2HexToken, oklch2HexToken, oklab2HexToken, cmyk2HexToken, hwb2HexToken, hsl2HexToken } from './hex.js';
18
- import { parseRelativeColorComponents } from './relativecolor.js';
18
+ import { parseRelativeColorComponents } from './relative-color.js';
19
19
  import { isIdentColor } from '../syntax.js';
20
20
  import { color2cmykToken, lch2cmykToken, lab2cmykToken, oklch2cmykToken, oklab2cmyk, hwb2cmykToken, hsl2cmykToken, rgb2cmykToken } from './cmyk.js';
21
21
  import { a98rgb2srgbvalues, srgb2a98values } from './a98rgb.js';
22
- import { definedPropertySettings, colorPrecision, colorFuncColorSpace, epsilon, anglePrecision } from '../constants.js';
22
+ import { LOC, colorPrecision, colorFuncColorSpace, epsilon, anglePrecision } from '../constants.js';
23
23
  import { trimArray } from '../../validation/match.js';
24
24
  import { alpha } from './alpha.js';
25
25
  import { equalsIgnoreCase } from '../../parser/utils/text.js';
@@ -39,7 +39,7 @@ import { equalsIgnoreCase } from '../../parser/utils/text.js';
39
39
  function convertColor(token, to) {
40
40
  if (token.kin == ColorType.SYS ||
41
41
  token.kin == ColorType.DPSYS ||
42
- (isIdentColor(token) && "currentcolor" == token.val.toLowerCase())) {
42
+ (isIdentColor(token) && "currentcolor" == token.val)) {
43
43
  return token;
44
44
  }
45
45
  if (token.kin == ColorType.ALPHA) {
@@ -86,7 +86,7 @@ function convertColor(token, to) {
86
86
  chi: [...(token.val == "color" ? [chi[offset]] : []), ...Object.values(components)],
87
87
  kin: ColorType[token.val.toUpperCase().replaceAll("-", "_")],
88
88
  };
89
- Object.defineProperty(tk, "loc", { ...definedPropertySettings, value: token.loc });
89
+ tk[LOC] = token[LOC];
90
90
  token = tk;
91
91
  }
92
92
  }
@@ -450,6 +450,10 @@ function srgb2srgbcolorspace(val, to) {
450
450
  // @ts-ignore
451
451
  values.push(...srgb2p3values(...val));
452
452
  break;
453
+ case ColorType.DISPLAY_P3_LINEAR:
454
+ // @ts-ignore
455
+ values.push(...srgb2lp3values(...val));
456
+ break;
453
457
  case ColorType.PROPHOTO_RGB:
454
458
  // @ts-ignore
455
459
  values.push(...srgb2prophotorgbvalues(...val));
@@ -484,11 +488,15 @@ function color2srgbvalues(token) {
484
488
  }
485
489
  const colorSpace = components.shift();
486
490
  let values = components.map((val) => getNumber(val));
487
- switch (colorSpace.val.toLowerCase()) {
491
+ switch (colorSpace.val) {
488
492
  case "display-p3":
489
493
  // @ts-ignore
490
494
  values = p32srgbvalues(...values);
491
495
  break;
496
+ case "display-p3-linear":
497
+ // @ts-ignore
498
+ values = lp32srgbvalues(...values);
499
+ break;
492
500
  case "srgb-linear":
493
501
  // @ts-ignore
494
502
  values = lsrgb2srgbvalues(...values);
@@ -599,15 +607,15 @@ function toPrecisionValue(value, precision = colorPrecision) {
599
607
  value = Math.round(value * div) / div;
600
608
  return Math.abs(value) < epsilon ? 0 : value;
601
609
  }
602
- function toPrecisionAngle(angle, precision = colorPrecision) {
610
+ function toPrecisionAngle(angle, precision = colorPrecision, correctValue = true) {
603
611
  angle = toPrecisionValue(angle, precision);
604
- if (Math.abs(angle) >= 360) {
612
+ if (correctValue && Math.abs(angle) >= 360) {
605
613
  angle %= 360;
606
614
  }
607
615
  if (Math.abs(angle) < anglePrecision) {
608
616
  angle = 0;
609
617
  }
610
- if (angle < 0) {
618
+ if (correctValue && angle < 0) {
611
619
  angle += 360;
612
620
  }
613
621
  return angle;
@@ -70,7 +70,7 @@ function color2HexToken(token) {
70
70
  if (value == null) {
71
71
  return null;
72
72
  }
73
- return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr).toString(16).padStart(2, "0"), "#"));
73
+ return hexToken(value.reduce((acc, curr) => acc + srgb2rgb(curr + Number.EPSILON).toString(16).padStart(2, "0"), "#"));
74
74
  }
75
75
  function oklab2HexToken(token) {
76
76
  let value = oklab2srgbvalues(token);
@@ -10,6 +10,14 @@ function srgb2p3values(r, g, b, alpha) {
10
10
  // @ts-ignore
11
11
  return lp32p3(...xyz2lp3(...srgb2xyz(r, g, b, alpha)));
12
12
  }
13
+ function srgb2lp3values(r, g, b, alpha) {
14
+ // @ts-ignore
15
+ return xyz2lp3(...srgb2xyz(r, g, b, alpha));
16
+ }
17
+ function lp32srgbvalues(r, g, b, alpha) {
18
+ // @ts-ignore
19
+ return xyz2srgb(...lp32xyz(r, g, b, alpha));
20
+ }
13
21
  function p32lp3(r, g, b, alpha) {
14
22
  // convert an array of display-p3 RGB values in the range 0.0 - 1.0
15
23
  // to linear light (un-companded) form.
@@ -49,4 +57,4 @@ function xyz2lp3(x, y, z, alpha) {
49
57
  return result;
50
58
  }
51
59
 
52
- export { lp32p3, lp32xyz, p32lp3, p32srgbvalues, srgb2p3values, xyz2lp3 };
60
+ export { lp32p3, lp32srgbvalues, lp32xyz, p32lp3, p32srgbvalues, srgb2lp3values, srgb2p3values, xyz2lp3 };
@@ -7,9 +7,9 @@ import { getColorComponents } from './components.js';
7
7
  * @returns
8
8
  */
9
9
  function getColorSpace(color) {
10
- if (!("chi" in color)) {
11
- return "rgb";
12
- }
10
+ // if (!("chi" in color)) {
11
+ // return "rgb";
12
+ // }
13
13
  let name;
14
14
  const components = getColorComponents(color);
15
15
  if (components == null || components.length < 3) {