HTMLHint is a comprehensive static analysis tool specifically designed for HTML code quality assurance and validation.
npx @tessl/cli install tessl/npm-htmlhint@1.6.0HTMLHint is a comprehensive static analysis tool specifically designed for HTML code quality assurance and validation. It provides developers with automated HTML linting capabilities through both command-line interface and programmatic API, offering extensive rule-based validation for HTML syntax, semantics, accessibility, and best practices.
npm install htmlhintimport { HTMLHint } from "htmlhint";For CommonJS:
const { HTMLHint } = require("htmlhint");Alternative imports for specific components:
import { HTMLHint, HTMLRules, Reporter, HTMLParser } from "htmlhint";import { HTMLHint } from "htmlhint";
// Basic HTML validation
const html = `<html>
<head><title>Test</title></head>
<body><h1>Hello World</h1></body>
</html>`;
// Validate with default rules
const hints = HTMLHint.verify(html);
if (hints.length > 0) {
console.log('Issues found:');
hints.forEach(hint => {
console.log(`${hint.line}:${hint.col} - ${hint.message} (${hint.rule.id})`);
});
} else {
console.log('No issues found!');
}HTMLHint is built around several key components:
HTMLHint uses a singleton pattern with the following exports:
HTMLHintHTMLRulesReporterHTMLParserThe HTMLHint singleton is created from HTMLHintCore and automatically registers all built-in rules during initialization.
Primary HTML validation functionality for analyzing HTML content against configurable rulesets. Supports both synchronous validation and custom rule configurations.
class HTMLHintCore {
rules: { [id: string]: Rule };
readonly defaultRuleset: Ruleset;
verify(html: string, ruleset?: Ruleset): Hint[];
addRule(rule: Rule): void;
format(arrMessages: Hint[], options?: FormatOptions): string[];
}
const HTMLHint: HTMLHintCore;Comprehensive collection of 43+ built-in HTML validation rules covering syntax, semantics, accessibility, and best practices. Supports extensible custom rule development.
interface Rule {
id: string;
description: string;
link?: string;
init(parser: HTMLParser, reporter: Reporter, options: unknown): void;
}
interface Ruleset {
'tagname-lowercase'?: boolean;
'attr-value-double-quotes'?: boolean;
'doctype-first'?: boolean;
'tag-pair'?: boolean;
'id-unique'?: boolean;
// ... 38+ more rules
[ruleId: string]: unknown;
}Event-driven HTML parser for processing document structures, extracting elements, attributes, and content with position tracking.
class HTMLParser {
lastEvent: Partial<Block> | null;
parse(html: string): void;
addListener(types: string, listener: Listener): void;
fire(type: string, data?: Partial<Block>): void;
removeListener(type: string, listener: Listener): void;
fixPos(event: Block, index: number): { line: number; col: number };
getMapAttrs(arrAttrs: Attr[]): { [name: string]: string };
}
type Listener = (event: Block) => void;Structured issue collection and reporting system with multiple severity levels, position tracking, and evidence capture.
class Reporter {
html: string;
lines: string[];
brLen: number;
ruleset: Ruleset;
messages: Hint[];
constructor(html: string, ruleset: Ruleset);
info(message: string, line: number, col: number, rule: Rule, raw: string): void;
warn(message: string, line: number, col: number, rule: Rule, raw: string): void;
error(message: string, line: number, col: number, rule: Rule, raw: string): void;
}
interface Hint {
type: ReportType;
message: string;
raw: string;
evidence: string;
line: number;
col: number;
rule: Rule;
}Full-featured CLI for batch HTML validation with file globbing, configuration management, multiple output formats, and CI/CD integration support.
interface Formatter extends EventEmitter {
getSupported(): string[];
init(htmlhint: HTMLHintCore, options: { nocolor?: boolean }): void;
setFormat(format: string): void;
}
// Available CLI formatters
type SupportedFormats = 'default' | 'json' | 'checkstyle' | 'junit' | 'sarif' | 'html' | 'markdown' | 'compact' | 'unix';interface FormatOptions {
colors?: boolean;
indent?: number;
}
enum ReportType {
error = "error",
warning = "warning",
info = "info"
}
interface Attr {
name: string;
value: string;
quote: string;
index: number;
raw: string;
}
interface Block {
tagName: string;
attrs: Attr[];
type: string;
raw: string;
pos: number;
line: number;
col: number;
content: string;
long: boolean;
close: string;
lastEvent?: Partial<Block>;
}