@tbela99/css-parser 1.4.11 → 1.6.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 (80) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +18 -11
  3. package/dist/index-umd-web.js +6558 -5310
  4. package/dist/index.cjs +6576 -5313
  5. package/dist/index.d.ts +805 -279
  6. package/dist/lib/ast/clone.js +13 -8
  7. package/dist/lib/ast/expand.js +65 -15
  8. package/dist/lib/ast/features/calc.js +19 -56
  9. package/dist/lib/ast/features/if.js +28 -4
  10. package/dist/lib/ast/features/inlinecssvariables.js +6 -5
  11. package/dist/lib/ast/features/prefix.js +52 -73
  12. package/dist/lib/ast/features/shorthand.js +11 -6
  13. package/dist/lib/ast/features/transform.js +6 -3
  14. package/dist/lib/ast/find.js +7 -5
  15. package/dist/lib/ast/math/expression.js +190 -32
  16. package/dist/lib/ast/minify.js +117 -49
  17. package/dist/lib/ast/node.js +52 -0
  18. package/dist/lib/ast/transform/compute.js +63 -4
  19. package/dist/lib/ast/transform/minify.js +6 -6
  20. package/dist/lib/ast/transform/perspective.js +1 -1
  21. package/dist/lib/ast/transform/rotate.js +1 -1
  22. package/dist/lib/ast/transform/scale.js +1 -1
  23. package/dist/lib/ast/transform/skew.js +1 -1
  24. package/dist/lib/ast/transform/translate.js +1 -1
  25. package/dist/lib/ast/transform/utils.js +46 -18
  26. package/dist/lib/ast/types.js +1 -1
  27. package/dist/lib/ast/walk.js +55 -11
  28. package/dist/lib/fs/resolve.js +68 -27
  29. package/dist/lib/parser/declaration/list.js +22 -7
  30. package/dist/lib/parser/declaration/map.js +40 -13
  31. package/dist/lib/parser/declaration/set.js +3 -1
  32. package/dist/lib/parser/linesmap.js +2 -12
  33. package/dist/lib/parser/parse.js +843 -912
  34. package/dist/lib/parser/source.js +16 -2
  35. package/dist/lib/parser/tokenize.js +1489 -725
  36. package/dist/lib/parser/utils/at-rule-container.js +29 -196
  37. package/dist/lib/parser/utils/at-rule-generic.js +6 -7
  38. package/dist/lib/parser/utils/at-rule-import.js +17 -13
  39. package/dist/lib/parser/utils/at-rule-media.js +36 -19
  40. package/dist/lib/parser/utils/at-rule-support.js +19 -9
  41. package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
  42. package/dist/lib/parser/utils/at-rule.js +0 -18
  43. package/dist/lib/parser/utils/declaration.js +30 -95
  44. package/dist/lib/parser/utils/hash.js +4 -4
  45. package/dist/lib/parser/utils/selector.js +164 -141
  46. package/dist/lib/parser/utils/text.js +4 -2
  47. package/dist/lib/renderer/render.js +230 -125
  48. package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
  49. package/dist/lib/renderer/sourcemap/sourcemap.js +187 -34
  50. package/dist/lib/syntax/color/a98rgb.js +14 -4
  51. package/dist/lib/syntax/color/cmyk.js +10 -18
  52. package/dist/lib/syntax/color/color-mix.js +21 -34
  53. package/dist/lib/syntax/color/color.js +32 -49
  54. package/dist/lib/syntax/color/hsl.js +15 -15
  55. package/dist/lib/syntax/color/hwb.js +18 -17
  56. package/dist/lib/syntax/color/lab.js +17 -17
  57. package/dist/lib/syntax/color/lch.js +13 -13
  58. package/dist/lib/syntax/color/oklab.js +10 -11
  59. package/dist/lib/syntax/color/oklch.js +15 -18
  60. package/dist/lib/syntax/color/p3.js +18 -12
  61. package/dist/lib/syntax/color/prophotorgb.js +26 -28
  62. package/dist/lib/syntax/color/rec2020.js +8 -4
  63. package/dist/lib/syntax/color/relative-color.js +26 -10
  64. package/dist/lib/syntax/color/srgb.js +5 -4
  65. package/dist/lib/syntax/color/utils/distance.js +1 -1
  66. package/dist/lib/syntax/color/xyz.js +2 -2
  67. package/dist/lib/syntax/color/xyzd50.js +2 -2
  68. package/dist/lib/syntax/constants.js +12 -2
  69. package/dist/lib/syntax/syntax.js +45 -258
  70. package/dist/lib/validation/config.json.js +3 -0
  71. package/dist/lib/validation/match.js +110 -27
  72. package/dist/node.js +55 -33
  73. package/dist/types.d.ts +5 -1
  74. package/dist/types.js +4 -0
  75. package/dist/utils/sync.d.ts +16 -0
  76. package/dist/utils/sync.js +73 -0
  77. package/dist/web.js +37 -30
  78. package/package.json +1 -1
  79. package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
  80. package/llms.txt +0 -73
