@tbela99/css-parser 1.4.2 → 1.4.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 (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -1,30 +1,14 @@
1
- import { getAngle, convertColor } from '../syntax/color/color.js';
2
- import { colorsFunc, funcLike } from '../syntax/color/utils/constants.js';
3
- import { EnumToken, ColorType } from '../ast/types.js';
4
- import '../ast/minify.js';
5
- import '../ast/walk.js';
1
+ import { convertColor, getAngle } from '../syntax/color/color.js';
2
+ import { reduceHexValue } from '../syntax/color/hex.js';
3
+ import { EnumToken, minifyNumber, ColorType } from '../ast/types.js';
6
4
  import { expand } from '../ast/expand.js';
7
- import { isColor, pseudoElements, mathFuncs, isNewLine } from '../syntax/syntax.js';
8
- import { minifyNumber } from '../syntax/utils.js';
9
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';
8
+ import { move } from '../parser/tokenize.js';
9
+ import { equalsIgnoreCase } from '../parser/utils/text.js';
10
+ import { toDegrees } from '../parser/utils/angle.js';
10
11
 
11
- /**
12
- * Update position
13
- * @param position
14
- * @param str
15
- */
16
- function update(position, str) {
17
- let i = 0;
18
- for (; i < str.length; i++) {
19
- if (isNewLine(str[i].charCodeAt(0))) {
20
- position.lin++;
21
- position.col = 0;
22
- }
23
- else {
24
- position.col++;
25
- }
26
- }
27
- }
28
12
  /**
29
13
  * render ast
30
14
  * @param data
@@ -36,21 +20,30 @@ function doRender(data, options = {}, mapping) {
36
20
  const minify = options.minify ?? true;
37
21
  const beautify = options.beautify ?? !minify;
38
22
  options = {
39
- ...(beautify ? {
40
- indent: ' ',
41
- newLine: '\n',
42
- } : {
43
- indent: '',
44
- newLine: '',
45
- }),
46
- ...(minify ? {
47
- removeEmpty: true,
48
- removeComments: true,
49
- minify: true
50
- } : {
51
- removeEmpty: false,
52
- removeComments: false,
53
- }), sourcemap: false, convertColor: true, expandNestingRules: false, preserveLicense: false, ...options
23
+ ...(beautify
24
+ ? {
25
+ indent: " ",
26
+ newLine: "\n",
27
+ }
28
+ : {
29
+ indent: "",
30
+ newLine: "",
31
+ }),
32
+ ...(minify
33
+ ? {
34
+ removeEmpty: true,
35
+ removeComments: true,
36
+ minify: true,
37
+ }
38
+ : {
39
+ removeEmpty: false,
40
+ removeComments: false,
41
+ }),
42
+ sourcemap: false,
43
+ convertColor: true,
44
+ expandNestingRules: false,
45
+ preserveLicense: false,
46
+ ...options,
54
47
  };
55
48
  if (options.withParents) {
56
49
  // @ts-ignore
@@ -67,35 +60,42 @@ function doRender(data, options = {}, mapping) {
67
60
  }
68
61
  const startTime = performance.now();
69
62
  const errors = [];
70
- const sourcemap = options.sourcemap ? new SourceMap : null;
63
+ const sourcemap = options.sourcemap ? new SourceMap() : null;
71
64
  const cache = Object.create(null);
72
65
  const position = {
73
66
  ind: 0,
74
67
  lin: 1,
75
- col: 1
68
+ col: 1,
76
69
  };
77
- let code = '';
70
+ let code = "";
78
71
  if (mapping != null) {
79
72
  if (mapping.importMapping != null) {
80
73
  for (const [key, value] of Object.entries(mapping.importMapping)) {
81
- code += `:import("${key}")${options.indent}{${options.newLine}${Object.entries(value).reduce((acc, [k, v]) => acc + (acc.length > 0 ? options.newLine : '') + `${options.indent}${v}:${options.indent}${k};`, '')}${options.newLine}}${options.newLine}`;
74
+ code += `:import("${key}")${options.indent}{${options.newLine}${Object.entries(value).reduce((acc, [k, v]) => acc + (acc.length > 0 ? options.newLine : "") + `${options.indent}${v}:${options.indent}${k};`, "")}${options.newLine}}${options.newLine}`;
82
75
  }
83
76
  }
84
- code += `:export${options.indent}{${options.newLine}${Object.entries(mapping.mapping).reduce((acc, [k, v]) => acc + (acc.length > 0 ? options.newLine : '') + `${options.indent}${k}:${options.indent}${v};`, '')}${options.newLine}}${options.newLine}`;
85
- update(position, code);
77
+ code += `:export${options.indent}{${options.newLine}${Object.entries(mapping.mapping).reduce((acc, [k, v]) => acc + (acc.length > 0 ? options.newLine : "") + `${options.indent}${k}:${options.indent}${v};`, "")}${options.newLine}}${options.newLine}`;
78
+ move(position, code);
86
79
  }
87
80
  const result = {
88
- code: code + renderAstNode(options.expandNestingRules && [EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(data.typ) && 'chi' in data ? expand(data) : data, options, sourcemap, position, errors, function reducer(acc, curr) {
89
- if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
90
- if (!options.preserveLicense || !curr.val.startsWith('/*!')) {
91
- return acc;
81
+ code: code +
82
+ renderAstNode(options.expandNestingRules &&
83
+ [EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(data.typ) &&
84
+ "chi" in data
85
+ ? expand(data)
86
+ : data, options, sourcemap, position, errors, function reducer(acc, curr) {
87
+ if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
88
+ if (!options.preserveLicense || !curr.val.startsWith("/*!")) {
89
+ return acc;
90
+ }
91
+ return acc + curr.val;
92
92
  }
93
- return acc + curr.val;
94
- }
95
- return acc + renderToken(curr, options, cache, reducer, errors);
96
- }, cache), errors, stats: {
97
- total: `${(performance.now() - startTime).toFixed(2)}ms`
98
- }
93
+ return acc + renderValue(curr, options, cache, reducer, errors);
94
+ }, cache),
95
+ errors,
96
+ stats: {
97
+ total: `${(performance.now() - startTime).toFixed(2)}ms`,
98
+ },
99
99
  };
100
100
  if (options.output != null) {
101
101
  // @ts-ignore
@@ -103,7 +103,7 @@ function doRender(data, options = {}, mapping) {
103
103
  }
104
104
  if (sourcemap != null) {
105
105
  result.map = sourcemap;
106
- if (options.sourcemap === 'inline') {
106
+ if (options.sourcemap === "inline") {
107
107
  result.code += `\n/*# sourceMappingURL=data:application/json,${encodeURIComponent(JSON.stringify(result.map))} */`;
108
108
  }
109
109
  }
@@ -122,27 +122,21 @@ function doRender(data, options = {}, mapping) {
122
122
  */
123
123
  function updateSourceMap(node, options, cache, sourcemap, position, str) {
124
124
  if ([
125
- EnumToken.RuleNodeType, EnumToken.AtRuleNodeType,
126
- EnumToken.KeyFramesRuleNodeType, EnumToken.KeyframesAtRuleNodeType
125
+ EnumToken.RuleNodeType,
126
+ EnumToken.AtRuleNodeType,
127
+ EnumToken.KeyFramesRuleNodeType,
128
+ EnumToken.KeyframesAtRuleNodeType,
127
129
  ].includes(node.typ)) {
128
- let src = node.loc?.src ?? '';
129
- let output = options.output ?? '';
130
- if (!(src in cache)) {
131
- // @ts-ignore
132
- cache[src] = options.resolve(src, options.cwd ?? '').relative;
133
- }
134
- if (!(output in cache)) {
135
- // @ts-ignore
136
- cache[output] = options.resolve(output, options.cwd).relative;
137
- }
130
+ let src = node.loc?.src ?? "";
131
+ sourcemap.add(
138
132
  // @ts-ignore
139
- sourcemap.add({ src: cache[output], sta: { ...position } }, {
133
+ { src, sta: { ...position } }, {
140
134
  ...node.loc,
141
135
  // @ts-ignore
142
- src: options.resolve(cache[src], options.cwd).relative
136
+ src,
143
137
  });
144
138
  }
145
- update(position, str);
139
+ move(position, str);
146
140
  }
147
141
  /**
148
142
  * render ast node
@@ -169,162 +163,225 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
169
163
  const indentSub = indents[level + 1];
170
164
  switch (data.typ) {
171
165
  case EnumToken.DeclarationNodeType:
172
- return `${data.nam}:${options.indent}${(options.minify ? filterValues(data.val) : data.val).reduce(reducer, '')}`;
166
+ return `${data.nam}:${options.indent}${(options.minify
167
+ ? filterValues(data.val)
168
+ : data.val).reduce(reducer, "")}`;
173
169
  case EnumToken.CommentNodeType:
174
170
  case EnumToken.CDOCOMMNodeType:
175
- if (data.val.startsWith('/*# sourceMappingURL=')) {
171
+ if (data.val.startsWith("/*# sourceMappingURL=")) {
176
172
  // ignore sourcemap
177
- return '';
173
+ return "";
178
174
  }
179
- return !options.removeComments || (options.preserveLicense && data.val.startsWith('/*!')) ? data.val : '';
175
+ return !options.removeComments || (options.preserveLicense && data.val.startsWith("/*!"))
176
+ ? data.val
177
+ : "";
180
178
  case EnumToken.StyleSheetNodeType:
181
179
  return data.chi.reduce((css, node) => {
182
180
  const str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level, indents);
183
- if (str === '') {
181
+ if (str === "") {
184
182
  return css;
185
183
  }
186
- if (css === '') {
184
+ if (css === "") {
187
185
  if (sourcemap != null && node.loc != null) {
188
186
  updateSourceMap(node, options, cache, sourcemap, position, str);
189
187
  }
190
188
  return str;
191
189
  }
192
190
  if (sourcemap != null && node.loc != null) {
193
- update(position, options.newLine);
191
+ move(position, options.newLine);
194
192
  updateSourceMap(node, options, cache, sourcemap, position, str);
195
193
  }
196
194
  return `${css}${options.newLine}${str}`;
197
- }, '');
195
+ }, "");
198
196
  case EnumToken.AtRuleNodeType:
199
197
  case EnumToken.RuleNodeType:
200
198
  case EnumToken.KeyFramesRuleNodeType:
201
199
  case EnumToken.KeyframesAtRuleNodeType:
202
- if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !('chi' in data)) {
203
- return `${indent}@${data.nam}${data.val === '' ? '' : options.indent || ' '}${data.val};`;
200
+ if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !("chi" in data)) {
201
+ return `${indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`;
204
202
  }
205
203
  // @ts-ignore
206
204
  let children = data.chi.reduce((css, node) => {
207
205
  let str;
208
206
  if (node.typ == EnumToken.CommentNodeType) {
209
- str = options.removeComments && (!options.preserveLicense || !node.val.startsWith('/*!')) ? '' : node.val;
207
+ str =
208
+ options.removeComments &&
209
+ (!options.preserveLicense || !node.val.startsWith("/*!"))
210
+ ? ""
211
+ : node.val;
210
212
  }
211
213
  else if (node.typ == EnumToken.DeclarationNodeType) {
212
- if (!node.nam.startsWith('--') && node.val.length == 0) {
214
+ if (!node.nam.startsWith("--") && node.val.length === 0) {
213
215
  // @ts-ignore
214
216
  errors.push({
215
- action: 'ignore',
217
+ action: "ignore",
216
218
  message: `render: invalid declaration ${JSON.stringify(node)}`,
217
- location: node.loc
219
+ location: node.loc,
218
220
  });
219
- return '';
221
+ return "";
220
222
  }
221
- str = `${node.nam}:${options.indent}${(options.minify ? filterValues(node.val) : node.val).reduce(reducer, '').trimEnd()};`;
223
+ str = `${node.nam}:${options.indent}${(options.minify
224
+ ? filterValues(node.val)
225
+ : node.val)
226
+ .reduce(reducer, "")
227
+ .trimEnd()};`;
222
228
  }
223
- else if (node.typ == EnumToken.AtRuleNodeType && !('chi' in node)) {
224
- str = `${node.val === '' ? '' : options.indent || ' '}${node.val};`;
229
+ else if (node.typ == EnumToken.AtRuleNodeType && !("chi" in node)) {
230
+ str = `${node.val === "" ? "" : options.indent || " "}${node.val};`;
225
231
  }
226
232
  else {
227
233
  str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level + 1, indents);
228
234
  }
229
- if (css === '') {
235
+ if (css === "") {
230
236
  return str;
231
237
  }
232
- if (str === '') {
238
+ if (str === "") {
233
239
  return css;
234
240
  }
235
241
  return `${css}${options.newLine}${indentSub}${str}`;
236
- }, '');
237
- if (options.removeEmpty && children === '') {
238
- return '';
242
+ }, "");
243
+ if (options.removeEmpty && children === "") {
244
+ return "";
239
245
  }
240
- if (children.endsWith(';')) {
246
+ if (children.endsWith(";")) {
241
247
  children = children.slice(0, -1);
242
248
  }
243
249
  if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ)) {
244
- return `@${data.nam}${data.val === '' ? '' : options.indent || ' '}${data.val}${options.indent}{${options.newLine}` + (children === '' ? '' : indentSub + children + options.newLine) + indent + `}`;
250
+ return (`@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val}${options.indent}{${options.newLine}` +
251
+ (children === "" ? "" : indentSub + children + options.newLine) +
252
+ indent +
253
+ `}`);
245
254
  }
246
- return data.sel + `${options.indent}{${options.newLine}` + (children === '' ? '' : indentSub + children + options.newLine) + indent + `}`;
255
+ return (data.sel +
256
+ `${options.indent}{${options.newLine}` +
257
+ (children === "" ? "" : indentSub + children + options.newLine) +
258
+ indent +
259
+ `}`);
247
260
  case EnumToken.CssVariableTokenType:
248
261
  case EnumToken.CssVariableImportTokenType:
249
- return `@value ${data.nam}:${options.indent}${filterValues((options.minify ? data.val : data.val)).reduce(reducer, '').trim()};`;
262
+ return `@value ${data.val}:${options.indent}${filterValues(options.minify
263
+ ? data.val
264
+ : data.val)
265
+ .reduce(reducer, "")
266
+ .trim()};`;
250
267
  case EnumToken.CssVariableDeclarationMapTokenType:
251
- return `@value ${filterValues(data.vars).reduce((acc, curr) => acc + renderToken(curr), '').trim()} from ${filterValues(data.from).reduce((acc, curr) => acc + renderToken(curr), '').trim()};`;
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()};`;
252
273
  case EnumToken.InvalidDeclarationNodeType:
253
- case EnumToken.InvalidRuleTokenType:
254
- case EnumToken.InvalidAtRuleTokenType:
274
+ case EnumToken.InvalidRuleNodeType:
275
+ case EnumToken.InvalidAtRuleNodeType:
255
276
  default:
256
- return '';
277
+ return "";
257
278
  }
