From 9067e5167a944647c65ee7abf320f8285abec47b Mon Sep 17 00:00:00 2001 From: Alex Coco Date: Thu, 15 Aug 2024 11:17:01 -0400 Subject: [PATCH] Handle interrupts in table row --- lib/liquid/tags/table_row.rb | 6 ++++ test/integration/tags/table_row_test.rb | 48 +++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/liquid/tags/table_row.rb b/lib/liquid/tags/table_row.rb index f7e8cd72d..d59791ce1 100644 --- a/lib/liquid/tags/table_row.rb +++ b/lib/liquid/tags/table_row.rb @@ -65,6 +65,12 @@ def render_to_output_buffer(context, output) super output << '' + # Handle any interrupts if they exist. + if context.interrupt? + interrupt = context.pop_interrupt + break if interrupt.is_a?(BreakInterrupt) + end + if tablerowloop.col_last && !tablerowloop.last output << "\n" end diff --git a/test/integration/tags/table_row_test.rb b/test/integration/tags/table_row_test.rb index 24fd53444..ddc0877df 100644 --- a/test/integration/tags/table_row_test.rb +++ b/test/integration/tags/table_row_test.rb @@ -207,4 +207,52 @@ def test_table_row_renders_correct_error_message_for_invalid_parameters render_errors: true, ) end + + def test_table_row_handles_interrupts + assert_template_result( + "\n 1 \n", + '{% tablerow n in (1...3) cols:2 %} {{n}} {% break %} {{n}} {% endtablerow %}', + ) + + assert_template_result( + "\n 1 2 \n 3 \n", + '{% tablerow n in (1...3) cols:2 %} {{n}} {% continue %} {{n}} {% endtablerow %}', + ) + end + + def test_table_row_does_not_leak_interrupts + template = <<~LIQUID + {% for i in (1..2) -%} + {% for j in (1..2) -%} + {% tablerow k in (1..3) %}{% break %}{% endtablerow -%} + loop j={{ j }} + {% endfor -%} + loop i={{ i }} + {% endfor -%} + after loop + LIQUID + + expected = <<~STR + + + loop j=1 + + + loop j=2 + loop i=1 + + + loop j=1 + + + loop j=2 + loop i=2 + after loop + STR + + assert_template_result( + expected, + template, + ) + end end