mirror of https://github.com/sveltejs/svelte
fix: allow slot attribute inside snippets (#12188)
Someone could render a snippet into a custom element, and therefore elements with slot attributes should be allowed within them and be treated as regular elements closes #12158pull/12195/head
parent
48d1ef96a6
commit
a4e994bdf4
@ -0,0 +1,5 @@
|
||||
---
|
||||
"svelte": patch
|
||||
---
|
||||
|
||||
fix: allow slot attribute inside snippets
|
@ -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>
|
@ -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>`
|
||||
);
|
||||
}
|
||||
});
|
@ -0,0 +1,10 @@
|
||||
<script>
|
||||
import Component from './Component.svelte';
|
||||
</script>
|
||||
|
||||
<Component>
|
||||
{#snippet children()}
|
||||
Default
|
||||
<span slot="slot">Slotted</span>
|
||||
{/snippet}
|
||||
</Component>
|
Loading…
Reference in new issue