Skip to content

Compare lineBreakMode in TextAttributes::operator== - #60455

Open
ide wants to merge 1 commit into
react:mainfrom
expo:@ide/text-attributes-line-break-mode
Open

ide wants to merge 1 commit into
react:mainfrom
expo:@ide/text-attributes-line-break-mode

Conversation

@ide

@ide ide commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

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.

So on main:

  • A change of lineBreakModeIOS alone reaches neither the default text attributes nor the text.
  • A recycled view keeps the props of the input it showed before (prepareForRecycle does not reset them), so an input that differs from that one only in lineBreakModeIOS keeps the default text attributes of the input before it.

Two inputs whose lineBreakModeIOS changes from "head" to "middle", one with a value and one with a placeholder. Left to right: before the change, after it on main, and after it with this change.

A TextInput whose lineBreakModeIOS changes from head to middle: before the change, after it on main, and after it with this change

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.

Not changed: the measure cache's own comparison of text attributes (areTextAttributesEquivalentLayoutWise in TextMeasureCache.h) does not look at lineBreakMode or lineBreakStrategy. Whether a cached size can be wrong for them is not measured here.

Changelog:

[IOS] [FIXED] - Apply a change of lineBreakModeIOS to a TextInput when no other text style changes with it

Test Plan:

C++

TextAttributesEqualityTest.everyFieldAffectsEquality, in TextAttributesTest.cpp, sets each field of TextAttributes in turn and expects the result to differ from default attributes. On main it fails for lineBreakMode.

The tests in ReactCommon build against the Fantom tester's libraries and run with CTest, as in the test_react_common_cxx job:

./gradlew :private:react-native-fantom:testReactCommon

100% tests passed, 0 tests failed out of 1615 on macOS (Apple clang 21).

To run only this directory's tests after that:

cmake --build private/react-native-fantom/build/tester --target react_renderer_attributedstring_tests
private/react-native-fantom/build/tester/react-common-tests/react_renderer_attributedstring_tests

All 21 pass. Each of these changes to operator== fails everyFieldAffectsEquality, naming the field, and no other test:

Change Field named
lineBreakMode is not compared (main's behavior) lineBreakMode
textTransform is not compared textTransform
letterSpacing is not compared letterSpacing

iOS

testChangeOfLineBreakModeAloneSetsDefaultTextAttributes in the new RCTTextInputComponentViewTests gives an RCTTextInputComponentView props with allowFontScaling, as <TextInput> sends, and a line break mode, and reads the paragraph style of the field's default text attributes:

  1. "head" on a new view gives NSLineBreakByTruncatingHead.
  2. A change to "middle" gives NSLineBreakByTruncatingMiddle.
  3. After prepareForRecycle, props with no line break mode give no paragraph style.

With lineBreakMode taken out of operator== again, step 2 still reads NSLineBreakByTruncatingHead and step 3 still has the paragraph style of step 1.

No CI job runs the tests in React/Tests. I ran this one with Xcode 27.1 on an iOS 27.0 simulator (iPhone 18 Pro) after these local changes, which are not part of this PR: :testspecs => ['Tests'] on the React-RCTFabric pod in scripts/cocoapods/fabric.rb; and in React-RCTFabric.podspec, a swift_version and a test spec that lists this file and an empty Swift file, which makes Xcode link the Swift runtime that RCTSwiftUI needs into the test bundle.

xcodebuild test -workspace packages/rn-tester/RNTesterPods.xcworkspace -scheme React-RCTFabric-Unit-Tests \
  -destination 'platform=iOS Simulator,id=<iPhone 18 Pro>' \
  -only-testing:React-RCTFabric-Unit-Tests/RCTTextInputComponentViewTests

Simulator

RNTester (Debug) on the same simulator, built from this branch and again with TextAttributes.cpp as on main. The page is RNTesterPlayground.js edited locally: two single-line inputs 280 pt wide with a 16 pt font, one with a value wider than the field and one with a placeholder that wide, and a timer that changes lineBreakModeIOS on both from "head" to "middle" and nothing else.

A TextInput whose lineBreakModeIOS changes from head to middle: before the change, after it on main, and after it with this change

Left to right: before the change; after it on main, where the value and the placeholder still end at the head; after it with this change, where both have the ellipsis in the middle. The value shows the attributed text from the state and the placeholder shows the default text attributes.

Not checked: the attributed text path through updateStateIfNeeded has no unit test of its own; the simulator check above covers it. No device was used.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 2, 2026
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 2, 2026
@ide ide closed this Dec 2, 2026
@ide ide reopened this Dec 2, 2026
`TextAttributes::operator==` compares every field except `lineBreakMode`, so text attributes that differ only in `lineBreakModeIOS` compare equal. react#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.
@ide
ide force-pushed the @ide/text-attributes-line-break-mode branch from 6ba1733 to ca0c41c Compare December 2, 2026 03:51
@ide
ide marked this pull request as ready for review December 2, 2026 05:17
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

1 participant