Skip to content

Commit ca0c41c

Browse files
committed
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<TextAttributes>`, 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: `<TextInput>` 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 `<Text>` and Android never set it and are unaffected.
1 parent 056e7ee commit ca0c41c

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <React/RCTTextInputComponentView.h>
9+
#import <XCTest/XCTest.h>
10+
#import <react/renderer/components/iostextinput/TextInputProps.h>
11+
#import <react/renderer/components/iostextinput/TextInputShadowNode.h>
12+
13+
using namespace facebook::react;
14+
15+
// Props with the text attributes `<TextInput>` always sends and the given line break mode.
16+
static std::shared_ptr<const TextInputProps> propsWithLineBreakMode(std::optional<LineBreakMode> lineBreakMode)
17+
{
18+
auto props = std::make_shared<TextInputProps>();
19+
props->textAttributes.allowFontScaling = true;
20+
props->textAttributes.lineBreakMode = lineBreakMode;
21+
return props;
22+
}
23+
24+
static UITextField *backedTextField(RCTTextInputComponentView *view)
25+
{
26+
return [view valueForKey:@"_backedTextInputView"];
27+
}
28+
29+
static NSParagraphStyle *defaultParagraphStyle(RCTTextInputComponentView *view)
30+
{
31+
return backedTextField(view).defaultTextAttributes[NSParagraphStyleAttributeName];
32+
}
33+
34+
@interface RCTTextInputComponentViewTests : XCTestCase
35+
@end
36+
37+
@implementation RCTTextInputComponentViewTests
38+
39+
- (void)testChangeOfLineBreakModeAloneSetsDefaultTextAttributes
40+
{
41+
RCTTextInputComponentView *view = [RCTTextInputComponentView new];
42+
auto headProps = propsWithLineBreakMode(LineBreakMode::Head);
43+
[view updateProps:headProps oldProps:nullptr];
44+
XCTAssertEqual(defaultParagraphStyle(view).lineBreakMode, NSLineBreakByTruncatingHead);
45+
46+
auto middleProps = propsWithLineBreakMode(LineBreakMode::Middle);
47+
[view updateProps:middleProps oldProps:headProps];
48+
XCTAssertEqual(defaultParagraphStyle(view).lineBreakMode, NSLineBreakByTruncatingMiddle);
49+
50+
// A recycled view keeps the props of the input it showed before and compares the next input's with them
51+
[view prepareForRecycle];
52+
[view updateProps:propsWithLineBreakMode(std::nullopt) oldProps:nullptr];
53+
XCTAssertNil(defaultParagraphStyle(view));
54+
}
55+
56+
@end

‎packages/react-native/ReactCommon/react/renderer/attributedstring/TextAttributes.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
146146
alignment,
147147
baseWritingDirection,
148148
lineBreakStrategy,
149+
lineBreakMode,
149150
textDecorationColor,
150151
textDecorationLineType,
151152
textDecorationStyle,
@@ -171,6 +172,7 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
171172
rhs.alignment,
172173
rhs.baseWritingDirection,
173174
rhs.lineBreakStrategy,
175+
rhs.lineBreakMode,
174176
rhs.textDecorationColor,
175177
rhs.textDecorationLineType,
176178
rhs.textDecorationStyle,

‎packages/react-native/ReactCommon/react/renderer/attributedstring/tests/TextAttributesTest.cpp‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <gtest/gtest.h>
99
#include <memory>
10+
#include <utility>
11+
#include <vector>
1012

1113
#include <react/featureflags/ReactNativeFeatureFlags.h>
1214
#include <react/featureflags/ReactNativeFeatureFlagsDefaults.h>
@@ -100,4 +102,80 @@ INSTANTIATE_TEST_SUITE_P(
100102
TextAttributesTest,
101103
::testing::Bool());
102104

105+
namespace {
106+
107+
template <typename T, typename V>
108+
void expectFieldAffectsEquality(
109+
const char* fieldName,
110+
T TextAttributes::* field,
111+
V&& value) {
112+
SCOPED_TRACE(fieldName);
113+
TextAttributes baseline;
114+
TextAttributes changed;
115+
changed.*field = std::forward<V>(value);
116+
117+
EXPECT_FALSE(baseline == changed);
118+
}
119+
120+
} // namespace
121+
122+
// A field added to TextAttributes belongs in operator== and in this list.
123+
TEST(TextAttributesEqualityTest, everyFieldAffectsEquality) {
124+
#define EXPECT_FIELD_AFFECTS_EQUALITY(field, value) \
125+
expectFieldAffectsEquality(#field, &TextAttributes::field, value)
126+
127+
// Color
128+
EXPECT_FIELD_AFFECTS_EQUALITY(foregroundColor, blackColor());
129+
EXPECT_FIELD_AFFECTS_EQUALITY(backgroundColor, blackColor());
130+
EXPECT_FIELD_AFFECTS_EQUALITY(opacity, 0.5);
131+
132+
// Font
133+
EXPECT_FIELD_AFFECTS_EQUALITY(fontFamily, "Menlo");
134+
EXPECT_FIELD_AFFECTS_EQUALITY(fontSize, 14);
135+
EXPECT_FIELD_AFFECTS_EQUALITY(fontSizeMultiplier, 2);
136+
EXPECT_FIELD_AFFECTS_EQUALITY(maxFontSizeMultiplier, 2);
137+
EXPECT_FIELD_AFFECTS_EQUALITY(letterSpacing, 1);
138+
EXPECT_FIELD_AFFECTS_EQUALITY(fontVariant, FontVariant::SmallCaps);
139+
EXPECT_FIELD_AFFECTS_EQUALITY(fontVariationSettings, "'wght' 400");
140+
EXPECT_FIELD_AFFECTS_EQUALITY(fontWeight, FontWeight::Bold);
141+
EXPECT_FIELD_AFFECTS_EQUALITY(fontStyle, FontStyle::Italic);
142+
EXPECT_FIELD_AFFECTS_EQUALITY(allowFontScaling, false);
143+
EXPECT_FIELD_AFFECTS_EQUALITY(dynamicTypeRamp, DynamicTypeRamp::Body);
144+
EXPECT_FIELD_AFFECTS_EQUALITY(textTransform, TextTransform::Uppercase);
145+
146+
// Paragraph Styles
147+
EXPECT_FIELD_AFFECTS_EQUALITY(lineHeight, 20);
148+
EXPECT_FIELD_AFFECTS_EQUALITY(alignment, TextAlignment::Center);
149+
EXPECT_FIELD_AFFECTS_EQUALITY(
150+
baseWritingDirection, WritingDirection::RightToLeft);
151+
EXPECT_FIELD_AFFECTS_EQUALITY(lineBreakStrategy, LineBreakStrategy::Standard);
152+
EXPECT_FIELD_AFFECTS_EQUALITY(lineBreakMode, LineBreakMode::Tail);
153+
154+
// Decoration
155+
EXPECT_FIELD_AFFECTS_EQUALITY(textDecorationColor, blackColor());
156+
EXPECT_FIELD_AFFECTS_EQUALITY(
157+
textDecorationLineType, TextDecorationLineType::Underline);
158+
EXPECT_FIELD_AFFECTS_EQUALITY(
159+
textDecorationStyle, TextDecorationStyle::Dashed);
160+
161+
// Shadow
162+
EXPECT_FIELD_AFFECTS_EQUALITY(
163+
textShadowOffset, (Size{.width = 1, .height = 2}));
164+
EXPECT_FIELD_AFFECTS_EQUALITY(textShadowRadius, 1);
165+
EXPECT_FIELD_AFFECTS_EQUALITY(textShadowColor, blackColor());
166+
167+
// Special
168+
EXPECT_FIELD_AFFECTS_EQUALITY(isHighlighted, false);
169+
EXPECT_FIELD_AFFECTS_EQUALITY(isPressable, false);
170+
EXPECT_FIELD_AFFECTS_EQUALITY(layoutDirection, LayoutDirection::RightToLeft);
171+
EXPECT_FIELD_AFFECTS_EQUALITY(accessibilityRole, AccessibilityRole::Button);
172+
EXPECT_FIELD_AFFECTS_EQUALITY(role, Role::Button);
173+
174+
// Text Effects
175+
EXPECT_FIELD_AFFECTS_EQUALITY(
176+
textEffects, (std::vector<TextEffectInfo>{{.name = "effect"}}));
177+
178+
#undef EXPECT_FIELD_AFFECTS_EQUALITY
179+
}
180+
103181
} // namespace facebook::react

0 commit comments

Comments
 (0)