fix: apply modifiers to bubbled events (#11369)

fixes #11365
pull/11335/head
Simon H 7 months ago committed by GitHub
parent 8b1a26904a
commit 500b2065e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: apply modifiers to bubbled events

@ -1170,8 +1170,11 @@ function serialize_render_stmt(state) {
* @param {import('../types.js').ComponentContext} context * @param {import('../types.js').ComponentContext} context
*/ */
function serialize_event_handler(node, { state, visit }) { function serialize_event_handler(node, { state, visit }) {
/** @type {import('estree').Expression} */
let handler;
if (node.expression) { if (node.expression) {
let handler = node.expression; handler = node.expression;
// Event handlers can be dynamic (source/store/prop/conditional etc) // Event handlers can be dynamic (source/store/prop/conditional etc)
const dynamic_handler = () => const dynamic_handler = () =>
@ -1209,34 +1212,34 @@ function serialize_event_handler(node, { state, visit }) {
} else { } else {
handler = /** @type {import('estree').Expression} */ (visit(handler)); handler = /** @type {import('estree').Expression} */ (visit(handler));
} }
if (node.modifiers.includes('stopPropagation')) {
handler = b.call('$.stopPropagation', handler);
}
if (node.modifiers.includes('stopImmediatePropagation')) {
handler = b.call('$.stopImmediatePropagation', handler);
}
if (node.modifiers.includes('preventDefault')) {
handler = b.call('$.preventDefault', handler);
}
if (node.modifiers.includes('self')) {
handler = b.call('$.self', handler);
}
if (node.modifiers.includes('trusted')) {
handler = b.call('$.trusted', handler);
}
return handler;
} else { } else {
state.analysis.needs_props = true; state.analysis.needs_props = true;
// Function + .call to preserve "this" context as much as possible // Function + .call to preserve "this" context as much as possible
return b.function( handler = b.function(
null, null,
[b.id('$$arg')], [b.id('$$arg')],
b.block([b.stmt(b.call('$.bubble_event.call', b.this, b.id('$$props'), b.id('$$arg')))]) b.block([b.stmt(b.call('$.bubble_event.call', b.this, b.id('$$props'), b.id('$$arg')))])
); );
} }
if (node.modifiers.includes('stopPropagation')) {
handler = b.call('$.stopPropagation', handler);
}
if (node.modifiers.includes('stopImmediatePropagation')) {
handler = b.call('$.stopImmediatePropagation', handler);
}
if (node.modifiers.includes('preventDefault')) {
handler = b.call('$.preventDefault', handler);
}
if (node.modifiers.includes('self')) {
handler = b.call('$.self', handler);
}
if (node.modifiers.includes('trusted')) {
handler = b.call('$.trusted', handler);
}
return handler;
} }
/** /**

@ -0,0 +1,12 @@
import { ok, test } from '../../test';
export default test({
async test({ assert, component, target }) {
const button = target.querySelector('button');
ok(button);
await button.click();
assert.ok(component.default_was_prevented);
}
});

@ -0,0 +1,11 @@
<script>
import Button from "./button.svelte";
export let default_was_prevented;
function handle_click(event) {
default_was_prevented = event.defaultPrevented;
}
</script>
<Button on:click={handle_click} />
Loading…
Cancel
Save