@tbela99/css-parser 1.4.2 → 1.4.4

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 (122) hide show
  1. package/CHANGELOG.md +354 -4
  2. package/README.md +69 -1077
  3. package/dist/config.json.js +76 -3
  4. package/dist/index-umd-web.js +26838 -18743
  5. package/dist/index.cjs +26819 -18731
  6. package/dist/index.d.ts +2471 -1884
  7. package/dist/lib/ast/clone.js +46 -0
  8. package/dist/lib/ast/expand.js +90 -65
  9. package/dist/lib/ast/features/calc.js +86 -43
  10. package/dist/lib/ast/features/if.js +221 -0
  11. package/dist/lib/ast/features/index.js +1 -0
  12. package/dist/lib/ast/features/inlinecssvariables.js +33 -28
  13. package/dist/lib/ast/features/prefix.js +406 -44
  14. package/dist/lib/ast/features/shorthand.js +6 -9
  15. package/dist/lib/ast/features/transform.js +14 -15
  16. package/dist/lib/ast/find.js +175 -0
  17. package/dist/lib/ast/math/expression.js +145 -99
  18. package/dist/lib/ast/math/math.js +30 -41
  19. package/dist/lib/ast/minify.js +551 -232
  20. package/dist/lib/ast/transform/compute.js +82 -69
  21. package/dist/lib/ast/transform/matrix.js +12 -26
  22. package/dist/lib/ast/transform/minify.js +154 -150
  23. package/dist/lib/ast/transform/perspective.js +1 -1
  24. package/dist/lib/ast/transform/rotate.js +1 -1
  25. package/dist/lib/ast/transform/scale.js +1 -1
  26. package/dist/lib/ast/transform/skew.js +1 -1
  27. package/dist/lib/ast/transform/translate.js +1 -1
  28. package/dist/lib/ast/transform/utils.js +3 -2
  29. package/dist/lib/ast/types.js +345 -30
  30. package/dist/lib/ast/walk.js +119 -64
  31. package/dist/lib/fs/resolve.js +94 -88
  32. package/dist/lib/parser/declaration/list.js +117 -26
  33. package/dist/lib/parser/declaration/map.js +141 -96
  34. package/dist/lib/parser/declaration/set.js +30 -25
  35. package/dist/lib/parser/parse.js +1810 -1739
  36. package/dist/lib/parser/tokenize.js +662 -403
  37. package/dist/lib/parser/utils/angle.js +29 -0
  38. package/dist/lib/parser/utils/at-rule-container.js +486 -0
  39. package/dist/lib/parser/utils/at-rule-font-feature-values.js +13 -0
  40. package/dist/lib/parser/utils/at-rule-generic.js +118 -0
  41. package/dist/lib/parser/utils/at-rule-import.js +393 -0
  42. package/dist/lib/parser/utils/at-rule-media.js +603 -0
  43. package/dist/lib/parser/utils/at-rule-support.js +431 -0
  44. package/dist/lib/parser/utils/at-rule-when-else.js +363 -0
  45. package/dist/lib/parser/utils/at-rule.js +31 -0
  46. package/dist/lib/parser/utils/cache.js +19 -0
  47. package/dist/lib/parser/utils/config.js +1 -0
  48. package/dist/lib/parser/utils/declaration.js +672 -91
  49. package/dist/lib/parser/utils/hash.js +1 -1
  50. package/dist/lib/parser/utils/selector.js +518 -0
  51. package/dist/lib/parser/utils/text.js +17 -1
  52. package/dist/lib/parser/utils/token.js +104 -0
  53. package/dist/lib/parser/utils/type.js +29 -18
  54. package/dist/lib/renderer/render.js +870 -269
  55. package/dist/lib/renderer/sourcemap/sourcemap.js +17 -7
  56. package/dist/lib/syntax/color/a98rgb.js +8 -12
  57. package/dist/lib/syntax/color/alpha.js +64 -0
  58. package/dist/lib/syntax/color/cmyk.js +23 -21
  59. package/dist/lib/syntax/color/color-mix.js +311 -237
  60. package/dist/lib/syntax/color/color.js +98 -92
  61. package/dist/lib/syntax/color/hex.js +29 -33
  62. package/dist/lib/syntax/color/hsl.js +7 -14
  63. package/dist/lib/syntax/color/hwb.js +14 -27
  64. package/dist/lib/syntax/color/lab.js +23 -24
  65. package/dist/lib/syntax/color/lch.js +16 -18
  66. package/dist/lib/syntax/color/oklab.js +21 -40
  67. package/dist/lib/syntax/color/oklch.js +16 -18
  68. package/dist/lib/syntax/color/p3.js +0 -8
  69. package/dist/lib/syntax/color/rec2020.js +9 -13
  70. package/dist/lib/syntax/color/relativecolor.js +160 -48
  71. package/dist/lib/syntax/color/rgb.js +14 -18
  72. package/dist/lib/syntax/color/srgb.js +30 -29
  73. package/dist/lib/syntax/color/utils/color-type.js +18 -0
  74. package/dist/lib/syntax/color/utils/colorspace.js +46 -0
  75. package/dist/lib/syntax/color/utils/components.js +36 -16
  76. package/dist/lib/syntax/color/utils/distance.js +21 -11
  77. package/dist/lib/syntax/color/utils/make-color.js +56 -0
  78. package/dist/lib/syntax/color/xyz.js +10 -22
  79. package/dist/lib/syntax/color/xyzd50.js +6 -20
  80. package/dist/lib/syntax/constants.js +464 -0
  81. package/dist/lib/syntax/syntax.js +825 -592
  82. package/dist/lib/validation/config.js +72 -33
  83. package/dist/lib/validation/config.json.js +1185 -85
  84. package/dist/lib/validation/match.js +3120 -0
  85. package/dist/lib/validation/parser/parse.js +683 -935
  86. package/dist/lib/validation/parser/typedef.js +106 -0
  87. package/dist/lib/validation/utils/list.js +4 -11
  88. package/dist/lib/validation/utils/whitespace.js +2 -8
  89. package/dist/node.js +102 -57
  90. package/dist/web.js +100 -48
  91. package/package.json +31 -17
  92. package/dist/lib/syntax/color/utils/constants.js +0 -214
  93. package/dist/lib/syntax/utils.js +0 -70
  94. package/dist/lib/validation/at-rules/container.js +0 -342
  95. package/dist/lib/validation/at-rules/counter-style.js +0 -90
  96. package/dist/lib/validation/at-rules/custom-media.js +0 -50
  97. package/dist/lib/validation/at-rules/document.js +0 -89
  98. package/dist/lib/validation/at-rules/else.js +0 -5
  99. package/dist/lib/validation/at-rules/font-feature-values.js +0 -63
  100. package/dist/lib/validation/at-rules/import.js +0 -150
  101. package/dist/lib/validation/at-rules/keyframes.js +0 -67
  102. package/dist/lib/validation/at-rules/layer.js +0 -41
  103. package/dist/lib/validation/at-rules/media.js +0 -255
  104. package/dist/lib/validation/at-rules/namespace.js +0 -81
  105. package/dist/lib/validation/at-rules/page-margin-box.js +0 -64
  106. package/dist/lib/validation/at-rules/page.js +0 -100
  107. package/dist/lib/validation/at-rules/supports.js +0 -295
  108. package/dist/lib/validation/at-rules/when.js +0 -185
  109. package/dist/lib/validation/atrule.js +0 -184
  110. package/dist/lib/validation/selector.js +0 -36
  111. package/dist/lib/validation/syntax.js +0 -1073
  112. package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -27
  113. package/dist/lib/validation/syntaxes/complex-selector.js +0 -52
  114. package/dist/lib/validation/syntaxes/compound-selector.js +0 -196
  115. package/dist/lib/validation/syntaxes/family-name.js +0 -57
  116. package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -36
  117. package/dist/lib/validation/syntaxes/layer-name.js +0 -57
  118. package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -31
  119. package/dist/lib/validation/syntaxes/relative-selector.js +0 -38
  120. package/dist/lib/validation/syntaxes/selector-list.js +0 -5
  121. package/dist/lib/validation/syntaxes/selector.js +0 -5
  122. package/dist/lib/validation/syntaxes/url.js +0 -40
