@tbela99/css-parser 1.8.2 → 2.0.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 (105) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/README.md +129 -31
  3. package/dist/index-umd-web.js +27383 -24083
  4. package/dist/index.d.ts +2737 -2200
  5. package/dist/package.json.js +2 -2
  6. package/dist/src/data/properties.json.js +1 -1
  7. package/dist/src/data/validation.json.js +1 -1
  8. package/dist/src/lib/ast/arena-node.js +548 -0
  9. package/dist/src/lib/ast/arena.js +3198 -0
  10. package/dist/src/lib/ast/features/calc.js +85 -76
  11. package/dist/src/lib/ast/features/if.js +155 -115
  12. package/dist/src/lib/ast/features/index.js +1 -1
  13. package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
  14. package/dist/src/lib/ast/features/prefix.js +184 -183
  15. package/dist/src/lib/ast/features/shorthand.js +40 -46
  16. package/dist/src/lib/ast/features/transform.js +24 -24
  17. package/dist/src/lib/ast/features/type.js +1 -1
  18. package/dist/src/lib/ast/fingerprint.js +333 -0
  19. package/dist/src/lib/ast/math/expression.js +458 -543
  20. package/dist/src/lib/ast/math/math.js +19 -41
  21. package/dist/src/lib/ast/minify.js +590 -647
  22. package/dist/src/lib/ast/transform/compute.js +82 -85
  23. package/dist/src/lib/ast/transform/matrix.js +21 -38
  24. package/dist/src/lib/ast/transform/minify.js +82 -193
  25. package/dist/src/lib/ast/transform/perspective.js +2 -3
  26. package/dist/src/lib/ast/transform/rotate.js +1 -1
  27. package/dist/src/lib/ast/transform/scale.js +1 -1
  28. package/dist/src/lib/ast/transform/skew.js +1 -1
  29. package/dist/src/lib/ast/transform/translate.js +1 -1
  30. package/dist/src/lib/ast/transform/utils.js +2 -2
  31. package/dist/src/lib/ast/types.js +263 -258
  32. package/dist/src/lib/ast/walk.js +246 -610
  33. package/dist/src/lib/fs/resolve.js +1 -1
  34. package/dist/src/lib/parser/declaration/list.js +223 -139
  35. package/dist/src/lib/parser/declaration/map.js +251 -238
  36. package/dist/src/lib/parser/declaration/set.js +74 -66
  37. package/dist/src/lib/parser/linesmap.js +1 -1
  38. package/dist/src/lib/parser/parse.js +1379 -1658
  39. package/dist/src/lib/parser/source.js +7 -4
  40. package/dist/src/lib/parser/tokenize.js +406 -199
  41. package/dist/src/lib/parser/utils/angle.js +11 -13
  42. package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
  43. package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
  44. package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
  45. package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
  46. package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
  47. package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
  48. package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
  49. package/dist/src/lib/parser/utils/at-rule.js +5 -5
  50. package/dist/src/lib/parser/utils/base64.js +2 -2
  51. package/dist/src/lib/parser/utils/cache.js +1 -1
  52. package/dist/src/lib/parser/utils/config.js +1 -1
  53. package/dist/src/lib/parser/utils/declaration.js +233 -234
  54. package/dist/src/lib/parser/utils/eq.js +50 -3
  55. package/dist/src/lib/parser/utils/hash.js +2 -35
  56. package/dist/src/lib/parser/utils/selector.js +309 -387
  57. package/dist/src/lib/parser/utils/text.js +18 -4
  58. package/dist/src/lib/parser/utils/token.js +16 -69
  59. package/dist/src/lib/parser/utils/type.js +37 -24
  60. package/dist/src/lib/printer/render.js +965 -511
  61. package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
  62. package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
  63. package/dist/src/lib/syntax/color/a98rgb.js +1 -1
  64. package/dist/src/lib/syntax/color/alpha.js +36 -37
  65. package/dist/src/lib/syntax/color/cmyk.js +35 -52
  66. package/dist/src/lib/syntax/color/color-mix.js +96 -132
  67. package/dist/src/lib/syntax/color/color.js +222 -231
  68. package/dist/src/lib/syntax/color/hex.js +45 -55
  69. package/dist/src/lib/syntax/color/hsl.js +54 -62
  70. package/dist/src/lib/syntax/color/hwb.js +60 -68
  71. package/dist/src/lib/syntax/color/lab.js +59 -67
  72. package/dist/src/lib/syntax/color/lch.js +59 -67
  73. package/dist/src/lib/syntax/color/oklab.js +60 -68
  74. package/dist/src/lib/syntax/color/oklch.js +60 -67
  75. package/dist/src/lib/syntax/color/p3.js +1 -1
  76. package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
  77. package/dist/src/lib/syntax/color/rec2020.js +1 -1
  78. package/dist/src/lib/syntax/color/relative-color.js +94 -144
  79. package/dist/src/lib/syntax/color/rgb.js +54 -57
  80. package/dist/src/lib/syntax/color/srgb.js +55 -53
  81. package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
  82. package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
  83. package/dist/src/lib/syntax/color/utils/components.js +19 -20
  84. package/dist/src/lib/syntax/color/utils/distance.js +15 -13
  85. package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
  86. package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
  87. package/dist/src/lib/syntax/color/xyz.js +1 -1
  88. package/dist/src/lib/syntax/color/xyzd50.js +1 -1
  89. package/dist/src/lib/syntax/constants.js +2 -16
  90. package/dist/src/lib/syntax/syntax.js +301 -407
  91. package/dist/src/lib/validation/config.js +1 -1
  92. package/dist/src/lib/validation/match.js +959 -723
  93. package/dist/src/lib/validation/parser/parse.js +230 -112
  94. package/dist/src/lib/validation/parser/typedef.js +1 -1
  95. package/dist/src/lib/validation/utils/list.js +27 -12
  96. package/dist/src/node.js +12 -72
  97. package/dist/src/types.js +1 -1
  98. package/dist/src/utils/sync.js +9 -6
  99. package/dist/src/web.js +11 -73
  100. package/package.json +9 -10
  101. package/dist/index.cjs +0 -35870
  102. package/dist/src/lib/ast/clone.js +0 -51
  103. package/dist/src/lib/ast/expand.js +0 -311
  104. package/dist/src/lib/ast/find.js +0 -192
  105. package/dist/src/lib/ast/node.js +0 -59
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
@@ -43,35 +43,110 @@ const makeBinaryOp = new Map([
43
43
  [ValidationTokenEnum.Column, ValidationTokenEnum.ColumnToken],
44
44
  [ValidationTokenEnum.Ampersand, ValidationTokenEnum.AmpersandToken],
45
45
  ]);
