Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: apply modifiers to bubbled events #11369

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-penguins-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: apply modifiers to bubbled events
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,11 @@ function serialize_render_stmt(state) {
* @param {import('../types.js').ComponentContext} context
*/
function serialize_event_handler(node, { state, visit }) {
/** @type {import('estree').Expression} */
let handler;

if (node.expression) {
let handler = node.expression;
handler = node.expression;

// Event handlers can be dynamic (source/store/prop/conditional etc)
const dynamic_handler = () =>
Expand Down Expand Up @@ -1209,34 +1212,34 @@ function serialize_event_handler(node, { state, visit }) {
} else {
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 {
state.analysis.needs_props = true;

// Function + .call to preserve "this" context as much as possible
return b.function(
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')) {
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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<button on:click|preventDefault>click me</button>
Original file line number Diff line number Diff line change
@@ -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