fix: inconsistent continue behaviour, fixes #779

This commit is contained in:
Harttle
2024-12-22 16:32:08 +08:00
committed by Jun Yang
parent 2af297f81a
commit e3ef574674
7 changed files with 37 additions and 12 deletions
+2
View File
@@ -25,6 +25,8 @@ export class Context {
*/
public globals: Scope
public sync: boolean
public breakCalled = false
public continueCalled = false
/**
* The normalized liquid options object
*/
+1 -1
View File
@@ -23,7 +23,7 @@ export class Render {
const html = yield tpl.render(ctx, emitter)
// if not, it'll return an `html`, write to the emitter for it
html && emitter.write(html)
if (emitter['break'] || emitter['continue']) break
if (ctx.breakCalled || ctx.continueCalled) break
} catch (e) {
const err = LiquidError.is(e) ? e : new RenderError(e as Error, tpl)
if (ctx.opts.catchAllErrors) errors.push(err)
+2 -2
View File
@@ -1,7 +1,7 @@
import { Context, Emitter, Tag } from '..'
export default class extends Tag {
render (ctx: Context, emitter: Emitter) {
emitter['break'] = true
render (ctx: Context, _emitter: Emitter) {
ctx.breakCalled = true
}
}
+2 -2
View File
@@ -1,7 +1,7 @@
import { Tag, Emitter, Context } from '..'
export default class extends Tag {
render (ctx: Context, emitter: Emitter) {
emitter['continue'] = true
render (ctx: Context, _emitter: Emitter) {
ctx.continueCalled = true
}
}
+2 -5
View File
@@ -68,12 +68,9 @@ export default class extends Tag {
ctx.push(scope)
for (const item of collection) {
scope[this.variable] = item
ctx.continueCalled = ctx.breakCalled = false
yield r.renderTemplates(this.templates, ctx, emitter)
if (emitter['break']) {
emitter['break'] = false
break
}
emitter['continue'] = false
if (ctx.breakCalled) break
scope.forloop.next()
}
ctx.pop()