Skip to content

Commit 0d95201

Browse files
Merge remote-tracking branch 'origin' into add-a11y-no-title-attribute
2 parents 35ef90c + 86d1fa5 commit 0d95201

File tree

8 files changed

+199
-156
lines changed

8 files changed

+199
-156
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ This config will be interpreted in the following way:
8585
| :------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------- | :- | :- | :- |
8686
| [a11y-aria-label-is-well-formatted](docs/rules/a11y-aria-label-is-well-formatted.md) | [aria-label] text should be formatted as you would visual text. | ⚛️ | | |
8787
| [a11y-no-generic-link-text](docs/rules/a11y-no-generic-link-text.md) | disallow generic link text | | ||
88-
| [a11y-no-title-attribute](docs/rules/a11y-no-title-attribute.md) | Guards against developers using the title attribute | | | |
88+
| [a11y-no-title-attribute](docs/rules/a11y-no-title-attribute.md) | Guards against developers using the title attribute | ⚛️ | | |
8989
| [a11y-no-visually-hidden-interactive-element](docs/rules/a11y-no-visually-hidden-interactive-element.md) | Ensures that interactive elements are not visually hidden | ⚛️ | | |
90+
| [a11y-svg-has-accessible-name](docs/rules/a11y-svg-has-accessible-name.md) | SVGs must have an accessible name | ⚛️ | | |
9091
| [array-foreach](docs/rules/array-foreach.md) | enforce `for..of` loops over `Array.forEach` || | |
9192
| [async-currenttarget](docs/rules/async-currenttarget.md) | disallow `event.currentTarget` calls inside of async functions | 🔍 | | |
9293
| [async-preventdefault](docs/rules/async-preventdefault.md) | disallow `event.preventDefault` calls inside of async functions | 🔍 | | |

docs/rules/a11y-no-title-attribute.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Guards against developers using the title attribute (`github/a11y-no-title-attribute`)
22

3+
💼 This rule is enabled in the ⚛️ `react` config.
4+
35
<!-- end auto-generated rule header -->
46

57
The title attribute is strongly discouraged. The only exception is on an `<iframe>` element. It is hardly useful and cannot be accessed by multiple groups of users including keyboard-only users and mobile users.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# SVGs must have an accessible name (`github/a11y-svg-has-accessible-name`)
2+
3+
💼 This rule is enabled in the ⚛️ `react` config.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
## Rule Details
8+
9+
An `<svg>` must have an accessible name. Set `aria-label` or `aria-labelledby`, or nest a `<title>` element as the first child of the `<svg>` element.
10+
11+
However, if the `<svg>` is purely decorative, hide it with `aria-hidden="true"` or `role="presentation"`.
12+
13+
## Resources
14+
15+
- [Accessible SVGs](https://css-tricks.com/accessible-svgs/)
16+
17+
## Examples
18+
19+
### **Incorrect** code for this rule 👎
20+
21+
```html
22+
<svg height='100' width='100'>
23+
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/>
24+
</svg>
25+
```
26+
27+
```html
28+
<svg height='100' width='100' title='Circle with a black outline and red fill'>
29+
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/>
30+
</svg>
31+
```
32+
33+
```html
34+
<svg height='100' width='100'>
35+
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/>
36+
<title>Circle with a black outline and red fill</title>
37+
</svg>
38+
```
39+
40+
### **Correct** code for this rule 👍
41+
42+
```html
43+
<svg height='100' width='100'>
44+
<title>Circle with a black outline and red fill</title>
45+
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/>
46+
</svg>
47+
```
48+
49+
```html
50+
<svg aria-label='Circle with a black outline and red fill' height='100' width='100'>
51+
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/>
52+
</svg>
53+
```
54+
55+
```html
56+
<svg aria-labelledby='circle_text' height='100' width='100'>
57+
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/>
58+
</svg>
59+
```
60+
61+
```html
62+
<svg aria-hidden='true' height='100' width='100'>
63+
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/>
64+
</svg>
65+
```
66+
67+
```html
68+
<svg role='presentation' height='100' width='100'>
69+
<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/>
70+
</svg>
71+
```
72+
73+
## Version

lib/configs/react.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
'github/a11y-aria-label-is-well-formatted': 'error',
1313
'github/a11y-no-visually-hidden-interactive-element': 'error',
1414
'github/a11y-no-title-attribute': 'error',
15+
'github/a11y-svg-has-accessible-name': 'error',
1516
'github/role-supports-aria-props': 'error',
1617
'jsx-a11y/no-aria-hidden-on-focusable': 'error',
1718
'jsx-a11y/no-autofocus': 'off',

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module.exports = {
44
'a11y-no-generic-link-text': require('./rules/a11y-no-generic-link-text'),
55
'a11y-no-title-attribute': require('./rules/a11y-no-title-attribute'),
66
'a11y-aria-label-is-well-formatted': require('./rules/a11y-aria-label-is-well-formatted'),
7+
'a11y-svg-has-accessible-name': require('./rules/a11y-svg-has-accessible-name'),
78
'array-foreach': require('./rules/array-foreach'),
89
'async-currenttarget': require('./rules/async-currenttarget'),
910
'async-preventdefault': require('./rules/async-preventdefault'),
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const {hasProp} = require('jsx-ast-utils')
2+
const {getElementType} = require('../utils/get-element-type')
3+
4+
module.exports = {
5+
meta: {
6+
docs: {
7+
description: 'SVGs must have an accessible name',
8+
url: require('../url')(module),
9+
},
10+
schema: [],
11+
},
12+
13+
create(context) {
14+
return {
15+
JSXOpeningElement: node => {
16+
const elementType = getElementType(context, node)
17+
if (elementType !== 'svg') return
18+
19+
// Check if there is a nested title element that is the first child of the `<svg>`
20+
const hasNestedTitleAsFirstChild =
21+
node.parent.children?.[0]?.type === 'JSXElement' &&
22+
node.parent.children?.[0]?.openingElement?.name?.name === 'title'
23+
24+
// Check if `aria-label` or `aria-labelledby` is set
25+
const hasAccessibleName = hasProp(node.attributes, 'aria-label') || hasProp(node.attributes, 'aria-labelledby')
26+
27+
// Check if SVG is decorative
28+
const isDecorative =
29+
hasProp(node.attributes, 'role', 'presentation') || hasProp(node.attributes, 'aria-hidden', 'true')
30+
31+
if (elementType === 'svg' && !hasAccessibleName && !isDecorative && !hasNestedTitleAsFirstChild) {
32+
context.report({
33+
node,
34+
message:
35+
'`<svg>` must have an accessible name. Set `aria-label` or `aria-labelledby`, or nest a `<title>` element. However, if the `<svg>` is purely decorative, hide it with `aria-hidden="true"` or `role="presentation"`.',
36+
})
37+
}
38+
},
39+
}
40+
},
41+
}

0 commit comments

Comments
 (0)