UNPKG

6.65 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.aliases = exports.pseudos = exports.filters = exports.is = exports.selectOne = exports.selectAll = exports.prepareContext = exports._compileToken = exports._compileUnsafe = exports.compile = void 0;
23var DomUtils = __importStar(require("domutils"));
24var boolbase_1 = require("boolbase");
25var compile_1 = require("./compile");
26var subselects_1 = require("./pseudo-selectors/subselects");
27var defaultEquals = function (a, b) { return a === b; };
28var defaultOptions = {
29 adapter: DomUtils,
30 equals: defaultEquals,
31};
32function convertOptionFormats(options) {
33 var _a, _b, _c, _d;
34 /*
35 * We force one format of options to the other one.
36 */
37 // @ts-expect-error Default options may have incompatible `Node` / `ElementNode`.
38 var opts = options !== null && options !== void 0 ? options : defaultOptions;
39 // @ts-expect-error Same as above.
40 (_a = opts.adapter) !== null && _a !== void 0 ? _a : (opts.adapter = DomUtils);
41 // @ts-expect-error `equals` does not exist on `Options`
42 (_b = opts.equals) !== null && _b !== void 0 ? _b : (opts.equals = (_d = (_c = opts.adapter) === null || _c === void 0 ? void 0 : _c.equals) !== null && _d !== void 0 ? _d : defaultEquals);
43 return opts;
44}
45function wrapCompile(func) {
46 return function addAdapter(selector, options, context) {
47 var opts = convertOptionFormats(options);
48 return func(selector, opts, context);
49 };
50}
51/**
52 * Compiles the query, returns a function.
53 */
54exports.compile = wrapCompile(compile_1.compile);
55exports._compileUnsafe = wrapCompile(compile_1.compileUnsafe);
56exports._compileToken = wrapCompile(compile_1.compileToken);
57function getSelectorFunc(searchFunc) {
58 return function select(query, elements, options) {
59 var opts = convertOptionFormats(options);
60 if (typeof query !== "function") {
61 query = (0, compile_1.compileUnsafe)(query, opts, elements);
62 }
63 var filteredElements = prepareContext(elements, opts.adapter, query.shouldTestNextSiblings);
64 return searchFunc(query, filteredElements, opts);
65 };
66}
67function prepareContext(elems, adapter, shouldTestNextSiblings) {
68 if (shouldTestNextSiblings === void 0) { shouldTestNextSiblings = false; }
69 /*
70 * Add siblings if the query requires them.
71 * See https://github.com/fb55/css-select/pull/43#issuecomment-225414692
72 */
73 if (shouldTestNextSiblings) {
74 elems = appendNextSiblings(elems, adapter);
75 }
76 return Array.isArray(elems)
77 ? adapter.removeSubsets(elems)
78 : adapter.getChildren(elems);
79}
80exports.prepareContext = prepareContext;
81function appendNextSiblings(elem, adapter) {
82 // Order matters because jQuery seems to check the children before the siblings
83 var elems = Array.isArray(elem) ? elem.slice(0) : [elem];
84 for (var i = 0; i < elems.length; i++) {
85 var nextSiblings = (0, subselects_1.getNextSiblings)(elems[i], adapter);
86 elems.push.apply(elems, nextSiblings);
87 }
88 return elems;
89}
90/**
91 * @template Node The generic Node type for the DOM adapter being used.
92 * @template ElementNode The Node type for elements for the DOM adapter being used.
93 * @param elems Elements to query. If it is an element, its children will be queried..
94 * @param query can be either a CSS selector string or a compiled query function.
95 * @param [options] options for querying the document.
96 * @see compile for supported selector queries.
97 * @returns All matching elements.
98 *
99 */
100exports.selectAll = getSelectorFunc(function (query, elems, options) {
101 return query === boolbase_1.falseFunc || !elems || elems.length === 0
102 ? []
103 : options.adapter.findAll(query, elems);
104});
105/**
106 * @template Node The generic Node type for the DOM adapter being used.
107 * @template ElementNode The Node type for elements for the DOM adapter being used.
108 * @param elems Elements to query. If it is an element, its children will be queried..
109 * @param query can be either a CSS selector string or a compiled query function.
110 * @param [options] options for querying the document.
111 * @see compile for supported selector queries.
112 * @returns the first match, or null if there was no match.
113 */
114exports.selectOne = getSelectorFunc(function (query, elems, options) {
115 return query === boolbase_1.falseFunc || !elems || elems.length === 0
116 ? null
117 : options.adapter.findOne(query, elems);
118});
119/**
120 * Tests whether or not an element is matched by query.
121 *
122 * @template Node The generic Node type for the DOM adapter being used.
123 * @template ElementNode The Node type for elements for the DOM adapter being used.
124 * @param elem The element to test if it matches the query.
125 * @param query can be either a CSS selector string or a compiled query function.
126 * @param [options] options for querying the document.
127 * @see compile for supported selector queries.
128 * @returns
129 */
130function is(elem, query, options) {
131 var opts = convertOptionFormats(options);
132 return (typeof query === "function" ? query : (0, compile_1.compile)(query, opts))(elem);
133}
134exports.is = is;
135/**
136 * Alias for selectAll(query, elems, options).
137 * @see [compile] for supported selector queries.
138 */
139exports.default = exports.selectAll;
140// Export filters, pseudos and aliases to allow users to supply their own.
141var pseudo_selectors_1 = require("./pseudo-selectors");
142Object.defineProperty(exports, "filters", { enumerable: true, get: function () { return pseudo_selectors_1.filters; } });
143Object.defineProperty(exports, "pseudos", { enumerable: true, get: function () { return pseudo_selectors_1.pseudos; } });
144Object.defineProperty(exports, "aliases", { enumerable: true, get: function () { return pseudo_selectors_1.aliases; } });