From 78a4550ebbece78bcd310b23cc9fe53392bd1c31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sat, 21 Sep 2019 23:05:44 +0200 Subject: [PATCH] [IMP] component tags: allow injecting values in xml tag string With this commit, we can now do this: const SUBTEMPLATE = xml`` class Parent extends Widget { static template = xml`
` state = {n: 42}; } --- src/tags.ts | 5 +++-- .../__snapshots__/component.test.ts.snap | 18 ++++++++++++++++++ tests/component/component.test.ts | 13 +++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/tags.ts b/src/tags.ts index 95675b56..630db926 100644 --- a/src/tags.ts +++ b/src/tags.ts @@ -19,8 +19,9 @@ import { QWeb } from "./qweb/index"; * } * ``` */ -export function xml(strings) { +export function xml(strings, ...args) { const name = `__template__${QWeb.nextId++}`; - QWeb.registerTemplate(name, strings[0]); + const value = String.raw(strings, ...args); + QWeb.registerTemplate(name, value); return name; } diff --git a/tests/component/__snapshots__/component.test.ts.snap b/tests/component/__snapshots__/component.test.ts.snap index f289b633..cf98c4bb 100644 --- a/tests/component/__snapshots__/component.test.ts.snap +++ b/tests/component/__snapshots__/component.test.ts.snap @@ -1068,6 +1068,24 @@ exports[`other directives with t-component t-on with stop and/or prevent modifie }" `; +exports[`random stuff/miscellaneous can inject values in tagged templates 1`] = ` +"function anonymous(context,extra +) { + var h = this.h; + let c1 = [], p1 = {key:1}; + var vn1 = h('div', p1, c1); + result = vn1; + let c2 = [], p2 = {key:2}; + var vn2 = h('span', p2, c2); + c1.push(vn2); + var _3 = context['state'].n; + if (_3 || _3 === 0) { + c2.push({text: _3}); + } + return vn1; +}" +`; + exports[`random stuff/miscellaneous snapshotting compiled code 1`] = ` "function anonymous(context,extra ) { diff --git a/tests/component/component.test.ts b/tests/component/component.test.ts index 027bf327..a13c5485 100644 --- a/tests/component/component.test.ts +++ b/tests/component/component.test.ts @@ -2310,6 +2310,19 @@ describe("random stuff/miscellaneous", () => { "C:patched" ]); }); + + test("can inject values in tagged templates", async () => { + const SUBTEMPLATE = xml`` + class Parent extends Widget { + static template = xml`
` + state = {n: 42}; + } + + const widget = new Parent(env); + await widget.mount(fixture); + expect(env.qweb.templates[Parent.template].fn.toString()).toMatchSnapshot(); + expect(fixture.innerHTML).toBe('
42
') + }); }); describe("async rendering", () => {