-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] parser: add support for custom directives
This commit adds the support for custom directives. To use the custom directive, an Object of functions needs to be configured on the owl APP: ```js new App(..., { customDirectives: { test_directive: function (el, value) { el.setAttribute("t-on-click", value); return el; } } }); ``` The functions will be called when a custom directive with the name of the function is found. The original element will be replaced with the one returned by the function. This : ```xml <div t-custom-test_directive="click" /> ``` will be replace by : ```xml <div t-on-click="value"/> ``` issue : #1650
- Loading branch information
Showing
8 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type customDirectives = Record< | ||
string, | ||
(node: Element, value: string, modifier?: string) => void | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`t-custom can use t-custom directive on a node 1`] = ` | ||
"function anonymous(app, bdom, helpers | ||
) { | ||
let { text, createBlock, list, multi, html, toggler, comment } = bdom; | ||
let block1 = createBlock(\`<div class=\\"my-div\\" block-handler-0=\\"click\\"/>\`); | ||
return function template(ctx, node, key = \\"\\") { | ||
let hdlr1 = [ctx['click'], ctx]; | ||
return block1([hdlr1]); | ||
} | ||
}" | ||
`; | ||
exports[`t-custom can use t-custom directive with modifier on a node 1`] = ` | ||
"function anonymous(app, bdom, helpers | ||
) { | ||
let { text, createBlock, list, multi, html, toggler, comment } = bdom; | ||
let block1 = createBlock(\`<div class=\\"my-div\\" block-handler-0=\\"click\\"/>\`); | ||
return function template(ctx, node, key = \\"\\") { | ||
let hdlr1 = [ctx['click'], ctx]; | ||
return block1([hdlr1]); | ||
} | ||
}" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { App, Component, xml } from "../../src"; | ||
import { makeTestFixture, snapshotEverything } from "../helpers"; | ||
|
||
let fixture: HTMLElement; | ||
|
||
snapshotEverything(); | ||
|
||
beforeEach(() => { | ||
fixture = makeTestFixture(); | ||
}); | ||
|
||
describe("t-custom", () => { | ||
test("can use t-custom directive on a node", async () => { | ||
const steps: string[] = []; | ||
class SomeComponent extends Component { | ||
static template = xml`<div t-custom-plop="click" class="my-div"/>`; | ||
click() { | ||
steps.push("clicked"); | ||
} | ||
} | ||
const app = new App(SomeComponent, { | ||
customDirectives: { | ||
plop: (node, value) => { | ||
node.setAttribute("t-on-click", value); | ||
}, | ||
}, | ||
}); | ||
await app.mount(fixture); | ||
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`); | ||
fixture.querySelector("div")!.click(); | ||
expect(steps).toEqual(["clicked"]); | ||
}); | ||
|
||
test("can use t-custom directive with modifier on a node", async () => { | ||
const steps: string[] = []; | ||
class SomeComponent extends Component { | ||
static template = xml`<div t-custom-plop.mouse="click" class="my-div"/>`; | ||
click() { | ||
steps.push("clicked"); | ||
} | ||
} | ||
const app = new App(SomeComponent, { | ||
customDirectives: { | ||
plop: (node, value, modifier) => { | ||
node.setAttribute("t-on-click", value); | ||
steps.push(modifier || ""); | ||
}, | ||
}, | ||
}); | ||
await app.mount(fixture); | ||
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`); | ||
fixture.querySelector("div")!.click(); | ||
expect(steps).toEqual(["mouse", "clicked"]); | ||
}); | ||
}); |