@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
@@ -1,15 +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.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { TokenField } from '../../ast/arena.js';
9
9
  import { EnumToken } from '../../ast/types.js';
10
10
  import { evaluate } from '../../ast/math/expression.js';
11
11
  import { gcd } from '../../ast/math/math.js';
12
- import { tokensfuncDefMap, mediaTypes, mFLT, mFGT } from '../../syntax/constants.js';
12
+ import { isFunctionDefType, mediaTypes, tokensfuncDefMap, mFLT, mFGT } from '../../syntax/constants.js';
13
13
  import { trimArray, matchAllSyntaxes, createValidationContext, getMFInfo, isMFValue } from '../../validation/match.js';
14
14
  import { ValidationSyntaxGroupEnum, MediaFeatureType } from '../../validation/parser/typedef.js';
15
15
  import { getParsedSyntax } from '../../validation/config.js';
@@ -32,7 +32,7 @@ function parseMediaqueryList(arena, stream, options) {
32
32
  continue;
33
33
  }
34
34
  parts[parts.length - 1].push(token);
35
- if (type === EnumToken.StartParensTokenType || tokensfuncDefMap.has(type)) {
35
+ if (type === EnumToken.StartParensTokenType || isFunctionDefType(type)) {
36
36
  matchCount++;
37
37
  }
38
38
  else if (type === EnumToken.EndParensTokenType && matchCount > 0) {
@@ -47,17 +47,17 @@ function parseMediaqueryList(arena, stream, options) {
47
47
  const scopes = [currentScope];
48
48
  const stack = [];
49
49
  const tokens = [];
50
- let expectAndOrComma = false;
51
50
  i = 0;
52
51
  while (i < stream.length &&
53
- (arena.dataTypes[stream[i]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[i]] === EnumToken.CommentTokenType)) {
52
+ (arena.dataTypes[stream[i]] === EnumToken.WhitespaceTokenType ||
53
+ arena.dataTypes[stream[i]] === EnumToken.CommentTokenType)) {
54
54
  tokens.push(stream[i]);
55
55
  i++;
56
56
  }
57
57
  // [not | only] <media-type> [ and <media-condition-without-or> ]?
58
58
  if (i < stream.length) {
59
59
  if (arena.dataTypes[stream[i]] === EnumToken.IdenTokenType) {
60
- const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
60
+ const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
61
61
  if (mediaTypes.includes(val)) {
62
62
  tokens.push(stream[i]);
63
63
  i++;
@@ -69,7 +69,10 @@ function parseMediaqueryList(arena, stream, options) {
69
69
  message: `expecting '<media-type>'`,
70
70
  node: stream[i],
71
71
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
72
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
72
+ location: [
73
+ options.source.file,
74
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i])),
75
+ ],
73
76
  });
74
77
  }
75
78
  }
@@ -80,22 +83,21 @@ function parseMediaqueryList(arena, stream, options) {
80
83
  message: `expecting '('`,
81
84
  node: stream[i],
82
85
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
83
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
86
+ location: [
87
+ options.source.file,
88
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i])),
89
+ ],
84
90
  });
85
91
  }
86
92
  }
