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

Commit fe68e43

Browse files
Claudio Procidafacebook-github-bot
Claudio Procida
authored andcommitted
fix(selection): Returns previous selection if either leaf is null (#2189)
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
1 parent 0c92bf7 commit fe68e43

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

meta/bundle-size-stats/Draft.js.json

+1-1
Large diffs are not rendered by default.

meta/bundle-size-stats/Draft.min.js.json

+1-1
Large diffs are not rendered by default.

src/component/selection/getUpdatedSelectionState.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ function getUpdatedSelectionState(
2828
const selection: SelectionState = nullthrows(editorState.getSelection());
2929
if (__DEV__) {
3030
if (!anchorKey || !focusKey) {
31-
/*eslint-disable no-console */
31+
/* eslint-disable fb-www/no-console */
3232
console.warn('Invalid selection state.', arguments, editorState.toJS());
33-
/*eslint-enable no-console */
33+
/* eslint-enable fb-www/no-console */
3434
return selection;
3535
}
3636
}
@@ -47,6 +47,16 @@ function getUpdatedSelectionState(
4747
.getBlockTree(focusBlockKey)
4848
.getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);
4949

50+
if (!anchorLeaf || !focusLeaf) {
51+
// If we cannot make sense of the updated selection state, stick to the current one.
52+
if (__DEV__) {
53+
/* eslint-disable fb-www/no-console */
54+
console.warn('Invalid selection state.', arguments, editorState.toJS());
55+
/* eslint-enable fb-www/no-console */
56+
}
57+
return selection;
58+
}
59+
5060
const anchorLeafStart: number = anchorLeaf.get('start');
5161
const focusLeafStart: number = focusLeaf.get('start');
5262

0 commit comments

Comments
 (0)