Skip to content

Commit 6ba1733

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 94aee19 commit 6ba1733

3 files changed

Lines changed: 150 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
@@ -137,6 +137,7 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
137137
alignment,
138138
baseWritingDirection,
139139
lineBreakStrategy,
140+
lineBreakMode,
140141
textDecorationColor,
141142
textDecorationLineType,
142143
textDecorationStyle,
@@ -162,6 +163,7 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
162163
rhs.alignment,
163164
rhs.baseWritingDirection,
164165
rhs.lineBreakStrategy,
166+
rhs.lineBreakMode,
165167
rhs.textDecorationColor,
166168
rhs.textDecorationLineType,
167169
rhs.textDecorationStyle,
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
#include <gtest/gtest.h>
9+
10+
#include <utility>
11+
#include <vector>
12+
13+
#include <react/renderer/attributedstring/TextAttributes.h>
14+
15+
namespace facebook::react {
16+
namespace {
17+
18+
template <typename T, typename V>
19+
void expectFieldAffectsEquality(
20+
const char* fieldName,
21+
T TextAttributes::* field,
22+
V&& value) {
23+
SCOPED_TRACE(fieldName);
24+
TextAttributes baseline;
25+
TextAttributes changed;
26+
changed.*field = std::forward<V>(value);
27+
28+
EXPECT_FALSE(baseline == changed);
29+
}
30+
31+
} // namespace
32+
33+
// A field added to TextAttributes belongs in operator== and in this list.
34+
TEST(TextAttributesTest, everyFieldAffectsEquality) {
35+
#define EXPECT_FIELD_AFFECTS_EQUALITY(field, value) \
36+
expectFieldAffectsEquality(#field, &TextAttributes::field, value)
37+
38+
// Color
39+
EXPECT_FIELD_AFFECTS_EQUALITY(foregroundColor, blackColor());
40+
EXPECT_FIELD_AFFECTS_EQUALITY(backgroundColor, blackColor());
41+
EXPECT_FIELD_AFFECTS_EQUALITY(opacity, 0.5);
42+
43+
// Font
44+
EXPECT_FIELD_AFFECTS_EQUALITY(fontFamily, "Menlo");
45+
EXPECT_FIELD_AFFECTS_EQUALITY(fontSize, 14);
46+
EXPECT_FIELD_AFFECTS_EQUALITY(fontSizeMultiplier, 2);
47+
EXPECT_FIELD_AFFECTS_EQUALITY(maxFontSizeMultiplier, 2);
48+
EXPECT_FIELD_AFFECTS_EQUALITY(letterSpacing, 1);
49+
EXPECT_FIELD_AFFECTS_EQUALITY(fontVariant, FontVariant::SmallCaps);
50+
EXPECT_FIELD_AFFECTS_EQUALITY(fontVariationSettings, "'wght' 400");
51+
EXPECT_FIELD_AFFECTS_EQUALITY(fontWeight, FontWeight::Bold);
52+
EXPECT_FIELD_AFFECTS_EQUALITY(fontStyle, FontStyle::Italic);
53+
EXPECT_FIELD_AFFECTS_EQUALITY(allowFontScaling, false);
54+
EXPECT_FIELD_AFFECTS_EQUALITY(dynamicTypeRamp, DynamicTypeRamp::Body);
55+
EXPECT_FIELD_AFFECTS_EQUALITY(textTransform, TextTransform::Uppercase);
56+
57+
// Paragraph Styles
58+
EXPECT_FIELD_AFFECTS_EQUALITY(lineHeight, 20);
59+
EXPECT_FIELD_AFFECTS_EQUALITY(alignment, TextAlignment::Center);
60+
EXPECT_FIELD_AFFECTS_EQUALITY(
61+
baseWritingDirection, WritingDirection::RightToLeft);
62+
EXPECT_FIELD_AFFECTS_EQUALITY(lineBreakStrategy, LineBreakStrategy::Standard);
63+
EXPECT_FIELD_AFFECTS_EQUALITY(lineBreakMode, LineBreakMode::Tail);
64+
65+
// Decoration
66+
EXPECT_FIELD_AFFECTS_EQUALITY(textDecorationColor, blackColor());
67+
EXPECT_FIELD_AFFECTS_EQUALITY(
68+
textDecorationLineType, TextDecorationLineType::Underline);
69+
EXPECT_FIELD_AFFECTS_EQUALITY(
70+
textDecorationStyle, TextDecorationStyle::Dashed);
71+
72+
// Shadow
73+
EXPECT_FIELD_AFFECTS_EQUALITY(
74+
textShadowOffset, (Size{.width = 1, .height = 2}));
75+
EXPECT_FIELD_AFFECTS_EQUALITY(textShadowRadius, 1);
76+
EXPECT_FIELD_AFFECTS_EQUALITY(textShadowColor, blackColor());
77+
78+
// Special
79+
EXPECT_FIELD_AFFECTS_EQUALITY(isHighlighted, false);
80+
EXPECT_FIELD_AFFECTS_EQUALITY(isPressable, false);
81+
EXPECT_FIELD_AFFECTS_EQUALITY(layoutDirection, LayoutDirection::RightToLeft);
82+
EXPECT_FIELD_AFFECTS_EQUALITY(accessibilityRole, AccessibilityRole::Button);
83+
EXPECT_FIELD_AFFECTS_EQUALITY(role, Role::Button);
84+
85+
// Text Effects
86+
EXPECT_FIELD_AFFECTS_EQUALITY(
87+
textEffects, (std::vector<TextEffectInfo>{{.name = "effect"}}));
88+
89+
#undef EXPECT_FIELD_AFFECTS_EQUALITY
90+
}
91+
92+
} // namespace facebook::react

0 commit comments

Comments
 (0)