Fix empty? semantics and string first/last for empty strings

- nil is NOT empty (but IS blank) - matches Shopify production
- String first/last returns '' for empty strings, not nil - matches ActiveSupport
- Add test for nil not being empty
This commit is contained in:
Tobi Lutke
2026-01-01 22:06:22 -05:00
parent b0fb0ad83f
commit ef13b2dfd5
5 changed files with 21 additions and 7 deletions
+1 -2
View File
@@ -160,10 +160,9 @@ module Liquid
end
# Implement empty? semantics
# Note: nil is NOT empty (but IS blank). empty? checks if a collection has zero elements.
def liquid_empty?(value)
case value
when NilClass
true
when String, Array, Hash
value.empty?
else
+4 -2
View File
@@ -768,7 +768,8 @@ module Liquid
# @liquid_syntax array | first
# @liquid_return [untyped]
def first(array)
return array[0] if array.is_a?(String)
# ActiveSupport returns "" for empty strings, not nil
return array[0] || "" if array.is_a?(String)
array.first if array.respond_to?(:first)
end
@@ -780,7 +781,8 @@ module Liquid
# @liquid_syntax array | last
# @liquid_return [untyped]
def last(array)
return array[-1] if array.is_a?(String)
# ActiveSupport returns "" for empty strings, not nil
return array[-1] || "" if array.is_a?(String)
array.last if array.respond_to?(:last)
end
+2 -1
View File
@@ -71,8 +71,9 @@ module Liquid
object = object.send(key).to_liquid
# Handle string first/last like ActiveSupport does (returns first/last character)
# ActiveSupport returns "" for empty strings, not nil
elsif lookup_command?(i) && object.is_a?(String) && (key == "first" || key == "last")
object = key == "first" ? object[0] : object[-1]
object = key == "first" ? (object[0] || "") : (object[-1] || "")
# No key was present with the desired value and it wasn't one of the directly supported
# keywords either. The only thing we got left is to return nil or