Skip to content

Commit

Permalink
feat: support function calls, closes #222
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Feb 12, 2021
1 parent 81e11bb commit e37824f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export class Context {
export function readProperty (obj: Scope, key: string) {
if (isNil(obj)) return obj
obj = toLiquid(obj)
if (isFunction(obj[key])) return obj[key]()
if (obj instanceof Drop) {
if (isFunction(obj[key])) return obj[key]()
if (obj.hasOwnProperty(key)) return obj[key]
return obj.liquidMethodMissing(key)
}
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,12 @@ describe('Issues', function () {
const html = await engine.parseAndRender(`{{ '{{' }}{{ '}}' }}`)
expect(html).to.equal('{{}}')
})
it('#222 Support function calls', async () => {
const engine = new Liquid()
const html = await engine.parseAndRender(
`{{ obj.property }}`,
{ obj: { property: () => 'BAR' } }
)
expect(html).to.equal('BAR')
})
})
3 changes: 0 additions & 3 deletions test/integration/builtin/filters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ describe('filters/html', function () {
it('should escape normal string', function () {
return test('{{ "Tetsuro Takara" | escape }}', 'Tetsuro Takara')
})
it('should escape function', function () {
return test('{{ func | escape }}', { func: function () {} }, 'function () { }')
})
it('should escape undefined', function () {
return test('{{ nonExistent.value | escape }}', '')
})
Expand Down
8 changes: 8 additions & 0 deletions test/unit/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe('Context', function () {
first: 'f',
last: 'l'
},
func: () => 'FUNC',
objFunc: () => ({ prop: 'PROP' }),
bar: {
zoo: 'coo',
'Mr.Smith': 'John',
Expand Down Expand Up @@ -59,6 +61,12 @@ describe('Context', function () {
it('should read .last of array', async function () {
expect(ctx.get(['bar', 'arr', 'last'])).to.equal('b')
})
it('should call function', async function () {
expect(ctx.get(['func'])).to.equal('FUNC')
})
it('should call function before read nested property', async function () {
expect(ctx.get(['objFunc', 'prop'])).to.equal('PROP')
})
})

describe('#getFromScope()', function () {
Expand Down

0 comments on commit e37824f

Please sign in to comment.