123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import * as DomUtils from "domutils";
- import boolbase from "boolbase";
- import { compile as compileRaw, compileUnsafe, compileToken, } from "./compile.js";
- import { getNextSiblings } from "./pseudo-selectors/subselects.js";
- const defaultEquals = (a, b) => a === b;
- const defaultOptions = {
- adapter: DomUtils,
- equals: defaultEquals,
- };
- function convertOptionFormats(options) {
- var _a, _b, _c, _d;
- /*
- * We force one format of options to the other one.
- */
- // @ts-expect-error Default options may have incompatible `Node` / `ElementNode`.
- const opts = options !== null && options !== void 0 ? options : defaultOptions;
- // @ts-expect-error Same as above.
- (_a = opts.adapter) !== null && _a !== void 0 ? _a : (opts.adapter = DomUtils);
- // @ts-expect-error `equals` does not exist on `Options`
- (_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);
- return opts;
- }
- function wrapCompile(func) {
- return function addAdapter(selector, options, context) {
- const opts = convertOptionFormats(options);
- return func(selector, opts, context);
- };
- }
- /**
- * Compiles the query, returns a function.
- */
- export const compile = wrapCompile(compileRaw);
- export const _compileUnsafe = wrapCompile(compileUnsafe);
- export const _compileToken = wrapCompile(compileToken);
- function getSelectorFunc(searchFunc) {
- return function select(query, elements, options) {
- const opts = convertOptionFormats(options);
- if (typeof query !== "function") {
- query = compileUnsafe(query, opts, elements);
- }
- const filteredElements = prepareContext(elements, opts.adapter, query.shouldTestNextSiblings);
- return searchFunc(query, filteredElements, opts);
- };
- }
- export function prepareContext(elems, adapter, shouldTestNextSiblings = false) {
- /*
- * Add siblings if the query requires them.
- * See https://github.com/fb55/css-select/pull/43#issuecomment-225414692
- */
- if (shouldTestNextSiblings) {
- elems = appendNextSiblings(elems, adapter);
- }
- return Array.isArray(elems)
- ? adapter.removeSubsets(elems)
- : adapter.getChildren(elems);
- }
- function appendNextSiblings(elem, adapter) {
- // Order matters because jQuery seems to check the children before the siblings
- const elems = Array.isArray(elem) ? elem.slice(0) : [elem];
- const elemsLength = elems.length;
- for (let i = 0; i < elemsLength; i++) {
- const nextSiblings = getNextSiblings(elems[i], adapter);
- elems.push(...nextSiblings);
- }
- return elems;
- }
- /**
- * @template Node The generic Node type for the DOM adapter being used.
- * @template ElementNode The Node type for elements for the DOM adapter being used.
- * @param elems Elements to query. If it is an element, its children will be queried..
- * @param query can be either a CSS selector string or a compiled query function.
- * @param [options] options for querying the document.
- * @see compile for supported selector queries.
- * @returns All matching elements.
- *
- */
- export const selectAll = getSelectorFunc((query, elems, options) => query === boolbase.falseFunc || !elems || elems.length === 0
- ? []
- : options.adapter.findAll(query, elems));
- /**
- * @template Node The generic Node type for the DOM adapter being used.
- * @template ElementNode The Node type for elements for the DOM adapter being used.
- * @param elems Elements to query. If it is an element, its children will be queried..
- * @param query can be either a CSS selector string or a compiled query function.
- * @param [options] options for querying the document.
- * @see compile for supported selector queries.
- * @returns the first match, or null if there was no match.
- */
- export const selectOne = getSelectorFunc((query, elems, options) => query === boolbase.falseFunc || !elems || elems.length === 0
- ? null
- : options.adapter.findOne(query, elems));
- /**
- * Tests whether or not an element is matched by query.
- *
- * @template Node The generic Node type for the DOM adapter being used.
- * @template ElementNode The Node type for elements for the DOM adapter being used.
- * @param elem The element to test if it matches the query.
- * @param query can be either a CSS selector string or a compiled query function.
- * @param [options] options for querying the document.
- * @see compile for supported selector queries.
- * @returns
- */
- export function is(elem, query, options) {
- const opts = convertOptionFormats(options);
- return (typeof query === "function" ? query : compileRaw(query, opts))(elem);
- }
- /**
- * Alias for selectAll(query, elems, options).
- * @see [compile] for supported selector queries.
- */
- export default selectAll;
- // Export filters, pseudos and aliases to allow users to supply their own.
- /** @deprecated Use the `pseudos` option instead. */
- export { filters, pseudos, aliases } from "./pseudo-selectors/index.js";
- //# sourceMappingURL=index.js.map
|