258
279
  }
259
280
  /**
260
281
  * render ast token
261
282
  * @param token
262
283
  * @param options
284
+ * @private
263
285
  */
264
- function renderToken(token, options = {}, cache = Object.create(null), reducer, errors) {
286
+ function renderValue(token, options = {}, cache = Object.create(null), reducer, errors) {
287
+ if (token.typ === EnumToken.WhenElseFunctionTokenType &&
288
+ equalsIgnoreCase(token.val, "supports")) {
289
+ options = { ...options, minify: false, convertColor: false };
290
+ reducer = null;
291
+ }
265
292
  if (reducer == null) {
266
293
  reducer = function (acc, curr) {
267
294
  if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
268
- if (!options.preserveLicense || !curr.val.startsWith('/*!')) {
295
+ if (!options.preserveLicense || !curr.val.startsWith("/*!")) {
269
296
  return acc;
270
297
  }
271
298
  return acc + curr.val;
272
299
  }
273
- return acc + renderToken(curr, options, cache, reducer, errors);
300
+ return acc + renderValue(curr, options, cache, reducer, errors);
274
301
  };
275
302
  }
276
303
  if (token.typ == EnumToken.FunctionTokenType && colorsFunc.includes(token.val)) {
277
304
  if (isColor(token)) {
278
305
  // @ts-ignore
279
306
  token.typ = EnumToken.ColorTokenType;
307
+ if (
280
308
  // @ts-ignore
281
- if (token.chi[0].typ == EnumToken.IdenTokenType && token.chi[0].val == 'from') {
309
+ token.chi[0].typ == EnumToken.IdenTokenType &&
282
310
  // @ts-ignore
283
- token.cal = 'rel';
311
+ token.chi[0].val == "from") {
312
+ // @ts-ignore
313
+ token.cal = "rel";
284
314
  }
285
- else { // @ts-ignore
286
- if (token.val == 'color-mix' && token.chi[0].typ == EnumToken.IdenTokenType && token.chi[0].val == 'in') {
315
+ else {
316
+ // @ts-ignore
317
+ if (token.val == "color-mix" &&
318
+ token.chi?.[0]?.typ == EnumToken.IdenTokenType &&
319
+ (token.chi?.[0]).val == "in") {
287
320
  // @ts-ignore
288
- token.cal = 'mix';
321
+ token.cal = "mix";
289
322
  }
290
323
  else {
291
324
  // @ts-ignore
292
- if (token.val == 'color') {
325
+ if (token.val == "color") {
293
326
  // @ts-ignore
294
- token.cal = 'col';
327
+ token.cal = "col";
295
328
  }
296
329
  // @ts-ignore
297
- token.chi = token.chi.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommaTokenType, EnumToken.CommentTokenType].includes(t.typ));
330
+ token.chi = token.chi.filter((t) => ![
331
+ EnumToken.WhitespaceTokenType,
332
+ EnumToken.CommaTokenType,
333
+ EnumToken.CommentTokenType,
334
+ ].includes(t.typ));
298
335
  }
299
336
  }
300
337
  }
301
338
  }
302
339
  switch (token.typ) {
340
+ case EnumToken.FunctionTokenDefType:
341
+ case EnumToken.UrlFunctionTokenDefType:
342
+ case EnumToken.MathFunctionTokenDefType:
343
+ case EnumToken.ImageFunctionTokenDefType:
344
+ case EnumToken.ColorFunctionTokenDefType:
345
+ case EnumToken.TimingFunctionTokenDefType:
346
+ case EnumToken.WhenElseFunctionTokenDefType:
347
+ case EnumToken.SupportsFunctionTokenDefType:
348
+ case EnumToken.TimelineFunctionTokenDefType:
349
+ case EnumToken.GridTemplateFuncTokenDefType:
350
+ case EnumToken.TransformFunctionTokenDefType:
351
+ case EnumToken.ContainerFunctionTokenDefType:
352
+ case EnumToken.PseudoClassFunctionTokenDefType:
353
+ case EnumToken.GeneralEnclosedFunctionTokenDefType:
354
+ case EnumToken.CustomFunctionTokenDefType:
355
+ case EnumToken.WildCardFunctionTokenDefType:
356
+ return token.val + "(";
303
357
  case EnumToken.ListToken:
304
- return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), '');
358
+ return token.chi.reduce((acc, curr) => acc + renderValue(curr, options, cache), "");
305
359
  case EnumToken.BinaryExpressionTokenType:
306
- if ([EnumToken.Mul, EnumToken.Div].includes(token.op)) {
307
- let result = '';
360
+ if (EnumToken.Mul === token.op ||
361
+ EnumToken.Div === token.op) {
362
+ let result = "";
308
363
  if (token.l.typ == EnumToken.BinaryExpressionTokenType &&
309
364
  [EnumToken.Add, EnumToken.Sub].includes(token.l.op)) {
310
- result = '(' + renderToken(token.l, options, cache) + ')';
365
+ result = "(" + renderValue(token.l, options, cache) + ")";
311
366
  }
312
367
  else {
313
- result = renderToken(token.l, options, cache);
368
+ result = renderValue(token.l, options, cache);
314
369
  }
315
- result += token.op == EnumToken.Mul ? '*' : '/';
370
+ result += token.op == EnumToken.Mul ? "*" : "/";
316
371
  if (token.r.typ == EnumToken.BinaryExpressionTokenType &&
317
372
  [EnumToken.Add, EnumToken.Sub].includes(token.r.op)) {
318
- result += '(' + renderToken(token.r, options, cache) + ')';
373
+ result += "(" + renderValue(token.r, options, cache) + ")";
319
374
  }
320
375
  else {
321
- result += renderToken(token.r, options, cache);
376
+ result += renderValue(token.r, options, cache);
322
377
  }
323
378
  return result;
324
379
  }
325
- return renderToken(token.l, options, cache) + (token.op == EnumToken.Add ? ' + ' : (token.op == EnumToken.Sub ? ' - ' : (token.op == EnumToken.Mul ? '*' : '/'))) + renderToken(token.r, options, cache);
380
+ return (renderValue(token.l, options, cache) +
381
+ (token.op == EnumToken.Add ? " + " : " - ") +
382
+ renderValue(token.r, options, cache));
326
383
  case EnumToken.FractionTokenType:
327
- const fraction = renderToken(token.l) + '/' + renderToken(token.r);
384
+ const fraction = renderValue(token.l) + "/" + renderValue(token.r);
328
385
  if (+token.r.val != 0) {
329
386
  const value = minifyNumber(+token.l.val / +token.r.val);
330
387
  if (value.length <= fraction.length) {
@@ -333,150 +390,661 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
333
390
  }
334
391
  return fraction;
335
392
  case EnumToken.Add:
336
- return ' + ';
393
+ return " + ";
337
394
  case EnumToken.Sub:
338
- return ' - ';
395
+ return " - ";
396
+ case EnumToken.Star:
339
397
  case EnumToken.UniversalSelectorTokenType:
340
398
  case EnumToken.Mul:
341
- return '*';
399
+ return "*";
342
400
  case EnumToken.Div:
343
- return '/';
401
+ return "/";
344
402
  case EnumToken.ColorTokenType:
345
- if (token.kin == ColorType.LIGHT_DARK || ('chi' in token && options.convertColor === false)) {
346
- return token.val + '(' + token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), '') + ')';
403
+ if (token.kin == ColorType.LIGHT_DARK ||
404
+ ("chi" in token && options.convertColor === false && token.chi.length == 2)) {
405
+ return (token.val +
406
+ "(" +
407
+ token.chi.reduce((acc, curr) => {
408
+ if (curr.typ === EnumToken.IdenTokenType) {
409
+ parseColor(curr);
410
+ }
411
+ return acc + renderValue(curr, options, cache);
412
+ }, "") +
413
+ ")");
347
414
  }
348
415
  if (options.convertColor !== false) {
349
- const value = convertColor(token, typeof options.convertColor == 'boolean' ? ColorType.HEX : ColorType[ColorType[options.convertColor ?? 'HEX']?.toUpperCase?.().replaceAll?.('-', '_')] ?? ColorType.HEX);
350
- //
416
+ const value = convertColor(token, typeof options.convertColor === "boolean"
417
+ ? ColorType.HEX
418
+ : (ColorType[ColorType[options.convertColor ?? "HEX"]
419
+ ?.toUpperCase?.()
420
+ .replaceAll?.("-", "_")] ?? ColorType.HEX));
351
421
  if (value != null) {
352
422
  token = value;
353
423
  }
424
+ if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
425
+ return reduceHexValue(token.val);
426
+ }
354
427
  }
355
- if ([ColorType.HEX, ColorType.LIT, ColorType.SYS, ColorType.DPSYS].includes(token.kin)) {
428
+ if (token.kin === ColorType.SYS ||
429
+ token.kin === ColorType.DPSYS ||
430
+ token.kin === ColorType.NON_STD) {
431
+ return token.val;
432
+ }
433
+ if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
356
434
  return token.val;
357
435
  }
358
436
  if (Array.isArray(token.chi)) {
359
- const isLegacy = ['rgb', 'rgba', 'hsl', 'hsla'].includes(token.val.toLowerCase());
360
- const useAlpha = (['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'oklab', 'oklch', 'lab', 'lch'].includes(token.val.toLowerCase()) && token.chi.length == 4) ||
361
- ('color' == token.val.toLowerCase() && token.chi.length == 5);
362
- return (token.val.endsWith('a') ? token.val.slice(0, -1) : token.val) + '(' + token.chi.reduce((acc, curr, index, array) => {
363
- if (/[,/]\s*$/.test(acc)) {
437
+ const fnName = token.val.toLowerCase();
438
+ const isLegacy = ["rgb", "rgba", "hsl", "hsla"].includes(token.val.toLowerCase());
439
+ const hasAlpha = [
440
+ "rgb",
441
+ "rgba",
442
+ "hsl",
443
+ "hsla",
444
+ "hwb",
445
+ "oklab",
446
+ "oklch",
447
+ "lab",
448
+ "lch",
449
+ ].includes(fnName);
450
+ const useAlpha = (hasAlpha && token.chi.length == (isLegacy ? 7 : 4)) ||
451
+ ("color" == token.val && token.chi.length == 5);
452
+ return ((hasAlpha && token.val.endsWith("a") ? fnName.slice(0, -1) : fnName) +
453
+ "(" +
454
+ token
455
+ .chi.reduce((acc, curr, index, array) => {
456
+ if (/[,/]\s*$/.test(acc)) {
457
+ if (curr.typ == EnumToken.WhitespaceTokenType) {
458
+ return acc.trimEnd();
459
+ }
460
+ return acc.trimStart() + renderValue(curr, options, cache);
461
+ }
462
+ if (isLegacy && curr.typ == EnumToken.CommaTokenType) {
463
+ return acc.trimEnd() + " ";
464
+ }
364
465
  if (curr.typ == EnumToken.WhitespaceTokenType) {
365
- return acc.trimEnd();
466
+ return acc.trimEnd() + " ";
366
467
  }
367
- return acc.trimStart() + renderToken(curr, options, cache);
368
- }
369
- if (isLegacy && curr.typ == EnumToken.CommaTokenType) {
370
- return acc.trimEnd() + ' ';
371
- }
372
- if (curr.typ == EnumToken.WhitespaceTokenType) {
373
- return acc.trimEnd() + ' ';
374
- }
375
- if (curr.typ == EnumToken.CommaTokenType || (curr.typ == EnumToken.LiteralTokenType && curr.val == '/')) {
376
- return acc.trimEnd() + (curr.typ == EnumToken.CommaTokenType ? ',' : '/');
468
+ if (curr.typ == EnumToken.CommaTokenType ||
469
+ (curr.typ == EnumToken.LiteralTokenType && curr.val == "/")) {
470
+ return acc.trimEnd() + (curr.typ == EnumToken.CommaTokenType ? "," : "/");
471
+ }
472
+ return (acc.trimEnd() +
473
+ (useAlpha && index == array.length - 1 ? "/" : " ") +
474
+ renderValue(curr, options, cache));
475
+ }, "")
476
+ .trimStart() +
477
+ ")");
478
+ }
479
+ case EnumToken.UrlFunctionTokenType:
480
+ if (options.minify && token.typ === EnumToken.UrlFunctionTokenType) {
481
+ for (const child of token.chi) {
482
+ if (child.typ === EnumToken.StringTokenType) {
483
+ if (child.val.slice(1, 5) !== "data:" &&
484
+ urlTokenMatcher.test(child.val)) {
485
+ Object.assign(child, {
486
+ typ: EnumToken.UrlTokenTokenType,
487
+ val: child.val.slice(1, -1),
488
+ });
489
+ }
490
+ break;
377
491
  }
378
- return acc.trimEnd() + (useAlpha && index == array.length - 1 ? '/' : ' ') + renderToken(curr, options, cache);
379
- }, '').trimStart() + ')';
492
+ }
380
493
  }
381
494
  case EnumToken.ParensTokenType:
382
495
  case EnumToken.FunctionTokenType:
383
- case EnumToken.UrlFunctionTokenType:
496
+ case EnumToken.MathFunctionTokenType:
384
497
  case EnumToken.ImageFunctionTokenType:
498
+ if (options.minify && token.typ === EnumToken.ImageFunctionTokenType) {
499
+ const slice = token.chi.slice();
500
+ switch (token.val) {
501
+ case "linear-gradient":
502
+ case "repeating-linear-gradient":
503
+ {
504
+ let i;
505
+ let k;
506
+ let j;
507
+ let key;
508
+ for (i = 0; i < slice.length; i++) {
509
+ if (slice[i].typ == EnumToken.ColorTokenType ||
510
+ slice[i].typ === EnumToken.CommaTokenType) {
511
+ break;
512
+ }
513
+ if (slice[i].typ == EnumToken.IdenTokenType &&
514
+ equalsIgnoreCase(slice[i].val, "to")) {
515
+ k = i + 1;
516
+ while (k < slice.length &&
517
+ (slice[k].typ === EnumToken.WhitespaceTokenType ||
518
+ slice[k].typ === EnumToken.CommentTokenType)) {
519
+ k++;
520
+ }
521
+ if (k < slice.length && slice[k].typ === EnumToken.IdenTokenType) {
522
+ // convert keyword to angle
523
+ key = slice[k].val.toLowerCase();
524
+ switch (key) {
525
+ case "right":
526
+ slice.splice(i, k - i + 1, {
527
+ typ: EnumToken.AngleTokenType,
528
+ val: 90,
529
+ });
530
+ break;
531
+ case "left":
532
+ slice.splice(i, k - i + 1, {
533
+ typ: EnumToken.AngleTokenType,
534
+ val: 270,
535
+ });
536
+ break;
537
+ case "top":
538
+ case "bottom":
539
+ j = k + 1;
540
+ while (j < slice.length &&
541
+ (slice[j].typ === EnumToken.WhitespaceTokenType ||
542
+ slice[j].typ === EnumToken.CommentTokenType)) {
543
+ j++;
544
+ }
545
+ if (j < slice.length && slice[j].typ !== EnumToken.CommaTokenType) {
546
+ i = j - 1;
547
+ break;
548
+ }
549
+ // https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/gradient/linear-gradient#:~:text=If%20unspecified%2C%20it%20defaults%20to%20to%20bottom
550
+ if (key === "bottom") {
551
+ // to bottom -> 180deg is the default angle and will be removed
552
+ while (k < slice.length &&
553
+ slice[k].typ != EnumToken.CommaTokenType) {
554
+ k++;
555
+ }
556
+ slice.splice(i, k - i + 1);
557
+ i--;
558
+ continue;
559
+ }
560
+ slice.splice(i, k - i + 1, {
561
+ typ: EnumToken.AngleTokenType,
562
+ val: key === "top" ? 0 : 180,
563
+ });
564
+ i--;
565
+ break;
566
+ }
567
+ }
568
+ }
569
+ else if (slice[i].typ == EnumToken.AngleTokenType) {
570
+ if ((180 - toDegrees(slice[i]).val) % 360 === 0) {
571
+ k = i + 1;
572
+ while (k < slice.length && slice[k].typ !== EnumToken.CommaTokenType) {
573
+ k++;
574
+ }
575
+ slice.splice(i, k - i + 1);
576
+ }
577
+ }
578
+ }
579
+ if (slice[i]?.typ === EnumToken.CommaTokenType) {
580
+ i++;
581
+ while (i < slice.length &&
582
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
583
+ slice[i].typ === EnumToken.CommentTokenType)) {
584
+ i++;
585
+ }
586
+ }
587
+ if (slice[i]?.typ === EnumToken.ColorTokenType) {
588
+ slice.push(...reduceColorStops(slice.splice(i, slice.length - i)));
589
+ }
590
+ }
591
+ break;
592
+ case "radial-gradient":
593
+ case "repeating-radial-gradient":
594
+ {
595
+ let i = 0;
596
+ while (i < slice.length &&
597
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
598
+ slice[i].typ === EnumToken.CommentTokenType)) {
599
+ i++;
600
+ }
601
+ const positions = [];
602
+ const form = [];
603
+ const size = [];
604
+ const colorSpaceDef = [];
605
+ if (slice[i]?.typ === EnumToken.IdenTokenType) {
606
+ if (equalsIgnoreCase(slice[i].val, "farthest-corner") ||
607
+ equalsIgnoreCase(slice[i].val, "ellipse")) {
608
+ i++;
609
+ }
610
+ else if (equalsIgnoreCase(slice[i].val, "closest-side") ||
611
+ equalsIgnoreCase(slice[i].val, "closest-corner") ||
612
+ equalsIgnoreCase(slice[i].val, "farthest-side")) {
613
+ size.push(slice[i++]);
614
+ }
615
+ else if (equalsIgnoreCase(slice[i].val, "circle")) {
616
+ form.push(slice[i++]);
617
+ }
618
+ while (slice[i]?.typ === EnumToken.WhitespaceTokenType ||
619
+ slice[i]?.typ === EnumToken.CommentTokenType) {
620
+ i++;
621
+ }
622
+ }
623
+ if (slice[i]?.typ === EnumToken.IdenTokenType) {
624
+ if (equalsIgnoreCase(slice[i].val, "farthest-corner") ||
625
+ equalsIgnoreCase(slice[i].val, "ellipse")) {
626
+ i++;
627
+ }
628
+ else if (equalsIgnoreCase(slice[i].val, "closest-side") ||
629
+ equalsIgnoreCase(slice[i].val, "closest-corner") ||
630
+ equalsIgnoreCase(slice[i].val, "farthest-side")) {
631
+ size.push(slice[i++]);
632
+ }
633
+ else if (equalsIgnoreCase(slice[i].val, "circle")) {
634
+ form.push(slice[i++]);
635
+ }
636
+ while (slice[i]?.typ === EnumToken.WhitespaceTokenType ||
637
+ slice[i]?.typ === EnumToken.CommentTokenType) {
638
+ i++;
639
+ }
640
+ }
641
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
642
+ equalsIgnoreCase(slice[i].val, "at")) {
643
+ i++;
644
+ }
645
+ while (slice[i]?.typ === EnumToken.WhitespaceTokenType ||
646
+ slice[i]?.typ === EnumToken.CommentTokenType) {
647
+ i++;
648
+ }
649
+ if (slice[i]?.typ === EnumToken.PercentageTokenType ||
650
+ (slice[i]?.typ === EnumToken.NumberTokenType && 0 === slice[i].val) ||
651
+ (slice[i]?.typ === EnumToken.IdenTokenType &&
652
+ (equalsIgnoreCase(slice[i].val, "center") ||
653
+ equalsIgnoreCase(slice[i].val, "top") ||
654
+ equalsIgnoreCase(slice[i].val, "bottom") ||
655
+ equalsIgnoreCase(slice[i].val, "left") ||
656
+ equalsIgnoreCase(slice[i].val, "right")))) {
657
+ do {
658
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
659
+ "in" === slice[i].val) {
660
+ break;
661
+ }
662
+ positions.push(slice[i]);
663
+ i++;
664
+ } while (slice[i]?.typ !== EnumToken.CommaTokenType);
665
+ }
666
+ while (slice[i]?.typ !== EnumToken.CommaTokenType) {
667
+ colorSpaceDef.push(slice[i++]);
668
+ }
669
+ if (slice[i]?.typ === EnumToken.CommaTokenType) {
670
+ i++;
671
+ }
672
+ if (positions.length > 0) {
673
+ let k;
674
+ let position1 = "";
675
+ let position2 = "";
676
+ for (k = 0; k < positions.length; k++) {
677
+ if (positions[k].typ === EnumToken.IdenTokenType) {
678
+ if (position1.length === 0) {
679
+ position1 = positions[k].val;
680
+ }
681
+ else {
682
+ position2 = positions[k].val;
683
+ }
684
+ }
685
+ else if (positions[k].typ === EnumToken.PercentageTokenType ||
686
+ positions[k].typ === EnumToken.NumberTokenType) {
687
+ if (position1.length === 0) {
688
+ position1 = positions[k].val + "%";
689
+ }
690
+ else {
691
+ position2 = positions[k].val + "%";
692
+ }
693
+ }
694
+ if (position2.length > 0) {
695
+ break;
696
+ }
697
+ }
698
+ if (position1.length > 0) {
699
+ let key = `${position1} ${position2}`.trim();
700
+ if (key === "50% 50%" ||
701
+ key === "center center" ||
702
+ key === "50%" ||
703
+ key === "center") {
704
+ positions.length = 0;
705
+ }
706
+ else if (key === "0% 0%" ||
707
+ key === "0 0" ||
708
+ key === "top left" ||
709
+ key === "left top") {
710
+ positions.splice(0, positions.length, {
711
+ typ: EnumToken.PercentageTokenType,
712
+ val: 0,
713
+ });
714
+ }
715
+ else if (key === "50% 0%" ||
716
+ key === "50% 0" ||
717
+ key === "center top" ||
718
+ key === "top center") {
719
+ positions.splice(0, positions.length, {
720
+ typ: EnumToken.IdenTokenType,
721
+ val: "top",
722
+ });
723
+ }
724
+ else if (key === "top right" || key === "right top") {
725
+ positions.splice(0, positions.length, {
726
+ typ: EnumToken.PercentageTokenType,
727
+ val: 100,
728
+ }, { typ: EnumToken.WhitespaceTokenType }, { typ: EnumToken.PercentageTokenType, val: 0 });
729
+ }
730
+ else if (key === "0 50%" ||
731
+ key === "0% 50%" ||
732
+ key === "left center" ||
733
+ key === "center left") {
734
+ positions.splice(0, positions.length, {
735
+ typ: EnumToken.IdenTokenType,
736
+ val: "left",
737
+ });
738
+ }
739
+ else if (key === "100% 50%" || key === "right center" || key === "center right") {
740
+ positions.splice(0, positions.length, {
741
+ typ: EnumToken.IdenTokenType,
742
+ val: "right",
743
+ });
744
+ }
745
+ else if (key === "0 100%" ||
746
+ key === "0% 100%" ||
747
+ key === "left bottom" ||
748
+ key === "bottom left") {
749
+ positions.splice(0, positions.length, {
750
+ typ: EnumToken.IdenTokenType,
751
+ val: "bottom",
752
+ });
753
+ }
754
+ else if (key === "50% 100%" ||
755
+ key === "center bottom" ||
756
+ key === "bottom center") {
757
+ positions.splice(0, positions.length, {
758
+ typ: EnumToken.IdenTokenType,
759
+ val: "bottom",
760
+ });
761
+ }
762
+ else if (key === "100% 100%" ||
763
+ key === "right bottom" ||
764
+ key === "bottom right") {
765
+ positions.splice(0, positions.length, {
766
+ typ: EnumToken.PercentageTokenType,
767
+ val: 100,
768
+ });
769
+ }
770
+ else if (position1 === position2) {
771
+ positions.splice(0, positions.length, positions[k]);
772
+ }
773
+ }
774
+ }
775
+ const result = [];
776
+ if (form.length > 0) {
777
+ result.push(...form);
778
+ }
779
+ if (size.length > 0) {
780
+ if (result.length > 0) {
781
+ result.push({ typ: EnumToken.WhitespaceTokenType });
782
+ }
783
+ result.push(...size);
784
+ }
785
+ if (positions.length > 0) {
786
+ if (result.length > 0) {
787
+ result.push({ typ: EnumToken.WhitespaceTokenType });
788
+ }
789
+ result.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }, ...positions);
790
+ }
791
+ if (colorSpaceDef.length > 0) {
792
+ if (result.length > 0) {
793
+ result.push({ typ: EnumToken.WhitespaceTokenType });
794
+ }
795
+ result.push(...colorSpaceDef);
796
+ }
797
+ if (result.length > 0) {
798
+ result.push({ typ: EnumToken.CommaTokenType });
799
+ }
800
+ result.push(...reduceColorStops(slice.slice(i)));
801
+ slice.length = 0;
802
+ slice.push(...result);
803
+ }
804
+ break;
805
+ case "conic-gradient":
806
+ case "repeating-conic-gradient":
807
+ {
808
+ let i = 0;
809
+ const angles = [];
810
+ const positions = [];
811
+ const colorSpaceDef = [];
812
+ while (i < slice.length &&
813
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
814
+ slice[i].typ === EnumToken.CommentTokenType)) {
815
+ i++;
816
+ }
817
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
818
+ equalsIgnoreCase(slice[i].val, "from")) {
819
+ angles.push(slice[i++]);
820
+ while (i < slice.length &&
821
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
822
+ slice[i].typ === EnumToken.CommentTokenType)) {
823
+ angles.push(slice[i++]);
824
+ }
825
+ if ((slice[i]?.typ === EnumToken.NumberTokenType ||
826
+ slice[i]?.typ === EnumToken.AngleTokenType) &&
827
+ 0 === toDegrees(slice[i]).val) {
828
+ angles.length = 0;
829
+ i++;
830
+ }
831
+ else if (slice[i]?.typ !== EnumToken.CommaTokenType &&
832
+ slice[i].typ != EnumToken.IdenTokenType) {
833
+ angles.push(slice[i++]);
834
+ }
835
+ while (i < slice.length &&
836
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
837
+ slice[i].typ === EnumToken.CommentTokenType)) {
838
+ i++;
839
+ }
840
+ }
841
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
842
+ equalsIgnoreCase(slice[i].val, "at")) {
843
+ i++;
844
+ while (i < slice.length &&
845
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
846
+ slice[i].typ === EnumToken.CommentTokenType)) {
847
+ i++;
848
+ }
849
+ let position1 = "";
850
+ let position2 = "";
851
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
852
+ !equalsIgnoreCase("in", slice[i].val)) {
853
+ position1 = slice[i].val;
854
+ positions.push(slice[i++]);
855
+ }
856
+ else if (slice[i]?.typ === EnumToken.PercentageTokenType ||
857
+ slice[i]?.typ === EnumToken.NumberTokenType) {
858
+ position1 = slice[i].val + "%";
859
+ positions.push(slice[i++]);
860
+ }
861
+ while (i < slice.length &&
862
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
863
+ slice[i].typ === EnumToken.CommentTokenType)) {
864
+ positions.push(slice[i++]);
865
+ }
866
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
867
+ !equalsIgnoreCase("in", slice[i].val)) {
868
+ position2 = slice[i].val;
869
+ positions.push(slice[i++]);
870
+ }
871
+ else if (slice[i]?.typ === EnumToken.PercentageTokenType ||
872
+ slice[i]?.typ === EnumToken.NumberTokenType) {
873
+ position2 = slice[i].val + "%";
874
+ positions.push(slice[i++]);
875
+ }
876
+ while (i < slice.length &&
877
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
878
+ slice[i].typ === EnumToken.CommentTokenType)) {
879
+ i++;
880
+ }
881
+ if (position1.length > 0) {
882
+ reducegradientBackgroundPosition(positions, `${position1} ${position2}`.trim());
883
+ }
884
+ }
885
+ while (i < slice.length &&
886
+ (slice[i].typ === EnumToken.WhitespaceTokenType ||
887
+ slice[i].typ === EnumToken.CommentTokenType)) {
888
+ i++;
889
+ }
890
+ if (slice[i]?.typ === EnumToken.IdenTokenType &&
891
+ equalsIgnoreCase("in", slice[i].val)) {
892
+ while (i < slice.length && slice[i].typ !== EnumToken.CommaTokenType) {
893
+ colorSpaceDef.push(slice[i++]);
894
+ }
895
+ }
896
+ if (slice[i]?.typ === EnumToken.CommaTokenType) {
897
+ i++;
898
+ }
899
+ const result = [];
900
+ if (angles.length > 0) {
901
+ result.push(...angles, { typ: EnumToken.CommaTokenType });
902
+ }
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
+ }
907
+ if (colorSpaceDef.length > 0) {
908
+ if (result.length > 0) {
909
+ result.push({ typ: EnumToken.WhitespaceTokenType });
910
+ }
911
+ result.push(...colorSpaceDef);
912
+ }
913
+ result.push({ typ: EnumToken.CommaTokenType });
914
+ }
915
+ result.push(...reduceConicColorStops(slice.slice(i)));
916
+ slice.length = 0;
917
+ slice.push(...result);
918
+ }
919
+ break;
920
+ }
921
+ return token.val + "(" + slice.reduce(reducer, "") + ")";
922
+ }
385
923
  case EnumToken.TimingFunctionTokenType:
