1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.findAll = exports.existsOne = exports.findOne = exports.findOneChild = exports.find = exports.filter = void 0;
|
4 | var domhandler_1 = require("domhandler");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function filter(test, node, recurse, limit) {
|
15 | if (recurse === void 0) { recurse = true; }
|
16 | if (limit === void 0) { limit = Infinity; }
|
17 | if (!Array.isArray(node))
|
18 | node = [node];
|
19 | return find(test, node, recurse, limit);
|
20 | }
|
21 | exports.filter = filter;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function find(test, nodes, recurse, limit) {
|
32 | var result = [];
|
33 | for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
|
34 | var elem = nodes_1[_i];
|
35 | if (test(elem)) {
|
36 | result.push(elem);
|
37 | if (--limit <= 0)
|
38 | break;
|
39 | }
|
40 | if (recurse && (0, domhandler_1.hasChildren)(elem) && elem.children.length > 0) {
|
41 | var children = find(test, elem.children, recurse, limit);
|
42 | result.push.apply(result, children);
|
43 | limit -= children.length;
|
44 | if (limit <= 0)
|
45 | break;
|
46 | }
|
47 | }
|
48 | return result;
|
49 | }
|
50 | exports.find = find;
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function findOneChild(test, nodes) {
|
59 | return nodes.find(test);
|
60 | }
|
61 | exports.findOneChild = findOneChild;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function findOne(test, nodes, recurse) {
|
71 | if (recurse === void 0) { recurse = true; }
|
72 | var elem = null;
|
73 | for (var i = 0; i < nodes.length && !elem; i++) {
|
74 | var checked = nodes[i];
|
75 | if (!(0, domhandler_1.isTag)(checked)) {
|
76 | continue;
|
77 | }
|
78 | else if (test(checked)) {
|
79 | elem = checked;
|
80 | }
|
81 | else if (recurse && checked.children.length > 0) {
|
82 | elem = findOne(test, checked.children);
|
83 | }
|
84 | }
|
85 | return elem;
|
86 | }
|
87 | exports.findOne = findOne;
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function existsOne(test, nodes) {
|
94 | return nodes.some(function (checked) {
|
95 | return (0, domhandler_1.isTag)(checked) &&
|
96 | (test(checked) ||
|
97 | (checked.children.length > 0 &&
|
98 | existsOne(test, checked.children)));
|
99 | });
|
100 | }
|
101 | exports.existsOne = existsOne;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | function findAll(test, nodes) {
|
112 | var _a;
|
113 | var result = [];
|
114 | var stack = nodes.filter(domhandler_1.isTag);
|
115 | var elem;
|
116 | while ((elem = stack.shift())) {
|
117 | var children = (_a = elem.children) === null || _a === void 0 ? void 0 : _a.filter(domhandler_1.isTag);
|
118 | if (children && children.length > 0) {
|
119 | stack.unshift.apply(stack, children);
|
120 | }
|
121 | if (test(elem))
|
122 | result.push(elem);
|
123 | }
|
124 | return result;
|
125 | }
|
126 | exports.findAll = findAll;
|