From 4f88e43c377ad483f0f67993c82ebd1230d66018 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 17 Oct 2018 10:51:27 -0400 Subject: [PATCH] handle elseif blocks with no else --- src/compile/render-dom/wrappers/IfBlock.ts | 3 +-- .../if-block-elseif-no-else/_config.js | 21 +++++++++++++++++++ .../samples/if-block-elseif-no-else/main.html | 5 +++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/runtime/samples/if-block-elseif-no-else/_config.js create mode 100644 test/runtime/samples/if-block-elseif-no-else/main.html diff --git a/src/compile/render-dom/wrappers/IfBlock.ts b/src/compile/render-dom/wrappers/IfBlock.ts index e45b17bcfd..284cd241de 100644 --- a/src/compile/render-dom/wrappers/IfBlock.ts +++ b/src/compile/render-dom/wrappers/IfBlock.ts @@ -221,7 +221,6 @@ export default class IfBlockWrapper extends Wrapper { ${this.branches .map(({ condition, block }) => `${condition ? `if (${condition}) ` : ''}return ${block.name};`) .join('\n')} - ${!hasElse && `return -1;`} } `); @@ -297,7 +296,7 @@ export default class IfBlockWrapper extends Wrapper { function ${select_block_type}(ctx) { ${this.branches - .map(({ condition, block }, i) => `${condition ? `if (${condition}) ` : ''}return ${block ? i : -1};`) + .map(({ condition }, i) => `${condition ? `if (${condition}) ` : ''}return ${i};`) .join('\n')} ${!hasElse && `return -1;`} } diff --git a/test/runtime/samples/if-block-elseif-no-else/_config.js b/test/runtime/samples/if-block-elseif-no-else/_config.js new file mode 100644 index 0000000000..a9b3b9c6ef --- /dev/null +++ b/test/runtime/samples/if-block-elseif-no-else/_config.js @@ -0,0 +1,21 @@ +export default { + data: { + x: 11 + }, + + html: ` +

x is greater than 10

+ `, + + test(assert, component, target) { + component.set({ x: 4 }); + assert.htmlEqual(target.innerHTML, ` +

x is less than 5

+ `); + + component.set({ x: 6 }); + assert.htmlEqual(target.innerHTML, ``); + + component.destroy(); + } +}; diff --git a/test/runtime/samples/if-block-elseif-no-else/main.html b/test/runtime/samples/if-block-elseif-no-else/main.html new file mode 100644 index 0000000000..a3bd474af7 --- /dev/null +++ b/test/runtime/samples/if-block-elseif-no-else/main.html @@ -0,0 +1,5 @@ +{#if x > 10} +

x is greater than 10

+{:elseif x < 5} +

x is less than 5

+{/if}