mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
+18
-14
@@ -1,5 +1,4 @@
|
|||||||
import { CompilationContext } from "./compilation_context";
|
import { CompilationContext } from "./compilation_context";
|
||||||
import { QWebExprVar } from "./expression_parser";
|
|
||||||
import { QWeb } from "./qweb";
|
import { QWeb } from "./qweb";
|
||||||
import { htmlToVDOM } from "../vdom/html_to_vdom";
|
import { htmlToVDOM } from "../vdom/html_to_vdom";
|
||||||
|
|
||||||
@@ -34,7 +33,7 @@ function compileValueNode(value: any, node: Element, qweb: QWeb, ctx: Compilatio
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value.xml instanceof NodeList) {
|
if (value.xml instanceof NodeList && !value.id) {
|
||||||
for (let node of Array.from(value.xml)) {
|
for (let node of Array.from(value.xml)) {
|
||||||
qweb._compileNode(<ChildNode>node, ctx);
|
qweb._compileNode(<ChildNode>node, ctx);
|
||||||
}
|
}
|
||||||
@@ -71,6 +70,12 @@ function compileValueNode(value: any, node: Element, qweb: QWeb, ctx: Compilatio
|
|||||||
qweb._compileChildren(node, ctx);
|
qweb._compileChildren(node, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (value.xml instanceof NodeList && value.id) {
|
||||||
|
ctx.addElse();
|
||||||
|
for (let node of Array.from(value.xml)) {
|
||||||
|
qweb._compileNode(<ChildNode>node, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
ctx.closeIf();
|
ctx.closeIf();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,22 +109,21 @@ QWeb.addDirective({
|
|||||||
atNodeEncounter({ node, ctx }): boolean {
|
atNodeEncounter({ node, ctx }): boolean {
|
||||||
const variable = node.getAttribute("t-set")!;
|
const variable = node.getAttribute("t-set")!;
|
||||||
let value = node.getAttribute("t-value")!;
|
let value = node.getAttribute("t-value")!;
|
||||||
|
ctx.variables[variable] = ctx.variables[variable] || {};
|
||||||
|
let qwebvar = ctx.variables[variable];
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
const formattedValue = ctx.formatExpression(value);
|
const formattedValue = ctx.formatExpression(value);
|
||||||
if (ctx.variables.hasOwnProperty(variable)) {
|
if (ctx.variables.hasOwnProperty(variable) && qwebvar.id) {
|
||||||
ctx.addLine(`${(<QWebExprVar>ctx.variables[variable]).id} = ${formattedValue}`);
|
ctx.addLine(`${qwebvar.id} = ${formattedValue}`);
|
||||||
} else {
|
} else {
|
||||||
const varName = `_${ctx.generateID()}`;
|
const varName = `_${ctx.generateID()}`;
|
||||||
ctx.addLine(`var ${varName} = ${formattedValue};`);
|
ctx.addLine(`var ${varName} = ${formattedValue};`);
|
||||||
ctx.variables[variable] = {
|
qwebvar.id = varName;
|
||||||
id: varName,
|
qwebvar.expr = formattedValue;
|
||||||
expr: formattedValue
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ctx.variables[variable] = {
|
qwebvar.xml = node.childNodes;
|
||||||
xml: node.childNodes
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -133,7 +137,7 @@ QWeb.addDirective({
|
|||||||
priority: 20,
|
priority: 20,
|
||||||
atNodeEncounter({ node, ctx }): boolean {
|
atNodeEncounter({ node, ctx }): boolean {
|
||||||
let cond = ctx.getValue(node.getAttribute("t-if")!);
|
let cond = ctx.getValue(node.getAttribute("t-if")!);
|
||||||
ctx.addIf(typeof cond === "string" ? ctx.formatExpression(cond) : cond.id);
|
ctx.addIf(typeof cond === "string" ? ctx.formatExpression(cond) : cond.id!);
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
finalize({ ctx }) {
|
finalize({ ctx }) {
|
||||||
@@ -244,8 +248,8 @@ QWeb.addDirective({
|
|||||||
// add new variables, if any
|
// add new variables, if any
|
||||||
for (let key in tempCtx.variables) {
|
for (let key in tempCtx.variables) {
|
||||||
const v = tempCtx.variables[key];
|
const v = tempCtx.variables[key];
|
||||||
if ((<QWebExprVar>v).expr) {
|
if (v.expr) {
|
||||||
ctx.addLine(`let ${(<QWebExprVar>v).id} = ${(<QWebExprVar>v).expr};`);
|
ctx.addLine(`let ${v.id} = ${v.expr};`);
|
||||||
}
|
}
|
||||||
// todo: handle XML variables...
|
// todo: handle XML variables...
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { compileExpr, QWebVar, QWebExprVar } from "./expression_parser";
|
import { compileExpr, QWebVar } from "./expression_parser";
|
||||||
|
|
||||||
export const INTERP_REGEXP = /\{\{.*?\}\}/g;
|
export const INTERP_REGEXP = /\{\{.*?\}\}/g;
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@@ -149,7 +149,7 @@ export class CompilationContext {
|
|||||||
this.addLine("}");
|
this.addLine("}");
|
||||||
}
|
}
|
||||||
|
|
||||||
getValue(val: any): QWebExprVar | string {
|
getValue(val: any): QWebVar | string {
|
||||||
return val in this.variables ? this.getValue(this.variables[val]) : val;
|
return val in this.variables ? this.getValue(this.variables[val]) : val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,17 +38,12 @@ const WORD_REPLACEMENT = {
|
|||||||
lte: "<="
|
lte: "<="
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface QWebExprVar {
|
export interface QWebVar {
|
||||||
id: string;
|
id?: string;
|
||||||
expr: string;
|
expr?: string;
|
||||||
|
xml?: NodeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QWebXMLVar {
|
|
||||||
xml: NodeList;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type QWebVar = QWebExprVar | QWebXMLVar;
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Tokenizer
|
// Tokenizer
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@@ -252,7 +247,7 @@ export function compileExpr(expr: string, vars: { [key: string]: QWebVar }): str
|
|||||||
}
|
}
|
||||||
if (isVar) {
|
if (isVar) {
|
||||||
if (token.value in vars && "id" in vars[token.value]) {
|
if (token.value in vars && "id" in vars[token.value]) {
|
||||||
token.value = (<QWebExprVar>vars[token.value]).id;
|
token.value = vars[token.value].id!;
|
||||||
} else {
|
} else {
|
||||||
token.value = `context['${token.value}']`;
|
token.value = `context['${token.value}']`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -699,25 +699,25 @@ exports[`misc global 1`] = `
|
|||||||
} else {
|
} else {
|
||||||
c17.push({text: \`foo default\`});
|
c17.push({text: \`foo default\`});
|
||||||
}
|
}
|
||||||
var _19 = 'bbb';
|
_11 = 'bbb'
|
||||||
let c20 = [], p20 = {key:20};
|
let c19 = [], p19 = {key:19};
|
||||||
var vn20 = h('span', p20, c20);
|
var vn19 = h('span', p19, c19);
|
||||||
c14.push(vn20);
|
c14.push(vn19);
|
||||||
if (_19 || _19 === 0) {
|
if (_11 || _11 === 0) {
|
||||||
c20.push({text: _19});
|
c19.push({text: _11});
|
||||||
} else {
|
} else {
|
||||||
c20.push({text: \`foo default\`});
|
c19.push({text: \`foo default\`});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let c21 = [], p21 = {key:21};
|
let c20 = [], p20 = {key:20};
|
||||||
var vn21 = h('div', p21, c21);
|
var vn20 = h('div', p20, c20);
|
||||||
c1.push(vn21);
|
c1.push(vn20);
|
||||||
var _22 = context['toto'];
|
var _21 = context['toto'];
|
||||||
if (_22 || _22 === 0) {
|
if (_21 || _21 === 0) {
|
||||||
c21.push(...utils.htmlToVDOM(_22));
|
c20.push(...utils.htmlToVDOM(_21));
|
||||||
} else {
|
} else {
|
||||||
c21.push({text: \`toto default\`});
|
c20.push({text: \`toto default\`});
|
||||||
}
|
}
|
||||||
return vn1;
|
return vn1;
|
||||||
}"
|
}"
|
||||||
@@ -2400,6 +2400,90 @@ exports[`t-set t-set should reuse variable if possible 1`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`t-set t-set, t-if, and mix of expression/body lookup, 1 1`] = `
|
||||||
|
"function anonymous(context,extra
|
||||||
|
) {
|
||||||
|
let sibling = null;
|
||||||
|
var h = this.h;
|
||||||
|
let c1 = [], p1 = {key:1};
|
||||||
|
var vn1 = h('div', p1, c1);
|
||||||
|
if (context['flag']) {
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var _2 = 0;
|
||||||
|
}
|
||||||
|
if (_2 || _2 === 0) {
|
||||||
|
c1.push({text: _2});
|
||||||
|
} else {
|
||||||
|
c1.push({text: \`1\`});
|
||||||
|
}
|
||||||
|
return vn1;
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`t-set t-set, t-if, and mix of expression/body lookup, 1 2`] = `
|
||||||
|
"function anonymous(context,extra
|
||||||
|
) {
|
||||||
|
let sibling = null;
|
||||||
|
var h = this.h;
|
||||||
|
let c1 = [], p1 = {key:1};
|
||||||
|
var vn1 = h('div', p1, c1);
|
||||||
|
if (context['flag']) {
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var _2 = 0;
|
||||||
|
}
|
||||||
|
if (_2 || _2 === 0) {
|
||||||
|
c1.push({text: _2});
|
||||||
|
} else {
|
||||||
|
c1.push({text: \`1\`});
|
||||||
|
}
|
||||||
|
return vn1;
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`t-set t-set, t-if, and mix of expression/body lookup, 2 1`] = `
|
||||||
|
"function anonymous(context,extra
|
||||||
|
) {
|
||||||
|
let sibling = null;
|
||||||
|
var h = this.h;
|
||||||
|
let c1 = [], p1 = {key:1};
|
||||||
|
var vn1 = h('div', p1, c1);
|
||||||
|
if (context['flag']) {
|
||||||
|
var _2 = 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
}
|
||||||
|
if (_2 || _2 === 0) {
|
||||||
|
c1.push({text: _2});
|
||||||
|
} else {
|
||||||
|
c1.push({text: \`0\`});
|
||||||
|
}
|
||||||
|
return vn1;
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`t-set t-set, t-if, and mix of expression/body lookup, 2 2`] = `
|
||||||
|
"function anonymous(context,extra
|
||||||
|
) {
|
||||||
|
let sibling = null;
|
||||||
|
var h = this.h;
|
||||||
|
let c1 = [], p1 = {key:1};
|
||||||
|
var vn1 = h('div', p1, c1);
|
||||||
|
if (context['flag']) {
|
||||||
|
var _2 = 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
}
|
||||||
|
if (_2 || _2 === 0) {
|
||||||
|
c1.push({text: _2});
|
||||||
|
} else {
|
||||||
|
c1.push({text: \`0\`});
|
||||||
|
}
|
||||||
|
return vn1;
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`t-set value priority 1`] = `
|
exports[`t-set value priority 1`] = `
|
||||||
"function anonymous(context,extra
|
"function anonymous(context,extra
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -242,6 +242,32 @@ describe("t-set", () => {
|
|||||||
);
|
);
|
||||||
expect(renderToString(qweb, "test", { somevariable: 43 })).toBe("<div>45</div>");
|
expect(renderToString(qweb, "test", { somevariable: 43 })).toBe("<div>45</div>");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("t-set, t-if, and mix of expression/body lookup, 1", () => {
|
||||||
|
qweb.addTemplate(
|
||||||
|
"test",
|
||||||
|
`<div>
|
||||||
|
<t t-if="flag" t-set="ourvar">1</t>
|
||||||
|
<t t-else="" t-set="ourvar" t-value="0"></t>
|
||||||
|
<t t-esc="ourvar"/>
|
||||||
|
</div>`
|
||||||
|
);
|
||||||
|
expect(renderToString(qweb, "test", { flag: true })).toBe("<div>1</div>");
|
||||||
|
expect(renderToString(qweb, "test", { flag: false })).toBe("<div>0</div>");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("t-set, t-if, and mix of expression/body lookup, 2", () => {
|
||||||
|
qweb.addTemplate(
|
||||||
|
"test",
|
||||||
|
`<div>
|
||||||
|
<t t-if="flag" t-set="ourvar" t-value="1"></t>
|
||||||
|
<t t-else="" t-set="ourvar">0</t>
|
||||||
|
<t t-esc="ourvar"/>
|
||||||
|
</div>`
|
||||||
|
);
|
||||||
|
expect(renderToString(qweb, "test", { flag: true })).toBe("<div>1</div>");
|
||||||
|
expect(renderToString(qweb, "test", { flag: false })).toBe("<div>0</div>");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("t-if", () => {
|
describe("t-if", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user