@tbela99/css-parser 1.4.11 → 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 (80) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +18 -11
  3. package/dist/index-umd-web.js +6558 -5310
  4. package/dist/index.cjs +6576 -5313
  5. package/dist/index.d.ts +805 -279
  6. package/dist/lib/ast/clone.js +13 -8
  7. package/dist/lib/ast/expand.js +65 -15
  8. package/dist/lib/ast/features/calc.js +19 -56
  9. package/dist/lib/ast/features/if.js +28 -4
  10. package/dist/lib/ast/features/inlinecssvariables.js +6 -5
  11. package/dist/lib/ast/features/prefix.js +52 -73
  12. package/dist/lib/ast/features/shorthand.js +11 -6
  13. package/dist/lib/ast/features/transform.js +6 -3
  14. package/dist/lib/ast/find.js +7 -5
  15. package/dist/lib/ast/math/expression.js +190 -32
  16. package/dist/lib/ast/minify.js +117 -49
  17. package/dist/lib/ast/node.js +52 -0
  18. package/dist/lib/ast/transform/compute.js +63 -4
  19. package/dist/lib/ast/transform/minify.js +6 -6
  20. package/dist/lib/ast/transform/perspective.js +1 -1
  21. package/dist/lib/ast/transform/rotate.js +1 -1
  22. package/dist/lib/ast/transform/scale.js +1 -1
  23. package/dist/lib/ast/transform/skew.js +1 -1
  24. package/dist/lib/ast/transform/translate.js +1 -1
  25. package/dist/lib/ast/transform/utils.js +46 -18
  26. package/dist/lib/ast/types.js +1 -1
  27. package/dist/lib/ast/walk.js +55 -11
  28. package/dist/lib/fs/resolve.js +68 -27
  29. package/dist/lib/parser/declaration/list.js +22 -7
  30. package/dist/lib/parser/declaration/map.js +40 -13
  31. package/dist/lib/parser/declaration/set.js +3 -1
  32. package/dist/lib/parser/linesmap.js +2 -12
  33. package/dist/lib/parser/parse.js +843 -912
  34. package/dist/lib/parser/source.js +16 -2
  35. package/dist/lib/parser/tokenize.js +1489 -725
  36. package/dist/lib/parser/utils/at-rule-container.js +29 -196
  37. package/dist/lib/parser/utils/at-rule-generic.js +6 -7
  38. package/dist/lib/parser/utils/at-rule-import.js +17 -13
  39. package/dist/lib/parser/utils/at-rule-media.js +36 -19
  40. package/dist/lib/parser/utils/at-rule-support.js +19 -9
  41. package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
  42. package/dist/lib/parser/utils/at-rule.js +0 -18
  43. package/dist/lib/parser/utils/declaration.js +30 -95
  44. package/dist/lib/parser/utils/hash.js +4 -4
  45. package/dist/lib/parser/utils/selector.js +164 -141
  46. package/dist/lib/parser/utils/text.js +4 -2
  47. package/dist/lib/renderer/render.js +230 -125
  48. package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
  49. package/dist/lib/renderer/sourcemap/sourcemap.js +187 -34
  50. package/dist/lib/syntax/color/a98rgb.js +14 -4
  51. package/dist/lib/syntax/color/cmyk.js +10 -18
  52. package/dist/lib/syntax/color/color-mix.js +21 -34
  53. package/dist/lib/syntax/color/color.js +32 -49
  54. package/dist/lib/syntax/color/hsl.js +15 -15
  55. package/dist/lib/syntax/color/hwb.js +18 -17
  56. package/dist/lib/syntax/color/lab.js +17 -17
  57. package/dist/lib/syntax/color/lch.js +13 -13
  58. package/dist/lib/syntax/color/oklab.js +10 -11
  59. package/dist/lib/syntax/color/oklch.js +15 -18
  60. package/dist/lib/syntax/color/p3.js +18 -12
  61. package/dist/lib/syntax/color/prophotorgb.js +26 -28
  62. package/dist/lib/syntax/color/rec2020.js +8 -4
  63. package/dist/lib/syntax/color/relative-color.js +26 -10
  64. package/dist/lib/syntax/color/srgb.js +5 -4
  65. package/dist/lib/syntax/color/utils/distance.js +1 -1
  66. package/dist/lib/syntax/color/xyz.js +2 -2
  67. package/dist/lib/syntax/color/xyzd50.js +2 -2
  68. package/dist/lib/syntax/constants.js +12 -2
  69. package/dist/lib/syntax/syntax.js +45 -258
  70. package/dist/lib/validation/config.json.js +3 -0
  71. package/dist/lib/validation/match.js +110 -27
  72. package/dist/node.js +55 -33
  73. package/dist/types.d.ts +5 -1
  74. package/dist/types.js +4 -0
  75. package/dist/utils/sync.d.ts +16 -0
  76. package/dist/utils/sync.js +73 -0
  77. package/dist/web.js +37 -30
  78. package/package.json +1 -1
  79. package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
  80. package/llms.txt +0 -73
