Skip to content

Commit 2663e9e

Browse files
committed
Ignore alt check for aria-hidden images
Treat <img> elements with aria-hidden="true" as exempt from the alt attribute requirement. Update rule implementation (src + built dist) to detect aria-hidden (case-insensitive) and skip the warning, add unit tests for aria-hidden="true" and "TRUE", and update the rule documentation with the new behavior and example.
1 parent 4b74a34 commit 2663e9e

4 files changed

Lines changed: 20 additions & 4 deletions

File tree

dist/core/rules/alt-require.js

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/rules/alt-require.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ export default {
88
parser.addListener('tagstart', (event) => {
99
const tagName = event.tagName.toLowerCase()
1010
const mapAttrs = parser.getMapAttrs(event.attrs)
11+
const isAriaHidden = mapAttrs['aria-hidden']?.toLowerCase() === 'true'
1112
const col = event.col + tagName.length + 1
1213
let selector
1314

14-
if (tagName === 'img' && !('alt' in mapAttrs)) {
15+
if (tagName === 'img' && !('alt' in mapAttrs) && !isAriaHidden) {
1516
reporter.warn(
1617
'An alt attribute must be present on <img> elements.',
1718
event.line,

test/rules/alt-require.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ describe(`Rules: ${ruleId}`, () => {
1818
expect(messages.length).toBe(0)
1919
})
2020

21+
it('Img tag with aria-hidden="true" and no alt attribute should not result in an error', () => {
22+
const code = '<img width="200" height="300" aria-hidden="true">'
23+
const messages = HTMLHint.verify(code, ruleOptions)
24+
expect(messages.length).toBe(0)
25+
})
26+
27+
it('Img tag with aria-hidden="TRUE" and no alt attribute should not result in an error', () => {
28+
const code = '<img width="200" height="300" aria-hidden="TRUE">'
29+
const messages = HTMLHint.verify(code, ruleOptions)
30+
expect(messages.length).toBe(0)
31+
})
32+
2133
it('Img tag have not alt attribute should result in an error', () => {
2234
const code = '<img width="200" height="300">'
2335
const messages = HTMLHint.verify(code, ruleOptions)

website/src/content/docs/rules/alt-require.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ keywords:
1010

1111
import { Badge } from '@astrojs/starlight/components';
1212

13-
Alt of `img` must be present and alt of `area[href]` and `input[type=image]` must be set with a value.
13+
Alt of `img` must be present unless the image is hidden from assistive technologies with `aria-hidden="true"`. Alt of `area[href]` and `input[type=image]` must be set with a value.
1414

1515
Level: <Badge text="Warning" variant="caution" />
1616

@@ -23,6 +23,7 @@ Level: <Badge text="Warning" variant="caution" />
2323

2424
```html
2525
<img src="test.png" alt="test" />
26+
<img src="decorative.png" aria-hidden="true" />
2627
<input type="image" alt="test" />
2728
<area shape="circle" coords="180,139,14" href="test.html" alt="test" />
2829
```

0 commit comments

Comments
 (0)