Fix warnings and make tags a proper syntax tree.

This commit is contained in:
Tristan Hume
2013-08-22 12:44:23 -04:00
parent 93fcd5687c
commit b0cba5298a
3 changed files with 13 additions and 7 deletions

View File

@@ -14,6 +14,9 @@ module Liquid
@nodelist ||= []
@nodelist.clear
# All child tags of the current block.
@children = []
while token = tokens.shift
case token
when IsTag
@@ -31,6 +34,7 @@ module Liquid
new_tag = tag.new_with_options($1, $2, tokens, @options || {})
@blank &&= new_tag.blank?
@nodelist << new_tag
@children << new_tag
else
# this tag is not registered with the system
# pass it to the current block for special handling or error reporting
@@ -40,7 +44,9 @@ module Liquid
raise SyntaxError, "Tag '#{token}' was not properly terminated with regexp: #{TagEnd.inspect} "
end
when IsVariable
@nodelist << create_variable(token)
new_var = create_variable(token)
@nodelist << new_var
@children << new_var
@blank = false
when ''
# pass
@@ -61,9 +67,8 @@ module Liquid
all_warnings = []
all_warnings.concat(@warnings) if @warnings
return all_warnings unless @nodelist
@nodelist.each do |node|
p node
return all_warnings unless @children
@children.each do |node|
node_warns = node.respond_to?(:warnings) ? node.warnings : nil
all_warnings.concat(node_warns) if node_warns
end

View File

@@ -13,12 +13,12 @@ module Liquid
class Variable
FilterParser = /(?:#{FilterSeparator}|(?:\s*(?:#{QuotedFragment}|#{ArgumentSeparator})\s*)+)/o
EasyParse = /^ *(\w+(?:\.\w+)*) *$/
attr_accessor :filters, :name
attr_accessor :filters, :name, :warnings
def initialize(markup, options = {})
@markup = markup
@name = nil
@warning = nil
@warnings = []
@options = options || {}
@@ -29,7 +29,7 @@ module Liquid
begin
strict_parse(markup)
rescue SyntaxError => e
@warning = e
@warnings << e
lax_parse(markup)
end
end

View File

@@ -95,6 +95,7 @@ class ErrorHandlingTest < Test::Unit::TestCase
template = Liquid::Template.parse('{% if ~~~ %}{{%%%}}{% else %}wat{% endif %}', :error_mode => :warn)
assert_equal 2, template.warnings.size
assert_equal 'Unexpected character ~ in "~~~"', template.warnings.first.message
assert_equal 'Unexpected character % in "{{%%%}}"', template.warnings.last.message
assert_equal 'wat', template.render
end