Skip to content
Closed
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
13 changes: 13 additions & 0 deletions packages/svelte/src/internal/client/dom/blocks/slot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { DEV } from 'esm-env';
import { hydrate_next, hydrating } from '../hydration.js';
import { validated_snippets } from './snippet.js';

/**
* @param {Comment} anchor
Expand All @@ -13,6 +15,17 @@ export function slot(anchor, $$props, name, slot_props, fallback_fn) {
}

var slot_fn = $$props.$$slots?.[name];

if (slot_fn === undefined) {
var possible_snippet = $$props[name];
if (
typeof possible_snippet === 'function' &&
(!DEV || validated_snippets.has(possible_snippet))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That feels brittle to me - it means things work subtly different in prod, which could break things.

) {
slot_fn = possible_snippet;
}
}

// Interop: Can use snippets to fill slots
var is_interop = false;
if (slot_fn === true) {
Expand Down
8 changes: 7 additions & 1 deletion packages/svelte/src/internal/client/dom/blocks/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { DEV } from 'esm-env';
import { get_first_child, get_next_sibling } from '../operations.js';
import { noop } from '../../../shared/utils.js';

export var validated_snippets = new WeakSet();

/**
* @template {(node: TemplateNode, ...args: any[]) => void} SnippetFn
* @param {TemplateNode} node
Expand Down Expand Up @@ -60,7 +62,7 @@ export function snippet(node, get_snippet, ...args) {
* @param {(node: TemplateNode, ...args: any[]) => void} fn
*/
export function wrap_snippet(component, fn) {
return (/** @type {TemplateNode} */ node, /** @type {any[]} */ ...args) => {
var wrapped_snippet = (/** @type {TemplateNode} */ node, /** @type {any[]} */ ...args) => {
var previous_component_function = dev_current_component_function;
set_dev_current_component_function(component);

Expand All @@ -70,6 +72,10 @@ export function wrap_snippet(component, fn) {
set_dev_current_component_function(previous_component_function);
}
};
if (DEV) {
validated_snippets.add(wrapped_snippet);
}
return wrapped_snippet;
}

/**
Expand Down