@stacksjs/ts-css 0.1.1 → 0.1.2
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.
- package/CHANGELOG.md +14 -0
- package/dist/index.js +57 -12868
- package/dist/optimize/index.js +1 -3389
- package/dist/parse/index.js +1 -2368
- package/dist/select/index.js +1 -1288
- package/dist/what/index.js +1 -513
- package/package.json +1 -1
package/dist/select/index.js
CHANGED
|
@@ -1,1289 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __returnValue = (v) => v;
|
|
4
|
-
function __exportSetter(name, newValue) {
|
|
5
|
-
this[name] = __returnValue.bind(null, newValue);
|
|
6
|
-
}
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, {
|
|
10
|
-
get: all[name],
|
|
11
|
-
enumerable: true,
|
|
12
|
-
configurable: true,
|
|
13
|
-
set: __exportSetter.bind(all, name)
|
|
14
|
-
});
|
|
15
|
-
};
|
|
16
|
-
var __require = import.meta.require;
|
|
17
|
-
|
|
18
|
-
// src/what/index.ts
|
|
19
|
-
var exports_what = {};
|
|
20
|
-
__export(exports_what, {
|
|
21
|
-
stringify: () => stringify,
|
|
22
|
-
parse: () => parse,
|
|
23
|
-
isTraversal: () => isTraversal,
|
|
24
|
-
IgnoreCaseMode: () => IgnoreCaseMode
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
// src/what/parse.ts
|
|
28
|
-
var RE_NAME_STICKY = /(?:\\(?:[\dA-Fa-f]{1,6} ?|[^])|[\w\-\u00B0-\uFFFF])+/y;
|
|
29
|
-
var RE_ESCAPE = /\\([\dA-Fa-f]{1,6} ?|[^])/g;
|
|
30
|
-
function unescape(name) {
|
|
31
|
-
return name.replace(RE_ESCAPE, (_m, escape) => {
|
|
32
|
-
if (escape.length > 1 && /^[\dA-Fa-f]/.test(escape)) {
|
|
33
|
-
const code = Number.parseInt(escape, 16);
|
|
34
|
-
if (code >= 55296 && code <= 57343)
|
|
35
|
-
return "\uFFFD";
|
|
36
|
-
return String.fromCodePoint(code);
|
|
37
|
-
}
|
|
38
|
-
return escape;
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
function unescapeIfNeeded(name) {
|
|
42
|
-
return name.indexOf("\\") < 0 ? name : unescape(name);
|
|
43
|
-
}
|
|
44
|
-
var ATTRIBUTES_QUIRKS = new Set([
|
|
45
|
-
"accept",
|
|
46
|
-
"accept-charset",
|
|
47
|
-
"align",
|
|
48
|
-
"alink",
|
|
49
|
-
"axis",
|
|
50
|
-
"bgcolor",
|
|
51
|
-
"charset",
|
|
52
|
-
"checked",
|
|
53
|
-
"clear",
|
|
54
|
-
"codetype",
|
|
55
|
-
"color",
|
|
56
|
-
"compact",
|
|
57
|
-
"declare",
|
|
58
|
-
"defer",
|
|
59
|
-
"dir",
|
|
60
|
-
"direction",
|
|
61
|
-
"disabled",
|
|
62
|
-
"enctype",
|
|
63
|
-
"face",
|
|
64
|
-
"frame",
|
|
65
|
-
"hreflang",
|
|
66
|
-
"http-equiv",
|
|
67
|
-
"lang",
|
|
68
|
-
"language",
|
|
69
|
-
"link",
|
|
70
|
-
"media",
|
|
71
|
-
"method",
|
|
72
|
-
"multiple",
|
|
73
|
-
"nohref",
|
|
74
|
-
"noresize",
|
|
75
|
-
"noshade",
|
|
76
|
-
"nowrap",
|
|
77
|
-
"readonly",
|
|
78
|
-
"rel",
|
|
79
|
-
"rev",
|
|
80
|
-
"rules",
|
|
81
|
-
"scope",
|
|
82
|
-
"scrolling",
|
|
83
|
-
"selected",
|
|
84
|
-
"shape",
|
|
85
|
-
"target",
|
|
86
|
-
"text",
|
|
87
|
-
"type",
|
|
88
|
-
"valign",
|
|
89
|
-
"valuetype",
|
|
90
|
-
"vlink"
|
|
91
|
-
]);
|
|
92
|
-
function actionFromChar(ch) {
|
|
93
|
-
switch (ch) {
|
|
94
|
-
case 126:
|
|
95
|
-
return "element";
|
|
96
|
-
case 94:
|
|
97
|
-
return "start";
|
|
98
|
-
case 36:
|
|
99
|
-
return "end";
|
|
100
|
-
case 42:
|
|
101
|
-
return "any";
|
|
102
|
-
case 33:
|
|
103
|
-
return "not";
|
|
104
|
-
case 124:
|
|
105
|
-
return "hyphen";
|
|
106
|
-
default:
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
function isWsCode(c) {
|
|
111
|
-
return c === 32 || c === 9 || c === 10 || c === 13 || c === 12;
|
|
112
|
-
}
|
|
113
|
-
function parse(selector, options = {}) {
|
|
114
|
-
const subselects = [];
|
|
115
|
-
const endIndex = parseSelectorImpl(subselects, selector, options, 0);
|
|
116
|
-
if (endIndex < selector.length)
|
|
117
|
-
throw new Error(`Unmatched selector: ${selector.slice(endIndex)}`);
|
|
118
|
-
return subselects;
|
|
119
|
-
}
|
|
120
|
-
function readName(selector, from) {
|
|
121
|
-
RE_NAME_STICKY.lastIndex = from;
|
|
122
|
-
const m = RE_NAME_STICKY.exec(selector);
|
|
123
|
-
if (!m)
|
|
124
|
-
throw new Error(`Expected name, found ${selector.slice(from)}`);
|
|
125
|
-
return { value: unescapeIfNeeded(m[0]), end: from + m[0].length };
|
|
126
|
-
}
|
|
127
|
-
function stripWS(selector, from) {
|
|
128
|
-
while (from < selector.length && isWsCode(selector.charCodeAt(from)))
|
|
129
|
-
from++;
|
|
130
|
-
return from;
|
|
131
|
-
}
|
|
132
|
-
function parseSelectorImpl(subselects, selector, options, startIndex) {
|
|
133
|
-
let tokens = [];
|
|
134
|
-
let i = stripWS(selector, startIndex);
|
|
135
|
-
const len = selector.length;
|
|
136
|
-
const xmlMode = options.xmlMode === true;
|
|
137
|
-
const lowerCaseAttrs = options.lowerCaseAttributeNames !== false && !xmlMode;
|
|
138
|
-
const lowerCaseTagsFlag = options.lowerCaseTags !== false;
|
|
139
|
-
while (i < len) {
|
|
140
|
-
const code = selector.charCodeAt(i);
|
|
141
|
-
if (isWsCode(code)) {
|
|
142
|
-
let trimmed = i + 1;
|
|
143
|
-
while (trimmed < len && isWsCode(selector.charCodeAt(trimmed)))
|
|
144
|
-
trimmed++;
|
|
145
|
-
if (tokens.length === 0)
|
|
146
|
-
return trimmed;
|
|
147
|
-
i = trimmed;
|
|
148
|
-
addTraversal(tokens, "descendant");
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
if (code === 62 || code === 60 || code === 126 || code === 43 || code === 124) {
|
|
152
|
-
let j = i + 1;
|
|
153
|
-
while (j < len && isWsCode(selector.charCodeAt(j)))
|
|
154
|
-
j++;
|
|
155
|
-
i = j;
|
|
156
|
-
switch (code) {
|
|
157
|
-
case 62:
|
|
158
|
-
addTraversal(tokens, "child");
|
|
159
|
-
break;
|
|
160
|
-
case 60:
|
|
161
|
-
addTraversal(tokens, "parent");
|
|
162
|
-
break;
|
|
163
|
-
case 126:
|
|
164
|
-
addTraversal(tokens, "sibling");
|
|
165
|
-
break;
|
|
166
|
-
case 43:
|
|
167
|
-
addTraversal(tokens, "adjacent");
|
|
168
|
-
break;
|
|
169
|
-
case 124:
|
|
170
|
-
if (i < len && selector.charCodeAt(i) === 124) {
|
|
171
|
-
i++;
|
|
172
|
-
i = stripWS(selector, i);
|
|
173
|
-
addTraversal(tokens, "column-combinator");
|
|
174
|
-
} else {
|
|
175
|
-
tokens.push({ type: "tag", name: "", namespace: "" });
|
|
176
|
-
}
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
if (code === 44) {
|
|
182
|
-
if (tokens.length === 0)
|
|
183
|
-
throw new Error("Empty sub-selector");
|
|
184
|
-
subselects.push(tokens);
|
|
185
|
-
tokens = [];
|
|
186
|
-
i = stripWS(selector, i + 1);
|
|
187
|
-
continue;
|
|
188
|
-
}
|
|
189
|
-
if (code === 47 && selector.charCodeAt(i + 1) === 42) {
|
|
190
|
-
const end = selector.indexOf("*/", i + 2);
|
|
191
|
-
if (end < 0)
|
|
192
|
-
throw new Error("Unmatched comment");
|
|
193
|
-
i = stripWS(selector, end + 2);
|
|
194
|
-
continue;
|
|
195
|
-
}
|
|
196
|
-
if (code === 42) {
|
|
197
|
-
i++;
|
|
198
|
-
tokens.push({ type: "universal", namespace: null });
|
|
199
|
-
continue;
|
|
200
|
-
}
|
|
201
|
-
if (code === 35) {
|
|
202
|
-
const r = readName(selector, i + 1);
|
|
203
|
-
i = r.end;
|
|
204
|
-
tokens.push({
|
|
205
|
-
type: "attribute",
|
|
206
|
-
name: "id",
|
|
207
|
-
action: "equals",
|
|
208
|
-
value: r.value,
|
|
209
|
-
namespace: null,
|
|
210
|
-
ignoreCase: false
|
|
211
|
-
});
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
|
-
if (code === 46) {
|
|
215
|
-
const r = readName(selector, i + 1);
|
|
216
|
-
i = r.end;
|
|
217
|
-
tokens.push({
|
|
218
|
-
type: "attribute",
|
|
219
|
-
name: "class",
|
|
220
|
-
action: "element",
|
|
221
|
-
value: r.value,
|
|
222
|
-
namespace: null,
|
|
223
|
-
ignoreCase: false
|
|
224
|
-
});
|
|
225
|
-
continue;
|
|
226
|
-
}
|
|
227
|
-
if (code === 91) {
|
|
228
|
-
i = parseAttribute(selector, i, tokens, options, xmlMode, lowerCaseAttrs);
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
if (code === 58) {
|
|
232
|
-
i = parsePseudo(selector, i, tokens, options);
|
|
233
|
-
continue;
|
|
234
|
-
}
|
|
235
|
-
if (code === 124) {
|
|
236
|
-
i++;
|
|
237
|
-
const r = readName(selector, i);
|
|
238
|
-
i = r.end;
|
|
239
|
-
tokens.push({ type: "tag", name: lowerCaseTagsFlag ? r.value.toLowerCase() : r.value, namespace: "" });
|
|
240
|
-
continue;
|
|
241
|
-
}
|
|
242
|
-
{
|
|
243
|
-
const r1 = readName(selector, i);
|
|
244
|
-
i = r1.end;
|
|
245
|
-
if (i < len && selector.charCodeAt(i) === 124 && selector.charCodeAt(i + 1) !== 61) {
|
|
246
|
-
i++;
|
|
247
|
-
const r2 = readName(selector, i);
|
|
248
|
-
i = r2.end;
|
|
249
|
-
tokens.push({ type: "tag", name: lowerCaseTagsFlag ? r2.value.toLowerCase() : r2.value, namespace: r1.value });
|
|
250
|
-
} else {
|
|
251
|
-
tokens.push({ type: "tag", name: lowerCaseTagsFlag ? r1.value.toLowerCase() : r1.value, namespace: null });
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
if (tokens.length > 0)
|
|
256
|
-
subselects.push(tokens);
|
|
257
|
-
return i;
|
|
258
|
-
}
|
|
259
|
-
function parseAttribute(selector, idx, tokens, options, xmlMode, lowerCaseAttrs) {
|
|
260
|
-
let i = idx + 1;
|
|
261
|
-
const len = selector.length;
|
|
262
|
-
let attribute;
|
|
263
|
-
if (selector.charCodeAt(i) === 124)
|
|
264
|
-
throw new Error("Empty namespace not supported");
|
|
265
|
-
if (selector.charCodeAt(i) === 42 && selector.charCodeAt(i + 1) === 124) {
|
|
266
|
-
i += 2;
|
|
267
|
-
const r = readName(selector, i);
|
|
268
|
-
i = r.end;
|
|
269
|
-
attribute = r.value;
|
|
270
|
-
} else {
|
|
271
|
-
const r = readName(selector, i);
|
|
272
|
-
i = r.end;
|
|
273
|
-
attribute = r.value;
|
|
274
|
-
if (selector.charCodeAt(i) === 124 && selector.charCodeAt(i + 1) !== 61) {
|
|
275
|
-
i++;
|
|
276
|
-
const r2 = readName(selector, i);
|
|
277
|
-
i = r2.end;
|
|
278
|
-
attribute = r2.value;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
i = stripWS(selector, i);
|
|
282
|
-
let action = "exists";
|
|
283
|
-
let value = "";
|
|
284
|
-
let ignoreCase = null;
|
|
285
|
-
const opCode = selector.charCodeAt(i);
|
|
286
|
-
if (opCode === 61) {
|
|
287
|
-
action = "equals";
|
|
288
|
-
i++;
|
|
289
|
-
} else if (opCode === 33 && selector.charCodeAt(i + 1) === 61) {
|
|
290
|
-
action = "not";
|
|
291
|
-
i += 2;
|
|
292
|
-
} else {
|
|
293
|
-
const a = actionFromChar(opCode);
|
|
294
|
-
if (a !== null && selector.charCodeAt(i + 1) === 61) {
|
|
295
|
-
action = a;
|
|
296
|
-
i += 2;
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
if (action !== "exists") {
|
|
300
|
-
i = stripWS(selector, i);
|
|
301
|
-
const q = selector.charCodeAt(i);
|
|
302
|
-
if (q === 34 || q === 39) {
|
|
303
|
-
const end = findEndOfString(selector, i + 1, q);
|
|
304
|
-
value = unescapeIfNeeded(selector.slice(i + 1, end));
|
|
305
|
-
i = end + 1;
|
|
306
|
-
} else {
|
|
307
|
-
const r = readName(selector, i);
|
|
308
|
-
value = r.value;
|
|
309
|
-
i = r.end;
|
|
310
|
-
}
|
|
311
|
-
i = stripWS(selector, i);
|
|
312
|
-
const flag = selector.charCodeAt(i);
|
|
313
|
-
if (flag === 105 || flag === 73) {
|
|
314
|
-
ignoreCase = true;
|
|
315
|
-
i++;
|
|
316
|
-
} else if (flag === 115 || flag === 83) {
|
|
317
|
-
ignoreCase = false;
|
|
318
|
-
i++;
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
if (selector.charCodeAt(i) !== 93)
|
|
322
|
-
throw new Error("Expected ]");
|
|
323
|
-
i++;
|
|
324
|
-
if (ignoreCase === null && !xmlMode && ATTRIBUTES_QUIRKS.has(attribute.toLowerCase()))
|
|
325
|
-
ignoreCase = "quirks";
|
|
326
|
-
tokens.push({
|
|
327
|
-
type: "attribute",
|
|
328
|
-
name: lowerCaseAttrs ? attribute.toLowerCase() : attribute,
|
|
329
|
-
action,
|
|
330
|
-
value,
|
|
331
|
-
namespace: null,
|
|
332
|
-
ignoreCase
|
|
333
|
-
});
|
|
334
|
-
return i;
|
|
335
|
-
}
|
|
336
|
-
function parsePseudo(selector, idx, tokens, options) {
|
|
337
|
-
if (selector.charCodeAt(idx + 1) === 58) {
|
|
338
|
-
let i2 = idx + 2;
|
|
339
|
-
const r2 = readName(selector, i2);
|
|
340
|
-
i2 = r2.end;
|
|
341
|
-
const name2 = r2.value.toLowerCase();
|
|
342
|
-
let data = null;
|
|
343
|
-
if (selector.charCodeAt(i2) === 40) {
|
|
344
|
-
const end = findClose(selector, i2);
|
|
345
|
-
data = selector.slice(i2 + 1, end).trim();
|
|
346
|
-
i2 = end + 1;
|
|
347
|
-
}
|
|
348
|
-
tokens.push({ type: "pseudo-element", name: name2, data });
|
|
349
|
-
return i2;
|
|
350
|
-
}
|
|
351
|
-
let i = idx + 1;
|
|
352
|
-
const r = readName(selector, i);
|
|
353
|
-
i = r.end;
|
|
354
|
-
const name = r.value.toLowerCase();
|
|
355
|
-
if (selector.charCodeAt(i) === 40) {
|
|
356
|
-
const end = findClose(selector, i);
|
|
357
|
-
const inner = selector.slice(i + 1, end);
|
|
358
|
-
i = end + 1;
|
|
359
|
-
if (name === "is" || name === "not" || name === "where" || name === "has" || name === "matches" || name === "-moz-any" || name === "-webkit-any") {
|
|
360
|
-
const sub = [];
|
|
361
|
-
parseSelectorImpl(sub, inner.trim(), options, 0);
|
|
362
|
-
tokens.push({ type: "pseudo", name, data: sub });
|
|
363
|
-
} else {
|
|
364
|
-
tokens.push({ type: "pseudo", name, data: inner.trim() });
|
|
365
|
-
}
|
|
366
|
-
} else {
|
|
367
|
-
tokens.push({ type: "pseudo", name, data: null });
|
|
368
|
-
}
|
|
369
|
-
return i;
|
|
370
|
-
}
|
|
371
|
-
function addTraversal(tokens, type) {
|
|
372
|
-
if (tokens.length > 0 && tokens[tokens.length - 1].type === "descendant" && type !== "descendant")
|
|
373
|
-
tokens.pop();
|
|
374
|
-
if (tokens.length > 0 && tokens[tokens.length - 1].type === type)
|
|
375
|
-
return;
|
|
376
|
-
tokens.push({ type });
|
|
377
|
-
}
|
|
378
|
-
function findEndOfString(selector, start, qCode) {
|
|
379
|
-
let i = start;
|
|
380
|
-
const len = selector.length;
|
|
381
|
-
while (i < len) {
|
|
382
|
-
const c = selector.charCodeAt(i);
|
|
383
|
-
if (c === 92) {
|
|
384
|
-
i += 2;
|
|
385
|
-
continue;
|
|
386
|
-
}
|
|
387
|
-
if (c === qCode)
|
|
388
|
-
return i;
|
|
389
|
-
i++;
|
|
390
|
-
}
|
|
391
|
-
throw new Error("Unterminated string");
|
|
392
|
-
}
|
|
393
|
-
function findClose(selector, openParen) {
|
|
394
|
-
let depth = 1;
|
|
395
|
-
let i = openParen + 1;
|
|
396
|
-
const len = selector.length;
|
|
397
|
-
while (i < len) {
|
|
398
|
-
const c = selector.charCodeAt(i);
|
|
399
|
-
if (c === 92) {
|
|
400
|
-
i += 2;
|
|
401
|
-
continue;
|
|
402
|
-
}
|
|
403
|
-
if (c === 34 || c === 39) {
|
|
404
|
-
i = findEndOfString(selector, i + 1, c) + 1;
|
|
405
|
-
continue;
|
|
406
|
-
}
|
|
407
|
-
if (c === 40)
|
|
408
|
-
depth++;
|
|
409
|
-
else if (c === 41) {
|
|
410
|
-
depth--;
|
|
411
|
-
if (depth === 0)
|
|
412
|
-
return i;
|
|
413
|
-
}
|
|
414
|
-
i++;
|
|
415
|
-
}
|
|
416
|
-
throw new Error("Unterminated parenthesis");
|
|
417
|
-
}
|
|
418
|
-
// src/what/stringify.ts
|
|
419
|
-
var COMBINATORS = {
|
|
420
|
-
child: " > ",
|
|
421
|
-
parent: " < ",
|
|
422
|
-
sibling: " ~ ",
|
|
423
|
-
adjacent: " + ",
|
|
424
|
-
descendant: " ",
|
|
425
|
-
"column-combinator": " || "
|
|
426
|
-
};
|
|
427
|
-
function stringify(selector) {
|
|
428
|
-
return selector.map(stringifySegments).join(", ");
|
|
429
|
-
}
|
|
430
|
-
function stringifySegments(tokens) {
|
|
431
|
-
return tokens.map((t, i) => stringifyOne(t, tokens[i - 1])).join("");
|
|
432
|
-
}
|
|
433
|
-
function stringifyOne(token, _prev) {
|
|
434
|
-
switch (token.type) {
|
|
435
|
-
case "tag":
|
|
436
|
-
return `${nsPrefix(token.namespace)}${escapeIdent(token.name)}`;
|
|
437
|
-
case "universal":
|
|
438
|
-
return `${nsPrefix(token.namespace)}*`;
|
|
439
|
-
case "attribute": {
|
|
440
|
-
if (token.name === "id" && token.action === "equals" && !token.ignoreCase && !token.namespace)
|
|
441
|
-
return `#${escapeIdent(token.value)}`;
|
|
442
|
-
if (token.name === "class" && token.action === "element" && !token.ignoreCase && !token.namespace)
|
|
443
|
-
return `.${escapeIdent(token.value)}`;
|
|
444
|
-
let out = `[${nsPrefix(token.namespace)}${escapeIdent(token.name)}`;
|
|
445
|
-
if (token.action !== "exists") {
|
|
446
|
-
const op = ACTION_OP[token.action] ?? "=";
|
|
447
|
-
out += op;
|
|
448
|
-
out += `"${token.value.replace(/"/g, "\\\"")}"`;
|
|
449
|
-
if (token.ignoreCase === true)
|
|
450
|
-
out += " i";
|
|
451
|
-
else if (token.ignoreCase === false)
|
|
452
|
-
out += " s";
|
|
453
|
-
}
|
|
454
|
-
out += "]";
|
|
455
|
-
return out;
|
|
456
|
-
}
|
|
457
|
-
case "pseudo":
|
|
458
|
-
if (token.data === null)
|
|
459
|
-
return `:${token.name}`;
|
|
460
|
-
if (typeof token.data === "string")
|
|
461
|
-
return `:${token.name}(${token.data})`;
|
|
462
|
-
return `:${token.name}(${stringify(token.data)})`;
|
|
463
|
-
case "pseudo-element":
|
|
464
|
-
return token.data === null ? `::${token.name}` : `::${token.name}(${token.data})`;
|
|
465
|
-
case "descendant":
|
|
466
|
-
case "child":
|
|
467
|
-
case "parent":
|
|
468
|
-
case "sibling":
|
|
469
|
-
case "adjacent":
|
|
470
|
-
case "column-combinator":
|
|
471
|
-
return COMBINATORS[token.type] ?? " ";
|
|
472
|
-
}
|
|
473
|
-
return "";
|
|
474
|
-
}
|
|
475
|
-
var ACTION_OP = {
|
|
476
|
-
equals: "=",
|
|
477
|
-
element: "~=",
|
|
478
|
-
start: "^=",
|
|
479
|
-
end: "$=",
|
|
480
|
-
any: "*=",
|
|
481
|
-
not: "!=",
|
|
482
|
-
hyphen: "|="
|
|
483
|
-
};
|
|
484
|
-
function nsPrefix(ns) {
|
|
485
|
-
if (ns === null)
|
|
486
|
-
return "";
|
|
487
|
-
if (ns === "")
|
|
488
|
-
return "|";
|
|
489
|
-
return `${escapeIdent(ns)}|`;
|
|
490
|
-
}
|
|
491
|
-
var RE_INVALID_ID_CHAR = /[^\w\u00B0-\uFFFF-]/g;
|
|
492
|
-
function escapeIdent(name) {
|
|
493
|
-
if (name === "")
|
|
494
|
-
return "";
|
|
495
|
-
return name.replace(RE_INVALID_ID_CHAR, (m) => `\\${m}`);
|
|
496
|
-
}
|
|
497
|
-
// src/what/traversal.ts
|
|
498
|
-
var TRAVERSAL_TYPES = new Set(["adjacent", "child", "descendant", "parent", "sibling", "column-combinator"]);
|
|
499
|
-
function isTraversal(token) {
|
|
500
|
-
return TRAVERSAL_TYPES.has(token.type);
|
|
501
|
-
}
|
|
502
|
-
// src/what/types.ts
|
|
503
|
-
var IgnoreCaseMode = {
|
|
504
|
-
Unknown: null,
|
|
505
|
-
QuirksMode: "quirks",
|
|
506
|
-
IgnoreCase: true,
|
|
507
|
-
CaseSensitive: false
|
|
508
|
-
};
|
|
509
|
-
// src/select/index.ts
|
|
510
|
-
var exports_select = {};
|
|
511
|
-
__export(exports_select, {
|
|
512
|
-
selectOne: () => selectOne,
|
|
513
|
-
selectAll: () => selectAll,
|
|
514
|
-
parseAndCompile: () => parseAndCompile,
|
|
515
|
-
is: () => is,
|
|
516
|
-
compile: () => compile,
|
|
517
|
-
clearSelectorCache: () => clearSelectorCache
|
|
518
|
-
});
|
|
519
|
-
|
|
520
|
-
// src/select/helpers/querying.ts
|
|
521
|
-
function findAll(query, nodes, adapter, xmlMode) {
|
|
522
|
-
const result = [];
|
|
523
|
-
const nodeStack = [nodes];
|
|
524
|
-
const indexStack = [0];
|
|
525
|
-
for (;; ) {
|
|
526
|
-
if (indexStack[0] >= nodeStack[0].length) {
|
|
527
|
-
if (nodeStack.length === 1)
|
|
528
|
-
return result;
|
|
529
|
-
nodeStack.shift();
|
|
530
|
-
indexStack.shift();
|
|
531
|
-
continue;
|
|
532
|
-
}
|
|
533
|
-
const element = nodeStack[0][indexStack[0]++];
|
|
534
|
-
if (!adapter.isTag(element))
|
|
535
|
-
continue;
|
|
536
|
-
if (query(element))
|
|
537
|
-
result.push(element);
|
|
538
|
-
if (xmlMode || adapter.getName(element) !== "template") {
|
|
539
|
-
const children = adapter.getChildren(element);
|
|
540
|
-
if (children.length > 0) {
|
|
541
|
-
nodeStack.unshift(children);
|
|
542
|
-
indexStack.unshift(0);
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
function findOne(query, nodes, adapter, xmlMode) {
|
|
548
|
-
const nodeStack = [nodes];
|
|
549
|
-
const indexStack = [0];
|
|
550
|
-
for (;; ) {
|
|
551
|
-
if (indexStack[0] >= nodeStack[0].length) {
|
|
552
|
-
if (nodeStack.length === 1)
|
|
553
|
-
return null;
|
|
554
|
-
nodeStack.shift();
|
|
555
|
-
indexStack.shift();
|
|
556
|
-
continue;
|
|
557
|
-
}
|
|
558
|
-
const element = nodeStack[0][indexStack[0]++];
|
|
559
|
-
if (!adapter.isTag(element))
|
|
560
|
-
continue;
|
|
561
|
-
if (query(element))
|
|
562
|
-
return element;
|
|
563
|
-
if (xmlMode || adapter.getName(element) !== "template") {
|
|
564
|
-
const children = adapter.getChildren(element);
|
|
565
|
-
if (children.length > 0) {
|
|
566
|
-
nodeStack.unshift(children);
|
|
567
|
-
indexStack.unshift(0);
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
function prepareContext(elements, adapter) {
|
|
573
|
-
if (Array.isArray(elements))
|
|
574
|
-
return adapter.removeSubsets(elements);
|
|
575
|
-
return adapter.getChildren(elements);
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
// src/select/helpers/always-true.ts
|
|
579
|
-
var ALWAYS_TRUE = (_) => {
|
|
580
|
-
return true;
|
|
581
|
-
};
|
|
582
|
-
|
|
583
|
-
// src/select/attributes.ts
|
|
584
|
-
function isWsCode2(c) {
|
|
585
|
-
return c === 32 || c === 9 || c === 10 || c === 12 || c === 13;
|
|
586
|
-
}
|
|
587
|
-
function containsWsToken(haystack, needle) {
|
|
588
|
-
const nlen = needle.length;
|
|
589
|
-
const hlen = haystack.length;
|
|
590
|
-
if (nlen === 0 || nlen > hlen)
|
|
591
|
-
return false;
|
|
592
|
-
let idx = haystack.indexOf(needle);
|
|
593
|
-
while (idx >= 0) {
|
|
594
|
-
const before = idx === 0 || isWsCode2(haystack.charCodeAt(idx - 1));
|
|
595
|
-
const afterIdx = idx + nlen;
|
|
596
|
-
const after = afterIdx === hlen || isWsCode2(haystack.charCodeAt(afterIdx));
|
|
597
|
-
if (before && after)
|
|
598
|
-
return true;
|
|
599
|
-
idx = haystack.indexOf(needle, idx + 1);
|
|
600
|
-
}
|
|
601
|
-
return false;
|
|
602
|
-
}
|
|
603
|
-
function caseInsensitive(value, attr, options) {
|
|
604
|
-
if (attr.ignoreCase === true)
|
|
605
|
-
return true;
|
|
606
|
-
if (attr.ignoreCase === false)
|
|
607
|
-
return false;
|
|
608
|
-
if (attr.ignoreCase === "quirks")
|
|
609
|
-
return !options.xmlMode;
|
|
610
|
-
return false;
|
|
611
|
-
}
|
|
612
|
-
function compileAttribute(selector, options, next) {
|
|
613
|
-
const adapter = options.adapter;
|
|
614
|
-
const action = selector.action;
|
|
615
|
-
const name = selector.name;
|
|
616
|
-
let value = selector.value;
|
|
617
|
-
const ci = caseInsensitive(value, selector, options);
|
|
618
|
-
if (ci)
|
|
619
|
-
value = value.toLowerCase();
|
|
620
|
-
return wrap(action, name, value, ci, adapter, next);
|
|
621
|
-
}
|
|
622
|
-
function wrap(action, name, value, ci, adapter, next) {
|
|
623
|
-
const isLeaf = next === ALWAYS_TRUE;
|
|
624
|
-
switch (action) {
|
|
625
|
-
case "exists":
|
|
626
|
-
if (isLeaf)
|
|
627
|
-
return (e) => adapter.hasAttrib(e, name);
|
|
628
|
-
return (e) => adapter.hasAttrib(e, name) && next(e);
|
|
629
|
-
case "equals":
|
|
630
|
-
if (!ci) {
|
|
631
|
-
if (isLeaf)
|
|
632
|
-
return (e) => adapter.getAttributeValue(e, name) === value;
|
|
633
|
-
return (e) => adapter.getAttributeValue(e, name) === value && next(e);
|
|
634
|
-
}
|
|
635
|
-
if (isLeaf) {
|
|
636
|
-
return (e) => {
|
|
637
|
-
const v = adapter.getAttributeValue(e, name);
|
|
638
|
-
if (v == null)
|
|
639
|
-
return false;
|
|
640
|
-
return v.toLowerCase() === value;
|
|
641
|
-
};
|
|
642
|
-
}
|
|
643
|
-
return (e) => {
|
|
644
|
-
const v = adapter.getAttributeValue(e, name);
|
|
645
|
-
if (v == null)
|
|
646
|
-
return false;
|
|
647
|
-
return v.toLowerCase() === value && next(e);
|
|
648
|
-
};
|
|
649
|
-
case "not":
|
|
650
|
-
return (e) => {
|
|
651
|
-
const v = adapter.getAttributeValue(e, name);
|
|
652
|
-
if (v == null)
|
|
653
|
-
return next(e);
|
|
654
|
-
return (ci ? v.toLowerCase() : v) !== value && next(e);
|
|
655
|
-
};
|
|
656
|
-
case "start":
|
|
657
|
-
if (value === "")
|
|
658
|
-
return () => false;
|
|
659
|
-
if (isLeaf) {
|
|
660
|
-
return (e) => {
|
|
661
|
-
const v = adapter.getAttributeValue(e, name);
|
|
662
|
-
if (v == null)
|
|
663
|
-
return false;
|
|
664
|
-
return (ci ? v.toLowerCase() : v).startsWith(value);
|
|
665
|
-
};
|
|
666
|
-
}
|
|
667
|
-
return (e) => {
|
|
668
|
-
const v = adapter.getAttributeValue(e, name);
|
|
669
|
-
if (v == null)
|
|
670
|
-
return false;
|
|
671
|
-
return (ci ? v.toLowerCase() : v).startsWith(value) && next(e);
|
|
672
|
-
};
|
|
673
|
-
case "end":
|
|
674
|
-
if (value === "")
|
|
675
|
-
return () => false;
|
|
676
|
-
if (isLeaf) {
|
|
677
|
-
return (e) => {
|
|
678
|
-
const v = adapter.getAttributeValue(e, name);
|
|
679
|
-
if (v == null)
|
|
680
|
-
return false;
|
|
681
|
-
return (ci ? v.toLowerCase() : v).endsWith(value);
|
|
682
|
-
};
|
|
683
|
-
}
|
|
684
|
-
return (e) => {
|
|
685
|
-
const v = adapter.getAttributeValue(e, name);
|
|
686
|
-
if (v == null)
|
|
687
|
-
return false;
|
|
688
|
-
return (ci ? v.toLowerCase() : v).endsWith(value) && next(e);
|
|
689
|
-
};
|
|
690
|
-
case "any": {
|
|
691
|
-
if (value === "")
|
|
692
|
-
return () => false;
|
|
693
|
-
if (ci) {
|
|
694
|
-
if (isLeaf) {
|
|
695
|
-
return (e) => {
|
|
696
|
-
const v = adapter.getAttributeValue(e, name);
|
|
697
|
-
if (v == null)
|
|
698
|
-
return false;
|
|
699
|
-
return v.toLowerCase().indexOf(value) >= 0;
|
|
700
|
-
};
|
|
701
|
-
}
|
|
702
|
-
return (e) => {
|
|
703
|
-
const v = adapter.getAttributeValue(e, name);
|
|
704
|
-
if (v == null)
|
|
705
|
-
return false;
|
|
706
|
-
return v.toLowerCase().indexOf(value) >= 0 && next(e);
|
|
707
|
-
};
|
|
708
|
-
}
|
|
709
|
-
if (isLeaf) {
|
|
710
|
-
return (e) => {
|
|
711
|
-
const v = adapter.getAttributeValue(e, name);
|
|
712
|
-
if (v == null)
|
|
713
|
-
return false;
|
|
714
|
-
return v.indexOf(value) >= 0;
|
|
715
|
-
};
|
|
716
|
-
}
|
|
717
|
-
return (e) => {
|
|
718
|
-
const v = adapter.getAttributeValue(e, name);
|
|
719
|
-
if (v == null)
|
|
720
|
-
return false;
|
|
721
|
-
return v.indexOf(value) >= 0 && next(e);
|
|
722
|
-
};
|
|
723
|
-
}
|
|
724
|
-
case "element": {
|
|
725
|
-
if (value === "" || /\s/.test(value))
|
|
726
|
-
return () => false;
|
|
727
|
-
if (ci) {
|
|
728
|
-
if (isLeaf) {
|
|
729
|
-
return (e) => {
|
|
730
|
-
const v = adapter.getAttributeValue(e, name);
|
|
731
|
-
if (v == null)
|
|
732
|
-
return false;
|
|
733
|
-
return containsWsToken(v.toLowerCase(), value);
|
|
734
|
-
};
|
|
735
|
-
}
|
|
736
|
-
return (e) => {
|
|
737
|
-
const v = adapter.getAttributeValue(e, name);
|
|
738
|
-
if (v == null)
|
|
739
|
-
return false;
|
|
740
|
-
return containsWsToken(v.toLowerCase(), value) && next(e);
|
|
741
|
-
};
|
|
742
|
-
}
|
|
743
|
-
if (isLeaf) {
|
|
744
|
-
return (e) => {
|
|
745
|
-
const v = adapter.getAttributeValue(e, name);
|
|
746
|
-
if (v == null)
|
|
747
|
-
return false;
|
|
748
|
-
return containsWsToken(v, value);
|
|
749
|
-
};
|
|
750
|
-
}
|
|
751
|
-
return (e) => {
|
|
752
|
-
const v = adapter.getAttributeValue(e, name);
|
|
753
|
-
if (v == null)
|
|
754
|
-
return false;
|
|
755
|
-
return containsWsToken(v, value) && next(e);
|
|
756
|
-
};
|
|
757
|
-
}
|
|
758
|
-
case "hyphen":
|
|
759
|
-
if (isLeaf) {
|
|
760
|
-
return (e) => {
|
|
761
|
-
const v = adapter.getAttributeValue(e, name);
|
|
762
|
-
if (v == null)
|
|
763
|
-
return false;
|
|
764
|
-
const lower = ci ? v.toLowerCase() : v;
|
|
765
|
-
if (lower === value)
|
|
766
|
-
return true;
|
|
767
|
-
if (lower.startsWith(value) && lower.charAt(value.length) === "-")
|
|
768
|
-
return true;
|
|
769
|
-
return false;
|
|
770
|
-
};
|
|
771
|
-
}
|
|
772
|
-
return (e) => {
|
|
773
|
-
const v = adapter.getAttributeValue(e, name);
|
|
774
|
-
if (v == null)
|
|
775
|
-
return false;
|
|
776
|
-
const lower = ci ? v.toLowerCase() : v;
|
|
777
|
-
if (lower === value)
|
|
778
|
-
return next(e);
|
|
779
|
-
if (lower.startsWith(value) && lower.charAt(value.length) === "-")
|
|
780
|
-
return next(e);
|
|
781
|
-
return false;
|
|
782
|
-
};
|
|
783
|
-
}
|
|
784
|
-
return (_) => {
|
|
785
|
-
return false;
|
|
786
|
-
};
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
// src/select/pseudo-selectors/index.ts
|
|
790
|
-
var STATEFUL_PSEUDOS = new Set([
|
|
791
|
-
"link",
|
|
792
|
-
"any-link",
|
|
793
|
-
"visited",
|
|
794
|
-
"hover",
|
|
795
|
-
"active",
|
|
796
|
-
"focus",
|
|
797
|
-
"focus-visible",
|
|
798
|
-
"focus-within",
|
|
799
|
-
"target",
|
|
800
|
-
"target-within",
|
|
801
|
-
"enabled",
|
|
802
|
-
"disabled",
|
|
803
|
-
"checked",
|
|
804
|
-
"required",
|
|
805
|
-
"optional",
|
|
806
|
-
"valid",
|
|
807
|
-
"invalid",
|
|
808
|
-
"selected",
|
|
809
|
-
"placeholder-shown",
|
|
810
|
-
"read-only",
|
|
811
|
-
"read-write",
|
|
812
|
-
"in-range",
|
|
813
|
-
"out-of-range",
|
|
814
|
-
"default",
|
|
815
|
-
"indeterminate"
|
|
816
|
-
]);
|
|
817
|
-
var SELECTOR_LIST_PSEUDOS = new Set([
|
|
818
|
-
"is",
|
|
819
|
-
"where",
|
|
820
|
-
"matches",
|
|
821
|
-
"-moz-any",
|
|
822
|
-
"-webkit-any"
|
|
823
|
-
]);
|
|
824
|
-
var NTH_PSEUDOS = new Set([
|
|
825
|
-
"nth-child",
|
|
826
|
-
"nth-last-child",
|
|
827
|
-
"nth-of-type",
|
|
828
|
-
"nth-last-of-type"
|
|
829
|
-
]);
|
|
830
|
-
function compilePseudo(token, options, next) {
|
|
831
|
-
const adapter = options.adapter;
|
|
832
|
-
const name = token.name;
|
|
833
|
-
const data = token.data;
|
|
834
|
-
if (token.type === "pseudo" && SELECTOR_LIST_PSEUDOS.has(name)) {
|
|
835
|
-
if (!Array.isArray(data))
|
|
836
|
-
return next;
|
|
837
|
-
const tests = data.map((seg) => parseAndCompile(seg, options));
|
|
838
|
-
if (tests.length === 1) {
|
|
839
|
-
const t0 = tests[0];
|
|
840
|
-
return (e) => t0(e) && next(e);
|
|
841
|
-
}
|
|
842
|
-
return (e) => {
|
|
843
|
-
for (const t of tests) {
|
|
844
|
-
if (t(e))
|
|
845
|
-
return next(e);
|
|
846
|
-
}
|
|
847
|
-
return false;
|
|
848
|
-
};
|
|
849
|
-
}
|
|
850
|
-
if (token.type === "pseudo" && name === "not") {
|
|
851
|
-
if (!Array.isArray(data))
|
|
852
|
-
return next;
|
|
853
|
-
const tests = data.map((seg) => parseAndCompile(seg, options));
|
|
854
|
-
if (tests.length === 1) {
|
|
855
|
-
const t0 = tests[0];
|
|
856
|
-
return (e) => !t0(e) && next(e);
|
|
857
|
-
}
|
|
858
|
-
return (e) => {
|
|
859
|
-
for (const t of tests) {
|
|
860
|
-
if (t(e))
|
|
861
|
-
return false;
|
|
862
|
-
}
|
|
863
|
-
return next(e);
|
|
864
|
-
};
|
|
865
|
-
}
|
|
866
|
-
if (token.type === "pseudo" && name === "has") {
|
|
867
|
-
if (!Array.isArray(data))
|
|
868
|
-
return next;
|
|
869
|
-
const tests = data.map((seg) => parseAndCompile(seg, options));
|
|
870
|
-
const subjectTest = tests.length === 1 ? tests[0] : (node) => {
|
|
871
|
-
for (const t of tests) {
|
|
872
|
-
if (t(node))
|
|
873
|
-
return true;
|
|
874
|
-
}
|
|
875
|
-
return false;
|
|
876
|
-
};
|
|
877
|
-
return (e) => {
|
|
878
|
-
const stack = adapter.getChildren(e).slice();
|
|
879
|
-
while (stack.length > 0) {
|
|
880
|
-
const cur = stack.pop();
|
|
881
|
-
if (!adapter.isTag(cur))
|
|
882
|
-
continue;
|
|
883
|
-
if (subjectTest(cur))
|
|
884
|
-
return next(e);
|
|
885
|
-
const kids = adapter.getChildren(cur);
|
|
886
|
-
for (let i = 0;i < kids.length; i++)
|
|
887
|
-
stack.push(kids[i]);
|
|
888
|
-
}
|
|
889
|
-
return false;
|
|
890
|
-
};
|
|
891
|
-
}
|
|
892
|
-
if (token.type === "pseudo" && NTH_PSEUDOS.has(name)) {
|
|
893
|
-
const fn = compileNth(typeof data === "string" ? data : "", name, options);
|
|
894
|
-
return (e) => fn(e) && next(e);
|
|
895
|
-
}
|
|
896
|
-
if (token.type === "pseudo" && name === "first-child") {
|
|
897
|
-
return (e) => {
|
|
898
|
-
const sibs = adapter.getSiblings(e);
|
|
899
|
-
for (const s of sibs) {
|
|
900
|
-
if (!adapter.isTag(s))
|
|
901
|
-
continue;
|
|
902
|
-
return s === e && next(e);
|
|
903
|
-
}
|
|
904
|
-
return false;
|
|
905
|
-
};
|
|
906
|
-
}
|
|
907
|
-
if (token.type === "pseudo" && name === "last-child") {
|
|
908
|
-
return (e) => {
|
|
909
|
-
const sibs = adapter.getSiblings(e);
|
|
910
|
-
for (let i = sibs.length - 1;i >= 0; i--) {
|
|
911
|
-
const s = sibs[i];
|
|
912
|
-
if (!adapter.isTag(s))
|
|
913
|
-
continue;
|
|
914
|
-
return s === e && next(e);
|
|
915
|
-
}
|
|
916
|
-
return false;
|
|
917
|
-
};
|
|
918
|
-
}
|
|
919
|
-
if (token.type === "pseudo" && name === "first-of-type") {
|
|
920
|
-
return (e) => {
|
|
921
|
-
const tag = adapter.getName(e);
|
|
922
|
-
const sibs = adapter.getSiblings(e);
|
|
923
|
-
for (const s of sibs) {
|
|
924
|
-
if (!adapter.isTag(s) || adapter.getName(s) !== tag)
|
|
925
|
-
continue;
|
|
926
|
-
return s === e && next(e);
|
|
927
|
-
}
|
|
928
|
-
return false;
|
|
929
|
-
};
|
|
930
|
-
}
|
|
931
|
-
if (token.type === "pseudo" && name === "last-of-type") {
|
|
932
|
-
return (e) => {
|
|
933
|
-
const tag = adapter.getName(e);
|
|
934
|
-
const sibs = adapter.getSiblings(e);
|
|
935
|
-
for (let i = sibs.length - 1;i >= 0; i--) {
|
|
936
|
-
const s = sibs[i];
|
|
937
|
-
if (!adapter.isTag(s) || adapter.getName(s) !== tag)
|
|
938
|
-
continue;
|
|
939
|
-
return s === e && next(e);
|
|
940
|
-
}
|
|
941
|
-
return false;
|
|
942
|
-
};
|
|
943
|
-
}
|
|
944
|
-
if (token.type === "pseudo" && name === "only-child") {
|
|
945
|
-
return (e) => {
|
|
946
|
-
const sibs = adapter.getSiblings(e);
|
|
947
|
-
let count = 0;
|
|
948
|
-
let found = false;
|
|
949
|
-
for (const s of sibs) {
|
|
950
|
-
if (!adapter.isTag(s))
|
|
951
|
-
continue;
|
|
952
|
-
count++;
|
|
953
|
-
if (count > 1)
|
|
954
|
-
return false;
|
|
955
|
-
if (s === e)
|
|
956
|
-
found = true;
|
|
957
|
-
}
|
|
958
|
-
return count === 1 && found && next(e);
|
|
959
|
-
};
|
|
960
|
-
}
|
|
961
|
-
if (token.type === "pseudo" && name === "only-of-type") {
|
|
962
|
-
return (e) => {
|
|
963
|
-
const tag = adapter.getName(e);
|
|
964
|
-
const sibs = adapter.getSiblings(e);
|
|
965
|
-
let count = 0;
|
|
966
|
-
let found = false;
|
|
967
|
-
for (const s of sibs) {
|
|
968
|
-
if (!adapter.isTag(s) || adapter.getName(s) !== tag)
|
|
969
|
-
continue;
|
|
970
|
-
count++;
|
|
971
|
-
if (count > 1)
|
|
972
|
-
return false;
|
|
973
|
-
if (s === e)
|
|
974
|
-
found = true;
|
|
975
|
-
}
|
|
976
|
-
return count === 1 && found && next(e);
|
|
977
|
-
};
|
|
978
|
-
}
|
|
979
|
-
if (token.type === "pseudo" && name === "empty") {
|
|
980
|
-
return (e) => adapter.getChildren(e).length === 0 && next(e);
|
|
981
|
-
}
|
|
982
|
-
if (token.type === "pseudo" && name === "root") {
|
|
983
|
-
return (e) => {
|
|
984
|
-
const p = adapter.getParent(e);
|
|
985
|
-
return (p == null || !adapter.isTag(p)) && next(e);
|
|
986
|
-
};
|
|
987
|
-
}
|
|
988
|
-
if (token.type === "pseudo" && name === "scope") {
|
|
989
|
-
if (options.context && Array.isArray(options.context)) {
|
|
990
|
-
const ctx = new Set(options.context);
|
|
991
|
-
return (e) => ctx.has(e) && next(e);
|
|
992
|
-
}
|
|
993
|
-
if (options.context) {
|
|
994
|
-
const ctx = options.context;
|
|
995
|
-
return (e) => e === ctx && next(e);
|
|
996
|
-
}
|
|
997
|
-
return (e) => {
|
|
998
|
-
const p = adapter.getParent(e);
|
|
999
|
-
return (p == null || !adapter.isTag(p)) && next(e);
|
|
1000
|
-
};
|
|
1001
|
-
}
|
|
1002
|
-
if (token.type === "pseudo" && STATEFUL_PSEUDOS.has(name)) {
|
|
1003
|
-
if (options.pseudos && options.pseudos[name]) {
|
|
1004
|
-
const ext = options.pseudos[name];
|
|
1005
|
-
if (typeof ext === "function")
|
|
1006
|
-
return (e) => ext(e, undefined) && next(e);
|
|
1007
|
-
}
|
|
1008
|
-
return (_) => {
|
|
1009
|
-
return false;
|
|
1010
|
-
};
|
|
1011
|
-
}
|
|
1012
|
-
if (token.type === "pseudo-element") {
|
|
1013
|
-
return next;
|
|
1014
|
-
}
|
|
1015
|
-
if (options.pseudos && options.pseudos[name]) {
|
|
1016
|
-
const ext = options.pseudos[name];
|
|
1017
|
-
if (typeof ext === "function") {
|
|
1018
|
-
return (e) => ext(e, typeof data === "string" ? data : undefined) && next(e);
|
|
1019
|
-
}
|
|
1020
|
-
if (typeof ext === "string") {
|
|
1021
|
-
const aliasGroups = parse(ext);
|
|
1022
|
-
const re = compileGeneric(aliasGroups, options);
|
|
1023
|
-
return (e) => re(e) && next(e);
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
return (_) => {
|
|
1027
|
-
return false;
|
|
1028
|
-
};
|
|
1029
|
-
}
|
|
1030
|
-
function parseAnPlusB(s) {
|
|
1031
|
-
const trimmed = s.trim();
|
|
1032
|
-
if (trimmed === "odd")
|
|
1033
|
-
return { a: 2, b: 1 };
|
|
1034
|
-
if (trimmed === "even")
|
|
1035
|
-
return { a: 2, b: 0 };
|
|
1036
|
-
const m = /^([+-]?\d*)n([+-]\d+)?$/.exec(trimmed.replace(/\s+/g, ""));
|
|
1037
|
-
if (m) {
|
|
1038
|
-
const a = m[1] === "" || m[1] === "+" ? 1 : m[1] === "-" ? -1 : Number(m[1]);
|
|
1039
|
-
const b = m[2] ? Number(m[2]) : 0;
|
|
1040
|
-
return { a, b };
|
|
1041
|
-
}
|
|
1042
|
-
const num = Number(trimmed);
|
|
1043
|
-
if (!Number.isNaN(num))
|
|
1044
|
-
return { a: 0, b: num };
|
|
1045
|
-
return null;
|
|
1046
|
-
}
|
|
1047
|
-
function compileNth(arg, kind, options) {
|
|
1048
|
-
const ab = parseAnPlusB(arg);
|
|
1049
|
-
if (!ab)
|
|
1050
|
-
return () => false;
|
|
1051
|
-
const { a, b } = ab;
|
|
1052
|
-
const adapter = options.adapter;
|
|
1053
|
-
const ofType = kind === "nth-of-type" || kind === "nth-last-of-type";
|
|
1054
|
-
const reverse = kind === "nth-last-child" || kind === "nth-last-of-type";
|
|
1055
|
-
return (e) => {
|
|
1056
|
-
const sibs = adapter.getSiblings(e);
|
|
1057
|
-
const tag = ofType ? adapter.getName(e) : "";
|
|
1058
|
-
let raw = 0;
|
|
1059
|
-
let total = 0;
|
|
1060
|
-
for (const s of sibs) {
|
|
1061
|
-
if (!adapter.isTag(s))
|
|
1062
|
-
continue;
|
|
1063
|
-
if (ofType && adapter.getName(s) !== tag)
|
|
1064
|
-
continue;
|
|
1065
|
-
total++;
|
|
1066
|
-
if (s === e)
|
|
1067
|
-
raw = total;
|
|
1068
|
-
}
|
|
1069
|
-
if (raw === 0)
|
|
1070
|
-
return false;
|
|
1071
|
-
const idx = reverse ? total - raw + 1 : raw;
|
|
1072
|
-
if (a === 0)
|
|
1073
|
-
return idx === b;
|
|
1074
|
-
return (idx - b) / a >= 0 && (idx - b) % a === 0;
|
|
1075
|
-
};
|
|
1076
|
-
}
|
|
1077
|
-
|
|
1078
|
-
// src/select/general.ts
|
|
1079
|
-
function compileToken(token, options, next) {
|
|
1080
|
-
const adapter = options.adapter;
|
|
1081
|
-
const isLeaf = next === ALWAYS_TRUE;
|
|
1082
|
-
switch (token.type) {
|
|
1083
|
-
case "tag": {
|
|
1084
|
-
const name = options.lowerCaseTags !== false && !options.xmlMode ? token.name.toLowerCase() : token.name;
|
|
1085
|
-
if (isLeaf)
|
|
1086
|
-
return (e) => adapter.getName(e) === name;
|
|
1087
|
-
return (e) => adapter.getName(e) === name && next(e);
|
|
1088
|
-
}
|
|
1089
|
-
case "universal":
|
|
1090
|
-
return next;
|
|
1091
|
-
case "attribute":
|
|
1092
|
-
return compileAttribute(token, options, next);
|
|
1093
|
-
case "pseudo":
|
|
1094
|
-
case "pseudo-element":
|
|
1095
|
-
return compilePseudo(token, options, next);
|
|
1096
|
-
case "descendant":
|
|
1097
|
-
case "child":
|
|
1098
|
-
case "parent":
|
|
1099
|
-
case "sibling":
|
|
1100
|
-
case "adjacent":
|
|
1101
|
-
case "column-combinator":
|
|
1102
|
-
return next;
|
|
1103
|
-
}
|
|
1104
|
-
return (_) => {
|
|
1105
|
-
return false;
|
|
1106
|
-
};
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
// src/select/pseudo-selectors/selectors.ts
|
|
1110
|
-
var PROCEDURE = {
|
|
1111
|
-
universal: 50,
|
|
1112
|
-
tag: 30,
|
|
1113
|
-
attribute: 1,
|
|
1114
|
-
pseudo: 0,
|
|
1115
|
-
"pseudo-element": 0
|
|
1116
|
-
};
|
|
1117
|
-
var ATTRIBUTE_PROCEDURE = {
|
|
1118
|
-
exists: 10,
|
|
1119
|
-
equals: 8,
|
|
1120
|
-
not: 7,
|
|
1121
|
-
start: 6,
|
|
1122
|
-
end: 6,
|
|
1123
|
-
hyphen: 5,
|
|
1124
|
-
any: 4,
|
|
1125
|
-
element: 3
|
|
1126
|
-
};
|
|
1127
|
-
function tokenProcedure(t) {
|
|
1128
|
-
const base = PROCEDURE[t.type] ?? 0;
|
|
1129
|
-
if (t.type === "attribute")
|
|
1130
|
-
return base + (ATTRIBUTE_PROCEDURE[t.action] ?? 0);
|
|
1131
|
-
return base;
|
|
1132
|
-
}
|
|
1133
|
-
function compileGeneric(segments, options) {
|
|
1134
|
-
if (segments.length === 1)
|
|
1135
|
-
return parseAndCompile(segments[0], options);
|
|
1136
|
-
const compiled = segments.map((seg) => parseAndCompile(seg, options));
|
|
1137
|
-
return (e) => compiled.some((c) => c(e));
|
|
1138
|
-
}
|
|
1139
|
-
function parseAndCompile(tokens, options) {
|
|
1140
|
-
const adapter = options.adapter;
|
|
1141
|
-
const compounds = [[]];
|
|
1142
|
-
const combs = [];
|
|
1143
|
-
for (const t of tokens) {
|
|
1144
|
-
if (t.type === "descendant" || t.type === "child" || t.type === "sibling" || t.type === "adjacent" || t.type === "parent" || t.type === "column-combinator") {
|
|
1145
|
-
combs.push(t);
|
|
1146
|
-
compounds.push([]);
|
|
1147
|
-
} else {
|
|
1148
|
-
compounds[compounds.length - 1].push(t);
|
|
1149
|
-
}
|
|
1150
|
-
}
|
|
1151
|
-
let func = ALWAYS_TRUE;
|
|
1152
|
-
for (let ci = 0;ci < compounds.length; ci++) {
|
|
1153
|
-
const compound = compounds[ci].slice().sort((a, b) => tokenProcedure(a) - tokenProcedure(b));
|
|
1154
|
-
for (const t of compound)
|
|
1155
|
-
func = compileToken(t, options, func);
|
|
1156
|
-
if (ci < combs.length)
|
|
1157
|
-
func = wrapCombinator(combs[ci], func, adapter);
|
|
1158
|
-
}
|
|
1159
|
-
return func;
|
|
1160
|
-
}
|
|
1161
|
-
function wrapCombinator(comb, after, adapter) {
|
|
1162
|
-
switch (comb.type) {
|
|
1163
|
-
case "descendant":
|
|
1164
|
-
case "column-combinator":
|
|
1165
|
-
return (elem) => {
|
|
1166
|
-
let p = adapter.getParent(elem);
|
|
1167
|
-
while (p) {
|
|
1168
|
-
if (adapter.isTag(p) && after(p))
|
|
1169
|
-
return true;
|
|
1170
|
-
p = adapter.getParent(p);
|
|
1171
|
-
}
|
|
1172
|
-
return false;
|
|
1173
|
-
};
|
|
1174
|
-
case "child":
|
|
1175
|
-
return (elem) => {
|
|
1176
|
-
const p = adapter.getParent(elem);
|
|
1177
|
-
return p != null && adapter.isTag(p) && after(p);
|
|
1178
|
-
};
|
|
1179
|
-
case "sibling":
|
|
1180
|
-
return (elem) => {
|
|
1181
|
-
const sibs = adapter.getSiblings(elem);
|
|
1182
|
-
for (const s of sibs) {
|
|
1183
|
-
if (s === elem)
|
|
1184
|
-
return false;
|
|
1185
|
-
if (adapter.isTag(s) && after(s))
|
|
1186
|
-
return true;
|
|
1187
|
-
}
|
|
1188
|
-
return false;
|
|
1189
|
-
};
|
|
1190
|
-
case "adjacent":
|
|
1191
|
-
return (elem) => {
|
|
1192
|
-
const sibs = adapter.getSiblings(elem);
|
|
1193
|
-
let prev = null;
|
|
1194
|
-
for (const s of sibs) {
|
|
1195
|
-
if (s === elem)
|
|
1196
|
-
return prev != null && adapter.isTag(prev) && after(prev);
|
|
1197
|
-
prev = s;
|
|
1198
|
-
}
|
|
1199
|
-
return false;
|
|
1200
|
-
};
|
|
1201
|
-
case "parent":
|
|
1202
|
-
return (elem) => adapter.existsOne(after, adapter.getChildren(elem));
|
|
1203
|
-
}
|
|
1204
|
-
return (_) => {
|
|
1205
|
-
return false;
|
|
1206
|
-
};
|
|
1207
|
-
}
|
|
1208
|
-
|
|
1209
|
-
// src/select/index.ts
|
|
1210
|
-
function getAdapter(options) {
|
|
1211
|
-
if (!options || !options.adapter)
|
|
1212
|
-
throw new Error("css-select: adapter is required");
|
|
1213
|
-
return options.adapter;
|
|
1214
|
-
}
|
|
1215
|
-
var adapterCaches = new WeakMap;
|
|
1216
|
-
function flagSig(options) {
|
|
1217
|
-
return (options.xmlMode ? 1 : 0) << 0 | (options.lowerCaseAttributeNames === false ? 1 : 0) << 1 | (options.lowerCaseTags === false ? 1 : 0) << 2;
|
|
1218
|
-
}
|
|
1219
|
-
function getCache(adapter, sig) {
|
|
1220
|
-
let bucket = adapterCaches.get(adapter);
|
|
1221
|
-
if (!bucket) {
|
|
1222
|
-
bucket = [];
|
|
1223
|
-
adapterCaches.set(adapter, bucket);
|
|
1224
|
-
}
|
|
1225
|
-
let m = bucket[sig];
|
|
1226
|
-
if (!m) {
|
|
1227
|
-
m = new Map;
|
|
1228
|
-
bucket[sig] = m;
|
|
1229
|
-
}
|
|
1230
|
-
return m;
|
|
1231
|
-
}
|
|
1232
|
-
function compileSelectorString(selector, options) {
|
|
1233
|
-
const ast = parse(selector, {
|
|
1234
|
-
xmlMode: options.xmlMode,
|
|
1235
|
-
lowerCaseAttributeNames: options.lowerCaseAttributeNames,
|
|
1236
|
-
lowerCaseTags: options.lowerCaseTags
|
|
1237
|
-
});
|
|
1238
|
-
return compileGeneric(ast, options);
|
|
1239
|
-
}
|
|
1240
|
-
function compile(selector, options) {
|
|
1241
|
-
if (typeof selector !== "string")
|
|
1242
|
-
return compileGeneric(selector, options);
|
|
1243
|
-
if (options.cacheResults === false || !options.adapter)
|
|
1244
|
-
return compileSelectorString(selector, options);
|
|
1245
|
-
const cache = getCache(options.adapter, flagSig(options));
|
|
1246
|
-
let compiled = cache.get(selector);
|
|
1247
|
-
if (compiled !== undefined)
|
|
1248
|
-
return compiled;
|
|
1249
|
-
compiled = compileSelectorString(selector, options);
|
|
1250
|
-
cache.set(selector, compiled);
|
|
1251
|
-
return compiled;
|
|
1252
|
-
}
|
|
1253
|
-
function clearSelectorCache(adapter, selector) {
|
|
1254
|
-
if (selector === undefined) {
|
|
1255
|
-
adapterCaches.delete(adapter);
|
|
1256
|
-
return;
|
|
1257
|
-
}
|
|
1258
|
-
const bucket = adapterCaches.get(adapter);
|
|
1259
|
-
if (!bucket)
|
|
1260
|
-
return;
|
|
1261
|
-
for (const m of bucket) {
|
|
1262
|
-
if (m)
|
|
1263
|
-
m.delete(selector);
|
|
1264
|
-
}
|
|
1265
|
-
}
|
|
1266
|
-
function selectAll(selector, root, options) {
|
|
1267
|
-
const adapter = getAdapter(options);
|
|
1268
|
-
const test = typeof selector === "function" ? selector : compile(selector, options);
|
|
1269
|
-
const ctx = prepareContext(root, adapter);
|
|
1270
|
-
return findAll(test, ctx, adapter, options.xmlMode === true);
|
|
1271
|
-
}
|
|
1272
|
-
function selectOne(selector, root, options) {
|
|
1273
|
-
const adapter = getAdapter(options);
|
|
1274
|
-
const test = typeof selector === "function" ? selector : compile(selector, options);
|
|
1275
|
-
const ctx = prepareContext(root, adapter);
|
|
1276
|
-
return findOne(test, ctx, adapter, options.xmlMode === true);
|
|
1277
|
-
}
|
|
1278
|
-
function is(node, selector, options) {
|
|
1279
|
-
const test = typeof selector === "function" ? selector : compile(selector, options);
|
|
1280
|
-
return test(node);
|
|
1281
|
-
}
|
|
1282
|
-
export {
|
|
1283
|
-
selectOne,
|
|
1284
|
-
selectAll,
|
|
1285
|
-
parseAndCompile,
|
|
1286
|
-
is,
|
|
1287
|
-
compile,
|
|
1288
|
-
clearSelectorCache
|
|
1289
|
-
};
|
|
2
|
+
var s=Object.defineProperty;var a=(X)=>X;function n(X,J){this[X]=a.bind(null,J)}var h=(X,J)=>{for(var Z in J)s(X,Z,{get:J[Z],enumerable:!0,configurable:!0,set:n.bind(J,Z)})};var IX=import.meta.require;var BX={};h(BX,{stringify:()=>A,parse:()=>P,isTraversal:()=>v,IgnoreCaseMode:()=>x});var C=/(?:\\(?:[\dA-Fa-f]{1,6} ?|[^])|[\w\-\u00B0-\uFFFF])+/y,o=/\\([\dA-Fa-f]{1,6} ?|[^])/g;function t(X){return X.replace(o,(J,Z)=>{if(Z.length>1&&/^[\dA-Fa-f]/.test(Z)){let Q=Number.parseInt(Z,16);if(Q>=55296&&Q<=57343)return"\uFFFD";return String.fromCodePoint(Q)}return Z})}function b(X){return X.indexOf("\\")<0?X:t(X)}var e=new Set(["accept","accept-charset","align","alink","axis","bgcolor","charset","checked","clear","codetype","color","compact","declare","defer","dir","direction","disabled","enctype","face","frame","hreflang","http-equiv","lang","language","link","media","method","multiple","nohref","noresize","noshade","nowrap","readonly","rel","rev","rules","scope","scrolling","selected","shape","target","text","type","valign","valuetype","vlink"]);function XX(X){switch(X){case 126:return"element";case 94:return"start";case 36:return"end";case 42:return"any";case 33:return"not";case 124:return"hyphen";default:return null}}function y(X){return X===32||X===9||X===10||X===13||X===12}function P(X,J={}){let Z=[],Q=g(Z,X,J,0);if(Q<X.length)throw Error(`Unmatched selector: ${X.slice(Q)}`);return Z}function U(X,J){C.lastIndex=J;let Z=C.exec(X);if(!Z)throw Error(`Expected name, found ${X.slice(J)}`);return{value:b(Z[0]),end:J+Z[0].length}}function O(X,J){while(J<X.length&&y(X.charCodeAt(J)))J++;return J}function g(X,J,Z,Q){let H=[],V=O(J,Q),z=J.length,$=Z.xmlMode===!0,K=Z.lowerCaseAttributeNames!==!1&&!$,B=Z.lowerCaseTags!==!1;while(V<z){let G=J.charCodeAt(V);if(y(G)){let j=V+1;while(j<z&&y(J.charCodeAt(j)))j++;if(H.length===0)return j;V=j,N(H,"descendant");continue}if(G===62||G===60||G===126||G===43||G===124){let j=V+1;while(j<z&&y(J.charCodeAt(j)))j++;switch(V=j,G){case 62:N(H,"child");break;case 60:N(H,"parent");break;case 126:N(H,"sibling");break;case 43:N(H,"adjacent");break;case 124:if(V<z&&J.charCodeAt(V)===124)V++,V=O(J,V),N(H,"column-combinator");else H.push({type:"tag",name:"",namespace:""});break}continue}if(G===44){if(H.length===0)throw Error("Empty sub-selector");X.push(H),H=[],V=O(J,V+1);continue}if(G===47&&J.charCodeAt(V+1)===42){let j=J.indexOf("*/",V+2);if(j<0)throw Error("Unmatched comment");V=O(J,j+2);continue}if(G===42){V++,H.push({type:"universal",namespace:null});continue}if(G===35){let j=U(J,V+1);V=j.end,H.push({type:"attribute",name:"id",action:"equals",value:j.value,namespace:null,ignoreCase:!1});continue}if(G===46){let j=U(J,V+1);V=j.end,H.push({type:"attribute",name:"class",action:"element",value:j.value,namespace:null,ignoreCase:!1});continue}if(G===91){V=zX(J,V,H,Z,$,K);continue}if(G===58){V=JX(J,V,H,Z);continue}if(G===124){V++;let j=U(J,V);V=j.end,H.push({type:"tag",name:B?j.value.toLowerCase():j.value,namespace:""});continue}{let j=U(J,V);if(V=j.end,V<z&&J.charCodeAt(V)===124&&J.charCodeAt(V+1)!==61){V++;let Y=U(J,V);V=Y.end,H.push({type:"tag",name:B?Y.value.toLowerCase():Y.value,namespace:j.value})}else H.push({type:"tag",name:B?j.value.toLowerCase():j.value,namespace:null})}}if(H.length>0)X.push(H);return V}function zX(X,J,Z,Q,H,V){let z=J+1,$=X.length,K;if(X.charCodeAt(z)===124)throw Error("Empty namespace not supported");if(X.charCodeAt(z)===42&&X.charCodeAt(z+1)===124){z+=2;let D=U(X,z);z=D.end,K=D.value}else{let D=U(X,z);if(z=D.end,K=D.value,X.charCodeAt(z)===124&&X.charCodeAt(z+1)!==61){z++;let F=U(X,z);z=F.end,K=F.value}}z=O(X,z);let B="exists",G="",j=null,Y=X.charCodeAt(z);if(Y===61)B="equals",z++;else if(Y===33&&X.charCodeAt(z+1)===61)B="not",z+=2;else{let D=XX(Y);if(D!==null&&X.charCodeAt(z+1)===61)B=D,z+=2}if(B!=="exists"){z=O(X,z);let D=X.charCodeAt(z);if(D===34||D===39){let q=f(X,z+1,D);G=b(X.slice(z+1,q)),z=q+1}else{let q=U(X,z);G=q.value,z=q.end}z=O(X,z);let F=X.charCodeAt(z);if(F===105||F===73)j=!0,z++;else if(F===115||F===83)j=!1,z++}if(X.charCodeAt(z)!==93)throw Error("Expected ]");if(z++,j===null&&!H&&e.has(K.toLowerCase()))j="quirks";return Z.push({type:"attribute",name:V?K.toLowerCase():K,action:B,value:G,namespace:null,ignoreCase:j}),z}function JX(X,J,Z,Q){if(X.charCodeAt(J+1)===58){let $=J+2,K=U(X,$);$=K.end;let B=K.value.toLowerCase(),G=null;if(X.charCodeAt($)===40){let j=S(X,$);G=X.slice($+1,j).trim(),$=j+1}return Z.push({type:"pseudo-element",name:B,data:G}),$}let H=J+1,V=U(X,H);H=V.end;let z=V.value.toLowerCase();if(X.charCodeAt(H)===40){let $=S(X,H),K=X.slice(H+1,$);if(H=$+1,z==="is"||z==="not"||z==="where"||z==="has"||z==="matches"||z==="-moz-any"||z==="-webkit-any"){let B=[];g(B,K.trim(),Q,0),Z.push({type:"pseudo",name:z,data:B})}else Z.push({type:"pseudo",name:z,data:K.trim()})}else Z.push({type:"pseudo",name:z,data:null});return H}function N(X,J){if(X.length>0&&X[X.length-1].type==="descendant"&&J!=="descendant")X.pop();if(X.length>0&&X[X.length-1].type===J)return;X.push({type:J})}function f(X,J,Z){let Q=J,H=X.length;while(Q<H){let V=X.charCodeAt(Q);if(V===92){Q+=2;continue}if(V===Z)return Q;Q++}throw Error("Unterminated string")}function S(X,J){let Z=1,Q=J+1,H=X.length;while(Q<H){let V=X.charCodeAt(Q);if(V===92){Q+=2;continue}if(V===34||V===39){Q=f(X,Q+1,V)+1;continue}if(V===40)Z++;else if(V===41){if(Z--,Z===0)return Q}Q++}throw Error("Unterminated parenthesis")}var ZX={child:" > ",parent:" < ",sibling:" ~ ",adjacent:" + ",descendant:" ","column-combinator":" || "};function A(X){return X.map($X).join(", ")}function $X(X){return X.map((J,Z)=>QX(J,X[Z-1])).join("")}function QX(X,J){switch(X.type){case"tag":return`${_(X.namespace)}${W(X.name)}`;case"universal":return`${_(X.namespace)}*`;case"attribute":{if(X.name==="id"&&X.action==="equals"&&!X.ignoreCase&&!X.namespace)return`#${W(X.value)}`;if(X.name==="class"&&X.action==="element"&&!X.ignoreCase&&!X.namespace)return`.${W(X.value)}`;let Z=`[${_(X.namespace)}${W(X.name)}`;if(X.action!=="exists"){let Q=HX[X.action]??"=";if(Z+=Q,Z+=`"${X.value.replace(/"/g,"\\\"")}"`,X.ignoreCase===!0)Z+=" i";else if(X.ignoreCase===!1)Z+=" s"}return Z+="]",Z}case"pseudo":if(X.data===null)return`:${X.name}`;if(typeof X.data==="string")return`:${X.name}(${X.data})`;return`:${X.name}(${A(X.data)})`;case"pseudo-element":return X.data===null?`::${X.name}`:`::${X.name}(${X.data})`;case"descendant":case"child":case"parent":case"sibling":case"adjacent":case"column-combinator":return ZX[X.type]??" "}return""}var HX={equals:"=",element:"~=",start:"^=",end:"$=",any:"*=",not:"!=",hyphen:"|="};function _(X){if(X===null)return"";if(X==="")return"|";return`${W(X)}|`}var VX=/[^\w\u00B0-\uFFFF-]/g;function W(X){if(X==="")return"";return X.replace(VX,(J)=>`\\${J}`)}var KX=new Set(["adjacent","child","descendant","parent","sibling","column-combinator"]);function v(X){return KX.has(X.type)}var x={Unknown:null,QuirksMode:"quirks",IgnoreCase:!0,CaseSensitive:!1};var EX={};h(EX,{selectOne:()=>yX,selectAll:()=>LX,parseAndCompile:()=>M,is:()=>wX,compile:()=>I,clearSelectorCache:()=>WX});function u(X,J,Z,Q){let H=[],V=[J],z=[0];for(;;){if(z[0]>=V[0].length){if(V.length===1)return H;V.shift(),z.shift();continue}let $=V[0][z[0]++];if(!Z.isTag($))continue;if(X($))H.push($);if(Q||Z.getName($)!=="template"){let K=Z.getChildren($);if(K.length>0)V.unshift(K),z.unshift(0)}}}function k(X,J,Z,Q){let H=[J],V=[0];for(;;){if(V[0]>=H[0].length){if(H.length===1)return null;H.shift(),V.shift();continue}let z=H[0][V[0]++];if(!Z.isTag(z))continue;if(X(z))return z;if(Q||Z.getName(z)!=="template"){let $=Z.getChildren(z);if($.length>0)H.unshift($),V.unshift(0)}}}function T(X,J){if(Array.isArray(X))return J.removeSubsets(X);return J.getChildren(X)}var R=(X)=>{return!0};function m(X){return X===32||X===9||X===10||X===12||X===13}function w(X,J){let Z=J.length,Q=X.length;if(Z===0||Z>Q)return!1;let H=X.indexOf(J);while(H>=0){let V=H===0||m(X.charCodeAt(H-1)),z=H+Z,$=z===Q||m(X.charCodeAt(z));if(V&&$)return!0;H=X.indexOf(J,H+1)}return!1}function jX(X,J,Z){if(J.ignoreCase===!0)return!0;if(J.ignoreCase===!1)return!1;if(J.ignoreCase==="quirks")return!Z.xmlMode;return!1}function d(X,J,Z){let Q=J.adapter,H=X.action,V=X.name,z=X.value,$=jX(z,X,J);if($)z=z.toLowerCase();return GX(H,V,z,$,Q,Z)}function GX(X,J,Z,Q,H,V){let z=V===R;switch(X){case"exists":if(z)return($)=>H.hasAttrib($,J);return($)=>H.hasAttrib($,J)&&V($);case"equals":if(!Q){if(z)return($)=>H.getAttributeValue($,J)===Z;return($)=>H.getAttributeValue($,J)===Z&&V($)}if(z)return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return K.toLowerCase()===Z};return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return K.toLowerCase()===Z&&V($)};case"not":return($)=>{let K=H.getAttributeValue($,J);if(K==null)return V($);return(Q?K.toLowerCase():K)!==Z&&V($)};case"start":if(Z==="")return()=>!1;if(z)return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return(Q?K.toLowerCase():K).startsWith(Z)};return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return(Q?K.toLowerCase():K).startsWith(Z)&&V($)};case"end":if(Z==="")return()=>!1;if(z)return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return(Q?K.toLowerCase():K).endsWith(Z)};return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return(Q?K.toLowerCase():K).endsWith(Z)&&V($)};case"any":{if(Z==="")return()=>!1;if(Q){if(z)return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return K.toLowerCase().indexOf(Z)>=0};return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return K.toLowerCase().indexOf(Z)>=0&&V($)}}if(z)return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return K.indexOf(Z)>=0};return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return K.indexOf(Z)>=0&&V($)}}case"element":{if(Z===""||/\s/.test(Z))return()=>!1;if(Q){if(z)return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return w(K.toLowerCase(),Z)};return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return w(K.toLowerCase(),Z)&&V($)}}if(z)return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return w(K,Z)};return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;return w(K,Z)&&V($)}}case"hyphen":if(z)return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;let B=Q?K.toLowerCase():K;if(B===Z)return!0;if(B.startsWith(Z)&&B.charAt(Z.length)==="-")return!0;return!1};return($)=>{let K=H.getAttributeValue($,J);if(K==null)return!1;let B=Q?K.toLowerCase():K;if(B===Z)return V($);if(B.startsWith(Z)&&B.charAt(Z.length)==="-")return V($);return!1}}return($)=>{return!1}}var YX=new Set(["link","any-link","visited","hover","active","focus","focus-visible","focus-within","target","target-within","enabled","disabled","checked","required","optional","valid","invalid","selected","placeholder-shown","read-only","read-write","in-range","out-of-range","default","indeterminate"]),DX=new Set(["is","where","matches","-moz-any","-webkit-any"]),FX=new Set(["nth-child","nth-last-child","nth-of-type","nth-last-of-type"]);function p(X,J,Z){let Q=J.adapter,H=X.name,V=X.data;if(X.type==="pseudo"&&DX.has(H)){if(!Array.isArray(V))return Z;let z=V.map(($)=>M($,J));if(z.length===1){let $=z[0];return(K)=>$(K)&&Z(K)}return($)=>{for(let K of z)if(K($))return Z($);return!1}}if(X.type==="pseudo"&&H==="not"){if(!Array.isArray(V))return Z;let z=V.map(($)=>M($,J));if(z.length===1){let $=z[0];return(K)=>!$(K)&&Z(K)}return($)=>{for(let K of z)if(K($))return!1;return Z($)}}if(X.type==="pseudo"&&H==="has"){if(!Array.isArray(V))return Z;let z=V.map((K)=>M(K,J)),$=z.length===1?z[0]:(K)=>{for(let B of z)if(B(K))return!0;return!1};return(K)=>{let B=Q.getChildren(K).slice();while(B.length>0){let G=B.pop();if(!Q.isTag(G))continue;if($(G))return Z(K);let j=Q.getChildren(G);for(let Y=0;Y<j.length;Y++)B.push(j[Y])}return!1}}if(X.type==="pseudo"&&FX.has(H)){let z=qX(typeof V==="string"?V:"",H,J);return($)=>z($)&&Z($)}if(X.type==="pseudo"&&H==="first-child")return(z)=>{let $=Q.getSiblings(z);for(let K of $){if(!Q.isTag(K))continue;return K===z&&Z(z)}return!1};if(X.type==="pseudo"&&H==="last-child")return(z)=>{let $=Q.getSiblings(z);for(let K=$.length-1;K>=0;K--){let B=$[K];if(!Q.isTag(B))continue;return B===z&&Z(z)}return!1};if(X.type==="pseudo"&&H==="first-of-type")return(z)=>{let $=Q.getName(z),K=Q.getSiblings(z);for(let B of K){if(!Q.isTag(B)||Q.getName(B)!==$)continue;return B===z&&Z(z)}return!1};if(X.type==="pseudo"&&H==="last-of-type")return(z)=>{let $=Q.getName(z),K=Q.getSiblings(z);for(let B=K.length-1;B>=0;B--){let G=K[B];if(!Q.isTag(G)||Q.getName(G)!==$)continue;return G===z&&Z(z)}return!1};if(X.type==="pseudo"&&H==="only-child")return(z)=>{let $=Q.getSiblings(z),K=0,B=!1;for(let G of $){if(!Q.isTag(G))continue;if(K++,K>1)return!1;if(G===z)B=!0}return K===1&&B&&Z(z)};if(X.type==="pseudo"&&H==="only-of-type")return(z)=>{let $=Q.getName(z),K=Q.getSiblings(z),B=0,G=!1;for(let j of K){if(!Q.isTag(j)||Q.getName(j)!==$)continue;if(B++,B>1)return!1;if(j===z)G=!0}return B===1&&G&&Z(z)};if(X.type==="pseudo"&&H==="empty")return(z)=>Q.getChildren(z).length===0&&Z(z);if(X.type==="pseudo"&&H==="root")return(z)=>{let $=Q.getParent(z);return($==null||!Q.isTag($))&&Z(z)};if(X.type==="pseudo"&&H==="scope"){if(J.context&&Array.isArray(J.context)){let z=new Set(J.context);return($)=>z.has($)&&Z($)}if(J.context){let z=J.context;return($)=>$===z&&Z($)}return(z)=>{let $=Q.getParent(z);return($==null||!Q.isTag($))&&Z(z)}}if(X.type==="pseudo"&&YX.has(H)){if(J.pseudos&&J.pseudos[H]){let z=J.pseudos[H];if(typeof z==="function")return($)=>z($,void 0)&&Z($)}return(z)=>{return!1}}if(X.type==="pseudo-element")return Z;if(J.pseudos&&J.pseudos[H]){let z=J.pseudos[H];if(typeof z==="function")return($)=>z($,typeof V==="string"?V:void 0)&&Z($);if(typeof z==="string"){let $=P(z),K=L($,J);return(B)=>K(B)&&Z(B)}}return(z)=>{return!1}}function UX(X){let J=X.trim();if(J==="odd")return{a:2,b:1};if(J==="even")return{a:2,b:0};let Z=/^([+-]?\d*)n([+-]\d+)?$/.exec(J.replace(/\s+/g,""));if(Z){let H=Z[1]===""||Z[1]==="+"?1:Z[1]==="-"?-1:Number(Z[1]),V=Z[2]?Number(Z[2]):0;return{a:H,b:V}}let Q=Number(J);if(!Number.isNaN(Q))return{a:0,b:Q};return null}function qX(X,J,Z){let Q=UX(X);if(!Q)return()=>!1;let{a:H,b:V}=Q,z=Z.adapter,$=J==="nth-of-type"||J==="nth-last-of-type",K=J==="nth-last-child"||J==="nth-last-of-type";return(B)=>{let G=z.getSiblings(B),j=$?z.getName(B):"",Y=0,D=0;for(let q of G){if(!z.isTag(q))continue;if($&&z.getName(q)!==j)continue;if(D++,q===B)Y=D}if(Y===0)return!1;let F=K?D-Y+1:Y;if(H===0)return F===V;return(F-V)/H>=0&&(F-V)%H===0}}function l(X,J,Z){let Q=J.adapter,H=Z===R;switch(X.type){case"tag":{let V=J.lowerCaseTags!==!1&&!J.xmlMode?X.name.toLowerCase():X.name;if(H)return(z)=>Q.getName(z)===V;return(z)=>Q.getName(z)===V&&Z(z)}case"universal":return Z;case"attribute":return d(X,J,Z);case"pseudo":case"pseudo-element":return p(X,J,Z);case"descendant":case"child":case"parent":case"sibling":case"adjacent":case"column-combinator":return Z}return(V)=>{return!1}}var MX={universal:50,tag:30,attribute:1,pseudo:0,"pseudo-element":0},OX={exists:10,equals:8,not:7,start:6,end:6,hyphen:5,any:4,element:3};function c(X){let J=MX[X.type]??0;if(X.type==="attribute")return J+(OX[X.action]??0);return J}function L(X,J){if(X.length===1)return M(X[0],J);let Z=X.map((Q)=>M(Q,J));return(Q)=>Z.some((H)=>H(Q))}function M(X,J){let Z=J.adapter,Q=[[]],H=[];for(let z of X)if(z.type==="descendant"||z.type==="child"||z.type==="sibling"||z.type==="adjacent"||z.type==="parent"||z.type==="column-combinator")H.push(z),Q.push([]);else Q[Q.length-1].push(z);let V=R;for(let z=0;z<Q.length;z++){let $=Q[z].slice().sort((K,B)=>c(K)-c(B));for(let K of $)V=l(K,J,V);if(z<H.length)V=NX(H[z],V,Z)}return V}function NX(X,J,Z){switch(X.type){case"descendant":case"column-combinator":return(Q)=>{let H=Z.getParent(Q);while(H){if(Z.isTag(H)&&J(H))return!0;H=Z.getParent(H)}return!1};case"child":return(Q)=>{let H=Z.getParent(Q);return H!=null&&Z.isTag(H)&&J(H)};case"sibling":return(Q)=>{let H=Z.getSiblings(Q);for(let V of H){if(V===Q)return!1;if(Z.isTag(V)&&J(V))return!0}return!1};case"adjacent":return(Q)=>{let H=Z.getSiblings(Q),V=null;for(let z of H){if(z===Q)return V!=null&&Z.isTag(V)&&J(V);V=z}return!1};case"parent":return(Q)=>Z.existsOne(J,Z.getChildren(Q))}return(Q)=>{return!1}}function r(X){if(!X||!X.adapter)throw Error("css-select: adapter is required");return X.adapter}var E=new WeakMap;function PX(X){return(X.xmlMode?1:0)<<0|(X.lowerCaseAttributeNames===!1?1:0)<<1|(X.lowerCaseTags===!1?1:0)<<2}function RX(X,J){let Z=E.get(X);if(!Z)Z=[],E.set(X,Z);let Q=Z[J];if(!Q)Q=new Map,Z[J]=Q;return Q}function i(X,J){let Z=P(X,{xmlMode:J.xmlMode,lowerCaseAttributeNames:J.lowerCaseAttributeNames,lowerCaseTags:J.lowerCaseTags});return L(Z,J)}function I(X,J){if(typeof X!=="string")return L(X,J);if(J.cacheResults===!1||!J.adapter)return i(X,J);let Z=RX(J.adapter,PX(J)),Q=Z.get(X);if(Q!==void 0)return Q;return Q=i(X,J),Z.set(X,Q),Q}function WX(X,J){if(J===void 0){E.delete(X);return}let Z=E.get(X);if(!Z)return;for(let Q of Z)if(Q)Q.delete(J)}function LX(X,J,Z){let Q=r(Z),H=typeof X==="function"?X:I(X,Z),V=T(J,Q);return u(H,V,Q,Z.xmlMode===!0)}function yX(X,J,Z){let Q=r(Z),H=typeof X==="function"?X:I(X,Z),V=T(J,Q);return k(H,V,Q,Z.xmlMode===!0)}function wX(X,J,Z){return(typeof J==="function"?J:I(J,Z))(X)}export{yX as selectOne,LX as selectAll,M as parseAndCompile,wX as is,I as compile,WX as clearSelectorCache};
|