Commit 216b4499 authored by David Sveningsson's avatar David Sveningsson

feat(rules): improved reported error location for some rules

parent 03182f39
Pipeline #112378916 passed with stages
in 10 minutes and 18 seconds
......@@ -9,14 +9,14 @@ Array [
"filePath": "inline",
"messages": Array [
Object {
"column": 20,
"column": 12,
"context": undefined,
"line": 1,
"message": "Close tags cannot have attributes",
"offset": 19,
"offset": 11,
"ruleId": "close-attr",
"severity": 2,
"size": 1,
"size": 2,
},
],
"source": "<div></div id=\\"foo\\">",
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`docs/rules/close-order.md inline validation: correct 1`] = `Array []`;
exports[`docs/rules/close-order.md inline validation: correct-1 1`] = `Array []`;
exports[`docs/rules/close-order.md inline validation: incorrect 1`] = `
exports[`docs/rules/close-order.md inline validation: correct-2 1`] = `Array []`;
exports[`docs/rules/close-order.md inline validation: incorrect-1 1`] = `
Array [
Object {
"errorCount": 2,
......@@ -11,9 +13,9 @@ Array [
Object {
"column": 13,
"context": undefined,
"line": 1,
"line": 2,
"message": "Mismatched close-tag, expected '</strong>' but found '</p>'.",
"offset": 12,
"offset": 43,
"ruleId": "close-order",
"severity": 2,
"size": 2,
......@@ -21,15 +23,64 @@ Array [
Object {
"column": 17,
"context": undefined,
"line": 1,
"line": 2,
"message": "Mismatched close-tag, expected '</p>' but found '</strong>'.",
"offset": 16,
"offset": 47,
"ruleId": "close-order",
"severity": 2,
"size": 7,
},
],
"source": "<p><strong></p></strong>",
"source": "<!-- closed in wrong order -->
<p><strong></p></strong>",
"warningCount": 0,
},
]
`;
exports[`docs/rules/close-order.md inline validation: incorrect-2 1`] = `
Array [
Object {
"errorCount": 1,
"filePath": "inline",
"messages": Array [
Object {
"column": 6,
"context": undefined,
"line": 2,
"message": "Missing close-tag, expected '</div>' but document ended before it was found.",
"offset": 36,
"ruleId": "close-order",
"severity": 2,
"size": 0,
},
],
"source": "<!-- opened but not closed -->
<div>",
"warningCount": 0,
},
]
`;
exports[`docs/rules/close-order.md inline validation: incorrect-3 1`] = `
Array [
Object {
"errorCount": 1,
"filePath": "inline",
"messages": Array [
Object {
"column": 2,
"context": undefined,
"line": 2,
"message": "Unexpected close-tag, expected opening tag.",
"offset": 32,
"ruleId": "close-order",
"severity": 2,
"size": 4,
},
],
"source": "<!-- closed but not opened -->
</div>",
"warningCount": 0,
},
]
......
......@@ -19,14 +19,14 @@ Array [
"size": 2,
},
Object {
"column": 11,
"column": 7,
"context": undefined,
"line": 5,
"message": "End tag for <img> must be omitted",
"offset": 47,
"offset": 43,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 4,
},
],
"source": "<fieldset>
......
import HtmlValidate from "../../../src/htmlvalidate";
const markup: { [key: string]: string } = {};
markup["incorrect"] = `<p><strong></p></strong>`;
markup["correct"] = `<p><strong></strong></p>`;
markup["incorrect-1"] = `<!-- closed in wrong order -->
<p><strong></p></strong>`;
markup["incorrect-2"] = `<!-- opened but not closed -->
<div>`;
markup["incorrect-3"] = `<!-- closed but not opened -->
</div>`;
markup["correct-1"] = `<p><strong></strong></p>`;
markup["correct-2"] = `<div></div>`;
describe("docs/rules/close-order.md", () => {
it("inline validation: incorrect", () => {
it("inline validation: incorrect-1", () => {
const htmlvalidate = new HtmlValidate({"rules":{"close-order":"error"}});
const report = htmlvalidate.validateString(markup["incorrect"]);
const report = htmlvalidate.validateString(markup["incorrect-1"]);
expect(report.results).toMatchSnapshot();
});
it("inline validation: correct", () => {
it("inline validation: incorrect-2", () => {
const htmlvalidate = new HtmlValidate({"rules":{"close-order":"error"}});
const report = htmlvalidate.validateString(markup["correct"]);
const report = htmlvalidate.validateString(markup["incorrect-2"]);
expect(report.results).toMatchSnapshot();
});
it("inline validation: incorrect-3", () => {
const htmlvalidate = new HtmlValidate({"rules":{"close-order":"error"}});
const report = htmlvalidate.validateString(markup["incorrect-3"]);
expect(report.results).toMatchSnapshot();
});
it("inline validation: correct-1", () => {
const htmlvalidate = new HtmlValidate({"rules":{"close-order":"error"}});
const report = htmlvalidate.validateString(markup["correct-1"]);
expect(report.results).toMatchSnapshot();
});
it("inline validation: correct-2", () => {
const htmlvalidate = new HtmlValidate({"rules":{"close-order":"error"}});
const report = htmlvalidate.validateString(markup["correct-2"]);
expect(report.results).toMatchSnapshot();
});
});
......@@ -4,7 +4,7 @@ name: close-order
summary: Require elements to be closed in correct order
---
# disallows elements closed in the wrong order (`close-order`)
# Disallows elements closed in the wrong order (`close-order`)
HTML requires elements to be closed in the correct order.
......@@ -12,12 +12,27 @@ HTML requires elements to be closed in the correct order.
Examples of **incorrect** code for this rule:
<validate name="incorrect" rules="close-order">
<validate name="incorrect-1" rules="close-order">
<!-- closed in wrong order -->
<p><strong></p></strong>
</validate>
<validate name="incorrect-2" rules="close-order">
<!-- opened but not closed -->
<div>
</validate>
<validate name="incorrect-3" rules="close-order">
<!-- closed but not opened -->
</div>
</validate>
Examples of **correct** code for this rule:
<validate name="correct" rules="close-order">
<validate name="correct-1" rules="close-order">
<p><strong></strong></p>
</validate>
<validate name="correct-2" rules="close-order">
<div></div>
</validate>
......@@ -229,24 +229,24 @@ Array [
"filePath": "test-files/elements/area-invalid.html",
"messages": Array [
Object {
"column": 14,
"column": 9,
"context": undefined,
"line": 6,
"message": "End tag for <area> must be omitted",
"offset": 101,
"offset": 96,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 5,
},
Object {
"column": 8,
"column": 3,
"context": undefined,
"line": 11,
"message": "End tag for <area> must be omitted",
"offset": 173,
"offset": 168,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 5,
},
Object {
"column": 3,
......@@ -540,14 +540,14 @@ Array [
"size": 4,
},
Object {
"column": 13,
"column": 8,
"context": undefined,
"line": 7,
"message": "End tag for <base> must be omitted",
"offset": 114,
"offset": 109,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 5,
},
],
"source": "<!-- should not be allowed outside <head> -->
......@@ -790,14 +790,14 @@ Array [
"filePath": "test-files/elements/br-invalid.html",
"messages": Array [
Object {
"column": 9,
"column": 6,
"context": undefined,
"line": 2,
"message": "End tag for <br> must be omitted",
"offset": 42,
"offset": 39,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 3,
},
],
"source": "<!-- should not allow end tag -->
......@@ -1083,14 +1083,14 @@ Array [
"size": 3,
},
Object {
"column": 13,
"column": 9,
"context": undefined,
"line": 9,
"message": "End tag for <col> must be omitted",
"offset": 138,
"offset": 134,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 4,
},
],
"source": "<!-- should not be allowed under other content -->
......@@ -1562,14 +1562,14 @@ Array [
"filePath": "test-files/elements/embed-invalid.html",
"messages": Array [
Object {
"column": 51,
"column": 45,
"context": undefined,
"line": 2,
"message": "End tag for <embed> must be omitted",
"offset": 97,
"offset": 91,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 6,
},
Object {
"column": 2,
......@@ -2451,14 +2451,14 @@ Array [
"filePath": "test-files/elements/hr-invalid.html",
"messages": Array [
Object {
"column": 9,
"column": 6,
"context": undefined,
"line": 2,
"message": "End tag for <hr> must be omitted",
"offset": 42,
"offset": 39,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 3,
},
Object {
"column": 8,
......@@ -2721,14 +2721,14 @@ Array [
"size": 3,
},
Object {
"column": 25,
"column": 21,
"context": undefined,
"line": 8,
"message": "End tag for <img> must be omitted",
"offset": 143,
"offset": 139,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 4,
},
Object {
"column": 3,
......@@ -2769,14 +2769,14 @@ Array [
"filePath": "test-files/elements/input-invalid.html",
"messages": Array [
Object {
"column": 27,
"column": 21,
"context": undefined,
"line": 2,
"message": "End tag for <input> must be omitted",
"offset": 60,
"offset": 54,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 6,
},
Object {
"column": 31,
......@@ -3119,14 +3119,14 @@ Array [
"filePath": "test-files/elements/keygen-invalid.html",
"messages": Array [
Object {
"column": 17,
"column": 10,
"context": undefined,
"line": 2,
"message": "End tag for <keygen> must be omitted",
"offset": 45,
"offset": 38,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 7,
},
],
"source": "<!-- should omit end tag -->
......@@ -3265,14 +3265,14 @@ Array [
"filePath": "test-files/elements/link-invalid.html",
"messages": Array [
Object {
"column": 28,
"column": 23,
"context": undefined,
"line": 2,
"message": "End tag for <link> must be omitted",
"offset": 61,
"offset": 56,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 5,
},
Object {
"column": 2,
......@@ -3595,14 +3595,14 @@ Array [
"filePath": "test-files/elements/meta-invalid.html",
"messages": Array [
Object {
"column": 13,
"column": 8,
"context": undefined,
"line": 2,
"message": "End tag for <meta> must be omitted",
"offset": 41,
"offset": 36,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 5,
},
],
"source": "<!-- should omit end tag -->
......@@ -4154,14 +4154,14 @@ Array [
"filePath": "test-files/elements/param-invalid.html",
"messages": Array [
Object {
"column": 15,
"column": 9,
"context": undefined,
"line": 2,
"message": "End tag for <param> must be omitted",
"offset": 43,
"offset": 37,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 6,
},
Object {
"column": 3,
......@@ -4795,14 +4795,14 @@ Array [
"filePath": "test-files/elements/source-invalid.html",
"messages": Array [
Object {
"column": 18,
"column": 11,
"context": undefined,
"line": 3,
"message": "End tag for <source> must be omitted",
"offset": 54,
"offset": 47,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 7,
},
Object {
"column": 3,
......@@ -5695,14 +5695,14 @@ Array [
"filePath": "test-files/elements/track-invalid.html",
"messages": Array [
Object {
"column": 16,
"column": 10,
"context": undefined,
"line": 3,
"message": "End tag for <track> must be omitted",
"offset": 52,
"offset": 46,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 6,
},
],
"source": "<!-- should omit end tag -->
......@@ -5955,14 +5955,14 @@ Array [
"filePath": "test-files/elements/wbr-invalid.html",
"messages": Array [
Object {
"column": 11,
"column": 7,
"context": undefined,
"line": 2,
"message": "End tag for <wbr> must be omitted",
"offset": 39,
"offset": 35,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 4,
},
],
"source": "<!-- should omit end tag -->
......
......@@ -331,14 +331,14 @@ Array [
"filePath": "test-files/config/file.html",
"messages": Array [
Object {
"column": 9,
"column": 6,
"context": undefined,
"line": 2,
"message": "End tag for <br> must be omitted",
"offset": 52,
"offset": 49,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 3,
},
],
"source": "<!-- default configuration yields error -->
......@@ -371,14 +371,14 @@ Array [
"filePath": "test-files/config/off/error/file.html",
"messages": Array [
Object {
"column": 9,
"column": 6,
"context": undefined,
"line": 2,
"message": "End tag for <br> must be omitted",
"offset": 45,
"offset": 42,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 3,
},
],
"source": "<!-- reconfigured to yield error -->
......@@ -428,14 +428,14 @@ Array [
"filePath": "test-files/config/warn/file.html",
"messages": Array [
Object {
"column": 9,
"column": 6,
"context": undefined,
"line": 2,
"message": "End tag for <br> must be omitted",
"offset": 47,
"offset": 44,
"ruleId": "void",
"severity": 1,
"size": 1,
"size": 3,
},
],
"source": "<!-- configured to yield a warning -->
......
......@@ -14,14 +14,14 @@ Array [
"filePath": "test-files/rules/close-attr.html",
"messages": Array [
Object {
"column": 15,
"column": 12,
"context": undefined,
"line": 3,
"message": "Close tags cannot have attributes",
"offset": 36,
"offset": 33,
"ruleId": "close-attr",
"severity": 2,
"size": 1,
"size": 3,
},
],
"source": "<input foo>
......
......@@ -24,14 +24,14 @@ Array [
"size": 4,
},
Object {
"column": 5,
"column": 2,
"context": undefined,
"line": 12,
"message": "Unexpected close-tag, expected opening tag.",
"offset": 112,
"offset": 109,
"ruleId": "close-order",
"severity": 2,
"size": 1,
"size": 3,
},
],
"source": "<div>
......
......@@ -7,14 +7,14 @@ Array [
"filePath": "test-files/rules/void.html",
"messages": Array [
Object {
"column": 16,
"column": 10,
"context": undefined,
"line": 10,
"message": "End tag for <input> must be omitted",
"offset": 69,
"offset": 63,
"ruleId": "void",
"severity": 2,
"size": 1,
"size": 6,
},
Object {
"column": 6,
......@@ -64,14 +64,14 @@ Array [
"size": 2,
},
Object {
"column": 16,
"column": 10,
"context": undefined,
"line": 10,
"message": "End tag for <input> must be omitted",
"offset": 69,
"offset": 63,