Skip to content

Commit

Permalink
[ASTextNode] Maintain isAccessibilityElement setting on text nodes wh…
Browse files Browse the repository at this point in the history
…en updating text (TextureGroup#1326)
  • Loading branch information
smeis authored and hebertialmeida committed May 10, 2019
1 parent 4ec9ac6 commit 8dea604
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Source/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,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 @@ -498,7 +502,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 @@ -396,6 +396,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 @@ -418,7 +419,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

0 comments on commit 8dea604

Please sign in to comment.