fix: break/continue stops whole template, #783

This commit is contained in:
Harttle
2025-01-04 22:15:39 +08:00
committed by Jun Yang
parent 8c32ab4f42
commit 5f1a4cfdc9
2 changed files with 35 additions and 0 deletions
+1
View File
@@ -74,6 +74,7 @@ export default class extends Tag {
if (ctx.breakCalled) break
scope.forloop.next()
}
ctx.continueCalled = ctx.breakCalled = false
ctx.pop()
}
+34
View File
@@ -178,6 +178,19 @@ describe('tags/for', function () {
const html = await liquid.parseAndRender(src, scope)
return expect(html).toBe(' before before')
})
it('should continue current forloop only', async () => {
const src = `
{%- for i in (1..2) -%}
i:{{ i }},
{%- for j in (1..2) -%}
j:{{ j }},
{%- continue -%}
after
{%- endfor -%}
{%- endfor -%}`
const html = await liquid.parseAndRender(src, scope)
return expect(html).toBe('i:1,j:1,j:2,i:2,j:1,j:2,')
})
})
describe('break', function () {
it('should support break', async function () {
@@ -196,6 +209,27 @@ describe('tags/for', function () {
const html = await liquid.parseAndRender(src, scope)
return expect(html).toBe('123breaking')
})
it('should not break template outside of forloop', async () => {
const src = '{% for i in (1..5) %}' +
'{{ i }}' +
'{% break %}' +
'{% endfor %}' +
' after'
const html = await liquid.parseAndRender(src, scope)
return expect(html).toBe('1 after')
})
it('should not break parent forloop', async function () {
const src = `
{%- for i in (1..3) -%}
i:{{ i }},
{%- for j in (1..3) -%}
j:{{ j }},
{%- break -%}
{%- endfor -%}
{%- endfor -%}`
const html = await liquid.parseAndRender(src, scope)
return expect(html).toBe('i:1,j:1,i:2,j:1,i:3,j:1,')
})
})
describe('limit', function () {