Skip to content

Commit

Permalink
Handle whitespace inside #each with animation (#5477)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
4 people authored Jan 15, 2022
1 parent e4a3a87 commit a4e4027
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Svelte changelog

## Unreleased

* Ignore whitespace in `{#each}` blocks when containing elements with `animate:` ([#5477](https://github.com/sveltejs/svelte/pull/5477))

## 3.46.2

* Export `FlipParams` interface from `svelte/animate` ([#7103](https://github.com/sveltejs/svelte/issues/7103))
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/compile/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Node } from 'estree';
import Component from '../Component';
import { TemplateNode } from '../../interfaces';
import compiler_errors from '../compiler_errors';
import { INode } from './interfaces';
import get_const_tags from './shared/get_const_tags';

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

if (this.has_animation) {
this.children = this.children.filter(child => !isEmptyNode(child));

if (this.children.length !== 1) {
const child = this.children.find(child => !!(child as Element).animation);
component.error((child as Element).animation, compiler_errors.invalid_animation_sole);
Expand All @@ -76,3 +79,7 @@ export default class EachBlock extends AbstractBlock {
: null;
}
}

function isEmptyNode(node: INode) {
return node.type === 'Text' && node.data.trim() === '';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
10 changes: 10 additions & 0 deletions test/validator/samples/animation-each-with-const/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
function flip(){}
</script>

<div>
{#each [] as n (n)}
{@const a = n}
<div animate:flip={a} />
{/each}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
function flip() {}
</script>

<div>
{#each [] as n (n)} <div animate:flip /> {/each}
</div>

0 comments on commit a4e4027

Please sign in to comment.