/
contains.ts
77 lines (66 loc) · 2.4 KB
/
contains.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
import safeInstanceOf from './safeInstanceOf';
import { NodeType } from 'roosterjs-editor-types';
/**
* Test if a node contains another node
* @param container The container node
* @param contained The node to check if it is inside container
* @param treatSameNodeAsContain When container and contained are the same node,
* return true if this param is set to true, otherwise return false. Default value is false
* @returns True if contained is inside container, or they are the same node when treatSameNodeAsContain is true.
* Otherwise false.
*/
export default function contains(
container: Node | null | undefined,
contained: Node | null | undefined,
treatSameNodeAsContain?: boolean
): boolean;
/**
* Test if a node contains a given range
* @param container The container node
* @param contained The range to check if it is inside container
* @returns True if contained is inside container, otherwise false
*/
export default function contains(
container: Node | null | undefined,
contained: Range | null | undefined
): boolean;
export default function contains(
container: Node | null | undefined,
contained: Node | Range | null | undefined,
treatSameNodeAsContain?: boolean
): boolean {
if (!container || !contained) {
return false;
}
if (treatSameNodeAsContain && container == contained) {
return true;
}
if (safeInstanceOf(contained, 'Range')) {
contained = contained && contained.commonAncestorContainer;
treatSameNodeAsContain = true;
}
if (contained && contained.nodeType == NodeType.Text) {
contained = contained.parentNode;
treatSameNodeAsContain = true;
}
if (container.nodeType != NodeType.Element && container.nodeType != NodeType.DocumentFragment) {
return !!treatSameNodeAsContain && container == contained;
}
return (
!!(treatSameNodeAsContain || container != contained) &&
internalContains(container, contained)
);
}
function internalContains(container: Node, contained: Node | null): boolean {
if (container.contains) {
return container.contains(contained);
} else {
while (contained) {
if (contained == container) {
return true;
}
contained = contained.parentNode;
}
return false;
}
}