@eslint/css-tree 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +194 -0
  3. package/cjs/convertor/create.cjs +32 -0
  4. package/cjs/convertor/index.cjs +8 -0
  5. package/cjs/data-patch.cjs +7 -0
  6. package/cjs/data.cjs +120 -0
  7. package/cjs/definition-syntax/SyntaxError.cjs +16 -0
  8. package/cjs/definition-syntax/generate.cjs +139 -0
  9. package/cjs/definition-syntax/index.cjs +13 -0
  10. package/cjs/definition-syntax/parse.cjs +556 -0
  11. package/cjs/definition-syntax/scanner.cjs +113 -0
  12. package/cjs/definition-syntax/walk.cjs +57 -0
  13. package/cjs/generator/create.cjs +102 -0
  14. package/cjs/generator/index.cjs +8 -0
  15. package/cjs/generator/sourceMap.cjs +96 -0
  16. package/cjs/generator/token-before.cjs +170 -0
  17. package/cjs/index.cjs +65 -0
  18. package/cjs/lexer/Lexer.cjs +517 -0
  19. package/cjs/lexer/error.cjs +128 -0
  20. package/cjs/lexer/generic-an-plus-b.cjs +235 -0
  21. package/cjs/lexer/generic-const.cjs +12 -0
  22. package/cjs/lexer/generic-urange.cjs +149 -0
  23. package/cjs/lexer/generic.cjs +589 -0
  24. package/cjs/lexer/index.cjs +7 -0
  25. package/cjs/lexer/match-graph.cjs +530 -0
  26. package/cjs/lexer/match.cjs +632 -0
  27. package/cjs/lexer/prepare-tokens.cjs +54 -0
  28. package/cjs/lexer/search.cjs +65 -0
  29. package/cjs/lexer/structure.cjs +173 -0
  30. package/cjs/lexer/trace.cjs +73 -0
  31. package/cjs/lexer/units.cjs +38 -0
  32. package/cjs/parser/SyntaxError.cjs +74 -0
  33. package/cjs/parser/create.cjs +378 -0
  34. package/cjs/parser/index.cjs +8 -0
  35. package/cjs/parser/parse-selector.cjs +8 -0
  36. package/cjs/parser/sequence.cjs +47 -0
  37. package/cjs/syntax/atrule/container.cjs +32 -0
  38. package/cjs/syntax/atrule/font-face.cjs +12 -0
  39. package/cjs/syntax/atrule/import.cjs +101 -0
  40. package/cjs/syntax/atrule/index.cjs +27 -0
  41. package/cjs/syntax/atrule/layer.cjs +16 -0
  42. package/cjs/syntax/atrule/media.cjs +16 -0
  43. package/cjs/syntax/atrule/nest.cjs +16 -0
  44. package/cjs/syntax/atrule/page.cjs +16 -0
  45. package/cjs/syntax/atrule/scope.cjs +16 -0
  46. package/cjs/syntax/atrule/starting-style.cjs +12 -0
  47. package/cjs/syntax/atrule/supports.cjs +16 -0
  48. package/cjs/syntax/config/generator.cjs +9 -0
  49. package/cjs/syntax/config/lexer.cjs +14 -0
  50. package/cjs/syntax/config/mix.cjs +126 -0
  51. package/cjs/syntax/config/parser-selector.cjs +19 -0
  52. package/cjs/syntax/config/parser.cjs +49 -0
  53. package/cjs/syntax/config/walker.cjs +9 -0
  54. package/cjs/syntax/create.cjs +58 -0
  55. package/cjs/syntax/function/expression.cjs +11 -0
  56. package/cjs/syntax/function/var.cjs +43 -0
  57. package/cjs/syntax/index.cjs +14 -0
  58. package/cjs/syntax/node/AnPlusB.cjs +293 -0
  59. package/cjs/syntax/node/Atrule.cjs +103 -0
  60. package/cjs/syntax/node/AtrulePrelude.cjs +52 -0
  61. package/cjs/syntax/node/AttributeSelector.cjs +148 -0
  62. package/cjs/syntax/node/Block.cjs +96 -0
  63. package/cjs/syntax/node/Brackets.cjs +38 -0
  64. package/cjs/syntax/node/CDC.cjs +26 -0
  65. package/cjs/syntax/node/CDO.cjs +26 -0
  66. package/cjs/syntax/node/ClassSelector.cjs +31 -0
  67. package/cjs/syntax/node/Combinator.cjs +61 -0
  68. package/cjs/syntax/node/Comment.cjs +40 -0
  69. package/cjs/syntax/node/Condition.cjs +120 -0
  70. package/cjs/syntax/node/Declaration.cjs +166 -0
  71. package/cjs/syntax/node/DeclarationList.cjs +62 -0
  72. package/cjs/syntax/node/Dimension.cjs +30 -0
  73. package/cjs/syntax/node/Feature.cjs +101 -0
  74. package/cjs/syntax/node/FeatureFunction.cjs +67 -0
  75. package/cjs/syntax/node/FeatureRange.cjs +133 -0
  76. package/cjs/syntax/node/Function.cjs +45 -0
  77. package/cjs/syntax/node/GeneralEnclosed.cjs +68 -0
  78. package/cjs/syntax/node/Hash.cjs +30 -0
  79. package/cjs/syntax/node/IdSelector.cjs +33 -0
  80. package/cjs/syntax/node/Identifier.cjs +25 -0
  81. package/cjs/syntax/node/Layer.cjs +35 -0
  82. package/cjs/syntax/node/LayerList.cjs +43 -0
  83. package/cjs/syntax/node/MediaQuery.cjs +100 -0
  84. package/cjs/syntax/node/MediaQueryList.cjs +41 -0
  85. package/cjs/syntax/node/NestingSelector.cjs +29 -0
  86. package/cjs/syntax/node/Nth.cjs +54 -0
  87. package/cjs/syntax/node/Number.cjs +25 -0
  88. package/cjs/syntax/node/Operator.cjs +28 -0
  89. package/cjs/syntax/node/Parentheses.cjs +38 -0
  90. package/cjs/syntax/node/Percentage.cjs +25 -0
  91. package/cjs/syntax/node/PseudoClassSelector.cjs +67 -0
  92. package/cjs/syntax/node/PseudoElementSelector.cjs +69 -0
  93. package/cjs/syntax/node/Ratio.cjs +71 -0
  94. package/cjs/syntax/node/Raw.cjs +48 -0
  95. package/cjs/syntax/node/Rule.cjs +58 -0
  96. package/cjs/syntax/node/Scope.cjs +69 -0
  97. package/cjs/syntax/node/Selector.cjs +38 -0
  98. package/cjs/syntax/node/SelectorList.cjs +43 -0
  99. package/cjs/syntax/node/String.cjs +26 -0
  100. package/cjs/syntax/node/StyleSheet.cjs +83 -0
  101. package/cjs/syntax/node/SupportsDeclaration.cjs +38 -0
  102. package/cjs/syntax/node/TypeSelector.cjs +59 -0
  103. package/cjs/syntax/node/UnicodeRange.cjs +158 -0
  104. package/cjs/syntax/node/Url.cjs +54 -0
  105. package/cjs/syntax/node/Value.cjs +26 -0
  106. package/cjs/syntax/node/WhiteSpace.cjs +34 -0
  107. package/cjs/syntax/node/index-generate.cjs +103 -0
  108. package/cjs/syntax/node/index-parse-selector.cjs +39 -0
  109. package/cjs/syntax/node/index-parse.cjs +103 -0
  110. package/cjs/syntax/node/index.cjs +103 -0
  111. package/cjs/syntax/pseudo/index.cjs +60 -0
  112. package/cjs/syntax/pseudo/lang.cjs +37 -0
  113. package/cjs/syntax/scope/atrulePrelude.cjs +9 -0
  114. package/cjs/syntax/scope/default.cjs +76 -0
  115. package/cjs/syntax/scope/index.cjs +11 -0
  116. package/cjs/syntax/scope/selector.cjs +88 -0
  117. package/cjs/syntax/scope/value.cjs +29 -0
  118. package/cjs/tokenizer/OffsetToLocation.cjs +91 -0
  119. package/cjs/tokenizer/TokenStream.cjs +358 -0
  120. package/cjs/tokenizer/adopt-buffer.cjs +13 -0
  121. package/cjs/tokenizer/char-code-definitions.cjs +236 -0
  122. package/cjs/tokenizer/index.cjs +554 -0
  123. package/cjs/tokenizer/names.cjs +32 -0
  124. package/cjs/tokenizer/types.cjs +57 -0
  125. package/cjs/tokenizer/utils.cjs +261 -0
  126. package/cjs/utils/List.cjs +473 -0
  127. package/cjs/utils/clone.cjs +25 -0
  128. package/cjs/utils/create-custom-error.cjs +18 -0
  129. package/cjs/utils/ident.cjs +102 -0
  130. package/cjs/utils/index.cjs +20 -0
  131. package/cjs/utils/names.cjs +113 -0
  132. package/cjs/utils/string.cjs +99 -0
  133. package/cjs/utils/url.cjs +108 -0
  134. package/cjs/version.cjs +5 -0
  135. package/cjs/walker/create.cjs +291 -0
  136. package/cjs/walker/index.cjs +8 -0
  137. package/data/patch.json +874 -0
  138. package/dist/csstree.esm.js +12 -0
  139. package/dist/csstree.js +12 -0
  140. package/dist/data.cjs +1341 -0
  141. package/dist/data.js +1341 -0
  142. package/dist/version.cjs +1 -0
  143. package/dist/version.js +1 -0
  144. package/lib/convertor/create.js +28 -0
  145. package/lib/convertor/index.js +4 -0
  146. package/lib/data-patch.js +6 -0
  147. package/lib/data.js +118 -0
  148. package/lib/definition-syntax/SyntaxError.js +12 -0
  149. package/lib/definition-syntax/generate.js +135 -0
  150. package/lib/definition-syntax/index.js +4 -0
  151. package/lib/definition-syntax/parse.js +552 -0
  152. package/lib/definition-syntax/scanner.js +109 -0
  153. package/lib/definition-syntax/walk.js +53 -0
  154. package/lib/generator/create.js +97 -0
  155. package/lib/generator/index.js +4 -0
  156. package/lib/generator/sourceMap.js +92 -0
  157. package/lib/generator/token-before.js +182 -0
  158. package/lib/index.d.ts +3016 -0
  159. package/lib/index.js +30 -0
  160. package/lib/lexer/Lexer.js +511 -0
  161. package/lib/lexer/error.js +123 -0
  162. package/lib/lexer/generic-an-plus-b.js +238 -0
  163. package/lib/lexer/generic-const.js +8 -0
  164. package/lib/lexer/generic-urange.js +151 -0
  165. package/lib/lexer/generic.js +622 -0
  166. package/lib/lexer/index.js +1 -0
  167. package/lib/lexer/match-graph.js +527 -0
  168. package/lib/lexer/match.js +630 -0
  169. package/lib/lexer/prepare-tokens.js +50 -0
  170. package/lib/lexer/search.js +61 -0
  171. package/lib/lexer/structure.js +169 -0
  172. package/lib/lexer/trace.js +66 -0
  173. package/lib/lexer/units.js +27 -0
  174. package/lib/parser/SyntaxError.js +70 -0
  175. package/lib/parser/create.js +388 -0
  176. package/lib/parser/index.js +4 -0
  177. package/lib/parser/parse-selector.js +4 -0
  178. package/lib/parser/sequence.js +43 -0
  179. package/lib/syntax/atrule/container.js +28 -0
  180. package/lib/syntax/atrule/font-face.js +8 -0
  181. package/lib/syntax/atrule/import.js +104 -0
  182. package/lib/syntax/atrule/index.js +23 -0
  183. package/lib/syntax/atrule/layer.js +12 -0
  184. package/lib/syntax/atrule/media.js +12 -0
  185. package/lib/syntax/atrule/nest.js +12 -0
  186. package/lib/syntax/atrule/page.js +12 -0
  187. package/lib/syntax/atrule/scope.js +12 -0
  188. package/lib/syntax/atrule/starting-style.js +8 -0
  189. package/lib/syntax/atrule/supports.js +12 -0
  190. package/lib/syntax/config/generator.js +5 -0
  191. package/lib/syntax/config/lexer.js +10 -0
  192. package/lib/syntax/config/mix.js +122 -0
  193. package/lib/syntax/config/parser-selector.js +15 -0
  194. package/lib/syntax/config/parser.js +45 -0
  195. package/lib/syntax/config/walker.js +5 -0
  196. package/lib/syntax/create.js +55 -0
  197. package/lib/syntax/function/expression.js +7 -0
  198. package/lib/syntax/function/var.js +39 -0
  199. package/lib/syntax/index.js +10 -0
  200. package/lib/syntax/node/AnPlusB.js +292 -0
  201. package/lib/syntax/node/Atrule.js +100 -0
  202. package/lib/syntax/node/AtrulePrelude.js +47 -0
  203. package/lib/syntax/node/AttributeSelector.js +147 -0
  204. package/lib/syntax/node/Block.js +95 -0
  205. package/lib/syntax/node/Brackets.js +35 -0
  206. package/lib/syntax/node/CDC.js +19 -0
  207. package/lib/syntax/node/CDO.js +19 -0
  208. package/lib/syntax/node/ClassSelector.js +24 -0
  209. package/lib/syntax/node/Combinator.js +54 -0
  210. package/lib/syntax/node/Comment.js +33 -0
  211. package/lib/syntax/node/Condition.js +123 -0
  212. package/lib/syntax/node/Declaration.js +165 -0
  213. package/lib/syntax/node/DeclarationList.js +62 -0
  214. package/lib/syntax/node/Dimension.js +23 -0
  215. package/lib/syntax/node/Feature.js +103 -0
  216. package/lib/syntax/node/FeatureFunction.js +63 -0
  217. package/lib/syntax/node/FeatureRange.js +133 -0
  218. package/lib/syntax/node/Function.js +41 -0
  219. package/lib/syntax/node/GeneralEnclosed.js +66 -0
  220. package/lib/syntax/node/Hash.js +23 -0
  221. package/lib/syntax/node/IdSelector.js +26 -0
  222. package/lib/syntax/node/Identifier.js +18 -0
  223. package/lib/syntax/node/Layer.js +28 -0
  224. package/lib/syntax/node/LayerList.js +36 -0
  225. package/lib/syntax/node/MediaQuery.js +102 -0
  226. package/lib/syntax/node/MediaQueryList.js +34 -0
  227. package/lib/syntax/node/NestingSelector.js +22 -0
  228. package/lib/syntax/node/Nth.js +47 -0
  229. package/lib/syntax/node/Number.js +18 -0
  230. package/lib/syntax/node/Operator.js +21 -0
  231. package/lib/syntax/node/Parentheses.js +34 -0
  232. package/lib/syntax/node/Percentage.js +18 -0
  233. package/lib/syntax/node/PseudoClassSelector.js +65 -0
  234. package/lib/syntax/node/PseudoElementSelector.js +66 -0
  235. package/lib/syntax/node/Ratio.js +68 -0
  236. package/lib/syntax/node/Raw.js +41 -0
  237. package/lib/syntax/node/Rule.js +51 -0
  238. package/lib/syntax/node/Scope.js +66 -0
  239. package/lib/syntax/node/Selector.js +31 -0
  240. package/lib/syntax/node/SelectorList.js +35 -0
  241. package/lib/syntax/node/String.js +19 -0
  242. package/lib/syntax/node/StyleSheet.js +82 -0
  243. package/lib/syntax/node/SupportsDeclaration.js +34 -0
  244. package/lib/syntax/node/TypeSelector.js +52 -0
  245. package/lib/syntax/node/UnicodeRange.js +156 -0
  246. package/lib/syntax/node/Url.js +52 -0
  247. package/lib/syntax/node/Value.js +19 -0
  248. package/lib/syntax/node/WhiteSpace.js +27 -0
  249. package/lib/syntax/node/index-generate.js +49 -0
  250. package/lib/syntax/node/index-parse-selector.js +17 -0
  251. package/lib/syntax/node/index-parse.js +49 -0
  252. package/lib/syntax/node/index.js +49 -0
  253. package/lib/syntax/pseudo/index.js +56 -0
  254. package/lib/syntax/pseudo/lang.js +33 -0
  255. package/lib/syntax/scope/atrulePrelude.js +5 -0
  256. package/lib/syntax/scope/default.js +85 -0
  257. package/lib/syntax/scope/index.js +3 -0
  258. package/lib/syntax/scope/selector.js +94 -0
  259. package/lib/syntax/scope/value.js +25 -0
  260. package/lib/tokenizer/OffsetToLocation.js +87 -0
  261. package/lib/tokenizer/TokenStream.js +366 -0
  262. package/lib/tokenizer/adopt-buffer.js +9 -0
  263. package/lib/tokenizer/char-code-definitions.js +212 -0
  264. package/lib/tokenizer/index.js +513 -0
  265. package/lib/tokenizer/names.js +28 -0
  266. package/lib/tokenizer/types.js +28 -0
  267. package/lib/tokenizer/utils.js +254 -0
  268. package/lib/utils/List.js +469 -0
  269. package/lib/utils/clone.js +21 -0
  270. package/lib/utils/create-custom-error.js +14 -0
  271. package/lib/utils/ident.js +101 -0
  272. package/lib/utils/index.js +6 -0
  273. package/lib/utils/names.js +106 -0
  274. package/lib/utils/string.js +99 -0
  275. package/lib/utils/url.js +108 -0
  276. package/lib/version.js +5 -0
  277. package/lib/walker/create.js +287 -0
  278. package/lib/walker/index.js +4 -0
  279. package/package.json +133 -0
