Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/compiler/compile/render_dom/wrappers/IfBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ export default class IfBlockWrapper extends Wrapper {
if (this.needs_update) {
block.chunks.init.push(b`
function ${select_block_type}(#ctx, #dirty) {
${this.branches.map(({ dependencies, condition, snippet, block }) => condition
${this.branches.map(({ dependencies, condition, snippet }) => {
return b`${snippet && dependencies.length > 0 ? b`if (${block.renderer.dirty(dependencies)}) ${condition} = null;` : null}`;
})}
${this.branches.map(({ condition, snippet, block }) => condition
? b`
${snippet && (
dependencies.length > 0
? b`if (${condition} == null || ${block.renderer.dirty(dependencies)}) ${condition} = !!${snippet}`
: b`if (${condition} == null) ${condition} = !!${snippet}`
${snippet && b`if (${condition} == null) ${condition} = !!${snippet}`}
if (${condition}) return ${block.name};`
: b`return ${block.name};`
)}
if (${condition}) return ${block.name};`
: b`return ${block.name};`)}
}
`);
} else {
Expand Down Expand Up @@ -387,13 +387,12 @@ export default class IfBlockWrapper extends Wrapper {
${this.needs_update
? b`
function ${select_block_type}(#ctx, #dirty) {
${this.branches.map(({ dependencies, condition, snippet }, i) => condition
${this.branches.map(({ dependencies, condition, snippet }) => {
return b`${snippet && dependencies.length > 0 ? b`if (${block.renderer.dirty(dependencies)}) ${condition} = null;` : null}`;
})}
${this.branches.map(({ condition, snippet }, i) => condition
? b`
${snippet && (
dependencies.length > 0
? b`if (${condition} == null || ${block.renderer.dirty(dependencies)}) ${condition} = !!${snippet}`
: b`if (${condition} == null) ${condition} = !!${snippet}`
)}
${snippet && b`if (${condition} == null) ${condition} = !!${snippet}`}
if (${condition}) return ${i};`
: b`return ${i};`)}
${!has_else && b`return -1;`}
Expand Down
46 changes: 46 additions & 0 deletions test/runtime/samples/if-block-else-update/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export default {
async test({ assert, component, target, window }) {
const [btn1, btn2] = target.querySelectorAll('button');

const clickEvent = new window.MouseEvent('click');

await btn2.dispatchEvent(clickEvent);
assert.htmlEqual(target.innerHTML, `
<button>Toggle foo</button>
<button>Toggle bar</button>
<hr>
foo: false, bar: true
<hr>
bar!
`);

await btn1.dispatchEvent(clickEvent);
assert.htmlEqual(target.innerHTML, `
<button>Toggle foo</button>
<button>Toggle bar</button>
<hr>
foo: true, bar: true
<hr>
foo!
`);

await btn2.dispatchEvent(clickEvent);
assert.htmlEqual(target.innerHTML, `
<button>Toggle foo</button>
<button>Toggle bar</button>
<hr>
foo: true, bar: false
<hr>
foo!
`);

await btn1.dispatchEvent(clickEvent);
assert.htmlEqual(target.innerHTML, `
<button>Toggle foo</button>
<button>Toggle bar</button>
<hr>
foo: false, bar: false
<hr>
`);
}
};
21 changes: 21 additions & 0 deletions test/runtime/samples/if-block-else-update/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script>
let foo = false
let bar = [false];
</script>

<button on:click={() => foo = !foo}>
Toggle foo
</button>
<button on:click={() => bar[0] = !bar[0]}>
Toggle bar
</button>

<hr>
{@html `foo: ${foo}, bar: ${bar.every(x => x)}`}
<hr>

{#if foo}
foo!
{:else if bar.every(x => x)}
bar!
{/if}