From 72dd1cf49478f58b428087734d2f5aafaf2ed4c1 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Fri, 15 Feb 2019 09:49:54 +0800 Subject: [PATCH 1/3] [iOS] Fix TextInput maxLength when insert characters at begin --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index 231680750dcd..3b6046af44b4 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -338,11 +338,7 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin NSString *previousText = [_predictedText substringWithRange:range] ?: @""; - if (!_predictedText || backedTextInputView.attributedText.string.length == 0) { - _predictedText = text; - } else { - _predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text]; - } + _predictedText = [backedTextInputView.attributedText.string stringByReplacingCharactersInRange:range withString:text]; if (_onTextInput) { _onTextInput(@{ From e8d5c5f40d1f0abd7e362a7e6113e083a66b1efe Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Thu, 7 Mar 2019 15:36:31 +0800 Subject: [PATCH 2/3] Clean up _predictedText and fix previousText --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index 3b6046af44b4..7e1fb6bb1b95 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -330,13 +330,7 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin } } - if (range.location + range.length > _predictedText.length) { - // _predictedText got out of sync in a bad way, so let's just force sync it. Haven't been able to repro this, but - // it's causing a real crash here: #6523822 - _predictedText = backedTextInputView.attributedText.string; - } - - NSString *previousText = [_predictedText substringWithRange:range] ?: @""; + NSString *previousText = backedTextInputView.attributedText.string ?: @""; _predictedText = [backedTextInputView.attributedText.string stringByReplacingCharactersInRange:range withString:text]; @@ -372,7 +366,6 @@ - (void)textInputDidChange [self textInputShouldChangeTextInRange:predictionRange replacementText:replacement]; // JS will assume the selection changed based on the location of our shouldChangeTextInRange, so reset it. [self textInputDidChangeSelection]; - _predictedText = backedTextInputView.attributedText.string; } _nativeEventCount++; From afe7a9c5670705b017062768c7945df4071e7e29 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Fri, 8 Mar 2019 09:22:05 +0800 Subject: [PATCH 3/3] Fix tests --- Libraries/Text/TextInput/RCTBaseTextInputView.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Libraries/Text/TextInput/RCTBaseTextInputView.m b/Libraries/Text/TextInput/RCTBaseTextInputView.m index 7e1fb6bb1b95..eb4127763366 100644 --- a/Libraries/Text/TextInput/RCTBaseTextInputView.m +++ b/Libraries/Text/TextInput/RCTBaseTextInputView.m @@ -331,8 +331,12 @@ - (BOOL)textInputShouldChangeTextInRange:(NSRange)range replacementText:(NSStrin } NSString *previousText = backedTextInputView.attributedText.string ?: @""; - - _predictedText = [backedTextInputView.attributedText.string stringByReplacingCharactersInRange:range withString:text]; + + if (range.location + range.length > backedTextInputView.attributedText.string.length) { + _predictedText = backedTextInputView.attributedText.string; + } else { + _predictedText = [backedTextInputView.attributedText.string stringByReplacingCharactersInRange:range withString:text]; + } if (_onTextInput) { _onTextInput(@{