Skip to content

Commit 1f6da27

Browse files
rozelefacebook-github-bot
authored andcommitted
Fix accessibilityLabelledBy crash on empty array (#57029)
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 1f6da27

2 files changed

Lines changed: 32 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package com.facebook.react.uimanager
1212
import android.view.View.OnFocusChangeListener
1313
import com.facebook.react.R
1414
import com.facebook.react.bridge.BridgeReactContext
15+
import com.facebook.react.bridge.JavaOnlyArray
1516
import com.facebook.react.bridge.JavaOnlyMap
1617
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
1718
import com.facebook.react.views.view.ReactViewGroup
@@ -101,4 +102,31 @@ class BaseViewManagerTest {
101102
verify(originalFocusListener, times(2)).onFocusChange(view, true)
102103
Assertions.assertThat(originalFocusListener).isEqualTo(view.onFocusChangeListener)
103104
}
105+
106+
@Test
107+
fun testAccessibilityLabelledByString() {
108+
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject("target_id"))
109+
Assertions.assertThat(view.getTag(R.id.labelled_by)).isEqualTo("target_id")
110+
}
111+
112+
@Test
113+
fun testAccessibilityLabelledByArray() {
114+
val array = JavaOnlyArray.of("first_id", "second_id")
115+
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject(array))
116+
Assertions.assertThat(view.getTag(R.id.labelled_by)).isEqualTo("first_id")
117+
}
118+
119+
@Test
120+
fun testAccessibilityLabelledByNullClearsTag() {
121+
view.setTag(R.id.labelled_by, "stale_id")
122+
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject(null))
123+
Assertions.assertThat(view.getTag(R.id.labelled_by)).isNull()
124+
}
125+
126+
@Test
127+
fun testAccessibilityLabelledByEmptyArrayDoesNotCrash() {
128+
view.setTag(R.id.labelled_by, "stale_id")
129+
viewManager.setAccessibilityLabelledBy(view, DynamicFromObject(JavaOnlyArray()))
130+
Assertions.assertThat(view.getTag(R.id.labelled_by)).isNull()
131+
}
104132
}

0 commit comments

Comments
 (0)