Skip to content

Commit fee4d35

Browse files
btk5hConduitry
authored andcommittedJun 21, 2019
preserve whitespace at each block boundaries (#713)
1 parent 5d51d50 commit fee4d35

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed
 

‎src/compiler/compile/render-dom/wrappers/Fragment.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ function link(next: Wrapper, prev: Wrapper) {
4242
if (next) next.prev = prev;
4343
}
4444

45+
function trimmable_at(child: INode, next_sibling: Wrapper): boolean {
46+
// Whitespace is trimmable if one of the following is true:
47+
// The child and its sibling share a common nearest each block (not at an each block boundary)
48+
// The next sibling's previous node is an each block
49+
return (next_sibling.node.find_nearest(/EachBlock/) === child.find_nearest(/EachBlock/)) || next_sibling.node.prev.type === 'EachBlock';
50+
}
51+
4552
export default class FragmentWrapper {
4653
nodes: Wrapper[];
4754

@@ -85,7 +92,7 @@ export default class FragmentWrapper {
8592
if (this.nodes.length === 0) {
8693
const should_trim = (
8794
// @ts-ignore todo: probably error, should it be next_sibling.node.data?
88-
next_sibling ? (next_sibling.node.type === 'Text' && /^\s/.test(next_sibling.data)) : !child.has_ancestor('EachBlock')
95+
next_sibling ? (next_sibling.node.type === 'Text' && /^\s/.test(next_sibling.data) && trimmable_at(child, next_sibling)) : !child.has_ancestor('EachBlock')
8996
);
9097

9198
if (should_trim) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const message = "the quick brown fox jumps over the lazy dog";
2+
const expected = [...message].map(c => `<span>${c + " "}</span>`).join("");
3+
4+
export default {
5+
props: {
6+
message
7+
},
8+
9+
async test({ assert, target }) {
10+
const firstSpanList = target.children[0];
11+
assert.equal(firstSpanList.innerHTML, expected);
12+
13+
const secondSpanList = target.children[1];
14+
assert.equal(secondSpanList.innerHTML, expected);
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
let message = "the quick brown fox jumps over the lazy dog"
3+
</script>
4+
5+
<div id="first">
6+
{#each message as char}
7+
<span>{char} </span>
8+
{/each}
9+
</div>
10+
11+
<div id="second">{#each message as char}<span>{char} </span>{/each}</div>

0 commit comments

Comments
 (0)
Please sign in to comment.