blob: 34fe8f77484748dcfba39c2661bf74936effa78e (
plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
const HIDDEN_VISIBILITY_VALUES = ['hidden', 'collapse'];
/**
* @internal
*/
export const checkVisibility = (
node: Node | null,
visible?: boolean
): Node | boolean => {
if (!node) {
return visible === false;
}
if (visible === undefined) {
return node;
}
const element = (
node.nodeType === Node.TEXT_NODE ? node.parentElement : node
) as Element;
const style = window.getComputedStyle(element);
const isVisible =
style &&
!HIDDEN_VISIBILITY_VALUES.includes(style.visibility) &&
!isBoundingBoxEmpty(element);
return visible === isVisible ? node : false;
};
function isBoundingBoxEmpty(element: Element): boolean {
const rect = element.getBoundingClientRect();
return rect.width === 0 || rect.height === 0;
}
const hasShadowRoot = (node: Node): node is Node & {shadowRoot: ShadowRoot} => {
return 'shadowRoot' in node && node.shadowRoot instanceof ShadowRoot;
};
/**
* @internal
*/
export function* pierce(root: Node): IterableIterator<Node | ShadowRoot> {
if (hasShadowRoot(root)) {
yield root.shadowRoot;
} else {
yield root;
}
}
/**
* @internal
*/
export function* pierceAll(root: Node): IterableIterator<Node | ShadowRoot> {
root = pierce(root).next().value;
yield root;
const walkers = [document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT)];
for (const walker of walkers) {
let node: Element | null;
while ((node = walker.nextNode() as Element | null)) {
if (!node.shadowRoot) {
continue;
}
yield node.shadowRoot;
walkers.push(
document.createTreeWalker(node.shadowRoot, NodeFilter.SHOW_ELEMENT)
);
}
}
}
|