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,6 +1212,16 @@ function serialize_event_handler(node, { state, visit }) {
} else { } else {
handler = /** @type {import('estree').Expression} */ (visit(handler)); handler = /** @type {import('estree').Expression} */ (visit(handler));
} }
} else {
state.analysis.needs_props = true;
// Function + .call to preserve "this" context as much as possible
handler = b.function(
null,
[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')) { if (node.modifiers.includes('stopPropagation')) {
handler = b.call('$.stopPropagation', handler); handler = b.call('$.stopPropagation', handler);
@ -1227,16 +1240,6 @@ function serialize_event_handler(node, { state, visit }) {
} }
return handler; return handler;
} else {
state.analysis.needs_props = true;
// Function + .call to preserve "this" context as much as possible
return b.function(
null,
[b.id('$$arg')],
b.block([b.stmt(b.call('$.bubble_event.call', b.this, b.id('$$props'), b.id('$$arg')))])
);
}
} }
/** /**

@ -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