386
924
  case EnumToken.PseudoClassFuncTokenType:
925
+ case EnumToken.WhenElseFunctionTokenType:
387
926
  case EnumToken.TimelineFunctionTokenType:
388
927
  case EnumToken.GridTemplateFuncTokenType:
389
- if (token.typ == EnumToken.FunctionTokenType &&
390
- mathFuncs.includes(token.val) &&
928
+ case EnumToken.SupportsFunctionTokenType:
929
+ case EnumToken.ContainerFunctionTokenType:
930
+ case EnumToken.TransformFunctionTokenType:
931
+ case EnumToken.GeneralEnclosedFunctionTokenType:
932
+ case EnumToken.CustomFunctionTokenType:
933
+ case EnumToken.WildCardFunctionTokenType:
934
+ if (token.typ == EnumToken.MathFunctionTokenType &&
391
935
  token.chi.length == 1 &&
392
936
  ![EnumToken.BinaryExpressionTokenType, EnumToken.FractionTokenType, EnumToken.IdenTokenType].includes(token.chi[0].typ) &&
393
937
  // @ts-ignore
394
- token.chi[0].val?.typ != EnumToken.FractionTokenType) {
395
- return token.val + '(' + token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache, reducer), '') + ')';
938
+ token.chi[0].val
939
+ ?.typ != EnumToken.FractionTokenType) {
940
+ return (token.val +
941
+ "(" +
942
+ token.chi.reduce((acc, curr) => acc +
943
+ renderValue(curr, token.typ == EnumToken.FunctionTokenType ? { minify: false } : options, cache, reducer), "") +
944
+ ")");
396
945
  }
