1 | "use strict";
|
---|
2 | var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
---|
3 | if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
---|
4 | if (ar || !(i in from)) {
|
---|
5 | if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
---|
6 | ar[i] = from[i];
|
---|
7 | }
|
---|
8 | }
|
---|
9 | return to.concat(ar || Array.prototype.slice.call(from));
|
---|
10 | };
|
---|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
12 | exports.isTraversal = void 0;
|
---|
13 | var reName = /^[^\\#]?(?:\\(?:[\da-f]{1,6}\s?|.)|[\w\-\u00b0-\uFFFF])+/;
|
---|
14 | var reEscape = /\\([\da-f]{1,6}\s?|(\s)|.)/gi;
|
---|
15 | var actionTypes = new Map([
|
---|
16 | ["~", "element"],
|
---|
17 | ["^", "start"],
|
---|
18 | ["$", "end"],
|
---|
19 | ["*", "any"],
|
---|
20 | ["!", "not"],
|
---|
21 | ["|", "hyphen"],
|
---|
22 | ]);
|
---|
23 | var Traversals = {
|
---|
24 | ">": "child",
|
---|
25 | "<": "parent",
|
---|
26 | "~": "sibling",
|
---|
27 | "+": "adjacent",
|
---|
28 | };
|
---|
29 | var attribSelectors = {
|
---|
30 | "#": ["id", "equals"],
|
---|
31 | ".": ["class", "element"],
|
---|
32 | };
|
---|
33 | // Pseudos, whose data property is parsed as well.
|
---|
34 | var unpackPseudos = new Set([
|
---|
35 | "has",
|
---|
36 | "not",
|
---|
37 | "matches",
|
---|
38 | "is",
|
---|
39 | "where",
|
---|
40 | "host",
|
---|
41 | "host-context",
|
---|
42 | ]);
|
---|
43 | var traversalNames = new Set(__spreadArray([
|
---|
44 | "descendant"
|
---|
45 | ], Object.keys(Traversals).map(function (k) { return Traversals[k]; }), true));
|
---|
46 | /**
|
---|
47 | * Attributes that are case-insensitive in HTML.
|
---|
48 | *
|
---|
49 | * @private
|
---|
50 | * @see https://html.spec.whatwg.org/multipage/semantics-other.html#case-sensitivity-of-selectors
|
---|
51 | */
|
---|
52 | var caseInsensitiveAttributes = new Set([
|
---|
53 | "accept",
|
---|
54 | "accept-charset",
|
---|
55 | "align",
|
---|
56 | "alink",
|
---|
57 | "axis",
|
---|
58 | "bgcolor",
|
---|
59 | "charset",
|
---|
60 | "checked",
|
---|
61 | "clear",
|
---|
62 | "codetype",
|
---|
63 | "color",
|
---|
64 | "compact",
|
---|
65 | "declare",
|
---|
66 | "defer",
|
---|
67 | "dir",
|
---|
68 | "direction",
|
---|
69 | "disabled",
|
---|
70 | "enctype",
|
---|
71 | "face",
|
---|
72 | "frame",
|
---|
73 | "hreflang",
|
---|
74 | "http-equiv",
|
---|
75 | "lang",
|
---|
76 | "language",
|
---|
77 | "link",
|
---|
78 | "media",
|
---|
79 | "method",
|
---|
80 | "multiple",
|
---|
81 | "nohref",
|
---|
82 | "noresize",
|
---|
83 | "noshade",
|
---|
84 | "nowrap",
|
---|
85 | "readonly",
|
---|
86 | "rel",
|
---|
87 | "rev",
|
---|
88 | "rules",
|
---|
89 | "scope",
|
---|
90 | "scrolling",
|
---|
91 | "selected",
|
---|
92 | "shape",
|
---|
93 | "target",
|
---|
94 | "text",
|
---|
95 | "type",
|
---|
96 | "valign",
|
---|
97 | "valuetype",
|
---|
98 | "vlink",
|
---|
99 | ]);
|
---|
100 | /**
|
---|
101 | * Checks whether a specific selector is a traversal.
|
---|
102 | * This is useful eg. in swapping the order of elements that
|
---|
103 | * are not traversals.
|
---|
104 | *
|
---|
105 | * @param selector Selector to check.
|
---|
106 | */
|
---|
107 | function isTraversal(selector) {
|
---|
108 | return traversalNames.has(selector.type);
|
---|
109 | }
|
---|
110 | exports.isTraversal = isTraversal;
|
---|
111 | var stripQuotesFromPseudos = new Set(["contains", "icontains"]);
|
---|
112 | var quotes = new Set(['"', "'"]);
|
---|
113 | // Unescape function taken from https://github.com/jquery/sizzle/blob/master/src/sizzle.js#L152
|
---|
114 | function funescape(_, escaped, escapedWhitespace) {
|
---|
115 | var high = parseInt(escaped, 16) - 0x10000;
|
---|
116 | // NaN means non-codepoint
|
---|
117 | return high !== high || escapedWhitespace
|
---|
118 | ? escaped
|
---|
119 | : high < 0
|
---|
120 | ? // BMP codepoint
|
---|
121 | String.fromCharCode(high + 0x10000)
|
---|
122 | : // Supplemental Plane codepoint (surrogate pair)
|
---|
123 | String.fromCharCode((high >> 10) | 0xd800, (high & 0x3ff) | 0xdc00);
|
---|
124 | }
|
---|
125 | function unescapeCSS(str) {
|
---|
126 | return str.replace(reEscape, funescape);
|
---|
127 | }
|
---|
128 | function isWhitespace(c) {
|
---|
129 | return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";
|
---|
130 | }
|
---|
131 | /**
|
---|
132 | * Parses `selector`, optionally with the passed `options`.
|
---|
133 | *
|
---|
134 | * @param selector Selector to parse.
|
---|
135 | * @param options Options for parsing.
|
---|
136 | * @returns Returns a two-dimensional array.
|
---|
137 | * The first dimension represents selectors separated by commas (eg. `sub1, sub2`),
|
---|
138 | * the second contains the relevant tokens for that selector.
|
---|
139 | */
|
---|
140 | function parse(selector, options) {
|
---|
141 | var subselects = [];
|
---|
142 | var endIndex = parseSelector(subselects, "" + selector, options, 0);
|
---|
143 | if (endIndex < selector.length) {
|
---|
144 | throw new Error("Unmatched selector: " + selector.slice(endIndex));
|
---|
145 | }
|
---|
146 | return subselects;
|
---|
147 | }
|
---|
148 | exports.default = parse;
|
---|
149 | function parseSelector(subselects, selector, options, selectorIndex) {
|
---|
150 | var _a, _b;
|
---|
151 | if (options === void 0) { options = {}; }
|
---|
152 | var tokens = [];
|
---|
153 | var sawWS = false;
|
---|
154 | function getName(offset) {
|
---|
155 | var match = selector.slice(selectorIndex + offset).match(reName);
|
---|
156 | if (!match) {
|
---|
157 | throw new Error("Expected name, found " + selector.slice(selectorIndex));
|
---|
158 | }
|
---|
159 | var name = match[0];
|
---|
160 | selectorIndex += offset + name.length;
|
---|
161 | return unescapeCSS(name);
|
---|
162 | }
|
---|
163 | function stripWhitespace(offset) {
|
---|
164 | while (isWhitespace(selector.charAt(selectorIndex + offset)))
|
---|
165 | offset++;
|
---|
166 | selectorIndex += offset;
|
---|
167 | }
|
---|
168 | function isEscaped(pos) {
|
---|
169 | var slashCount = 0;
|
---|
170 | while (selector.charAt(--pos) === "\\")
|
---|
171 | slashCount++;
|
---|
172 | return (slashCount & 1) === 1;
|
---|
173 | }
|
---|
174 | function ensureNotTraversal() {
|
---|
175 | if (tokens.length > 0 && isTraversal(tokens[tokens.length - 1])) {
|
---|
176 | throw new Error("Did not expect successive traversals.");
|
---|
177 | }
|
---|
178 | }
|
---|
179 | stripWhitespace(0);
|
---|
180 | while (selector !== "") {
|
---|
181 | var firstChar = selector.charAt(selectorIndex);
|
---|
182 | if (isWhitespace(firstChar)) {
|
---|
183 | sawWS = true;
|
---|
184 | stripWhitespace(1);
|
---|
185 | }
|
---|
186 | else if (firstChar in Traversals) {
|
---|
187 | ensureNotTraversal();
|
---|
188 | tokens.push({ type: Traversals[firstChar] });
|
---|
189 | sawWS = false;
|
---|
190 | stripWhitespace(1);
|
---|
191 | }
|
---|
192 | else if (firstChar === ",") {
|
---|
193 | if (tokens.length === 0) {
|
---|
194 | throw new Error("Empty sub-selector");
|
---|
195 | }
|
---|
196 | subselects.push(tokens);
|
---|
197 | tokens = [];
|
---|
198 | sawWS = false;
|
---|
199 | stripWhitespace(1);
|
---|
200 | }
|
---|
201 | else if (selector.startsWith("/*", selectorIndex)) {
|
---|
202 | var endIndex = selector.indexOf("*/", selectorIndex + 2);
|
---|
203 | if (endIndex < 0) {
|
---|
204 | throw new Error("Comment was not terminated");
|
---|
205 | }
|
---|
206 | selectorIndex = endIndex + 2;
|
---|
207 | }
|
---|
208 | else {
|
---|
209 | if (sawWS) {
|
---|
210 | ensureNotTraversal();
|
---|
211 | tokens.push({ type: "descendant" });
|
---|
212 | sawWS = false;
|
---|
213 | }
|
---|
214 | if (firstChar in attribSelectors) {
|
---|
215 | var _c = attribSelectors[firstChar], name_1 = _c[0], action = _c[1];
|
---|
216 | tokens.push({
|
---|
217 | type: "attribute",
|
---|
218 | name: name_1,
|
---|
219 | action: action,
|
---|
220 | value: getName(1),
|
---|
221 | namespace: null,
|
---|
222 | // TODO: Add quirksMode option, which makes `ignoreCase` `true` for HTML.
|
---|
223 | ignoreCase: options.xmlMode ? null : false,
|
---|
224 | });
|
---|
225 | }
|
---|
226 | else if (firstChar === "[") {
|
---|
227 | stripWhitespace(1);
|
---|
228 | // Determine attribute name and namespace
|
---|
229 | var namespace = null;
|
---|
230 | if (selector.charAt(selectorIndex) === "|") {
|
---|
231 | namespace = "";
|
---|
232 | selectorIndex += 1;
|
---|
233 | }
|
---|
234 | if (selector.startsWith("*|", selectorIndex)) {
|
---|
235 | namespace = "*";
|
---|
236 | selectorIndex += 2;
|
---|
237 | }
|
---|
238 | var name_2 = getName(0);
|
---|
239 | if (namespace === null &&
|
---|
240 | selector.charAt(selectorIndex) === "|" &&
|
---|
241 | selector.charAt(selectorIndex + 1) !== "=") {
|
---|
242 | namespace = name_2;
|
---|
243 | name_2 = getName(1);
|
---|
244 | }
|
---|
245 | if ((_a = options.lowerCaseAttributeNames) !== null && _a !== void 0 ? _a : !options.xmlMode) {
|
---|
246 | name_2 = name_2.toLowerCase();
|
---|
247 | }
|
---|
248 | stripWhitespace(0);
|
---|
249 | // Determine comparison operation
|
---|
250 | var action = "exists";
|
---|
251 | var possibleAction = actionTypes.get(selector.charAt(selectorIndex));
|
---|
252 | if (possibleAction) {
|
---|
253 | action = possibleAction;
|
---|
254 | if (selector.charAt(selectorIndex + 1) !== "=") {
|
---|
255 | throw new Error("Expected `=`");
|
---|
256 | }
|
---|
257 | stripWhitespace(2);
|
---|
258 | }
|
---|
259 | else if (selector.charAt(selectorIndex) === "=") {
|
---|
260 | action = "equals";
|
---|
261 | stripWhitespace(1);
|
---|
262 | }
|
---|
263 | // Determine value
|
---|
264 | var value = "";
|
---|
265 | var ignoreCase = null;
|
---|
266 | if (action !== "exists") {
|
---|
267 | if (quotes.has(selector.charAt(selectorIndex))) {
|
---|
268 | var quote = selector.charAt(selectorIndex);
|
---|
269 | var sectionEnd = selectorIndex + 1;
|
---|
270 | while (sectionEnd < selector.length &&
|
---|
271 | (selector.charAt(sectionEnd) !== quote ||
|
---|
272 | isEscaped(sectionEnd))) {
|
---|
273 | sectionEnd += 1;
|
---|
274 | }
|
---|
275 | if (selector.charAt(sectionEnd) !== quote) {
|
---|
276 | throw new Error("Attribute value didn't end");
|
---|
277 | }
|
---|
278 | value = unescapeCSS(selector.slice(selectorIndex + 1, sectionEnd));
|
---|
279 | selectorIndex = sectionEnd + 1;
|
---|
280 | }
|
---|
281 | else {
|
---|
282 | var valueStart = selectorIndex;
|
---|
283 | while (selectorIndex < selector.length &&
|
---|
284 | ((!isWhitespace(selector.charAt(selectorIndex)) &&
|
---|
285 | selector.charAt(selectorIndex) !== "]") ||
|
---|
286 | isEscaped(selectorIndex))) {
|
---|
287 | selectorIndex += 1;
|
---|
288 | }
|
---|
289 | value = unescapeCSS(selector.slice(valueStart, selectorIndex));
|
---|
290 | }
|
---|
291 | stripWhitespace(0);
|
---|
292 | // See if we have a force ignore flag
|
---|
293 | var forceIgnore = selector.charAt(selectorIndex);
|
---|
294 | // If the forceIgnore flag is set (either `i` or `s`), use that value
|
---|
295 | if (forceIgnore === "s" || forceIgnore === "S") {
|
---|
296 | ignoreCase = false;
|
---|
297 | stripWhitespace(1);
|
---|
298 | }
|
---|
299 | else if (forceIgnore === "i" || forceIgnore === "I") {
|
---|
300 | ignoreCase = true;
|
---|
301 | stripWhitespace(1);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | // If `xmlMode` is set, there are no rules; otherwise, use the `caseInsensitiveAttributes` list.
|
---|
305 | if (!options.xmlMode) {
|
---|
306 | // TODO: Skip this for `exists`, as there is no value to compare to.
|
---|
307 | ignoreCase !== null && ignoreCase !== void 0 ? ignoreCase : (ignoreCase = caseInsensitiveAttributes.has(name_2));
|
---|
308 | }
|
---|
309 | if (selector.charAt(selectorIndex) !== "]") {
|
---|
310 | throw new Error("Attribute selector didn't terminate");
|
---|
311 | }
|
---|
312 | selectorIndex += 1;
|
---|
313 | var attributeSelector = {
|
---|
314 | type: "attribute",
|
---|
315 | name: name_2,
|
---|
316 | action: action,
|
---|
317 | value: value,
|
---|
318 | namespace: namespace,
|
---|
319 | ignoreCase: ignoreCase,
|
---|
320 | };
|
---|
321 | tokens.push(attributeSelector);
|
---|
322 | }
|
---|
323 | else if (firstChar === ":") {
|
---|
324 | if (selector.charAt(selectorIndex + 1) === ":") {
|
---|
325 | tokens.push({
|
---|
326 | type: "pseudo-element",
|
---|
327 | name: getName(2).toLowerCase(),
|
---|
328 | });
|
---|
329 | continue;
|
---|
330 | }
|
---|
331 | var name_3 = getName(1).toLowerCase();
|
---|
332 | var data = null;
|
---|
333 | if (selector.charAt(selectorIndex) === "(") {
|
---|
334 | if (unpackPseudos.has(name_3)) {
|
---|
335 | if (quotes.has(selector.charAt(selectorIndex + 1))) {
|
---|
336 | throw new Error("Pseudo-selector " + name_3 + " cannot be quoted");
|
---|
337 | }
|
---|
338 | data = [];
|
---|
339 | selectorIndex = parseSelector(data, selector, options, selectorIndex + 1);
|
---|
340 | if (selector.charAt(selectorIndex) !== ")") {
|
---|
341 | throw new Error("Missing closing parenthesis in :" + name_3 + " (" + selector + ")");
|
---|
342 | }
|
---|
343 | selectorIndex += 1;
|
---|
344 | }
|
---|
345 | else {
|
---|
346 | selectorIndex += 1;
|
---|
347 | var start = selectorIndex;
|
---|
348 | var counter = 1;
|
---|
349 | for (; counter > 0 && selectorIndex < selector.length; selectorIndex++) {
|
---|
350 | if (selector.charAt(selectorIndex) === "(" &&
|
---|
351 | !isEscaped(selectorIndex)) {
|
---|
352 | counter++;
|
---|
353 | }
|
---|
354 | else if (selector.charAt(selectorIndex) === ")" &&
|
---|
355 | !isEscaped(selectorIndex)) {
|
---|
356 | counter--;
|
---|
357 | }
|
---|
358 | }
|
---|
359 | if (counter) {
|
---|
360 | throw new Error("Parenthesis not matched");
|
---|
361 | }
|
---|
362 | data = selector.slice(start, selectorIndex - 1);
|
---|
363 | if (stripQuotesFromPseudos.has(name_3)) {
|
---|
364 | var quot = data.charAt(0);
|
---|
365 | if (quot === data.slice(-1) && quotes.has(quot)) {
|
---|
366 | data = data.slice(1, -1);
|
---|
367 | }
|
---|
368 | data = unescapeCSS(data);
|
---|
369 | }
|
---|
370 | }
|
---|
371 | }
|
---|
372 | tokens.push({ type: "pseudo", name: name_3, data: data });
|
---|
373 | }
|
---|
374 | else {
|
---|
375 | var namespace = null;
|
---|
376 | var name_4 = void 0;
|
---|
377 | if (firstChar === "*") {
|
---|
378 | selectorIndex += 1;
|
---|
379 | name_4 = "*";
|
---|
380 | }
|
---|
381 | else if (reName.test(selector.slice(selectorIndex))) {
|
---|
382 | if (selector.charAt(selectorIndex) === "|") {
|
---|
383 | namespace = "";
|
---|
384 | selectorIndex += 1;
|
---|
385 | }
|
---|
386 | name_4 = getName(0);
|
---|
387 | }
|
---|
388 | else {
|
---|
389 | /*
|
---|
390 | * We have finished parsing the selector.
|
---|
391 | * Remove descendant tokens at the end if they exist,
|
---|
392 | * and return the last index, so that parsing can be
|
---|
393 | * picked up from here.
|
---|
394 | */
|
---|
395 | if (tokens.length &&
|
---|
396 | tokens[tokens.length - 1].type === "descendant") {
|
---|
397 | tokens.pop();
|
---|
398 | }
|
---|
399 | addToken(subselects, tokens);
|
---|
400 | return selectorIndex;
|
---|
401 | }
|
---|
402 | if (selector.charAt(selectorIndex) === "|") {
|
---|
403 | namespace = name_4;
|
---|
404 | if (selector.charAt(selectorIndex + 1) === "*") {
|
---|
405 | name_4 = "*";
|
---|
406 | selectorIndex += 2;
|
---|
407 | }
|
---|
408 | else {
|
---|
409 | name_4 = getName(1);
|
---|
410 | }
|
---|
411 | }
|
---|
412 | if (name_4 === "*") {
|
---|
413 | tokens.push({ type: "universal", namespace: namespace });
|
---|
414 | }
|
---|
415 | else {
|
---|
416 | if ((_b = options.lowerCaseTags) !== null && _b !== void 0 ? _b : !options.xmlMode) {
|
---|
417 | name_4 = name_4.toLowerCase();
|
---|
418 | }
|
---|
419 | tokens.push({ type: "tag", name: name_4, namespace: namespace });
|
---|
420 | }
|
---|
421 | }
|
---|
422 | }
|
---|
423 | }
|
---|
424 | addToken(subselects, tokens);
|
---|
425 | return selectorIndex;
|
---|
426 | }
|
---|
427 | function addToken(subselects, tokens) {
|
---|
428 | if (subselects.length > 0 && tokens.length === 0) {
|
---|
429 | throw new Error("Empty sub-selector");
|
---|
430 | }
|
---|
431 | subselects.push(tokens);
|
---|
432 | }
|
---|