X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Flint-html.js;h=1f2b437c302effc58cc4de8a80d8254822bbe0d3;hb=3501e7042eeb4a104e4f001b2641d41023087b1a;hp=26cfed2aed97b19f2c89a2b0fb6e6f69cda119e6;hpb=12a56b9b35eeb6181ce1d4b4fa2a0536984374d8;p=squeep-html-template-helper diff --git a/test/lint-html.js b/test/lint-html.js index 26cfed2..1f2b437 100644 --- a/test/lint-html.js +++ b/test/lint-html.js @@ -1,28 +1,38 @@ 'use strict'; /** - * A brief wrapper around html-validate + * A brief assertion wrapper around html-validate. */ const assert = require('node:assert'); -const { HtmlValidate } = require('html-validate'); // eslint-disable-line node/no-unpublished-require -const stubLogger = require('./stub-logger'); -class LintHtml { - constructor(logger, ...args) { - this.logger = logger; - this.htmlValidate = new HtmlValidate(...args); - } - - async lint(html) { - const ruleViolations = []; - const report = await this.htmlValidate.validateString(html); - report.results.forEach((m) => { - ruleViolations.push(m.ruleId); - stubLogger?.debug('LintHtml', 'message', m); - }) - assert(report.valid, '' + ruleViolations.join(', ')); +/** + * Given an instance of html-validate, returns a function which asserts validity of some HTML. + * @param {ConsoleLike} logger + * @param {HtmlValidate} htmlValidate + * @returns {(html: String) => Promise} + */ +function makeHtmlLint(logger, htmlValidate) { + function note(violations, message) { + violations.push(message.ruleId); + logger.debug('HtmlLint', message); } + return async function HtmlLint(html) { + const violations = []; + const report = await htmlValidate.validateString(html); + report.results.forEach((r) => { + if (Array.isArray(r)) { + r.forEach((m) => { + note(violations, m); + }); + } else { + note(violations, r); + } + }); + assert(report.valid, 'HTML violations: ' + violations.join(', ')); + }; } -module.exports = LintHtml; \ No newline at end of file +module.exports = { + makeHtmlLint, +}; \ No newline at end of file