Skip to content

Commit

Permalink
Implement logic for stringify Hashes to keep compat with 3.4 (#1892)
Browse files Browse the repository at this point in the history
* Exploring

* Bump to v5.6.5

---------

Co-authored-by: Dominic Petrick <dominic.petrick@shopify.com>
  • Loading branch information
ianks and dpetrick authored Jan 15, 2025
1 parent ecf25ea commit 4b65a28
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/liquid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- { ruby: 3.0, allowed-failure: false } # minimum supported
- { ruby: 3.2, allowed-failure: false }
- { ruby: 3.3, allowed-failure: false }
- { ruby: "3.4.0-rc1", allowed-failure: false } # latest
- { ruby: 3.4, allowed-failure: false } # latest
- { ruby: ruby-head, allowed-failure: false }
name: Test Ruby ${{ matrix.entry.ruby }}
steps:
Expand Down
85 changes: 53 additions & 32 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def size(input)
# @liquid_syntax string | downcase
# @liquid_return [string]
def downcase(input)
input.to_s.downcase
Utils.to_s(input).downcase
end

# @liquid_public_docs
Expand All @@ -75,7 +75,7 @@ def downcase(input)
# @liquid_syntax string | upcase
# @liquid_return [string]
def upcase(input)
input.to_s.upcase
Utils.to_s(input).upcase
end

# @liquid_public_docs
Expand All @@ -86,7 +86,7 @@ def upcase(input)
# @liquid_syntax string | capitalize
# @liquid_return [string]
def capitalize(input)
input.to_s.capitalize
Utils.to_s(input).capitalize
end

# @liquid_public_docs
Expand All @@ -97,7 +97,7 @@ def capitalize(input)
# @liquid_syntax string | escape
# @liquid_return [string]
def escape(input)
CGI.escapeHTML(input.to_s) unless input.nil?
CGI.escapeHTML(Utils.to_s(input)) unless input.nil?
end
alias_method :h, :escape

Expand All @@ -109,7 +109,7 @@ def escape(input)
# @liquid_syntax string | escape_once
# @liquid_return [string]
def escape_once(input)
input.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE)
Utils.to_s(input).gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE)
end

# @liquid_public_docs
Expand All @@ -124,7 +124,7 @@ def escape_once(input)
# @liquid_syntax string | url_encode
# @liquid_return [string]
def url_encode(input)
CGI.escape(input.to_s) unless input.nil?
CGI.escape(Utils.to_s(input)) unless input.nil?
end

# @liquid_public_docs
Expand All @@ -138,7 +138,7 @@ def url_encode(input)
def url_decode(input)
return if input.nil?

result = CGI.unescape(input.to_s)
result = CGI.unescape(Utils.to_s(input))
raise Liquid::ArgumentError, "invalid byte sequence in #{result.encoding}" unless result.valid_encoding?

result
Expand All @@ -152,7 +152,7 @@ def url_decode(input)
# @liquid_syntax string | base64_encode
# @liquid_return [string]
def base64_encode(input)
Base64.strict_encode64(input.to_s)
Base64.strict_encode64(Utils.to_s(input))
end

# @liquid_public_docs
Expand All @@ -163,7 +163,7 @@ def base64_encode(input)
# @liquid_syntax string | base64_decode
# @liquid_return [string]
def base64_decode(input)
input = input.to_s
input = Utils.to_s(input)
StandardFilters.try_coerce_encoding(Base64.strict_decode64(input), encoding: input.encoding)
rescue ::ArgumentError
raise Liquid::ArgumentError, "invalid base64 provided to base64_decode"
Expand All @@ -177,7 +177,7 @@ def base64_decode(input)
# @liquid_syntax string | base64_url_safe_encode
# @liquid_return [string]
def base64_url_safe_encode(input)
Base64.urlsafe_encode64(input.to_s)
Base64.urlsafe_encode64(Utils.to_s(input))
end

