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

Support setting accessibilityElements #1488

Closed
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
10 changes: 10 additions & 0 deletions Source/ASDisplayNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,12 @@ AS_EXTERN NSInteger const ASDefaultDrawingPriority;

@end

/**
* Accessibility elements modification block. Used to modify/override the elements returned by
* `accessibilityElements` for a node.
*/
typedef NSArray *_Nonnull(^ASDisplayNodeAccessibilityElementsBlock)(NSArray * _Nonnull);

@interface ASDisplayNode (UIViewBridgeAccessibility)

// Accessibility support
Expand All @@ -802,13 +808,17 @@ AS_EXTERN NSInteger const ASDefaultDrawingPriority;
@property BOOL shouldGroupAccessibilityChildren;
@property UIAccessibilityNavigationStyle accessibilityNavigationStyle;
@property (nullable, copy) NSArray *accessibilityCustomActions API_AVAILABLE(ios(8.0),tvos(9.0));

#if TARGET_OS_TV
@property (nullable, copy) NSArray *accessibilityHeaderElements;
#endif

// Accessibility identification support
@property (nullable, copy) NSString *accessibilityIdentifier;

/// Block used for modifying/overriding the a11y elements returned by `accessibilityElements`.
@property (nullable, copy) ASDisplayNodeAccessibilityElementsBlock accessibilityElementsBlock;

@end

@interface ASDisplayNode (ASLayoutElement) <ASLayoutElement>
Expand Down
12 changes: 12 additions & 0 deletions Source/ASDisplayNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3512,6 +3512,18 @@ - (BOOL)isAccessibilityContainer
return _flags.isAccessibilityContainer;
}

- (void)setAccessibilityElementsBlock:(ASDisplayNodeAccessibilityElementsBlock)accessibilityElementsBlock
{
MutexLocker l(__instanceLock__);
_accessibilityElementsBlock = [accessibilityElementsBlock copy];
}

- (ASDisplayNodeAccessibilityElementsBlock)accessibilityElementsBlock
{
MutexLocker l(__instanceLock__);
return _accessibilityElementsBlock;
}

- (NSString *)defaultAccessibilityLabel
{
return nil;
Expand Down
6 changes: 6 additions & 0 deletions Source/Details/_ASDisplayViewAccessiblity.mm
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ - (NSArray *)accessibilityElements
NSMutableArray *accessibilityElements = [[NSMutableArray alloc] init];
CollectAccessibilityElementsForView(self.view, accessibilityElements);
SortAccessibilityElements(accessibilityElements);

if (_accessibilityElementsBlock) {
NSArray *accessibilityElementsCopy = [accessibilityElements copy];
accessibilityElements = _accessibilityElementsBlock(accessibilityElementsCopy);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it's ok to give the block a mutable array, so that the block may modify it directly and we don't need to copy it beforehand?

I'm not sure if it's a good API design?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work too, I just felt this might be safer, sort of forcing people to create their own mutable array and add the object as needed and return a copy of it. Do we have an example of supporting custom blocks like this? it would be nice to follow an existing pattern too if there is one.

}

return accessibilityElements;
}

Expand Down
1 change: 1 addition & 0 deletions Source/Private/ASDisplayNodeInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ static constexpr CACornerMask kASCACornerAllCorners =
NSArray *_accessibilityHeaderElements;
CGPoint _accessibilityActivationPoint;
UIBezierPath *_accessibilityPath;
ASDisplayNodeAccessibilityElementsBlock _accessibilityElementsBlock;


// Safe Area support
Expand Down
34 changes: 34 additions & 0 deletions Tests/ASDisplayViewAccessibilityTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,40 @@ - (void)testAccessibilityElementsAccessors
XCTAssertEqual([node.view indexOfAccessibilityElement:node.view.accessibilityElements.firstObject], 0);*/
}

- (void)testAccessibilityElementsBlock
{
// Setup nodes
ASDisplayNode *node = nil;
ASDisplayNode *innerNode1 = nil;
ASDisplayNode *innerNode2 = nil;
node = [[ASDisplayNode alloc] init];
innerNode1 = [[ASDisplayNode alloc] init];
innerNode2 = [[ASDisplayNode alloc] init];

innerNode1.accessibilityLabel = @"hello";
innerNode1.isAccessibilityElement = YES;
innerNode2.accessibilityLabel = @"world";
innerNode2.isAccessibilityElement = YES;

// Attach the subnodes to the parent node, then check that all the subnodes added are returned as accessibilityElements
[node addSubnode:innerNode1];
[node addSubnode:innerNode2];

XCTAssertTrue(node.view.accessibilityElements.count == 2);
XCTAssertEqualObjects([node.view.accessibilityElements[0] accessibilityLabel], [innerNode1 accessibilityLabel]);
XCTAssertEqualObjects([node.view.accessibilityElements[1] accessibilityLabel], [innerNode2 accessibilityLabel]);

// Override accessibilityElements, check that only the specified element is returned as accessibilityElements
node.accessibilityElementsBlock = ^NSArray * _Nonnull(NSArray *_Nonnull elements) {
return @[innerNode2];
};

XCTAssertTrue(node.view.accessibilityElements.count == 1);
XCTAssertEqualObjects([node.view.accessibilityElements.firstObject accessibilityLabel],
[innerNode2 accessibilityLabel],
@"Parent node accessibilityElements override broken");
}

- (void)testThatSubnodeAccessibilityLabelAggregationWorks
{
// Setup nodes
Expand Down