small qweb cleanup

This commit is contained in:
Géry Debongnie
2019-01-17 09:43:50 +01:00
parent 0f8cdfa87d
commit ddd70b8526
2 changed files with 10 additions and 16 deletions
+6 -12
View File
@@ -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)) {
+4 -4
View File
@@ -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;
}