Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: break/continue stops whole template, #783 #786

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/tags/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default class extends Tag {
if (ctx.breakCalled) break
scope.forloop.next()
}
ctx.continueCalled = ctx.breakCalled = false
ctx.pop()
}

Expand Down
34 changes: 34 additions & 0 deletions test/integration/tags/for.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down
Loading