-
Notifications
You must be signed in to change notification settings - Fork 0
/
getDomTags.ts
39 lines (34 loc) · 1.08 KB
/
getDomTags.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
const getTagRecursive = (tree: HTMLElement | Element, set: Set<string>) => {
set.add(tree.tagName.toLowerCase());
const childs = tree.children;
for (let i = 0; i < childs.length; i++) {
if (childs[i]) {
getTagRecursive(childs[i], set);
}
}
};
const getTagIterative = (tree: HTMLElement | Element, set: Set<string>) => {
let queue: (HTMLElement | Element)[] = [];
queue.push(tree);
//for iterative version you can use any traversal techniques. I am using Level order traversal.
while (queue.length !== 0) {
let childQueue: (HTMLElement | Element)[] = [];
while (queue.length !== 0) {
const element = queue.pop();
if (element) {
set.add(element.tagName.toLowerCase());
const child = element.children;
for (let i = 0; i < child.length; i++) {
childQueue.push(child[i]);
}
}
}
queue = [...childQueue];
}
};
function getTags(tree: HTMLElement | Element) {
const tagSet = new Set<string>();
// getTagRecursive(tree, tagSet);
getTagIterative(tree, tagSet);
return Array.from(tagSet);
}