Skip to content

Commit

Permalink
Chore: cleanup redundant alt attribute check (#101)
Browse files Browse the repository at this point in the history
* chore: Remove check for no alt attribute

* Remove code comment

* Update test
  • Loading branch information
khiga8 authored Feb 13, 2024
1 parent add9f62 commit 9ba0286
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 14 deletions.
9 changes: 0 additions & 9 deletions src/rules/no-empty-alt-text.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// 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 @@ -19,7 +18,6 @@ module.exports = {
);

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;
Expand All @@ -35,20 +33,13 @@ module.exports = {
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
6 changes: 1 addition & 5 deletions test/no-empty-alt-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@ 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);

const failedRules = results
.map((result) => result.ruleNames)
.flat()
.filter((name) => !name.includes("GH"));

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

0 comments on commit 9ba0286

Please sign in to comment.