iOS: Avoid adding extra spaces to accessibility label

Summary:
In some cases, `RCTRecursiveAccessibilityLabel` could return an accessibility label that had leading space, trailing space, or multiple spaces between words. This is because it always added a space before adding a label even if the label turned out to be empty.

This is fixed by being stricter about adding spaces.

Found test cases that used to introduce leading space, trailing space, or multiple spaces between words and verified that there aren't any extra spaces after the fix.

```
{/* Used to have leading space */}
<View accessible={true}>
  <View />
  <View accessibilityLabel='Two' />
  <View accessibilityLabel='Three' />
</View>

{/* Used to have 2 spaces between "One" and "Three" */}
<View accessible={true}>
  <View accessibilityLabel='One' />
  <View />
  <View accessibilityLabel='Three' />
</View>

{/* Used to have trailing space */}
<View accessible={true}>
  <View accessibilityLabel='One' />
  <View accessibilityLabel='Two' />
  <View />
</View>
```

Additionally, my team is using this fix in our app.

Adam Comella
Microsoft Corp.
Closes https://github.com/facebook/react-native/pull/14177

Differential Revision: D5127891

Pulled By: shergin

fbshipit-source-id: 42c3022895d844959e0037eaf381b326af3cd6d1
This commit is contained in:
Adam Comella 2017-05-24 23:01:10 -07:00 committed by Facebook Github Bot
parent c9d32688a6
commit 35338e9008
1 changed files with 7 additions and 10 deletions

View File

@ -81,20 +81,17 @@
static NSString *RCTRecursiveAccessibilityLabel(UIView *view)
{
BOOL isFirstIteration = YES;
NSMutableString *str = [NSMutableString stringWithString:@""];
for (UIView *subview in view.subviews) {
if (isFirstIteration) {
isFirstIteration = NO;
} else {
[str appendString:@" "];
}
NSString *label = subview.accessibilityLabel;
if (label) {
if (!label) {
label = RCTRecursiveAccessibilityLabel(subview);
}
if (label && label.length > 0) {
if (str.length > 0) {
[str appendString:@" "];
}
[str appendString:label];
} else {
[str appendString:RCTRecursiveAccessibilityLabel(subview)];
}
}
return str;