# @liquid_public_docs
Expand All @@ -188,7 +188,7 @@ def base64_url_safe_encode(input)
# @liquid_syntax string | base64_url_safe_decode
# @liquid_return [string]
def base64_url_safe_decode(input)
input = input.to_s
input = Utils.to_s(input)
StandardFilters.try_coerce_encoding(Base64.urlsafe_decode64(input), encoding: input.encoding)
rescue ::ArgumentError
raise Liquid::ArgumentError, "invalid base64 provided to base64_url_safe_decode"
Expand All @@ -212,7 +212,7 @@ def slice(input, offset, length = nil)
if input.is_a?(Array)
input.slice(offset, length) || []
else
input.to_s.slice(offset, length) || ''
Utils.to_s(input).slice(offset, length) || ''
end
rescue RangeError
if I64_RANGE.cover?(length) && I64_RANGE.cover?(offset)
Expand All @@ -236,10 +236,10 @@ def slice(input, offset, length = nil)
# @liquid_return [string]
def truncate(input, length = 50, truncate_string = "...")
return if input.nil?
input_str = input.to_s
input_str = Utils.to_s(input)
length = Utils.to_integer(length)

truncate_string_str = truncate_string.to_s
truncate_string_str = Utils.to_s(truncate_string)

l = length - truncate_string_str.length
l = 0 if l < 0
Expand All @@ -263,7 +263,7 @@ def truncate(input, length = 50, truncate_string = "...")
# @liquid_return [string]
def truncatewords(input, words = 15, truncate_string = "...")
return if input.nil?
input = input.to_s
input = Utils.to_s(input)
words = Utils.to_integer(words)
words = 1 if words <= 0

Expand All @@ -277,7 +277,8 @@ def truncatewords(input, words = 15, truncate_string = "...")
return input if wordlist.length <= words

wordlist.pop
wordlist.join(" ").concat(truncate_string.to_s)
truncate_string = Utils.to_s(truncate_string)
wordlist.join(" ").concat(truncate_string)
end

# @liquid_public_docs
Expand All @@ -288,7 +289,9 @@ def truncatewords(input, words = 15, truncate_string = "...")
# @liquid_syntax string | split: string
# @liquid_return [array[string]]
def split(input, pattern)
input.to_s.split(pattern.to_s)
pattern = Utils.to_s(pattern)
input = Utils.to_s(input)
input.split(pattern)
end

# @liquid_public_docs
Expand All @@ -299,7 +302,8 @@ def split(input, pattern)
# @liquid_syntax string | strip
# @liquid_return [string]
def strip(input)
input.to_s.strip
input = Utils.to_s(input)
input.strip
end

# @liquid_public_docs
Expand All @@ -310,7 +314,8 @@ def strip(input)
# @liquid_syntax string | lstrip
# @liquid_return [string]
def lstrip(input)
input.to_s.lstrip
input = Utils.to_s(input)
input.lstrip
end

# @liquid_public_docs
Expand All @@ -321,7 +326,8 @@ def lstrip(input)
# @liquid_syntax string | rstrip
# @liquid_return [string]
def rstrip(input)
input.to_s.rstrip
input = Utils.to_s(input)
input.rstrip
end

# @liquid_public_docs
Expand All @@ -332,8 +338,9 @@ def rstrip(input)
# @liquid_syntax string | strip_html
# @liquid_return [string]
def strip_html(input)
input = Utils.to_s(input)
empty = ''
result = input.to_s.gsub(STRIP_HTML_BLOCKS, empty)
result = input.gsub(STRIP_HTML_BLOCKS, empty)
result.gsub!(STRIP_HTML_TAGS, empty)
result
end
Expand All @@ -346,7 +353,8 @@ def strip_html(input)
# @liquid_syntax string | strip_newlines
# @liquid_return [string]
def strip_newlines(input)
input.to_s.gsub(/\r?\n/, '')
input = Utils.to_s(input)
input.gsub(/\r?\n/, '')
end

# @liquid_public_docs
Expand All @@ -357,6 +365,7 @@ def strip_newlines(input)
# @liquid_syntax array | join
# @liquid_return [string]
def join(input, glue = ' ')
glue = Utils.to_s(glue)
InputIterator.new(input, context).join(glue)
end

Expand Down Expand Up @@ -573,7 +582,10 @@ def compact(input, property = nil)
# @liquid_syntax string | replace: string, string
# @liquid_return [string]
def replace(input, string, replacement = '')
input.to_s.gsub(string.to_s, replacement.to_s)
string = Utils.to_s(string)
replacement = Utils.to_s(replacement)
input = Utils.to_s(input)
input.gsub(string, replacement)
end

