throw qweb error if invalid loop in t-foreach

This commit is contained in:
Géry Debongnie
2019-02-01 11:28:03 +01:00
parent d2d622b3c3
commit c46bad6e35
2 changed files with 11 additions and 0 deletions
+3
View File
@@ -644,6 +644,9 @@ const forEachDirective: Directive = {
const name = node.getAttribute("t-as")!; const name = node.getAttribute("t-as")!;
let arrayID = ctx.generateID(); let arrayID = ctx.generateID();
ctx.addLine(`let _${arrayID} = ${qweb._formatExpression(elems)};`); ctx.addLine(`let _${arrayID} = ${qweb._formatExpression(elems)};`);
ctx.addLine(
`if (!_${arrayID}) { throw new Error('QWeb error: Invalid loop expression')}`
);
ctx.addLine( ctx.addLine(
`if (typeof _${arrayID} === 'number') { _${arrayID} = Array.from(Array(_${arrayID}).keys())}` `if (typeof _${arrayID} === 'number') { _${arrayID} = Array.from(Array(_${arrayID}).keys())}`
); );
+8
View File
@@ -545,6 +545,14 @@ describe("foreach", () => {
renderToString(qweb, "test", context); renderToString(qweb, "test", context);
expect(Object.keys(context).length).toBe(0); expect(Object.keys(context).length).toBe(0);
}); });
test("throws error if invalid loop expression", () => {
qweb.addTemplate(
"test",
`<div><t t-foreach="abc" t-as="item"><span/></t></div>`
);
expect(() => qweb.render("test")).toThrow("Invalid loop expression");
});
}); });
describe("misc", () => { describe("misc", () => {