@tbela99/css-parser 1.0.0 → 1.1.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 (86) hide show
  1. package/CHANGELOG.md +265 -0
  2. package/README.md +16 -11
  3. package/dist/index-umd-web.js +3613 -1829
  4. package/dist/index.cjs +3611 -1827
  5. package/dist/index.d.ts +160 -50
  6. package/dist/lib/ast/expand.js +2 -1
  7. package/dist/lib/ast/features/calc.js +12 -1
  8. package/dist/lib/ast/features/inlinecssvariables.js +47 -24
  9. package/dist/lib/ast/features/prefix.js +117 -86
  10. package/dist/lib/ast/features/shorthand.js +29 -6
  11. package/dist/lib/ast/features/transform.js +10 -3
  12. package/dist/lib/ast/features/type.js +7 -0
  13. package/dist/lib/ast/math/expression.js +7 -1
  14. package/dist/lib/ast/math/math.js +6 -0
  15. package/dist/lib/ast/minify.js +165 -77
  16. package/dist/lib/ast/transform/compute.js +1 -0
  17. package/dist/lib/ast/transform/matrix.js +1 -0
  18. package/dist/lib/ast/types.js +17 -15
  19. package/dist/lib/ast/walk.js +33 -7
  20. package/dist/lib/fs/resolve.js +10 -0
  21. package/dist/lib/parser/declaration/list.js +48 -45
  22. package/dist/lib/parser/declaration/map.js +1 -0
  23. package/dist/lib/parser/declaration/set.js +2 -1
  24. package/dist/lib/parser/parse.js +364 -276
  25. package/dist/lib/parser/tokenize.js +147 -72
  26. package/dist/lib/parser/utils/declaration.js +4 -3
  27. package/dist/lib/parser/utils/type.js +2 -1
  28. package/dist/lib/renderer/color/a98rgb.js +2 -1
  29. package/dist/lib/renderer/color/color-mix.js +10 -7
  30. package/dist/lib/renderer/color/color.js +171 -153
  31. package/dist/lib/renderer/color/hex.js +2 -1
  32. package/dist/lib/renderer/color/hsl.js +2 -1
  33. package/dist/lib/renderer/color/hwb.js +2 -1
  34. package/dist/lib/renderer/color/lab.js +2 -1
  35. package/dist/lib/renderer/color/lch.js +2 -1
  36. package/dist/lib/renderer/color/oklab.js +2 -1
  37. package/dist/lib/renderer/color/oklch.js +2 -1
  38. package/dist/lib/renderer/color/p3.js +2 -1
  39. package/dist/lib/renderer/color/rec2020.js +2 -1
  40. package/dist/lib/renderer/color/relativecolor.js +17 -11
  41. package/dist/lib/renderer/color/rgb.js +4 -3
  42. package/dist/lib/renderer/color/srgb.js +18 -17
  43. package/dist/lib/renderer/color/utils/components.js +6 -5
  44. package/dist/lib/renderer/color/utils/constants.js +47 -3
  45. package/dist/lib/renderer/color/xyz.js +2 -1
  46. package/dist/lib/renderer/color/xyzd50.js +2 -1
  47. package/dist/lib/renderer/render.js +48 -20
  48. package/dist/lib/syntax/syntax.js +253 -140
  49. package/dist/lib/validation/at-rules/container.js +75 -97
  50. package/dist/lib/validation/at-rules/counter-style.js +9 -8
  51. package/dist/lib/validation/at-rules/custom-media.js +13 -15
  52. package/dist/lib/validation/at-rules/document.js +22 -27
  53. package/dist/lib/validation/at-rules/font-feature-values.js +8 -8
  54. package/dist/lib/validation/at-rules/import.js +30 -81
  55. package/dist/lib/validation/at-rules/keyframes.js +18 -22
  56. package/dist/lib/validation/at-rules/layer.js +5 -5
  57. package/dist/lib/validation/at-rules/media.js +42 -52
  58. package/dist/lib/validation/at-rules/namespace.js +19 -23
  59. package/dist/lib/validation/at-rules/page-margin-box.js +15 -18
  60. package/dist/lib/validation/at-rules/page.js +8 -7
  61. package/dist/lib/validation/at-rules/supports.js +73 -82
  62. package/dist/lib/validation/at-rules/when.js +32 -36
  63. package/dist/lib/validation/atrule.js +15 -14
  64. package/dist/lib/validation/config.js +24 -1
  65. package/dist/lib/validation/config.json.js +563 -63
  66. package/dist/lib/validation/parser/parse.js +196 -185
  67. package/dist/lib/validation/parser/types.js +1 -1
  68. package/dist/lib/validation/selector.js +3 -3
  69. package/dist/lib/validation/syntax.js +828 -0
  70. package/dist/lib/validation/syntaxes/complex-selector-list.js +10 -11
  71. package/dist/lib/validation/syntaxes/complex-selector.js +10 -11
  72. package/dist/lib/validation/syntaxes/compound-selector.js +40 -50
  73. package/dist/lib/validation/syntaxes/family-name.js +9 -8
  74. package/dist/lib/validation/syntaxes/keyframe-block-list.js +4 -3
  75. package/dist/lib/validation/syntaxes/keyframe-selector.js +15 -18
  76. package/dist/lib/validation/syntaxes/layer-name.js +6 -5
  77. package/dist/lib/validation/syntaxes/relative-selector-list.js +7 -6
  78. package/dist/lib/validation/syntaxes/relative-selector.js +2 -1
  79. package/dist/lib/validation/syntaxes/url.js +18 -22
  80. package/dist/lib/validation/utils/list.js +2 -1
  81. package/dist/lib/validation/utils/whitespace.js +2 -1
  82. package/dist/node/index.js +4 -2
  83. package/dist/node/load.js +5 -0
  84. package/dist/web/index.js +4 -2
  85. package/dist/web/load.js +5 -0
  86. package/package.json +12 -11
