From ca5bc5d75b6328225686c2cdd549a12255954073 Mon Sep 17 00:00:00 2001 From: Nick Jones Date: Wed, 12 Jun 2013 09:13:41 -0700 Subject: [PATCH 1/4] Correct if-statement nodelist. The nodelist returned by all tags is a list of containing nodes, except for the if tag. This correct that inconsistency --- lib/liquid/tags/if.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/liquid/tags/if.rb b/lib/liquid/tags/if.rb index c376f6c..1202d58 100644 --- a/lib/liquid/tags/if.rb +++ b/lib/liquid/tags/if.rb @@ -19,6 +19,10 @@ module Liquid super end + def nodelist + @blocks.map(&:attachment).flatten + end + def unknown_tag(tag, markup, tokens) if ['elsif', 'else'].include?(tag) push_block(tag, markup) From 90593d3f1856ba3f1f62058aba6bc152d979a66e Mon Sep 17 00:00:00 2001 From: Nicholas Jones Date: Sat, 7 Sep 2013 11:15:49 -0700 Subject: [PATCH 2/4] Add a test for corrected if-nodelist --- test/liquid/tags/if_else_tag_test.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/liquid/tags/if_else_tag_test.rb b/test/liquid/tags/if_else_tag_test.rb index 1282c2d..6fc3792 100644 --- a/test/liquid/tags/if_else_tag_test.rb +++ b/test/liquid/tags/if_else_tag_test.rb @@ -157,4 +157,9 @@ class IfElseTagTest < Test::Unit::TestCase assert_template_result('yes', %({% if 'gnomeslab-and-or-liquid' contains 'gnomeslab-and-or-liquid' %}yes{% endif %})) end + + def test_if_nodelist + template = Liquid::Template.parse('{% if true %}IF{% else %}ELSE{% endif %}') + assert_equal ['IF', 'ELSE'], template.root.nodelist[0].nodelist + end end # IfElseTest From 06e2f2577f7a2b22762f0061bfbb9a067a7b245c Mon Sep 17 00:00:00 2001 From: Nicholas Jones Date: Mon, 13 Jan 2014 11:50:35 -0800 Subject: [PATCH 3/4] Add `else` blocks to `for` and `case` nodelists --- lib/liquid/tag.rb | 4 ++-- lib/liquid/tags/case.rb | 4 ++++ lib/liquid/tags/for.rb | 8 ++++++++ test/liquid/tags/case_tag_test.rb | 10 ++++++++++ test/liquid/tags/for_tag_test.rb | 10 ++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test/liquid/tags/case_tag_test.rb diff --git a/lib/liquid/tag.rb b/lib/liquid/tag.rb index c119562..fd72652 100644 --- a/lib/liquid/tag.rb +++ b/lib/liquid/tag.rb @@ -1,7 +1,7 @@ module Liquid class Tag - attr_accessor :nodelist, :options - attr_reader :warnings + attr_accessor :options + attr_reader :nodelist, :warnings def self.new_with_options(tag_name, markup, tokens, options) # Forgive me Matz for I have sinned. I know this code is weird diff --git a/lib/liquid/tags/case.rb b/lib/liquid/tags/case.rb index ce2ad88..2b7179f 100644 --- a/lib/liquid/tags/case.rb +++ b/lib/liquid/tags/case.rb @@ -15,6 +15,10 @@ module Liquid super end + def nodelist + @blocks.map(&:attachment).flatten + end + def unknown_tag(tag, markup, tokens) @nodelist = [] case tag diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index 5902704..d343ed5 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -52,6 +52,14 @@ module Liquid super end + def nodelist + if @else_block + @for_block + @else_block + else + @for_block + end + end + def unknown_tag(tag, markup, tokens) return super unless tag == 'else' @nodelist = @else_block = [] diff --git a/test/liquid/tags/case_tag_test.rb b/test/liquid/tags/case_tag_test.rb new file mode 100644 index 0000000..f117856 --- /dev/null +++ b/test/liquid/tags/case_tag_test.rb @@ -0,0 +1,10 @@ +require 'test_helper' + +class CaseTagTest < Test::Unit::TestCase + include Liquid + + def test_case_nodelist + template = Liquid::Template.parse('{% case var %}{% when true %}WHEN{% else %}ELSE{% endcase %}') + assert_equal ['WHEN', 'ELSE'], template.root.nodelist[0].nodelist + end +end # CaseTest diff --git a/test/liquid/tags/for_tag_test.rb b/test/liquid/tags/for_tag_test.rb index 9186d3f..99d9a5f 100644 --- a/test/liquid/tags/for_tag_test.rb +++ b/test/liquid/tags/for_tag_test.rb @@ -294,4 +294,14 @@ HERE assigns = {'items' => [1,2,3,4,5]} assert_template_result(expected, template, assigns) end + + def test_for_nodelist + template = Liquid::Template.parse('{% for item in items %}FOR{% endfor %}') + assert_equal ['FOR'], template.root.nodelist[0].nodelist + end + + def test_for_else_nodelist + template = Liquid::Template.parse('{% for item in items %}FOR{% else %}ELSE{% endfor %}') + assert_equal ['FOR', 'ELSE'], template.root.nodelist[0].nodelist + end end From 5570f697fda7612c361e1ed302571d64826d1dfa Mon Sep 17 00:00:00 2001 From: Nicholas Jones Date: Mon, 13 Jan 2014 12:46:43 -0800 Subject: [PATCH 4/4] Update history --- History.md | 1 + 1 file changed, 1 insertion(+) diff --git a/History.md b/History.md index 4af0dd9..6cd27fb 100644 --- a/History.md +++ b/History.md @@ -3,6 +3,7 @@ ## 3.0.0 / not yet released / branch "master" * ... +* Make if, for & case tags return complete and consistent nodelists, see #250 [Nick Jones, dntj] * Prevent arbitrary method invocation on condition objects, see #274 [Dylan Thacker-Smith, dylanahsmith] * Don't call to_sym when creating conditions for security reasons, see #273 [Bouke van der Bijl, bouk] * Fix resource counting bug with respond_to?(:length), see #263 [Florian Weingarten, fw42]