Skip to content

Ignore alt check for aria-hidden images#1851

Merged
coliff merged 3 commits intomainfrom
1778-if-an-image-has-aria-hiddentrue-it-does-not-need-an-alt-attribute
Apr 17, 2026
Merged

Ignore alt check for aria-hidden images#1851
coliff merged 3 commits intomainfrom
1778-if-an-image-has-aria-hiddentrue-it-does-not-need-an-alt-attribute

Conversation

@coliff
Copy link
Copy Markdown
Member

@coliff coliff commented Apr 16, 2026

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.

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.
Copilot AI review requested due to automatic review settings April 16, 2026 01:15
@coliff coliff linked an issue Apr 16, 2026 that may be closed by this pull request
@coliff coliff added the bug Functionality that does not work as intended/expected label Apr 16, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the alt-require rule to permit img tags without an alt attribute if they are marked with aria-hidden="true". The changes include updates to the core logic, documentation, and new test cases. Feedback suggests optimizing the attribute check by moving it within the specific tag condition to avoid unnecessary processing and adding .trim() for better robustness. Additionally, further test cases for aria-hidden="false" and whitespace handling were recommended.

Comment thread src/core/rules/alt-require.ts Outdated
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)
})

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the alt-require rule so <img> elements hidden from assistive technologies via aria-hidden="true" don’t trigger the missing-alt warning, aligning lint behavior with the documented exemption.

Changes:

  • Skip the <img> missing-alt warning when aria-hidden is present with value true (value check is case-insensitive).
  • Add unit tests for aria-hidden="true" and aria-hidden="TRUE".
  • Update rule documentation and examples to reflect the new exemption; commit updated dist output.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 4 comments.

File Description
website/src/content/docs/rules/alt-require.mdx Documents the aria-hidden="true" exemption and adds an example.
test/rules/alt-require.spec.js Adds coverage ensuring hidden images don’t produce messages.
src/core/rules/alt-require.ts Implements the aria-hidden="true" exemption in the rule logic.
dist/core/rules/alt-require.js Updates compiled output to match the rule behavior change.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/core/rules/alt-require.ts Outdated
Comment thread src/core/rules/alt-require.ts Outdated
Comment thread test/rules/alt-require.spec.js
Comment thread website/src/content/docs/rules/alt-require.mdx
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.
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Apr 16, 2026

Deploying htmlhint with  Cloudflare Pages  Cloudflare Pages

Latest commit: db67e8e
Status: ✅  Deploy successful!
Preview URL: https://3b8b0c81.htmlhint.pages.dev
Branch Preview URL: https://1778-if-an-image-has-aria-hi.htmlhint.pages.dev

View logs

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).
@coliff coliff merged commit c18c0fa into main Apr 17, 2026
22 checks passed
@coliff coliff deleted the 1778-if-an-image-has-aria-hiddentrue-it-does-not-need-an-alt-attribute branch April 17, 2026 00:58
@coliff coliff mentioned this pull request Apr 17, 2026
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Functionality that does not work as intended/expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

If an image has aria-hidden="true", it does not need an alt attribute

2 participants