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