diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt index 9706c8effdd..fe7956e5268 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewAccessibilityDelegate.kt @@ -290,22 +290,26 @@ internal class ReactTextViewAccessibilityDelegate( init { val accessibleLinks = mutableListOf() val spans = text.getSpans(0, text.length, ClickableSpan::class.java) - spans.sortBy { text.getSpanStart(it) } - for (i in spans.indices) { - val span = spans[i] - val start = text.getSpanStart(span) - val end = text.getSpanEnd(span) - // zero length spans, and out of range spans should not be included. - if (start == end || start < 0 || end < 0 || start > text.length || end > text.length) { - continue - } - val link = AccessibleLink() - link.description = text.subSequence(start, end).toString() - link.start = start - link.end = end - link.id = i - accessibleLinks.add(link) + // Do not generate virtual views if whole text is a single link + if (!isWholeTextSingleLink(text, spans)) { + spans.sortBy { text.getSpanStart(it) } + for (i in spans.indices) { + val span = spans[i] + val start = text.getSpanStart(span) + val end = text.getSpanEnd(span) + // zero length spans, and out of range spans should not be included. + if (start == end || start < 0 || end < 0 || start > text.length || end > text.length) { + continue + } + + val link = AccessibleLink() + link.description = text.subSequence(start, end).toString() + link.start = start + link.end = end + link.id = i + accessibleLinks.add(link) + } } links = accessibleLinks } @@ -342,3 +346,14 @@ internal class ReactTextViewAccessibilityDelegate( } } } + +private fun isWholeTextSingleLink(text: Spanned, spans: Array): Boolean { + if (spans.size != 1) { + return false + } + + val span = spans[0] + val start = text.getSpanStart(span) + val end = text.getSpanEnd(span) + return start == 0 && end == text.length +} diff --git a/packages/rn-tester/js/examples/Text/TextSharedExamples.js b/packages/rn-tester/js/examples/Text/TextSharedExamples.js index fb483463394..cdbb36d8955 100644 --- a/packages/rn-tester/js/examples/Text/TextSharedExamples.js +++ b/packages/rn-tester/js/examples/Text/TextSharedExamples.js @@ -14,7 +14,7 @@ import type {TextLayoutLine} from 'react-native/Libraries/Types/CoreEventTypes'; import RNTesterText from '../../components/RNTesterText'; import {useTheme} from '../../components/RNTesterTheme'; import {useState} from 'react'; -import {View} from 'react-native'; +import {StyleSheet, Text, View} from 'react-native'; function InlineView(props: { textAlign: 'auto' | 'left' | 'right' | 'center' | 'justify', @@ -193,6 +193,49 @@ component RtlAndInlineViewsExample() { ); } +component TextWithLinkRoleExample() { + const handlePress = () => console.log('Link Press'); + + return ( + + + Link Text + + + + + Nested Link + + + + + Before{' '} + + Nested Link + + {' After'} + + + + + Nested Link 1 + + {' - '} + + Nested Link 2 + + + + ); +} + +const styles = StyleSheet.create({ + link: { + color: 'teal', + textDecorationLine: 'underline', + }, +}); + export default [ { title: 'Empty Text', @@ -222,4 +265,10 @@ export default [ scrollable: true, render: NumberOfLinesTextLayoutExample, }, + { + title: 'Text with link role', + name: 'textWithLinkRole', + description: 'Shows the a11y behavior of Text with role="link"', + render: TextWithLinkRoleExample, + }, ] as $ReadOnlyArray;