diff --git a/demo/markdownlint-browser.js b/demo/markdownlint-browser.js index 37ff289a..a5fcc78c 100644 --- a/demo/markdownlint-browser.js +++ b/demo/markdownlint-browser.js @@ -5834,19 +5834,37 @@ module.exports = { +function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), - addError = _require.addError, - forEachInlineChild = _require.forEachInlineChild; + addError = _require.addError; +var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"), + filterByTypes = _require2.filterByTypes; module.exports = { "names": ["MD045", "no-alt-text"], "description": "Images should have alternate text (alt text)", "tags": ["accessibility", "images"], "function": function MD045(params, onError) { - forEachInlineChild(params, "image", function forToken(token) { - if (token.content === "") { - addError(onError, token.lineNumber); + var images = filterByTypes(params.parsers.micromark.tokens, ["image"]); + var _iterator = _createForOfIteratorHelper(images), + _step; + try { + for (_iterator.s(); !(_step = _iterator.n()).done;) { + var image = _step.value; + var labelTexts = filterByTypes(image.children, ["labelText"]); + if (labelTexts.some(function (labelText) { + return labelText.text.length === 0; + })) { + var range = image.startLine === image.endLine ? [image.startColumn, image.endColumn - image.startColumn] : undefined; + addError(onError, image.startLine, undefined, undefined, range); + } } - }); + } catch (err) { + _iterator.e(err); + } finally { + _iterator.f(); + } } }; diff --git a/lib/md045.js b/lib/md045.js index 8d15aedd..891dd165 100644 --- a/lib/md045.js +++ b/lib/md045.js @@ -2,17 +2,29 @@ "use strict"; -const { addError, forEachInlineChild } = require("../helpers"); +const { addError } = require("../helpers"); +const { filterByTypes } = require("../helpers/micromark.cjs"); module.exports = { "names": [ "MD045", "no-alt-text" ], "description": "Images should have alternate text (alt text)", "tags": [ "accessibility", "images" ], "function": function MD045(params, onError) { - forEachInlineChild(params, "image", function forToken(token) { - if (token.content === "") { - addError(onError, token.lineNumber); + const images = filterByTypes(params.parsers.micromark.tokens, [ "image" ]); + for (const image of images) { + const labelTexts = filterByTypes(image.children, [ "labelText" ]); + if (labelTexts.some((labelText) => labelText.text.length === 0)) { + const range = (image.startLine === image.endLine) ? + [ image.startColumn, image.endColumn - image.startColumn ] : + undefined; + addError( + onError, + image.startLine, + undefined, + undefined, + range + ); } - }); + } } }; diff --git a/test/no-alt-text.md b/test/no-alt-text.md index 768e537c..b685ef8d 100644 --- a/test/no-alt-text.md +++ b/test/no-alt-text.md @@ -22,5 +22,11 @@ Link to image with alternate text [![Alternate text](image.jpg)](image.jpg) Link to image without alternate text [![](image.jpg)](image.jpg) {MD045} +Multi-line image with alternate text ![Alternate text](image.jpg "Title" +) + +Multi-line image without alternate text ![](image.jpg "Title" +) {MD045:28} + [notitle]: image.jpg [title]: image.jpg "Title" diff --git a/test/snapshots/markdownlint-test-micromark.mjs.snap b/test/snapshots/markdownlint-test-micromark.mjs.snap index cb3649af..7f03eeb0 100644 Binary files a/test/snapshots/markdownlint-test-micromark.mjs.snap and b/test/snapshots/markdownlint-test-micromark.mjs.snap differ diff --git a/test/snapshots/markdownlint-test-scenarios.js.md b/test/snapshots/markdownlint-test-scenarios.js.md index bdde5c41..9be6d9a9 100644 --- a/test/snapshots/markdownlint-test-scenarios.js.md +++ b/test/snapshots/markdownlint-test-scenarios.js.md @@ -1860,7 +1860,10 @@ Generated by [AVA](https://avajs.dev). { errorContext: null, errorDetail: null, - errorRange: null, + errorRange: [ + 33, + 14, + ], fixInfo: null, lineNumber: 19, ruleDescription: 'Images should have alternate text (alt text)', @@ -6741,7 +6744,10 @@ Generated by [AVA](https://avajs.dev). { errorContext: null, errorDetail: null, - errorRange: null, + errorRange: [ + 1, + 14, + ], fixInfo: null, lineNumber: 85, ruleDescription: 'Images should have alternate text (alt text)', @@ -33064,7 +33070,10 @@ Generated by [AVA](https://avajs.dev). { errorContext: null, errorDetail: null, - errorRange: null, + errorRange: [ + 1, + 14, + ], fixInfo: null, lineNumber: 5, ruleDescription: 'Images should have alternate text (alt text)', @@ -33077,7 +33086,10 @@ Generated by [AVA](https://avajs.dev). { errorContext: null, errorDetail: null, - errorRange: null, + errorRange: [ + 1, + 22, + ], fixInfo: null, lineNumber: 9, ruleDescription: 'Images should have alternate text (alt text)', @@ -33090,7 +33102,10 @@ Generated by [AVA](https://avajs.dev). { errorContext: null, errorDetail: null, - errorRange: null, + errorRange: [ + 30, + 14, + ], fixInfo: null, lineNumber: 11, ruleDescription: 'Images should have alternate text (alt text)', @@ -33103,7 +33118,10 @@ Generated by [AVA](https://avajs.dev). { errorContext: null, errorDetail: null, - errorRange: null, + errorRange: [ + 40, + 12, + ], fixInfo: null, lineNumber: 15, ruleDescription: 'Images should have alternate text (alt text)', @@ -33116,7 +33134,10 @@ Generated by [AVA](https://avajs.dev). { errorContext: null, errorDetail: null, - errorRange: null, + errorRange: [ + 50, + 10, + ], fixInfo: null, lineNumber: 19, ruleDescription: 'Images should have alternate text (alt text)', @@ -33129,7 +33150,10 @@ Generated by [AVA](https://avajs.dev). { errorContext: null, errorDetail: null, - errorRange: null, + errorRange: [ + 39, + 14, + ], fixInfo: null, lineNumber: 23, ruleDescription: 'Images should have alternate text (alt text)', @@ -33139,6 +33163,19 @@ Generated by [AVA](https://avajs.dev). 'no-alt-text', ], }, + { + errorContext: null, + errorDetail: null, + errorRange: null, + fixInfo: null, + lineNumber: 28, + ruleDescription: 'Images should have alternate text (alt text)', + ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md045.md', + ruleNames: [ + 'MD045', + 'no-alt-text', + ], + }, ], fixed: `# Images with and without alternate text␊ ␊ @@ -33164,6 +33201,12 @@ Generated by [AVA](https://avajs.dev). ␊ Link to image without alternate text [![](image.jpg)](image.jpg) {MD045}␊ ␊ + Multi-line image with alternate text ![Alternate text](image.jpg "Title"␊ + )␊ + ␊ + Multi-line image without alternate text ![](image.jpg "Title"␊ + ) {MD045:28}␊ + ␊ [notitle]: image.jpg␊ [title]: image.jpg "Title"␊ `, diff --git a/test/snapshots/markdownlint-test-scenarios.js.snap b/test/snapshots/markdownlint-test-scenarios.js.snap index 47a09967..76ab131c 100644 Binary files a/test/snapshots/markdownlint-test-scenarios.js.snap and b/test/snapshots/markdownlint-test-scenarios.js.snap differ