@tbela99/css-parser 2.1.0 → 2.3.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 (130) hide show
  1. package/CHANGELOG.md +66 -1
  2. package/README.md +14 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/lib/ast → ast}/arena-node.js +10 -10
  5. package/dist/{src/lib/ast → ast}/arena.js +304 -139
  6. package/dist/{src/lib/ast → ast}/cache.js +40 -24
  7. package/dist/{src/lib/ast → ast}/features/calc.js +11 -9
  8. package/dist/{src/lib/ast → ast}/features/if.js +6 -6
  9. package/dist/ast/features/index.d.ts +7 -0
  10. package/dist/{src/lib/ast → ast}/features/index.js +3 -2
  11. package/dist/{src/lib/ast → ast}/features/inlinecssvariables.js +12 -10
  12. package/dist/ast/features/mixin.js +953 -0
  13. package/dist/{src/lib/ast → ast}/features/prefix.js +39 -39
  14. package/dist/ast/features/shorthand.js +129 -0
  15. package/dist/ast/features/transform.js +188 -0
  16. package/dist/{src/lib/ast → ast}/features/type.js +2 -2
  17. package/dist/{src/lib/ast → ast}/math/expression.js +24 -24
  18. package/dist/{src/lib/ast → ast}/math/math.js +2 -2
  19. package/dist/{src/lib/ast → ast}/minify.js +745 -201
  20. package/dist/ast/mixins/custom-function.js +393 -0
  21. package/dist/ast/mixins/utils.js +53 -0
  22. package/dist/ast/mixins/variables.js +678 -0
  23. package/dist/ast/strings.js +910 -0
  24. package/dist/ast/transform/compute.js +560 -0
  25. package/dist/ast/transform/matrix.js +67 -0
  26. package/dist/ast/transform/minify.js +325 -0
  27. package/dist/ast/transform/perspective.js +24 -0
  28. package/dist/{src/lib/ast → ast}/transform/rotate.js +16 -7
  29. package/dist/ast/transform/scale.js +43 -0
  30. package/dist/ast/transform/skew.js +38 -0
  31. package/dist/ast/transform/translate.js +56 -0
  32. package/dist/ast/transform/utils.js +390 -0
  33. package/dist/{src/lib/ast → ast}/types.js +2 -2
  34. package/dist/{src/lib/ast → ast}/walk.js +2 -2
  35. package/dist/{src/data → data}/properties.json.js +86 -2
  36. package/dist/{src/data → data}/validation.json.js +30 -21
  37. package/dist/{src/lib/fs → fs}/resolve.js +2 -2
  38. package/dist/index-umd-web.js +19727 -10719
  39. package/dist/index.d.ts +738 -100
  40. package/dist/{src/node.js → node.js} +70 -52
  41. package/dist/{src/lib/parser → parser}/declaration/list.js +25 -8
  42. package/dist/{src/lib/parser → parser}/declaration/map.js +15 -15
  43. package/dist/{src/lib/parser → parser}/declaration/set.js +5 -5
  44. package/dist/{src/lib/parser → parser}/input.js +18 -3
  45. package/dist/{src/lib/parser → parser}/linesmap.js +2 -2
  46. package/dist/{src/lib/parser → parser}/parse.js +444 -278
  47. package/dist/{src/lib/parser → parser}/source.js +3 -3
  48. package/dist/{src/lib/parser → parser}/tokenize.js +480 -312
  49. package/dist/{src/lib/parser → parser}/utils/angle.js +3 -3
  50. package/dist/{src/lib/parser → parser}/utils/at-rule-container.js +12 -12
  51. package/dist/{src/lib/parser → parser}/utils/at-rule-font-feature-values.js +3 -3
  52. package/dist/{src/lib/parser → parser}/utils/at-rule-generic.js +7 -7
  53. package/dist/{src/lib/parser → parser}/utils/at-rule-import.js +10 -10
  54. package/dist/{src/lib/parser → parser}/utils/at-rule-media.js +14 -14
  55. package/dist/{src/lib/parser → parser}/utils/at-rule-support.js +9 -14
  56. package/dist/{src/lib/parser → parser}/utils/at-rule-when-else.js +6 -6
  57. package/dist/{src/lib/parser → parser}/utils/at-rule.js +3 -3
  58. package/dist/{src/lib/parser → parser}/utils/base64.js +2 -2
  59. package/dist/parser/utils/cache.js +53 -0
  60. package/dist/parser/utils/canonical.js +78 -0
  61. package/dist/parser/utils/codes.js +93 -0
  62. package/dist/{src/lib/parser → parser}/utils/config.js +3 -3
  63. package/dist/{src/lib/parser → parser}/utils/declaration.js +31 -26
  64. package/dist/{src/lib/parser → parser}/utils/eq.js +2 -2
  65. package/dist/{src/lib/parser → parser}/utils/hash.js +2 -2
  66. package/dist/{src/lib/parser → parser}/utils/selector.js +339 -87
  67. package/dist/{src/lib/parser → parser}/utils/text.js +2 -2
  68. package/dist/{src/lib/parser → parser}/utils/token.js +2 -2
  69. package/dist/{src/lib/parser → parser}/utils/type.js +9 -7
  70. package/dist/{src/lib/printer/sourcemap → printer}/lib/codec.js +2 -2
  71. package/dist/{src/lib/printer → printer}/render.js +582 -153
  72. package/dist/printer/result.js +148 -0
  73. package/dist/{src/lib/printer/sourcemap → printer}/sourcemap.js +4 -3
  74. package/dist/printer/writer.js +252 -0
  75. package/dist/{src/lib/syntax → syntax}/color/a98rgb.js +2 -2
  76. package/dist/{src/lib/syntax → syntax}/color/alpha.js +10 -10
  77. package/dist/{src/lib/syntax → syntax}/color/cmyk.js +2 -2
  78. package/dist/{src/lib/syntax → syntax}/color/color-mix.js +18 -18
  79. package/dist/{src/lib/syntax → syntax}/color/color.js +18 -18
  80. package/dist/{src/lib/syntax → syntax}/color/hex.js +3 -3
  81. package/dist/{src/lib/syntax → syntax}/color/hsl.js +2 -2
  82. package/dist/{src/lib/syntax → syntax}/color/hwb.js +2 -2
  83. package/dist/{src/lib/syntax → syntax}/color/lab.js +2 -2
  84. package/dist/{src/lib/syntax → syntax}/color/lch.js +2 -2
  85. package/dist/{src/lib/syntax → syntax}/color/oklab.js +2 -2
  86. package/dist/{src/lib/syntax → syntax}/color/oklch.js +3 -3
  87. package/dist/{src/lib/syntax → syntax}/color/p3.js +2 -2
  88. package/dist/{src/lib/syntax → syntax}/color/prophotorgb.js +2 -2
  89. package/dist/{src/lib/syntax → syntax}/color/rec2020.js +2 -2
  90. package/dist/{src/lib/syntax → syntax}/color/relative-color.js +14 -14
  91. package/dist/{src/lib/syntax → syntax}/color/rgb.js +4 -4
  92. package/dist/{src/lib/syntax → syntax}/color/srgb.js +5 -5
  93. package/dist/{src/lib/syntax → syntax}/color/utils/color-type.js +4 -4
  94. package/dist/{src/lib/syntax → syntax}/color/utils/colorspace.js +10 -10
  95. package/dist/{src/lib/syntax → syntax}/color/utils/components.js +5 -5
  96. package/dist/{src/lib/syntax → syntax}/color/utils/distance.js +2 -2
  97. package/dist/{src/lib/syntax → syntax}/color/utils/make-color.js +3 -3
  98. package/dist/{src/lib/syntax → syntax}/color/utils/matrix.js +2 -2
  99. package/dist/{src/lib/syntax → syntax}/color/xyz.js +2 -2
  100. package/dist/{src/lib/syntax → syntax}/color/xyzd50.js +2 -2
  101. package/dist/{src/lib/syntax → syntax}/constants.js +27 -4
  102. package/dist/{src/lib/syntax → syntax}/syntax.js +37 -29
  103. package/dist/{src/types.js → types.js} +2 -2
  104. package/dist/{src/utils → utils}/sync.js +12 -7
  105. package/dist/validation/compiled/builtins.js +32 -0
  106. package/dist/validation/compiled/runtime.js +705 -0
  107. package/dist/validation/compiled/syntaxes.js +1620 -0
  108. package/dist/{src/lib/validation → validation}/config.js +63 -16
  109. package/dist/validation/first.js +574 -0
  110. package/dist/{src/lib/validation → validation}/match.js +376 -161
  111. package/dist/{src/lib/validation → validation}/parser/parse.js +2 -2
  112. package/dist/{src/lib/validation → validation}/parser/typedef.js +2 -2
  113. package/dist/{src/lib/validation → validation}/utils/list.js +3 -19
  114. package/dist/version.js +13 -0
  115. package/dist/{src/web.js → web.js} +68 -55
  116. package/package.json +15 -13
  117. package/dist/package.json.js +0 -10
  118. package/dist/src/lib/ast/features/shorthand.js +0 -95
  119. package/dist/src/lib/ast/features/transform.js +0 -90
  120. package/dist/src/lib/ast/transform/compute.js +0 -362
  121. package/dist/src/lib/ast/transform/matrix.js +0 -88
  122. package/dist/src/lib/ast/transform/minify.js +0 -267
  123. package/dist/src/lib/ast/transform/perspective.js +0 -16
  124. package/dist/src/lib/ast/transform/scale.js +0 -39
  125. package/dist/src/lib/ast/transform/skew.js +0 -30
  126. package/dist/src/lib/ast/transform/translate.js +0 -39
  127. package/dist/src/lib/ast/transform/utils.js +0 -287
  128. package/dist/src/lib/parser/utils/cache.js +0 -54
  129. package/dist/types.d.ts +0 -21
  130. package/dist/utils/sync.d.ts +0 -16
