Skip to content

Commit ec36605

Browse files
committed
add test cases and update docs
1 parent 09d02ff commit ec36605

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

docs/rules/GH003-no-empty-string-alt.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
## Rule details
44

5-
This rule is _off_ by default and is only applicable for GitHub rendered markdown.
5+
⚠️ This rule is _off_ by default and is only applicable for GitHub rendered markdown.
66

77
Currently, all images on github.com are automatically wrapped in an anchor tag.
88

9-
As a result, images that are intentionally marked as decorative (via `alt=""``) end up as a link without an accessible name. This is confusing for assistive technology users.
9+
As a result, images that are intentionally marked as decorative (via `alt=""``) end up rendering as a link without an accessible name. This is confusing and inaccessible for assistive technology users.
1010

1111
This rule can be enabled to enforce that the alt attribute is always set to descriptive text.
1212

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ module.exports = {
88
function: function GH003(params, onError) {
99
const htmlTagsWithImages = params.parsers.markdownit.tokens.filter(
1010
(token) => {
11-
return token.type === "html_block" && token.content.includes("<img");
11+
return (
12+
(token.type === "html_block" && token.content.includes("<img")) ||
13+
(token.type === "inline" &&
14+
token.content.includes("<img") &&
15+
token.children.some((child) => child.type === "html_inline"))
16+
);
1217
},
1318
);
1419
const inlineImages = params.parsers.markdownit.tokens.filter(
@@ -17,8 +22,8 @@ module.exports = {
1722
token.children.some((child) => child.type === "image"),
1823
);
1924

20-
const htmlAltRegex = new RegExp(/alt=""|alt=''/i);
21-
const markdownAltRegex = new RegExp(/!\[""\]|!\[''\]/i);
25+
const htmlAltRegex = new RegExp(/alt=""|alt=''/, "gid");
26+
const markdownAltRegex = new RegExp(/!\[""\]|!\[''\]/, "gid");
2227

2328
for (const token of [...htmlTagsWithImages, ...inlineImages]) {
2429
const lineRange = token.map;
@@ -27,15 +32,18 @@ module.exports = {
2732

2833
for (let i = 0; i < lines.length; i++) {
2934
const line = lines[i];
30-
let fails;
35+
let matches;
3136
if (token.type === "inline") {
32-
fails = markdownAltRegex.test(line);
37+
matches = line.matchAll(markdownAltRegex);
3338
} else {
34-
fails = htmlAltRegex.test(line);
39+
matches = line.matchAll(htmlAltRegex);
3540
}
36-
if (fails) {
41+
for (const match of matches) {
42+
const matchingContent = match[0];
43+
const startIndex = match.indices[0][0];
3744
onError({
3845
lineNumber: lineNumber + i,
46+
range: [startIndex + 1, matchingContent.length],
3947
});
4048
}
4149
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ describe("GH003: No Empty String Alt", () => {
66
test("inline", async () => {
77
const strings = [
88
"![Chart with a single root node reading 'Example'](https://user-images.githubusercontent.com/abcdef.png)",
9+
"`![''](image.png)`", // code block
10+
'`![""](image.png)`', // code block
911
];
1012

1113
const results = await runTest(strings, noEmptyStringAltRule);
@@ -17,6 +19,7 @@ describe("GH003: No Empty String Alt", () => {
1719
test("html image", async () => {
1820
const strings = [
1921
'<img alt="A helpful description" src="https://user-images.githubusercontent.com/abcdef.png">',
22+
"`<img alt='' src='image.png'`", // code block
2023
];
2124

2225
const results = await runTest(strings, noEmptyStringAltRule);
@@ -76,9 +79,12 @@ describe("GH003: No Empty String Alt", () => {
7679
expect(results[0].ruleDescription).toMatch(
7780
"Please provide an alternative text for the image.",
7881
);
82+
expect(results[0].errorRange).toEqual([1, 5]);
83+
7984
expect(results[1].ruleDescription).toMatch(
8085
"Please provide an alternative text for the image.",
8186
);
87+
expect(results[1].errorRange).toEqual([6, 6]);
8288
});
8389
});
8490
});

0 commit comments

Comments
 (0)