allow slot not to be top level if it is within a custom element

pull/4509/head
Tan Li Hau 6 years ago
parent 6f634b8fc6
commit 45632d0f7b

@ -395,7 +395,7 @@ export default class Element extends Node {
component.slot_outlets.add(name); component.slot_outlets.add(name);
} }
if (!(parent.type === 'InlineComponent' || (parent.type === 'Element' && /-/.test(parent.name)))) { if (!(parent.type === 'InlineComponent' || within_custom_element(parent))) {
component.error(attribute, { component.error(attribute, {
code: `invalid-slotted-content`, code: `invalid-slotted-content`,
message: `Element with a slot='...' attribute must be a child of a component or custom element`, message: `Element with a slot='...' attribute must be a child of a component or custom element`,
@ -760,3 +760,12 @@ function should_have_attribute(
message: `A11y: <${name}> element should have ${article} ${sequence} attribute` message: `A11y: <${name}> element should have ${article} ${sequence} attribute`
}); });
} }
function within_custom_element(parent: INode) {
while (parent) {
if (parent.type === 'InlineComponent') return false;
if (parent.type === 'Element' && /-/.test(parent.name)) return true;
parent = parent.parent;
}
return false;
}

@ -0,0 +1,9 @@
[
{
"code": "invalid-slotted-content",
"message": "Element with a slot='...' attribute must be a child of a component or custom element",
"start": { "line": 10, "column": 9, "character": 138 },
"end": { "line": 10, "column": 19, "character": 148 },
"pos": 138
}
]

@ -0,0 +1,14 @@
<script>
import Nested from './Nested.svelte';
let thing = false;
</script>
<custom-element>
<Nested>
{#if thing}
<div>
<div slot='bar' />
</div>
{/if}
</Nested>
</custom-element>

@ -0,0 +1,15 @@
<script>
import Nested from './Nested.svelte';
let thing = false;
</script>
<Nested>
<custom-element>
<div>
<div slot='foo' />
</div>
{#if thing}
<div slot='bar' />
{/if}
</custom-element>
</Nested>
Loading…
Cancel
Save