Skip to content

Commit 5f1a4cf

Browse files
committed
fix: break/continue stops whole template, #783
1 parent 8c32ab4 commit 5f1a4cf

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/tags/for.ts

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export default class extends Tag {
7474
if (ctx.breakCalled) break
7575
scope.forloop.next()
7676
}
77+
ctx.continueCalled = ctx.breakCalled = false
7778
ctx.pop()
7879
}
7980

test/integration/tags/for.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ describe('tags/for', function () {
178178
const html = await liquid.parseAndRender(src, scope)
179179
return expect(html).toBe(' before before')
180180
})
181+
it('should continue current forloop only', async () => {
182+
const src = `
183+
{%- for i in (1..2) -%}
184+
i:{{ i }},
185+
{%- for j in (1..2) -%}
186+
j:{{ j }},
187+
{%- continue -%}
188+
after
189+
{%- endfor -%}
190+
{%- endfor -%}`
191+
const html = await liquid.parseAndRender(src, scope)
192+
return expect(html).toBe('i:1,j:1,j:2,i:2,j:1,j:2,')
193+
})
181194
})
182195
describe('break', function () {
183196
it('should support break', async function () {
@@ -196,6 +209,27 @@ describe('tags/for', function () {
196209
const html = await liquid.parseAndRender(src, scope)
197210
return expect(html).toBe('123breaking')
198211
})
212+
it('should not break template outside of forloop', async () => {
213+
const src = '{% for i in (1..5) %}' +
214+
'{{ i }}' +
215+
'{% break %}' +
216+
'{% endfor %}' +
217+
' after'
218+
const html = await liquid.parseAndRender(src, scope)
219+
return expect(html).toBe('1 after')
220+
})
221+
it('should not break parent forloop', async function () {
222+
const src = `
223+
{%- for i in (1..3) -%}
224+
i:{{ i }},
225+
{%- for j in (1..3) -%}
226+
j:{{ j }},
227+
{%- break -%}
228+
{%- endfor -%}
229+
{%- endfor -%}`
230+
const html = await liquid.parseAndRender(src, scope)
231+
return expect(html).toBe('i:1,j:1,i:2,j:1,i:3,j:1,')
232+
})
199233
})
200234

201235
describe('limit', function () {

0 commit comments

Comments
 (0)