mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] components: allow prop names that are not valid bare property name
This commit is contained in:
committed by
Aaron Bohy
parent
b90180a9e0
commit
93b88cad8d
@@ -967,7 +967,7 @@ export class CodeGenerator {
|
|||||||
const props: string[] = [];
|
const props: string[] = [];
|
||||||
let hasSlotsProp = false;
|
let hasSlotsProp = false;
|
||||||
for (let p in ast.props) {
|
for (let p in ast.props) {
|
||||||
const propName = p.includes("-") ? `'${p}'` : p;
|
const propName = /^[a-z_]+$/i.test(p) ? p : `'${p}'`;
|
||||||
props.push(`${propName}: ${this.captureExpression(ast.props[p]) || undefined}`);
|
props.push(`${propName}: ${this.captureExpression(ast.props[p]) || undefined}`);
|
||||||
if (p === "slots") {
|
if (p === "slots") {
|
||||||
hasSlotsProp = true;
|
hasSlotsProp = true;
|
||||||
|
|||||||
@@ -123,6 +123,33 @@ exports[`basics prop names can contain - 2`] = `
|
|||||||
}"
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`basics support prop names that aren't valid bare object property names 1`] = `
|
||||||
|
"function anonymous(bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, component } = bdom;
|
||||||
|
let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber, safeOutput } = helpers;
|
||||||
|
|
||||||
|
let block1 = createBlock(\`<button block-handler-0=\\"click\\"/>\`);
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
let d1 = [ctx['props'].onClick, ctx];
|
||||||
|
return block1([d1]);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`basics support prop names that aren't valid bare object property names 2`] = `
|
||||||
|
"function anonymous(bdom, helpers
|
||||||
|
) {
|
||||||
|
let { text, createBlock, list, multi, html, toggler, component } = bdom;
|
||||||
|
let { withDefault, getTemplate, prepareList, withKey, zero, call, callSlot, capture, isBoundary, shallowEqual, setContextValue, toNumber, safeOutput } = helpers;
|
||||||
|
|
||||||
|
return function template(ctx, node, key = \\"\\") {
|
||||||
|
return component(\`Child\`, {'some-dashed-prop': 5,'a.b': 'keyword prop'}, key + \`__1\`, node, ctx);
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
`;
|
||||||
|
|
||||||
exports[`basics t-set with a body expression can be passed in props, and then t-out 1`] = `
|
exports[`basics t-set with a body expression can be passed in props, and then t-out 1`] = `
|
||||||
"function anonymous(bdom, helpers
|
"function anonymous(bdom, helpers
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -141,4 +141,21 @@ describe("basics", () => {
|
|||||||
expect(onClickArgs![0]).toBe(1);
|
expect(onClickArgs![0]).toBe(1);
|
||||||
expect(onClickArgs![1]).toBeInstanceOf(MouseEvent);
|
expect(onClickArgs![1]).toBeInstanceOf(MouseEvent);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("support prop names that aren't valid bare object property names", async () => {
|
||||||
|
expect.assertions(4);
|
||||||
|
class Child extends Component {
|
||||||
|
static template = xml`<button t-on-click="props.onClick"/>`;
|
||||||
|
setup() {
|
||||||
|
expect(this.props["some-dashed-prop"]).toBe(5);
|
||||||
|
expect(this.props["a.b"]).toBe("keyword prop");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Parent extends Component {
|
||||||
|
static template = xml`<Child some-dashed-prop="5" a.b="'keyword prop'"/>`;
|
||||||
|
static components = { Child };
|
||||||
|
}
|
||||||
|
await mount(Parent, fixture);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user