Skip to content

Commit fa9845d

Browse files
lb-ljharb
authored andcommitted
[patch] no-redundandant-roles: allow <img src="*.svg" role="img" />
Setting role="img" is a valid use case to work around a Safari bug for better accessibility when the source image is an SVG file. This improvement does not account for variables in the `src` attribute but adds a valid exception for when we can parse the string. Fixes #936
1 parent daba189 commit fa9845d

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

__tests__/src/rules/no-redundant-roles-test.js

+4
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,16 @@ ruleTester.run(`${ruleName}:recommended (valid list role override)`, rule, {
8383
{ code: '<ul role="list" />' },
8484
{ code: '<ol role="list" />' },
8585
{ code: '<dl role="list" />' },
86+
{ code: '<img src="example.svg" role="img" />' },
87+
{ code: '<svg role="img" />' },
8688
))
8789
.map(ruleOptionsMapperFactory(listException))
8890
.map(parserOptionsMapper),
8991
invalid: parsers.all([].concat(
9092
{ code: '<ul role="list" />', errors: [expectedError('ul', 'list')] },
9193
{ code: '<ol role="list" />', errors: [expectedError('ol', 'list')] },
94+
{ code: '<img role="img" />', errors: [expectedError('img', 'img')] },
95+
{ code: '<img src={someVariable} role="img" />', errors: [expectedError('img', 'img')] },
9296
))
9397
.map(parserOptionsMapper),
9498
});

docs/rules/no-redundant-roles.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ General best practice (reference resources)
4343
### Resources
4444

4545
- [ARIA Spec, ARIA Adds Nothing to Default Semantics of Most HTML Elements](https://www.w3.org/TR/using-aria/#aria-does-nothing)
46+
- [Identifying SVG as an image](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#identifying_svg_as_an_image)

src/util/implicitRoles/img.js

+10
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,15 @@ export default function getImplicitRoleForImg(attributes) {
1010
return '';
1111
}
1212

13+
/**
14+
* If the src attribute can be determined to be an svg, allow the role to be set to 'img'
15+
* so that VoiceOver on Safari can be better supported.
16+
*
17+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#identifying_svg_as_an_image
18+
* @see https://bugs.webkit.org/show_bug.cgi?id=216364
19+
*/
20+
const src = getProp(attributes, 'src');
21+
if (src && getLiteralPropValue(src)?.includes('.svg')) { return ''; }
22+
1323
return 'img';
1424
}

0 commit comments

Comments
 (0)