Skip to content

Commit

Permalink
tablerow: Avoid accidental special case for constant nil cols (#1644)
Browse files Browse the repository at this point in the history
It should behave the same as an expression that evaluates to nil
  • Loading branch information
dylanahsmith committed Oct 25, 2022
1 parent c99c932 commit 6ce4ec1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/liquid/tags/table_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def render_to_output_buffer(context, output)
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 << "<tr class=\"row1\">\n"
context.stack do
Expand Down
14 changes: 14 additions & 0 deletions test/integration/tags/table_row_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ def test_blank_string_not_iterable
{ 'characters' => '' })
end

def test_cols_nil_constant_same_as_evaluated_nil_expression
expect = "<tr class=\"row1\">\n" \
"<td class=\"col1\">false</td>" \
"<td class=\"col2\">false</td>" \
"</tr>\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) %}
Expand Down

0 comments on commit 6ce4ec1

Please sign in to comment.