Skip to content

Commit

Permalink
Implement logic for stringify Hashes to keep compat with 3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks committed Jan 15, 2025
1 parent c2530b2 commit ffca018
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 64 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
106 changes: 53 additions & 53 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,6 @@ def try_coerce_encoding(input, encoding:)
end
input
end

def stringify_object(object)
output = String.new
self.stringify_inner(object, output)
output
end

def stringify_inner(obj, output)
case obj
when NilClass
# Do nothing
when Array
obj.each do |o|
self.stringify_object(o, output)
end
when Hash
output << obj.to_s_legacy
when
output << obj.to_s
end
end
end

# @liquid_public_docs
Expand All @@ -85,7 +64,7 @@ def size(input)
# @liquid_syntax string | downcase
# @liquid_return [string]
def downcase(input)
StandardFilters.stringify_object(input).downcase
Utils.to_s(input).downcase
end

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

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

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

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

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

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

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

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

# @liquid_public_docs
Expand All @@ -184,7 +163,7 @@ def base64_encode(input)
# @liquid_syntax string | base64_decode
# @liquid_return [string]
def base64_decode(input)
input = StandardFilters.stringify_object(input)
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 @@ -198,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(StandardFilters.stringify_object(input))
Base64.urlsafe_encode64(Utils.to_s(input))
end

# @liquid_public_docs
Expand All @@ -209,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 = StandardFilters.stringify_object(input)
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 @@ -233,7 +212,7 @@ def slice(input, offset, length = nil)
if input.is_a?(Array)
input.slice(offset, length) || []
else
StandardFilters.stringify_object(input).slice(offset, length) || ''
Utils.to_s(input).slice(offset, length) || ''
end
rescue RangeError
if I64_RANGE.cover?(length) && I64_RANGE.cover?(offset)
Expand All @@ -257,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 = StandardFilters.stringify_object(input)
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 @@ -284,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 = StandardFilters.stringify_object(input)
input = Utils.to_s(input)
words = Utils.to_integer(words)
words = 1 if words <= 0

Expand All @@ -298,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 @@ -309,7 +289,9 @@ def truncatewords(input, words = 15, truncate_string = "...")
# @liquid_syntax string | split: string
# @liquid_return [array[string]]
def split(input, pattern)
StandardFilters.stringify_object(input).split(pattern.to_s)
pattern = Utils.to_s(pattern)
input = Utils.to_s(input)
input.split(pattern)
end

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

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

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

# @liquid_public_docs
Expand All @@ -353,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 = StandardFilters.stringify_object(input).gsub(STRIP_HTML_BLOCKS, empty)
result = input.gsub(STRIP_HTML_BLOCKS, empty)
result.gsub!(STRIP_HTML_TAGS, empty)
result
end
Expand All @@ -367,7 +353,8 @@ def strip_html(input)
# @liquid_syntax string | strip_newlines
# @liquid_return [string]
def strip_newlines(input)
StandardFilters.stringify_object(input).gsub(/\r?\n/, '')
input = Utils.to_s(input)
input.gsub(/\r?\n/, '')
end

# @liquid_public_docs
Expand All @@ -378,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 @@ -594,7 +582,10 @@ def compact(input, property = nil)
# @liquid_syntax string | replace: string, string
# @liquid_return [string]
def replace(input, string, replacement = '')
StandardFilters.stringify_object(input).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 @@ -605,7 +596,10 @@ def replace(input, string, replacement = '')
# @liquid_syntax string | replace_first: string, string
# @liquid_return [string]
def replace_first(input, string, replacement = '')
StandardFilters.stringify_object(input).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 @@ -616,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 = StandardFilters.stringify_object(input)
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 @@ -670,7 +664,9 @@ def remove_last(input, string)
# @liquid_syntax string | append: string
# @liquid_return [string]
def append(input, string)
StandardFilters.stringify_object(input) + string.to_s
input = Utils.to_s(input)
string = Utils.to_s(string)
input + string
end

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

# @liquid_public_docs
Expand All @@ -710,7 +708,8 @@ def prepend(input, string)
# @liquid_syntax string | newline_to_br
# @liquid_return [string]
def newline_to_br(input)
StandardFilters.stringify_object(input).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 @@ -745,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
Loading

0 comments on commit ffca018

Please sign in to comment.