Skip to content

Commit

Permalink
Update no-empty-alt-text to check for no alt (#88)
Browse files Browse the repository at this point in the history
* Update no-empty-alt-text to check for no alt

* clean

* Update no-empty-alt-text.js

* Update no-empty-alt-text.js
  • Loading branch information
kendallgassner authored Oct 11, 2023
1 parent ccc7d23 commit 74fe5f9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
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

0 comments on commit 74fe5f9

Please sign in to comment.