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

[Text Nodes] Maintain isAccessibilityElement setting on text nodes when updating text #1326

Merged
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
11 changes: 10 additions & 1 deletion Source/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,16 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
attributedText = [[NSAttributedString alloc] initWithString:@"" attributes:nil];
}

NSAttributedString *oldAttributedText = nil;

{
ASLockScopeSelf();
if (ASObjectIsEqual(attributedText, _attributedText)) {
return;
}

oldAttributedText = _attributedText;

NSAttributedString *cleanedAttributedString = ASCleanseAttributedStringOfCoreTextAttributes(attributedText);

// Invalidating the truncation text must be done while we still hold the lock. Because after we release it,
Expand Down Expand Up @@ -496,7 +500,12 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
// Accessiblity
const auto currentAttributedText = self.attributedText; // Grab attributed string again in case it changed in the meantime
self.accessibilityLabel = self.defaultAccessibilityLabel;
self.isAccessibilityElement = (currentAttributedText.length != 0); // We're an accessibility element by default if there is a string.

// We update the isAccessibilityElement setting if this node is not switching between strings.
if (oldAttributedText.length == 0 || currentAttributedText.length == 0) {
// We're an accessibility element by default if there is a string.
self.isAccessibilityElement = (currentAttributedText.length != 0);
}

#if AS_TEXTNODE_RECORD_ATTRIBUTED_STRINGS
[ASTextNode _registerAttributedText:_attributedText];
Expand Down
8 changes: 7 additions & 1 deletion Source/ASTextNode2.mm
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
// Holding it for the duration of the method is more efficient in this case.
ASLockScopeSelf();

NSAttributedString *oldAttributedText = _attributedText;
if (!ASCompareAssignCopy(_attributedText, attributedText)) {
return;
}
Expand All @@ -424,7 +425,12 @@ - (void)setAttributedText:(NSAttributedString *)attributedText

// Accessiblity
self.accessibilityLabel = self.defaultAccessibilityLabel;
self.isAccessibilityElement = (length != 0); // We're an accessibility element by default if there is a string.

// We update the isAccessibilityElement setting if this node is not switching between strings.
if (oldAttributedText.length == 0 || length == 0) {
// We're an accessibility element by default if there is a string.
self.isAccessibilityElement = (length != 0);
}

#if AS_TEXTNODE2_RECORD_ATTRIBUTED_STRINGS
[ASTextNode _registerAttributedText:_attributedText];
Expand Down
16 changes: 16 additions & 0 deletions Tests/ASTextNode2Tests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,20 @@ - (void)testAccessibility
_textNode.defaultAccessibilityLabel, _attributedText.string);
}

- (void)testRespectingAccessibilitySetting
{
ASTextNode2 *textNode = [[ASTextNode2 alloc] init];
textNode.attributedText = _attributedText;
textNode.isAccessibilityElement = NO;

textNode.attributedText = [[NSAttributedString alloc] initWithString:@"new string"];
XCTAssertFalse(textNode.isAccessibilityElement);

// Ensure removing string on an accessible text node updates the setting.
ASTextNode2 *accessibleTextNode = [ASTextNode2 new];
accessibleTextNode.attributedText = _attributedText;
accessibleTextNode.attributedText = nil;
XCTAssertFalse(accessibleTextNode.isAccessibilityElement);
}

@end
17 changes: 17 additions & 0 deletions Tests/ASTextNodeTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ - (void)testAccessibility
XCTAssertTrue([_textNode.accessibilityLabel isEqualToString:_attributedText.string], @"Accessibility label is incorrectly set to \n%@\n when it should be \n%@\n", _textNode.accessibilityLabel, _attributedText.string);
}

- (void)testRespectingAccessibilitySetting
{
ASTextNode *textNode = [ASTextNode new];

textNode.attributedText = _attributedText;
textNode.isAccessibilityElement = NO;

textNode.attributedText = [[NSAttributedString alloc] initWithString:@"new string"];
XCTAssertFalse(textNode.isAccessibilityElement);

// Ensure removing string on an accessible text node updates the setting.
ASTextNode *accessibleTextNode = [ASTextNode new];
accessibleTextNode.attributedText = _attributedText;
accessibleTextNode.attributedText = nil;
XCTAssertFalse(accessibleTextNode.isAccessibilityElement);
}

- (void)testLinkAttribute
{
NSString *linkAttributeName = @"MockLinkAttributeName";
Expand Down