Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
Type selection object in getDraftEditorSelection.js
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mrkev authored and facebook-github-bot committed Apr 13, 2020
1 parent 2299714 commit dfacb1b
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/component/selection/getDraftEditorSelection.js
Expand Up @@ -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');
Expand All @@ -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,
Expand All @@ -37,10 +51,10 @@ function getDraftEditorSelection(
return getDraftEditorSelectionWithNodes(
editorState,
root,
selection.anchorNode,
selection.anchorOffset,
selection.focusNode,
selection.focusOffset,
anchorNode,
anchorOffset,
focusNode,
focusOffset,
);
}

Expand Down

0 comments on commit dfacb1b

Please sign in to comment.