@tbela99/css-parser 2.1.0 → 2.2.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 (125) hide show
  1. package/CHANGELOG.md +42 -1
  2. package/README.md +14 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/data → data}/properties.json.js +86 -2
  5. package/dist/{src/data → data}/validation.json.js +5 -2
  6. package/dist/index-umd-web.js +10738 -4359
  7. package/dist/index.d.ts +734 -100
  8. package/dist/{src/lib → lib}/ast/arena-node.js +10 -10
  9. package/dist/{src/lib → lib}/ast/arena.js +304 -139
  10. package/dist/{src/lib → lib}/ast/cache.js +2 -2
  11. package/dist/{src/lib → lib}/ast/features/calc.js +11 -9
  12. package/dist/{src/lib → lib}/ast/features/if.js +6 -6
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +12 -10
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +39 -39
  17. package/dist/lib/ast/features/shorthand.js +129 -0
  18. package/dist/lib/ast/features/transform.js +188 -0
  19. package/dist/{src/lib → lib}/ast/features/type.js +2 -2
  20. package/dist/{src/lib → lib}/ast/math/expression.js +24 -24
  21. package/dist/{src/lib → lib}/ast/math/math.js +2 -2
  22. package/dist/{src/lib → lib}/ast/minify.js +745 -201
  23. package/dist/lib/ast/mixins/custom-function.js +393 -0
  24. package/dist/lib/ast/mixins/utils.js +53 -0
  25. package/dist/lib/ast/mixins/variables.js +678 -0
  26. package/dist/lib/ast/strings.js +906 -0
  27. package/dist/lib/ast/transform/compute.js +560 -0
  28. package/dist/lib/ast/transform/matrix.js +67 -0
  29. package/dist/lib/ast/transform/minify.js +325 -0
  30. package/dist/lib/ast/transform/perspective.js +24 -0
  31. package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
  32. package/dist/lib/ast/transform/scale.js +43 -0
  33. package/dist/lib/ast/transform/skew.js +38 -0
  34. package/dist/lib/ast/transform/translate.js +56 -0
  35. package/dist/lib/ast/transform/utils.js +390 -0
  36. package/dist/{src/lib → lib}/ast/types.js +2 -2
  37. package/dist/{src/lib → lib}/ast/walk.js +2 -2
  38. package/dist/{src/lib → lib}/fs/resolve.js +2 -2
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +25 -8
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +15 -15
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +5 -5
  42. package/dist/{src/lib → lib}/parser/input.js +18 -3
  43. package/dist/{src/lib → lib}/parser/linesmap.js +2 -2
  44. package/dist/{src/lib → lib}/parser/parse.js +425 -258
  45. package/dist/{src/lib → lib}/parser/source.js +2 -2
  46. package/dist/{src/lib → lib}/parser/tokenize.js +480 -312
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +12 -12
  49. package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
  50. package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +7 -7
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +10 -10
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +14 -14
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +9 -14
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +6 -6
  55. package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
  56. package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
  57. package/dist/lib/parser/utils/cache.js +53 -0
  58. package/dist/lib/parser/utils/canonical.js +78 -0
  59. package/dist/lib/parser/utils/codes.js +93 -0
  60. package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
  61. package/dist/{src/lib → lib}/parser/utils/declaration.js +31 -26
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +2 -2
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +2 -2
  64. package/dist/{src/lib → lib}/parser/utils/selector.js +305 -63
  65. package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
  66. package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
  67. package/dist/{src/lib → lib}/parser/utils/type.js +9 -7
  68. package/dist/{src/lib → lib}/printer/render.js +420 -152
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +2 -2
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +2 -2
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +2 -2
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +10 -10
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +2 -2
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +18 -18
  77. package/dist/{src/lib → lib}/syntax/color/color.js +18 -18
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +3 -3
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +2 -2
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +2 -2
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +2 -2
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +2 -2
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +2 -2
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +3 -3
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +2 -2
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +2 -2
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +14 -14
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +4 -4
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +5 -5
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +4 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +10 -10
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +5 -5
  94. package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
  95. package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +3 -3
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +2 -2
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +2 -2
  99. package/dist/{src/lib → lib}/syntax/constants.js +26 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +37 -29
  101. package/dist/{src/lib → lib}/validation/config.js +23 -10
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +292 -150
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +2 -2
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +2 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +52 -34
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +10 -5
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +47 -34
  113. package/package.json +12 -12
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -95
  116. package/dist/src/lib/ast/features/transform.js +0 -90
  117. package/dist/src/lib/ast/transform/compute.js +0 -362
  118. package/dist/src/lib/ast/transform/matrix.js +0 -88
  119. package/dist/src/lib/ast/transform/minify.js +0 -267
  120. package/dist/src/lib/ast/transform/perspective.js +0 -16
  121. package/dist/src/lib/ast/transform/scale.js +0 -39
  122. package/dist/src/lib/ast/transform/skew.js +0 -30
  123. package/dist/src/lib/ast/transform/translate.js +0 -39
  124. package/dist/src/lib/ast/transform/utils.js +0 -287
  125. package/dist/src/lib/parser/utils/cache.js +0 -54
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  var ValidationTokenEnum;
@@ -1,28 +1,12 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
9
9
 
