From 5c082472a10a7795fa188dfd01b0a2763fea006d Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Mon, 26 Oct 2020 16:07:43 -0400 Subject: [PATCH] Address comments --- lib/liquid/block.rb | 18 ++++-------------- lib/liquid/block_body.rb | 2 +- lib/liquid/tags/case.rb | 4 ++++ lib/liquid/tags/for.rb | 6 ++++++ lib/liquid/tags/if.rb | 4 ++++ 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/liquid/block.rb b/lib/liquid/block.rb index 4299915..dc22a47 100644 --- a/lib/liquid/block.rb +++ b/lib/liquid/block.rb @@ -11,7 +11,7 @@ module Liquid def parse(tokens) @body = new_body - while parse_body(@body, tokens, partial: true) + while parse_body(@body, tokens) end @body.freeze end @@ -68,9 +68,7 @@ module Liquid end # @api public - def parse_body(body, tokens, partial: false) - block_terminated = true - + def parse_body(body, tokens) if parse_context.depth >= MAX_DEPTH raise StackLevelError, "Nesting too deep" end @@ -79,10 +77,7 @@ module Liquid body.parse(tokens, parse_context) do |end_tag_name, end_tag_params| @blank &&= body.blank? - if end_tag_name == block_delimiter - block_terminated = false - next - end + return false if end_tag_name == block_delimiter raise_tag_never_closed(block_name) unless end_tag_name # this tag is not registered with the system @@ -93,12 +88,7 @@ module Liquid parse_context.depth -= 1 end - unless partial - body.remove_blank_strings if body.blank? - body.freeze - end - - block_terminated + true end end end diff --git a/lib/liquid/block_body.rb b/lib/liquid/block_body.rb index b8350a3..af62a14 100644 --- a/lib/liquid/block_body.rb +++ b/lib/liquid/block_body.rb @@ -199,7 +199,7 @@ module Liquid end def render_to_output_buffer(context, output) - raise "Can only render when frozen" unless frozen? + freeze unless frozen? context.resource_limits.increment_render_score(@nodelist.length) diff --git a/lib/liquid/tags/case.rb b/lib/liquid/tags/case.rb index 734f166..caa6fff 100644 --- a/lib/liquid/tags/case.rb +++ b/lib/liquid/tags/case.rb @@ -21,6 +21,10 @@ module Liquid def parse(tokens) body = new_body body = @blocks.last.attachment while parse_body(body, tokens) + if blank? + @blocks.each { |condition| condition.attachment.remove_blank_strings } + end + @blocks.each { |condition| condition.attachment.freeze } end def nodelist diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index d7e93bb..df2d4db 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -62,6 +62,12 @@ module Liquid if parse_body(@for_block, tokens) parse_body(@else_block, tokens) end + if blank? + @for_block.remove_blank_strings + @else_block&.remove_blank_strings + end + @for_block.freeze + @else_block&.freeze end def nodelist diff --git a/lib/liquid/tags/if.rb b/lib/liquid/tags/if.rb index c3b8915..7bef1af 100644 --- a/lib/liquid/tags/if.rb +++ b/lib/liquid/tags/if.rb @@ -31,6 +31,10 @@ module Liquid def parse(tokens) while parse_body(@blocks.last.attachment, tokens) end + if blank? + @blocks.each { |condition| condition.attachment.remove_blank_strings } + end + @blocks.each { |block| block.attachment.freeze } end def unknown_tag(tag, markup, tokens)