mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
add word replacements feature in qweb_vdom
This commit is contained in:
@@ -14,6 +14,15 @@ const RESERVED_WORDS = "true,false,NaN,null,undefined,debugger,console,window,in
|
|||||||
","
|
","
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const WORD_REPLACEMENT = {
|
||||||
|
and: "&&",
|
||||||
|
or: "||",
|
||||||
|
gt: ">",
|
||||||
|
gte: ">=",
|
||||||
|
lt: "<",
|
||||||
|
lte: "<="
|
||||||
|
};
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Compilation Context
|
// Compilation Context
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@@ -528,7 +537,7 @@ export class QWeb {
|
|||||||
} else if (c.match(/\W/) && invar.length) {
|
} else if (c.match(/\W/) && invar.length) {
|
||||||
// TODO: Should check for possible spaces before dot
|
// TODO: Should check for possible spaces before dot
|
||||||
if (chars[invarPos - 1] !== "." && RESERVED_WORDS.indexOf(invar) < 0) {
|
if (chars[invarPos - 1] !== "." && RESERVED_WORDS.indexOf(invar) < 0) {
|
||||||
invar = "context['" + invar + "']";
|
invar = WORD_REPLACEMENT[invar] || "context['" + invar + "']";
|
||||||
}
|
}
|
||||||
r += invar;
|
r += invar;
|
||||||
invar = "";
|
invar = "";
|
||||||
|
|||||||
@@ -909,6 +909,58 @@ exports[`t-if boolean value true condition 1`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`t-if can use some boolean operators in expressions 1`] = `
|
||||||
|
"function anonymous(context,extra
|
||||||
|
) {
|
||||||
|
let h = this.utils.h;
|
||||||
|
let c1 = [], p1 = {key:1};
|
||||||
|
let vn1 = h('div', p1, c1);
|
||||||
|
c1.push({text: \`
|
||||||
|
\`});
|
||||||
|
if (context['cond1'] && context['cond2']) {
|
||||||
|
c1.push({text: \`and\`});
|
||||||
|
}
|
||||||
|
c1.push({text: \`
|
||||||
|
\`});
|
||||||
|
if (context['cond1'] && context['cond3']) {
|
||||||
|
c1.push({text: \`nope\`});
|
||||||
|
}
|
||||||
|
c1.push({text: \`
|
||||||
|
\`});
|
||||||
|
if (context['cond1'] || context['cond3']) {
|
||||||
|
c1.push({text: \`or\`});
|
||||||
|
}
|
||||||
|
c1.push({text: \`
|
||||||
|
\`});
|
||||||
|
if (context['cond3'] || context['cond4']) {
|
||||||
|
c1.push({text: \`nope\`});
|
||||||
|
}
|
||||||
|
c1.push({text: \`
|
||||||
|
\`});
|
||||||
|
if (context['m'] > 3) {
|
||||||
|
c1.push({text: \`mgt\`});
|
||||||
|
}
|
||||||
|
c1.push({text: \`
|
||||||
|
\`});
|
||||||
|
if (context['n'] > 3) {
|
||||||
|
c1.push({text: \`ngt\`});
|
||||||
|
}
|
||||||
|
c1.push({text: \`
|
||||||
|
\`});
|
||||||
|
if (context['m'] < 3) {
|
||||||
|
c1.push({text: \`mlt\`});
|
||||||
|
}
|
||||||
|
c1.push({text: \`
|
||||||
|
\`});
|
||||||
|
if (context['n'] < 3) {
|
||||||
|
c1.push({text: \`nlt\`});
|
||||||
|
}
|
||||||
|
c1.push({text: \`
|
||||||
|
\`});
|
||||||
|
return vn1;
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`t-on can bind event handler 1`] = `
|
exports[`t-on can bind event handler 1`] = `
|
||||||
"function anonymous(context,extra
|
"function anonymous(context,extra
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import sdAttributes from "../../libs/snabbdom/src/modules/attributes";
|
|||||||
import sdListeners from "../../libs/snabbdom/src/modules/eventlisteners";
|
import sdListeners from "../../libs/snabbdom/src/modules/eventlisteners";
|
||||||
import { init } from "../../libs/snabbdom/src/snabbdom";
|
import { init } from "../../libs/snabbdom/src/snabbdom";
|
||||||
import { EvalContext, QWeb } from "../../src/ts/core/qweb_vdom";
|
import { EvalContext, QWeb } from "../../src/ts/core/qweb_vdom";
|
||||||
|
import { normalize } from "../helpers";
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Setup and helpers
|
// Setup and helpers
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@@ -288,6 +288,33 @@ describe("t-if", () => {
|
|||||||
"<div><span>begin</span>fail-else<span>end</span></div>"
|
"<div><span>begin</span>fail-else<span>end</span></div>"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("can use some boolean operators in expressions", () => {
|
||||||
|
qweb.addTemplate(
|
||||||
|
"test",
|
||||||
|
`<div>
|
||||||
|
<t t-if="cond1 and cond2">and</t>
|
||||||
|
<t t-if="cond1 and cond3">nope</t>
|
||||||
|
<t t-if="cond1 or cond3">or</t>
|
||||||
|
<t t-if="cond3 or cond4">nope</t>
|
||||||
|
<t t-if="m gt 3">mgt</t>
|
||||||
|
<t t-if="n gt 3">ngt</t>
|
||||||
|
<t t-if="m lt 3">mlt</t>
|
||||||
|
<t t-if="n lt 3">nlt</t>
|
||||||
|
</div>`
|
||||||
|
);
|
||||||
|
const context = {
|
||||||
|
cond1: true,
|
||||||
|
cond2: true,
|
||||||
|
cond3: false,
|
||||||
|
cond4: false,
|
||||||
|
m: 5,
|
||||||
|
n: 2
|
||||||
|
};
|
||||||
|
expect(normalize(renderToString(qweb, "test", context))).toBe(
|
||||||
|
"<div>andormgtnlt</div>"
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("attributes", () => {
|
describe("attributes", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user