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

fix(rules): `no-dup-id` handles when id is set but omitted value

parent 7f27c8b6
......@@ -17,6 +17,11 @@ describe("rule no-dup-id", () => {
expect(report).toBeValid();
});
it("should not report when id is missing value", () => {
const report = htmlvalidate.validateString("<hr id><hr id>");
expect(report).toBeValid();
});
it("should report when id is duplicated", () => {
const report = htmlvalidate.validateString(
'<p id="foo"></p><p id="foo"></p>'
......
......@@ -13,13 +13,19 @@ class NoDupID extends Rule {
this.on("dom:ready", (event: DOMReadyEvent) => {
const existing: { [key: string]: boolean } = {};
const elements = event.document.querySelectorAll("[id]");
elements.forEach(el => {
for (const el of elements) {
/* handle when the id attribute is set but omitted value: <p id></p> */
if (!el.id) {
continue;
}
if (el.id in existing) {
const attr = el.getAttribute("id");
this.report(el, `Duplicate ID "${el.id}"`, attr.valueLocation);
}
existing[el.id] = true;
});
}
});
}
}
......
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