From f5116da1b4449cca8b11c822dbf6fdb914dd8ce4 Mon Sep 17 00:00:00 2001 From: Harttle Date: Sat, 4 Jan 2025 21:48:24 +0800 Subject: [PATCH] fix: break/continue stops whole template, #783 --- src/tags/for.ts | 1 + test/integration/tags/for.spec.ts | 34 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/tags/for.ts b/src/tags/for.ts index 741f77bb17..779eee207b 100644 --- a/src/tags/for.ts +++ b/src/tags/for.ts @@ -74,6 +74,7 @@ export default class extends Tag { if (ctx.breakCalled) break scope.forloop.next() } + ctx.continueCalled = ctx.breakCalled = false ctx.pop() } diff --git a/test/integration/tags/for.spec.ts b/test/integration/tags/for.spec.ts index 53a8678f1b..034800d560 100644 --- a/test/integration/tags/for.spec.ts +++ b/test/integration/tags/for.spec.ts @@ -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 () {