mirror of
https://github.com/kemko/liquid.git
synced 2026-01-01 15:55:40 +03:00
Compare commits
10 Commits
inline-com
...
fix-consta
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3dfcd9dedc | ||
|
|
4ca476f71e | ||
|
|
12f702c431 | ||
|
|
da5688aeb2 | ||
|
|
9c0bdf80bd | ||
|
|
da2fe9cab0 | ||
|
|
00c8ca559a | ||
|
|
26eb27f79f | ||
|
|
05c0bcf609 | ||
|
|
a4ec6a08cb |
@@ -21,25 +21,6 @@ Lint/InheritException:
|
||||
Metrics/LineLength:
|
||||
Max: 294
|
||||
|
||||
# Offense count: 44
|
||||
Naming/ConstantName:
|
||||
Exclude:
|
||||
- 'lib/liquid.rb'
|
||||
- 'lib/liquid/block_body.rb'
|
||||
- 'lib/liquid/tags/assign.rb'
|
||||
- 'lib/liquid/tags/capture.rb'
|
||||
- 'lib/liquid/tags/case.rb'
|
||||
- 'lib/liquid/tags/cycle.rb'
|
||||
- 'lib/liquid/tags/for.rb'
|
||||
- 'lib/liquid/tags/if.rb'
|
||||
- 'lib/liquid/tags/include.rb'
|
||||
- 'lib/liquid/tags/raw.rb'
|
||||
- 'lib/liquid/tags/table_row.rb'
|
||||
- 'lib/liquid/variable.rb'
|
||||
- 'performance/shopify/comment_form.rb'
|
||||
- 'performance/shopify/paginate.rb'
|
||||
- 'test/integration/tags/include_tag_test.rb'
|
||||
|
||||
# Offense count: 5
|
||||
Style/ClassVars:
|
||||
Exclude:
|
||||
|
||||
@@ -22,25 +22,25 @@
|
||||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
module Liquid
|
||||
FilterSeparator = /\|/
|
||||
ArgumentSeparator = ','
|
||||
FilterArgumentSeparator = ':'
|
||||
VariableAttributeSeparator = '.'
|
||||
WhitespaceControl = '-'
|
||||
TagStart = /\{\%/
|
||||
TagEnd = /\%\}/
|
||||
VariableSignature = /\(?[\w\-\.\[\]]\)?/
|
||||
VariableSegment = /[\w\-]/
|
||||
VariableStart = /\{\{/
|
||||
VariableEnd = /\}\}/
|
||||
VariableIncompleteEnd = /\}\}?/
|
||||
QuotedString = /"[^"]*"|'[^']*'/
|
||||
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/o
|
||||
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/o
|
||||
AnyStartingTag = /#{TagStart}|#{VariableStart}/o
|
||||
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
|
||||
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
|
||||
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/o
|
||||
FILTER_SEPARATOR = /\|/
|
||||
ARGUMENT_SEPARATOR = ','
|
||||
FILTER_ARGUMENT_SEPARATOR = ':'
|
||||
VARIABLE_ATTRIBUTE_SEPARATOR = '.'
|
||||
WHITESPACE_CONTROL = '-'
|
||||
TAG_START = /\{\%/
|
||||
TAG_END = /\%\}/
|
||||
VARIABLE_SIGNATURE = /\(?[\w\-\.\[\]]\)?/
|
||||
VARIABLE_SEGMENT = /[\w\-]/
|
||||
VARIABLE_START = /\{\{/
|
||||
VARIABLE_END = /\}\}/
|
||||
VARIABLE_INCOMPLETE_END = /\}\}?/
|
||||
QUOTED_STRING = /"[^"]*"|'[^']*'/
|
||||
QUOTED_FRAGMENT = /#{QUOTED_STRING}|(?:[^\s,\|'"]|#{QUOTED_STRING})+/o
|
||||
TAG_ATTRIBUTES = /(\w+)\s*\:\s*(#{QUOTED_FRAGMENT})/o
|
||||
ANY_STARTING_TAG = /#{TAG_START}|#{VARIABLE_START}/o
|
||||
PARTIAL_TEMPLATE_PARSER = /#{TAG_START}.*?#{TAG_END}|#{VARIABLE_START}.*?#{VARIABLE_INCOMPLETE_END}/om
|
||||
TEMPLATE_PARSER = /(#{PARTIAL_TEMPLATE_PARSER}|#{ANY_STARTING_TAG})/om
|
||||
VARIABLE_PARSER = /\[[^\]]+\]|#{VARIABLE_SEGMENT}+\??/o
|
||||
|
||||
singleton_class.send(:attr_accessor, :cache_classes)
|
||||
self.cache_classes = true
|
||||
@@ -85,3 +85,5 @@ require 'liquid/static_registers'
|
||||
#
|
||||
Dir["#{__dir__}/liquid/tags/*.rb"].each { |f| require f }
|
||||
Dir["#{__dir__}/liquid/registers/*.rb"].each { |f| require f }
|
||||
|
||||
require 'liquid/legacy'
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
module Liquid
|
||||
class BlockBody
|
||||
LiquidTagToken = /\A\s*(\w+)\s*(.*?)\z/o
|
||||
FullToken = /\A#{TagStart}#{WhitespaceControl}?(\s*)(\w+)(\s*)(.*?)#{WhitespaceControl}?#{TagEnd}\z/om
|
||||
ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
|
||||
WhitespaceOrNothing = /\A\s*\z/
|
||||
TAGSTART = "{%"
|
||||
VARSTART = "{{"
|
||||
LIQUID_TAG_TOKEN = /\A\s*(\w+)\s*(.*?)\z/o
|
||||
FULL_TOKEN = /\A#{TAG_START}#{WHITESPACE_CONTROL}?(\s*)(\w+)(\s*)(.*?)#{WHITESPACE_CONTROL}?#{TAG_END}\z/om
|
||||
CONTENT_OF_VARIABLE = /\A#{VARIABLE_START}#{WHITESPACE_CONTROL}?(.*?)#{WHITESPACE_CONTROL}?#{VARIABLE_END}\z/om
|
||||
WHITESPACE_OR_NOTHING = /\A\s*\z/
|
||||
TAG_START_STRING = "{%"
|
||||
VAR_START_STRING = "{{"
|
||||
|
||||
attr_reader :nodelist
|
||||
|
||||
@@ -28,8 +28,8 @@ module Liquid
|
||||
|
||||
private def parse_for_liquid_tag(tokenizer, parse_context)
|
||||
while (token = tokenizer.shift)
|
||||
unless token.empty? || token =~ WhitespaceOrNothing
|
||||
unless token =~ LiquidTagToken
|
||||
unless token.empty? || token =~ WHITESPACE_OR_NOTHING
|
||||
unless token =~ LIQUID_TAG_TOKEN
|
||||
# line isn't empty but didn't match tag syntax, yield and let the
|
||||
# caller raise a syntax error
|
||||
return yield token, token
|
||||
@@ -55,9 +55,9 @@ module Liquid
|
||||
while (token = tokenizer.shift)
|
||||
next if token.empty?
|
||||
case
|
||||
when token.start_with?(TAGSTART)
|
||||
when token.start_with?(TAG_START_STRING)
|
||||
whitespace_handler(token, parse_context)
|
||||
unless token =~ FullToken
|
||||
unless token =~ FULL_TOKEN
|
||||
raise_missing_tag_terminator(token, parse_context)
|
||||
end
|
||||
tag_name = Regexp.last_match(2)
|
||||
@@ -82,7 +82,7 @@ module Liquid
|
||||
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
|
||||
@blank &&= new_tag.blank?
|
||||
@nodelist << new_tag
|
||||
when token.start_with?(VARSTART)
|
||||
when token.start_with?(VAR_START_STRING)
|
||||
whitespace_handler(token, parse_context)
|
||||
@nodelist << create_variable(token, parse_context)
|
||||
@blank = false
|
||||
@@ -92,7 +92,7 @@ module Liquid
|
||||
end
|
||||
parse_context.trim_whitespace = false
|
||||
@nodelist << token
|
||||
@blank &&= !!(token =~ WhitespaceOrNothing)
|
||||
@blank &&= !!(token =~ WHITESPACE_OR_NOTHING)
|
||||
end
|
||||
parse_context.line_number = tokenizer.line_number
|
||||
end
|
||||
@@ -101,13 +101,13 @@ module Liquid
|
||||
end
|
||||
|
||||
def whitespace_handler(token, parse_context)
|
||||
if token[2] == WhitespaceControl
|
||||
if token[2] == WHITESPACE_CONTROL
|
||||
previous_token = @nodelist.last
|
||||
if previous_token.is_a?(String)
|
||||
previous_token.rstrip!
|
||||
end
|
||||
end
|
||||
parse_context.trim_whitespace = (token[-3] == WhitespaceControl)
|
||||
parse_context.trim_whitespace = (token[-3] == WHITESPACE_CONTROL)
|
||||
end
|
||||
|
||||
def blank?
|
||||
@@ -180,7 +180,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def create_variable(token, parse_context)
|
||||
token.scan(ContentOfVariable) do |content|
|
||||
token.scan(CONTENT_OF_VARIABLE) do |content|
|
||||
markup = content.first
|
||||
return Variable.new(markup, parse_context)
|
||||
end
|
||||
@@ -188,11 +188,11 @@ module Liquid
|
||||
end
|
||||
|
||||
def raise_missing_tag_terminator(token, parse_context)
|
||||
raise SyntaxError, parse_context.locale.t("errors.syntax.tag_termination", token: token, tag_end: TagEnd.inspect)
|
||||
raise SyntaxError, parse_context.locale.t("errors.syntax.tag_termination", token: token, tag_end: TAG_END.inspect)
|
||||
end
|
||||
|
||||
def raise_missing_variable_terminator(token, parse_context)
|
||||
raise SyntaxError, parse_context.locale.t("errors.syntax.variable_termination", token: token, tag_end: VariableEnd.inspect)
|
||||
raise SyntaxError, parse_context.locale.t("errors.syntax.variable_termination", token: token, tag_end: VARIABLE_END.inspect)
|
||||
end
|
||||
|
||||
def registered_tags
|
||||
|
||||
79
lib/liquid/legacy.rb
Normal file
79
lib/liquid/legacy.rb
Normal file
@@ -0,0 +1,79 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
FilterSeparator = FILTER_SEPARATOR
|
||||
ArgumentSeparator = ARGUMENT_SEPARATOR
|
||||
FilterArgumentSeparator = FILTER_ARGUMENT_SEPARATOR
|
||||
VariableAttributeSeparator = VARIABLE_ATTRIBUTE_SEPARATOR
|
||||
WhitespaceControl = WHITESPACE_CONTROL
|
||||
TagStart = TAG_START
|
||||
TagEnd = TAG_END
|
||||
VariableSignature = VARIABLE_SIGNATURE
|
||||
VariableSegment = VARIABLE_SEGMENT
|
||||
VariableStart = VARIABLE_START
|
||||
VariableEnd = VARIABLE_END
|
||||
VariableIncompleteEnd = VARIABLE_INCOMPLETE_END
|
||||
QuotedString = QUOTED_STRING
|
||||
QuotedFragment = QUOTED_FRAGMENT
|
||||
TagAttributes = TAG_ATTRIBUTES
|
||||
AnyStartingTag = ANY_STARTING_TAG
|
||||
PartialTemplateParser = PARTIAL_TEMPLATE_PARSER
|
||||
TemplateParser = TEMPLATE_PARSER
|
||||
VariableParser = VARIABLE_PARSER
|
||||
|
||||
class BlockBody
|
||||
FullToken = FULL_TOKEN
|
||||
ContentOfVariable = CONTENT_OF_VARIABLE
|
||||
WhitespaceOrNothing = WHITESPACE_OR_NOTHING
|
||||
TAGSTART = TAG_START_STRING
|
||||
VARSTART = VAR_START_STRING
|
||||
end
|
||||
|
||||
class Assign < Tag
|
||||
Syntax = SYNTAX
|
||||
end
|
||||
|
||||
class Capture < Block
|
||||
Syntax = SYNTAX
|
||||
end
|
||||
|
||||
class Case < Block
|
||||
Syntax = SYNTAX
|
||||
WhenSyntax = WHEN_SYNTAX
|
||||
end
|
||||
|
||||
class Cycle < Tag
|
||||
SimpleSyntax = SIMPLE_SYNTAX
|
||||
NamedSyntax = NAMED_SYNTAX
|
||||
end
|
||||
|
||||
class For < Block
|
||||
Syntax = SYNTAX
|
||||
end
|
||||
|
||||
class If < Block
|
||||
Syntax = SYNTAX
|
||||
ExpressionsAndOperators = EXPRESSIONS_AND_OPERATORS
|
||||
end
|
||||
|
||||
class Include < Tag
|
||||
Syntax = SYNTAX
|
||||
end
|
||||
|
||||
class Raw < Block
|
||||
Syntax = SYNTAX
|
||||
FullTokenPossiblyInvalid = FULL_TOKEN_POSSIBLY_INVALID
|
||||
end
|
||||
|
||||
class TableRow < Block
|
||||
Syntax = SYNTAX
|
||||
end
|
||||
|
||||
class Variable
|
||||
FilterMarkupRegex = FILTER_MARKUP_REGEX
|
||||
FilterParser = FILTER_PARSER
|
||||
FilterArgsRegex = FILTER_ARGS_REGEX
|
||||
JustTagAttributes = JUST_TAG_ATTRIBUTES
|
||||
MarkupWithQuotedFragment = MARKUP_WITH_QUOTED_FRAGMENT
|
||||
end
|
||||
end
|
||||
@@ -10,7 +10,7 @@ module Liquid
|
||||
# {{ foo }}
|
||||
#
|
||||
class Assign < Tag
|
||||
Syntax = /(#{VariableSignature}+)\s*=\s*(.*)\s*/om
|
||||
SYNTAX = /(#{VARIABLE_SIGNATURE}+)\s*=\s*(.*)\s*/om
|
||||
|
||||
def self.syntax_error_translation_key
|
||||
"errors.syntax.assign"
|
||||
@@ -20,8 +20,8 @@ module Liquid
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
if markup =~ Syntax
|
||||
@to = Regexp.last_match(1)
|
||||
if markup =~ SYNTAX
|
||||
@to = Regexp.last_match(1)
|
||||
@from = Variable.new(Regexp.last_match(2), options)
|
||||
else
|
||||
raise SyntaxError, options[:locale].t(self.class.syntax_error_translation_key)
|
||||
|
||||
@@ -13,11 +13,11 @@ module Liquid
|
||||
# in a sidebar or footer.
|
||||
#
|
||||
class Capture < Block
|
||||
Syntax = /(#{VariableSignature}+)/o
|
||||
SYNTAX = /(#{VARIABLE_SIGNATURE}+)/o
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
if markup =~ Syntax
|
||||
if markup =~ SYNTAX
|
||||
@to = Regexp.last_match(1)
|
||||
else
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.capture")
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
module Liquid
|
||||
class Case < Block
|
||||
Syntax = /(#{QuotedFragment})/o
|
||||
WhenSyntax = /(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/om
|
||||
SYNTAX = /(#{QUOTED_FRAGMENT})/o
|
||||
WHEN_SYNTAX = /(#{QUOTED_FRAGMENT})(?:(?:\s+or\s+|\s*\,\s*)(#{QUOTED_FRAGMENT}.*))?/om
|
||||
|
||||
attr_reader :blocks, :left
|
||||
|
||||
@@ -11,7 +11,7 @@ module Liquid
|
||||
super
|
||||
@blocks = []
|
||||
|
||||
if markup =~ Syntax
|
||||
if markup =~ SYNTAX
|
||||
@left = Expression.parse(Regexp.last_match(1))
|
||||
else
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.case")
|
||||
@@ -59,7 +59,7 @@ module Liquid
|
||||
body = BlockBody.new
|
||||
|
||||
while markup
|
||||
unless markup =~ WhenSyntax
|
||||
unless markup =~ WHEN_SYNTAX
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.case_invalid_when")
|
||||
end
|
||||
|
||||
|
||||
@@ -14,18 +14,18 @@ module Liquid
|
||||
# <div class="green"> Item five</div>
|
||||
#
|
||||
class Cycle < Tag
|
||||
SimpleSyntax = /\A#{QuotedFragment}+/o
|
||||
NamedSyntax = /\A(#{QuotedFragment})\s*\:\s*(.*)/om
|
||||
SIMPLE_SYNTAX = /\A#{QUOTED_FRAGMENT}+/o
|
||||
NAMED_SYNTAX = /\A(#{QUOTED_FRAGMENT})\s*\:\s*(.*)/om
|
||||
|
||||
attr_reader :variables
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
case markup
|
||||
when NamedSyntax
|
||||
when NAMED_SYNTAX
|
||||
@variables = variables_from_string(Regexp.last_match(2))
|
||||
@name = Expression.parse(Regexp.last_match(1))
|
||||
when SimpleSyntax
|
||||
when SIMPLE_SYNTAX
|
||||
@variables = variables_from_string(markup)
|
||||
@name = @variables.to_s
|
||||
else
|
||||
@@ -60,7 +60,7 @@ module Liquid
|
||||
|
||||
def variables_from_string(markup)
|
||||
markup.split(',').collect do |var|
|
||||
var =~ /\s*(#{QuotedFragment})\s*/o
|
||||
var =~ /\s*(#{QUOTED_FRAGMENT})\s*/o
|
||||
Regexp.last_match(1) ? Expression.parse(Regexp.last_match(1)) : nil
|
||||
end.compact
|
||||
end
|
||||
|
||||
@@ -46,7 +46,7 @@ module Liquid
|
||||
# forloop.parentloop:: Provides access to the parent loop, if present.
|
||||
#
|
||||
class For < Block
|
||||
Syntax = /\A(#{VariableSegment}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o
|
||||
SYNTAX = /\A(#{VARIABLE_SEGMENT}+)\s+in\s+(#{QUOTED_FRAGMENT}+)\s*(reversed)?/o
|
||||
|
||||
attr_reader :collection_name, :variable_name, :limit, :from
|
||||
|
||||
@@ -87,13 +87,13 @@ module Liquid
|
||||
protected
|
||||
|
||||
def lax_parse(markup)
|
||||
if markup =~ Syntax
|
||||
@variable_name = Regexp.last_match(1)
|
||||
collection_name = Regexp.last_match(2)
|
||||
@reversed = !!Regexp.last_match(3)
|
||||
@name = "#{@variable_name}-#{collection_name}"
|
||||
if markup =~ SYNTAX
|
||||
@variable_name = Regexp.last_match(1)
|
||||
collection_name = Regexp.last_match(2)
|
||||
@reversed = !!Regexp.last_match(3)
|
||||
@name = "#{@variable_name}-#{collection_name}"
|
||||
@collection_name = Expression.parse(collection_name)
|
||||
markup.scan(TagAttributes) do |key, value|
|
||||
markup.scan(TAG_ATTRIBUTES) do |key, value|
|
||||
set_attribute(key, value)
|
||||
end
|
||||
else
|
||||
|
||||
@@ -12,9 +12,9 @@ module Liquid
|
||||
# There are {% if count < 5 %} less {% else %} more {% endif %} items than you need.
|
||||
#
|
||||
class If < Block
|
||||
Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/o
|
||||
ExpressionsAndOperators = /(?:\b(?:\s?and\s?|\s?or\s?)\b|(?:\s*(?!\b(?:\s?and\s?|\s?or\s?)\b)(?:#{QuotedFragment}|\S+)\s*)+)/o
|
||||
BOOLEAN_OPERATORS = %w(and or).freeze
|
||||
SYNTAX = /(#{QUOTED_FRAGMENT})\s*([=!<>a-z_]+)?\s*(#{QUOTED_FRAGMENT})?/o
|
||||
EXPRESSIONS_AND_OPERATORS = /(?:\b(?:\s?and\s?|\s?or\s?)\b|(?:\s*(?!\b(?:\s?and\s?|\s?or\s?)\b)(?:#{QUOTED_FRAGMENT}|\S+)\s*)+)/o
|
||||
BOOLEAN_OPERATORS = %w(and or).freeze
|
||||
|
||||
attr_reader :blocks
|
||||
|
||||
@@ -65,15 +65,15 @@ module Liquid
|
||||
end
|
||||
|
||||
def lax_parse(markup)
|
||||
expressions = markup.scan(ExpressionsAndOperators)
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop =~ Syntax
|
||||
expressions = markup.scan(EXPRESSIONS_AND_OPERATORS)
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop =~ SYNTAX
|
||||
|
||||
condition = Condition.new(Expression.parse(Regexp.last_match(1)), Regexp.last_match(2), Expression.parse(Regexp.last_match(3)))
|
||||
|
||||
until expressions.empty?
|
||||
operator = expressions.pop.to_s.strip
|
||||
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop.to_s =~ Syntax
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop.to_s =~ SYNTAX
|
||||
|
||||
new_condition = Condition.new(Expression.parse(Regexp.last_match(1)), Regexp.last_match(2), Expression.parse(Regexp.last_match(3)))
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.if") unless BOOLEAN_OPERATORS.include?(operator)
|
||||
|
||||
@@ -17,7 +17,6 @@ module Liquid
|
||||
#
|
||||
class Include < Tag
|
||||
SYNTAX = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||
Syntax = SYNTAX
|
||||
|
||||
attr_reader :template_name_expr, :variable_name_expr, :attributes
|
||||
|
||||
@@ -34,7 +33,7 @@ module Liquid
|
||||
@template_name_expr = Expression.parse(template_name)
|
||||
@attributes = {}
|
||||
|
||||
markup.scan(TagAttributes) do |key, value|
|
||||
markup.scan(TAG_ATTRIBUTES) do |key, value|
|
||||
@attributes[key] = Expression.parse(value)
|
||||
end
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
module Liquid
|
||||
class Raw < Block
|
||||
Syntax = /\A\s*\z/
|
||||
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
|
||||
SYNTAX = /\A\s*\z/
|
||||
FULL_TOKEN_POSSIBLY_INVALID = /\A(.*)#{TAG_START}\s*(\w+)\s*(.*)?#{TAG_END}\z/om
|
||||
|
||||
def initialize(tag_name, markup, parse_context)
|
||||
super
|
||||
@@ -14,7 +14,7 @@ module Liquid
|
||||
def parse(tokens)
|
||||
@body = +''
|
||||
while (token = tokens.shift)
|
||||
if token =~ FullTokenPossiblyInvalid
|
||||
if token =~ FULL_TOKEN_POSSIBLY_INVALID
|
||||
@body << Regexp.last_match(1) if Regexp.last_match(1) != ""
|
||||
return if block_delimiter == Regexp.last_match(2)
|
||||
end
|
||||
@@ -40,7 +40,7 @@ module Liquid
|
||||
protected
|
||||
|
||||
def ensure_valid_markup(tag_name, markup, parse_context)
|
||||
unless Syntax.match?(markup)
|
||||
unless SYNTAX.match?(markup)
|
||||
raise SyntaxError, parse_context.locale.t("errors.syntax.tag_unexpected_args", tag: tag_name)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,7 +21,7 @@ module Liquid
|
||||
@template_name_expr = Expression.parse(template_name)
|
||||
|
||||
@attributes = {}
|
||||
markup.scan(TagAttributes) do |key, value|
|
||||
markup.scan(TAG_ATTRIBUTES) do |key, value|
|
||||
@attributes[key] = Expression.parse(value)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,17 +2,17 @@
|
||||
|
||||
module Liquid
|
||||
class TableRow < Block
|
||||
Syntax = /(\w+)\s+in\s+(#{QuotedFragment}+)/o
|
||||
SYNTAX = /(\w+)\s+in\s+(#{QUOTED_FRAGMENT}+)/o
|
||||
|
||||
attr_reader :variable_name, :collection_name, :attributes
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
if markup =~ Syntax
|
||||
if markup =~ SYNTAX
|
||||
@variable_name = Regexp.last_match(1)
|
||||
@collection_name = Expression.parse(Regexp.last_match(2))
|
||||
@attributes = {}
|
||||
markup.scan(TagAttributes) do |key, value|
|
||||
markup.scan(TAG_ATTRIBUTES) do |key, value|
|
||||
@attributes[key] = Expression.parse(value)
|
||||
end
|
||||
else
|
||||
|
||||
@@ -28,7 +28,7 @@ module Liquid
|
||||
|
||||
return @source.split("\n") if @for_liquid_tag
|
||||
|
||||
tokens = @source.split(TemplateParser)
|
||||
tokens = @source.split(TEMPLATE_PARSER)
|
||||
|
||||
# removes the rogue empty element at the beginning of the array
|
||||
tokens.shift if tokens[0]&.empty?
|
||||
|
||||
@@ -12,11 +12,11 @@ module Liquid
|
||||
# {{ user | link }}
|
||||
#
|
||||
class Variable
|
||||
FilterMarkupRegex = /#{FilterSeparator}\s*(.*)/om
|
||||
FilterParser = /(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o
|
||||
FilterArgsRegex = /(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o
|
||||
JustTagAttributes = /\A#{TagAttributes}\z/o
|
||||
MarkupWithQuotedFragment = /(#{QuotedFragment})(.*)/om
|
||||
FILTER_MARKUP_REGEX = /#{FILTER_SEPARATOR}\s*(.*)/om
|
||||
FILTER_PARSER = /(?:\s+|#{QUOTED_FRAGMENT}|#{ARGUMENT_SEPARATOR})+/o
|
||||
FILTER_ARGS_REGEX . = /(?:#{FILTER_ARGUMENT_SEPARATOR}|#{ARGUMENT_SEPARATOR})\s*((?:\w+\s*\:\s*)?#{QUOTED_FRAGMENT})/o
|
||||
JUST_TAG_ATTRIBUTES = /\A#{TAG_ATTRIBUTES}\z/o
|
||||
MARKUP_WITH_QUOTED_FRAGMENT = /(#{QUOTED_FRAGMENT})(.*)/om
|
||||
|
||||
attr_accessor :filters, :name, :line_number
|
||||
attr_reader :parse_context
|
||||
@@ -43,17 +43,17 @@ module Liquid
|
||||
|
||||
def lax_parse(markup)
|
||||
@filters = []
|
||||
return unless markup =~ MarkupWithQuotedFragment
|
||||
return unless markup =~ MARKUP_WITH_QUOTED_FRAGMENT
|
||||
|
||||
name_markup = Regexp.last_match(1)
|
||||
filter_markup = Regexp.last_match(2)
|
||||
@name = Expression.parse(name_markup)
|
||||
if filter_markup =~ FilterMarkupRegex
|
||||
filters = Regexp.last_match(1).scan(FilterParser)
|
||||
if filter_markup =~ FILTER_MARKUP_REGEX
|
||||
filters = Regexp.last_match(1).scan(FILTER_PARSER)
|
||||
filters.each do |f|
|
||||
next unless f =~ /\w+/
|
||||
filtername = Regexp.last_match(0)
|
||||
filterargs = f.scan(FilterArgsRegex).flatten
|
||||
filterargs = f.scan(FILTER_ARGS_REGEX).flatten
|
||||
@filters << parse_filter_expressions(filtername, filterargs)
|
||||
end
|
||||
end
|
||||
@@ -118,7 +118,7 @@ module Liquid
|
||||
filter_args = []
|
||||
keyword_args = nil
|
||||
unparsed_args.each do |a|
|
||||
if (matches = a.match(JustTagAttributes))
|
||||
if (matches = a.match(JUST_TAG_ATTRIBUTES))
|
||||
keyword_args ||= {}
|
||||
keyword_args[matches[1]] = Expression.parse(matches[2])
|
||||
else
|
||||
@@ -146,7 +146,7 @@ module Liquid
|
||||
return unless obj.tainted?
|
||||
return if Template.taint_mode == :lax
|
||||
|
||||
@markup =~ QuotedFragment
|
||||
@markup =~ QUOTED_FRAGMENT
|
||||
name = Regexp.last_match(0)
|
||||
|
||||
error = TaintedError.new("variable '#{name}' is tainted and was not escaped")
|
||||
|
||||
@@ -12,7 +12,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def initialize(markup)
|
||||
lookups = markup.scan(VariableParser)
|
||||
lookups = markup.scan(VARIABLE_PARSER)
|
||||
|
||||
name = lookups.shift
|
||||
if name =~ SQUARE_BRACKETED
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CommentForm < Liquid::Block
|
||||
Syntax = /(#{Liquid::VariableSignature}+)/
|
||||
SYNTAX = /(#{Liquid::VariableSignature}+)/
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
|
||||
if markup =~ Syntax
|
||||
if markup =~ SYNTAX
|
||||
@variable_name = Regexp.last_match(1)
|
||||
@attributes = {}
|
||||
else
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Paginate < Liquid::Block
|
||||
Syntax = /(#{Liquid::QuotedFragment})\s*(by\s*(\d+))?/
|
||||
SYNTAX = /(#{Liquid::QUOTED_FRAGMENT})\s*(by\s*(\d+))?/
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
|
||||
if markup =~ Syntax
|
||||
if markup =~ SYNTAX
|
||||
@collection_name = Regexp.last_match(1)
|
||||
@page_size = if Regexp.last_match(2)
|
||||
Regexp.last_match(3).to_i
|
||||
@@ -15,7 +15,7 @@ class Paginate < Liquid::Block
|
||||
end
|
||||
|
||||
@attributes = { 'window_size' => 3 }
|
||||
markup.scan(Liquid::TagAttributes) do |key, value|
|
||||
markup.scan(Liquid::TAG_ATTRIBUTES) do |key, value|
|
||||
@attributes[key] = value
|
||||
end
|
||||
else
|
||||
|
||||
@@ -60,10 +60,10 @@ class CountingFileSystem
|
||||
end
|
||||
|
||||
class CustomInclude < Liquid::Tag
|
||||
Syntax = /(#{Liquid::QuotedFragment}+)(\s+(?:with|for)\s+(#{Liquid::QuotedFragment}+))?/o
|
||||
SYNTAX = /(#{Liquid::QUOTED_FRAGMENT}+)(\s+(?:with|for)\s+(#{Liquid::QUOTED_FRAGMENT}+))?/o
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
markup =~ Syntax
|
||||
markup =~ SYNTAX
|
||||
@template_name = Regexp.last_match(1)
|
||||
super
|
||||
end
|
||||
|
||||
@@ -6,41 +6,41 @@ class RegexpUnitTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_empty
|
||||
assert_equal([], ''.scan(QuotedFragment))
|
||||
assert_equal [], ''.scan(QUOTED_FRAGMENT)
|
||||
end
|
||||
|
||||
def test_quote
|
||||
assert_equal(['"arg 1"'], '"arg 1"'.scan(QuotedFragment))
|
||||
assert_equal ['"arg 1"'], '"arg 1"'.scan(QUOTED_FRAGMENT)
|
||||
end
|
||||
|
||||
def test_words
|
||||
assert_equal(['arg1', 'arg2'], 'arg1 arg2'.scan(QuotedFragment))
|
||||
assert_equal ['arg1', 'arg2'], 'arg1 arg2'.scan(QUOTED_FRAGMENT)
|
||||
end
|
||||
|
||||
def test_tags
|
||||
assert_equal(['<tr>', '</tr>'], '<tr> </tr>'.scan(QuotedFragment))
|
||||
assert_equal(['<tr></tr>'], '<tr></tr>'.scan(QuotedFragment))
|
||||
assert_equal(['<style', 'class="hello">', '</style>'], %(<style class="hello">' </style>).scan(QuotedFragment))
|
||||
assert_equal ['<tr>', '</tr>'], '<tr> </tr>'.scan(QUOTED_FRAGMENT)
|
||||
assert_equal ['<tr></tr>'], '<tr></tr>'.scan(QUOTED_FRAGMENT)
|
||||
assert_equal ['<style', 'class="hello">', '</style>'], %(<style class="hello">' </style>).scan(QUOTED_FRAGMENT)
|
||||
end
|
||||
|
||||
def test_double_quoted_words
|
||||
assert_equal(['arg1', 'arg2', '"arg 3"'], 'arg1 arg2 "arg 3"'.scan(QuotedFragment))
|
||||
assert_equal ['arg1', 'arg2', '"arg 3"'], 'arg1 arg2 "arg 3"'.scan(QUOTED_FRAGMENT)
|
||||
end
|
||||
|
||||
def test_single_quoted_words
|
||||
assert_equal(['arg1', 'arg2', "'arg 3'"], 'arg1 arg2 \'arg 3\''.scan(QuotedFragment))
|
||||
assert_equal ['arg1', 'arg2', "'arg 3'"], 'arg1 arg2 \'arg 3\''.scan(QUOTED_FRAGMENT)
|
||||
end
|
||||
|
||||
def test_quoted_words_in_the_middle
|
||||
assert_equal(['arg1', 'arg2', '"arg 3"', 'arg4'], 'arg1 arg2 "arg 3" arg4 '.scan(QuotedFragment))
|
||||
assert_equal ['arg1', 'arg2', '"arg 3"', 'arg4'], 'arg1 arg2 "arg 3" arg4 '.scan(QUOTED_FRAGMENT)
|
||||
end
|
||||
|
||||
def test_variable_parser
|
||||
assert_equal(['var'], 'var'.scan(VariableParser))
|
||||
assert_equal(['var', 'method'], 'var.method'.scan(VariableParser))
|
||||
assert_equal(['var', '[method]'], 'var[method]'.scan(VariableParser))
|
||||
assert_equal(['var', '[method]', '[0]'], 'var[method][0]'.scan(VariableParser))
|
||||
assert_equal(['var', '["method"]', '[0]'], 'var["method"][0]'.scan(VariableParser))
|
||||
assert_equal(['var', '[method]', '[0]', 'method'], 'var[method][0].method'.scan(VariableParser))
|
||||
assert_equal ['var'], 'var'.scan(VARIABLE_PARSER)
|
||||
assert_equal ['var', 'method'], 'var.method'.scan(VARIABLE_PARSER)
|
||||
assert_equal ['var', '[method]'], 'var[method]'.scan(VARIABLE_PARSER)
|
||||
assert_equal ['var', '[method]', '[0]'], 'var[method][0]'.scan(VARIABLE_PARSER)
|
||||
assert_equal ['var', '["method"]', '[0]'], 'var["method"][0]'.scan(VARIABLE_PARSER)
|
||||
assert_equal ['var', '[method]', '[0]', 'method'], 'var[method][0].method'.scan(VARIABLE_PARSER)
|
||||
end
|
||||
end # RegexpTest
|
||||
|
||||
Reference in New Issue
Block a user