-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathaccessibility.ts
252 lines (205 loc) · 7.92 KB
/
accessibility.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import type { AccessibilityRole, AccessibilityState, AccessibilityValue, Role } from 'react-native';
import { StyleSheet } from 'react-native';
import type { ReactTestInstance } from 'react-test-renderer';
import { getHostSiblings, getUnsafeRootElement, isHostElement } from './component-tree';
import { findAll } from './find-all';
import { isHostImage, isHostSwitch, isHostText, isHostTextInput } from './host-component-names';
import { getTextContent } from './text-content';
import { isEditableTextInput } from './text-input';
type IsInaccessibleOptions = {
cache?: WeakMap<ReactTestInstance, boolean>;
};
export const accessibilityStateKeys: (keyof AccessibilityState)[] = [
'disabled',
'selected',
'checked',
'busy',
'expanded',
];
export const accessibilityValueKeys: (keyof AccessibilityValue)[] = ['min', 'max', 'now', 'text'];
export function isHiddenFromAccessibility(
element: ReactTestInstance | null,
{ cache }: IsInaccessibleOptions = {},
): boolean {
if (element == null) {
return true;
}
let current: ReactTestInstance | null = element;
while (current) {
let isCurrentSubtreeInaccessible = cache?.get(current);
if (isCurrentSubtreeInaccessible === undefined) {
isCurrentSubtreeInaccessible = isSubtreeInaccessible(current);
cache?.set(current, isCurrentSubtreeInaccessible);
}
if (isCurrentSubtreeInaccessible) {
return true;
}
current = current.parent;
}
return false;
}
/** RTL-compatibility alias for `isHiddenFromAccessibility` */
export const isInaccessible = isHiddenFromAccessibility;
function isSubtreeInaccessible(element: ReactTestInstance): boolean {
// Null props can happen for React.Fragments
if (element.props == null) {
return false;
}
// See: https://reactnative.dev/docs/accessibility#aria-hidden
if (element.props['aria-hidden']) {
return true;
}
// iOS: accessibilityElementsHidden
// See: https://reactnative.dev/docs/accessibility#accessibilityelementshidden-ios
if (element.props.accessibilityElementsHidden) {
return true;
}
// Android: importantForAccessibility
// See: https://reactnative.dev/docs/accessibility#importantforaccessibility-android
if (element.props.importantForAccessibility === 'no-hide-descendants') {
return true;
}
// Note that `opacity: 0` is not treated as inaccessible on iOS
const flatStyle = StyleSheet.flatten(element.props.style) ?? {};
if (flatStyle.display === 'none') return true;
// iOS: accessibilityViewIsModal or aria-modal
// See: https://reactnative.dev/docs/accessibility#accessibilityviewismodal-ios
const hostSiblings = getHostSiblings(element);
if (hostSiblings.some((sibling) => computeAriaModal(sibling))) {
return true;
}
return false;
}
export function isAccessibilityElement(element: ReactTestInstance | null): boolean {
if (element == null) {
return false;
}
// https://github.com/facebook/react-native/blob/8dabed60f456e76a9e53273b601446f34de41fb5/packages/react-native/Libraries/Image/Image.ios.js#L172
if (isHostImage(element) && element.props.alt !== undefined) {
return true;
}
if (element.props.accessible !== undefined) {
return element.props.accessible;
}
return isHostText(element) || isHostTextInput(element) || isHostSwitch(element);
}
/**
* Returns the accessibility role for given element. It will return explicit
* role from either `role` or `accessibilityRole` props if set.
*
* If explicit role is not available, it would try to return default element
* role:
* - `text` for `Text` elements
*
* In all other cases this functions returns `none`.
*
* @param element
* @returns
*/
export function getRole(element: ReactTestInstance): Role | AccessibilityRole {
const explicitRole = element.props.role ?? element.props.accessibilityRole;
if (explicitRole) {
return normalizeRole(explicitRole);
}
if (isHostText(element)) {
return 'text';
}
// Note: host Image elements report "image" role in screen reader only on Android, but not on iOS.
// It's better to require explicit role for Image elements.
return 'none';
}
/**
* There are some duplications between (ARIA) `Role` and `AccessibilityRole` types.
* Resolve them by using ARIA `Role` type where possible.
*
* @param role Role to normalize
* @returns Normalized role
*/
export function normalizeRole(role: string): Role | AccessibilityRole {
if (role === 'image') {
return 'img';
}
return role as Role | AccessibilityRole;
}
export function computeAriaModal(element: ReactTestInstance): boolean | undefined {
return element.props['aria-modal'] ?? element.props.accessibilityViewIsModal;
}
export function computeAriaLabel(element: ReactTestInstance): string | undefined {
const labelElementId = element.props['aria-labelledby'] ?? element.props.accessibilityLabelledBy;
if (labelElementId) {
const rootElement = getUnsafeRootElement(element);
const labelElement = findAll(
rootElement,
(node) => isHostElement(node) && node.props.nativeID === labelElementId,
{ includeHiddenElements: true },
);
if (labelElement.length > 0) {
return getTextContent(labelElement[0]);
}
}
const explicitLabel = element.props['aria-label'] ?? element.props.accessibilityLabel;
if (explicitLabel) {
return explicitLabel;
}
//https://github.com/facebook/react-native/blob/8dabed60f456e76a9e53273b601446f34de41fb5/packages/react-native/Libraries/Image/Image.ios.js#L173
if (isHostImage(element) && element.props.alt) {
return element.props.alt;
}
return undefined;
}
// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#busy-state
export function computeAriaBusy({ props }: ReactTestInstance): boolean {
return props['aria-busy'] ?? props.accessibilityState?.busy ?? false;
}
// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#checked-state
export function computeAriaChecked(element: ReactTestInstance): AccessibilityState['checked'] {
const { props } = element;
if (isHostSwitch(element)) {
return props.value;
}
const role = getRole(element);
if (!rolesSupportingCheckedState[role]) {
return undefined;
}
return props['aria-checked'] ?? props.accessibilityState?.checked;
}
// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#disabled-state
export function computeAriaDisabled(element: ReactTestInstance): boolean {
if (isHostTextInput(element) && !isEditableTextInput(element)) {
return true;
}
const { props } = element;
return props['aria-disabled'] ?? props.accessibilityState?.disabled ?? false;
}
// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#expanded-state
export function computeAriaExpanded({ props }: ReactTestInstance): boolean | undefined {
return props['aria-expanded'] ?? props.accessibilityState?.expanded;
}
// See: https://github.com/callstack/react-native-testing-library/wiki/Accessibility:-State#selected-state
export function computeAriaSelected({ props }: ReactTestInstance): boolean {
return props['aria-selected'] ?? props.accessibilityState?.selected ?? false;
}
export function computeAriaValue(element: ReactTestInstance): AccessibilityValue {
const {
accessibilityValue,
'aria-valuemax': ariaValueMax,
'aria-valuemin': ariaValueMin,
'aria-valuenow': ariaValueNow,
'aria-valuetext': ariaValueText,
} = element.props;
return {
max: ariaValueMax ?? accessibilityValue?.max,
min: ariaValueMin ?? accessibilityValue?.min,
now: ariaValueNow ?? accessibilityValue?.now,
text: ariaValueText ?? accessibilityValue?.text,
};
}
export function computeAccessibleName(element: ReactTestInstance): string | undefined {
return computeAriaLabel(element) ?? getTextContent(element);
}
type RoleSupportMap = Partial<Record<Role | AccessibilityRole, true>>;
export const rolesSupportingCheckedState: RoleSupportMap = {
checkbox: true,
radio: true,
switch: true,
};