Skip to content

Commit

Permalink
feat: pass context to filters
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Apr 17, 2019
1 parent 6fb4796 commit 00bc1ef
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 85 deletions.
4 changes: 1 addition & 3 deletions src/builtin/filters/string.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { FilterImpl } from '../../template/filter/filter-impl'

export default {
'append': (v: string, arg: string) => v + arg,
'prepend': (v: string, arg: string) => arg + v,
Expand Down Expand Up @@ -27,4 +25,4 @@ export default {
if (arr.length >= l) ret += o
return ret
}
} as {[key: string]: FilterImpl}
}
2 changes: 1 addition & 1 deletion src/builtin/tags/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export default {
this.value = match[2]
},
render: async function (ctx: Context) {
ctx.scopes[0][this.key] = await this.liquid.evalValue(this.value, ctx)
ctx.front()[this.key] = await this.liquid.evalValue(this.value, ctx)
}
} as ITagImplOptions
7 changes: 4 additions & 3 deletions src/builtin/tags/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ export default {
stream.start()
},
render: async function (ctx: Context) {
const childDefined = ctx.blocks[this.block]
const blocks = ctx.getRegister('blocks')
const childDefined = blocks[this.block]
const html = childDefined !== undefined
? childDefined
: await this.liquid.renderer.renderTemplates(this.tpls, ctx)

if (ctx.blockMode === BlockMode.STORE) {
ctx.blocks[this.block] = html
if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) {
blocks[this.block] = html
return ''
}
return html
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/tags/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export default {
},
render: async function (ctx: Context) {
const html = await this.liquid.renderer.renderTemplates(this.templates, ctx)
ctx.scopes[0][this.variable] = html
ctx.front()[this.variable] = html
}
} as ITagImplOptions
2 changes: 1 addition & 1 deletion src/builtin/tags/cycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default <ITagImplOptions>{
render: async function (ctx: Context) {
const group = await evalValue(this.group, ctx)
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
const groups = ctx.groups
const groups = ctx.getRegister('cycle')
let idx = groups[fingerprint]

if (idx === undefined) {
Expand Down
14 changes: 7 additions & 7 deletions src/builtin/tags/include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ export default <ITagImplOptions>{
}
assert(filepath, `cannot include with empty filename`)

const originBlocks = ctx.blocks
const originBlockMode = ctx.blockMode
const originBlocks = ctx.getRegister('blocks')
const originBlockMode = ctx.getRegister('blockMode')

ctx.blocks = {}
ctx.blockMode = BlockMode.OUTPUT
ctx.setRegister('blocks', {})
ctx.setRegister('blockMode', BlockMode.OUTPUT)
if (this.with) {
hash[filepath] = await evalValue(this.with, ctx)
}
const templates = await this.liquid.getTemplate(filepath, ctx.opts)
ctx.push(hash)
const html = await this.liquid.renderer.renderTemplates(templates, ctx)
ctx.pop(hash)
ctx.blocks = originBlocks
ctx.blockMode = originBlockMode
ctx.pop()
ctx.setRegister('blocks', originBlocks)
ctx.setRegister('blockMode', originBlockMode)
return html
}
}
11 changes: 6 additions & 5 deletions src/builtin/tags/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ export default {
assert(layout, `cannot apply layout with empty filename`)

// render the remaining tokens immediately
ctx.blockMode = BlockMode.STORE
ctx.setRegister('blockMode', BlockMode.STORE)
const blocks = ctx.getRegister('blocks')
const html = await this.liquid.renderer.renderTemplates(this.tpls, ctx)
if (ctx.blocks[''] === undefined) {
ctx.blocks[''] = html
if (blocks[''] === undefined) {
blocks[''] = html
}
const templates = await this.liquid.getTemplate(layout, ctx.opts)
ctx.push(hash)
ctx.blockMode = BlockMode.OUTPUT
ctx.setRegister('blockMode', BlockMode.OUTPUT)
const partial = await this.liquid.renderer.renderTemplates(templates, ctx)
ctx.pop(hash)
ctx.pop()
return partial
}
} as ITagImplOptions
2 changes: 1 addition & 1 deletion src/builtin/tags/tablerow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default {
html += '</td>'
}
if (collection.length) html += '</tr>'
ctx.pop(scope)
ctx.pop()
return html
}
} as ITagImplOptions
50 changes: 25 additions & 25 deletions src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import { Drop } from '../drop/drop'
import { __assign } from 'tslib'
import assert from '../util/assert'
import { NormalizedFullOptions, applyDefault } from '../liquid-options'
import BlockMode from './block-mode'
import { Scope } from './scope'

