Skip to content

Commit 4c0c5d4

Browse files
authored
Merge pull request #67 from github/spaces
Split data-action by spaces/tabs/line feeds
2 parents 3bad2a8 + 50a8a52 commit 4c0c5d4

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/bind.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function handleEvent(event: Event) {
7373

7474
type Binding = {type: string; tag: string; method: string}
7575
function* bindings(el: Element): Iterable<Binding> {
76-
for (const action of (el.getAttribute('data-action') || '').split(' ')) {
76+
for (const action of (el.getAttribute('data-action') || '').trim().split(/\s+/)) {
7777
const eventSep = action.lastIndexOf(':')
7878
const methodSep = action.lastIndexOf('#')
7979
const type = action.slice(0, eventSep)

test/bind.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ describe('bind', () => {
106106
expect(calls).to.have.nested.property('[1][0].type', 'submit')
107107
})
108108

109+
it('can bind multiple actions separated by line feed', () => {
110+
const instance = document.createElement('bind-test-element')
111+
chai.spy.on(instance, 'foo')
112+
chai.spy.on(instance, 'bar')
113+
const el = document.createElement('div')
114+
el.setAttribute('data-action', `click:bind-test-element#foo\nclick:bind-test-element#bar`)
115+
instance.appendChild(el)
116+
bind(instance)
117+
expect(instance.foo).to.have.not.been.called()
118+
el.dispatchEvent(new CustomEvent('click'))
119+
expect(instance.foo).to.have.been.called.exactly(1)
120+
expect(instance.bar).to.have.been.called.exactly(1)
121+
expect(instance.foo.__spy.calls).to.have.nested.property('[0][0].type', 'click')
122+
expect(instance.bar.__spy.calls).to.have.nested.property('[0][0].type', 'click')
123+
})
124+
109125
it('can bind multiple elements to the same event', () => {
110126
const instance = document.createElement('bind-test-element')
111127
chai.spy.on(instance, 'foo')

0 commit comments

Comments
 (0)