From 302185a7fca8e315017ec501684aff8c4007cc30 Mon Sep 17 00:00:00 2001 From: Konstantin Tennhard Date: Fri, 9 Sep 2016 16:50:50 -0400 Subject: [PATCH 1/2] Standard filter truncatewords: force truncate_string to string Currently, `truncatewords` raises a TypeError when the argument `truncate_string` is an interger. This PR forces string coercion for any value provided for this argument. Thus, ```ruby assert_equal 'one two1', @filters.truncatewords("one two three", 2, 1) ``` holds true. Another option would be to raise a `Liquid::ArgumentError`. What is preferred? --- lib/liquid/standardfilters.rb | 2 +- test/integration/standard_filter_test.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 90dfdd3..50f2dc4 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -76,7 +76,7 @@ module Liquid words = Utils.to_integer(words) l = words - 1 l = 0 if l < 0 - wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string : input + wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string.to_s : input end # Split input string into an array of substrings separated by given pattern. diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index a20e1c3..909ad44 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -154,6 +154,7 @@ class StandardFiltersTest < Minitest::Test assert_equal 'one two three', @filters.truncatewords('one two three') assert_equal 'Two small (13” x 5.5” x 10” high) baskets fit inside one large basket (13”...', @filters.truncatewords('Two small (13” x 5.5” x 10” high) baskets fit inside one large basket (13” x 16” x 10.5” high) with cover.', 15) assert_equal "测试测试测试测试", @filters.truncatewords('测试测试测试测试', 5) + assert_equal 'one two1', @filters.truncatewords("one two three", 2, 1) end def test_strip_html From 95d5c24bfc154046c5fb4e4e41d170a42350e2d9 Mon Sep 17 00:00:00 2001 From: Konstantin Tennhard Date: Mon, 12 Sep 2016 12:13:12 -0400 Subject: [PATCH 2/2] 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. --- lib/liquid/standardfilters.rb | 5 +++-- test/integration/standard_filter_test.rb | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 50f2dc4..c87cd7c 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -65,9 +65,10 @@ module Liquid return if input.nil? input_str = input.to_s 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 - 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 def truncatewords(input, words = 15, truncate_string = "...".freeze) diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 909ad44..c852ac6 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -115,6 +115,7 @@ class StandardFiltersTest < Minitest::Test assert_equal '...', @filters.truncate('1234567890', 0) assert_equal '1234567890', @filters.truncate('1234567890') assert_equal "测试...", @filters.truncate("测试测试测试测试", 5) + assert_equal '12341', @filters.truncate("1234567890", 5, 1) end def test_split