Skip to content

Commit d55fd47

Browse files
smeisnguyenhuy
authored andcommitted
[ASTextNode] Maintain isAccessibilityElement setting on text nodes when updating text (facebookarchive#1326)
1 parent b9c8b32 commit d55fd47

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

Source/ASTextNode.mm

+10-1
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,16 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
464464
attributedText = [[NSAttributedString alloc] initWithString:@"" attributes:nil];
465465
}
466466

467+
NSAttributedString *oldAttributedText = nil;
468+
467469
{
468470
ASLockScopeSelf();
469471
if (ASObjectIsEqual(attributedText, _attributedText)) {
470472
return;
471473
}
472474

475+
oldAttributedText = _attributedText;
476+
473477
NSAttributedString *cleanedAttributedString = ASCleanseAttributedStringOfCoreTextAttributes(attributedText);
474478

475479
// Invalidating the truncation text must be done while we still hold the lock. Because after we release it,
@@ -498,7 +502,12 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
498502
// Accessiblity
499503
const auto currentAttributedText = self.attributedText; // Grab attributed string again in case it changed in the meantime
500504
self.accessibilityLabel = self.defaultAccessibilityLabel;
501-
self.isAccessibilityElement = (currentAttributedText.length != 0); // We're an accessibility element by default if there is a string.
505+
506+
// We update the isAccessibilityElement setting if this node is not switching between strings.
507+
if (oldAttributedText.length == 0 || currentAttributedText.length == 0) {
508+
// We're an accessibility element by default if there is a string.
509+
self.isAccessibilityElement = (currentAttributedText.length != 0);
510+
}
502511

503512
#if AS_TEXTNODE_RECORD_ATTRIBUTED_STRINGS
504513
[ASTextNode _registerAttributedText:_attributedText];

Source/ASTextNode2.mm

+7-1
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
396396
// Holding it for the duration of the method is more efficient in this case.
397397
ASLockScopeSelf();
398398

399+
NSAttributedString *oldAttributedText = _attributedText;
399400
if (!ASCompareAssignCopy(_attributedText, attributedText)) {
400401
return;
401402
}
@@ -418,7 +419,12 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
418419

419420
// Accessiblity
420421
self.accessibilityLabel = self.defaultAccessibilityLabel;
421-
self.isAccessibilityElement = (length != 0); // We're an accessibility element by default if there is a string.
422+
423+
// We update the isAccessibilityElement setting if this node is not switching between strings.
424+
if (oldAttributedText.length == 0 || length == 0) {
425+
// We're an accessibility element by default if there is a string.
426+
self.isAccessibilityElement = (length != 0);
427+
}
422428

423429
#if AS_TEXTNODE2_RECORD_ATTRIBUTED_STRINGS
424430
[ASTextNode _registerAttributedText:_attributedText];

Tests/ASTextNode2Tests.mm

+16
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,20 @@ - (void)testAccessibility
9191
_textNode.defaultAccessibilityLabel, _attributedText.string);
9292
}
9393

94+
- (void)testRespectingAccessibilitySetting
95+
{
96+
ASTextNode2 *textNode = [[ASTextNode2 alloc] init];
97+
textNode.attributedText = _attributedText;
98+
textNode.isAccessibilityElement = NO;
99+
100+
textNode.attributedText = [[NSAttributedString alloc] initWithString:@"new string"];
101+
XCTAssertFalse(textNode.isAccessibilityElement);
102+
103+
// Ensure removing string on an accessible text node updates the setting.
104+
ASTextNode2 *accessibleTextNode = [ASTextNode2 new];
105+
accessibleTextNode.attributedText = _attributedText;
106+
accessibleTextNode.attributedText = nil;
107+
XCTAssertFalse(accessibleTextNode.isAccessibilityElement);
108+
}
109+
94110
@end

Tests/ASTextNodeTests.mm

+17
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ - (void)testAccessibility
175175
XCTAssertTrue([_textNode.accessibilityLabel isEqualToString:_attributedText.string], @"Accessibility label is incorrectly set to \n%@\n when it should be \n%@\n", _textNode.accessibilityLabel, _attributedText.string);
176176
}
177177

178+
- (void)testRespectingAccessibilitySetting
179+
{
180+
ASTextNode *textNode = [ASTextNode new];
181+
182+
textNode.attributedText = _attributedText;
183+
textNode.isAccessibilityElement = NO;
184+
185+
textNode.attributedText = [[NSAttributedString alloc] initWithString:@"new string"];
186+
XCTAssertFalse(textNode.isAccessibilityElement);
187+
188+
// Ensure removing string on an accessible text node updates the setting.
189+
ASTextNode *accessibleTextNode = [ASTextNode new];
190+
accessibleTextNode.attributedText = _attributedText;
191+
accessibleTextNode.attributedText = nil;
192+
XCTAssertFalse(accessibleTextNode.isAccessibilityElement);
193+
}
194+
178195
- (void)testLinkAttribute
179196
{
180197
NSString *linkAttributeName = @"MockLinkAttributeName";

0 commit comments

Comments
 (0)