Skip to content

Commit 1cbbcfe

Browse files
committed
Skip default slot if it contains only spaces (sveltejs#4546)
1 parent 148b610 commit 1cbbcfe

File tree

9 files changed

+52
-3
lines changed

9 files changed

+52
-3
lines changed

src/compiler/compile/render_dom/Block.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ export default class Block {
428428
this.has_animation;
429429
}
430430

431+
has_only_spaces(): boolean {
432+
return this.wrappers.every(w => w.has_only_spaces());
433+
}
434+
431435
render() {
432436
const key = this.key && this.get_unique_name('key');
433437

src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ export default class InlineComponentWrapper extends Wrapper {
151151

152152
// removing empty slot
153153
for (const slot of this.slots.keys()) {
154-
if (!this.slots.get(slot).block.has_content()) {
154+
const bl = this.slots.get(slot).block;
155+
156+
if (!bl.has_content() || (slot === 'default' && bl.has_only_spaces())) {
155157
this.renderer.remove_block(this.slots.get(slot).block);
156158
this.slots.delete(slot);
157159
}

src/compiler/compile/render_dom/wrappers/Text.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export default class TextWrapper extends Wrapper {
4040
return true;
4141
}
4242

43+
has_only_spaces() {
44+
return !/\S/.test(this.data);
45+
}
46+
4347
render(block: Block, parent_node: Identifier, parent_nodes: Identifier) {
4448
if (this.skip) return;
4549
const use_space = this.use_space();

src/compiler/compile/render_dom/wrappers/shared/Wrapper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export default class Wrapper {
8585
);
8686
}
8787

88+
has_only_spaces() {
89+
return false;
90+
}
91+
8892
render(_block: Block, _parent_node: Identifier, _parent_nodes: Identifier) {
8993
throw Error('Wrapper class is not renderable');
9094
}

src/compiler/compile/render_ssr/handlers/InlineComponent.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { get_slot_scope } from './shared/get_slot_scope';
44
import InlineComponent from '../../nodes/InlineComponent';
55
import remove_whitespace_children from './utils/remove_whitespace_children';
66
import { p, x } from 'code-red';
7+
import { TemplateLiteral } from 'estree';
8+
79

810
function get_prop_value(attribute) {
911
if (attribute.is_true) return x`true`;
@@ -85,7 +87,7 @@ export default function(node: InlineComponent, renderer: Renderer, options: Rend
8587
});
8688

8789
slot_scopes.forEach(({ input, output }, name) => {
88-
if (!is_empty_template_literal(output)) {
90+
if (!is_empty_template_literal(output) && (name !== 'default' || !has_only_spaces(output))) {
8991
slot_fns.push(
9092
p`${name}: (${input}) => ${output}`
9193
);
@@ -100,10 +102,16 @@ export default function(node: InlineComponent, renderer: Renderer, options: Rend
100102
renderer.add_expression(x`@validate_component(${expression}, "${node.name}").$$render($$result, ${props}, ${bindings}, ${slots})`);
101103
}
102104

103-
function is_empty_template_literal(template_literal) {
105+
function is_empty_template_literal(template_literal: TemplateLiteral) {
104106
return (
105107
template_literal.expressions.length === 0 &&
106108
template_literal.quasis.length === 1 &&
107109
template_literal.quasis[0].value.raw === ''
108110
);
109111
}
112+
113+
function has_only_spaces(template_literal: TemplateLiteral): boolean {
114+
return template_literal.expressions.length === 0 &&
115+
template_literal.quasis.length === 1 &&
116+
!/\S/.test(template_literal.quasis[0].value.raw);
117+
}

test/runtime/samples/component-slot-warning/main.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<Nested>
55
<input slot="slot1">
66
<input slot="slot2">
7+
unexpected_default
78
</Nested>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
3+
</script>
4+
5+
<slot name='slotA'/>
6+
<slot name='slotB'/>
7+
<slot>default value</slot>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
html: `
3+
<div slot='slotA'>A</div>
4+
<div slot='slotB'>B</div>
5+
default value
6+
`
7+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import Component from './Component.svelte';
3+
</script>
4+
5+
<Component>
6+
7+
8+
<div slot='slotA'>A</div>
9+
<div slot='slotB'>B</div>
10+
11+
12+
</Component>

0 commit comments

Comments
 (0)