mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 14:00:39 -07:00
fix(filters): return empty for out-of-range slice begin or negative length (#928)
Ruby/Shopify `slice` returns nil (rendered as an empty string or array) when
the begin offset falls outside the negative range or when the length is
negative. liquidjs forwarded the adjusted indices straight to
Array/String.prototype.slice, whose own negative-index handling produced
non-empty, incorrect output:
{{ "hello" | slice: -10, 2 }} => "he" (expected "")
{{ "Liquid" | slice: 1, -2 }} => "iqui" (expected "")
Guard the adjusted begin and the length before slicing.
This commit is contained in:
@@ -114,6 +114,7 @@ export function slice<T> (this: FilterImpl, v: T[] | string, begin: number, leng
|
||||
if (isNil(v)) return []
|
||||
if (!isArray(v)) v = stringify(v)
|
||||
begin = begin < 0 ? v.length + begin : begin
|
||||
if (begin < 0 || length < 0) return isArray(v) ? [] : ''
|
||||
this.context.memoryLimit.use(length)
|
||||
return isArray(v)
|
||||
? Array.prototype.slice.call(v, begin, begin + length)
|
||||
|
||||
Reference in New Issue
Block a user