GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update no-empty-alt-text to check for no alt (#88) · github/markdownlint-github@74fe5f9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/rules
    • no-empty-alt-text.js
  • test
    • no-empty-alt-text.test.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: src/rules/no-empty-alt-text.js
+27-10Lines changed: 27 additions & 10 deletions
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
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/no-empty-alt-text.test.js
+4-1Lines changed: 4 additions & 1 deletion
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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.