diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index 598d7bee5..879d752aa 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -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 diff --git a/test/integration/tags/for_tag_test.rb b/test/integration/tags/for_tag_test.rb index 36510eeab..0b54e1fda 100644 --- a/test/integration/tags/for_tag_test.rb +++ b/test/integration/tags/for_tag_test.rb @@ -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] } }