46
- function move(position, chr) {
47
- for (const c of chr) {
48
- position.col++;
49
- position.ind += c.length;
46
+ /**
47
+ * single character symbols, by character code
48
+ */
49
+ const symbolTypes = new Map();
50
+ for (const [key, type] of Object.entries(SymbolsMapTokens)) {
51
+ if (key.length == 1) {
52
+ symbolTypes.set(key.charCodeAt(0), type);
53
+ }
54
+ }
55
+ /**
56
+ * two characters symbols (the second character code by first character code)
57
+ */
58
+ const pairSymbols = new Map();
59
+ for (const [key, type] of Object.entries(SymbolsMapTokens)) {
60
+ if (key.length == 2) {
61
+ const list = pairSymbols.get(key.charCodeAt(0)) ?? [];
62
+ list.push([key.charCodeAt(1), type]);
63
+ pairSymbols.set(key.charCodeAt(0), list);
64
+ }
65
+ }
66
+ /**
67
+ * symbol made of the character at index and the next one. at the end of the string, a single character symbol
68
+ * @param syntax
69
+ * @param index
70
+ */
71
+ function pairSymbolType(syntax, index) {
72
+ if (index + 1 >= syntax.length) {
73
+ return symbolTypes.get(syntax.charCodeAt(index)) ?? null;
74
+ }
75
+ const list = pairSymbols.get(syntax.charCodeAt(index));
76
+ if (list != null) {
77
+ const next = syntax.charCodeAt(index + 1);
78
+ for (let k = 0; k < list.length; k++) {
79
+ if (list[k][0] == next) {
80
+ return list[k][1];
81
+ }
82
+ }
83
+ }
84
+ return null;
85
+ }
86
+ /**
87
+ * number of code points of a range of a string
88
+ * @param str
89
+ * @param start
90
+ * @param end
91
+ */
92
+ function codePoints(str, start, end) {
93
+ let count = 0;
94
+ for (let k = start; k < end; k++) {
95
+ const code = str.charCodeAt(k);
96
+ // a surrogate pair is a single code point
97
+ if (code >= 0xd800 && code <= 0xdbff && k + 1 < end) {
98
+ const low = str.charCodeAt(k + 1);
99
+ if (low >= 0xdc00 && low <= 0xdfff) {
100
+ k++;
101
+ }
102
+ }
103
+ count++;
50
104
  }
51
- return position;
105
+ return count;
52
106
  }
53
- function getTokenType(token, position, currentPosition) {
54
- const pos = { ...position };
55
- Object.assign(position, currentPosition);
107
+ /**
108
+ * true if the string is made of ascii digits
109
+ * @param str
110
+ */
111
+ function isDigits(str) {
112
+ if (str.length == 0) {
113
+ return false;
114
+ }
115
+ for (let k = 0; k < str.length; k++) {
116
+ const code = str.charCodeAt(k);
117
+ if (code < 48 || code > 57) {
118
+ return false;
119
+ }
120
+ }
121
+ return true;
122
+ }
123
+ /**
124
+ * build a token from a buffered string
125
+ * @param token
126
+ * @param loc
127
+ */
128
+ function getTokenType(token, loc) {
56
129
  if (token === "<an+b>") {
57
130
  return {
58
131
  typ: ValidationTokenEnum.PropertyType,
59
132
  val: token.slice(1, -1),
60
- [LOC]: pos,
133
+ [LOC]: loc,
61
134
  };
62
135
  }
63
- if (token.charAt(0) == '"' || token.charAt(0) == "'") {
136
+ const first = token.charCodeAt(0);
137
+ // " or '
138
+ if (first == 34 || first == 39) {
64
139
  return {
65
140
  typ: ValidationTokenEnum.StringToken,
66
141
  val: token,
67
- [LOC]: pos,
142
+ [LOC]: loc,
68
143
  };
69
144
  }
70
- if (token.match(/^\d+$/)) {
145
+ if (isDigits(token)) {
71
146
  return {
72
147
  typ: ValidationTokenEnum.Number,
73
148
  val: Number(token),
74
- [LOC]: pos,
149
+ [LOC]: loc,
75
150
  };
76
151
  }
77
152
  let dimension = parseDimension(token);
@@ -82,27 +157,28 @@ function getTokenType(token, position, currentPosition) {
82
157
  unit: EnumToken[dimension.typ],
83
158
  val: dimension.val,
84
159
  unitText: dimension.unit,
85
- [LOC]: pos,
160
+ [LOC]: loc,
86
161
  };
87
162
  }
88
163
  if (isPseudo(token)) {
89
164
  return {
90
165
  typ: ValidationTokenEnum.PseudoClassToken,
91
166
  val: token,
92
- [LOC]: pos,
167
+ [LOC]: loc,
93
168
  };
94
169
  }
95
- if (token.startsWith("@") && isIdent(token.slice(1))) {
170
+ // @
171
+ if (first == 64 && isIdent(token.slice(1))) {
96
172
  return {
97
173
  typ: ValidationTokenEnum.AtRule,
98
174
  val: token.slice(1),
99
- [LOC]: pos,
175
+ [LOC]: loc,
100
176
  };
101
177
  }
102
178
  return {
103
179
  typ: ValidationTokenEnum.Keyword,
104
180
  val: token,
105
- [LOC]: pos,
181
+ [LOC]: loc,
106
182
  };
107
183
  }
108
184
  function trimSyntaxArray(tokens) {
@@ -125,7 +201,15 @@ function trimSyntaxArray(tokens) {
125
201
  }
126
202
  return tokens;
127
203
  }
128
- function* tokenizeSyntax(syntax, position = {
204
+ /**
205
+ * tokenize a syntax definition. the characters are scanned by code: a string is allocated for each token only.
206
+ * the positions are tracked as numbers: `position` is the start of the pending token, `currentPosition` the current
207
+ * character (columns are counted in code points, indexes in utf-16 code units)
208
+ * @param syntax
209
+ * @param position
210
+ * @param currentPosition
211
+ */
212
+ function tokenizeSyntax(syntax, position = {
129
213
  ind: 0,
130
214
  lin: 1,
131
215
  col: 0,
@@ -134,135 +218,169 @@ function* tokenizeSyntax(syntax, position = {
134
218
  lin: 1,
135
219
  col: 0,
136
220
  }) {
221
+ const tokens = [];
222
+ const length = syntax.length;
223
+ let posInd = position.ind;
224
+ let posLin = position.lin;
225
+ let posCol = position.col;
226
+ let curInd = currentPosition.ind;
227
+ let curLin = currentPosition.lin;
228
+ let curCol = currentPosition.col;
229
+ // the buffer is `prefix` + syntax[bufferStart, bufferEnd). the prefix is only used when `<an+b>` interrupts a word
230
+ let prefix = "";
231
+ let bufferStart = 0;
232
+ let bufferEnd = 0;
137
233
  let i = -1;
138
- let buffer = "";
139
- let pos;
140
- while (++i < syntax.length) {
141
- let chr = syntax.charAt(i);
142
- move(currentPosition, chr);
143
- if (chr === "<" && "an+b>" === syntax.slice(i + 1, i + 6)) {
144
- // if (buffer.length > 0) {
145
- // yield getTokenType(buffer, position, currentPosition);
146
- // buffer = "";
147
- // }
148
- yield getTokenType("<an+b>", position, currentPosition);
149
- move(currentPosition, "an+b>");
234
+ let code;
235
+ let type;
236
+ const bufferLength = () => prefix.length + bufferEnd - bufferStart;
237
+ const buffer = () => prefix.length == 0 ? syntax.slice(bufferStart, bufferEnd) : prefix + syntax.slice(bufferStart, bufferEnd);
238
+ const append = (index) => {
239
+ if (bufferEnd == bufferStart) {
240
+ bufferStart = index;
241
+ }
242
+ bufferEnd = index + 1;
243
+ };
244
+ const clear = () => {
245
+ prefix = "";
246
+ bufferStart = bufferEnd = 0;
247
+ };
248
+ // the location of the token starts at `position`. `position` moves to the current character
249
+ const startToken = () => {
250
+ const loc = { ind: posInd, lin: posLin, col: posCol };
251
+ posInd = curInd;
252
+ posLin = curLin;
253
+ posCol = curCol;
254
+ return loc;
255
+ };
256
+ while (++i < length) {
257
+ code = syntax.charCodeAt(i);
258
+ curCol++;
259
+ curInd++;
260
+ // <an+b>
261
+ if (code == 60 && syntax.startsWith("an+b>", i + 1)) {
262
+ if (bufferLength() > 0) {
263
+ prefix = buffer();
264
+ bufferStart = bufferEnd = 0;
265
+ }
266
+ tokens.push(getTokenType("<an+b>", startToken()));
267
+ curCol += 5;
268
+ curInd += 5;
150
269
  i += 5;
151
270
  continue;
152
271
  }
153
- if (SymbolsMapTokens[chr + syntax.charAt(i + 1)] != null) {
154
- chr += syntax.charAt(++i);
155
- move(currentPosition, syntax.charAt(i));
156
- pos = { ...position };
157
- Object.assign(position, currentPosition);
158
- if (buffer.length > 0) {
159
- yield getTokenType(buffer, position, currentPosition);
160
- buffer = "";
272
+ type = pairSymbolType(syntax, i);
273
+ if (type != null) {
274
+ if (i + 1 < length) {
275
+ i++;
276
+ curCol++;
277
+ curInd++;
278
+ }
279
+ const loc = startToken();
280
+ if (bufferLength() > 0) {
281
+ tokens.push(getTokenType(buffer(), startToken()));
282
+ clear();
161
283
  }
162
- yield {
163
- typ: SymbolsMapTokens[chr],
164
- [LOC]: pos,
165
- };
284
+ tokens.push({
285
+ typ: type,
286
+ [LOC]: loc,
287
+ });
166
288
  continue;
167
289
  }
168
- if (chr === ":") {
169
- if (syntax.charAt(i + 1) === ":") {
290
+ // :
291
+ if (code == 58) {
292
+ if (syntax.charCodeAt(i + 1) == 58) {
170
293
  if (isIdentStart(syntax.charCodeAt(i + 2)) ||
171
- (syntax.charAt(i + 2) === "-" && isIdentStart(syntax.charCodeAt(i + 3)))) {
172
- buffer += chr + ":";
173
- i++;
294
+ (syntax.charCodeAt(i + 2) == 45 && isIdentStart(syntax.charCodeAt(i + 3)))) {
295
+ append(i);
296
+ append(++i);
174
297
  continue;
175
298
  }
176
299
  }
177
300
  if (isIdentStart(syntax.charCodeAt(i + 1)) ||
178
- (syntax.charAt(i + 1) === "-" && isIdentStart(syntax.charCodeAt(i + 2)))) {
179
- buffer += chr;
301
+ (syntax.charCodeAt(i + 1) == 45 && isIdentStart(syntax.charCodeAt(i + 2)))) {
302
+ append(i);
180
303
  continue;
181
304
  }
182
305
  }
183
- if (SymbolsMapTokens[chr] != null) {
184
- if (SymbolsMapTokens[chr] === ValidationTokenEnum.Whitespace) {
185
- let ch;
306
+ type = symbolTypes.get(code) ?? null;
307
+ if (type != null) {
308
+ if (type === ValidationTokenEnum.Whitespace) {
186
309
  let k = i;
187
- while ((ch = syntax.charAt(k + 1)) && SymbolsMapTokens[ch] === ValidationTokenEnum.Whitespace) {
310
+ while (k + 1 < length && symbolTypes.get(syntax.charCodeAt(k + 1)) === ValidationTokenEnum.Whitespace) {
188
311
  k++;
189
312
  }
190
313
  if (k != i) {
191
- move(currentPosition, syntax.slice(i + 1, k + 1));
314
+ curCol += k - i;
315
+ curInd += k - i;
192
316
  i = k;
193
317
  }
194
318
  }
195
- pos = { ...position };
196
- Object.assign(position, currentPosition);
197
- if (buffer.length > 0) {
198
- yield getTokenType(buffer, position, currentPosition);
199
- buffer = "";
319
+ const loc = startToken();
320
+ if (bufferLength() > 0) {
321
+ tokens.push(getTokenType(buffer(), startToken()));
322
+ clear();
200
323
  }
201
- yield {
202
- typ: SymbolsMapTokens[chr],
203
- [LOC]: pos,
204
- };
324
+ tokens.push({
325
+ typ: type,
326
+ [LOC]: loc,
327
+ });
205
328
  continue;
206
329
  }
207
- switch (chr) {
208
- // case "\\":
209
- // if (buffer.length > 0) {
210
- // yield getTokenType(buffer, position, currentPosition);
211
- // buffer = "";
212
- // }
213
- // buffer += chr;
214
- // chr = syntax.charAt(++i);
215
- // buffer += chr;
216
- // move(currentPosition, chr);
217
- // break;
218
- case ":":
219
- if (isIdent(buffer)) {
220
- yield {
330
+ switch (code) {
331
+ // :
332
+ case 58:
333
+ if (isIdent(buffer())) {
334
+ tokens.push({
221
335
  typ: ValidationTokenEnum.DeclarationNameToken,
222
- val: buffer,
223
- [LOC]: { ...position },
224
- };
225
- buffer = "";
226
- move(currentPosition, chr);
336
+ val: buffer(),
337
+ [LOC]: { ind: posInd, lin: posLin, col: posCol },
338
+ });
339
+ clear();
340
+ curCol++;
341
+ curInd++;
227
342
  break;
228
343
  }
229
- buffer += chr;
344
+ append(i);
230
345
  break;
231
- case '"':
232
- case "'":
233
- // if (buffer.length > 0) {
234
- // yield getTokenType(buffer, position, currentPosition);
235
- // buffer = "";
236
- // }
237
- buffer += chr;
238
- while (i + 1 < syntax.length) {
239
- chr = syntax.charAt(++i);
240
- buffer += chr;
241
- // if (chr == "\\") {
242
- // chr = syntax.charAt(++i);
243
- // buffer += chr;
244
- // continue;
245
- // }
246
- if (chr == buffer.charAt(0)) {
247
- break;
346
+ // " and '
347
+ case 34:
348
+ case 39:
349
+ {
350
+ const quote = bufferLength() > 0 ? buffer().charCodeAt(0) : code;
351
+ append(i);
352
+ while (i + 1 < length) {
353
+ append(++i);
354
+ if (syntax.charCodeAt(i) == quote) {
355
+ break;
356
+ }
248
357
  }
358
+ const str = buffer();
359
+ tokens.push(getTokenType(str, startToken()));
360
+ curCol += codePoints(str, 0, str.length);
361
+ curInd += str.length;
362
+ clear();
249
363
  }
250
- yield getTokenType(buffer, position, currentPosition);
251
- move(currentPosition, buffer);
252
- buffer = "";
253
364
  break;
254
365
  default:
255
- buffer += chr;
366
+ append(i);
256
367
  break;
257
368
  }
258
369
  }
259
- if (buffer.length > 0) {
260
- yield getTokenType(buffer, position, currentPosition);
370
+ if (bufferLength() > 0) {
371
+ tokens.push(getTokenType(buffer(), startToken()));
261
372
  }
262
- yield {
373
+ tokens.push({
263
374
  typ: ValidationTokenEnum.EOF,
264
- [LOC]: { ...position },
265
- };
375
+ [LOC]: { ind: posInd, lin: posLin, col: posCol },
376
+ });
377
+ position.ind = posInd;
378
+ position.lin = posLin;
379
+ position.col = posCol;
380
+ currentPosition.ind = curInd;
381
+ currentPosition.lin = curLin;
382
+ currentPosition.col = curCol;
383
+ return tokens;
266
384
  }
267
385
  /**
268
386
  * parse mdn data syntax
@@ -589,7 +707,7 @@ function parseSyntax(syntax) {
589
707
  // if (
590
708
  // slice[2].typ != ValidationTokenEnum.Number &&
591
709
  // slice[2].typ != ValidationTokenEnum.InfinityToken
592
- // ) {
710
+ //) {
593
711
  // throw new SyntaxError(
594
712
  // `Expecting number or infinity at ${slice[2][LOC].lin}:${slice[2][LOC].col}`,
595
713
  // );
@@ -2,7 +2,7 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.0
6
6
  License: MIT
7
7
  */
8
8
  var ValidationTokenEnum;
@@ -2,34 +2,49 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
9
9
 
10
- function stripCommaToken(tokenList) {
10
+ /**
11
+ * remove comma and comment tokens
12
+ * @param arena
13
+ * @param tokenList
14
+ */
15
+ function stripCommaToken(arena, tokenList) {
11
16
  let result = [];
12
17
  for (let i = 0; i < tokenList.length; i++) {
13
- if (tokenList[i].typ == EnumToken.CommentTokenType || tokenList[i].typ == EnumToken.CommaTokenType) {
18
+ const typ = arena.dataTypes[tokenList[i]];
19
+ if (typ == EnumToken.CommentTokenType || typ == EnumToken.CommaTokenType) {
14
20
  continue;
15
21
  }
16
22
  result.push(tokenList[i]);
17
23
  }
18
24
  return result;
19
25
  }
20
- function splitTokenList(tokenList, split = [EnumToken.CommaTokenType], includeSplitToken = false) {
21
- return tokenList.reduce((acc, curr) => {
22
- if (split.includes(curr.typ)) {
23
- if (includeSplitToken && Array.isArray(acc[acc.length - 1])) {
24
- acc[acc.length - 1].push(curr);
26
+ /**
27
+ * split a token list
28
+ * @param arena
29
+ * @param tokenList
30
+ * @param split
31
+ * @param includeSplitToken
32
+ */
33
+ function splitTokenList(arena, tokenList, split = [EnumToken.CommaTokenType], includeSplitToken = false) {
34
+ const result = [[]];
35
+ for (let i = 0; i < tokenList.length; i++) {
36
+ const curr = tokenList[i];
37
+ if (split.includes(arena.dataTypes[curr])) {
38
+ if (includeSplitToken) {
39
+ result[result.length - 1].push(curr);
25
40
  }
26
- acc.push([]);
41
+ result.push([]);
27
42
  }
28
43
  else {
29
- acc[acc.length - 1].push(curr);
44
+ result[result.length - 1].push(curr);
30
45
  }
31
- return acc;
32
- }, [[]]);
46
+ }
47
+ return result;
33
48
  }
34
49
 
35
50
  export { splitTokenList, stripCommaToken };
package/dist/src/node.js CHANGED
@@ -2,10 +2,9 @@
2
2
  CSS parser, minifier and validator for node and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 1.8.2
5
+ Version: 2.0.0
6
6
  License: MIT
7
7
  */
8
- import { deprecate } from 'node:util';
9
8
  import { Readable } from 'node:stream';
10
9
  import { createReadStream } from 'node:fs';
11
10
  import { lstat, readFile } from 'node:fs/promises';
@@ -14,25 +13,21 @@ export { parseDeclarations, parseString } from './lib/parser/parse.js';
14
13
  import { doRender } from './lib/printer/render.js';
15
14
  export { renderValue as renderToken } from './lib/printer/render.js';
16
15
  import { ModuleScopeEnumOptions } from './lib/ast/types.js';
17
- export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, ValidationLevel } from './lib/ast/types.js';
16
+ export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, WalkerEvent, WalkerOptionEnum } from './lib/ast/types.js';
18
17
  import { Tokenizer } from './lib/parser/tokenize.js';
19
- import { dirname, resolve, matchUrl } from './lib/fs/resolve.js';
18
+ import { resolve, matchUrl, dirname } from './lib/fs/resolve.js';
20
19
  import { ResponseType } from './types.js';
21
20
  import { resolve as resolve$1 } from 'node:path';
22
21
  import { SourceFile } from './lib/parser/source.js';
23
22
  import { cwd } from 'node:process';
24
23
  import { parseResult, validateSyncArguments } from './utils/sync.js';
25
24
  export { minify } from './lib/ast/minify.js';
26
- export { expand } from './lib/ast/expand.js';
27
- export { WalkerEvent, WalkerOptionEnum, walk, walkValues } from './lib/ast/walk.js';
28
25
  export { convertColor } from './lib/syntax/color/color.js';
29
26
  export { isOkLabClose, okLabDistance } from './lib/syntax/color/utils/distance.js';
30
- export { find, findAll, findByValue, findLast } from './lib/ast/find.js';
31
- export { cloneNode } from './lib/ast/clone.js';
32
- export { replaceNodeOrValue } from './lib/parser/utils/token.js';
27
+ export { Arena, AstArena, TokenField, arenaColumns } from './lib/ast/arena.js';
28
+ export { ARENA, ARENAID, AstArenaNode } from './lib/ast/arena-node.js';
33
29
  export { SourceMap } from './lib/printer/sourcemap/sourcemap.js';
34
30
  export { FeatureWalkMode } from './lib/ast/features/type.js';
35
- export { getNodeProperty, setNodeProperty } from './lib/ast/node.js';
36
31
  export { parseSyntax } from './lib/validation/parser/parse.js';
37
32
  export { version } from '../package.json.js';
38
33
 
@@ -102,13 +97,13 @@ async function load(url, currentDirectory = ".", responseType = false) {
102
97
  * const css = 'body { color: color(from hsl(0 100% 50%) xyz x y z); }';
103
98
  * const parseResult = await parse(css);
104
99
  *
105
- * let renderResult = render(parseResult.ast);
100
+ * let renderResult = render(parseResult.arena, parseResult.ast);
106
101
  * console.log(result.code);
107
102
  *
108
103
  * // body{color:red}
109
104
  *
110
105
  *
111
- * renderResult = render(parseResult.ast, {beautify: true, convertColor: ColorType.SRGB});
106
+ * renderResult = render(parseResult.arena, parseResult.ast, {beautify: true, convertColor: ColorType.SRGB});
112
107
  * console.log(renderResult.code);
113
108
  *
114
109
  * // body {
@@ -116,35 +111,9 @@ async function load(url, currentDirectory = ".", responseType = false) {
116
111
  * // }
117
112
  * ```
118
113
  */
119
- function render(data, options = {}, mapping) {
120
- return doRender(data, Object.assign(options, { resolve, dirname, cwd: options.cwd ?? resolve$1() }), mapping);
114
+ function render(arena, data, options = {}, mapping) {
115
+ return doRender(arena, data, Object.assign(options, { resolve, dirname, cwd: options.cwd ?? resolve$1() }), mapping);
121
116
  }
122
- /**
123
- * Parse CSS file
124
- * @param file url or path
125
- * @param options
126
- * @param asStream load file as stream
127
- *
128
- * @deprecated
129
- * @see {@link parse}
130
- * @throws Error file not found
131
- *
132
- * Example:
133
- *
134
- * ```ts
135
- *
136
- * import {parseFile} from '@tbela99/css-parser';
137
- *
138
- * // remote file
139
- * let result = await parseFile('https://docs.deno.com/styles.css');
140
- * console.log(result.ast);
141
- *
142
- * // local file
143
- * result = await parseFile('./css/styles.css');
144
- * console.log(result.ast);
145
- * ```
146
- */
147
- const parseFile = deprecate(async (file, options = {}, asStream = false) => parse({ file, asStream, ...options }), "parseFile is deprecated, use parse instead as parse({file, asStream, ...options})");
148
117
  /**
149
118
  * Parse CSS
150
119
  * @param args
@@ -254,7 +223,7 @@ function transformSync(...args) {
254
223
  importMapping = parseResult.importMapping;
255
224
  }
256
225
  // ast already expanded by parse
257
- const rendered = render(parseResult.ast, {
226
+ const rendered = render(parseResult.arena, parseResult.ast, {
258
227
  ...options,
259
228
  expandNestingRules: false,
260
229
  }, mapping != null ? { mapping, importMapping } : null);
@@ -363,35 +332,6 @@ async function parse(...args) {
363
332
  ? result
364
333
  : parseResult(result, options));
365
334
  }
366
- /**
367
- * Transform CSS file
368
- * @param file url or path
369
- * @param options
370
- * @param asStream load file as stream
371
- *
372
- * @deprecated Use transform() instead.
373
- * @throws Error file not found
374
- *
375
- * Example:
376
- *
377
- * ```ts
378
- *
379
- * import {transform} from '@tbela99/css-parser';
380
- *
381
- * // remote file
382
- * let result = await transform({file: 'https://docs.deno.com/styles.css'});
383
- * console.log(result.code);
384
- *
385
- * // local file
386
- * result = await transform({file: './css/styles.css'});
387
- * console.log(result.code);
388
- * ```
389
- */
390
- const transformFile = deprecate(async (file, options = {}, asStream = false) => transform({
391
- file,
392
- asStream,
393
- ...options,
394
- }), "transformFile is deprecated, use transform instead as transform({file, asStream, ...options})");
395
335
  /**
396
336
  * Transform CSS
397
337
  *
@@ -474,7 +414,7 @@ async function transform(...args) {
474
414
  importMapping = parseResult.importMapping;
475
415
  }
476
416
  // ast already expanded by parse
477
- const rendered = render(parseResult.ast, {
417
+ const rendered = render(parseResult.arena, parseResult.ast, {
478
418
  ...options,
479
419
  expandNestingRules: false,
480
420
  }, mapping != null ? { mapping, importMapping } : null);
@@ -492,4 +432,4 @@ async function transform(...args) {
492
432
  });
493
433
  }
494
434
 
495
- export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseFile, parseSync, render, resolve, transform, transformFile, transformSync };
435
+ export { ModuleScopeEnumOptions, ResponseType, dirname, load, parse, parseSync, render, resolve, transform, transformSync };