From 8f9639f1d63b999fc8b4adf5bc4d428e2fd61a7f Mon Sep 17 00:00:00 2001 From: Memmie Lenglet Date: Thu, 6 May 2021 15:39:54 +0200 Subject: [PATCH] feat: when tag with multiple values --- docs/source/tags/case.md | 2 +- docs/source/zh-cn/tags/case.md | 2 +- src/builtin/tags/case.ts | 24 ++++++++++++++++++------ test/integration/builtin/tags/case.ts | 7 +++++++ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/docs/source/tags/case.md b/docs/source/tags/case.md index 89864def2b..e4d6b33c57 100644 --- a/docs/source/tags/case.md +++ b/docs/source/tags/case.md @@ -12,7 +12,7 @@ Input {% case handle %} {% when "cake" %} This is a cake - {% when "cookie" %} + {% when "cookie", "biscuit" %} This is a cookie {% else %} This is not a cake nor a cookie diff --git a/docs/source/zh-cn/tags/case.md b/docs/source/zh-cn/tags/case.md index 80d8a3cc0a..1f4e0c08b1 100644 --- a/docs/source/zh-cn/tags/case.md +++ b/docs/source/zh-cn/tags/case.md @@ -12,7 +12,7 @@ title: case {% case handle %} {% when "cake" %} This is a cake - {% when "cookie" %} + {% when "cookie", "biscuit" %} This is a cookie {% else %} This is not a cake nor a cookie diff --git a/src/builtin/tags/case.ts b/src/builtin/tags/case.ts index b0756a80d4..4033ffc683 100644 --- a/src/builtin/tags/case.ts +++ b/src/builtin/tags/case.ts @@ -1,4 +1,5 @@ -import { toValue, Value, Emitter, TagToken, TopLevelToken, Context, Template, TagImplOptions, ParseStream } from '../../types' +import { toValue, evalToken, Value, Emitter, TagToken, TopLevelToken, Context, Template, TagImplOptions, ParseStream } from '../../types' +import { Tokenizer } from '../../parser/tokenizer' export default { parse: function (tagToken: TagToken, remainTokens: TopLevelToken[]) { @@ -9,10 +10,21 @@ export default { let p: Template[] = [] const stream: ParseStream = this.liquid.parser.parseStream(remainTokens) .on('tag:when', (token: TagToken) => { - this.cases.push({ - val: new Value(token.args, this.liquid), - templates: p = [] - }) + p = [] + + const tokenizer = new Tokenizer(token.args, this.liquid.options.operatorsTrie) + + while (!tokenizer.end()) { + const value = tokenizer.readValue() + if (value) { + this.cases.push({ + val: value, + templates: p + }) + } + + tokenizer.readTo(',') + } }) .on('tag:else', () => (p = this.elseTemplates)) .on('tag:endcase', () => stream.stop()) @@ -28,7 +40,7 @@ export default { const r = this.liquid.renderer const cond = toValue(yield this.cond.value(ctx, ctx.opts.lenientIf)) for (const branch of this.cases) { - const val = toValue(yield branch.val.value(ctx, ctx.opts.lenientIf)) + const val = evalToken(branch.val, ctx, ctx.opts.lenientIf) if (val === cond) { yield r.renderTemplates(branch.templates, ctx, emitter) return diff --git a/test/integration/builtin/tags/case.ts b/test/integration/builtin/tags/case.ts index 6044eebc23..23b89bb25b 100644 --- a/test/integration/builtin/tags/case.ts +++ b/test/integration/builtin/tags/case.ts @@ -64,4 +64,11 @@ describe('tags/case', function () { return expect(html).to.equal('d') }) }) + it('should support case with multiple values', async function () { + const src = '{% case "b" %}' + + '{% when "a", "b" %}foo' + + '{%endcase%}' + const html = await liquid.parseAndRender(src) + return expect(html).to.equal('foo') + }) })