Merge pull request #1017 from sveltejs/gh-1007

allow components without slots to have whitespace as only child
pull/1021/head
Rich Harris 8 years ago committed by GitHub
commit 05439df9c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -29,7 +29,7 @@ export default class Element extends Node {
this.cannotUseInnerHTML(); this.cannotUseInnerHTML();
} }
const parentElement = this.parent && this.parent.findNearest('Element'); const parentElement = this.parent && this.parent.findNearest(/^Element/);
this.namespace = this.name === 'svg' ? this.namespace = this.name === 'svg' ?
namespaces.svg : namespaces.svg :
parentElement ? parentElement.namespace : this.generator.namespace; parentElement ? parentElement.namespace : this.generator.namespace;
@ -133,7 +133,7 @@ export default class Element extends Node {
this.cannotUseInnerHTML(); this.cannotUseInnerHTML();
this.slotted = true; this.slotted = true;
// TODO validate slots — no nesting, no dynamic names... // TODO validate slots — no nesting, no dynamic names...
const component = this.findNearest('Component'); const component = this.findNearest(/^Component/);
component._slots.add(slot); component._slots.add(slot);
} }
@ -171,7 +171,7 @@ export default class Element extends Node {
const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot'); const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot');
const initialMountNode = this.slotted ? const initialMountNode = this.slotted ?
`${this.findNearest('Component').var}._slotted.${slot.value[0].data}` : // TODO this looks bonkers `${this.findNearest(/^Component/).var}._slotted.${slot.value[0].data}` : // TODO this looks bonkers
parentNode; parentNode;
block.addVariable(name); block.addVariable(name);

@ -17,15 +17,26 @@ const elementsWithoutText = new Set([
'video', 'video',
]); ]);
function shouldSkip(node: Text) {
if (/\S/.test(node.data)) return false;
const parentElement = node.findNearest(/(?:Element|Component)/);
if (!parentElement) return false;
if (parentElement.type === 'Component') return parentElement.children.length === 1 && node === parentElement.children[0];
return parentElement.namespace || elementsWithoutText.has(parentElement.name);
}
export default class Text extends Node { export default class Text extends Node {
type: 'Text'; type: 'Text';
data: string; data: string;
shouldSkip: boolean; shouldSkip: boolean;
init(block: Block) { init(block: Block) {
const parentElement = this.findNearest('Element'); const parentElement = this.findNearest(/(?:Element|Component)/);
if (!/\S/.test(this.data) && parentElement && (parentElement.namespace || elementsWithoutText.has(parentElement.name))) { if (shouldSkip(this)) {
this.shouldSkip = true; this.shouldSkip = true;
return; return;
} }

@ -133,9 +133,9 @@ export default class Node {
false; false;
} }
findNearest(type: string) { findNearest(selector: RegExp) {
if (this.type === type) return this; if (selector.test(this.type)) return this;
if (this.parent) return this.parent.findNearest(type); if (this.parent) return this.parent.findNearest(selector);
} }
getOrCreateAnchor(block: Block, parentNode: string) { getOrCreateAnchor(block: Block, parentNode: string) {

@ -0,0 +1,3 @@
export default {
html: '<p>no slot here</p>'
};

@ -0,0 +1,12 @@
<Nested>
</Nested>
<script>
import Nested from './Nested.html';
export default {
components: {
Nested
}
};
</script>
Loading…
Cancel
Save