[FIX] qweb: properly calls directive finalizers

closes #382
This commit is contained in:
Géry Debongnie
2019-10-22 20:20:07 +02:00
committed by aab-odoo
parent da6c24bbca
commit be556a970e
5 changed files with 56 additions and 12 deletions
-4
View File
@@ -511,10 +511,6 @@ QWeb.addDirective({
ctx.addLine(`extra.promises.push(def${defID});`); ctx.addLine(`extra.promises.push(def${defID});`);
} }
if (node.hasAttribute("t-if") || node.hasAttribute("t-else") || node.hasAttribute("t-elif")) {
ctx.closeIf();
}
return true; return true;
} }
}); });
-4
View File
@@ -258,10 +258,6 @@ QWeb.addDirective({
ctx.addLine("}"); ctx.addLine("}");
} }
if (node.hasAttribute("t-if") || node.hasAttribute("t-else") || node.hasAttribute("t-elif")) {
ctx.closeIf();
}
return true; return true;
} }
}); });
+11 -4
View File
@@ -452,6 +452,8 @@ export class QWeb extends EventBus {
fullName: string; fullName: string;
}[] = []; }[] = [];
const finalizers: typeof validDirectives = [];
// maybe this is not optimal: we iterate on all attributes here, and again // maybe this is not optimal: we iterate on all attributes here, and again
// just after for each directive. // just after for each directive.
for (let i = 0; i < attributes.length; i++) { for (let i = 0; i < attributes.length; i++) {
@@ -496,7 +498,11 @@ export class QWeb extends EventBus {
} }
} }
} }
for (let { directive, value, fullName } of validDirectives) { for (let { directive, value, fullName } of validDirectives) {
if (directive.finalize) {
finalizers.push({ directive, value, fullName });
}
if (directive.atNodeEncounter) { if (directive.atNodeEncounter) {
const isDone = directive.atNodeEncounter({ const isDone = directive.atNodeEncounter({
node, node,
@@ -506,6 +512,9 @@ export class QWeb extends EventBus {
value value
}); });
if (isDone) { if (isDone) {
for (let { directive, value, fullName } of finalizers) {
directive.finalize!({ node, qweb: this, ctx, fullName, value });
}
return; return;
} }
} }
@@ -564,10 +573,8 @@ export class QWeb extends EventBus {
ctx.addLine(`utils.addNameSpace(vn${ctx.parentNode});`); ctx.addLine(`utils.addNameSpace(vn${ctx.parentNode});`);
} }
for (let { directive, value, fullName } of validDirectives) { for (let { directive, value, fullName } of finalizers) {
if (directive.finalize) { directive.finalize!({ node, qweb: this, ctx, fullName, value });
directive.finalize({ node, qweb: this, ctx, fullName, value });
}
} }
} }
@@ -1467,6 +1467,41 @@ exports[`t-if can use some boolean operators in expressions 1`] = `
}" }"
`; `;
exports[`t-if t-esc with t-elif 1`] = `
"function anonymous(context,extra
) {
var h = this.h;
let c1 = [], p1 = {key:1};
var vn1 = h('div', p1, c1);
if (false) {
c1.push({text: \`abc\`});
}
else {
var _2 = 'x';
if (_2 || _2 === 0) {
c1.push({text: _2});
}
}
return vn1;
}"
`;
exports[`t-if t-esc with t-if 1`] = `
"function anonymous(context,extra
) {
var h = this.h;
let c1 = [], p1 = {key:1};
var vn1 = h('div', p1, c1);
if (true) {
var _2 = 'x';
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
) { ) {
+10
View File
@@ -328,6 +328,16 @@ describe("t-if", () => {
}; };
expect(normalize(renderToString(qweb, "test", context))).toBe("<div>andormgtnlt</div>"); expect(normalize(renderToString(qweb, "test", context))).toBe("<div>andormgtnlt</div>");
}); });
test("t-esc with t-if", () => {
qweb.addTemplate("test", `<div><t t-if="true" t-esc="'x'"/></div>`);
expect(renderToString(qweb, "test")).toBe("<div>x</div>");
});
test("t-esc with t-elif", () => {
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>");
});
}); });
describe("attributes", () => { describe("attributes", () => {