diff --git a/src/compile/nodes/EventHandler.ts b/src/compile/nodes/EventHandler.ts index 06d63f7f97..54dbd66157 100644 --- a/src/compile/nodes/EventHandler.ts +++ b/src/compile/nodes/EventHandler.ts @@ -77,16 +77,18 @@ export default class EventHandler extends Node { } } - this.args.forEach(arg => { - arg.overwriteThis(this.parent.var); - }); - - if (this.isCustomEvent && this.callee && this.callee.name === 'this') { - const node = this.callee.nodes[0]; - compiler.code.overwrite(node.start, node.end, this.parent.var, { - storeName: true, - contentOnly: true + if (this.isCustomEvent) { + this.args.forEach(arg => { + arg.overwriteThis(this.parent.var); }); + + if (this.callee && this.callee.name === 'this') { + const node = this.callee.nodes[0]; + compiler.code.overwrite(node.start, node.end, this.parent.var, { + storeName: true, + contentOnly: true + }); + } } } } \ No newline at end of file diff --git a/test/runtime/samples/event-handler-each-this/_config.js b/test/runtime/samples/event-handler-each-this/_config.js new file mode 100644 index 0000000000..e6d1056e7e --- /dev/null +++ b/test/runtime/samples/event-handler-each-this/_config.js @@ -0,0 +1,28 @@ +export default { + data: { + items: ['foo', 'bar', 'baz'], + }, + + html: ` + + + + `, + + test(assert, component, target, window) { + const buttons = target.querySelectorAll('button'); + const event = new window.MouseEvent('click'); + + const clicked = []; + + component.on('clicked', event => { + clicked.push(event.node); + }); + + buttons[1].dispatchEvent(event); + + assert.equal(clicked.length, 1); + assert.equal(clicked[0].nodeName, 'BUTTON'); + assert.equal(clicked[0].textContent, 'bar'); + } +}; diff --git a/test/runtime/samples/event-handler-each-this/main.html b/test/runtime/samples/event-handler-each-this/main.html new file mode 100644 index 0000000000..9e5ea88a50 --- /dev/null +++ b/test/runtime/samples/event-handler-each-this/main.html @@ -0,0 +1,3 @@ +{#each items as item} + +{/each} \ No newline at end of file