Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[patch] no-redundandant-roles: allow <img src="*.svg" role="img" /> #1026

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions __tests__/src/rules/aria-role-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ ruleTester.run('aria-role', rule, {
code: '<Box asChild="div" role="button" />',
settings: customDivSettings,
},
{
code: '<svg role="graphics-document document" />',
},
{ code: '<svg role="graphics-document document" />' },
{ code: '<svg role="img" />' },
)).concat(validTests).map(parserOptionsMapper),

invalid: parsers.all([].concat(
Expand Down
4 changes: 4 additions & 0 deletions __tests__/src/rules/no-redundant-roles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,16 @@ ruleTester.run(`${ruleName}:recommended (valid list role override)`, rule, {
{ code: '<ul role="list" />' },
{ code: '<ol role="list" />' },
{ code: '<dl role="list" />' },
{ code: '<img src="example.svg" role="img" />' },
{ code: '<svg role="img" />' },
))
.map(ruleOptionsMapperFactory(listException))
.map(parserOptionsMapper),
invalid: parsers.all([].concat(
{ code: '<ul role="list" />', errors: [expectedError('ul', 'list')] },
{ code: '<ol role="list" />', errors: [expectedError('ol', 'list')] },
{ code: '<img role="img" />', errors: [expectedError('img', 'img')] },
{ code: '<img src={someVariable} role="img" />', errors: [expectedError('img', 'img')] },
))
.map(parserOptionsMapper),
});
1 change: 1 addition & 0 deletions docs/rules/no-redundant-roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ General best practice (reference resources)
### Resources

- [ARIA Spec, ARIA Adds Nothing to Default Semantics of Most HTML Elements](https://www.w3.org/TR/using-aria/#aria-does-nothing)
- [Identifying SVG as an image](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#identifying_svg_as_an_image)
10 changes: 10 additions & 0 deletions src/util/implicitRoles/img.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,15 @@ export default function getImplicitRoleForImg(attributes) {
return '';
}

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

return 'img';
}