From 6ebd3b046e5b71130281f1a7dbe7220eff95d74a Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Mon, 28 Oct 2019 22:37:12 -0700 Subject: [PATCH] Cap selection indices when text changes (#26680) Summary: This PR https://github.com/facebook/react-native/pull/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 https://github.com/facebook/react-native/issues/25265 and other issues of `setSpan(4 ... 4) ends beyond length` ## Changelog [Android] [fixed] - Crash in TextInput Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- .../views/textinput/ReactTextInputShadowNode.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputShadowNode.java index c0cfd51661626f..eeda18dedf5cba 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputShadowNode.java @@ -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(); }