From 7cd143374aad53e29fc24a3275d1a4a86a3872bd Mon Sep 17 00:00:00 2001 From: Andreas Ehrencrona Date: Wed, 30 Sep 2020 15:13:08 +0200 Subject: [PATCH] Strip out whitespace inside each when it has an animation --- src/compiler/compile/nodes/EachBlock.ts | 7 ++ .../input.svelte | 1 + .../output.json | 94 +++++++++++++++++++ test/parser/samples/ssswell/input.svelte | 5 + 4 files changed, 107 insertions(+) create mode 100644 test/parser/samples/animation-each-with-whitespace/input.svelte create mode 100644 test/parser/samples/animation-each-with-whitespace/output.json create mode 100644 test/parser/samples/ssswell/input.svelte diff --git a/src/compiler/compile/nodes/EachBlock.ts b/src/compiler/compile/nodes/EachBlock.ts index c03128c388..e83a862227 100644 --- a/src/compiler/compile/nodes/EachBlock.ts +++ b/src/compiler/compile/nodes/EachBlock.ts @@ -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'; export default class EachBlock extends AbstractBlock { type: 'EachBlock'; @@ -60,6 +61,8 @@ export default class EachBlock extends AbstractBlock { this.children = map_children(component, this, this.scope, info.children); 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); @@ -74,3 +77,7 @@ export default class EachBlock extends AbstractBlock { : null; } } + +function isEmptyNode(node: INode) { + return node.type === 'Text' && node.data.trim() === ''; +} \ No newline at end of file diff --git a/test/parser/samples/animation-each-with-whitespace/input.svelte b/test/parser/samples/animation-each-with-whitespace/input.svelte new file mode 100644 index 0000000000..ca12ffa82d --- /dev/null +++ b/test/parser/samples/animation-each-with-whitespace/input.svelte @@ -0,0 +1 @@ +
{#each [] as n (n)}
{/each}
\ No newline at end of file diff --git a/test/parser/samples/animation-each-with-whitespace/output.json b/test/parser/samples/animation-each-with-whitespace/output.json new file mode 100644 index 0000000000..da9d7feb9e --- /dev/null +++ b/test/parser/samples/animation-each-with-whitespace/output.json @@ -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" + } + } + ] + } + ] + } +} \ No newline at end of file diff --git a/test/parser/samples/ssswell/input.svelte b/test/parser/samples/ssswell/input.svelte new file mode 100644 index 0000000000..d24d5b4576 --- /dev/null +++ b/test/parser/samples/ssswell/input.svelte @@ -0,0 +1,5 @@ + + +sssweet \ No newline at end of file