Skip to content

Commit

Permalink
Cap selection indices when text changes (#26680)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MarcoPolo authored and facebook-github-bot committed Oct 29, 2019
1 parent 0bea6a9 commit 6ebd3b0
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ public void setMostRecentEventCount(int mostRecentEventCount) {
@ReactProp(name = PROP_TEXT)
public void setText(@Nullable String text) {
mText = text;
if (text != null) {
// The selection shouldn't be bigger than the length of the text
if (mSelectionStart > text.length()) {
mSelectionStart = text.length();
}
if (mSelectionEnd > text.length()) {
mSelectionEnd = text.length();
}
} else {
mSelectionStart = UNSET;
mSelectionEnd = UNSET;
}
markUpdated();
}

Expand Down

0 comments on commit 6ebd3b0

Please sign in to comment.