Skip to content

Commit 05439df

Browse files
authored
Merge pull request #1017 from sveltejs/gh-1007
allow components without slots to have whitespace as only child
2 parents 76356ce + 9377331 commit 05439df

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

src/generators/nodes/Element.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class Element extends Node {
2929
this.cannotUseInnerHTML();
3030
}
3131

32-
const parentElement = this.parent && this.parent.findNearest('Element');
32+
const parentElement = this.parent && this.parent.findNearest(/^Element/);
3333
this.namespace = this.name === 'svg' ?
3434
namespaces.svg :
3535
parentElement ? parentElement.namespace : this.generator.namespace;
@@ -133,7 +133,7 @@ export default class Element extends Node {
133133
this.cannotUseInnerHTML();
134134
this.slotted = true;
135135
// TODO validate slots — no nesting, no dynamic names...
136-
const component = this.findNearest('Component');
136+
const component = this.findNearest(/^Component/);
137137
component._slots.add(slot);
138138
}
139139

@@ -171,7 +171,7 @@ export default class Element extends Node {
171171

172172
const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot');
173173
const initialMountNode = this.slotted ?
174-
`${this.findNearest('Component').var}._slotted.${slot.value[0].data}` : // TODO this looks bonkers
174+
`${this.findNearest(/^Component/).var}._slotted.${slot.value[0].data}` : // TODO this looks bonkers
175175
parentNode;
176176

177177
block.addVariable(name);

src/generators/nodes/Text.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,26 @@ const elementsWithoutText = new Set([
1717
'video',
1818
]);
1919

20+
function shouldSkip(node: Text) {
21+
if (/\S/.test(node.data)) return false;
22+
23+
const parentElement = node.findNearest(/(?:Element|Component)/);
24+
if (!parentElement) return false;
25+
26+
if (parentElement.type === 'Component') return parentElement.children.length === 1 && node === parentElement.children[0];
27+
28+
return parentElement.namespace || elementsWithoutText.has(parentElement.name);
29+
}
30+
2031
export default class Text extends Node {
2132
type: 'Text';
2233
data: string;
2334
shouldSkip: boolean;
2435

2536
init(block: Block) {
26-
const parentElement = this.findNearest('Element');
37+
const parentElement = this.findNearest(/(?:Element|Component)/);
2738

28-
if (!/\S/.test(this.data) && parentElement && (parentElement.namespace || elementsWithoutText.has(parentElement.name))) {
39+
if (shouldSkip(this)) {
2940
this.shouldSkip = true;
3041
return;
3142
}

src/generators/nodes/shared/Node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ export default class Node {
133133
false;
134134
}
135135

136-
findNearest(type: string) {
137-
if (this.type === type) return this;
138-
if (this.parent) return this.parent.findNearest(type);
136+
findNearest(selector: RegExp) {
137+
if (selector.test(this.type)) return this;
138+
if (this.parent) return this.parent.findNearest(selector);
139139
}
140140

141141
getOrCreateAnchor(block: Block, parentNode: string) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>no slot here</p>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
html: '<p>no slot here</p>'
3+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Nested>
2+
</Nested>
3+
4+
<script>
5+
import Nested from './Nested.html';
6+
7+
export default {
8+
components: {
9+
Nested
10+
}
11+
};
12+
</script>

0 commit comments

Comments
 (0)