Use nodeContains where appropriate.
Ben Newman committed
Oct 1, 2013 at 17:54 UTC
0a02b55d9583efd2b3058bd157307c1ec92a5988
2 files changed
+9
-33
src/core/ReactInputSelection.js
+3
-1
@@ -20,6 +20,8 @@
20
21
var ReactDOMSelection = require('ReactDOMSelection');
22
23
+var nodeContains = require('nodeContains');
24
+
25
// It is not safe to read the document.activeElement property in IE if there's
26
// nothing focused.
27
function getActiveElement() {
@@ -30,7 +32,7 @@ function getActiveElement() {
32
}
33
34
function isInDocument(node) {
33
- return document.documentElement.contains(node);
35
+ return nodeContains(document.documentElement, node);
36
}
37
38
/**
src/core/ReactMount.js
+6
-32
@@ -18,11 +18,14 @@
18
19
"use strict";
20
21
-var invariant = require('invariant');
22
-var getReactRootElementInContainer = require('getReactRootElementInContainer');
21
var ReactEventEmitter = require('ReactEventEmitter');
22
var ReactInstanceHandles = require('ReactInstanceHandles');
23
24
+var $ = require('$');
25
+var getReactRootElementInContainer = require('getReactRootElementInContainer');
26
+var invariant = require('invariant');
27
+var nodeContains = require('nodeContains');
28
+
29
var SEPARATOR = ReactInstanceHandles.SEPARATOR;
30
31
var ATTR_NAME = 'data-reactid';
@@ -31,8 +34,6 @@ var nodeCache = {};
34
var ELEMENT_NODE_TYPE = 1;
35
var DOC_NODE_TYPE = 9;
36
34
-var $ = require('$');
35
-
37
/** Mapping from reactRootID to React component instance. */
38
var instancesByReactRootID = {};
39
@@ -140,36 +141,9 @@ function isValid(node, id) {
141
);
142
143
var container = ReactMount.findReactContainerForID(id);
143
- if (container && contains(container, node)) {
144
- return true;
145
- }
146
- }
147
-
148
- return false;
149
-}
150
-
151
-function contains(ancestor, descendant) {
152
- if (ancestor.contains) {
153
- // Supported natively in virtually all browsers, but not in jsdom.
154
- return ancestor.contains(descendant);
155
- }
156
-
157
- if (descendant === ancestor) {
158
- return true;
159
- }
160
-
161
- if (descendant.nodeType === 3) {
162
- // If descendant is a text node, start from descendant.parentNode
163
- // instead, so that we can assume all ancestors worth considering are
164
- // element nodes with nodeType === 1.
165
- descendant = descendant.parentNode;
166
- }
167
-
168
- while (descendant && descendant.nodeType === 1) {
169
- if (descendant === ancestor) {
144
+ if (container && nodeContains(container, node)) {
145
return true;
146
}
172
- descendant = descendant.parentNode;
147
}
148
149
return false;