import * as DomUtils from "domutils" ; import { falseFunc } from "boolbase" ; import type { Node as DomHandlerNode , Element as DomHandlerElement , } from "domhandler" ; import { compile as compileRaw , compileUnsafe , compileToken } from "./compile" ; import type { CompiledQuery , Options , InternalOptions , Query , Adapter , Predicate , } from "./types" ; import { getNextSiblings } from "./pseudo-selectors/subselects" ; export type { Options } ; const defaultEquals = < Node > ( a , b ) => a === b ; const defaultOptions = { adapter : DomUtils , equals : defaultEquals , } ; function convertOptionFormats < Node , ElementNode extends Node > ( options ? ) { const opts = options ?? defaultOptions ; opts . adapter ??= DomUtils ; opts . equals ??= opts . adapter ?. equals ?? defaultEquals ; return opts as InternalOptions < Node , ElementNode > ; } function wrapCompile < Selector , Node , ElementNode extends Node , R extends Node > ( func ) { return function addAdapter ( selector , options ? , context ? ) : CompiledQuery < R > { 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 < Node , ElementNode extends Node , T > ( searchFunc ) { return function select ( query , elements , options ? ) : T { const opts = convertOptionFormats ( options ) ; if ( typeof query !== "function" ) { query = compileUnsafe < Node , ElementNode > ( query , opts , elements ) ; } const filteredElements = prepareContext ( elements , opts . adapter , query . shouldTestNextSiblings ) ; return searchFunc ( query , filteredElements , opts ) ; } ; } export function prepareContext < Node , ElementNode extends Node > ( elems , adapter , shouldTestNextSiblings = false ) { if ( shouldTestNextSiblings ) { elems = appendNextSiblings ( elems , adapter ) ; } return Array . isArray ( elems ) ? adapter . removeSubsets ( elems ) : adapter . getChildren ( elems ) ; } function appendNextSiblings < Node , ElementNode extends Node > ( 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 ; } template Node template ElementNode param elems param query param options see compile returns export const selectAll = getSelectorFunc ( < Node , ElementNode extends Node > ( query , elems , options ) : ElementNode [ ] => query === falseFunc || ! elems || elems . length === 0 ? [ ] : options . adapter . findAll ( query , elems ) ) ; template Node template ElementNode param elems param query param options see compile returns export const selectOne = getSelectorFunc ( < Node , ElementNode extends Node > ( query , elems , options ) : ElementNode | null => query === falseFunc || ! elems || elems . length === 0 ? null : options . adapter . findOne ( query , elems ) ) ; template Node template ElementNode param elem param query param options see compile returns export function is < Node , ElementNode extends Node > ( elem , query , options ? ) { const opts = convertOptionFormats ( options ) ; return ( typeof query === "function" ? query : compileRaw ( query , opts ) ) ( elem ) ; } see export default selectAll ; export { filters , pseudos , aliases } from "./pseudo-selectors" ;