mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 02:40:41 -07:00
optionally remove empty nodes from AST
This commit is contained in:
@@ -59,7 +59,7 @@ module Liquid
|
||||
end
|
||||
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
|
||||
|
||||
next if new_tag.is_a?(Comment)
|
||||
next if parse_context.omit_blank_nodes && blank_node?(new_tag)
|
||||
|
||||
@blank &&= new_tag.blank?
|
||||
@nodelist << new_tag
|
||||
@@ -157,7 +157,7 @@ module Liquid
|
||||
end
|
||||
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
|
||||
|
||||
next if new_tag.is_a?(Comment)
|
||||
next if parse_context.omit_blank_nodes && blank_node?(new_tag)
|
||||
|
||||
@blank &&= new_tag.blank?
|
||||
@nodelist << new_tag
|
||||
@@ -275,5 +275,18 @@ module Liquid
|
||||
def raise_missing_variable_terminator(token, parse_context)
|
||||
BlockBody.raise_missing_variable_terminator(token, parse_context)
|
||||
end
|
||||
|
||||
def blank_node?(node)
|
||||
case node
|
||||
when Comment
|
||||
true
|
||||
when BlockBody
|
||||
true if node.nodelist.empty?
|
||||
when Tag
|
||||
node.nodelist.all? { |n| blank_node?(n) }
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
module Liquid
|
||||
class ParseContext
|
||||
attr_accessor :locale, :line_number, :trim_whitespace, :depth
|
||||
attr_reader :partial, :warnings, :error_mode, :environment
|
||||
attr_reader :partial, :warnings, :error_mode, :environment, :omit_blank_nodes
|
||||
|
||||
def initialize(options = Const::EMPTY_HASH)
|
||||
@environment = options.fetch(:environment, Environment.default)
|
||||
@@ -12,6 +12,10 @@ module Liquid
|
||||
@locale = @template_options[:locale] ||= I18n.new
|
||||
@warnings = []
|
||||
|
||||
# remove blank nodes such as
|
||||
# comment tags, empty if tags, etc from the AST
|
||||
@omit_blank_nodes = options.fetch(:omit_blank_nodes, false)
|
||||
|
||||
self.depth = 0
|
||||
self.partial = false
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user