Merge pull request #250 from wildfireapp/correct-if-nodelists

Correct if-statement nodelist.
This commit is contained in:
Florian Weingarten
2014-01-13 12:47:51 -08:00
8 changed files with 44 additions and 2 deletions
+1
View File
@@ -3,6 +3,7 @@
## 3.0.0 / not yet released / branch "master" ## 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] * 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] * 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] * Fix resource counting bug with respond_to?(:length), see #263 [Florian Weingarten, fw42]
+2 -2
View File
@@ -1,7 +1,7 @@
module Liquid module Liquid
class Tag class Tag
attr_accessor :nodelist, :options attr_accessor :options
attr_reader :warnings attr_reader :nodelist, :warnings
def self.new_with_options(tag_name, markup, tokens, options) def self.new_with_options(tag_name, markup, tokens, options)
# Forgive me Matz for I have sinned. I know this code is weird # Forgive me Matz for I have sinned. I know this code is weird
+4
View File
@@ -15,6 +15,10 @@ module Liquid
super super
end end
def nodelist
@blocks.map(&:attachment).flatten
end
def unknown_tag(tag, markup, tokens) def unknown_tag(tag, markup, tokens)
@nodelist = [] @nodelist = []
case tag case tag
+8
View File
@@ -52,6 +52,14 @@ module Liquid
super super
end end
def nodelist
if @else_block
@for_block + @else_block
else
@for_block
end
end
def unknown_tag(tag, markup, tokens) def unknown_tag(tag, markup, tokens)
return super unless tag == 'else' return super unless tag == 'else'
@nodelist = @else_block = [] @nodelist = @else_block = []
+4
View File
@@ -20,6 +20,10 @@ module Liquid
super super
end end
def nodelist
@blocks.map(&:attachment).flatten
end
def unknown_tag(tag, markup, tokens) def unknown_tag(tag, markup, tokens)
if ['elsif', 'else'].include?(tag) if ['elsif', 'else'].include?(tag)
push_block(tag, markup) push_block(tag, markup)
+10
View File
@@ -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
+10
View File
@@ -295,6 +295,16 @@ HERE
assert_template_result(expected, template, assigns) assert_template_result(expected, template, assigns)
end 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
class LoaderDrop < Liquid::Drop class LoaderDrop < Liquid::Drop
attr_accessor :each_called, :load_slice_called attr_accessor :each_called, :load_slice_called
+5
View File
@@ -158,6 +158,11 @@ class IfElseTagTest < Test::Unit::TestCase
%({% if 'gnomeslab-and-or-liquid' contains 'gnomeslab-and-or-liquid' %}yes{% endif %})) %({% if 'gnomeslab-and-or-liquid' contains 'gnomeslab-and-or-liquid' %}yes{% endif %}))
end 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
def test_operators_are_whitelisted def test_operators_are_whitelisted
assert_raise(SyntaxError) do assert_raise(SyntaxError) do
assert_template_result('', %({% if 1 or throw or or 1 %}yes{% endif %})) assert_template_result('', %({% if 1 or throw or or 1 %}yes{% endif %}))