@@ -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, isPseudo, isIdent, isWhiteSpace, isNumber, isHexColor, isHash, isPercentage, parseDimension, isNewLine } 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,714 +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);
140
- let value;
141
- let buffer = quote;
142
- const result = [];
143
- while ((value = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1))) {
144
- if (value == "\\") {
145
- if ("\\" == parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 2)) {
146
- buffer += next(parseInfo, 2);
147
- continue;
148
- }
149
- const sequence = peek(parseInfo, 6);
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
- if (codepoint == 0 ||
170
- // leading surrogate
171
- (0xd800 <= codepoint && codepoint <= 0xdbff) ||
172
- // trailing surrogate
173
- (0xdc00 <= codepoint && codepoint <= 0xdfff)) {
174
- buffer += String.fromCodePoint(0xfffd);
175
- }
176
- else {
177
- buffer += String.fromCodePoint(codepoint);
178
- }
179
- next(parseInfo, escapeSequence.length +
180
- 1 +
181
- (isWhiteSpace(parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1)?.charCodeAt(0))
182
- ? 1
183
- : 0));
184
- continue;
185
- }
186
- buffer += next(parseInfo, 2);
187
- continue;
188
- }
189
- if (value == quote) {
190
- buffer += value;
191
- result.push(yieldResult(buffer, parseInfo,
192
- /* hasNewLine ? EnumToken.BadStringTokenType : */ EnumToken.StringTokenType));
193
- next(parseInfo);
194
- buffer = "";
195
- return result;
196
147
  }
197
- if (isNewLine(value.charCodeAt(0))) {
198
- result.push(yieldResult(buffer + next(parseInfo), parseInfo, EnumToken.BadStringTokenType));
199
- return result;
148
+ if (match) {
149
+ return SymbolsMapTokens[key];
200
150
  }
201
- buffer += value;
202
- next(parseInfo);
203
151
  }
204
- // EOF - 'Unclosed-string' fixed
205
- result.push(yieldResult(buffer + quote, parseInfo, EnumToken.StringTokenType));
206
- return result;
152
+ return null;
207
153
  }
