Skip to content

Commit a4e4027

Browse files
ehrencronadummdidummtanhauhau
authored
Handle whitespace inside #each with animation (#5477)
* Strip out whitespace inside each when it has an animation * remove accidentally committed file * lint * add test to validate no error * update changelog Co-authored-by: Simon H <[email protected]> Co-authored-by: tanhauhau <[email protected]> Co-authored-by: Tan Li Hau <[email protected]>
1 parent e4a3a87 commit a4e4027

File tree

6 files changed

+30
-0
lines changed

6 files changed

+30
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Svelte changelog
22

3+
## Unreleased
4+
5+
* Ignore whitespace in `{#each}` blocks when containing elements with `animate:` ([#5477](https://github.com/sveltejs/svelte/pull/5477))
6+
37
## 3.46.2
48

59
* Export `FlipParams` interface from `svelte/animate` ([#7103](https://github.com/sveltejs/svelte/issues/7103))

src/compiler/compile/nodes/EachBlock.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Node } from 'estree';
99
import Component from '../Component';
1010
import { TemplateNode } from '../../interfaces';
1111
import compiler_errors from '../compiler_errors';
12+
import { INode } from './interfaces';
1213
import get_const_tags from './shared/get_const_tags';
1314

1415
export default class EachBlock extends AbstractBlock {
@@ -62,6 +63,8 @@ export default class EachBlock extends AbstractBlock {
6263
([this.const_tags, this.children] = get_const_tags(info.children, component, this, this));
6364

6465
if (this.has_animation) {
66+
this.children = this.children.filter(child => !isEmptyNode(child));
67+
6568
if (this.children.length !== 1) {
6669
const child = this.children.find(child => !!(child as Element).animation);
6770
component.error((child as Element).animation, compiler_errors.invalid_animation_sole);
@@ -76,3 +79,7 @@ export default class EachBlock extends AbstractBlock {
7679
: null;
7780
}
7881
}
82+
83+
function isEmptyNode(node: INode) {
84+
return node.type === 'Text' && node.data.trim() === '';
85+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
function flip(){}
3+
</script>
4+
5+
<div>
6+
{#each [] as n (n)}
7+
{@const a = n}
8+
<div animate:flip={a} />
9+
{/each}
10+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
function flip() {}
3+
</script>
4+
5+
<div>
6+
{#each [] as n (n)} <div animate:flip /> {/each}
7+
</div>

0 commit comments

Comments
 (0)