@tbela99/css-parser 2.0.1 → 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 (128) hide show
  1. package/CHANGELOG.md +86 -1
  2. package/README.md +15 -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 +4848 -3750
  6. package/dist/index-umd-web.js +18234 -9498
  7. package/dist/index.d.ts +1251 -373
  8. package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
  9. package/dist/{src/lib → lib}/ast/arena.js +1034 -655
  10. package/dist/{src/lib → lib}/ast/cache.js +36 -31
  11. package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
  12. package/dist/{src/lib → lib}/ast/features/if.js +24 -19
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
  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 +150 -150
  21. package/dist/{src/lib → lib}/ast/math/math.js +8 -8
  22. package/dist/{src/lib → lib}/ast/minify.js +932 -354
  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 +7 -15
  37. package/dist/{src/lib → lib}/ast/walk.js +19 -19
  38. package/dist/{src/lib → lib}/fs/resolve.js +15 -15
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
  42. package/dist/lib/parser/input.js +85 -0
  43. package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
  44. package/dist/{src/lib → lib}/parser/parse.js +852 -619
  45. package/dist/lib/parser/source.js +71 -0
  46. package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
  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 +31 -13
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
  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 +67 -49
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
  64. package/dist/lib/parser/utils/selector.js +720 -0
  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 +11 -9
  68. package/dist/{src/lib → lib}/printer/render.js +536 -243
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
  77. package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
  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 +6 -4
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
  99. package/dist/{src/lib → lib}/syntax/constants.js +30 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
  101. package/dist/lib/validation/config.js +173 -0
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +1256 -579
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +92 -80
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +13 -8
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +80 -75
  113. package/package.json +13 -13
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -87
  116. package/dist/src/lib/ast/features/transform.js +0 -85
  117. package/dist/src/lib/ast/transform/compute.js +0 -358
  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/source.js +0 -127
  126. package/dist/src/lib/parser/utils/cache.js +0 -54
  127. package/dist/src/lib/parser/utils/selector.js +0 -440
  128. package/dist/src/lib/validation/config.js +0 -120
