From 3dbb2201694a21de329dde83e17b9e4ca96c8ea3 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:53:12 +0200 Subject: [PATCH] fix: ensure `$$slots` returns a record of booleans (#12359) Returned the underlying functions previously --- .changeset/shaggy-comics-jog.md | 5 +++ .../src/internal/client/dom/blocks/slot.js | 14 ++++++++ packages/svelte/src/internal/client/index.js | 4 +-- packages/svelte/src/internal/client/render.js | 10 ------ packages/svelte/src/internal/server/index.js | 10 ++++-- .../runtime-legacy/samples/slot/A.svelte | 35 +++++++++---------- 6 files changed, 45 insertions(+), 33 deletions(-) create mode 100644 .changeset/shaggy-comics-jog.md diff --git a/.changeset/shaggy-comics-jog.md b/.changeset/shaggy-comics-jog.md new file mode 100644 index 0000000000..cb5d956eb8 --- /dev/null +++ b/.changeset/shaggy-comics-jog.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ensure `$$slots` returns a record of booleans diff --git a/packages/svelte/src/internal/client/dom/blocks/slot.js b/packages/svelte/src/internal/client/dom/blocks/slot.js index 6ef1885e00..7db3aa114b 100644 --- a/packages/svelte/src/internal/client/dom/blocks/slot.js +++ b/packages/svelte/src/internal/client/dom/blocks/slot.js @@ -13,3 +13,17 @@ export function slot(anchor, slot_fn, slot_props, fallback_fn) { slot_fn(anchor, slot_props); } } + +/** + * @param {Record} props + * @returns {Record} + */ +export function sanitize_slots(props) { + /** @type {Record} */ + const sanitized = {}; + if (props.children) sanitized.default = true; + for (const key in props.$$slots) { + sanitized[key] = true; + } + return sanitized; +} diff --git a/packages/svelte/src/internal/client/index.js b/packages/svelte/src/internal/client/index.js index 4575f75f0d..a847cebe27 100644 --- a/packages/svelte/src/internal/client/index.js +++ b/packages/svelte/src/internal/client/index.js @@ -15,7 +15,7 @@ export { key_block as key } from './dom/blocks/key.js'; export { css_props } from './dom/blocks/css-props.js'; export { index, each } from './dom/blocks/each.js'; export { html } from './dom/blocks/html.js'; -export { slot } from './dom/blocks/slot.js'; +export { sanitize_slots, slot } from './dom/blocks/slot.js'; export { snippet, wrap_snippet } from './dom/blocks/snippet.js'; export { component } from './dom/blocks/svelte-component.js'; export { element } from './dom/blocks/svelte-element.js'; @@ -119,7 +119,7 @@ export { update_pre_store, update_store } from './reactivity/store.js'; -export { append_styles, sanitize_slots, set_text } from './render.js'; +export { append_styles, set_text } from './render.js'; export { get, invalidate_inner_signals, diff --git a/packages/svelte/src/internal/client/render.js b/packages/svelte/src/internal/client/render.js index 7dfa695851..4e1fba2bd4 100644 --- a/packages/svelte/src/internal/client/render.js +++ b/packages/svelte/src/internal/client/render.js @@ -267,16 +267,6 @@ export function unmount(component) { fn?.(); } -/** - * @param {Record} props - * @returns {Record} - */ -export function sanitize_slots(props) { - const sanitized = { ...props.$$slots }; - if (props.children) sanitized.default = props.children; - return sanitized; -} - /** * @param {Node} target * @param {string} style_sheet_id diff --git a/packages/svelte/src/internal/server/index.js b/packages/svelte/src/internal/server/index.js index fc3ad10d30..6597755fef 100644 --- a/packages/svelte/src/internal/server/index.js +++ b/packages/svelte/src/internal/server/index.js @@ -446,11 +446,15 @@ export function sanitize_props(props) { /** * @param {Record} props - * @returns {Record} + * @returns {Record} */ export function sanitize_slots(props) { - const sanitized = { ...props.$$slots }; - if (props.children) sanitized.default = props.children; + /** @type {Record} */ + const sanitized = {}; + if (props.children) sanitized.default = true; + for (const key in props.$$slots) { + sanitized[key] = true; + } return sanitized; } diff --git a/packages/svelte/tests/runtime-legacy/samples/slot/A.svelte b/packages/svelte/tests/runtime-legacy/samples/slot/A.svelte index ad125d1823..35a05a209a 100644 --- a/packages/svelte/tests/runtime-legacy/samples/slot/A.svelte +++ b/packages/svelte/tests/runtime-legacy/samples/slot/A.svelte @@ -1,21 +1,20 @@ @@ -24,9 +23,9 @@ $$slots: {toString($$slots)} {#if $$slots.b} -
- -
+
+ +
{:else} - Slot b is not available + Slot b is not available {/if}