208
- function yieldResult(val, parseInfo, hint) {
209
- let token = null;
210
- let dimension;
211
- // console.debug(`Yield result: ${val}, ${hint}`);
212
- if (hint != null) {
213
- let searchArray = null;
214
- switch (hint) {
215
- case EnumToken.TransformFunctionTokenDefType:
216
- searchArray = transformFunctions;
217
- break;
218
- case EnumToken.ColorFunctionTokenDefType:
219
- searchArray = colorsFunc;
220
- break;
221
- case EnumToken.ContainerFunctionTokenDefType:
222
- searchArray = containerFunc;
223
- break;
224
- case EnumToken.UrlFunctionTokenDefType:
225
- searchArray = urlFunc;
226
- break;
227
- case EnumToken.GridTemplateFuncTokenDefType:
228
- searchArray = gridTemplateFunc;
229
- break;
230
- case EnumToken.ImageFunctionTokenDefType:
231
- searchArray = imageFunc;
232
- break;
233
- case EnumToken.TimelineFunctionTokenDefType:
234
- searchArray = timelineFunc;
235
- break;
236
- // case EnumToken.GeneralEnclosedFunctionTokenDefType:
237
- // searchArray = generalEnclosedFunc;
238
- // break;
239
- case EnumToken.SupportsFunctionTokenDefType:
240
- searchArray = supportFunc;
241
- break;
242
- case EnumToken.TimingFunctionTokenDefType:
243
- searchArray = timingFunc;
244
- break;
245
- case EnumToken.MathFunctionTokenDefType:
246
- searchArray = mathFuncs;
247
- break;
248
- case EnumToken.WhenElseFunctionTokenDefType:
249
- searchArray = whenElseFunc;
250
- break;
251
- case EnumToken.WildCardFunctionTokenDefType:
252
- 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;
253
177
  break;
254
- }
255
- if (searchArray != null) {
256
- val = searchArray.find((v) => equalsIgnoreCase(v, val));
257
- }
258
- token = hintsEnum.has(hint) ? { typ: hint } : { typ: hint, val };
259
- }
260
- else {
261
- let slice = val.slice(1);
262
- const chr = val.charAt(0);
263
- if (chr == "@" && isIdent(slice)) {
264
- token = {
265
- typ: EnumToken.AtRuleTokenType,
266
- nam: slice,
267
- };
268
- }
269
- else if (chr == "." && isIdent(slice)) {
270
- token = {
271
- typ: EnumToken.ClassSelectorTokenType,
272
- val,
273
- };
274
- }
275
- else if (chr == "#") {
276
- if (isHexColor(val)) {
277
- token = {
278
- typ: EnumToken.ColorTokenType,
279
- val: val,
280
- kin: ColorType.HEX,
281
- };
282
- }
283
- else if (isHash(val)) {
284
- token = {
285
- typ: EnumToken.HashTokenType,
286
- val: val,
287
- };
288
178
  }
289
179
  }
290
- else if ("\"'".includes(chr)) {
291
- token = {
292
- typ: EnumToken.UnclosedStringTokenType,
293
- val: val,
294
- };
295
- }
296
- else if (isNumber(val)) {
297
- token =
298
- val[0] === "-" || val[0] === "+"
299
- ? {
300
- typ: EnumToken.NumberTokenType,
301
- sign: val[0],
302
- val: +val,
303
- }
304
- : {
305
- typ: EnumToken.NumberTokenType,
306
- val: +val,
307
- };
308
- }
309
- else if (isPercentage(val)) {
310
- token = {
311
- typ: EnumToken.PercentageTokenType,
312
- val: +val.slice(0, -1),
313
- };
314
- }
315
- else if ((dimension = parseDimension(val))) {
316
- token = dimension;
317
- }
318
- else if (isIdent(val)) {
319
- token = {
320
- typ: val.startsWith("--") ? EnumToken.DashedIdenTokenType : EnumToken.IdenTokenType,
321
- val,
322
- };
180
+ if (match) {
181
+ return arrayItem;
323
182
  }
324
183
  }
325
- if (token == null) {
326
- token = {
327
- typ: EnumToken.LiteralTokenType,
328
- val,
329
- };
330
- }
331
- // return token;
332
- token[LOC] = {
333
- srcId: parseInfo.source.id,
334
- sta: parseInfo.position,
335
- end: parseInfo.currentPosition,
336
- };
337
- parseInfo.position = parseInfo.currentPosition;
338
- return { token, bytesIn: parseInfo.currentPosition + 1 };
184
+ return null;
339
185
  }
340
- function match(parseInfo, input) {
341
- let position = parseInfo.currentPosition - parseInfo.offset;
342
- for (let i = 0; i < input.length; i++) {
343
- if (parseInfo.stream[position + i + 1] != input.charAt(i)) {
344
- return false;
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
+ };
258
+ }
345
259
  }
346
260
  }