87
93
  for (; i < stream.length; i++) {
88
94
  tokens.push(stream[i]);
89
- if (arena.dataTypes[stream[i]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[i]] === EnumToken.CommentTokenType) {
95
+ if (arena.dataTypes[stream[i]] === EnumToken.WhitespaceTokenType ||
96
+ arena.dataTypes[stream[i]] === EnumToken.CommentTokenType) {
90
97
  continue;
91
98
  }
92
- if (expectAndOrComma) {
93
- if (arena.dataTypes[stream[i]] === EnumToken.IdenTokenType) {
94
- arena.strings[arena.stringValue[stream[i]]].toLowerCase();
95
- }
96
- expectAndOrComma = false;
97
- }
98
- if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType || tokensfuncDefMap.has(arena.dataTypes[stream[i]])) {
99
+ if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType ||
100
+ isFunctionDefType(arena.dataTypes[stream[i]])) {
99
101
  scopes.push((currentScope = new Set()));
100
102
  stack.push(stream[i]);
101
103
  continue;
@@ -111,11 +113,12 @@ function parseMediaqueryList(arena, stream, options) {
111
113
  break;
112
114
  case EnumToken.IdenTokenType:
113
115
  {
114
- const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
116
+ const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
115
117
  if (val === "and" || val === "or") {
116
- arena.dataTypes[stream[i]] = val === "and" ? EnumToken.AndTokenType : EnumToken.OrTokenType;
118
+ arena.dataTypes[stream[i]] =
119
+ val === "and" ? EnumToken.AndTokenType : EnumToken.OrTokenType;
117
120
  if (val === "or" &&
118
- scopes.length == 1 &&
121
+ scopes.length === 1 &&
119
122
  scopes[0].size > 0 &&
120
123
  !scopes[0].has(EnumToken.OrTokenType)) {
121
124
  success = false;
@@ -124,7 +127,10 @@ function parseMediaqueryList(arena, stream, options) {
124
127
  node: stream[i],
125
128
  message: `<or> is not allowed outside of parentheses`,
126
129
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
127
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
130
+ location: [
131
+ options.source.file,
132
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i])),
133
+ ],
128
134
  });
129
135
  break;
130
136
  }
@@ -135,7 +141,10 @@ function parseMediaqueryList(arena, stream, options) {
135
141
  node: stream[i],
136
142
  message: `cannot mix <and> and <or> at the same level`,
137
143
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
138
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
144
+ location: [
145
+ options.source.file,
146
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i])),
147
+ ],
139
148
  });
140
149
  }
141
150
  currentScope.add(arena.dataTypes[stream[i]]);
@@ -144,13 +153,13 @@ function parseMediaqueryList(arena, stream, options) {
144
153
  }
145
154
  break;
146
155
  case EnumToken.EndParensTokenType:
