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