[FIX] qweb: do not format expression twice in t-if

closes #392
This commit is contained in:
Géry Debongnie
2019-10-24 13:25:18 +02:00
committed by aab-odoo
parent a3317ab997
commit c2f42d3b82
3 changed files with 71 additions and 3 deletions
+2 -2
View File
@@ -133,7 +133,7 @@ QWeb.addDirective({
priority: 20,
atNodeEncounter({ node, ctx }): boolean {
let cond = ctx.getValue(node.getAttribute("t-if")!);
ctx.addIf(`${ctx.formatExpression(typeof cond === 'string' ? cond : cond.expr)}`);
ctx.addIf(typeof cond === 'string' ? ctx.formatExpression(cond) : cond.id);
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(typeof cond === 'string' ? cond : cond.expr)}) {`);
ctx.addLine(`else if (${typeof cond === 'string' ? ctx.formatExpression(cond) : cond.id}) {`);
ctx.indent();
return false;
},
+43 -1
View File
@@ -1502,6 +1502,30 @@ exports[`t-if t-esc with t-if 1`] = `
}"
`;
exports[`t-if t-set, then t-elif, part 3 1`] = `
"function anonymous(context,extra
) {
var h = this.h;
let c1 = [], p1 = {key:1};
var vn1 = h('div', p1, c1);
var _2 = false;
var _3 = _2;
if (_3) {
let c4 = [], p4 = {key:4};
var vn4 = h('span', p4, c4);
c1.push(vn4);
c4.push({text: \`AAA\`});
}
else if (!_3) {
let c5 = [], p5 = {key:5};
var vn5 = h('span', p5, c5);
c1.push(vn5);
c5.push({text: \`BBB\`});
}
return vn1;
}"
`;
exports[`t-if t-set, then t-if 1`] = `
"function anonymous(context,extra
) {
@@ -1509,7 +1533,7 @@ exports[`t-if t-set, then t-if 1`] = `
let c1 = [], p1 = {key:1};
var vn1 = h('div', p1, c1);
var _2 = 'test';
if ('test') {
if (_2) {
if (_2 || _2 === 0) {
c1.push({text: _2});
}
@@ -1518,6 +1542,24 @@ exports[`t-if t-set, then t-if 1`] = `
}"
`;
exports[`t-if t-set, then t-if, part 2 1`] = `
"function anonymous(context,extra
) {
var h = this.h;
let c1 = [], p1 = {key:1};
var vn1 = h('div', p1, c1);
var _2 = true;
var _3 = _2;
if (_3) {
let c4 = [], p4 = {key:4};
var vn4 = h('span', p4, c4);
c1.push(vn4);
c4.push({text: \`COUCOU\`});
}
return vn1;
}"
`;
exports[`t-key can use t-key directive on a node 1`] = `
"function anonymous(context,extra
) {
+26
View File
@@ -349,6 +349,32 @@ describe("t-if", () => {
const expected = `<div>test</div>`;
expect(result).toBe(expected);
});
test("t-set, then t-if, part 2", () => {
qweb.addTemplate("test", `
<div>
<t t-set="y" t-value="true"/>
<t t-set="x" t-value="y"/>
<span t-if="x">COUCOU</span>
</div>`);
const result = renderToString(qweb, "test");
const expected = `<div><span>COUCOU</span></div>`;
expect(result).toBe(expected);
});
test("t-set, then t-elif, part 3", () => {
qweb.addTemplate("test", `
<div>
<t t-set="y" t-value="false"/>
<t t-set="x" t-value="y"/>
<span t-if="x">AAA</span>
<span t-elif="!x">BBB</span>
</div>`);
const result = renderToString(qweb, "test");
const expected = `<div><span>BBB</span></div>`;
expect(result).toBe(expected);
});
});
describe("attributes", () => {