Skip to content

Commit 500b206

Browse files
authored
fix: apply modifiers to bubbled events (#11369)
fixes #11365
1 parent 8b1a269 commit 500b206

File tree

5 files changed

+52
-20
lines changed

5 files changed

+52
-20
lines changed

.changeset/fast-penguins-matter.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: apply modifiers to bubbled events

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,8 +1170,11 @@ function serialize_render_stmt(state) {
11701170
* @param {import('../types.js').ComponentContext} context
11711171
*/
11721172
function serialize_event_handler(node, { state, visit }) {
1173+
/** @type {import('estree').Expression} */
1174+
let handler;
1175+
11731176
if (node.expression) {
1174-
let handler = node.expression;
1177+
handler = node.expression;
11751178

11761179
// Event handlers can be dynamic (source/store/prop/conditional etc)
11771180
const dynamic_handler = () =>
@@ -1209,34 +1212,34 @@ function serialize_event_handler(node, { state, visit }) {
12091212
} else {
12101213
handler = /** @type {import('estree').Expression} */ (visit(handler));
12111214
}
1212-
1213-
if (node.modifiers.includes('stopPropagation')) {
1214-
handler = b.call('$.stopPropagation', handler);
1215-
}
1216-
if (node.modifiers.includes('stopImmediatePropagation')) {
1217-
handler = b.call('$.stopImmediatePropagation', handler);
1218-
}
1219-
if (node.modifiers.includes('preventDefault')) {
1220-
handler = b.call('$.preventDefault', handler);
1221-
}
1222-
if (node.modifiers.includes('self')) {
1223-
handler = b.call('$.self', handler);
1224-
}
1225-
if (node.modifiers.includes('trusted')) {
1226-
handler = b.call('$.trusted', handler);
1227-
}
1228-
1229-
return handler;
12301215
} else {
12311216
state.analysis.needs_props = true;
12321217

12331218
// Function + .call to preserve "this" context as much as possible
1234-
return b.function(
1219+
handler = b.function(
12351220
null,
12361221
[b.id('$$arg')],
12371222
b.block([b.stmt(b.call('$.bubble_event.call', b.this, b.id('$$props'), b.id('$$arg')))])
12381223
);
12391224
}
1225+
1226+
if (node.modifiers.includes('stopPropagation')) {
1227+
handler = b.call('$.stopPropagation', handler);
1228+
}
1229+
if (node.modifiers.includes('stopImmediatePropagation')) {
1230+
handler = b.call('$.stopImmediatePropagation', handler);
1231+
}
1232+
if (node.modifiers.includes('preventDefault')) {
1233+
handler = b.call('$.preventDefault', handler);
1234+
}
1235+
if (node.modifiers.includes('self')) {
1236+
handler = b.call('$.self', handler);
1237+
}
1238+
if (node.modifiers.includes('trusted')) {
1239+
handler = b.call('$.trusted', handler);
1240+
}
1241+
1242+
return handler;
12401243
}
12411244

12421245
/**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ok, test } from '../../test';
2+
3+
export default test({
4+
async test({ assert, component, target }) {
5+
const button = target.querySelector('button');
6+
ok(button);
7+
8+
await button.click();
9+
10+
assert.ok(component.default_was_prevented);
11+
}
12+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<button on:click|preventDefault>click me</button>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import Button from "./button.svelte";
3+
4+
export let default_was_prevented;
5+
6+
function handle_click(event) {
7+
default_was_prevented = event.defaultPrevented;
8+
}
9+
</script>
10+
11+
<Button on:click={handle_click} />

0 commit comments

Comments
 (0)