Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Value } from '../../template/value' | ||
import { Emitter } from '../../emitters/emitter' | ||
import { TagImplOptions, TagToken, Context } from '../../types' | ||
|
||
export default { | ||
parse: function (token: TagToken) { | ||
this.value = new Value(token.args, this.liquid) | ||
}, | ||
render: function * (ctx: Context, emitter: Emitter): Generator<unknown, void, unknown> { | ||
const val = yield this.value.value(ctx, false) | ||
emitter.write(val) | ||
} | ||
} as TagImplOptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Emitter } from '../../emitters/emitter' | ||
import { TagImplOptions, TagToken, Context } from '../../types' | ||
import { Tokenizer } from '../../parser/tokenizer' | ||
|
||
export default { | ||
parse: function (token: TagToken) { | ||
const tokenizer = new Tokenizer(token.args, this.liquid.options.operatorsTrie) | ||
const tokens = tokenizer.readLiquidTagTokens(this.liquid.options) | ||
this.tpls = this.liquid.parser.parseTokens(tokens) | ||
}, | ||
render: function * (ctx: Context, emitter: Emitter): Generator<unknown, void, unknown> { | ||
yield this.liquid.renderer.renderTemplates(this.tpls, ctx, emitter) | ||
} | ||
} as TagImplOptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { DelimitedToken } from './delimited-token' | ||
import { TokenizationError } from '../util/error' | ||
import { NormalizedFullOptions } from '../liquid-options' | ||
import { TokenKind } from '../parser/token-kind' | ||
import { Tokenizer } from '../parser/tokenizer' | ||
|
||
export class LiquidTagToken extends DelimitedToken { | ||
public name: string | ||
public args: string | ||
public constructor ( | ||
input: string, | ||
begin: number, | ||
end: number, | ||
options: NormalizedFullOptions, | ||
file?: string | ||
) { | ||
const value = input.slice(begin, end) | ||
super(TokenKind.Tag, value, input, begin, end, false, false, file) | ||
|
||
if (!/\S/.test(value)) { | ||
// A line that contains only whitespace. | ||
this.name = '' | ||
this.args = '' | ||
} else { | ||
const tokenizer = new Tokenizer(this.content, options.operatorsTrie) | ||
this.name = tokenizer.readIdentifier().getText() | ||
if (!this.name) throw new TokenizationError(`illegal liquid tag syntax`, this) | ||
|
||
tokenizer.skipBlank() | ||
this.args = tokenizer.remaining() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Liquid } from '../../../../src/liquid' | ||
import { expect, use } from 'chai' | ||
import * as chaiAsPromised from 'chai-as-promised' | ||
|
||
use(chaiAsPromised) | ||
|
||
describe('tags/echo', function () { | ||
const liquid = new Liquid() | ||
|
||
it('should output literals', async function () { | ||
const src = '{% echo 1 %} {% echo "1" %} {% echo 1.1 %}' | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('1 1 1.1') | ||
}) | ||
|
||
it('should output variables', async function () { | ||
const src = '{% echo people.users[0].name %}' | ||
const html = await liquid.parseAndRender(src, { people: { users: [ { name: 'Sally' } ] } }) | ||
return expect(html).to.equal('Sally') | ||
}) | ||
|
||
it('should apply filters before output', async function () { | ||
const src = '{% echo user.name | upcase | prepend: "Hello, " | append: "!" %}' | ||
const html = await liquid.parseAndRender(src, { user: { name: 'Sally' } }) | ||
return expect(html).to.equal('Hello, SALLY!') | ||
}) | ||
|
||
it('should handle empty tag', async function () { | ||
const src = '{% echo %}' | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('') | ||
}) | ||
|
||
it('should handle extra whitespace', async function () { | ||
const src = `{% echo | ||
user.name | ||
| upcase | prepend: | ||
"Hello, " | append: "!" | ||
%}` | ||
const html = await liquid.parseAndRender(src, { user: { name: 'Sally' } }) | ||
return expect(html).to.equal('Hello, SALLY!') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Liquid } from '../../../../src/liquid' | ||
import { expect, use } from 'chai' | ||
import * as chaiAsPromised from 'chai-as-promised' | ||
|
||
use(chaiAsPromised) | ||
|
||
describe('tags/liquid', function () { | ||
const liquid = new Liquid() | ||
|
||
it('should support shorthand syntax', async function () { | ||
const src = ` | ||
{%- liquid | ||
for value in array | ||
echo value | ||
unless forloop.last | ||
echo '#' | ||
endunless | ||
endfor | ||
-%} | ||
` | ||
const html = await liquid.parseAndRender(src, { array: [1, 2, 3] }) | ||
return expect(html).to.equal('1#2#3') | ||
}) | ||
|
||
it('should support shorthand syntax with assignments and filters', async function () { | ||
const src = ` | ||
{%- liquid | ||
for value in array | ||
assign double_value = value | times: 2 | ||
echo double_value | times: 2 | ||
unless forloop.last | ||
echo '#' | ||
endunless | ||
endfor | ||
echo '#' | ||
echo double_value | ||
-%} | ||
` | ||
const html = await liquid.parseAndRender(src, { array: [1, 2, 3] }) | ||
return expect(html).to.equal('4#8#12#6') | ||
}) | ||
|
||
it('should handle empty tag', async function () { | ||
const src = '{% liquid %}' | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('') | ||
}) | ||
|
||
it('should handle lines containing only whitespace', async function () { | ||
const src = `{% liquid | ||
echo 'hello ' | ||
\t | ||
echo 'goodbye' | ||
%}` | ||
const html = await liquid.parseAndRender(src) | ||
return expect(html).to.equal('hello goodbye') | ||
}) | ||
|
||
it('should fail with carriage return terminated tags', async function () { | ||
const src = [ | ||
'{%- liquid', | ||
' for value in array', | ||
' echo value', | ||
' unless forloop.last', | ||
' echo "#"', | ||
' endunless', | ||
'endfor', | ||
'-%}' | ||
].join('\r') | ||
return expect(liquid.parseAndRender(src)) | ||
.to.be.rejectedWith(/not closed/) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters