From dfacb1b45cfc4c3b603c01a45a83dae331df02cb Mon Sep 17 00:00:00 2001 From: Kevin Chavez Date: Mon, 13 Apr 2020 16:05:21 -0700 Subject: [PATCH] Type selection object in getDraftEditorSelection.js Summary: We were missing a possible case: if there has been no selection, anchorNode and focusNode can be null. Reviewed By: jack-arms Differential Revision: D21002172 fbshipit-source-id: d84a5c6eb551d88898148d84e4961515f152f02c --- .../selection/getDraftEditorSelection.js | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) 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, ); }