diff --git a/src/tags/case.ts b/src/tags/case.ts index bf940cafde..223701de69 100644 --- a/src/tags/case.ts +++ b/src/tags/case.ts @@ -35,7 +35,6 @@ export default class extends Tag { }) }) .on('tag:else', () => { - p = [] elseCount++ p = this.elseTemplates }) diff --git a/src/tags/if.ts b/src/tags/if.ts index 779b7e58b8..c42833fdea 100644 --- a/src/tags/if.ts +++ b/src/tags/if.ts @@ -13,10 +13,16 @@ export default class extends Tag { value: new Value(tagToken.args, this.liquid), templates: (p = []) })) - .on('tag:elsif', (token: TagToken) => this.branches.push({ - value: new Value(token.args, this.liquid), - templates: (p = []) - })) + .on('tag:elsif', (token: TagToken) => { + if (elseCount > 0) { + p = [] + return + } + this.branches.push({ + value: new Value(token.args, this.liquid), + templates: (p = []) + }) + }) .on('tag:else', () => { elseCount++ p = this.elseTemplates diff --git a/src/tags/unless.ts b/src/tags/unless.ts index 11a798e86c..160f71c8d1 100644 --- a/src/tags/unless.ts +++ b/src/tags/unless.ts @@ -13,11 +13,17 @@ export default class extends Tag { test: isFalsy, templates: (p = []) })) - .on('tag:elsif', (token: TagToken) => this.branches.push({ - value: new Value(token.args, this.liquid), - test: isTruthy, - templates: (p = []) - })) + .on('tag:elsif', (token: TagToken) => { + if (elseCount > 0) { + p = [] + return + } + this.branches.push({ + value: new Value(token.args, this.liquid), + test: isTruthy, + templates: (p = []) + }) + }) .on('tag:else', () => { elseCount++ p = this.elseTemplates diff --git a/test/e2e/issues.spec.ts b/test/e2e/issues.spec.ts index ca9ebad3ed..a73257c999 100644 --- a/test/e2e/issues.spec.ts +++ b/test/e2e/issues.spec.ts @@ -477,4 +477,12 @@ describe('Issues', function () { '{% else %}don\'t show{% endif %}', {}) expect(result).toEqual('show this') }) + it('#672 Should not render an elseif after an else branch', () => { + const engine = new Liquid() + const result = engine.parseAndRenderSync('{% if false %}don\'t show' + + '{% else %}show' + + '{% elsif true %}don\'t show' + + '{% endif %}', {}) + expect(result).toEqual('show') + }) }) diff --git a/test/integration/tags/unless.spec.ts b/test/integration/tags/unless.spec.ts index e2c80af884..5ca0446a69 100644 --- a/test/integration/tags/unless.spec.ts +++ b/test/integration/tags/unless.spec.ts @@ -62,6 +62,14 @@ describe('tags/unless', function () { '{% endunless %}', {}) expect(html).toEqual('') }) + it('should not render an elseif after an else branch', () => { + const engine = new Liquid() + const result = engine.parseAndRenderSync('{% unless true %}don\'t show' + + '{% else %}show' + + '{% elsif true %}don\'t show' + + '{% endunless %}', {}) + expect(result).toEqual('show') + }) describe('sync support', function () { it('should render else when predicate yields true', function () {