qweb: only protect context if necessary

This commit is contained in:
Géry Debongnie
2019-01-26 23:21:40 +01:00
parent 5496935c61
commit 19ce38f29d
+5 -1
View File
@@ -28,6 +28,7 @@ export class Context {
rootContext: Context;
caller: Element | undefined;
shouldDefineOwner: boolean = false;
shouldProtectContext: boolean = false;
constructor() {
this.rootContext = this;
@@ -199,7 +200,6 @@ export class QWeb {
const doc = this.parsedTemplates[name];
const ctx = new Context();
ctx.addLine("context = Object.create(context);");
const mainNode = doc.firstChild!;
this._compileNode(mainNode, ctx);
@@ -208,6 +208,9 @@ export class QWeb {
// pollute the rendering context by adding some keys in it.
ctx.code.unshift("let owner = context;");
}
if (ctx.shouldProtectContext) {
ctx.code.unshift("context = Object.create(context);");
}
if (!ctx.rootNode) {
throw new Error("A template should have one root node");
@@ -620,6 +623,7 @@ const forEachDirective: Directive = {
name: "foreach",
priority: 10,
atNodeEncounter({ node, qweb, ctx }): boolean {
ctx.rootContext.shouldProtectContext = true;
const elems = node.getAttribute("t-foreach")!;
const name = node.getAttribute("t-as")!;
let arrayID = ctx.generateID();