From 6ce4ec1011c35dd29ba68bf94dd2686e19e95604 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Tue, 25 Oct 2022 10:21:03 -0400 Subject: [PATCH] tablerow: Avoid accidental special case for constant nil cols (#1644) It should behave the same as an expression that evaluates to nil --- lib/liquid/tags/table_row.rb | 2 +- test/integration/tags/table_row_test.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/liquid/tags/table_row.rb b/lib/liquid/tags/table_row.rb index 5371f70a..87f3e096 100644 --- a/lib/liquid/tags/table_row.rb +++ b/lib/liquid/tags/table_row.rb @@ -51,7 +51,7 @@ module Liquid collection = Utils.slice_collection(collection, from, to) length = collection.length - cols = @attributes['cols'].nil? ? length : context.evaluate(@attributes['cols']).to_i + cols = @attributes.key?('cols') ? context.evaluate(@attributes['cols']).to_i : length output << "\n" context.stack do diff --git a/test/integration/tags/table_row_test.rb b/test/integration/tags/table_row_test.rb index 1f6edda4..8556ed2d 100644 --- a/test/integration/tags/table_row_test.rb +++ b/test/integration/tags/table_row_test.rb @@ -66,6 +66,20 @@ class TableRowTest < Minitest::Test { 'characters' => '' }) end + def test_cols_nil_constant_same_as_evaluated_nil_expression + expect = "\n" \ + "false" \ + "false" \ + "\n" + + assert_template_result(expect, + "{% tablerow i in (1..2) cols:nil %}{{ tablerowloop.col_last }}{% endtablerow %}") + + assert_template_result(expect, + "{% tablerow i in (1..2) cols:var %}{{ tablerowloop.col_last }}{% endtablerow %}", + { "var" => nil }) + end + def test_tablerow_loop_drop_attributes template = <<~LIQUID.chomp {% tablerow i in (1...2) %}