traversalDOMWithDFS

说明

DFS遍历指定DOM节点

源码

用法

traversalDOMWithDFS.test.ts
1
import { traversalDOMWithDFS } from "../../utility/dom/traversalDOMWithDFS";
2
3
describe('traversalDOMWithDFS', () => {
4
test('traversalDOMWithDFS should traversal DOM Tree by DFS', () => {
5
document.body.innerHTML = `
6
<div id="app">
7
<h3 class="title">
8
<a class="link"></a>
9
</h3>
10
<section class="post">
11
<p class="text"></p>
12
</section>
13
</div>
14
`;
15
16
const expected = [
17
(document.getElementById('app') as HTMLDivElement),
18
(document.querySelector('.title') as HTMLTitleElement),
19
(document.querySelector('.link') as HTMLAnchorElement),
20
(document.querySelector('.post') as HTMLDivElement),
21
(document.querySelector('.text') as HTMLParagraphElement),
22
];
23
const result: HTMLElement[] = [];
24
25
traversalDOMWithDFS(
26
(document.getElementById('app') as HTMLElement),
27
(node) => {
28
result.push(node);
29
}
30
);
31
32
for (const [index, node] of result.entries()) {
33
expect(node).toBe(expected[index]);
34
}
35
})
36
})
Copied!
最近更新 1yr ago
复制链接