Skip to content

Commit aefb769

Browse files
rozelefacebook-github-bot
authored andcommitted
Fix accessibilityLabelledBy crash on empty array
Summary: Fixes a crash in `BaseViewManager.setAccessibilityLabelledBy` when an empty array is passed. The method now guards against accessing index 0 on empty arrays and properly clears the tag when null or empty arrays are provided, instead of leaving stale associations. ## Changelog [Android][Fixed] - Issue when clearing accessibilityLabelledBy Reviewed By: bvanderhoof Differential Revision: D107127388
1 parent 7f396d1 commit aefb769

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,17 @@ public void setNativeId(@NonNull T view, @Nullable String nativeId) {
309309

310310
@ReactProp(name = ViewProps.ACCESSIBILITY_LABELLED_BY)
311311
public void setAccessibilityLabelledBy(@NonNull T view, @Nullable Dynamic nativeId) {
312-
if (nativeId.isNull()) {
312+
if (nativeId == null || nativeId.isNull()) {
313+
view.setTag(R.id.labelled_by, null);
313314
return;
314315
}
315316
if (nativeId.getType() == ReadableType.String) {
316317
view.setTag(R.id.labelled_by, nativeId.asString());
317318
} else if (nativeId.getType() == ReadableType.Array) {
318319
// On Android, this takes a single View as labeledBy. If an array is specified, set the first
319320
// element in the tag.
320-
view.setTag(R.id.labelled_by, nativeId.asArray().getString(0));
321+
ReadableArray array = nativeId.asArray();
322+
view.setTag(R.id.labelled_by, array.size() > 0 ? array.getString(0) : null);
321323
}
322324
}
323325

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,31 @@ class BaseViewManagerTest {
101101
verify(originalFocusListener, times(2)).onFocusChange(view, true)
102102
Assertions.assertThat(originalFocusListener).isEqualTo(view.onFocusChangeListener)
103103
}
104+
105+
@Test
106+
fun testAccessibilityLabelledByString() {
107+
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject("target_id"))
108+
Assertions.assertThat(view.getTag(R.id.labelled_by)).isEqualTo("target_id")
109+
}
110+
111+
@Test
112+
fun testAccessibilityLabelledByArray() {
113+
val array = JavaOnlyArray.of("first_id", "second_id")
114+
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject(array))
115+
Assertions.assertThat(view.getTag(R.id.labelled_by)).isEqualTo("first_id")
116+
}
117+
118+
@Test
119+
fun testAccessibilityLabelledByNullClearsTag() {
120+
view.setTag(R.id.labelled_by, "stale_id")
121+
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject(null))
122+
Assertions.assertThat(view.getTag(R.id.labelled_by)).isNull()
123+
}
124+
125+
@Test
126+
fun testAccessibilityLabelledByEmptyArrayDoesNotCrash() {
127+
view.setTag(R.id.labelled_by, "stale_id")
128+
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject(JavaOnlyArray()))
129+
Assertions.assertThat(view.getTag(R.id.labelled_by)).isNull()
130+
}
104131
}

0 commit comments

Comments
 (0)