[FIX] qweb: handle variable expressions in t-if

closes #390
closes #362
This commit is contained in:
Géry Debongnie
2019-10-23 18:33:51 +02:00
committed by aab-odoo
parent 403935a41e
commit a3317ab997
6 changed files with 37 additions and 10 deletions
+5 -5
View File
@@ -461,16 +461,16 @@ the context of the `t-foreach`, the value is copied at the end of the foreach
into the global context. into the global context.
```xml ```xml
<t t-set="existing_variable" t-value="False"/> <t t-set="existing_variable" t-value="false"/>
<!-- existing_variable now False --> <!-- existing_variable now False -->
<p t-foreach="Array(3)" t-as="i"> <p t-foreach="Array(3)" t-as="i">
<t t-set="existing_variable" t-value="True"/> <t t-set="existing_variable" t-value="true"/>
<t t-set="new_variable" t-value="True"/> <t t-set="new_variable" t-value="true"/>
<!-- existing_variable and new_variable now True --> <!-- existing_variable and new_variable now true -->
</p> </p>
<!-- existing_variable always True --> <!-- existing_variable always true -->
<!-- new_variable undefined --> <!-- new_variable undefined -->
``` ```
+2 -2
View File
@@ -133,7 +133,7 @@ QWeb.addDirective({
priority: 20, priority: 20,
atNodeEncounter({ node, ctx }): boolean { atNodeEncounter({ node, ctx }): boolean {
let cond = ctx.getValue(node.getAttribute("t-if")!); 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; return false;
}, },
finalize({ ctx }) { finalize({ ctx }) {
@@ -146,7 +146,7 @@ QWeb.addDirective({
priority: 30, priority: 30,
atNodeEncounter({ node, ctx }): boolean { atNodeEncounter({ node, ctx }): boolean {
let cond = ctx.getValue(node.getAttribute("t-elif")!); 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(); ctx.indent();
return false; return false;
}, },
+2 -2
View File
@@ -1,4 +1,4 @@
import { compileExpr, QWebVar } from "./expression_parser"; import { compileExpr, QWebVar, QWebExprVar } from "./expression_parser";
export const INTERP_REGEXP = /\{\{.*?\}\}/g; export const INTERP_REGEXP = /\{\{.*?\}\}/g;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -145,7 +145,7 @@ export class CompilationContext {
this.addLine("}"); this.addLine("}");
} }
getValue(val: any): any { getValue(val: any): QWebExprVar | string {
return val in this.variables ? this.getValue(this.variables[val]) : val; return val in this.variables ? this.getValue(this.variables[val]) : val;
} }
+1 -1
View File
@@ -645,7 +645,7 @@ export class QWeb extends EventBus {
if (name.startsWith("t-att-")) { if (name.startsWith("t-att-")) {
let attName = name.slice(6); let attName = name.slice(6);
const v = ctx.getValue(value); 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") { if (attName === "class") {
ctx.rootContext.shouldDefineUtils = true; ctx.rootContext.shouldDefineUtils = true;
@@ -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`] = ` exports[`t-key can use t-key directive on a node 1`] = `
"function anonymous(context,extra "function anonymous(context,extra
) { ) {
+11
View File
@@ -338,6 +338,17 @@ describe("t-if", () => {
qweb.addTemplate("test", `<div><t t-if="false">abc</t><t t-else="1" t-esc="'x'"/></div>`); qweb.addTemplate("test", `<div><t t-if="false">abc</t><t t-else="1" t-esc="'x'"/></div>`);
expect(renderToString(qweb, "test")).toBe("<div>x</div>"); expect(renderToString(qweb, "test")).toBe("<div>x</div>");
}); });
test("t-set, then t-if", () => {
qweb.addTemplate("test", `
<div>
<t t-set="title" t-value="'test'"/>
<t t-if="title"><t t-esc="title"/></t>
</div>`);
const result = renderToString(qweb, "test");
const expected = `<div>test</div>`;
expect(result).toBe(expected);
});
}); });
describe("attributes", () => { describe("attributes", () => {