function findFilterElements(root, selector, options, queryForSelector, totalLimit) {
    const filterIndex = selector.findIndex(isFilter);
    const sub = selector.slice(0, filterIndex);
    const filter = selector[filterIndex];
    // If we are at the end of the selector, we can limit the number of elements to retrieve.
    const partLimit = selector.length - 1 === filterIndex ? totalLimit : Infinity;
    /*
     * Set the number of elements to retrieve.
     * Eg. for :first, we only have to get a single element.
     */
    const limit = getLimit(filter.name, filter.data, partLimit);
    if (limit === 0)
        return [];
    /*
     * Skip `findElements` call if our selector starts with a positional
     * pseudo.
     */
    const elemsNoLimit = sub.length === 0 && !Array.isArray(root)
        ? DomUtils.getChildren(root).filter(DomUtils.isTag)
        : sub.length === 0
            ? (Array.isArray(root) ? root : [root]).filter(DomUtils.isTag)
            : queryForSelector || sub.some(isTraversal)
                ? findElements(root, [sub], options, limit)
                : filterElements(root, [sub], options);
    const elems = elemsNoLimit.slice(0, limit);
    let result = filterByPosition(filter.name, elems, filter.data, options);
    if (result.length === 0 || selector.length === filterIndex + 1) {
        return result;
    }
    const remainingSelector = selector.slice(filterIndex + 1);
    const remainingHasTraversal = remainingSelector.some(isTraversal);
    if (remainingHasTraversal) {
        if (isTraversal(remainingSelector[0])) {
            const { type } = remainingSelector[0];
            if (type === SelectorType.Sibling ||
                type === SelectorType.Adjacent) {
                // If we have a sibling traversal, we need to also look at the siblings.
                result = prepareContext(result, DomUtils, true);
            }
            // Avoid a traversal-first selector error.
            remainingSelector.unshift(UNIVERSAL_SELECTOR);
        }
        options = {
            ...options,
            // Avoid absolutizing the selector
            relativeSelector: false,
            /*
             * Add a custom root func, to make sure traversals don't match elements
             * that aren't a part of the considered tree.
             */
            rootFunc: (el) => result.includes(el),
        };
    }
    else if (options.rootFunc && options.rootFunc !== boolbase.trueFunc) {
        options = { ...options, rootFunc: boolbase.trueFunc };
    }
    /*
     * If we have another filter, recursively call `findFilterElements`,
     * with the `recursive` flag disabled. We only have to look for more
     * elements when we see a traversal.
     *
     * Otherwise,
     */
    return remainingSelector.some(isFilter)
        ? findFilterElements(result, remainingSelector, options, false, totalLimit)
        : remainingHasTraversal
            ? // Query existing elements to resolve traversal.
                findElements(result, [remainingSelector], options, totalLimit)
            : // If we don't have any more traversals, simply filter elements.
                filterElements(result, [remainingSelector], options);
}
/**
 *
 * @param root Element(s) to search from.
 * @param selector Selector to look for.
 * @param options Options for querying.
 * @param queryForSelector Query multiple levels deep for the initial selector, even if it doesn't contain a traversal.
 */
