@tbela99/css-parser 1.5.0 → 1.6.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 (73) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +7 -7
  3. package/dist/index-umd-web.js +5562 -4731
  4. package/dist/index.cjs +5565 -4736
  5. package/dist/index.d.ts +51 -37
  6. package/dist/lib/ast/expand.js +21 -7
  7. package/dist/lib/ast/features/calc.js +17 -55
  8. package/dist/lib/ast/features/if.js +7 -3
  9. package/dist/lib/ast/features/inlinecssvariables.js +6 -5
  10. package/dist/lib/ast/features/prefix.js +41 -44
  11. package/dist/lib/ast/features/shorthand.js +10 -5
  12. package/dist/lib/ast/features/transform.js +1 -1
  13. package/dist/lib/ast/math/expression.js +190 -32
  14. package/dist/lib/ast/minify.js +37 -14
  15. package/dist/lib/ast/node.js +5 -3
  16. package/dist/lib/ast/transform/compute.js +63 -4
  17. package/dist/lib/ast/transform/minify.js +6 -6
  18. package/dist/lib/ast/transform/perspective.js +1 -1
  19. package/dist/lib/ast/transform/rotate.js +1 -1
  20. package/dist/lib/ast/transform/scale.js +1 -1
  21. package/dist/lib/ast/transform/skew.js +1 -1
  22. package/dist/lib/ast/transform/translate.js +1 -1
  23. package/dist/lib/ast/transform/utils.js +46 -18
  24. package/dist/lib/ast/walk.js +28 -4
  25. package/dist/lib/fs/resolve.js +11 -2
  26. package/dist/lib/parser/declaration/list.js +22 -7
  27. package/dist/lib/parser/declaration/map.js +26 -8
  28. package/dist/lib/parser/declaration/set.js +3 -1
  29. package/dist/lib/parser/linesmap.js +2 -4
  30. package/dist/lib/parser/parse.js +388 -227
  31. package/dist/lib/parser/tokenize.js +1478 -818
  32. package/dist/lib/parser/utils/at-rule-container.js +28 -196
  33. package/dist/lib/parser/utils/at-rule-generic.js +6 -7
  34. package/dist/lib/parser/utils/at-rule-import.js +17 -13
  35. package/dist/lib/parser/utils/at-rule-media.js +36 -19
  36. package/dist/lib/parser/utils/at-rule-support.js +19 -9
  37. package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
  38. package/dist/lib/parser/utils/at-rule.js +0 -18
  39. package/dist/lib/parser/utils/declaration.js +30 -95
  40. package/dist/lib/parser/utils/hash.js +4 -4
  41. package/dist/lib/parser/utils/selector.js +89 -144
  42. package/dist/lib/parser/utils/text.js +4 -2
  43. package/dist/lib/renderer/render.js +102 -72
  44. package/dist/lib/renderer/sourcemap/sourcemap.js +2 -5
  45. package/dist/lib/syntax/color/a98rgb.js +14 -4
  46. package/dist/lib/syntax/color/cmyk.js +10 -18
  47. package/dist/lib/syntax/color/color-mix.js +21 -34
  48. package/dist/lib/syntax/color/color.js +32 -49
  49. package/dist/lib/syntax/color/hsl.js +15 -15
  50. package/dist/lib/syntax/color/hwb.js +18 -17
  51. package/dist/lib/syntax/color/lab.js +17 -17
  52. package/dist/lib/syntax/color/lch.js +13 -13
  53. package/dist/lib/syntax/color/oklab.js +10 -11
  54. package/dist/lib/syntax/color/oklch.js +15 -18
  55. package/dist/lib/syntax/color/p3.js +18 -12
  56. package/dist/lib/syntax/color/prophotorgb.js +26 -28
  57. package/dist/lib/syntax/color/rec2020.js +8 -4
  58. package/dist/lib/syntax/color/relative-color.js +26 -10
  59. package/dist/lib/syntax/color/srgb.js +5 -4
  60. package/dist/lib/syntax/color/utils/distance.js +1 -1
  61. package/dist/lib/syntax/color/xyz.js +2 -2
  62. package/dist/lib/syntax/color/xyzd50.js +2 -2
  63. package/dist/lib/syntax/constants.js +12 -2
  64. package/dist/lib/syntax/syntax.js +32 -108
  65. package/dist/lib/validation/config.json.js +3 -0
  66. package/dist/lib/validation/match.js +29 -26
  67. package/dist/node.js +16 -9
  68. package/dist/types.d.ts +5 -1
  69. package/dist/types.js +4 -0
  70. package/dist/utils/sync.js +22 -1
  71. package/dist/web.js +13 -4
  72. package/package.json +1 -1
  73. package/llms.txt +0 -80
@@ -1,97 +1,81 @@
1
1
  import { EnumToken, ColorType } from '../ast/types.js';
2
- import { LOC, wildCardFuncs, whenElseFunc, transformFunctions, mathFuncs, colorsFunc, timingFunc, supportFunc, timelineFunc, imageFunc, gridTemplateFunc, urlFunc, containerFunc, pseudoElements } from '../syntax/constants.js';
3
- import { isDigit, isWhiteSpace, isIdent, isHexColor, isHash, isNumber, isPercentage, parseDimension, isNewLine, isIdentStart, isIdentCodepoint, isNonPrintable } from '../syntax/syntax.js';
2
+ import { wildCardFuncs, whenElseFunc, mathFuncs, timingFunc, supportFunc, timelineFunc, imageFunc, gridTemplateFunc, urlFunc, containerFunc, colorsFunc, transformFunctions, pseudoElements } from '../syntax/constants.js';
3
+ import { isWhiteSpace, isNewLine, isDigit, isLetter, isIdentStart, isIdentCodepoint, isNonPrintable, timeUnits, angleUnits, flexUnits, dimensionUnits, resolutionUnits, frequencyUnits } from '../syntax/syntax.js';
4
4
  import { SourceFile } from './source.js';
5
- import { equalsIgnoreCase } from './utils/text.js';
6
5
 
7
- const SymbolsMapTokens = {
8
- "+": EnumToken.Plus,
9
- "=": EnumToken.DelimTokenType,
10
- "|": EnumToken.Pipe,
11
- "||": EnumToken.ColumnCombinatorTokenType,
12
- "|=": EnumToken.DashMatchTokenType,
13
- "&": EnumToken.NestingSelectorTokenType,
14
- "*": EnumToken.Star,
15
- "*=": EnumToken.ContainMatchTokenType,
16
- "~": EnumToken.Tilda,
17
- "~=": EnumToken.IncludeMatchTokenType,
18
- "^=": EnumToken.StartMatchTokenType,
19
- "$=": EnumToken.EndMatchTokenType,
20
- ",": EnumToken.Comma,
21
- ":": EnumToken.ColonTokenType,
22
- "::": EnumToken.DoubleColonTokenType,
23
- ";": EnumToken.SemiColonTokenType,
24
- "(": EnumToken.StartParensTokenType,
25
- ")": EnumToken.EndParensTokenType,
26
- "[": EnumToken.AttrStartTokenType,
27
- "]": EnumToken.AttrEndTokenType,
28
- "{": EnumToken.BlockStartTokenType,
29
- "}": EnumToken.BlockEndTokenType,
30
- "<=": EnumToken.LteTokenType,
31
- ">": EnumToken.GtTokenType,
32
- ">=": EnumToken.GteTokenType,
33
- " ": EnumToken.Whitespace,
34
- "\t": EnumToken.Whitespace,
35
- "\r": EnumToken.Whitespace,
36
- "\n": EnumToken.Whitespace,
37
- "\f": EnumToken.Whitespace,
38
- ...pseudoElements.reduce((acc, curr) => {
39
- acc[curr] = EnumToken.PseudoElementTokenType;
40
- return acc;
41
- }, Object.create(null)),
42
- ...containerFunc.reduce((acc, curr) => {
43
- acc[curr + "("] = EnumToken.ContainerFunctionTokenDefType;
44
- return acc;
45
- }, Object.create(null)),
46
- ...urlFunc.reduce((acc, curr) => {
47
- acc[curr + "("] = EnumToken.UrlFunctionTokenDefType;
48
- return acc;
49
- }, Object.create(null)),
50
- ...gridTemplateFunc.reduce((acc, curr) => {
51
- acc[curr + "("] = EnumToken.GridTemplateFuncTokenDefType;
52
- return acc;
53
- }, Object.create(null)),
54
- ...imageFunc.reduce((acc, curr) => {
55
- acc[curr + "("] = EnumToken.ImageFunctionTokenDefType;
56
- return acc;
57
- }, Object.create(null)),
58
- ...timelineFunc.reduce((acc, curr) => {
59
- acc[curr + "("] = EnumToken.TimelineFunctionTokenDefType;
60
- return acc;
61
- }, Object.create(null)),
62
- // ...generalEnclosedFunc.reduce((acc, curr: string) => {
63
- // acc[curr + "("] = EnumToken.GeneralEnclosedFunctionTokenDefType;
64
- // return acc;
65
- // }, Object.create(null)),
66
- ...supportFunc.reduce((acc, curr) => {
67
- acc[curr + "("] = EnumToken.SupportsFunctionTokenDefType;
68
- return acc;
69
- }, Object.create(null)),
70
- ...timingFunc.reduce((acc, curr) => {
71
- acc[curr + "("] = EnumToken.TimingFunctionTokenDefType;
72
- return acc;
73
- }, Object.create(null)),
74
- ...colorsFunc.reduce((acc, curr) => {
75
- acc[curr + "("] = EnumToken.ColorFunctionTokenDefType;
76
- return acc;
77
- }, Object.create(null)),
78
- ...mathFuncs.reduce((acc, curr) => {
79
- acc[curr + "("] = EnumToken.MathFunctionTokenDefType;
80
- return acc;
81
- }, Object.create(null)),
82
- ...transformFunctions.reduce((acc, curr) => {
83
- acc[curr.toLowerCase() + "("] = EnumToken.TransformFunctionTokenDefType;
84
- return acc;
85
- }, Object.create(null)),
86
- ...whenElseFunc.reduce((acc, curr) => {
87
- acc[curr + "("] = EnumToken.WhenElseFunctionTokenDefType;
88
- return acc;
89
- }, Object.create(null)),
90
- ...wildCardFuncs.reduce((acc, curr) => {
91
- acc[curr + "("] = EnumToken.WildCardFunctionTokenDefType;
92
- return acc;
93
- }, Object.create(null)),
94
- };
6
+ const SymbolsMapTokens = Object.create(null);
7
+ // Regex for escape sequence decoding - compile once, reuse many times
8
+ const ESCAPE_SEQUENCE_REGEX = /\\([0-9a-fA-F]{1,6})(?:\s)?/g;
9
+ function decodeEscapeSequences(value) {
10
+ return value.replace(ESCAPE_SEQUENCE_REGEX, (_, sequence) => {
11
+ const codepoint = parseInt(sequence, 16);
12
+ if (codepoint == 0 ||
13
+ // leading surrogate
14
+ (0xd800 <= codepoint && codepoint <= 0xdbff) ||
15
+ // trailing surrogate
16
+ (0xdc00 <= codepoint && codepoint <= 0xdfff) ||
17
+ codepoint > 0x10ffff) {
18
+ return "\uFFFD";
19
+ }
20
+ return String.fromCodePoint(codepoint);
21
+ });
22
+ }
23
+ function assignTokenMap(entries, tokenType, suffix = "", lowercase = false) {
24
+ for (const entry of entries) {
25
+ SymbolsMapTokens[(lowercase ? entry.toLowerCase() : entry) + suffix] = tokenType;
26
+ }
27
+ }
28
+ SymbolsMapTokens[""] = EnumToken.DelimTokenType;
29
+ SymbolsMapTokens["+"] = EnumToken.Plus;
30
+ SymbolsMapTokens["="] = EnumToken.DelimTokenType;
31
+ SymbolsMapTokens["|"] = EnumToken.Pipe;
32
+ SymbolsMapTokens["||"] = EnumToken.ColumnCombinatorTokenType;
33
+ SymbolsMapTokens["|="] = EnumToken.DashMatchTokenType;
34
+ SymbolsMapTokens["&"] = EnumToken.NestingSelectorTokenType;
35
+ SymbolsMapTokens["*"] = EnumToken.Star;
36
+ SymbolsMapTokens["*="] = EnumToken.ContainMatchTokenType;
37
+ SymbolsMapTokens["~"] = EnumToken.Tilda;
38
+ SymbolsMapTokens["~="] = EnumToken.IncludeMatchTokenType;
39
+ SymbolsMapTokens["^="] = EnumToken.StartMatchTokenType;
40
+ SymbolsMapTokens["$="] = EnumToken.EndMatchTokenType;
41
+ SymbolsMapTokens[","] = EnumToken.Comma;
42
+ SymbolsMapTokens[":"] = EnumToken.ColonTokenType;
43
+ SymbolsMapTokens["::"] = EnumToken.DoubleColonTokenType;
44
+ SymbolsMapTokens[";"] = EnumToken.SemiColonTokenType;
45
+ SymbolsMapTokens["("] = EnumToken.StartParensTokenType;
46
+ SymbolsMapTokens[")"] = EnumToken.EndParensTokenType;
47
+ SymbolsMapTokens["["] = EnumToken.AttrStartTokenType;
48
+ SymbolsMapTokens["]"] = EnumToken.AttrEndTokenType;
49
+ SymbolsMapTokens["{"] = EnumToken.BlockStartTokenType;
50
+ SymbolsMapTokens["}"] = EnumToken.BlockEndTokenType;
51
+ SymbolsMapTokens["<="] = EnumToken.LteTokenType;
52
+ SymbolsMapTokens[">"] = EnumToken.GtTokenType;
53
+ SymbolsMapTokens[">="] = EnumToken.GteTokenType;
54
+ SymbolsMapTokens[" "] = EnumToken.Whitespace;
55
+ SymbolsMapTokens["\t"] = EnumToken.Whitespace;
56
+ SymbolsMapTokens["\r"] = EnumToken.Whitespace;
57
+ SymbolsMapTokens["\n"] = EnumToken.Whitespace;
58
+ SymbolsMapTokens["\f"] = EnumToken.Whitespace;
59
+ assignTokenMap(flexUnits, EnumToken.FlexTokenType);
60
+ assignTokenMap(dimensionUnits, EnumToken.LengthTokenType);
61
+ assignTokenMap(resolutionUnits, EnumToken.ResolutionTokenType);
62
+ assignTokenMap(angleUnits, EnumToken.AngleTokenType);
63
+ assignTokenMap(timeUnits, EnumToken.TimeTokenType);
64
+ assignTokenMap(frequencyUnits, EnumToken.FrequencyTokenType);
65
+ assignTokenMap(pseudoElements, EnumToken.PseudoElementTokenType);
66
+ assignTokenMap(containerFunc, EnumToken.ContainerFunctionTokenDefType, "(");
67
+ assignTokenMap(urlFunc, EnumToken.UrlFunctionTokenDefType, "(");
68
+ assignTokenMap(gridTemplateFunc, EnumToken.GridTemplateFuncTokenDefType, "(");
69
+ assignTokenMap(imageFunc, EnumToken.ImageFunctionTokenDefType, "(");
70
+ assignTokenMap(timelineFunc, EnumToken.TimelineFunctionTokenDefType, "(");
71
+ assignTokenMap(supportFunc, EnumToken.SupportsFunctionTokenDefType, "(");
72
+ assignTokenMap(timingFunc, EnumToken.TimingFunctionTokenDefType, "(");
73
+ assignTokenMap(colorsFunc, EnumToken.ColorFunctionTokenDefType, "(");
74
+ assignTokenMap(mathFuncs, EnumToken.MathFunctionTokenDefType, "(");
75
+ assignTokenMap(transformFunctions, EnumToken.TransformFunctionTokenDefType, "(", true);
76
+ assignTokenMap(whenElseFunc, EnumToken.WhenElseFunctionTokenDefType, "(");
77
+ assignTokenMap(wildCardFuncs, EnumToken.WildCardFunctionTokenDefType, "(");
78
+ const SymbolsMapTokensKeys = Object.keys(SymbolsMapTokens);
95
79
  // do not capture the value
