Merge upstream branch 'master' into this branch

This commit is contained in:
Ashwin Maroli
2020-10-28 22:14:09 +05:30
21 changed files with 139 additions and 44 deletions

View File

@@ -13,6 +13,7 @@ module Liquid
@body = new_body
while parse_body(@body, tokens)
end
@body.freeze
end
# For backwards compatibility

View File

@@ -19,6 +19,8 @@ module Liquid
end
def parse(tokenizer, parse_context, &block)
raise FrozenError, "can't modify frozen Liquid::BlockBody" if frozen?
parse_context.line_number = tokenizer.line_number
if tokenizer.for_liquid_tag
@@ -28,6 +30,11 @@ module Liquid
end
end
def freeze
@nodelist.freeze
super
end
private def parse_for_liquid_tag(tokenizer, parse_context)
while (token = tokenizer.shift)
unless token.empty? || token =~ WhitespaceOrNothing
@@ -192,6 +199,8 @@ module Liquid
end
def render_to_output_buffer(context, output)
freeze unless frozen?
context.resource_limits.increment_render_score(@nodelist.length)
idx = 0

View File

@@ -45,8 +45,8 @@ module Liquid
@@operators
end
def self.parse_expression(markup)
@@method_literals[markup] || Expression.parse(markup)
def self.parse_expression(parse_context, markup)
@@method_literals[markup] || parse_context.parse_expression(markup)
end
attr_reader :attachment, :child_condition

View File

@@ -22,6 +22,7 @@ module Liquid
def parse(tokenizer, parse_context)
while parse_body(tokenizer)
end
@body.freeze
rescue SyntaxError => e
e.line_number ||= parse_context.line_number
raise

View File

@@ -10,25 +10,28 @@ module Liquid
'empty' => ''
}.freeze
SINGLE_QUOTED_STRING = /\A'(.*)'\z/m
DOUBLE_QUOTED_STRING = /\A"(.*)"\z/m
INTEGERS_REGEX = /\A(-?\d+)\z/
FLOATS_REGEX = /\A(-?\d[\d\.]+)\z/
RANGES_REGEX = /\A\((\S+)\.\.(\S+)\)\z/
SINGLE_QUOTED_STRING = /\A\s*'(.*)'\s*\z/m
DOUBLE_QUOTED_STRING = /\A\s*"(.*)"\s*\z/m
INTEGERS_REGEX = /\A\s*(-?\d+)\s*\z/
FLOATS_REGEX = /\A\s*(-?\d[\d\.]+)\s*\z/
RANGES_REGEX = /\A\s*\(\s*(\S+)\s*\.\.\s*(\S+)\s*\)\s*\z/
def self.parse(markup)
if LITERALS.key?(markup)
LITERALS[markup]
case markup
when nil
nil
when SINGLE_QUOTED_STRING, DOUBLE_QUOTED_STRING
Regexp.last_match(1)
when INTEGERS_REGEX
Regexp.last_match(1).to_i
when RANGES_REGEX
RangeLookup.parse(Regexp.last_match(1), Regexp.last_match(2))
when FLOATS_REGEX
Regexp.last_match(1).to_f
else
case markup
when SINGLE_QUOTED_STRING, DOUBLE_QUOTED_STRING
Regexp.last_match(1)
when INTEGERS_REGEX
Regexp.last_match(1).to_i
when RANGES_REGEX
RangeLookup.parse(Regexp.last_match(1), Regexp.last_match(2))
when FLOATS_REGEX
Regexp.last_match(1).to_f
markup = markup.strip
if LITERALS.key?(markup)
LITERALS[markup]
else
VariableLookup.parse(markup)
end

View File

@@ -23,6 +23,10 @@ module Liquid
Liquid::BlockBody.new
end
def parse_expression(markup)
Expression.parse(markup)
end
def partial=(value)
@partial = value
@options = value ? partial_options : @template_options

View File

@@ -2,6 +2,18 @@
module Liquid
module ParserSwitching
def strict_parse_with_error_mode_fallback(markup)
strict_parse_with_error_context(markup)
rescue SyntaxError => e
case parse_context.error_mode
when :strict
raise
when :warn
parse_context.warnings << e
end
lax_parse(markup)
end
def parse_with_selected_parser(markup)
case parse_context.error_mode
when :strict then strict_parse_with_error_context(markup)

View File

@@ -55,5 +55,11 @@ module Liquid
def blank?
false
end
private
def parse_expression(markup)
parse_context.parse_expression(markup)
end
end
end

View File

