Add a nil check to prevent a crash (#35941)

Summary:
This is a [change](https://github.com/microsoft/react-native-macos/pull/1120) we made in our fork (React Native macOS) that we are now upstreaming to reduce the number of diffs between React Native Core and React Native macOS. Also.. one less crash �!

Resolves https://github.com/microsoft/react-native-macos/issues/1679

Original PR notes:

> We've seen a crash downstream where -[NSString stringByReplacingCharactersInRange:withString:] receives a nil value as the replacement string. This is not good, since we expect that argument to be non-null.
>
>We believe that a cause of this is that -[RCTUITextField textView:shouldChangeTextInRange:replacementString:] is being called with nil as the replacement string. (This is legal, as per [Apple's documentation](https://developer.apple.com/documentation/appkit/nstextviewdelegate/1449325-textview?language=objc).) Right now, the only check that this delegate method does is enforcing the maxLength parameter if it exists, and changes in attributes shouldn't affect the length of the string.

## Changelog

[IOS] [FIXED] - `-[RCTUITextField textView:shouldChangeTextInRange:replacementString:]` no longer crashes when we pass in a `nil` replacement string

Pull Request resolved: https://github.com/facebook/react-native/pull/35941

Test Plan: Build should pass. This change has been running in our fork in production for a while so we're fairly confident of it.

Reviewed By: cipolleschi

Differential Revision: D42705382

Pulled By: jacdebug

fbshipit-source-id: 066cd8a4ba134a681f0f4c955594b1fcda61a30e
This commit is contained in:
Saad Najmi 2023-01-24 03:57:45 -08:00 коммит произвёл Facebook GitHub Bot
Родитель b8f1bb50f7
Коммит d5e6d9cecd
1 изменённых файлов: 1 добавлений и 1 удалений

Просмотреть файл

@ -458,7 +458,7 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
if (range.location + range.length > backedTextInputView.attributedText.string.length) {
_predictedText = backedTextInputView.attributedText.string;
} else {
} else if (text != nil) {
_predictedText = [backedTextInputView.attributedText.string stringByReplacingCharactersInRange:range
withString:text];
}