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: disallow mounting a snippet #11347

Merged
merged 1 commit into from Apr 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-panthers-shave.md
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: disallow mounting a snippet
9 changes: 9 additions & 0 deletions packages/svelte/src/internal/client/render.js
Expand Up @@ -21,6 +21,7 @@ import { handle_event_propagation } from './dom/elements/events.js';
import { reset_head_anchor } from './dom/blocks/svelte-head.js';
import * as w from './warnings.js';
import * as e from './errors.js';
import { validate_component } from '../shared/validate.js';

/** @type {Set<string>} */
export const all_registered_events = new Set();
Expand Down Expand Up @@ -102,6 +103,10 @@ export function stringify(value) {
* @returns {Exports}
*/
export function mount(component, options) {
if (DEV) {
validate_component(component);
}

const anchor = options.anchor ?? options.target.appendChild(empty());
// Don't flush previous effects to ensure order of outer effects stays consistent
return flush_sync(() => _mount(component, { ...options, anchor }), false);
Expand All @@ -125,6 +130,10 @@ export function mount(component, options) {
* @returns {Exports}
*/
export function hydrate(component, options) {
if (DEV) {
validate_component(component);
}

const target = options.target;
const previous_hydrate_nodes = hydrate_nodes;

Expand Down
@@ -0,0 +1,12 @@
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},
async test({ assert, target }) {
const div = target.querySelector('div');
assert.htmlEqual(div?.innerHTML || '', '');
},
runtime_error: 'snippet_used_as_component\nA snippet must be rendered with `{@render ...}`'
});
@@ -0,0 +1,14 @@
<script>
import { onMount, mount } from 'svelte';

let el;

onMount(() => {
mount(foo, { target: el });
});
</script>

<div bind:this={el}></div>
{#snippet foo()}
shouldnt be rendered
{/snippet}