diff --git a/src/renderers/dom/shared/ReactDOMSelection.js b/src/renderers/dom/shared/ReactDOMSelection.js index aff9e894bba15..cbce488ebefdc 100644 --- a/src/renderers/dom/shared/ReactDOMSelection.js +++ b/src/renderers/dom/shared/ReactDOMSelection.js @@ -40,7 +40,7 @@ function isCollapsed(anchorNode, anchorOffset, focusNode, focusOffset) { * @return {object} */ function getIEOffsets(node) { - var selection = document.selection; + var selection = node.ownerDocument.selection; var selectedRange = selection.createRange(); var selectedLength = selectedRange.text.length; @@ -63,7 +63,8 @@ function getIEOffsets(node) { * @return {?object} */ function getModernOffsets(node) { - var selection = window.getSelection && window.getSelection(); + var win = node.ownerDocument.defaultView; + var selection = win.getSelection && win.getSelection(); if (!selection || selection.rangeCount === 0) { return null; @@ -119,7 +120,7 @@ function getModernOffsets(node) { var end = start + rangeLength; // Detect whether the selection is backward. - var detectionRange = document.createRange(); + var detectionRange = node.ownerDocument.createRange(); detectionRange.setStart(anchorNode, anchorOffset); detectionRange.setEnd(focusNode, focusOffset); var isBackward = detectionRange.collapsed; @@ -135,7 +136,7 @@ function getModernOffsets(node) { * @param {object} offsets */ function setIEOffsets(node, offsets) { - var range = document.selection.createRange().duplicate(); + var range = node.ownerDocument.selection.createRange().duplicate(); var start, end; if (offsets.end === undefined) { @@ -169,11 +170,12 @@ function setIEOffsets(node, offsets) { * @param {object} offsets */ function setModernOffsets(node, offsets) { - if (!window.getSelection) { + var win = node.ownerDocument.defaultView; + if (!win.getSelection) { return; } - var selection = window.getSelection(); + var selection = win.getSelection(); var length = node[getTextContentAccessor()].length; var start = Math.min(offsets.start, length); var end = offsets.end === undefined ? @@ -191,7 +193,7 @@ function setModernOffsets(node, offsets) { var endMarker = getNodeForCharacterOffset(node, end); if (startMarker && endMarker) { - var range = document.createRange(); + var range = node.ownerDocument.createRange(); range.setStart(startMarker.node, startMarker.offset); selection.removeAllRanges(); diff --git a/src/renderers/dom/shared/ReactInputSelection.js b/src/renderers/dom/shared/ReactInputSelection.js index 3229f854006fc..e9f35a4574190 100644 --- a/src/renderers/dom/shared/ReactInputSelection.js +++ b/src/renderers/dom/shared/ReactInputSelection.js @@ -18,7 +18,7 @@ var focusNode = require('focusNode'); var getActiveElement = require('getActiveElement'); function isInDocument(node) { - return containsNode(document.documentElement, node); + return containsNode(node.ownerDocument.documentElement, node); } /** @@ -85,10 +85,10 @@ var ReactInputSelection = { start: input.selectionStart, end: input.selectionEnd, }; - } else if (document.selection && + } else if (input.ownerDocument.selection && (input.nodeName && input.nodeName.toLowerCase() === 'input')) { // IE8 input. - var range = document.selection.createRange(); + var range = input.ownerDocument.selection.createRange(); // There can only be one selection per document in IE, so it must // be in our element. if (range.parentElement() === input) { diff --git a/src/renderers/dom/shared/eventPlugins/SelectEventPlugin.js b/src/renderers/dom/shared/eventPlugins/SelectEventPlugin.js index 75b182f372a3d..13952c81f46de 100644 --- a/src/renderers/dom/shared/eventPlugins/SelectEventPlugin.js +++ b/src/renderers/dom/shared/eventPlugins/SelectEventPlugin.js @@ -73,16 +73,19 @@ function getSelection(node) { start: node.selectionStart, end: node.selectionEnd, }; - } else if (window.getSelection) { - var selection = window.getSelection(); + } + var doc = node.ownerDocument || node.document || node; + var win = doc.defaultView || doc.parentWindow; + if (win.getSelection) { + var selection = win.getSelection(); return { anchorNode: selection.anchorNode, anchorOffset: selection.anchorOffset, focusNode: selection.focusNode, focusOffset: selection.focusOffset, }; - } else if (document.selection) { - var range = document.selection.createRange(); + } else if (doc.selection) { + var range = doc.selection.createRange(); return { parentElement: range.parentElement(), text: range.text,