Skip to content

Commit 94037f1

Browse files
authored
Merge pull request #718 from sveltejs/gh-713
don't strip whitespace at the end of an each block
2 parents 1474556 + b9d8263 commit 94037f1

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

src/generators/dom/preprocess.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const preprocessors = {
9292
block: Block,
9393
state: State,
9494
node: Node,
95+
inEachBlock: boolean,
9596
elementStack: Node[],
9697
stripWhitespace: boolean,
9798
nextSibling: Node
@@ -112,7 +113,7 @@ const preprocessors = {
112113
node._state = getChildState(state);
113114

114115
blocks.push(node._block);
115-
preprocessChildren(generator, node._block, node._state, node, elementStack, stripWhitespace, nextSibling);
116+
preprocessChildren(generator, node._block, node._state, node, inEachBlock, elementStack, stripWhitespace, nextSibling);
116117

117118
if (node._block.dependencies.size > 0) {
118119
dynamic = true;
@@ -137,6 +138,7 @@ const preprocessors = {
137138
node.else._block,
138139
node.else._state,
139140
node.else,
141+
inEachBlock,
140142
elementStack,
141143
stripWhitespace,
142144
nextSibling
@@ -165,6 +167,7 @@ const preprocessors = {
165167
block: Block,
166168
state: State,
167169
node: Node,
170+
inEachBlock: boolean,
168171
elementStack: Node[],
169172
stripWhitespace: boolean,
170173
nextSibling: Node
@@ -214,7 +217,7 @@ const preprocessors = {
214217
});
215218

216219
generator.blocks.push(node._block);
217-
preprocessChildren(generator, node._block, node._state, node, elementStack, stripWhitespace, nextSibling);
220+
preprocessChildren(generator, node._block, node._state, node, true, elementStack, stripWhitespace, nextSibling);
218221
block.addDependencies(node._block.dependencies);
219222
node._block.hasUpdateMethod = node._block.dependencies.size > 0;
220223

@@ -231,6 +234,7 @@ const preprocessors = {
231234
node.else._block,
232235
node.else._state,
233236
node.else,
237+
inEachBlock,
234238
elementStack,
235239
stripWhitespace,
236240
nextSibling
@@ -244,6 +248,7 @@ const preprocessors = {
244248
block: Block,
245249
state: State,
246250
node: Node,
251+
inEachBlock: boolean,
247252
elementStack: Node[],
248253
stripWhitespace: boolean,
249254
nextSibling: Node
@@ -344,12 +349,12 @@ const preprocessors = {
344349
});
345350

346351
generator.blocks.push(node._block);
347-
preprocessChildren(generator, node._block, node._state, node, elementStack, stripWhitespace, nextSibling);
352+
preprocessChildren(generator, node._block, node._state, node, inEachBlock, elementStack, stripWhitespace, nextSibling);
348353
block.addDependencies(node._block.dependencies);
349354
node._block.hasUpdateMethod = node._block.dependencies.size > 0;
350355
} else {
351356
if (node.name === 'pre' || node.name === 'textarea') stripWhitespace = false;
352-
preprocessChildren(generator, block, node._state, node, elementStack.concat(node), stripWhitespace, nextSibling);
357+
preprocessChildren(generator, block, node._state, node, inEachBlock, elementStack.concat(node), stripWhitespace, nextSibling);
353358
}
354359
}
355360
},
@@ -360,6 +365,7 @@ function preprocessChildren(
360365
block: Block,
361366
state: State,
362367
node: Node,
368+
inEachBlock: boolean,
363369
elementStack: Node[],
364370
stripWhitespace: boolean,
365371
nextSibling: Node
@@ -390,7 +396,7 @@ function preprocessChildren(
390396

391397
cleaned.forEach((child: Node, i: number) => {
392398
const preprocessor = preprocessors[child.type];
393-
if (preprocessor) preprocessor(generator, block, state, child, elementStack, stripWhitespace, cleaned[i + 1] || nextSibling);
399+
if (preprocessor) preprocessor(generator, block, state, child, inEachBlock, elementStack, stripWhitespace, cleaned[i + 1] || nextSibling);
394400

395401
if (lastChild) {
396402
lastChild.next = child;
@@ -402,8 +408,12 @@ function preprocessChildren(
402408

403409
// We want to remove trailing whitespace inside an element/component/block,
404410
// *unless* there is no whitespace between this node and its next sibling
405-
if (lastChild && lastChild.type === 'Text') {
406-
if (stripWhitespace && (!nextSibling || (nextSibling.type === 'Text' && /^\s/.test(nextSibling.data)))) {
411+
if (stripWhitespace && lastChild && lastChild.type === 'Text') {
412+
const shouldTrim = (
413+
nextSibling ? (nextSibling.type === 'Text' && /^\s/.test(nextSibling.data)) : !inEachBlock
414+
);
415+
416+
if (shouldTrim) {
407417
lastChild.data = trimEnd(lastChild.data);
408418
if (!lastChild.data) {
409419
cleaned.pop();
@@ -449,7 +459,7 @@ export default function preprocess(
449459
};
450460

451461
generator.blocks.push(block);
452-
preprocessChildren(generator, block, state, node, [], true, null);
462+
preprocessChildren(generator, block, state, node, false, [], true, null);
453463
block.hasUpdateMethod = block.dependencies.size > 0;
454464

455465
return { block, state };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default {
2+
data: {
3+
name: 'world'
4+
},
5+
6+
test ( assert, component, target ) {
7+
assert.equal(
8+
target.textContent,
9+
`Hello world! How are you?`
10+
);
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Hello <strong>{{name}}! </strong><span>How are you?</span></h1>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
export default {
22
data: {
3-
name: 'world'
3+
characters: ['a', 'b', 'c']
44
},
55

66
test ( assert, component, target ) {
77
assert.equal(
88
target.textContent,
9-
`Hello world! How are you?`
9+
`a b c `
1010
);
1111
}
1212
};
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
<h1>Hello <strong>{{name}}! </strong><span>How are you?</span></h1>
1+
{{#each characters as char}}
2+
<span>{{char}} </span>
3+
{{/each}}

0 commit comments

Comments
 (0)