@@ -1,37 +1,42 @@
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.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { Readable } from 'node:stream';
9
9
  import { createReadStream } from 'node:fs';
10
10
  import { lstat, readFile } from 'node:fs/promises';
11
- import { doParse, doParseSync } from './lib/parser/parse.js';
12
- export { parseDeclarations, parseString } from './lib/parser/parse.js';
13
- import { doRender } from './lib/printer/render.js';
14
- export { renderValue } from './lib/printer/render.js';
15
- import { ModuleScopeEnumOptions } from './lib/ast/types.js';
16
- export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, WalkerEvent, WalkerOptionEnum } from './lib/ast/types.js';
17
- import { createParseInfo, Tokenizer } from './lib/parser/tokenize.js';
18
- import { isCssInput } from './lib/parser/input.js';
19
- import { resolve, matchUrl, dirname } from './lib/fs/resolve.js';
11
+ import { doParse, doParseSync } from './parser/parse.js';
12
+ export { parseDeclarations, parseString } from './parser/parse.js';
13
+ import { doRender } from './printer/render.js';
14
+ export { renderValue } from './printer/render.js';
15
+ import { TransformResult, RenderResult } from './printer/result.js';
16
+ import { useNativeBuffer } from './printer/writer.js';
17
+ import { Buffer } from 'node:buffer';
18
+ import { ModuleScopeEnumOptions } from './ast/types.js';
19
+ export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, WalkerEvent, WalkerOptionEnum } from './ast/types.js';
20
+ import { createParseInfo, Tokenizer } from './parser/tokenize.js';
21
+ import { isCssInput } from './parser/input.js';
22
+ import { resolve, matchUrl, dirname } from './fs/resolve.js';
20
23
  import { ResponseType } from './types.js';
