From d654332535252b2e2197152100f065f5fde1fcfb Mon Sep 17 00:00:00 2001 From: Mathieu Duckerts-Antoine Date: Wed, 10 Jan 2024 16:49:32 +0100 Subject: [PATCH] [IMP] template set config: getTemplate function --- src/runtime/template_set.ts | 5 +- .../__snapshots__/template_set.test.ts.snap | 52 ++++++++++++++++ tests/compiler/template_set.test.ts | 60 +++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) diff --git a/src/runtime/template_set.ts b/src/runtime/template_set.ts index 5cee95867..f87c68da8 100644 --- a/src/runtime/template_set.ts +++ b/src/runtime/template_set.ts @@ -13,6 +13,7 @@ export interface TemplateSetConfig { translatableAttributes?: string[]; translateFn?: (s: string) => string; templates?: string | Document | Record; + getTemplate?: (s: string) => (Element|string|null); } export class TemplateSet { @@ -22,6 +23,7 @@ export class TemplateSet { dev: boolean; rawTemplates: typeof globalTemplates = Object.create(globalTemplates); templates: { [name: string]: Template } = {}; + getRawTemplate?: (s: string) => (Element|string|null); translateFn?: (s: string) => string; translatableAttributes?: string[]; Portal = Portal; @@ -39,6 +41,7 @@ export class TemplateSet { } } } + this.getRawTemplate = config.getTemplate; } addTemplate(name: string, template: string | Element) { @@ -77,7 +80,7 @@ export class TemplateSet { getTemplate(name: string): Template { if (!(name in this.templates)) { - const rawTemplate = this.rawTemplates[name]; + const rawTemplate = this.getRawTemplate?.(name) || this.rawTemplates[name]; if (rawTemplate === undefined) { let extraInfo = ""; try { diff --git a/tests/compiler/__snapshots__/template_set.test.ts.snap b/tests/compiler/__snapshots__/template_set.test.ts.snap index 370787a8c..1817c054d 100644 --- a/tests/compiler/__snapshots__/template_set.test.ts.snap +++ b/tests/compiler/__snapshots__/template_set.test.ts.snap @@ -101,3 +101,55 @@ exports[`loading templates can load a few templates from an XMLDocument 2`] = ` } }" `; + +exports[`loading templates function getTemplate in config 1`] = ` +"function anonymous(app, bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, comment } = bdom; + + let block1 = createBlock(\`
Hello World!
\`); + + return function template(ctx, node, key = \\"\\") { + return block1(); + } +}" +`; + +exports[`loading templates function getTemplate in config 2`] = ` +"function anonymous(app, bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, comment } = bdom; + + let block1 = createBlock(\`
Hello World!
\`); + + return function template(ctx, node, key = \\"\\") { + return block1(); + } +}" +`; + +exports[`loading templates function getTemplate in config 3`] = ` +"function anonymous(app, bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, comment } = bdom; + + let block1 = createBlock(\`
Hello World!
\`); + + return function template(ctx, node, key = \\"\\") { + return block1(); + } +}" +`; + +exports[`loading templates function getTemplate in config 4`] = ` +"function anonymous(app, bdom, helpers +) { + let { text, createBlock, list, multi, html, toggler, comment } = bdom; + + let block1 = createBlock(\`
Hello World!
\`); + + return function template(ctx, node, key = \\"\\") { + return block1(); + } +}" +`; diff --git a/tests/compiler/template_set.test.ts b/tests/compiler/template_set.test.ts index 731cb7f36..8da199ebd 100644 --- a/tests/compiler/template_set.test.ts +++ b/tests/compiler/template_set.test.ts @@ -78,4 +78,64 @@ describe("loading templates", () => { context.addTemplates(xml); expect(Object.keys(context.rawTemplates)).toEqual([]); }); + + test("function getTemplate in config", () => { + const context = new TestContext({ + getTemplate: (name) => { + if (name === "main") { + const data = `
Hello World!
`; + const xml = new DOMParser().parseFromString(data, "text/xml"); + return xml.firstChild as Element; + } + return null; + } + }); + const result = context.renderToString("main"); + expect(result).toBe("
Hello World!
"); + }); + + test("function getTemplate in config", () => { + const context = new TestContext({ + getTemplate: (name) => { + if (name === "main") { + const doc = new Document(); + const div = doc.createElement("div") as Element; + div.append(doc.createTextNode("Hello World!")); + return div; + } + return null; + } + }); + const result = context.renderToString("main"); + expect(result).toBe("
Hello World!
"); + }); + + test("function getTemplate in config", () => { + const context = new TestContext({ + getTemplate: (name) => { + if (name === "main") { + return `
Hello World!
` + } + return null; + } + }); + const result = context.renderToString("main"); + expect(result).toBe("
Hello World!
"); + }); + + test("function getTemplate in config", () => { + const context = new TestContext({ + getTemplate: () => { + return null; + } + }); + const data = ` + +
Hello World!
+
`; + const xml = new DOMParser().parseFromString(data, "text/xml"); + context.addTemplates(xml); + const result = context.renderToString("main"); + expect(result).toBe("
Hello World!
"); + }); });