147
- if (stack.length > 0 && tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]])) {
156
+ if (stack.length > 0 && isFunctionDefType(arena.dataTypes[stack[stack.length - 1]])) {
148
157
  const index = tokens.indexOf(stack[stack.length - 1]);
149
158
  arena.spanLocation(tokens[index], tokens[index], stream[i]);
150
159
  arena.dataTypes[tokens[index]] = tokensfuncDefMap.get(arena.dataTypes[tokens[index]]);
151
160
  arena.setTokenList(tokens[index], trimArray(arena, tokens.slice(index + 1, tokens.length - 1)));
152
161
  tokens.length = index + 1;
153
- const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, arena.strings[arena.stringValue[tokens[index]]] + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
162
+ const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, arena.resolveString(arena.stringValue[tokens[index]]) + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
154
163
  stack.pop();
155
164
  scopes.pop();
156
165
  currentScope = scopes.at(-1);
@@ -178,19 +187,20 @@ function parseMediaqueryList(arena, stream, options) {
178
187
  const left = trimArray(arena, tokens.slice(index3 + 1, index2));
179
188
  const right = trimArray(arena, tokens.slice(index + 1, tokens.length - 1));
180
189
  const names = trimArray(arena, tokens.slice(index2 + 1, index));
181
- const filteredNames = names.filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType && arena.dataTypes[n] !== EnumToken.CommentTokenType);
182
- const name = arena.strings[arena.stringValue[filteredNames[0]]];
190
+ const filteredNames = names.filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType &&
191
+ arena.dataTypes[n] !== EnumToken.CommentTokenType);
192
+ const name = arena.resolveString(arena.stringValue[filteredNames[0]]);
183
193
  const mfInfo = getMFInfo(name);
184
194
  for (const val of [left, right]) {
185
195
  if (options.computeCalcExpression) {
186
196
  if (mfInfo == null ||
187
- (mfInfo.type != MediaFeatureType.KeywordType &&
188
- mfInfo.type != MediaFeatureType.StringType)) {
197
+ (mfInfo.type !== MediaFeatureType.KeywordType &&
198
+ mfInfo.type !== MediaFeatureType.StringType)) {
189
199
  for (let l = 0; l < val.length; l++) {
190
200
  if (arena.dataTypes[val[l]] === EnumToken.MathFunctionTokenType &&
191
- arena.strings[arena.stringValue[val[l]]] === "calc") {
201
+ arena.resolveString(arena.stringValue[val[l]]) === "calc") {
192
202
  const value = evaluate(arena, [val[l]]);
193
- if (value.length == 1) {
203
+ if (value.length === 1) {
194
204
  arena.copyLocation(value[0], val[l]);
195
205
  val[l] = value[0];
196
206
  }
@@ -225,7 +235,9 @@ function parseMediaqueryList(arena, stream, options) {
225
235
  // inlined: arena.setField(rangeQuery, TokenField.Op, prevToken)
226
236
  arena.setTokenList(arena.fieldList(rangeQuery, TokenField.Op), [prevToken]);
227
237
  // inlined: arena.setField(rangeQuery, TokenField.Op2, stack[stack.length - 1])
228
- arena.setTokenList(arena.fieldList(rangeQuery, TokenField.Op2), [stack[stack.length - 1]]);
238
+ arena.setTokenList(arena.fieldList(rangeQuery, TokenField.Op2), [
239
+ stack[stack.length - 1],
240
+ ]);
229
241
  // inlined: arena.setField(rangeQuery, TokenField.R, ...right)
230
242
  arena.setTokenList(arena.fieldList(rangeQuery, TokenField.R), right);
231
243
  if (left.length > 0 && right.length > 0) {
@@ -245,19 +257,20 @@ function parseMediaqueryList(arena, stream, options) {
245
257
  const index3 = tokens.indexOf(stack.at(-2));
246
258
  let names = trimArray(arena, tokens.slice(index3 + 1, index2));
247
259
  let values = trimArray(arena, tokens.slice(index2 + 1, tokens.length - 1));
248
- const filteredNames = (names).filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType && arena.dataTypes[n] !== EnumToken.CommentTokenType);
249
- const name = arena.strings[arena.stringValue[filteredNames[0]]];
260
+ const filteredNames = (names).filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType &&
261
+ arena.dataTypes[n] !== EnumToken.CommentTokenType);
262
+ const name = arena.resolveString(arena.stringValue[filteredNames[0]]);
250
263
  const mfInfo = getMFInfo(name);
251
264
  if (options.computeCalcExpression) {
252
265
  if (mfInfo == null ||
253
- (mfInfo.type != MediaFeatureType.KeywordType &&
254
- mfInfo.type != MediaFeatureType.StringType)) {
266
+ (mfInfo.type !== MediaFeatureType.KeywordType &&
267
+ mfInfo.type !== MediaFeatureType.StringType)) {
255
268
  const val = values;
256
269
  for (let l = 0; l < val.length; l++) {
257
270
  if (arena.dataTypes[val[l]] === EnumToken.MathFunctionTokenType &&
258
- arena.strings[arena.stringValue[val[l]]] === "calc") {
271
+ arena.resolveString(arena.stringValue[val[l]]) === "calc") {
259
272
  const value = evaluate(arena, [val[l]]);
260
- if (value.length == 1) {
273
+ if (value.length === 1) {
261
274
  arena.copyLocation(value[0], val[l]);
262
275
  val[l] = value[0];
263
276
  }
@@ -275,14 +288,18 @@ function parseMediaqueryList(arena, stream, options) {
275
288
  action: "drop",
276
289
  node: arr[0],
277
290
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(arr[0]))
278
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(arr[0]))],
291
+ location: [
292
+ options.source.file,
293
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(arr[0])),
294
+ ],
279
295
  message: `${mfValue.isValueAllowed === false ? "invalid <mf-name>" : "expected <mf-value>"}`,
280
296
  });
281
297
  break;
282
298
  }
283
299
  if (mfInfo?.type === MediaFeatureType.RatioType) {
284
300
  const val = values;
285
- const filteredValues = val.filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType && arena.dataTypes[n] !== EnumToken.CommentTokenType);
301
+ const filteredValues = val.filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType &&
302
+ arena.dataTypes[n] !== EnumToken.CommentTokenType);
286
303
  if (filteredValues.length === 3) {
287
304
  if (options.computeCalcExpression) {
288
305
  const div = gcd(arena.numericValue[filteredValues[0]], arena.numericValue[filteredValues[2]]);
@@ -327,7 +344,10 @@ function parseMediaqueryList(arena, stream, options) {
327
344
  action: "drop",
328
345
  node: stream[i],
329
346
  // inlined: options.source!.getSourceLocation(arena.getSpanStart(stream[i]))
330
- location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]))],
347
+ location: [
348
+ options.source.file,
349
+ ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i])),
350
+ ],
331
351
  message: `unmatched ')'`,
332
352
  });
