fix: in conditionals, don't render anything after an else branch (#671)

* fix: only render the first 'else' template in the case of multiples

* fix: empty else block and cases with when conditions

* fix: don't render elsif after else

* chore: Update src/tags/unless.ts

* chore: Update src/tags/if.ts

* chore: Update src/tags/case.ts

---------

Co-authored-by: Jun Yang <[email protected]>
This commit is contained in:
Joel Hamilton
2024-02-18 13:30:16 +08:00
committed by GitHub
co-authored by Jun Yang
parent 1937aa1f1d
commit f816955704
7 changed files with 132 additions and 17 deletions
+28
View File
@@ -91,4 +91,32 @@ describe('tags/case', function () {
const html = await liquid.parseAndRender(src)
return expect(html).toBe('and or or')
})
it('should not render anything after an else branch', async function () {
const html = await liquid.parseAndRenderSync('{% assign value = "this" %}' +
'{% case true %}' +
'{% when false %}don\'t show' +
'{% else %}show {{ value }}' +
'{% else %}don\'t show' +
'{% endcase %}', {})
expect(html).toEqual('show this')
})
it('should not render anything after an else branch even when first else branch is empty', async function () {
const html = await liquid.parseAndRenderSync('{% case true %}' +
'{% when false %}don\'t show' +
'{% else %}' +
'{% else %}don\'t show' +
'{% endcase %}', {})
expect(html).toEqual('')
})
it('should not render anything after an else branch even when there are \'when\' conditions', () => {
const engine = new Liquid()
const result = engine.parseAndRenderSync('{% assign value = "this" %}' +
'{% case true -%}' +
'{% when false -%}don\'t show' +
'{% else %}show {{ value }}' +
'{% else %}don\'t show' +
'{%- when true -%}don\'t show' +
'{%- endcase %}', {})
expect(result).toEqual('show this')
})
})
+8
View File
@@ -143,4 +143,12 @@ describe('tags/if', function () {
const html = await liquid.parseAndRender(src, scope)
return expect(html).toBe('success')
})
it('should not render anything after an else branch even when first else branch is empty', () => {
const engine = new Liquid()
const result = engine.parseAndRenderSync('{% if false %}don\'t show' +
'{% else %}' +
'{% else %}don\'t show' +
'%{% endif %}', {})
expect(result).toEqual('')
})
})
+23
View File
@@ -47,6 +47,29 @@ describe('tags/unless', function () {
Inside wonderland
After wonderland`)
})
it('should not render anything after an else branch', async function () {
const html = await liquid.parseAndRenderSync('{% assign value = "this" %}' +
'{% unless true %}don\'t show' +
'{% else %}show {{ value }}' +
'{% else %}don\'t show' +
'{% endunless %}', {})
expect(html).toEqual('show this')
})
it('should not render anything after an else branch even when first else branch is empty', async function () {
const html = await liquid.parseAndRenderSync('{% unless true %}don\'t show' +
'{% else %}' +
'{% else %}don\'t show' +
'{% 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 () {