From bb5cb77ffa93c84bca23b0dd87ff00bb7e7d81d0 Mon Sep 17 00:00:00 2001 From: Kendall Gassner Date: Tue, 10 Oct 2023 16:13:19 +0000 Subject: [PATCH 1/4] Update no-empty-alt-text to check for no alt --- src/rules/no-empty-alt-text.js | 15 ++++++++++++--- test/no-empty-alt-text.test.js | 4 +++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/rules/no-empty-alt-text.js b/src/rules/no-empty-alt-text.js index 02cab26..05a596d 100644 --- a/src/rules/no-empty-alt-text.js +++ b/src/rules/no-empty-alt-text.js @@ -17,15 +17,18 @@ module.exports = { }, ); - const htmlAltRegex = new RegExp(/alt=['"]['"]/, "gid"); - + const htmlAltRegex = new RegExp(/alt=['"]/, "gid"); + const htmlEmptyAltRegex = new RegExp(/alt=['"]['"]/, "gid"); for (const token of htmlTagsWithImages) { const lineRange = token.map; const lineNumber = token.lineNumber; const lines = params.lines.slice(lineRange[0], lineRange[1]); for (const [i, line] of lines.entries()) { - const matches = line.matchAll(htmlAltRegex); + const noAlt = [...line.matchAll(htmlAltRegex)].length === 0; + + const matches = line.matchAll(htmlEmptyAltRegex); + for (const match of matches) { const matchingContent = match[0]; const startIndex = match.indices[0][0]; @@ -34,6 +37,12 @@ module.exports = { range: [startIndex + 1, matchingContent.length], }); } + + if (noAlt) { + onError({ + lineNumber: lineNumber + i, + }); + } } } }, diff --git a/test/no-empty-alt-text.test.js b/test/no-empty-alt-text.test.js index 0b6fd43..53512d9 100644 --- a/test/no-empty-alt-text.test.js +++ b/test/no-empty-alt-text.test.js @@ -19,6 +19,8 @@ describe("GH003: No Empty Alt Text", () => { '', "", ' ', + '', + '', ]; const results = await runTest(strings, noEmptyStringAltRule); @@ -28,7 +30,7 @@ describe("GH003: No Empty Alt Text", () => { .flat() .filter((name) => !name.includes("GH")); - expect(failedRules).toHaveLength(4); + expect(failedRules).toHaveLength(6); for (const rule of failedRules) { expect(rule).toBe("no-empty-alt-text"); } From c960168fbb949961778d7a75a914cee882806c13 Mon Sep 17 00:00:00 2001 From: Kendall Gassner Date: Tue, 10 Oct 2023 21:59:41 +0000 Subject: [PATCH 2/4] clean --- src/rules/no-empty-alt-text.js | 35 ++++++++++++++++++++-------------- test/no-empty-alt-text.test.js | 3 ++- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/rules/no-empty-alt-text.js b/src/rules/no-empty-alt-text.js index 05a596d..8005c43 100644 --- a/src/rules/no-empty-alt-text.js +++ b/src/rules/no-empty-alt-text.js @@ -17,6 +17,7 @@ module.exports = { }, ); + const ImageRegex = new RegExp(//, "gid"); const htmlAltRegex = new RegExp(/alt=['"]/, "gid"); const htmlEmptyAltRegex = new RegExp(/alt=['"]['"]/, "gid"); for (const token of htmlTagsWithImages) { @@ -25,23 +26,29 @@ module.exports = { const lines = params.lines.slice(lineRange[0], lineRange[1]); for (const [i, line] of lines.entries()) { - const noAlt = [...line.matchAll(htmlAltRegex)].length === 0; + const imageTags = line.matchAll(ImageRegex); - const matches = line.matchAll(htmlEmptyAltRegex); + for (const imageTag of imageTags) { + const imageTagIndex = imageTag.indices[0][0]; - for (const match of matches) { - const matchingContent = match[0]; - const startIndex = match.indices[0][0]; - onError({ - lineNumber: lineNumber + i, - range: [startIndex + 1, matchingContent.length], - }); - } + const emptyAltMatches = [ + ...imageTag[0].matchAll(htmlEmptyAltRegex), + ][0]; + const noAltMatches = [...imageTag[0].matchAll(htmlAltRegex)]; - if (noAlt) { - onError({ - lineNumber: lineNumber + i, - }); + 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], + }); + } } } } diff --git a/test/no-empty-alt-text.test.js b/test/no-empty-alt-text.test.js index 53512d9..1479bf7 100644 --- a/test/no-empty-alt-text.test.js +++ b/test/no-empty-alt-text.test.js @@ -21,6 +21,7 @@ describe("GH003: No Empty Alt Text", () => { ' ', '', '', + "", ]; const results = await runTest(strings, noEmptyStringAltRule); @@ -30,7 +31,7 @@ describe("GH003: No Empty Alt Text", () => { .flat() .filter((name) => !name.includes("GH")); - expect(failedRules).toHaveLength(6); + expect(failedRules).toHaveLength(8); for (const rule of failedRules) { expect(rule).toBe("no-empty-alt-text"); } From 3da79d07f31973252f58b1d00551f6b28ced3992 Mon Sep 17 00:00:00 2001 From: Kendall Gassner Date: Tue, 10 Oct 2023 17:16:44 -0700 Subject: [PATCH 3/4] Update no-empty-alt-text.js --- src/rules/no-empty-alt-text.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rules/no-empty-alt-text.js b/src/rules/no-empty-alt-text.js index 8005c43..065bb51 100644 --- a/src/rules/no-empty-alt-text.js +++ b/src/rules/no-empty-alt-text.js @@ -1,3 +1,4 @@ +// 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.", From 8b7889aa01809a26c00b5712c0e58271491abb41 Mon Sep 17 00:00:00 2001 From: Kendall Gassner Date: Wed, 11 Oct 2023 06:56:02 -0700 Subject: [PATCH 4/4] Update no-empty-alt-text.js --- src/rules/no-empty-alt-text.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/no-empty-alt-text.js b/src/rules/no-empty-alt-text.js index 065bb51..0ade74b 100644 --- a/src/rules/no-empty-alt-text.js +++ b/src/rules/no-empty-alt-text.js @@ -1,4 +1,4 @@ -// TODO: Clean up when https://github.com/DavidAnson/markdownlint/pull/993 is merged +// 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.",