21
24
  import { resolve as resolve$1 } from 'node:path';
22
- import { SourceFile } from './lib/parser/source.js';
25
+ import { SourceFile } from './parser/source.js';
23
26
  import { cwd } from 'node:process';
24
- import { parseResult, validateSyncArguments } from './utils/sync.js';
25
- export { minify } from './lib/ast/minify.js';
26
- export { convertColor } from './lib/syntax/color/color.js';
27
- export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
28
- export { Arena, AstArena, TokenField, arenaColumns } from './lib/ast/arena.js';
29
- export { ARENA, ARENAID, AstArenaNode } from './lib/ast/arena-node.js';
30
- export { SourceMap } from './lib/printer/sourcemap/sourcemap.js';
31
- export { FeatureWalkMode } from './lib/ast/features/type.js';
32
- export { parseSyntax } from './lib/validation/parser/parse.js';
33
- export { version } from '../package.json.js';
27
+ import { validateSyncArguments, parseResult } from './utils/sync.js';
28
+ export { minify } from './ast/minify.js';
29
+ export { convertColor } from './syntax/color/color.js';
30
+ export { isOkLabClose, okLabDistance } from './syntax/color/utils/distance.js';
31
+ export { Arena, AstArena, TokenField, arenaColumns } from './ast/arena.js';
32
+ export { ARENA, ARENAID, AstArenaNode } from './ast/arena-node.js';
33
+ export { SourceMap } from './printer/sourcemap.js';
34
+ export { FeatureWalkMode } from './ast/features/type.js';
35
+ export { parseSyntax } from './validation/parser/parse.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,14 +1,14 @@
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.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { PropertySet } from './set.js';
9
9
  import { getConfig } from '../utils/config.js';
