traversalDOMWithBFS

说明

BFS遍历指定DOM节点

源码

用法

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