@tbela99/css-parser 1.4.2 → 1.4.3

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