Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions lib/liquid/expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Expression
RANGES_REGEX = /\A\(\s*(?>(\S+)\s*\.\.)\s*(\S+)\s*\)\z/
INTEGER_REGEX = /\A(-?\d+)\z/
FLOAT_REGEX = /\A(-?\d+)\.\d+\z/
RANGE_MARKUP_WITH_NESTED_LOOKUPS = /["'\[]/

class << self
def safe_parse(parser, ss = StringScanner.new(""), cache = nil)
Expand Down Expand Up @@ -57,13 +58,14 @@ def parse(markup, ss = StringScanner.new(""), cache = nil)
end

def inner_parse(markup, ss, cache)
if markup.start_with?("(") && markup.end_with?(")") && markup =~ RANGES_REGEX
return RangeLookup.parse(
Regexp.last_match(1),
Regexp.last_match(2),
ss,
cache,
)
if markup.start_with?("(") && markup.end_with?(")") && (match = RANGES_REGEX.match(markup))
start_markup = match[1]
if start_markup.match?(RANGE_MARKUP_WITH_NESTED_LOOKUPS) && !range_markup_start_balanced?(start_markup)
range_markups = parse_range_markup(markup)
return RangeLookup.parse(range_markups[0], range_markups[1], ss, cache) if range_markups
else
return RangeLookup.parse(start_markup, match[2], ss, cache)
end
end

if (num = parse_number(markup, ss))
Expand Down Expand Up @@ -125,6 +127,56 @@ def parse_number(markup, ss)
markup.byteslice(0, first_dot_pos).to_f
end
end

# A separator inside a quoted lookup leaves the regex's left capture unbalanced.
def range_markup_start_balanced?(markup)
markup.count("[") == markup.count("]") && markup.count('"').even? && markup.count("'").even?
end
private :range_markup_start_balanced?

def parse_range_markup(markup)
quote = nil
bracket_depth = 0
delimiter_index = nil
index = 1
end_index = markup.bytesize - 1

# Keep the regex's rightmost-separator behaviour for triple-dot ranges.
while index < end_index
byte = markup.getbyte(index)

if quote
if byte == "\\".ord
index += 2
next
elsif byte == quote
quote = nil
end
else
case byte
when 34, 39
quote = byte
when "[".ord
bracket_depth += 1
when "]".ord
bracket_depth -= 1 if bracket_depth.positive?
when DOT
delimiter_index = index if bracket_depth.zero? && markup.getbyte(index + 1) == DOT
end
end

index += 1
end

return unless delimiter_index

start_markup = markup.byteslice(1...delimiter_index).strip
end_markup = markup.byteslice((delimiter_index + 2)...-1).strip
return if start_markup.empty? || end_markup.empty?

[start_markup, end_markup]
end
private :parse_range_markup
end
end
end
30 changes: 30 additions & 0 deletions test/integration/tags/for_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ def test_for_with_variable_range
assert_template_result(' 1 2 3 ', '{%for item in (1..foobar) %} {{item}} {%endfor%}', { "foobar" => 3 })
end

def test_for_with_range_endpoints_containing_double_dots
assigns = { 'paths' => { '..' => 1, '...' => 5 } }

assert_template_result('12345', '{% for item in (1..paths["..."]) %}{{ item }}{% endfor %}', assigns)
assert_template_result('12345', '{% for item in (paths[".."]..paths["..."]) %}{{ item }}{% endfor %}', assigns)
end

def test_for_with_multibyte_dotted_range_keys
["é", "界", "🍎"].each do |prefix|
assigns = { 'paths' => { "#{prefix}.." => 1, "#{prefix}..." => 5 } }
ranges = [
'(paths["' + prefix + '.."]..paths["' + prefix + '..."])',
'(paths["' + prefix + '.."]..5)',
'(1..paths["' + prefix + '..."])',
]

ranges.each do |range|
assert_template_result('12345', '{% for item in ' + range + ' %}{{ item }}{% endfor %}', assigns)
end
end
end

def test_for_with_dotted_range_keys_in_non_utf8_encoding
lower = "é..".encode(Encoding::ISO_8859_1)
upper = "é...".encode(Encoding::ISO_8859_1)
markup = '{% for item in (paths["' + lower + '"]..paths["' + upper + '"]) %}{{ item }}{% endfor %}'

assert_template_result('12345', markup, { 'paths' => { lower => 1, upper => 5 } })
end

def test_for_with_hash_value_range
foobar = { "value" => 3 }
assert_template_result(' 1 2 3 ', '{%for item in (1..foobar.value) %} {{item}} {%endfor%}', { "foobar" => foobar })
Expand Down
Loading