mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 17:30:43 -07:00
Support usecase where a nil variable value is used in a logical expression
This commit is contained in:
@@ -28,7 +28,7 @@ module Liquid
|
||||
FLOAT_REGEX = /\A(-?\d+)\.\d+\z/
|
||||
|
||||
class << self
|
||||
def parse(markup, ss = StringScanner.new(""), cache = nil)
|
||||
def parse(markup, ss = StringScanner.new(""), cache = nil, logical_expression = false)
|
||||
return unless markup
|
||||
|
||||
markup = markup.strip # markup can be a frozen string
|
||||
@@ -44,13 +44,13 @@ module Liquid
|
||||
if cache
|
||||
return cache[markup] if cache.key?(markup)
|
||||
|
||||
cache[markup] = inner_parse(markup, ss, cache).freeze
|
||||
cache[markup] = inner_parse(markup, ss, cache, logical_expression).freeze
|
||||
else
|
||||
inner_parse(markup, ss, nil).freeze
|
||||
inner_parse(markup, ss, nil, logical_expression).freeze
|
||||
end
|
||||
end
|
||||
|
||||
def inner_parse(markup, ss, cache)
|
||||
def inner_parse(markup, ss, cache, logical_expression = false)
|
||||
return LogicalExpression.parse(markup, ss, cache) if LogicalExpression.logical?(markup)
|
||||
return ComparisonExpression.parse(markup, ss, cache) if ComparisonExpression.comparison?(markup)
|
||||
|
||||
@@ -66,7 +66,7 @@ module Liquid
|
||||
if (num = parse_number(markup, ss))
|
||||
num
|
||||
else
|
||||
VariableLookup.parse(markup, ss, cache)
|
||||
VariableLookup.parse(markup, ss, cache, logical_expression)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ module Liquid
|
||||
elsif logical?(last_expr)
|
||||
LogicalExpression.parse(last_expr, ss, cache)
|
||||
else
|
||||
Condition.new(Expression.parse(last_expr, ss, cache), nil, nil)
|
||||
Condition.new(Expression.parse(last_expr, ss, cache, true), nil, nil)
|
||||
end
|
||||
|
||||
until expressions.empty?
|
||||
@@ -40,7 +40,7 @@ module Liquid
|
||||
elsif logical?(expr)
|
||||
LogicalExpression.parse(expr, ss, cache)
|
||||
else
|
||||
Condition.new(Expression.parse(expr, ss, cache), nil, nil)
|
||||
Condition.new(Expression.parse(expr, ss, cache, true), nil, nil)
|
||||
end
|
||||
|
||||
if operator == 'and'
|
||||
|
||||
@@ -5,12 +5,14 @@ module Liquid
|
||||
COMMAND_METHODS = ['size', 'first', 'last'].freeze
|
||||
|
||||
attr_reader :name, :lookups
|
||||
attr_accessor :logical_expression
|
||||
|
||||
def self.parse(markup, string_scanner = StringScanner.new(""), cache = nil)
|
||||
new(markup, string_scanner, cache)
|
||||
def self.parse(markup, string_scanner = StringScanner.new(""), cache = nil, logical_expression = false)
|
||||
new(markup, string_scanner, cache, logical_expression)
|
||||
end
|
||||
|
||||
def initialize(markup, string_scanner = StringScanner.new(""), cache = nil)
|
||||
def initialize(markup, string_scanner = StringScanner.new(""), cache = nil, logical_expression = false)
|
||||
@logical_expression = logical_expression
|
||||
lookups = markup.scan(VariableParser)
|
||||
|
||||
name = lookups.shift
|
||||
@@ -45,9 +47,17 @@ module Liquid
|
||||
end
|
||||
|
||||
def evaluate(context)
|
||||
puts "variable_lookup #evaluate #{@name} #{logical_expression?}"
|
||||
name = context.evaluate(@name)
|
||||
object = context.find_variable(name)
|
||||
|
||||
# When evaluating a logical expression, this variable lookup is part of a chain of conditions
|
||||
# If the variable lookup returns nil, we must use the falsey value of the variable lookup
|
||||
# rather than nil which is reserved for the usecase of rendering nothing.
|
||||
if logical_expression? && object.nil?
|
||||
return false
|
||||
end
|
||||
|
||||
@lookups.each_index do |i|
|
||||
key = context.evaluate(@lookups[i])
|
||||
|
||||
@@ -89,6 +99,10 @@ module Liquid
|
||||
self.class == other.class && state == other.state
|
||||
end
|
||||
|
||||
def logical_expression?
|
||||
@logical_expression
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def state
|
||||
|
||||
Reference in New Issue
Block a user