@@ -1,81 +1,152 @@
1
- import { EnumToken } from '../ast/types.js';
2
- import '../ast/minify.js';
3
- import '../ast/walk.js';
4
- import './parse.js';
5
- import './utils/config.js';
6
- import { isWhiteSpace, isIdentStart, isIdent, isNewLine, isDigit } from '../syntax/syntax.js';
7
- import '../syntax/color/utils/constants.js';
8
- import '../renderer/sourcemap/lib/encode.js';
1
+ import { EnumToken, ColorType } from '../ast/types.js';
2
+ import { definedPropertySettings, wildCardFuncs, whenElseFunc, transformFunctions, mathFuncs, colorsFunc, timingFunc, supportFunc, generalEnclosedFunc, timelineFunc, imageFunc, gridTemplateFunc, urlFunc, containerFunc } from '../syntax/constants.js';
3
+ import { isDigit, isPseudo, isIdent, isWhiteSpace, isNumber, isNewLine, isHexColor, isHash, isPercentage, parseDimension } from '../syntax/syntax.js';
4
+ import { equalsIgnoreCase } from './utils/text.js';
9
5
 
6
+ const SymbolsMapTokens = {
7
+ "+": EnumToken.Plus,
8
+ "=": EnumToken.DelimTokenType,
9
+ "|": EnumToken.Pipe,
10
+ "||": EnumToken.ColumnCombinatorTokenType,
11
+ "|=": EnumToken.DashMatchTokenType,
12
+ "&": EnumToken.NestingSelectorTokenType,
13
+ "*": EnumToken.Star,
14
+ "*=": EnumToken.ContainMatchTokenType,
15
+ "~": EnumToken.Tilda,
16
+ "~=": EnumToken.IncludeMatchTokenType,
17
+ "^=": EnumToken.StartMatchTokenType,
18
+ "$=": EnumToken.EndMatchTokenType,
19
+ ",": EnumToken.Comma,
20
+ ":": EnumToken.ColonTokenType,
21
+ "::": EnumToken.DoubleColonTokenType,
22
+ ";": EnumToken.SemiColonTokenType,
23
+ "(": EnumToken.StartParensTokenType,
24
+ ")": EnumToken.EndParensTokenType,
25
+ "[": EnumToken.AttrStartTokenType,
26
+ "]": EnumToken.AttrEndTokenType,
27
+ "{": EnumToken.BlockStartTokenType,
28
+ "}": EnumToken.BlockEndTokenType,
29
+ "<=": EnumToken.LteTokenType,
30
+ ">": EnumToken.GtTokenType,
31
+ ">=": EnumToken.GteTokenType,
32
+ " ": EnumToken.Whitespace,
33
+ "\t": EnumToken.Whitespace,
34
+ "\r": EnumToken.Whitespace,
35
+ "\n": EnumToken.Whitespace,
36
+ "\f": EnumToken.Whitespace,
37
+ ...containerFunc.reduce((acc, curr) => {
38
+ acc[curr + "("] = EnumToken.ContainerFunctionTokenDefType;
39
+ return acc;
40
+ }, Object.create(null)),
41
+ ...urlFunc.reduce((acc, curr) => {
42
+ acc[curr + "("] = EnumToken.UrlFunctionTokenDefType;
43
+ return acc;
44
+ }, Object.create(null)),
45
+ ...gridTemplateFunc.reduce((acc, curr) => {
46
+ acc[curr + "("] = EnumToken.GridTemplateFuncTokenDefType;
47
+ return acc;
48
+ }, Object.create(null)),
49
+ ...imageFunc.reduce((acc, curr) => {
50
+ acc[curr + "("] = EnumToken.ImageFunctionTokenDefType;
51
+ return acc;
52
+ }, Object.create(null)),
53
+ ...timelineFunc.reduce((acc, curr) => {
54
+ acc[curr + "("] = EnumToken.TimelineFunctionTokenDefType;
55
+ return acc;
56
+ }, Object.create(null)),
57
+ ...generalEnclosedFunc.reduce((acc, curr) => {
58
+ acc[curr + "("] = EnumToken.GeneralEnclosedFunctionTokenDefType;
59
+ return acc;
60
+ }, Object.create(null)),
61
+ ...supportFunc.reduce((acc, curr) => {
62
+ acc[curr + "("] = EnumToken.SupportsFunctionTokenDefType;
63
+ return acc;
64
+ }, Object.create(null)),
65
+ ...timingFunc.reduce((acc, curr) => {
66
+ acc[curr + "("] = EnumToken.TimingFunctionTokenDefType;
67
+ return acc;
68
+ }, Object.create(null)),
69
+ ...colorsFunc.reduce((acc, curr) => {
70
+ acc[curr + "("] = EnumToken.ColorFunctionTokenDefType;
71
+ return acc;
72
+ }, Object.create(null)),
73
+ ...mathFuncs.reduce((acc, curr) => {
74
+ acc[curr + "("] = EnumToken.MathFunctionTokenDefType;
75
+ return acc;
76
+ }, Object.create(null)),
77
+ ...transformFunctions.reduce((acc, curr) => {
78
+ acc[curr.toLowerCase() + "("] = EnumToken.TransformFunctionTokenDefType;
79
+ return acc;
80
+ }, Object.create(null)),
81
+ ...whenElseFunc.reduce((acc, curr) => {
82
+ acc[curr + "("] = EnumToken.WhenElseFunctionTokenDefType;
83
+ return acc;
84
+ }, Object.create(null)),
85
+ ...wildCardFuncs.reduce((acc, curr) => {
86
+ acc[curr + "("] = EnumToken.WildCardFunctionTokenDefType;
87
+ return acc;
88
+ }, Object.create(null)),
89
+ };
90
+ // do not capture the value
91
+ const hintsEnum = new Set([
92
+ EnumToken.CommaTokenType,
93
+ EnumToken.ImportantTokenType,
94
+ EnumToken.SemiColonTokenType,
95
+ EnumToken.BlockStartTokenType,
96
+ EnumToken.BlockEndTokenType,
97
+ EnumToken.StartParensTokenType,
98
+ EnumToken.EndParensTokenType,
99
+ EnumToken.ColonTokenType,
100
+ EnumToken.EOFTokenType,
101
+ ]);
10
102
  var TokenMap;
