diff --git a/src/drop/empty-drop.ts b/src/drop/empty-drop.ts index 7b7ec958a..cf9664bda 100644 --- a/src/drop/empty-drop.ts +++ b/src/drop/empty-drop.ts @@ -1,9 +1,11 @@ import { Drop } from './drop' import { Comparable } from './comparable' -import { isObject, isString, isArray } from '../util/underscore' +import { isObject, isString, isArray, toValue } from '../util/underscore' export class EmptyDrop extends Drop implements Comparable { public equals (value: any) { + if (value instanceof EmptyDrop) return false + value = toValue(value) if (isString(value) || isArray(value)) return value.length === 0 if (isObject(value)) return Object.keys(value).length === 0 return false diff --git a/src/drop/null-drop.ts b/src/drop/null-drop.ts index 9b86e0ea5..ea7e322c3 100644 --- a/src/drop/null-drop.ts +++ b/src/drop/null-drop.ts @@ -1,11 +1,10 @@ import { Drop } from './drop' import { Comparable } from './comparable' import { isNil, toValue } from '../util/underscore' -import { BlankDrop } from '../drop/blank-drop' export class NullDrop extends Drop implements Comparable { public equals (value: any) { - return isNil(toValue(value)) || value instanceof BlankDrop + return isNil(toValue(value)) } public gt () { return false diff --git a/src/util/literal.ts b/src/util/literal.ts index c1091e5d5..212d0424f 100644 --- a/src/util/literal.ts +++ b/src/util/literal.ts @@ -2,11 +2,12 @@ import { NullDrop } from '../drop/null-drop' import { EmptyDrop } from '../drop/empty-drop' import { BlankDrop } from '../drop/blank-drop' +const nil = new NullDrop() export const literalValues = { 'true': true, 'false': false, - 'nil': new NullDrop(), - 'null': new NullDrop(), + 'nil': nil, + 'null': nil, 'empty': new EmptyDrop(), 'blank': new BlankDrop() } diff --git a/test/e2e/issues.ts b/test/e2e/issues.ts index 63ca145ab..4c7c0b038 100644 --- a/test/e2e/issues.ts +++ b/test/e2e/issues.ts @@ -87,4 +87,12 @@ describe('Issues', function () { const html = await engine.parseAndRender(`{{ name | default: "default name" }}`) expect(html).to.equal('default name') }) + it('#321 comparison for empty/nil', async () => { + const engine = new Liquid() + const html = await engine.parseAndRender( + '{% if empty == nil %}true{%else%}false{%endif%}' + + '{% if nil == empty %}true{%else%}false{%endif%}' + ) + expect(html).to.equal('falsefalse') + }) }) diff --git a/test/integration/drop/empty-drop.ts b/test/integration/drop/empty-drop.ts index 26045057b..0760d43af 100644 --- a/test/integration/drop/empty-drop.ts +++ b/test/integration/drop/empty-drop.ts @@ -79,4 +79,14 @@ describe('drop/empty-drop', function () { const html = await liquid.parseAndRender(src) expect(html).to.equal('true') }) + it('empty != empty', async function () { + const src = '{%if empty == empty %}true{%else%}false{% endif %}' + const html = await liquid.parseAndRender(src) + expect(html).to.equal('false') + }) + it('empty != nil', async function () { + const src = '{%if empty == nil %}true{%else%}false{% endif %}' + const html = await liquid.parseAndRender(src) + expect(html).to.equal('false') + }) }) diff --git a/test/integration/drop/null-drop.ts b/test/integration/drop/null-drop.ts index a0c1b3cf7..fd2ae96c7 100644 --- a/test/integration/drop/null-drop.ts +++ b/test/integration/drop/null-drop.ts @@ -13,22 +13,27 @@ describe('drop/null-drop', function () { const html = await liquid.parseAndRender('{{null}}') expect(html).to.equal('') }) - it('undefined variable should equal to null', async function () { + it('undefined == null', async function () { const src = '{%if foo == nil %}foo == nil{%else%}foo != nil{% endif %}' const html = await liquid.parseAndRender(src) expect(html).to.equal('foo == nil') }) - it('nil equals blank', async function () { - const src = '{%if nil == blank %}nil == blank{%else%}nil != blank{% endif %}' + it('nil != blank', async function () { + const src = '{%if nil == blank %}eq{%else%}neq{% endif %}' const html = await liquid.parseAndRender(src) - expect(html).to.equal('nil == blank') + expect(html).to.equal('neq') }) - it('0 should not equal to null', async function () { + it('nil != empty', async function () { + const src = '{%if nil == empty %}eq{%else%}neq{% endif %}' + const html = await liquid.parseAndRender(src) + expect(html).to.equal('neq') + }) + it('0 != null', async function () { const src = '{%if 0 == null %}0 == null{%else%}0 != null{% endif %}' const html = await liquid.parseAndRender(src) expect(html).to.equal('0 != null') }) - it('nil should equal to null', async function () { + it('nil == null', async function () { const src = '{%if nil == null %}nil == null{%else%}nil != null{% endif %}' const html = await liquid.parseAndRender(src) expect(html).to.equal('nil == null')