10
- /**
11
- * remove comma and comment tokens
12
- * @param arena
13
- * @param tokenList
14
- */
15
- function stripCommaToken(arena, tokenList) {
16
- let result = [];
17
- for (let i = 0; i < tokenList.length; i++) {
18
- const typ = arena.dataTypes[tokenList[i]];
19
- if (typ === EnumToken.CommentTokenType || typ === EnumToken.CommaTokenType) {
20
- continue;
21
- }
22
- result.push(tokenList[i]);
23
- }
24
- return result;
25
- }
26
10
  /**
27
11
  * split a token list
28
12
  * @param arena
@@ -47,4 +31,4 @@ function splitTokenList(arena, tokenList, split = [EnumToken.CommaTokenType], in
47
31
  return result;
48
32
  }
49
33
 
50
- export { splitTokenList, stripCommaToken };
34
+ export { splitTokenList };
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { Readable } from 'node:stream';
@@ -12,6 +12,9 @@ import { doParse, doParseSync } from './lib/parser/parse.js';
12
12
  export { parseDeclarations, parseString } from './lib/parser/parse.js';
13
13
  import { doRender } from './lib/printer/render.js';
14
14
  export { renderValue } from './lib/printer/render.js';
15
+ import { TransformResult, RenderResult } from './lib/printer/result.js';
16
+ import { useNativeBuffer } from './lib/printer/writer.js';
17
+ import { Buffer } from 'node:buffer';
15
18
  import { ModuleScopeEnumOptions } from './lib/ast/types.js';
16
19
  export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, WalkerEvent, WalkerOptionEnum } from './lib/ast/types.js';
17
20
  import { createParseInfo, Tokenizer } from './lib/parser/tokenize.js';
@@ -21,7 +24,7 @@ import { ResponseType } from './types.js';
21
24
  import { resolve as resolve$1 } from 'node:path';
22
25
  import { SourceFile } from './lib/parser/source.js';
23
26
  import { cwd } from 'node:process';
24
- import { parseResult, validateSyncArguments } from './utils/sync.js';
27
+ import { validateSyncArguments, parseResult } from './utils/sync.js';
25
28
  export { minify } from './lib/ast/minify.js';
26
29
  export { convertColor } from './lib/syntax/color/color.js';
27
30
  export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
@@ -30,8 +33,10 @@ export { ARENA, ARENAID, AstArenaNode } from './lib/ast/arena-node.js';
30
33
  export { SourceMap } from './lib/printer/sourcemap/sourcemap.js';
31
34
  export { FeatureWalkMode } from './lib/ast/features/type.js';
32
35
  export { parseSyntax } from './lib/validation/parser/parse.js';
33
- export { version } from '../package.json.js';
36
+ export { version } from './version.js';
34
37
 
38
+ // the rendered css is encoded with the node.js Buffer: faster than TextEncoder.encodeInto()
39
+ useNativeBuffer(Buffer);
35
40
  /**
36
41
  * Load file or url
37
42
  * @param url
@@ -117,7 +122,9 @@ async function load(url, currentDirectory = ".", responseType = false) {
117
122
  * ```
118
123
  */
