mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] qweb: properly parse short object description
Before this commit, using an expression such as "{machin}" was compiled
in qweb into "{scope['machin']}" which is not valid.
With this commit, we instead transform it into "{machin:
scope['machin']}".
closes #885
This commit is contained in:
@@ -210,6 +210,10 @@ export function tokenize(expr: string): Token[] {
|
|||||||
// Expression "evaluator"
|
// Expression "evaluator"
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const isLeftSeparator = (token) => token && (token.type === "LEFT_BRACE" || token.type === "COMMA");
|
||||||
|
const isRightSeparator = (token) =>
|
||||||
|
token && (token.type === "RIGHT_BRACE" || token.type === "COMMA");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the main function exported by this file. This is the code that will
|
* This is the main function exported by this file. This is the code that will
|
||||||
* process an expression (given as a string) and returns another expression with
|
* process an expression (given as a string) and returns another expression with
|
||||||
@@ -238,13 +242,22 @@ export function tokenize(expr: string): Token[] {
|
|||||||
export function compileExprToArray(expr: string, scope: { [key: string]: QWebVar }): Token[] {
|
export function compileExprToArray(expr: string, scope: { [key: string]: QWebVar }): Token[] {
|
||||||
scope = Object.create(scope);
|
scope = Object.create(scope);
|
||||||
const tokens = tokenize(expr);
|
const tokens = tokenize(expr);
|
||||||
for (let i = 0; i < tokens.length; i++) {
|
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
while (i < tokens.length) {
|
||||||
let token = tokens[i];
|
let token = tokens[i];
|
||||||
let prevToken = tokens[i - 1];
|
let prevToken = tokens[i - 1];
|
||||||
let nextToken = tokens[i + 1];
|
let nextToken = tokens[i + 1];
|
||||||
let isVar = token.type === "SYMBOL" && !RESERVED_WORDS.includes(token.value);
|
let isVar = token.type === "SYMBOL" && !RESERVED_WORDS.includes(token.value);
|
||||||
if (token.type === "SYMBOL" && !RESERVED_WORDS.includes(token.value)) {
|
if (token.type === "SYMBOL" && !RESERVED_WORDS.includes(token.value)) {
|
||||||
if (prevToken) {
|
if (prevToken) {
|
||||||
|
// normalize missing tokens: {a} should be equivalent to {a:a}
|
||||||
|
if (isLeftSeparator(prevToken) && isRightSeparator(nextToken)) {
|
||||||
|
tokens.splice(i + 1, 0, { type: "COLON", value: ":" }, { ...token });
|
||||||
|
nextToken = tokens[i + 1];
|
||||||
|
}
|
||||||
|
|
||||||
if (prevToken.type === "OPERATOR" && prevToken.value === ".") {
|
if (prevToken.type === "OPERATOR" && prevToken.value === ".") {
|
||||||
isVar = false;
|
isVar = false;
|
||||||
} else if (prevToken.type === "LEFT_BRACE" || prevToken.type === "COMMA") {
|
} else if (prevToken.type === "LEFT_BRACE" || prevToken.type === "COMMA") {
|
||||||
@@ -278,6 +291,7 @@ export function compileExprToArray(expr: string, scope: { [key: string]: QWebVar
|
|||||||
token.value = `scope['${token.value}']`;
|
token.value = `scope['${token.value}']`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -200,4 +200,10 @@ describe("expression evaluation", () => {
|
|||||||
test("works with builtin properties", () => {
|
test("works with builtin properties", () => {
|
||||||
expect(compileExpr("state.constructor.name", {})).toBe("scope['state'].constructor.name");
|
expect(compileExpr("state.constructor.name", {})).toBe("scope['state'].constructor.name");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("works with shortcut object key description", () => {
|
||||||
|
expect(compileExpr("{a}", {})).toBe("{a:scope['a']}");
|
||||||
|
expect(compileExpr("{a,b}", {})).toBe("{a:scope['a'],b:scope['b']}");
|
||||||
|
expect(compileExpr("{a,b:3,c}", {})).toBe("{a:scope['a'],b:3,c:scope['c']}");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user