Commit 203debef authored by David Sveningsson's avatar David Sveningsson

fix(rules): fix issue in `no-dup-id` where value is dynamic

fixes #96
parent 99685bb8
import HtmlValidate from "../htmlvalidate";
import { processAttribute } from "../transform/mocks/attribute";
import "../matchers";
describe("rule no-dup-id", () => {
......@@ -22,6 +23,24 @@ describe("rule no-dup-id", () => {
expect(report).toBeValid();
});
it("should not report error for interpolated attributes", () => {
expect.assertions(1);
const markup = '<p id="{{ interpolated }}"></p><p id="{{ interpolated }}"></p>';
const report = htmlvalidate.validateString(markup, {
processAttribute,
});
expect(report).toBeValid();
});
it("should not report error for dynamic attributes", () => {
expect.assertions(1);
const markup = '<p dynamic-id="myVariable"></p><p dynamic-id="myVariable"></p>';
const report = htmlvalidate.validateString(markup, {
processAttribute,
});
expect(report).toBeValid();
});
it("should report when id is duplicated", () => {
expect.assertions(2);
const report = htmlvalidate.validateString('<p id="foo"></p><p id="foo"></p>');
......
......@@ -14,13 +14,19 @@ export default class NoDupID extends Rule {
const existing: { [key: string]: boolean } = {};
const elements = event.document.querySelectorAll("[id]");
for (const el of elements) {
const attr = el.getAttribute("id");
/* handle when the id attribute is set but omitted value: <p id></p> */
if (!el.id) {
if (!attr.value) {
continue;
}
/* ignore id where value is dynamic */
if (attr.isDynamic) {
continue;
}
if (el.id in existing) {
const attr = el.getAttribute("id");
this.report(el, `Duplicate ID "${el.id}"`, attr.valueLocation);
}
......
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