Skip to content

Commit a2ad2e6

Browse files
committed
Fix iOS multiline TextInput styling after stale events
Compare styles with the last React snapshot applied to the native multiline input so rejected updates cannot hide later style changes.
1 parent 8c7c463 commit a2ad2e6

2 files changed

Lines changed: 101 additions & 17 deletions

File tree

‎packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm‎

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ @implementation RCTTextInputComponentView {
6262
UIView<RCTBackedTextInputViewProtocol, RCTBackedTextInputViewPendingEdit> *_backedTextInputView;
6363
NSUInteger _mostRecentEventCount;
6464
NSAttributedString *_lastStringStateWasUpdatedWith;
65+
// The React styles currently applied to the multiline input.
66+
NSAttributedString *_lastAppliedMultilineReactText;
6567

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

365367
if (!_state) {
366368
assert(false && "State is `null` for <TextInput> component.");
369+
_lastAppliedMultilineReactText = nil;
367370
_backedTextInputView.attributedText = nil;
368371
return;
369372
}
@@ -376,21 +379,22 @@ - (void)updateState:(const State::Shared &)state oldState:(const State::Shared &
376379

377380
if (_mostRecentEventCount == _state->getData().mostRecentEventCount) {
378381
NSAttributedString *attributedString = RCTNSAttributedStringFromAttributedStringBox(data.attributedStringBox);
379-
BOOL sameText = [attributedString.string isEqualToString:_backedTextInputView.attributedText.string];
382+
NSAttributedString *nativeText = _backedTextInputView.attributedText;
383+
BOOL sameText = [attributedString.string isEqualToString:nativeText.string];
380384
if (!sameText) {
381385
[_backedTextInputView rct_invalidatePendingTextInputEdit];
382386
}
383387
_comingFromJS = YES;
384-
BOOL handled = NO;
385-
if (sameText && oldState && [_backedTextInputView isKindOfClass:[RCTUITextView class]] &&
386-
data.attributedStringBox.getMode() == AttributedStringBox::Mode::Value) {
387-
const auto &oldData = static_cast<const TextInputShadowNode::ConcreteState &>(*oldState).getData();
388-
handled = [self
389-
_applyMultilineStyleUpdate:attributedString
390-
previousReactText:RCTNSAttributedStringFromAttributedString(oldData.reactTreeAttributedString)];
391-
}
392-
if (!handled) {
393-
[self _setAttributedString:attributedString];
388+
BOOL tracksReactStyle = [_backedTextInputView isKindOfClass:[RCTUITextView class]] &&
389+
data.attributedStringBox.getMode() == AttributedStringBox::Mode::Value;
390+
if (!(sameText && tracksReactStyle &&
391+
[self _applyMultilineStyleUpdate:attributedString previousReactText:_lastAppliedMultilineReactText])) {
392+
if ([self _setAttributedString:attributedString]) {
393+
_lastAppliedMultilineReactText = tracksReactStyle ? attributedString : nil;
394+
} else if (
395+
sameText && tracksReactStyle && [self _multilineReactText:attributedString matchesNativeText:nativeText]) {
396+
_lastAppliedMultilineReactText = attributedString;
397+
}
394398
}
395399
_comingFromJS = NO;
396400
}
@@ -421,6 +425,7 @@ - (void)prepareForRecycle
421425
_mostRecentEventCount = 0;
422426
_comingFromJS = NO;
423427
_lastStringStateWasUpdatedWith = nil;
428+
_lastAppliedMultilineReactText = nil;
424429
_ignoreNextTextInputCall = NO;
425430
_didMoveToWindow = NO;
426431
_backedTextInputView.inputAccessoryViewID = nil;
@@ -640,6 +645,7 @@ - (void)focus
640645
const auto &props = static_cast<const TextInputProps &>(*_props);
641646

642647
if (props.traits.clearTextOnFocus) {
648+
_lastAppliedMultilineReactText = nil;
643649
_backedTextInputView.attributedText = nil;
644650
[self textInputDidChange];
645651
}
@@ -670,7 +676,9 @@ - (void)setTextAndSelection:(NSInteger)eventCount
670676
if (value && ![value isEqualToString:_backedTextInputView.attributedText.string]) {
671677
NSAttributedString *attributedString =
672678
[[NSAttributedString alloc] initWithString:value attributes:_backedTextInputView.defaultTextAttributes];
673-
[self _setAttributedString:attributedString];
679+
if ([self _setAttributedString:attributedString]) {
680+
_lastAppliedMultilineReactText = nil;
681+
}
674682
[self _updateState];
675683
}
676684
@@ -820,6 +828,10 @@ - (void)_updateState
820828
return;
821829
}
822830
NSAttributedString *attributedString = _backedTextInputView.attributedText;
831+
if (_lastAppliedMultilineReactText &&
832+
![_lastAppliedMultilineReactText.string isEqualToString:attributedString.string]) {
833+
_lastAppliedMultilineReactText = nil;
834+
}
823835
auto data = _state->getData();
824836
_lastStringStateWasUpdatedWith = attributedString;
825837
data.attributedStringBox = RCTAttributedStringBoxFromNSAttributedString(attributedString);
@@ -859,10 +871,10 @@ - (void)_restoreTextSelectionAndIgnoreCaretChange:(BOOL)ignore
859871
[_backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
860872
}
861873
862-
- (void)_setAttributedString:(NSAttributedString *)attributedString
874+
- (BOOL)_setAttributedString:(NSAttributedString *)attributedString
863875
{
864876
if ([self _textOf:attributedString equals:_backedTextInputView.attributedText]) {
865-
return;
877+
return NO;
866878
}
867879
UITextRange *selectedRange = _backedTextInputView.selectedTextRange;
868880
NSInteger oldTextLength = _backedTextInputView.attributedText.string.length;
@@ -893,6 +905,34 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString
893905
[self _restoreTextSelectionAndIgnoreCaretChange:YES];
894906
[self _updateTypingAttributes];
895907
_lastStringStateWasUpdatedWith = attributedString;
908+
return YES;
909+
}
910+
911+
- (BOOL)_multilineReactText:(NSAttributedString *)text matchesNativeText:(NSAttributedString *)nativeText
912+
{
913+
if ([self _shouldCompareTextWithoutAttributes]) {
914+
return NO;
915+
}
916+
917+
// A native edit can leave UIKit's fallback font on a React text echo.
918+
__block NSMutableAttributedString *comparableText = nil;
919+
[nativeText enumerateAttribute:@"NSOriginalFont"
920+
inRange:NSMakeRange(0, nativeText.length)
921+
options:0
922+
usingBlock:^(id font, NSRange range, BOOL *stop) {
923+
if (font) {
924+
if (!comparableText) {
925+
comparableText = [nativeText mutableCopy];
926+
}
927+
[comparableText addAttribute:NSFontAttributeName value:font range:range];
928+
[comparableText removeAttribute:@"NSOriginalFont" range:range];
929+
}
930+
}];
931+
return RCTIsAttributedStringEffectivelySame(
932+
text,
933+
comparableText ?: nativeText,
934+
_originalTypingAttributes,
935+
static_cast<const TextInputProps &>(*_props).textAttributes);
896936
}
897937
898938
- (BOOL)_applyMultilineStyleUpdate:(NSAttributedString *)text previousReactText:(NSAttributedString *)previousReactText
@@ -955,6 +995,7 @@ - (BOOL)_applyMultilineStyleUpdate:(NSAttributedString *)text previousReactText:
955995
[self _updateTypingAttributes];
956996
_lastStringStateWasUpdatedWith = textView.attributedText;
957997
}
998+
_lastAppliedMultilineReactText = text;
958999
return YES;
9591000
}
9601001
@@ -986,6 +1027,7 @@ - (void)scrollCursorIntoView
9861027
9871028
- (void)_setMultiline:(BOOL)multiline
9881029
{
1030+
_lastAppliedMultilineReactText = nil;
9891031
[_backedTextInputView removeFromSuperview];
9901032
UIView<RCTBackedTextInputViewProtocol, RCTBackedTextInputViewPendingEdit> *backedTextInputView =
9911033
multiline ? [RCTUITextView new] : [RCTUITextField new];

‎packages/rn-tester/RNTesterUnitTests/RCTTextInputComponentViewTests.mm‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ - (void)testEquivalentReactStylesLeaveNativeStorageUntouched
131131
[component updateState:initial oldState:nullptr];
132132
133133
NSTextStorage *storage = textView.textStorage;
134+
[textView setSelectedRange:NSMakeRange(2, 3)];
134135
[storage addAttribute:@"EventEmitter" value:@1 range:NSMakeRange(0, 5)];
135136
NSAttributedString *snapshot = [storage copy];
136-
[textView setSelectedRange:NSMakeRange(2, 3)];
137137
138138
// The second native range has the same rendered style but a different React event target.
139139
text.getFragments().back().parentShadowView.componentHandle = 1;
@@ -144,18 +144,60 @@ - (void)testEquivalentReactStylesLeaveNativeStorageUntouched
144144
XCTAssertTrue(NSEqualRanges(textView.selectedRange, NSMakeRange(2, 3)));
145145
}
146146
147+
- (void)testMultilineStyleUpdateAfterNativeTextEchoWithFallback
148+
{
149+
RCTTextInputComponentView *component = multilineInput();
150+
RCTUITextView *textView = (RCTUITextView *)component.accessibilityElement;
151+
auto text = styledText(false, false);
152+
text.getFragments().pop_back();
153+
auto initial = textState(text);
154+
[component updateState:initial oldState:nullptr];
155+
156+
NSMutableAttributedString *nativeText = [textView.attributedText mutableCopy];
157+
[nativeText appendAttributedString:[[NSAttributedString alloc] initWithString:@" ✅ world"
158+
attributes:[nativeText attributesAtIndex:0
159+
effectiveRange:NULL]]];
160+
textView.attributedText = nativeText;
161+
object_setClass(textView, [RCTTextInputFontFallbackView class]);
162+
XCTAssertNotNil([textView.attributedText attribute:@"NSOriginalFont" atIndex:6 effectiveRange:NULL]);
163+
164+
auto echo = textState(styledText(false, false), initial, 1);
165+
[component updateState:echo oldState:initial];
166+
auto updated = textState(styledText(true, false), echo, 1);
167+
[component updateState:updated oldState:echo];
168+
169+
XCTAssertEqualObjects(
170+
[textView.textStorage attribute:NSForegroundColorAttributeName atIndex:0 effectiveRange:NULL], UIColor.redColor);
171+
}
172+
147173
- (void)testMultilineStyleUpdateIgnoresStaleState
148174
{
149175
RCTTextInputComponentView *component = multilineInput();
150176
RCTUITextView *textView = (RCTUITextView *)component.accessibilityElement;
151-
auto initial = textState(styledText(false), nullptr, 1);
177+
TextAttributes attributes;
178+
attributes.foregroundColor = blackColor();
179+
AttributedString text;
180+
text.appendFragment({.string = "Hello world", .textAttributes = attributes});
181+
auto initial = textState(text, nullptr, 1);
152182
[component updateState:initial oldState:nullptr];
153183
154184
NSAttributedString *snapshot = [textView.attributedText copy];
155-
auto stale = textState(styledText(true), initial);
185+
auto &fragment = text.getFragments().front();
186+
fragment.textAttributes.foregroundColor = colorFromRGBA(255, 0, 0, 255);
187+
auto stale = textState(text, initial, 0);
156188
[component updateState:stale oldState:initial];
157189
158190
XCTAssertEqualObjects(textView.attributedText, snapshot);
191+
192+
fragment.textAttributes.textDecorationLineType = TextDecorationLineType::Underline;
193+
auto updated = textState(text, stale, 1);
194+
[component updateState:updated oldState:stale];
195+
196+
NSTextStorage *storage = textView.textStorage;
197+
XCTAssertEqualObjects(
198+
[storage attribute:NSForegroundColorAttributeName atIndex:0 effectiveRange:NULL], UIColor.redColor);
199+
XCTAssertEqualObjects(
200+
[storage attribute:NSUnderlineStyleAttributeName atIndex:0 effectiveRange:NULL], @(NSUnderlineStyleSingle));
159201
}
160202
161203
@end

0 commit comments

Comments
 (0)