mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 04:40:39 -07:00
feature: separate options for value/tag trimming, working on #17
This commit is contained in:
+4
-100
@@ -40,14 +40,14 @@ describe('liquid', function () {
|
||||
expect(liquid.options.root).to.deep.equal([])
|
||||
})
|
||||
})
|
||||
describe('{{output}}', function () {
|
||||
it('should output object', function () {
|
||||
describe('{{value}}', function () {
|
||||
it('should value object', function () {
|
||||
return expect(engine.parseAndRender('{{obj}}', ctx)).to.eventually.equal('{"foo":"bar"}')
|
||||
})
|
||||
it('should output array', function () {
|
||||
it('should value array', function () {
|
||||
return expect(engine.parseAndRender('{{arr}}', ctx)).to.eventually.equal('[-2,"a"]')
|
||||
})
|
||||
it('should output undefined to empty', function () {
|
||||
it('should value undefined to empty', function () {
|
||||
return expect(engine.parseAndRender('foo{{zzz}}bar', ctx)).to.eventually.equal('foobar')
|
||||
})
|
||||
it('should render as null when filter undefined', function () {
|
||||
@@ -133,100 +133,4 @@ describe('liquid', function () {
|
||||
.be.rejectedWith(/EACCES/)
|
||||
})
|
||||
})
|
||||
describe('strict', function () {
|
||||
it('should not throw when strict_variables false (default)', function () {
|
||||
return expect(engine.parseAndRender('before{{notdefined}}after', ctx)).to
|
||||
.eventually.equal('beforeafter')
|
||||
})
|
||||
it('should throw when strict_variables true', function () {
|
||||
var tpl = engine.parse('before{{notdefined}}after')
|
||||
var opts = {
|
||||
strict_variables: true
|
||||
}
|
||||
return expect(engine.render(tpl, ctx, opts)).to
|
||||
.be.rejectedWith(/undefined variable: notdefined/)
|
||||
})
|
||||
it('should pass strict_variables to render by parseAndRender', function () {
|
||||
var html = 'before{{notdefined}}after'
|
||||
var opts = {
|
||||
strict_variables: true
|
||||
}
|
||||
return expect(engine.parseAndRender(html, ctx, opts)).to
|
||||
.be.rejectedWith(/undefined variable: notdefined/)
|
||||
})
|
||||
})
|
||||
describe('cache', function () {
|
||||
it('should be disabled by default', function () {
|
||||
return engine.renderFile('files/foo')
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
.then(() => mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
}))
|
||||
.then(() => engine.renderFile('files/foo'))
|
||||
.then(x => expect(x).to.equal('bar'))
|
||||
})
|
||||
it('should respect cache=true option', function () {
|
||||
engine = Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
return engine.renderFile('files/foo')
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
.then(() => mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
}))
|
||||
.then(() => engine.renderFile('files/foo'))
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
})
|
||||
})
|
||||
describe('trim_left, trim_right', function () {
|
||||
it('should trim_left for tags when trim_left=true', function () {
|
||||
engine = Liquid({
|
||||
trim_left: true
|
||||
})
|
||||
return expect(engine.parseAndRender(' \n \t{%if true%}foo{%endif%} '))
|
||||
.to.eventually.equal('foo ')
|
||||
})
|
||||
it('should trim_right for tags when trim_right=true', function () {
|
||||
engine = Liquid({
|
||||
trim_right: true
|
||||
})
|
||||
return expect(engine.parseAndRender('\t{%if true%}foo{%endif%} \n'))
|
||||
.to.eventually.equal('\tfoo')
|
||||
})
|
||||
it('should trim all blanks before and after when greedy=true', function () {
|
||||
engine = Liquid({
|
||||
greedy: true
|
||||
})
|
||||
return expect(engine.parseAndRender('\t{%-if true%}foo{%endif-%} \n \n'))
|
||||
.to.eventually.equal('foo')
|
||||
})
|
||||
it('should support trim using markup', function () {
|
||||
engine = Liquid()
|
||||
var src = [
|
||||
'{%- assign username = "John G. Chalmers-Smith" -%}',
|
||||
'{%- if username and username.length > 10 -%}',
|
||||
' Wow, {{ username }}, you have a long name!',
|
||||
'{%- else -%}',
|
||||
' Hello there!',
|
||||
'{%- endif -%}'
|
||||
].join('\n')
|
||||
var dst = 'Wow, John G. Chalmers-Smith, you have a long name!'
|
||||
return expect(engine.parseAndRender(src)).to.eventually.equal(dst)
|
||||
})
|
||||
it('should not trim when not specified', function () {
|
||||
engine = Liquid()
|
||||
var src = [
|
||||
'{% assign username = "John G. Chalmers-Smith" %}',
|
||||
'{% if username and username.length > 10 %}',
|
||||
' Wow, {{ username }}, you have a long name!',
|
||||
'{% else %}',
|
||||
' Hello there!',
|
||||
'{% endif %}'
|
||||
].join('\n')
|
||||
var dst = '\n\n Wow, John G. Chalmers-Smith, you have a long name!\n'
|
||||
return expect(engine.parseAndRender(src)).to.eventually.equal(dst)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
const mock = require('mock-fs')
|
||||
const Liquid = require('../..')
|
||||
chai.use(require('chai-as-promised'))
|
||||
|
||||
describe('cache options', function () {
|
||||
var engine
|
||||
beforeEach(function () {
|
||||
engine = Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html'
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
})
|
||||
afterEach(function () {
|
||||
mock.restore()
|
||||
})
|
||||
it('should be disabled by default', function () {
|
||||
return engine.renderFile('files/foo')
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
.then(() => mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
}))
|
||||
.then(() => engine.renderFile('files/foo'))
|
||||
.then(x => expect(x).to.equal('bar'))
|
||||
})
|
||||
it('should respect cache=true option', function () {
|
||||
engine = Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
return engine.renderFile('files/foo')
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
.then(() => mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
}))
|
||||
.then(() => engine.renderFile('files/foo'))
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,35 @@
|
||||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
const Liquid = require('../..')
|
||||
chai.use(require('chai-as-promised'))
|
||||
|
||||
describe('strict options', function () {
|
||||
var engine
|
||||
var ctx = {}
|
||||
beforeEach(function () {
|
||||
engine = Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html'
|
||||
})
|
||||
})
|
||||
it('should not throw when strict_variables false (default)', function () {
|
||||
return expect(engine.parseAndRender('before{{notdefined}}after', ctx)).to
|
||||
.eventually.equal('beforeafter')
|
||||
})
|
||||
it('should throw when strict_variables true', function () {
|
||||
var tpl = engine.parse('before{{notdefined}}after')
|
||||
var opts = {
|
||||
strict_variables: true
|
||||
}
|
||||
return expect(engine.render(tpl, ctx, opts)).to
|
||||
.be.rejectedWith(/undefined variable: notdefined/)
|
||||
})
|
||||
it('should pass strict_variables to render by parseAndRender', function () {
|
||||
var html = 'before{{notdefined}}after'
|
||||
var opts = {
|
||||
strict_variables: true
|
||||
}
|
||||
return expect(engine.parseAndRender(html, ctx, opts)).to
|
||||
.be.rejectedWith(/undefined variable: notdefined/)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,84 @@
|
||||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
const Liquid = require('../..')
|
||||
chai.use(require('chai-as-promised'))
|
||||
|
||||
describe('trimming', function () {
|
||||
var ctx = {name: 'harttle'}
|
||||
|
||||
describe('tag trimming', function () {
|
||||
it('should respect trim_tag_left', function () {
|
||||
var engine = Liquid({ trim_tag_left: true })
|
||||
return expect(engine.parseAndRender(' \n \t{%if true%}foo{%endif%} '))
|
||||
.to.eventually.equal('foo ')
|
||||
})
|
||||
it('should respect trim_tag_right', function () {
|
||||
var engine = Liquid({ trim_tag_right: true })
|
||||
return expect(engine.parseAndRender('\t{%if true%}foo{%endif%} \n'))
|
||||
.to.eventually.equal('\tfoo')
|
||||
})
|
||||
it('should not trim value', function () {
|
||||
var engine = Liquid({ trim_tag_left: true, trim_tag_right: true })
|
||||
return expect(engine.parseAndRender('{%if true%}a {{name}} b{%endif%}', ctx))
|
||||
.to.eventually.equal('a harttle b')
|
||||
})
|
||||
})
|
||||
describe('value trimming', function () {
|
||||
it('should respect trim_value_left', function () {
|
||||
var engine = Liquid({ trim_value_left: true })
|
||||
return expect(engine.parseAndRender(' \n \t{{name}} ', ctx))
|
||||
.to.eventually.equal('harttle ')
|
||||
})
|
||||
it('should respect trim_value_right', function () {
|
||||
var engine = Liquid({ trim_value_right: true })
|
||||
return expect(engine.parseAndRender(' \n \t{{name}} ', ctx))
|
||||
.to.eventually.equal(' \n \tharttle')
|
||||
})
|
||||
it('should respect not trim tag', function () {
|
||||
var engine = Liquid({ trim_value_left: true, trim_value_right: true })
|
||||
return expect(engine.parseAndRender('\t{% if true %} aha {%endif%}\t'))
|
||||
.to.eventually.equal('\t aha \t')
|
||||
})
|
||||
})
|
||||
describe('greedy', function () {
|
||||
var src = '\n {%-if true-%}\n a \n{{-name-}}{%-endif-%}\n '
|
||||
it('should enable greedy by default', function () {
|
||||
var engine = Liquid()
|
||||
return expect(engine.parseAndRender(src, ctx))
|
||||
.to.eventually.equal('aharttle')
|
||||
})
|
||||
it('should respect to greedy:false by default', function () {
|
||||
var engine = Liquid({greedy: false})
|
||||
return expect(engine.parseAndRender(src, ctx))
|
||||
.to.eventually.equal('\n a \nharttle ')
|
||||
})
|
||||
})
|
||||
describe('markup', function () {
|
||||
it('should support trim using markup', function () {
|
||||
var engine = Liquid()
|
||||
var src = [
|
||||
'{%- assign username = "John G. Chalmers-Smith" -%}',
|
||||
'{%- if username and username.length > 10 -%}',
|
||||
' Wow, {{ username }}, you have a long name!',
|
||||
'{%- else -%}',
|
||||
' Hello there!',
|
||||
'{%- endif -%}'
|
||||
].join('\n')
|
||||
var dst = 'Wow, John G. Chalmers-Smith, you have a long name!'
|
||||
return expect(engine.parseAndRender(src)).to.eventually.equal(dst)
|
||||
})
|
||||
it('should not trim when not specified', function () {
|
||||
var engine = Liquid()
|
||||
var src = [
|
||||
'{% assign username = "John G. Chalmers-Smith" %}',
|
||||
'{% if username and username.length > 10 %}',
|
||||
' Wow, {{ username }}, you have a long name!',
|
||||
'{% else %}',
|
||||
' Hello there!',
|
||||
'{% endif %}'
|
||||
].join('\n')
|
||||
var dst = '\n\n Wow, John G. Chalmers-Smith, you have a long name!\n'
|
||||
return expect(engine.parseAndRender(src)).to.eventually.equal(dst)
|
||||
})
|
||||
})
|
||||
})
|
||||
+10
-10
@@ -19,28 +19,28 @@ describe('template', function () {
|
||||
template = Template(tag, filter)
|
||||
})
|
||||
|
||||
it('should throw when output string illegal', function () {
|
||||
it('should throw when value string illegal', function () {
|
||||
expect(function () {
|
||||
template.parseOutput('/')
|
||||
}).to.throw(/illegal output string/)
|
||||
template.parseValue('/')
|
||||
}).to.throw(/illegal value string/)
|
||||
})
|
||||
|
||||
it('should parse output string', function () {
|
||||
var tpl = template.parseOutput('foo')
|
||||
expect(tpl.type).to.equal('output')
|
||||
it('should parse value string', function () {
|
||||
var tpl = template.parseValue('foo')
|
||||
expect(tpl.type).to.equal('value')
|
||||
expect(tpl.initial).to.equal('foo')
|
||||
expect(tpl.filters).to.deep.equal([])
|
||||
})
|
||||
|
||||
it('should parse output string with a simple filter', function () {
|
||||
var tpl = template.parseOutput('foo | add: 3, "foo"')
|
||||
it('should parse value string with a simple filter', function () {
|
||||
var tpl = template.parseValue('foo | add: 3, "foo"')
|
||||
expect(tpl.initial).to.equal('foo')
|
||||
expect(tpl.filters.length).to.equal(1)
|
||||
expect(tpl.filters[0].filter).to.equal(add)
|
||||
})
|
||||
|
||||
it('should parse output string with filters', function () {
|
||||
var tpl = template.parseOutput('foo | add: "|" | add')
|
||||
it('should parse value string with filters', function () {
|
||||
var tpl = template.parseValue('foo | add: "|" | add')
|
||||
expect(tpl.initial).to.equal('foo')
|
||||
expect(tpl.filters.length).to.equal(2)
|
||||
})
|
||||
|
||||
+7
-7
@@ -44,23 +44,23 @@ describe('render', function () {
|
||||
var time = sinon.spy()
|
||||
filter.register('date', date)
|
||||
filter.register('time', time)
|
||||
var tpl = Template.parseOutput('foo.bar[0] | date: "b" | time:2')
|
||||
render.evalOutput(tpl, scope)
|
||||
var tpl = Template.parseValue('foo.bar[0] | date: "b" | time:2')
|
||||
render.evalValue(tpl, scope)
|
||||
expect(date).to.have.been.calledWith('a', 'b')
|
||||
expect(time).to.have.been.calledWith('y', 2)
|
||||
})
|
||||
|
||||
describe('.evalOutput()', function () {
|
||||
describe('.evalValue()', function () {
|
||||
it('should throw when scope undefined', function () {
|
||||
expect(function () {
|
||||
render.evalOutput()
|
||||
render.evalValue()
|
||||
}).to.throw(/scope undefined/)
|
||||
})
|
||||
it('should eval output', function () {
|
||||
it('should eval value', function () {
|
||||
filter.register('date', (l, r) => l + r)
|
||||
filter.register('time', (l, r) => l + 3 * r)
|
||||
var tpl = Template.parseOutput('foo.bar[0] | date: "b" | time:2')
|
||||
expect(render.evalOutput(tpl, scope)).to.equal('ab6')
|
||||
var tpl = Template.parseValue('foo.bar[0] | date: "b" | time:2')
|
||||
expect(render.evalValue(tpl, scope)).to.equal('ab6')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+6
-6
@@ -25,20 +25,20 @@ describe('tokenizer', function () {
|
||||
expect(tokens[1].type).to.equal('tag')
|
||||
expect(tokens[1].value).to.equal('for p in a[1]')
|
||||
})
|
||||
it('should handle output syntax', function () {
|
||||
it('should handle value syntax', function () {
|
||||
var html = '<p>{{foo | date: "%Y-%m-%d"}}</p>'
|
||||
var tokens = parse(html)
|
||||
|
||||
expect(tokens.length).to.equal(3)
|
||||
expect(tokens[1].type).to.equal('output')
|
||||
expect(tokens[1].type).to.equal('value')
|
||||
expect(tokens[1].value).to.equal('foo | date: "%Y-%m-%d"')
|
||||
})
|
||||
it('should handle successive outputs and tags', function () {
|
||||
it('should handle successive value and tags', function () {
|
||||
var html = '{{foo}}{{bar}}{%foo%}{%bar%}'
|
||||
var tokens = parse(html)
|
||||
|
||||
expect(tokens.length).to.equal(4)
|
||||
expect(tokens[0].type).to.equal('output')
|
||||
expect(tokens[0].type).to.equal('value')
|
||||
expect(tokens[3].type).to.equal('tag')
|
||||
|
||||
expect(tokens[1].value).to.equal('bar')
|
||||
@@ -61,11 +61,11 @@ describe('tokenizer', function () {
|
||||
expect(tokens[0].args).to.equal('a:a\nb:1.23')
|
||||
expect(tokens[0].raw).to.equal('{%foo\na:a\nb:1.23\n%}')
|
||||
})
|
||||
it('should handle multiple lines output', function () {
|
||||
it('should handle multiple lines value', function () {
|
||||
var html = '{{foo\n|date:\n"%Y-%m-%d"\n}}'
|
||||
var tokens = parse(html)
|
||||
expect(tokens.length).to.equal(1)
|
||||
expect(tokens[0].type).to.equal('output')
|
||||
expect(tokens[0].type).to.equal('value')
|
||||
expect(tokens[0].raw).to.equal('{{foo\n|date:\n"%Y-%m-%d"\n}}')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user