Commit 69e81024 authored by David Sveningsson's avatar David Sveningsson

feat(api): add additional prototypes to `validateString`

parent fcd85e05
Pipeline #197705141 passed with stages
in 9 minutes and 18 seconds
export { Source, ProcessElementContext } from "./source";
export { Source, SourceHooks, ProcessElementContext } from "./source";
export { Location, sliceLocation } from "./location";
export { Context, ContentModel } from "./context";
import { Config, ConfigLoader, Severity } from "./config";
import { Source } from "./context";
import { Config, ConfigData, ConfigLoader, Severity } from "./config";
import { Source, SourceHooks } from "./context";
import HtmlValidate from "./htmlvalidate";
import { Parser } from "./parser";
import { Message } from "./reporter";
......@@ -108,6 +108,136 @@ describe("HtmlValidate", () => {
},
});
});
describe("prototype", () => {
const report = "mock-report";
const markup = "<i></i>";
let htmlvalidate: HtmlValidate;
beforeEach(() => {
htmlvalidate = new HtmlValidate();
engine.lint.mockReturnValue(report);
});
it("str", () => {
expect.assertions(1);
htmlvalidate.validateString(markup);
expect(engine.lint).toHaveBeenCalledWith([
{
line: 1,
column: 1,
offset: 0,
data: markup,
filename: "inline",
},
]);
});
it("str, filename", () => {
expect.assertions(1);
htmlvalidate.validateString(markup, "my-file.html");
expect(engine.lint).toHaveBeenCalledWith([
{
line: 1,
column: 1,
offset: 0,
data: markup,
filename: "my-file.html",
},
]);
});
it("str, hooks", () => {
expect.assertions(1);
const hooks: SourceHooks = {
processElement: () => {
return null;
},
};
htmlvalidate.validateString(markup, hooks);
expect(engine.lint).toHaveBeenCalledWith([
{
line: 1,
column: 1,
offset: 0,
data: markup,
filename: "inline",
hooks,
},
]);
});
it("str, options", () => {
expect.assertions(1);
const options: ConfigData = {};
htmlvalidate.validateString(markup, options);
expect(engine.lint).toHaveBeenCalledWith([
{
line: 1,
column: 1,
offset: 0,
data: markup,
filename: "inline",
},
]);
});
it("str, filename, hooks", () => {
expect.assertions(1);
const hooks: SourceHooks = {
processAttribute: () => {
return null;
},
};
htmlvalidate.validateString(markup, "my-file.html", hooks);
expect(engine.lint).toHaveBeenCalledWith([
{
line: 1,
column: 1,
offset: 0,
data: markup,
filename: "my-file.html",
hooks,
},
]);
});
it("str, filename, options", () => {
expect.assertions(1);
const options: ConfigData = {};
htmlvalidate.validateString(markup, "my-file.html", options);
expect(engine.lint).toHaveBeenCalledWith([
{
line: 1,
column: 1,
offset: 0,
data: markup,
filename: "my-file.html",
},
]);
});
it("str, filename, options, hooks", () => {
expect.assertions(1);
const options: ConfigData = {};
const hooks: SourceHooks = {
processAttribute: () => {
return null;
},
};
htmlvalidate.validateString(markup, "my-file.html", options, hooks);
expect(engine.lint).toHaveBeenCalledWith([
{
line: 1,
column: 1,
offset: 0,
data: markup,
filename: "my-file.html",
hooks,
},
]);
});
});
});
it("validateSource() should lint given source", () => {
......
......@@ -8,7 +8,17 @@ import { Report, Reporter } from "./reporter";
import { RuleDocumentation } from "./rule";
function isSourceHooks(value: any): value is SourceHooks {
return Boolean(value && (value.processAttribute || value.processElement));
if (!value || typeof value === "string") {
return false;
}
return Boolean(value.processAttribute || value.processElement);
}
function isConfigData(value: any): value is ConfigData {
if (!value || typeof value === "string") {
return false;
}
return !(value.processAttribute || value.processElement);
}
/**
......@@ -40,18 +50,30 @@ class HtmlValidate {
* @param hooks - Optional hooks (see [[Source]]) for definition.
* @returns Report output.
*/
public validateString(str: string): Report;
public validateString(str: string, filename: string): Report;
public validateString(str: string, hooks: SourceHooks): Report;
public validateString(str: string, options: ConfigData): Report;
public validateString(str: string, filename: string, hooks: SourceHooks): Report;
public validateString(str: string, filename: string, options: ConfigData): Report;
public validateString(
str: string,
filename?: string,
options?: SourceHooks | ConfigData,
hooks?: SourceHooks
filename: string,
options: ConfigData,
hooks: SourceHooks
): Report;
public validateString(
str: string,
arg1?: string | SourceHooks | ConfigData,
arg2?: SourceHooks | ConfigData,
arg3?: SourceHooks
): Report {
if (isSourceHooks(options)) {
return this.validateString(str, filename, null, options);
}
const filename = typeof arg1 === "string" ? arg1 : "inline";
const options = isConfigData(arg1) ? arg1 : isConfigData(arg2) ? arg2 : undefined;
const hooks = isSourceHooks(arg1) ? arg1 : isSourceHooks(arg2) ? arg2 : arg3;
const source = {
data: str,
filename: filename || "inline",
filename,
line: 1,
column: 1,
offset: 0,
......
......@@ -39,7 +39,7 @@ describe("rule allowed-links", () => {
"allowed-links": ["error", { allowRelative: false }],
},
});
const report = htmlvalidate.validateString('<a dynamic-src="{{ expr }}"></a>', null, {
const report = htmlvalidate.validateString('<a dynamic-src="{{ expr }}"></a>', {
processAttribute,
});
expect(report).toBeValid();
......
......@@ -214,7 +214,7 @@ describe("rule attr-case", () => {
htmlvalidate = new HtmlValidate({
rules: { "attr-case": "error" },
});
const report = htmlvalidate.validateString('<input dynamic-fooBar="foo">', null, {
const report = htmlvalidate.validateString('<input dynamic-fooBar="foo">', {
processAttribute,
});
expect(report).toBeInvalid();
......
......@@ -70,7 +70,7 @@ describe("rule attribute-allowed-values", () => {
expect.assertions(1);
const report = htmlvalidate.validateString(
'<input type="{{ interpolated }}" required="{{ interpolated }}"><input dynamic-type="dynamic" dynamic-required="dynamic">',
null,
{
processAttribute,
}
......
......@@ -64,7 +64,7 @@ describe("rule attribute-boolean-style", () => {
it("should report error when attribute is interpolated", () => {
expect.assertions(2);
const report = htmlvalidate.validateString('<input required="{{ dynamic }}">', null, {
const report = htmlvalidate.validateString('<input required="{{ dynamic }}">', {
processAttribute,
});
expect(report).toBeInvalid();
......@@ -76,7 +76,7 @@ describe("rule attribute-boolean-style", () => {
it("should not report error when attribute is dynamic", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<input dynamic-required="dynamic">', null, {
const report = htmlvalidate.validateString('<input dynamic-required="dynamic">', {
processAttribute,
});
expect(report).toBeValid();
......@@ -124,7 +124,7 @@ describe("rule attribute-boolean-style", () => {
it("should report error when attribute is dynamic", () => {
expect.assertions(2);
const report = htmlvalidate.validateString('<input required="{{ dynamic }}">', null, {
const report = htmlvalidate.validateString('<input required="{{ dynamic }}">', {
processAttribute,
});
expect(report).toBeInvalid();
......@@ -176,7 +176,7 @@ describe("rule attribute-boolean-style", () => {
it("should report error when attribute is dynamic", () => {
expect.assertions(2);
const report = htmlvalidate.validateString('<input required="{{ dynamic }}">', null, {
const report = htmlvalidate.validateString('<input required="{{ dynamic }}">', {
processAttribute,
});
expect(report).toBeInvalid();
......
......@@ -57,7 +57,7 @@ describe("rule attribute-empty-style", () => {
it("should not report error when attribute is interpolated", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<a download="{{ dynamic }}">', null, {
const report = htmlvalidate.validateString('<a download="{{ dynamic }}">', {
processAttribute,
});
expect(report).toBeValid();
......@@ -65,7 +65,7 @@ describe("rule attribute-empty-style", () => {
it("should not report error when attribute is dynamic", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<input dynamic-required="dynamic">', null, {
const report = htmlvalidate.validateString('<input dynamic-required="dynamic">', {
processAttribute,
});
expect(report).toBeValid();
......@@ -109,7 +109,7 @@ describe("rule attribute-empty-style", () => {
it("should not report error when attribute is interpolated", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<a download="{{ dynamic }}">', null, {
const report = htmlvalidate.validateString('<a download="{{ dynamic }}">', {
processAttribute,
});
expect(report).toBeValid();
......@@ -117,7 +117,7 @@ describe("rule attribute-empty-style", () => {
it("should not report error when attribute is dynamic", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<input dynamic-required="dynamic">', null, {
const report = htmlvalidate.validateString('<input dynamic-required="dynamic">', {
processAttribute,
});
expect(report).toBeValid();
......
......@@ -28,7 +28,7 @@ describe("rule empty-heading", () => {
function processElement(node: HtmlElement): void {
node.appendText(new DynamicValue(""));
}
const report = htmlvalidate.validateString("<h1></h1>", null, {
const report = htmlvalidate.validateString("<h1></h1>", {
processElement,
});
expect(report).toBeValid();
......
......@@ -28,7 +28,7 @@ describe("rule empty-title", () => {
function processElement(node: HtmlElement): void {
node.appendText(new DynamicValue(""));
}
const report = htmlvalidate.validateString("<title></title>", null, {
const report = htmlvalidate.validateString("<title></title>", {
processElement,
});
expect(report).toBeValid();
......
......@@ -19,7 +19,7 @@ describe("rule id-pattern", () => {
it("should not report error when id is interpolated", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<p id="{{ interpolated }}"></p>', null, {
const report = htmlvalidate.validateString('<p id="{{ interpolated }}"></p>', {
processAttribute,
});
expect(report).toBeValid();
......
......@@ -25,7 +25,7 @@ describe("rule no-autoplay", () => {
expect.assertions(1);
const report = htmlvalidate.validateString(
'<video dynamic-autoplay="enableAutoplay">',
null,
{
processAttribute,
}
......
......@@ -19,7 +19,7 @@ describe("rule no-dup-attr", () => {
it("should not report error when attribute is dynamic", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<input class="foo" dynamic-class="bar">', null, {
const report = htmlvalidate.validateString('<input class="foo" dynamic-class="bar">', {
processAttribute,
});
expect(report).toBeValid();
......@@ -41,11 +41,9 @@ describe("rule no-dup-attr", () => {
it("should report error when dynamic element is used multiple times", () => {
expect.assertions(2);
const report = htmlvalidate.validateString(
'<input dynamic-class="foo" dynamic-class="bar">',
null,
{ processAttribute }
);
const report = htmlvalidate.validateString('<input dynamic-class="foo" dynamic-class="bar">', {
processAttribute,
});
expect(report).toBeInvalid();
expect(report).toHaveError("no-dup-attr", 'Attribute "dynamic-class" duplicated');
});
......
......@@ -21,7 +21,7 @@ describe("rule no-inline-style", () => {
it("should report when dynamic style attribute is used", () => {
expect.assertions(2);
const report = htmlvalidate.validateString('<p dynamic-style=""></p>', null, {
const report = htmlvalidate.validateString('<p dynamic-style=""></p>', {
processAttribute,
});
expect(report).toBeInvalid();
......@@ -45,7 +45,7 @@ describe("rule no-inline-style", () => {
it("should not report when dynamic style attribute is used", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<p dynamic-style=""></p>', null, {
const report = htmlvalidate.validateString('<p dynamic-style=""></p>', {
processAttribute,
});
expect(report).toBeValid();
......@@ -67,7 +67,7 @@ describe("rule no-inline-style", () => {
it("should report when dynamic style attribute is used", () => {
expect.assertions(2);
const report = htmlvalidate.validateString('<p dynamic-style=""></p>', null, {
const report = htmlvalidate.validateString('<p dynamic-style=""></p>', {
processAttribute,
});
expect(report).toBeInvalid();
......
......@@ -31,7 +31,7 @@ describe("rule no-redundant-role", () => {
it("should not report error for dynamic attributes", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<input dynamic-role="main">', null, {
const report = htmlvalidate.validateString('<input dynamic-role="main">', {
processAttribute,
});
expect(report).toBeValid();
......
......@@ -27,7 +27,7 @@ describe("rule prefer-button", () => {
it("should not report error for dynamic attributes", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<input dynamic-type="inputType">', null, {
const report = htmlvalidate.validateString('<input dynamic-type="inputType">', {
processAttribute,
});
expect(report).toBeValid();
......
......@@ -26,7 +26,7 @@ describe("rule prefer-native-element", () => {
it("should not report error for dynamic attributes", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<input dynamic-role="main">', null, {
const report = htmlvalidate.validateString('<input dynamic-role="main">', {
processAttribute,
});
expect(report).toBeValid();
......
......@@ -31,7 +31,7 @@ describe("rule script-type", () => {
it("should not report error for dynamic attributes", () => {
expect.assertions(1);
const report = htmlvalidate.validateString('<script dynamic-type="type">', null, {
const report = htmlvalidate.validateString('<script dynamic-type="type">', {
processAttribute,
});
expect(report).toBeValid();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment