Skip to content

Update no-empty-alt-text to check for no alt #88

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 4 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/rules/no-empty-alt-text.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: Clean up when https://github.com/DavidAnson/markdownlint/pull/993 is merged
module.exports = {
names: ["GH003", "no-empty-alt-text"],
description: "Please provide an alternative text for the image.",
Expand All @@ -17,22 +18,38 @@ module.exports = {
},
);

const htmlAltRegex = new RegExp(/alt=['"]['"]/, "gid");

const ImageRegex = new RegExp(/<img(.*?)>/, "gid");
const htmlAltRegex = new RegExp(/alt=['"]/, "gid");
const htmlEmptyAltRegex = 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 (const [i, line] of lines.entries()) {
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],
});
const imageTags = line.matchAll(ImageRegex);

for (const imageTag of imageTags) {
const imageTagIndex = imageTag.indices[0][0];

const emptyAltMatches = [
...imageTag[0].matchAll(htmlEmptyAltRegex),
][0];
const noAltMatches = [...imageTag[0].matchAll(htmlAltRegex)];

if (emptyAltMatches) {
const matchingContent = emptyAltMatches[0];
const startIndex = emptyAltMatches.indices[0][0];
onError({
lineNumber: lineNumber + i,
range: [imageTagIndex + startIndex + 1, matchingContent.length],
});
} else if (noAltMatches.length === 0) {
onError({
lineNumber: lineNumber + i,
range: [imageTagIndex + 1, imageTag[0].length],
});
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/no-empty-alt-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ describe("GH003: No Empty Alt Text", () => {
'<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="" />',
'<img src="dog.png" />',
'<img alt src="dog.png" />',
"<img alt><img alt>",
];

const results = await runTest(strings, noEmptyStringAltRule);
Expand All @@ -28,7 +31,7 @@ describe("GH003: No Empty Alt Text", () => {
.flat()
.filter((name) => !name.includes("GH"));

expect(failedRules).toHaveLength(4);
expect(failedRules).toHaveLength(8);
for (const rule of failedRules) {
expect(rule).toBe("no-empty-alt-text");
}
Expand Down