96
80
  const hintsEnum = new Set([
97
81
  EnumToken.CommaTokenType,
@@ -134,818 +118,1494 @@ var TokenMap;
134
118
  TokenMap[TokenMap["PLUS"] = 43] = "PLUS";
135
119
  TokenMap[TokenMap["MINUS"] = 45] = "MINUS";
136
120
  TokenMap[TokenMap["GREATERTHAN"] = 62] = "GREATERTHAN";
121
+ TokenMap[TokenMap["PERCENTAGE"] = 37] = "PERCENTAGE";
137
122
  })(TokenMap || (TokenMap = {}));
138
- function consumeString(parseInfo) {
139
- const quote = next(parseInfo).charCodeAt(0);
140
- let charCode;
141
- let decodeSegments = false;
142
- const result = [];
143
- while ((charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset)) == charCode) {
144
- if (charCode == 92 /* TokenMap.REVERSE_SOLIDUS */) {
145
- if (charCode == parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1)) {
146
- next(parseInfo, 2);
147
- continue;
148
- }
149
- const sequence = peek(parseInfo, 7);
150
- let escapeSequence = "";
151
- let codepoint;
152
- let i;
153
- for (i = 1; i < sequence.length; i++) {
154
- codepoint = sequence.charCodeAt(i);
155
- if (codepoint == 0x20 ||
156
- (codepoint >= 0x61 && codepoint <= 0x66) ||
157
- (codepoint >= 0x41 && codepoint <= 0x46) ||
158
- (codepoint >= 0x30 && codepoint <= 0x39)) {
159
- escapeSequence += sequence[i];
160
- if (codepoint == 0x20) {
161
- break;
162
- }
163
- continue;
164
- }
123
+ function getSymbolHint(parseInfo, start, end) {
124
+ const len = end - start;
125
+ const keysLength = SymbolsMapTokensKeys.length;
126
+ // Early exit for impossible lengths
127
+ if (len < 0)
128
+ return null;
129
+ for (let i = 0; i < keysLength; i++) {
130
+ const key = SymbolsMapTokensKeys[i];
131
+ if (key.length !== len)
132
+ continue;
133
+ // Match character by character
134
+ let match = true;
135
+ for (let j = 0; j < len; j++) {
136
+ let ca = key.charCodeAt(j);
137
+ let cb = parseInfo.stream.charCodeAt(start + j);
138
+ // Normalize A-Z to a-z
139
+ if (ca >= 65 && ca <= 90)
140
+ ca += 32;
141
+ if (cb >= 65 && cb <= 90)
142
+ cb += 32;
143
+ if (ca !== cb) {
144
+ match = false;
165
145
  break;
166
146
  }
167
- if (escapeSequence.trimEnd().length > 0) {
168
- // const codepoint = parseInt(escapeSequence, 16);
169
- // TODO set decode flag ON
170
- // if (
171
- // codepoint == 0 ||
172
- // // leading surrogate
173
- // (0xd800 <= codepoint && codepoint <= 0xdbff) ||
174
- // // trailing surrogate
175
- // (0xdc00 <= codepoint && codepoint <= 0xdfff)
176
- // ) {
177
- // buffer += String.fromCodePoint(0xfffd);
178
- // } else {
179
- // buffer += String.fromCodePoint(codepoint);
180
- // }
181
- const length = escapeSequence.length +
182
- 1 +
183
- (isWhiteSpace(parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset)?.charCodeAt(0))
184
- ? 1
185
- : 0);
186
- decodeSegments = true;
187
- next(parseInfo, length);
188
- continue;
189
- }
190
- next(parseInfo, 2);
191
- continue;
192
147
  }
193
- if (charCode == quote) {
194
- next(parseInfo);
195
- result.push(yieldResult(parseInfo,
196
- /* hasNewLine ? EnumToken.BadStringTokenType : */ EnumToken.StringTokenType, decodeSegments ? { decodeSegments } : null));
197
- return result;
148
+ if (match) {
149
+ return SymbolsMapTokens[key];
198
150
  }
199
- if (isNewLine(charCode)) {
200
- next(parseInfo);
201
- result.push(yieldResult(parseInfo, EnumToken.BadStringTokenType));
202
- return result;
203
- }
204
- next(parseInfo);
205
151
  }
206
- // EOF - 'Unclosed-string' fixed
207
- result.push(yieldResult(parseInfo, EnumToken.StringTokenType));
208
- return result;
152
+ return null;
209
153
  }
210
- function yieldResult(parseInfo, hint, options) {
211
- let val = parseInfo.stream.slice(parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
212
- let token = null;
213
- let dimension;
214
- if (options?.decodeSegments) {
215
- val = val.replace(/\\([0-9a-fA-F]{1,6})(?:\s)?/g, (_, sequence) => {
216
- const codepoint = parseInt(sequence, 16);
217
- if (codepoint == 0 ||
218
- // leading surrogate
219
- (0xd800 <= codepoint && codepoint <= 0xdbff) ||
220
- // trailing surrogate
221
- (0xdc00 <= codepoint && codepoint <= 0xdfff) ||
222
- codepoint > 0x10ffff) {
223
- return "\uFFFD";
224
- }
225
- return String.fromCodePoint(codepoint);
226
- });
227
- }
228
- if (hint != null) {
229
- let searchArray = null;
230
- switch (hint) {
231
- case EnumToken.TransformFunctionTokenDefType:
232
- searchArray = transformFunctions;
233
- break;
234
- case EnumToken.ColorFunctionTokenDefType:
235
- searchArray = colorsFunc;
236
- break;
237
- case EnumToken.ContainerFunctionTokenDefType:
238
- searchArray = containerFunc;
239
- break;
240
- case EnumToken.UrlFunctionTokenDefType:
241
- searchArray = urlFunc;
242
- break;
243
- case EnumToken.GridTemplateFuncTokenDefType:
244
- searchArray = gridTemplateFunc;
245
- break;
246
- case EnumToken.ImageFunctionTokenDefType:
247
- searchArray = imageFunc;
248
- break;
249
- case EnumToken.TimelineFunctionTokenDefType:
250
- searchArray = timelineFunc;
251
- break;
252
- // case EnumToken.GeneralEnclosedFunctionTokenDefType:
253
- // searchArray = generalEnclosedFunc;
254
- // break;
255
- case EnumToken.SupportsFunctionTokenDefType:
256
- searchArray = supportFunc;
257
- break;
258
- case EnumToken.TimingFunctionTokenDefType:
259
- searchArray = timingFunc;
260
- break;
261
- case EnumToken.MathFunctionTokenDefType:
262
- searchArray = mathFuncs;
263
- break;
264
- case EnumToken.WhenElseFunctionTokenDefType:
265
- searchArray = whenElseFunc;
266
- break;
267
- case EnumToken.WildCardFunctionTokenDefType:
268
- searchArray = wildCardFuncs;
154
+ function searchArray(array, parseInfo, start, end) {
155
+ const len = end - start;
156
+ // Early exit for impossible lengths
157
+ if (len < 0)
158
+ return null;
159
+ // Use a simple linear search optimized with length pre-filtering
160
+ let i = array.length;
161
+ while (i--) {
162
+ if (array[i].length !== len)
163
+ continue;
164
+ // Match character by character
165
+ let match = true;
166
+ const arrayItem = array[i];
167
+ for (let j = 0; j < len; j++) {
168
+ let ca = arrayItem.charCodeAt(j);
169
+ let cb = parseInfo.stream.charCodeAt(start + j);
170
+ // Normalize A-Z to a-z
171
+ if (ca >= 65 && ca <= 90)
172
+ ca += 32;
173
+ if (cb >= 65 && cb <= 90)
174
+ cb += 32;
175
+ if (ca != cb) {
176
+ match = false;
269
177
  break;
270
- }
271
- if (searchArray != null) {
272
- val = searchArray.find((v) => equalsIgnoreCase(v, val));
273
- }
274
- token = hintsEnum.has(hint) ? { typ: hint } : { typ: hint, val };
275
- }
276
- else {
277
- let slice = val.slice(1);
278
- const chr = val.charAt(0);
279
- if (chr == "!" && equalsIgnoreCase("!important", val)) {
280
- token = {
281
- typ: EnumToken.ImportantTokenType,
282
- };
283
- }
284
- else if (chr == "@" && isIdent(slice)) {
285
- token = {
286
- typ: EnumToken.AtRuleTokenType,
287
- nam: slice,
288
- };
289
- }
290
- else if (chr == "." && isIdent(slice)) {
291
- token = {
292
- typ: EnumToken.ClassSelectorTokenType,
293
- val,
294
- };
295
- }
296
- else if (chr == "#") {
297
- if (isHexColor(val)) {
298
- token = {
299
- typ: EnumToken.ColorTokenType,
300
- val: val,
301
- kin: ColorType.HEX,
302
- };
303
- }
304
- else if (isHash(val)) {
305
- token = {
306
- typ: EnumToken.HashTokenType,
307
- val: val,
308
- };
309
178
  }
310
179
  }
311
- else if ("\"'".includes(chr)) {
312
- token = {
313
- typ: EnumToken.UnclosedStringTokenType,
314
- val: val,
315
- };
316
- }
317
- else if (isNumber(val)) {
318
- token =
319
- val[0] === "-" || val[0] === "+"
320
- ? {
321
- typ: EnumToken.NumberTokenType,
322
- sign: val[0],
323
- val: +val,
324
- }
325
- : {
326
- typ: EnumToken.NumberTokenType,
327
- val: +val,
328
- };
329
- }
330
- else if (isPercentage(val)) {
331
- token = {
332
- typ: EnumToken.PercentageTokenType,
333
- val: +val.slice(0, -1),
334
- };
335
- }
336
- else if ((dimension = parseDimension(val))) {
337
- token = dimension;
338
- }
339
- else if (isIdent(val)) {
340
- token = {
341
- typ: val.startsWith("--") ? EnumToken.DashedIdenTokenType : EnumToken.IdenTokenType,
342
- val,
343
- };
180
+ if (match) {
181
+ return arrayItem;
344
182
  }
345
183
  }
346
- if (token == null) {
347
- token = {
348
- typ: EnumToken.LiteralTokenType,
349
- val,
350
- };
351
- }
352
- // return token;
353
- token[LOC] = {
354
- srcId: parseInfo.source.id,
355
- sta: parseInfo.position,
356
- end: parseInfo.currentPosition,
357
- };
358
- parseInfo.position = parseInfo.currentPosition;
359
- return { token, bytesIn: parseInfo.currentPosition };
360
- }
361
- function match(parseInfo, input) {
362
- let position = parseInfo.currentPosition - parseInfo.offset;
363
- for (let i = 0; i < input.length; i++) {
364
- if (parseInfo.stream[position + i] != input.charAt(i)) {
365
- return false;
366
- }
367
- }
368
- return true;
369
- }
370
- function peek(parseInfo, count = 1) {
371
- if (count == 1) {
372
- return parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset);
373
- }
374
- const position = parseInfo.currentPosition - parseInfo.offset;
375
- return parseInfo.stream.slice(position, position + count);
184
+ return null;
376
185
  }
377
- function next(parseInfo, count = 1) {
378
- let position = parseInfo.currentPosition - parseInfo.offset;
379
- let char = count == 1 ? parseInfo.stream.charAt(position) : parseInfo.stream.slice(position, position + count);
380
- let i = 0;
381
- let codepoint;
382
- for (; i < char.length; i++) {
383
- codepoint = char[i].charCodeAt(0);
384
- if (codepoint == 0xa || // \n
385
- codepoint == 0xb || // \v
386
- codepoint == 0xc || // \f
387
- codepoint == 0xd || // \r
388
- codepoint == 0x2028 || // \u2028
389
- codepoint == 0x2029 // \u2029
390
- ) {
391
- // \r\n
392
- if (codepoint == 0xa && i > 0 && char.charCodeAt(i - 1) == 0xd) ;
393
- else {
394
- parseInfo.source.lineStarts.lineStarts.push(position + i);
186
+ /**
187
+ * tokenizer class
188
+ */
189
+ class Tokenizer {
190
+ parseInfo;
191
+ input;
192
+ /**
193
+ * token type
194
+ */
195
+ typ = null;
196
+ /**
197
+ * token kind
198
+ */
199
+ kin = null;
200
+ /**
201
+ * token name
202
+ */
203
+ nam = null;
204
+ /**
205
+ * token value
206
+ */
207
+ val = null;
208
+ /**
209
+ * token unit
210
+ */
211
+ unit = null;
212
+ /**
213
+ * source id
214
+ */
215
+ srcId = null;
216
+ /**
217
+ * token start
218
+ */
219
+ sta = null;
220
+ /**
221
+ * token end
222
+ */
223
+ end = null;
224
+ /**
225
+ * bytes in
226
+ */
227
+ bytesIn = null;
228
+ /**
229
+ * decode string
230
+ */
231
+ decodeString = null;
232
+ /**
233
+ * token slice
234
+ */
235
+ slice = null;
236
+ /**
237
+ * source file
238
+ */
239
+ source = null;
240
+ /**
241
+ * token hint
242
+ */
243
+ hint = null;
244
+ state = null;
245
+ constructor(parseInfo, input = null) {
246
+ this.parseInfo = parseInfo;
247
+ this.input = input;
248
+ if (typeof this.parseInfo == "string") {
249
+ if (typeof parseInfo == "string") {
250
+ this.parseInfo = {
251
+ stream: parseInfo,
252
+ source: new SourceFile(parseInfo, [], ""),
253
+ offset: 0,
254
+ time: 0,
255
+ position: 0,
256
+ currentPosition: 0,
257
+ };
395
258
  }
396
259
  }
397
260
  }
398
- parseInfo.currentPosition += char.length;
399
- return char;
400
- }
401
- function isIdentToken(parseInfo, start, end) {
402
- let j = parseInfo.currentPosition - parseInfo.offset;
403
- let i = parseInfo.position - parseInfo.offset;
404
- if (start != null) {
405
- if (end == null) {
406
- if (start < 0) {
407
- j += start;
408
- }
409
- else {
410
- i += start;
261
+ /**
262
+ *
263
+ * @param parseInfo
264
+ * @returns
265
+ */
266
+ consumeString(parseInfo) {
267
+ const quote = this.advance(parseInfo).charCodeAt(0);
268
+ let charCode;
269
+ let decodeSegments = false;
270
+ while ((charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset)) == charCode) {
271
+ if (charCode == 92 /* TokenMap.REVERSE_SOLIDUS */) {
272
+ if (charCode == parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1)) {
273
+ this.advance(parseInfo, 2);
274
+ continue;
275
+ }
276
+ const sequence = this.peek(parseInfo, 7);
277
+ let escapeSequence = "";
278
+ let codepoint;
279
+ let i;
280
+ for (i = 1; i < sequence.length; i++) {
281
+ codepoint = sequence.charCodeAt(i);
282
+ if (codepoint == 0x20 ||
283
+ (codepoint >= 0x61 && codepoint <= 0x66) ||
284
+ (codepoint >= 0x41 && codepoint <= 0x46) ||
285
+ (codepoint >= 0x30 && codepoint <= 0x39)) {
286
+ escapeSequence += sequence[i];
287
+ if (codepoint == 0x20) {
288
+ break;
289
+ }
290
+ continue;
291
+ }
292
+ break;
293
+ }
294
+ if (escapeSequence.trimEnd().length > 0) {
295
+ const length = escapeSequence.length +
296
+ 1 +
297
+ (isWhiteSpace(parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset)?.charCodeAt(0))
298
+ ? 1
299
+ : 0);
300
+ decodeSegments = true;
301
+ this.advance(parseInfo, length);
302
+ continue;
303
+ }
304
+ this.advance(parseInfo, 2);
305
+ continue;
411
306
  }
412
- }
413
- else {
414
- if (end < 0) {
415
- j += end;
307
+ if (charCode == quote) {
308
+ this.advance(parseInfo);
309
+ return this.makeToken(parseInfo,
310
+ /* hasNewLine ? EnumToken.BadStringTokenType : */ EnumToken.StringTokenType, decodeSegments ? { decodeSegments } : null);
416
311
  }
417
- else {
418
- j = parseInfo.position + end;
312
+ if (isNewLine(charCode)) {
313
+ this.advance(parseInfo);
314
+ return this.makeToken(parseInfo, EnumToken.BadStringTokenType);
419
315
  }
316
+ this.advance(parseInfo);
420
317
  }
318
+ // EOF - 'Unclosed-string' fixed
319
+ return this.makeToken(parseInfo, EnumToken.StringTokenType);
320
+ // return result;
421
321
  }
422
- j--;
423
- let codepoint = parseInfo.stream.charCodeAt(i);
424
- // -
425
- if (codepoint == 0x2d) {
426
- let nextCodepoint;
427
- if ((nextCodepoint = parseInfo.stream.charCodeAt(i + 1)) != nextCodepoint) {
428
- return false;
429
- }
430
- if (isDigit(nextCodepoint)) {
431
- return false;
432
- }
433
- codepoint = nextCodepoint;
434
- i++;
435
- }
436
- if (codepoint !== 0x2d && !isIdentStart(codepoint)) {
437
- return false;
438
- }
439
- if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
440
- codepoint = parseInfo.stream.charCodeAt(i + 1);
441
- // if (!isIdentCodepoint(codepoint)) {
442
- // return false;
443
- // }
444
- i += String.fromCodePoint(codepoint).length;
445
- // if (i < j) {
446
- // codepoint = name.charCodeAt(i) as number;
447
- // if (!isIdentCodepoint(codepoint)) {
448
- // return false;
449
- // }
450
- // }
451
- }
452
- while (i < j) {
453
- i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
454
- codepoint = parseInfo.stream.charCodeAt(i);
455
- if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
456
- i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
457
- codepoint = parseInfo.stream.charCodeAt(i);
458
- i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
459
- continue;
460
- }
461
- if (codepoint !== 0x2d && !isIdentCodepoint(codepoint)) {
462
- return false;
463
- }
464
- }
465
- return true;
466
- }
467
- function isPseudo(parseInfo) {
468
- let position = parseInfo.currentPosition - parseInfo.offset;
469
- let endPosition = parseInfo.currentPosition - parseInfo.offset;
470
- return (parseInfo.stream.charAt(position) == ":" &&
471
- parseInfo.stream.charAt(endPosition - 1) == "(" &&
472
- (parseInfo.stream.charAt(position + 1) == ":"
473
- ? isIdentToken(parseInfo, 2, -1)
474
- : isIdentToken(parseInfo, 1, -1))) ||
475
- parseInfo.stream.charAt(position + 1) == ":"
476
- ? isIdentToken(parseInfo, 2)
477
- : isIdentToken(parseInfo, 1);
478
- }
479
- function startsWith(parseInfo, input) {
480
- let i = 0;
481
- let j = input.length;
482
- while (i < j) {
483
- if (parseInfo.stream.charAt(parseInfo.position - parseInfo.offset + i) != input.charAt(i)) {
484
- return false;
322
+ /**
323
+ *
324
+ * @param parseInfo
325
+ * @returns
326
+ */
327
+ consumeURLToken(parseInfo) {
328
+ const quote = this.advance(parseInfo).charCodeAt(0);
329
+ let charCode;
330
+ let decodeSegments = false;
331
+ while ((charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset)) == charCode) {
332
+ if (charCode == 92 /* TokenMap.REVERSE_SOLIDUS */) {
333
+ if (charCode == parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1)) {
334
+ this.advance(parseInfo, 2);
335
+ continue;
336
+ }
337
+ const sequence = this.peek(parseInfo, 7);
338
+ let escapeSequence = "";
339
+ let codepoint;
340
+ let i;
341
+ for (i = 1; i < sequence.length; i++) {
342
+ codepoint = sequence.charCodeAt(i);
343
+ if (codepoint == 0x20 ||
344
+ (codepoint >= 0x61 && codepoint <= 0x66) ||
345
+ (codepoint >= 0x41 && codepoint <= 0x46) ||
346
+ (codepoint >= 0x30 && codepoint <= 0x39)) {
347
+ escapeSequence += sequence[i];
348
+ if (codepoint == 0x20) {
349
+ break;
350
+ }
351
+ continue;
352
+ }
353
+ break;
354
+ }
355
+ if (escapeSequence.trimEnd().length > 0) {
356
+ const length = escapeSequence.length +
357
+ 1 +
358
+ (isWhiteSpace(parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset)?.charCodeAt(0))
359
+ ? 1
360
+ : 0);
361
+ decodeSegments = true;
362
+ this.advance(parseInfo, length);
363
+ continue;
364
+ }
365
+ this.advance(parseInfo, 2);
366
+ continue;
367
+ }
368
+ if (charCode == quote) {
369
+ this.advance(parseInfo);
370
+ let k = 1;
371
+ let end = parseInfo.stream.length - parseInfo.offset;
372
+ let position = parseInfo.currentPosition - parseInfo.offset;
373
+ while (position + k < end) {
374
+ charCode = parseInfo.stream.charCodeAt(position);
375
+ // NaN != NaN
376
+ if (charCode != charCode) {
377
+ this.advance(parseInfo, k);
378
+ return this.makeToken(parseInfo, EnumToken.BadUrlTokenType);
379
+ }
380
+ if (isWhiteSpace(charCode)) {
381
+ this.advance(parseInfo, k);
382
+ k++;
383
+ continue;
384
+ }
385
+ if (charCode != 41 /* TokenMap.RIGHT_PARENTHESIS */) {
386
+ this.advance(parseInfo, k);
387
+ return this.makeToken(parseInfo, EnumToken.BadUrlTokenType);
388
+ }
389
+ break;
390
+ }
391
+ // consume until the ')'
392
+ return this.makeToken(parseInfo,
393
+ /* hasNewLine ? EnumToken.BadStringTokenType : */ EnumToken.StringTokenType, decodeSegments ? { decodeSegments } : null);
394
+ // return result;
395
+ }
396
+ if (isNewLine(charCode)) {
397
+ // bad string
398
+ this.advance(parseInfo);
399
+ while ((charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset)) == charCode) {
400
+ if (charCode == 92 /* TokenMap.REVERSE_SOLIDUS */) {
401
+ this.advance(parseInfo, 2);
402
+ continue;
403
+ }
404
+ if (charCode == 41 /* TokenMap.RIGHT_PARENTHESIS */) {
405
+ return this.makeToken(parseInfo, EnumToken.BadUrlTokenType);
406
+ }
407
+ this.advance(parseInfo);
408
+ }
409
+ return this.makeToken(parseInfo, EnumToken.BadStringTokenType);
410
+ }
411
+ this.advance(parseInfo);
485
412
  }
486
- i++;
413
+ // EOF - bad url token
414
+ return this.makeToken(parseInfo, EnumToken.BadUrlTokenType);
415
+ // return result;
487
416
  }
488
- return true;
489
- }
490
- function isURLToken(parseInfo) {
491
- let i = parseInfo.position - parseInfo.offset;
492
- let c;
493
- while (++i < parseInfo.currentPosition) {
494
- c = parseInfo.stream.charCodeAt(i);
495
- // single quote or double quote or start parenthesis or close parenthesis
496
- if (isNonPrintable(c) || c == 0x27 || c == 0x22 || c == 0x28 || c == 0x29) {
497
- return false;
417
+ /**
418
+ * consume number, dimension, or percentage
419
+ * @param parseInfo
420
+ * @returns
421
+ */
422
+ consumeNumericToken(parseInfo) {
423
+ let position = parseInfo.currentPosition - parseInfo.offset;
424
+ let offset = position;
425
+ let hasDigits = false;
426
+ let hasLetter = false;
427
+ let hasPercent = false;
428
+ let codepoint = parseInfo.stream.charCodeAt(position);
429
+ this.slice = null;
430
+ this.hint = null;
431
+ // '+' '-'
432
+ if (codepoint == 0x2b || codepoint == 0x2d) {
433
+ position++;
498
434
  }
499
- // valid escape
500
- if (c == 92 /* TokenMap.REVERSE_SOLIDUS */) {
501
- i++;
502
- if (i >= parseInfo.currentPosition) {
503
- return false;
435
+ // consume digits
436
+ while (position < parseInfo.stream.length) {
437
+ codepoint = parseInfo.stream.charCodeAt(position);
438
+ if (isDigit(codepoint)) {
439
+ hasDigits = true;
440
+ position++;
441
+ continue;
504
442
  }
505
- c = parseInfo.stream.charCodeAt(i);
506
- // c is not '\n' or '\r' or '\f'
507
- if (c == 0x6e || c == 0x72 || c == 0x66) {
508
- return false;
443
+ // '.' 'E' 'e'
444
+ if (codepoint == 0x2e || codepoint == 0x45 || codepoint == 0x65) {
445
+ position++;
446
+ break;
509
447
  }
510
- continue;
511
- }
512
- // is white space
513
- if (c == 0x20 || c == 0x09) {
514
- break;
515
- }
516
- }
517
- return i == parseInfo.currentPosition;
518
- }
519
- /**
520
- * Tokenize CSS string
521
- * @param parseInfo
522
- * @param yieldEOFToken
523
- */
524
- function tokenize(parseInfo, yieldEOFToken = true) {
525
- if (typeof parseInfo == "string") {
526
- parseInfo = {
527
- stream: parseInfo,
528
- source: new SourceFile(parseInfo, [], ""),
529
- offset: 0,
530
- time: 0,
531
- position: 0,
532
- currentPosition: 0,
533
- };
534
- }
535
- let charCode;
536
- let nextCharCode;
537
- const startTime = performance.now();
538
- const result = [];
539
- // allow 10 characters buffer for the streaming parser to avoid incomplete tokens
540
- const endPosition = parseInfo.stream.length - 1;
541
- // NaN is not equal to NaN
542
- while ((charCode = peek(parseInfo).charCodeAt(0)) == charCode) {
543
- switch (charCode) {
544
- case 61 /* TokenMap.EQUALS */:
545
- if (parseInfo.position < parseInfo.currentPosition) {
546
- result.push(yieldResult(parseInfo));
547
- }
548
- next(parseInfo);
549
- result.push(yieldResult(parseInfo, EnumToken.DelimTokenType));
448
+ if (isWhiteSpace(codepoint) ||
449
+ codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
450
+ codepoint == 59 /* TokenMap.SEMICOLON */ ||
451
+ codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
452
+ codepoint == 47 /* TokenMap.SLASH */ ||
453
+ codepoint == 42 /* TokenMap.STAR */ ||
454
+ codepoint == 44 /* TokenMap.COMMA */) {
455
+ return !hasDigits ? 0 : position - offset;
456
+ }
457
+ if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
458
+ hasPercent = true;
550
459
  break;
551
- // '+' or '-'
552
- case 43 /* TokenMap.PLUS */:
553
- case 45 /* TokenMap.MINUS */:
554
- nextCharCode = peek(parseInfo).charCodeAt(0);
555
- // not a number
556
- if (charCode === 43 /* TokenMap.PLUS */ && !(nextCharCode >= 0x30 && nextCharCode <= 0x39)) {
557
- if (parseInfo.position < parseInfo.currentPosition) {
558
- result.push(yieldResult(parseInfo));
559
- }
560
- next(parseInfo);
561
- result.push(yieldResult(parseInfo, SymbolsMapTokens[parseInfo.stream
562
- .slice(parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset)
563
- .toLowerCase()]));
564
- break;
565
- }
566
- next(parseInfo);
460
+ }
461
+ if (isLetter(codepoint)) {
462
+ hasLetter = true;
567
463
  break;
568
- // '{'
569
- case 123 /* TokenMap.LEFT_BRACE */:
570
- if (parseInfo.position < parseInfo.currentPosition) {
571
- result.push(yieldResult(parseInfo));
464
+ }
465
+ return 0;
466
+ }
467
+ if (!hasLetter && !hasPercent) {
468
+ // '.'
469
+ if (codepoint == 0x2e) {
470
+ codepoint = parseInfo.stream.charCodeAt(position);
471
+ if (codepoint != codepoint) {
472
+ return !hasDigits ? 0 : position - offset;
572
473
  }
573
- next(parseInfo);
574
- result.push(yieldResult(parseInfo, EnumToken.BlockStartTokenType));
575
- break;
576
- // '}'
577
- case 125 /* TokenMap.RIGHT_BRACE */:
578
- if (parseInfo.position < parseInfo.currentPosition) {
579
- result.push(yieldResult(parseInfo));
474
+ if (isWhiteSpace(codepoint) ||
475
+ codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
476
+ codepoint == 59 /* TokenMap.SEMICOLON */ ||
477
+ codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
478
+ codepoint == 47 /* TokenMap.SLASH */ ||
479
+ codepoint == 42 /* TokenMap.STAR */ ||
480
+ codepoint == 44 /* TokenMap.COMMA */) {
481
+ return !hasDigits ? 0 : position - offset;
580
482
  }
581
- next(parseInfo);
582
- result.push(yieldResult(parseInfo, EnumToken.BlockEndTokenType));
583
- break;
584
- // '('
585
- case 40 /* TokenMap.LEFT_PARENTHESIS */:
586
- if (parseInfo.position < parseInfo.currentPosition) {
587
- if (parseInfo.stream[parseInfo.position - parseInfo.offset] === ":" && isPseudo(parseInfo)) {
588
- next(parseInfo);
589
- result.push(yieldResult(parseInfo, EnumToken.PseudoClassFunctionTokenDefType));
483
+ if (!isDigit(codepoint)) {
484
+ if (!hasDigits) {
485
+ return 0;
486
+ }
487
+ if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
488
+ hasPercent = true;
489
+ }
490
+ else if (isLetter(codepoint)) {
491
+ hasLetter = true;
492
+ }
493
+ else {
494
+ return 0;
495
+ }
496
+ }
497
+ else {
498
+ position++;
499
+ hasDigits = true;
500
+ }
501
+ }
502
+ if (!hasLetter && !hasPercent) {
503
+ while (position + 1 <= parseInfo.stream.length) {
504
+ codepoint = parseInfo.stream.charCodeAt(position);
505
+ if (isDigit(codepoint)) {
506
+ position++;
507
+ continue;
508
+ }
509
+ if (!hasDigits) {
510
+ return 0;
511
+ }
512
+ // 'E' 'e'
513
+ if (codepoint == 0x45 || codepoint == 0x65) {
514
+ position++;
590
515
  break;
591
516
  }
592
- else if (isIdentToken(parseInfo)) {
593
- const hint = startsWith(parseInfo, "--")
594
- ? EnumToken.CustomFunctionTokenDefType
595
- : (SymbolsMapTokens[parseInfo.stream
596
- .slice(parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset)
597
- .toLowerCase() + "("] ?? EnumToken.FunctionTokenDefType);
598
- result.push(yieldResult(parseInfo, hint));
599
- next(parseInfo);
600
- // consume '('
601
- parseInfo.position = parseInfo.currentPosition;
602
- if (hint === EnumToken.UrlFunctionTokenDefType) {
603
- // consume an <url>
604
- while (isWhiteSpace(peek(parseInfo).charCodeAt(0))) {
605
- next(parseInfo);
517
+ if (isWhiteSpace(codepoint) ||
518
+ codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
519
+ codepoint == 59 /* TokenMap.SEMICOLON */ ||
520
+ codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
521
+ codepoint == 47 /* TokenMap.SLASH */ ||
522
+ codepoint == 42 /* TokenMap.STAR */ ||
523
+ codepoint == 44 /* TokenMap.COMMA */) {
524
+ return position - offset;
525
+ }
526
+ if (isLetter(codepoint)) {
527
+ hasLetter = true;
528
+ break;
529
+ }
530
+ if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
531
+ hasPercent = true;
532
+ break;
533
+ }
534
+ return 0;
535
+ }
536
+ // 'E' 'e' - 'em'
537
+ if ((codepoint == 0x45 || codepoint == 0x65) && hasDigits && !hasLetter && !hasPercent) {
538
+ if (isLetter(parseInfo.stream.charCodeAt(position))) {
539
+ hasLetter = true;
540
+ }
541
+ }
542
+ if (!hasLetter && !hasPercent) {
543
+ // 'E' 'e'
544
+ if (codepoint == 0x45 || codepoint == 0x65) {
545
+ codepoint = parseInfo.stream.charCodeAt(position + 1);
546
+ // '+' '-'
547
+ if (codepoint == 0x2b || codepoint == 0x2d) {
548
+ position++;
549
+ }
550
+ codepoint = position = parseInfo.stream.charCodeAt(position + 1);
551
+ if (!isDigit(codepoint)) {
552
+ if (!hasDigits) {
553
+ return 0;
554
+ }
555
+ if (isLetter(codepoint)) {
556
+ hasLetter = true;
606
557
  }
607
- charCode = peek(parseInfo).charCodeAt(0);
608
- let values = null;
609
- if (charCode == 34 /* TokenMap.DOUBLE_QUOTE */ || charCode == 39 /* TokenMap.SINGLE_QUOTE */) {
610
- values = consumeString(parseInfo);
558
+ else if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
559
+ hasPercent = true;
611
560
  }
612
561
  else {
613
- do {
614
- next(parseInfo);
615
- // value = peek(parseInfo);
616
- charCode = peek(parseInfo).charCodeAt(0);
617
- } while (
618
- // !(value === "/" && match(parseInfo, "/*") &&
619
- charCode !== 41 /* TokenMap.RIGHT_PARENTHESIS */ &&
620
- parseInfo.currentPosition < endPosition);
562
+ return 0;
621
563
  }
622
- if (values != null) {
623
- // NaN is not equal to NaN
624
- if ((charCode = peek(parseInfo).charCodeAt(0)) != charCode) {
625
- for (let i = 0; i < values.length; i++) {
626
- values[i].token.typ = EnumToken.BadUrlTokenType;
627
- }
628
- }
629
- result.push(...values);
564
+ }
565
+ }
566
+ if (!hasLetter && !hasPercent) {
567
+ while (++position < parseInfo.stream.length) {
568
+ codepoint = parseInfo.stream.charCodeAt(position);
569
+ // eof
570
+ if (codepoint != codepoint) {
571
+ break;
572
+ }
573
+ if (isDigit(codepoint)) {
574
+ position++;
575
+ continue;
576
+ }
577
+ if (!hasDigits) {
578
+ return 0;
630
579
  }
631
- else if (parseInfo.position < parseInfo.currentPosition) {
632
- result.push(yieldResult(parseInfo,
633
- // parseInfo.position < parseInfo.currentPosition
634
- (charCode = peek(parseInfo).charCodeAt(0)) != charCode || !isURLToken(parseInfo)
635
- ? EnumToken.BadUrlTokenType
636
- : EnumToken.UrlTokenTokenType));
580
+ if (isWhiteSpace(codepoint) ||
581
+ codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
582
+ codepoint == 59 /* TokenMap.SEMICOLON */ ||
583
+ codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
584
+ codepoint == 47 /* TokenMap.SLASH */ ||
585
+ codepoint == 42 /* TokenMap.STAR */ ||
586
+ codepoint == 44 /* TokenMap.COMMA */) {
587
+ return position - offset;
588
+ }
589
+ else if (isLetter(codepoint)) {
590
+ hasLetter = true;
591
+ break;
592
+ }
593
+ else if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
594
+ hasPercent = true;
595
+ break;
596
+ }
597
+ else {
598
+ return 0;
637
599
  }
638
600
  }
639
- break;
601
+ if (!hasLetter && !hasPercent) {
602
+ return position - offset;
603
+ }
640
604
  }
641
605
  }
642
- next(parseInfo);
643
- result.push(yieldResult(parseInfo, EnumToken.StartParensTokenType));
644
- break;
645
- // ')'
646
- case 41 /* TokenMap.RIGHT_PARENTHESIS */:
647
- if (parseInfo.position < parseInfo.currentPosition) {
648
- result.push(yieldResult(parseInfo));
606
+ }
607
+ }
608
+ if (!hasDigits) {
609
+ return 0;
610
+ }
611
+ if (hasPercent) {
612
+ const slice = position;
613
+ codepoint = parseInfo.stream.charCodeAt(++position);
614
+ if (codepoint != codepoint ||
615
+ isWhiteSpace(codepoint) ||
616
+ codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
617
+ codepoint == 59 /* TokenMap.SEMICOLON */ ||
618
+ codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
619
+ codepoint == 47 /* TokenMap.SLASH */ ||
620
+ codepoint == 42 /* TokenMap.STAR */ ||
621
+ codepoint == 44 /* TokenMap.COMMA */) {
622
+ this.slice = slice;
623
+ this.hint = EnumToken.PercentageTokenType;
624
+ return position - offset;
625
+ }
626
+ return 0;
627
+ }
628
+ if (hasLetter) {
629
+ codepoint = parseInfo.stream.charCodeAt(position - 1);
630
+ // 'E' 'e'
631
+ const slice = codepoint == 0x45 || codepoint == 0x65 ? position - 1 : position;
632
+ while (position + 1 <= parseInfo.stream.length) {
633
+ codepoint = parseInfo.stream.charCodeAt(++position);
634
+ if (!isLetter(codepoint)) {
635
+ break;
649
636
  }
650
- next(parseInfo);
651
- result.push(yieldResult(parseInfo, EnumToken.EndParensTokenType));
652
- break;
653
- // '['
654
- case 91 /* TokenMap.LEFT_BRACKETS */:
655
- if (parseInfo.position < parseInfo.currentPosition) {
656
- result.push(yieldResult(parseInfo));
637
+ }
638
+ if (codepoint != codepoint ||
639
+ isWhiteSpace(codepoint) ||
640
+ codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
641
+ codepoint == 59 /* TokenMap.SEMICOLON */ ||
642
+ codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
643
+ codepoint == 43 /* TokenMap.PLUS */ ||
644
+ codepoint == 47 /* TokenMap.SLASH */ ||
645
+ codepoint == 42 /* TokenMap.STAR */ ||
646
+ codepoint == 44 /* TokenMap.COMMA */) {
647
+ this.slice = slice;
648
+ this.hint = getSymbolHint(parseInfo, slice, position) ?? EnumToken.DimensionTokenType;
649
+ return position - offset;
650
+ }
651
+ return 0;
652
+ }
653
+ return 0;
654
+ }
655
+ /**
656
+ *
657
+ * @param parseInfo
658
+ * @returns
659
+ */
660
+ consumeIdentToken(parseInfo) {
661
+ let position = parseInfo.currentPosition - parseInfo.offset;
662
+ let offset = position;
663
+ let codepoint = parseInfo.stream.charCodeAt(position);
664
+ if (!isIdentStart(codepoint) && codepoint != 45 /* TokenMap.MINUS */) {
665
+ return 0;
666
+ }
667
+ if (codepoint == 45 /* TokenMap.MINUS */) {
668
+ position++;
669
+ codepoint = parseInfo.stream.charCodeAt(position);
670
+ if (!isIdentStart(codepoint) && codepoint != 45 /* TokenMap.MINUS */) {
671
+ return 0;
672
+ }
673
+ }
674
+ while ((codepoint = parseInfo.stream.charCodeAt(position)) == codepoint) {
675
+ if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
676
+ // eof
677
+ if ((codepoint = parseInfo.stream.charCodeAt(position + 1)) != codepoint) {
678
+ // this.next(parseInfo, position);
679
+ return 0;
657
680
  }
658
- next(parseInfo);
659
- result.push(yieldResult(parseInfo, EnumToken.AttrStartTokenType));
660
- break;
661
- // ']'
662
- case 93 /* TokenMap.RIGHT_BRACKETS */:
663
- if (parseInfo.position < parseInfo.currentPosition) {
664
- result.push(yieldResult(parseInfo));
681
+ // \n \r \f \v
682
+ if (codepoint == 0xa ||
683
+ codepoint == 0xb ||
684
+ codepoint == 0xc ||
685
+ codepoint == 0xd ||
686
+ codepoint == 0x2028 ||
687
+ codepoint == 0x2029) {
688
+ return 0;
665
689
  }
666
- next(parseInfo);
667
- result.push(yieldResult(parseInfo, EnumToken.AttrEndTokenType));
668
- break;
669
- case 59 /* TokenMap.SEMICOLON */:
670
- if (parseInfo.position < parseInfo.currentPosition) {
671
- result.push(yieldResult(parseInfo));
690
+ position += 2;
691
+ continue;
692
+ }
693
+ if (codepoint == 0x2d || isIdentCodepoint(codepoint)) {
694
+ position++;
695
+ }
696
+ else {
697
+ switch (codepoint) {
698
+ case 58 /* TokenMap.COLON */:
699
+ case 123 /* TokenMap.LEFT_BRACE */:
700
+ case 125 /* TokenMap.RIGHT_BRACE */:
701
+ case 40 /* TokenMap.LEFT_PARENTHESIS */:
702
+ case 41 /* TokenMap.RIGHT_PARENTHESIS */:
703
+ case 91 /* TokenMap.LEFT_BRACKETS */:
704
+ case 93 /* TokenMap.RIGHT_BRACKETS */:
705
+ case 59 /* TokenMap.SEMICOLON */:
706
+ case 33 /* TokenMap.EXCLAMATION */:
707
+ case 47 /* TokenMap.SLASH */:
708
+ case 35 /* TokenMap.HASH */:
709
+ case 42 /* TokenMap.STAR */:
710
+ case 61 /* TokenMap.EQUALS */:
711
+ case 126 /* TokenMap.TILDA */:
712
+ case 124 /* TokenMap.PIPE */:
713
+ case 94 /* TokenMap.CARET */:
714
+ case 36 /* TokenMap.DOLLAR */:
715
+ case 44 /* TokenMap.COMMA */:
716
+ case 62 /* TokenMap.GREATERTHAN */:
717
+ case 46 /* TokenMap.DOT */:
718
+ case 43 /* TokenMap.PLUS */:
719
+ return position - offset;
672
720
  }
673
- next(parseInfo);
674
- result.push(yieldResult(parseInfo, EnumToken.SemiColonTokenType));
675
- break;
676
- case 58 /* TokenMap.COLON */:
677
- if (parseInfo.position < parseInfo.currentPosition) {
678
- result.push(yieldResult(parseInfo));
721
+ if (codepoint != codepoint || isWhiteSpace(codepoint)) {
722
+ return position - offset;
679
723
  }
680
- next(parseInfo);
681
- if (peek(parseInfo).charCodeAt(0) == 58 /* TokenMap.COLON */) {
682
- next(parseInfo);
683
- result.push(yieldResult(parseInfo, EnumToken.DoubleColonTokenType));
724
+ return 0;
725
+ }
726
+ }
727
+ return position - offset;
728
+ }
729
+ /**
730
+ *
731
+ * @param parseInfo
732
+ * @returns
733
+ */
734
+ consumeColor(parseInfo) {
735
+ let position = parseInfo.currentPosition - parseInfo.offset;
736
+ let offset = position;
737
+ let codepoint = parseInfo.stream.charCodeAt(position);
738
+ if (codepoint != 35 /* TokenMap.HASH */) {
739
+ return 0;
740
+ }
741
+ position++;
742
+ let count = 0;
743
+ while (true) {
744
+ codepoint = parseInfo.stream.charCodeAt(position);
745
+ // 'a-f0-9' 'A-F0-9'
746
+ if ((codepoint >= 0x30 && codepoint <= 0x39) ||
747
+ (codepoint >= 0x61 && codepoint <= 0x66) ||
748
+ (codepoint >= 0x41 && codepoint <= 0x46)) {
749
+ position++;
750
+ count++;
751
+ continue;
752
+ }
753
+ break;
754
+ }
755
+ if (count != 3 && count != 4 && count != 6 && count != 8) {
756
+ return 0;
757
+ }
758
+ codepoint = parseInfo.stream.charCodeAt(position);
759
+ if (codepoint != codepoint ||
760
+ isWhiteSpace(codepoint) ||
761
+ codepoint == 41 /* TokenMap.RIGHT_PARENTHESIS */ ||
762
+ codepoint == 59 /* TokenMap.SEMICOLON */ ||
763
+ codepoint == 125 /* TokenMap.RIGHT_BRACE */ ||
764
+ codepoint == 44 /* TokenMap.COMMA */) {
765
+ return position - offset;
766
+ }
767
+ return 0;
768
+ }
769
+ parseURLToken(parseInfo, endPosition) {
770
+ let charCode;
771
+ // consume an <url>
772
+ while (isWhiteSpace(this.peekCharCode(parseInfo))) {
773
+ this.advance(parseInfo);
774
+ }
775
+ charCode = this.peekCharCode(parseInfo);
776
+ if (charCode == 34 /* TokenMap.DOUBLE_QUOTE */ || charCode == 39 /* TokenMap.SINGLE_QUOTE */) {
777
+ return this.consumeURLToken(parseInfo);
778
+ }
779
+ do {
780
+ this.advance(parseInfo);
781
+ charCode = this.peekCharCode(parseInfo);
782
+ } while (
783
+ // !(value === "/" && this.match(parseInfo, "/*") &&
784
+ charCode !== 41 /* TokenMap.RIGHT_PARENTHESIS */ &&
785
+ parseInfo.currentPosition < endPosition);
786
+ // if (parseInfo.position < parseInfo.currentPosition) {
787
+ return this.makeToken(parseInfo,
788
+ // parseInfo.position < parseInfo.currentPosition
789
+ (charCode = this.peekCharCode(parseInfo)) != charCode || !this.isURLToken(parseInfo)
790
+ ? EnumToken.BadUrlTokenType
791
+ : EnumToken.UrlTokenTokenType);
792
+ // }
793
+ }
794
+ /**
795
+ *
796
+ * @param parseInfo
797
+ * @param hint
798
+ * @param options
799
+ * @returns
800
+ */
801
+ makeToken(parseInfo, hint, options) {
802
+ let val = null;
803
+ this.typ = null;
804
+ this.nam = null;
805
+ this.val = null;
806
+ this.unit = null;
807
+ this.kin = null;
808
+ this.decodeString = null;
809
+ this.slice = null;
810
+ this.hint = null;
811
+ if (options?.slice) {
812
+ this.slice = options.slice;
813
+ }
814
+ if (options?.decodeSegments) {
815
+ this.decodeString = true;
816
+ }
817
+ if (hint != null) {
818
+ let array = null;
819
+ let hasUnit = false;
820
+ switch (hint) {
821
+ case EnumToken.TransformFunctionTokenDefType:
822
+ array = transformFunctions;
684
823
  break;
685
- }
686
- result.push(yieldResult(parseInfo, EnumToken.ColonTokenType));
687
- break;
688
- // \n \r \f \v \t space
689
- case 0x9:
690
- case 0x20:
691
- case 0xa:
692
- case 0xb:
693
- case 0xc:
694
- case 0xd:
695
- case 0x2028:
696
- case 0x2029:
697
- if (parseInfo.position < parseInfo.currentPosition) {
698
- result.push(yieldResult(parseInfo));
699
- }
700
- next(parseInfo);
701
- nextCharCode = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset).charCodeAt(0);
702
- while (nextCharCode == 0x20 ||
703
- (nextCharCode >= 0x9 && nextCharCode <= 0xd) ||
704
- nextCharCode == 0x2028 ||
705
- nextCharCode == 0x2029) {
706
- next(parseInfo);
707
- nextCharCode = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset).charCodeAt(0);
708
- }
709
- result.push(yieldResult(parseInfo, EnumToken.WhitespaceTokenType));
710
- break;
711
- case 44 /* TokenMap.COMMA */:
712
- if (parseInfo.position < parseInfo.currentPosition) {
713
- result.push(yieldResult(parseInfo));
714
- }
715
- next(parseInfo);
716
- result.push(yieldResult(parseInfo, EnumToken.CommaTokenType));
717
- break;
718
- case 36 /* TokenMap.DOLLAR */:
719
- if (parseInfo.position < parseInfo.currentPosition) {
720
- result.push(yieldResult(parseInfo));
721
- }
722
- if (match(parseInfo, "$=")) {
723
- next(parseInfo, 2);
724
- result.push(yieldResult(parseInfo, EnumToken.EndMatchTokenType));
824
+ case EnumToken.ColorFunctionTokenDefType:
825
+ array = colorsFunc;
725
826
  break;
726
- }
727
- next(parseInfo);
728
- break;
729
- case 126 /* TokenMap.TILDA */:
730
- if (parseInfo.position < parseInfo.currentPosition) {
731
- result.push(yieldResult(parseInfo));
732
- }
733
- if (match(parseInfo, "~=")) {
734
- next(parseInfo, 2);
735
- result.push(yieldResult(parseInfo, EnumToken.IncludeMatchTokenType));
827
+ case EnumToken.ContainerFunctionTokenDefType:
828
+ array = containerFunc;
829
+ break;
830
+ case EnumToken.UrlFunctionTokenDefType:
831
+ array = urlFunc;
832
+ break;
833
+ case EnumToken.GridTemplateFuncTokenDefType:
834
+ array = gridTemplateFunc;
835
+ break;
836
+ case EnumToken.ImageFunctionTokenDefType:
837
+ array = imageFunc;
838
+ break;
839
+ case EnumToken.TimelineFunctionTokenDefType:
840
+ array = timelineFunc;
841
+ break;
842
+ // case EnumToken.GeneralEnclosedFunctionTokenDefType:
843
+ // searchArray = generalEnclosedFunc;
844
+ // break;
845
+ case EnumToken.SupportsFunctionTokenDefType:
846
+ array = supportFunc;
847
+ break;
848
+ case EnumToken.TimingFunctionTokenDefType:
849
+ array = timingFunc;
850
+ break;
851
+ case EnumToken.MathFunctionTokenDefType:
852
+ array = mathFuncs;
736
853
  break;
854
+ case EnumToken.WhenElseFunctionTokenDefType:
855
+ array = whenElseFunc;
856
+ break;
857
+ case EnumToken.WildCardFunctionTokenDefType:
858
+ array = wildCardFuncs;
859
+ break;
860
+ case EnumToken.FrequencyTokenType:
861
+ array = frequencyUnits;
862
+ hasUnit = true;
863
+ break;
864
+ case EnumToken.ResolutionTokenType:
865
+ array = resolutionUnits;
866
+ hasUnit = true;
867
+ break;
868
+ case EnumToken.LengthTokenType:
869
+ array = dimensionUnits;
870
+ hasUnit = true;
871
+ break;
872
+ case EnumToken.FlexTokenType:
873
+ array = flexUnits;
874
+ hasUnit = true;
875
+ break;
876
+ case EnumToken.AngleTokenType:
877
+ array = angleUnits;
878
+ hasUnit = true;
879
+ break;
880
+ case EnumToken.TimeTokenType:
881
+ array = timeUnits;
882
+ hasUnit = true;
883
+ break;
884
+ case EnumToken.DimensionTokenType:
885
+ hasUnit = true;
886
+ break;
887
+ }
888
+ if (array != null) {
889
+ val = searchArray(array, parseInfo, hasUnit ? options?.slice : parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
890
+ }
891
+ else if (!hintsEnum.has(hint)) {
892
+ val = parseInfo.stream.slice(options?.slice ?? parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
893
+ }
894
+ if (this.decodeString) {
895
+ val = decodeEscapeSequences(val);
896
+ }
897
+ if (hintsEnum.has(hint)) {
898
+ this.typ = hint;
899
+ }
900
+ else {
901
+ this.typ = hint;
902
+ if (hasUnit || hint == EnumToken.PercentageTokenType || hint == EnumToken.DimensionTokenType) {
903
+ this.val = parseFloat(parseInfo.stream.slice(parseInfo.position - parseInfo.offset, options?.slice));
904
+ if (hint != EnumToken.PercentageTokenType) {
905
+ this.unit = val;
906
+ }
737
907
  }
738
- next(parseInfo);
739
- result.push(yieldResult(parseInfo, EnumToken.Tilda));
740
- break;
741
- // case '^':
742
- case 94 /* TokenMap.CARET */:
743
- if (parseInfo.position < parseInfo.currentPosition) {
744
- result.push(yieldResult(parseInfo));
908
+ else if (hint == EnumToken.NumberTokenType) {
909
+ this.val = parseFloat(val);
745
910
  }
746
- if (match(parseInfo, "^=")) {
747
- next(parseInfo, 2);
748
- result.push(yieldResult(parseInfo, EnumToken.StartMatchTokenType));
749
- break;
911
+ else if (hint == EnumToken.AtRuleTokenType) {
912
+ this.nam = val;
750
913
  }
751
- next(parseInfo);
752
- break;
753
- case 42 /* TokenMap.STAR */:
754
- if (parseInfo.position < parseInfo.currentPosition) {
755
- result.push(yieldResult(parseInfo));
914
+ else {
915
+ this.val = val;
916
+ if (hint == EnumToken.ColorTokenType) {
917
+ this.kin = ColorType.HEX;
918
+ }
756
919
  }
757
- if (match(parseInfo, "*=")) {
758
- next(parseInfo, 2);
759
- result.push(yieldResult(parseInfo, EnumToken.ContainMatchTokenType));
760
- break;
920
+ }
921
+ }
922
+ else {
923
+ if (this.equalsIgnoreCase(parseInfo, "!important")) {
924
+ this.typ = EnumToken.ImportantTokenType;
925
+ }
926
+ }
927
+ if (this.typ == null) {
928
+ val = parseInfo.stream.slice(parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset);
929
+ if (options?.decodeSegments) {
930
+ val = decodeEscapeSequences(val);
931
+ this.decodeString = true;
932
+ }
933
+ this.typ = EnumToken.LiteralTokenType;
934
+ this.val = val;
935
+ }
936
+ this.srcId = parseInfo.source.id;
937
+ this.sta = parseInfo.position;
938
+ this.end = parseInfo.currentPosition;
939
+ this.bytesIn = parseInfo.currentPosition;
940
+ parseInfo.position = parseInfo.currentPosition;
941
+ return this;
942
+ }
943
+ /**
944
+ *
945
+ * @param parseInfo
946
+ * @param input
947
+ * @returns
948
+ */
949
+ equalsIgnoreCase(parseInfo, input) {
950
+ let position = parseInfo.currentPosition - parseInfo.offset;
951
+ let ca;
952
+ let cb;
953
+ for (let i = 0; i < input.length; i++) {
954
+ ca = parseInfo.stream.charCodeAt(position + i);
955
+ cb = input.charCodeAt(i);
956
+ // Normalize A-Z to a-z
957
+ if (ca >= 65 && ca <= 90)
958
+ ca += 32;
959
+ if (cb >= 65 && cb <= 90)
960
+ cb += 32;
961
+ if (ca != cb) {
962
+ return false;
963
+ }
964
+ }
965
+ return true;
966
+ }
967
+ /**
968
+ *
969
+ * @param parseInfo
970
+ * @param input
971
+ * @returns
972
+ */
973
+ match(parseInfo, input) {
974
+ let position = parseInfo.currentPosition - parseInfo.offset;
975
+ for (let i = 0; i < input.length; i++) {
976
+ if (parseInfo.stream[position + i] != input.charAt(i)) {
977
+ return false;
978
+ }
979
+ }
980
+ return true;
981
+ }
982
+ /**
983
+ * Get the current character code without creating a string
984
+ * @param parseInfo
985
+ * @returns charCode at current position
986
+ */
987
+ peekCharCode(parseInfo) {
988
+ return parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
989
+ }
990
+ /**
991
+ *
992
+ * @param parseInfo
993
+ * @param count
994
+ * @returns
995
+ */
996
+ peek(parseInfo, count = 1) {
997
+ if (count == 1) {
998
+ return parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset);
999
+ }
1000
+ const position = parseInfo.currentPosition - parseInfo.offset;
1001
+ return parseInfo.stream.slice(position, position + count);
1002
+ }
1003
+ /**
1004
+ *
1005
+ * @param parseInfo
1006
+ * @param count
1007
+ * @returns
1008
+ */
1009
+ advance(parseInfo, count = 1) {
1010
+ let position = parseInfo.currentPosition - parseInfo.offset;
1011
+ let char = count == 1 ? parseInfo.stream.charAt(position) : parseInfo.stream.slice(position, position + count);
1012
+ let i = 0;
1013
+ let codepoint;
1014
+ const lineStarts = parseInfo.source.lineStarts.lineStarts;
1015
+ for (; i < char.length; i++) {
1016
+ codepoint = char.charCodeAt(i);
1017
+ if (codepoint == 0xa || // \n
1018
+ codepoint == 0xb || // \v
1019
+ codepoint == 0xc || // \f
1020
+ codepoint == 0xd || // \r
1021
+ codepoint == 0x2028 || // \u2028
1022
+ codepoint == 0x2029 // \u2029
1023
+ ) {
1024
+ // \r\n
1025
+ if (codepoint == 0xa && i > 0 && char.charCodeAt(i - 1) == 0xd) ;
1026
+ else {
1027
+ lineStarts.push(position + parseInfo.offset + i);
761
1028
  }
762
- next(parseInfo);
763
- result.push(yieldResult(parseInfo, EnumToken.Star));
764
- break;
765
- case 38 /* TokenMap.AMPERSAND */:
766
- if (parseInfo.position < parseInfo.currentPosition) {
767
- result.push(yieldResult(parseInfo));
1029
+ }
1030
+ }
1031
+ parseInfo.currentPosition += char.length;
1032
+ return char;
1033
+ }
1034
+ /**
1035
+ *
1036
+ * @param parseInfo
1037
+ * @param start
1038
+ * @param end
1039
+ * @returns
1040
+ */
1041
+ isIdentToken(parseInfo, start, end) {
1042
+ let j = parseInfo.currentPosition - parseInfo.offset;
1043
+ let i = parseInfo.position - parseInfo.offset;
1044
+ if (start != null) {
1045
+ if (end == null) {
1046
+ if (start < 0) {
1047
+ j += start;
768
1048
  }
769
- next(parseInfo);
770
- result.push(yieldResult(parseInfo, EnumToken.NestingSelectorTokenType));
771
- break;
772
- case 124 /* TokenMap.PIPE */:
773
- if (parseInfo.position < parseInfo.currentPosition) {
774
- result.push(yieldResult(parseInfo));
1049
+ else {
1050
+ i += start;
775
1051
  }
776
- // '||'
777
- if (match(parseInfo, "||")) {
778
- next(parseInfo, 2);
779
- result.push(yieldResult(parseInfo, EnumToken.ColumnCombinatorTokenType));
780
- break;
1052
+ }
1053
+ else {
1054
+ if (end < 0) {
1055
+ j += end;
781
1056
  }
782
- else if (match(parseInfo, "|=")) {
783
- next(parseInfo, 2);
784
- result.push(yieldResult(parseInfo, EnumToken.DashMatchTokenType));
785
- break;
1057
+ else {
1058
+ j = parseInfo.position + end;
786
1059
  }
787
- next(parseInfo);
788
- result.push(yieldResult(parseInfo, EnumToken.Pipe));
789
- break;
790
- case 33 /* TokenMap.EXCLAMATION */:
791
- if (parseInfo.position < parseInfo.currentPosition) {
792
- result.push(yieldResult(parseInfo));
1060
+ }
1061
+ }
1062
+ j--;
1063
+ let codepoint = parseInfo.stream.charCodeAt(i);
1064
+ // -
1065
+ if (codepoint == 0x2d) {
1066
+ let nextCodepoint;
1067
+ // NaN != NaN
1068
+ if ((nextCodepoint = parseInfo.stream.charCodeAt(i + 1)) != nextCodepoint) {
1069
+ return false;
1070
+ }
1071
+ if (!isIdentStart(nextCodepoint) && nextCodepoint != 0x2d) {
1072
+ return false;
1073
+ }
1074
+ codepoint = nextCodepoint;
1075
+ i++;
1076
+ }
1077
+ if (codepoint !== 0x2d && !isIdentStart(codepoint)) {
1078
+ return false;
1079
+ }
1080
+ if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
1081
+ codepoint = parseInfo.stream.charCodeAt(i + 1);
1082
+ i += String.fromCodePoint(codepoint).length;
1083
+ }
1084
+ while (i < j) {
1085
+ i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1086
+ codepoint = parseInfo.stream.charCodeAt(i);
1087
+ if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
1088
+ i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1089
+ codepoint = parseInfo.stream.charCodeAt(i);
1090
+ i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1091
+ continue;
1092
+ }
1093
+ if (codepoint !== 0x2d && !isIdentCodepoint(codepoint)) {
1094
+ return false;
1095
+ }
1096
+ }
1097
+ return true;
1098
+ }
1099
+ /**
1100
+ *
1101
+ * @param parseInfo
1102
+ * @returns
1103
+ */
1104
+ isPseudo(parseInfo) {
1105
+ let position = parseInfo.currentPosition - parseInfo.offset;
1106
+ let endPosition = parseInfo.currentPosition - parseInfo.offset;
1107
+ return (parseInfo.stream.charAt(position) == ":" &&
1108
+ parseInfo.stream.charAt(endPosition - 1) == "(" &&
1109
+ (parseInfo.stream.charAt(position + 1) == ":"
1110
+ ? this.isIdentToken(parseInfo, 2, -1)
1111
+ : this.isIdentToken(parseInfo, 1, -1))) ||
1112
+ parseInfo.stream.charAt(position + 1) == ":"
1113
+ ? this.isIdentToken(parseInfo, 2)
1114
+ : this.isIdentToken(parseInfo, 1);
1115
+ }
1116
+ /**
1117
+ *
1118
+ * @param parseInfo
1119
+ * @param input
1120
+ * @returns
1121
+ */
1122
+ startsWith(parseInfo, input) {
1123
+ let i = 0;
1124
+ let j = input.length;
1125
+ while (i < j) {
1126
+ if (parseInfo.stream.charAt(parseInfo.position - parseInfo.offset + i) != input.charAt(i)) {
1127
+ return false;
1128
+ }
1129
+ i++;
1130
+ }
1131
+ return true;
1132
+ }
1133
+ /**
1134
+ *
1135
+ * @param parseInfo
1136
+ * @returns
1137
+ */
1138
+ isURLToken(parseInfo) {
1139
+ let i = parseInfo.position - parseInfo.offset;
1140
+ let c;
1141
+ while (++i < parseInfo.currentPosition) {
1142
+ c = parseInfo.stream.charCodeAt(i);
1143
+ // single quote or double quote or start parenthesis or close parenthesis
1144
+ if (isNonPrintable(c) || c == 0x27 || c == 0x22 || c == 0x28 || c == 0x29) {
1145
+ return false;
1146
+ }
1147
+ // valid escape
1148
+ if (c == 92 /* TokenMap.REVERSE_SOLIDUS */) {
1149
+ i++;
1150
+ if (i >= parseInfo.currentPosition) {
1151
+ return false;
793
1152
  }
794
- if (match(parseInfo, "!important")) {
795
- next(parseInfo, 10);
796
- result.push(yieldResult(parseInfo, EnumToken.ImportantTokenType));
797
- break;
1153
+ c = parseInfo.stream.charCodeAt(i);
1154
+ // c is not '\n' or '\r' or '\f'
1155
+ if (c == 0x6e || c == 0x72 || c == 0x66) {
1156
+ return false;
798
1157
  }
799
- next(parseInfo);
1158
+ continue;
1159
+ }
1160
+ // is white space
1161
+ if (c == 0x20 || c == 0x09) {
800
1162
  break;
801
- case 47 /* TokenMap.SLASH */:
802
- if (parseInfo.position < parseInfo.currentPosition) {
803
- result.push(yieldResult(parseInfo));
804
- }
805
- if (!match(parseInfo, "/*")) {
806
- next(parseInfo);
807
- result.push(yieldResult(parseInfo, SymbolsMapTokens[parseInfo.stream.slice(parseInfo.position, parseInfo.currentPosition)]));
808
- break;
1163
+ }
1164
+ }
1165
+ return i == parseInfo.currentPosition;
1166
+ }
1167
+ done() {
1168
+ return this.typ === EnumToken.EOF;
1169
+ }
1170
+ /**
1171
+ * Tokenize CSS string
1172
+ * @param parseInfo
1173
+ * @param yieldEOFToken
1174
+ */
1175
+ next( /* parseInfo: ParseInfo | string, yieldEOFToken: boolean = true */) {
1176
+ const parseInfo = this.parseInfo;
1177
+ this.source = parseInfo.source;
1178
+ let charCode;
1179
+ let nextCharCode;
1180
+ // const result: TokenizeResult[] = [];
1181
+ // allow 10 characters buffer for the streaming parser to avoid incomplete tokens
1182
+ const endPosition = parseInfo.stream.length - 1; // yieldEOFToken ? parseInfo.stream.length - 1 : parseInfo.stream.length - 10;
1183
+ let tokensCount;
1184
+ // NaN is not equal to NaN
1185
+ while ((charCode = this.peekCharCode(parseInfo)) == charCode) {
1186
+ if (this.state === EnumToken.UrlFunctionTokenDefType) {
1187
+ this.state = null;
1188
+ return this.parseURLToken(parseInfo, endPosition);
1189
+ }
1190
+ if (parseInfo.position == parseInfo.currentPosition) {
1191
+ if (charCode == 45 /* TokenMap.MINUS */ ||
1192
+ charCode == 43 /* TokenMap.PLUS */ ||
1193
+ charCode == 46 /* TokenMap.DOT */ ||
1194
+ isDigit(charCode)) {
1195
+ tokensCount = this.consumeNumericToken(parseInfo);
1196
+ if (tokensCount > 0) {
1197
+ this.advance(parseInfo, tokensCount);
1198
+ return this.makeToken(parseInfo, this.hint ?? EnumToken.NumberTokenType, {
1199
+ slice: this.slice,
1200
+ sign: charCode == 45 /* TokenMap.MINUS */ ? "-" : charCode == 43 /* TokenMap.PLUS */ ? "+" : null,
1201
+ });
1202
+ }
809
1203
  }
810
- next(parseInfo, 2);
811
- while ((charCode = next(parseInfo).charCodeAt(0)) == charCode) {
812
- if (charCode == 42 /* TokenMap.STAR */) {
813
- if (match(parseInfo, "/")) {
814
- next(parseInfo);
815
- result.push(yieldResult(parseInfo, EnumToken.CommentTokenType));
816
- break;
1204
+ if (isIdentStart(charCode) || charCode == 45 /* TokenMap.MINUS */) {
1205
+ tokensCount = this.consumeIdentToken(parseInfo);
1206
+ if (tokensCount > 0) {
1207
+ this.advance(parseInfo, tokensCount);
1208
+ charCode = this.peekCharCode(parseInfo);
1209
+ // do not match function
1210
+ if (40 /* TokenMap.LEFT_PARENTHESIS */ != charCode) {
1211
+ return this.makeToken(parseInfo, this.startsWith(parseInfo, "--")
1212
+ ? EnumToken.DashedIdenTokenType
1213
+ : EnumToken.IdenTokenType);
817
1214
  }
818
1215
  }
819
- // else {
820
- // buffer += value;
821
- // }
822
1216
  }
823
- if (parseInfo.position < parseInfo.currentPosition) {
824
- result.push(yieldResult(parseInfo, EnumToken.BadCommentTokenType));
1217
+ if (charCode == 64 /* TokenMap.AT */) {
1218
+ this.advance(parseInfo);
1219
+ charCode = this.peekCharCode(parseInfo);
1220
+ // match at-rule
1221
+ if (charCode == 45 /* TokenMap.MINUS */ || isIdentStart(this.peekCharCode(parseInfo))) {
1222
+ // consume '@'
1223
+ parseInfo.position = parseInfo.currentPosition;
1224
+ tokensCount = this.consumeIdentToken(parseInfo);
1225
+ if (tokensCount > 0) {
1226
+ this.advance(parseInfo, tokensCount);
1227
+ return this.makeToken(parseInfo, EnumToken.AtRuleTokenType);
1228
+ }
1229
+ }
825
1230
  }
826
- break;
827
- case 62 /* TokenMap.GREATERTHAN */:
828
- if (parseInfo.position < parseInfo.currentPosition) {
829
- result.push(yieldResult(parseInfo));
1231
+ if (charCode == 35 /* TokenMap.HASH */) {
1232
+ tokensCount = this.consumeColor(parseInfo);
1233
+ if (tokensCount > 0) {
1234
+ this.advance(parseInfo, tokensCount);
1235
+ return this.makeToken(parseInfo, EnumToken.ColorTokenType);
1236
+ }
1237
+ this.advance(parseInfo);
1238
+ tokensCount = this.consumeIdentToken(parseInfo);
1239
+ if (tokensCount > 0) {
1240
+ this.advance(parseInfo, tokensCount);
1241
+ return this.makeToken(parseInfo, EnumToken.HashTokenType);
1242
+ }
830
1243
  }
831
- if (match(parseInfo, ">=")) {
832
- next(parseInfo, 2);
833
- result.push(yieldResult(parseInfo, EnumToken.GteTokenType));
1244
+ }
1245
+ // EOF
1246
+ switch (charCode) {
1247
+ case 61 /* TokenMap.EQUALS */:
1248
+ if (parseInfo.position < parseInfo.currentPosition) {
1249
+ return this.makeToken(parseInfo);
1250
+ }
1251
+ this.advance(parseInfo);
1252
+ return this.makeToken(parseInfo, EnumToken.DelimTokenType);
1253
+ // '+' or '-'
1254
+ case 43 /* TokenMap.PLUS */:
1255
+ if (parseInfo.position < parseInfo.currentPosition) {
1256
+ return this.makeToken(parseInfo);
1257
+ }
1258
+ this.advance(parseInfo);
1259
+ charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
1260
+ if (isDigit(charCode)) {
1261
+ tokensCount = this.consumeNumericToken(parseInfo);
1262
+ if (tokensCount > 0) {
1263
+ this.advance(parseInfo, tokensCount);
1264
+ return this.makeToken(parseInfo, this.hint ?? EnumToken.NumberTokenType, {
1265
+ slice: this.slice,
1266
+ sign: "+",
1267
+ });
1268
+ }
1269
+ }
1270
+ return this.makeToken(parseInfo, EnumToken.Plus);
1271
+ case 45 /* TokenMap.MINUS */:
1272
+ if (parseInfo.position == parseInfo.currentPosition) {
1273
+ nextCharCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1);
1274
+ // not a number
1275
+ if (isWhiteSpace(nextCharCode)) {
1276
+ this.advance(parseInfo);
1277
+ return this.makeToken(parseInfo, EnumToken.Sub);
1278
+ }
1279
+ if (charCode == 45 /* TokenMap.MINUS */ &&
1280
+ (nextCharCode == 45 /* TokenMap.MINUS */ || isIdentStart(nextCharCode))) {
1281
+ this.advance(parseInfo);
1282
+ tokensCount = this.consumeIdentToken(parseInfo);
1283
+ if (tokensCount > 0) {
1284
+ this.advance(parseInfo, tokensCount);
1285
+ return this.makeToken(parseInfo, EnumToken.IdenTokenType);
1286
+ }
1287
+ }
1288
+ }
1289
+ this.advance(parseInfo);
834
1290
  break;
835
- }
836
- next(parseInfo);
837
- result.push(yieldResult(parseInfo, EnumToken.GtTokenType));
838
- break;
839
- case 60 /* TokenMap.LOWERTHAN */:
840
- if (parseInfo.position < parseInfo.currentPosition) {
841
- result.push(yieldResult(parseInfo));
842
- }
843
- if (match(parseInfo, "<=")) {
844
- next(parseInfo, 2);
845
- result.push(yieldResult(parseInfo, EnumToken.LteTokenType));
1291
+ // '{'
1292
+ case 123 /* TokenMap.LEFT_BRACE */:
1293
+ if (parseInfo.position < parseInfo.currentPosition) {
1294
+ return this.makeToken(parseInfo);
1295
+ }
1296
+ this.advance(parseInfo);
1297
+ return this.makeToken(parseInfo, EnumToken.BlockStartTokenType);
1298
+ // '}'
1299
+ case 125 /* TokenMap.RIGHT_BRACE */:
1300
+ if (parseInfo.position < parseInfo.currentPosition) {
1301
+ return this.makeToken(parseInfo);
1302
+ }
1303
+ this.advance(parseInfo);
1304
+ return this.makeToken(parseInfo, EnumToken.BlockEndTokenType);
1305
+ // '('
1306
+ case 40 /* TokenMap.LEFT_PARENTHESIS */:
1307
+ if (parseInfo.position < parseInfo.currentPosition) {
1308
+ if (parseInfo.stream[parseInfo.position - parseInfo.offset] === ":" &&
1309
+ this.isPseudo(parseInfo)) {
1310
+ this.advance(parseInfo);
1311
+ return this.makeToken(parseInfo, EnumToken.PseudoClassFunctionTokenDefType);
1312
+ }
1313
+ else if (this.isIdentToken(parseInfo)) {
1314
+ const hint = this.startsWith(parseInfo, "--")
1315
+ ? EnumToken.CustomFunctionTokenDefType
1316
+ : (getSymbolHint(parseInfo, parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset + 1) ?? EnumToken.FunctionTokenDefType);
1317
+ this.makeToken(parseInfo, hint);
1318
+ this.advance(parseInfo);
1319
+ // consume '('
1320
+ parseInfo.position = parseInfo.currentPosition;
1321
+ if (hint === EnumToken.UrlFunctionTokenDefType) {
1322
+ this.state = hint;
1323
+ }
1324
+ return this;
1325
+ }
1326
+ }
1327
+ this.advance(parseInfo);
1328
+ return this.makeToken(parseInfo, EnumToken.StartParensTokenType);
1329
+ // ')'
1330
+ case 41 /* TokenMap.RIGHT_PARENTHESIS */:
1331
+ if (parseInfo.position < parseInfo.currentPosition) {
1332
+ return this.makeToken(parseInfo);
1333
+ }
1334
+ this.advance(parseInfo);
1335
+ return this.makeToken(parseInfo, EnumToken.EndParensTokenType);
1336
+ // '['
1337
+ case 91 /* TokenMap.LEFT_BRACKETS */:
1338
+ if (parseInfo.position < parseInfo.currentPosition) {
1339
+ return this.makeToken(parseInfo);
1340
+ }
1341
+ this.advance(parseInfo);
1342
+ return this.makeToken(parseInfo, EnumToken.AttrStartTokenType);
1343
+ // ']'
1344
+ case 93 /* TokenMap.RIGHT_BRACKETS */:
1345
+ if (parseInfo.position < parseInfo.currentPosition) {
1346
+ return this.makeToken(parseInfo);
1347
+ }
1348
+ this.advance(parseInfo);
1349
+ return this.makeToken(parseInfo, EnumToken.AttrEndTokenType);
1350
+ case 59 /* TokenMap.SEMICOLON */:
1351
+ if (parseInfo.position < parseInfo.currentPosition) {
1352
+ return this.makeToken(parseInfo);
1353
+ }
1354
+ this.advance(parseInfo);
1355
+ return this.makeToken(parseInfo, EnumToken.SemiColonTokenType);
1356
+ case 58 /* TokenMap.COLON */:
1357
+ if (parseInfo.position < parseInfo.currentPosition) {
1358
+ return this.makeToken(parseInfo);
1359
+ }
1360
+ this.advance(parseInfo);
1361
+ if (this.peekCharCode(parseInfo) == 58 /* TokenMap.COLON */) {
1362
+ this.advance(parseInfo);
1363
+ return this.makeToken(parseInfo, EnumToken.DoubleColonTokenType);
1364
+ }
1365
+ return this.makeToken(parseInfo, EnumToken.ColonTokenType);
1366
+ // \n \r \f \v \t space
1367
+ case 0x9:
1368
+ case 0x20:
1369
+ case 0xa:
1370
+ case 0xb:
1371
+ case 0xc:
1372
+ case 0xd:
1373
+ case 0x2028:
1374
+ case 0x2029:
1375
+ if (parseInfo.position < parseInfo.currentPosition) {
1376
+ return this.makeToken(parseInfo);
1377
+ }
1378
+ this.advance(parseInfo);
1379
+ nextCharCode = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset).charCodeAt(0);
1380
+ while (nextCharCode == 0x20 ||
1381
+ (nextCharCode >= 0x9 && nextCharCode <= 0xd) ||
1382
+ nextCharCode == 0x2028 ||
1383
+ nextCharCode == 0x2029) {
1384
+ this.advance(parseInfo);
1385
+ nextCharCode = parseInfo.stream
1386
+ .charAt(parseInfo.currentPosition - parseInfo.offset)
1387
+ .charCodeAt(0);
1388
+ }
1389
+ return this.makeToken(parseInfo, EnumToken.WhitespaceTokenType);
1390
+ case 44 /* TokenMap.COMMA */:
1391
+ if (parseInfo.position < parseInfo.currentPosition) {
1392
+ return this.makeToken(parseInfo);
1393
+ }
1394
+ this.advance(parseInfo);
1395
+ return this.makeToken(parseInfo, EnumToken.CommaTokenType);
1396
+ case 36 /* TokenMap.DOLLAR */:
1397
+ if (parseInfo.position < parseInfo.currentPosition) {
1398
+ return this.makeToken(parseInfo);
1399
+ }
1400
+ if (this.match(parseInfo, "$=")) {
1401
+ this.advance(parseInfo, 2);
1402
+ return this.makeToken(parseInfo, EnumToken.EndMatchTokenType);
1403
+ }
1404
+ this.advance(parseInfo);
846
1405
  break;
847
- }
848
- next(parseInfo);
849
- if (match(parseInfo, "!--")) {
850
- next(parseInfo, 3);
851
- while ((charCode = next(parseInfo).charCodeAt(0)) == charCode) {
852
- if (charCode == 45 /* TokenMap.MINUS */ && match(parseInfo, "->")) {
853
- break;
1406
+ case 126 /* TokenMap.TILDA */:
1407
+ if (parseInfo.position < parseInfo.currentPosition) {
1408
+ return this.makeToken(parseInfo);
1409
+ }
1410
+ if (this.match(parseInfo, "~=")) {
1411
+ this.advance(parseInfo, 2);
1412
+ return this.makeToken(parseInfo, EnumToken.IncludeMatchTokenType);
1413
+ }
1414
+ this.advance(parseInfo);
1415
+ return this.makeToken(parseInfo, EnumToken.Tilda);
1416
+ // case '^':
1417
+ case 94 /* TokenMap.CARET */:
1418
+ if (parseInfo.position < parseInfo.currentPosition) {
1419
+ return this.makeToken(parseInfo);
1420
+ }
1421
+ if (this.match(parseInfo, "^=")) {
1422
+ this.advance(parseInfo, 2);
1423
+ return this.makeToken(parseInfo, EnumToken.StartMatchTokenType);
1424
+ }
1425
+ this.advance(parseInfo);
1426
+ break;
1427
+ case 42 /* TokenMap.STAR */:
1428
+ if (parseInfo.position < parseInfo.currentPosition) {
1429
+ return this.makeToken(parseInfo);
1430
+ }
1431
+ if (this.match(parseInfo, "*=")) {
1432
+ this.advance(parseInfo, 2);
1433
+ return this.makeToken(parseInfo, EnumToken.ContainMatchTokenType);
1434
+ }
1435
+ this.advance(parseInfo);
1436
+ return this.makeToken(parseInfo, EnumToken.Star);
1437
+ case 38 /* TokenMap.AMPERSAND */:
1438
+ if (parseInfo.position < parseInfo.currentPosition) {
1439
+ return this.makeToken(parseInfo);
1440
+ }
1441
+ this.advance(parseInfo);
1442
+ return this.makeToken(parseInfo, EnumToken.NestingSelectorTokenType);
1443
+ case 124 /* TokenMap.PIPE */:
1444
+ if (parseInfo.position < parseInfo.currentPosition) {
1445
+ return this.makeToken(parseInfo);
1446
+ }
1447
+ // '||'
1448
+ if (this.match(parseInfo, "||")) {
1449
+ this.advance(parseInfo, 2);
1450
+ return this.makeToken(parseInfo, EnumToken.ColumnCombinatorTokenType);
1451
+ }
1452
+ else if (this.match(parseInfo, "|=")) {
1453
+ this.advance(parseInfo, 2);
1454
+ return this.makeToken(parseInfo, EnumToken.DashMatchTokenType);
1455
+ }
1456
+ this.advance(parseInfo);
1457
+ return this.makeToken(parseInfo, EnumToken.Pipe);
1458
+ case 33 /* TokenMap.EXCLAMATION */:
1459
+ if (parseInfo.position < parseInfo.currentPosition) {
1460
+ return this.makeToken(parseInfo);
1461
+ }
1462
+ if (this.match(parseInfo, "!important")) {
1463
+ this.advance(parseInfo, 10);
1464
+ return this.makeToken(parseInfo, EnumToken.ImportantTokenType);
1465
+ }
1466
+ this.advance(parseInfo);
1467
+ break;
1468
+ case 47 /* TokenMap.SLASH */:
1469
+ if (parseInfo.position < parseInfo.currentPosition) {
1470
+ return this.makeToken(parseInfo);
1471
+ }
1472
+ if (!this.match(parseInfo, "/*")) {
1473
+ this.advance(parseInfo);
1474
+ return this.makeToken(parseInfo, getSymbolHint(parseInfo, parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset));
1475
+ }
1476
+ this.advance(parseInfo, 2);
1477
+ while ((charCode = this.advance(parseInfo).charCodeAt(0)) == charCode) {
1478
+ if (charCode == 42 /* TokenMap.STAR */) {
1479
+ if (this.match(parseInfo, "/")) {
1480
+ this.advance(parseInfo);
1481
+ return this.makeToken(parseInfo, EnumToken.CommentTokenType);
1482
+ }
854
1483
  }
855
1484
  }
856
- if (parseInfo.currentPosition >= endPosition) {
857
- result.push(yieldResult(parseInfo, EnumToken.BadCdoTokenType));
1485
+ if (parseInfo.position < parseInfo.currentPosition) {
1486
+ return this.makeToken(parseInfo, EnumToken.BadCommentTokenType);
858
1487
  }
859
- else {
860
- next(parseInfo, 2);
861
- result.push(yieldResult(parseInfo, EnumToken.CDOCOMMTokenType));
1488
+ break;
1489
+ case 62 /* TokenMap.GREATERTHAN */:
1490
+ if (parseInfo.position < parseInfo.currentPosition) {
1491
+ return this.makeToken(parseInfo);
1492
+ }
1493
+ if (this.match(parseInfo, ">=")) {
1494
+ this.advance(parseInfo, 2);
1495
+ return this.makeToken(parseInfo, EnumToken.GteTokenType);
1496
+ }
1497
+ this.advance(parseInfo);
1498
+ return this.makeToken(parseInfo, EnumToken.GtTokenType);
1499
+ case 60 /* TokenMap.LOWERTHAN */:
1500
+ if (parseInfo.position < parseInfo.currentPosition) {
1501
+ return this.makeToken(parseInfo);
1502
+ }
1503
+ if (this.match(parseInfo, "<=")) {
1504
+ this.advance(parseInfo, 2);
1505
+ return this.makeToken(parseInfo, EnumToken.LteTokenType);
1506
+ }
1507
+ this.advance(parseInfo);
1508
+ if (this.match(parseInfo, "!--")) {
1509
+ this.advance(parseInfo, 3);
1510
+ while ((charCode = this.advance(parseInfo).charCodeAt(0)) == charCode) {
1511
+ if (charCode == 45 /* TokenMap.MINUS */ && this.match(parseInfo, "->")) {
1512
+ break;
1513
+ }
1514
+ }
1515
+ if (parseInfo.currentPosition >= endPosition) {
1516
+ return this.makeToken(parseInfo, EnumToken.BadCdoTokenType);
1517
+ }
1518
+ else {
1519
+ this.advance(parseInfo, 2);
1520
+ return this.makeToken(parseInfo, EnumToken.CDOCOMMTokenType);
1521
+ }
862
1522
  }
863
- }
864
- break;
865
- case 35 /* TokenMap.HASH */:
866
- if (parseInfo.position < parseInfo.currentPosition) {
867
- result.push(yieldResult(parseInfo));
868
- }
869
- next(parseInfo);
870
- break;
871
- case 92 /* TokenMap.REVERSE_SOLIDUS */:
872
- if (!yieldEOFToken && parseInfo.stream.length == parseInfo.currentPosition - parseInfo.offset + 1) {
873
1523
  break;
874
- }
875
- next(parseInfo);
876
- // EOF
877
- if (!peek(parseInfo)) {
878
- if (!yieldEOFToken) {
1524
+ case 35 /* TokenMap.HASH */:
1525
+ if (parseInfo.position < parseInfo.currentPosition) {
1526
+ return this.makeToken(parseInfo);
1527
+ }
1528
+ this.advance(parseInfo);
1529
+ break;
1530
+ case 92 /* TokenMap.REVERSE_SOLIDUS */:
1531
+ // if (!yieldEOFToken && parseInfo.stream.length == parseInfo.currentPosition - parseInfo.offset + 1) {
1532
+ // break;
1533
+ // }
1534
+ this.advance(parseInfo);
1535
+ // EOF
1536
+ if (!this.peek(parseInfo)) {
1537
+ // if (!yieldEOFToken) {
1538
+ // break;
1539
+ // }
1540
+ // end of stream ignore \\
1541
+ if (parseInfo.position < parseInfo.currentPosition) {
1542
+ return this.makeToken(parseInfo);
1543
+ }
879
1544
  break;
880
1545
  }
881
- // end of stream ignore \\
1546
+ this.advance(parseInfo);
1547
+ break;
1548
+ case 39 /* TokenMap.SINGLE_QUOTE */:
1549
+ case 34 /* TokenMap.DOUBLE_QUOTE */:
882
1550
  if (parseInfo.position < parseInfo.currentPosition) {
883
- result.push(yieldResult(parseInfo));
1551
+ return this.makeToken(parseInfo);
884
1552
  }
1553
+ return this.consumeString(parseInfo);
1554
+ case 46 /* TokenMap.DOT */:
1555
+ const codepoint = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1);
1556
+ if (isIdentStart(codepoint) || codepoint == 45 /* TokenMap.MINUS */) {
1557
+ this.advance(parseInfo);
1558
+ let tokensCount = this.consumeIdentToken(parseInfo);
1559
+ if (tokensCount > 0) {
1560
+ this.advance(parseInfo, tokensCount);
1561
+ return this.makeToken(parseInfo, EnumToken.ClassSelectorTokenType);
1562
+ }
1563
+ }
1564
+ if (!isDigit(codepoint) && parseInfo.position !== parseInfo.currentPosition) {
1565
+ this.makeToken(parseInfo);
1566
+ this.advance(parseInfo, 2);
1567
+ return this;
1568
+ }
1569
+ this.advance(parseInfo);
885
1570
  break;
886
- }
887
- next(parseInfo);
888
- break;
889
- case 39 /* TokenMap.SINGLE_QUOTE */:
890
- case 34 /* TokenMap.DOUBLE_QUOTE */:
891
- if (parseInfo.position < parseInfo.currentPosition) {
892
- result.push(yieldResult(parseInfo));
893
- }
894
- result.push(...consumeString(parseInfo));
895
- break;
896
- case 46 /* TokenMap.DOT */:
897
- const codepoint = parseInfo.stream
898
- .charAt(parseInfo.currentPosition - parseInfo.offset + 1)
899
- .charCodeAt(0);
900
- if (!isDigit(codepoint) && parseInfo.position !== parseInfo.currentPosition) {
901
- result.push(yieldResult(parseInfo));
902
- next(parseInfo, 2);
1571
+ default:
1572
+ this.advance(parseInfo);
903
1573
  break;
904
- }
905
- next(parseInfo);
906
- break;
907
- default:
908
- next(parseInfo);
909
- break;
910
- }
911
- if (!yieldEOFToken && endPosition <= parseInfo.currentPosition - parseInfo.offset + 1) {
912
- break;
1574
+ }
1575
+ // if (!yieldEOFToken && endPosition <= parseInfo.currentPosition - parseInfo.offset + 1) {
1576
+ // break;
1577
+ // }
913
1578
  }
914
- }
915
- if (yieldEOFToken) {
1579
+ // if (yieldEOFToken) {
916
1580
  if (parseInfo.position < parseInfo.currentPosition) {
917
- result.push(yieldResult(parseInfo));
1581
+ return this.makeToken(parseInfo);
918
1582
  }
919
- result.push(yieldResult(parseInfo, EnumToken.EOFTokenType));
1583
+ return this.makeToken(parseInfo, EnumToken.EOFTokenType);
1584
+ // }
920
1585
  }
921
- parseInfo.time += performance.now() - startTime;
922
- return result;
923
- }
924
- /**
925
- * tokenize readable stream
926
- * @param input
927
- * @param parseInfo
928
- */
929
- async function* tokenizeStream(input, parseInfo) {
930
- const decoder = new TextDecoder("utf-8");
931
- const reader = input.getReader();
932
- parseInfo.stream = "";
933
- while (true) {
934
- const { done, value } = await reader.read();
935
- const stream = ArrayBuffer.isView(value) ? decoder.decode(value, { stream: true }) : value;
936
- if (!done) {
937
- parseInfo.source.append(stream);
938
- parseInfo.stream = (parseInfo.stream.slice(parseInfo.position - parseInfo.offset) + stream);
939
- parseInfo.offset = parseInfo.offset = parseInfo.position;
940
- }
941
- else {
942
- parseInfo.stream = "";
943
- }
944
- yield* tokenize(parseInfo, done);
945
- if (done) {
946
- break;
1586
+ /**
1587
+ * tokenize readable stream
1588
+ * @param input
1589
+ * @param parseInfo
1590
+ */
1591
+ async tokenizeStream() {
1592
+ const decoder = new TextDecoder("utf-8");
1593
+ const reader = this.input.getReader();
1594
+ let parseInfo = this.parseInfo;
1595
+ parseInfo.stream = "";
1596
+ while (true) {
1597
+ const { done, value } = await reader.read();
1598
+ const stream = ArrayBuffer.isView(value) ? decoder.decode(value, { stream: true }) : value;
1599
+ if (!done) {
1600
+ parseInfo.source.append(stream);
1601
+ }
1602
+ else {
1603
+ break;
1604
+ }
947
1605
  }
1606
+ parseInfo.stream = parseInfo.source.getContent();
1607
+ return this; // .next();
948
1608
  }
949
1609
  }
950
1610
 
951
- export { SymbolsMapTokens, TokenMap, consumeString, hintsEnum, match, next, peek, tokenize, tokenizeStream, yieldResult };
1611
+ export { TokenMap, Tokenizer, hintsEnum };