Skip to content

Commit

Permalink
fix: throws on invalid arguments for prepend/append, fixes #208
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Mar 24, 2020
1 parent 60c14ba commit 479c633
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 115 deletions.
31 changes: 12 additions & 19 deletions src/builtin/filters/array.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
import { isArray, last } from '../../util/underscore'
import { isArray, last as arrayLast } from '../../util/underscore'
import { isTruthy } from '../../render/boolean'
import { FilterImpl } from '../../template/filter/filter-impl'

export default {
'join': (v: any[], arg: string) => v.join(arg === undefined ? ' ' : arg),
'last': (v: any) => isArray(v) ? last(v) : '',
'first': (v: any) => isArray(v) ? v[0] : '',
'map': map,
'reverse': (v: any[]) => [...v].reverse(),
'sort': <T>(v: T[], arg: (lhs: T, rhs: T) => number) => v.sort(arg),
'size': (v: string | any[]) => (v && v.length) || 0,
'concat': concat,
'slice': slice,
'uniq': uniq,
'where': where
}
export const join = (v: any[], arg: string) => v.join(arg === undefined ? ' ' : arg)
export const last = (v: any) => isArray(v) ? arrayLast(v) : ''
export const first = (v: any) => isArray(v) ? v[0] : ''
export const reverse = (v: any[]) => [...v].reverse()
export const sort = <T>(v: T[], arg: (lhs: T, rhs: T) => number) => v.sort(arg)
export const size = (v: string | any[]) => (v && v.length) || 0