@param root Element(s) to search from.
@param selector Selector to look for.
@param options Options for querying.
@param queryForSelector Query multiple levels deep for the initial selector, even if it doesn't contain a traversal.
@param root Element ( s ) to search from . @param selector Selector to look for . @param options Options for querying . @param queryForSelector Query multiple levels deep for the initial selector even if it doesn ' t contain a traversal .
function findFilterElements(root, selector, options, queryForSelector, totalLimit) {
    const filterIndex = selector.findIndex(isFilter);
    const sub = selector.slice(0, filterIndex);
    const filter = selector[filterIndex];
    const partLimit = selector.length - 1 === filterIndex ? totalLimit : Infinity;
    const limit = getLimit(filter.name, filter.data, partLimit);
    if (limit === 0)
        return [];
    const elemsNoLimit = sub.length === 0 && !Array.isArray(root)
        ? DomUtils.getChildren(root).filter(DomUtils.isTag)
        : sub.length === 0
            ? (Array.isArray(root) ? root : [root]).filter(DomUtils.isTag)
            : queryForSelector || sub.some(isTraversal)
                ? findElements(root, [sub], options, limit)
                : filterElements(root, [sub], options);
    const elems = elemsNoLimit.slice(0, limit);
    let result = filterByPosition(filter.name, elems, filter.data, options);
    if (result.length === 0 || selector.length === filterIndex + 1) {
        return result;
    }
    const remainingSelector = selector.slice(filterIndex + 1);
    const remainingHasTraversal = remainingSelector.some(isTraversal);
    if (remainingHasTraversal) {
        if (isTraversal(remainingSelector[0])) {
            const { type } = remainingSelector[0];
            if (type === SelectorType.Sibling ||
                type === SelectorType.Adjacent) {
                result = prepareContext(result, DomUtils, true);
            }
            remainingSelector.unshift(UNIVERSAL_SELECTOR);
        }
        options = {
            ...options,
            relativeSelector: false,
            rootFunc: (el) => result.includes(el),
        };
    }
    else if (options.rootFunc && options.rootFunc !== boolbase.trueFunc) {
        options = { ...options, rootFunc: boolbase.trueFunc };
    }
    return remainingSelector.some(isFilter)
        ? findFilterElements(result, remainingSelector, options, false, totalLimit)
        : remainingHasTraversal
            ? 
                findElements(result, [remainingSelector], options, totalLimit)
            : 
                filterElements(result, [remainingSelector], options);
}
function findFilterElements ( root , selector , options , queryForSelector , totalLimit ) { const filterIndex = selector . findIndex ( isFilter ) ; const sub = selector . slice ( 0 , filterIndex ) ; const filter = selector [ filterIndex ] ; const partLimit = selector . length - 1 === filterIndex ? totalLimit : Infinity ; const limit = getLimit ( filter . name , filter . data , partLimit ) ; if ( limit === 0 ) return [ ] ; const elemsNoLimit = sub . length === 0 && ! Array . isArray ( root ) ? DomUtils . getChildren ( root ) . filter ( DomUtils . isTag ) : sub . length === 0 ? ( Array . isArray ( root ) ? root : [ root ] ) . filter ( DomUtils . isTag ) : queryForSelector || sub . some ( isTraversal ) ? findElements ( root , [ sub ] , options , limit ) : filterElements ( root , [ sub ] , options ) ; const elems = elemsNoLimit . slice ( 0 , limit ) ; let result = filterByPosition ( filter . name , elems , filter . data , options ) ; if ( result . length === 0 || selector . length === filterIndex + 1 ) { return result ; } const remainingSelector = selector . slice ( filterIndex + 1 ) ; const remainingHasTraversal = remainingSelector . some ( isTraversal ) ; if ( remainingHasTraversal ) { if ( isTraversal ( remainingSelector [ 0 ] ) ) { const { type } = remainingSelector [ 0 ] ; if ( type === SelectorType . Sibling || type === SelectorType . Adjacent ) { result = prepareContext ( result , DomUtils , true ) ; } remainingSelector . unshift ( UNIVERSAL_SELECTOR ) ; } options = { ... options , relativeSelector : false , rootFunc : ( el ) => result . includes ( el ) , } ; } else if ( options . rootFunc && options . rootFunc !== boolbase . trueFunc ) { options = { ... options , rootFunc : boolbase . trueFunc } ; } return remainingSelector . some ( isFilter ) ? findFilterElements ( result , remainingSelector , options , false , totalLimit ) : remainingHasTraversal ? findElements ( result , [ remainingSelector ] , options , totalLimit ) : filterElements ( result , [ remainingSelector ] , options ) ; }
@param root Element(s) to search from.
@param root Element ( s ) to search from .
// If we are at the end of the selector, we can limit the number of elements to retrieve. /*
     * Set the number of elements to retrieve.
     * Eg. for :first, we only have to get a single element.
     */ /*
     * Skip `findElements` call if our selector starts with a positional
     * pseudo.
     */ // If we have a sibling traversal, we need to also look at the siblings. // Avoid a traversal-first selector error. // Avoid absolutizing the selector /*
             * Add a custom root func, to make sure traversals don't match elements
             * that aren't a part of the considered tree.
             */ /*
     * If we have another filter, recursively call `findFilterElements`,
     * with the `recursive` flag disabled. We only have to look for more
     * elements when we see a traversal.
     *
     * Otherwise,
     */ // Query existing elements to resolve traversal. // If we don't have any more traversals, simply filter elements.