Skip to content

Commit

Permalink
Avoid global document and window objects, facebook#427
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Nov 21, 2016
1 parent fbf75dd commit b8fcd96
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/renderers/dom/shared/ReactDOMSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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 ?
Expand All @@ -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();

Expand Down
6 changes: 3 additions & 3 deletions src/renderers/dom/shared/ReactInputSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 7 additions & 4 deletions src/renderers/dom/shared/eventPlugins/SelectEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b8fcd96

Please sign in to comment.