mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Standard filter truncate: truncate_string string coercion
The argument `truncate_string` is now coerced into a string to avoid `NoMethodError`s. This is mostly for added resiliency. It is doubtful that someone would actually intent to use a number as truncate string, but accidentally supplying one is entirely possible.
This commit is contained in:
@@ -65,9 +65,10 @@ module Liquid
|
|||||||
return if input.nil?
|
return if input.nil?
|
||||||
input_str = input.to_s
|
input_str = input.to_s
|
||||||
length = Utils.to_integer(length)
|
length = Utils.to_integer(length)
|
||||||
l = length - truncate_string.length
|
truncate_string_str = truncate_string.to_s
|
||||||
|
l = length - truncate_string_str.length
|
||||||
l = 0 if l < 0
|
l = 0 if l < 0
|
||||||
input_str.length > length ? input_str[0...l] + truncate_string : input_str
|
input_str.length > length ? input_str[0...l] + truncate_string_str : input_str
|
||||||
end
|
end
|
||||||
|
|
||||||
def truncatewords(input, words = 15, truncate_string = "...".freeze)
|
def truncatewords(input, words = 15, truncate_string = "...".freeze)
|
||||||
|
|||||||
@@ -115,6 +115,7 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal '...', @filters.truncate('1234567890', 0)
|
assert_equal '...', @filters.truncate('1234567890', 0)
|
||||||
assert_equal '1234567890', @filters.truncate('1234567890')
|
assert_equal '1234567890', @filters.truncate('1234567890')
|
||||||
assert_equal "测试...", @filters.truncate("测试测试测试测试", 5)
|
assert_equal "测试...", @filters.truncate("测试测试测试测试", 5)
|
||||||
|
assert_equal '12341', @filters.truncate("1234567890", 5, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_split
|
def test_split
|
||||||
|
|||||||
Reference in New Issue
Block a user