Commit c2e09a26 authored by David Sveningsson's avatar David Sveningsson

feat(htmlvalidate): support passing filename to `validateString`

parent 09d66871
Pipeline #90030847 passed with stages
in 7 minutes
......@@ -56,21 +56,33 @@ describe("HtmlValidate", () => {
);
});
it("validateString() should lint given string", () => {
const mockReport = "mock-report";
engine.lint.mockReturnValue(mockReport);
const htmlvalidate = new HtmlValidate();
const str = "foobar";
const report = htmlvalidate.validateString(str);
expect(report).toEqual(mockReport);
expect(engine.lint).toHaveBeenCalledWith([
{
column: 1,
data: str,
filename: "inline",
line: 1,
},
]);
describe("validateString()", () => {
it("should validate given string", () => {
const mockReport = "mock-report";
engine.lint.mockReturnValue(mockReport);
const htmlvalidate = new HtmlValidate();
const str = "foobar";
const report = htmlvalidate.validateString(str);
expect(report).toEqual(mockReport);
expect(engine.lint).toHaveBeenCalledWith([
{
column: 1,
data: str,
filename: "inline",
line: 1,
},
]);
});
it("should load configuration if filename is given", () => {
const mockReport = "mock-report";
engine.lint.mockReturnValue(mockReport);
const htmlvalidate = new HtmlValidate();
const spy = jest.spyOn(htmlvalidate, "getConfigFor");
const str = "foobar";
htmlvalidate.validateString(str, "my-file.html");
expect(spy).toHaveBeenCalledWith("my-file.html");
});
});
it("validateSource() should lint given source", () => {
......
......@@ -33,14 +33,19 @@ class HtmlValidate {
* Parse and validate HTML from string.
*
* @param str - Text to parse.
* @param filename - If set configuration is loaded for given filename.
* @param hooks - Optional hooks (see [[Source]]) for definition.
* @returns Report output.
*/
public validateString(str: string, hooks?: SourceHooks): Report {
public validateString(
str: string,
filename?: string,
hooks?: SourceHooks
): Report {
const source = {
column: 1,
data: str,
filename: "inline",
filename: filename || "inline",
line: 1,
hooks,
};
......@@ -54,7 +59,7 @@ class HtmlValidate {
* @returns Report output.
*/
public validateSource(source: Source): Report {
const config = this.getConfigFor("inline");
const config = this.getConfigFor(source.filename);
const engine = new Engine(config, Parser);
return engine.lint([source]);
}
......
......@@ -211,9 +211,13 @@ describe("rule attr-case", () => {
htmlvalidate = new HtmlValidate({
rules: { "attr-case": "error" },
});
const report = htmlvalidate.validateString('<input dynamic-fooBar="foo">', {
processAttribute,
});
const report = htmlvalidate.validateString(
'<input dynamic-fooBar="foo">',
null,
{
processAttribute,
}
);
expect(report).toBeInvalid();
expect(report).toHaveErrors([
{
......
......@@ -56,6 +56,7 @@ describe("rule attribute-allowed-values", () => {
it("should not report error when attribute is dynamic", () => {
const report = htmlvalidate.validateString(
'<input type="{{ interpolated }}" required="{{ interpolated }}"><input dynamic-type="dynamic" dynamic-required="dynamic">',
null,
{
processAttribute,
}
......
......@@ -51,6 +51,7 @@ describe("rule attribute-boolean-style", () => {
it("should report error when attribute is interpolated", () => {
const report = htmlvalidate.validateString(
'<input required="{{ dynamic }}">',
null,
{ processAttribute }
);
expect(report).toBeInvalid();
......@@ -63,6 +64,7 @@ describe("rule attribute-boolean-style", () => {
it("should not report error when attribute is dynamic", () => {
const report = htmlvalidate.validateString(
'<input dynamic-required="dynamic">',
null,
{ processAttribute }
);
expect(report).toBeValid();
......@@ -109,6 +111,7 @@ describe("rule attribute-boolean-style", () => {
it("should report error when attribute is dynamic", () => {
const report = htmlvalidate.validateString(
'<input required="{{ dynamic }}">',
null,
{ processAttribute }
);
expect(report).toBeInvalid();
......@@ -159,6 +162,7 @@ describe("rule attribute-boolean-style", () => {
it("should report error when attribute is dynamic", () => {
const report = htmlvalidate.validateString(
'<input required="{{ dynamic }}">',
null,
{ processAttribute }
);
expect(report).toBeInvalid();
......
......@@ -27,7 +27,7 @@ describe("rule empty-heading", () => {
function processElement(node: HtmlElement): void {
node.appendText(new DynamicValue(""));
}
const report = htmlvalidate.validateString("<h1></h1>", {
const report = htmlvalidate.validateString("<h1></h1>", null, {
processElement,
});
expect(report).toBeValid();
......
......@@ -27,7 +27,7 @@ describe("rule empty-title", () => {
function processElement(node: HtmlElement): void {
node.appendText(new DynamicValue(""));
}
const report = htmlvalidate.validateString("<title></title>", {
const report = htmlvalidate.validateString("<title></title>", null, {
processElement,
});
expect(report).toBeValid();
......
......@@ -19,6 +19,7 @@ describe("rule id-pattern", () => {
it("should not report error when id is interpolated", () => {
const report = htmlvalidate.validateString(
'<p id="{{ interpolated }}"></p>',
null,
{ processAttribute }
);
expect(report).toBeValid();
......
......@@ -19,6 +19,7 @@ describe("rule no-dup-attr", () => {
it("should not report error when attribute is dynamic", () => {
const report = htmlvalidate.validateString(
'<input class="foo" dynamic-class="bar">',
null,
{ processAttribute }
);
expect(report).toBeValid();
......@@ -43,6 +44,7 @@ describe("rule no-dup-attr", () => {
it("should report error when dynamic element is used multiple times", () => {
const report = htmlvalidate.validateString(
'<input dynamic-class="foo" dynamic-class="bar">',
null,
{ processAttribute }
);
expect(report).toBeInvalid();
......
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