Merge pull request #1311 from sveltejs/gh-1278

treat component events the same as element events
pull/1332/head
Rich Harris 7 years ago committed by GitHub
commit 07a53e55de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,4 +1,6 @@
import deindent from '../../utils/deindent'; import deindent from '../../utils/deindent';
import flattenReference from '../../utils/flattenReference';
import validCalleeObjects from '../../utils/validCalleeObjects';
import stringifyProps from '../../utils/stringifyProps'; import stringifyProps from '../../utils/stringifyProps';
import CodeBuilder from '../../utils/CodeBuilder'; import CodeBuilder from '../../utils/CodeBuilder';
import getTailSnippet from '../../utils/getTailSnippet'; import getTailSnippet from '../../utils/getTailSnippet';
@ -479,10 +481,17 @@ function mungeEventHandler(generator: DomGenerator, node: Node, handler: Node, b
if (handler.expression) { if (handler.expression) {
generator.addSourcemapLocations(handler.expression); generator.addSourcemapLocations(handler.expression);
// TODO try out repetition between this and element counterpart
const flattened = flattenReference(handler.expression.callee);
if (!validCalleeObjects.has(flattened.name)) {
// allow event.stopPropagation(), this.select() etc
// TODO verify that it's a valid callee (i.e. built-in or declared method)
generator.code.prependRight( generator.code.prependRight(
handler.expression.start, handler.expression.start,
`${block.alias('component')}.` `${block.alias('component')}.`
); );
}
handler.expression.arguments.forEach((arg: Node) => { handler.expression.arguments.forEach((arg: Node) => {
const { contexts } = block.contextualise(arg, null, true); const { contexts } = block.contextualise(arg, null, true);

@ -0,0 +1 @@
<button on:click>click me</button>

@ -0,0 +1,25 @@
export default {
html: '<button>click me</button>',
test(assert, component, target) {
const button = target.querySelector('button');
const messages = [];
const log = console.log;
console.log = msg => {
messages.push(msg);
};
try {
button.dispatchEvent(new window.MouseEvent('click'));
assert.deepEqual(messages, [
'clicked'
]);
} catch (err) {
console.log = log;
throw err;
}
console.log = log;
},
};

@ -0,0 +1,9 @@
<Widget on:click="console.log('clicked')"/>
<script>
import Widget from './Widget.html';
export default {
components: { Widget }
};
</script>
Loading…
Cancel
Save