Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some unit tests for RCTIsAttributedStringEffectivelySame #47038

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign, readonly) CGFloat zoomScale;
@property (nonatomic, assign, readonly) CGPoint contentOffset;
@property (nonatomic, assign, readonly) UIEdgeInsets contentInset;
@property (nullable, nonatomic, copy) NSDictionary<NSAttributedStringKey, id> *typingAttributes;

// This protocol disallows direct access to `selectedTextRange` property because
// unwise usage of it can break the `delegate` behavior. So, we always have to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ @implementation RCTTextInputComponentView {
*/
BOOL _comingFromJS;
BOOL _didMoveToWindow;

/*
* Newly initialized default typing attributes contain a no-op NSParagraphStyle and NSShadow. These cause inequality
* between the AttributedString backing the input and those generated from state. We store these attributes to make
* later comparison insensitive to them.
*/
NSDictionary<NSAttributedStringKey, id> *_originalTypingAttributes;
}

#pragma mark - UIView overrides
Expand All @@ -76,6 +83,7 @@ - (instancetype)initWithFrame:(CGRect)frame
_ignoreNextTextInputCall = NO;
_comingFromJS = NO;
_didMoveToWindow = NO;
_originalTypingAttributes = [_backedTextInputView.typingAttributes copy];

[self addSubview:_backedTextInputView];
[self initializeReturnKeyType];
Expand All @@ -84,6 +92,20 @@ - (instancetype)initWithFrame:(CGRect)frame
return self;
}

- (void)updateEventEmitter:(const EventEmitter::Shared &)eventEmitter
{
[super updateEventEmitter:eventEmitter];

NSMutableDictionary<NSAttributedStringKey, id> *defaultAttributes =
[_backedTextInputView.defaultTextAttributes mutableCopy];

RCTWeakEventEmitterWrapper *eventEmitterWrapper = [RCTWeakEventEmitterWrapper new];
eventEmitterWrapper.eventEmitter = _eventEmitter;
defaultAttributes[RCTAttributedStringEventEmitterKey] = eventEmitterWrapper;

_backedTextInputView.defaultTextAttributes = defaultAttributes;
}

- (void)didMoveToWindow
{
[super didMoveToWindow];
Expand Down Expand Up @@ -236,8 +258,11 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
}

if (newTextInputProps.textAttributes != oldTextInputProps.textAttributes) {
_backedTextInputView.defaultTextAttributes =
NSMutableDictionary<NSAttributedStringKey, id> *defaultAttributes =
RCTNSTextAttributesFromTextAttributes(newTextInputProps.getEffectiveTextAttributes(RCTFontSizeMultiplier()));
defaultAttributes[RCTAttributedStringEventEmitterKey] =
_backedTextInputView.defaultTextAttributes[RCTAttributedStringEventEmitterKey];
_backedTextInputView.defaultTextAttributes = defaultAttributes;
}

if (newTextInputProps.selectionColor != oldTextInputProps.selectionColor) {
Expand Down Expand Up @@ -418,6 +443,7 @@ - (void)textInputDidChange

- (void)textInputDidChangeSelection
{
[self _updateTypingAttributes];
if (_comingFromJS) {
return;
}
Expand Down Expand Up @@ -674,9 +700,26 @@ - (void)_setAttributedString:(NSAttributedString *)attributedString
[_backedTextInputView scrollRangeToVisible:NSMakeRange(offsetStart, 0)];
}
[self _restoreTextSelection];
[self _updateTypingAttributes];
_lastStringStateWasUpdatedWith = attributedString;
}

// Ensure that newly typed text will inherit any custom attributes. We follow the logic of RN Android, where attributes
// to the left of the cursor are copied into new text, unless we are at the start of the field, in which case we will
// copy the attributes from text to the right. This allows consistency between backed input and new AttributedText
// https://github.com/facebook/react-native/blob/3102a58df38d96f3dacef0530e4dbb399037fcd2/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/internal/span/SetSpanOperation.kt#L30
- (void)_updateTypingAttributes
{
if (_backedTextInputView.attributedText.length > 0) {
NSUInteger offsetStart = [_backedTextInputView offsetFromPosition:_backedTextInputView.beginningOfDocument
toPosition:_backedTextInputView.selectedTextRange.start];

NSUInteger samplePoint = offsetStart == 0 ? 0 : offsetStart - 1;
_backedTextInputView.typingAttributes = [_backedTextInputView.attributedText attributesAtIndex:samplePoint
effectiveRange:NULL];
}
}

- (void)_setMultiline:(BOOL)multiline
{
[_backedTextInputView removeFromSuperview];
Expand Down Expand Up @@ -732,9 +775,10 @@ - (BOOL)_textOf:(NSAttributedString *)newText equals:(NSAttributedString *)oldTe
_backedTextInputView.markedTextRange || _backedTextInputView.isSecureTextEntry || fontHasBeenUpdatedBySystem;

if (shouldFallbackToBareTextComparison) {
return ([newText.string isEqualToString:oldText.string]);
return [newText.string isEqualToString:oldText.string];
} else {
return ([newText isEqualToAttributedString:oldText]);
return RCTIsAttributedStringEffectivelySame(
newText, oldText, _originalTypingAttributes, static_cast<const TextInputProps &>(*_props).textAttributes);
}
}