347
- return true;
348
- }
349
- function peek(parseInfo, count = 1) {
350
- if (count == 1) {
351
- return parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1);
352
- }
353
- const position = parseInfo.currentPosition - parseInfo.offset;
354
- return parseInfo.stream.slice(position + 1, position + count + 1);
355
- }
356
- function next(parseInfo, count = 1) {
357
- let position = parseInfo.currentPosition - parseInfo.offset;
358
- let char = count == 1 ? parseInfo.stream.charAt(position + 1) : parseInfo.stream.slice(position + 1, position + 1 + count);
359
- let i = 0;
360
- let codepoint;
361
- for (; i < char.length; i++) {
362
- codepoint = char[i].charCodeAt(0);
363
- if (codepoint == 0xa || // \n
364
- codepoint == 0xb || // \v
365
- codepoint == 0xc || // \f
366
- codepoint == 0xd || // \r
367
- codepoint == 0x2028 || // \u2028
368
- codepoint == 0x2029 // \u2029
369
- ) {
370
- // \r\n
371
- if (codepoint == 0xa && i > 0 && char.charCodeAt(i - 1) == 0xd) ;
372
- else {
373
- parseInfo.source.lineStarts.lineStarts.push(position + i);
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;
306
+ }
307
+ if (charCode == quote) {
308
+ this.advance(parseInfo);
309
+ return this.makeToken(parseInfo,
310
+ /* hasNewLine ? EnumToken.BadStringTokenType : */ EnumToken.StringTokenType, decodeSegments ? { decodeSegments } : null);
374
311
  }
312
+ if (isNewLine(charCode)) {
313
+ this.advance(parseInfo);
314
+ return this.makeToken(parseInfo, EnumToken.BadStringTokenType);
315
+ }
316
+ this.advance(parseInfo);
375
317
  }
318
+ // EOF - 'Unclosed-string' fixed
319
+ return this.makeToken(parseInfo, EnumToken.StringTokenType);
320
+ // return result;
376
321
  }
377
- parseInfo.currentPosition += char.length;
378
- return char;
379
- }
380
- /**
381
- * Tokenize css string
382
- * @param parseInfo
383
- * @param yieldEOFToken
384
- */
385
- function tokenize(parseInfo, yieldEOFToken = true) {
386
- if (typeof parseInfo == "string") {
387
- parseInfo = {
388
- buffer: "",
389
- stream: parseInfo,
390
- source: new SourceFile(parseInfo, [], ""),
391
- offset: 0,
392
- time: 0,
393
- position: 0,
394
- currentPosition: -1,
395
- };
396
- }
397
- let value;
398
- let buffer = parseInfo.buffer;
399
- let charCode;
400
- let nextCharCode;
401
- const startTime = performance.now();
402
- const result = [];
403
- // allow 10 characters buffer for the streaming parser to avoid incomplete tokens
404
- const endPosition = parseInfo.stream.length - 10;
405
- parseInfo.buffer = "";
406
- while ((value = peek(parseInfo))) {
407
- charCode = value.charCodeAt(0);
408
- // nextCharCode = nextValue.charCodeAt(0);
409
- // console.debug({value, buffer});
410
- switch (charCode) {
411
- case 61 /* TokenMap.EQUALS */:
412
- if (buffer.length > 0) {
413
- result.push(yieldResult(buffer, parseInfo));
414
- buffer = "";
415
- }
416
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.DelimTokenType));
417
- break;
418
- // '+' or '-'
419
- case 43 /* TokenMap.PLUS */:
420
- case 45 /* TokenMap.MINUS */:
421
- next(parseInfo);
422
- if (charCode === 43 /* TokenMap.PLUS */ && !isNumber(peek(parseInfo))) {
423
- if (buffer.length > 0) {
424
- result.push(yieldResult(buffer, parseInfo));
425
- buffer = "";
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;
426
352
  }
427
- result.push(yieldResult(value, parseInfo, SymbolsMapTokens[value]));
428
353
  break;
429
354
  }
430
- buffer += value;
431
- break;
432
- // '{'
433
- case 123 /* TokenMap.LEFT_BRACE */:
434
- if (buffer.length > 0) {
435
- result.push(yieldResult(buffer, parseInfo));
436
- buffer = "";
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;
437
364
  }
438
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.BlockStartTokenType));
439
- break;
440
- // '}'
441
- case 125 /* TokenMap.RIGHT_BRACE */:
442
- if (buffer.length > 0) {
443
- result.push(yieldResult(buffer, parseInfo));
444
- buffer = "";
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);
445
408
  }
446
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.BlockEndTokenType));
409
+ return this.makeToken(parseInfo, EnumToken.BadStringTokenType);
410
+ }
411
+ this.advance(parseInfo);
412
+ }
413
+ // EOF - bad url token
414
+ return this.makeToken(parseInfo, EnumToken.BadUrlTokenType);
415
+ // return result;
416
+ }
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++;
434
+ }
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;
442
+ }
443
+ // '.' 'E' 'e'
444
+ if (codepoint == 0x2e || codepoint == 0x45 || codepoint == 0x65) {
445
+ position++;
447
446
  break;
448
- // '('
449
- case 40 /* TokenMap.LEFT_PARENTHESIS */:
450
- if (buffer.length > 0) {
451
- if (buffer[0] === ":" && isPseudo(buffer)) {
452
- next(parseInfo);
453
- result.push(yieldResult(buffer, parseInfo, EnumToken.PseudoClassFunctionTokenDefType));
454
- buffer = "";
447
+ }
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;
459
+ break;
460
+ }
461
+ if (isLetter(codepoint)) {
462
+ hasLetter = true;
463
+ break;
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;
473
+ }
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;
482
+ }
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++;
455
515
  break;
456
516
  }
