From 5b3f4195f0287e083236cd8c32fea613f4f50eac Mon Sep 17 00:00:00 2001 From: harttle Date: Sat, 3 Oct 2020 22:01:56 +0800 Subject: [PATCH] fix: allow quoted variable name in capture, fixes #252 --- src/builtin/tags/capture.ts | 10 +++++++++- test/e2e/issues.ts | 8 ++++++++ test/integration/builtin/tags/capture.ts | 6 ++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/builtin/tags/capture.ts b/src/builtin/tags/capture.ts index 8fce59f023..139d9b613e 100644 --- a/src/builtin/tags/capture.ts +++ b/src/builtin/tags/capture.ts @@ -1,9 +1,10 @@ import { Tokenizer, assert, Template, Context, TagImplOptions, TagToken, TopLevelToken } from '../../types' +import { evalQuotedToken } from '../../render/expression' export default { parse: function (tagToken: TagToken, remainTokens: TopLevelToken[]) { const tokenizer = new Tokenizer(tagToken.args) - this.variable = tokenizer.readWord().content + this.variable = readVariableName(tokenizer) assert(this.variable, () => `${tagToken.args} not valid identifier`) this.templates = [] @@ -22,3 +23,10 @@ export default { ctx.bottom()[this.variable] = html } } as TagImplOptions + +function readVariableName (tokenizer: Tokenizer) { + const word = tokenizer.readWord().content + if (word) return word + const quoted = tokenizer.readQuoted() + if (quoted) return evalQuotedToken(quoted) +} diff --git a/test/e2e/issues.ts b/test/e2e/issues.ts index f7e4b50101..972bb1a5fa 100644 --- a/test/e2e/issues.ts +++ b/test/e2e/issues.ts @@ -10,4 +10,12 @@ describe('Issues', function () { const html = engine.parseAndRenderSync('{{huh | truncate: 11}}', { huh: 'fdsafdsafdsafdsaaaaa' }) expect(html).to.equal('fdsafdsa...') }) + it('#252 "Not valid identifier" error for a quotes-containing identifier', async () => { + const template = `{% capture "form_classes" -%} + foo + {%- endcapture %}{{form_classes}}` + const engine = new Liquid() + const html = await engine.parseAndRender(template) + expect(html).to.equal('foo') + }) }) diff --git a/test/integration/builtin/tags/capture.ts b/test/integration/builtin/tags/capture.ts index 4aed916ed9..921f9ec20b 100644 --- a/test/integration/builtin/tags/capture.ts +++ b/test/integration/builtin/tags/capture.ts @@ -13,6 +13,12 @@ describe('tags/capture', function () { return expect(html).to.equal('A') }) + it('should support quoted variable name', async function () { + const src = '{% capture "f" %}{{"a" | capitalize}}{%endcapture%}{{f}}' + const html = await liquid.parseAndRender(src) + return expect(html).to.equal('A') + }) + it('should shading rather than overwriting', async function () { const src = '{% capture var %}10{% endcapture %}{{var}}' const ctx = { 'var': 20 }