mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
throw qweb error if invalid loop in t-foreach
This commit is contained in:
@@ -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())}`
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -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", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user