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

Chore: cleanup redundant alt attribute check #101

Merged
merged 3 commits into from
Feb 13, 2024
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
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>",
smockle marked this conversation as resolved.
Show resolved Hide resolved
];

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