Skip to content

Commit

Permalink
fix: ensure $$slots returns a record of booleans
Browse files Browse the repository at this point in the history
Returned the underlying functions previously
  • Loading branch information
dummdidumm committed Jul 9, 2024
1 parent 056a1ae commit ae56bee
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-comics-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure `$$slots` returns a record of booleans
14 changes: 14 additions & 0 deletions packages/svelte/src/internal/client/dom/blocks/slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@ export function slot(anchor, slot_fn, slot_props, fallback_fn) {
slot_fn(anchor, slot_props);
}
}

/**
* @param {Record<string, any>} props
* @returns {Record<string, boolean>}
*/
export function sanitize_slots(props) {
/** @type {Record<string, boolean>} */
const sanitized = {};
if (props.children) sanitized.default = true;
for (const key in props.$$slots) {
sanitized[key] = true;
}
return sanitized;
}
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 0 additions & 10 deletions packages/svelte/src/internal/client/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,6 @@ export function unmount(component) {
fn?.();
}

/**
* @param {Record<string, any>} props
* @returns {Record<string, any>}
*/
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
Expand Down
10 changes: 7 additions & 3 deletions packages/svelte/src/internal/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,15 @@ export function sanitize_props(props) {

/**
* @param {Record<string, any>} props
* @returns {Record<string, any>}
* @returns {Record<string, boolean>}
*/
export function sanitize_slots(props) {
const sanitized = { ...props.$$slots };
if (props.children) sanitized.default = props.children;
/** @type {Record<string, boolean>} */
const sanitized = {};
if (props.children) sanitized.default = true;
for (const key in props.$$slots) {
sanitized[key] = true;
}
return sanitized;
}

Expand Down
35 changes: 17 additions & 18 deletions packages/svelte/tests/runtime-legacy/samples/slot/A.svelte
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<script>
let data = '';
let data = '';
if ($$slots.b) {
data = 'foo';
}
if ($$slots.b) {
data = 'foo';
}
export function getData() {
return data;
}
export function getData() {
return data;
}
function toString(data) {
const result = {};
const sortedKeys = Object.keys(data).sort();
// TODO added !! to make it work since $$slots exposes the slot functions - we need to decide what to do with them
sortedKeys.forEach((key) => (result[key] = !!data[key]));
return JSON.stringify(result);
}
function toString(data) {
const result = {};
const sortedKeys = Object.keys(data).sort();
sortedKeys.forEach((key) => (result[key] = data[key]));
return JSON.stringify(result);
}
</script>

<slot />
Expand All @@ -24,9 +23,9 @@
$$slots: {toString($$slots)}

{#if $$slots.b}
<div>
<slot name="b" />
</div>
<div>
<slot name="b" />
</div>
{:else}
Slot b is not available
Slot b is not available
{/if}

0 comments on commit ae56bee

Please sign in to comment.