333
353
  break;
@@ -371,35 +391,35 @@ function parseMediaqueryList(arena, stream, options) {
371
391
  // media range query
372
392
  if (options.minify && arena.dataTypes[op] === EnumToken.AndTokenType) {
373
393
  const leftCondition = left.length === 1 &&
374
- arena.dataTypes[left[0]] == EnumToken.ParensTokenType &&
375
- arena.tokenCount(left[0]) == 1
394
+ arena.dataTypes[left[0]] === EnumToken.ParensTokenType &&
395
+ arena.tokenCount(left[0]) === 1
376
396
  ? arena.firstToken[left[0]]
377
397
  : null;
378
398
  const rightCondition = right.length === 1 &&
379
- arena.dataTypes[right[0]] == EnumToken.ParensTokenType &&
380
- arena.tokenCount(right[0]) == 1
399
+ arena.dataTypes[right[0]] === EnumToken.ParensTokenType &&
400
+ arena.tokenCount(right[0]) === 1
381
401
  ? arena.firstToken[right[0]]
382
402
  : null;
383
403
  if (leftCondition != null &&
384
404
  rightCondition != null &&
385
- arena.dataTypes[leftCondition] == EnumToken.MediaQueryConditionTokenType &&
386
- arena.dataTypes[rightCondition] == EnumToken.MediaQueryConditionTokenType) {
405
+ arena.dataTypes[leftCondition] === EnumToken.MediaQueryConditionTokenType &&
406
+ arena.dataTypes[rightCondition] === EnumToken.MediaQueryConditionTokenType) {
387
407
  const leftNames = arena.fieldTokens(leftCondition, TokenField.L);
388
408
  const rightNames = arena.fieldTokens(rightCondition, TokenField.L);
389
409
  const leftOp = arena.field(leftCondition, TokenField.Op);
390
410
  const rightOp = arena.field(rightCondition, TokenField.Op);
391
- if (leftNames.length == 1 &&
392
- arena.dataTypes[leftNames[0]] == EnumToken.IdenTokenType &&
393
- (arena.dataTypes[leftOp] == EnumToken.GtTokenType ||
394
- arena.dataTypes[leftOp] == EnumToken.GteTokenType) &&
395
- rightNames.length == 1 &&
396
- arena.dataTypes[rightNames[0]] == EnumToken.IdenTokenType &&
397
- (arena.dataTypes[rightOp] == EnumToken.LtTokenType ||
398
- arena.dataTypes[rightOp] == EnumToken.LteTokenType) &&
399
- equalsIgnoreCase(arena.strings[arena.stringValue[leftNames[0]]], arena.strings[arena.stringValue[rightNames[0]]])) {
411
+ if (leftNames.length === 1 &&
412
+ arena.dataTypes[leftNames[0]] === EnumToken.IdenTokenType &&
413
+ (arena.dataTypes[leftOp] === EnumToken.GtTokenType ||
414
+ arena.dataTypes[leftOp] === EnumToken.GteTokenType) &&
415
+ rightNames.length === 1 &&
416
+ arena.dataTypes[rightNames[0]] === EnumToken.IdenTokenType &&
417
+ (arena.dataTypes[rightOp] === EnumToken.LtTokenType ||
418
+ arena.dataTypes[rightOp] === EnumToken.LteTokenType) &&
419
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[leftNames[0]]), arena.resolveString(arena.stringValue[rightNames[0]]))) {
400
420
  const rangeQuery = arena.createToken(EnumToken.MediaRangeQueryTokenType);
401
421
  arena.dataTypes[leftOp] =
402
- arena.dataTypes[leftOp] == EnumToken.GteTokenType
422
+ arena.dataTypes[leftOp] === EnumToken.GteTokenType
403
423
  ? EnumToken.LteTokenType
404
424
  : EnumToken.LtTokenType;
405
425
  // inlined: arena.setField(rangeQuery, TokenField.L, ...arena.fieldTokens(leftCondition, TokenField.R))
@@ -421,7 +441,6 @@ function parseMediaqueryList(arena, stream, options) {
421
441
  }
422
442
  }
423
443
  }
424
- expectAndOrComma = true;
425
444
  }