@@ -0,0 +1,148 @@
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
+ * shared decoder
10
+ */
11
+ const decoder = new TextDecoder();
12
+ /**
13
+ * Render result.
14
+ *
15
+ * The generated css is stored as UTF-8 bytes in {@link RenderResult.write}: it can be written to a file or sent over
16
+ * the network as is. {@link RenderResult.code} decodes the bytes the first time it is read, the string is kept.
17
+ *
18
+ * ```ts
19
+ * import {render} from '@tbela99/css-parser';
20
+ * import {writeFile} from 'node:fs/promises';
21
+ *
22
+ * const result = render(parseResult.arena, parseResult.ast);
23
+ *
24
+ * // bytes, no string conversion
25
+ * await writeFile('out.css', result.write);
26
+ *
27
+ * // string, decoded once
28
+ * console.log(result.code);
29
+ * ```
30
+ */
31
+ class RenderResult {
32
+ /**
33
+ * An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
34
+ * collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
35
+ *
36
+ * @internal
37
+ */
38
+ static shape = new RenderResult(new Uint8Array(0), [], { total: "0ms" });
39
+ /**
40
+ * Rendered CSS, encoded as UTF-8
41
+ */
42
+ write;
43
+ /**
44
+ * Render errors
45
+ */
46
+ errors;
47
+ /**
48
+ * Render stats
49
+ */
50
+ stats;
51
+ /**
52
+ * Source map
53
+ */
54
+ map;
55
+ /**
56
+ * decoded css
57
+ */
58
+ #code = null;
59
+ /**
60
+ * length of the decoded css: `code.length`
61
+ */
62
+ #codeLength;
63
+ /**
64
+ * @param write rendered css, encoded as UTF-8
65
+ * @param errors
66
+ * @param stats
67
+ * @param map
68
+ * @param codeLength length of the decoded css (`code.length`), computed when it is not provided
69
+ */
70
+ constructor(write, errors, stats, map, codeLength = -1) {
71
+ this.write = write;
72
+ this.errors = errors;
73
+ this.stats = stats;
74
+ this.map = map;
75
+ this.#codeLength = codeLength;
76
+ }
77
+ /**
78
+ * length of the rendered css once decoded (`result.code.length`), without decoding it
79
+ * @param result
80
+ *
81
+ * @internal
82
+ */
83
+ static codeLength(result) {
84
+ return result.#codeLength >= 0 ? result.#codeLength : result.code.length;
85
+ }
86
+ /**
87
+ * Rendered CSS. The bytes of {@link RenderResult.write} are decoded the first time the property is read
88
+ */
89
+ get code() {
90
+ if (this.#code === null) {
91
+ this.#code = decoder.decode(this.write);
92
+ }
93
+ return this.#code;
94
+ }
95
+ /**
96
+ * JSON representation: the css is serialized as a string, the bytes are not serialized
97
+ */
98
+ toJSON() {
99
+ const { write, ...json } = this;
100
+ json.code = this.code;
101
+ return json;
102
+ }
103
+ }
104
+ /**
105
+ * Transform result: the parse result and the render result
106
+ */
107
+ class TransformResult extends RenderResult {
108
+ /**
109
+ * An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
110
+ * collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
111
+ *
112
+ * @internal
113
+ */
114
+ static shape = new TransformResult({ ast: 0, arena: null, errors: [], stats: {} }, RenderResult.shape, {});
115
+ /**
116
+ * Parsed ast tree: the id of the stylesheet node in {@link TransformResult.arena}
117
+ */
118
+ ast;
119
+ /**
120
+ * arena the ast nodes are stored in
121
+ */
122
+ arena;
123
+ /**
124
+ * @param parseResult
125
+ * @param renderResult
126
+ * @param stats
127
+ */
128
+ constructor(parseResult, renderResult, stats) {
129
+ super(renderResult.write, parseResult.errors.concat(renderResult.errors), stats, renderResult.map, RenderResult.codeLength(renderResult));
130
+ this.ast = parseResult.ast;
131
+ this.arena = parseResult.arena;
132
+ // css module properties are only defined when they are set
133
+ if (parseResult.mapping !== undefined) {
134
+ this.mapping = parseResult.mapping;
135
+ }
136
+ if (parseResult.cssModuleVariables !== undefined) {
137
+ this.cssModuleVariables = parseResult.cssModuleVariables;
138
+ }
139
+ if (parseResult.importMapping !== undefined) {
140
+ this.importMapping = parseResult.importMapping;
141
+ }
142
+ if (parseResult.revMapping !== undefined) {
143
+ this.revMapping = parseResult.revMapping;
144
+ }
145
+ }
146
+ }
147
+
148
+ export { RenderResult, TransformResult };
@@ -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.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  // from https://github.com/Rich-Harris/vlq/tree/master
@@ -10,7 +10,7 @@
10
10
  const integer_to_char = {};
11
11
  const char_to_integer = {};
12
12
  let i = 0;