@@ -2,12 +2,13 @@ import { EnumToken } from '../../ast/types.js';
2
2
  import '../../ast/minify.js';
3
3
  import '../../ast/walk.js';
4
4
  import '../../parser/parse.js';
5
+ import '../../parser/tokenize.js';
6
+ import '../../parser/utils/config.js';
5
7
  import { minmax, getNumber } from './color.js';
6
8
  import { hsl2rgb, hwb2rgb, cmyk2rgb, oklab2rgb, oklch2rgb, lab2rgb, lch2rgb } from './rgb.js';
7
9
  import { NAMES_COLORS } from './utils/constants.js';
8
10
  import { getComponents } from './utils/components.js';
9
11
  import '../sourcemap/lib/encode.js';
10
- import '../../parser/utils/config.js';
11
12
 
12
13
  function toHexString(acc, value) {
13
14
  return acc + value.toString(16).padStart(2, '0');
@@ -7,9 +7,10 @@ import { EnumToken } from '../../ast/types.js';
7
7
  import '../../ast/minify.js';
8
8
  import '../../ast/walk.js';
9
9
  import '../../parser/parse.js';
10
+ import '../../parser/tokenize.js';
11
+ import '../../parser/utils/config.js';
10
12
  import { hslvalues } from './srgb.js';
11
13
  import '../sourcemap/lib/encode.js';
12
- import '../../parser/utils/config.js';
13
14
 
14
15
  function hex2hsl(token) {
15
16
  // @ts-ignore
@@ -6,9 +6,10 @@ import { EnumToken } from '../../ast/types.js';
6
6
  import '../../ast/minify.js';
7
7
  import '../../ast/walk.js';
8
8
  import '../../parser/parse.js';
9
+ import '../../parser/tokenize.js';
10
+ import '../../parser/utils/config.js';
9
11
  import { lch2srgb, lab2srgb, oklch2srgb, oklab2srgb } from './srgb.js';
10
12
  import '../sourcemap/lib/encode.js';
11
- import '../../parser/utils/config.js';
12
13
 
13
14
  function rgb2hwb(token) {
14
15
  // @ts-ignore
@@ -9,9 +9,10 @@ import { EnumToken } from '../../ast/types.js';
9
9
  import '../../ast/minify.js';
10
10
  import '../../ast/walk.js';
11
11
  import '../../parser/parse.js';
12
+ import '../../parser/tokenize.js';
13
+ import '../../parser/utils/config.js';
12
14
  import { xyzd502srgb } from './xyzd50.js';
13
15
  import '../sourcemap/lib/encode.js';
14
- import '../../parser/utils/config.js';
15
16
 
16
17
  // L: 0% = 0.0, 100% = 100.0
17
18
  // for a and b: -100% = -125, 100% = 125
@@ -5,9 +5,10 @@ import { EnumToken } from '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
6
  import '../../ast/walk.js';
7
7
  import '../../parser/parse.js';
8
+ import '../../parser/tokenize.js';
9
+ import '../../parser/utils/config.js';
8
10
  import { srgb2lab, xyz2lab, oklch2lab, oklab2lab, getLABComponents, hwb2lab, hsl2lab, rgb2lab, hex2lab } from './lab.js';
9
11
  import '../sourcemap/lib/encode.js';
10
- import '../../parser/utils/config.js';
11
12
 
12
13
  function hex2lch(token) {
13
14
  // @ts-ignore
@@ -7,10 +7,11 @@ import { EnumToken } from '../../ast/types.js';
7
7
  import '../../ast/minify.js';
8
8
  import '../../ast/walk.js';
9
9
  import '../../parser/parse.js';
10
+ import '../../parser/tokenize.js';
11
+ import '../../parser/utils/config.js';
10
12
  import { lch2labvalues } from './lab.js';
11
13
  import { getOKLCHComponents } from './oklch.js';
12
14
  import '../sourcemap/lib/encode.js';
13
- import '../../parser/utils/config.js';
14
15
 
15
16
  function hex2oklab(token) {
16
17
  // @ts-ignore
@@ -5,10 +5,11 @@ import { EnumToken } from '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
6
  import '../../ast/walk.js';
7
7
  import '../../parser/parse.js';
8
+ import '../../parser/tokenize.js';
9
+ import '../../parser/utils/config.js';
8
10
  import { lab2lchvalues } from './lch.js';
9
11
  import { srgb2oklab, lch2oklab, getOKLABComponents, lab2oklab, hwb2oklab, hsl2oklab, rgb2oklab, hex2oklab } from './oklab.js';
10
12
  import '../sourcemap/lib/encode.js';
11
- import '../../parser/utils/config.js';
12
13
 
13
14
  function hex2oklch(token) {
14
15
  // @ts-ignore
@@ -5,9 +5,10 @@ import '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
6
  import '../../ast/walk.js';
7
7
  import '../../parser/parse.js';
8
+ import '../../parser/tokenize.js';
9
+ import '../../parser/utils/config.js';
8
10
  import { srgb2xyz } from './xyz.js';
9
11
  import '../sourcemap/lib/encode.js';
10
- import '../../parser/utils/config.js';
11
12
 
12
13
  function p32srgbvalues(r, g, b, alpha) {
13
14
  // @ts-ignore
@@ -5,9 +5,10 @@ import '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
6
  import '../../ast/walk.js';
7
7
  import '../../parser/parse.js';
8
+ import '../../parser/tokenize.js';
9
+ import '../../parser/utils/config.js';
8
10
  import { srgb2xyz } from './xyz.js';
9
11
  import '../sourcemap/lib/encode.js';
10
- import '../../parser/utils/config.js';
11
12
 
12
13
  function rec20202srgb(r, g, b, a) {
13
14
  // @ts-ignore
@@ -3,11 +3,12 @@ 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';
6
+ import '../../parser/tokenize.js';
7
7
  import '../../parser/utils/config.js';
8
+ import { mathFuncs } from '../../syntax/syntax.js';
8
9
  import { reduceNumber } from '../render.js';
10
+ import { ColorKind, colorRange } from './utils/constants.js';
9
11
  import { evaluateFunc, evaluate } from '../../ast/math/expression.js';
10
- import { colorRange } from './utils/constants.js';
11
12
 
12
13
  function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
13
14
  let r;
@@ -18,21 +19,23 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
18
19
  let values = {};
19
20
  // colorFuncColorSpace x,y,z or r,g,b
20
21
  const names = relativeKeys.startsWith('xyz') ? 'xyz' : relativeKeys.slice(-3);
21
- // @ts-ignore
22
- const converted = convert(original, relativeKeys);
22
+ const converted = convert(original, ColorKind[relativeKeys.toUpperCase().replaceAll('-', '_')]);
23
23
  if (converted == null) {
24
24
  return null;
25
25
  }
26
26
  const children = converted.chi.filter(t => ![EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType, EnumToken.CommentTokenType].includes(t.typ));
27
- [r, g, b, alpha] = converted.kin == 'color' ? children.slice(1) : children;
27
+ [r, g, b, alpha] = converted.kin == ColorKind.COLOR ? children.slice(1) : children;
28
28
  values = {
29
29
  [names[0]]: getValue(r, converted, names[0]),
30
30
  [names[1]]: getValue(g, converted, names[1]), // string,
31
31
  [names[2]]: getValue(b, converted, names[2]),
32
32
  // @ts-ignore
33
- alpha: alpha == null || (alpha.typ == EnumToken.IdenTokenType && alpha.val == 'none') ? {
33
+ alpha: alpha == null ? {
34
34
  typ: EnumToken.NumberTokenType,
35
35
  val: '1'
36
+ } : (alpha.typ == EnumToken.IdenTokenType && alpha.val == 'none') ? {
37
+ typ: EnumToken.NumberTokenType,
38
+ val: '0'
36
39
  } : (alpha.typ == EnumToken.PercentageTokenType ? {
37
40
  typ: EnumToken.NumberTokenType,
38
41
  val: String(getNumber(alpha))
@@ -43,9 +46,12 @@ function parseRelativeColor(relativeKeys, original, rExp, gExp, bExp, aExp) {
43
46
  [names[1]]: getValue(gExp, converted, names[1]),
44
47
  [names[2]]: getValue(bExp, converted, names[2]),
45
48
  // @ts-ignore
46
- alpha: getValue(aExp == null || (aExp.typ == EnumToken.IdenTokenType && aExp.val == 'none') ? {
49
+ alpha: getValue(aExp == null ? {
47
50
  typ: EnumToken.NumberTokenType,
48
51
  val: '1'
52
+ } : (aExp.typ == EnumToken.IdenTokenType && aExp.val == 'none') ? {
53
+ typ: EnumToken.NumberTokenType,
54
+ val: '0'
49
55
  } : aExp)
50
56
  };
51
57
  return computeComponentValue(keys, converted, values);
@@ -56,9 +62,10 @@ function getValue(t, converted, component) {
56
62
  }
57
63
  if (t.typ == EnumToken.PercentageTokenType) {
58
64
  let value = getNumber(t);
59
- if (converted.kin in colorRange) {
65
+ let colorSpace = ColorKind[converted.kin].toLowerCase().replaceAll('-', '_');
66
+ if (colorSpace in colorRange) {
60
67
  // @ts-ignore
61
- value *= colorRange[converted.kin][component].at(-1);
68
+ value *= colorRange[colorSpace][component].at(-1);
62
69
  }
63
70
  return {
64
71
  typ: EnumToken.NumberTokenType,
@@ -71,7 +78,6 @@ function computeComponentValue(expr, converted, values) {
71
78
  for (const object of [values, expr]) {
72
79
  if ('h' in object) {
73
80
  // normalize hue
74
- // @ts-ignore
75
81
  for (const k of walkValues([object.h])) {
76
82
  if (k.value.typ == EnumToken.AngleTokenType && k.value.unit == 'deg') {
77
83
  // @ts-ignore
@@ -159,4 +165,4 @@ function replaceValue(parent, value, newValue) {
159
165
  }
160
166
  }
161
167
 
162
- export { parseRelativeColor };
168
+ export { parseRelativeColor, replaceValue };
@@ -1,19 +1,20 @@
1
1
  import { minmax } from './color.js';
2
- import { COLORS_NAMES } from './utils/constants.js';
2
+ import { COLORS_NAMES, ColorKind } from './utils/constants.js';
3
3
  import '../../ast/types.js';
4
4
  import '../../ast/minify.js';
5
5
  import '../../ast/walk.js';
6
6
  import '../../parser/parse.js';
7
+ import '../../parser/tokenize.js';
8
+ import '../../parser/utils/config.js';
7
9
  import { expandHexValue } from './hex.js';
8
10
  import { hslvalues, hsl2srgbvalues, hwb2srgb, cmyk2srgb, oklab2srgb, oklch2srgb, lab2srgb, lch2srgb } from './srgb.js';
9
11
  import '../sourcemap/lib/encode.js';
10
- import '../../parser/utils/config.js';
11
12
 
12
13
  function srgb2rgb(value) {
13
14
  return minmax(Math.round(value * 255), 0, 255);
14
15
  }
15
16
  function hex2rgb(token) {
16
- const value = expandHexValue(token.kin == 'lit' ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
17
+ const value = expandHexValue(token.kin == ColorKind.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
17
18
  const rgb = [];
18
19
  for (let i = 1; i < value.length; i += 2) {
19
20
  rgb.push(parseInt(value.slice(i, i + 2), 16));
@@ -1,10 +1,12 @@
1
- import { COLORS_NAMES } from './utils/constants.js';
1
+ import { ColorKind, COLORS_NAMES } from './utils/constants.js';
2
2
  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
6
  import '../../ast/walk.js';
7
7
  import '../../parser/parse.js';
8
+ import '../../parser/tokenize.js';
9
+ import '../../parser/utils/config.js';
8
10
  import { expandHexValue } from './hex.js';
9
11
  import { lch2labvalues, Lab_to_sRGB, getLABComponents } from './lab.js';
10
12
  import { OKLab_to_sRGB, getOKLABComponents } from './oklab.js';
@@ -12,33 +14,32 @@ import { getLCHComponents } from './lch.js';
12
14
  import { getOKLCHComponents } from './oklch.js';
13
15
  import { XYZ_to_lin_sRGB } from './xyz.js';
14
16
  import '../sourcemap/lib/encode.js';
15
- import '../../parser/utils/config.js';
16
17
 
17
18
  // from https://www.w3.org/TR/css-color-4/#color-conversion-code
18
19
  // srgb-linear -> srgb
19
20
  // 0 <= r, g, b <= 1
20
21
  function srgbvalues(token) {
21
22
  switch (token.kin) {
22
- case 'lit':
23
- case 'hex':
23
+ case ColorKind.LIT:
24
+ case ColorKind.HEX:
24
25
  return hex2srgb(token);
25
- case 'rgb':
26
- case 'rgba':
26
+ case ColorKind.RGB:
27
+ case ColorKind.RGBA:
27
28
  return rgb2srgb(token);
28
- case 'hsl':
29
- case 'hsla':
29
+ case ColorKind.HSL:
30
+ case ColorKind.HSLA:
30
31
  return hsl2srgb(token);
31
- case 'hwb':
32
+ case ColorKind.HWB:
32
33
  return hwb2srgb(token);
33
- case 'lab':
34
+ case ColorKind.LAB:
34
35
  return lab2srgb(token);
35
- case 'lch':
36
+ case ColorKind.LCH:
36
37
  return lch2srgb(token);
37
- case 'oklab':
38
+ case ColorKind.OKLAB:
38
39
  return oklab2srgb(token);
39
- case 'oklch':
40
+ case ColorKind.OKLCH:
40
41
  return oklch2srgb(token);
41
- case 'color':
42
+ case ColorKind.COLOR:
42
43
  return color2srgbvalues(token);
43
44
  }
44
45
  return null;
@@ -47,7 +48,7 @@ function rgb2srgb(token) {
47
48
  return getComponents(token)?.map?.((t, index) => index == 3 ? ((t.typ == EnumToken.IdenTokenType && t.val == 'none') ? 1 : getNumber(t)) : (t.typ == EnumToken.PercentageTokenType ? 255 : 1) * getNumber(t) / 255) ?? null;
48
49
  }
49
50
  function hex2srgb(token) {
50
- const value = expandHexValue(token.kin == 'lit' ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
51
+ const value = expandHexValue(token.kin == ColorKind.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
51
52
  const rgb = [];
52
53
  for (let i = 1; i < value.length; i += 2) {
53
54
  rgb.push(parseInt(value.slice(i, i + 2), 16) / 255);
@@ -155,9 +156,9 @@ function hslvalues(token) {
155
156
  // @ts-ignore
156
157
  let l = getNumber(t);
157
158
  let a = null;
158
- if (token.chi?.length == 4) {
159
+ if (components.length == 4) {
159
160
  // @ts-ignore
160
- t = token.chi[3];
161
+ t = components[3];
161
162
  // @ts-ignore
162
163
  a = getNumber(t);
163
164
  }
@@ -2,14 +2,15 @@ import { EnumToken } from '../../../ast/types.js';
2
2
  import '../../../ast/minify.js';
3
3
  import '../../../ast/walk.js';
4
4
  import '../../../parser/parse.js';
5
- import { COLORS_NAMES } from './constants.js';
5
+ import '../../../parser/tokenize.js';
6
+ import '../../../parser/utils/config.js';
7
+ import { ColorKind, COLORS_NAMES } from './constants.js';
6
8
  import { expandHexValue } from '../hex.js';
7
9
  import '../../sourcemap/lib/encode.js';
8
- import '../../../parser/utils/config.js';
9
10
 
10
11
  function getComponents(token) {
11
- if (token.kin == 'hex' || token.kin == 'lit') {
12
- const value = expandHexValue(token.kin == 'lit' ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
12
+ if (token.kin == ColorKind.HEX || token.kin == ColorKind.LIT) {
13
+ const value = expandHexValue(token.kin == ColorKind.LIT ? COLORS_NAMES[token.val.toLowerCase()] : token.val);
13
14
  // @ts-ignore
14
15
  return value.slice(1).match(/([a-fA-F0-9]{2})/g).map((t) => {
15
16
  return { typ: EnumToken.Number, val: parseInt(t, 16).toString() };
@@ -22,7 +23,7 @@ function getComponents(token) {
22
23
  ].includes(child.typ)) {
23
24
  continue;
24
25
  }
25
- if (child.typ == EnumToken.ColorTokenType && child.val == 'currentcolor') {
26
+ if (child.typ == EnumToken.ColorTokenType && child.val.localeCompare('currentcolor', undefined, { sensitivity: 'base' }) == 0) {
26
27
  return null;
27
28
  }
28
29
  result.push(child);
@@ -2,8 +2,9 @@ import { EnumToken } from '../../../ast/types.js';
2
2
  import '../../../ast/minify.js';
3
3
  import '../../../ast/walk.js';
4
4
  import '../../../parser/parse.js';
5
- import '../../sourcemap/lib/encode.js';
5
+ import '../../../parser/tokenize.js';
6
6
  import '../../../parser/utils/config.js';
7
+ import '../../sourcemap/lib/encode.js';
7
8
 
8
9
  const colorRange = {
9
10
  lab: {
@@ -27,8 +28,51 @@ const colorRange = {
27
28
  b: [0, 0.4]
28
29
  }
29
30
  };
31
+ // xyz-d65 is an alias for xyz
32
+ // display-p3 is an alias for srgb
33
+ var ColorKind;
34
+ (function (ColorKind) {
35
+ ColorKind[ColorKind["SYS"] = 0] = "SYS";
36
+ ColorKind[ColorKind["DPSYS"] = 1] = "DPSYS";
37
+ ColorKind[ColorKind["LIT"] = 2] = "LIT";
38
+ ColorKind[ColorKind["HEX"] = 3] = "HEX";
39
+ ColorKind[ColorKind["RGB"] = 4] = "RGB";
40
+ ColorKind[ColorKind["RGBA"] = 5] = "RGBA";
41
+ ColorKind[ColorKind["HSL"] = 6] = "HSL";
42
+ ColorKind[ColorKind["HSLA"] = 7] = "HSLA";
43
+ ColorKind[ColorKind["HWB"] = 8] = "HWB";
44
+ ColorKind[ColorKind["DEVICE_CMYK"] = 9] = "DEVICE_CMYK";
45
+ ColorKind[ColorKind["OKLAB"] = 10] = "OKLAB";
46
+ ColorKind[ColorKind["OKLCH"] = 11] = "OKLCH";
47
+ ColorKind[ColorKind["LAB"] = 12] = "LAB";
48
+ ColorKind[ColorKind["LCH"] = 13] = "LCH";
49
+ ColorKind[ColorKind["COLOR"] = 14] = "COLOR";
50
+ ColorKind[ColorKind["SRGB"] = 15] = "SRGB";
51
+ ColorKind[ColorKind["PROPHOTO_RGB"] = 16] = "PROPHOTO_RGB";
52
+ ColorKind[ColorKind["A98_RGB"] = 17] = "A98_RGB";
53
+ ColorKind[ColorKind["REC2020"] = 18] = "REC2020";
54
+ ColorKind[ColorKind["DISPLAY_P3"] = 19] = "DISPLAY_P3";
55
+ ColorKind[ColorKind["SRGB_LINEAR"] = 20] = "SRGB_LINEAR";
56
+ ColorKind[ColorKind["XYZ"] = 21] = "XYZ";
57
+ ColorKind[ColorKind["XYZ_D50"] = 22] = "XYZ_D50";
58
+ ColorKind[ColorKind["XYZ_D65"] = 23] = "XYZ_D65";
59
+ ColorKind[ColorKind["LIGHT_DARK"] = 24] = "LIGHT_DARK";
60
+ ColorKind[ColorKind["COLOR_MIX"] = 25] = "COLOR_MIX";
61
+ })(ColorKind || (ColorKind = {}));
62
+ const generalEnclosedFunc = ['selector', 'font-tech', 'font-format', 'media', 'supports'];
63
+ const funcLike = [
64
+ EnumToken.ParensTokenType,
65
+ EnumToken.FunctionTokenType,
66
+ EnumToken.UrlFunctionTokenType,
67
+ EnumToken.StartParensTokenType,
68
+ EnumToken.ImageFunctionTokenType,
69
+ EnumToken.TimingFunctionTokenType,
70
+ EnumToken.TimingFunctionTokenType,
71
+ EnumToken.PseudoClassFuncTokenType,
72
+ EnumToken.GridTemplateFuncTokenType
73
+ ];
74
+ const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk', 'color-mix', 'color', 'oklab', 'lab', 'oklch', 'lch', 'light-dark'];
30
75
  const colorFuncColorSpace = ['srgb', 'srgb-linear', 'display-p3', 'prophoto-rgb', 'a98-rgb', 'rec2020', 'xyz', 'xyz-d65', 'xyz-d50'];
31
- ({ typ: EnumToken.IdenTokenType});
32
76
  const D50 = [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585];
33
77
  const k = Math.pow(29, 3) / Math.pow(3, 3);
34
78
  const e = Math.pow(6, 3) / Math.pow(29, 3);
@@ -194,4 +238,4 @@ const NAMES_COLORS = Object.seal(Object.entries(COLORS_NAMES).reduce((acc, [key,
194
238
  return acc;
195
239
  }, Object.create(null)));
196
240
 
197
- export { COLORS_NAMES, D50, NAMES_COLORS, colorFuncColorSpace, colorRange, deprecatedSystemColors, e, k, systemColors };
241
+ export { COLORS_NAMES, ColorKind, D50, NAMES_COLORS, colorFuncColorSpace, colorRange, colorsFunc, deprecatedSystemColors, e, funcLike, generalEnclosedFunc, k, systemColors };
@@ -4,9 +4,10 @@ import '../../ast/types.js';
4
4
  import '../../ast/minify.js';
5
5
  import '../../ast/walk.js';
6
6
  import '../../parser/parse.js';
7
+ import '../../parser/tokenize.js';
8
+ import '../../parser/utils/config.js';
7
9
  import { srgb2lsrgbvalues } from './srgb.js';
8
10
  import '../sourcemap/lib/encode.js';
9
- import '../../parser/utils/config.js';
10
11
 
11
12
  function XYZ_to_lin_sRGB(x, y, z) {
12
13
  // convert XYZ to linear-light sRGB
@@ -5,11 +5,12 @@ import '../../ast/types.js';
5
5
  import '../../ast/minify.js';
6
6
  import '../../ast/walk.js';
7
7
  import '../../parser/parse.js';
8
+ import '../../parser/tokenize.js';
9
+ import '../../parser/utils/config.js';
8
10
  import { xyz2lab } from './lab.js';
9
11
  import { lab2lchvalues } from './lch.js';
10
12
  import { XYZ_D50_to_D65 } from './xyz.js';
11
13
  import '../sourcemap/lib/encode.js';
12
- import '../../parser/utils/config.js';
13
14
 
14
15
  /*
15
16
  */
@@ -1,8 +1,8 @@
1
1
  import { getAngle, color2srgbvalues, clamp } from './color/color.js';
2
- import { colorFuncColorSpace, COLORS_NAMES } from './color/utils/constants.js';
2
+ import { colorsFunc, ColorKind, colorFuncColorSpace, COLORS_NAMES, funcLike } from './color/utils/constants.js';
3
3
  import { getComponents } from './color/utils/components.js';
4
4
  import { reduceHexValue, srgb2hexvalues, rgb2hex, hsl2hex, hwb2hex, cmyk2hex, oklab2hex, oklch2hex, lab2hex, lch2hex } from './color/hex.js';
5
- import { EnumToken, funcLike } from '../ast/types.js';
5
+ import { EnumToken } from '../ast/types.js';
6
6
  import '../ast/minify.js';
7
7
  import '../ast/walk.js';
8
8
  import { expand } from '../ast/expand.js';
@@ -11,7 +11,6 @@ import { parseRelativeColor } from './color/relativecolor.js';
11
11
  import { SourceMap } from './sourcemap/sourcemap.js';
12
12
  import { isColor, pseudoElements, mathFuncs, isNewLine } from '../syntax/syntax.js';
13
13
 
14
- const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk', 'color-mix', 'color', 'oklab', 'lab', 'oklch', 'lch', 'light-dark'];
15
14
  function reduceNumber(val) {
16
15
  val = String(+val);
17
16
  if (val === '0') {
@@ -59,7 +58,8 @@ function doRender(data, options = {}) {
59
58
  }),
60
59
  ...(minify ? {
61
60
  removeEmpty: true,
62
- removeComments: true
61
+ removeComments: true,
62
+ minify: true
63
63
  } : {
64
64
  removeEmpty: false,
65
65
  removeComments: false,
@@ -105,11 +105,17 @@ function doRender(data, options = {}) {
105
105
  }
106
106
  if (sourcemap != null) {
107
107
  result.map = sourcemap;
108
+ if (options.sourcemap === 'inline') {
109
+ result.code += `\n/*# sourceMappingURL=data:application/json,${encodeURIComponent(JSON.stringify(result.map))} */`;
110
+ }
108
111
  }
109
112
  return result;
110
113
  }
111
114
  function updateSourceMap(node, options, cache, sourcemap, position, str) {
112
- if ([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType, EnumToken.KeyFrameRuleNodeType, EnumToken.KeyframeAtRuleNodeType].includes(node.typ)) {
115
+ if ([
116
+ EnumToken.RuleNodeType, EnumToken.AtRuleNodeType,
117
+ EnumToken.KeyFrameRuleNodeType, EnumToken.KeyframeAtRuleNodeType
118
+ ].includes(node.typ)) {
113
119
  let src = node.loc?.src ?? '';
114
120
  let output = options.output ?? '';
115
121
  if (!(src in cache)) {
@@ -227,6 +233,7 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
227
233
  return `@${data.nam}${data.val === '' ? '' : options.indent || ' '}${data.val}${options.indent}{${options.newLine}` + (children === '' ? '' : indentSub + children + options.newLine) + indent + `}`;
228
234
  }
229
235
  return data.sel + `${options.indent}{${options.newLine}` + (children === '' ? '' : indentSub + children + options.newLine) + indent + `}`;
236
+ case EnumToken.InvalidDeclarationNodeType:
230
237
  case EnumToken.InvalidRuleTokenType:
231
238
  case EnumToken.InvalidAtRuleTokenType:
232
239
  return '';
@@ -324,7 +331,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
324
331
  case EnumToken.Div:
325
332
  return '/';
326
333
  case EnumToken.ColorTokenType:
327
- if (token.kin == 'light-dark') {
334
+ if (token.kin == ColorKind.LIGHT_DARK || ('chi' in token && !options.convertColor)) {
328
335
  return token.val + '(' + token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), '') + ')';
329
336
  }
330
337
  if (options.convertColor) {
@@ -334,7 +341,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
334
341
  acc.push([t]);
335
342
  }
336
343
  else {
337
- if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ)) {
344
+ if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(t.typ)) {
338
345
  acc[acc.length - 1].push(t);
339
346
  }
340
347
  }
@@ -389,23 +396,45 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
389
396
  }
390
397
  return clamp(token).val + '(' + (slice ? token.chi.slice(0, -2) : token.chi).reduce((acc, curr) => {
391
398
  const val = renderToken(curr, options, cache);
392
- if ([EnumToken.LiteralTokenType, EnumToken.CommaTokenType].includes(curr.typ)) {
393
- return acc + val;
399
+ if (curr.typ == EnumToken.LiteralTokenType && curr.val == '/') {
400
+ return acc.trimEnd() + '/';
401
+ }
402
+ if (curr.typ == EnumToken.CommaTokenType) {
403
+ return acc.trimEnd() + ',';
404
+ }
405
+ if (curr.typ == EnumToken.WhitespaceTokenType) {
406
+ const v = acc.at(-1);
407
+ if (v == ' ' || v == ',' || v == '/') {
408
+ return acc.trimEnd();
409
+ }
410
+ return acc.trimEnd() + ' ';
394
411
  }
395
412
  if (acc.length > 0) {
396
- return acc + (['/', ','].includes(acc.at(-1)) ? '' : ' ') + val;
413
+ return acc + (['/', ',', ' '].includes(acc.at(-1)) ? '' : ' ') + val;
397
414
  }
398
415
  return val;
399
416
  }, '') + ')';
400
417
  }
401
- if (token.kin == 'lit' && token.val.localeCompare('currentcolor', undefined, { sensitivity: 'base' }) == 0) {
418
+ if (token.kin == ColorKind.LIT && token.val.localeCompare('currentcolor', undefined, { sensitivity: 'base' }) == 0) {
402
419
  return 'currentcolor';
403
420
  }
404
421
  clamp(token);
405
422
  if (Array.isArray(token.chi) && token.chi.some((t) => t.typ == EnumToken.FunctionTokenType || (t.typ == EnumToken.ColorTokenType && Array.isArray(t.chi)))) {
406
- return (token.val.endsWith('a') ? token.val.slice(0, -1) : token.val) + '(' + token.chi.reduce((acc, curr) => acc + (acc.length > 0 && !(acc.endsWith('/') || curr.typ == EnumToken.LiteralTokenType) ? ' ' : '') + renderToken(curr, options, cache), '') + ')';
423
+ const replaceSemiColon = /^((rgba?)|(hsla?)|(hwb)|((ok)?lab)|((ok)?lch))$/i.test(token.val);
424
+ return (token.val.endsWith('a') ? token.val.slice(0, -1) : token.val) + '(' + token.chi.reduce((acc, curr, index, array) => {
425
+ if (curr.typ == EnumToken.Literal && curr.val == '/') {
426
+ return acc.trimEnd() + '/';
427
+ }
428
+ if (curr.typ == EnumToken.CommaTokenType) {
429
+ return acc.trimEnd() + (replaceSemiColon ? ' ' : ',');
430
+ }
431
+ if (curr.typ == EnumToken.WhitespaceTokenType) {
432
+ return /[,\/\s]/.test(acc.at(-1)) ? acc.trimEnd() : acc.trimEnd() + ' ';
433
+ }
434
+ return acc + renderToken(curr, options, cache);
435
+ }, '') + ')';
407
436
  }
408
- let value = token.kin == 'hex' ? token.val.toLowerCase() : (token.kin == 'lit' ? COLORS_NAMES[token.val.toLowerCase()] : '');
437
+ let value = token.kin == ColorKind.HEX ? token.val.toLowerCase() : (token.kin == ColorKind.LIT ? COLORS_NAMES[token.val.toLowerCase()] : '');
409
438
  if (token.val == 'rgb' || token.val == 'rgba') {
410
439
  value = rgb2hex(token);
411
440
  }
@@ -434,7 +463,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
434
463
  return reduceHexValue(value);
435
464
  }
436
465
  }
437
- if (['hex', 'lit', 'sys', 'dpsys'].includes(token.kin)) {
466
+ if ([ColorKind.HEX, ColorKind.LIT, ColorKind.SYS, ColorKind.DPSYS].includes(token.kin)) {
438
467
  return token.val;
439
468
  }
440
469
  if (Array.isArray(token.chi)) {
@@ -454,9 +483,8 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
454
483
  ![EnumToken.BinaryExpressionTokenType, EnumToken.FractionTokenType, EnumToken.IdenTokenType].includes(token.chi[0].typ) &&
455
484
  // @ts-ignore
456
485
  token.chi[0].val?.typ != EnumToken.FractionTokenType) {
457
- return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer), '');
486
+ return token.val + '(' + token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer), '') + ')';
458
487
  }
459
- // @ts-ignore
460
488
  return ( /* options.minify && 'Pseudo-class-func' == token.typ && token.val.slice(0, 2) == '::' ? token.val.slice(1) :*/token.val ?? '') + '(' + token.chi.reduce(reducer, '') + ')';
461
489
  case EnumToken.MatchExpressionTokenType:
462
490
  return renderToken(token.l, options, cache, reducer, errors) +
@@ -596,6 +624,9 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
596
624
  }
597
625
  return val + 's';
598
626
  }
627
+ if (token.typ == EnumToken.ResolutionTokenType && unit == 'dppx') {
628
+ unit = 'x';
629
+ }
599
630
  return val.includes('/') ? val.replace('/', unit + '/') : val + unit;
600
631
  case EnumToken.FlexTokenType:
601
632
  case EnumToken.PercentageTokenType:
@@ -670,9 +701,6 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
670
701
  return 'and';
671
702
  case EnumToken.MediaFeatureOrTokenType:
672
703
  return 'or';
673
- // default:
674
- //
675
- // throw new Error(`render: unexpected token ${JSON.stringify(token, null, 1)}`);
676
704
  }
677
705
  errors?.push({ action: 'ignore', message: `render: unexpected token ${JSON.stringify(token, null, 1)}` });
678
706
  return '';
@@ -690,4 +718,4 @@ function filterValues(values) {
690
718
  return values;
691
719
  }
692
720
 
693
- export { colorsFunc, doRender, filterValues, reduceNumber, renderToken };
721
+ export { doRender, filterValues, reduceNumber, renderToken };