Skip to content

Commit 6ebd3b0

Browse files
MarcoPolofacebook-github-bot
authored andcommitted
Cap selection indices when text changes (#26680)
Summary: This PR #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 #25265 and other issues of `setSpan(4 ... 4) ends beyond length` ## Changelog [Android] [fixed] - Crash in TextInput Pull Request resolved: #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 0bea6a9 commit 6ebd3b0

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

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

0 commit comments

Comments
 (0)