@tbela99/css-parser 1.5.0 → 1.6.1

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 (78) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/README.md +10 -10
  3. package/dist/index-umd-web.js +5998 -4972
  4. package/dist/index.cjs +6001 -4977
  5. package/dist/index.d.ts +52 -38
  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 +2 -2
  13. package/dist/lib/ast/math/expression.js +207 -34
  14. package/dist/lib/ast/minify.js +121 -24
  15. package/dist/lib/ast/node.js +5 -3
  16. package/dist/lib/ast/transform/compute.js +37 -5
  17. package/dist/lib/ast/transform/minify.js +7 -7
  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/{config.json.js → lib/data/properties.json.js} +106 -0
  26. package/dist/lib/{validation/config.json.js → data/validation.json.js} +14 -8
  27. package/dist/lib/fs/resolve.js +11 -2
  28. package/dist/lib/parser/declaration/list.js +22 -7
  29. package/dist/lib/parser/declaration/map.js +26 -8
  30. package/dist/lib/parser/declaration/set.js +3 -1
  31. package/dist/lib/parser/linesmap.js +2 -4
  32. package/dist/lib/parser/parse.js +393 -231
  33. package/dist/lib/parser/tokenize.js +1478 -819
  34. package/dist/lib/parser/utils/at-rule-container.js +28 -196
  35. package/dist/lib/parser/utils/at-rule-generic.js +6 -7
  36. package/dist/lib/parser/utils/at-rule-import.js +17 -13
  37. package/dist/lib/parser/utils/at-rule-media.js +36 -19
  38. package/dist/lib/parser/utils/at-rule-support.js +19 -9
  39. package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
  40. package/dist/lib/parser/utils/at-rule.js +0 -18
  41. package/dist/lib/parser/utils/config.js +1 -1
  42. package/dist/lib/parser/utils/declaration.js +40 -97
  43. package/dist/lib/parser/utils/hash.js +4 -4
  44. package/dist/lib/parser/utils/selector.js +179 -224
  45. package/dist/lib/parser/utils/text.js +4 -2
  46. package/dist/lib/renderer/render.js +104 -74
  47. package/dist/lib/renderer/sourcemap/lib/codec.js +3 -3
  48. package/dist/lib/renderer/sourcemap/sourcemap.js +2 -5
  49. package/dist/lib/syntax/color/a98rgb.js +14 -4
  50. package/dist/lib/syntax/color/cmyk.js +14 -21
  51. package/dist/lib/syntax/color/color-mix.js +21 -34
  52. package/dist/lib/syntax/color/color.js +32 -49
  53. package/dist/lib/syntax/color/hsl.js +15 -15
  54. package/dist/lib/syntax/color/hwb.js +18 -17
  55. package/dist/lib/syntax/color/lab.js +17 -17
  56. package/dist/lib/syntax/color/lch.js +13 -13
  57. package/dist/lib/syntax/color/oklab.js +10 -11
  58. package/dist/lib/syntax/color/oklch.js +15 -18
  59. package/dist/lib/syntax/color/p3.js +18 -12
  60. package/dist/lib/syntax/color/prophotorgb.js +26 -28
  61. package/dist/lib/syntax/color/rec2020.js +8 -4
  62. package/dist/lib/syntax/color/relative-color.js +26 -10
  63. package/dist/lib/syntax/color/srgb.js +9 -7
  64. package/dist/lib/syntax/color/utils/distance.js +1 -1
  65. package/dist/lib/syntax/color/xyz.js +2 -2
  66. package/dist/lib/syntax/color/xyzd50.js +2 -2
  67. package/dist/lib/syntax/constants.js +13 -3
  68. package/dist/lib/syntax/syntax.js +32 -108
  69. package/dist/lib/validation/config.js +1 -1
  70. package/dist/lib/validation/match.js +29 -26
  71. package/dist/lib/validation/parser/parse.js +7 -3
  72. package/dist/node.js +16 -9
  73. package/dist/types.d.ts +5 -1
  74. package/dist/types.js +4 -0
  75. package/dist/utils/sync.js +22 -1
  76. package/dist/web.js +13 -4
  77. package/package.json +4 -4
  78. 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,1493 @@ 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
+ consumeStringAsURLToken(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;
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;
515
466
  }
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));
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;
547
473
  }
548
- next(parseInfo);
549
- result.push(yieldResult(parseInfo, EnumToken.DelimTokenType));
550
- 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;
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;
565
482
  }
