Skip to content

Commit 7fac8f1

Browse files
committed
Merge pull request react-bootstrap#807 from react-bootstrap/node-contains
Add contains "polyfill" to domUtils
2 parents f2da250 + 19b0cd4 commit 7fac8f1

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

src/RootCloseWrapper.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,6 @@ import EventListener from './utils/EventListener';
44

55
// TODO: Merge this logic with dropdown logic once #526 is done.
66

7-
/**
8-
* Checks whether a node is within
9-
* a root nodes tree
10-
*
11-
* @param {DOMElement} node
12-
* @param {DOMElement} root
13-
* @returns {boolean}
14-
*/
15-
function isNodeInRoot(node, root) {
16-
while (node) {
17-
if (node === root) {
18-
return true;
19-
}
20-
node = node.parentNode;
21-
}
22-
23-
return false;
24-
}
257

268
export default class RootCloseWrapper extends React.Component {
279
constructor(props) {
@@ -44,7 +26,7 @@ export default class RootCloseWrapper extends React.Component {
4426
// If the click originated from within this component, don't do anything.
4527
// e.srcElement is required for IE8 as e.target is undefined
4628
let target = e.target || e.srcElement;
47-
if (isNodeInRoot(target, React.findDOMNode(this))) {
29+
if (domUtils.contains(React.findDOMNode(this), target)) {
4830
return;
4931
}
5032

src/utils/domUtils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,32 @@ function offsetParentFunc(elem) {
113113
return offsetParent || docElem;
114114
}
115115

116+
/**
117+
* Cross browser .contains() polyfill
118+
* @param {HTMLElement} elem
119+
* @param {HTMLElement} inner
120+
* @return {bool}
121+
*/
122+
function contains(elem, inner){
123+
function ie8Contains(root, node) {
124+
while (node) {
125+
if (node === root) {
126+
return true;
127+
}
128+
node = node.parentNode;
129+
}
130+
return false;
131+
}
132+
133+
return (elem && elem.contains)
134+
? elem.contains(inner)
135+
: (elem && elem.compareDocumentPosition)
136+
? elem === inner || !!(elem.compareDocumentPosition(inner) & 16)
137+
: ie8Contains(elem, inner);
138+
}
139+
116140
export default {
141+
contains,
117142
ownerDocument,
118143
getComputedStyles,
119144
getOffset,

0 commit comments

Comments
 (0)