From f0da48495c12a02f6dd9f3374aaa040672aef42a Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Thu, 3 Sep 2020 11:08:17 -0400 Subject: [PATCH] Default Block#blank? to false regardless of whether the body is blank --- lib/liquid/block.rb | 11 ----------- lib/liquid/tags/case.rb | 7 ++++++- lib/liquid/tags/for.rb | 7 ++++++- lib/liquid/tags/if.rb | 7 ++++++- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/liquid/block.rb b/lib/liquid/block.rb index 9d7b752..7155490 100644 --- a/lib/liquid/block.rb +++ b/lib/liquid/block.rb @@ -4,11 +4,6 @@ module Liquid class Block < Tag MAX_DEPTH = 100 - def initialize(tag_name, markup, options) - super - @blank = true - end - def parse(tokens) @body = BlockBody.new while parse_body(@body, tokens) @@ -20,10 +15,6 @@ module Liquid @body.render(context) end - def blank? - @blank - end - def nodelist @body.nodelist end @@ -64,8 +55,6 @@ module Liquid parse_context.depth += 1 begin body.parse(tokens, parse_context) do |end_tag_name, end_tag_params| - @blank &&= body.blank? - return false if end_tag_name == block_delimiter unless end_tag_name raise SyntaxError, parse_context.locale.t("errors.syntax.tag_never_closed", block_name: block_name) diff --git a/lib/liquid/tags/case.rb b/lib/liquid/tags/case.rb index f6e0e70..f65fe9b 100644 --- a/lib/liquid/tags/case.rb +++ b/lib/liquid/tags/case.rb @@ -21,11 +21,16 @@ module Liquid def parse(tokens) body = BlockBody.new body = @blocks.last.attachment while parse_body(body, tokens) - if blank? + @blank = @blocks.all? { |condition| condition.attachment.blank? } + if @blank @blocks.each { |condition| condition.attachment.remove_blank_strings } end end + def blank? + @blank + end + def nodelist @blocks.map(&:attachment) end diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index bb03392..a323034 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -62,12 +62,17 @@ module Liquid if parse_body(@for_block, tokens) parse_body(@else_block, tokens) end - if blank? + @blank = @for_block.blank? && (@else_block.nil? || @else_block.blank?) + if @blank @for_block.remove_blank_strings @else_block&.remove_blank_strings end end + def blank? + @blank + end + def nodelist @else_block ? [@for_block, @else_block] : [@for_block] end diff --git a/lib/liquid/tags/if.rb b/lib/liquid/tags/if.rb index 43c8b0a..bc03e18 100644 --- a/lib/liquid/tags/if.rb +++ b/lib/liquid/tags/if.rb @@ -31,11 +31,16 @@ module Liquid def parse(tokens) while parse_body(@blocks.last.attachment, tokens) end - if blank? + @blank = @blocks.all? { |condition| condition.attachment.blank? } + if @blank @blocks.each { |condition| condition.attachment.remove_blank_strings } end end + def blank? + @blank + end + def unknown_tag(tag, markup, tokens) if ['elsif', 'else'].include?(tag) push_block(tag, markup)