Skip to content

Commit

Permalink
Avoid invoking defaultView getter unnecessarily
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Jan 23, 2018
1 parent b08a270 commit 51be426
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
12 changes: 5 additions & 7 deletions packages/react-dom/src/client/ReactDOMSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import {TEXT_NODE} from '../shared/HTMLNodeType';
* @return {?object}
*/
export function getOffsets(outerNode) {
let win = window;
if (outerNode.ownerDocument && outerNode.ownerDocument.defaultView) {
win = outerNode.ownerDocument.defaultView;
}
const win =
(outerNode.ownerDocument && outerNode.ownerDocument.defaultView) || window;
const selection = win.getSelection && win.getSelection();

if (!selection || selection.rangeCount === 0) {
Expand Down Expand Up @@ -155,12 +153,12 @@ export function getModernOffsetsFromPoints(
*/
export function setOffsets(node, offsets) {
const doc = node.ownerDocument || document;

if (!doc.defaultView.getSelection) {
const win = doc ? doc.defaultView : window;
if (!win.getSelection) {
return;
}

const selection = doc.defaultView.getSelection();
const selection = win.getSelection();
const length = node[getTextContentAccessor()].length;
let start = Math.min(offsets.start, length);
let end = offsets.end === undefined ? start : Math.min(offsets.end, length);
Expand Down
6 changes: 2 additions & 4 deletions packages/react-dom/src/events/SelectEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ function getSelection(node) {
end: node.selectionEnd,
};
} else {
let win = window;
if (node.ownerDocument && node.ownerDocument.defaultView) {
win = node.ownerDocument.defaultView;
}
const win =
(node.ownerDocument && node.ownerDocument.defaultView) || window;
if (win.getSelection) {
const selection = win.getSelection();
return {
Expand Down

0 comments on commit 51be426

Please sign in to comment.