index.js
12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.select = exports.filter = exports.some = exports.is = exports.aliases = exports.pseudos = exports.filters = void 0;
var css_what_1 = require("css-what");
var css_select_1 = require("css-select");
var DomUtils = __importStar(require("domutils"));
var helpers_1 = require("./helpers");
var positionals_1 = require("./positionals");
// Re-export pseudo extension points
var css_select_2 = require("css-select");
Object.defineProperty(exports, "filters", { enumerable: true, get: function () { return css_select_2.filters; } });
Object.defineProperty(exports, "pseudos", { enumerable: true, get: function () { return css_select_2.pseudos; } });
Object.defineProperty(exports, "aliases", { enumerable: true, get: function () { return css_select_2.aliases; } });
/** Used to indicate a scope should be filtered. Might be ignored when filtering. */
var SCOPE_PSEUDO = {
type: "pseudo",
name: "scope",
data: null,
};
/** Used for actually filtering for scope. */
var CUSTOM_SCOPE_PSEUDO = __assign({}, SCOPE_PSEUDO);
var UNIVERSAL_SELECTOR = { type: "universal", namespace: null };
function is(element, selector, options) {
if (options === void 0) { options = {}; }
if (typeof selector === "function")
return selector(element);
var _a = helpers_1.groupSelectors(css_what_1.parse(selector, options)), plain = _a[0], filtered = _a[1];
return ((plain.length > 0 && css_select_1.is(element, plain, options)) ||
filtered.some(function (sel) { return filterBySelector(sel, [element], options).length > 0; }));
}
exports.is = is;
function some(elements, selector, options) {
if (options === void 0) { options = {}; }
if (typeof selector === "function")
return elements.some(selector);
var _a = helpers_1.groupSelectors(css_what_1.parse(selector, options)), plain = _a[0], filtered = _a[1];
return ((plain.length > 0 && elements.some(css_select_1._compileToken(plain, options))) ||
filtered.some(function (sel) { return filterBySelector(sel, elements, options).length > 0; }));
}
exports.some = some;
function filterByPosition(filter, elems, data, options) {
var num = typeof data === "string" ? parseInt(data, 10) : NaN;
switch (filter) {
case "first":
case "lt":
// Already done in `getLimit`
return elems;
case "last":
return elems.length > 0 ? [elems[elems.length - 1]] : elems;
case "nth":
case "eq":
return isFinite(num) && Math.abs(num) < elems.length
? [num < 0 ? elems[elems.length + num] : elems[num]]
: [];
case "gt":
return isFinite(num) ? elems.slice(num + 1) : [];
case "even":
return elems.filter(function (_, i) { return i % 2 === 0; });
case "odd":
return elems.filter(function (_, i) { return i % 2 === 1; });
case "not": {
var filtered_1 = new Set(filterParsed(data, elems, options));
return elems.filter(function (e) { return !filtered_1.has(e); });
}
}
}
function filter(selector, elements, options) {
if (options === void 0) { options = {}; }
return filterParsed(css_what_1.parse(selector, options), elements, options);
}
exports.filter = filter;
/**
* Filter a set of elements by a selector.
*
* Will return elements in the original order.
*
* @param selector Selector to filter by.
* @param elements Elements to filter.
* @param options Options for selector.
*/
function filterParsed(selector, elements, options) {
if (elements.length === 0)
return [];
var _a = helpers_1.groupSelectors(selector), plainSelectors = _a[0], filteredSelectors = _a[1];
var found;
if (plainSelectors.length) {
var filtered = filterElements(elements, plainSelectors, options);
// If there are no filters, just return
if (filteredSelectors.length === 0) {
return filtered;
}
// Otherwise, we have to do some filtering
if (filtered.length) {
found = new Set(filtered);
}
}
for (var i = 0; i < filteredSelectors.length && (found === null || found === void 0 ? void 0 : found.size) !== elements.length; i++) {
var filteredSelector = filteredSelectors[i];
var missing = found
? elements.filter(function (e) { return !found.has(e); })
: elements;
if (missing.length === 0)
break;
var filtered = filterBySelector(filteredSelector, elements, options);
if (filtered.length) {
if (!found) {
/*
* If we haven't found anything before the last selector,
* just return what we found now.
*/
if (i === filteredSelectors.length - 1) {
return filtered;
}
found = new Set(filtered);
}
else {
filtered.forEach(function (el) { return found.add(el); });
}
}
}
return typeof found !== "undefined"
? found.size === elements.length
? elements
: elements.filter(function (el) { return found.has(el); })
: [];
}
function filterBySelector(selector, elements, options) {
if (selector.some(css_what_1.isTraversal)) {
/*
* Get one root node, run selector with the scope
* set to all of our nodes.
*/
var root = helpers_1.getDocumentRoot(elements[0]);
var sel = __spreadArray(__spreadArray([], selector), [CUSTOM_SCOPE_PSEUDO]);
return findFilterElements(root, sel, options, true, elements);
}
// Performance optimization: If we don't have to traverse, just filter set.
return findFilterElements(elements, selector, options, false);
}
function select(selector, root, options) {
if (options === void 0) { options = {}; }
if (typeof selector === "function") {
return find(root, selector);
}
var _a = helpers_1.groupSelectors(css_what_1.parse(selector, options)), plain = _a[0], filtered = _a[1];
var results = filtered.map(function (sel) {
return findFilterElements(root, sel, options, true);
});
// Plain selectors can be queried in a single go
if (plain.length) {
results.push(findElements(root, plain, options, Infinity));
}
// If there was only a single selector, just return the result
if (results.length === 1) {
return results[0];
}
// Sort results, filtering for duplicates
return DomUtils.uniqueSort(results.reduce(function (a, b) { return __spreadArray(__spreadArray([], a), b); }));
}
exports.select = select;
// Traversals that are treated differently in css-select.
var specialTraversal = new Set(["descendant", "adjacent"]);
function includesScopePseudo(t) {
return (t !== SCOPE_PSEUDO &&
t.type === "pseudo" &&
(t.name === "scope" ||
(Array.isArray(t.data) &&
t.data.some(function (data) { return data.some(includesScopePseudo); }))));
}
function addContextIfScope(selector, options, scopeContext) {
return scopeContext && selector.some(includesScopePseudo)
? __assign(__assign({}, options), { context: scopeContext }) : options;
}
/**
*
* @param root Element(s) to search from.
* @param selector Selector to look for.
* @param options Options for querying.
* @param queryForSelector Query multiple levels deep for the initial selector, even if it doesn't contain a traversal.
* @param scopeContext Optional context for a :scope.
*/
function findFilterElements(root, selector, options, queryForSelector, scopeContext) {
var filterIndex = selector.findIndex(positionals_1.isFilter);
var sub = selector.slice(0, filterIndex);
var filter = selector[filterIndex];
/*
* Set the number of elements to retrieve.
* Eg. for :first, we only have to get a single element.
*/
var limit = positionals_1.getLimit(filter.name, filter.data);
if (limit === 0)
return [];
var subOpts = addContextIfScope(sub, options, scopeContext);
/*
* Skip `findElements` call if our selector starts with a positional
* pseudo.
*/
var elemsNoLimit = sub.length === 0 && !Array.isArray(root)
? DomUtils.getChildren(root).filter(DomUtils.isTag)
: sub.length === 0 || (sub.length === 1 && sub[0] === SCOPE_PSEUDO)
? (Array.isArray(root) ? root : [root]).filter(DomUtils.isTag)
: queryForSelector || sub.some(css_what_1.isTraversal)
? findElements(root, [sub], subOpts, limit)
: filterElements(root, [sub], subOpts);
var elems = elemsNoLimit.slice(0, limit);
var result = filterByPosition(filter.name, elems, filter.data, options);
if (result.length === 0 || selector.length === filterIndex + 1) {
return result;
}
var remainingSelector = selector.slice(filterIndex + 1);
var remainingHasTraversal = remainingSelector.some(css_what_1.isTraversal);
var remainingOpts = addContextIfScope(remainingSelector, options, scopeContext);
if (remainingHasTraversal) {
/*
* Some types of traversals have special logic when they start a selector
* in css-select. If this is the case, add a universal selector in front of
* the selector to avoid this behavior.
*/
if (specialTraversal.has(remainingSelector[0].type)) {
remainingSelector.unshift(UNIVERSAL_SELECTOR);
}
/*
* Add a scope token in front of the remaining selector,
* to make sure traversals don't match elements that aren't a
* part of the considered tree.
*/
remainingSelector.unshift(SCOPE_PSEUDO);
}
/*
* If we have another filter, recursively call `findFilterElements`,
* with the `recursive` flag disabled. We only have to look for more
* elements when we see a traversal.
*
* Otherwise,
*/
return remainingSelector.some(positionals_1.isFilter)
? findFilterElements(result, remainingSelector, options, false, scopeContext)
: remainingHasTraversal
? // Query existing elements to resolve traversal.
findElements(result, [remainingSelector], remainingOpts, Infinity)
: // If we don't have any more traversals, simply filter elements.
filterElements(result, [remainingSelector], remainingOpts);
}
function findElements(root, sel, options, limit) {
if (limit === 0)
return [];
var query = css_select_1._compileToken(sel, options, root);
return find(root, query, limit);
}
function find(root, query, limit) {
if (limit === void 0) { limit = Infinity; }
var elems = css_select_1.prepareContext(root, DomUtils, query.shouldTestNextSiblings);
return DomUtils.find(function (node) { return DomUtils.isTag(node) && query(node); }, elems, true, limit);
}
function filterElements(elements, sel, options) {
var els = (Array.isArray(elements) ? elements : [elements]).filter(DomUtils.isTag);
if (els.length === 0)
return els;
var query = css_select_1._compileToken(sel, options);
return els.filter(query);
}