457
- else if (isIdent(buffer)) {
458
- const hint = buffer.startsWith("--")
459
- ? EnumToken.CustomFunctionTokenDefType
460
- : (SymbolsMapTokens[buffer.toLowerCase() + "("] ?? EnumToken.FunctionTokenDefType);
461
- result.push(yieldResult(buffer, parseInfo, hint));
462
- next(parseInfo);
463
- buffer = "";
464
- if (hint === EnumToken.UrlFunctionTokenDefType) {
465
- buffer = "";
466
- value = peek(parseInfo);
467
- // consume an <url>
468
- while (isWhiteSpace((charCode = value.charCodeAt(0)))) {
469
- buffer += next(parseInfo);
470
- value = peek(parseInfo);
471
- charCode = value.charCodeAt(0);
472
- if (value === "/" && match(parseInfo, "/*")) {
473
- if (buffer.length > 0) {
474
- result.push(yieldResult(buffer, parseInfo));
475
- buffer = "";
476
- }
477
- buffer += next(parseInfo, 2);
478
- while ((value = next(parseInfo))) {
479
- if (value == "*") {
480
- buffer += value;
481
- if (match(parseInfo, "/")) {
482
- result.push(yieldResult(buffer + next(parseInfo), parseInfo, EnumToken.CommentTokenType));
483
- buffer = "";
484
- break;
485
- }
486
- }
487
- else {
488
- buffer += value;
489
- }
490
- }
491
- if (buffer.length > 0) {
492
- result.push(yieldResult(buffer, parseInfo, EnumToken.BadCommentTokenType));
493
- buffer = "";
494
- }
495
- value = peek(parseInfo);
496
- charCode = value.charCodeAt(0);
497
- }
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;
557
+ }
558
+ else if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
559
+ hasPercent = true;
560
+ }
561
+ else {
562
+ return 0;
563
+ }
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;
498
579
  }
499
- if (buffer.length > 0) {
500
- result.push(yieldResult(buffer, parseInfo, EnumToken.WhitespaceTokenType));
501
- buffer = "";
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;
502
588
  }
503
- if (value === ")" || value === '"' || value === "'") {
589
+ else if (isLetter(codepoint)) {
590
+ hasLetter = true;
504
591
  break;
505
592
  }
506
- do {
507
- buffer += next(parseInfo);
508
- value = peek(parseInfo);
509
- charCode = value.charCodeAt(0);
510
- } while (value !== ")" &&
511
- !isWhiteSpace(charCode) &&
512
- !(value === "/" && match(parseInfo, "/*")));
513
- if (buffer.length > 0) {
514
- result.push(yieldResult(buffer, parseInfo, peek(parseInfo) === ""
515
- ? EnumToken.BadUrlTokenType
516
- : EnumToken.UrlTokenTokenType));
517
- buffer = "";
593
+ else if (codepoint == 37 /* TokenMap.PERCENTAGE */) {
594
+ hasPercent = true;
595
+ break;
596
+ }
597
+ else {
598
+ return 0;
518
599
  }
519
600
  }
520
- // console.debug({value: peek(parseInfo)});
521
- break;
601
+ if (!hasLetter && !hasPercent) {
602
+ return position - offset;
603
+ }
522
604
  }
523
605
  }
524
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.StartParensTokenType));
525
- buffer = "";
526
- break;
527
- // ')'
528
- case 41 /* TokenMap.RIGHT_PARENTHESIS */:
529
- if (buffer.length > 0) {
530
- result.push(yieldResult(buffer, parseInfo));
531
- buffer = "";
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;
532
636
  }
533
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.EndParensTokenType));
534
- break;
535
- // '['
536
- case 91 /* TokenMap.LEFT_BRACKETS */:
537
- if (buffer.length > 0) {
538
- result.push(yieldResult(buffer, parseInfo));
539
- buffer = "";
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;
540
680
  }
541
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.AttrStartTokenType));
542
- break;
543
- // ']'
544
- case 93 /* TokenMap.RIGHT_BRACKETS */:
545
- if (buffer.length > 0) {
546
- result.push(yieldResult(buffer, parseInfo));
547
- buffer = "";
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;
548
689
  }
549
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.AttrEndTokenType));
550
- break;
551
- case 59 /* TokenMap.SEMICOLON */:
552
- if (buffer.length > 0) {
553
- result.push(yieldResult(buffer, parseInfo));
554
- buffer = "";
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;
555
720
  }
