From 9fc15beb6ed18e19e07ba49893ac472b411b8fb0 Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Fri, 8 Mar 2024 11:42:28 -0800 Subject: [PATCH] rework html linting --- index.js | 4 ++-- test/lib/template-helper.js | 18 ++++++++------- test/lint-html.js | 46 ++++++++++++++++++++++--------------- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index 5a14ebf..2bbc356 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ 'use strict'; const TemplateHelper = require('./lib/template-helper'); -const LintHtml = require('./test/lint-html'); +const { makeHtmlLint } = require('./test/lint-html'); module.exports = { TemplateHelper, - LintHtml, + makeHtmlLint, }; diff --git a/test/lib/template-helper.js b/test/lib/template-helper.js index 968a85f..ec7ab5d 100644 --- a/test/lib/template-helper.js +++ b/test/lib/template-helper.js @@ -4,9 +4,11 @@ const assert = require('assert'); const th = require('../../lib/template-helper'); const stubLogger = require('../stub-logger'); -const LintHtml = require('../lint-html'); -const lintHtml = new LintHtml(stubLogger); +const { makeHtmlLint } = require('../lint-html'); +const { HtmlValidate } = require('html-validate'); +const htmlValidate = new HtmlValidate(); +const htmlLint = makeHtmlLint(stubLogger, htmlValidate); describe('Template Helper', function () { let ctx, options, pagePathLevel; @@ -266,12 +268,12 @@ describe('Template Helper', function () { }); it('covers', async function () { const result = th.htmlPage(pagePathLevel, ctx, options, main); - await lintHtml.lint(result); + await htmlLint(result); assert(result); }); it('covers defaults', async function () { const result = th.htmlPage(pagePathLevel, ctx, options, main); - await lintHtml.lint(result); + await htmlLint(result); assert(result); }); it('covers user', async function () { @@ -279,7 +281,7 @@ describe('Template Helper', function () { authenticatedProfile: 'https://user.example.com/', }; const result = th.htmlPage(pagePathLevel, ctx, options, main); - await lintHtml.lint(result); + await htmlLint(result); assert(result); }); it('covers user at root path', async function () { @@ -288,7 +290,7 @@ describe('Template Helper', function () { }; pagePathLevel = 0; const result = th.htmlPage(pagePathLevel, ctx, options, main); - await lintHtml.lint(result); + await htmlLint(result); assert(result); }); it('covers logout redirect', async function () { @@ -297,7 +299,7 @@ describe('Template Helper', function () { }; ctx.url = 'https://app.example.com/this_page'; const result = th.htmlPage(pagePathLevel, ctx, options, main); - await lintHtml.lint(result); + await htmlLint(result); assert(result); }); it('covers existing navLinks', async function () { @@ -309,7 +311,7 @@ describe('Template Helper', function () { href: 'link', }]; const result = th.htmlPage(pagePathLevel, ctx, options); - await lintHtml.lint(result); + await htmlLint(result); assert(result); }); }); // htmlPage diff --git a/test/lint-html.js b/test/lint-html.js index 26cfed2..50abb84 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)) { + result.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 -- 2.44.1