Add getHtmlAttributeRe helper for creating a RegExp to match an HTML attribute name/value.

This commit is contained in:
David Anson 2023-10-18 23:45:39 -07:00
parent 531e58ed9a
commit 06466905a5
4 changed files with 36 additions and 15 deletions

View file

@ -168,6 +168,16 @@ function cloneIfUrl(url) {
} }
module.exports.cloneIfUrl = cloneIfUrl; module.exports.cloneIfUrl = cloneIfUrl;
/**
* Gets a Regular Expression for matching the specified HTML attribute.
*
* @param {string} name HTML attribute name.
* @returns {RegExp} Regular Expression for matching.
*/
module.exports.getHtmlAttributeRe = function getHtmlAttributeRe(name) {
return new RegExp("\\s".concat(name, "\\s*=\\s*['\"]?([^'\"\\s>]*)"), "iu");
};
/** /**
* Returns true iff the input line is blank (contains nothing, whitespace, or * Returns true iff the input line is blank (contains nothing, whitespace, or
* comments (unclosed start/end comments allowed)). * comments (unclosed start/end comments allowed)).
@ -5992,13 +6002,12 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
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; } 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"), var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
addError = _require.addError, addError = _require.addError,
getHtmlAttributeRe = _require.getHtmlAttributeRe,
nextLinesRe = _require.nextLinesRe; nextLinesRe = _require.nextLinesRe;
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"), var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
filterByTypes = _require2.filterByTypes, filterByTypes = _require2.filterByTypes,
getHtmlTagInfo = _require2.getHtmlTagInfo; getHtmlTagInfo = _require2.getHtmlTagInfo;
var altRe = getHtmlAttributeRe("alt");
// Regular expression for identifying alt attribute
var altRe = /\salt=/i;
module.exports = { module.exports = {
"names": ["MD045", "no-alt-text"], "names": ["MD045", "no-alt-text"],
"description": "Images should have alternate text (alt text)", "description": "Images should have alternate text (alt text)",
@ -6038,7 +6047,7 @@ module.exports = {
startLine = htmlText.startLine, startLine = htmlText.startLine,
text = htmlText.text; text = htmlText.text;
var htmlTagInfo = getHtmlTagInfo(htmlText); var htmlTagInfo = getHtmlTagInfo(htmlText);
if (htmlTagInfo && htmlTagInfo.name.toLowerCase() === "img" && !altRe.test(text)) { if (htmlTagInfo && !htmlTagInfo.close && htmlTagInfo.name.toLowerCase() === "img" && !altRe.test(text)) {
var _range = [startColumn, text.replace(nextLinesRe, "").length]; var _range = [startColumn, text.replace(nextLinesRe, "").length];
addError(onError, startLine, undefined, undefined, _range); addError(onError, startLine, undefined, undefined, _range);
} }
@ -6292,15 +6301,16 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
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; } 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"), var _require = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"),
addError = _require.addError, addError = _require.addError,
addErrorDetailIf = _require.addErrorDetailIf; addErrorDetailIf = _require.addErrorDetailIf,
getHtmlAttributeRe = _require.getHtmlAttributeRe;
var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"), var _require2 = __webpack_require__(/*! ../helpers/micromark.cjs */ "../helpers/micromark.cjs"),
filterByPredicate = _require2.filterByPredicate, filterByPredicate = _require2.filterByPredicate,
filterByTypes = _require2.filterByTypes, filterByTypes = _require2.filterByTypes,
getHtmlTagInfo = _require2.getHtmlTagInfo; getHtmlTagInfo = _require2.getHtmlTagInfo;
// Regular expression for identifying HTML anchor names // Regular expression for identifying HTML anchor names
var idRe = /[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]id[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*=[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*["']?((?:(?![\t-\r "'>\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uD800-\uDFFF\uFEFF])[\s\S]|[\uD800-\uDBFF][\uDC00-\uDFFF])+)/i; var idRe = getHtmlAttributeRe("id");
var nameRe = /[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]name[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*=[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*["']?((?:(?![\t-\r "'>\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uD800-\uDFFF\uFEFF])[\s\S]|[\uD800-\uDBFF][\uDC00-\uDFFF])+)/i; var nameRe = getHtmlAttributeRe("name");
var anchorRe = /\{(#[0-9a-z]+(?:[\x2D_][0-9a-z]+)*)\}/g; var anchorRe = /\{(#[0-9a-z]+(?:[\x2D_][0-9a-z]+)*)\}/g;
// Sets for filtering heading tokens during conversion // Sets for filtering heading tokens during conversion
@ -6393,7 +6403,7 @@ module.exports = {
var htmlTagInfo = getHtmlTagInfo(token); var htmlTagInfo = getHtmlTagInfo(token);
if (htmlTagInfo && !htmlTagInfo.close) { if (htmlTagInfo && !htmlTagInfo.close) {
var anchorMatch = idRe.exec(token.text) || htmlTagInfo.name.toLowerCase() === "a" && nameRe.exec(token.text); var anchorMatch = idRe.exec(token.text) || htmlTagInfo.name.toLowerCase() === "a" && nameRe.exec(token.text);
if (anchorMatch) { if (anchorMatch && anchorMatch.length > 0) {
fragments.set("#".concat(anchorMatch[1]), 0); fragments.set("#".concat(anchorMatch[1]), 0);
} }
} }

View file

@ -126,6 +126,16 @@ function cloneIfUrl(url) {
} }
module.exports.cloneIfUrl = cloneIfUrl; module.exports.cloneIfUrl = cloneIfUrl;
/**
* Gets a Regular Expression for matching the specified HTML attribute.
*
* @param {string} name HTML attribute name.
* @returns {RegExp} Regular Expression for matching.
*/
module.exports.getHtmlAttributeRe = function getHtmlAttributeRe(name) {
return new RegExp(`\\s${name}\\s*=\\s*['"]?([^'"\\s>]*)`, "iu");
};
/** /**
* Returns true iff the input line is blank (contains nothing, whitespace, or * Returns true iff the input line is blank (contains nothing, whitespace, or
* comments (unclosed start/end comments allowed)). * comments (unclosed start/end comments allowed)).

View file

@ -2,11 +2,10 @@
"use strict"; "use strict";
const { addError, nextLinesRe } = require("../helpers"); const { addError, getHtmlAttributeRe, nextLinesRe } = require("../helpers");
const { filterByTypes, getHtmlTagInfo } = require("../helpers/micromark.cjs"); const { filterByTypes, getHtmlTagInfo } = require("../helpers/micromark.cjs");
// Regular expression for identifying alt attribute const altRe = getHtmlAttributeRe("alt");
const altRe = /\salt=/i;
module.exports = { module.exports = {
"names": [ "MD045", "no-alt-text" ], "names": [ "MD045", "no-alt-text" ],
@ -40,6 +39,7 @@ module.exports = {
const htmlTagInfo = getHtmlTagInfo(htmlText); const htmlTagInfo = getHtmlTagInfo(htmlText);
if ( if (
htmlTagInfo && htmlTagInfo &&
!htmlTagInfo.close &&
(htmlTagInfo.name.toLowerCase() === "img") && (htmlTagInfo.name.toLowerCase() === "img") &&
!altRe.test(text) !altRe.test(text)
) { ) {

View file

@ -2,13 +2,14 @@
"use strict"; "use strict";
const { addError, addErrorDetailIf } = require("../helpers"); const { addError, addErrorDetailIf, getHtmlAttributeRe } =
require("../helpers");
const { filterByPredicate, filterByTypes, getHtmlTagInfo } = const { filterByPredicate, filterByTypes, getHtmlTagInfo } =
require("../helpers/micromark.cjs"); require("../helpers/micromark.cjs");
// Regular expression for identifying HTML anchor names // Regular expression for identifying HTML anchor names
const idRe = /\sid\s*=\s*['"]?([^'"\s>]+)/iu; const idRe = getHtmlAttributeRe("id");
const nameRe = /\sname\s*=\s*['"]?([^'"\s>]+)/iu; const nameRe = getHtmlAttributeRe("name");
const anchorRe = /\{(#[a-z\d]+(?:[-_][a-z\d]+)*)\}/gu; const anchorRe = /\{(#[a-z\d]+(?:[-_][a-z\d]+)*)\}/gu;
// Sets for filtering heading tokens during conversion // Sets for filtering heading tokens during conversion
@ -101,7 +102,7 @@ module.exports = {
if (htmlTagInfo && !htmlTagInfo.close) { if (htmlTagInfo && !htmlTagInfo.close) {
const anchorMatch = idRe.exec(token.text) || const anchorMatch = idRe.exec(token.text) ||
(htmlTagInfo.name.toLowerCase() === "a" && nameRe.exec(token.text)); (htmlTagInfo.name.toLowerCase() === "a" && nameRe.exec(token.text));
if (anchorMatch) { if (anchorMatch && anchorMatch.length > 0) {
fragments.set(`#${anchorMatch[1]}`, 0); fragments.set(`#${anchorMatch[1]}`, 0);
} }
} }