10
10
  import { PropertyMap } from './map.js';
11
- import { EnumAstNodeStatus, EnumToken } from '../../ast/types.js';
11
+ import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
12
12
  import { getParsedSyntax } from '../../validation/config.js';
13
13
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
14
14
  import { isValidDeclarationValue } from '../../validation/match.js';
@@ -108,17 +108,24 @@ class PropertyList {
108
108
  * keys shared by the lists that use the same arena
109
109
  */
110
110
  memo;
111
+ /**
112
+ * syntaxes of the descriptors of the at-rule that contains the declarations: `result` in `@function`. the
113
+ * declarations that are not validated are validated with them
114
+ */
115
+ descriptors;
111
116
  /**
112
117
  *
113
118
  * @param arena
114
119
  * @param options
115
120
  * @param memo keys shared by the lists that use the same arena
121
+ * @param descriptors syntaxes of the descriptors of the at-rule that contains the declarations
116
122
  */
117
- constructor(arena, options = {}, memo = null) {
123
+ constructor(arena, options = {}, memo = null, descriptors = null) {
118
124
  this.arena = arena;
119
125
  this.options = options;
120
126
  this.declarations = new Map();
121
127
  this.memo = memo ?? new DeclarationMemo();
128
+ this.descriptors = descriptors;
122
129
  }
123
130
  /**
124
131
  * add a declaration
@@ -131,7 +138,7 @@ class PropertyList {
131
138
  name =
132
139
  arena.dataTypes[declaration] !== EnumToken.DeclarationNodeType
133
140
  ? null
134
- : arena.strings[arena.name[declaration]];
141
+ : arena.resolveString(arena.name[declaration]);
135
142
  // do not compute shorthand for invalid declarations
136
143
  if (name == null) {
137
144
  this.declarations.set(this.memo.key(arena, declaration), declaration);
@@ -162,7 +169,9 @@ class PropertyList {
162
169
  return this;
163
170
  }
164
171
  if (arena.flags[declaration] & EnumAstNodeStatus.Unvalidated) {
165
- syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, lowercase(name));
172
+ syntaxRules =
173
+ this.descriptors?.[lowercase(name)] ??
174
+ getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, lowercase(name));
166
175
  if (syntaxRules != null) {
167
176
  arena.flags[declaration] |=
168
177
  (isValidDeclarationValue(arena, syntaxRules, declaration, this.options)
@@ -272,6 +281,14 @@ class PropertyList {
272
281
  if (shorthand != null) {
273
282
  this.mapValues(declaration, shorthand);
274
283
  }
284
+ const existing = this.declarations.get(propertyName);
285
+ // a previous declaration with the same name takes precedence when it is !important and the new one is not
286
+ if (existing != null &&
287
+ typeof existing === "number" &&
288
+ arena.flags[existing] & EnumAstNodeStatus.ImportantDeclaration &&
289
+ (arena.flags[declaration] & EnumAstNodeStatus.ImportantDeclaration) == 0) {
290
+ return this;
291
+ }
275
292
  this.declarations.set(propertyName, declaration);
276
293
  }
277
294
  return this;
@@ -305,7 +322,7 @@ class PropertyList {
305
322
  default: {
306
323
  for (const v of set) {
307
324
  if (arena.dataTypes[v] === EnumToken.IdenTokenType) {
308
- const value = arena.strings[arena.stringValue[v]].toLowerCase();
325
+ const value = arena.resolveString(arena.stringValue[v]).toLowerCase();
309
326
  if (value === pattern || new RegExp(`(^|\|)${pattern}(\||$)`).test(value)) {
310
327
  val.push(v);
311
328
  set.delete(v);
@@ -329,7 +346,7 @@ class PropertyList {
329
346
  }
330
347
  for (const val of declarationValues) {
331
348
  if (arena.dataTypes[val] === EnumToken.IdenTokenType) {
332
- name = arena.strings[arena.stringValue[val]].toLowerCase();
349
+ name = arena.resolveString(arena.stringValue[val]).toLowerCase();
333
350
  if (mapping.mapping[name] != null) {
334
351
  const typ = EnumToken[mapping.mapping[name].typ];
335
352
  arena.resetToken(val, typ, mapping.mapping[name].val);
@@ -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.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { eqToken } from '../utils/eq.js';
@@ -41,7 +41,7 @@ function configToken(arena, config) {
41
41
  function matchConfigToken(arena, token, config) {
42
42
  return (config != null &&
43
43
  arena.dataTypes[token] === configType(config) &&
44
- (config.val == null || arena.strings[arena.stringValue[token]] === config.val));
44
+ (config.val == null || arena.resolveString(arena.stringValue[token]) === config.val));
45
45
  }
46
46
  /**
47
47
  * map of declarations that can be merged into a single shorthand declaration.
@@ -83,7 +83,7 @@ class PropertyMap {
83
83
  */
84
84
  add(declaration) {
85
85
  const arena = this.arena;
86
- const nam = arena.strings[arena.name[declaration]];
86
+ const nam = arena.resolveString(arena.name[declaration]);
87
87
  if (nam === this.config.shorthand) {
88
88
  this.declarations.clear();
89
89
  this.declarations.set(nam, declaration);
@@ -270,7 +270,7 @@ class PropertyMap {
270
270
  else {
271
271
  // a property set has no name
272
272
  key =
273
- value instanceof PropertySet ? "undefined" : arena.strings[arena.name[value]];
273
+ value instanceof PropertySet ? "undefined" : arena.resolveString(arena.name[value]);
274
274
  mapped[key] = value;
275
275
  }
276
276
  }
@@ -413,7 +413,7 @@ class PropertyMap {
413
413
  if (declaration instanceof PropertySet || declaration instanceof PropertyMap) {
414
414
  continue;
415
415
  }
416
- const nam = arena.strings[arena.name[declaration]];
416
+ const nam = arena.resolveString(arena.name[declaration]);
417
417
  const config = nam === this.config.shorthand ? this.config : (propertiesMapping[nam] ?? this.config);
418
418
  if (!("mapping" in config)) {
419
419
  continue;
@@ -474,7 +474,7 @@ class PropertyMap {
474
474
  props.separator != null &&
475
475
  configType(props.separator) === valType &&
476
476
  (valType === EnumToken.CommaTokenType ||
477
- props.separator.val === arena.strings[arena.stringValue[val]])) {
477
+ props.separator.val === arena.resolveString(arena.stringValue[val]))) {
478
478
  continue;
479
479
  }
480
480
  match = matchType(arena, val, curr[1]);
@@ -621,7 +621,7 @@ class PropertyMap {
621
621
  EnumToken.IdenTokenType,
622
622
  ].includes(arena.dataTypes[v]) ||
623
623
  (arena.dataTypes[v] === EnumToken.IdenTokenType &&
624
- !propertiesMapping[curr[0]].default.includes(arena.strings[arena.stringValue[v]]))) {
624
+ !propertiesMapping[curr[0]].default.includes(arena.resolveString(arena.stringValue[v])))) {
625
625
  doFilterDefault = false;
626
626
  break;
627
627
  }
@@ -635,7 +635,7 @@ class PropertyMap {
635
635
  }
636
636
  return (!doFilterDefault ||
637
637
  !(arena.dataTypes[val] === EnumToken.IdenTokenType &&
638
- props.default.includes(arena.strings[arena.stringValue[val]])));
638
+ props.default.includes(arena.resolveString(arena.stringValue[val]))));
639
639
  });