@@ -0,0 +1,552 @@
1
+ import { Scanner } from './scanner.js';
2
+
3
+ const TAB = 9;
4
+ const N = 10;
5
+ const F = 12;
6
+ const R = 13;
7
+ const SPACE = 32;
8
+ const EXCLAMATIONMARK = 33; // !
9
+ const NUMBERSIGN = 35; // #
10
+ const AMPERSAND = 38; // &
11
+ const APOSTROPHE = 39; // '
12
+ const LEFTPARENTHESIS = 40; // (
13
+ const RIGHTPARENTHESIS = 41; // )
14
+ const ASTERISK = 42; // *
15
+ const PLUSSIGN = 43; // +
16
+ const COMMA = 44; // ,
17
+ const HYPERMINUS = 45; // -
18
+ const LESSTHANSIGN = 60; // <
19
+ const GREATERTHANSIGN = 62; // >
20
+ const QUESTIONMARK = 63; // ?
21
+ const COMMERCIALAT = 64; // @
22
+ const LEFTSQUAREBRACKET = 91; // [
23
+ const RIGHTSQUAREBRACKET = 93; // ]
24
+ const LEFTCURLYBRACKET = 123; // {
25
+ const VERTICALLINE = 124; // |
26
+ const RIGHTCURLYBRACKET = 125; // }
27
+ const INFINITY = 8734; // ∞
28
+ const COMBINATOR_PRECEDENCE = {
29
+ ' ': 1,
30
+ '&&': 2,
31
+ '||': 3,
32
+ '|': 4
33
+ };
34
+
35
+ function readMultiplierRange(scanner) {
36
+ let min = null;
37
+ let max = null;
38
+
39
+ scanner.eat(LEFTCURLYBRACKET);
40
+ scanner.skipWs();
41
+
42
+ min = scanner.scanNumber(scanner);
43
+ scanner.skipWs();
44
+
45
+ if (scanner.charCode() === COMMA) {
46
+ scanner.pos++;
47
+ scanner.skipWs();
48
+
49
+ if (scanner.charCode() !== RIGHTCURLYBRACKET) {
50
+ max = scanner.scanNumber(scanner);
51
+ scanner.skipWs();
52
+ }
53
+ } else {
54
+ max = min;
55
+ }
56
+
57
+ scanner.eat(RIGHTCURLYBRACKET);
58
+
59
+ return {
60
+ min: Number(min),
61
+ max: max ? Number(max) : 0
62
+ };
63
+ }
64
+
65
+ function readMultiplier(scanner) {
66
+ let range = null;
67
+ let comma = false;
68
+
69
+ switch (scanner.charCode()) {
70
+ case ASTERISK:
71
+ scanner.pos++;
72
+
73
+ range = {
74
+ min: 0,
75
+ max: 0
76
+ };
77
+
78
+ break;
79
+
80
+ case PLUSSIGN:
81
+ scanner.pos++;
82
+
83
+ range = {
84
+ min: 1,
85
+ max: 0
86
+ };
87
+
88
+ break;
89
+
90
+ case QUESTIONMARK:
91
+ scanner.pos++;
92
+
93
+ range = {
94
+ min: 0,
95
+ max: 1
96
+ };
97
+
98
+ break;
99
+
100
+ case NUMBERSIGN:
101
+ scanner.pos++;
102
+
103
+ comma = true;
104
+
105
+ if (scanner.charCode() === LEFTCURLYBRACKET) {
106
+ range = readMultiplierRange(scanner);
107
+ } else if (scanner.charCode() === QUESTIONMARK) {
108
+ // https://www.w3.org/TR/css-values-4/#component-multipliers
109
+ // > the # and ? multipliers may be stacked as #?
110
+ // In this case just treat "#?" as a single multiplier
111
+ // { min: 0, max: 0, comma: true }
112
+ scanner.pos++;
113
+ range = {
114
+ min: 0,
115
+ max: 0
116
+ };
117
+ } else {
118
+ range = {
119
+ min: 1,
120
+ max: 0
121
+ };
122
+ }
123
+
124
+ break;
125
+
126
+ case LEFTCURLYBRACKET:
127
+ range = readMultiplierRange(scanner);
128
+ break;
129
+
130
+ default:
131
+ return null;
132
+ }
133
+
134
+ return {
135
+ type: 'Multiplier',
136
+ comma,
137
+ min: range.min,
138
+ max: range.max,
139
+ term: null
140
+ };
141
+ }
142
+
143
+ function maybeMultiplied(scanner, node) {
144
+ const multiplier = readMultiplier(scanner);
145
+
146
+ if (multiplier !== null) {
147
+ multiplier.term = node;
148
+
149
+ // https://www.w3.org/TR/css-values-4/#component-multipliers
150
+ // > The + and # multipliers may be stacked as +#;
151
+ // Represent "+#" as nested multipliers:
152
+ // { ...<multiplier #>,
153
+ // term: {
154
+ // ...<multipler +>,
155
+ // term: node
156
+ // }
157
+ // }
158
+ if (scanner.charCode() === NUMBERSIGN &&
159
+ scanner.charCodeAt(scanner.pos - 1) === PLUSSIGN) {
160
+ return maybeMultiplied(scanner, multiplier);
161
+ }
162
+
163
+ return multiplier;
164
+ }
165
+
166
+ return node;
167
+ }
168
+
169
+ function maybeToken(scanner) {
170
+ const ch = scanner.peek();
171
+
172
+ if (ch === '') {
173
+ return null;
174
+ }
175
+
176
+ return maybeMultiplied(scanner, {
177
+ type: 'Token',
178
+ value: ch
179
+ });
180
+ }
181
+
182
+ function readProperty(scanner) {
183
+ let name;
184
+
185
+ scanner.eat(LESSTHANSIGN);
186
+ scanner.eat(APOSTROPHE);
187
+
188
+ name = scanner.scanWord();
189
+
190
+ scanner.eat(APOSTROPHE);
191
+ scanner.eat(GREATERTHANSIGN);
192
+
193
+ return maybeMultiplied(scanner, {
194
+ type: 'Property',
195
+ name
196
+ });
197
+ }
198
+
199
+ // https://drafts.csswg.org/css-values-3/#numeric-ranges
200
+ // 4.1. Range Restrictions and Range Definition Notation
201
+ //
202
+ // Range restrictions can be annotated in the numeric type notation using CSS bracketed
203
+ // range notation—[min,max]—within the angle brackets, after the identifying keyword,
204
+ // indicating a closed range between (and including) min and max.
205
+ // For example, <integer [0, 10]> indicates an integer between 0 and 10, inclusive.
206
+ function readTypeRange(scanner) {
207
+ // use null for Infinity to make AST format JSON serializable/deserializable
208
+ let min = null; // -Infinity
209
+ let max = null; // Infinity
210
+ let sign = 1;
211
+
212
+ scanner.eat(LEFTSQUAREBRACKET);
213
+
214
+ if (scanner.charCode() === HYPERMINUS) {
215
+ scanner.peek();
216
+ sign = -1;
217
+ }
218
+
219
+ if (sign == -1 && scanner.charCode() === INFINITY) {
220
+ scanner.peek();
221
+ } else {
222
+ min = sign * Number(scanner.scanNumber(scanner));
223
+
224
+ if (scanner.isNameCharCode()) {
225
+ min += scanner.scanWord();
226
+ }
227
+ }
228
+
229
+ scanner.skipWs();
230
+ scanner.eat(COMMA);
231
+ scanner.skipWs();
232
+
233
+ if (scanner.charCode() === INFINITY) {
234
+ scanner.peek();
235
+ } else {
236
+ sign = 1;
237
+
238
+ if (scanner.charCode() === HYPERMINUS) {
239
+ scanner.peek();
240
+ sign = -1;
241
+ }
242
+
243
+ max = sign * Number(scanner.scanNumber(scanner));
244
+
245
+ if (scanner.isNameCharCode()) {
246
+ max += scanner.scanWord();
247
+ }
248
+ }
249
+
250
+ scanner.eat(RIGHTSQUAREBRACKET);
251
+
252
+ return {
253
+ type: 'Range',
254
+ min,
255
+ max
256
+ };
257
+ }
258
+
259
+ function readType(scanner) {
260
+ let name;
261
+ let opts = null;
262
+
263
+ scanner.eat(LESSTHANSIGN);
264
+ name = scanner.scanWord();
265
+
266
+ // https://drafts.csswg.org/css-values-5/#boolean
267
+ if (name === 'boolean-expr') {
268
+ scanner.eat(LEFTSQUAREBRACKET);
269
+
270
+ const implicitGroup = readImplicitGroup(scanner, RIGHTSQUAREBRACKET);
271
+
272
+ scanner.eat(RIGHTSQUAREBRACKET);
273
+ scanner.eat(GREATERTHANSIGN);
274
+
275
+ return maybeMultiplied(scanner, {
276
+ type: 'Boolean',
277
+ term: implicitGroup.terms.length === 1
278
+ ? implicitGroup.terms[0]
279
+ : implicitGroup
280
+ });
281
+ }
282
+
283
+ if (scanner.charCode() === LEFTPARENTHESIS &&
284
+ scanner.nextCharCode() === RIGHTPARENTHESIS) {
285
+ scanner.pos += 2;
286
+ name += '()';
287
+ }
288
+
289
+ if (scanner.charCodeAt(scanner.findWsEnd(scanner.pos)) === LEFTSQUAREBRACKET) {
290
+ scanner.skipWs();
291
+ opts = readTypeRange(scanner);
292
+ }
293
+
294
+ scanner.eat(GREATERTHANSIGN);
295
+
296
+ return maybeMultiplied(scanner, {
297
+ type: 'Type',
298
+ name,
299
+ opts
300
+ });
301
+ }
302
+
303
+ function readKeywordOrFunction(scanner) {
304
+ const name = scanner.scanWord();
305
+
306
+ if (scanner.charCode() === LEFTPARENTHESIS) {
307
+ scanner.pos++;
308
+
309
+ return {
310
+ type: 'Function',
311
+ name
312
+ };
313
+ }
314
+
315
+ return maybeMultiplied(scanner, {
316
+ type: 'Keyword',
317
+ name
318
+ });
319
+ }
320
+
321
+ function regroupTerms(terms, combinators) {
322
+ function createGroup(terms, combinator) {
323
+ return {
324
+ type: 'Group',
325
+ terms,
326
+ combinator,
327
+ disallowEmpty: false,
328
+ explicit: false
329
+ };
330
+ }
331
+
332
+ let combinator;
333
+
334
+ combinators = Object.keys(combinators)
335
+ .sort((a, b) => COMBINATOR_PRECEDENCE[a] - COMBINATOR_PRECEDENCE[b]);
336
+
337
+ while (combinators.length > 0) {
338
+ combinator = combinators.shift();
339
+
340
+ let i = 0;
341
+ let subgroupStart = 0;
342
+
343
+ for (; i < terms.length; i++) {
344
+ const term = terms[i];
345
+
346
+ if (term.type === 'Combinator') {
347
+ if (term.value === combinator) {
348
+ if (subgroupStart === -1) {
349
+ subgroupStart = i - 1;
350
+ }
351
+ terms.splice(i, 1);
352
+ i--;
353
+ } else {
354
+ if (subgroupStart !== -1 && i - subgroupStart > 1) {
355
+ terms.splice(
356
+ subgroupStart,
357
+ i - subgroupStart,
358
+ createGroup(terms.slice(subgroupStart, i), combinator)
359
+ );
360
+ i = subgroupStart + 1;
361
+ }
362
+ subgroupStart = -1;
363
+ }
364
+ }
365
+ }
366
+
367
+ if (subgroupStart !== -1 && combinators.length) {
368
+ terms.splice(
369
+ subgroupStart,
370
+ i - subgroupStart,
371
+ createGroup(terms.slice(subgroupStart, i), combinator)
372
+ );
373
+ }
374
+ }
375
+
376
+ return combinator;
377
+ }
378
+
379
+ function readImplicitGroup(scanner, stopCharCode) {
380
+ const combinators = Object.create(null);
381
+ const terms = [];
382
+ let token;
383
+ let prevToken = null;
384
+ let prevTokenPos = scanner.pos;
385
+
386
+ while (scanner.charCode() !== stopCharCode && (token = peek(scanner, stopCharCode))) {
387
+ if (token.type !== 'Spaces') {
388
+ if (token.type === 'Combinator') {
389
+ // check for combinator in group beginning and double combinator sequence
390
+ if (prevToken === null || prevToken.type === 'Combinator') {
391
+ scanner.pos = prevTokenPos;
392
+ scanner.error('Unexpected combinator');
393
+ }
394
+
395
+ combinators[token.value] = true;
396
+ } else if (prevToken !== null && prevToken.type !== 'Combinator') {
397
+ combinators[' '] = true; // a b
398
+ terms.push({
399
+ type: 'Combinator',
400
+ value: ' '
401
+ });
402
+ }
403
+
404
+ terms.push(token);
405
+ prevToken = token;
406
+ prevTokenPos = scanner.pos;
407
+ }
408
+ }
409
+
410
+ // check for combinator in group ending
411
+ if (prevToken !== null && prevToken.type === 'Combinator') {
412
+ scanner.pos -= prevTokenPos;
413
+ scanner.error('Unexpected combinator');
414
+ }
415
+
416
+ return {
417
+ type: 'Group',
418
+ terms,
419
+ combinator: regroupTerms(terms, combinators) || ' ',
420
+ disallowEmpty: false,
421
+ explicit: false
422
+ };
423
+ }
424
+
425
+ function readGroup(scanner, stopCharCode) {
426
+ let result;
427
+
428
+ scanner.eat(LEFTSQUAREBRACKET);
429
+ result = readImplicitGroup(scanner, stopCharCode);
430
+ scanner.eat(RIGHTSQUAREBRACKET);
431
+
432
+ result.explicit = true;
433
+
434
+ if (scanner.charCode() === EXCLAMATIONMARK) {
435
+ scanner.pos++;
436
+ result.disallowEmpty = true;
437
+ }
438
+
439
+ return result;
440
+ }
441
+
442
+ function peek(scanner, stopCharCode) {
443
+ let code = scanner.charCode();
444
+
445
+ switch (code) {
446
+ case RIGHTSQUAREBRACKET:
447
+ // don't eat, stop scan a group
448
+ break;
449
+
450
+ case LEFTSQUAREBRACKET:
451
+ return maybeMultiplied(scanner, readGroup(scanner, stopCharCode));
452
+
453
+ case LESSTHANSIGN:
454
+ return scanner.nextCharCode() === APOSTROPHE
455
+ ? readProperty(scanner)
456
+ : readType(scanner);
457
+
458
+ case VERTICALLINE:
459
+ return {
460
+ type: 'Combinator',
461
+ value: scanner.substringToPos(
462
+ scanner.pos + (scanner.nextCharCode() === VERTICALLINE ? 2 : 1)
463
+ )
464
+ };
465
+
466
+ case AMPERSAND:
467
+ scanner.pos++;
468
+ scanner.eat(AMPERSAND);
469
+
470
+ return {
471
+ type: 'Combinator',
472
+ value: '&&'
473
+ };
474
+
475
+ case COMMA:
476
+ scanner.pos++;
477
+ return {
478
+ type: 'Comma'
479
+ };
480
+
481
+ case APOSTROPHE:
482
+ return maybeMultiplied(scanner, {
483
+ type: 'String',
484
+ value: scanner.scanString()
485
+ });
486
+
487
+ case SPACE:
488
+ case TAB:
489
+ case N:
490
+ case R:
491
+ case F:
492
+ return {
493
+ type: 'Spaces',
494
+ value: scanner.scanSpaces()
495
+ };
496
+
497
+ case COMMERCIALAT:
498
+ code = scanner.nextCharCode();
499
+
500
+ if (scanner.isNameCharCode(code)) {
501
+ scanner.pos++;
502
+ return {
503
+ type: 'AtKeyword',
504
+ name: scanner.scanWord()
505
+ };
506
+ }
507
+
508
+ return maybeToken(scanner);
509
+
510
+ case ASTERISK:
511
+ case PLUSSIGN:
512
+ case QUESTIONMARK:
513
+ case NUMBERSIGN:
514
+ case EXCLAMATIONMARK:
515
+ // prohibited tokens (used as a multiplier start)
516
+ break;
517
+
518
+ case LEFTCURLYBRACKET:
519
+ // LEFTCURLYBRACKET is allowed since mdn/data uses it w/o quoting
520
+ // check next char isn't a number, because it's likely a disjoined multiplier
521
+ code = scanner.nextCharCode();
522
+
523
+ if (code < 48 || code > 57) {
524
+ return maybeToken(scanner);
525
+ }
526
+
527
+ break;
528
+
529
+ default:
530
+ if (scanner.isNameCharCode(code)) {
531
+ return readKeywordOrFunction(scanner);
532
+ }
533
+
534
+ return maybeToken(scanner);
535
+ }
536
+ }
537
+
538
+ export function parse(source) {
539
+ const scanner = new Scanner(source);
540
+ const result = readImplicitGroup(scanner);
541
+
542
+ if (scanner.pos !== source.length) {
543
+ scanner.error('Unexpected input');
544
+ }
545
+
546
+ // reduce redundant groups with single group term
547
+ if (result.terms.length === 1 && result.terms[0].type === 'Group') {
548
+ return result.terms[0];
549
+ }
550
+
551
+ return result;
552
+ };
@@ -0,0 +1,109 @@
1
+ import { SyntaxError } from './SyntaxError.js';
2
+
3
+ const TAB = 9;
4
+ const N = 10;
5
+ const F = 12;
6
+ const R = 13;
7
+ const SPACE = 32;
8
+ const NAME_CHAR = new Uint8Array(128).map((_, idx) =>
9
+ /[a-zA-Z0-9\-]/.test(String.fromCharCode(idx)) ? 1 : 0
10
+ );
11
+
12
+ export class Scanner {
13
+ constructor(str) {
14
+ this.str = str;
15
+ this.pos = 0;
16
+ }
17
+
18
+ charCodeAt(pos) {
19
+ return pos < this.str.length ? this.str.charCodeAt(pos) : 0;
20
+ }
21
+ charCode() {
22
+ return this.charCodeAt(this.pos);
23
+ }
24
+ isNameCharCode(code = this.charCode()) {
25
+ return code < 128 && NAME_CHAR[code] === 1;
26
+ }
27
+ nextCharCode() {
28
+ return this.charCodeAt(this.pos + 1);
29
+ }
30
+ nextNonWsCode(pos) {
31
+ return this.charCodeAt(this.findWsEnd(pos));
32
+ }
33
+ skipWs() {
34
+ this.pos = this.findWsEnd(this.pos);
35
+ }
36
+ findWsEnd(pos) {
37
+ for (; pos < this.str.length; pos++) {
38
+ const code = this.str.charCodeAt(pos);
39
+ if (code !== R && code !== N && code !== F && code !== SPACE && code !== TAB) {
40
+ break;
41
+ }
42
+ }
43
+
44
+ return pos;
45
+ }
46
+ substringToPos(end) {
47
+ return this.str.substring(this.pos, this.pos = end);
48
+ }
49
+ eat(code) {
50
+ if (this.charCode() !== code) {
51
+ this.error('Expect `' + String.fromCharCode(code) + '`');
52
+ }
53
+
54
+ this.pos++;
55
+ }
56
+ peek() {
57
+ return this.pos < this.str.length ? this.str.charAt(this.pos++) : '';
58
+ }
59
+ error(message) {
60
+ throw new SyntaxError(message, this.str, this.pos);
61
+ }
62
+
63
+ scanSpaces() {
64
+ return this.substringToPos(this.findWsEnd(this.pos));
65
+ }
66
+ scanWord() {
67
+ let end = this.pos;
68
+
69
+ for (; end < this.str.length; end++) {
70
+ const code = this.str.charCodeAt(end);
71
+ if (code >= 128 || NAME_CHAR[code] === 0) {
72
+ break;
73
+ }
74
+ }
75
+
76
+ if (this.pos === end) {
77
+ this.error('Expect a keyword');
78
+ }
79
+
80
+ return this.substringToPos(end);
81
+ }
82
+ scanNumber() {
83
+ let end = this.pos;
84
+
85
+ for (; end < this.str.length; end++) {
86
+ const code = this.str.charCodeAt(end);
87
+
88
+ if (code < 48 || code > 57) {
89
+ break;
90
+ }
91
+ }
92
+
93
+ if (this.pos === end) {
94
+ this.error('Expect a number');
95
+ }
96
+
97
+ return this.substringToPos(end);
98
+ }
99
+ scanString() {
100
+ const end = this.str.indexOf('\'', this.pos + 1);
101
+
102
+ if (end === -1) {
103
+ this.pos = this.str.length;
104
+ this.error('Expect an apostrophe');
105
+ }
106
+
107
+ return this.substringToPos(end + 1);
108
+ }
109
+ };
@@ -0,0 +1,53 @@
1
+ const noop = function() {};
2
+
3
+ function ensureFunction(value) {
4
+ return typeof value === 'function' ? value : noop;
5
+ }
6
+
7
+ export function walk(node, options, context) {
8
+ function walk(node) {
9
+ enter.call(context, node);
10
+
11
+ switch (node.type) {
12
+ case 'Group':
13
+ node.terms.forEach(walk);
14
+ break;
15
+
16
+ case 'Multiplier':
17
+ case 'Boolean':
18
+ walk(node.term);
19
+ break;
20
+
21
+ case 'Type':
22
+ case 'Property':
23
+ case 'Keyword':
24
+ case 'AtKeyword':
25
+ case 'Function':
26
+ case 'String':
27
+ case 'Token':
28
+ case 'Comma':
29
+ break;
30
+
31
+ default:
32
+ throw new Error('Unknown type: ' + node.type);
33
+ }
34
+
35
+ leave.call(context, node);
36
+ }
37
+
38
+ let enter = noop;
39
+ let leave = noop;
40
+
41
+ if (typeof options === 'function') {
42
+ enter = options;
43
+ } else if (options) {
44
+ enter = ensureFunction(options.enter);
45
+ leave = ensureFunction(options.leave);
46
+ }
47
+
48
+ if (enter === noop && leave === noop) {
49
+ throw new Error('Neither `enter` nor `leave` walker handler is set or both aren\'t a function');
50
+ }
51
+
52
+ walk(node, context);
53
+ };