@@ -268,6 +268,11 @@ type ComputeAccessibleNameOptions = {
268268 root ?: boolean ;
269269} ;
270270
271+ type AccessibleNamePart = {
272+ text : string ;
273+ isInlineText : boolean ;
274+ } ;
275+
271276export function computeAccessibleName (
272277 instance : TestInstance ,
273278 options ?: ComputeAccessibleNameOptions ,
@@ -281,21 +286,36 @@ export function computeAccessibleName(
281286 return instance . props . placeholder ;
282287 }
283288
284- const parts = [ ] ;
289+ const parts : AccessibleNamePart [ ] = [ ] ;
285290 for ( const child of instance . children ) {
286291 if ( typeof child === 'string' ) {
287292 if ( child ) {
288- parts . push ( child ) ;
293+ parts . push ( { text : child , isInlineText : true } ) ;
289294 }
290295 } else {
291296 const childLabel = computeAccessibleName ( child , { root : false } ) ;
292297 if ( childLabel ) {
293- parts . push ( childLabel ) ;
298+ parts . push ( { text : childLabel , isInlineText : isHostText ( child ) } ) ;
294299 }
295300 }
296301 }
297302
298- return parts . join ( ' ' ) ;
303+ return joinAccessibleNameParts ( parts , { inline : isHostText ( instance ) } ) ;
304+ }
305+
306+ function joinAccessibleNameParts (
307+ parts : AccessibleNamePart [ ] ,
308+ options : { inline : boolean } ,
309+ ) : string {
310+ return parts . reduce ( ( accessibleName , part , index ) => {
311+ if ( index === 0 ) {
312+ return part . text ;
313+ }
314+
315+ const previousPart = parts [ index - 1 ] ;
316+ const separator = options . inline && previousPart . isInlineText && part . isInlineText ? '' : ' ' ;
317+ return `${ accessibleName } ${ separator } ${ part . text } ` ;
318+ } , '' ) ;
299319}
300320
301321type RoleSupportMap = Partial < Record < Role | AccessibilityRole , true > > ;
0 commit comments