13
- for (const char of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=') {
13
+ for (const char of "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=") {
14
14
  char_to_integer[char] = i;
15
15
  integer_to_char[i++] = char;
16
16
  }
@@ -24,9 +24,6 @@ function decode(str) {
24
24
  let value = 0;
25
25
  for (let i = 0; i < str.length; i += 1) {
26
26
  let integer = char_to_integer[str[i]];
27
- // if (integer === undefined) {
28
- // throw new Error('Invalid character (' + str[i] + ')');
29
- // }
30
27
  const has_continuation_bit = integer & 32;
31
28
  integer &= 31;
32
29
  value += integer << shift;
@@ -57,7 +54,7 @@ function encode(value) {
57
54
  // if (typeof value === 'number') {
58
55
  // return encode_integer(value);
59
56
  // }
60
- let result = '';
57
+ let result = "";
61
58
  for (let i = 0; i < value.length; i += 1) {
62
59
  result += encode_integer(value[i]);
63
60
  }
@@ -69,7 +66,7 @@ function encode(value) {
69
66
  * @returns
70
67
  */
71
68
  function encode_integer(num) {
72
- let result = '';
69
+ let result = "";
73
70
  if (num < 0) {
74
71
  num = (-num << 1) | 1;
75
72
  }
@@ -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.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { toBase64 } from '../../parser/utils/base64.js';
@@ -96,13 +96,13 @@ class SourceMap {
96
96
  if (sourcemaps.startsWith("data:")) {
97
97
  let encoding = "";
98
98
  let offset = sourcemaps.indexOf(",") + 1;
99
- if (offset == 0) {
99
+ if (offset === 0) {
100
100
  offset = sourcemaps.lastIndexOf(";") + 1;
101
101
  }
102
102
  else {
103
103
  encoding = sourcemaps.slice(sourcemaps.lastIndexOf(";") + 1, offset - 1);
104
104
  }
105
- if (encoding == "base64") {
105
+ if (encoding === "base64") {
106
106
  sourcemaps = atob(sourcemaps.slice(offset));
107
107
  }
108
108
  else {
@@ -119,8 +119,8 @@ class SourceMap {
119
119
  .map((mapping) => mapping.split(",").map((mapping) => decode(mapping)));
120
120
  this.line = decodedMappings.length - 1;
121
121
  for (let index = 0; index < decodedMappings.length; index++) {
122
- if (decodedMappings[index].length == 0 ||
123
- (decodedMappings[index].length == 1 && decodedMappings[index][0].length == 0)) {
122
+ if (decodedMappings[index].length === 0 ||
123
+ (decodedMappings[index].length === 1 && decodedMappings[index][0].length === 0)) {
124
124
  continue;
125
125
  }
126
126
  this.map.set(index, decodedMappings[index]);
@@ -162,7 +162,7 @@ class SourceMap {
162
162
  this.line = line;
163
163
  }
164
164
  srcIndex = this.sourcesMap.indexOf(srcId);
165
- if (srcIndex == -1) {
165
+ if (srcIndex === -1) {
166
166
  throw new Error(`Source file ${srcId} not added to sourcemap`);
167
167
  }
168
168
  if (!this.map.has(line)) {
@@ -205,12 +205,12 @@ class SourceMap {
205
205
  if (segment.length === 0) {
206
206
  return [];
207
207
  }
208
- generatedCodeColumn = index == 0 ? segment[0] : segment[0] + array[0][0];
208
+ generatedCodeColumn = index === 0 ? segment[0] : segment[0] + array[0][0];
209
209
  result = [generatedCodeColumn];
210
210
  if (segment.length <= 1) {
211
211
  return result;
212
212
  }
213
- sourceFileIndex = index == 0 ? segment[1] : segment[1] + array[0][1];
213
+ sourceFileIndex = index === 0 ? segment[1] : segment[1] + array[0][1];
214
214
  sourceCodeLine += segment[2];
215
215
  sourceCodeColumn += segment[3];
216
216
  result.push(sourceFileIndex, sourceCodeLine, sourceCodeColumn);
@@ -227,7 +227,7 @@ class SourceMap {
227
227
  }
228
228
  return a[0] - b[0];
229
229
  });
230
- if (line.length == 0 || (line.length == 1 && line[0].length == 0)) {
230
+ if (line.length === 0 || (line.length === 1 && line[0].length === 0)) {
231
231
  continue;
232
232
  }
233
233
  this.reverseMap.set(i, line);
@@ -239,7 +239,7 @@ class SourceMap {
239
239
  * @param column generated column
240
240
  */
241
241
  find(line, column) {
242
- if (this.reverseMap.size == 0) {
242
+ if (this.reverseMap.size === 0) {
243
243
  this.computePositions();
244
244
  }
245
245
  if (!this.reverseMap.has(--line)) {
@@ -248,7 +248,7 @@ class SourceMap {
248
248
  column--;
249
249
  const result = [];
250
250
  for (const record of this.reverseMap.get(line)) {
251
- if (record.length == 0 || record[0] < column) {
251
+ if (record.length === 0 || record[0] < column) {
252
252
  continue;
253
253
  }
254
254
  if (record[0] > column) {
@@ -261,7 +261,7 @@ class SourceMap {
261
261
  this.sourcesContent?.[record[1]] ?? null,
262
262
  ]);
263
263
  }
264
- return result.length == 0 ? null : result;
264
+ return result.length === 0 ? null : result;
265
265
  }
266
266
  /**
267
267
  * Convert to URL encoded string
@@ -0,0 +1,252 @@
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
+ * shared encoder
10
+ */
11
+ const encoder = new TextEncoder();
12
+ /**
13
+ * strings longer than this are encoded natively ({@link TextEncoder.encodeInto} or the node.js Buffer): a loop is
14
+ * faster for very short strings. the strings are of different representations (flat, concatenated, sliced, ...): the
15
+ * loop is not specialized for one of them, it is slow for long strings
16
+ */
17
+ const NATIVE_THRESHOLD = 24;
18
+ /**
19
+ * node.js `Buffer` class, see {@link useNativeBuffer}
20
+ */
21
+ let NativeBufferClass = null;
22
+ /**
23
+ * encode the strings with the node.js `Buffer` class: faster than {@link TextEncoder.encodeInto}. the rendered bytes are
24
+ * still stored in a `Uint8Array`
25
+ * @param bufferClass node.js `Buffer`
26
+ *
27
+ * @internal
28
+ */
29
+ function useNativeBuffer(bufferClass) {
30
+ NativeBufferClass = bufferClass;
31
+ }
32
+ /**
33
+ * the node.js `Buffer` class, null when it is not used (see {@link useNativeBuffer})
34
+ *
35
+ * @internal
36
+ */
37
+ function nativeBufferClass() {
38
+ return NativeBufferClass;
39
+ }
40
+ /**
41
+ * last writer id
42
+ */
43
+ let writerId = 0;
44
+ /**
45
+ * a new writer id: a positive 31 bits integer (the ids are stored in an Int32Array, 0 is not an id)
46
+ */
47
+ function nextWriterId() {
48
+ writerId = (writerId % 0x7ffffffe) + 1;
49
+ return writerId;
50
+ }
51
+ /**
52
+ * default capacity of a writer, in bytes
53
+ */
54
+ const DEFAULT_CAPACITY = 1 << 12;
55
+ /**
56
+ * Growable buffer the rendered css is written to, encoded as UTF-8.
57
+ *
58
+ * The renderer writes the rendered css directly into the buffer: the strings and the single characters. Identical
59
+ * declarations are copied from the bytes already written ({@link copy}). The bytes written last can be discarded
60
+ * ({@link truncate}): the renderer drops the empty rules and the semicolon that ends the last declaration of a block.
61
+ * The bytes of a declaration that is copied are never discarded: a rule that contains a declaration is not empty.
62
+ *
63
+ * The capacity should be large enough for the whole output (the renderer allocates it from the input length, see
64
+ * `outputCapacity()`): the buffer is copied when it grows.
65
+ *
66
+ * @internal
67
+ */
68
+ class Writer {
69
+ /**
70
+ * An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
71
+ * collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
72
+ */
73
+ static shape = new Writer(16);
74
+ /**
75
+ * underlying buffer. only the first {@link length} bytes are meaningful. it is replaced when the writer grows
76
+ */
77
+ buffer;
78
+ /**
79
+ * unique id of the content of the writer: it changes when the writer is {@link reset}
80
+ */
81
+ id = nextWriterId();
82
+ /**
83
+ * number of bytes written. it can be decreased to drop the bytes written last
84
+ */
85
+ length = 0;
86
+ /**
87
+ * number of bytes written minus the number of UTF-16 code units of the written strings: 0 when only ascii is
88
+ * written. the length of the decoded css is {@link codeLength}
89
+ */
90
+ extra = 0;
91
+ /**
92
+ * node.js `Buffer` view of {@link buffer}, null when `Buffer` is not available
93
+ */
94
+ view;
95
+ /**
96
+ * @param capacity initial capacity in bytes
97
+ */
98
+ constructor(capacity = DEFAULT_CAPACITY) {
99
+ this.buffer = new Uint8Array(capacity > 0 ? capacity : DEFAULT_CAPACITY);
100
+ this.view =
101
+ NativeBufferClass == null ? null : NativeBufferClass.from(this.buffer.buffer, 0, this.buffer.length);
102
+ }
103
+ /**
104
+ * write a string, encoded as UTF-8
105
+ * @param str
106
+ */
107
+ write(str) {
108
+ const len = str.length;
109
+ let pos = this.length;
110
+ if (len > NATIVE_THRESHOLD) {
111
+ if (this.view !== null) {
112
+ // a UTF-16 code unit is encoded with at most 3 bytes
113
+ if (pos + len * 3 > this.buffer.length &&
114
+ pos + NativeBufferClass.byteLength(str, "utf8") > this.buffer.length) {
115
+ this.grow(pos + NativeBufferClass.byteLength(str, "utf8"));
116
+ }
117
+ const written = this.view.write(str, pos);
118
+ this.length = pos + written;
119
+ this.extra += written - len;
120
+ return;
121
+ }
122
+ this.encode(str);
123
+ return;
124
+ }
125
+ // enough room for ascii strings
126
+ if (pos + len > this.buffer.length) {
127
+ this.grow(pos + len);
128
+ }
129
+ const buffer = this.buffer;
130
+ let i = 0;
131
+ for (; i < len; i++) {
132
+ const code = str.charCodeAt(i);
133
+ if (code > 0x7f) {
134
+ break;
135
+ }
136
+ buffer[pos++] = code;
137
+ }
138
+ this.length = pos;
139
+ if (i < len) {
140
+ this.encode(i === 0 ? str : str.slice(i));
141
+ }
142
+ }
143
+ /**
144
+ * write an ascii character
145
+ * @param code character code, less than 0x80
146
+ */
147
+ writeByte(code) {
148
+ if (this.length === this.buffer.length) {
149
+ this.grow(this.length + 1);
150
+ }
151
+ this.buffer[this.length++] = code;
152
+ }
153
+ /**
154
+ * copy bytes already written: the bytes between `start` and `end` are written again at the end of the buffer
155
+ * @param start start offset
156
+ * @param end end offset
157
+ * @param extra number of bytes minus the number of UTF-16 code units of the copied bytes
158
+ */
159
+ copy(start, end, extra) {
160
+ const pos = this.length;
161
+ if (pos + end - start > this.buffer.length) {
162
+ this.grow(pos + end - start);
163
+ }
164
+ this.buffer.copyWithin(pos, start, end);
165
+ this.length = pos + end - start;
166
+ this.extra += extra;
167
+ }
168
+ /**
169
+ * length of the written css once decoded, in UTF-16 code units (`code.length`)
170
+ */
171
+ get codeLength() {
172
+ return this.length - this.extra;
173
+ }
174
+ /**
175
+ * drop the written bytes: the writer is reused. it gets a new id
176
+ */
177
+ reset() {
178
+ this.id = nextWriterId();
179
+ this.length = 0;
180
+ this.extra = 0;
181
+ }
182
+ /**
183
+ * drop the bytes written after a position
184
+ * @param length position, a previous value of {@link length}
185
+ * @param extra value of {@link extra} at this position
186
+ */
187
+ truncate(length, extra) {
188
+ this.length = length;
189
+ this.extra = extra;
190
+ }
191
+ /**
192
+ * last byte written, -1 when nothing is written
193
+ */
194
+ lastByte() {
195
+ return this.length === 0 ? -1 : this.buffer[this.length - 1];
196
+ }
197
+ /**
198
+ * make room for `size` more bytes
199
+ * @param size
200
+ */
201
+ reserve(size) {
202
+ if (this.length + size > this.buffer.length) {
203
+ this.grow(this.length + size);
204
+ }
205
+ }
206
+ /**
207
+ * the written bytes: the buffer, or `buffer.subarray(0, length)` when the buffer is larger than the written bytes
208
+ */
209
+ bytes() {
210
+ return this.length === this.buffer.length ? this.buffer : this.buffer.subarray(0, this.length);
211
+ }
212
+ /**
213
+ * encode a string with the native encoder
214
+ * @param str
215
+ */
216
+ encode(str) {
217
+ let pos = this.length;
218
+ // every code unit is read: the difference is the number of written bytes minus the length
219
+ this.extra -= str.length + pos;
220
+ while (true) {
221
+ const result = encoder.encodeInto(str, pos === 0 ? this.buffer : this.buffer.subarray(pos));
222
+ pos += result.written;
223
+ if (result.read === str.length) {
224
+ break;
225
+ }
226
+ // a UTF-16 code unit is encoded with at most 3 bytes
227
+ str = str.slice(result.read);
228
+ this.length = pos;
229
+ this.grow(pos + str.length * 3);
230
+ }
231
+ this.length = pos;
232
+ this.extra += pos;
233
+ }
234
+ /**
235
+ * make room for at least `size` bytes. the capacity is at least doubled
236
+ * @param size minimum capacity
237
+ */
238
+ grow(size) {
239
+ let capacity = this.buffer.length * 2;
240
+ if (capacity < size) {
241
+ capacity = size;
242
+ }
243
+ const buffer = new Uint8Array(capacity);
244
+ buffer.set(this.length === this.buffer.length ? this.buffer : this.buffer.subarray(0, this.length));
245
+ this.buffer = buffer;
246
+ if (NativeBufferClass != null) {
247
+ this.view = NativeBufferClass.from(buffer.buffer, 0, capacity);
248
+ }
249
+ }
250
+ }
251
+
252
+ export { DEFAULT_CAPACITY, Writer, nativeBufferClass, useNativeBuffer };
@@ -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.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { xyz2srgb } from './srgb.js';
@@ -38,7 +38,7 @@ function a98rgb2la98(r, g, b, a = null) {
38
38
  let abs = Math.abs(val);
39
39
  return sign * Math.pow(abs, 563 / 256);
40
40
  })
41
- .concat(a == null || a == 1 ? [] : [a]);
41
+ .concat(a == null || a === 1 ? [] : [a]);
42
42
  }
43
43
  function la98rgb2a98rgb(r, g, b, a = null) {
44
44
  // convert an array of linear-light a98-rgb in the range 0.0-1.0
@@ -50,7 +50,7 @@ function la98rgb2a98rgb(r, g, b, a = null) {
50
50
  let abs = Math.abs(val);
51
51
  return sign * Math.pow(abs, 256 / 563);
52
52
  })
53
- .concat(a == null || a == 1 ? [] : [a]);
53
+ .concat(a == null || a === 1 ? [] : [a]);
54
54
  }
55
55
  function la98rgb2xyz(r, g, b, a = null) {
56
56
  // convert an array of linear-light a98-rgb values to CIE XYZ
@@ -65,7 +65,7 @@ function la98rgb2xyz(r, g, b, a = null) {
65
65
  [591459 / 1989134, 6239551 / 9945670, 374412 / 4972835],
66
66
  [53769 / 1989134, 351524 / 4972835, 4929758 / 4972835],
67
67
  ];
68
- return multiplyMatrices(M, [r, g, b]).concat(a == null || a == 1 ? [] : [a]);
68
+ return multiplyMatrices(M, [r, g, b]).concat(a == null || a === 1 ? [] : [a]);
69
69
  }
70
70
  function xyz2la98rgb(x, y, z, a = null) {
71
71
  // convert XYZ to linear-light a98-rgb
@@ -74,7 +74,7 @@ function xyz2la98rgb(x, y, z, a = null) {
74
74
  [-851781 / 878810, 1648619 / 878810, 36519 / 878810],
75
75
  [16779 / 1248040, -147721 / 1248040, 1266979 / 1248040],
76
76
  ];
77
- return multiplyMatrices(M, [x, y, z]).concat(a == null || a == 1 ? [] : [a]);
77
+ return multiplyMatrices(M, [x, y, z]).concat(a == null || a === 1 ? [] : [a]);
78
78
  }
79
79
 
80
80
  export { a98rgb2srgbvalues, srgb2a98values };
@@ -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.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { ColorType, EnumToken } from '../../ast/types.js';
@@ -16,6 +16,7 @@ import { ValueWalker } from '../../ast/walk.js';
16
16
 
17
17
  /**
18
18
  * Compute alpha color
19
+ * @param arena
19
20
  * @param color
20
21
  * @param alpha
21
22
  * @returns
@@ -33,7 +34,7 @@ function alpha(arena, color, alpha) {
33
34
  // inlined: arena.walkValues(arena.tokens(alpha))
34
35
  for (const value of new ValueWalker(arena, arena.tokens(alpha))) {
35
36
  if (arena.dataTypes[value] === EnumToken.IdenTokenType) {
36
- if (equalsIgnoreCase(arena.strings[arena.stringValue[value]], "alpha")) {
37
+ if (equalsIgnoreCase(arena.resolveString(arena.stringValue[value]), "alpha")) {
37
38
  // replace the identifier with the alpha value
38
39
  if (components?.[3]) {
39
40
  arena.copyToken(value, components[3]);
@@ -43,7 +44,7 @@ function alpha(arena, color, alpha) {
43
44
  }
44
45
  // continue;
45
46
  }
46
- // else if (equalsIgnoreCase(arena.strings[arena.stringValue[value]], "none")) {
47
+ // else if (equalsIgnoreCase(arena.resolveString(arena.stringValue[value]), "none")) {
47
48
  // Object.assign(value, {
48
49
  // typ: EnumToken.NumberTokenType,
49
50
  // val: 0,
@@ -52,7 +53,7 @@ function alpha(arena, color, alpha) {
52
53
  }
53
54
  }
54
55
  const result = evaluate(arena, [alpha]);
55
- if (result.length == 1) {
56
+ if (result.length === 1) {
56
57
  alpha = result[0];
57
58
  }
58
59
  // else {
@@ -67,13 +68,13 @@ function alpha(arena, color, alpha) {
67
68
  return null;
68
69
  }
69
70
  if (arena.dataTypes[alpha] === EnumToken.IdenTokenType) {
70
- if (equalsIgnoreCase(arena.strings[arena.stringValue[alpha]], "NaN") ||
71
- equalsIgnoreCase(arena.strings[arena.stringValue[alpha]], "Infinity") ||
72
- equalsIgnoreCase(arena.strings[arena.stringValue[alpha]], "-Infinity")) {
71
+ if (equalsIgnoreCase(arena.resolveString(arena.stringValue[alpha]), "NaN") ||
72
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[alpha]), "Infinity") ||
73
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[alpha]), "-Infinity")) {
73
74
  return null;
74
75
  }
75
76
  }
76
- if (arena.colorType[color] === ColorType.COLOR_MIX || arena.strings[arena.unit[color]] === "rel") {
77
+ if (arena.colorType[color] === ColorType.COLOR_MIX || arena.resolveString(arena.unit[color]) === "rel") {
77
78
  const converted = convertColor(arena, color, getColorType(arena, color));
78
79
  if (converted == null) {
79
80
  return null;
@@ -85,13 +86,13 @@ function alpha(arena, color, alpha) {
85
86
  return null;
86
87
  }
87
88
  if (arena.dataTypes[alpha] === EnumToken.IdenTokenType) {
88
- // if (equalsIgnoreCase(arena.strings[arena.stringValue[alpha]], "alpha")) {
89
+ // if (equalsIgnoreCase(arena.resolveString(arena.stringValue[alpha]), "alpha")) {
89
90
  // alpha = components[3] ?? {
90
91
  // typ: EnumToken.NumberTokenType,
91
92
  // val: 1,
92
93
  // };
93
94
  // } else
94
- if (equalsIgnoreCase(arena.strings[arena.stringValue[alpha]], "none")) {
95
+ if (equalsIgnoreCase(arena.resolveString(arena.stringValue[alpha]), "none")) {
95
96
  arena.resetToken(alpha, EnumToken.NumberTokenType, 0);
96
97
  }
97
98
  }