mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-26 13:45:13 -07:00
Handle truncatewords word length out of range as if no truncation is needed
This commit is contained in:
@@ -6,7 +6,7 @@ require 'bigdecimal'
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
module StandardFilters
|
module StandardFilters
|
||||||
MAX_INT = (1 << 31) - 1
|
MAX_I32 = (1 << 31) - 1
|
||||||
HTML_ESCAPE = {
|
HTML_ESCAPE = {
|
||||||
'&' => '&',
|
'&' => '&',
|
||||||
'>' => '>',
|
'>' => '>',
|
||||||
@@ -239,9 +239,9 @@ module Liquid
|
|||||||
wordlist = begin
|
wordlist = begin
|
||||||
input.split(" ", words + 1)
|
input.split(" ", words + 1)
|
||||||
rescue RangeError
|
rescue RangeError
|
||||||
raise if words + 1 < MAX_INT
|
# integer too big for String#split, but we can semantically assume no truncation is needed
|
||||||
# e.g. integer #{words} too big to convert to `int'
|
return input if words + 1 > MAX_I32
|
||||||
raise Liquid::ArgumentError, "integer #{words} too big for truncatewords"
|
raise # unexpected error
|
||||||
end
|
end
|
||||||
return input if wordlist.length <= words
|
return input if wordlist.length <= words
|
||||||
|
|
||||||
|
|||||||
@@ -227,10 +227,8 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal('one two three...', @filters.truncatewords("one two\tthree\nfour", 3))
|
assert_equal('one two three...', @filters.truncatewords("one two\tthree\nfour", 3))
|
||||||
assert_equal('one two...', @filters.truncatewords("one two three four", 2))
|
assert_equal('one two...', @filters.truncatewords("one two three four", 2))
|
||||||
assert_equal('one...', @filters.truncatewords("one two three four", 0))
|
assert_equal('one...', @filters.truncatewords("one two three four", 0))
|
||||||
exception = assert_raises(Liquid::ArgumentError) do
|
assert_equal('one two three four', @filters.truncatewords("one two three four", 1 << 31))
|
||||||
@filters.truncatewords("one two three four", 1 << 31)
|
assert_equal('one...', @filters.truncatewords("one two three four", -(1 << 32)))
|
||||||
end
|
|
||||||
assert_equal("Liquid error: integer #{1 << 31} too big for truncatewords", exception.message)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_strip_html
|
def test_strip_html
|
||||||
|
|||||||
Reference in New Issue
Block a user