Skip to content

Commit

Permalink
fix: don't render elsif after else
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-hamilton committed Feb 18, 2024
1 parent 7eaec4d commit 827f596
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/tags/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export default class extends Tag {
})
})
.on('tag:else', () => {
p = []
elseCount++
p = this.elseTemplates
})
Expand Down
14 changes: 10 additions & 4 deletions src/tags/if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions src/tags/unless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/issues.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
8 changes: 8 additions & 0 deletions test/integration/tags/unless.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 827f596

Please sign in to comment.