| 1 | # jsx-a11y/anchor-ambiguous-text
|
|---|
| 2 |
|
|---|
| 3 | 🚫 This rule is _disabled_ in the ☑️ `recommended` config.
|
|---|
| 4 |
|
|---|
| 5 | <!-- end auto-generated rule header -->
|
|---|
| 6 |
|
|---|
| 7 | Enforces `<a>` values are not exact matches for the phrases "click here", "here", "link", "a link", or "learn more". Screen readers announce tags as links/interactive, but rely on values for context. Ambiguous anchor descriptions do not provide sufficient context for users.
|
|---|
| 8 |
|
|---|
| 9 | ## Rule options
|
|---|
| 10 |
|
|---|
| 11 | This rule takes one optional object argument with the parameter `words`.
|
|---|
| 12 |
|
|---|
| 13 | ```json
|
|---|
| 14 | {
|
|---|
| 15 | "rules": {
|
|---|
| 16 | "jsx-a11y/anchor-ambiguous-text": [2, {
|
|---|
| 17 | "words": ["click this"],
|
|---|
| 18 | }],
|
|---|
| 19 | }
|
|---|
| 20 | }
|
|---|
| 21 | ```
|
|---|
| 22 |
|
|---|
| 23 | The `words` option allows users to modify the strings that can be checked for in the anchor text. Useful for specifying other words in other languages. The default value is set by `DEFAULT_AMBIGUOUS_WORDS`:
|
|---|
| 24 |
|
|---|
| 25 | ```js
|
|---|
| 26 | const DEFAULT_AMBIGUOUS_WORDS = ['click here', 'here', 'link', 'a link', 'learn more'];
|
|---|
| 27 | ```
|
|---|
| 28 |
|
|---|
| 29 | The logic to calculate the inner text of an anchor is as follows:
|
|---|
| 30 |
|
|---|
| 31 | - if an element has the `aria-label` property, its value is used instead of the inner text
|
|---|
| 32 | - if an element has `aria-hidden="true`, it is skipped over
|
|---|
| 33 | - if an element is `<img />` or configured to be interpreted like one, its `alt` value is used as its inner text
|
|---|
| 34 |
|
|---|
| 35 | Note that this rule still disallows ambiguous `aria-label` or `alt` values.
|
|---|
| 36 |
|
|---|
| 37 | Note that this rule is case-insensitive, trims whitespace, and ignores certain punctuation (`[,.?¿!‽¡;:]`). It only looks for **exact matches**.
|
|---|
| 38 |
|
|---|
| 39 | ### Succeed
|
|---|
| 40 |
|
|---|
| 41 | ```jsx
|
|---|
| 42 | <a>read this tutorial</a> // passes since it is not one of the disallowed words
|
|---|
| 43 | <a>${here}</a> // this is valid since 'here' is a variable name
|
|---|
| 44 | <a aria-label="tutorial on using eslint-plugin-jsx-a11y">click here</a> // the aria-label supersedes the inner text
|
|---|
| 45 | ```
|
|---|
| 46 |
|
|---|
| 47 | ### Fail
|
|---|
| 48 |
|
|---|
| 49 | ```jsx
|
|---|
| 50 | <a>here</a>
|
|---|
| 51 | <a>HERE</a>
|
|---|
| 52 | <a>link</a>
|
|---|
| 53 | <a>click here</a>
|
|---|
| 54 | <a>learn more</a>
|
|---|
| 55 | <a>learn more.</a>
|
|---|
| 56 | <a>learn more,</a>
|
|---|
| 57 | <a>learn more?</a>
|
|---|
| 58 | <a>learn more!</a>
|
|---|
| 59 | <a>learn more:</a>
|
|---|
| 60 | <a>learn more;</a>
|
|---|
| 61 | <a>a link</a>
|
|---|
| 62 | <a> a link </a>
|
|---|
| 63 | <a><span> click </span> here</a> // goes through element children
|
|---|
| 64 | <a>a<i></i> link</a>
|
|---|
| 65 | <a><i></i>a link</a>
|
|---|
| 66 | <a><span aria-hidden="true">more text</span>learn more</a> // skips over elements with aria-hidden=true
|
|---|
| 67 | <a aria-label="click here">something</a> // the aria-label here is inaccessible
|
|---|
| 68 | <a><img alt="click here"/></a> // the alt tag is still ambiguous
|
|---|
| 69 | <a alt="tutorial on using eslint-plugin-jsx-a11y">click here</a> // the alt tag is only parsed on img
|
|---|
| 70 | ```
|
|---|
| 71 |
|
|---|
| 72 | ## Accessibility guidelines
|
|---|
| 73 |
|
|---|
| 74 | Ensure anchor tags describe the content of the link, opposed to simply describing them as a link.
|
|---|
| 75 |
|
|---|
| 76 | Compare
|
|---|
| 77 |
|
|---|
| 78 | ```jsx
|
|---|
| 79 | <p><a href="#">click here</a> to read a tutorial by Foo Bar</p>
|
|---|
| 80 | ```
|
|---|
| 81 |
|
|---|
| 82 | which can be more concise and accessible with
|
|---|
| 83 |
|
|---|
| 84 | ```jsx
|
|---|
| 85 | <p>read <a href="#">a tutorial by Foo Bar</a></p>
|
|---|
| 86 | ```
|
|---|
| 87 |
|
|---|
| 88 | ### Resources
|
|---|
| 89 |
|
|---|
| 90 | 1. [WebAIM, Hyperlinks](https://webaim.org/techniques/hypertext/)
|
|---|
| 91 | 2. [Deque University, Link Checklist - 'Avoid "link" (or similar) in the link text'](https://dequeuniversity.com/checklists/web/links)
|
|---|