566
- next(parseInfo);
567
- break;
568
- // '{'
569
- case 123 /* TokenMap.LEFT_BRACE */:
570
- if (parseInfo.position < parseInfo.currentPosition) {
571
- result.push(yieldResult(parseInfo));
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
+ }
572
496
  }
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));
497
+ else {
498
+ position++;
499
+ hasDigits = true;
580
500
  }
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));
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;
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;
621
579
  }
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);
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;
630
588
  }
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));
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));
649
- }
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));
657
- }
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));
665
- }
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));
672
- }
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));
679
- }
680
- next(parseInfo);
681
- if (peek(parseInfo).charCodeAt(0) == 58 /* TokenMap.COLON */) {
682
- next(parseInfo);
683
- result.push(yieldResult(parseInfo, EnumToken.DoubleColonTokenType));
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)) {
684
635
  break;
685
636
  }
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));
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;
699
680
  }
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);
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;
708
689
  }
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));
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;
714
720
  }
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
+ if (codepoint != codepoint || isWhiteSpace(codepoint)) {
722
+ return position - offset;
721
723
  }
722
- if (match(parseInfo, "$=")) {
723
- next(parseInfo, 2);
724
- result.push(yieldResult(parseInfo, EnumToken.EndMatchTokenType));
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
+ consumeURLToken(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.consumeStringAsURLToken(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;
725
823
  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));
824
+ case EnumToken.ColorFunctionTokenDefType:
825
+ array = colorsFunc;
736
826
  break;
737
- }
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));
745
- }
746
- if (match(parseInfo, "^=")) {
747
- next(parseInfo, 2);
748
- result.push(yieldResult(parseInfo, EnumToken.StartMatchTokenType));
827
+ case EnumToken.ContainerFunctionTokenDefType:
828
+ array = containerFunc;
749
829
  break;
750
- }
751
- next(parseInfo);
752
- break;
753
- case 42 /* TokenMap.STAR */:
754
- if (parseInfo.position < parseInfo.currentPosition) {
755
- result.push(yieldResult(parseInfo));
756
- }
757
- if (match(parseInfo, "*=")) {
758
- next(parseInfo, 2);
759
- result.push(yieldResult(parseInfo, EnumToken.ContainMatchTokenType));
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;
871
+ break;
872
+ case EnumToken.FlexTokenType:
873
+ array = flexUnits;
874
+ hasUnit = true;
875
+ break;
876
+ case EnumToken.AngleTokenType:
877
+ array = angleUnits;
878
+ hasUnit = true;
760
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
+ }
761
907
  }
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));
908
+ else if (hint == EnumToken.NumberTokenType) {
909
+ this.val = parseFloat(val);
768
910
  }
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));
911
+ else if (hint == EnumToken.AtRuleTokenType) {
912
+ this.nam = val;
775
913
  }
