Skip to content

Commit

Permalink
Merge pull request #1376 from sveltejs/gh-1297
Browse files Browse the repository at this point in the history
overwrite this in custom event handlers
  • Loading branch information
Rich-Harris authored Apr 29, 2018
2 parents ddf2d9d + 1fb4041 commit bf58a20
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/compile/nodes/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,13 @@ 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
});
}
}
}
10 changes: 4 additions & 6 deletions src/compile/nodes/shared/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class Expression {
}

if (isReference(node, parent)) {
const { name } = flattenReference(node);
const { name, nodes } = flattenReference(node);

if (currentScope.has(name) || (name === 'event' && isEventHandler)) return;

Expand Down Expand Up @@ -139,11 +139,9 @@ export default class Expression {
}

if (node.type === 'MemberExpression') {
walk(node, {
enter(node) {
code.addSourcemapLocation(node.start);
code.addSourcemapLocation(node.end);
}
nodes.forEach(node => {
code.addSourcemapLocation(node.start);
code.addSourcemapLocation(node.end);
});
}

Expand Down
7 changes: 6 additions & 1 deletion src/utils/flattenReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { Node } from '../interfaces';

export default function flattenReference(node: Node) {
if (node.type === 'Expression') throw new Error('bad');
const nodes = [];
const parts = [];
const propEnd = node.end;

while (node.type === 'MemberExpression') {
if (node.computed) return null;

nodes.unshift(node.property);
parts.unshift(node.property.name);

node = node.object;
Expand All @@ -20,5 +23,7 @@ export default function flattenReference(node: Node) {
if (!name) return null;

parts.unshift(name);
return { name, parts, keypath: `${name}[✂${propStart}-${propEnd}✂]` };
nodes.unshift(node);

return { name, nodes, parts, keypath: `${name}[✂${propStart}-${propEnd}✂]` };
}
22 changes: 22 additions & 0 deletions test/runtime/samples/event-handler-custom-this/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default {
html: `<input>`,

test(assert, component, target, window) {
const input = target.querySelector('input');
const event = new window.KeyboardEvent('keydown', {
which: 13
});

let blurred = false;

input.focus();

input.addEventListener('blur', () => {
blurred = true;
});

input.dispatchEvent(event);

assert.ok(blurred);
},
};
23 changes: 23 additions & 0 deletions test/runtime/samples/event-handler-custom-this/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<input on:enter='this.blur()'>

<script>
export default {
events: {
enter(node, callback) {
function handleKeydown(event) {
if (event.which === 13) {
callback();
}
}

node.addEventListener('keydown', handleKeydown);

return {
destroy() {
node.removeEventListener('keydown', handleKeydown);
}
};
}
}
};
</script>

0 comments on commit bf58a20

Please sign in to comment.