Skip to content

Commit 65fa6f2

Browse files
Merge pull request #453 from github/add-a11y-no-title-attribute
Create rule: a11y-no-title-attribute
2 parents 86d1fa5 + 0d95201 commit 65fa6f2

8 files changed

+177
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ 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 | ⚛️ | | |
8889
| [a11y-no-visually-hidden-interactive-element](docs/rules/a11y-no-visually-hidden-interactive-element.md) | Ensures that interactive elements are not visually hidden | ⚛️ | | |
8990
| [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` || | |

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

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Guards against developers using the title attribute (`github/a11y-no-title-attribute`)
2+
3+
💼 This rule is enabled in the ⚛️ `react` config.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
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.
8+
9+
The `title` attribute is commonly set on links, matching the link text. This is redundant and unnecessary so it can be simply be removed.
10+
11+
If you are considering the `title` attribute to provide supplementary description, consider whether the text in question can be persisted in the design. Alternatively, if it's important to display supplementary text that is hidden by default, consider using an accessible tooltip implementation that uses the aria-labelledby or aria-describedby semantics. Even so, proceed with caution: tooltips should only be used on interactive elements like links or buttons. See [Tooltip alternatives](https://primer.style/design/guides/accessibility/tooltip-alternatives) for more accessible alternatives.
12+
13+
### Should I use the title attribute to provide an accessible name for an <svg>?
14+
15+
Use a <title> element instead of the title attribute, or an aria-label.
16+
17+
## Rule Details
18+
19+
👎 Examples of **incorrect** code for this rule:
20+
21+
```jsx
22+
<a src="https://www.github.com" title="A home for all developers">
23+
GitHub
24+
</a>
25+
```
26+
27+
```jsx
28+
<a href="/" title="github.com">
29+
GitHub
30+
</a>
31+
```
32+
33+
```jsx
34+
<span src="https://www.github.com" title="supercalifragilisticexpialidocious">
35+
supercali...
36+
</span>
37+
```
38+
39+
👍 Examples of **correct** code for this rule:
40+
41+
```jsx
42+
<iframe src="https://www.github.com" title="Github"></iframe>
43+
```
44+
45+
## Version

lib/configs/react.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
'jsx-a11y/role-supports-aria-props': 'off', // Override with github/role-supports-aria-props until https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/issues/910 is resolved
1212
'github/a11y-aria-label-is-well-formatted': 'error',
1313
'github/a11y-no-visually-hidden-interactive-element': 'error',
14+
'github/a11y-no-title-attribute': 'error',
1415
'github/a11y-svg-has-accessible-name': 'error',
1516
'github/role-supports-aria-props': 'error',
1617
'jsx-a11y/no-aria-hidden-on-focusable': 'error',

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
rules: {
33
'a11y-no-visually-hidden-interactive-element': require('./rules/a11y-no-visually-hidden-interactive-element'),
44
'a11y-no-generic-link-text': require('./rules/a11y-no-generic-link-text'),
5+
'a11y-no-title-attribute': require('./rules/a11y-no-title-attribute'),
56
'a11y-aria-label-is-well-formatted': require('./rules/a11y-aria-label-is-well-formatted'),
67
'a11y-svg-has-accessible-name': require('./rules/a11y-svg-has-accessible-name'),
78
'array-foreach': require('./rules/array-foreach'),

lib/rules/a11y-no-title-attribute.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const {getProp, getPropValue} = require('jsx-ast-utils')
2+
const {getElementType} = require('../utils/get-element-type')
3+
4+
const SEMANTIC_ELEMENTS = [
5+
'a',
6+
'button',
7+
'summary',
8+
'select',
9+
'option',
10+
'textarea',
11+
'input',
12+
'span',
13+
'div',
14+
'p',
15+
'h1',
16+
'h2',
17+
'h3',
18+
'h4',
19+
'h5',
20+
'h6',
21+
'details',
22+
'summary',
23+
'dialog',
24+
'tr',
25+
'th',
26+
'td',
27+
'label',
28+
]
29+
30+
const ifSemanticElement = (context, node) => {
31+
const elementType = getElementType(context, node.openingElement, true)
32+
33+
for (const semanticElement of SEMANTIC_ELEMENTS) {
34+
if (elementType === semanticElement) {
35+
return true
36+
}
37+
}
38+
return false
39+
}
40+
41+
module.exports = {
42+
meta: {
43+
docs: {
44+
description: 'Guards against developers using the title attribute',
45+
url: require('../url')(module),
46+
},
47+
schema: [],
48+
},
49+
50+
create(context) {
51+
return {
52+
JSXElement: node => {
53+
const elementType = getElementType(context, node.openingElement)
54+
if (elementType !== `iframe` && ifSemanticElement(context, node)) {
55+
const titleProp = getPropValue(getProp(node.openingElement.attributes, `title`))
56+
if (titleProp) {
57+
context.report({
58+
node,
59+
message: 'The title attribute is not accessible and should never be used unless for an `<iframe>`.',
60+
})
61+
}
62+
}
63+
},
64+
}
65+
},
66+
}

lib/rules/a11y-no-visually-hidden-interactive-element.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ const schema = generateObjSchema({
1515
* because a visually hidden input field might cause a false positive.
1616
* (e.g. fileUpload https://github.com/primer/react/pull/3492)
1717
*/
18-
const INTERACTIVELEMENTS = ['a', 'button', 'summary', 'select', 'option', 'textarea']
18+
const INTERACTIVE_ELEMENTS = ['a', 'button', 'summary', 'select', 'option', 'textarea']
1919

2020
const checkIfInteractiveElement = (context, node) => {
2121
const elementType = getElementType(context, node.openingElement)
2222

23-
for (const interactiveElement of INTERACTIVELEMENTS) {
23+
for (const interactiveElement of INTERACTIVE_ELEMENTS) {
2424
if (elementType === interactiveElement) {
2525
return true
2626
}
@@ -76,7 +76,6 @@ module.exports = {
7676
message:
7777
'Avoid visually hidding interactive elements. Visually hiding interactive elements can be confusing to sighted keyboard users as it appears their focus has been lost when they navigate to the hidden element.',
7878
})
79-
return
8079
}
8180
},
8281
}

lib/utils/get-element-type.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ If a prop determines the type, it can be specified with `props`.
77
88
For now, we only support the mapping of one prop type to an element type, rather than combinations of props.
99
*/
10-
function getElementType(context, node) {
10+
function getElementType(context, node, ignoreMap = false) {
1111
const {settings} = context
1212

1313
// check if the node contains a polymorphic prop
1414
const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as'
1515
const rawElement = getPropValue(getProp(node.attributes, polymorphicPropName)) ?? elementType(node)
1616

1717
// if a component configuration does not exists, return the raw element
18-
if (!settings?.github?.components?.[rawElement]) return rawElement
18+
if (ignoreMap || !settings?.github?.components?.[rawElement]) return rawElement
1919

2020
const defaultComponent = settings.github.components[rawElement]
2121

tests/a11y-no-title-attribute.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const rule = require('../lib/rules/a11y-no-title-attribute')
2+
const RuleTester = require('eslint').RuleTester
3+
4+
const ruleTester = new RuleTester({
5+
parserOptions: {
6+
ecmaVersion: 'latest',
7+
sourceType: 'module',
8+
ecmaFeatures: {
9+
jsx: true,
10+
},
11+
},
12+
})
13+
14+
const errorMessage = 'The title attribute is not accessible and should never be used unless for an `<iframe>`.'
15+
16+
ruleTester.run('a11y-no-title-attribute', rule, {
17+
valid: [
18+
{code: '<button>Submit</button>'},
19+
{code: '<iframe title="an allowed title">GitHub</iframe>'},
20+
{code: '<span>some information</span>'},
21+
{code: '<a href="github.com">GitHub</a>'},
22+
{
23+
code: '<Component title="some title">Submit</Component>',
24+
settings: {
25+
github: {
26+
components: {
27+
Component: 'iframe',
28+
},
29+
},
30+
},
31+
},
32+
{
33+
// Note: we are only checking semantic elements. We cannot make assumptions about how a React Components is using the title prop.
34+
code: '<Link title="some title">Submit</Link>',
35+
settings: {
36+
github: {
37+
components: {
38+
Link: 'a',
39+
},
40+
},
41+
},
42+
},
43+
],
44+
invalid: [
45+
{code: '<a title="some title" href="github.com">GitHub</a>', errors: [{message: errorMessage}]},
46+
{code: '<span><button title="some title">submit</button></span>', errors: [{message: errorMessage}]},
47+
{
48+
code: '<Component as="a" title="some title">Submit</Component>',
49+
errors: [{message: errorMessage}],
50+
settings: {
51+
github: {
52+
components: {
53+
Component: 'iframe',
54+
},
55+
},
56+
},
57+
},
58+
],
59+
})

0 commit comments

Comments
 (0)