Skip to content

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

Open
KumJungMin wants to merge 2 commits into
eslint:mainfrom
KumJungMin:fix/require-alt-text-quoted
Open

fix: support unquoted aria-hidden value in require-alt-text#719
KumJungMin wants to merge 2 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. :)


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

@DMartens DMartens moved this from Needs Triage to Implementing in Triage Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Implementing

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