@@ -3,12 +3,13 @@ import { reduceHexValue } from '../syntax/color/hex.js';
3
3
  import { EnumToken, ColorType } from '../ast/types.js';
4
4
  import { expand } from '../ast/expand.js';
5
5
  import { SourceMap } from './sourcemap/sourcemap.js';
6
- import { pseudoElements, urlTokenMatcher, PARENT, tokensfuncSet, LOC, colorPrecision } from '../syntax/constants.js';
7
- import { minifyNumber, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor, toPrecisionAngle, toPrecisionValue } from '../syntax/syntax.js';
6
+ import { pseudoElements, anglePrecision, urlTokenMatcher, PARENT, tokensfuncSet, LOCSTA, LOCSRCID } from '../syntax/constants.js';
7
+ import { minifyNumber, toPrecisionAngle, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor, isWhiteSpace, toPrecisionValue } from '../syntax/syntax.js';
8
8
  import { equalsIgnoreCase } from '../parser/utils/text.js';
9
9
  import { toDegrees } from '../parser/utils/angle.js';
10
10
  import { LineMap } from '../parser/linesmap.js';
11
11
  import { dirname } from '../fs/resolve.js';
12
+ import { SourceFile } from '../parser/source.js';
12
13
 
13
14
  /**
14
15
  * render ast
@@ -61,22 +62,28 @@ function doRender(data, options = {}, mapping) {
61
62
  const startTime = performance.now();
62
63
  const errors = [];
63
64
  const sourcemap = options.sourcemap ? new SourceMap() : null;
65
+ const sourcemaps = options.sourcemap ? { sources: [], maps: [] } : null;
64
66
  const cache = Object.create(null);
65
67
  const sourceLocation = {
66
- srcId: 0,
67
- sta: 0,
68
68
  end: 0,
69
69
  };
70
- const linesMap = new LineMap([]);
70
+ const linesMap = options.sourcemap ? new LineMap() : null;
71
71
  let code = "";
72
72
  if (mapping != null) {
73
73
  if (mapping.importMapping != null) {
74
- for (const [key, value] of Object.entries(mapping.importMapping)) {
74
+ const absolutePath = options.resolve(options.output != null ? dirname(options.output) : dirname(options.src), options.cwd).absolute;
75
+ for (let [key, value] of Object.entries(mapping.importMapping)) {
76
+ key = options.resolve(options.resolve(key, options.cwd).absolute, absolutePath).relative;
77
+ if (!key.startsWith("/") && !key.startsWith(".")) {
78
+ key = "./" + key;
79
+ }
75
80
  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}`;
76
81
  }
77
82
  }
78
83
  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}`;
79
- move(sourceLocation, linesMap, code);
84
+ if (sourcemap != null) {
85
+ move(sourceLocation, linesMap, code);
86
+ }
80
87
  }
81
88
  if (options.output != null) {
82
89
  // @ts-ignore
@@ -88,7 +95,7 @@ function doRender(data, options = {}, mapping) {
88
95
  [EnumToken.StyleSheetNodeType, EnumToken.AtRuleNodeType, EnumToken.RuleNodeType].includes(data.typ) &&
89
96
  "chi" in data
90
97
  ? expand(data)
91
- : data, options, sourcemap, sourceLocation, linesMap, errors, function reducer(acc, curr) {
98
+ : data, options, sourcemaps, sourceLocation, linesMap, errors, function reducer(acc, curr) {
92
99
  if (curr.typ == EnumToken.CommentTokenType && options.removeComments) {
93
100
  if (!options.preserveLicense || !curr.val.startsWith("/*!")) {
94
101
  return acc;
@@ -103,6 +110,12 @@ function doRender(data, options = {}, mapping) {
103
110
  },
104
111
  };
105
112
  if (sourcemap != null) {
113
+ let source;
114
+ for (const sourceId of sourcemaps.sources) {
115
+ source = options.sourcesMap.get(sourceId);
116
+ sourcemap.addSourceContent(source.id, source.getFileName(), source.getContent());
117
+ }
118
+ sourcemap.add(sourcemaps.maps);
106
119
  result.map = sourcemap;
107
120
  if (options.sourcemap === "inline") {
108
121
  result.code += `\n/*# sourceMappingURL=${result.map.toUrl()} */`;
@@ -115,44 +128,107 @@ function doRender(data, options = {}, mapping) {
115
128
  * @param node
116
129
  * @param options
117
130
  * @param cache
118
- * @param sourcemap
119
- * @param position
131
+ * @param sourcemaps
132
+ * @param sourceLocation
133
+ * @param linesMap
120
134
  * @param str
121
135
  *
122
136
  * @internal
123
137
  */
124
- function updateSourceMap(node, options, cache, sourcemap, sourceLocation, linesMap, str) {
125
- if ([
126
- EnumToken.RuleNodeType,
127
- EnumToken.AtRuleNodeType,
128
- EnumToken.KeyFramesRuleNodeType,
129
- EnumToken.KeyframesAtRuleNodeType,
130
- ].includes(node.typ)) {
131
- let srcId = node[LOC]?.srcId ?? 0;
132
- let sourceFileName = options.sourcesMap?.get(srcId)?.getFileName?.() || null;
133
- if (sourceFileName != null && options.output != null) {
134
- if (cache[sourceFileName] == null) {
135
- cache[sourceFileName] = options.resolve(sourceFileName, dirname(options.output)).relative;
138
+ function updateSourceMap(node, options, cache, sourcemaps, sourceLocation, linesMap, str) {
139
+ let offset = 0;
140
+ // eat leanding whitespace
141
+ while (offset < str.length && isWhiteSpace(str.charCodeAt(offset))) {
142
+ offset++;
143
+ }
144
+ if (offset > 0) {
145
+ move(sourceLocation, linesMap, str, 0, offset + 1);
146
+ }
147
+ if (node[LOCSTA] != null) {
148
+ const source = options.sourcesMap.get(node[LOCSRCID]);
149
+ const inputSourceMap = source.getInputSourceMap();
150
+ const offsets = source.getOffsets(node[LOCSTA]);
151
+ const [newLine, newColumn] = linesMap.getOffsets(sourceLocation.end);
152
+ let records = null;
153
+ let srcId = node[LOCSRCID];
154
+ let sourceFileName = source.getFileName() || null;
155
+ let sourceContent; // = (source.getContent() as string) || null;
156
+ if (inputSourceMap != null && (records = inputSourceMap.find(offsets[0], offsets[1])) != null) {
157
+ let newId = null;
158
+ for (const record of records) {
159
+ newId = null;
160
+ // @ts-ignore
161
+ sourceFileName = record[0] || null;
162
+ // @ts-ignore
163
+ offsets[0] = record[1];
164
+ // @ts-ignore
165
+ offsets[1] = record[2];
166
+ sourceContent = record[3] || null;
167
+ if (sourceFileName != null && options.output != null && !sourceFileName.startsWith("data:")) {
168
+ if (cache[sourceFileName] == null) {
169
+ const absolute = options.resolve(dirname(options.output), options.cwd)
170
+ .absolute;
171
+ const absoluteSourcePath = options.resolve(dirname(options.src || ""), options.cwd).absolute;
172
+ // resolution is relative to the source file
173
+ const absoluteSourceFileName = options.resolve(sourceFileName, absoluteSourcePath)
174
+ .absolute;
175
+ cache[sourceFileName] = options.resolve(absoluteSourceFileName, absolute).relative;
176
+ }
177
+ sourceFileName = cache[sourceFileName];
178
+ }
179
+ for (const [id, file] of options.sourcesMap.entries()) {
180
+ if (file.getFileName() === sourceFileName) {
181
+ newId = id;
182
+ break;
183
+ }
184
+ if (sourceFileName == null && file.getContent() === sourceContent) {
185
+ newId = id;
186
+ break;
187
+ }
188
+ }
189
+ if (newId == null) {
190
+ const source = new SourceFile(sourceContent, [], sourceFileName);
191
+ options.sourcesMap.set(source.id, source);
192
+ newId = source.id;
193
+ }
194
+ srcId = newId;
195
+ if (!sourcemaps.sources.includes(srcId)) {
196
+ sourcemaps.sources.push(srcId);
197
+ }
198
+ sourcemaps.maps.push([newLine, newColumn, srcId, offsets[0], offsets[1]]);
136
199
  }
137
- sourceFileName = cache[sourceFileName];
138
200
  }
139
- // @ts-ignore
140
- sourcemap.add(...linesMap.getOffsets(sourceLocation.end), srcId,
141
- // @ts-ignore
142
- ...options.sourcesMap?.get(srcId)?.getOffsets(sourceLocation.sta), sourceFileName, options.sourcesMap?.get(srcId)?.getContent?.());
201
+ else {
202
+ // if (sourceFileName != null && options.output != null && !sourceFileName.startsWith("data:")) {
203
+ // if (cache[sourceFileName] == null) {
204
+ // const absolute = options.resolve!(dirname(options.output as string), options.cwd as string)
205
+ // .absolute as string;
206
+ // const absoluteSourceFileName = options.resolve!(sourceFileName, options.cwd as string)
207
+ // .absolute as string;
208
+ // cache[sourceFileName] = options.resolve!(absoluteSourceFileName, absolute).relative as string;
209
+ // }
210
+ // sourceFileName = cache[sourceFileName] as string;
211
+ // }
212
+ if (!sourcemaps.sources.includes(srcId)) {
213
+ sourcemaps.sources.push(srcId);
214
+ }
215
+ sourcemaps.maps.push([newLine, newColumn, srcId, offsets[0], offsets[1]]);
216
+ }
143
217
  }
144
- move(sourceLocation, linesMap, str);
218
+ move(sourceLocation, linesMap, str, offset);
145
219
  }
146
220
  /**
147
221
  * Update position
148
- * @param position
222
+ * @param sourceLocation
223
+ * @param linesMap
149
224
  * @param str
150
225
  */
151
- function move(sourceLocation, linesMap, str) {
152
- let i = 0;
226
+ function move(sourceLocation, linesMap, str, start, end) {
227
+ let i = start ?? 0;
228
+ let j = end ?? str.length;
153
229
  let codepoint;
154
230
  let char;
155
- for (; i < str.length; i++) {
231
+ for (; i < j; i++) {
156
232
  char = str.charAt(i);
157
233
  codepoint = char.charCodeAt(0);
158
234
  sourceLocation.end += char.length;
@@ -175,8 +251,9 @@ function move(sourceLocation, linesMap, str) {
175
251
  * render ast node
176
252
  * @param data
177
253
  * @param options
178
- * @param sourcemap
179
- * @param position
254
+ * @param sourcemaps
255
+ * @param sourceLocation
256
+ * @param linesMap
180
257
  * @param errors
181
258
  * @param reducer
182
259
  * @param cache
@@ -185,13 +262,16 @@ function move(sourceLocation, linesMap, str) {
185
262
  *
186
263
  * @internal
187
264
  */
188
- function renderAstNode(data, options, sourcemap, sourceLocation, linesMap, errors, reducer, cache, level = 0, indents = []) {
265
+ function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level = 0, indents = []) {
189
266
  if (indents.length < level + 1) {
190
267
  indents.push(options.indent.repeat(level));
191
268
  }
192
269
  if (indents.length < level + 2) {
193
270
  indents.push(options.indent.repeat(level + 1));
194
271
  }
272
+ // @ts-ignore
273
+ let children = "";
274
+ let str = "";
195
275
  const indent = indents[level];
196
276
  const indentSub = indents[level + 1];
197
277
  switch (data.typ) {
@@ -202,37 +282,47 @@ function renderAstNode(data, options, sourcemap, sourceLocation, linesMap, error
202
282
  case EnumToken.CommentNodeType:
203
283
  case EnumToken.CDOCOMMNodeType:
204
284
  if (data.val.startsWith("/*# sourceMappingURL=")) {
205
- // ignore sourcemap
285
+ // ignore sourcemap comment
206
286
  return "";
207
287
  }
208
288
  return !options.removeComments || (options.preserveLicense && data.val.startsWith("/*!"))
209
289
  ? data.val
210
290
  : "";
211
291
  case EnumToken.StyleSheetNodeType:
212
- return data.chi.reduce((css, node) => {
213
- const hasPreviousContent = css !== "";
214
- const str = renderAstNode(node, options, sourcemap, sourceLocation, linesMap, errors, reducer, cache, level, indents);
292
+ for (const node of data.chi) {
293
+ str = renderAstNode(node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level, indents);
215
294
  if (str === "") {
216
- return css;
295
+ continue;
217
296
  }
218
- if (sourcemap != null && node[LOC] != null) {
219
- updateSourceMap(node, options, cache, sourcemap, sourceLocation, linesMap, (hasPreviousContent ? options.newLine : "") + str);
297
+ if (children.length > 0) {
298
+ str = options.newLine + str;
220
299
  }
221
- if (!hasPreviousContent) {
222
- return str;
300
+ children += str;
301
+ if (sourcemaps != null && str !== "" && options.newLine) {
302
+ move(sourceLocation, linesMap, options.newLine);
223
303
  }
224
- return `${css}${options.newLine}${str}`;
225
- }, "");
304
+ }
305
+ return children;
226
306
  case EnumToken.AtRuleNodeType:
227
307
  case EnumToken.RuleNodeType:
228
- case EnumToken.KeyFramesRuleNodeType:
308
+ case EnumToken.KeyframesRuleNodeType:
229
309
  case EnumToken.KeyframesAtRuleNodeType:
230
310
  if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !("chi" in data)) {
231
311
  return `${indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`;
232
312
  }
233
- // @ts-ignore
234
- let children = data.chi.reduce((css, node) => {
235
- let str;
313
+ const lineMapLength = linesMap ? linesMap.getLineStarts().length : 0;
314
+ const prelude = (indent.length > 0 ? options.newLine : "") +
315
+ indent +
316
+ ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ)
317
+ ? `@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val}${options.indent}{`
318
+ : data.sel + `${options.indent}{`);
319
+ if (sourcemaps != null) {
320
+ updateSourceMap(data, options, cache, sourcemaps, sourceLocation, linesMap, prelude);
321
+ }
322
+ let node;
323
+ let recordDeclarationSourceMap = data.typ == EnumToken.AtRuleNodeType;
324
+ for (let i = 0; i < data.chi.length; i++) {
325
+ node = data.chi[i];
236
326
  if (node.typ == EnumToken.CommentNodeType) {
237
327
  str =
238
328
  options.removeComments &&
@@ -241,73 +331,58 @@ function renderAstNode(data, options, sourcemap, sourceLocation, linesMap, error
241
331
  : node.val;
242
332
  }
243
333
  else if (node.typ == EnumToken.DeclarationNodeType) {
244
- // if (!(<AstDeclaration>node).nam.startsWith("--") && (<AstDeclaration>node).val.length === 0) {
245
- // // @ts-ignore
246
- // errors.push(<ErrorDescription>{
247
- // action: "ignore",
248
- // message: `render: invalid declaration ${JSON.stringify(node)}`,
249
- // location: node[LOC],
250
- // });
251
- // return "";
252
- // }
253
334
  str = `${node.nam}:${options.indent}${(options.minify
254
335
  ? filterValues(node.val)
255
336
  : node.val)
256
337
  .reduce(reducer, "")
257
338
  .trimEnd()};`;
258
339
  }
259
- // else if (node.typ == EnumToken.AtRuleNodeType && !("chi" in node)) {
260
- // str = `${(<AstAtRule>node).val === "" ? "" : options.indent || " "}${(<AstAtRule>node).val};`;
261
- // }
262
340
  else {
263
- str = renderAstNode(node, options, sourcemap, sourceLocation, linesMap, errors, reducer, cache, level + 1, indents);
264
- }
265
- if (css === "") {
266
- return str;
341
+ str = renderAstNode(node, options, sourcemaps, sourceLocation, linesMap, errors, reducer, cache, level + 1, indents);
342
+ if (str === "") {
343
+ continue;
344
+ }
345
+ children += str;
346
+ str = "";
347
+ continue;
267
348
  }
268
349
  if (str === "") {
269
- return css;
350
+ continue;
351
+ }
352
+ str = options.newLine + indentSub + str;
353
+ children += str;
354
+ if (sourcemaps != null && str !== "") {
355
+ if (node.typ == EnumToken.DeclarationNodeType && recordDeclarationSourceMap) {
356
+ // if declaration is child of at-rule, then record it
357
+ // .rule {
358
+ // @media screen {
359
+ // color: red;
360
+ // }
361
+ // }
362
+ // @ts-ignore
363
+ updateSourceMap(node, options, cache, sourcemaps, sourceLocation, linesMap, str);
364
+ }
365
+ else {
366
+ move(sourceLocation, linesMap, str);
367
+ }
270
368
  }
271
- return `${css}${options.newLine}${indentSub}${str}`;
272
- }, "");
273
- if (options.removeEmpty && children === "") {
274
- return "";
275
369
  }
276
370
  if (children.endsWith(";")) {
277
371
  children = children.slice(0, -1);
372
+ sourceLocation.end--;
373
+ }
374
+ if (options.removeEmpty && children === "") {
375
+ if (sourcemaps != null) {
376
+ sourceLocation.end -= prelude.length;
377
+ linesMap.getLineStarts().length = lineMapLength;
378
+ }
379
+ return "";
278
380
  }
279
- const rendered = [EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ)
280
- ? `@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val}${options.indent}{${options.newLine}` +
281
- (children === "" ? "" : indentSub + children + options.newLine) +
282
- indent +
283
- `}`
284
- : data.sel +
285
- `${options.indent}{${options.newLine}` +
286
- (children === "" ? "" : indentSub + children + options.newLine) +
287
- indent +
288
- `}`;
289
- if (sourcemap != null && data[LOC] != null) {
290
- updateSourceMap(data, options, cache, sourcemap, { ...sourceLocation }, linesMap.clone(), rendered);
381
+ const end = options.newLine + indent + `}`;
382
+ if (sourcemaps != null) {
383
+ move(sourceLocation, linesMap, end);
291
384
  }
292
- return rendered;
293
- // case EnumToken.CssVariableTokenType:
294
- // case EnumToken.CssVariableImportTokenType:
295
- // return `@value ${(<CssVariableToken | CssVariableImportTokenType>data).val}:${options.indent}${filterValues(
296
- // options.minify
297
- // ? (<CssVariableToken | CssVariableImportTokenType>data).val
298
- // : (<CssVariableToken>data).val,
299
- // )
300
- // .reduce(reducer, "")
301
- // .trim()};`;
302
- // case EnumToken.CssVariableDeclarationMapTokenType:
303
- // return `@value ${filterValues((data as CssVariableMapTokenType).vars)
304
- // .reduce((acc, curr) => acc + renderValue(curr), "")
305
- // .trim()} from ${filterValues((data as CssVariableMapTokenType).from)
306
- // .reduce((acc, curr) => acc + renderValue(curr), "")
307
- // .trim()};`;
308
- // case EnumToken.InvalidDeclarationNodeType:
309
- // case EnumToken.InvalidRuleNodeType:
310
- // case EnumToken.InvalidAtRuleNodeType:
385
+ return prelude + children + end;
311
386
  default:
312
387
  return "";
313
388
  }
@@ -316,6 +391,9 @@ function renderAstNode(data, options, sourcemap, sourceLocation, linesMap, error
316
391
  * render ast token
317
392
  * @param token
318
393
  * @param options
394
+ * @param cache
395
+ * @param reducer
396
+ * @param errors
319
397
  * @private
320
398
  */
321
399
  function renderValue(token, options = {}, cache = Object.create(null), reducer, errors) {
@@ -392,8 +470,8 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
392
470
  return " + ";
393
471
  case EnumToken.Sub:
394
472
  return " - ";
395
- case EnumToken.Star:
396
473
  case EnumToken.UniversalSelectorTokenType:
474
+ case EnumToken.Star:
397
475
  case EnumToken.Mul:
398
476
  return "*";
399
477
  case EnumToken.Div:
@@ -590,7 +668,9 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
590
668
  // }
591
669
  }
592
670
  if (slice[i]?.typ === EnumToken.ColorTokenType) {
593
- slice.push(...reduceColorStops(slice.splice(i, slice.length - i)));
671
+ for (const token of reduceColorStops(slice.splice(i, slice.length - i))) {
672
+ slice.push(token);
673
+ }
594
674
  }
595
675
  }
596
676
  break;
@@ -781,32 +861,45 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
781
861
  }
