Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] compiler: multiple modifiers on t-custom #1654

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type customDirectives = Record<
string,
(node: Element, value: string, modifier?: string) => void
(node: Element, value: string, modifier: string[]) => void
>;
4 changes: 2 additions & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ function parseTCustom(node: Element, ctx: ParsingContext): AST | null {
throw new OwlError(`Custom directive "${directiveName}" is not defined`);
}
const value = node.getAttribute(attr)!;
const modifier = attr.split(".").length > 1 ? attr.split(".")[1] : undefined;
const modifiers = attr.split(".").slice(1);
node.removeAttribute(attr);
try {
customDirective(node, value, modifier);
customDirective(node, value, modifiers);
} catch (error) {
throw new OwlError(
`Custom directive "${directiveName}" throw the following error: ${error}`
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/__snapshots__/t_custom.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports[`t-custom can use t-custom directive on a node 1`] = `
}"
`;

exports[`t-custom can use t-custom directive with modifier on a node 1`] = `
exports[`t-custom can use t-custom directive with modifiers on a node 1`] = `
"function anonymous(app, bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, comment } = bdom;
Expand Down
12 changes: 7 additions & 5 deletions tests/compiler/t_custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,27 @@ describe("t-custom", () => {
expect(steps).toEqual(["clicked"]);
});

test("can use t-custom directive with modifier on a node", async () => {
test("can use t-custom directive with modifiers on a node", async () => {
const steps: string[] = [];
class SomeComponent extends Component {
static template = xml`<div t-custom-plop.mouse="click" class="my-div"/>`;
static template = xml`<div t-custom-plop.mouse.stop="click" class="my-div"/>`;
click() {
steps.push("clicked");
}
}
const app = new App(SomeComponent, {
customDirectives: {
plop: (node, value, modifier) => {
plop: (node, value, modifiers) => {
node.setAttribute("t-on-click", value);
steps.push(modifier || "");
for (let mod of modifiers) {
steps.push(mod);
}
},
},
});
await app.mount(fixture);
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`);
fixture.querySelector("div")!.click();
expect(steps).toEqual(["mouse", "clicked"]);
expect(steps).toEqual(["mouse", "stop", "clicked"]);
});
});
Loading