fix: sanitize component event names (#10235)

fixes #9916
pull/10259/head
Simon H 10 months ago committed by GitHub
parent 6b8b57c431
commit 434a58711f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: sanitize component event names

@ -871,14 +871,10 @@ function serialize_inline_component(node, component_name, context) {
if (Object.keys(events).length > 0) { if (Object.keys(events).length > 0) {
const events_expression = b.object( const events_expression = b.object(
Object.keys(events).map((name) => Object.keys(events).map((name) =>
b.prop( b.init(name, events[name].length > 1 ? b.array(events[name]) : events[name][0])
'init',
b.id(name),
events[name].length > 1 ? b.array(events[name]) : events[name][0]
)
) )
); );
push_prop(b.prop('init', b.id('$$events'), events_expression)); push_prop(b.init('$$events', events_expression));
} }
/** @type {import('estree').Statement[]} */ /** @type {import('estree').Statement[]} */
@ -932,19 +928,18 @@ function serialize_inline_component(node, component_name, context) {
if (slot_name === 'default') { if (slot_name === 'default') {
push_prop( push_prop(
b.prop( b.init(
'init', 'children',
b.id('children'),
context.state.options.dev ? b.call('$.add_snippet_symbol', slot_fn) : slot_fn context.state.options.dev ? b.call('$.add_snippet_symbol', slot_fn) : slot_fn
) )
); );
} else { } else {
serialized_slots.push(b.prop('init', b.key(slot_name), slot_fn)); serialized_slots.push(b.init(slot_name, slot_fn));
} }
} }
if (serialized_slots.length > 0) { if (serialized_slots.length > 0) {
push_prop(b.prop('init', b.id('$$slots'), b.object(serialized_slots))); push_prop(b.init('$$slots', b.object(serialized_slots)));
} }
const props_expression = const props_expression =

@ -0,0 +1,7 @@
<script>
import { createEventDispatcher } from "svelte";
const dispatch = createEventDispatcher();
</script>
<button on:click={() => dispatch('event-name')}>toggle</button>

@ -3,11 +3,14 @@ import { ok, test } from '../../test';
export default test({ export default test({
html: ` html: `
<div>toggle</div> <div>toggle</div>
<button>toggle</button>
`, `,
async test({ assert, target, window }) { async test({ assert, target, window }) {
const div = target.querySelector('div'); const div = target.querySelector('div');
const button = target.querySelector('button');
ok(div); ok(div);
ok(button);
const event = new window.MouseEvent('some-event'); const event = new window.MouseEvent('some-event');
await div.dispatchEvent(event); await div.dispatchEvent(event);
@ -15,15 +18,17 @@ export default test({
target.innerHTML, target.innerHTML,
` `
<div>toggle</div> <div>toggle</div>
<button>toggle</button>
<p>hello!</p> <p>hello!</p>
` `
); );
await div.dispatchEvent(event); await button.click();
assert.htmlEqual( assert.htmlEqual(
target.innerHTML, target.innerHTML,
` `
<div>toggle</div> <div>toggle</div>
<button>toggle</button>
` `
); );
} }

@ -1,8 +1,11 @@
<script> <script>
import Component from "./Component.svelte";
export let visible; export let visible;
</script> </script>
<div on:some-event='{() => visible = !visible}'>toggle</div> <div on:some-event='{() => visible = !visible}'>toggle</div>
<Component on:event-name={() => visible = !visible}></Component>
{#if visible} {#if visible}
<p>hello!</p> <p>hello!</p>

Loading…
Cancel
Save