From ddd70b8526cbb4578725e7d555834979dda7a221 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 17 Jan 2019 09:43:50 +0100 Subject: [PATCH] small qweb cleanup --- src/core/qweb.ts | 18 ++++++------------ src/core/qweb_directives.ts | 8 ++++---- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/core/qweb.ts b/src/core/qweb.ts index b096aa46..31018779 100644 --- a/src/core/qweb.ts +++ b/src/core/qweb.ts @@ -18,7 +18,7 @@ export class Context { code: string[] = []; variables: { [key: string]: any } = {}; escaping: boolean = false; - parentNode: string | undefined; + parentNode: string; indentLevel: number = 0; rootContext: Context; caller: Element | undefined; @@ -27,6 +27,7 @@ export class Context { constructor() { this.rootContext = this; this.fragmentID = this.generateID(); + this.parentNode = this.fragmentID; } generateID(): string { @@ -68,11 +69,7 @@ export class Context { } addNode(nodeID: string) { - if (this.parentNode) { - this.addLine(`${this.parentNode}.appendChild(${nodeID})`); - } else { - this.addLine(`${this.fragmentID}.appendChild(${nodeID})`); - } + this.addLine(`${this.parentNode}.appendChild(${nodeID})`); } addLine(line: string) { const prefix = new Array(this.indentLevel).join("\t"); @@ -80,6 +77,9 @@ export class Context { const suffix = lastChar !== "}" && lastChar !== "{" ? ";" : ""; this.code.push(prefix + line + suffix); } + getValue(val: any): any { + return val in this.variables ? this.getValue(this.variables[val]) : val; + } } /** @@ -298,12 +298,6 @@ export default class QWeb { } } - _getValue(val: any, ctx: Context): any { - if (val in ctx.variables) { - return this._getValue(ctx.variables[val], ctx); - } - return val; - } _compileChildren(node: ChildNode, ctx: Context) { if (node.childNodes.length > 0) { for (let child of Array.from(node.childNodes)) { diff --git a/src/core/qweb_directives.ts b/src/core/qweb_directives.ts index b330b141..2594e56d 100644 --- a/src/core/qweb_directives.ts +++ b/src/core/qweb_directives.ts @@ -61,7 +61,7 @@ const ifDirective: Directive = { name: "if", priority: 20, atNodeEncounter({ node, qweb, ctx }): boolean { - let cond = qweb._getValue(node.getAttribute("t-if")!, ctx); + let cond = ctx.getValue(node.getAttribute("t-if")!); ctx.addLine(`if (${qweb._formatExpression(cond)}) {`); ctx.indent(); return false; @@ -76,7 +76,7 @@ const elifDirective: Directive = { name: "elif", priority: 30, atNodeEncounter({ node, qweb, ctx }): boolean { - let cond = qweb._getValue(node.getAttribute("t-elif")!, ctx); + let cond = ctx.getValue(node.getAttribute("t-elif")!); ctx.addLine(`else if (${qweb._formatExpression(cond)}) {`); ctx.indent(); return false; @@ -180,7 +180,7 @@ const escDirective: Directive = { let nodeID = qweb._compileGenericNode(node, ctx); ctx = ctx.withParent(nodeID); } - let value = qweb._getValue(node.getAttribute("t-esc")!, ctx); + let value = ctx.getValue(node.getAttribute("t-esc")!); compileValueNode(value, node, qweb, ctx.withEscaping()); return true; } @@ -194,7 +194,7 @@ const rawDirective: Directive = { let nodeID = qweb._compileGenericNode(node, ctx); ctx = ctx.withParent(nodeID); } - let value = qweb._getValue(node.getAttribute("t-raw")!, ctx); + let value = ctx.getValue(node.getAttribute("t-raw")!); compileValueNode(value, node, qweb, ctx); return true; }