From e790b60f602a0ca70a95625750a209f62b27557b Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Thu, 28 May 2015 11:56:52 -0400 Subject: [PATCH 1/3] Fix exception from using an empty string for the table row collection. --- lib/liquid/utils.rb | 2 +- test/integration/tags/table_row_test.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/liquid/utils.rb b/lib/liquid/utils.rb index ed1e9d9..c1c71eb 100644 --- a/lib/liquid/utils.rb +++ b/lib/liquid/utils.rb @@ -17,7 +17,7 @@ module Liquid index = 0 # Maintains Ruby 1.8.7 String#each behaviour on 1.9 - return [collection] if non_blank_string?(collection) + return collection != ''.freeze ? [collection] : [] if collection.is_a?(String) collection.each do |item| if to && to <= index diff --git a/test/integration/tags/table_row_test.rb b/test/integration/tags/table_row_test.rb index 6405a9c..d7bc14c 100644 --- a/test/integration/tags/table_row_test.rb +++ b/test/integration/tags/table_row_test.rb @@ -57,4 +57,8 @@ class TableRowTest < Minitest::Test '{% tablerow n in numbers cols:3 offset:1 limit:6%} {{n}} {% endtablerow %}', 'numbers' => [0, 1, 2, 3, 4, 5, 6, 7]) end + + def test_blank_string_not_iterable + assert_template_result("\n\n", "{% tablerow char in characters cols:3 %}I WILL NOT BE OUTPUT{% endtablerow %}", 'characters' => '') + end end From 873eddbb85a8a2ad2555caf0da408f3b81b69f63 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Thu, 28 May 2015 12:55:04 -0400 Subject: [PATCH 2/3] Split a line and use String#empty? for readability --- lib/liquid/utils.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/liquid/utils.rb b/lib/liquid/utils.rb index c1c71eb..09e0a17 100644 --- a/lib/liquid/utils.rb +++ b/lib/liquid/utils.rb @@ -17,7 +17,9 @@ module Liquid index = 0 # Maintains Ruby 1.8.7 String#each behaviour on 1.9 - return collection != ''.freeze ? [collection] : [] if collection.is_a?(String) + if collection.is_a?(String) + return collection.empty? ? [] : [collection] + end collection.each do |item| if to && to <= index From c1eb694057d17538ee88943e4b9f11527d7b03d0 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Thu, 28 May 2015 16:04:23 -0400 Subject: [PATCH 3/3] Remove the redundant iterable check in the for tag. Just do it in slice_collection for consistency with the tablerow tag. --- lib/liquid/tags/for.rb | 7 ------- lib/liquid/utils.rb | 5 +---- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index cd0b764..76aaa6f 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -76,9 +76,6 @@ module Liquid collection = context.evaluate(@collection_name) collection = collection.to_a if collection.is_a?(Range) - # Maintains Ruby 1.8.7 String#each behaviour on 1.9 - return render_else(context) unless iterable?(collection) - from = if @from == :continue for_offsets[@name].to_i else @@ -189,10 +186,6 @@ module Liquid def render_else(context) @else_block ? @else_block.render(context) : ''.freeze end - - def iterable?(collection) - collection.respond_to?(:each) || Utils.non_blank_string?(collection) - end end Template.register_tag('for'.freeze, For) diff --git a/lib/liquid/utils.rb b/lib/liquid/utils.rb index 09e0a17..ba8a2ca 100644 --- a/lib/liquid/utils.rb +++ b/lib/liquid/utils.rb @@ -8,10 +8,6 @@ module Liquid end end - def self.non_blank_string?(collection) - collection.is_a?(String) && collection != ''.freeze - end - def self.slice_collection_using_each(collection, from, to) segments = [] index = 0 @@ -20,6 +16,7 @@ module Liquid if collection.is_a?(String) return collection.empty? ? [] : [collection] end + return [] unless collection.respond_to?(:each) collection.each do |item| if to && to <= index