This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-types.ts
111 lines (102 loc) · 3.38 KB
/
node-types.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Checks whether the given DOM node is an [element](https://developer.mozilla.org/en-US/docs/Web/API/Element).
*
* @param node - A node to check.
*
* @returns `true` for `Element`, or `false` everything else.
*/
export function isElementNode(node: Node): node is Element {
return node.nodeType === 1;
}
/**
* Checks whether the given DOM node is an [attribute](https://developer.mozilla.org/en-US/docs/Web/API/Attr).
*
* @param node - A node to check.
*
* @returns `true` for `Attr`, or `false` everything else.
*/
export function isAttributeNode(node: Node): node is Attr {
return node.nodeType === 2;
}
/**
* Checks whether the given DOM node is a [text](https://developer.mozilla.org/en-US/docs/Web/API/Text).
*
* @param node - A node to check.
*
* @returns `true` for `Text`, or `false` everything else.
*/
export function isTextNode(node: Node): node is Text {
return node.nodeType === 3;
}
/**
* Checks whether the given DOM node is a [CDATA section](https://developer.mozilla.org/en-US/docs/Web/API/CDATASection).
*
* @param node - A node to check.
*
* @returns `true` for `CDATASection`, or `false` everything else.
*/
export function isCDATASectionNode(node: Node): node is CDATASection {
return node.nodeType === 4;
}
/**
* Checks whether the given DOM node is a [processing instruction](https://developer.mozilla.org/en-US/docs/Web/API/ProcessingInstruction).
*
* @param node - A node to check.
*
* @returns `true` for `ProcessingInstruction`, or `false` everything else.
*/
export function isProcessingInstructionNode(node: Node): node is ProcessingInstruction {
return node.nodeType === 7;
}
/**
* Checks whether the given DOM node is a [comment](https://developer.mozilla.org/en-US/docs/Web/API/Comment).
*
* @param node - A node to check.
*
* @returns `true` for `Comment`, or `false` everything else.
*/
export function isCommentNode(node: Node): node is Comment {
return node.nodeType === 8;
}
/**
* Checks whether the given DOM node is a [document](https://developer.mozilla.org/en-US/docs/Web/API/Document).
*
* @param node - A node to check.
*
* @returns `true` for `Document`, or `false` everything else.
*/
export function isDocumentNode(node: Node): node is Document {
return node.nodeType === 9;
}
/**
* Checks whether the given DOM node is a [document type](https://developer.mozilla.org/en-US/docs/Web/API/DocumentType).
*
* @param node - A node to check.
*
* @returns `true` for `DocumentType`, or `false` everything else.
*/
export function isDocumentTypeNode(node: Node): node is DocumentType {
return node.nodeType === 10;
}
/**
* Checks whether the given DOM node is a [document fragment](https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment).
*
* @param node - A node to check.
*
* @returns `true` for `DocumentFragment`, or `false` everything else.
*/
export function isDocumentFragmentNode(node: Node): node is DocumentFragment {
return node.nodeType === 11;
}
/**
* Checks whether the given DOM node is a [shadow root](https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot).
*
* Note that shadow root is a kind of {@link isDocumentFragmentNode document fragment}.
*
* @param node - A node to check.
*
* @returns `true` for `ShadowRoot`, or `false` everything else.
*/
export function isShadowRootNode(node: Node): node is ShadowRoot {
return isDocumentFragmentNode(node) && !!(node as Partial<ShadowRoot>).host;
}