public_library_map/node_modules/css-select/lib/pseudo-selectors/subselects.js

112 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-11-24 19:46:47 +11:00
"use strict";
2022-09-03 17:12:22 +10:00
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
2021-11-24 19:46:47 +11:00
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.subselects = exports.getNextSiblings = exports.ensureIsTag = exports.PLACEHOLDER_ELEMENT = void 0;
2022-09-03 17:12:22 +10:00
var boolbase_1 = __importDefault(require("boolbase"));
var sort_js_1 = require("../sort.js");
2021-11-24 19:46:47 +11:00
/** Used as a placeholder for :has. Will be replaced with the actual element. */
exports.PLACEHOLDER_ELEMENT = {};
function ensureIsTag(next, adapter) {
2022-09-03 17:12:22 +10:00
if (next === boolbase_1.default.falseFunc)
return boolbase_1.default.falseFunc;
2021-11-24 19:46:47 +11:00
return function (elem) { return adapter.isTag(elem) && next(elem); };
}
exports.ensureIsTag = ensureIsTag;
function getNextSiblings(elem, adapter) {
var siblings = adapter.getSiblings(elem);
if (siblings.length <= 1)
return [];
var elemIndex = siblings.indexOf(elem);
if (elemIndex < 0 || elemIndex === siblings.length - 1)
return [];
return siblings.slice(elemIndex + 1).filter(adapter.isTag);
}
exports.getNextSiblings = getNextSiblings;
2022-09-03 17:12:22 +10:00
function copyOptions(options) {
// Not copied: context, rootFunc
return {
2021-11-24 19:46:47 +11:00
xmlMode: !!options.xmlMode,
2022-09-03 17:12:22 +10:00
lowerCaseAttributeNames: !!options.lowerCaseAttributeNames,
lowerCaseTags: !!options.lowerCaseTags,
quirksMode: !!options.quirksMode,
cacheResults: !!options.cacheResults,
pseudos: options.pseudos,
2021-11-24 19:46:47 +11:00
adapter: options.adapter,
equals: options.equals,
};
2022-09-03 17:12:22 +10:00
}
var is = function (next, token, options, context, compileToken) {
var func = compileToken(token, copyOptions(options), context);
return func === boolbase_1.default.trueFunc
? next
: func === boolbase_1.default.falseFunc
? boolbase_1.default.falseFunc
: function (elem) { return func(elem) && next(elem); };
2021-11-24 19:46:47 +11:00
};
/*
2022-09-03 17:12:22 +10:00
* :not, :has, :is, :matches and :where have to compile selectors
2021-11-24 19:46:47 +11:00
* doing this in src/pseudos.ts would lead to circular dependencies,
* so we add them here
*/
exports.subselects = {
is: is,
/**
2022-09-03 17:12:22 +10:00
* `:matches` and `:where` are aliases for `:is`.
2021-11-24 19:46:47 +11:00
*/
matches: is,
2022-09-03 17:12:22 +10:00
where: is,
2021-11-24 19:46:47 +11:00
not: function (next, token, options, context, compileToken) {
2022-09-03 17:12:22 +10:00
var func = compileToken(token, copyOptions(options), context);
return func === boolbase_1.default.falseFunc
? next
: func === boolbase_1.default.trueFunc
? boolbase_1.default.falseFunc
: function (elem) { return !func(elem) && next(elem); };
2021-11-24 19:46:47 +11:00
},
has: function (next, subselect, options, _context, compileToken) {
var adapter = options.adapter;
2022-09-03 17:12:22 +10:00
var opts = copyOptions(options);
opts.relativeSelector = true;
var context = subselect.some(function (s) { return s.some(sort_js_1.isTraversal); })
? // Used as a placeholder. Will be replaced with the actual element.
[exports.PLACEHOLDER_ELEMENT]
2021-11-24 19:46:47 +11:00
: undefined;
var compiled = compileToken(subselect, opts, context);
2022-09-03 17:12:22 +10:00
if (compiled === boolbase_1.default.falseFunc)
return boolbase_1.default.falseFunc;
2021-11-24 19:46:47 +11:00
var hasElement = ensureIsTag(compiled, adapter);
2022-09-03 17:12:22 +10:00
// If `compiled` is `trueFunc`, we can skip this.
if (context && compiled !== boolbase_1.default.trueFunc) {
/*
* `shouldTestNextSiblings` will only be true if the query starts with
* a traversal (sibling or adjacent). That means we will always have a context.
*/
var _a = compiled.shouldTestNextSiblings, shouldTestNextSiblings_1 = _a === void 0 ? false : _a;
2021-11-24 19:46:47 +11:00
return function (elem) {
2022-09-03 17:12:22 +10:00
if (!next(elem))
return false;
2021-11-24 19:46:47 +11:00
context[0] = elem;
var childs = adapter.getChildren(elem);
2022-09-03 17:12:22 +10:00
var nextElements = shouldTestNextSiblings_1
? __spreadArray(__spreadArray([], childs, true), getNextSiblings(elem, adapter), true) : childs;
return adapter.existsOne(hasElement, nextElements);
2021-11-24 19:46:47 +11:00
};
}
return function (elem) {
return next(elem) &&
adapter.existsOne(hasElement, adapter.getChildren(elem));
};
},
};
2022-09-03 17:12:22 +10:00
//# sourceMappingURL=subselects.js.map