Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dist/core/rules/alt-require.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/core/rules/alt-require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export default {
const col = event.col + tagName.length + 1
let selector

if (tagName === 'img' && !('alt' in mapAttrs)) {
if (
tagName === 'img' &&
!('alt' in mapAttrs) &&
mapAttrs['aria-hidden']?.trim().toLowerCase() !== 'true'
) {
reporter.warn(
'An alt attribute must be present on <img> elements.',
event.line,
Expand Down
28 changes: 28 additions & 0 deletions test/rules/alt-require.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ describe(`Rules: ${ruleId}`, () => {
expect(messages.length).toBe(0)
})

it('Img tag with aria-hidden="true" and no alt attribute should not result in an error', () => {
const code = '<img width="200" height="300" aria-hidden="true">'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).toBe(0)
})

it('Img tag with aria-hidden="TRUE" and no alt attribute should not result in an error', () => {
const code = '<img width="200" height="300" aria-hidden="TRUE">'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).toBe(0)
})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It would be beneficial to add test cases for aria-hidden="false" (to ensure it still warns when alt is missing) and for values with whitespace (e.g., "true ") to verify the robustness of the implementation.

Suggested change
})
})
it('Img tag with aria-hidden="false" and no alt attribute should result in an error', () => {
const code = '<img width="200" height="300" aria-hidden="false">'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).toBe(1)
})
it('Img tag with aria-hidden="true " and no alt attribute should not result in an error', () => {
const code = '<img width="200" height="300" aria-hidden="true ">'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).toBe(0)
})


Comment thread
coliff marked this conversation as resolved.
it('Img tag with aria-hidden=" true " and no alt attribute should not result in an error', () => {
const code = '<img width="200" height="300" aria-hidden=" true ">'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).toBe(0)
})

it('Img tag with aria-hidden="false" and no alt attribute should result in an error', () => {
const code = '<img width="200" height="300" aria-hidden="false">'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).toBe(1)
expect(messages[0].rule.id).toBe(ruleId)
expect(messages[0].line).toBe(1)
expect(messages[0].col).toBe(5)
expect(messages[0].type).toBe('warning')
})

it('Img tag have not alt attribute should result in an error', () => {
const code = '<img width="200" height="300">'
const messages = HTMLHint.verify(code, ruleOptions)
Expand Down
3 changes: 2 additions & 1 deletion website/src/content/docs/rules/alt-require.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords:

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

Alt of `img` must be present and alt of `area[href]` and `input[type=image]` must be set with a value.
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.

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

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

```html
<img src="test.png" alt="test" />
<img src="decorative.png" aria-hidden="true" />
<input type="image" alt="test" />
Comment thread
coliff marked this conversation as resolved.
<area shape="circle" coords="180,139,14" href="test.html" alt="test" />
```
Expand Down
Loading