Strip out whitespace inside each when it has an animation

pull/5477/head
Andreas Ehrencrona 6 years ago
parent ff6ce725bf
commit 7cd143374a

@ -9,6 +9,7 @@ import { Node } from 'estree';
import Component from '../Component'; import Component from '../Component';
import { TemplateNode } from '../../interfaces'; import { TemplateNode } from '../../interfaces';
import compiler_errors from '../compiler_errors'; import compiler_errors from '../compiler_errors';
import { INode } from './interfaces';
export default class EachBlock extends AbstractBlock { export default class EachBlock extends AbstractBlock {
type: 'EachBlock'; type: 'EachBlock';
@ -60,6 +61,8 @@ export default class EachBlock extends AbstractBlock {
this.children = map_children(component, this, this.scope, info.children); this.children = map_children(component, this, this.scope, info.children);
if (this.has_animation) { if (this.has_animation) {
this.children = this.children.filter(child => !isEmptyNode(child));
if (this.children.length !== 1) { if (this.children.length !== 1) {
const child = this.children.find(child => !!(child as Element).animation); const child = this.children.find(child => !!(child as Element).animation);
component.error((child as Element).animation, compiler_errors.invalid_animation_sole); component.error((child as Element).animation, compiler_errors.invalid_animation_sole);
@ -74,3 +77,7 @@ export default class EachBlock extends AbstractBlock {
: null; : null;
} }
} }
function isEmptyNode(node: INode) {
return node.type === 'Text' && node.data.trim() === '';
}

@ -0,0 +1 @@
<div>{#each [] as n (n)} <div animate:flip /> {/each}</div>

@ -0,0 +1,94 @@
{
"html": {
"start": 0,
"end": 59,
"type": "Fragment",
"children": [
{
"start": 0,
"end": 59,
"type": "Element",
"name": "div",
"attributes": [],
"children": [
{
"start": 5,
"end": 53,
"type": "EachBlock",
"expression": {
"type": "ArrayExpression",
"start": 12,
"end": 14,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 14
}
},
"elements": []
},
"children": [
{
"start": 24,
"end": 25,
"type": "Text",
"raw": " ",
"data": " "
},
{
"start": 25,
"end": 45,
"type": "Element",
"name": "div",
"attributes": [
{
"start": 30,
"end": 42,
"type": "Animation",
"name": "flip",
"modifiers": [],
"expression": null
}
],
"children": []
},
{
"start": 45,
"end": 46,
"type": "Text",
"raw": " ",
"data": " "
}
],
"context": {
"type": "Identifier",
"name": "n",
"start": 18,
"end": 19
},
"key": {
"type": "Identifier",
"start": 21,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 22
}
},
"name": "n"
}
}
]
}
]
}
}

@ -0,0 +1,5 @@
<style>
</style>
sssweet
Loading…
Cancel
Save