From ca0c41cb173b83a7d73f56d18f6f1b5d2724c63f Mon Sep 17 00:00:00 2001 From: James Ide Date: Tue, 1 Dec 2026 17:48:55 -0800 Subject: [PATCH] Compare lineBreakMode in TextAttributes::operator== `TextAttributes::operator==` compares every field except `lineBreakMode`, so text attributes that differ only in `lineBreakModeIOS` compare equal. #46130 added the field to the struct and to `std::hash`, and left the operator as it was. An iOS `TextInput` decides with this operator whether its text attributes changed. `RCTTextInputComponentView updateProps:` sets the default text attributes of the field, which style the placeholder and typed text, only when the props' text attributes differ from the props the view already has. `BaseTextInputShadowNode::updateStateIfNeeded` gives the view new attributed text only when the attributed string differs from the one in the state. A change of `lineBreakModeIOS` alone therefore reaches neither the default text attributes nor the text. A recycled view keeps the props of the input it showed before, so an input that differs from that one only in `lineBreakModeIOS` keeps the default text attributes of the input before it. A newly created view is not affected: `` sends `allowFontScaling` (`true` unless the app passes another value), which differs from the view's initial props. The operator now compares `lineBreakMode`, so attributes that differ only in it no longer compare equal while hashing differently. `lineBreakModeIOS` is only in the iOS view config of `TextInput`, and only `RCTAttributedTextUtils.mm` turns `lineBreakMode` into a text style, so `` and Android never set it and are unaffected. --- .../Text/RCTTextInputComponentViewTests.mm | 56 +++++++++++++ .../attributedstring/TextAttributes.cpp | 2 + .../tests/TextAttributesTest.cpp | 78 +++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 packages/react-native/React/Tests/Text/RCTTextInputComponentViewTests.mm diff --git a/packages/react-native/React/Tests/Text/RCTTextInputComponentViewTests.mm b/packages/react-native/React/Tests/Text/RCTTextInputComponentViewTests.mm new file mode 100644 index 000000000000..396191cb9ebb --- /dev/null +++ b/packages/react-native/React/Tests/Text/RCTTextInputComponentViewTests.mm @@ -0,0 +1,56 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import +#import +#import +#import + +using namespace facebook::react; + +// Props with the text attributes `` always sends and the given line break mode. +static std::shared_ptr propsWithLineBreakMode(std::optional lineBreakMode) +{ + auto props = std::make_shared(); + props->textAttributes.allowFontScaling = true; + props->textAttributes.lineBreakMode = lineBreakMode; + return props; +} + +static UITextField *backedTextField(RCTTextInputComponentView *view) +{ + return [view valueForKey:@"_backedTextInputView"]; +} + +static NSParagraphStyle *defaultParagraphStyle(RCTTextInputComponentView *view) +{ + return backedTextField(view).defaultTextAttributes[NSParagraphStyleAttributeName]; +} + +@interface RCTTextInputComponentViewTests : XCTestCase +@end + +@implementation RCTTextInputComponentViewTests + +- (void)testChangeOfLineBreakModeAloneSetsDefaultTextAttributes +{ + RCTTextInputComponentView *view = [RCTTextInputComponentView new]; + auto headProps = propsWithLineBreakMode(LineBreakMode::Head); + [view updateProps:headProps oldProps:nullptr]; + XCTAssertEqual(defaultParagraphStyle(view).lineBreakMode, NSLineBreakByTruncatingHead); + + auto middleProps = propsWithLineBreakMode(LineBreakMode::Middle); + [view updateProps:middleProps oldProps:headProps]; + XCTAssertEqual(defaultParagraphStyle(view).lineBreakMode, NSLineBreakByTruncatingMiddle); + + // A recycled view keeps the props of the input it showed before and compares the next input's with them + [view prepareForRecycle]; + [view updateProps:propsWithLineBreakMode(std::nullopt) oldProps:nullptr]; + XCTAssertNil(defaultParagraphStyle(view)); +} + +@end diff --git a/packages/react-native/ReactCommon/react/renderer/attributedstring/TextAttributes.cpp b/packages/react-native/ReactCommon/react/renderer/attributedstring/TextAttributes.cpp index ce756b823d01..5d27865ee84c 100644 --- a/packages/react-native/ReactCommon/react/renderer/attributedstring/TextAttributes.cpp +++ b/packages/react-native/ReactCommon/react/renderer/attributedstring/TextAttributes.cpp @@ -146,6 +146,7 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const { alignment, baseWritingDirection, lineBreakStrategy, + lineBreakMode, textDecorationColor, textDecorationLineType, textDecorationStyle, @@ -171,6 +172,7 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const { rhs.alignment, rhs.baseWritingDirection, rhs.lineBreakStrategy, + rhs.lineBreakMode, rhs.textDecorationColor, rhs.textDecorationLineType, rhs.textDecorationStyle, diff --git a/packages/react-native/ReactCommon/react/renderer/attributedstring/tests/TextAttributesTest.cpp b/packages/react-native/ReactCommon/react/renderer/attributedstring/tests/TextAttributesTest.cpp index b80e5aa8fd9f..5ef486da55fb 100644 --- a/packages/react-native/ReactCommon/react/renderer/attributedstring/tests/TextAttributesTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/attributedstring/tests/TextAttributesTest.cpp @@ -7,6 +7,8 @@ #include #include +#include +#include #include #include @@ -100,4 +102,80 @@ INSTANTIATE_TEST_SUITE_P( TextAttributesTest, ::testing::Bool()); +namespace { + +template +void expectFieldAffectsEquality( + const char* fieldName, + T TextAttributes::* field, + V&& value) { + SCOPED_TRACE(fieldName); + TextAttributes baseline; + TextAttributes changed; + changed.*field = std::forward(value); + + EXPECT_FALSE(baseline == changed); +} + +} // namespace + +// A field added to TextAttributes belongs in operator== and in this list. +TEST(TextAttributesEqualityTest, everyFieldAffectsEquality) { +#define EXPECT_FIELD_AFFECTS_EQUALITY(field, value) \ + expectFieldAffectsEquality(#field, &TextAttributes::field, value) + + // Color + EXPECT_FIELD_AFFECTS_EQUALITY(foregroundColor, blackColor()); + EXPECT_FIELD_AFFECTS_EQUALITY(backgroundColor, blackColor()); + EXPECT_FIELD_AFFECTS_EQUALITY(opacity, 0.5); + + // Font + EXPECT_FIELD_AFFECTS_EQUALITY(fontFamily, "Menlo"); + EXPECT_FIELD_AFFECTS_EQUALITY(fontSize, 14); + EXPECT_FIELD_AFFECTS_EQUALITY(fontSizeMultiplier, 2); + EXPECT_FIELD_AFFECTS_EQUALITY(maxFontSizeMultiplier, 2); + EXPECT_FIELD_AFFECTS_EQUALITY(letterSpacing, 1); + EXPECT_FIELD_AFFECTS_EQUALITY(fontVariant, FontVariant::SmallCaps); + EXPECT_FIELD_AFFECTS_EQUALITY(fontVariationSettings, "'wght' 400"); + EXPECT_FIELD_AFFECTS_EQUALITY(fontWeight, FontWeight::Bold); + EXPECT_FIELD_AFFECTS_EQUALITY(fontStyle, FontStyle::Italic); + EXPECT_FIELD_AFFECTS_EQUALITY(allowFontScaling, false); + EXPECT_FIELD_AFFECTS_EQUALITY(dynamicTypeRamp, DynamicTypeRamp::Body); + EXPECT_FIELD_AFFECTS_EQUALITY(textTransform, TextTransform::Uppercase); + + // Paragraph Styles + EXPECT_FIELD_AFFECTS_EQUALITY(lineHeight, 20); + EXPECT_FIELD_AFFECTS_EQUALITY(alignment, TextAlignment::Center); + EXPECT_FIELD_AFFECTS_EQUALITY( + baseWritingDirection, WritingDirection::RightToLeft); + EXPECT_FIELD_AFFECTS_EQUALITY(lineBreakStrategy, LineBreakStrategy::Standard); + EXPECT_FIELD_AFFECTS_EQUALITY(lineBreakMode, LineBreakMode::Tail); + + // Decoration + EXPECT_FIELD_AFFECTS_EQUALITY(textDecorationColor, blackColor()); + EXPECT_FIELD_AFFECTS_EQUALITY( + textDecorationLineType, TextDecorationLineType::Underline); + EXPECT_FIELD_AFFECTS_EQUALITY( + textDecorationStyle, TextDecorationStyle::Dashed); + + // Shadow + EXPECT_FIELD_AFFECTS_EQUALITY( + textShadowOffset, (Size{.width = 1, .height = 2})); + EXPECT_FIELD_AFFECTS_EQUALITY(textShadowRadius, 1); + EXPECT_FIELD_AFFECTS_EQUALITY(textShadowColor, blackColor()); + + // Special + EXPECT_FIELD_AFFECTS_EQUALITY(isHighlighted, false); + EXPECT_FIELD_AFFECTS_EQUALITY(isPressable, false); + EXPECT_FIELD_AFFECTS_EQUALITY(layoutDirection, LayoutDirection::RightToLeft); + EXPECT_FIELD_AFFECTS_EQUALITY(accessibilityRole, AccessibilityRole::Button); + EXPECT_FIELD_AFFECTS_EQUALITY(role, Role::Button); + + // Text Effects + EXPECT_FIELD_AFFECTS_EQUALITY( + textEffects, (std::vector{{.name = "effect"}})); + +#undef EXPECT_FIELD_AFFECTS_EQUALITY +} + } // namespace facebook::react