Don't render blank blocks

This commit is contained in:
Florian Weingarten
2013-06-26 22:21:47 -04:00
parent 7a3746ad77
commit d6e13faa43
4 changed files with 27 additions and 7 deletions
+16 -4
View File
@@ -1,17 +1,22 @@
module Liquid module Liquid
class Block < Tag class Block < Tag
attr_reader :blank
IsTag = /^#{TagStart}/o IsTag = /^#{TagStart}/o
IsVariable = /^#{VariableStart}/o IsVariable = /^#{VariableStart}/o
FullToken = /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/o FullToken = /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/o
ContentOfVariable = /^#{VariableStart}(.*)#{VariableEnd}$/o ContentOfVariable = /^#{VariableStart}(.*)#{VariableEnd}$/o
def self.blank?
false
end
def parse(tokens) def parse(tokens)
@blank = true
@nodelist ||= [] @nodelist ||= []
@nodelist.clear @nodelist.clear
while token = tokens.shift while token = tokens.shift
case token case token
when IsTag when IsTag
if token =~ FullToken if token =~ FullToken
@@ -20,12 +25,15 @@ module Liquid
# proceed # proceed
if block_delimiter == $1 if block_delimiter == $1
end_tag end_tag
@blank = true if self.class.blank?
return return
end end
# fetch the tag from registered blocks # fetch the tag from registered blocks
if tag = Template.tags[$1] if tag = Template.tags[$1]
@nodelist << tag.new($1, $2, tokens) new_tag = tag.new($1, $2, tokens)
@blank = false if new_tag.is_a?(Block) && !new_tag.blank
@nodelist << new_tag
else else
# this tag is not registered with the system # this tag is not registered with the system
# pass it to the current block for special handling or error reporting # pass it to the current block for special handling or error reporting
@@ -36,10 +44,12 @@ module Liquid
end end
when IsVariable when IsVariable
@nodelist << create_variable(token) @nodelist << create_variable(token)
@blank = false
when '' when ''
# pass # pass
else else
@nodelist << token @nodelist << token
@blank = false unless token =~ /\A\s*\z/
end end
end end
@@ -112,7 +122,9 @@ module Liquid
context.resource_limits[:reached] = true context.resource_limits[:reached] = true
raise MemoryError.new("Memory limits exceeded") raise MemoryError.new("Memory limits exceeded")
end end
output << token_output unless token.respond_to?(:blank) && token.blank
output << token_output
end
rescue MemoryError => e rescue MemoryError => e
raise e raise e
rescue ::StandardError => e rescue ::StandardError => e
+3 -3
View File
@@ -1,7 +1,5 @@
module Liquid module Liquid
class Tag class Tag
attr_accessor :nodelist attr_accessor :nodelist
def initialize(tag_name, markup, tokens) def initialize(tag_name, markup, tokens)
@@ -21,6 +19,8 @@ module Liquid
'' ''
end end
def self.blank?
true
end
end # Tag end # Tag
end # Tag end # Tag
+4
View File
@@ -30,6 +30,10 @@ module Liquid
context.resource_limits[:assign_score_current] += (output.respond_to?(:length) ? output.length : 1) context.resource_limits[:assign_score_current] += (output.respond_to?(:length) ? output.length : 1)
'' ''
end end
def self.blank?
true
end
end end
Template.register_tag('capture', Capture) Template.register_tag('capture', Capture)
+4
View File
@@ -3,6 +3,10 @@ module Liquid
def render(context) def render(context)
'' ''
end end
def self.blank?
true
end
end end
Template.register_tag('comment', Comment) Template.register_tag('comment', Comment)