Skip to content

Commit

Permalink
Adapt getSelection to use node’s window object
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Oct 30, 2017
1 parent 0d4dddc commit 3f97e67
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
15 changes: 12 additions & 3 deletions packages/react-dom/src/client/ReactDOMSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 14 additions & 8 deletions packages/react-dom/src/events/SelectEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}
}

Expand Down

0 comments on commit 3f97e67

Please sign in to comment.