640
640
  if (filtered.length > 0 ||
641
641
  !(this.requiredCount == requiredCount && propertiesMapping[curr[0]].required)) {
@@ -651,15 +651,15 @@ class PropertyMap {
651
651
  while (i--) {
652
652
  if (arena.dataTypes[values[i]] === EnumToken.IdenTokenType &&
653
653
  // @ts-expect-error
654
- arena.strings[arena.stringValue[values[i]]] in props.mapping) {
655
- // inlined: this.parse(props.mapping[arena.strings[arena.stringValue[values[i]]]])
654
+ arena.resolveString(arena.stringValue[values[i]]) in props.mapping) {
655
+ // inlined: this.parse(props.mapping[arena.resolveString(arena.stringValue[values[i]])])
656
656
  values.splice(i, 1, ...(this.memo == null
657
657
  ? parseString(this.arena,
658
658
  // @ts-ignore
659
- props.mapping[arena.strings[arena.stringValue[values[i]]]])
659
+ props.mapping[arena.resolveString(arena.stringValue[values[i]])])
660
660
  : this.memo.parse(this.arena,
661
661
  // @ts-ignore
662
- props.mapping[arena.strings[arena.stringValue[values[i]]]])));
662
+ props.mapping[arena.resolveString(arena.stringValue[values[i]])])));
663
663
  }