# @liquid_public_docs
Expand All @@ -584,7 +596,10 @@ def replace(input, string, replacement = '')
# @liquid_syntax string | replace_first: string, string
# @liquid_return [string]
def replace_first(input, string, replacement = '')
input.to_s.sub(string.to_s, replacement.to_s)
string = Utils.to_s(string)
replacement = Utils.to_s(replacement)
input = Utils.to_s(input)
input.sub(string, replacement)
end

# @liquid_public_docs
Expand All @@ -595,9 +610,9 @@ def replace_first(input, string, replacement = '')
# @liquid_syntax string | replace_last: string, string
# @liquid_return [string]
def replace_last(input, string, replacement)
input = input.to_s
string = string.to_s
replacement = replacement.to_s
input = Utils.to_s(input)
string = Utils.to_s(string)
replacement = Utils.to_s(replacement)

start_index = input.rindex(string)

Expand Down Expand Up @@ -649,7 +664,9 @@ def remove_last(input, string)
# @liquid_syntax string | append: string
# @liquid_return [string]
def append(input, string)
input.to_s + string.to_s
input = Utils.to_s(input)
string = Utils.to_s(string)
input + string
end

# @liquid_public_docs
Expand Down Expand Up @@ -678,7 +695,9 @@ def concat(input, array)
# @liquid_syntax string | prepend: string
# @liquid_return [string]
def prepend(input, string)
string.to_s + input.to_s
input = Utils.to_s(input)
string = Utils.to_s(string)
string + input
end

# @liquid_public_docs
Expand All @@ -689,7 +708,8 @@ def prepend(input, string)
# @liquid_syntax string | newline_to_br
# @liquid_return [string]
def newline_to_br(input)
input.to_s.gsub(/\r?\n/, "<br />\n")
input = Utils.to_s(input)
input.gsub(/\r?\n/, "<br />\n")
end

# Reformat a date using Ruby's core Time#strftime( string ) -> string
Expand Down Expand Up @@ -724,11 +744,12 @@ def newline_to_br(input)
#
# See also: http://www.ruby-doc.org/core/Time.html#method-i-strftime
def date(input, format)
return input if format.to_s.empty?
str_format = Utils.to_s(format)
return input if str_format.empty?

return input unless (date = Utils.to_date(input))

date.strftime(format.to_s)
date.strftime(str_format)
end

# @liquid_public_docs
Expand Down
86 changes: 86 additions & 0 deletions lib/liquid/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,91 @@ def self.to_liquid_value(obj)
# Otherwise return the object itself
obj
end

if RUBY_VERSION >= '3.4'
def self.to_s(obj, seen = {})
case obj
when Hash
hash_inspect(obj, seen)
when Array
array_inspect(obj, seen)
else
obj.to_s
end
end

def self.inspect(obj, seen = {})
case obj
when Hash
hash_inspect(obj, seen)
when Array
array_inspect(obj, seen)
else
obj.inspect
end
end
else
def self.to_s(obj, seen = nil)
obj.to_s
end

def self.inspect(obj, seen = nil)
obj.inspect
end
end

def self.array_inspect(arr, seen = {})
if seen[arr.object_id]
return "[...]"
end

seen[arr.object_id] = true
str = +"["
cursor = 0
len = arr.length

while cursor < len
if cursor > 0
str << ", "
end

item_str = inspect(arr[cursor], seen)
str << item_str
cursor += 1
end

str << "]"
str
ensure
seen.delete(arr.object_id)
end

def self.hash_inspect(hash, seen = {})
if seen[hash.object_id]
return "{...}"
end
seen[hash.object_id] = true

str = +"{"
first = true
hash.each do |key, value|
if first
first = false
else
str << ", "
end

key_str = inspect(key, seen)
str << key_str
str << "=>"

value_str = inspect(value, seen)
str << value_str
end
str << "}"
str
ensure
seen.delete(hash.object_id)
end
end
end
Loading

0 comments on commit 4b65a28

Please sign in to comment.