diff --git a/src/app/template_set.ts b/src/app/template_set.ts index c043f19a..a7e5e394 100644 --- a/src/app/template_set.ts +++ b/src/app/template_set.ts @@ -79,18 +79,20 @@ export class TemplateSet { this.helpers = makeHelpers(this.getTemplate.bind(this)); } - addTemplate( - name: string, - template: string | Element, - options: { allowDuplicate?: boolean } = {} - ) { - if (name in this.rawTemplates && !options.allowDuplicate) { - throw new Error(`Template ${name} already defined`); + addTemplate(name: string, template: string | Element) { + if (name in this.rawTemplates) { + const rawTemplate = this.rawTemplates[name]; + const currentAsString = typeof rawTemplate === "string" ? rawTemplate : rawTemplate.outerHTML; + const newAsString = typeof template === "string" ? template : template.outerHTML; + if (currentAsString === newAsString) { + return; + } + throw new Error(`Template ${name} already defined with different content`); } this.rawTemplates[name] = template; } - addTemplates(xml: string | Document, options: { allowDuplicate?: boolean } = {}) { + addTemplates(xml: string | Document) { if (!xml) { // empty string return; @@ -98,7 +100,7 @@ export class TemplateSet { xml = xml instanceof Document ? xml : parseXML(xml); for (const template of xml.querySelectorAll("[t-name]")) { const name = template.getAttribute("t-name")!; - this.addTemplate(name, template, options); + this.addTemplate(name, template); } } diff --git a/tests/compiler/error_handling.test.ts b/tests/compiler/error_handling.test.ts index 4371b08c..f9cebdfd 100644 --- a/tests/compiler/error_handling.test.ts +++ b/tests/compiler/error_handling.test.ts @@ -12,15 +12,6 @@ describe("error handling", () => { expect(() => context.renderToString("invalidname")).toThrow("Missing template"); }); - test("cannot add twice the same template", () => { - const context = new TestContext(); - context.addTemplate("test", ``); - expect(() => context.addTemplate("test", "
", { allowDuplicate: true })).not.toThrow( - "already defined" - ); - expect(() => context.addTemplate("test", "
")).toThrow("already defined"); - }); - test("addTemplates throw if parser error", () => { const context = new TestContext(); expect(() => { diff --git a/tests/compiler/validation.test.ts b/tests/compiler/validation.test.ts index 9c3499a6..98531121 100644 --- a/tests/compiler/validation.test.ts +++ b/tests/compiler/validation.test.ts @@ -11,11 +11,12 @@ describe("basic validation", () => { expect(() => context.getTemplate("invalidname")).toThrow("Missing template"); }); - test("cannot add twice the same template", () => { + test("cannot add a different template with the same name", () => { const context = new TemplateSet(); - expect(() => context.addTemplate("test", "
", { allowDuplicate: true })).not.toThrow( - "already defined" - ); + context.addTemplate("test", ``); + // Same template with the same name is fine + expect(() => context.addTemplate("test", "")).not.toThrow(); + // Different template with the same name crashes expect(() => context.addTemplate("test", "
")).toThrow("already defined"); });