Skip to content

Commit

Permalink
allow components without slots to have whitespace as only child - fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 13, 2017
1 parent 76356ce commit 9377331
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/generators/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class Element extends Node {
this.cannotUseInnerHTML();
}

const parentElement = this.parent && this.parent.findNearest('Element');
const parentElement = this.parent && this.parent.findNearest(/^Element/);
this.namespace = this.name === 'svg' ?
namespaces.svg :
parentElement ? parentElement.namespace : this.generator.namespace;
Expand Down Expand Up @@ -133,7 +133,7 @@ export default class Element extends Node {
this.cannotUseInnerHTML();
this.slotted = true;
// TODO validate slots — no nesting, no dynamic names...
const component = this.findNearest('Component');
const component = this.findNearest(/^Component/);
component._slots.add(slot);
}

Expand Down Expand Up @@ -171,7 +171,7 @@ export default class Element extends Node {

const slot = this.attributes.find((attribute: Node) => attribute.name === 'slot');
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;

block.addVariable(name);
Expand Down
15 changes: 13 additions & 2 deletions src/generators/nodes/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@ const elementsWithoutText = new Set([
'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 {
type: 'Text';
data: string;
shouldSkip: boolean;

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;
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/generators/nodes/shared/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ export default class Node {
false;
}

findNearest(type: string) {
if (this.type === type) return this;
if (this.parent) return this.parent.findNearest(type);
findNearest(selector: RegExp) {
if (selector.test(this.type)) return this;
if (this.parent) return this.parent.findNearest(selector);
}

getOrCreateAnchor(block: Block, parentNode: string) {
Expand Down
1 change: 1 addition & 0 deletions test/runtime/samples/component-slot-empty/Nested.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>no slot here</p>
3 changes: 3 additions & 0 deletions test/runtime/samples/component-slot-empty/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
html: '<p>no slot here</p>'
};
12 changes: 12 additions & 0 deletions test/runtime/samples/component-slot-empty/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Nested>
</Nested>

<script>
import Nested from './Nested.html';

export default {
components: {
Nested
}
};
</script>

0 comments on commit 9377331

Please sign in to comment.