-
Notifications
You must be signed in to change notification settings - Fork 5
Introduce a no-empty-alt-text
rule
#85
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0533b65
Make sure that HTML that is inlined is supported
khiga8 1a5e09e
Add detail
khiga8 0f707f9
Update the helpers to support multiple errors in one line
khiga8 8cf33cc
add test support
khiga8 488fbb4
Add test cases
khiga8 111d3ac
Update configg
khiga8 012612f
Update export
khiga8 09d02ff
Add new rule
khiga8 ec36605
add test cases and update docs
khiga8 88e1852
Update test matchers
khiga8 e725906
Update src/rules/no-empty-string-alt.js
khiga8 5b17abf
Update src/rules/no-empty-string-alt.js
khiga8 ff25a34
Revert "Update src/rules/no-empty-string-alt.js"
khiga8 9d75081
Fix Regex syntax
khiga8 00fc5dd
Remove markdown syntax support
khiga8 0ba6e3c
Update doc to remove markdown syntax
khiga8 8f80d4d
Add test case for multiple images in one line
khiga8 f2d9774
Rename rule to no-empty-alt-text
khiga8 6bf95bb
Add missing bracket
khiga8 f11f800
Update README with the rule
khiga8 c60b8a7
Update docs/rules/GH003-no-empty-alt-text.md
khiga8 0927174
Update src/rules/no-empty-alt-text.js
khiga8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# GH003 No Empty Alt Text | ||
|
||
## Rule details | ||
|
||
⚠️ This rule is _off_ by default and is only applicable for GitHub rendered markdown. | ||
|
||
Currently, all images on github.com are automatically wrapped in an anchor tag. | ||
|
||
As a result, images that are intentionally marked as decorative (via `alt=""``) end up rendering as a link without an accessible name. This is confusing and inaccessible for assistive technology users. | ||
|
||
This rule can be enabled to enforce that the alt attribute is always set to descriptive text. | ||
|
||
This rule should be removed once this behavior is updated on GitHub's UI. | ||
|
||
## Examples | ||
|
||
### Incorrect 👎 | ||
|
||
```html | ||
<img src="cat.png" alt=""> | ||
``` | ||
|
||
### Correct 👍 | ||
|
||
```html | ||
<img src="mona.png" alt="Mona Lisa, the Octocat"> | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
module.exports = { | ||
rules: [require("./no-default-alt-text"), require("./no-generic-link-text")], | ||
rules: [ | ||
require("./no-default-alt-text"), | ||
require("./no-generic-link-text"), | ||
require("./no-empty-alt-text"), | ||
], | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module.exports = { | ||
names: ["GH003", "no-empty-alt-text"], | ||
description: "Please provide an alternative text for the image.", | ||
information: new URL( | ||
"https://github.com/github/markdownlint-github/blob/main/docs/rules/GH003-no-empty-alt-text.md", | ||
), | ||
tags: ["accessibility", "images"], | ||
function: function GH003(params, onError) { | ||
const htmlTagsWithImages = params.parsers.markdownit.tokens.filter( | ||
(token) => { | ||
return ( | ||
(token.type === "html_block" && token.content.includes("<img")) || | ||
(token.type === "inline" && | ||
token.content.includes("<img") && | ||
token.children.some((child) => child.type === "html_inline")) | ||
); | ||
}, | ||
); | ||
|
||
const htmlAltRegex = new RegExp(/alt=['"]['"]/, "gid"); | ||
|
||
for (const token of htmlTagsWithImages) { | ||
const lineRange = token.map; | ||
const lineNumber = token.lineNumber; | ||
const lines = params.lines.slice(lineRange[0], lineRange[1]); | ||
|
||
for (let i = 0; i < lines.length; i++) { | ||
const line = lines[i]; | ||
khiga8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const matches = line.matchAll(htmlAltRegex); | ||
for (const match of matches) { | ||
const matchingContent = match[0]; | ||
const startIndex = match.indices[0][0]; | ||
onError({ | ||
lineNumber: lineNumber + i, | ||
range: [startIndex + 1, matchingContent.length], | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# Example Violations | ||
|
||
 | ||
|
||
<img alt="image"><img alt="Image"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const noEmptyStringAltRule = require("../src/rules/no-empty-alt-text"); | ||
const runTest = require("./utils/run-test").runTest; | ||
|
||
describe("GH003: No Empty Alt Text", () => { | ||
describe("successes", () => { | ||
test("html image", async () => { | ||
const strings = [ | ||
'<img alt="A helpful description" src="https://user-images.githubusercontent.com/abcdef.png">', | ||
"`<img alt='' src='image.png'>`", // code block | ||
]; | ||
|
||
const results = await runTest(strings, noEmptyStringAltRule); | ||
expect(results).toHaveLength(0); | ||
}); | ||
}); | ||
describe("failures", () => { | ||
test("HTML example", async () => { | ||
const strings = [ | ||
'<img alt="" src="https://user-images.githubusercontent.com/abcdef.png">', | ||
"<img alt='' src='https://user-images.githubusercontent.com/abcdef.png'>", | ||
'<img src="cat.png" alt="" /> <img src="dog.png" alt="" />', | ||
]; | ||
|
||
const results = await runTest(strings, noEmptyStringAltRule); | ||
|
||
const failedRules = results | ||
.map((result) => result.ruleNames) | ||
.flat() | ||
.filter((name) => !name.includes("GH")); | ||
|
||
expect(failedRules).toHaveLength(4); | ||
for (const rule of failedRules) { | ||
expect(rule).toBe("no-empty-alt-text"); | ||
} | ||
}); | ||
|
||
test("error message", async () => { | ||
const strings = [ | ||
'<img alt="" src="https://user-images.githubusercontent.com/abcdef.png">', | ||
'<img src="cat.png" alt="" /> <img src="dog.png" alt="" />', | ||
]; | ||
|
||
const results = await runTest(strings, noEmptyStringAltRule); | ||
|
||
expect(results[0].ruleDescription).toMatch( | ||
"Please provide an alternative text for the image.", | ||
); | ||
expect(results[0].errorRange).toEqual([6, 6]); | ||
|
||
expect(results[1].ruleDescription).toMatch( | ||
"Please provide an alternative text for the image.", | ||
); | ||
expect(results[1].errorRange).toEqual([20, 6]); | ||
expect(results[2].ruleDescription).toMatch( | ||
"Please provide an alternative text for the image.", | ||
); | ||
expect(results[2].errorRange).toEqual([49, 6]); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.