Skip to content

Commit 74fe5f9

Browse files
Update no-empty-alt-text to check for no alt (#88)
* Update no-empty-alt-text to check for no alt * clean * Update no-empty-alt-text.js * Update no-empty-alt-text.js
1 parent ccc7d23 commit 74fe5f9

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/rules/no-empty-alt-text.js

+27-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TODO: Clean up when https://github.com/DavidAnson/markdownlint/pull/993 is merged
12
module.exports = {
23
names: ["GH003", "no-empty-alt-text"],
34
description: "Please provide an alternative text for the image.",
@@ -17,22 +18,38 @@ module.exports = {
1718
},
1819
);
1920

20-
const htmlAltRegex = new RegExp(/alt=['"]['"]/, "gid");
21-
21+
const ImageRegex = new RegExp(/<img(.*?)>/, "gid");
22+
const htmlAltRegex = new RegExp(/alt=['"]/, "gid");
23+
const htmlEmptyAltRegex = new RegExp(/alt=['"]['"]/, "gid");
2224
for (const token of htmlTagsWithImages) {
2325
const lineRange = token.map;
2426
const lineNumber = token.lineNumber;
2527
const lines = params.lines.slice(lineRange[0], lineRange[1]);
2628

2729
for (const [i, line] of lines.entries()) {
28-
const matches = line.matchAll(htmlAltRegex);
29-
for (const match of matches) {
30-
const matchingContent = match[0];
31-
const startIndex = match.indices[0][0];
32-
onError({
33-
lineNumber: lineNumber + i,
34-
range: [startIndex + 1, matchingContent.length],
35-
});
30+
const imageTags = line.matchAll(ImageRegex);
31+
32+
for (const imageTag of imageTags) {
33+
const imageTagIndex = imageTag.indices[0][0];
34+
35+
const emptyAltMatches = [
36+
...imageTag[0].matchAll(htmlEmptyAltRegex),
37+
][0];
38+
const noAltMatches = [...imageTag[0].matchAll(htmlAltRegex)];
39+
40+
if (emptyAltMatches) {
41+
const matchingContent = emptyAltMatches[0];
42+
const startIndex = emptyAltMatches.indices[0][0];
43+
onError({
44+
lineNumber: lineNumber + i,
45+
range: [imageTagIndex + startIndex + 1, matchingContent.length],
46+
});
47+
} else if (noAltMatches.length === 0) {
48+
onError({
49+
lineNumber: lineNumber + i,
50+
range: [imageTagIndex + 1, imageTag[0].length],
51+
});
52+
}
3653
}
3754
}
3855
}

test/no-empty-alt-text.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ describe("GH003: No Empty Alt Text", () => {
1919
'<img alt="" src="https://user-images.githubusercontent.com/abcdef.png">',
2020
"<img alt='' src='https://user-images.githubusercontent.com/abcdef.png'>",
2121
'<img src="cat.png" alt="" /> <img src="dog.png" alt="" />',
22+
'<img src="dog.png" />',
23+
'<img alt src="dog.png" />',
24+
"<img alt><img alt>",
2225
];
2326

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

31-
expect(failedRules).toHaveLength(4);
34+
expect(failedRules).toHaveLength(8);
3235
for (const rule of failedRules) {
3336
expect(rule).toBe("no-empty-alt-text");
3437
}

0 commit comments

Comments
 (0)