diff --git a/src/syntax.js b/src/syntax.js index 90d209c72c..8ae44e502e 100644 --- a/src/syntax.js +++ b/src/syntax.js @@ -43,12 +43,11 @@ function evalValue(str, scope) { } function isTruthy(val) { - if (val instanceof Array) return !!val.length; - return !!val; + return !isFalsy(val); } function isFalsy(val) { - return !isTruthy(val); + return false === val || undefined === val || null === val;; } module.exports = { diff --git a/tags/for.js b/tags/for.js index e75ed1d207..b15cfac183 100644 --- a/tags/for.js +++ b/tags/for.js @@ -36,7 +36,9 @@ module.exports = function(liquid) { render: function(scope, hash) { var collection = Liquid.evalExp(this.collection, scope); - if (Liquid.isFalsy(collection)) { + + if (!Array.isArray(collection) || + (Array.isArray(collection) && collection.length === 0)) { return liquid.renderer.renderTemplates(this.elseTemplates, scope); } diff --git a/test/tags/for.js b/test/tags/for.js index 6872286fd7..cfe2607f86 100644 --- a/test/tags/for.js +++ b/test/tags/for.js @@ -10,6 +10,7 @@ describe('tags/for', function() { ctx = { one: 1, alpha: ['a', 'b', 'c'], + emptyArray: [] }; }); it('should support for', function() { @@ -24,6 +25,12 @@ describe('tags/for', function() { .to.be.rejectedWith(/tag .* not closed/); }); + it('should return else when for in empty array', function() { + var src = '{%for c in emptyArray%}a{%else%}b{%endfor%}'; + return expect(liquid.parseAndRender(src, ctx)) + .to.eventually.equal('b'); + }); + it('should support for else', function() { var src = '{%for c in ""%}a{%else%}b{%endfor%}'; return expect(liquid.parseAndRender(src, ctx)) @@ -80,13 +87,13 @@ describe('tags/for', function() { .to.eventually.equal('21'); }); - it('should support for reversed in the middle position', function() { + it('should support for reversed in the first position', function() { var src = '{% for i in (1..5) reversed limit:2 %}{{ i }}{% endfor %}'; return expect(liquid.parseAndRender(src, ctx)) .to.eventually.equal('21'); }); - it('should support for reversed in the first position', function() { + it('should support for reversed in the middle position', function() { var src = '{% for i in (1..5) offset:2 reversed limit:4 %}{{ i }}{% endfor %}'; return expect(liquid.parseAndRender(src, ctx)) .to.eventually.equal('543'); diff --git a/test/tags/if.js b/test/tags/if.js index 4992af9eef..14af05ede9 100644 --- a/test/tags/if.js +++ b/test/tags/if.js @@ -8,6 +8,7 @@ describe('tags/if', function() { var ctx = { one: 1, two: 2, + emptyString: '', emptyArray: [] }; @@ -19,7 +20,7 @@ describe('tags/if', function() { it('should support if 2', function() { var src = '{%if emptyArray%}a{%endif%}'; return expect(liquid.parseAndRender(src, ctx)) - .to.eventually.equal(''); + .to.eventually.equal('a'); }); it('should support if 3', function() { var src = '{% if 2==3 %}yes{%else%}no{%endif%}'; @@ -46,4 +47,9 @@ describe('tags/if', function() { return expect(liquid.parseAndRender(src, ctx)) .to.eventually.equal(''); }); + it('should return true if empty string', function() { + var src = "{%if emptyString%}a{%endif%}"; + return expect(liquid.parseAndRender(src, ctx)) + .to.eventually.equal('a'); + }); }); diff --git a/test/tags/unless.js b/test/tags/unless.js index dbb5c93f9d..0913b4f00f 100644 --- a/test/tags/unless.js +++ b/test/tags/unless.js @@ -7,12 +7,13 @@ describe('tags/unless', function() { var liquid = Liquid(); it('should render else when predicate yields true', function() { - var src = '{% unless 1 %}yes{%else%}no{%endunless%}'; + // 0 is truthy + var src = '{% unless 0 %}yes{%else%}no{%endunless%}'; return expect(liquid.parseAndRender(src)) .to.eventually.equal('no'); }); it('should render unless when predicate yields false', function() { - var src = '{% unless 0 %}yes{%else%}no{%endunless%}'; + var src = '{% unless false %}yes{%else%}no{%endunless%}'; return expect(liquid.parseAndRender(src)) .to.eventually.equal('yes'); });