782
862
  const result = [];
783
863
  if (form.length > 0) {
784
- result.push(...form);
864
+ for (const token of form) {
865
+ result.push(token);
866
+ }
785
867
  }
786
868
  if (size.length > 0) {
787
869
  if (result.length > 0) {
788
870
  result.push({ typ: EnumToken.WhitespaceTokenType });
789
871
  }
790
- result.push(...size);
872
+ for (const token of size) {
873
+ result.push(token);
874
+ }
791
875
  }
792
876
  if (positions.length > 0) {
793
877
  if (result.length > 0) {
794
878
  result.push({ typ: EnumToken.WhitespaceTokenType });
795
879
  }
796
- result.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }, ...positions);
880
+ result.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType });
881
+ for (const token of positions) {
882
+ result.push(token);
883
+ }
797
884
  }
798
885
  if (colorSpaceDef.length > 0) {
799
886
  if (result.length > 0) {
800
887
  result.push({ typ: EnumToken.WhitespaceTokenType });
801
888
  }
802
- result.push(...colorSpaceDef);
889
+ for (const token of colorSpaceDef) {
890
+ result.push(token);
891
+ }
803
892
  }
804
893
  if (result.length > 0) {
805
894
  result.push({ typ: EnumToken.CommaTokenType });
806
895
  }
