|
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | */ |
7 | 7 |
|
8 | | -import containsNode from 'fbjs/lib/containsNode'; |
9 | | -import getActiveElement from 'fbjs/lib/getActiveElement'; |
| 8 | +import getActiveElement from './getActiveElement'; |
10 | 9 |
|
11 | 10 | import * as ReactDOMSelection from './ReactDOMSelection'; |
12 | | -import {ELEMENT_NODE} from '../shared/HTMLNodeType'; |
| 11 | +import {ELEMENT_NODE, TEXT_NODE} from '../shared/HTMLNodeType'; |
| 12 | + |
| 13 | +// TODO: this code is originally inlined from fbjs. |
| 14 | +// It is likely that we don't actually need all these checks |
| 15 | +// for the particular use case in this file. |
| 16 | +function isNode(object) { |
| 17 | + const doc = object ? object.ownerDocument || object : document; |
| 18 | + const defaultView = doc.defaultView || window; |
| 19 | + return !!( |
| 20 | + object && |
| 21 | + (typeof defaultView.Node === 'function' |
| 22 | + ? object instanceof defaultView.Node |
| 23 | + : typeof object === 'object' && |
| 24 | + typeof object.nodeType === 'number' && |
| 25 | + typeof object.nodeName === 'string') |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +function isTextNode(object) { |
| 30 | + return isNode(object) && object.nodeType === TEXT_NODE; |
| 31 | +} |
| 32 | + |
| 33 | +function containsNode(outerNode, innerNode) { |
| 34 | + if (!outerNode || !innerNode) { |
| 35 | + return false; |
| 36 | + } else if (outerNode === innerNode) { |
| 37 | + return true; |
| 38 | + } else if (isTextNode(outerNode)) { |
| 39 | + return false; |
| 40 | + } else if (isTextNode(innerNode)) { |
| 41 | + return containsNode(outerNode, innerNode.parentNode); |
| 42 | + } else if ('contains' in outerNode) { |
| 43 | + return outerNode.contains(innerNode); |
| 44 | + } else if (outerNode.compareDocumentPosition) { |
| 45 | + return !!(outerNode.compareDocumentPosition(innerNode) & 16); |
| 46 | + } else { |
| 47 | + return false; |
| 48 | + } |
| 49 | +} |
13 | 50 |
|
14 | 51 | function isInDocument(node) { |
15 | 52 | return containsNode(document.documentElement, node); |
|
0 commit comments