397
- return ( /* options.minify && 'Pseudo-class-func' == token.typ && token.val.slice(0, 2) == '::' ? token.val.slice(1) :*/token.val ?? '') + '(' + token.chi.reduce(reducer, '') + ')';
946
+ return (
947
+ /* options.minify && 'Pseudo-class-func' == token.typ && token.val.slice(0, 2) == '::' ? token.val.slice(1) :*/ (token.val ?? "") +
948
+ "(" +
949
+ token.chi.reduce(reducer, "") +
950
+ ")");
398
951
  case EnumToken.MatchExpressionTokenType:
399
- return renderToken(token.l, options, cache, reducer, errors) +
400
- renderToken(token.op, options, cache, reducer, errors) +
401
- renderToken(token.r, options, cache, reducer, errors) +
402
- (token.attr ? ' ' + token.attr : '');
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 : ""));
403
956
  case EnumToken.NameSpaceAttributeTokenType:
404
- return (token.l == null ? '' : renderToken(token.l, options, cache, reducer, errors)) + '|' +
405
- renderToken(token.r, options, cache, reducer, errors);
957
+ return ((token.l == null
958
+ ? ""
959
+ : renderValue(token.l, options, cache, reducer, errors)) +
960
+ "|" +
961
+ renderValue(token.r, options, cache, reducer, errors));
406
962
  case EnumToken.ComposesSelectorNodeType:
