Commit 5b6991b6 authored by David Sveningsson's avatar David Sveningsson

fix: allow both null and empty string when attribute allows empty values

parent 74e88667
......@@ -25,6 +25,9 @@
"name",
"urn"
],
"attributes": {
"download": ["", "/.+/"]
},
"permittedDescendants": [{ "exclude": "@interactive" }]
},
......
......@@ -589,6 +589,18 @@ describe("Meta validator", () => {
).toBeTruthy();
});
it("should consider empty value as either null or empty string", () => {
const rules = {
foo: [""] as string[],
};
expect(
Validator.validateAttribute(new Attribute("foo", null), rules)
).toBeTruthy();
expect(
Validator.validateAttribute(new Attribute("foo", ""), rules)
).toBeTruthy();
});
it("should normalize boolean attributes", () => {
const rules = {
foo: [] as string[],
......
......@@ -183,10 +183,17 @@ export class Validator {
if (value instanceof DynamicValue) {
return true;
}
const empty = value === null || value === "";
/* consider an empty array as being a boolean attribute */
if (rule.length === 0) {
return value === null || value === "" || value === attr.key;
return empty || value === attr.key;
}
/* if the empty string is present allow both "" and null
* (boolean-attribute-style will regulate which is allowed) */
if (rule.includes("") && empty) {
return true;
}
return rule.some((entry: string | RegExp) => {
......
......@@ -64,6 +64,21 @@ describe("rule attribute-allowed-values", () => {
expect(report).toBeValid();
});
it("should not report error when element allows empty value and attribute is null", () => {
const report = htmlvalidate.validateString("<a download>");
expect(report).toBeValid();
});
it("should not report error when element allows empty value and attribute is empty string", () => {
const report = htmlvalidate.validateString('<a download="">');
expect(report).toBeValid();
});
it("should not report error when element allows empty and other values and attribute is non-empty string", () => {
const report = htmlvalidate.validateString('<a download="foobar">');
expect(report).toBeValid();
});
it("smoketest", () => {
const report = htmlvalidate.validateFile(
"test-files/rules/attribute-allowed-values.html"
......
......@@ -9,3 +9,8 @@
<!-- should be transparent -->
<div><a><div>foo</div></a></div>
<span><a><span>foo</span></a></span>
<!-- should allow download attribute with or without value -->
<a download>foo</a>
<a download="">foo</a>
<a download="foo.txtr">foo</a>
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