mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 19:00:39 -07:00
Make sure the limit and offset values are integers
This commit is contained in:
+12
-3
@@ -124,14 +124,23 @@ module Liquid
|
|||||||
from = if @from == :continue
|
from = if @from == :continue
|
||||||
offsets[@name].to_i
|
offsets[@name].to_i
|
||||||
else
|
else
|
||||||
context.evaluate(@from).to_i
|
from_value = context.evaluate(@from)
|
||||||
|
if from_value.nil?
|
||||||
|
0
|
||||||
|
else
|
||||||
|
Utils.to_integer(from_value)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
collection = context.evaluate(@collection_name)
|
collection = context.evaluate(@collection_name)
|
||||||
collection = collection.to_a if collection.is_a?(Range)
|
collection = collection.to_a if collection.is_a?(Range)
|
||||||
|
|
||||||
limit = context.evaluate(@limit)
|
limit_value = context.evaluate(@limit)
|
||||||
to = limit ? limit.to_i + from : nil
|
to = if limit_value.nil?
|
||||||
|
nil
|
||||||
|
else
|
||||||
|
Utils.to_integer(limit_value) + from
|
||||||
|
end
|
||||||
|
|
||||||
segment = Utils.slice_collection(collection, from, to)
|
segment = Utils.slice_collection(collection, from, to)
|
||||||
segment.reverse! if @reversed
|
segment.reverse! if @reversed
|
||||||
|
|||||||
@@ -103,6 +103,34 @@ HERE
|
|||||||
assert_template_result('3456', '{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}', assigns)
|
assert_template_result('3456', '{%for i in array limit: 4 offset: 2 %}{{ i }}{%endfor%}', assigns)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_limiting_with_invalid_limit
|
||||||
|
assigns = { 'array' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] }
|
||||||
|
template = <<-MKUP
|
||||||
|
{% for i in array limit: true offset: 1 %}
|
||||||
|
{{ i }}
|
||||||
|
{% endfor %}
|
||||||
|
MKUP
|
||||||
|
|
||||||
|
exception = assert_raises(Liquid::ArgumentError) do
|
||||||
|
Template.parse(template).render!(assigns)
|
||||||
|
end
|
||||||
|
assert_equal("Liquid error: invalid integer", exception.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_limiting_with_invalid_offset
|
||||||
|
assigns = { 'array' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] }
|
||||||
|
template = <<-MKUP
|
||||||
|
{% for i in array limit: 1 offset: true %}
|
||||||
|
{{ i }}
|
||||||
|
{% endfor %}
|
||||||
|
MKUP
|
||||||
|
|
||||||
|
exception = assert_raises(Liquid::ArgumentError) do
|
||||||
|
Template.parse(template).render!(assigns)
|
||||||
|
end
|
||||||
|
assert_equal("Liquid error: invalid integer", exception.message)
|
||||||
|
end
|
||||||
|
|
||||||
def test_dynamic_variable_limiting
|
def test_dynamic_variable_limiting
|
||||||
assigns = { 'array' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] }
|
assigns = { 'array' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] }
|
||||||
assigns['limit'] = 2
|
assigns['limit'] = 2
|
||||||
|
|||||||
Reference in New Issue
Block a user