Skip to content

Commit 91f0937

Browse files
committed
fix(tre): do not set accessibilityLabel for images with presentation role
1 parent 2db3ef8 commit 91f0937

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

packages/transient-render-engine/src/flow/__tests__/translate.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ReactNativePropsSwitch } from '../../helper-types';
12
import HTMLModelRegistry from '../../model/HTMLModelRegistry';
23
import { TStyles } from '../../styles/TStyles';
34
import { TEmptyCtor } from '../../tree/TEmptyCtor';
@@ -107,6 +108,68 @@ describe('translateNode function', () => {
107108
expect(child.nodeIndex).toBe(i);
108109
});
109110
});
111+
describe('regarding images accessiblity', () => {
112+
it('should not provide accessibility to images with role="presentation"', () => {
113+
const ttree = translateTreeTest(
114+
'<img role="presentation" alt="foo" src="https://" />'
115+
);
116+
expect(ttree.getReactNativeProps()).toEqual<ReactNativePropsSwitch>({
117+
text: {
118+
accessibilityRole: 'none'
119+
},
120+
view: {
121+
accessibilityRole: 'none'
122+
}
123+
});
124+
});
125+
126+
it('should provide accessibility to images with role="image" and non-empty alt', () => {
127+
const ttree = translateTreeTest(
128+
'<img role="image" alt="foo" src="https://" />'
129+
);
130+
expect(ttree.getReactNativeProps()).toEqual<ReactNativePropsSwitch>({
131+
text: {
132+
accessibilityLabel: 'foo',
133+
accessibilityRole: 'image'
134+
},
135+
view: {
136+
accessibilityLabel: 'foo',
137+
accessibilityRole: 'image'
138+
}
139+
});
140+
});
141+
it('should provide accessibility to images with role="image" and non-empty aria-label', () => {
142+
const ttree = translateTreeTest(
143+
'<img role="image" aria-label="foo" src="https://" />'
144+
);
145+
expect(ttree.getReactNativeProps()).toEqual<ReactNativePropsSwitch>({
146+
text: {
147+
accessibilityLabel: 'foo',
148+
accessibilityRole: 'image'
149+
},
150+
view: {
151+
accessibilityLabel: 'foo',
152+
accessibilityRole: 'image'
153+
}
154+
});
155+
});
156+
157+
it('should prefer aria-label over alt to generate an accessibilityLabel prop', () => {
158+
const ttree = translateTreeTest(
159+
'<img role="image" aria-label="foo" alt="bar" src="https://" />'
160+
);
161+
expect(ttree.getReactNativeProps()).toEqual<ReactNativePropsSwitch>({
162+
text: {
163+
accessibilityLabel: 'foo',
164+
accessibilityRole: 'image'
165+
},
166+
view: {
167+
accessibilityLabel: 'foo',
168+
accessibilityRole: 'image'
169+
}
170+
});
171+
});
172+
});
110173
describe('regarding markers', () => {
111174
it('should set an anchor marker with <a> elements', () => {
112175
const ttree = translateTreeTest('<a href="hello">Yo!</a>');

packages/transient-render-engine/src/model/defaultHTMLElementModels.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,10 @@ const renderedEmbeddedModelMap: HTMLModelRecord<
556556
tagName: 'img',
557557
category: 'embedded',
558558
isVoid: true,
559-
getReactNativeProps({ attributes }) {
559+
getReactNativeProps({ attributes }, props) {
560560
// see https://w3c.github.io/html-aria/#el-img
561561
const label = attributes['aria-label'] || attributes.alt;
562-
if (label) {
562+
if (label && (!props?.view || props.view.accessibilityRole !== 'none')) {
563563
return {
564564
native: {
565565
accessibilityLabel: label,

0 commit comments

Comments
 (0)