diff --git a/src/component/selection/getDraftEditorSelection.js b/src/component/selection/getDraftEditorSelection.js index f445caac94..ef4b3247b0 100644 --- a/src/component/selection/getDraftEditorSelection.js +++ b/src/component/selection/getDraftEditorSelection.js @@ -12,6 +12,7 @@ 'use strict'; import type {DOMDerivedSelection} from 'DOMDerivedSelection'; +import type {SelectionObject} from 'DraftDOMTypes'; import type EditorState from 'EditorState'; const getDraftEditorSelectionWithNodes = require('getDraftEditorSelectionWithNodes'); @@ -24,10 +25,23 @@ function getDraftEditorSelection( editorState: EditorState, root: HTMLElement, ): DOMDerivedSelection { - const selection = root.ownerDocument.defaultView.getSelection(); + const selection: SelectionObject = root.ownerDocument.defaultView.getSelection(); + const { + anchorNode, + anchorOffset, + focusNode, + focusOffset, + rangeCount, + } = selection; - // No active selection. - if (selection.rangeCount === 0) { + if ( + // No active selection. + rangeCount === 0 || + // No selection, ever. As in, the user hasn't selected anything since + // opening the document. + anchorNode == null || + focusNode == null + ) { return { selectionState: editorState.getSelection().set('hasFocus', false), needsRecovery: false, @@ -37,10 +51,10 @@ function getDraftEditorSelection( return getDraftEditorSelectionWithNodes( editorState, root, - selection.anchorNode, - selection.anchorOffset, - selection.focusNode, - selection.focusOffset, + anchorNode, + anchorOffset, + focusNode, + focusOffset, ); }