From daba18974473bce1301ffaafb822558a9e1738ae Mon Sep 17 00:00:00 2001 From: LB Date: Wed, 23 Oct 2024 14:22:29 +1000 Subject: [PATCH 1/2] [Tests] `aria-role`: Add valid test for `` See #936 --- __tests__/src/rules/aria-role-test.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/__tests__/src/rules/aria-role-test.js b/__tests__/src/rules/aria-role-test.js index 9a8ec3a89..c9c3b0356 100644 --- a/__tests__/src/rules/aria-role-test.js +++ b/__tests__/src/rules/aria-role-test.js @@ -84,9 +84,8 @@ ruleTester.run('aria-role', rule, { code: '', settings: customDivSettings, }, - { - code: '', - }, + { code: '' }, + { code: '' }, )).concat(validTests).map(parserOptionsMapper), invalid: parsers.all([].concat( From fa9845ddbf51283257ae6e67df48e13508eb6dd2 Mon Sep 17 00:00:00 2001 From: LB Date: Wed, 23 Oct 2024 14:25:44 +1000 Subject: [PATCH 2/2] [patch] `no-redundandant-roles`: allow `` 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 --- __tests__/src/rules/no-redundant-roles-test.js | 4 ++++ docs/rules/no-redundant-roles.md | 1 + src/util/implicitRoles/img.js | 10 ++++++++++ 3 files changed, 15 insertions(+) diff --git a/__tests__/src/rules/no-redundant-roles-test.js b/__tests__/src/rules/no-redundant-roles-test.js index 60cc2185e..068e54049 100644 --- a/__tests__/src/rules/no-redundant-roles-test.js +++ b/__tests__/src/rules/no-redundant-roles-test.js @@ -83,12 +83,16 @@ ruleTester.run(`${ruleName}:recommended (valid list role override)`, rule, { { code: '
    ' }, { code: '
      ' }, { code: '
      ' }, + { code: '' }, + { code: '' }, )) .map(ruleOptionsMapperFactory(listException)) .map(parserOptionsMapper), invalid: parsers.all([].concat( { code: '
        ', errors: [expectedError('ul', 'list')] }, { code: '
          ', errors: [expectedError('ol', 'list')] }, + { code: '', errors: [expectedError('img', 'img')] }, + { code: '', errors: [expectedError('img', 'img')] }, )) .map(parserOptionsMapper), }); diff --git a/docs/rules/no-redundant-roles.md b/docs/rules/no-redundant-roles.md index 0506b6066..6b20240cf 100644 --- a/docs/rules/no-redundant-roles.md +++ b/docs/rules/no-redundant-roles.md @@ -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) diff --git a/src/util/implicitRoles/img.js b/src/util/implicitRoles/img.js index a03217ffd..e62bbc128 100644 --- a/src/util/implicitRoles/img.js +++ b/src/util/implicitRoles/img.js @@ -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'; }