11
103
  (function (TokenMap) {
104
+ TokenMap[TokenMap["EXCLAMATION"] = 33] = "EXCLAMATION";
12
105
  TokenMap[TokenMap["SLASH"] = 47] = "SLASH";
13
- TokenMap[TokenMap["AMPERSAND"] = 38] = "AMPERSAND";
14
106
  TokenMap[TokenMap["LOWERTHAN"] = 60] = "LOWERTHAN";
15
107
  TokenMap[TokenMap["HASH"] = 35] = "HASH";
16
108
  TokenMap[TokenMap["REVERSE_SOLIDUS"] = 92] = "REVERSE_SOLIDUS";
17
109
  TokenMap[TokenMap["DOUBLE_QUOTE"] = 34] = "DOUBLE_QUOTE";
18
110
  TokenMap[TokenMap["SINGLE_QUOTE"] = 39] = "SINGLE_QUOTE";
19
- // ^
20
- TokenMap[TokenMap["CIRCUMFLEX"] = 94] = "CIRCUMFLEX";
21
- TokenMap[TokenMap["TILDA"] = 126] = "TILDA";
22
- TokenMap[TokenMap["PIPE"] = 124] = "PIPE";
23
- TokenMap[TokenMap["DOLLAR"] = 36] = "DOLLAR";
24
- TokenMap[TokenMap["GREATER_THAN"] = 62] = "GREATER_THAN";
25
111
  TokenMap[TokenMap["DOT"] = 46] = "DOT";
26
- TokenMap[TokenMap["PLUS"] = 43] = "PLUS";
112
+ TokenMap[TokenMap["AT"] = 64] = "AT";
113
+ TokenMap[TokenMap["PIPE"] = 124] = "PIPE";
114
+ TokenMap[TokenMap["EQUALS"] = 61] = "EQUALS";
115
+ TokenMap[TokenMap["AMPERSAND"] = 38] = "AMPERSAND";
27
116
  TokenMap[TokenMap["STAR"] = 42] = "STAR";
28
- TokenMap[TokenMap["COLON"] = 58] = "COLON";
117
+ TokenMap[TokenMap["TILDA"] = 126] = "TILDA";
118
+ TokenMap[TokenMap["CARET"] = 94] = "CARET";
119
+ TokenMap[TokenMap["DOLLAR"] = 36] = "DOLLAR";
29
120
  TokenMap[TokenMap["COMMA"] = 44] = "COMMA";
30
- TokenMap[TokenMap["EQUAL"] = 61] = "EQUAL";
31
- TokenMap[TokenMap["CLOSE_PAREN"] = 41] = "CLOSE_PAREN";
32
- TokenMap[TokenMap["OPEN_PAREN"] = 40] = "OPEN_PAREN";
33
- TokenMap[TokenMap["OPEN_BRACKET"] = 91] = "OPEN_BRACKET";
34
- TokenMap[TokenMap["CLOSE_BRACKET"] = 93] = "CLOSE_BRACKET";
35
- TokenMap[TokenMap["OPEN_CURLY_BRACE"] = 123] = "OPEN_CURLY_BRACE";
36
- TokenMap[TokenMap["CLOSE_CURLY_BRACE"] = 125] = "CLOSE_CURLY_BRACE";
121
+ TokenMap[TokenMap["COLON"] = 58] = "COLON";
37
122
  TokenMap[TokenMap["SEMICOLON"] = 59] = "SEMICOLON";
38
- TokenMap[TokenMap["EXCLAMATION"] = 33] = "EXCLAMATION";
39
- TokenMap[TokenMap["AT"] = 64] = "AT";
123
+ TokenMap[TokenMap["LEFT_PARENTHESIS"] = 40] = "LEFT_PARENTHESIS";
124
+ TokenMap[TokenMap["RIGHT_PARENTHESIS"] = 41] = "RIGHT_PARENTHESIS";
125
+ TokenMap[TokenMap["LEFT_BRACKETS"] = 91] = "LEFT_BRACKETS";
126
+ TokenMap[TokenMap["RIGHT_BRACKETS"] = 93] = "RIGHT_BRACKETS";
127
+ TokenMap[TokenMap["LEFT_BRACE"] = 123] = "LEFT_BRACE";
128
+ TokenMap[TokenMap["RIGHT_BRACE"] = 125] = "RIGHT_BRACE";
129
+ TokenMap[TokenMap["PLUS"] = 43] = "PLUS";
130
+ TokenMap[TokenMap["MINUS"] = 45] = "MINUS";
131
+ TokenMap[TokenMap["GREATERTHAN"] = 62] = "GREATERTHAN";
40
132
  })(TokenMap || (TokenMap = {}));
