[6a3a178] | 1 | "use strict";
|
---|
| 2 | var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
---|
| 3 | for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
---|
| 4 | to[j] = from[i];
|
---|
| 5 | return to;
|
---|
| 6 | };
|
---|
| 7 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 8 | exports.subselects = exports.getNextSiblings = exports.ensureIsTag = exports.PLACEHOLDER_ELEMENT = void 0;
|
---|
| 9 | var boolbase_1 = require("boolbase");
|
---|
| 10 | var procedure_1 = require("../procedure");
|
---|
| 11 | /** Used as a placeholder for :has. Will be replaced with the actual element. */
|
---|
| 12 | exports.PLACEHOLDER_ELEMENT = {};
|
---|
| 13 | function ensureIsTag(next, adapter) {
|
---|
| 14 | if (next === boolbase_1.falseFunc)
|
---|
| 15 | return boolbase_1.falseFunc;
|
---|
| 16 | return function (elem) { return adapter.isTag(elem) && next(elem); };
|
---|
| 17 | }
|
---|
| 18 | exports.ensureIsTag = ensureIsTag;
|
---|
| 19 | function getNextSiblings(elem, adapter) {
|
---|
| 20 | var siblings = adapter.getSiblings(elem);
|
---|
| 21 | if (siblings.length <= 1)
|
---|
| 22 | return [];
|
---|
| 23 | var elemIndex = siblings.indexOf(elem);
|
---|
| 24 | if (elemIndex < 0 || elemIndex === siblings.length - 1)
|
---|
| 25 | return [];
|
---|
| 26 | return siblings.slice(elemIndex + 1).filter(adapter.isTag);
|
---|
| 27 | }
|
---|
| 28 | exports.getNextSiblings = getNextSiblings;
|
---|
| 29 | var is = function (next, token, options, context, compileToken) {
|
---|
| 30 | var opts = {
|
---|
| 31 | xmlMode: !!options.xmlMode,
|
---|
| 32 | adapter: options.adapter,
|
---|
| 33 | equals: options.equals,
|
---|
| 34 | };
|
---|
| 35 | var func = compileToken(token, opts, context);
|
---|
| 36 | return function (elem) { return func(elem) && next(elem); };
|
---|
| 37 | };
|
---|
| 38 | /*
|
---|
| 39 | * :not, :has, :is and :matches have to compile selectors
|
---|
| 40 | * doing this in src/pseudos.ts would lead to circular dependencies,
|
---|
| 41 | * so we add them here
|
---|
| 42 | */
|
---|
| 43 | exports.subselects = {
|
---|
| 44 | is: is,
|
---|
| 45 | /**
|
---|
| 46 | * `:matches` is an alias for `:is`.
|
---|
| 47 | */
|
---|
| 48 | matches: is,
|
---|
| 49 | not: function (next, token, options, context, compileToken) {
|
---|
| 50 | var opts = {
|
---|
| 51 | xmlMode: !!options.xmlMode,
|
---|
| 52 | adapter: options.adapter,
|
---|
| 53 | equals: options.equals,
|
---|
| 54 | };
|
---|
| 55 | var func = compileToken(token, opts, context);
|
---|
| 56 | if (func === boolbase_1.falseFunc)
|
---|
| 57 | return next;
|
---|
| 58 | if (func === boolbase_1.trueFunc)
|
---|
| 59 | return boolbase_1.falseFunc;
|
---|
| 60 | return function not(elem) {
|
---|
| 61 | return !func(elem) && next(elem);
|
---|
| 62 | };
|
---|
| 63 | },
|
---|
| 64 | has: function (next, subselect, options, _context, compileToken) {
|
---|
| 65 | var adapter = options.adapter;
|
---|
| 66 | var opts = {
|
---|
| 67 | xmlMode: !!options.xmlMode,
|
---|
| 68 | adapter: adapter,
|
---|
| 69 | equals: options.equals,
|
---|
| 70 | };
|
---|
| 71 | // @ts-expect-error Uses an array as a pointer to the current element (side effects)
|
---|
| 72 | var context = subselect.some(function (s) {
|
---|
| 73 | return s.some(procedure_1.isTraversal);
|
---|
| 74 | })
|
---|
| 75 | ? [exports.PLACEHOLDER_ELEMENT]
|
---|
| 76 | : undefined;
|
---|
| 77 | var compiled = compileToken(subselect, opts, context);
|
---|
| 78 | if (compiled === boolbase_1.falseFunc)
|
---|
| 79 | return boolbase_1.falseFunc;
|
---|
| 80 | if (compiled === boolbase_1.trueFunc) {
|
---|
| 81 | return function (elem) {
|
---|
| 82 | return adapter.getChildren(elem).some(adapter.isTag) && next(elem);
|
---|
| 83 | };
|
---|
| 84 | }
|
---|
| 85 | var hasElement = ensureIsTag(compiled, adapter);
|
---|
| 86 | var _a = compiled.shouldTestNextSiblings, shouldTestNextSiblings = _a === void 0 ? false : _a;
|
---|
| 87 | /*
|
---|
| 88 | * `shouldTestNextSiblings` will only be true if the query starts with
|
---|
| 89 | * a traversal (sibling or adjacent). That means we will always have a context.
|
---|
| 90 | */
|
---|
| 91 | if (context) {
|
---|
| 92 | return function (elem) {
|
---|
| 93 | context[0] = elem;
|
---|
| 94 | var childs = adapter.getChildren(elem);
|
---|
| 95 | var nextElements = shouldTestNextSiblings
|
---|
| 96 | ? __spreadArray(__spreadArray([], childs), getNextSiblings(elem, adapter)) : childs;
|
---|
| 97 | return (next(elem) && adapter.existsOne(hasElement, nextElements));
|
---|
| 98 | };
|
---|
| 99 | }
|
---|
| 100 | return function (elem) {
|
---|
| 101 | return next(elem) &&
|
---|
| 102 | adapter.existsOne(hasElement, adapter.getChildren(elem));
|
---|
| 103 | };
|
---|
| 104 | },
|
---|
| 105 | };
|
---|