Skip to content

Commit f5d5c10

Browse files
MarcoPolotsapeta
authored andcommitted
Cap selection indices when text changes (facebook#26680)
Summary: This PR facebook#22723 cached selections, so if you had a cached selection indicies, but updated the text to be an empty string, then this would crash. As reported in facebook#25265 and other issues of `setSpan(4 ... 4) ends beyond length` ## Changelog [Android] [fixed] - Crash in TextInput Pull Request resolved: facebook#26680 Test Plan: ``` input.setNativeProps({ text: "xxx", selection: {"begin": 0, "end": 3}}); input.setNativeProps({ text: ""}); ``` Differential Revision: D18189703 Pulled By: cpojer fbshipit-source-id: 67d9615a863fd22598be8d6d4553dec5ac8837ed
1 parent 4af2ba2 commit f5d5c10

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputShadowNode.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ public void setMostRecentEventCount(int mostRecentEventCount) {
156156
@ReactProp(name = PROP_TEXT)
157157
public void setText(@Nullable String text) {
158158
mText = text;
159+
if (text != null) {
160+
// The selection shouldn't be bigger than the length of the text
161+
if (mSelectionStart > text.length()) {
162+
mSelectionStart = text.length();
163+
}
164+
if (mSelectionEnd > text.length()) {
165+
mSelectionEnd = text.length();
166+
}
167+
} else {
168+
mSelectionStart = UNSET;
169+
mSelectionEnd = UNSET;
170+
}
159171
markUpdated();
160172
}
161173

0 commit comments

Comments
 (0)