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

Fix: a11y crash when an accessible link is ellipsized away #37050

Closed
wants to merge 1 commit into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,18 @@ protected void onPopulateNodeForVirtualView(
return;
}

// NOTE: The span may not actually have visible bounds within its parent,
// due to line limits, etc.
final Rect bounds = getBoundsInParent(accessibleTextSpan);
if (bounds == null) {
node.setContentDescription("");
node.setBoundsInParent(new Rect(0, 0, 1, 1));
return;
}
Comment on lines +642 to +646
Copy link
Contributor

@Pranav-yadav Pranav-yadav Apr 24, 2023

Choose a reason for hiding this comment

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

Just curious, why we're not adding the "action" AccessibilityNodeInfoCompat.ACTION_CLICK as well; here?

Suggested change
if (bounds == null) {
node.setContentDescription("");
node.setBoundsInParent(new Rect(0, 0, 1, 1));
return;
}
if (bounds == null) {
node.setContentDescription("");
node.addAction(AccessibilityNodeInfoCompat.ACTION_CLICK);
node.setBoundsInParent(new Rect(0, 0, 1, 1));
return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The link has been ellipsized away so it's not visible (IE would not actually be tappable). The two checks above don't include it when there isn't a valid link, etc; that seemed to apply in this case, so I mirrored that approach.


node.setContentDescription(accessibleTextSpan.description);
node.addAction(AccessibilityNodeInfoCompat.ACTION_CLICK);
node.setBoundsInParent(getBoundsInParent(accessibleTextSpan));
node.setBoundsInParent(bounds);
node.setRoleDescription(mView.getResources().getString(R.string.link_description));
node.setClassName(AccessibilityRole.getValue(AccessibilityRole.BUTTON));
}
Expand All @@ -655,10 +664,19 @@ private Rect getBoundsInParent(AccessibilityLinks.AccessibleLink accessibleLink)
return new Rect(0, 0, textView.getWidth(), textView.getHeight());
}

Rect rootRect = new Rect();

double startOffset = accessibleLink.start;
double endOffset = accessibleLink.end;

// Ensure the link hasn't been ellipsized away; in such cases,
// getPrimaryHorizontal will crash (and the link isn't rendered anyway).
int startOffsetLineNumber = textViewLayout.getLineForOffset((int) startOffset);
int lineEndOffset = textViewLayout.getLineEnd(startOffsetLineNumber);
if (startOffset > lineEndOffset) {
return null;
}

Rect rootRect = new Rect();

double startXCoordinates = textViewLayout.getPrimaryHorizontal((int) startOffset);

final Paint paint = new Paint();
Expand All @@ -668,7 +686,6 @@ private Rect getBoundsInParent(AccessibilityLinks.AccessibleLink accessibleLink)
paint.setTextSize(textSize);
int textWidth = (int) Math.ceil(paint.measureText(accessibleLink.description));

int startOffsetLineNumber = textViewLayout.getLineForOffset((int) startOffset);
int endOffsetLineNumber = textViewLayout.getLineForOffset((int) endOffset);
boolean isMultiline = startOffsetLineNumber != endOffsetLineNumber;
textViewLayout.getLineBounds(startOffsetLineNumber, rootRect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ class AccessibilityAndroidExample extends React.Component<
render(): React.Node {
return (
<RNTesterPage title={'Accessibility Android APIs'}>
<RNTesterBlock title="Ellipsized Accessible Links">
<Text numberOfLines={3}>
<Text>
Bacon {this.state.count} Ipsum{'\n'}
</Text>
<Text>Dolor sit amet{'\n'}</Text>
<Text>Eggsecetur{'\n'}</Text>
<Text>{'\n'}</Text>
<Text accessibilityRole="link" onPress={this._addOne}>
http://github.com
</Text>
</Text>
</RNTesterBlock>

<RNTesterBlock title="LiveRegion">
<TouchableWithoutFeedback onPress={this._addOne}>
<View style={styles.embedded}>
Expand Down