export default class Context {
opts: NormalizedFullOptions
scopes: Array<Scope> = [{}]
environments: Scope
blocks: object = {}
groups: {[key: string]: number} = {}
blockMode: BlockMode = BlockMode.OUTPUT
private scopes: Array<Scope> = [{}]
private registers = {}
constructor (ctx: object = {}, opts?: NormalizedFullOptions) {
this.opts = applyDefault(opts)
this.environments = ctx
}
getRegister(key: string, defaultValue = {}) {
return this.registers[key] = this.registers[key] || defaultValue
}
setRegister(key: string, value: any) {
return this.registers[key] = value
}
getAll () {
return [this.environments, ...this.scopes]
.reduce((ctx, val) => __assign(ctx, val), {})
Expand All @@ -25,7 +28,7 @@ export default class Context {
const paths = await this.parseProp(path)
let ctx = this.findScope(paths[0]) || this.environments
for (const path of paths) {
ctx = this.readProperty(ctx, path)
ctx = readProperty(ctx, path)
if (_.isNil(ctx) && this.opts.strictVariables) {
throw new TypeError(`undefined variable: ${path}`)
}
Expand All @@ -35,15 +38,11 @@ export default class Context {
push (ctx: object) {
return this.scopes.push(ctx)
}
pop (ctx?: object): object | undefined {
if (!arguments.length) {
return this.scopes.pop()
}
const i = this.scopes.findIndex(scope => scope === ctx)
if (i === -1) {
throw new TypeError('scope not found, cannot pop')
}
return this.scopes.splice(i, 1)[0]
pop () {
return this.scopes.pop()
}
front () {
return this.scopes[0]
}
private findScope (key: string) {
for (let i = this.scopes.length - 1; i >= 0; i--) {
Expand All @@ -54,16 +53,6 @@ export default class Context {
}
return null
}
private readProperty (obj: Scope, key: string) {
if (_.isNil(obj)) return obj
obj = _.toLiquid(obj)
if (obj instanceof Drop) {
if (_.isFunction(obj[key])) return obj[key]()
if (obj.hasOwnProperty(key)) return obj[key]
return obj.liquidMethodMissing(key)
}
return key === 'size' ? readSize(obj) : obj[key]
}

/*
* Parse property access sequence from access string
Expand Down Expand Up @@ -124,6 +113,17 @@ export default class Context {
}
}

function readProperty (obj: Scope, key: string) {
if (_.isNil(obj)) return obj
obj = _.toLiquid(obj)
if (obj instanceof Drop) {
if (_.isFunction(obj[key])) return obj[key]()
if (obj.hasOwnProperty(key)) return obj[key]
return obj.liquidMethodMissing(key)
}
return key === 'size' ? readSize(obj) : obj[key]
}

function readSize (obj: Scope) {
if (!_.isNil(obj['size'])) return obj['size']
if (_.isArray(obj) || _.isString(obj)) return obj.length
Expand Down
4 changes: 2 additions & 2 deletions src/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { isTruthy, isFalsy, evalExp, evalValue } from './render/syntax'
import builtinTags from './builtin/tags'
import builtinFilters from './builtin/filters'
import { LiquidOptions, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
import { FilterImpl } from './template/filter/filter-impl'
import { FilterImplOptions } from './template/filter/filter-impl-options'

export default class Liquid {
public options: NormalizedFullOptions
Expand Down Expand Up @@ -72,7 +72,7 @@ export default class Liquid {
evalValue (str: string, ctx: Context) {
return new Value(str, this.options.strictFilters).value(ctx)
}
registerFilter (name: string, filter: FilterImpl) {
registerFilter (name: string, filter: FilterImplOptions) {
return Filter.register(name, filter)
}
registerTag (name: string, tag: ITagImplOptions) {
Expand Down
3 changes: 3 additions & 0 deletions src/template/filter/filter-impl-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { FilterImpl } from "./filter-impl";

export type FilterImplOptions = (this: FilterImpl, value: any, ...args: any[]) => any
6 changes: 5 additions & 1 deletion src/template/filter/filter-impl.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export type FilterImpl = (value: any, ...args: any[]) => any
import Context from "../../context/context";

export interface FilterImpl {
context: Context
}
16 changes: 8 additions & 8 deletions src/template/filter/filter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { evalValue } from '../../render/syntax'
import Context from '../../context/context'
import { isArray } from '../../util/underscore'
import { FilterImpl } from './filter-impl'
import { FilterImplOptions } from './filter-impl-options'

export type FilterArgs = Array<string|[string?, string?]>

export class Filter {
name: string
impl: FilterImpl
impl: FilterImplOptions
args: FilterArgs
private static impls: {[key: string]: FilterImpl} = {}
private static impls: {[key: string]: FilterImplOptions} = {}

constructor (name: string, args: FilterArgs, strictFilters: boolean) {
const impl = Filter.impls[name]
Expand All @@ -19,15 +19,15 @@ export class Filter {
this.impl = impl || (x => x)
this.args = args
}
async render (value: any, ctx: Context) {
async render (value: any, context: Context) {
const argv: any[] = []
for (const arg of this.args) {
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], ctx)])
else argv.push(await evalValue(arg, ctx))
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], context)])
else argv.push(await evalValue(arg, context))
}
return this.impl.apply(null, [value, ...argv])
return this.impl.apply({ context }, [value, ...argv])
}
static register (name: string, filter: FilterImpl) {
static register (name: string, filter: FilterImplOptions) {
Filter.impls[name] = filter
}
static clear () {
Expand Down
31 changes: 5 additions & 26 deletions test/unit/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ describe('scope', function () {
return expect(ctx.get('notdefined')).to.be.rejectedWith(/undefined variable: notdefined/)
})
it('should throw when deep variable not exist', async function () {
ctx.scopes.push({ 'foo': 'FOO' })
ctx.push({ foo: 'FOO' })
return expect(ctx.get('foo.bar.not.defined')).to.be.rejectedWith(/undefined variable: bar/)
})
it('should throw when itself not defined', async function () {
ctx.scopes.push({ 'foo': 'FOO' })
ctx.push({ foo: 'FOO' })
return expect(ctx.get('foo.BAR')).to.be.rejectedWith(/undefined variable: BAR/)
})
it('should find variable in parent scope', async function () {
ctx.scopes.push({ 'foo': 'foo' })
ctx.push({ 'foo': 'foo' })
ctx.push({
'bar': 'bar'
})
Expand All @@ -161,15 +161,15 @@ describe('scope', function () {

describe('.push()', function () {
it('should push scope', async function () {
ctx.scopes.push({ 'bar': 'bar' })
ctx.push({ 'bar': 'bar' })
ctx.push({
foo: 'foo'
})
expect(await ctx.get('foo')).to.equal('foo')
expect(await ctx.get('bar')).to.equal('bar')
})
it('should hide deep properties by push', async function () {
ctx.scopes.push({ 'bar': { bar: 'bar' } })
ctx.push({ bar: { bar: 'bar' } })
ctx.push({ bar: { foo: 'foo' } })
expect(await ctx.get('bar.foo')).to.equal('foo')
expect(await ctx.get('bar.bar')).to.equal(undefined)
Expand All @@ -184,25 +184,4 @@ describe('scope', function () {
expect(await ctx.get('foo')).to.equal('zoo')
})
})
it('should pop specified scope', async function () {
const scope1 = {
foo: 'foo'
}
const scope2 = {
bar: 'bar'
}
ctx.push(scope1)
ctx.push(scope2)
expect(await ctx.get('foo')).to.equal('foo')
expect(await ctx.get('bar')).to.equal('bar')
ctx.pop(scope1)
expect(await ctx.get('foo')).to.equal('zoo')
expect(await ctx.get('bar')).to.equal('bar')
})
it('should throw when specified scope not found', function () {
const scope1 = {
foo: 'foo'
}
expect(() => ctx.pop(scope1)).to.throw('scope not found, cannot pop')
})
})
8 changes: 7 additions & 1 deletion test/unit/template/filter/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ describe('filter', function () {
expect(await new Filter('undefined', [], false).render('foo', ctx)).to.equal('foo')
})

it('should call filter impl with corrct arguments', async function () {
it('should call filter impl with correct arguments', async function () {
const spy = sinon.spy()
Filter.register('foo', spy)
await new Filter('foo', ['33'], false).render('foo', ctx)
expect(spy).to.have.been.calledWith('foo', 33)
})
it('should call filter impl with correct this arg', async function () {
const spy = sinon.spy()
Filter.register('foo', spy)
await new Filter('foo', ['33'], false).render('foo', ctx)
expect(spy).to.have.been.calledOn(sinon.match.has('context', ctx))
})
it('should render a simple filter', async function () {
Filter.register('upcase', x => x.toUpperCase())
expect(await new Filter('upcase', [], false).render('foo', ctx)).to.equal('FOO')
Expand Down

0 comments on commit 00bc1ef

Please sign in to comment.