Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ @implementation RCTTextInputComponentView {
UIView<RCTBackedTextInputViewProtocol, RCTBackedTextInputViewPendingEdit> *_backedTextInputView;
NSUInteger _mostRecentEventCount;
NSAttributedString *_lastStringStateWasUpdatedWith;
// The React styles currently applied to the multiline input.
NSAttributedString *_lastAppliedMultilineReactText;

/*
* UIKit uses either UITextField or UITextView as its UIKit element for <TextInput>. UITextField is for single line
Expand Down Expand Up @@ -364,6 +366,7 @@ - (void)updateState:(const State::Shared &)state oldState:(const State::Shared &

if (!_state) {
assert(false && "State is `null` for <TextInput> component.");
_lastAppliedMultilineReactText = nil;
_backedTextInputView.attributedText = nil;
return;
}
Expand All @@ -376,11 +379,23 @@ - (void)updateState:(const State::Shared &)state oldState:(const State::Shared &

if (_mostRecentEventCount == _state->getData().mostRecentEventCount) {
NSAttributedString *attributedString = RCTNSAttributedStringFromAttributedStringBox(data.attributedStringBox);
if (![attributedString.string isEqualToString:_backedTextInputView.attributedText.string]) {
NSAttributedString *nativeText = _backedTextInputView.attributedText;
BOOL sameText = [attributedString.string isEqualToString:nativeText.string];
if (!sameText) {
[_backedTextInputView rct_invalidatePendingTextInputEdit];
}
_comingFromJS = YES;
[self _setAttributedString:attributedString];
BOOL tracksReactStyle = [_backedTextInputView isKindOfClass:[RCTUITextView class]] &&
data.attributedStringBox.getMode() == AttributedStringBox::Mode::Value;
if (!(sameText && tracksReactStyle &&
[self _applyMultilineStyleUpdate:attributedString previousReactText:_lastAppliedMultilineReactText])) {
if ([self _setAttributedString:attributedString]) {
_lastAppliedMultilineReactText = tracksReactStyle ? attributedString : nil;
} else if (
sameText && tracksReactStyle && [self _multilineReactText:attributedString matchesNativeText:nativeText]) {
_lastAppliedMultilineReactText = attributedString;
}
}
_comingFromJS = NO;
}
}
Expand Down Expand Up @@ -410,6 +425,7 @@ - (void)prepareForRecycle
_mostRecentEventCount = 0;
_comingFromJS = NO;
_lastStringStateWasUpdatedWith = nil;
_lastAppliedMultilineReactText = nil;
_ignoreNextTextInputCall = NO;
_didMoveToWindow = NO;
_backedTextInputView.inputAccessoryViewID = nil;
Expand Down Expand Up @@ -629,6 +645,7 @@ - (void)focus
const auto &props = static_cast<const TextInputProps &>(*_props);

if (props.traits.clearTextOnFocus) {
_lastAppliedMultilineReactText = nil;
_backedTextInputView.attributedText = nil;
[self textInputDidChange];
}
Expand Down Expand Up @@ -659,7 +676,9 @@ - (void)setTextAndSelection:(NSInteger)eventCount
if (value && ![value isEqualToString:_backedTextInputView.attributedText.string]) {
NSAttributedString *attributedString =
[[NSAttributedString alloc] initWithString:value attributes:_backedTextInputView.defaultTextAttributes];
[self _setAttributedString:attributedString];
if ([self _setAttributedString:attributedString]) {
_lastAppliedMultilineReactText = nil;
}
[self _updateState];
}

Expand Down Expand Up @@ -809,6 +828,10 @@ - (void)_updateState
return;
}
NSAttributedString *attributedString = _backedTextInputView.attributedText;
if (_lastAppliedMultilineReactText &&
![_lastAppliedMultilineReactText.string isEqualToString:attributedString.string]) {
_lastAppliedMultilineReactText = nil;
}
auto data = _state->getData();
_lastStringStateWasUpdatedWith = attributedString;
data.attributedStringBox = RCTAttributedStringBoxFromNSAttributedString(attributedString);
Expand Down Expand Up @@ -848,10 +871,10 @@ - (void)_restoreTextSelectionAndIgnoreCaretChange:(BOOL)ignore
[_backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
}

- (void)_setAttributedString:(NSAttributedString *)attributedString
- (BOOL)_setAttributedString:(NSAttributedString *)attributedString
{
if ([self _textOf:attributedString equals:_backedTextInputView.attributedText]) {
return;
return NO;
}
UITextRange *selectedRange = _backedTextInputView.selectedTextRange;
NSInteger oldTextLength = _backedTextInputView.attributedText.string.length;
Expand Down Expand Up @@ -882,6 +905,98 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString
[self _restoreTextSelectionAndIgnoreCaretChange:YES];
[self _updateTypingAttributes];
_lastStringStateWasUpdatedWith = attributedString;
return YES;
}

- (BOOL)_multilineReactText:(NSAttributedString *)text matchesNativeText:(NSAttributedString *)nativeText
{
if ([self _shouldCompareTextWithoutAttributes]) {
return NO;
}

// A native edit can leave UIKit's fallback font on a React text echo.
__block NSMutableAttributedString *comparableText = nil;
[nativeText enumerateAttribute:@"NSOriginalFont"
inRange:NSMakeRange(0, nativeText.length)
options:0
usingBlock:^(id font, NSRange range, BOOL *stop) {
if (font) {
if (!comparableText) {
comparableText = [nativeText mutableCopy];
}
[comparableText addAttribute:NSFontAttributeName value:font range:range];
[comparableText removeAttribute:@"NSOriginalFont" range:range];
}
}];
return RCTIsAttributedStringEffectivelySame(
text,
comparableText ?: nativeText,
_originalTypingAttributes,
static_cast<const TextInputProps &>(*_props).textAttributes);
}

- (BOOL)_applyMultilineStyleUpdate:(NSAttributedString *)text previousReactText:(NSAttributedString *)previousReactText
{
if (![text.string isEqualToString:previousReactText.string]) {
return NO;
}
if ([self _shouldCompareTextWithoutAttributes]) {
return YES;
}

RCTUITextView *textView = (RCTUITextView *)_backedTextInputView;
NSTextStorage *storage = textView.textStorage;
NSRange selection = textView.selectedRange;
CGPoint contentOffset = textView.contentOffset;
BOOL edited = NO;
[storage beginEditing];
// Compare two React snapshots so UIKit's fallback fonts and transient attributes do not cause an edit.
for (NSUInteger location = 0; location < text.length;) {
NSRange remaining = NSMakeRange(location, text.length - location);
NSRange currentRange;
NSRange previousRange;
NSDictionary<NSAttributedStringKey, id> *attributes = [text attributesAtIndex:location
longestEffectiveRange:&currentRange
inRange:remaining];
NSDictionary<NSAttributedStringKey, id> *previousAttributes = [previousReactText attributesAtIndex:location
longestEffectiveRange:&previousRange
inRange:remaining];
NSRange range = NSIntersectionRange(currentRange, previousRange);

for (NSAttributedStringKey key in attributes) {
if ([key isEqualToString:RCTAttributedStringEventEmitterKey] ||
[attributes[key] isEqual:previousAttributes[key]]) {
continue;
}
if ([key isEqualToString:NSFontAttributeName]) {
[storage removeAttribute:@"NSOriginalFont" range:range];
}
[storage addAttribute:key value:attributes[key] range:range];
edited = YES;
}
for (NSAttributedStringKey key in previousAttributes) {
if (attributes[key] == nil && ![key isEqualToString:RCTAttributedStringEventEmitterKey]) {
[storage removeAttribute:key range:range];
edited = YES;
}
}
location = NSMaxRange(range);
}
[storage endEditing];

if (edited) {
if (selection.location != NSNotFound && !NSEqualRanges(selection, textView.selectedRange)) {
textView.selectedRange = selection;
}
[self _restoreTextSelection];
if (!CGPointEqualToPoint(contentOffset, textView.contentOffset)) {
textView.contentOffset = contentOffset;
}
[self _updateTypingAttributes];
_lastStringStateWasUpdatedWith = textView.attributedText;
}
_lastAppliedMultilineReactText = text;
return YES;
}

// Ensure that newly typed text will inherit any custom attributes. We follow the logic of RN Android, where attributes
Expand Down Expand Up @@ -912,6 +1027,7 @@ - (void)scrollCursorIntoView

- (void)_setMultiline:(BOOL)multiline
{
_lastAppliedMultilineReactText = nil;
[_backedTextInputView removeFromSuperview];
UIView<RCTBackedTextInputViewProtocol, RCTBackedTextInputViewPendingEdit> *backedTextInputView =
multiline ? [RCTUITextView new] : [RCTUITextField new];
Expand All @@ -938,7 +1054,7 @@ - (void)_setShowSoftInputOnFocus:(BOOL)showSoftInputOnFocus
}
}

- (BOOL)_textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldText
- (BOOL)_shouldCompareTextWithoutAttributes
{
// When the dictation is running we can't update the attributed text on the backed up text view
// because setting the attributed string will kill the dictation. This means that we can't impose
Expand All @@ -947,9 +1063,15 @@ - (BOOL)_textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldTe
// text that we should disregard. See
// https://developer.apple.com/documentation/uikit/uitextinput/1614489-markedtextrange?language=objc for more info.
// Also, updating the attributed text while inputting Korean language will break input mechanism.
// If the user added an emoji, the system adds a font attribute for the emoji and stores the original font in
// NSOriginalFont. Lastly, when entering a password, etc., there will be additional styling on the field as the native
// text view handles showing the last character for a split second.
// Secure text entry adds styling while showing the last character for a split second.
return [_backedTextInputView.textInputMode.primaryLanguage isEqualToString:@"dictation"] ||
[_backedTextInputView.textInputMode.primaryLanguage isEqualToString:@"ko-KR"] ||
_backedTextInputView.markedTextRange || _backedTextInputView.isSecureTextEntry;
}

- (BOOL)_textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldText
{
// When UIKit substitutes a fallback font, it stores the requested font under NSOriginalFont.
__block BOOL fontHasBeenUpdatedBySystem = false;
[oldText enumerateAttribute:@"NSOriginalFont"
inRange:NSMakeRange(0, oldText.length)
Expand All @@ -960,12 +1082,7 @@ - (BOOL)_textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldTe
}
}];

BOOL shouldFallbackToBareTextComparison =
[_backedTextInputView.textInputMode.primaryLanguage isEqualToString:@"dictation"] ||
[_backedTextInputView.textInputMode.primaryLanguage isEqualToString:@"ko-KR"] ||
_backedTextInputView.markedTextRange || _backedTextInputView.isSecureTextEntry || fontHasBeenUpdatedBySystem;

if (shouldFallbackToBareTextComparison) {
if ([self _shouldCompareTextWithoutAttributes] || fontHasBeenUpdatedBySystem) {
return [newText.string isEqualToString:oldText.string];
} else {
return RCTIsAttributedStringEffectivelySame(
Expand Down
4 changes: 4 additions & 0 deletions packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
602160B9478DAFF2BAB56595 /* RCTUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D5D13FB1445C5C19639921CC /* RCTUtilsTests.m */; };
79B29C2E2E607A99007612A5 /* SceneDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 79B29C2D2E607A99007612A5 /* SceneDelegate.mm */; };
7F806D4D4D97AD6403558B35 /* RCTStatusBarAppearanceTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 67B0814814F7C07E196BC295 /* RCTStatusBarAppearanceTests.mm */; };
FB66101562BC48A6B52F9860 /* RCTTextInputComponentViewTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3CC79368B7744ABBB0DCABBC /* RCTTextInputComponentViewTests.mm */; };
A8717727467541429C262C63 /* RCTTimingClockChangeTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = A8717727467541429C262C64 /* RCTTimingClockChangeTests.mm */; };
8145AE06241172D900A3F8DA /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8145AE05241172D900A3F8DA /* LaunchScreen.storyboard */; };
832F45BB2A8A6E1F0097B4E6 /* SwiftTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 832F45BA2A8A6E1F0097B4E6 /* SwiftTest.swift */; };
Expand Down Expand Up @@ -91,6 +92,7 @@
51BC9297B6C3163C14532020 /* Pods-RNTester.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNTester.release.xcconfig"; path = "Target Support Files/Pods-RNTester/Pods-RNTester.release.xcconfig"; sourceTree = "<group>"; };
5C60EB1B226440DB0018C04F /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AppDelegate.mm; path = RNTester/AppDelegate.mm; sourceTree = "<group>"; };
67B0814814F7C07E196BC295 /* RCTStatusBarAppearanceTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTStatusBarAppearanceTests.mm; sourceTree = "<group>"; };
3CC79368B7744ABBB0DCABBC /* RCTTextInputComponentViewTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTTextInputComponentViewTests.mm; sourceTree = "<group>"; };
A8717727467541429C262C64 /* RCTTimingClockChangeTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RCTTimingClockChangeTests.mm; sourceTree = "<group>"; };
79B29C2C2E607A99007612A5 /* SceneDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SceneDelegate.h; path = RNTester/SceneDelegate.h; sourceTree = "<group>"; };
79B29C2D2E607A99007612A5 /* SceneDelegate.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; name = SceneDelegate.mm; path = RNTester/SceneDelegate.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -323,6 +325,7 @@
D5D13FB1445C5C19639921CC /* RCTUtilsTests.m */,
E7DB20B322B2BAA4005AC45F /* RNTesterUnitTestsBundle.js */,
67B0814814F7C07E196BC295 /* RCTStatusBarAppearanceTests.mm */,
3CC79368B7744ABBB0DCABBC /* RCTTextInputComponentViewTests.mm */,
A8717727467541429C262C64 /* RCTTimingClockChangeTests.mm */,
);
path = RNTesterUnitTests;
Expand Down Expand Up @@ -644,6 +647,7 @@
E7DB20D822B2BAA6005AC45F /* RCTJSONTests.m in Sources */,
602160B9478DAFF2BAB56595 /* RCTUtilsTests.m in Sources */,
7F806D4D4D97AD6403558B35 /* RCTStatusBarAppearanceTests.mm in Sources */,
FB66101562BC48A6B52F9860 /* RCTTextInputComponentViewTests.mm in Sources */,
A8717727467541429C262C63 /* RCTTimingClockChangeTests.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Loading
Loading