Tim van der Lippe | 706ec96 | 2021-06-04 13:24:42 +0100 | [diff] [blame^] | 1 | "use strict"; |
| 2 | var __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 | })); |
| 9 | var __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 | }); |
| 14 | var __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 | }; |
| 21 | Object.defineProperty(exports, "__esModule", { value: true }); |
| 22 | exports.pseudos = exports.filters = exports.is = exports.selectOne = exports.selectAll = exports.prepareContext = exports._compileToken = exports._compileUnsafe = exports.compile = void 0; |
| 23 | var DomUtils = __importStar(require("domutils")); |
| 24 | var boolbase_1 = require("boolbase"); |
| 25 | var compile_1 = require("./compile"); |
| 26 | var subselects_1 = require("./pseudo-selectors/subselects"); |
| 27 | var defaultEquals = function (a, b) { return a === b; }; |
| 28 | var defaultOptions = { |
| 29 | adapter: DomUtils, |
| 30 | equals: defaultEquals, |
| 31 | }; |
| 32 | function 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 | } |
| 45 | function 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 | */ |
| 54 | exports.compile = wrapCompile(compile_1.compile); |
| 55 | exports._compileUnsafe = wrapCompile(compile_1.compileUnsafe); |
| 56 | exports._compileToken = wrapCompile(compile_1.compileToken); |
| 57 | function getSelectorFunc(searchFunc) { |
| 58 | return function select(query, elements, options) { |
| 59 | var opts = convertOptionFormats(options); |
| 60 | if (typeof query !== "function") { |
| 61 | query = compile_1.compileUnsafe(query, opts, elements); |
| 62 | } |
| 63 | var filteredElements = prepareContext(elements, opts.adapter, query.shouldTestNextSiblings); |
| 64 | return searchFunc(query, filteredElements, opts); |
| 65 | }; |
| 66 | } |
| 67 | function 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 | } |
| 80 | exports.prepareContext = prepareContext; |
| 81 | function 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 = 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 | */ |
| 100 | exports.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 | */ |
| 114 | exports.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 | */ |
| 130 | function is(elem, query, options) { |
| 131 | var opts = convertOptionFormats(options); |
| 132 | return (typeof query === "function" ? query : compile_1.compile(query, opts))(elem); |
| 133 | } |
| 134 | exports.is = is; |
| 135 | /** |
| 136 | * Alias for selectAll(query, elems, options). |
| 137 | * @see [compile] for supported selector queries. |
| 138 | */ |
| 139 | exports.default = exports.selectAll; |
| 140 | // Export filters and pseudos to allow users to supply their own. |
| 141 | var pseudo_selectors_1 = require("./pseudo-selectors"); |
| 142 | Object.defineProperty(exports, "filters", { enumerable: true, get: function () { return pseudo_selectors_1.filters; } }); |
| 143 | Object.defineProperty(exports, "pseudos", { enumerable: true, get: function () { return pseudo_selectors_1.pseudos; } }); |