From 76b6b1ac103266f183cc1148bde82c628df16fcc Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sat, 11 Jan 2020 22:43:17 +0800 Subject: [PATCH 1/3] fix hydrating each else --- .../compile/render_dom/wrappers/EachBlock.ts | 13 +++++++++++++ test/hydration/samples/each-else/_after.html | 4 ++++ test/hydration/samples/each-else/_before.html | 4 ++++ test/hydration/samples/each-else/main.svelte | 15 +++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 test/hydration/samples/each-else/_after.html create mode 100644 test/hydration/samples/each-else/_before.html create mode 100644 test/hydration/samples/each-else/main.svelte diff --git a/src/compiler/compile/render_dom/wrappers/EachBlock.ts b/src/compiler/compile/render_dom/wrappers/EachBlock.ts index 5b13b486e61d..238fe917fae7 100644 --- a/src/compiler/compile/render_dom/wrappers/EachBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/EachBlock.ts @@ -264,10 +264,23 @@ export default class EachBlockWrapper extends Wrapper { block.chunks.init.push(b` if (!${this.vars.data_length}) { ${each_block_else} = ${this.else.block.name}(#ctx); + } + `); + + block.chunks.create.push(b` + if (${each_block_else}) { ${each_block_else}.c(); } `); + if (component.compile_options.hydratable) { + block.chunks.claim.push(b` + if (${each_block_else}) { + ${each_block_else}.l(${parent_nodes}); + } + `); + } + block.chunks.mount.push(b` if (${each_block_else}) { ${each_block_else}.m(${initial_mount_node}, ${initial_anchor_node}); diff --git a/test/hydration/samples/each-else/_after.html b/test/hydration/samples/each-else/_after.html new file mode 100644 index 000000000000..7920500ec3fe --- /dev/null +++ b/test/hydration/samples/each-else/_after.html @@ -0,0 +1,4 @@ +

Hello, world

+

+ weird +

\ No newline at end of file diff --git a/test/hydration/samples/each-else/_before.html b/test/hydration/samples/each-else/_before.html new file mode 100644 index 000000000000..7920500ec3fe --- /dev/null +++ b/test/hydration/samples/each-else/_before.html @@ -0,0 +1,4 @@ +

Hello, world

+

+ weird +

\ No newline at end of file diff --git a/test/hydration/samples/each-else/main.svelte b/test/hydration/samples/each-else/main.svelte new file mode 100644 index 000000000000..64d3a37c5878 --- /dev/null +++ b/test/hydration/samples/each-else/main.svelte @@ -0,0 +1,15 @@ + + +

Hello, {name}

+{#each array as elem} +

+ item +

+{:else} +

+ weird +

+{/each} From eff4b287d92e3eb0b606df65562e6755f3e7a4bd Mon Sep 17 00:00:00 2001 From: Conduitry Date: Sun, 12 Jan 2020 15:34:20 -0500 Subject: [PATCH 2/3] tidy --- src/compiler/compile/render_dom/wrappers/EachBlock.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/compile/render_dom/wrappers/EachBlock.ts b/src/compiler/compile/render_dom/wrappers/EachBlock.ts index 238fe917fae7..4928b5a38cd4 100644 --- a/src/compiler/compile/render_dom/wrappers/EachBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/EachBlock.ts @@ -273,7 +273,7 @@ export default class EachBlockWrapper extends Wrapper { } `); - if (component.compile_options.hydratable) { + if (this.renderer.options.hydratable) { block.chunks.claim.push(b` if (${each_block_else}) { ${each_block_else}.l(${parent_nodes}); From 2a21f55a90b2988f111b8bfac1c240c7fd16d13e Mon Sep 17 00:00:00 2001 From: Conduitry Date: Sun, 12 Jan 2020 15:37:36 -0500 Subject: [PATCH 3/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e5d5df95f6a..aeb917efac58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Prevent text input cursor jumping in Safari with one-way binding ([#3449](https://github.com/sveltejs/svelte/issues/3449)) * Expose compiler version in dev events ([#4047](https://github.com/sveltejs/svelte/issues/4047)) * Fix reactive assignments with destructuring and stores where the destructured value should be undefined ([#4170](https://github.com/sveltejs/svelte/issues/4170)) +* Fix hydrating `{:else}` in `{#each}` ([#4202](https://github.com/sveltejs/svelte/issues/4202)) * Do not automatically declare variables in reactive declarations when assigning to a member expression ([#4212](https://github.com/sveltejs/svelte/issues/4212)) ## 3.16.7