-
Notifications
You must be signed in to change notification settings - Fork 350
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 user-space directives
This commit adds the support for user-space directives. To use the user-space directive, an Object of functions needs to be configured on the owl APP: ```js new App(..., { userDirectives: { my-user-directive: function (el, value) { el.setAttribute("t-on-click", value); return el; } } }); ``` The functions will be called when a user 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-user-my-user-directive="click" /> ``` will be replace by : ```xml <div t-on-click="value"/> ``` issue : #1650
- Loading branch information
Showing
6 changed files
with
137 additions
and
3 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
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-user can use t-user 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-user can use t-user 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,57 @@ | ||
import { App, Component, xml } from "../../src"; | ||
import { makeTestFixture, snapshotEverything } from "../helpers"; | ||
|
||
let fixture: HTMLElement; | ||
|
||
snapshotEverything(); | ||
|
||
beforeEach(() => { | ||
fixture = makeTestFixture(); | ||
}); | ||
|
||
describe("t-user", () => { | ||
test("can use t-user directive on a node", async () => { | ||
const steps: string[] = []; | ||
class SomeComponent extends Component { | ||
static template = xml`<div t-user-plop="click" class="my-div"/>`; | ||
click() { | ||
steps.push("clicked"); | ||
} | ||
} | ||
const app = new App(SomeComponent, { | ||
userDirectives: { | ||
plop: (node, value) => { | ||
node.setAttribute("t-on-click", value); | ||
return node; | ||
}, | ||
}, | ||
}); | ||
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-user directive with modifier on a node", async () => { | ||
const steps: string[] = []; | ||
class SomeComponent extends Component { | ||
static template = xml`<div t-user-plop.mouse="click" class="my-div"/>`; | ||
click() { | ||
steps.push("clicked"); | ||
} | ||
} | ||
const app = new App(SomeComponent, { | ||
userDirectives: { | ||
plop: (node, value, modifier) => { | ||
node.setAttribute("t-on-click", value); | ||
steps.push(modifier || ""); | ||
return node; | ||
}, | ||
}, | ||
}); | ||
await app.mount(fixture); | ||
expect(fixture.innerHTML).toBe(`<div class="my-div"></div>`); | ||
fixture.querySelector("div")!.click(); | ||
expect(steps).toEqual(["mouse", "clicked"]); | ||
}); | ||
}); |