556
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.SemiColonTokenType));
557
- break;
558
- case 58 /* TokenMap.COLON */:
559
- if (buffer.length > 0) {
560
- result.push(yieldResult(buffer, parseInfo));
561
- buffer = "";
721
+ if (codepoint != codepoint || isWhiteSpace(codepoint)) {
722
+ return position - offset;
562
723
  }
563
- next(parseInfo);
564
- if (peek(parseInfo).charCodeAt(0) == 58 /* TokenMap.COLON */) {
565
- result.push(yieldResult(value + next(parseInfo), 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;
823
+ break;
824
+ case EnumToken.ColorFunctionTokenDefType:
825
+ array = colorsFunc;
826
+ break;
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;
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;
566
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
+ }
567
907
  }
568
- result.push(yieldResult(value, parseInfo, EnumToken.ColonTokenType));
569
- break;
570
- // \n \r \f \v \t space
571
- case 0x9:
572
- case 0x20:
573
- case 0xa:
574
- case 0xb:
575
- case 0xc:
576
- case 0xd:
577
- case 0x2028:
578
- case 0x2029:
579
- if (buffer.length > 0) {
580
- result.push(yieldResult(buffer, parseInfo));
581
- buffer = "";
582
- }
583
- buffer += next(parseInfo);
584
- nextCharCode = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset + 1).charCodeAt(0);
585
- while (nextCharCode == 0x20 ||
586
- (nextCharCode >= 0x9 && nextCharCode <= 0xd) ||
587
- nextCharCode == 0x2028 ||
588
- nextCharCode == 0x2029) {
589
- value += next(parseInfo);
590
- nextCharCode = parseInfo.stream
591
- .charAt(parseInfo.currentPosition - parseInfo.offset + 1)
592
- .charCodeAt(0);
593
- }
594
- result.push(yieldResult(value, parseInfo, EnumToken.WhitespaceTokenType));
595
- buffer = "";
596
- break;
597
- case 44 /* TokenMap.COMMA */:
598
- if (buffer.length > 0) {
599
- result.push(yieldResult(buffer, parseInfo));
600
- buffer = "";
908
+ else if (hint == EnumToken.NumberTokenType) {
909
+ this.val = parseFloat(val);
601
910
  }
602
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.CommaTokenType));
603
- break;
604
- case 36 /* TokenMap.DOLLAR */:
605
- if (buffer.length > 0) {
606
- result.push(yieldResult(buffer, parseInfo));
607
- buffer = "";
911
+ else if (hint == EnumToken.AtRuleTokenType) {
912
+ this.nam = val;
608
913
  }
609
- if (match(parseInfo, "$=")) {
610
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.EndMatchTokenType));
611
- break;
914
+ else {
915
+ this.val = val;
916
+ if (hint == EnumToken.ColorTokenType) {
917
+ this.kin = ColorType.HEX;
918
+ }
612
919
  }
613
- buffer += next(parseInfo);
614
- break;
615
- case 126 /* TokenMap.TILDA */:
616
- if (buffer.length > 0) {
617
- result.push(yieldResult(buffer, parseInfo));
618
- buffer = "";
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);
619
1028
  }
620
- if (match(parseInfo, "~=")) {
621
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.IncludeMatchTokenType));
622
- break;
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;
623
1048
  }
624
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.Tilda));
625
- buffer = "";
626
- break;
627
- // case '^':
628
- case 94 /* TokenMap.CARET */:
629
- if (buffer.length > 0) {
630
- result.push(yieldResult(buffer, parseInfo));
631
- buffer = "";
1049
+ else {
1050
+ i += start;
632
1051
  }
633
- if (match(parseInfo, "^=")) {
634
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.StartMatchTokenType));
635
- break;
1052
+ }
1053
+ else {
1054
+ if (end < 0) {
1055
+ j += end;
636
1056
  }
637
- buffer += next(parseInfo);
638
- break;
639
- case 42 /* TokenMap.STAR */:
640
- if (buffer.length > 0) {
641
- result.push(yieldResult(buffer, parseInfo));
642
- buffer = "";
1057
+ else {
1058
+ j = parseInfo.position + end;
643
1059
  }
644
- if (match(parseInfo, "*=")) {
645
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.ContainMatchTokenType));
646
- break;
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;
647
1152
  }
648
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.Star));
649
- buffer = "";
650
- break;
651
- case 38 /* TokenMap.AMPERSAND */:
652
- if (buffer.length > 0) {
653
- result.push(yieldResult(buffer, parseInfo));
654
- buffer = "";
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;
655
1157
  }
