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 Nov 1, 2017
1 parent e1d4110 commit c5e3925
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
14 changes: 10 additions & 4 deletions packages/react-dom/src/client/ReactDOMSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ var {TEXT_NODE} = require('../shared/HTMLNodeType');
* @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 @@ -153,11 +157,13 @@ function getModernOffsetsFromPoints(
* @param {object} offsets
*/
function setModernOffsets(node, offsets) {
if (!window.getSelection) {
var doc = node.ownerDocument || document;

if (!doc.defaultView.getSelection) {
return;
}

var selection = window.getSelection();
var selection = doc.defaultView.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 All @@ -183,7 +189,7 @@ function setModernOffsets(node, offsets) {
) {
return;
}
var range = document.createRange();
var range = doc.createRange();
range.setStart(startMarker.node, startMarker.offset);
selection.removeAllRanges();

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 @@ -71,14 +71,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
8 changes: 5 additions & 3 deletions packages/react-dom/src/events/SyntheticClipboardEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ var SyntheticEvent = require('events/SyntheticEvent');
*/
var ClipboardEventInterface = {
clipboardData: function(event) {
return 'clipboardData' in event
? event.clipboardData
: window.clipboardData;
if ('clipboardData' in event) {
return event.clipboardData;
}
var doc = (event.target && event.target.ownerDocument) || document;
return doc.defaultView.clipboardData;
},
};

Expand Down

0 comments on commit c5e3925

Please sign in to comment.