diff --git a/lib/liquid/block.rb b/lib/liquid/block.rb index a0a07e4..022a5e5 100644 --- a/lib/liquid/block.rb +++ b/lib/liquid/block.rb @@ -1,17 +1,22 @@ module Liquid - class Block < Tag + attr_reader :blank + IsTag = /^#{TagStart}/o IsVariable = /^#{VariableStart}/o FullToken = /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/o ContentOfVariable = /^#{VariableStart}(.*)#{VariableEnd}$/o + def self.blank? + false + end + def parse(tokens) + @blank = true @nodelist ||= [] @nodelist.clear while token = tokens.shift - case token when IsTag if token =~ FullToken @@ -20,12 +25,15 @@ module Liquid # proceed if block_delimiter == $1 end_tag + @blank = true if self.class.blank? return end # fetch the tag from registered blocks 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 # this tag is not registered with the system # pass it to the current block for special handling or error reporting @@ -36,10 +44,12 @@ module Liquid end when IsVariable @nodelist << create_variable(token) + @blank = false when '' # pass else @nodelist << token + @blank = false unless token =~ /\A\s*\z/ end end @@ -112,7 +122,9 @@ module Liquid context.resource_limits[:reached] = true raise MemoryError.new("Memory limits exceeded") end - output << token_output + unless token.respond_to?(:blank) && token.blank + output << token_output + end rescue MemoryError => e raise e rescue ::StandardError => e diff --git a/lib/liquid/tag.rb b/lib/liquid/tag.rb index b7f2aa4..c672344 100644 --- a/lib/liquid/tag.rb +++ b/lib/liquid/tag.rb @@ -1,7 +1,5 @@ module Liquid - class Tag - attr_accessor :nodelist def initialize(tag_name, markup, tokens) @@ -21,6 +19,8 @@ module Liquid '' end + def self.blank? + true + end end # Tag - end # Tag diff --git a/lib/liquid/tags/capture.rb b/lib/liquid/tags/capture.rb index 495a6f7..76c464f 100644 --- a/lib/liquid/tags/capture.rb +++ b/lib/liquid/tags/capture.rb @@ -30,6 +30,10 @@ module Liquid context.resource_limits[:assign_score_current] += (output.respond_to?(:length) ? output.length : 1) '' end + + def self.blank? + true + end end Template.register_tag('capture', Capture) diff --git a/lib/liquid/tags/comment.rb b/lib/liquid/tags/comment.rb index 37fb4c8..99e08c1 100644 --- a/lib/liquid/tags/comment.rb +++ b/lib/liquid/tags/comment.rb @@ -3,6 +3,10 @@ module Liquid def render(context) '' end + + def self.blank? + true + end end Template.register_tag('comment', Comment)