407
- return token.l.reduce((acc, curr) => acc + renderToken(curr, options, cache), '') + (token.r == null ? '' : ' from ' + renderToken(token.r, options, cache, reducer, errors));
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)));
408
968
  case EnumToken.BlockStartTokenType:
409
- return '{';
969
+ return "{";
410
970
  case EnumToken.BlockEndTokenType:
411
- return '}';
971
+ return "}";
412
972
  case EnumToken.StartParensTokenType:
413
- return '(';
973
+ return "(";
414
974
  case EnumToken.DelimTokenType:
415
975
  case EnumToken.EqualMatchTokenType:
416
- return '=';
976
+ return "=";
417
977
  case EnumToken.IncludeMatchTokenType:
418
- return '~=';
978
+ return "~=";
419
979
  case EnumToken.DashMatchTokenType:
420
- return '|=';
980
+ return "|=";
421
981
  case EnumToken.StartMatchTokenType:
422
- return '^=';
982
+ return "^=";
423
983
  case EnumToken.EndMatchTokenType:
424
- return '$=';
984
+ return "$=";
425
985
  case EnumToken.ContainMatchTokenType:
426
- return '*=';
986
+ return "*=";
427
987
  case EnumToken.LtTokenType:
428
- return '<';
988
+ return "<";
429
989
  case EnumToken.LteTokenType:
