Skip to content

fix: support unquoted aria-hidden value in require-alt-text - #719

Merged
DMartens merged 3 commits into
eslint:mainfrom
KumJungMin:fix/require-alt-text-quoted
Aug 20, 2026
Merged

fix: support unquoted aria-hidden value in require-alt-text#719
DMartens merged 3 commits into
eslint:mainfrom
KumJungMin:fix/require-alt-text-quoted

Conversation

@KumJungMin

@KumJungMin KumJungMin commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Disclosure: I'm a participant of open source contribution program OSSCA

Prerequisites checklist

AI acknowledgment

  • I did not use AI to generate this PR.
  • (If the above is not checked) I have reviewed the AI-generated content before submitting.

What is the purpose of this pull request?

This PR updates require-alt-text to recognize an unquoted aria-hidden=true attribute.

HTML permits attribute values to be written without quotes, but the existing implementation handles the following forms differently:

<img src="quoted.png" aria-hidden="true" />
<img src="unquoted.png" aria-hidden=true />

Since both forms set the value of aria-hidden to true, this PR updates the rule to skip the alternative text check in both cases.

What changes did you make? (Give an overview)

  • Added a dedicated regular expression that recognizes double-quoted, single-quoted, and unquoted aria-hidden=true attributes.
  • Added support for optional whitespace around the equals sign.
  • Made matching case-insensitive for both the attribute name and value.
  • Added a boundary check to ensure that the alternative text check is skipped only when the value is exactly true.
  • Added valid test cases for the following unquoted forms:
    • aria-hidden=true
    • aria-hidden = true
    • ARIA-HIDDEN=TRUE
  • Added invalid test cases to verify that aria-hidden=false and aria-hidden=truefoo are still reported.

The regex test cases for this change are available on RegExr.

Related Issues

fixes #718

Is there anything you'd like reviewers to focus on?

To keep the scope of this change minimal, the implementation adds a dedicated regular expression for detecting aria-hidden=true instead of extending getHtmlAttributeRe().

There are two reasons for this approach:

  1. It avoids affecting the existing alt attribute parsing behavior and avoids recreating the aria-hidden regular expression each time an image tag is processed.
  2. aria-hidden requires checking whether its value is exactly true, while getHtmlAttributeRe("alt") captures the value of the alt attribute to determine whether it consists only of whitespace. I considered these attributes to have different validation purposes and criteria.

I would appreciate feedback on whether this dedicated regular expression is appropriate or whether it would be better to generalize getHtmlAttributeRe() to handle both quoted and unquoted attribute values. :)

Comment thread src/rules/require-alt-text.js Outdated

const imgTagPattern = /<img(?:\s(?:[^>"']|"[^"]*"|'[^']*')*)?\/?>/giu;
const ariaHiddenTruePattern =
/\saria-hidden\s*=\s*(?:"true"|'true'|true)(?=\s|\/?>)/iu;

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.

There does not have to be whitespace around an HTML attribute, e.g. <a before="true"aria-hidden="true"after="true"></a> is valid.
In other words the before "aria-hidden" can be a quote or whitespace and alpha characters if the attribute value is quoted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I’d like to update it as follows to account for the additional cases.

Case Before After Reason
Boundary before aria-hidden \s (?:\s|["']) Allows aria-hidden to be preceded not only by whitespace, but also directly by the closing quote (" or ') of the previous attribute.
Boundary after true (?:"true"|'true'|true)(?=\s|/?>) (?:"true"|'true'|true(?=\s|/?>)) Allows another attribute to immediately follow "true" or 'true' without whitespace, while keeping the existing boundary check for unquoted true so that cases like aria-hidden=truefoo are not matched incorrectly.

I’m thinking of updating the final pattern as follows. Is there any case I may have misunderstood? (https://regexr.com/8o87r)

/(?:\s|["'])aria-hidden\s*=\s*(?:"true"|'true'|true(?=\s|\/?&gt;))/iu

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.

Yes the new pattern works for the mentioned cases.
Can you please add test cases for this and replace &gt; with > (no need to escape it in this case) in the pattern?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I updated the regular expression to address the regexp/prefer-character-class ESLint error.

스크린샷 2026-08-20 오후 7 31 10

The previous pattern used a disjunction of single-character alternatives:

(?:\s|["'])

Since this matches either whitespace, " or ', it can be expressed more directly as a character class without changing its meaning:

[\s"']

So I updated the pattern as follows:

/[\s"']aria-hidden\s*=\s*(?:"true"|'true'|true(?=\s|\/?>))/iu

Here, [\s"'] allows aria-hidden to be preceded by whitespace or by the closing quote of the previous attribute. For the unquoted true form, the lookahead (?=\s|\/?>) is kept to ensure that values such as aria-hidden=truefoo are not matched.

I also added test cases for attributes without whitespace between them and confirmed that the previously discussed cases still work as expected. (https://regexr.com/8o8jo)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The updated commit is b403e09

@DMartens DMartens moved this from Needs Triage to Implementing in Triage Aug 18, 2026
@KumJungMin
KumJungMin requested a review from DMartens August 20, 2026 10:38

@DMartens DMartens left a comment

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.

Changes LGTM, thanks.

@DMartens
DMartens merged commit c4a148b into eslint:main Aug 20, 2026
39 checks passed
@github-project-automation github-project-automation Bot moved this from Implementing to Complete in Triage Aug 20, 2026
@KumJungMin
KumJungMin deleted the fix/require-alt-text-quoted branch August 21, 2026 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Complete

Development

Successfully merging this pull request may close these issues.

Bug: require-alt-text does not recognize an unquoted aria-hidden=true attribute

3 participants