1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const predicates_1 = require("./predicates");
|
17 | const util_1 = require("./util");
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function* treeMap(node, mapfn, getChildNodes) {
|
23 | for (const child of depthFirst(node, getChildNodes)) {
|
24 | yield* mapfn(child);
|
25 | }
|
26 | }
|
27 | exports.treeMap = treeMap;
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function* depthFirst(node, getChildNodes = util_1.defaultChildNodes) {
|
34 | yield node;
|
35 | const childNodes = getChildNodes(node);
|
36 | if (childNodes === undefined) {
|
37 | return;
|
38 | }
|
39 | for (const child of childNodes) {
|
40 | yield* depthFirst(child, getChildNodes);
|
41 | }
|
42 | }
|
43 | exports.depthFirst = depthFirst;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function* depthFirstReversed(node, getChildNodes = util_1.defaultChildNodes) {
|
51 | const childNodes = getChildNodes(node);
|
52 | if (childNodes !== undefined) {
|
53 | for (const child of reversedView(childNodes)) {
|
54 | yield* depthFirstReversed(child, getChildNodes);
|
55 | }
|
56 | }
|
57 | yield node;
|
58 | }
|
59 | exports.depthFirstReversed = depthFirstReversed;
|
60 |
|
61 |
|
62 |
|
63 | function depthFirstIncludingTemplates(node) {
|
64 | return depthFirst(node, util_1.childNodesIncludeTemplate);
|
65 | }
|
66 | exports.depthFirstIncludingTemplates = depthFirstIncludingTemplates;
|
67 |
|
68 |
|
69 |
|
70 | function* ancestors(node) {
|
71 | let currNode = node;
|
72 | while (currNode !== undefined) {
|
73 | yield currNode;
|
74 | currNode = currNode.parentNode;
|
75 | }
|
76 | }
|
77 | exports.ancestors = ancestors;
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | function* previousSiblings(node) {
|
86 | const parent = node.parentNode;
|
87 | if (parent === undefined) {
|
88 | return;
|
89 | }
|
90 | const siblings = parent.childNodes;
|
91 | if (siblings === undefined) {
|
92 | throw new Error(`Inconsistent parse5 tree: parent does not have children`);
|
93 | }
|
94 | const index = siblings.indexOf(node);
|
95 | if (index === -1) {
|
96 | throw new Error(`Inconsistent parse5 tree: parent does not know about child`);
|
97 | }
|
98 | yield* reversedView(siblings, index - 1);
|
99 | }
|
100 | exports.previousSiblings = previousSiblings;
|
101 |
|
102 | function* reversedView(arr, initialIndex = arr.length - 1) {
|
103 | for (let index = initialIndex; index >= 0; index--) {
|
104 | yield arr[index];
|
105 | }
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function* prior(node) {
|
131 | for (const previousSibling of previousSiblings(node)) {
|
132 | yield* depthFirstReversed(previousSibling);
|
133 | }
|
134 | const parent = node.parentNode;
|
135 | if (parent) {
|
136 | yield parent;
|
137 | yield* prior(parent);
|
138 | }
|
139 | }
|
140 | exports.prior = prior;
|
141 |
|
142 |
|
143 |
|
144 | function query(node, predicate, getChildNodes = util_1.defaultChildNodes) {
|
145 | for (const result of queryAll(node, predicate, getChildNodes)) {
|
146 | return result;
|
147 | }
|
148 | return null;
|
149 | }
|
150 | exports.query = query;
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | function* queryAll(node, predicate, getChildNodes = util_1.defaultChildNodes) {
|
156 | const elementPredicate = predicates_1.predicates.AND(predicates_1.isElement, predicate);
|
157 | for (const desc of depthFirst(node, getChildNodes)) {
|
158 | if (elementPredicate(desc)) {
|
159 | yield desc;
|
160 | }
|
161 | }
|
162 | }
|
163 | exports.queryAll = queryAll;
|
164 |
|
\ | No newline at end of file |