656
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.NestingSelectorTokenType));
657
- buffer = "";
1158
+ continue;
1159
+ }
1160
+ // is white space
1161
+ if (c == 0x20 || c == 0x09) {
658
1162
  break;
659
- case 124 /* TokenMap.PIPE */:
660
- if (buffer.length > 0) {
661
- result.push(yieldResult(buffer, parseInfo));
662
- buffer = "";
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
+ }
663
1203
  }
664
- // '||'
665
- if (match(parseInfo, "||")) {
666
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.ColumnCombinatorTokenType));
667
- 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);
1214
+ }
1215
+ }
668
1216
  }
669
- else if (match(parseInfo, "|=")) {
670
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.DashMatchTokenType));
671
- break;
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
+ }
672
1230
  }
673
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.Pipe));
674
- buffer = "";
675
- break;
676
- case 33 /* TokenMap.EXCLAMATION */:
677
- if (buffer.length > 0) {
678
- result.push(yieldResult(buffer, parseInfo));
679
- buffer = "";
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
+ }
680
1243
  }
681
- if (match(parseInfo, "!important")) {
682
- result.push(yieldResult(next(parseInfo, 10), parseInfo, EnumToken.ImportantTokenType));
683
- buffer = "";
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);
684
1290
  break;
685
- }
686
- buffer += next(parseInfo);
687
- break;
688
- case 47 /* TokenMap.SLASH */:
689
- if (buffer.length > 0) {
690
- result.push(yieldResult(buffer, parseInfo));
691
- buffer = "";
692
- }
693
- if (!match(parseInfo, "/*")) {
694
- result.push(yieldResult(next(parseInfo), parseInfo, SymbolsMapTokens[value]));
695
- break;
696
- }
697
- buffer += next(parseInfo, 2);
698
- while ((value = next(parseInfo))) {
699
- if (value == "*") {
700
- buffer += value;
701
- if (match(parseInfo, "/")) {
702
- result.push(yieldResult(buffer + next(parseInfo), parseInfo, EnumToken.CommentTokenType));
703
- buffer = "";
704
- break;
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;
705
1325
  }
706
1326
  }
707
- else {
708
- buffer += value;
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);
709
1333
  }
710
- }
711
- if (buffer.length > 0) {
712
- result.push(yieldResult(buffer, parseInfo, EnumToken.BadCommentTokenType));
713
- buffer = "";
714
- }
715
- break;
716
- case 62 /* TokenMap.GREATERTHAN */:
717
- if (buffer.length > 0) {
718
- result.push(yieldResult(buffer, parseInfo));
719
- buffer = "";
720
- }
721
- if (match(parseInfo, ">=")) {
722
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.GteTokenType));
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);
723
1405
  break;
724
- }
725
- result.push(yieldResult(next(parseInfo), parseInfo, EnumToken.GtTokenType));
726
- buffer = "";
727
- break;
728
- case 60 /* TokenMap.LOWERTHAN */:
729
- if (buffer.length > 0) {
730
- result.push(yieldResult(buffer, parseInfo));
731
- buffer = "";
732
- }
733
- if (match(parseInfo, "<=")) {
734
- result.push(yieldResult(next(parseInfo, 2), parseInfo, EnumToken.LteTokenType));
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);
735
1426
  break;
736
- }
737
- buffer += next(parseInfo);
738
- if (match(parseInfo, "!--")) {
739
- buffer += next(parseInfo, 3);
740
- while ((value = next(parseInfo))) {
741
- buffer += value;
742
- if (value == "-" && match(parseInfo, "->")) {
743
- 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
+ }
744
1483
  }
745
1484
  }