807
- result.push(...reduceColorStops(slice.slice(i)));
896
+ for (const token of reduceColorStops(slice.slice(i))) {
897
+ result.push(token);
898
+ }
808
899
  slice.length = 0;
809
- slice.push(...result);
900
+ for (const token of result) {
901
+ slice.push(token);
902
+ }
810
903
  }
811
904
  break;
812
905
  case "conic-gradient":
@@ -911,24 +1004,36 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
911
1004
  if (angles.length > 0) {
912
1005
  angles.push({ typ: EnumToken.WhitespaceTokenType });
913
1006
  }
914
- angles.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }, ...positions);
1007
+ angles.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType });
1008
+ for (const position of positions) {
1009
+ angles.push(position);
1010
+ }
915
1011
  }
916
1012
  }
917
1013
  if (angles.length > 0) {
918
- result.push(...angles, { typ: EnumToken.CommaTokenType });
1014
+ for (const angle of angles) {
1015
+ result.push(angle);
1016
+ }
1017
+ result.push({ typ: EnumToken.CommaTokenType });
919
1018
  }
920
1019
  if (colorSpaceDef.length > 0) {
921
1020
  if (colorSpaceDef.length > 0) {
922
1021
  if (result.length > 0) {
923
1022
  result.push({ typ: EnumToken.WhitespaceTokenType });
924
1023
  }
925
- result.push(...colorSpaceDef);
1024
+ for (const token of colorSpaceDef) {
1025
+ result.push(token);
1026
+ }
926
1027
  }
927
1028
  result.push({ typ: EnumToken.CommaTokenType });
928
1029
  }