430
- return '<=';
990
+ return "<=";
991
+ case EnumToken.Tilda:
431
992
  case EnumToken.SubsequentSiblingCombinatorTokenType:
432
- return '~';
993
+ return "~";
994
+ case EnumToken.Plus:
433
995
  case EnumToken.NextSiblingCombinatorTokenType:
434
- return '+';
996
+ return "+";
435
997
  case EnumToken.GtTokenType:
436
998
  case EnumToken.ChildCombinatorTokenType:
437
- return '>';
999
+ return ">";
438
1000
  case EnumToken.GteTokenType:
439
- return '>=';
1001
+ return ">=";
440
1002
  case EnumToken.ColumnCombinatorTokenType:
441
- return '||';
1003
+ return "||";
442
1004
  case EnumToken.EndParensTokenType:
443
- return ')';
1005
+ return ")";
444
1006
  case EnumToken.AttrStartTokenType:
445
- return '[';
1007
+ return "[";
446
1008
  case EnumToken.AttrEndTokenType:
447
- return ']';
1009
+ return "]";
448
1010
  case EnumToken.DescendantCombinatorTokenType:
449
1011
  case EnumToken.WhitespaceTokenType:
450
- return ' ';
1012
+ return " ";
451
1013
  case EnumToken.ColonTokenType:
