allow event handlers to call event methods e.g. stopPropagation – closes #162

pull/170/head
Rich-Harris 8 years ago
parent 941de39523
commit 8529e28c11

@ -1,6 +1,7 @@
import attributeLookup from './lookup.js'; import attributeLookup from './lookup.js';
import createBinding from './binding/index.js'; import createBinding from './binding/index.js';
import deindent from '../../../utils/deindent.js'; import deindent from '../../../utils/deindent.js';
import flattenReference from '../../../utils/flattenReference.js';
export default function addElementAttributes ( generator, node, local ) { export default function addElementAttributes ( generator, node, local ) {
node.attributes.forEach( attribute => { node.attributes.forEach( attribute => {
@ -114,7 +115,12 @@ export default function addElementAttributes ( generator, node, local ) {
else if ( attribute.type === 'EventHandler' ) { else if ( attribute.type === 'EventHandler' ) {
// TODO verify that it's a valid callee (i.e. built-in or declared method) // TODO verify that it's a valid callee (i.e. built-in or declared method)
generator.addSourcemapLocations( attribute.expression ); generator.addSourcemapLocations( attribute.expression );
generator.code.prependRight( attribute.expression.start, 'component.' );
const flattened = flattenReference( attribute.expression.callee );
if ( flattened.name !== 'event' ) {
// allow event.stopPropagation() etc
generator.code.prependRight( attribute.expression.start, 'component.' );
}
const usedContexts = new Set(); const usedContexts = new Set();
attribute.expression.arguments.forEach( arg => { attribute.expression.arguments.forEach( arg => {

@ -0,0 +1,15 @@
export default {
solo: true,
show: true,
test ( assert, component, target, window ) {
const allow = target.querySelector( '.allow-propagation' );
const stop = target.querySelector( '.stop-propagation' );
allow.dispatchEvent( new window.MouseEvent( 'click', { bubbles: true }) );
stop.dispatchEvent( new window.MouseEvent( 'click', { bubbles: true }) );
assert.equal( component.get( 'foo' ), true );
assert.equal( component.get( 'bar' ), false );
}
};

@ -0,0 +1,18 @@
<div on:click='set({ foo: true })'>
<button class='allow-propagation'>click me</button>
</div>
<div on:click='set({ bar: true })'>
<button class='stop-propagation' on:click='event.stopPropagation()'>click me</button>
</div>
<script>
export default {
data () {
return {
foo: false,
bar: false
};
}
};
</script>
Loading…
Cancel
Save