function map<T1, T2> (arr: {[key: string]: T1}[], arg: string): T1[] {
export function map<T1, T2> (arr: {[key: string]: T1}[], arg: string): T1[] {
return arr.map(v => v[arg])
}

function concat<T1, T2> (v: T1[], arg: T2[] | T2): (T1 | T2)[] {
export function concat<T1, T2> (v: T1[], arg: T2[] | T2): (T1 | T2)[] {
return Array.prototype.concat.call(v, arg)
}

function slice<T> (v: T[], begin: number, length = 1): T[] {
export function slice<T> (v: T[], begin: number, length = 1): T[] {
begin = begin < 0 ? v.length + begin : begin
return v.slice(begin, begin + length)
}

function where<T extends object> (this: FilterImpl, arr: T[], property: string, expected?: any): T[] {
export function where<T extends object> (this: FilterImpl, arr: T[], property: string, expected?: any): T[] {
return arr.filter(obj => {
const value = this.context.getFromScope(obj, property.split('.'))
return expected === undefined ? isTruthy(value) : value === expected
})
}

function uniq<T> (arr: T[]): T[] {
export function uniq<T> (arr: T[]): T[] {
const u = {}
return (arr || []).filter(val => {
if (u.hasOwnProperty(String(val))) return false
Expand Down
20 changes: 9 additions & 11 deletions src/builtin/filters/date.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import strftime from '../../util/strftime'
import { isString, isNumber } from '../../util/underscore'

export default {
'date': (v: string | Date, arg: string) => {
let date = v
if (v === 'now' || v === 'today') {
date = new Date()
} else if (isNumber(v)) {
date = new Date(v * 1000)
} else if (isString(v)) {
date = /^\d+$/.test(v) ? new Date(+v * 1000) : new Date(v)
}
return isValidDate(date) ? strftime(date, arg) : v
export function date (v: string | Date, arg: string) {
let date = v
if (v === 'now' || v === 'today') {
date = new Date()
} else if (isNumber(v)) {
date = new Date(v * 1000)
} else if (isString(v)) {
date = /^\d+$/.test(v) ? new Date(+v * 1000) : new Date(v)
}
return isValidDate(date) ? strftime(date, arg) : v
}

function isValidDate (date: any): date is Date {
Expand Down
17 changes: 11 additions & 6 deletions src/builtin/filters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ const unescapeMap = {
'&#39;': "'"
}

function escape (str: string) {
export function escape (str: string) {
return stringify(str).replace(/&|<|>|"|'/g, m => escapeMap[m])
}

function unescape (str: string) {
return String(str).replace(/&(amp|lt|gt|#34|#39);/g, m => unescapeMap[m])
}

export default {
'escape': escape,
'escape_once': (str: string) => escape(unescape(str)),
'newline_to_br': (v: string) => v.replace(/\n/g, '<br />'),
'strip_html': (v: string) => v.replace(/<script.*?<\/script>|<!--.*?-->|<style.*?<\/style>|<.*?>/g, '')
export function escapeOnce (str: string) {
return escape(unescape(str))
}

export function newlineToBr (v: string) {
return v.replace(/\n/g, '<br />')
}

export function stripHtml (v: string) {
return v.replace(/<script.*?<\/script>|<!--.*?-->|<style.*?<\/style>|<.*?>/g, '')
}
16 changes: 7 additions & 9 deletions src/builtin/filters/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import html from './html'
import str from './string'
import math from './math'
import url from './url'
import array from './array'
import date from './date'
import obj from './object'

export default { ...html, ...str, ...math, ...url, ...date, ...obj, ...array }
export * from './html'
export * from './math'
export * from './url'
export * from './array'
export * from './date'
export * from './object'
export * from './string'
35 changes: 18 additions & 17 deletions src/builtin/filters/math.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { caseInsensitiveCompare } from '../../util/underscore'

export default {
'abs': (v: number) => Math.abs(v),
'at_least': (v: number, n: number) => Math.max(v, n),
'at_most': (v: number, n: number) => Math.min(v, n),
'ceil': (v: number) => Math.ceil(v),
'divided_by': (v: number, arg: number) => v / arg,
'floor': (v: number) => Math.floor(v),
'minus': (v: number, arg: number) => v - arg,
'modulo': (v: number, arg: number) => v % arg,
'round': (v: number, arg = 0) => {
const amp = Math.pow(10, arg)
return Math.round(v * amp) / amp
},
'plus': (v: number, arg: number) => Number(v) + Number(arg),
'sort_natural': sortNatural,
'times': (v: number, arg: number) => v * arg
export const abs = Math.abs
export const atLeast = Math.max
export const atMost = Math.min
export const ceil = Math.ceil
export const dividedBy = (v: number, arg: number) => v / arg
export const floor = Math.floor
export const minus = (v: number, arg: number) => v - arg
export const modulo = (v: number, arg: number) => v % arg
export const times = (v: number, arg: number) => v * arg

export function round (v: number, arg = 0) {
const amp = Math.pow(10, arg)
return Math.round(v * amp) / amp
}

export function plus (v: number, arg: number) {
return Number(v) + Number(arg)
}

function sortNatural (input: any[], property?: string) {
export function sortNatural (input: any[], property?: string) {
if (!input || !input.sort) return []
if (property !== undefined) {
return [...input].sort(
Expand Down
12 changes: 5 additions & 7 deletions src/builtin/filters/object.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { isFalsy } from '../../render/boolean'
import { toValue } from '../../util/underscore'

export default {
'default': function<T1, T2> (v: string | T1, arg: T2): string | T1 | T2 {
return isFalsy(toValue(v)) || v === '' ? arg : v
},
'json': function (v: any) {
return JSON.stringify(v)
}
export function Default<T1, T2> (v: string | T1, arg: T2): string | T1 | T2 {
return isFalsy(toValue(v)) || v === '' ? arg : v
}
export function json (v: any) {
return JSON.stringify(v)
}
76 changes: 52 additions & 24 deletions src/builtin/filters/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,74 @@
* * prefer stringify() to String() since `undefined`, `null` should eval ''
*/
import { stringify } from '../../util/underscore'
import { assert } from '../../util/assert'

export default {
'append': (v: string, arg: string) => stringify(v) + stringify(arg),
'prepend': (v: string, arg: string) => stringify(arg) + stringify(v),
'capitalize': capitalize,
'lstrip': (v: string) => stringify(v).replace(/^\s+/, ''),
'downcase': (v: string) => stringify(v).toLowerCase(),
'upcase': (str: string) => stringify(str).toUpperCase(),
'remove': (v: string, arg: string) => stringify(v).split(arg).join(''),
'remove_first': (v: string, l: string) => stringify(v).replace(l, ''),
'replace': replace,
'replace_first': replaceFirst,
'rstrip': (str: string) => stringify(str).replace(/\s+$/, ''),
'split': (v: string, arg: string) => stringify(v).split(arg),
'strip': (v: string) => stringify(v).trim(),
'strip_newlines': (v: string) => stringify(v).replace(/\n/g, ''),
'truncate': truncate,
'truncatewords': truncateWords
}

function capitalize (str: string) {
export function append (v: string, arg: string) {
assert(arg !== undefined, () => 'append expect 2 arguments')
return stringify(v) + stringify(arg)
}

export function prepend (v: string, arg: string) {
assert(arg !== undefined, () => 'prepend expect 2 arguments')
return stringify(arg) + stringify(v)
}

export function lstrip (v: string) {
return stringify(v).replace(/^\s+/, '')
}

export function downcase (v: string) {
return stringify(v).toLowerCase()
}

export function upcase (str: string) {
return stringify(str).toUpperCase()
}

export function remove (v: string, arg: string) {
return stringify(v).split(arg).join('')
}

export function removeFirst (v: string, l: string) {
return stringify(v).replace(l, '')
}

export function rstrip (str: string) {
return stringify(str).replace(/\s+$/, '')
}

export function split (v: string, arg: string) {
return stringify(v).split(arg)
}

export function strip (v: string) {
return stringify(v).trim()
}

export function stripNewlines (v: string) {
return stringify(v).replace(/\n/g, '')
}

export function capitalize (str: string) {
str = stringify(str)
return str.charAt(0).toUpperCase() + str.slice(1)
}

function replace (v: string, pattern: string, replacement: string) {
export function replace (v: string, pattern: string, replacement: string) {
return stringify(v).split(pattern).join(replacement)
}

function replaceFirst (v: string, arg1: string, arg2: string) {
export function replaceFirst (v: string, arg1: string, arg2: string) {
return stringify(v).replace(arg1, arg2)
}

function truncate (v: string, l = 50, o = '...') {
export function truncate (v: string, l = 50, o = '...') {
v = stringify(v)
if (v.length <= l) return v
return v.substr(0, l - o.length) + o
}

function truncateWords (v: string, l = 15, o = '...') {
export function truncatewords (v: string, l = 15, o = '...') {
const arr = v.split(/\s+/)
let ret = arr.slice(0, l).join(' ')
if (arr.length >= l) ret += o
Expand Down
6 changes: 2 additions & 4 deletions src/builtin/filters/url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export default {
'url_decode': (x: string) => x.split('+').map(decodeURIComponent).join(' '),
'url_encode': (x: string) => x.split(' ').map(encodeURIComponent).join('+')
}
export const urlDecode = (x: string) => x.split('+').map(decodeURIComponent).join(' ')
export const urlEncode = (x: string) => x.split(' ').map(encodeURIComponent).join('+')
8 changes: 4 additions & 4 deletions src/liquid.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Context } from './context/context'
import * as fs from './fs/node'
import * as _ from './util/underscore'
import { forOwn, snakeCase } from './util/underscore'
import { Template } from './template/template'
import { Tokenizer } from './parser/tokenizer'
import { Render } from './render/render'
import Parser from './parser/parser'
import { TagImplOptions } from './template/tag/tag-impl-options'
import { Value } from './template/value'
import builtinTags from './builtin/tags'
import builtinFilters from './builtin/filters'
import * as builtinFilters from './builtin/filters'
import { TagMap } from './template/tag/tag-map'
import { FilterMap } from './template/filter/filter-map'
import { LiquidOptions, normalizeStringArray, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
Expand All @@ -34,8 +34,8 @@ export class Liquid {
this.filters = new FilterMap(this.options.strictFilters)
this.tags = new TagMap()

_.forOwn(builtinTags, (conf, name) => this.registerTag(name, conf))
_.forOwn(builtinFilters, (handler, name) => this.registerFilter(name, handler))
forOwn(builtinTags, (conf, name) => this.registerTag(snakeCase(name), conf))
forOwn(builtinFilters, (handler, name) => this.registerFilter(snakeCase(name), handler))
}
public parse (html: string, filepath?: string): Template[] {
const tokenizer = new Tokenizer(html, filepath)
Expand Down
7 changes: 7 additions & 0 deletions src/util/underscore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ export function identify<T> (val: T): T {
return val
}

export function snakeCase (str: string) {
return str.replace(
/(\w?)([A-Z])/g,
(_, a, b) => (a ? a + '_' : '') + b.toLowerCase()
)
}

export function changeCase (str: string): string {
const hasLowerCase = [...str].some(ch => ch >= 'a' && ch <= 'z')
return hasLowerCase ? str.toUpperCase() : str.toLowerCase()
Expand Down
13 changes: 13 additions & 0 deletions test/integration/builtin/filters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ describe('filters/html', function () {
it('should not escape twice',
() => test('{{ "1 &lt; 2 &amp; 3" | escape_once }}', '1 &lt; 2 &amp; 3'))
})
describe('newline_to_br', function () {
it('should support string_with_newlines', function () {
const src = '{% capture string_with_newlines %}\n' +
'Hello\n' +
'there\n' +
'{% endcapture %}' +
'{{ string_with_newlines | newline_to_br }}'
const dst = '<br />' +
'Hello<br />' +
'there<br />'
return test(src, dst)
})
})
describe('strip_html', function () {
it('should strip all tags', function () {
return test('{{ "Have <em>you</em> read <cite><a href=&quot;https://en.wikipedia.org/wiki/Ulysses_(novel)&quot;>Ulysses</a></cite>?" | strip_html }}',
Expand Down
Loading

0 comments on commit 479c633

Please sign in to comment.