452
- return ':';
1014
+ return ":";
1015
+ case EnumToken.DoubleColonTokenType:
1016
+ return "::";
453
1017
  case EnumToken.SemiColonTokenType:
454
- return ';';
1018
+ return ";";
455
1019
  case EnumToken.CommaTokenType:
456
- return ',';
1020
+ return ",";
457
1021
  case EnumToken.ImportantTokenType:
458
- return '!important';
1022
+ return "!important";
1023
+ case EnumToken.Pipe:
1024
+ return "|";
459
1025
  case EnumToken.AttrTokenType:
460
1026
  case EnumToken.IdenListTokenType:
461
- return '[' + token.chi.reduce(reducer, '') + ']';
1027
+ return "[" + token.chi.reduce(reducer, "") + "]";
462
1028
  case EnumToken.TimeTokenType:
463
1029
  case EnumToken.AngleTokenType:
464
1030
  case EnumToken.LengthTokenType:
465
1031
  case EnumToken.DimensionTokenType:
466
1032
  case EnumToken.FrequencyTokenType:
467
1033
  case EnumToken.ResolutionTokenType:
468
- let val = token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : minifyNumber(token.val);
1034
+ let val = token.val.typ == EnumToken.FractionTokenType
1035
+ ? renderValue(token.val, options, cache)
1036
+ : minifyNumber(token.val);
469
1037
  let unit = token.unit;
470
- if (token.typ == EnumToken.AngleTokenType && !val.includes('/')) {
1038
+ if (token.typ == EnumToken.AngleTokenType && !val.includes("/")) {
471
1039
  const angle = getAngle(token);
472
1040
  let v;
473
1041
  let value = val + unit;
474
- for (const u of ['turn', 'deg', 'rad', 'grad']) {
1042
+ for (const u of ["turn", "deg", "rad", "grad"]) {
475
1043
  if (token.unit == u) {
476
1044
  continue;
477
1045
  }
478
1046
  switch (u) {
479
- case 'turn':
1047
+ case "turn":
480
1048
  v = minifyNumber(angle);
481
1049
  if (v.length + 4 < value.length) {
482
1050
  val = v;
@@ -484,7 +1052,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
484
1052
  value = v + u;
485
1053
  }
486
1054
  break;
487
- case 'deg':
1055
+ case "deg":
488
1056
  v = minifyNumber(angle * 360);
489
1057
  if (v.length + 3 < value.length) {
490
1058
  val = v;
@@ -492,7 +1060,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
492
1060
  value = v + u;
493
1061
  }
494
1062
  break;
495
- case 'rad':
1063
+ case "rad":
496
1064
  v = minifyNumber(angle * (2 * Math.PI));
497
1065
  if (v.length + 3 < value.length) {
498
1066
  val = v;
@@ -500,7 +1068,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
500
1068
  value = v + u;
501
1069
  }
502
1070
  break;
503
- case 'grad':
1071
+ case "grad":
504
1072
  v = minifyNumber(angle * 400);
505
1073
  if (v.length + 4 < value.length) {
506
1074
  val = v;
@@ -511,119 +1079,149 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
511
1079
  }
512
1080
  }
513
1081
  }
514
- if (val === '0') {
1082
+ if (val === "0") {
515
1083
  if (token.typ == EnumToken.TimeTokenType) {
516
- return '0s';
1084
+ return "0s";
517
1085
  }
518
1086
  if (token.typ == EnumToken.FrequencyTokenType) {
519
- return '0Hz';
1087
+ return "0Hz";
520
1088
  }
521
1089
  // @ts-ignore
522
1090
  if (token.typ == EnumToken.ResolutionTokenType) {
523
- return '0x';
1091
+ return "0x";
524
1092
  }
525
- return '0';
1093
+ return "0";
526
1094
  }
527
1095
  if (token.typ == EnumToken.TimeTokenType) {
528
- if (unit == 'ms') {
1096
+ if (unit == "ms") {
529
1097
  // @ts-ignore
530
1098
  const v = minifyNumber(val / 1000);
531
1099
  if (v.length + 1 <= val.length) {
532
- return v + 's';
1100
+ return v + "s";
533
1101
  }
534
- return val + 'ms';
1102
+ return val + "ms";
535
1103
  }
536
- return val + 's';
1104
+ return val + "s";
537
1105
  }
538
- if (token.typ == EnumToken.ResolutionTokenType && unit == 'dppx') {
539
- unit = 'x';
1106
+ if (token.typ == EnumToken.ResolutionTokenType && unit == "dppx") {
1107
+ unit = "x";
540
1108
  }
541
- return val.includes('/') ? val.replace('/', unit + '/') : val + unit;
1109
+ return val.includes("/") ? val.replace("/", unit + "/") : val + unit;
542
1110
  case EnumToken.FlexTokenType:
543
1111
  case EnumToken.PercentageTokenType:
544
- const uni = token.typ == EnumToken.PercentageTokenType ? '%' : 'fr';
545
- const perc = token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : minifyNumber(token.val);
546
- return options.minify && perc == '0' ? '0' : (perc.includes('/') ? perc.replace('/', uni + '/') : perc + uni);
1112
+ const uni = token.typ == EnumToken.PercentageTokenType ? "%" : "fr";
1113
+ const perc = token.val.typ == EnumToken.FractionTokenType
1114
+ ? renderValue(token.val, options, cache)
1115
+ : minifyNumber(token.val);
1116
+ return options.minify && perc == "0" ? "0" : perc.includes("/") ? perc.replace("/", uni + "/") : perc + uni;
547
1117
  case EnumToken.NumberTokenType:
548
- return token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : minifyNumber(token.val);
1118
+ return token.val.typ == EnumToken.FractionTokenType
1119
+ ? renderValue(token.val, options, cache)
1120
+ : minifyNumber(token.val);
1121
+ case EnumToken.AtRuleTokenType:
1122
+ return "@" + token.nam;
549
1123
  case EnumToken.CommentTokenType:
550
- if (options.removeComments && (!options.preserveLicense || !token.val.startsWith('/*!'))) {
551
- return '';
1124
+ case EnumToken.CDOCOMMNodeType:
1125
+ if (options.removeComments &&
1126
+ (!options.preserveLicense || !token.val.startsWith("/*!"))) {
1127
+ return "";
552
1128
  }
553
1129
  case EnumToken.PseudoClassTokenType:
554
1130
  case EnumToken.PseudoElementTokenType:
555
1131
  // https://www.w3.org/TR/selectors-4/#single-colon-pseudos
556
- if (token.typ == EnumToken.PseudoElementTokenType && pseudoElements.includes(token.val.slice(1))) {
1132
+ if (token.typ == EnumToken.PseudoElementTokenType &&
1133
+ pseudoElements.includes(token.val.slice(1))) {
557
1134
  return token.val.slice(1);
558
1135
  }
559
1136
  case EnumToken.UrlTokenTokenType:
560
- // if (token.typ == EnumToken.UrlTokenTokenType) {
561
- //
562
- // if (options.output != null) {
563
- //
564
- // if (!('original' in token)) {
565
- //
566
- // // do not modify original token
567
- // token = {...token};
568
- // Object.defineProperty(token, 'original', {
569
- // enumerable: false,
570
- // writable: false,
571
- // value: (token as UrlToken).val
572
- // })
573
- // }
574
- //
575
- // // @ts-ignore
576
- // if (!(token.original in cache)) {
577
- //
578
- // let output: string = <string>options.output ?? '';
579
- // const key = output + 'abs';
580
- //
581
- // if (!(key in cache)) {
582
- //
583
- // // @ts-ignore
584
- // cache[key] = options.dirname(options.resolve(output, options.cwd).absolute);
585
- // }
586
- //
587
- // // @ts-ignore
588
- // cache[token.original] = options.resolve(token.original, cache[key]).relative;
589
- // }
590
- //
591
- // // @ts-ignore
592
- // token.val = cache[token.original];
593
- // }
594
- // }
595
1137
  case EnumToken.HashTokenType:
596
1138
  case EnumToken.IdenTokenType:
597
- case EnumToken.AtRuleTokenType:
598
1139
  case EnumToken.StringTokenType:
599
1140
  case EnumToken.LiteralTokenType:
600
1141
  case EnumToken.DashedIdenTokenType:
601
1142
  case EnumToken.PseudoPageTokenType:
602
1143
  case EnumToken.ClassSelectorTokenType:
603
- return /* options.minify && 'Pseudo-class' == token.typ && '::' == token.val.slice(0, 2) ? token.val.slice(1) : */ token.val;
1144
+ return token.val;
604
1145
  case EnumToken.NestingSelectorTokenType:
605
- return '&';
1146
+ return "&";
606
1147
  case EnumToken.InvalidAttrTokenType:
607
- return '[' + token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), '');
1148
+ return ("[" +
1149
+ token.chi.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
608
1150
  case EnumToken.InvalidClassSelectorTokenType:
609
1151
  return token.val;
1152
+ case EnumToken.SupportsQueryUnaryConditionTokenType:
1153
+ case EnumToken.WhenElseUnaryConditionTokenType:
1154
+ return (renderValue(token.l, options, cache, reducer, errors) +
1155
+ " " +
1156
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), ""));
1157
+ case EnumToken.SupportsQueryConditionTokenType:
1158
+ case EnumToken.WhenElseQueryConditionTokenType:
1159
+ return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), "") +
1160
+ " " +
1161
+ renderValue(token.op, options, cache, reducer, errors) +
1162
+ " " +
1163
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), ""));
1164
+ case EnumToken.IfConditionTokenType:
1165
+ return token.l.length == 0
1166
+ ? ""
1167
+ : token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), "") +
1168
+ ":" +
1169
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), "");
1170
+ case EnumToken.IfElseConditionTokenType:
1171
+ return renderValue(token.l) + renderValue(token.r);
610
1172
  case EnumToken.DeclarationNodeType:
611
- return token.nam + ':' + (options.minify ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderToken(curr, options, cache), '');
612
- case EnumToken.MediaQueryConditionTokenType:
613
- return renderToken(token.l, options, cache, reducer, errors) + renderToken(token.op, options, cache, reducer, errors) + token.r.reduce((acc, curr) => acc + renderToken(curr, options, cache), '');
1173
+ return (token.nam +
1174
+ ":" +
1175
+ (options.minify ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
1176
+ case EnumToken.MediaQueryUnaryFeatureTokenType:
1177
+ return (renderValue(token.l, options, cache, reducer, errors) +
1178
+ " " +
1179
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
1180
+ case EnumToken.MediaQueryConditionTokenType: {
1181
+ const indent = token.op.typ == EnumToken.LtTokenType ||
1182
+ token.op.typ == EnumToken.GtTokenType ||
1183
+ token.op.typ == EnumToken.ColonTokenType ||
1184
+ token.op.typ == EnumToken.DelimTokenType ||
1185
+ token.op.typ == EnumToken.LteTokenType ||
1186
+ token.op.typ == EnumToken.GteTokenType
1187
+ ? ""
1188
+ : " ";
1189
+ return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), "") +
1190
+ indent +
1191
+ renderValue(token.op, options, cache, reducer, errors) +
1192
+ indent +
1193
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
1194
+ }
1195
+ case EnumToken.MediaRangeQueryTokenType:
1196
+ return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
1197
+ renderValue(token.op1) +
1198
+ token.val.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer, errors), "") +
1199
+ renderValue(token.op2) +
1200
+ token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
614
1201
  case EnumToken.MediaFeatureTokenType:
615
1202
  return token.val;
616
- case EnumToken.MediaFeatureNotTokenType:
617
- return 'not ' + renderToken(token.val, options, cache, reducer, errors);
618
- case EnumToken.MediaFeatureOnlyTokenType:
619
- return 'only ' + renderToken(token.val, options, cache, reducer, errors);
620
- case EnumToken.MediaFeatureAndTokenType:
621
- return 'and';
622
- case EnumToken.MediaFeatureOrTokenType:
623
- return 'or';
1203
+ case EnumToken.NotTokenType:
1204
+ return "not";
1205
+ case EnumToken.OnlyTokenType:
1206
+ return "only";
1207
+ case EnumToken.AndTokenType:
1208
+ return "and";
1209
+ case EnumToken.OrTokenType:
1210
+ return "or";
1211
+ case EnumToken.InvalidMediaQueryTokenType:
1212
+ case EnumToken.InvalidDeclarationNodeType:
1213
+ case EnumToken.InvalidCommentTokenType:
1214
+ case EnumToken.BadCommentTokenType:
1215
+ case EnumToken.BadCdoTokenType:
1216
+ case EnumToken.BadStringTokenType:
1217
+ case EnumToken.EOFTokenType:
1218
+ return "";
1219
+ default:
1220
+ console.debug({ token });
1221
+ throw new Error(`Unsupported token type for ${EnumToken[token.typ]}`);
624
1222
  }
625
- errors?.push({ action: 'ignore', message: `render: unexpected token ${JSON.stringify(token, null, 1)}` });
626
- return '';
1223
+ errors?.push({ action: "ignore", message: `render: unexpected token ${JSON.stringify(token, null, 1)}` });
1224
+ return "";
627
1225
  }
628
1226
  /**
629
1227
  * Remove whitespace tokens that are not needed
@@ -634,14 +1232,17 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
634
1232
  function filterValues(values) {
635
1233
  let i = 0;
636
1234
  for (; i < values.length; i++) {
637
- if (values[i].typ == EnumToken.ImportantTokenType && values[i - 1]?.typ == EnumToken.WhitespaceTokenType) {
1235
+ if (values[i].typ == EnumToken.ImportantTokenType && values[i - 1]?.typ === EnumToken.WhitespaceTokenType) {
638
1236
  values.splice(i - 1, 1);
639
1237
  }
640
- else if (funcLike.includes(values[i].typ) && !['var', 'calc'].includes(values[i].val) && values[i + 1]?.typ == EnumToken.WhitespaceTokenType) {
1238
+ else if (tokensfuncSet.has(values[i].typ) &&
1239
+ "chi" in values[i] &&
1240
+ values[i].typ != EnumToken.WildCardFunctionTokenType &&
1241
+ values[i + 1]?.typ == EnumToken.WhitespaceTokenType) {
641
1242
  values.splice(i + 1, 1);
642
1243
  }
643
1244
  }
644
1245
  return values;
645
1246
  }
646
1247
 
647
- export { doRender, filterValues, renderToken };
1248
+ export { doRender, filterValues, renderValue };