41
- function consumeWhiteSpace(parseInfo) {
42
- let count = 0;
43
- while (isWhiteSpace(parseInfo.stream.charCodeAt(count + parseInfo.currentPosition.ind + 1))) {
44
- count++;
45
- }
46
- next(parseInfo, count);
47
- return count;
48
- }
49
- function pushToken(token, parseInfo, hint) {
50
- const result = {
51
- token,
52
- len: parseInfo.currentPosition.ind - parseInfo.position.ind - 1,
53
- hint,
54
- sta: { ...parseInfo.position },
55
- end: { ...parseInfo.currentPosition },
56
- bytesIn: parseInfo.currentPosition.ind + 1
57
- };
58
- parseInfo.position.ind = parseInfo.currentPosition.ind;
59
- parseInfo.position.lin = parseInfo.currentPosition.lin;
60
- parseInfo.position.col = Math.max(parseInfo.currentPosition.col, 1);
61
- if (result.end.col == 0) {
62
- result.end.col = 1;
63
- }
64
- return result;
65
- }
66
- function* consumeString(quoteStr, buffer, parseInfo) {
133
+ function consumeString(quoteStr, buffer, parseInfo) {
67
134
  const quote = quoteStr;
68
135
  let value;
69
- let hasNewLine = false;
136
+ const result = [];
70
137
  if (buffer.length > 0) {
71
- yield pushToken(buffer, parseInfo);
72
- buffer = '';
138
+ result.push(yieldResult(buffer, parseInfo));
139
+ buffer = "";
73
140
  }
74
141
  buffer += quoteStr;
75
- while (value = peek(parseInfo)) {
76
- if (value == '\\') {
142
+ while ((value = parseInfo.stream.charAt(parseInfo.currentPosition.ind - parseInfo.offset + 1))) {
143
+ if (value == "\\") {
144
+ if ("\\" == parseInfo.stream.charAt(parseInfo.currentPosition.ind - parseInfo.offset + 2)) {
145
+ buffer += next(parseInfo, 2);
146
+ continue;
147
+ }
77
148
  const sequence = peek(parseInfo, 6);
78
- let escapeSequence = '';
149
+ let escapeSequence = "";
79
150
  let codepoint;
80
151
  let i;
81
152
  for (i = 1; i < sequence.length; i++) {
@@ -96,15 +167,21 @@ function* consumeString(quoteStr, buffer, parseInfo) {
96
167
  const codepoint = parseInt(escapeSequence, 16);
97
168
  if (codepoint == 0 ||
98
169
  // leading surrogate
99
- (0xD800 <= codepoint && codepoint <= 0xDBFF) ||
170
+ (0xd800 <= codepoint && codepoint <= 0xdbff) ||
100
171
  // trailing surrogate
101
- (0xDC00 <= codepoint && codepoint <= 0xDFFF)) {
102
- buffer += String.fromCodePoint(0xFFFD);
172
+ (0xdc00 <= codepoint && codepoint <= 0xdfff)) {
173
+ buffer += String.fromCodePoint(0xfffd);
103
174
  }
104
175
  else {
105
176
  buffer += String.fromCodePoint(codepoint);
106
177
  }
107
- next(parseInfo, escapeSequence.length + 1 + (isWhiteSpace(peek(parseInfo)?.charCodeAt(0)) ? 1 : 0));
178
+ next(parseInfo, escapeSequence.length +
179
+ 1 +
180
+ (isWhiteSpace(parseInfo.stream
181
+ .charAt(parseInfo.currentPosition.ind - parseInfo.offset + 1)
182
+ ?.charCodeAt(0))
183
+ ? 1
184
+ : 0));
108
185
  continue;
109
186
  }
110
187
  buffer += next(parseInfo, 2);
@@ -112,34 +189,170 @@ function* consumeString(quoteStr, buffer, parseInfo) {
112
189
  }
113
190
  if (value == quote) {
114
191
  buffer += value;
115
- yield pushToken(buffer, parseInfo, hasNewLine ? EnumToken.BadStringTokenType : EnumToken.StringTokenType);
192
+ result.push(yieldResult(buffer, parseInfo,
193
+ /* hasNewLine ? EnumToken.BadStringTokenType : */ EnumToken.StringTokenType));
116
194
  next(parseInfo);
117
- buffer = '';
118
- return;
195
+ buffer = "";
196
+ return result;
119
197
  }
120
198
  if (isNewLine(value.charCodeAt(0))) {
121
- hasNewLine = true;
122
- }
123
- if (hasNewLine && value == ';') {
124
- yield pushToken(buffer + value, parseInfo, EnumToken.BadStringTokenType);
125
- buffer = '';
126
- next(parseInfo);
127
- break;
199
+ result.push(yieldResult(buffer + next(parseInfo), parseInfo, EnumToken.BadStringTokenType));
200
+ return result;
128
201
  }
129
202
  buffer += value;
130
203
  next(parseInfo);
131
204
  }
132
- if (hasNewLine) {
133
- yield pushToken(buffer, parseInfo, EnumToken.BadStringTokenType);
205
+ // EOF - 'Unclosed-string' fixed
206
+ result.push(yieldResult(buffer + quote, parseInfo, EnumToken.StringTokenType));
207
+ return result;
208
+ }
209
+ function getTokenType(val, hint) {
210
+ let token = null;
211
+ let dimension;
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;
253
+ 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 };
134
259
  }
135
260
  else {
136
- // EOF - 'Unclosed-string' fixed
137
- yield pushToken(buffer + quote, parseInfo, EnumToken.StringTokenType);
261
+ let slice = val.slice(1);
262
+ if (val.charAt(0) == "@" && isIdent(slice)) {
263
+ token = {
264
+ typ: EnumToken.AtRuleTokenType,
265
+ nam: slice,
266
+ };
267
+ }
268
+ else if (val.charAt(0) == "." && isIdent(slice)) {
269
+ token = {
270
+ typ: EnumToken.ClassSelectorTokenType,
271
+ val,
272
+ };
273
+ }
274
+ else if (val.charAt(0) == "#") {
275
+ if (isHexColor(val)) {
276
+ token = {
277
+ typ: EnumToken.ColorTokenType,
278
+ val: val,
279
+ kin: ColorType.HEX,
280
+ };
281
+ }
282
+ else if (isHash(val)) {
283
+ token = {
284
+ typ: EnumToken.HashTokenType,
285
+ val: val,
286
+ };
287
+ }
288
+ }
289
+ else if ("\"'".includes(val.charAt(0))) {
290
+ token = {
291
+ typ: EnumToken.UnclosedStringTokenType,
292
+ val: val,
293
+ };
294
+ }
295
+ else if (isNumber(val)) {
296
+ token =
297
+ val[0] === "-" || val[0] === "+"
298
+ ? {
299
+ typ: EnumToken.NumberTokenType,
300
+ sign: val[0],
301
+ val: +val,
302
+ }
303
+ : {
304
+ typ: EnumToken.NumberTokenType,
305
+ val: +val,
306
+ };
307
+ }
308
+ else if (isPercentage(val)) {
309
+ token = {
310
+ typ: EnumToken.PercentageTokenType,
311
+ val: +val.slice(0, -1),
312
+ };
313
+ }
314
+ else if ((dimension = parseDimension(val))) {
315
+ token = dimension;
316
+ }
317
+ else if (isIdent(val)) {
318
+ token = {
319
+ typ: val.startsWith("--") ? EnumToken.DashedIdenTokenType : EnumToken.IdenTokenType,
320
+ val,
321
+ };
322
+ }
323
+ }
324
+ if (token == null) {
325
+ token = {
326
+ typ: EnumToken.LiteralTokenType,
327
+ val,
328
+ };
138
329
  }
330
+ return token;
331
+ }
332
+ function yieldResult(val, parseInfo, hint) {
333
+ const token = getTokenType(val, hint);
334
+ Object.defineProperty(token, "loc", {
335
+ ...definedPropertySettings,
336
+ enumerable: false,
337
+ value: {
338
+ src: parseInfo.src,
339
+ sta: { ...parseInfo.position },
340
+ end: { ...parseInfo.currentPosition },
341
+ },
342
+ });
343
+ parseInfo.position.ind = parseInfo.currentPosition.ind;
344
+ parseInfo.position.lin = parseInfo.currentPosition.lin;
345
+ parseInfo.position.col = parseInfo.currentPosition.col;
346
+ return { token, bytesIn: parseInfo.currentPosition.ind + 1 };
139
347
  }
140
348
  function match(parseInfo, input) {
141
- const position = parseInfo.currentPosition.ind - parseInfo.offset;
142
- return parseInfo.stream.slice(position + 1, position + input.length + 1) == input;
349
+ let position = parseInfo.currentPosition.ind - parseInfo.offset;
350
+ for (let i = 0; i < input.length; i++) {
351
+ if (parseInfo.stream[position + i + 1] != input.charAt(i)) {
352
+ return false;
353
+ }
354
+ }
355
+ return true;
143
356
  }
144
357
  function peek(parseInfo, count = 1) {
145
358
  if (count == 1) {
@@ -148,408 +361,433 @@ function peek(parseInfo, count = 1) {
148
361
  const position = parseInfo.currentPosition.ind - parseInfo.offset;
149
362
  return parseInfo.stream.slice(position + 1, position + count + 1);
150
363
  }
151
- function prev(parseInfo) {
152
- return parseInfo.offset == parseInfo.currentPosition.ind ? parseInfo.buffer.slice(-1) : parseInfo.stream.charAt(parseInfo.currentPosition.ind - parseInfo.offset - 1);
153
- }
154
364
  function next(parseInfo, count = 1) {
155
- let char = '';
156
- let chr = '';
157
365
  let position = parseInfo.currentPosition.ind - parseInfo.offset;
158
- while (count-- && (chr = parseInfo.stream.charAt(position + 1))) {
159
- char += chr;
160
- const codepoint = parseInfo.stream.charCodeAt(++position);
161
- ++parseInfo.currentPosition.ind;
162
- if (isNewLine(codepoint)) {
366
+ let char = count == 1 ? parseInfo.stream.charAt(position + 1) : parseInfo.stream.slice(position + 1, position + 1 + count);
367
+ let i = 0;
368
+ for (; i < char.length; i++) {
369
+ const codepoint = char[i].charCodeAt(0);
370
+ if (codepoint == 0xa ||
371
+ codepoint == 0xb ||
372
+ codepoint == 0xc ||
373
+ codepoint == 0xd ||
374
+ codepoint == 0x2028 ||
375
+ codepoint == 0x2029) {
163
376
  parseInfo.currentPosition.lin++;
164
- parseInfo.currentPosition.col = 0;
377
+ parseInfo.currentPosition.col = 1;
165
378
  }
166
379
  else {
167
380
  parseInfo.currentPosition.col++;
168
381
  }
169
382
  }
383
+ parseInfo.currentPosition.ind += char.length;
170
384
  return char;
171
385
  }
172
386
  /**
173
- * tokenize css string
387
+ * Tokenize css string
174
388
  * @param parseInfo
175
389
  * @param yieldEOFToken
176
390
  */
177
- function* tokenize(parseInfo, yieldEOFToken = true) {
391
+ function tokenize(parseInfo, yieldEOFToken = true) {
392
+ if (typeof parseInfo == "string") {
393
+ parseInfo = {
394
+ stream: parseInfo,
395
+ buffer: "",
396
+ src: "",
397
+ offset: 0,
398
+ time: 0,
399
+ position: { ind: 0, lin: 1, col: 0 },
400
+ currentPosition: { ind: -1, lin: 1, col: 0 },
401
+ };
402
+ }
178
403
  let value;
404
+ let nextValue;
179
405
  let buffer = parseInfo.buffer;
180
406
  let charCode;
181
- parseInfo.buffer = '';
182
- while (value = next(parseInfo)) {
407
+ let nextCharCode;
408
+ const startTime = performance.now();
409
+ const result = [];
410
+ parseInfo.buffer = "";
411
+ while ((value = next(parseInfo))) {
412
+ nextValue = parseInfo.stream.charAt(parseInfo.currentPosition.ind - parseInfo.offset + 1);
183
413
  charCode = value.charCodeAt(0);
184
- if (isWhiteSpace(charCode)) {
185
- if (buffer.length > 0) {
186
- yield pushToken(buffer, parseInfo);
187
- buffer = '';
188
- }
189
- buffer += value;
190
- while (value = next(parseInfo)) {
191
- charCode = value.charCodeAt(0);
192
- if (!isWhiteSpace(charCode)) {
414
+ nextCharCode = nextValue.charCodeAt(0);
415
+ switch (charCode) {
416
+ case 61 /* TokenMap.EQUALS */:
417
+ if (buffer.length > 0) {
418
+ result.push(yieldResult(buffer, parseInfo));
419
+ buffer = "";
420
+ }
421
+ result.push(yieldResult(value, parseInfo, EnumToken.DelimTokenType));
422
+ break;
423
+ // '+' or '-'
424
+ case 43 /* TokenMap.PLUS */:
425
+ case 45 /* TokenMap.MINUS */:
426
+ if (charCode === 43 /* TokenMap.PLUS */ && !isNumber(nextValue)) {
427
+ if (buffer.length > 0) {
428
+ result.push(yieldResult(buffer, parseInfo));
429
+ buffer = "";
430
+ }
431
+ result.push(yieldResult(value, parseInfo, SymbolsMapTokens[value]));
193
432
  break;
194
433
  }
195
434
  buffer += value;
196
- }
197
- yield pushToken(buffer, parseInfo, EnumToken.WhitespaceTokenType);
198
- buffer = '';
199
- }
200
- switch (charCode) {
201
- case 47 /* TokenMap.SLASH */:
435
+ break;
436
+ // '{'
437
+ case 123 /* TokenMap.LEFT_BRACE */:
202
438
  if (buffer.length > 0) {
203
- yield pushToken(buffer, parseInfo);
204
- buffer = '';
205
- if (!match(parseInfo, '*')) {
206
- yield pushToken(value, parseInfo);
439
+ result.push(yieldResult(buffer, parseInfo));
440
+ buffer = "";
441
+ }
442
+ result.push(yieldResult(value, parseInfo, EnumToken.BlockStartTokenType));
443
+ break;
444
+ // '}'
445
+ case 125 /* TokenMap.RIGHT_BRACE */:
446
+ if (buffer.length > 0) {
447
+ result.push(yieldResult(buffer, parseInfo));
448
+ buffer = "";
449
+ }
450
+ result.push(yieldResult(value, parseInfo, EnumToken.BlockEndTokenType));
451
+ break;
452
+ // '('
453
+ case 40 /* TokenMap.LEFT_PARENTHESIS */:
454
+ if (buffer.length > 0) {
455
+ if (buffer[0] === ":" && isPseudo(buffer)) {
456
+ result.push(yieldResult(buffer, parseInfo, EnumToken.PseudoClassFunctionTokenDefType));
457
+ buffer = "";
207
458
  break;
208
459
  }
209
- }
210
- buffer += value;
211
- if (match(parseInfo, '*')) {
212
- buffer += next(parseInfo);
213
- while (value = next(parseInfo)) {
214
- if (value == '*') {
215
- buffer += value;
216
- if (match(parseInfo, '/')) {
217
- yield pushToken(buffer + next(parseInfo), parseInfo, EnumToken.CommentTokenType);
218
- buffer = '';
460
+ else if (isIdent(buffer)) {
461
+ const hint = buffer.startsWith("--")
462
+ ? EnumToken.CustomFunctionTokenDefType
463
+ : (SymbolsMapTokens[buffer.toLowerCase() + "("] ?? EnumToken.FunctionTokenDefType);
464
+ result.push(yieldResult(buffer, parseInfo, hint));
465
+ buffer = "";
466
+ if (hint === EnumToken.UrlFunctionTokenDefType) {
467
+ value = peek(parseInfo);
468
+ // consume an <url>
469
+ while (isWhiteSpace((charCode = value.charCodeAt(0)))) {
470
+ buffer += next(parseInfo);
471
+ value = peek(parseInfo);
472
+ charCode = value.charCodeAt(0);
473
+ if (value === "/" && match(parseInfo, "/*")) {
474
+ if (buffer.length > 0) {
475
+ result.push(yieldResult(buffer, parseInfo));
476
+ buffer = "";
477
+ }
478
+ buffer += next(parseInfo, 2);
479
+ while ((value = next(parseInfo))) {
480
+ if (value == "*") {
481
+ buffer += value;
482
+ if (match(parseInfo, "/")) {
483
+ result.push(yieldResult(buffer + next(parseInfo), parseInfo, EnumToken.CommentTokenType));
484
+ buffer = "";
485
+ break;
486
+ }
487
+ }
488
+ else {
489
+ buffer += value;
490
+ }
491
+ }
492
+ if (buffer.length > 0) {
493
+ result.push(yieldResult(buffer, parseInfo, EnumToken.BadCommentTokenType));
494
+ buffer = "";
495
+ }
496
+ value = peek(parseInfo);
497
+ charCode = value.charCodeAt(0);
498
+ }
499
+ }
500
+ if (buffer.length > 0) {
501
+ result.push(yieldResult(buffer, parseInfo, EnumToken.WhitespaceTokenType));
502
+ buffer = "";
503
+ }
504
+ if (value === ")" || value === '"' || value === "'") {
219
505
  break;
220
506
  }
507
+ do {
508
+ buffer += next(parseInfo);
509
+ value = peek(parseInfo);
510
+ charCode = value.charCodeAt(0);
511
+ } while (value !== ")" &&
512
+ !isWhiteSpace(charCode) &&
513
+ !(value === "/" && match(parseInfo, "/*")));
514
+ if (buffer.length > 0) {
515
+ result.push(yieldResult(buffer, parseInfo, peek(parseInfo) === ""
516
+ ? EnumToken.BadUrlTokenType
517
+ : EnumToken.UrlTokenTokenType));
518
+ buffer = "";
519
+ }
221
520
  }
222
- else {
223
- buffer += value;
224
- }
225
- }
226
- if (buffer.length > 0) {
227
- yield pushToken(buffer, parseInfo, EnumToken.BadCommentTokenType);
228
- buffer = '';
521
+ break;
229
522
  }
230
523
  }
524
+ result.push(yieldResult(value, parseInfo, EnumToken.StartParensTokenType));
525
+ buffer = "";
231
526
  break;
232
- case 38 /* TokenMap.AMPERSAND */:
527
+ // ')'
528
+ case 41 /* TokenMap.RIGHT_PARENTHESIS */:
233
529
  if (buffer.length > 0) {
234
- yield pushToken(buffer, parseInfo);
235
- buffer = '';
530
+ result.push(yieldResult(buffer, parseInfo));
531
+ buffer = "";
236
532
  }
237
- yield pushToken(value, parseInfo);
533
+ result.push(yieldResult(value, parseInfo, EnumToken.EndParensTokenType));
238
534
  break;
239
- case 60 /* TokenMap.LOWERTHAN */:
535
+ // '['
536
+ case 91 /* TokenMap.LEFT_BRACKETS */:
240
537
  if (buffer.length > 0) {
241
- yield pushToken(buffer, parseInfo);
242
- buffer = '';
538
+ result.push(yieldResult(buffer, parseInfo));
539
+ buffer = "";
243
540
  }
244
- if (match(parseInfo, '=')) {
245
- yield pushToken(value + next(parseInfo), parseInfo, EnumToken.LteTokenType);
246
- break;
247
- }
248
- buffer += value;
249
- if (match(parseInfo, '!--')) {
250
- buffer += next(parseInfo, 3);
251
- while (value = next(parseInfo)) {
252
- buffer += value;
253
- if (value == '-' && match(parseInfo, '->')) {
254
- break;
255
- }
256
- }
257
- if (value === '') {
258
- yield pushToken(buffer, parseInfo, EnumToken.BadCdoTokenType);
259
- }
260
- else {
261
- yield pushToken(buffer + next(parseInfo, 2), parseInfo, EnumToken.CDOCOMMTokenType);
262
- }
263
- buffer = '';
541
+ result.push(yieldResult(value, 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 = "";
264
548
  }
549
+ result.push(yieldResult(value, parseInfo, EnumToken.AttrEndTokenType));
265
550
  break;
266
- case 35 /* TokenMap.HASH */:
551
+ case 59 /* TokenMap.SEMICOLON */:
267
552
  if (buffer.length > 0) {
268
- yield pushToken(buffer, parseInfo);
269
- buffer = '';
553
+ result.push(yieldResult(buffer, parseInfo));
554
+ buffer = "";
270
555
  }
271
- buffer += value;
556
+ result.push(yieldResult(value, parseInfo, EnumToken.SemiColonTokenType));
272
557
  break;
273
- case 92 /* TokenMap.REVERSE_SOLIDUS */:
274
- // EOF
275
- if (!(value = next(parseInfo))) {
276
- // end of stream ignore \\
277
- if (buffer.length > 0) {
278
- yield pushToken(buffer, parseInfo);
279
- buffer = '';
280
- }
558
+ case 58 /* TokenMap.COLON */:
559
+ if (buffer.length > 0) {
560
+ result.push(yieldResult(buffer, parseInfo));
561
+ buffer = "";
562
+ }
563
+ if (nextCharCode == 58 /* TokenMap.COLON */) {
564
+ result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.DoubleColonTokenType));
281
565
  break;
282
566
  }
283
- buffer += prev(parseInfo) + value;
567
+ result.push(yieldResult(value, parseInfo, EnumToken.ColonTokenType));
284
568
  break;
285
- case 39 /* TokenMap.SINGLE_QUOTE */:
286
- case 34 /* TokenMap.DOUBLE_QUOTE */:
287
- yield* consumeString(value, buffer, parseInfo);
288
- buffer = '';
569
+ // \n \r \f \v \t space
570
+ case 0x9:
571
+ case 0x20:
572
+ case 0xa:
573
+ case 0xb:
574
+ case 0xc:
575
+ case 0xd:
576
+ case 0x2028:
577
+ case 0x2029:
578
+ if (buffer.length > 0) {
579
+ result.push(yieldResult(buffer, parseInfo));
580
+ buffer = "";
581
+ }
582
+ buffer += value;
583
+ nextCharCode = parseInfo.stream
584
+ .charAt(parseInfo.currentPosition.ind - parseInfo.offset + 1)
585
+ .charCodeAt(0);
586
+ while (nextCharCode == 0x20 ||
587
+ (nextCharCode >= 0x9 && nextCharCode <= 0xd) ||
588
+ nextCharCode == 0x2028 ||
589
+ nextCharCode == 0x2029) {
590
+ value += next(parseInfo);
591
+ nextCharCode = parseInfo.stream
592
+ .charAt(parseInfo.currentPosition.ind - parseInfo.offset + 1)
593
+ .charCodeAt(0);
594
+ }
595
+ result.push(yieldResult(value, parseInfo, EnumToken.WhitespaceTokenType));
596
+ buffer = "";
597
+ break;
598
+ case 44 /* TokenMap.COMMA */:
599
+ if (buffer.length > 0) {
600
+ result.push(yieldResult(buffer, parseInfo));
601
+ buffer = "";
602
+ }
603
+ result.push(yieldResult(value, parseInfo, EnumToken.CommaTokenType));
289
604
  break;
290
- case 94 /* TokenMap.CIRCUMFLEX */:
291
- case 126 /* TokenMap.TILDA */:
292
- case 124 /* TokenMap.PIPE */:
293
605
  case 36 /* TokenMap.DOLLAR */:
294
606
  if (buffer.length > 0) {
295
- yield pushToken(buffer, parseInfo);
296
- buffer = '';
607
+ result.push(yieldResult(buffer, parseInfo));
608
+ buffer = "";
297
609
  }
298
- if (charCode == 124 /* TokenMap.PIPE */) {
299
- if (match(parseInfo, '|')) {
300
- yield pushToken(value + next(parseInfo), parseInfo, EnumToken.ColumnCombinatorTokenType);
301
- }
302
- else if (match(parseInfo, '=')) {
303
- buffer += next(parseInfo);
304
- yield pushToken(buffer, parseInfo);
305
- }
306
- else {
307
- yield pushToken('|', parseInfo);
308
- }
309
- buffer = '';
310
- continue;
610
+ if (nextCharCode == 61 /* TokenMap.EQUALS */) {
611
+ result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.EndMatchTokenType));
612
+ break;
311
613
  }
614
+ buffer += value;
615
+ break;
616
+ case 126 /* TokenMap.TILDA */:
312
617
  if (buffer.length > 0) {
313
- yield pushToken(buffer, parseInfo);
314
- buffer = '';
618
+ result.push(yieldResult(buffer, parseInfo));
619
+ buffer = "";
315
620
  }
316
- buffer += value;
317
- if (!(value = peek(parseInfo))) {
318
- yield pushToken(buffer, parseInfo);
319
- buffer = '';
621
+ if (nextCharCode == 61 /* TokenMap.EQUALS */) {
622
+ result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.IncludeMatchTokenType));
320
623
  break;
321
624
  }
322
- // ~=
323
- // ^=
324
- // $=
325
- // |=
326
- if (match(parseInfo, '=')) {
327
- next(parseInfo);
328
- switch (buffer.charCodeAt(0)) {
329
- case 126 /* TokenMap.TILDA */:
330
- yield pushToken(buffer, parseInfo, EnumToken.IncludeMatchTokenType);
331
- break;
332
- case 94 /* TokenMap.CIRCUMFLEX */:
333
- yield pushToken(buffer, parseInfo, EnumToken.StartMatchTokenType);
334
- break;
335
- case 36 /* TokenMap.DOLLAR */:
336
- yield pushToken(buffer, parseInfo, EnumToken.EndMatchTokenType);
337
- break;
338
- case 124 /* TokenMap.PIPE */:
339
- yield pushToken(buffer, parseInfo, EnumToken.DashMatchTokenType);
340
- break;
341
- }
342
- buffer = '';
625
+ result.push(yieldResult(value, parseInfo, EnumToken.Tilda));
626
+ buffer = "";
627
+ break;
628
+ // case '^':
629
+ case 94 /* TokenMap.CARET */:
630
+ if (buffer.length > 0) {
631
+ result.push(yieldResult(buffer, parseInfo));
632
+ buffer = "";
633
+ }
634
+ if (nextCharCode == 61 /* TokenMap.EQUALS */) {
635
+ result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.StartMatchTokenType));
343
636
  break;
344
637
  }
345
- yield pushToken(buffer, parseInfo);
346
- buffer = '';
638
+ buffer += value;
347
639
  break;
348
- case 62 /* TokenMap.GREATER_THAN */:
349
- if (buffer !== '') {
350
- yield pushToken(buffer, parseInfo);
351
- buffer = '';
352
- }
353
- if (match(parseInfo, '=')) {
354
- yield pushToken(value + next(parseInfo), parseInfo, EnumToken.GteTokenType);
640
+ case 42 /* TokenMap.STAR */:
641
+ if (buffer.length > 0) {
642
+ result.push(yieldResult(buffer, parseInfo));
643
+ buffer = "";
355
644
  }
356
- else {
357
- yield pushToken(value, parseInfo, EnumToken.GtTokenType);
645
+ if (nextCharCode == 61 /* TokenMap.EQUALS */) {
646
+ result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.ContainMatchTokenType));
647
+ break;
358
648
  }
359
- consumeWhiteSpace(parseInfo);
649
+ result.push(yieldResult(value, parseInfo, EnumToken.Star));
650
+ buffer = "";
360
651
  break;
361
- case 46 /* TokenMap.DOT */:
362
- const codepoint = peek(parseInfo).charCodeAt(0);
363
- if (!isDigit(codepoint) && buffer !== '') {
364
- yield pushToken(buffer, parseInfo);
365
- buffer = value;
366
- break;
652
+ case 38 /* TokenMap.AMPERSAND */:
653
+ if (buffer.length > 0) {
654
+ result.push(yieldResult(buffer, parseInfo));
655
+ buffer = "";
367
656
  }
368
- buffer += value;
657
+ result.push(yieldResult(value, parseInfo, EnumToken.NestingSelectorTokenType));
658
+ buffer = "";
369
659
  break;
370
- case 58 /* TokenMap.COLON */:
371
- case 43 /* TokenMap.PLUS */:
372
- case 42 /* TokenMap.STAR */:
373
- case 44 /* TokenMap.COMMA */:
374
- case 61 /* TokenMap.EQUAL */:
375
- if (buffer.length > 0 && buffer != ':') {
376
- yield pushToken(buffer, parseInfo);
377
- buffer = '';
378
- }
379
- const val = peek(parseInfo);
380
- if (val == '=') {
381
- next(parseInfo);
382
- yield pushToken(value + val, parseInfo, EnumToken.ContainMatchTokenType);
660
+ case 124 /* TokenMap.PIPE */:
661
+ if (buffer.length > 0) {
662
+ result.push(yieldResult(buffer, parseInfo));
663
+ buffer = "";
664
+ }
665
+ // '||'
666
+ if (nextCharCode == 124 /* TokenMap.PIPE */) {
667
+ result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.ColumnCombinatorTokenType));
383
668
  break;
384
669
  }
385
- if (value == ':') {
386
- if (isWhiteSpace(val.codePointAt(0))) {
387
- yield pushToken(value, parseInfo, EnumToken.ColonTokenType);
388
- buffer = '';
389
- break;
390
- }
391
- buffer += value;
670
+ else if (nextCharCode == 61 /* TokenMap.EQUALS */) {
671
+ result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.DashMatchTokenType));
392
672
  break;
393
673
  }
394
- yield pushToken(value, parseInfo);
395
- buffer = '';
396
- if (['+', '*', '/'].includes(value) && isWhiteSpace(peek(parseInfo).charCodeAt(0))) {
397
- yield pushToken(next(parseInfo), parseInfo);
674
+ result.push(yieldResult(value, parseInfo, EnumToken.Pipe));
675
+ buffer = "";
676
+ break;
677
+ case 33 /* TokenMap.EXCLAMATION */:
678
+ if (buffer.length > 0) {
679
+ result.push(yieldResult(buffer, parseInfo));
680
+ buffer = "";
398
681
  }
399
- while (isWhiteSpace(peek(parseInfo).charCodeAt(0))) {
400
- next(parseInfo);
682
+ if (match(parseInfo, "important")) {
683
+ result.push(yieldResult(value + next(parseInfo, 9), parseInfo, EnumToken.ImportantTokenType));
684
+ buffer = "";
401
685
  }
402
686
  break;
403
- case 41 /* TokenMap.CLOSE_PAREN */:
687
+ case 47 /* TokenMap.SLASH */:
404
688
  if (buffer.length > 0) {
405
- yield pushToken(buffer, parseInfo);
406
- buffer = '';
689
+ result.push(yieldResult(buffer, parseInfo));
690
+ buffer = "";
407
691
  }
408
- yield pushToken(value, parseInfo, EnumToken.EndParensTokenType);
409
- break;
410
- case 40 /* TokenMap.OPEN_PAREN */:
411
- if (buffer.length == 0) {
412
- yield pushToken(value, parseInfo);
692
+ if (!match(parseInfo, "*")) {
693
+ result.push(yieldResult(value, parseInfo, SymbolsMapTokens[value]));
413
694
  break;
414
695
  }
415
- buffer += value;
416
- // @ts-ignore
417
- if (buffer == 'url(') {
418
- yield pushToken(buffer, parseInfo);
419
- buffer = '';
420
- consumeWhiteSpace(parseInfo);
421
- value = peek(parseInfo);
422
- // let cp: number;
423
- let whitespace = '';
424
- if (value == '"' || value == "'") {
425
- const quote = value;
426
- let inquote = true;
427
- let hasNewLine = false;
428
- buffer = next(parseInfo);
429
- while (value = next(parseInfo)) {
430
- charCode = value.charCodeAt(0);
431
- // consume an invalid string
432
- if (inquote) {
433
- buffer += value;
434
- if (isNewLine(charCode)) {
435
- hasNewLine = true;
436
- while (value = next(parseInfo)) {
437
- buffer += value;
438
- if (value == ';') {
439
- inquote = false;
440
- break;
441
- }
442
- }
443
- charCode = value.charCodeAt(0);
444
- }
445
- // '\\'
446
- if (charCode == 0x5c) {
447
- buffer += next(parseInfo);
448
- }
449
- else if (value == quote) {
450
- inquote = false;
451
- }
452
- continue;
453
- }
454
- if (!inquote) {
455
- if (isWhiteSpace(charCode)) {
456
- whitespace += value;
457
- while (value = peek(parseInfo)) {
458
- if (isWhiteSpace(value?.charCodeAt(0))) {
459
- whitespace += next(parseInfo);
460
- continue;
461
- }
462
- break;
463
- }
464
- if (!(value = next(parseInfo))) {
465
- yield pushToken(buffer, parseInfo, hasNewLine ? EnumToken.BadUrlTokenType : EnumToken.UrlTokenTokenType);
466
- buffer = '';
467
- break;
468
- }
469
- }
470
- charCode = value.charCodeAt(0);
471
- // ')'
472
- if (charCode == 0x29) {
473
- yield pushToken(buffer, parseInfo, hasNewLine ? EnumToken.BadStringTokenType : EnumToken.StringTokenType);
474
- yield pushToken(value, parseInfo, EnumToken.EndParensTokenType);
475
- buffer = '';
476
- break;
477
- }
478
- while (value = next(parseInfo)) {
479
- charCode = value.charCodeAt(0);
480
- buffer += value;
481
- }
482
- if (hasNewLine) {
483
- yield pushToken(buffer, parseInfo, EnumToken.BadStringTokenType);
484
- buffer = '';
485
- }
486
- break;
487
- }
488
- buffer += value;
696
+ buffer += value + next(parseInfo);
697
+ while ((value = next(parseInfo))) {
698
+ if (value == "*") {
699
+ buffer += value;
700
+ if (match(parseInfo, "/")) {
701
+ result.push(yieldResult(buffer + next(parseInfo), parseInfo, EnumToken.CommentTokenType));
702
+ buffer = "";
703
+ break;
489
704
  }
490
- break;
491
705
  }
492
706
  else {
493
- buffer = '';
494
- while (value = next(parseInfo)) {
495
- charCode = value.charCodeAt(0);
496
- if (charCode == 0x29) { // ')'
497
- yield pushToken(buffer, parseInfo, EnumToken.UrlTokenTokenType);
498
- yield pushToken(value, parseInfo, EnumToken.EndParensTokenType);
499
- buffer = '';
500
- break;
501
- }
502
- buffer += value;
503
- }
504
- }
505
- if (buffer !== '') {
506
- yield pushToken(buffer, parseInfo, EnumToken.UrlTokenTokenType);
507
- buffer = '';
508
- break;
707
+ buffer += value;
509
708
  }
510
- break;
511
709
  }
512
- yield pushToken(buffer, parseInfo);
513
- buffer = '';
710
+ if (buffer.length > 0) {
711
+ result.push(yieldResult(buffer, parseInfo, EnumToken.BadCommentTokenType));
712
+ buffer = "";
713
+ }
514
714
  break;
515
- case 91 /* TokenMap.OPEN_BRACKET */:
516
- case 93 /* TokenMap.CLOSE_BRACKET */:
517
- case 123 /* TokenMap.OPEN_CURLY_BRACE */:
518
- case 125 /* TokenMap.CLOSE_CURLY_BRACE */:
519
- case 59 /* TokenMap.SEMICOLON */:
715
+ case 62 /* TokenMap.GREATERTHAN */:
520
716
  if (buffer.length > 0) {
521
- yield pushToken(buffer, parseInfo);
522
- buffer = '';
717
+ result.push(yieldResult(buffer, parseInfo));
718
+ buffer = "";
719
+ }
720
+ if (match(parseInfo, "=")) {
721
+ result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.GteTokenType));
722
+ break;
523
723
  }
524
- yield pushToken(value, parseInfo);
724
+ result.push(yieldResult(value, parseInfo, EnumToken.GtTokenType));
725
+ buffer = "";
525
726
  break;
526
- case 33 /* TokenMap.EXCLAMATION */:
727
+ case 60 /* TokenMap.LOWERTHAN */:
527
728
  if (buffer.length > 0) {
528
- yield pushToken(buffer, parseInfo);
529
- buffer = '';
729
+ result.push(yieldResult(buffer, parseInfo));
730
+ buffer = "";
530
731
  }
531
- if (match(parseInfo, 'important')) {
532
- yield pushToken(value + next(parseInfo, 9), parseInfo, EnumToken.ImportantTokenType);
533
- buffer = '';
732
+ if (match(parseInfo, "=")) {
733
+ result.push(yieldResult(value + next(parseInfo), parseInfo, EnumToken.LteTokenType));
534
734
  break;
535
735
  }
536
- buffer = '!';
537
- break;
538
- case 64 /* TokenMap.AT */:
539
- buffer = value;
540
- {
541
- let val = peek(parseInfo);
542
- if (val == '-' || isIdentStart(val.charCodeAt(0))) {
543
- buffer = next(parseInfo);
544
- val = peek(parseInfo);
545
- while (isIdent(val) || val == '-') {
546
- buffer += next(parseInfo);
547
- val = peek(parseInfo);
736
+ buffer += value;
737
+ if (match(parseInfo, "!--")) {
738
+ buffer += next(parseInfo, 3);
739
+ while ((value = next(parseInfo))) {
740
+ buffer += value;
741
+ if (value == "-" && match(parseInfo, "->")) {
742
+ break;
548
743
  }
549
- yield pushToken(buffer, parseInfo, EnumToken.AtRuleTokenType);
550
- buffer = '';
551
744
  }
745
+ if (value === "") {
746
+ result.push(yieldResult(buffer, parseInfo, EnumToken.BadCdoTokenType));
747
+ }
748
+ else {
749
+ result.push(yieldResult(buffer + next(parseInfo, 2), parseInfo, EnumToken.CDOCOMMTokenType));
750
+ }
751
+ buffer = "";
752
+ }
753
+ break;
754
+ case 35 /* TokenMap.HASH */:
755
+ if (buffer.length > 0) {
756
+ result.push(yieldResult(buffer, parseInfo));
757
+ buffer = "";
758
+ }
759
+ buffer += value;
760
+ break;
761
+ case 92 /* TokenMap.REVERSE_SOLIDUS */:
762
+ // EOF
763
+ if (!(value = next(parseInfo))) {
764
+ // end of stream ignore \\
765
+ if (buffer.length > 0) {
766
+ result.push(yieldResult(buffer, parseInfo));
767
+ buffer = "";
768
+ }
769
+ break;
770
+ }
771
+ buffer +=
772
+ (parseInfo.offset == parseInfo.currentPosition.ind
773
+ ? parseInfo.buffer.slice(-1)
774
+ : parseInfo.stream.charAt(parseInfo.currentPosition.ind - parseInfo.offset - 1)) + value;
775
+ break;
776
+ case 39 /* TokenMap.SINGLE_QUOTE */:
777
+ case 34 /* TokenMap.DOUBLE_QUOTE */:
778
+ result.push(...consumeString(value, buffer, parseInfo));
779
+ buffer = "";
780
+ break;
781
+ case 46 /* TokenMap.DOT */:
782
+ const codepoint = parseInfo.stream
783
+ .charAt(parseInfo.currentPosition.ind - parseInfo.offset + 1)
784
+ .charCodeAt(0);
785
+ if (!isDigit(codepoint) && buffer !== "") {
786
+ result.push(yieldResult(buffer, parseInfo));
787
+ buffer = value;
788
+ break;
552
789
  }
790
+ buffer += value;
553
791
  break;
554
792
  default:
555
793
  buffer += value;
@@ -558,27 +796,31 @@ function* tokenize(parseInfo, yieldEOFToken = true) {
558
796
  }
559
797
  if (yieldEOFToken) {
560
798
  if (buffer.length > 0) {
561
- yield pushToken(buffer, parseInfo);
799
+ result.push(yieldResult(buffer, parseInfo));
562
800
  }
563
- yield pushToken('', parseInfo, EnumToken.EOFTokenType);
801
+ result.push(yieldResult("", parseInfo, EnumToken.EOFTokenType));
564
802
  }
565
803
  else {
566
804
  parseInfo.buffer = buffer;
567
805
  }
806
+ parseInfo.time += performance.now() - startTime;
807
+ return result;
568
808
  }
569
809
  /**
570
810
  * tokenize readable stream
571
811
  * @param input
572
812
  */
573
- async function* tokenizeStream(input) {
574
- const parseInfo = {
575
- stream: '',
576
- buffer: '',
813
+ async function* tokenizeStream(input, parseInfo) {
814
+ parseInfo ??= {
815
+ stream: "",
816
+ buffer: "",
817
+ src: "",
577
818
  offset: 0,
578
- position: { ind: 0, lin: 1, col: 1 },
579
- currentPosition: { ind: -1, lin: 1, col: 0 }
819
+ time: 0,
820
+ position: { ind: 0, lin: 1, col: 0 },
821
+ currentPosition: { ind: -1, lin: 1, col: 0 },
580
822
  };
581
- const decoder = new TextDecoder('utf-8');
823
+ const decoder = new TextDecoder("utf-8");
582
824
  const reader = input.getReader();
583
825
  while (true) {
584
826
  const { done, value } = await reader.read();
@@ -599,5 +841,22 @@ async function* tokenizeStream(input) {
599
841
  }
600
842
  }
601
843
  }
844
+ /**
845
+ * Update position
846
+ * @param position
847
+ * @param str
848
+ */
849
+ function move(position, str) {
850
+ let i = 0;
851
+ for (; i < str.length; i++) {
852
+ if (isNewLine(str[i].charCodeAt(0))) {
853
+ position.lin++;
854
+ position.col = 0;
855
+ }
856
+ else {
857
+ position.col++;
858
+ }
859
+ }
860
+ }
602
861
 
603
- export { tokenize, tokenizeStream };
862
+ export { SymbolsMapTokens, TokenMap, consumeString, getTokenType, hintsEnum, match, move, next, peek, tokenize, tokenizeStream, yieldResult };