Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a no-empty-alt-text rule #85

Merged
merged 22 commits into from
Oct 9, 2023
Merged

Conversation

khiga8
Copy link
Contributor

@khiga8 khiga8 commented Oct 6, 2023

This PR introduces a new rule, no-empty-alt-text which nudges people to add alt text when they set an image to decorative. Setting alt="" to mark decorative images is usually acceptable, but currently leads to unexpected behaviors, specifically on GitHub.

There is currently a known issue on GitHub where all images are rendered inside of an anchor tag. As a result of this, decorative images result in links without an accessible name. Therefore, it's a good idea to always set alt text on markdown images on GitHub. However, this rule isn't really a long term solution and this issue is best fixed in GitHub in the long-term.

⚠️ This rule is SPECIFIC to GitHub's UI markdown behavior and is OFF by default since it's building on top of unideal behavior.

This rule should be removed in the future when we stops wrapping images in anchor tags.

We plan to enable this in github/accessibility-alt-text-bot#33.

@khiga8 khiga8 marked this pull request as ready for review October 6, 2023 14:10
@khiga8 khiga8 requested a review from a team as a code owner October 6, 2023 14:10
### Incorrect 👎

```md
![""](cat.png)
Copy link
Member

@iansan5653 iansan5653 Oct 6, 2023

Choose a reason for hiding this comment

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

I'm not sure about this example. The alt text in the Markdown form is never quoted like an HTML property, so this would result in an image tag like <img alt='""' src="cat.png" />. While an alt text of "" is not useful, it's not empty either. The only way to create a truly empty alt tag in Markdown is with the HTML form alt="", so maybe we should only focus on HTML for this rule.

Copy link
Member

Choose a reason for hiding this comment

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

I think a useful-alt-text rule might be an interesting avenue to explore, where we ban things like "screenshot", "image", "example", '""', etc. But I think that's a separate rule from empty text.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah! You're right -- thank you for catching that! I'll stick with HTML for this rule!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

useful-alt-text

The no-default-alt-text currently flags alternative text like, image, Image, Screen Shot 2022-06-26 at 7 41 30 PM though it's not comprehensive by any means. We could potentially expand on that?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think we definitely could expand on that! It might be interesting to see if we can run a query to figure out what the most common alt texts are across GitHub -- I bet the top 20 or so are all things we'd probably want to lint against.

Comment on lines 19 to 23
const inlineImages = params.parsers.markdownit.tokens.filter(
(token) =>
token.type === "inline" &&
token.children.some((child) => child.type === "image"),
);
Copy link
Member

Choose a reason for hiding this comment

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

Per other comment - we probably should limit this rule to just HTML tags

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed markdown syntax in 00fc5dd!

src/rules/no-empty-string-alt.js Outdated Show resolved Hide resolved
src/rules/no-empty-string-alt.js Outdated Show resolved Hide resolved
expect(results).toHaveLength(0);
});
});
describe("failures", () => {
Copy link
Member

Choose a reason for hiding this comment

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

Not super important, but it might also be useful to have a test for two failures in one line, like:

<img src="cat.png" alt="" /> <img src="dog.png" alt="" />

I think we do handle this correctly with the g flag and iterating over matches per line, but it probably would still be good to test it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea! Done in 8f80d4d.

rules: [
require("./no-default-alt-text"),
require("./no-generic-link-text"),
require("./no-empty-string-alt"),
Copy link
Member

Choose a reason for hiding this comment

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

🤔 Maybe we should call this one no-empty-alt or no-empty-alt-text?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like no-empty-alt-text!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed in f2d9774.

@khiga8 khiga8 marked this pull request as draft October 6, 2023 16:17
@khiga8 khiga8 changed the title Introduce a no-empty-string-alt rule Introduce a no-empty-alt-text rule Oct 6, 2023
@khiga8 khiga8 marked this pull request as ready for review October 6, 2023 21:08
Copy link
Contributor

@kendallgassner kendallgassner left a comment

Choose a reason for hiding this comment

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

Should we export a preset for GitHub markdown?

Base automatically changed from kh-add-more-coverge to main October 9, 2023 14:55
docs/rules/GH003-no-empty-alt-text.md Outdated Show resolved Hide resolved
src/rules/no-empty-alt-text.js Outdated Show resolved Hide resolved
khiga8 and others added 2 commits October 9, 2023 11:02
Co-authored-by: Ian Sanders <iansan5653@github.com>
Co-authored-by: Ian Sanders <iansan5653@github.com>
@khiga8
Copy link
Contributor Author

khiga8 commented Oct 9, 2023

Should we export a preset for GitHub markdown?

discussing over Slack!

@khiga8 khiga8 merged commit ccc7d23 into main Oct 9, 2023
1 check passed
@khiga8 khiga8 deleted the kh-enforce-alt-is-always-set branch October 9, 2023 15:04
@janbrasna janbrasna mentioned this pull request Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants