Skip to content
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
58 changes: 15 additions & 43 deletions src/rules/require-alt-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
// Imports
//-----------------------------------------------------------------------------

import { findOffsets, stripHtmlComments } from "../util.js";
import { stripHtmlComments } from "../util.js";

//-----------------------------------------------------------------------------
// Type Definitions
//-----------------------------------------------------------------------------

/**
* @import { Image, ImageReference } from "mdast";
* @import { MarkdownRuleDefinition } from "../types.js";
* @typedef {"altTextRequired"} RequireAltTextMessageIds
* @typedef {[]} RequireAltTextOptions
Expand Down Expand Up @@ -56,17 +57,12 @@ export default {
},

create(context) {
return {
image(node) {
if (node.alt.trim().length === 0) {
context.report({
loc: node.position,
messageId: "altTextRequired",
});
}
},
const { sourceCode } = context;

imageReference(node) {
return {
"image, imageReference"(
/** @type {Image | ImageReference} */ node,
) {
if (node.alt.trim().length === 0) {
context.report({
loc: node.position,
Expand All @@ -78,14 +74,17 @@ export default {
html(node) {
const text = stripHtmlComments(node.value);

/** @type {RegExpExecArray} */
let match;

while ((match = imgTagPattern.exec(text)) !== null) {
const imgTag = match[0];
const ariaHiddenMatch = imgTag.match(
getHtmlAttributeRe("aria-hidden"),
);
if (
ariaHiddenMatch &&
ariaHiddenMatch[1] &&
Copy link
Member Author

@lumirlumir lumirlumir Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place where the bug fix is needed (it's a one-line change).

Other changes are related to code refactoring.

I could use optional chaining, but I used this check to match the implementation on line 97.

image

ariaHiddenMatch[1].toLowerCase() === "true"
) {
continue;
Expand All @@ -98,41 +97,14 @@ export default {
altMatch[1].trim().length === 0 &&
altMatch[1].length > 0)
) {
const {
lineOffset: startLineOffset,
columnOffset: startColumnOffset,
} = findOffsets(node.value, match.index);

const {
lineOffset: endLineOffset,
columnOffset: endColumnOffset,
} = findOffsets(
node.value,
match.index + imgTag.length,
);

const nodeStartLine = node.position.start.line;
const nodeStartColumn = node.position.start.column;
const startLine = nodeStartLine + startLineOffset;
const endLine = nodeStartLine + endLineOffset;
const startColumn =
(startLine === nodeStartLine
? nodeStartColumn
: 1) + startColumnOffset;
const endColumn =
(endLine === nodeStartLine ? nodeStartColumn : 1) +
endColumnOffset;
const startOffset = // Adjust `imgTagPattern` match indices to the full source code.
match.index + node.position.start.offset;
const endOffset = startOffset + imgTag.length;

context.report({
loc: {
start: {
line: startLine,
column: startColumn,
},
end: {
line: endLine,
column: endColumn,
},
start: sourceCode.getLocFromIndex(startOffset),
end: sourceCode.getLocFromIndex(endOffset),
},
messageId: "altTextRequired",
});
Expand Down
1 change: 1 addition & 0 deletions tests/rules/require-alt-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ruleTester.run("require-alt-text", rule, {
'<img src="image.png" alt="" />',
"<img src=\"image.png\" alt='' />",
'<IMG SRC="image.png" ALT="Descriptive text"/>',
'<img src="image.png" aria-hidden alt="alt">',
'<img src="image.png" aria-hidden="true"/>',
'<img src="image.png" ARIA-HIDDEN="TRUE" />',
'<p><img src="image.png" alt="Descriptive text" /></p>',
Expand Down
Loading