Skip to content

Commit ae56bee

Browse files
committed
fix: ensure $$slots returns a record of booleans
Returned the underlying functions previously
1 parent 056a1ae commit ae56bee

File tree

6 files changed

+45
-33
lines changed

6 files changed

+45
-33
lines changed

.changeset/shaggy-comics-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ensure `$$slots` returns a record of booleans

packages/svelte/src/internal/client/dom/blocks/slot.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ export function slot(anchor, slot_fn, slot_props, fallback_fn) {
1313
slot_fn(anchor, slot_props);
1414
}
1515
}
16+
17+
/**
18+
* @param {Record<string, any>} props
19+
* @returns {Record<string, boolean>}
20+
*/
21+
export function sanitize_slots(props) {
22+
/** @type {Record<string, boolean>} */
23+
const sanitized = {};
24+
if (props.children) sanitized.default = true;
25+
for (const key in props.$$slots) {
26+
sanitized[key] = true;
27+
}
28+
return sanitized;
29+
}

packages/svelte/src/internal/client/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export { key_block as key } from './dom/blocks/key.js';
1515
export { css_props } from './dom/blocks/css-props.js';
1616
export { index, each } from './dom/blocks/each.js';
1717
export { html } from './dom/blocks/html.js';
18-
export { slot } from './dom/blocks/slot.js';
18+
export { sanitize_slots, slot } from './dom/blocks/slot.js';
1919
export { snippet, wrap_snippet } from './dom/blocks/snippet.js';
2020
export { component } from './dom/blocks/svelte-component.js';
2121
export { element } from './dom/blocks/svelte-element.js';
@@ -119,7 +119,7 @@ export {
119119
update_pre_store,
120120
update_store
121121
} from './reactivity/store.js';
122-
export { append_styles, sanitize_slots, set_text } from './render.js';
122+
export { append_styles, set_text } from './render.js';
123123
export {
124124
get,
125125
invalidate_inner_signals,

packages/svelte/src/internal/client/render.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,6 @@ export function unmount(component) {
267267
fn?.();
268268
}
269269

270-
/**
271-
* @param {Record<string, any>} props
272-
* @returns {Record<string, any>}
273-
*/
274-
export function sanitize_slots(props) {
275-
const sanitized = { ...props.$$slots };
276-
if (props.children) sanitized.default = props.children;
277-
return sanitized;
278-
}
279-
280270
/**
281271
* @param {Node} target
282272
* @param {string} style_sheet_id

packages/svelte/src/internal/server/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,15 @@ export function sanitize_props(props) {
446446

447447
/**
448448
* @param {Record<string, any>} props
449-
* @returns {Record<string, any>}
449+
* @returns {Record<string, boolean>}
450450
*/
451451
export function sanitize_slots(props) {
452-
const sanitized = { ...props.$$slots };
453-
if (props.children) sanitized.default = props.children;
452+
/** @type {Record<string, boolean>} */
453+
const sanitized = {};
454+
if (props.children) sanitized.default = true;
455+
for (const key in props.$$slots) {
456+
sanitized[key] = true;
457+
}
454458
return sanitized;
455459
}
456460

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
<script>
2-
let data = '';
2+
let data = '';
33
4-
if ($$slots.b) {
5-
data = 'foo';
6-
}
4+
if ($$slots.b) {
5+
data = 'foo';
6+
}
77
8-
export function getData() {
9-
return data;
10-
}
8+
export function getData() {
9+
return data;
10+
}
1111
12-
function toString(data) {
13-
const result = {};
14-
const sortedKeys = Object.keys(data).sort();
15-
// TODO added !! to make it work since $$slots exposes the slot functions - we need to decide what to do with them
16-
sortedKeys.forEach((key) => (result[key] = !!data[key]));
17-
return JSON.stringify(result);
18-
}
12+
function toString(data) {
13+
const result = {};
14+
const sortedKeys = Object.keys(data).sort();
15+
sortedKeys.forEach((key) => (result[key] = data[key]));
16+
return JSON.stringify(result);
17+
}
1918
</script>
2019

2120
<slot />
@@ -24,9 +23,9 @@
2423
$$slots: {toString($$slots)}
2524

2625
{#if $$slots.b}
27-
<div>
28-
<slot name="b" />
29-
</div>
26+
<div>
27+
<slot name="b" />
28+
</div>
3029
{:else}
31-
Slot b is not available
30+
Slot b is not available
3231
{/if}

0 commit comments

Comments
 (0)