Skip to content

fix: allow slot attribute inside snippets #12188

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

Merged
merged 1 commit into from
Jun 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-bees-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: allow slot attribute inside snippets
10 changes: 9 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,16 @@ function validate_attribute_name(attribute) {
* @param {boolean} is_component
*/
function validate_slot_attribute(context, attribute, is_component = false) {
const parent = context.path.at(-2);
let owner = undefined;

if (parent?.type === 'SnippetBlock') {
if (!is_text_attribute(attribute)) {
e.slot_attribute_invalid(attribute);
}
return;
}

let i = context.path.length;
while (i--) {
const ancestor = context.path[i];
Expand All @@ -283,7 +291,7 @@ function validate_slot_attribute(context, attribute, is_component = false) {
owner.type === 'SvelteComponent' ||
owner.type === 'SvelteSelf'
) {
if (owner !== context.path.at(-2)) {
if (owner !== parent) {
e.slot_attribute_invalid_placement(attribute);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script context="module">
if (!customElements.get('my-custom-element')) {
customElements.define('my-custom-element', class extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = '|<slot></slot>|<slot name="slot"></slot>|';
}
});
}
</script>

<script>
const { children } = $props();
</script>

<my-custom-element>
{@render children()}
</my-custom-element>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test } from '../../test';

export default test({
mode: ['client', 'server'],
html: `<my-custom-element>Default <span slot="slot">Slotted</span></my-custom-element>`,
test({ target, assert }) {
const shadowRoot = /** @type {ShadowRoot} */ (
target.querySelector('my-custom-element')?.shadowRoot
);
const [defaultSlot, namedSlot] = shadowRoot.querySelectorAll('slot');
const assignedDefaultNodes = defaultSlot.assignedNodes();
const assignedNamedNodes = namedSlot.assignedNodes();

assert.equal(assignedDefaultNodes.length, 1);
assert.equal(assignedNamedNodes.length, 1);
assert.htmlEqual(assignedDefaultNodes[0].textContent || '', `Default`);
assert.htmlEqual(
/** @type {HTMLElement} */ (assignedNamedNodes[0]).outerHTML,
`<span slot="slot">Slotted</span>`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import Component from './Component.svelte';
</script>

<Component>
{#snippet children()}
Default
<span slot="slot">Slotted</span>
{/snippet}
</Component>
Loading