Skip to content

Commit

Permalink
fix: filters break when argument contains [()|, fixes #89
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Feb 23, 2019
1 parent 279458c commit e977669
Show file tree
Hide file tree
Showing 10 changed files with 2,100 additions and 2,092 deletions.
3,864 changes: 1,932 additions & 1,932 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 2 additions & 18 deletions src/template/filter/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,13 @@ export default class Filter {
args: string[]
private static impls: {[key: string]: FilterImpl} = {}

constructor (str: string, strictFilters: boolean = false) {
const match = lexical.filterLine.exec(str) as string[]
assert(match, 'illegal filter: ' + str)

const name = match[1]
const argList = match[2] || ''
constructor (name: string, args: string[], strictFilters: boolean) {
const impl = Filter.impls[name]
if (!impl && strictFilters) throw new TypeError(`undefined filter: ${name}`)

this.name = name
this.impl = impl || (x => x)
this.args = this.parseArgs(argList)
}
parseArgs (argList: string): string[] {
let match; const args: string[] = []
while ((match = valueRE.exec(argList.trim()))) {
const v = match[0]
const re = new RegExp(`${v}\\s*:`, 'g')
const keyMatch = re.exec(match.input)
const currentMatchIsKey = keyMatch && keyMatch.index === match.index
currentMatchIsKey ? args.push(`'${v}'`) : args.push(v)
}
return args
this.args = args
}
render (value: any, scope: Scope): any {
const args = this.args.map(arg => evalValue(arg, scope))
Expand Down
2 changes: 1 addition & 1 deletion src/template/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import OutputToken from 'src/parser/output-token'

export default class Output extends Template<OutputToken> implements ITemplate {
value: Value
constructor (token: OutputToken, strictFilters?: boolean) {
constructor (token: OutputToken, strictFilters: boolean) {
super(token)
this.value = new Value(token.value, strictFilters)
}
Expand Down
79 changes: 71 additions & 8 deletions src/template/value.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,84 @@
import { evalExp } from 'src/render/syntax'
import * as lexical from 'src/parser/lexical'
import assert from 'src/util/assert'
import Filter from './filter/filter'
import Scope from 'src/scope/scope'


enum ParseState {
INIT = 0,
FILTER_NAME = 1,
FILTER_ARG = 2
}

export default class {
initial: any
filters: Array<Filter> = []
constructor (str: string, strictFilters?: boolean) {
let match: RegExpExecArray | null = lexical.matchValue(str) as RegExpExecArray
assert(match, `illegal value string: ${str}`)

this.initial = match[0]
str = str.substr(match.index + match[0].length)
/**
* @param str value string, like: "i have a dream | truncate: 3
*/
constructor (str: string, strictFilters: boolean) {
const N = str.length
let buffer = ''
let quoted = ''
let state = ParseState.INIT
let sealed = false

let filterName = ''
let filterArgs: string[] = []

for(let i = 0; i < str.length; i++) {
if (quoted) {
if (str[i] == quoted) {
quoted = ''
sealed = true
}
buffer += str[i]
}
else if (/\s/.test(str[i])) {
if (!buffer) continue
else sealed = true
}
else if (str[i] === '|') {
if (state === ParseState.INIT) {
this.initial = buffer
}
else {
if (state === ParseState.FILTER_NAME) filterName = buffer
else filterArgs.push(buffer)
this.filters.push(new Filter(filterName, filterArgs, strictFilters))
filterName = ''
filterArgs = []
}
state = ParseState.FILTER_NAME
buffer = ''
sealed = false
}
else if (state === ParseState.FILTER_NAME && str[i] === ':') {
filterName = buffer
state = ParseState.FILTER_ARG
buffer = ''
sealed = false
}
else if (state === ParseState.FILTER_ARG && str[i] === ',') {
filterArgs.push(buffer)
buffer = ''
sealed = false
}
else if (sealed) continue
else {
if ((str[i] === '"' || str[i] === "'") && !quoted) quoted = str[i]
buffer += str[i]
}
}

while ((match = lexical.filter.exec(str))) {
this.filters.push(new Filter(match[0].trim(), strictFilters))
if (buffer) {
if (state === ParseState.INIT) this.initial = buffer
else if (state === ParseState.FILTER_NAME) this.filters.push(new Filter(buffer, [], strictFilters))
else {
filterArgs.push(buffer)
this.filters.push(new Filter(filterName, filterArgs, strictFilters))
}
}
}
value (scope: Scope) {
Expand Down
12 changes: 12 additions & 0 deletions test/unit/liquid/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ describe('Liquid', function () {
expect(html).to.equal('true')
})
})
describe('#parseAndRender', function () {
const engine = new Liquid()
it('should parse and render variable output', async function () {
const html = await engine.parseAndRender('{{"foo"}}')
expect(html).to.equal('foo')
})
it('should parse and render complex output', async function () {
const tpl = '{{ "Welcome|to]Liquid" | split: "|" | join: "("}}'
const html = await engine.parseAndRender(tpl)
expect(html).to.equal('Welcome(to]Liquid')
})
})
describe('#express()', function () {
const liquid = new Liquid({ root: '/root' })
const render = liquid.express()
Expand Down
95 changes: 0 additions & 95 deletions test/unit/template/filter.ts

This file was deleted.

51 changes: 51 additions & 0 deletions test/unit/template/filter/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as chai from 'chai'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import Filter from 'src/template/filter/filter'
import Scope from 'src/scope/scope'

chai.use(sinonChai)
const expect = chai.expect

describe('filter', function () {
let scope: Scope
beforeEach(function () {
Filter.clear()
scope = new Scope()
})
it('should create default filter if not registered', function () {
const result = new Filter('foo', [], false)
expect(result.name).to.equal('foo')
})

it('should render input if filter not registered', function () {
expect(new Filter('undefined', [], false).render('foo', scope)).to.equal('foo')
})

it('should call filter impl with corrct arguments', function () {
const spy = sinon.spy()
Filter.register('foo', spy)
new Filter('foo', ['33'], false).render('foo', scope)
expect(spy).to.have.been.calledWith('foo', 33)
})
it('should render a simple filter', function () {
Filter.register('upcase', x => x.toUpperCase())
expect(new Filter('upcase', [], false).render('foo', scope)).to.equal('FOO')
})

it('should render filters with argument', function () {
Filter.register('add', (a, b) => a + b)
expect(new Filter('add', ["2"], false).render(3, scope)).to.equal(5)
})

it('should render filters with multiple arguments', function () {
Filter.register('add', (a, b, c) => a + b + c)
expect(new Filter('add', ['2', '"c"'], false).render(3, scope)).to.equal('5c')
})

it('should not throw when filter name illegal', function () {
expect(function () {
new Filter('/', [], false)
}).to.not.throw()
})
})
14 changes: 7 additions & 7 deletions test/unit/template/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,46 @@ describe('Output', function () {
const scope = new Scope({
bar: { to_liquid: () => 'custom' }
})
const output = new Output({ value: 'bar' } as OutputToken)
const output = new Output({ value: 'bar' } as OutputToken, false)
const html = await output.render(scope)
return expect(html).to.equal('custom')
})
it('should stringify objects', async function () {
const scope = new Scope({
foo: { obj: { arr: ['a', 2] } }
})
const output = new Output({ value: 'foo' } as OutputToken)
const output = new Output({ value: 'foo' } as OutputToken, false)
const html = await output.render(scope)
return expect(html).to.equal('{"obj":{"arr":["a",2]}}')
})
it('should skip circular property', async function () {
const ctx = { foo: { num: 2 }, bar: 'bar' } as any
ctx.foo.circular = ctx
const output = new Output({ value: 'foo' } as OutputToken)
const output = new Output({ value: 'foo' } as OutputToken, false)
const html = await output.render(new Scope(ctx))
return expect(html).equal('{"num":2,"circular":{"bar":"bar"}}')
})
it('should skip function property', async function () {
const scope = new Scope({ obj: { foo: 'foo', bar: (x: any) => x } })
const output = new Output({ value: 'obj' } as OutputToken)
const output = new Output({ value: 'obj' } as OutputToken, false)
const html = await output.render(scope)
return expect(html).to.equal('{"foo":"foo"}')
})
it('should respect to .toString()', async () => {
const scope = new Scope({ obj: { toString: () => 'FOO' } })
const output = new Output({ value: 'obj' } as OutputToken)
const output = new Output({ value: 'obj' } as OutputToken, false)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
})
it('should respect to .to_s()', async () => {
const scope = new Scope({ obj: { to_s: () => 'FOO' } })
const output = new Output({ value: 'obj' } as OutputToken)
const output = new Output({ value: 'obj' } as OutputToken, false)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
})
it('should respect to .liquid_method_missing()', async () => {
const scope = new Scope({ obj: { liquid_method_missing: (x: string) => x.toUpperCase() } })
const output = new Output({ value: 'obj.foo' } as OutputToken)
const output = new Output({ value: 'obj.foo' } as OutputToken, false)
const str = await output.render(scope)
return expect(str).to.equal('FOO')
})
Expand Down
Loading

0 comments on commit e977669

Please sign in to comment.