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

Commit

Permalink
fix(selection): Returns previous selection if either leaf is null (#2189
Browse files Browse the repository at this point in the history
)

Summary:
**Summary**

Bails out early from the `getUpdatedSelectionState` method with previous selection when `anchorLeaf` or `focusLeaf` are null to prevent an error surfaced when using a decorator for emoji inserted between two spans of text entered via IME.

Thanks to robbertbrak for suggesting and cherry-picking a fix from their fork 🎉

Fixes #2187

**Test Plan**
1. Build Draft.js
1. Inject into jsfiddle reported by jdecked https://jsfiddle.net/36tvhmce/
1. Try to repro issues with Pinyin and Hiragana IME
1. Verify there is no error in the console and text is entered normally
Pull Request resolved: #2189

Reviewed By: kedromelon

Differential Revision: D17504999

Pulled By: claudiopro

fbshipit-source-id: dbd2180cf5c1af5bbe1c2b94c50767c58f524dcf
  • Loading branch information
Claudio Procida authored and facebook-github-bot committed Sep 23, 2019
1 parent 0c92bf7 commit fe68e43
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion meta/bundle-size-stats/Draft.js.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion meta/bundle-size-stats/Draft.min.js.json

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions src/component/selection/getUpdatedSelectionState.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function getUpdatedSelectionState(
const selection: SelectionState = nullthrows(editorState.getSelection());
if (__DEV__) {
if (!anchorKey || !focusKey) {
/*eslint-disable no-console */
/* eslint-disable fb-www/no-console */
console.warn('Invalid selection state.', arguments, editorState.toJS());
/*eslint-enable no-console */
/* eslint-enable fb-www/no-console */
return selection;
}
}
Expand All @@ -47,6 +47,16 @@ function getUpdatedSelectionState(
.getBlockTree(focusBlockKey)
.getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);

if (!anchorLeaf || !focusLeaf) {
// If we cannot make sense of the updated selection state, stick to the current one.
if (__DEV__) {
/* eslint-disable fb-www/no-console */
console.warn('Invalid selection state.', arguments, editorState.toJS());
/* eslint-enable fb-www/no-console */
}
return selection;
}

const anchorLeafStart: number = anchorLeaf.get('start');
const focusLeafStart: number = focusLeaf.get('start');

Expand Down

0 comments on commit fe68e43

Please sign in to comment.