Skip to content

Commit 30032c8

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
Make invalid accessibility roles non-fatal (#58803)
Summary: `AccessibilityRole.fromValue` throws for an unrecognized role and can crash Android rendering. Return `NONE` for unknown values instead. It doesn't make sense to throw here, especially since different platforms like iOS support different values Changelog: [Android][Fixed] - Prevent invalid `accessibilityRole` values from crashing the app. Differential Revision: D122813073
1 parent 51e9a97 commit 30032c8

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ public open class ReactAccessibilityDelegate( // The View this delegate is attac
499499
}
500500
}
501501

502+
/** Returns the matching role, or [NONE] when [value] is null or unrecognized. */
502503
@JvmStatic
503504
public fun fromValue(value: String?): AccessibilityRole {
504505
if (value == null) {
@@ -510,7 +511,7 @@ public open class ReactAccessibilityDelegate( // The View this delegate is attac
510511
return role
511512
}
512513
}
513-
throw IllegalArgumentException("Invalid accessibility role value: $value")
514+
return NONE
514515
}
515516

516517
@JvmStatic

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactAccessibilityDelegateTest.kt‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,13 @@ class ReactAccessibilityDelegateTest {
356356
.isEqualTo("android.view.View")
357357
}
358358

359+
@Test
360+
fun testAccessibilityRole_fromValue_invalidRole_returnsNone() {
361+
val role = ReactAccessibilityDelegate.AccessibilityRole.fromValue("invalid-role")
362+
363+
assertThat(role).isEqualTo(ReactAccessibilityDelegate.AccessibilityRole.NONE)
364+
}
365+
359366
@Test
360367
fun testPerformAccessibilityAction_collapseAction_fromAccessibilityActions() {
361368
val accessibilityActions = JavaOnlyArray()

0 commit comments

Comments
 (0)