|
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; |
|
|
|
|
|
|
|
|
|
const opts = options !== null && options !== void 0 ? options : defaultOptions; |
|
|
|
(_a = opts.adapter) !== null && _a !== void 0 ? _a : (opts.adapter = DomUtils); |
|
|
|
(_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); |
|
}; |
|
} |
|
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
|
|
|
if (shouldTestNextSiblings) { |
|
elems = appendNextSiblings(elems, adapter); |
|
} |
|
return Array.isArray(elems) |
|
? adapter.removeSubsets(elems) |
|
: adapter.getChildren(elems); |
|
} |
|
function appendNextSiblings(elem, adapter) { |
|
|
|
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; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const selectAll = getSelectorFunc((query, elems, options) => query === boolbase.falseFunc || !elems || elems.length === 0 |
|
? [] |
|
: options.adapter.findAll(query, elems)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const selectOne = getSelectorFunc((query, elems, options) => query === boolbase.falseFunc || !elems || elems.length === 0 |
|
? null |
|
: options.adapter.findOne(query, elems)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function is(elem, query, options) { |
|
const opts = convertOptionFormats(options); |
|
return (typeof query === "function" ? query : compileRaw(query, opts))(elem); |
|
} |
|
|
|
|
|
|
|
|
|
export default selectAll; |
|
|
|
|
|
export { filters, pseudos, aliases } from "./pseudo-selectors/index.js"; |
|
|