-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
136 additions
and
46 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/compiler/compile/render_dom/wrappers/Element/create_slot_block.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import ElementWrapper from './index'; | ||
import SlotWrapper from '../Slot'; | ||
import Block from '../../Block'; | ||
import { sanitize } from '../../../../utils/names'; | ||
import InlineComponentWrapper from '../InlineComponent'; | ||
import create_debugging_comment from '../shared/create_debugging_comment'; | ||
import { get_slot_definition } from '../shared/get_slot_definition'; | ||
|
||
export default function create_slot_block(attribute, element: ElementWrapper | SlotWrapper, block: Block) { | ||
const owner = find_slot_owner(element.parent); | ||
|
||
if (owner && owner.node.type === 'InlineComponent') { | ||
const name = attribute.get_static_value() as string; | ||
|
||
if (!((owner as unknown) as InlineComponentWrapper).slots.has(name)) { | ||
const child_block = block.child({ | ||
comment: create_debugging_comment(element.node, element.renderer.component), | ||
name: element.renderer.component.get_unique_name( | ||
`create_${sanitize(name)}_slot` | ||
), | ||
type: 'slot', | ||
}); | ||
|
||
const { scope, lets } = element.node; | ||
const seen = new Set(lets.map(l => l.name.name)); | ||
|
||
((owner as unknown) as InlineComponentWrapper).node.lets.forEach(l => { | ||
if (!seen.has(l.name.name)) lets.push(l); | ||
}); | ||
|
||
((owner as unknown) as InlineComponentWrapper).slots.set( | ||
name, | ||
get_slot_definition(child_block, scope, lets) | ||
); | ||
element.renderer.blocks.push(child_block); | ||
} | ||
|
||
element.slot_block = ((owner as unknown) as InlineComponentWrapper).slots.get( | ||
name | ||
).block; | ||
|
||
return element.slot_block; | ||
} | ||
|
||
return block; | ||
} | ||
|
||
function find_slot_owner(owner) { | ||
while (owner) { | ||
if (owner.node.type === 'InlineComponent') { | ||
break; | ||
} | ||
|
||
if (owner.node.type === 'Element' && /-/.test(owner.node.name)) { | ||
break; | ||
} | ||
|
||
owner = owner.parent; | ||
} | ||
return owner; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
test/runtime/samples/component-slot-nested-in-slot/One.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script> | ||
import Two from './Two.svelte'; | ||
export let a, b; | ||
</script> | ||
|
||
<Two {b} let:two> | ||
<slot name="one" slot="two" one={a} two={two}></slot> | ||
</Two> |
4 changes: 4 additions & 0 deletions
4
test/runtime/samples/component-slot-nested-in-slot/Two.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<script> | ||
export let b; | ||
</script> | ||
<slot name="two" two={b}></slot> |
18 changes: 18 additions & 0 deletions
18
test/runtime/samples/component-slot-nested-in-slot/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export default { | ||
html: ` | ||
<p slot='one'>one: 1 two: 2</p> | ||
`, | ||
test({ assert, component, target }) { | ||
component.a = 3; | ||
component.b = 4; | ||
assert.htmlEqual(target.innerHTML, ` | ||
<p slot='one'>one: 3 two: 4</p> | ||
`); | ||
|
||
component.a = 5; | ||
component.b = 6; | ||
assert.htmlEqual(target.innerHTML, ` | ||
<p slot='one'>one: 5 two: 6</p> | ||
`); | ||
} | ||
}; |
9 changes: 9 additions & 0 deletions
9
test/runtime/samples/component-slot-nested-in-slot/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script> | ||
import One from './One.svelte'; | ||
export let a = 1; | ||
export let b = 2; | ||
</script> | ||
|
||
<One {a} {b}> | ||
<p slot='one' let:one let:two>one: {one} two: {two}</p> | ||
</One> |