929
- result.push(...reduceConicColorStops(slice.slice(i)));
1030
+ for (const token of reduceConicColorStops(slice.slice(i))) {
1031
+ result.push(token);
1032
+ }
930
1033
  slice.length = 0;
931
- slice.push(...result);
1034
+ for (let j = 0; j < result.length; j++) {
1035
+ slice.push(result[j]);
1036
+ }
932
1037
  }
933
1038
  break;
934
1039
  }
@@ -1062,29 +1167,29 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1062
1167
  const angle = getAngle(token);
1063
1168
  let v;
1064
1169
  let value = val + unit;
1065
- for (const u of ["turn", "deg", "rad", "grad"]) {
1170
+ for (const u of ["deg", "turn", "rad", "grad"]) {
1066
1171
  if (token.unit == u) {
1067
1172
  continue;
1068
1173
  }
1069
1174
  switch (u) {
1070
- case "turn":
1071
- v = minifyNumber(toPrecisionAngle(angle, colorPrecision, false));
1072
- if (v.length + 4 < value.length) {
1175
+ case "deg":
1176
+ v = minifyNumber(toPrecisionAngle(angle * 360, anglePrecision, false).toFixed(anglePrecision));
1177
+ if (v.length + 3 < value.length) {
1073
1178
  val = v;
1074
1179
  unit = u;
1075
1180
  value = v + u;
1076
1181
  }
1077
1182
  break;
1078
- case "deg":
1079
- v = minifyNumber(toPrecisionAngle(angle * 360, colorPrecision, false));
1080
- if (v.length + 3 < value.length) {
1183
+ case "turn":
1184
+ v = minifyNumber(toPrecisionAngle(angle, anglePrecision, false).toFixed(anglePrecision));
1185
+ if (v.length + 4 < value.length) {
1081
1186
  val = v;
1082
1187
  unit = u;
1083
1188
  value = v + u;
1084
1189
  }
1085
1190
  break;
1086
1191
  case "rad":
1087
- v = minifyNumber(toPrecisionAngle(angle * (2 * Math.PI), colorPrecision, false));
1192
+ v = minifyNumber(toPrecisionAngle(angle * (2 * Math.PI), anglePrecision, false).toFixed(anglePrecision));
1088
1193
  if (v.length + 3 < value.length) {
1089
1194
  val = v;
1090
1195
  unit = u;
@@ -1092,7 +1197,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1092
1197
  }
1093
1198
  break;
1094
1199
  case "grad":
1095
- v = minifyNumber(toPrecisionAngle(angle * 400, colorPrecision, false));
1200
+ v = minifyNumber(toPrecisionAngle(angle * 400, anglePrecision, false).toFixed(anglePrecision));
1096
1201
  if (v.length + 4 < value.length) {
1097
1202
  val = v;
1098
1203
  unit = u;
@@ -1232,11 +1337,11 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
1232
1337
  case EnumToken.OrTokenType:
1233
1338
  return "or";
1234
1339
  case EnumToken.InvalidMediaQueryTokenType:
1235
- // case EnumToken.InvalidDeclarationNodeType:
1236
1340
  case EnumToken.InvalidCommentTokenType:
1237
1341
  case EnumToken.BadCommentTokenType:
1238
1342
  case EnumToken.BadCdoTokenType:
1239
1343
  case EnumToken.BadStringTokenType:
1344
+ case EnumToken.BadUrlTokenType:
1240
1345
  case EnumToken.EOFTokenType:
1241
1346
  return "";
1242
1347
  default:
@@ -0,0 +1,78 @@
1
+ // from https://github.com/Rich-Harris/vlq/tree/master
2
+ // credit: Rich Harris
3
+ const integer_to_char = {};
4
+ const char_to_integer = {};
5
+ let i = 0;
6
+ for (const char of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=') {
7
+ char_to_integer[char] = i;
8
+ integer_to_char[i++] = char;
9
+ }
10
+ /**
11
+ * @param {string} str
12
+ */
13
+ function decode(str) {
14
+ /** @type {number[]} */
15
+ let result = [];
16
+ let shift = 0;
17
+ let value = 0;
18
+ for (let i = 0; i < str.length; i += 1) {
19
+ let integer = char_to_integer[str[i]];
20
+ // if (integer === undefined) {
21
+ // throw new Error('Invalid character (' + str[i] + ')');
22
+ // }
23
+ const has_continuation_bit = integer & 32;
24
+ integer &= 31;
25
+ value += integer << shift;
26
+ if (has_continuation_bit) {
27
+ shift += 5;
28
+ }
29
+ else {
30
+ const should_negate = value & 1;
31
+ value >>>= 1;
32
+ if (should_negate) {
33
+ result.push(value === 0 ? -2147483648 : -value);
34
+ }
35
+ else {
36
+ result.push(value);
37
+ }
38
+ // reset
39
+ value = shift = 0;
40
+ }
41
+ }
42
+ return result;
43
+ }
44
+ /**
45
+ *
46
+ * @param value
47
+ * @returns
48
+ */
49
+ function encode(value) {
50
+ if (typeof value === 'number') {
51
+ return encode_integer(value);
52
+ }
53
+ let result = '';
54
+ for (let i = 0; i < value.length; i += 1) {
55
+ result += encode_integer(value[i]);
56
+ }
57
+ return result;
58
+ }
59
+ function encode_integer(num) {
60
+ let result = '';
61
+ if (num < 0) {
62
+ num = (-num << 1) | 1;
63
+ }
64
+ else {
65
+ num <<= 1;
66
+ }
67
+ do {
68
+ let clamped = num & 31;
69
+ num >>>= 5;
70
+ if (num > 0) {
71
+ clamped |= 32;
72
+ }
73
+ result += integer_to_char[clamped];
74
+ } while (num > 0);
75
+ return result;
76
+ }
77
+
78
+ export { decode, encode };