Commit 12ed8b83 authored by David Sveningsson's avatar David Sveningsson
Browse files

feat(rules): add autofix support to `doctype-html`

parent f5bc9349
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ exports[`docs/rules/doctype-html.md inline validation: incorrect 1`] = `
    "messages": [
      {
        "column": 11,
        "fix": [Function],
        "line": 1,
        "message": "doctype should be "html"",
        "offset": 10,
@@ -34,6 +35,7 @@ exports[`docs/rules/doctype-html.md inline validation: legacy 1`] = `
    "messages": [
      {
        "column": 11,
        "fix": [Function],
        "line": 1,
        "message": "doctype should be "html"",
        "offset": 10,
+13 −0
Original line number Diff line number Diff line
@@ -60,4 +60,17 @@ describe("rule doctype-html", () => {
		const docs = await htmlvalidate.getRuleDocumentation("doctype-html");
		expect(docs).toMatchSnapshot();
	});

	describe("autofix", () => {
		it("should replace legacy doctype with html", async () => {
			expect.assertions(1);
			const html =
				/* eslint-disable-next-line unicorn/prefer-https -- test should ensure this works */
				'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
			const report = await htmlvalidate.validateString(html);
			const [message] = report.results[0].messages;
			const result = await htmlvalidate.autofixString("inline", html, message.fix!);
			expect(result).toBe("<!DOCTYPE html>");
		});
	});
});
+10 −1
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@ import { type DoctypeEvent } from "../event";
import { type RuleDocumentation, Rule, ruleDocumentationUrl } from "../rule";

export default class NoStyleTag extends Rule {
	public static override readonly fixable = true;

	public override documentation(): RuleDocumentation {
		return {
			description: [
@@ -19,7 +21,14 @@ export default class NoStyleTag extends Rule {
		this.on("doctype", (event: DoctypeEvent) => {
			const doctype = event.value.toLowerCase();
			if (doctype !== "html") {
				this.report(null, 'doctype should be "html"', event.valueLocation);
				this.report({
					node: null,
					message: 'doctype should be "html"',
					location: event.valueLocation,
					fix(fixer) {
						fixer.replaceText(event.valueLocation, "html");
					},
				});
			}
		});
	}