From 06e2f2577f7a2b22762f0061bfbb9a067a7b245c Mon Sep 17 00:00:00 2001 From: Nicholas Jones Date: Mon, 13 Jan 2014 11:50:35 -0800 Subject: [PATCH] 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 c1195622..fd726525 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 ce2ad886..2b7179fa 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 5902704c..d343ed5a 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 00000000..f117856f --- /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 9186d3fd..99d9a5f7 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