426
445
  }
427
446
  break;
@@ -443,7 +462,7 @@ function parseMediaqueryList(arena, stream, options) {
443
462
  stream.length = 0;
444
463
  let hasPart = false;
445
464
  for (const part of parts) {
446
- if (part.length == 0) {
465
+ if (part.length === 0) {
447
466
  continue;
448
467
  }
449
468
  if (hasPart) {
@@ -1,12 +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.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
9
- import { pseudoElements, tokensfuncDefMap } from '../../syntax/constants.js';
9
+ import { pseudoElements, isFunctionDefType, tokensfuncDefMap } from '../../syntax/constants.js';
10
10
  import { getSyntaxConfig, getParsedSyntax } from '../../validation/config.js';
11
11
  import { trimArray, matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
12
12
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
@@ -33,21 +33,21 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
33
33
  ]);
34
34
  for (; i < stream.length; i++) {
35
35
  tokens.push(stream[i]);
36
- if (arena.dataTypes[stream[i]] == EnumToken.ColonTokenType) {
37
- if (arena.dataTypes[stream[i + 1]] == EnumToken.IdenTokenType) {
38
- const val = arena.strings[arena.stringValue[stream[i + 1]]];
36
+ if (arena.dataTypes[stream[i]] === EnumToken.ColonTokenType) {
37
+ if (arena.dataTypes[stream[i + 1]] === EnumToken.IdenTokenType) {
38
+ const val = arena.resolveString(arena.stringValue[stream[i + 1]]);
39
39
  arena.dataTypes[stream[i]] = pseudoElements.includes(val)
40
40
  ? EnumToken.PseudoElementTokenType
41
41
  : EnumToken.PseudoClassTokenType;
42
42
  // inlined: arena.setStr(stream[i], ":" + val)
43
- arena.stringValue[stream[i]] = arena.internString((":" + val));
43
+ arena.stringValue[stream[i]] = arena.internString(":" + val);
44
44
  // inlined: arena.setSpanEnd(stream[i], arena.getSpanEnd(stream[i + 1]))
45
45
  arena.setSpan(stream[i], arena.getSourceId(stream[i]), arena.getSpanStart(stream[i]), arena.getSpanEnd(stream[i + 1]));
46
46
  stream.splice(i + 1, 1);
47
47
  continue;
48
48
  }
49
- if (arena.dataTypes[stream[i + 1]] == EnumToken.FunctionTokenDefType) {
50
- val = ":" + arena.strings[arena.stringValue[stream[i + 1]]];
49
+ if (arena.dataTypes[stream[i + 1]] === EnumToken.FunctionTokenDefType) {
50
+ val = ":" + arena.resolveString(arena.stringValue[stream[i + 1]]);
51
51
  arena.dataTypes[stream[i]] =
52
52
  val + "()" in getSyntaxConfig().selectors
53
53
  ? EnumToken.PseudoClassFunctionTokenDefType
@@ -74,13 +74,15 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
74
74
  if (expectAndOr) {
75
75
  let k = i;
76
76
  while (k < stream.length &&
77
- (arena.dataTypes[stream[k]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[k]] === EnumToken.CommentTokenType)) {
77
+ (arena.dataTypes[stream[k]] === EnumToken.WhitespaceTokenType ||
78
+ arena.dataTypes[stream[k]] === EnumToken.CommentTokenType)) {
78
79
  tokens.push(stream[k]);
79
80
  k++;
80
81
  }
81
82
  expectAndOr = false;
82
83
  }
83
- if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType || tokensfuncDefMap.has(arena.dataTypes[stream[i]])) {
84
+ if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType ||
85
+ isFunctionDefType(arena.dataTypes[stream[i]])) {
84
86
  scopes.push((scope = new Set()));
85
87
  stack.push(stream[i]);
86
88
  continue;
@@ -112,7 +114,7 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
112
114
  scope = scopes[scopes.length - 1];
113
115
  expectAndOr = true;
114
116
  }
115
- else if (stack.length > 0 && tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]])) {
117
+ else if (stack.length > 0 && isFunctionDefType(arena.dataTypes[stack[stack.length - 1]])) {
116
118
  const func = stack[stack.length - 1];
117
119
  const index = tokens.indexOf(func);
118
120
  arena.dataTypes[func] = tokensfuncDefMap.get(arena.dataTypes[func]);
@@ -120,7 +122,7 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
120
122
  arena.spanLocation(func, func, stream[i]);
121
123
  if (arena.dataTypes[tokens[index]] === EnumToken.PseudoClassFuncTokenType) {
122
124
  // not a declaration
123
- const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, arena.strings[arena.stringValue[tokens[index]]] + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
125
+ const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, arena.resolveString(arena.stringValue[tokens[index]]) + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
124
126
  if (!result.success) {
125
127
  return {
126
128
  success: false,
@@ -131,11 +133,6 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
131
133
  tokens.pop();
132
134
  break;
133
135
  }
134
- //
135
- let k = stack.length - 1;
136
- while (tokensfuncDefMap.has(arena.dataTypes[stack[k]])) {
137
- k--;
138
- }
139
136
  stack.pop();
140
137
  tokens.pop();
141
138
  scopes.pop();
@@ -155,7 +152,8 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
155
152
  stack.pop();
156
153
  }
157
154
  // inlined: arena.typeOf(stack.at(-1))
158
- if (arena.dataTypes[stack.at(-1)] === EnumToken.AndTokenType || arena.dataTypes[stack.at(-1)] === EnumToken.OrTokenType) {
155
+ if (arena.dataTypes[stack.at(-1)] === EnumToken.AndTokenType ||
156
+ arena.dataTypes[stack.at(-1)] === EnumToken.OrTokenType) {
159
157
  const index = tokens.indexOf(stack.at(-1));
160
158
  const index2 = stack.length > 1 ? tokens.indexOf(stack.at(-2)) + 1 : 0;
161
159
  const op = stack[stack.length - 1];
@@ -179,7 +177,7 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
179
177
  break;
180
178
  case EnumToken.IdenTokenType:
181
179
  {
182
- const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
180
+ const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
183
181
  if ("not" === val) {
184
182
  stack.push(stream[i]);
185
183
  arena.dataTypes[stream[i]] = EnumToken.NotTokenType;
@@ -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.0.1
5
+ Version: 2.2.0
6
6
  License: MIT
7
7
  */
8
8
  import { TokenField } from '../../ast/arena.js';
9
9
  import { EnumToken } from '../../ast/types.js';
10
10
  import { trimArray } from '../../validation/match.js';
11
- import { tokensfuncDefMap } from '../../syntax/constants.js';
11
+ import { tokensfuncDefMap, isFunctionDefType } from '../../syntax/constants.js';
12
12
  import { parseMediaqueryList } from './at-rule-media.js';
13
13
  import { parseAtRuleSupportSyntax } from './at-rule-support.js';
14
14
 
@@ -26,7 +26,8 @@ function matchAtRuleWhenElseSyntax(arena, stream, context, options = {}) {
26
26
  if (expectAndOr) {
27
27
  let k = i;
28
28
  while (k < stream.length &&
29
- (arena.dataTypes[stream[k]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[k]] === EnumToken.CommentTokenType)) {
29
+ (arena.dataTypes[stream[k]] === EnumToken.WhitespaceTokenType ||
30
+ arena.dataTypes[stream[k]] === EnumToken.CommentTokenType)) {
30
31
  tokens.push(stream[k]);
31
32
  k++;
32
33
  }
@@ -35,7 +36,7 @@ function matchAtRuleWhenElseSyntax(arena, stream, context, options = {}) {
35
36
  switch (arena.dataTypes[stream[i]]) {
36
37
  case EnumToken.IdenTokenType:
37
38
  {
38
- const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
39
+ const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
39
40
  if ("and" === val || "or" === val) {
40
41
  arena.dataTypes[stream[i]] = "or" === val ? EnumToken.OrTokenType : EnumToken.AndTokenType;
41
42
  // scope.add(arena.dataTypes[stream[i]]);
@@ -57,12 +58,12 @@ function matchAtRuleWhenElseSyntax(arena, stream, context, options = {}) {
57
58
  }
58
59
  }
59
60
  else if (arena.dataTypes[stream[j]] === EnumToken.StartParensTokenType ||
60
- tokensfuncDefMap.has(arena.dataTypes[stream[j]])) {
61
+ isFunctionDefType(arena.dataTypes[stream[j]])) {
61
62
  matchCount++;
62
63
  }
63
64
  }
64
65
  const slice = stream.slice(i, j + 1);
65
- const funcName = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
66
+ const funcName = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
66
67
  // the function token may be converted by the sub parsers
67
68
  const isSupportsFunction = arena.dataTypes[stream[i]] === EnumToken.SupportsFunctionTokenDefType;
68
69
  const funcType = tokensfuncDefMap.get(arena.dataTypes[stream[i]]);
@@ -90,7 +91,7 @@ function matchAtRuleWhenElseSyntax(arena, stream, context, options = {}) {
90
91
  }
91
92
  arena.spanLocation(stream[i], stream[i], stream[j]);
92
93
  const chi = isSupportsFunction
93
- ? arena.firstToken[stream[i]] != 0
94
+ ? arena.firstToken[stream[i]] !== 0
94
95
  ? arena.tokens(stream[i])
95
96
  : trimArray(arena, slice.slice(1, -1))
96
97
  : arena.tokens(tokenList[0]);
@@ -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 { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
@@ -10,7 +10,7 @@ import { getSyntaxRule } from '../../validation/config.js';
10
10
  import { trimArray, matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
11
11
 
12
12
  function matchAtRuleSyntax(arena, atRule, stream, options) {
13
- const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.strings[arena.name[atRule]]);
13
+ const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.resolveString(arena.name[atRule]));
14
14
  const syntax = syntaxRules?.getPreludeRules()?.slice?.(1);
15
15
  trimArray(arena, stream);
16
16
  if (syntax.length === 0) {
@@ -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
  /**
@@ -0,0 +1,53 @@
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
+ * memoize a function by its arguments (compared with `Map` semantics). the `null` and `undefined` results are cached
10
+ * too: an unknown name is looked up once
11
+ * @param fn
12
+ * @returns
13
+ */
14
+ function memoize(fn) {
15
+ const buckets = new Map();
16
+ return function (...args) {
17
+ const n = args.length;
18
+ let bucket = buckets.get(n);
19
+ if (bucket == null) {
20
+ bucket = new Map();
21
+ buckets.set(n, bucket);
22
+ }
23
+ if (n === 1) {
24
+ const key = args[0];
25
+ let value = bucket.get(key);
26
+ if (value === undefined && !bucket.has(key)) {
27
+ value = fn(key);
28
+ bucket.set(key, value);
29
+ }
30
+ return value;
31
+ }
32
+ // the maps of the first arguments, then the results by last argument
33
+ let node = bucket;
34
+ for (let i = 0; i < n - 1; i++) {
35
+ const key = args[i];
36
+ let next = node.get(key);
37
+ if (next == null) {
38
+ next = new Map();
39
+ node.set(key, next);
40
+ }
41
+ node = next;
42
+ }
43
+ const last = n === 0 ? undefined : args[n - 1];
44
+ let value = node.get(last);
45
+ if (value === undefined && !node.has(last)) {
46
+ value = fn(...args);
47
+ node.set(last, value);
48
+ }
49
+ return value;
50
+ };
51
+ }
52
+
53
+ export { memoize };
@@ -0,0 +1,78 @@
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
+ * Canonical strings: the spellings of the known units and function names (`dB`, `translateX`, ...).
10
+ *
11
+ * The canonical strings are registered once, when the tables of the tokenizer are built: they are stored in a single
12
+ * text. A canonical string is a range of this text: the string table of the arena references the range
13
+ * (see `StringSource.Canonical`), no string is created when a token is interned with its canonical spelling.
14
+ *
15
+ * @module
16
+ * @internal
17
+ */
18
+ /**
19
+ * offset of the registered strings in {@link text}
20
+ */
21
+ const offsets = new Map();
22
+ /**
23
+ * registered strings, concatenated
24
+ */
25
+ let text = "";
26
+ /**
27
+ * code units of {@link text}
28
+ */
29
+ let codes = new Uint16Array(0);
30
+ /**
31
+ * register a canonical string
32
+ * @param str
33
+ * @returns the range of the string in the canonical text, packed: `start << 8 | length` (see {@link canonicalStart}
34
+ * and {@link canonicalEnd})
35
+ */
36
+ function registerCanonical(str) {
37
+ let start = offsets.get(str);
38
+ if (start === undefined) {
39
+ start = text.length;
40
+ offsets.set(str, start);
41
+ text += str;
42
+ }
43
+ return (start << 8) | str.length;
44
+ }
45
+ /**
46
+ * start of a packed range
47
+ * @param range
48
+ */
49
+ function canonicalStart(range) {
50
+ return range >>> 8;
51
+ }
52
+ /**
53
+ * end of a packed range
54
+ * @param range
55
+ */
56
+ function canonicalEnd(range) {
57
+ return (range >>> 8) + (range & 0xff);
58
+ }
59
+ /**
60
+ * the canonical text
61
+ */
62
+ function canonicalText() {
63
+ return text;
64
+ }
65
+ /**
66
+ * the code units of the canonical text
67
+ */
68
+ function canonicalCodes() {
69
+ if (codes.length !== text.length) {
70
+ codes = new Uint16Array(text.length);
71
+ for (let i = 0; i < text.length; i++) {
72
+ codes[i] = text.charCodeAt(i);
73
+ }
74
+ }
75
+ return codes;
76
+ }
77
+
78
+ export { canonicalCodes, canonicalEnd, canonicalStart, canonicalText, registerCanonical };