Expand Down
274 changes: 274 additions & 0 deletions packages/react-native/React/Tests/Text/RCTAttributedTextUtilsTest.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
/*
* 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 <Foundation/Foundation.h>
#import <XCTest/XCTest.h>

#import <react/renderer/textlayoutmanager/RCTAttributedTextUtils.h>

using namespace facebook::react;

@interface RCTAttributedTextUtilsTest : XCTestCase

@end

@implementation RCTAttributedTextUtilsTest

- (void)testSamenessOfEmptyAttributedStrings
{
NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@""];
NSAttributedString *attributedString2 = [[NSAttributedString alloc] initWithString:@""];
NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{};

XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, TextAttributes{}));
XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, TextAttributes{}));
}

- (void)testSamenessOfExactAttributedStrings
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{};

XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, TextAttributes{}));
XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, TextAttributes{}));
}

- (void)testDifferenceOfDifferentString
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"hello World!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{};

XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, TextAttributes{}));
XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, TextAttributes{}));
}

- (void)testDifferenceOfDifferentFragmentRanges
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 6)];

NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{};

XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, TextAttributes{}));
XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, TextAttributes{}));
}

- (void)testDifferenceOfDifferentAttributes
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)];

NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{};

XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, TextAttributes{}));
XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, TextAttributes{}));
}

- (void)testDifferenceOfExtraAttributes
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];
[attributedString1 addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{};

XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, TextAttributes{}));
XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, TextAttributes{}));
}

- (void)testSamenessOfMissingInsensitiveAttributes
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];
[attributedString1 addAttribute:NSShadowAttributeName value:[NSShadow new] range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{
NSShadowAttributeName : [NSShadow new],
};

XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, TextAttributes{}));
XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, TextAttributes{}));
}

- (void)testSamenessOfResolvedParagraphStyleLtr
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];
[attributedString1 addAttribute:NSParagraphStyleAttributeName
value:NSParagraphStyle.defaultParagraphStyle
range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableParagraphStyle *str2ParagraphStyle = [NSMutableParagraphStyle new];
str2ParagraphStyle.alignment = NSTextAlignmentLeft;

// Base writing direction unless overriden by prop is determined by locale, and we assume this test runs in an LTR
// locale.
str2ParagraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:str2ParagraphStyle range:NSMakeRange(0, 5)];

TextAttributes textAttributes;
textAttributes.layoutDirection = LayoutDirection::LeftToRight;
NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{};

XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, textAttributes));
XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, textAttributes));
}

- (void)testSamenessOfResolvedParagraphStyleRtl
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];
[attributedString1 addAttribute:NSParagraphStyleAttributeName
value:NSParagraphStyle.defaultParagraphStyle
range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableParagraphStyle *str2ParagraphStyle = [NSMutableParagraphStyle new];
str2ParagraphStyle.alignment = NSTextAlignmentRight;

// Base writing direction unless overriden by prop is determined by locale, and we assume this test runs in an LTR
// locale.
str2ParagraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:str2ParagraphStyle range:NSMakeRange(0, 5)];

TextAttributes textAttributes;
textAttributes.layoutDirection = LayoutDirection::RightToLeft;
NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{};

XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, textAttributes));
XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, textAttributes));
}

- (void)testSamenessOfResolvedInsensitiveParagraphStyle
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableParagraphStyle *str2ParagraphStyle = [NSMutableParagraphStyle new];
str2ParagraphStyle.alignment = NSTextAlignmentLeft;

// Base writing direction unless overriden by prop is determined by locale, and we assume this test runs in an LTR
// locale.
str2ParagraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:str2ParagraphStyle range:NSMakeRange(0, 5)];

TextAttributes textAttributes;
textAttributes.layoutDirection = LayoutDirection::LeftToRight;
NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{
NSParagraphStyleAttributeName : NSParagraphStyle.defaultParagraphStyle,
};

XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, textAttributes));
XCTAssertTrue(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, textAttributes));
}

- (void)testDifferenceOfResolvedParagraphStyle
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];
[attributedString1 addAttribute:NSParagraphStyleAttributeName
value:NSParagraphStyle.defaultParagraphStyle
range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableParagraphStyle *str2ParagraphStyle = [NSMutableParagraphStyle new];
str2ParagraphStyle.alignment = NSTextAlignmentCenter;

// Base writing direction unless overriden by prop is determined by locale, and we assume this test runs in an LTR
// locale.
str2ParagraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:str2ParagraphStyle range:NSMakeRange(0, 5)];

TextAttributes textAttributes;
textAttributes.layoutDirection = LayoutDirection::LeftToRight;
NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{};

XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, textAttributes));
XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, textAttributes));
}

- (void)testDifferenceOfMissingParagraphStyle
{
NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:@"Hello world!"];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 5)];

NSMutableParagraphStyle *str2ParagraphStyle = [NSMutableParagraphStyle new];
str2ParagraphStyle.alignment = NSTextAlignmentLeft;

// Base writing direction unless overriden by prop is determined by locale, and we assume this test runs in an LTR
// locale.
str2ParagraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:str2ParagraphStyle range:NSMakeRange(0, 5)];

TextAttributes textAttributes;
textAttributes.layoutDirection = LayoutDirection::LeftToRight;
NSDictionary<NSAttributedStringKey, id> *insensitiveAttributes = @{};

XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString1, attributedString2, insensitiveAttributes, textAttributes));
XCTAssertFalse(RCTIsAttributedStringEffectivelySame(
attributedString2, attributedString1, insensitiveAttributes, textAttributes));
}

@end
Loading
Loading