mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Introduce :rigid parsing mode
This commit is contained in:
committed by
Guilherme Carreiro
parent
1c1e711906
commit
edf06c2882
@@ -34,7 +34,7 @@ module Liquid
|
||||
# @param file_system The default file system that is used
|
||||
# to load templates from.
|
||||
# @param error_mode [Symbol] The default error mode for all templates
|
||||
# (either :strict, :warn, or :lax).
|
||||
# (either :strict, :warn, :lax, or :rigid).
|
||||
# @param exception_renderer [Proc] The exception renderer that is used to
|
||||
# render exceptions.
|
||||
# @yieldparam environment [Environment] The environment instance that is being built.
|
||||
|
||||
@@ -51,6 +51,26 @@ module Liquid
|
||||
end
|
||||
|
||||
def parse_expression(markup)
|
||||
if @error_mode == :rigid
|
||||
parser = new_parser(markup)
|
||||
|
||||
# Return nil immediately if the markup is empty or contains only
|
||||
# whitespaces
|
||||
return if parser.look(:end_of_string)
|
||||
|
||||
expression_string = parser.expression
|
||||
|
||||
# In rigid mode, verify that all tokens have been consumed
|
||||
#
|
||||
# Extra tokens remaining after the expression indicate invalid syntaxes,
|
||||
# such as: "product title" (instead of "product.title")
|
||||
parser.consume(:end_of_string) unless parser.look(:end_of_string)
|
||||
|
||||
# Use Parser for strict token validation, but still return
|
||||
# Expression objects for compatibility with the rendering pipeline.
|
||||
markup = expression_string
|
||||
end
|
||||
|
||||
Expression.parse(markup, @string_scanner, @expression_cache)
|
||||
end
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ module Liquid
|
||||
case parse_context.error_mode
|
||||
when :strict
|
||||
raise
|
||||
when :rigid
|
||||
raise
|
||||
when :warn
|
||||
parse_context.warnings << e
|
||||
end
|
||||
@@ -17,6 +19,7 @@ module Liquid
|
||||
def parse_with_selected_parser(markup)
|
||||
case parse_context.error_mode
|
||||
when :strict then strict_parse_with_error_context(markup)
|
||||
when :rigid then strict_parse_with_error_context(markup)
|
||||
when :lax then lax_parse(markup)
|
||||
when :warn
|
||||
begin
|
||||
|
||||
Reference in New Issue
Block a user