mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-17 05:10:40 -07:00
feat: passing liquid to FilterImpl, closes #277
This commit is contained in:
+2
-2
@@ -31,7 +31,7 @@ export class Liquid {
|
|||||||
this.parser = new Parser(this)
|
this.parser = new Parser(this)
|
||||||
this.renderer = new Render()
|
this.renderer = new Render()
|
||||||
this.fs = opts.fs || fs
|
this.fs = opts.fs || fs
|
||||||
this.filters = new FilterMap(this.options.strictFilters)
|
this.filters = new FilterMap(this.options.strictFilters, this)
|
||||||
this.tags = new TagMap()
|
this.tags = new TagMap()
|
||||||
|
|
||||||
forOwn(builtinTags, (conf: TagImplOptions, name: string) => this.registerTag(snakeCase(name), conf))
|
forOwn(builtinTags, (conf: TagImplOptions, name: string) => this.registerTag(snakeCase(name), conf))
|
||||||
@@ -104,7 +104,7 @@ export class Liquid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public _evalValue (str: string, ctx: Context): IterableIterator<any> {
|
public _evalValue (str: string, ctx: Context): IterableIterator<any> {
|
||||||
const value = new Value(str, this.filters)
|
const value = new Value(str, this.filters, this)
|
||||||
return value.value(ctx)
|
return value.value(ctx)
|
||||||
}
|
}
|
||||||
public async evalValue (str: string, ctx: Context): Promise<any> {
|
public async evalValue (str: string, ctx: Context): Promise<any> {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export default class Parser {
|
|||||||
return new Tag(token, remainTokens, this.liquid)
|
return new Tag(token, remainTokens, this.liquid)
|
||||||
}
|
}
|
||||||
if (isOutputToken(token)) {
|
if (isOutputToken(token)) {
|
||||||
return new Output(token as OutputToken, this.liquid.filters)
|
return new Output(token as OutputToken, this.liquid.filters, this.liquid)
|
||||||
}
|
}
|
||||||
return new HTML(token)
|
return new HTML(token)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { Context } from '../../context/context'
|
import { Context } from '../../context/context'
|
||||||
|
import { Liquid } from '../../liquid'
|
||||||
|
|
||||||
export interface FilterImpl {
|
export interface FilterImpl {
|
||||||
context: Context;
|
context: Context;
|
||||||
|
liquid: Liquid;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,15 @@ import { FilterImplOptions } from './filter-impl-options'
|
|||||||
import { Filter } from './filter'
|
import { Filter } from './filter'
|
||||||
import { FilterArg } from '../../parser/filter-arg'
|
import { FilterArg } from '../../parser/filter-arg'
|
||||||
import { assert } from '../../util/assert'
|
import { assert } from '../../util/assert'
|
||||||
|
import { Liquid } from '../../liquid'
|
||||||
|
|
||||||
export class FilterMap {
|
export class FilterMap {
|
||||||
private impls: {[key: string]: FilterImplOptions} = {}
|
private impls: {[key: string]: FilterImplOptions} = {}
|
||||||
|
|
||||||
constructor (private readonly strictFilters: boolean) {}
|
constructor (
|
||||||
|
private readonly strictFilters: boolean,
|
||||||
|
private readonly liquid: Liquid
|
||||||
|
) {}
|
||||||
|
|
||||||
get (name: string) {
|
get (name: string) {
|
||||||
const impl = this.impls[name]
|
const impl = this.impls[name]
|
||||||
@@ -19,6 +23,6 @@ export class FilterMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
create (name: string, args: FilterArg[]) {
|
create (name: string, args: FilterArg[]) {
|
||||||
return new Filter(name, this.get(name), args)
|
return new Filter(name, this.get(name), args, this.liquid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,16 +3,19 @@ import { Context } from '../../context/context'
|
|||||||
import { identify } from '../../util/underscore'
|
import { identify } from '../../util/underscore'
|
||||||
import { FilterImplOptions } from './filter-impl-options'
|
import { FilterImplOptions } from './filter-impl-options'
|
||||||
import { FilterArg, isKeyValuePair } from '../../parser/filter-arg'
|
import { FilterArg, isKeyValuePair } from '../../parser/filter-arg'
|
||||||
|
import { Liquid } from '../../liquid'
|
||||||
|
|
||||||
export class Filter {
|
export class Filter {
|
||||||
public name: string
|
public name: string
|
||||||
public args: FilterArg[]
|
public args: FilterArg[]
|
||||||
private impl: FilterImplOptions
|
private impl: FilterImplOptions
|
||||||
|
private liquid: Liquid
|
||||||
|
|
||||||
public constructor (name: string, impl: FilterImplOptions, args: FilterArg[]) {
|
public constructor (name: string, impl: FilterImplOptions, args: FilterArg[], liquid: Liquid) {
|
||||||
this.name = name
|
this.name = name
|
||||||
this.impl = impl || identify
|
this.impl = impl || identify
|
||||||
this.args = args
|
this.args = args
|
||||||
|
this.liquid = liquid
|
||||||
}
|
}
|
||||||
public * render (value: any, context: Context) {
|
public * render (value: any, context: Context) {
|
||||||
const argv: any[] = []
|
const argv: any[] = []
|
||||||
@@ -20,6 +23,6 @@ export class Filter {
|
|||||||
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
|
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
|
||||||
else argv.push(yield evalToken(arg, context))
|
else argv.push(yield evalToken(arg, context))
|
||||||
}
|
}
|
||||||
return yield this.impl.apply({ context }, [value, ...argv])
|
return yield this.impl.apply({ context, liquid: this.liquid }, [value, ...argv])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ import { Template } from '../template/template'
|
|||||||
import { Context } from '../context/context'
|
import { Context } from '../context/context'
|
||||||
import { Emitter } from '../render/emitter'
|
import { Emitter } from '../render/emitter'
|
||||||
import { OutputToken } from '../tokens/output-token'
|
import { OutputToken } from '../tokens/output-token'
|
||||||
|
import { Liquid } from '../liquid'
|
||||||
|
|
||||||
export class Output extends TemplateImpl<OutputToken> implements Template {
|
export class Output extends TemplateImpl<OutputToken> implements Template {
|
||||||
private value: Value
|
private value: Value
|
||||||
public constructor (token: OutputToken, filters: FilterMap) {
|
public constructor (token: OutputToken, filters: FilterMap, liquid: Liquid) {
|
||||||
super(token)
|
super(token)
|
||||||
this.value = new Value(token.content, filters)
|
this.value = new Value(token.content, filters, liquid)
|
||||||
}
|
}
|
||||||
public * render (ctx: Context, emitter: Emitter) {
|
public * render (ctx: Context, emitter: Emitter) {
|
||||||
const val = yield this.value.value(ctx)
|
const val = yield this.value.value(ctx)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { Filter } from './filter/filter'
|
|||||||
import { Context } from '../context/context'
|
import { Context } from '../context/context'
|
||||||
import { ValueToken } from '../tokens/value-token'
|
import { ValueToken } from '../tokens/value-token'
|
||||||
import { assert } from '../util/assert'
|
import { assert } from '../util/assert'
|
||||||
|
import { Liquid } from '../liquid'
|
||||||
|
|
||||||
export class Value {
|
export class Value {
|
||||||
public readonly filters: Filter[] = []
|
public readonly filters: Filter[] = []
|
||||||
@@ -13,14 +14,14 @@ export class Value {
|
|||||||
/**
|
/**
|
||||||
* @param str the value to be valuated, eg.: "foobar" | truncate: 3
|
* @param str the value to be valuated, eg.: "foobar" | truncate: 3
|
||||||
*/
|
*/
|
||||||
public constructor (str: string, private readonly filterMap: FilterMap) {
|
public constructor (str: string, private readonly filterMap: FilterMap, liquid: Liquid) {
|
||||||
const tokenizer = new Tokenizer(str)
|
const tokenizer = new Tokenizer(str)
|
||||||
this.initial = tokenizer.readValue()
|
this.initial = tokenizer.readValue()
|
||||||
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args))
|
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args, liquid))
|
||||||
}
|
}
|
||||||
public * value (ctx: Context) {
|
public * value (ctx: Context) {
|
||||||
assert(ctx, () => 'unable to evaluate: context not defined')
|
assert(ctx, () => 'unable to evaluate: context not defined')
|
||||||
const lenient = ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name == "default"
|
const lenient = ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default'
|
||||||
|
|
||||||
let val = yield evalToken(this.initial, ctx, lenient)
|
let val = yield evalToken(this.initial, ctx, lenient)
|
||||||
for (const filter of this.filters) {
|
for (const filter of this.filters) {
|
||||||
|
|||||||
@@ -54,4 +54,15 @@ describe('Issues', function () {
|
|||||||
const html = engine.parseAndRenderSync(template, { condition1: true, condition2: true })
|
const html = engine.parseAndRenderSync(template, { condition1: true, condition2: true })
|
||||||
expect(html).to.equal('<div>Y</div>')
|
expect(html).to.equal('<div>Y</div>')
|
||||||
})
|
})
|
||||||
|
it('#277 Passing liquid in FilterImpl', () => {
|
||||||
|
const engine = new Liquid()
|
||||||
|
engine.registerFilter('render', function (template: string, name: string) {
|
||||||
|
return this.liquid.parseAndRenderSync(decodeURIComponent(template), { name })
|
||||||
|
})
|
||||||
|
const html = engine.parseAndRenderSync(
|
||||||
|
`{{ subtemplate | render: "foo" }}`,
|
||||||
|
{ subtemplate: encodeURIComponent('hello {{ name }}') }
|
||||||
|
)
|
||||||
|
expect(html).to.equal('hello foo')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -14,8 +14,9 @@ const expect = chai.expect
|
|||||||
describe('filter', function () {
|
describe('filter', function () {
|
||||||
let ctx: Context
|
let ctx: Context
|
||||||
let filters: FilterMap
|
let filters: FilterMap
|
||||||
|
const liquid = {} as any
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
filters = new FilterMap(false)
|
filters = new FilterMap(false, liquid)
|
||||||
ctx = new Context()
|
ctx = new Context()
|
||||||
})
|
})
|
||||||
it('should create default filter if not registered', async function () {
|
it('should create default filter if not registered', async function () {
|
||||||
@@ -34,12 +35,13 @@ describe('filter', function () {
|
|||||||
await toThenable(filters.create('foo', [thirty]).render('foo', ctx))
|
await toThenable(filters.create('foo', [thirty]).render('foo', ctx))
|
||||||
expect(spy).to.have.been.calledWith('foo', 30)
|
expect(spy).to.have.been.calledWith('foo', 30)
|
||||||
})
|
})
|
||||||
it('should call filter impl with correct this arg', async function () {
|
it('should call filter impl with correct this', async function () {
|
||||||
const spy = sinon.spy()
|
const spy = sinon.spy()
|
||||||
filters.set('foo', spy)
|
filters.set('foo', spy)
|
||||||
const thirty = new NumberToken(new IdentifierToken('33', 0, 2), undefined)
|
const thirty = new NumberToken(new IdentifierToken('33', 0, 2), undefined)
|
||||||
await toThenable(filters.create('foo', [thirty]).render('foo', ctx))
|
await toThenable(filters.create('foo', [thirty]).render('foo', ctx))
|
||||||
expect(spy).to.have.been.calledOn(sinon.match.has('context', ctx))
|
expect(spy).to.have.been.calledOn(sinon.match.has('context', ctx))
|
||||||
|
expect(spy).to.have.been.calledOn(sinon.match.has('liquid', liquid))
|
||||||
})
|
})
|
||||||
it('should render a simple filter', async function () {
|
it('should render a simple filter', async function () {
|
||||||
filters.set('upcase', x => x.toUpperCase())
|
filters.set('upcase', x => x.toUpperCase())
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ const expect = chai.expect
|
|||||||
|
|
||||||
describe('Output', function () {
|
describe('Output', function () {
|
||||||
const emitter: any = { write: (html: string) => (emitter.html += html), html: '' }
|
const emitter: any = { write: (html: string) => (emitter.html += html), html: '' }
|
||||||
|
const liquid = {} as any
|
||||||
let filters: FilterMap
|
let filters: FilterMap
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
filters = new FilterMap(false)
|
filters = new FilterMap(false, liquid)
|
||||||
emitter.html = ''
|
emitter.html = ''
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -19,25 +20,25 @@ describe('Output', function () {
|
|||||||
const scope = new Context({
|
const scope = new Context({
|
||||||
foo: { obj: { arr: ['a', 2] } }
|
foo: { obj: { arr: ['a', 2] } }
|
||||||
})
|
})
|
||||||
const output = new Output({ content: 'foo' } as OutputToken, filters)
|
const output = new Output({ content: 'foo' } as OutputToken, filters, liquid)
|
||||||
await toThenable(output.render(scope, emitter))
|
await toThenable(output.render(scope, emitter))
|
||||||
return expect(emitter.html).to.equal('[object Object]')
|
return expect(emitter.html).to.equal('[object Object]')
|
||||||
})
|
})
|
||||||
it('should skip function property', async function () {
|
it('should skip function property', async function () {
|
||||||
const scope = new Context({ obj: { foo: 'foo', bar: (x: any) => x } })
|
const scope = new Context({ obj: { foo: 'foo', bar: (x: any) => x } })
|
||||||
const output = new Output({ content: 'obj' } as OutputToken, filters)
|
const output = new Output({ content: 'obj' } as OutputToken, filters, liquid)
|
||||||
await toThenable(output.render(scope, emitter))
|
await toThenable(output.render(scope, emitter))
|
||||||
return expect(emitter.html).to.equal('[object Object]')
|
return expect(emitter.html).to.equal('[object Object]')
|
||||||
})
|
})
|
||||||
it('should respect to .toString()', async () => {
|
it('should respect to .toString()', async () => {
|
||||||
const scope = new Context({ obj: { toString: () => 'FOO' } })
|
const scope = new Context({ obj: { toString: () => 'FOO' } })
|
||||||
const output = new Output({ content: 'obj' } as OutputToken, filters)
|
const output = new Output({ content: 'obj' } as OutputToken, filters, liquid)
|
||||||
await toThenable(output.render(scope, emitter))
|
await toThenable(output.render(scope, emitter))
|
||||||
return expect(emitter.html).to.equal('FOO')
|
return expect(emitter.html).to.equal('FOO')
|
||||||
})
|
})
|
||||||
it('should respect to .toString()', async () => {
|
it('should respect to .toString()', async () => {
|
||||||
const scope = new Context({ obj: { toString: () => 'FOO' } })
|
const scope = new Context({ obj: { toString: () => 'FOO' } })
|
||||||
const output = new Output({ content: 'obj' } as OutputToken, filters)
|
const output = new Output({ content: 'obj' } as OutputToken, filters, liquid)
|
||||||
await toThenable(output.render(scope, emitter))
|
await toThenable(output.render(scope, emitter))
|
||||||
return expect(emitter.html).to.equal('FOO')
|
return expect(emitter.html).to.equal('FOO')
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -12,15 +12,17 @@ chai.use(sinonChai)
|
|||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
|
|
||||||
describe('Value', function () {
|
describe('Value', function () {
|
||||||
|
const liquid = {} as any
|
||||||
|
|
||||||
describe('#constructor()', function () {
|
describe('#constructor()', function () {
|
||||||
const filterMap = new FilterMap(false)
|
const filterMap = new FilterMap(false, liquid)
|
||||||
it('should parse "foo', function () {
|
it('should parse "foo', function () {
|
||||||
const tpl = new Value('foo', filterMap)
|
const tpl = new Value('foo', filterMap, liquid)
|
||||||
expect(tpl.initial!.getText()).to.equal('foo')
|
expect(tpl.initial!.getText()).to.equal('foo')
|
||||||
expect(tpl.filters).to.deep.equal([])
|
expect(tpl.filters).to.deep.equal([])
|
||||||
})
|
})
|
||||||
it('should parse filters in value content', function () {
|
it('should parse filters in value content', function () {
|
||||||
const f = new Value('o | foo: a: "a"', filterMap)
|
const f = new Value('o | foo: a: "a"', filterMap, liquid)
|
||||||
expect(f.filters[0].name).to.equal('foo')
|
expect(f.filters[0].name).to.equal('foo')
|
||||||
expect(f.filters[0].args).to.have.lengthOf(1)
|
expect(f.filters[0].args).to.have.lengthOf(1)
|
||||||
const [k, v] = f.filters[0].args[0] as any
|
const [k, v] = f.filters[0].args[0] as any
|
||||||
@@ -34,10 +36,10 @@ describe('Value', function () {
|
|||||||
it('should call chained filters correctly', async function () {
|
it('should call chained filters correctly', async function () {
|
||||||
const date = sinon.stub().returns('y')
|
const date = sinon.stub().returns('y')
|
||||||
const time = sinon.spy()
|
const time = sinon.spy()
|
||||||
const filterMap = new FilterMap(false)
|
const filterMap = new FilterMap(false, liquid)
|
||||||
filterMap.set('date', date)
|
filterMap.set('date', date)
|
||||||
filterMap.set('time', time)
|
filterMap.set('time', time)
|
||||||
const tpl = new Value('foo.bar | date: "b" | time:2', filterMap)
|
const tpl = new Value('foo.bar | date: "b" | time:2', filterMap, liquid)
|
||||||
const scope = new Context({
|
const scope = new Context({
|
||||||
foo: { bar: 'bar' }
|
foo: { bar: 'bar' }
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user