-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathhtmlDomUtils.ts
More file actions
61 lines (50 loc) · 1.74 KB
/
htmlDomUtils.ts
File metadata and controls
61 lines (50 loc) · 1.74 KB
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
export function isTextNode(node: Node): node is Text {
return node.nodeType === Node.TEXT_NODE
}
export function isCommentNode(node: Node): node is Comment {
return node.nodeType === Node.COMMENT_NODE
}
export function isElementNode(node: Node): node is Element {
return node.nodeType === Node.ELEMENT_NODE
}
export function isNodeShadowHost(node: Node): node is Element & { shadowRoot: ShadowRoot } {
return isElementNode(node) && Boolean(node.shadowRoot)
}
export function isNodeShadowRoot(node: Node): node is ShadowRoot {
const shadowRoot = node as ShadowRoot
return !!shadowRoot.host && shadowRoot.nodeType === Node.DOCUMENT_FRAGMENT_NODE && isElementNode(shadowRoot.host)
}
export function hasChildNodes(node: Node) {
return node.childNodes.length > 0 || isNodeShadowHost(node)
}
export function forEachChildNodes(node: Node, callback: (child: Node) => void) {
let child = node.firstChild
while (child) {
callback(child)
child = child.nextSibling
}
if (isNodeShadowHost(node)) {
callback(node.shadowRoot)
}
}
/**
* Return `host` in case if the current node is a shadow root otherwise will return the `parentNode`
*/
export function getParentNode(node: Node): Node | null {
return isNodeShadowRoot(node) ? node.host : node.parentNode
}
/**
* Return the parent element, crossing shadow DOM boundaries.
* If the element is a direct child of a shadow root, returns the shadow host.
*/
export function getParentElement(element: Element): Element | null {
if (element.parentElement) {
return element.parentElement
}
// If parentElement is null, check if we're in a shadow root
const parentNode = element.parentNode
if (parentNode && isNodeShadowRoot(parentNode)) {
return parentNode.host
}
return null
}