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
6 changes: 5 additions & 1 deletion lib/liquid/tags/for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ def collection_segment(context)
)
segment.reverse! if @reversed

offsets[@name] = from + segment.length
# The slice is clamped to the start of the collection, so a negative
# offset still renders from the first item. Clamp the stored cursor
# the same way, otherwise `offset: continue` resumes from
# `from + segment.length` and replays items (or skips past the end).
offsets[@name] = [from, 0].max + segment.length

segment
end
Expand Down
14 changes: 14 additions & 0 deletions test/integration/tags/for_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ def test_pause_resume_big_offset
assert_template_result(expected, markup, assigns)
end

def test_pause_resume_after_negative_offset
assigns = { 'array' => { 'items' => [1, 2, 3] } }
assert_template_result(
'1|23',
'{%for i in array.items offset: -1 limit: 2 %}{{i}}{%endfor%}|{%for i in array.items offset:continue %}{{i}}{%endfor%}',
assigns
)
assert_template_result(
'123|',
'{%for i in array.items offset: -1 %}{{i}}{%endfor%}|{%for i in array.items offset:continue %}{{i}}{%endfor%}',
assigns
)
end

def test_for_with_break
assigns = { 'array' => { 'items' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } }

Expand Down
Loading