@@ -12,7 +12,7 @@ module Liquid
@blocks = []
if markup =~ Syntax
@left = Expression.parse(Regexp.last_match(1))
@left = parse_expression(Regexp.last_match(1))
else
raise SyntaxError, options[:locale].t("errors.syntax.case")
end
@@ -21,8 +21,9 @@ 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 }
@blocks.each do |condition|
condition.attachment.remove_blank_strings if blank?
condition.attachment.freeze
end
end
@@ -68,7 +69,7 @@ module Liquid
markup = Regexp.last_match(2)
block = Condition.new(@left, '==', Condition.parse_expression(Regexp.last_match(1)))
block = Condition.new(@left, '==', Condition.parse_expression(parse_context, Regexp.last_match(1)))
block.attach(body)
@blocks << block
end

View File

@@ -24,7 +24,7 @@ module Liquid
case markup
when NamedSyntax
@variables = variables_from_string(Regexp.last_match(2))
@name = Expression.parse(Regexp.last_match(1))
@name = parse_expression(Regexp.last_match(1))
when SimpleSyntax
@variables = variables_from_string(markup)
@name = @variables.to_s
@@ -61,7 +61,7 @@ module Liquid
def variables_from_string(markup)
markup.split(',').collect do |var|
var =~ /\s*(#{QuotedFragment})\s*/o
Regexp.last_match(1) ? Expression.parse(Regexp.last_match(1)) : nil
Regexp.last_match(1) ? parse_expression(Regexp.last_match(1)) : nil
end.compact
end

View File

@@ -66,6 +66,8 @@ module Liquid
@for_block.remove_blank_strings
@else_block&.remove_blank_strings
end
@for_block.freeze
@else_block&.freeze
end
def nodelist
@@ -97,7 +99,7 @@ module Liquid
collection_name = Regexp.last_match(2)
@reversed = !!Regexp.last_match(3)
@name = "#{@variable_name}-#{collection_name}"
@collection_name = Expression.parse(collection_name)
@collection_name = parse_expression(collection_name)
markup.scan(TagAttributes) do |key, value|
set_attribute(key, value)
end
@@ -112,7 +114,7 @@ module Liquid
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in')
collection_name = p.expression
@collection_name = Expression.parse(collection_name)
@collection_name = parse_expression(collection_name)
@name = "#{@variable_name}-#{collection_name}"
@reversed = p.id?('reversed')
@@ -198,10 +200,10 @@ module Liquid
@from = if expr == 'continue'
:continue
else
Expression.parse(expr)
parse_expression(expr)
end
when 'limit'
@limit = Expression.parse(expr)
@limit = parse_expression(expr)
end
end

View File

@@ -31,8 +31,9 @@ module Liquid
def parse(tokens)
while parse_body(@blocks.last.attachment, tokens)
end
if blank?
@blocks.each { |condition| condition.attachment.remove_blank_strings }
@blocks.each do |block|
block.attachment.remove_blank_strings if blank?
block.attachment.freeze
end
end
@@ -71,7 +72,7 @@ module Liquid
end
def parse_expression(markup)
Condition.parse_expression(markup)
Condition.parse_expression(parse_context, markup)
end
def lax_parse(markup)

View File

@@ -32,12 +32,12 @@ module Liquid
variable_name = Regexp.last_match(3)
@alias_name = Regexp.last_match(5)
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
@template_name_expr = Expression.parse(template_name)
@variable_name_expr = variable_name ? parse_expression(variable_name) : nil
@template_name_expr = parse_expression(template_name)
@attributes = {}
markup.scan(TagAttributes) do |key, value|
@attributes[key] = Expression.parse(value)
@attributes[key] = parse_expression(value)
end
else

View File

@@ -19,13 +19,13 @@ module Liquid
variable_name = Regexp.last_match(4)
@alias_name = Regexp.last_match(6)
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
@template_name_expr = Expression.parse(template_name)
@variable_name_expr = variable_name ? parse_expression(variable_name) : nil
@template_name_expr = parse_expression(template_name)
@for = (with_or_for == FOR)
@attributes = {}
markup.scan(TagAttributes) do |key, value|
@attributes[key] = Expression.parse(value)
@attributes[key] = parse_expression(value)
end
end

View File

@@ -10,10 +10,10 @@ module Liquid
super
if markup =~ Syntax
@variable_name = Regexp.last_match(1)
@collection_name = Expression.parse(Regexp.last_match(2))
@collection_name = parse_expression(Regexp.last_match(2))
@attributes = {}
markup.scan(TagAttributes) do |key, value|
@attributes[key] = Expression.parse(value)
@attributes[key] = parse_expression(value)
end
else
raise SyntaxError, options[:locale].t("errors.syntax.table_row")

View File

@@ -30,7 +30,7 @@ module Liquid
@parse_context = parse_context
@line_number = parse_context.line_number
parse_with_selected_parser(markup)
strict_parse_with_error_mode_fallback(markup)
end
def raw