bump package version to 1.6.1
[squeep-html-template-helper] / test / lint-html.js
1 'use strict';
2
3 /**
4 * A brief assertion wrapper around html-validate.
5 */
6
7 const assert = require('node:assert');
8
9 /**
10 * Given an instance of html-validate, returns a function which asserts validity of some HTML.
11 * @param {ConsoleLike} logger
12 * @param {HtmlValidate} htmlValidate
13 * @returns {(html: String) => Promise<void>}
14 */
15 function makeHtmlLint(logger, htmlValidate) {
16 function note(violations, message) {
17 violations.push(message.ruleId);
18 logger.debug('HtmlLint', message);
19 }
20 return async function HtmlLint(html) {
21 const violations = [];
22 const report = await htmlValidate.validateString(html);
23 report.results.forEach((r) => {
24 if (Array.isArray(r)) {
25 r.forEach((m) => {
26 note(violations, m);
27 });
28 } else {
29 note(violations, r);
30 }
31 });
32 assert(report.valid, 'HTML violations: ' + violations.join(', '));
33 };
34 }
35
36 module.exports = {
37 makeHtmlLint,
38 };