forked from Viewtiful/fs-dialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-root-node-polyfill.js
56 lines (46 loc) · 1.54 KB
/
get-root-node-polyfill.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
// service to handle stacking of dialogs
(function () {
// Node getRootNode(optional GetRootNodeOptions options);
/**
* Returns the context object’s shadow-including root if options’s composed is true.
* Returns the context object’s root otherwise.
*
* The root of an object is itself, if its parent is null, or else it is the root of its parent.
*
* The shadow-including root of an object is its root’s host’s shadow-including root,
* if the object’s root is a shadow root, and its root otherwise.
*
* https://dom.spec.whatwg.org/#dom-node-getrootnode
*
* @memberof Node.prototype
* @param {!object} [opt = {}] - Options.
* @param {!boolean} [opt.composed] - See above description.
* @returns {!Node} The root node.
*/
function getRootNode (opt) {
var composed = typeof opt === 'object' && Boolean(opt.composed);
return composed ? getShadowIncludingRoot(this) : getRoot(this);
}
function getShadowIncludingRoot (node) {
var root = getRoot(node);
if (isShadowRoot(root)) {
return getShadowIncludingRoot(root.host);
}
return root;
}
function getRoot (node) {
if (node.parentNode !== null) {
return getRoot(node.parentNode);
}
return node;
}
function isShadowRoot (node) {
return typeof ShadowRoot === 'function' && node instanceof ShadowRoot;
}
function isImplemented () {
return Object.prototype.hasOwnProperty.call(Node.prototype, 'getRootNode');
}
if (!isImplemented()) {
Node.prototype.getRootNode = getRootNode;
}
})();