746
- if (value === "") {
747
- result.push(yieldResult(buffer, parseInfo, EnumToken.BadCdoTokenType));
1485
+ if (parseInfo.position < parseInfo.currentPosition) {
1486
+ return this.makeToken(parseInfo, EnumToken.BadCommentTokenType);
748
1487
  }
749
- else {
750
- result.push(yieldResult(buffer + next(parseInfo, 2), 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
+ }
751
1522
  }
752
- buffer = "";
753
- }
754
- break;
755
- case 35 /* TokenMap.HASH */:
756
- if (buffer.length > 0) {
757
- result.push(yieldResult(buffer, parseInfo));
758
- buffer = "";
759
- }
760
- buffer += next(parseInfo);
761
- break;
762
- case 92 /* TokenMap.REVERSE_SOLIDUS */:
763
- next(parseInfo);
764
- // EOF
765
- if (!(peek(parseInfo))) {
766
- // end of stream ignore \\
767
- if (buffer.length > 0) {
768
- result.push(yieldResult(buffer, parseInfo));
769
- buffer = "";
770
- }
771
- break;
772
- }
773
- buffer += value + next(parseInfo);
774
- // buffer +=
775
- // (parseInfo.offset == parseInfo.currentPosition
776
- // ? parseInfo.buffer.slice(-1)
777
- // : parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset - 1)) + value;
778
- break;
779
- case 39 /* TokenMap.SINGLE_QUOTE */:
780
- case 34 /* TokenMap.DOUBLE_QUOTE */:
781
- if (buffer.length > 0) {
782
- result.push(yieldResult(buffer, parseInfo));
783
- buffer = "";
784
- }
785
- result.push(...consumeString(parseInfo));
786
- break;
787
- case 46 /* TokenMap.DOT */:
788
- const codepoint = parseInfo.stream
789
- .charAt(parseInfo.currentPosition - parseInfo.offset + 2)
790
- .charCodeAt(0);
791
- if (!isDigit(codepoint) && buffer !== "") {
792
- result.push(yieldResult(buffer, parseInfo));
793
- buffer = next(parseInfo, 2);
794
1523
  break;
795
- }
796
- buffer += next(parseInfo);
797
- break;
798
- default:
799
- buffer += next(parseInfo);
800
- break;
801
- }
802
- if (!yieldEOFToken && endPosition <= parseInfo.stream.length - parseInfo.currentPosition + parseInfo.offset) {
803
- break;
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
+ }
1544
+ break;
1545
+ }
1546
+ this.advance(parseInfo);
1547
+ break;
1548
+ case 39 /* TokenMap.SINGLE_QUOTE */:
1549
+ case 34 /* TokenMap.DOUBLE_QUOTE */:
1550
+ if (parseInfo.position < parseInfo.currentPosition) {
1551
+ return this.makeToken(parseInfo);
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);
1570
+ break;
1571
+ default:
1572
+ this.advance(parseInfo);
1573
+ break;
1574
+ }
1575
+ // if (!yieldEOFToken && endPosition <= parseInfo.currentPosition - parseInfo.offset + 1) {
1576
+ // break;
1577
+ // }
804
1578
  }
805
- }
806
- if (yieldEOFToken) {
807
- if (buffer.length > 0) {
808
- result.push(yieldResult(buffer, parseInfo));
1579
+ // if (yieldEOFToken) {
1580
+ if (parseInfo.position < parseInfo.currentPosition) {
1581
+ return this.makeToken(parseInfo);
809
1582
  }
810
- result.push(yieldResult("", parseInfo, EnumToken.EOFTokenType));
811
- }
812
- else {
813
- parseInfo.buffer = buffer;
1583
+ return this.makeToken(parseInfo, EnumToken.EOFTokenType);
1584
+ // }
814
1585
  }
815
- parseInfo.time += performance.now() - startTime;
816
- return result;
817
- }
818
- /**
819
- * tokenize readable stream
820
- * @param input
821
- * @param parseInfo
822
- */
823
- async function* tokenizeStream(input, parseInfo) {
824
- const decoder = new TextDecoder("utf-8");
825
- const reader = input.getReader();
826
- while (true) {
827
- const { done, value } = await reader.read();
828
- const stream = ArrayBuffer.isView(value) ? decoder.decode(value, { stream: true }) : value;
829
- if (!done) {
830
- parseInfo.source.append(stream);
831
- if (typeof parseInfo.stream != "string") {
832
- parseInfo.stream = stream;
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);
833
1601
  }
834
1602
  else {
835
- parseInfo.stream = (parseInfo.stream.slice(parseInfo.currentPosition - parseInfo.offset + 1) +
836
- stream);
1603
+ break;
837
1604
  }
838
- parseInfo.offset = parseInfo.currentPosition + 1;
839
- }
840
- yield* tokenize(parseInfo, done);
841
- if (done) {
842
- break;
843
1605
  }
1606
+ parseInfo.stream = parseInfo.source.getContent();
1607
+ return this; // .next();
844
1608
  }
845
1609
  }
846
1610
 
847
- export { SymbolsMapTokens, TokenMap, consumeString, hintsEnum, match, next, peek, tokenize, tokenizeStream, yieldResult };
1611
+ export { TokenMap, Tokenizer, hintsEnum };