mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 22:10:41 -07:00
perf: introduce AST to avoid reparse
This commit is contained in:
@@ -93,7 +93,7 @@ describe('Liquid', function () {
|
||||
})
|
||||
const tpls = await engine.getTemplate('mocha')
|
||||
expect(tpls.length).to.gte(1)
|
||||
expect(tpls[0].token.raw).to.contain('module.exports')
|
||||
expect(tpls[0].token.getText()).to.contain('module.exports')
|
||||
})
|
||||
})
|
||||
describe('#evalValue', function () {
|
||||
|
||||
@@ -21,7 +21,7 @@ describe('liquid#registerTag()', function () {
|
||||
it('should have access to ctx in render()', async () => {
|
||||
const liquid = new Liquid()
|
||||
liquid.registerTag('dynamic-string', {
|
||||
render: async (ctx) => ctx.get('c')
|
||||
render: async (ctx) => ctx.get(['c'])
|
||||
})
|
||||
const html = await liquid.parseAndRender(`A{% dynamic-string %}C`, {
|
||||
c: 'B'
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { normalize } from '../../../src/liquid-options'
|
||||
import { expect } from 'chai'
|
||||
|
||||
describe('LiquidOptions', function () {
|
||||
describe('LiquidOptions#root', function () {
|
||||
describe('#normalize ()', function () {
|
||||
it('should normalize string typed root array', function () {
|
||||
const options = normalize({ root: 'foo' })
|
||||
@@ -45,7 +45,7 @@ describe('LiquidOptions#trimming', function () {
|
||||
const html = await engine.parseAndRender(src, ctx)
|
||||
return expect(html).to.equal('aharttle')
|
||||
})
|
||||
it('should respect to greedy:false by default', async function () {
|
||||
it('should allow greedy:false', async function () {
|
||||
const engine = new Liquid({ greedy: false } as any)
|
||||
const html = await engine.parseAndRender(src, ctx)
|
||||
return expect(html).to.equal('\n a \nharttle ')
|
||||
|
||||
@@ -0,0 +1,450 @@
|
||||
import { expect } from 'chai'
|
||||
import { Liquid } from '../../../src/liquid'
|
||||
|
||||
const liquid = new Liquid()
|
||||
|
||||
const cases = [
|
||||
{
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{{ 'John' }}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
John
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
|
||||
|
||||
{{- 'John' -}}
|
||||
|
||||
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>John</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
|
||||
|
||||
{%- if true -%}
|
||||
yes
|
||||
{%- endif -%}
|
||||
|
||||
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>yes</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{% if true %}
|
||||
yes
|
||||
{% endif %}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
|
||||
yes
|
||||
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{% if false %}
|
||||
no
|
||||
{% endif %}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: '<p>{{- \'John\' -}}</p>',
|
||||
expected: '<p>John</p>'
|
||||
}, {
|
||||
text: '<p>{%- if true -%}yes{%- endif -%}</p>',
|
||||
expected: '<p>yes</p>'
|
||||
}, {
|
||||
text: '<p>{%- if false -%}no{%- endif -%}</p>',
|
||||
expected: '<p></p>'
|
||||
}, {
|
||||
text: '<p> {%- if true %} yes {% endif -%} </p>',
|
||||
expected: '<p> yes </p>'
|
||||
}, {
|
||||
text: '<p> {%- if false %} no {% endif -%} </p>',
|
||||
expected: '<p></p>'
|
||||
}, {
|
||||
text: '<p> {% if true -%} yes {%- endif %} </p>',
|
||||
expected: '<p> yes </p>'
|
||||
}, {
|
||||
text: '<p> {% if false -%} no {%- endif %} </p>',
|
||||
expected: '<p> </p>'
|
||||
}, {
|
||||
text: '<p> {% if true -%} yes {% endif -%} </p>',
|
||||
expected: '<p> yes </p>'
|
||||
}, {
|
||||
text: '<p> {% if false -%} no {% endif -%} </p>',
|
||||
expected: '<p> </p>'
|
||||
}, {
|
||||
text: '<p> {%- if true %} yes {%- endif %} </p>',
|
||||
expected: '<p> yes </p>'
|
||||
}, {
|
||||
text: '<p> {%- if false %} no {%- endif %} </p>',
|
||||
expected: '<p> </p>'
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{{- 'John' }}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>John
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{%- if true %}
|
||||
yes
|
||||
{%- endif %}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
yes
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{%- if false %}
|
||||
no
|
||||
{%- endif %}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{{ 'John' -}}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
John</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{% if true -%}
|
||||
yes
|
||||
{% endif -%}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
yes
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{% if false -%}
|
||||
no
|
||||
{% endif -%}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{%- if true %}
|
||||
yes
|
||||
{% endif -%}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
yes
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{%- if false %}
|
||||
no
|
||||
{% endif -%}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p></p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{% if true -%}
|
||||
yes
|
||||
{%- endif %}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
yes
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{% if false -%}
|
||||
no
|
||||
{%- endif %}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>
|
||||
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{{- 'John' -}}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>John</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{%- if true -%}
|
||||
yes
|
||||
{%- endif -%}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>yes</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{%- if false -%}
|
||||
no
|
||||
{%- endif -%}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p></p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{{- 'John' -}},
|
||||
{{- '30' -}}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>John,30</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{%- if true -%}
|
||||
yes
|
||||
{%- endif -%}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>yes</p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{%- if false -%}
|
||||
no
|
||||
{%- endif -%}
|
||||
</p>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p></p>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
<p>
|
||||
{{- 'John' -}}
|
||||
{{- '30' -}}
|
||||
</p>
|
||||
<b>
|
||||
{{ 'John' -}}
|
||||
{{- '30' }}
|
||||
</b>
|
||||
<i>
|
||||
{{- 'John' }}
|
||||
{{ '30' -}}
|
||||
</i>
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
<p>John30</p>
|
||||
<b>
|
||||
John30
|
||||
</b>
|
||||
<i>John
|
||||
30</i>
|
||||
</div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
{%- if true -%}
|
||||
{%- if true -%}
|
||||
<p>
|
||||
{{- 'John' -}}
|
||||
</p>
|
||||
{%- endif -%}
|
||||
{%- endif -%}
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div><p>John</p></div>
|
||||
`
|
||||
}, {
|
||||
text: `
|
||||
<div>
|
||||
{% raw %}
|
||||
{%- if true -%}
|
||||
<p>
|
||||
{{- 'John' -}}
|
||||
</p>
|
||||
{%- endif -%}
|
||||
{% endraw %}
|
||||
</div>
|
||||
`,
|
||||
expected: `
|
||||
<div>
|
||||
|
||||
{%- if true -%}
|
||||
<p>
|
||||
{{- 'John' -}}
|
||||
</p>
|
||||
{%- endif -%}
|
||||
|
||||
</div>
|
||||
`
|
||||
}
|
||||
]
|
||||
|
||||
describe('Whitespace Control', function () {
|
||||
cases.forEach(item => it(
|
||||
item.text,
|
||||
async () => {
|
||||
const html = await liquid.parseAndRender(item.text)
|
||||
expect(html).to.equal(item.expected)
|
||||
}
|
||||
))
|
||||
})
|
||||
Reference in New Issue
Block a user