Skip to content

Commit a27bbe6

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. Reviewed By: cortinico Differential Revision: D122813073
1 parent 55c951c commit a27bbe6

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

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

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

502+
/**
503+
* Returns the matching role, or [NONE] when [value] is null. Logs a soft exception and
504+
* returns [NONE] when [value] is unrecognized.
505+
*/
502506
@JvmStatic
503507
public fun fromValue(value: String?): AccessibilityRole {
504508
if (value == null) {
@@ -510,7 +514,11 @@ public open class ReactAccessibilityDelegate( // The View this delegate is attac
510514
return role
511515
}
512516
}
513-
throw IllegalArgumentException("Invalid accessibility role value: $value")
517+
logSoftException(
518+
ReactAccessibilityDelegate.TAG,
519+
ReactNoCrashSoftException("Invalid accessibility role value: $value"),
520+
)
521+
return NONE
514522
}
515523

516524
@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)