From a3317ab9970c04dd272a6a54a8d62ed9497022d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 23 Oct 2019 18:33:51 +0200 Subject: [PATCH] [FIX] qweb: handle variable expressions in t-if closes #390 closes #362 --- doc/qweb.md | 10 +++++----- src/qweb/base_directives.ts | 4 ++-- src/qweb/compilation_context.ts | 4 ++-- src/qweb/qweb.ts | 2 +- tests/qweb/__snapshots__/qweb.test.ts.snap | 16 ++++++++++++++++ tests/qweb/qweb.test.ts | 11 +++++++++++ 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/doc/qweb.md b/doc/qweb.md index 918c5ec8..99ac5d85 100644 --- a/doc/qweb.md +++ b/doc/qweb.md @@ -461,16 +461,16 @@ the context of the `t-foreach`, the value is copied at the end of the foreach into the global context. ```xml - +

- - - + + +

- + ``` diff --git a/src/qweb/base_directives.ts b/src/qweb/base_directives.ts index a44e6e32..804236c3 100644 --- a/src/qweb/base_directives.ts +++ b/src/qweb/base_directives.ts @@ -133,7 +133,7 @@ QWeb.addDirective({ priority: 20, atNodeEncounter({ node, ctx }): boolean { let cond = ctx.getValue(node.getAttribute("t-if")!); - ctx.addIf(`${ctx.formatExpression(cond)}`); + ctx.addIf(`${ctx.formatExpression(typeof cond === 'string' ? cond : cond.expr)}`); return false; }, finalize({ ctx }) { @@ -146,7 +146,7 @@ QWeb.addDirective({ priority: 30, atNodeEncounter({ node, ctx }): boolean { let cond = ctx.getValue(node.getAttribute("t-elif")!); - ctx.addLine(`else if (${ctx.formatExpression(cond)}) {`); + ctx.addLine(`else if (${ctx.formatExpression(typeof cond === 'string' ? cond : cond.expr)}) {`); ctx.indent(); return false; }, diff --git a/src/qweb/compilation_context.ts b/src/qweb/compilation_context.ts index a95615e0..e6f00a78 100644 --- a/src/qweb/compilation_context.ts +++ b/src/qweb/compilation_context.ts @@ -1,4 +1,4 @@ -import { compileExpr, QWebVar } from "./expression_parser"; +import { compileExpr, QWebVar, QWebExprVar } from "./expression_parser"; export const INTERP_REGEXP = /\{\{.*?\}\}/g; //------------------------------------------------------------------------------ @@ -145,7 +145,7 @@ export class CompilationContext { this.addLine("}"); } - getValue(val: any): any { + getValue(val: any): QWebExprVar | string { return val in this.variables ? this.getValue(this.variables[val]) : val; } diff --git a/src/qweb/qweb.ts b/src/qweb/qweb.ts index b638bada..e54ac6b1 100644 --- a/src/qweb/qweb.ts +++ b/src/qweb/qweb.ts @@ -645,7 +645,7 @@ export class QWeb extends EventBus { if (name.startsWith("t-att-")) { let attName = name.slice(6); const v = ctx.getValue(value); - let formattedValue = v.id || ctx.formatExpression(v); + let formattedValue = typeof v === 'string' ? ctx.formatExpression(v) : v.id; if (attName === "class") { ctx.rootContext.shouldDefineUtils = true; diff --git a/tests/qweb/__snapshots__/qweb.test.ts.snap b/tests/qweb/__snapshots__/qweb.test.ts.snap index b7767f3d..0050e242 100644 --- a/tests/qweb/__snapshots__/qweb.test.ts.snap +++ b/tests/qweb/__snapshots__/qweb.test.ts.snap @@ -1502,6 +1502,22 @@ exports[`t-if t-esc with t-if 1`] = ` }" `; +exports[`t-if t-set, then t-if 1`] = ` +"function anonymous(context,extra +) { + var h = this.h; + let c1 = [], p1 = {key:1}; + var vn1 = h('div', p1, c1); + var _2 = 'test'; + if ('test') { + if (_2 || _2 === 0) { + c1.push({text: _2}); + } + } + return vn1; +}" +`; + exports[`t-key can use t-key directive on a node 1`] = ` "function anonymous(context,extra ) { diff --git a/tests/qweb/qweb.test.ts b/tests/qweb/qweb.test.ts index e3e62969..fa902500 100644 --- a/tests/qweb/qweb.test.ts +++ b/tests/qweb/qweb.test.ts @@ -338,6 +338,17 @@ describe("t-if", () => { qweb.addTemplate("test", `
abc
`); expect(renderToString(qweb, "test")).toBe("
x
"); }); + + test("t-set, then t-if", () => { + qweb.addTemplate("test", ` +
+ + +
`); + const result = renderToString(qweb, "test"); + const expected = `
test
`; + expect(result).toBe(expected); + }); }); describe("attributes", () => {