664
664
  }
665
665
  }
@@ -722,8 +722,8 @@ class PropertyMap {
722
722
  }
723
723
  if (values.length === 1 &&
724
724
  arena.stringValue[values[0]] !== 0 &&
725
- this.config.default.includes(arena.strings[arena.stringValue[values[0]]].toLowerCase()) &&
726
- this.config.default[0] !== arena.strings[arena.stringValue[values[0]]].toLowerCase()) {
725
+ this.config.default.includes(arena.resolveString(arena.stringValue[values[0]]).toLowerCase()) &&
726
+ this.config.default[0] !== arena.resolveString(arena.stringValue[values[0]]).toLowerCase()) {
727
727
  // inlined: this.parse(this.config.default[0])
728
728
  values =
729
729
  this.memo == null
@@ -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.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { numericTokenTypes } from '../../ast/arena.js';
@@ -57,7 +57,7 @@ class PropertySet {
57
57
  }
58
58
  add(declaration) {
59
59
  const arena = this.arena;
60
- const nam = arena.strings[arena.name[declaration]];
60
+ const nam = arena.resolveString(arena.name[declaration]);
61
61
  if (nam === this.config.shorthand) {
62
62
  this.declarations = new Map();
63
63
  }
@@ -86,7 +86,7 @@ class PropertySet {
86
86
  }
87
87
  if (typ !== EnumToken.WhitespaceTokenType && typ !== EnumToken.CommentTokenType) {
88
88
  if (typ === EnumToken.IdenTokenType &&
89
- this.config.keywords.includes(arena.strings[arena.stringValue[token]])) {
89
+ this.config.keywords.includes(arena.resolveString(arena.stringValue[token]))) {
90
90
  if (tokens.length === 0) {
91
91
  tokens.push([]);
92
92
  current++;
@@ -94,7 +94,7 @@ class PropertySet {
94
94
  tokens[current].push(token);
95
95
  }
96
96
  if (typ === EnumToken.LiteralTokenType &&
97
- arena.strings[arena.stringValue[token]] === this.config.separator?.val) {
97
+ arena.resolveString(arena.stringValue[token]) === this.config.separator?.val) {
98
98
  tokens.push([]);
99
99
  current++;
100
100
  continue;
@@ -1,11 +1,17 @@
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.3.0
6
6
  License: MIT
7
7
  */
8
+ import { nativeBufferClass } from '../printer/writer.js';
9
+
8
10
  const encoder = new TextEncoder();
11
+ /**
12
+ * the code units of a Uint16Array are stored in the platform byte order
13
+ */
14
+ const littleEndian = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
9
15
  const decoder = new TextDecoder();
10
16
  const nonAscii = /[^\x00-\x7f]/;
11
17
  const empty = new Uint8Array(0);
@@ -39,6 +45,13 @@ function bytesOf(input) {
39
45
  * @param text
40
46
  */
41
47
  function codeUnitsOf(text) {
48
+ const native = nativeBufferClass();
49
+ // node.js: the code units are written by the native encoder (little endian platforms)
50
+ if (native !== null && littleEndian) {
51
+ const buffer = native.allocUnsafeSlow(text.length * 2);
52
+ buffer.write(text, 0, text.length * 2, "utf16le");
53
+ return new Uint16Array(buffer.buffer, buffer.byteOffset, text.length);
54
+ }
42
55
  const codes = new Uint16Array(text.length);
43
56
  for (let i = 0; i < text.length; i++) {
44
57
  codes[i] = text.charCodeAt(i);
@@ -56,7 +69,9 @@ function toCodeUnits(input) {
56
69
  return { codes: empty, text: "" };
57
70
  }
58
71
  if (typeof input === "string") {
59
- return { codes: nonAscii.test(input) ? codeUnitsOf(input) : encoder.encode(input), text: input };
72
+ // an ascii string has a byte for each code unit: the encoded string is the code units
73
+ const bytes = encoder.encode(input);
74
+ return { codes: bytes.length === input.length ? bytes : codeUnitsOf(input), text: input };
60
75
  }
61
76
  const bytes = bytesOf(input);
62
77
  const text = decoder.decode(bytes);
@@ -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.3.0
6
6
  License: MIT
7
7
  */
8
8
  /**