diff --git a/packages/react-dom/src/client/ReactDOMSelection.js b/packages/react-dom/src/client/ReactDOMSelection.js index 61a1882636a03..8272f645fdd9a 100644 --- a/packages/react-dom/src/client/ReactDOMSelection.js +++ b/packages/react-dom/src/client/ReactDOMSelection.js @@ -19,7 +19,11 @@ var getTextContentAccessor = require('getTextContentAccessor'); * @return {?object} */ function getModernOffsets(outerNode) { - var selection = window.getSelection && window.getSelection(); + var win = window; + if (outerNode.ownerDocument && outerNode.ownerDocument.defaultView) { + win = outerNode.ownerDocument.defaultView; + } + var selection = win.getSelection && win.getSelection(); if (!selection || selection.rangeCount === 0) { return null; @@ -156,11 +160,16 @@ function getModernOffsetsFromPoints( * @param {object} offsets */ function setModernOffsets(node, offsets) { - if (!window.getSelection) { + var win = window; + if (node.ownerDocument && node.ownerDocument.defaultView) { + 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 ? start : Math.min(offsets.end, length); diff --git a/packages/react-dom/src/events/SelectEventPlugin.js b/packages/react-dom/src/events/SelectEventPlugin.js index c434b3afc3bb7..56cc8d3d0f3e2 100644 --- a/packages/react-dom/src/events/SelectEventPlugin.js +++ b/packages/react-dom/src/events/SelectEventPlugin.js @@ -88,14 +88,20 @@ function getSelection(node) { start: node.selectionStart, end: node.selectionEnd, }; - } else if (window.getSelection) { - var selection = window.getSelection(); - return { - anchorNode: selection.anchorNode, - anchorOffset: selection.anchorOffset, - focusNode: selection.focusNode, - focusOffset: selection.focusOffset, - }; + } else { + var win = window; + if (node.ownerDocument && node.ownerDocument.defaultView) { + win = node.ownerDocument.defaultView; + } + if (win.getSelection) { + var selection = win.getSelection(); + return { + anchorNode: selection.anchorNode, + anchorOffset: selection.anchorOffset, + focusNode: selection.focusNode, + focusOffset: selection.focusOffset, + }; + } } }