119
124
  function render(arena, data, options = {}, mapping) {
120
- return doRender(arena, data, Object.assign(options, { resolve, dirname, cwd: options.cwd ?? resolve$1() }), mapping);
125
+ return doRender(arena, data,
126
+ // the options of the caller are not modified
127
+ { ...options, resolve, dirname, cwd: options.cwd ?? resolve$1() }, mapping);
121
128
  }
122
129
  /**
123
130
  * Parse CSS
@@ -150,7 +157,15 @@ function parseSync(...args) {
150
157
  if (options != null) {
151
158
  validateSyncArguments(options);
152
159
  }
153
- options ??= {};
160
+ // the options of the caller are not modified: they may be reused
161
+ return parseSyncInput(stream, { ...options });
162
+ }
163
+ /**
164
+ * parse a css input. the options belong to the call: they are modified (source, parse info, ...)
165
+ * @param stream
166
+ * @param options
167
+ */
168
+ function parseSyncInput(stream, options) {
154
169
  options.src ??= "";
155
170
  options.sourcesMap ??= new Map();
156
171
  Object.assign(options, {
@@ -197,7 +212,11 @@ function transformSync(...args) {
197
212
  options = opt;
198
213
  stream = input;
199
214
  }
200
- options ??= {};
215
+ if (options != null) {
216
+ validateSyncArguments(options);
217
+ }
218
+ // the options of the caller are not modified: they may be reused
219
+ options = { ...options };
201
220
  if (options.minify == null) {
202
221
  options.minify = true;
203
222
  }
@@ -208,7 +227,7 @@ function transformSync(...args) {
208
227
  options.removeCharset = true;
209
228
  }
210
229
  const startTime = performance.now();
211
- const parseResult = parseSync(stream, options);
230
+ const parseResult = parseSyncInput(stream, options);
212
231
  let mapping = null;
213
232
  let importMapping = null;
214
233
  if ((typeof options.module === "number" && options.module & ModuleScopeEnumOptions.ICSS) ||
@@ -223,17 +242,12 @@ function transformSync(...args) {
223
242
  ...options,
224
243
  expandNestingRules: false,
225
244
  }, mapping != null ? { mapping, importMapping } : null);
226
- return {
227
- ...parseResult,
228
- ...rendered,
229
- errors: parseResult.errors.concat(rendered.errors),
230
- stats: {
231
- bytesOut: rendered.code.length,
232
- ...parseResult.stats,
233
- render: rendered.stats.total,
234
- total: `${(performance.now() - startTime).toFixed(2)}ms`,
235
- },
236
- };
245
+ return new TransformResult(parseResult, rendered, {
246
+ bytesOut: RenderResult.codeLength(rendered),
247
+ ...parseResult.stats,
248
+ render: rendered.stats.total,
249
+ total: `${(performance.now() - startTime).toFixed(2)}ms`,
250
+ });
237
251
  }
238
252
  /**
239
253
  * Parse CSS
@@ -300,7 +314,15 @@ async function parse(...args) {
300
314
  stream = input;
301
315
  }
302
316
  }
303
- options ??= {};
317
+ // the options of the caller are not modified: they may be reused
318
+ return parseInput(stream, { ...options });
319
+ }
320
+ /**
321
+ * parse a css input. the options belong to the call: they are modified (source, parse info, ...)
322
+ * @param stream
323
+ * @param options
324
+ */
325
+ async function parseInput(stream, options) {
304
326
  options.src ??= "";
305
327
  options.sourcesMap ??= new Map();
306
328
  Object.assign(options, {
@@ -386,7 +408,8 @@ async function transform(...args) {
386
408
  stream = input;
387
409
  }
388
410
  }
389
- options ??= {};
411
+ // the options of the caller are not modified: they may be reused
412
+ options = { ...options };
390
413
  if (options.minify == null) {
391
414
  options.minify = true;
392
415
  }
@@ -397,7 +420,7 @@ async function transform(...args) {
397
420
  options.removeCharset = true;
398
421
  }
399
422
  const startTime = performance.now();
400
- return parse(stream, options).then((parseResult) => {
423
+ return parseInput(stream, options).then((parseResult) => {
401
424
  let mapping = null;
402
425
  let importMapping = null;
403
426
  if ((typeof options.module === "number" && options.module & ModuleScopeEnumOptions.ICSS) ||
@@ -412,18 +435,13 @@ async function transform(...args) {
412
435
  ...options,
413
436
  expandNestingRules: false,
414
437
  }, mapping != null ? { mapping, importMapping } : null);
415
- return {
416
- ...parseResult,
417
- ...rendered,
418
- errors: parseResult.errors.concat(rendered.errors),
419
- stats: {
420
- bytesOut: rendered.code.length,
421
- ...parseResult.stats,
422
- render: rendered.stats.total,
423
- total: `${(performance.now() - startTime).toFixed(2)}ms`,
424
- },
425
- };
438
+ return new TransformResult(parseResult, rendered, {
439
+ bytesOut: RenderResult.codeLength(rendered),
440
+ ...parseResult.stats,
441
+ render: rendered.stats.total,
442
+ total: `${(performance.now() - startTime).toFixed(2)}ms`,
443
+ });
426
444
  });
427
445
  }
428
446
 
429
- export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseSync, render, resolve, transform, transformSync };
447
+ export { ModuleScopeEnumOptions, RenderResult, ResponseType, TransformResult, dirname, load, parse, parseSync, render, resolve, transform, transformSync };
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -1,12 +1,11 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../lib/ast/types.js';
9
- import { AstArena } from '../lib/ast/arena.js';
10
9
  import { dirname } from '../lib/fs/resolve.js';
11
10
  import { ResponseType } from '../types.js';
12
11
 
@@ -29,7 +28,7 @@ function parseResult(result, options) {
29
28
  if (token !== 0 &&
30
29
  arena.dataTypes[token] === EnumToken.CommentTokenType &&
31
30
  arena.startsWith(token, "val", "/*# sourceMappingURL=")) {
32
- let data = arena.strings[arena.stringValue[token]].slice(21, -2).trim();
31
+ let data = arena.resolveString(arena.stringValue[token]).slice(21, -2).trim();
33
32
  if (data.endsWith(".map")) {
34
33
  if (options.load == null) {
35
34
  data = "";
@@ -74,7 +73,13 @@ function validateSyncArguments(options, prefix = "options.") {
74
73
  throw new Error(`[${prefix + key}]: Async functions are not supported in sync mode. Use parse() or transform() instead.`);
75
74
  }
76
75
  }
77
- else if (value != null && typeof value === "object" && !(value instanceof AstArena)) {
76
+ else if (value != null &&
77
+ typeof value === "object" &&
78
+ // the plain objects (visitors, ...) and the arrays: the class instances (arenas, source files, maps, typed
79
+ // arrays, ...) are not traversed
80
+ (Array.isArray(value) ||
81
+ Object.getPrototypeOf(value) === Object.prototype ||
82
+ Object.getPrototypeOf(value) === null)) {
78
83
  validateSyncArguments(value, prefix + key + ".");
79
84
  }
80
85
  }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Version number
3
+ */
4
+ export declare const version = "2.2.0";
@@ -0,0 +1,13 @@
1
+ /*
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.2.0
6
+ License: MIT
7
+ */
8
+ /**
9
+ * Version number
10
+ */
11
+ const version = "2.2.0";
12
+
13
+ export { version };
@@ -1,14 +1,15 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { doParse, doParseSync } from './lib/parser/parse.js';
9
9
  export { parseDeclarations, parseString } from './lib/parser/parse.js';
10
10
  import { doRender } from './lib/printer/render.js';
11
11
  export { renderValue } from './lib/printer/render.js';
12
+ import { TransformResult, RenderResult } from './lib/printer/result.js';
12
13
  import { ModuleScopeEnumOptions } from './lib/ast/types.js';
13
14
  export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, WalkerEvent, WalkerOptionEnum } from './lib/ast/types.js';
14
15
  import { createParseInfo, Tokenizer } from './lib/parser/tokenize.js';
@@ -16,7 +17,7 @@ import { isCssInput } from './lib/parser/input.js';
16
17
  import { matchUrl, resolve, dirname } from './lib/fs/resolve.js';
17
18
  import { ResponseType } from './types.js';
18
19
  import { SourceFile } from './lib/parser/source.js';
19
- import { parseResult, validateSyncArguments } from './utils/sync.js';
20
+ import { validateSyncArguments, parseResult } from './utils/sync.js';
20
21
  export { minify } from './lib/ast/minify.js';
21
22
  export { convertColor } from './lib/syntax/color/color.js';
22
23
  export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
@@ -26,7 +27,7 @@ export { PropertyList } from './lib/parser/declaration/list.js';
26
27
  export { SourceMap } from './lib/printer/sourcemap/sourcemap.js';
27
28
  export { FeatureWalkMode } from './lib/ast/features/type.js';
28
29
  export { parseSyntax } from './lib/validation/parser/parse.js';
29
- export { version } from '../package.json.js';
30
+ export { version } from './version.js';
30
31
 
31
32
  /**
32
33
  * Load file or url
@@ -102,13 +103,16 @@ async function load(url, currentDirectory = ".", responseType = false) {
102
103
  * ```
103
104
  */
104
105
  function render(arena, data, options = {}, mapping) {
105
- return doRender(arena, data, Object.assign(options, {
106
+ return doRender(arena, data,
107
+ // the options of the caller are not modified
108
+ {
109
+ ...options,
106
110
  resolve,
107
111
  dirname,
108
112
  cwd: (options.cwd ?? self.location.pathname.endsWith("/"))
109
113
  ? self.location.pathname
110
114
  : dirname(self.location.pathname),
111
- }), mapping);
115
+ }, mapping);
112
116
  }
113
117
  /**
114
118
  * Parse CSS
@@ -141,7 +145,15 @@ function parseSync(...args) {
141
145
  if (options != null) {
142
146
  validateSyncArguments(options);
143
147
  }
144
- options ??= {};
148
+ // the options of the caller are not modified: they may be reused
149
+ return parseSyncInput(stream, { ...options });
150
+ }
151
+ /**
152
+ * parse a css input. the options belong to the call: they are modified (source, parse info, ...)
153
+ * @param stream
154
+ * @param options
155
+ */
156
+ function parseSyncInput(stream, options) {
145
157
  options.src ??= "";
146
158
  options.sourcesMap ??= new Map();
147
159
  Object.assign(options, {
@@ -190,10 +202,13 @@ function transformSync(...args) {
190
202
  options = opt;
191
203
  stream = input;
192
204
  }
193
- options ??= {};
205
+ if (options != null) {
206
+ validateSyncArguments(options);
207
+ }
208
+ // the options of the caller are not modified: they may be reused
194
209
  options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
195
210
  const startTime = performance.now();
196
- const parseResult = parseSync(stream, options);
211
+ const parseResult = parseSyncInput(stream, options);
197
212
  let mapping = null;
198
213
  let importMapping = null;
199
214
  if ((typeof options.module === "number" && options.module & ModuleScopeEnumOptions.ICSS) ||
@@ -208,17 +223,12 @@ function transformSync(...args) {
208
223
  ...options,
209
224
  expandNestingRules: false,
210
225
  }, mapping != null ? { mapping, importMapping } : null);
211
- return {
212
- ...parseResult,
213
- ...rendered,
214
- errors: parseResult.errors.concat(rendered.errors),
215
- stats: {
216
- bytesOut: rendered.code.length,
217
- ...parseResult.stats,
218
- render: rendered.stats.total,
219
- total: `${(performance.now() - startTime).toFixed(2)}ms`,
220
- },
221
- };
226
+ return new TransformResult(parseResult, rendered, {
227
+ bytesOut: RenderResult.codeLength(rendered),
228
+ ...parseResult.stats,
229
+ render: rendered.stats.total,
230
+ total: `${(performance.now() - startTime).toFixed(2)}ms`,
231
+ });
222
232
  }
223
233
  /**
224
234
  * Parse CSS
@@ -268,7 +278,15 @@ async function parse(...args) {
268
278
  stream = input;
269
279
  }
270
280
  }
271
- options ??= {};
281
+ // the options of the caller are not modified: they may be reused
282
+ return parseInput(stream, { ...options });
283
+ }
284
+ /**
285
+ * parse a css input. the options belong to the call: they are modified (source, parse info, ...)
286
+ * @param stream
287
+ * @param options
288
+ */
289
+ async function parseInput(stream, options) {
272
290
  options.src ??= "";
273
291
  options.sourcesMap ??= new Map();
274
292
  Object.assign(options, {
@@ -337,7 +355,7 @@ async function transform(...args) {
337
355
  }
338
356
  options = { minify: true, removeEmpty: true, removeCharset: true, ...options };
339
357
  const startTime = performance.now();
340
- return parse(stream, options).then((parseResult) => {
358
+ return parseInput(stream, options).then((parseResult) => {
341
359
  let mapping = null;
342
360
  let importMapping = null;
343
361
  if ((typeof options.module === "number" && options.module & ModuleScopeEnumOptions.ICSS) ||
@@ -352,18 +370,13 @@ async function transform(...args) {
352
370
  ...options,
353
371
  expandNestingRules: false,
354
372
  }, mapping != null ? { mapping, importMapping } : null);
355
- return {
356
- ...parseResult,
357
- ...rendered,
358
- errors: parseResult.errors.concat(rendered.errors),
359
- stats: {
360
- bytesOut: rendered.code.length,
361
- ...parseResult.stats,
362
- render: rendered.stats.total,
363
- total: `${(performance.now() - startTime).toFixed(2)}ms`,
364
- },
365
- };
373
+ return new TransformResult(parseResult, rendered, {
374
+ bytesOut: RenderResult.codeLength(rendered),
375
+ ...parseResult.stats,
376
+ render: rendered.stats.total,
377
+ total: `${(performance.now() - startTime).toFixed(2)}ms`,
378
+ });
366
379
  });
367
380
  }
368
381
 
369
- export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseSync, render, resolve, transform, transformSync };
382
+ export { ModuleScopeEnumOptions, RenderResult, ResponseType, TransformResult, dirname, load, parse, parseSync, render, resolve, transform, transformSync };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@tbela99/css-parser",
3
- "description": "CSS parser, minifier and validator for node and the browser",
4
- "version": "2.1.0",
3
+ "description": "CSS parser, minifier and validator for node, deno, bun and the browser",
4
+ "version": "2.2.0",
5
5
  "exports": {
6
- ".": "./dist/src/node.js",
7
- "./node": "./dist/src/node.js",
8
- "./web": "./dist/src/web.js"
6
+ ".": "./dist/node.js",
7
+ "./node": "./dist/node.js",
8
+ "./web": "./dist/web.js"
9
9
  },
10
10
  "type": "module",
11
11
  "typings": "dist/index.d.ts",
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "scripts": {
16
16
  "doc": "typedoc --tsconfig typedoc-tsconfig.jsonc",
17
- "build": "rollup -c;./build.sh ./dist/index.d.ts 'declare interface' 'declare type'",
17
+ "build": "rollup -c;sh build.sh ./dist/index.d.ts 'declare interface' 'declare type'",
18
18
  "watch": "rollup -c -w",
19
19
  "test:watch": "web-test-runner \"./test/specs/web.spec.js\" --timeout 30000 --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --watch",
20
20
  "test": "web-test-runner \"./test/specs/web.spec.js\" --timeout=10000 --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/specs/node.spec.js\"",
@@ -68,17 +68,17 @@
68
68
  "@types/chai-as-promised": "^8.0.2",
69
69
  "@types/mocha": "^10.0.10",
70
70
  "@types/node": "^26.6.6",
71
- "@types/web": "^0.0.359",
71
+ "@types/web": "^0.0.367",
72
72
  "@typescript/typescript6": "^6.0.2",
73
- "@web/test-runner": "^1.0.0",
74
- "@web/test-runner-playwright": "^1.0.0",
73
+ "@web/test-runner": "^1.0.1",
74
+ "@web/test-runner-playwright": "^1.0.2",
75
75
  "chai": "^6.3.0",
76
76
  "chai-as-promised": "^8.0.2",
77
77
  "esno": "^4.8.0",
78
78
  "markdown-it-task-lists": "^2.1.1",
79
79
  "mocha": "^12.0.3",
80
- "playwright": "^1.64.1",
81
- "rollup": "^4.67.2",
80
+ "playwright": "^1.65.1",
81
+ "rollup": "^4.68.5",
82
82
  "rollup-plugin-dts": "^6.5.1",
83
83
  "rollup-plugin-version-injector": "^1.3.3",
84
84
  "superc8": "^12.7.0",
@@ -90,4 +90,4 @@
90
90
  "typedoc-unhoax-theme": "^0.5.3"
91
91
  },
92
92
  "sideEffects": false
93
- }
93
+ }
@@ -1,10 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 2.1.0
6
- License: MIT
7
- */
8
- var version = "2.1.0";
9
-
10
- export { version };
@@ -1,95 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 2.1.0
6
- License: MIT
7
- */
8
- import { DeclarationMemo, PropertyList } from '../../parser/declaration/list.js';
9
- import { EnumToken, EnumAstNodeStatus } from '../types.js';
10
- import { FeatureWalkMode } from './type.js';
11
-
12
- class ComputeShorthandFeature {
13
- accept = new Set([
14
- EnumToken.RuleNodeType,
15
- EnumToken.AtRuleNodeType,
16
- EnumToken.KeyframesRuleNodeType,
17
- ]);
18
- get ordering() {
19
- return 5;
20
- }
21
- get processMode() {
22
- return FeatureWalkMode.Post;
23
- }
24
- static register(options) {
25
- if (options.computeShorthand) {
26
- // @ts-ignore
27
- options.features.push(new ComputeShorthandFeature(options));
28
- }
29
- }
30
- /**
31
- *
32
- * @param arena
33
- * @param ast
34
- * @param options
35
- * @param _parent
36
- * @param context
37
- */
38
- run(arena, ast, options, _parent, context) {
39
- if (!(arena.flags[ast] & EnumAstNodeStatus.HasChildren) || arena.firstChild[ast] === 0) {
40
- return null;
41
- }
42
- // the keys of the declarations are shared by the rules of the minification
43
- const memo = context == null ? null : (context.declarations ??= new DeclarationMemo());
44
- const list = new PropertyList(arena, options, memo);
45
- let doNotSort = false;
46
- let hasDeclarations = false;
47
- if (arena.dataTypes[ast] === EnumToken.AtRuleNodeType) {
48
- switch (arena.strings[arena.name[ast]]) {
49
- case "function":
50
- case "mixin":
51
- doNotSort = true;
52
- break;
53
- }
54
- }
55
- // the declarations (with their leading comments) are moved into the property list,
56
- // the rules (with their leading comments) stay in place
57
- let child = arena.firstChild[ast];
58
- while (child !== 0) {
59
- let last = child;
60
- if (!doNotSort) {
61
- // capture comments with the next node
62
- while (arena.nextSibling[last] !== 0 && arena.dataTypes[last] === EnumToken.CommentNodeType) {
63
- last = arena.nextSibling[last];
64
- }
65
- }
66
- const next = arena.nextSibling[last];
67
- // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example
68
- if (doNotSort || arena.dataTypes[last] === EnumToken.DeclarationNodeType) {
69
- hasDeclarations = true;
70
- for (let node = child; node !== next;) {
71
- const nextNode = arena.nextSibling[node];
72
- arena.detach(node);
73
- list.addDeclaration(node);
74
- node = nextNode;
75
- }
76
- }
77
- child = next;
78
- }
79
- if (hasDeclarations) {
80
- // the declarations are inserted before the rules
81
- const first = arena.firstChild[ast];
82
- for (const node of list) {
83
- if (first === 0) {
84
- arena.appendChild(ast, node);
85
- }
86
- else {
87
- arena.insertBefore(first, node);
88
- }
89
- }
90
- }
91
- return ast;
92
- }
93
- }
94
-
95
- export { ComputeShorthandFeature };