-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
42 lines (39 loc) · 983 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Element } from "./Element.ts";
export function* findChildrenByClassName(
node: Element,
className: string,
): Generator<Element, void, void> {
if (node.className === className) {
yield node;
}
if (Array.isArray(node.children)) {
for (const child of node.children) {
yield* findChildrenByClassName(child, className);
}
}
}
export function* findChildrenByTag(
node: Element,
name: string,
): Generator<Element, void, void> {
if (node.tagName === name) {
yield node;
}
for (let i = 0; i < node.children.length; i++) {
const child = node.children.item(i)!;
yield* findChildrenByTag(child, name);
}
}
export function findChildById(node: Element, id: string): Element | null {
if (node.id === id) {
return node;
} else {
for (let i = 0; i < node.children.length; i++) {
const found = findChildById(node.children.item(i)!, id);
if (found) {
return found;
}
}
return null;
}
}