776
- // '||'
777
- if (match(parseInfo, "||")) {
778
- next(parseInfo, 2);
779
- result.push(yieldResult(parseInfo, EnumToken.ColumnCombinatorTokenType));
780
- break;
914
+ else {
915
+ this.val = val;
916
+ if (hint == EnumToken.ColorTokenType) {
917
+ this.kin = ColorType.HEX;
918
+ }
781
919
  }
782
- else if (match(parseInfo, "|=")) {
783
- next(parseInfo, 2);
784
- result.push(yieldResult(parseInfo, EnumToken.DashMatchTokenType));
785
- 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);
786
1028
  }
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));
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?: number, end?: number */) {
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;
1048
+ // } else {
1049
+ // i += start;
1050
+ // }
1051
+ // } else {
1052
+ // if (end < 0) {
1053
+ // j += end;
1054
+ // } else {
1055
+ // j = parseInfo.position + end;
1056
+ // }
1057
+ // }
1058
+ // }
1059
+ j--;
1060
+ let codepoint = parseInfo.stream.charCodeAt(i);
1061
+ // -
1062
+ if (codepoint == 0x2d) {
1063
+ let nextCodepoint;
1064
+ // NaN != NaN
1065
+ if ((nextCodepoint = parseInfo.stream.charCodeAt(i + 1)) != nextCodepoint) {
1066
+ return false;
1067
+ }
1068
+ if (!isIdentStart(nextCodepoint) && nextCodepoint != 0x2d) {
1069
+ return false;
1070
+ }
1071
+ codepoint = nextCodepoint;
1072
+ i++;
1073
+ }
1074
+ if (codepoint !== 0x2d && !isIdentStart(codepoint)) {
1075
+ return false;
1076
+ }
1077
+ if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
1078
+ codepoint = parseInfo.stream.charCodeAt(i + 1);
1079
+ i += String.fromCodePoint(codepoint).length;
1080
+ }
1081
+ while (i < j) {
1082
+ i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1083
+ codepoint = parseInfo.stream.charCodeAt(i);
1084
+ if (codepoint == 92 /* TokenMap.REVERSE_SOLIDUS */) {
1085
+ i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1086
+ codepoint = parseInfo.stream.charCodeAt(i);
1087
+ i += codepoint < 0x80 ? 1 : String.fromCodePoint(codepoint).length;
1088
+ continue;
1089
+ }
1090
+ if (codepoint !== 0x2d && !isIdentCodepoint(codepoint)) {
1091
+ return false;
1092
+ }
1093
+ }
1094
+ return true;
1095
+ }
1096
+ /**
1097
+ *
1098
+ * @param parseInfo
1099
+ * @returns
1100
+ */
1101
+ // isPseudo(parseInfo: ParseInfo): boolean {
1102
+ // let position: number = parseInfo.currentPosition - parseInfo.offset;
1103
+ // let endPosition: number = parseInfo.currentPosition - parseInfo.offset;
1104
+ // return (parseInfo.stream.charAt(position) == ":" &&
1105
+ // parseInfo.stream.charAt(endPosition - 1) == "(" &&
1106
+ // (parseInfo.stream.charAt(position + 1) == ":"
1107
+ // ? this.isIdentToken(parseInfo, 2, -1)
1108
+ // : this.isIdentToken(parseInfo, 1, -1))) ||
1109
+ // parseInfo.stream.charAt(position + 1) == ":"
1110
+ // ? this.isIdentToken(parseInfo, 2)
1111
+ // : this.isIdentToken(parseInfo, 1);
1112
+ // }
1113
+ /**
1114
+ *
1115
+ * @param parseInfo
1116
+ * @param input
1117
+ * @returns
1118
+ */
1119
+ startsWith(parseInfo, input) {
1120
+ let i = 0;
1121
+ let j = input.length;
1122
+ while (i < j) {
1123
+ if (parseInfo.stream.charAt(parseInfo.position - parseInfo.offset + i) != input.charAt(i)) {
1124
+ return false;
1125
+ }
1126
+ i++;
1127
+ }
1128
+ return true;
1129
+ }
1130
+ /**
1131
+ *
1132
+ * @param parseInfo
1133
+ * @returns
1134
+ */
1135
+ isURLToken(parseInfo) {
1136
+ let i = parseInfo.position - parseInfo.offset;
1137
+ let c;
1138
+ while (++i < parseInfo.currentPosition) {
1139
+ c = parseInfo.stream.charCodeAt(i);
1140
+ // single quote or double quote or start parenthesis or close parenthesis
1141
+ if (isNonPrintable(c) || c == 0x27 || c == 0x22 || c == 0x28 || c == 0x29) {
1142
+ return false;
1143
+ }
1144
+ // valid escape
1145
+ if (c == 92 /* TokenMap.REVERSE_SOLIDUS */) {
1146
+ i++;
1147
+ if (i >= parseInfo.currentPosition) {
1148
+ return false;
793
1149
  }
794
- if (match(parseInfo, "!important")) {
795
- next(parseInfo, 10);
796
- result.push(yieldResult(parseInfo, EnumToken.ImportantTokenType));
797
- break;
1150
+ c = parseInfo.stream.charCodeAt(i);
1151
+ // c is not '\n' or '\r' or '\f'
1152
+ if (c == 0x6e || c == 0x72 || c == 0x66) {
1153
+ return false;
798
1154
  }
799
- next(parseInfo);
1155
+ continue;
1156
+ }
1157
+ // is white space
1158
+ if (c == 0x20 || c == 0x09) {
800
1159
  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;
1160
+ }
1161
+ }
1162
+ return i == parseInfo.currentPosition;
1163
+ }
1164
+ done() {
1165
+ return this.typ === EnumToken.EOF;
1166
+ }
1167
+ /**
1168
+ * Tokenize CSS string
1169
+ * @param parseInfo
1170
+ * @param yieldEOFToken
1171
+ */
1172
+ next( /* parseInfo: ParseInfo | string, yieldEOFToken: boolean = true */) {
1173
+ const parseInfo = this.parseInfo;
1174
+ this.source = parseInfo.source;
1175
+ let charCode;
1176
+ let nextCharCode;
1177
+ // const result: TokenizeResult[] = [];
1178
+ // allow 10 characters buffer for the streaming parser to avoid incomplete tokens
1179
+ const endPosition = parseInfo.stream.length - 1; // yieldEOFToken ? parseInfo.stream.length - 1 : parseInfo.stream.length - 10;
1180
+ let tokensCount;
1181
+ // NaN is not equal to NaN
1182
+ while ((charCode = this.peekCharCode(parseInfo)) == charCode) {
1183
+ if (this.state === EnumToken.UrlFunctionTokenDefType) {
1184
+ this.state = null;
1185
+ return this.consumeURLToken(parseInfo, endPosition);
1186
+ }
1187
+ if (parseInfo.position == parseInfo.currentPosition) {
1188
+ if (charCode == 45 /* TokenMap.MINUS */ ||
1189
+ charCode == 43 /* TokenMap.PLUS */ ||
1190
+ charCode == 46 /* TokenMap.DOT */ ||
1191
+ isDigit(charCode)) {
1192
+ tokensCount = this.consumeNumericToken(parseInfo);
1193
+ if (tokensCount > 0) {
1194
+ this.advance(parseInfo, tokensCount);
1195
+ return this.makeToken(parseInfo, this.hint ?? EnumToken.NumberTokenType, {
1196
+ slice: this.slice,
1197
+ sign: charCode == 45 /* TokenMap.MINUS */ ? "-" : charCode == 43 /* TokenMap.PLUS */ ? "+" : null,
1198
+ });
1199
+ }
809
1200
  }
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;
1201
+ if (isIdentStart(charCode) || charCode == 45 /* TokenMap.MINUS */) {
1202
+ tokensCount = this.consumeIdentToken(parseInfo);
1203
+ if (tokensCount > 0) {
1204
+ this.advance(parseInfo, tokensCount);
1205
+ charCode = this.peekCharCode(parseInfo);
1206
+ // do not match function
1207
+ if (40 /* TokenMap.LEFT_PARENTHESIS */ != charCode) {
1208
+ return this.makeToken(parseInfo, this.startsWith(parseInfo, "--")
1209
+ ? EnumToken.DashedIdenTokenType
1210
+ : EnumToken.IdenTokenType);
817
1211
  }
818
1212
  }
819
- // else {
820
- // buffer += value;
821
- // }
822
1213
  }
823
- if (parseInfo.position < parseInfo.currentPosition) {
824
- result.push(yieldResult(parseInfo, EnumToken.BadCommentTokenType));
1214
+ if (charCode == 64 /* TokenMap.AT */) {
1215
+ this.advance(parseInfo);
1216
+ charCode = this.peekCharCode(parseInfo);
1217
+ // match at-rule
1218
+ if (charCode == 45 /* TokenMap.MINUS */ || isIdentStart(this.peekCharCode(parseInfo))) {
1219
+ // consume '@'
1220
+ parseInfo.position = parseInfo.currentPosition;
1221
+ tokensCount = this.consumeIdentToken(parseInfo);
1222
+ if (tokensCount > 0) {
1223
+ this.advance(parseInfo, tokensCount);
1224
+ return this.makeToken(parseInfo, EnumToken.AtRuleTokenType);
1225
+ }
1226
+ }
825
1227
  }
826
- break;
827
- case 62 /* TokenMap.GREATERTHAN */:
828
- if (parseInfo.position < parseInfo.currentPosition) {
829
- result.push(yieldResult(parseInfo));
1228
+ if (charCode == 35 /* TokenMap.HASH */) {
1229
+ tokensCount = this.consumeColor(parseInfo);
1230
+ if (tokensCount > 0) {
1231
+ this.advance(parseInfo, tokensCount);
1232
+ return this.makeToken(parseInfo, EnumToken.ColorTokenType);
1233
+ }
1234
+ this.advance(parseInfo);
1235
+ tokensCount = this.consumeIdentToken(parseInfo);
1236
+ if (tokensCount > 0) {
1237
+ this.advance(parseInfo, tokensCount);
1238
+ return this.makeToken(parseInfo, EnumToken.HashTokenType);
1239
+ }
830
1240
  }
831
- if (match(parseInfo, ">=")) {
832
- next(parseInfo, 2);
833
- result.push(yieldResult(parseInfo, EnumToken.GteTokenType));
1241
+ }
1242
+ // EOF
1243
+ switch (charCode) {
1244
+ case 61 /* TokenMap.EQUALS */:
1245
+ if (parseInfo.position < parseInfo.currentPosition) {
1246
+ return this.makeToken(parseInfo);
1247
+ }
1248
+ this.advance(parseInfo);
1249
+ return this.makeToken(parseInfo, EnumToken.DelimTokenType);
1250
+ // '+' or '-'
1251
+ case 43 /* TokenMap.PLUS */:
1252
+ if (parseInfo.position < parseInfo.currentPosition) {
1253
+ return this.makeToken(parseInfo);
1254
+ }
1255
+ this.advance(parseInfo);
1256
+ charCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset);
1257
+ if (isDigit(charCode)) {
1258
+ tokensCount = this.consumeNumericToken(parseInfo);
1259
+ if (tokensCount > 0) {
1260
+ this.advance(parseInfo, tokensCount);
1261
+ return this.makeToken(parseInfo, this.hint ?? EnumToken.NumberTokenType, {
1262
+ slice: this.slice,
1263
+ sign: "+",
1264
+ });
1265
+ }
1266
+ }
1267
+ return this.makeToken(parseInfo, EnumToken.Plus);
1268
+ case 45 /* TokenMap.MINUS */:
1269
+ if (parseInfo.position == parseInfo.currentPosition) {
1270
+ nextCharCode = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1);
1271
+ // not a number
1272
+ if (isWhiteSpace(nextCharCode)) {
1273
+ this.advance(parseInfo);
1274
+ return this.makeToken(parseInfo, EnumToken.Sub);
1275
+ }
1276
+ if (charCode == 45 /* TokenMap.MINUS */ &&
1277
+ (nextCharCode == 45 /* TokenMap.MINUS */ || isIdentStart(nextCharCode))) {
1278
+ this.advance(parseInfo);
1279
+ tokensCount = this.consumeIdentToken(parseInfo);
1280
+ if (tokensCount > 0) {
1281
+ this.advance(parseInfo, tokensCount);
1282
+ return this.makeToken(parseInfo, EnumToken.IdenTokenType);
1283
+ }
1284
+ }
1285
+ }
1286
+ this.advance(parseInfo);
834
1287
  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));
1288
+ // '{'
1289
+ case 123 /* TokenMap.LEFT_BRACE */:
1290
+ if (parseInfo.position < parseInfo.currentPosition) {
1291
+ return this.makeToken(parseInfo);
1292
+ }
1293
+ this.advance(parseInfo);
1294
+ return this.makeToken(parseInfo, EnumToken.BlockStartTokenType);
1295
+ // '}'
1296
+ case 125 /* TokenMap.RIGHT_BRACE */:
1297
+ if (parseInfo.position < parseInfo.currentPosition) {
1298
+ return this.makeToken(parseInfo);
1299
+ }
1300
+ this.advance(parseInfo);
1301
+ return this.makeToken(parseInfo, EnumToken.BlockEndTokenType);
1302
+ // '('
1303
+ case 40 /* TokenMap.LEFT_PARENTHESIS */:
1304
+ if (parseInfo.position < parseInfo.currentPosition) {
1305
+ // if (
1306
+ // parseInfo.stream[parseInfo.position - parseInfo.offset] === ":" &&
1307
+ // this.isPseudo(parseInfo)
1308
+ // ) {
1309
+ // this.advance(parseInfo);
1310
+ // return this.makeToken(parseInfo, EnumToken.PseudoClassFunctionTokenDefType);
1311
+ // } else
1312
+ if (this.isIdentToken(parseInfo)) {
1313
+ const hint = this.startsWith(parseInfo, "--")
1314
+ ? EnumToken.CustomFunctionTokenDefType
1315
+ : (getSymbolHint(parseInfo, parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset + 1) ?? EnumToken.FunctionTokenDefType);
1316
+ this.makeToken(parseInfo, hint);
1317
+ this.advance(parseInfo);
1318
+ // consume '('
1319
+ parseInfo.position = parseInfo.currentPosition;
1320
+ if (hint === EnumToken.UrlFunctionTokenDefType) {
1321
+ this.state = hint;
1322
+ }
1323
+ return this;
1324
+ }
1325
+ }
1326
+ this.advance(parseInfo);
1327
+ return this.makeToken(parseInfo, EnumToken.StartParensTokenType);
1328
+ // ')'
1329
+ case 41 /* TokenMap.RIGHT_PARENTHESIS */:
1330
+ if (parseInfo.position < parseInfo.currentPosition) {
1331
+ return this.makeToken(parseInfo);
1332
+ }
1333
+ this.advance(parseInfo);
1334
+ return this.makeToken(parseInfo, EnumToken.EndParensTokenType);
1335
+ // '['
1336
+ case 91 /* TokenMap.LEFT_BRACKETS */:
1337
+ if (parseInfo.position < parseInfo.currentPosition) {
1338
+ return this.makeToken(parseInfo);
1339
+ }
1340
+ this.advance(parseInfo);
1341
+ return this.makeToken(parseInfo, EnumToken.AttrStartTokenType);
1342
+ // ']'
1343
+ case 93 /* TokenMap.RIGHT_BRACKETS */:
1344
+ if (parseInfo.position < parseInfo.currentPosition) {
1345
+ return this.makeToken(parseInfo);
1346
+ }
1347
+ this.advance(parseInfo);
1348
+ return this.makeToken(parseInfo, EnumToken.AttrEndTokenType);
1349
+ case 59 /* TokenMap.SEMICOLON */:
1350
+ if (parseInfo.position < parseInfo.currentPosition) {
1351
+ return this.makeToken(parseInfo);
1352
+ }
1353
+ this.advance(parseInfo);
1354
+ return this.makeToken(parseInfo, EnumToken.SemiColonTokenType);
1355
+ case 58 /* TokenMap.COLON */:
1356
+ if (parseInfo.position < parseInfo.currentPosition) {
1357
+ return this.makeToken(parseInfo);
1358
+ }
1359
+ this.advance(parseInfo);
1360
+ if (this.peekCharCode(parseInfo) == 58 /* TokenMap.COLON */) {
1361
+ this.advance(parseInfo);
1362
+ return this.makeToken(parseInfo, EnumToken.DoubleColonTokenType);
1363
+ }
1364
+ return this.makeToken(parseInfo, EnumToken.ColonTokenType);
1365
+ // \n \r \f \v \t space
1366
+ case 0x9:
1367
+ case 0x20:
1368
+ case 0xa:
1369
+ case 0xb:
1370
+ case 0xc:
1371
+ case 0xd:
1372
+ case 0x2028:
1373
+ case 0x2029:
1374
+ if (parseInfo.position < parseInfo.currentPosition) {
1375
+ return this.makeToken(parseInfo);
1376
+ }
1377
+ this.advance(parseInfo);
1378
+ nextCharCode = parseInfo.stream.charAt(parseInfo.currentPosition - parseInfo.offset).charCodeAt(0);
1379
+ while (nextCharCode == 0x20 ||
1380
+ (nextCharCode >= 0x9 && nextCharCode <= 0xd) ||
1381
+ nextCharCode == 0x2028 ||
1382
+ nextCharCode == 0x2029) {
1383
+ this.advance(parseInfo);
1384
+ nextCharCode = parseInfo.stream
1385
+ .charAt(parseInfo.currentPosition - parseInfo.offset)
1386
+ .charCodeAt(0);
1387
+ }
1388
+ return this.makeToken(parseInfo, EnumToken.WhitespaceTokenType);
1389
+ case 44 /* TokenMap.COMMA */:
1390
+ if (parseInfo.position < parseInfo.currentPosition) {
1391
+ return this.makeToken(parseInfo);
1392
+ }
1393
+ this.advance(parseInfo);
1394
+ return this.makeToken(parseInfo, EnumToken.CommaTokenType);
1395
+ case 36 /* TokenMap.DOLLAR */:
1396
+ if (parseInfo.position < parseInfo.currentPosition) {
1397
+ return this.makeToken(parseInfo);
1398
+ }
1399
+ if (this.match(parseInfo, "$=")) {
1400
+ this.advance(parseInfo, 2);
1401
+ return this.makeToken(parseInfo, EnumToken.EndMatchTokenType);
1402
+ }
1403
+ this.advance(parseInfo);
846
1404
  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;
1405
+ case 126 /* TokenMap.TILDA */:
1406
+ if (parseInfo.position < parseInfo.currentPosition) {
1407
+ return this.makeToken(parseInfo);
1408
+ }
1409
+ if (this.match(parseInfo, "~=")) {
1410
+ this.advance(parseInfo, 2);
1411
+ return this.makeToken(parseInfo, EnumToken.IncludeMatchTokenType);
1412
+ }
1413
+ this.advance(parseInfo);
1414
+ return this.makeToken(parseInfo, EnumToken.Tilda);
1415
+ // case '^':
1416
+ case 94 /* TokenMap.CARET */:
1417
+ if (parseInfo.position < parseInfo.currentPosition) {
1418
+ return this.makeToken(parseInfo);
1419
+ }
1420
+ if (this.match(parseInfo, "^=")) {
1421
+ this.advance(parseInfo, 2);
1422
+ return this.makeToken(parseInfo, EnumToken.StartMatchTokenType);
1423
+ }
1424
+ this.advance(parseInfo);
1425
+ break;
1426
+ case 42 /* TokenMap.STAR */:
1427
+ if (parseInfo.position < parseInfo.currentPosition) {
1428
+ return this.makeToken(parseInfo);
1429
+ }
1430
+ if (this.match(parseInfo, "*=")) {
1431
+ this.advance(parseInfo, 2);
1432
+ return this.makeToken(parseInfo, EnumToken.ContainMatchTokenType);
1433
+ }
1434
+ this.advance(parseInfo);
1435
+ return this.makeToken(parseInfo, EnumToken.Star);
1436
+ case 38 /* TokenMap.AMPERSAND */:
1437
+ if (parseInfo.position < parseInfo.currentPosition) {
1438
+ return this.makeToken(parseInfo);
1439
+ }
1440
+ this.advance(parseInfo);
1441
+ return this.makeToken(parseInfo, EnumToken.NestingSelectorTokenType);
1442
+ case 124 /* TokenMap.PIPE */:
1443
+ if (parseInfo.position < parseInfo.currentPosition) {
1444
+ return this.makeToken(parseInfo);
1445
+ }
1446
+ // '||'
1447
+ if (this.match(parseInfo, "||")) {
1448
+ this.advance(parseInfo, 2);
1449
+ return this.makeToken(parseInfo, EnumToken.ColumnCombinatorTokenType);
1450
+ }
1451
+ else if (this.match(parseInfo, "|=")) {
1452
+ this.advance(parseInfo, 2);
1453
+ return this.makeToken(parseInfo, EnumToken.DashMatchTokenType);
1454
+ }
1455
+ this.advance(parseInfo);
1456
+ return this.makeToken(parseInfo, EnumToken.Pipe);
1457
+ case 33 /* TokenMap.EXCLAMATION */:
1458
+ if (parseInfo.position < parseInfo.currentPosition) {
1459
+ return this.makeToken(parseInfo);
1460
+ }
1461
+ if (this.match(parseInfo, "!important")) {
1462
+ this.advance(parseInfo, 10);
1463
+ return this.makeToken(parseInfo, EnumToken.ImportantTokenType);
1464
+ }
1465
+ this.advance(parseInfo);
1466
+ break;
1467
+ case 47 /* TokenMap.SLASH */:
1468
+ if (parseInfo.position < parseInfo.currentPosition) {
1469
+ return this.makeToken(parseInfo);
1470
+ }
1471
+ if (!this.match(parseInfo, "/*")) {
1472
+ this.advance(parseInfo);
1473
+ return this.makeToken(parseInfo, getSymbolHint(parseInfo, parseInfo.position - parseInfo.offset, parseInfo.currentPosition - parseInfo.offset));
1474
+ }
1475
+ this.advance(parseInfo, 2);
1476
+ while ((charCode = this.advance(parseInfo).charCodeAt(0)) == charCode) {
1477
+ if (charCode == 42 /* TokenMap.STAR */) {
1478
+ if (this.match(parseInfo, "/")) {
1479
+ this.advance(parseInfo);
1480
+ return this.makeToken(parseInfo, EnumToken.CommentTokenType);
1481
+ }
854
1482
  }
855
1483
  }
856
- if (parseInfo.currentPosition >= endPosition) {
857
- result.push(yieldResult(parseInfo, EnumToken.BadCdoTokenType));
1484
+ if (parseInfo.position < parseInfo.currentPosition) {
1485
+ return this.makeToken(parseInfo, EnumToken.BadCommentTokenType);
858
1486
  }
859
- else {
860
- next(parseInfo, 2);
861
- result.push(yieldResult(parseInfo, EnumToken.CDOCOMMTokenType));
1487
+ break;
1488
+ case 62 /* TokenMap.GREATERTHAN */:
1489
+ if (parseInfo.position < parseInfo.currentPosition) {
1490
+ return this.makeToken(parseInfo);
1491
+ }
1492
+ if (this.match(parseInfo, ">=")) {
1493
+ this.advance(parseInfo, 2);
1494
+ return this.makeToken(parseInfo, EnumToken.GteTokenType);
1495
+ }
1496
+ this.advance(parseInfo);
1497
+ return this.makeToken(parseInfo, EnumToken.GtTokenType);
1498
+ case 60 /* TokenMap.LOWERTHAN */:
1499
+ if (parseInfo.position < parseInfo.currentPosition) {
1500
+ return this.makeToken(parseInfo);
1501
+ }
1502
+ if (this.match(parseInfo, "<=")) {
1503
+ this.advance(parseInfo, 2);
1504
+ return this.makeToken(parseInfo, EnumToken.LteTokenType);
1505
+ }
1506
+ this.advance(parseInfo);
1507
+ if (this.match(parseInfo, "!--")) {
1508
+ this.advance(parseInfo, 3);
1509
+ while ((charCode = this.advance(parseInfo).charCodeAt(0)) == charCode) {
1510
+ if (charCode == 45 /* TokenMap.MINUS */ && this.match(parseInfo, "->")) {
1511
+ break;
1512
+ }
1513
+ }
1514
+ if (parseInfo.currentPosition >= endPosition) {
1515
+ return this.makeToken(parseInfo, EnumToken.BadCdoTokenType);
1516
+ }
1517
+ else {
1518
+ this.advance(parseInfo, 2);
1519
+ return this.makeToken(parseInfo, EnumToken.CDOCOMMTokenType);
1520
+ }
862
1521
  }
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
1522
  break;
874
- }
875
- next(parseInfo);
876
- // EOF
877
- if (!peek(parseInfo)) {
878
- if (!yieldEOFToken) {
1523
+ case 35 /* TokenMap.HASH */:
1524
+ if (parseInfo.position < parseInfo.currentPosition) {
1525
+ return this.makeToken(parseInfo);
1526
+ }
1527
+ this.advance(parseInfo);
1528
+ break;
1529
+ case 92 /* TokenMap.REVERSE_SOLIDUS */:
1530
+ // if (!yieldEOFToken && parseInfo.stream.length == parseInfo.currentPosition - parseInfo.offset + 1) {
1531
+ // break;
1532
+ // }
1533
+ this.advance(parseInfo);
1534
+ // EOF
1535
+ if (!this.peek(parseInfo)) {
1536
+ // if (!yieldEOFToken) {
1537
+ // break;
1538
+ // }
1539
+ // end of stream ignore \\
1540
+ if (parseInfo.position < parseInfo.currentPosition) {
1541
+ return this.makeToken(parseInfo);
1542
+ }
879
1543
  break;
880
1544
  }
881
- // end of stream ignore \\
1545
+ this.advance(parseInfo);
1546
+ break;
1547
+ case 39 /* TokenMap.SINGLE_QUOTE */:
1548
+ case 34 /* TokenMap.DOUBLE_QUOTE */:
882
1549
  if (parseInfo.position < parseInfo.currentPosition) {
883
- result.push(yieldResult(parseInfo));
1550
+ return this.makeToken(parseInfo);
884
1551
  }
1552
+ return this.consumeString(parseInfo);
1553
+ case 46 /* TokenMap.DOT */:
1554
+ const codepoint = parseInfo.stream.charCodeAt(parseInfo.currentPosition - parseInfo.offset + 1);
1555
+ if (isIdentStart(codepoint) || codepoint == 45 /* TokenMap.MINUS */) {
1556
+ this.advance(parseInfo);
1557
+ let tokensCount = this.consumeIdentToken(parseInfo);
1558
+ if (tokensCount > 0) {
1559
+ this.advance(parseInfo, tokensCount);
1560
+ return this.makeToken(parseInfo, EnumToken.ClassSelectorTokenType);
1561
+ }
1562
+ }
1563
+ if (!isDigit(codepoint) && parseInfo.position !== parseInfo.currentPosition) {
1564
+ this.makeToken(parseInfo);
1565
+ this.advance(parseInfo, 2);
1566
+ return this;
1567
+ }
1568
+ this.advance(parseInfo);
885
1569
  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);
1570
+ default:
1571
+ this.advance(parseInfo);
903
1572
  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;
1573
+ }
1574
+ // if (!yieldEOFToken && endPosition <= parseInfo.currentPosition - parseInfo.offset + 1) {
1575
+ // break;
1576
+ // }
913
1577
  }
914
- }
915
- if (yieldEOFToken) {
1578
+ // if (yieldEOFToken) {
916
1579
  if (parseInfo.position < parseInfo.currentPosition) {
917
- result.push(yieldResult(parseInfo));
1580
+ return this.makeToken(parseInfo);
918
1581
  }
919
- result.push(yieldResult(parseInfo, EnumToken.EOFTokenType));
1582
+ return this.makeToken(parseInfo, EnumToken.EOFTokenType);
1583
+ // }
920
1584
  }
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;
1585
+ /**
1586
+ * tokenize readable stream
1587
+ * @param input
1588
+ * @param parseInfo
1589
+ */
1590
+ async tokenizeStream() {
1591
+ const decoder = new TextDecoder("utf-8");
1592
+ const reader = this.input.getReader();
1593
+ let parseInfo = this.parseInfo;
1594
+ parseInfo.stream = "";
1595
+ while (true) {
1596
+ const { done, value } = await reader.read();
1597
+ const stream = ArrayBuffer.isView(value) ? decoder.decode(value, { stream: true }) : value;
1598
+ if (!done) {
1599
+ parseInfo.source.append(stream);
1600
+ }
1601
+ else {
1602
+ break;
1603
+ }
947
1604
  }
1605
+ parseInfo.stream = parseInfo.source.getContent();
1606
+ return this; // .next();
948
1607
  }
949
1608
  }
950
1609
 
951
- export { SymbolsMapTokens, TokenMap, consumeString, hintsEnum, match, next, peek, tokenize, tokenizeStream, yieldResult };
1610
+ export { TokenMap, Tokenizer, hintsEnum };