Skip to content

Commit c18c0fa

Browse files
authored
Ignore alt check for aria-hidden images (#1851)
* 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. * Handle trimmed aria-hidden for <img> alt rule Refine alt-require rule to trim and normalize the aria-hidden attribute when deciding whether an <img> needs an alt. The check now inlines mapAttrs['aria-hidden']?.trim().toLowerCase() !== 'true' (removing the previous isAriaHidden variable) so values with surrounding whitespace are treated correctly. Changes applied to both src (TS) and compiled dist (JS) files. * Test aria-hidden behavior in alt rule Add tests to alt-require.spec.js to verify aria-hidden handling: an <img> with aria-hidden=" true " (with surrounding spaces) should not trigger an alt error, while an <img> with aria-hidden="false" should produce a warning (checks include rule id, line, column and type).
1 parent 4b74a34 commit c18c0fa

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

dist/core/rules/alt-require.js

Lines changed: 5 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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ export default {
1111
const col = event.col + tagName.length + 1
1212
let selector
1313

14-
if (tagName === 'img' && !('alt' in mapAttrs)) {
14+
if (
15+
tagName === 'img' &&
16+
!('alt' in mapAttrs) &&
17+
mapAttrs['aria-hidden']?.trim().toLowerCase() !== 'true'
18+
) {
1519
reporter.warn(
1620
'An alt attribute must be present on <img> elements.',
1721
event.line,

test/rules/alt-require.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@ 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+
33+
it('Img tag with aria-hidden=" true " and no alt attribute should not result in an error', () => {
34+
const code = '<img width="200" height="300" aria-hidden=" true ">'
35+
const messages = HTMLHint.verify(code, ruleOptions)
36+
expect(messages.length).toBe(0)
37+
})
38+
39+
it('Img tag with aria-hidden="false" and no alt attribute should result in an error', () => {
40+
const code = '<img width="200" height="300" aria-hidden="false">'
41+
const messages = HTMLHint.verify(code, ruleOptions)
42+
expect(messages.length).toBe(1)
43+
expect(messages[0].rule.id).toBe(ruleId)
44+
expect(messages[0].line).toBe(1)
45+
expect(messages[0].col).toBe(5)
46+
expect(messages[0].type).toBe('warning')
47+
})
48+
2149
it('Img tag have not alt attribute should result in an error', () => {
2250
const code = '<img width="200" height="300">'
2351
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)