aboutsummaryrefslogtreecommitdiff
path: root/node_modules/domutils/lib/querying.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-02-09 17:58:07 +0100
committerMinteck <contact@minteck.org>2022-02-09 17:58:07 +0100
commit22a25ded9f7d9c9a96cce8d1bc12475ca0434201 (patch)
tree0e33d0650fe58f41c00bbc4b8047956905766823 /node_modules/domutils/lib/querying.js
parent8f54d903fb3470823a5e4d6ff4655de009836245 (diff)
downloadyoutoo-22a25ded9f7d9c9a96cce8d1bc12475ca0434201.tar.gz
youtoo-22a25ded9f7d9c9a96cce8d1bc12475ca0434201.tar.bz2
youtoo-22a25ded9f7d9c9a96cce8d1bc12475ca0434201.zip
Major update
Diffstat (limited to 'node_modules/domutils/lib/querying.js')
-rw-r--r--node_modules/domutils/lib/querying.js126
1 files changed, 126 insertions, 0 deletions
diff --git a/node_modules/domutils/lib/querying.js b/node_modules/domutils/lib/querying.js
new file mode 100644
index 0000000..1eb20e9
--- /dev/null
+++ b/node_modules/domutils/lib/querying.js
@@ -0,0 +1,126 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.findAll = exports.existsOne = exports.findOne = exports.findOneChild = exports.find = exports.filter = void 0;
+var domhandler_1 = require("domhandler");
+/**
+ * Search a node and its children for nodes passing a test function.
+ *
+ * @param test Function to test nodes on.
+ * @param node Node to search. Will be included in the result set if it matches.
+ * @param recurse Also consider child nodes.
+ * @param limit Maximum number of nodes to return.
+ * @returns All nodes passing `test`.
+ */
+function filter(test, node, recurse, limit) {
+ if (recurse === void 0) { recurse = true; }
+ if (limit === void 0) { limit = Infinity; }
+ if (!Array.isArray(node))
+ node = [node];
+ return find(test, node, recurse, limit);
+}
+exports.filter = filter;
+/**
+ * Search an array of node and its children for nodes passing a test function.
+ *
+ * @param test Function to test nodes on.
+ * @param nodes Array of nodes to search.
+ * @param recurse Also consider child nodes.
+ * @param limit Maximum number of nodes to return.
+ * @returns All nodes passing `test`.
+ */
+function find(test, nodes, recurse, limit) {
+ var result = [];
+ for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
+ var elem = nodes_1[_i];
+ if (test(elem)) {
+ result.push(elem);
+ if (--limit <= 0)
+ break;
+ }
+ if (recurse && (0, domhandler_1.hasChildren)(elem) && elem.children.length > 0) {
+ var children = find(test, elem.children, recurse, limit);
+ result.push.apply(result, children);
+ limit -= children.length;
+ if (limit <= 0)
+ break;
+ }
+ }
+ return result;
+}
+exports.find = find;
+/**
+ * Finds the first element inside of an array that matches a test function.
+ *
+ * @param test Function to test nodes on.
+ * @param nodes Array of nodes to search.
+ * @returns The first node in the array that passes `test`.
+ */
+function findOneChild(test, nodes) {
+ return nodes.find(test);
+}
+exports.findOneChild = findOneChild;
+/**
+ * Finds one element in a tree that passes a test.
+ *
+ * @param test Function to test nodes on.
+ * @param nodes Array of nodes to search.
+ * @param recurse Also consider child nodes.
+ * @returns The first child node that passes `test`.
+ */
+function findOne(test, nodes, recurse) {
+ if (recurse === void 0) { recurse = true; }
+ var elem = null;
+ for (var i = 0; i < nodes.length && !elem; i++) {
+ var checked = nodes[i];
+ if (!(0, domhandler_1.isTag)(checked)) {
+ continue;
+ }
+ else if (test(checked)) {
+ elem = checked;
+ }
+ else if (recurse && checked.children.length > 0) {
+ elem = findOne(test, checked.children);
+ }
+ }
+ return elem;
+}
+exports.findOne = findOne;
+/**
+ * @param test Function to test nodes on.
+ * @param nodes Array of nodes to search.
+ * @returns Whether a tree of nodes contains at least one node passing a test.
+ */
+function existsOne(test, nodes) {
+ return nodes.some(function (checked) {
+ return (0, domhandler_1.isTag)(checked) &&
+ (test(checked) ||
+ (checked.children.length > 0 &&
+ existsOne(test, checked.children)));
+ });
+}
+exports.existsOne = existsOne;
+/**
+ * Search and array of nodes and its children for nodes passing a test function.
+ *
+ * Same as `find`, only with less options, leading to reduced complexity.
+ *
+ * @param test Function to test nodes on.
+ * @param nodes Array of nodes to search.
+ * @returns All nodes passing `test`.
+ */
+function findAll(test, nodes) {
+ var _a;
+ var result = [];
+ var stack = nodes.filter(domhandler_1.isTag);
+ var elem;
+ while ((elem = stack.shift())) {
+ var children = (_a = elem.children) === null || _a === void 0 ? void 0 : _a.filter(domhandler_1.isTag);
+ if (children && children.length > 0) {
+ stack.unshift.apply(stack, children);
+ }
+ if (test(elem))
+ result.push(elem);
+ }
+ return result;
+}
+exports.findAll = findAll;