-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (44 loc) · 1.32 KB
/
index.js
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
/**
* Module exports.
*/
module.exports = getDocument;
// defined by w3c
var DOCUMENT_NODE = 9;
/**
* Returns `true` if `w` is a Document object, or `false` otherwise.
*
* @param {?} d - Document object, maybe
* @return {Boolean}
* @private
*/
function isDocument (d) {
return d && d.nodeType === DOCUMENT_NODE;
}
/**
* Returns the `document` object associated with the given `node`, which may be
* a DOM element, the Window object, a Selection, a Range. Basically any DOM
* object that references the Document in some way, this function will find it.
*
* @param {Mixed} node - DOM node, selection, or range in which to find the `document` object
* @return {Document} the `document` object associated with `node`
* @public
*/
function getDocument(node) {
if (isDocument(node)) {
return node;
} else if (isDocument(node.ownerDocument)) {
return node.ownerDocument;
} else if (isDocument(node.document)) {
return node.document;
} else if (node.parentNode) {
return getDocument(node.parentNode);
// Range support
} else if (node.commonAncestorContainer) {
return getDocument(node.commonAncestorContainer);
} else if (node.startContainer) {
return getDocument(node.startContainer);
// Selection support
} else if (node.anchorNode) {
return getDocument(node.anchorNode);
}
}