mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 16:25:42 +03:00
Compare commits
4 Commits
fix-consta
...
template-f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8f31046a9 | ||
|
|
6c6382ed69 | ||
|
|
53ba1372f9 | ||
|
|
57c9cf64eb |
@@ -21,6 +21,25 @@ 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:
|
||||
|
||||
@@ -15,6 +15,7 @@ matrix:
|
||||
name: Profiling Memory Usage
|
||||
allow_failures:
|
||||
- rvm: ruby-head
|
||||
- rvm: 2.7
|
||||
|
||||
branches:
|
||||
only:
|
||||
|
||||
@@ -22,25 +22,25 @@
|
||||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
module Liquid
|
||||
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
|
||||
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
|
||||
|
||||
singleton_class.send(:attr_accessor, :cache_classes)
|
||||
self.cache_classes = true
|
||||
@@ -80,10 +80,9 @@ require 'liquid/partial_cache'
|
||||
require 'liquid/usage'
|
||||
require 'liquid/register'
|
||||
require 'liquid/static_registers'
|
||||
require 'liquid/template_factory'
|
||||
|
||||
# Load all the tags of the standard library
|
||||
#
|
||||
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
|
||||
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 = "{{"
|
||||
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 = "{{"
|
||||
|
||||
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 =~ WHITESPACE_OR_NOTHING
|
||||
unless token =~ LIQUID_TAG_TOKEN
|
||||
unless token.empty? || token =~ WhitespaceOrNothing
|
||||
unless token =~ LiquidTagToken
|
||||
# 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?(TAG_START_STRING)
|
||||
when token.start_with?(TAGSTART)
|
||||
whitespace_handler(token, parse_context)
|
||||
unless token =~ FULL_TOKEN
|
||||
unless token =~ FullToken
|
||||
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?(VAR_START_STRING)
|
||||
when token.start_with?(VARSTART)
|
||||
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 =~ WHITESPACE_OR_NOTHING)
|
||||
@blank &&= !!(token =~ WhitespaceOrNothing)
|
||||
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] == WHITESPACE_CONTROL
|
||||
if token[2] == WhitespaceControl
|
||||
previous_token = @nodelist.last
|
||||
if previous_token.is_a?(String)
|
||||
previous_token.rstrip!
|
||||
end
|
||||
end
|
||||
parse_context.trim_whitespace = (token[-3] == WHITESPACE_CONTROL)
|
||||
parse_context.trim_whitespace = (token[-3] == WhitespaceControl)
|
||||
end
|
||||
|
||||
def blank?
|
||||
@@ -180,7 +180,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def create_variable(token, parse_context)
|
||||
token.scan(CONTENT_OF_VARIABLE) do |content|
|
||||
token.scan(ContentOfVariable) 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: TAG_END.inspect)
|
||||
raise SyntaxError, parse_context.locale.t("errors.syntax.tag_termination", token: token, tag_end: TagEnd.inspect)
|
||||
end
|
||||
|
||||
def raise_missing_variable_terminator(token, parse_context)
|
||||
raise SyntaxError, parse_context.locale.t("errors.syntax.variable_termination", token: token, tag_end: VARIABLE_END.inspect)
|
||||
raise SyntaxError, parse_context.locale.t("errors.syntax.variable_termination", token: token, tag_end: VariableEnd.inspect)
|
||||
end
|
||||
|
||||
def registered_tags
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
# 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
|
||||
@@ -12,7 +12,10 @@ module Liquid
|
||||
|
||||
parse_context.partial = true
|
||||
|
||||
partial = Liquid::Template.parse(source, parse_context)
|
||||
template_factory = (context.registers[:template_factory] ||= Liquid::TemplateFactory.new)
|
||||
template = template_factory.for(template_name)
|
||||
|
||||
partial = template.parse(source, parse_context)
|
||||
cached_partials[template_name] = partial
|
||||
ensure
|
||||
parse_context.partial = false
|
||||
|
||||
@@ -10,7 +10,7 @@ module Liquid
|
||||
# {{ foo }}
|
||||
#
|
||||
class Assign < Tag
|
||||
SYNTAX = /(#{VARIABLE_SIGNATURE}+)\s*=\s*(.*)\s*/om
|
||||
Syntax = /(#{VariableSignature}+)\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 = /(#{VARIABLE_SIGNATURE}+)/o
|
||||
Syntax = /(#{VariableSignature}+)/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 = /(#{QUOTED_FRAGMENT})/o
|
||||
WHEN_SYNTAX = /(#{QUOTED_FRAGMENT})(?:(?:\s+or\s+|\s*\,\s*)(#{QUOTED_FRAGMENT}.*))?/om
|
||||
Syntax = /(#{QuotedFragment})/o
|
||||
WhenSyntax = /(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/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 =~ WHEN_SYNTAX
|
||||
unless markup =~ WhenSyntax
|
||||
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
|
||||
SIMPLE_SYNTAX = /\A#{QUOTED_FRAGMENT}+/o
|
||||
NAMED_SYNTAX = /\A(#{QUOTED_FRAGMENT})\s*\:\s*(.*)/om
|
||||
SimpleSyntax = /\A#{QuotedFragment}+/o
|
||||
NamedSyntax = /\A(#{QuotedFragment})\s*\:\s*(.*)/om
|
||||
|
||||
attr_reader :variables
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
case markup
|
||||
when NAMED_SYNTAX
|
||||
when NamedSyntax
|
||||
@variables = variables_from_string(Regexp.last_match(2))
|
||||
@name = Expression.parse(Regexp.last_match(1))
|
||||
when SIMPLE_SYNTAX
|
||||
when SimpleSyntax
|
||||
@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*(#{QUOTED_FRAGMENT})\s*/o
|
||||
var =~ /\s*(#{QuotedFragment})\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(#{VARIABLE_SEGMENT}+)\s+in\s+(#{QUOTED_FRAGMENT}+)\s*(reversed)?/o
|
||||
Syntax = /\A(#{VariableSegment}+)\s+in\s+(#{QuotedFragment}+)\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(TAG_ATTRIBUTES) do |key, value|
|
||||
markup.scan(TagAttributes) 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 = /(#{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
|
||||
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
|
||||
|
||||
attr_reader :blocks
|
||||
|
||||
@@ -65,15 +65,15 @@ module Liquid
|
||||
end
|
||||
|
||||
def lax_parse(markup)
|
||||
expressions = markup.scan(EXPRESSIONS_AND_OPERATORS)
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.if") unless expressions.pop =~ SYNTAX
|
||||
expressions = markup.scan(ExpressionsAndOperators)
|
||||
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,6 +17,7 @@ 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
|
||||
|
||||
@@ -33,7 +34,7 @@ module Liquid
|
||||
@template_name_expr = Expression.parse(template_name)
|
||||
@attributes = {}
|
||||
|
||||
markup.scan(TAG_ATTRIBUTES) do |key, value|
|
||||
markup.scan(TagAttributes) do |key, value|
|
||||
@attributes[key] = Expression.parse(value)
|
||||
end
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
module Liquid
|
||||
class Raw < Block
|
||||
SYNTAX = /\A\s*\z/
|
||||
FULL_TOKEN_POSSIBLY_INVALID = /\A(.*)#{TAG_START}\s*(\w+)\s*(.*)?#{TAG_END}\z/om
|
||||
Syntax = /\A\s*\z/
|
||||
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\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 =~ FULL_TOKEN_POSSIBLY_INVALID
|
||||
if token =~ FullTokenPossiblyInvalid
|
||||
@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
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
module Liquid
|
||||
class Render < Tag
|
||||
SYNTAX = /(#{QuotedString}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||
FOR = 'for'
|
||||
SYNTAX = /(#{QuotedString}+)(\s+(with|#{FOR})\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||
|
||||
disable_tags "include"
|
||||
|
||||
@@ -14,14 +15,16 @@ module Liquid
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
|
||||
|
||||
template_name = Regexp.last_match(1)
|
||||
variable_name = Regexp.last_match(3)
|
||||
with_or_for = Regexp.last_match(3)
|
||||
variable_name = Regexp.last_match(4)
|
||||
|
||||
@alias_name = Regexp.last_match(5)
|
||||
@alias_name = Regexp.last_match(6)
|
||||
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
|
||||
@template_name_expr = Expression.parse(template_name)
|
||||
@for = (with_or_for == FOR)
|
||||
|
||||
@attributes = {}
|
||||
markup.scan(TAG_ATTRIBUTES) do |key, value|
|
||||
markup.scan(TagAttributes) do |key, value|
|
||||
@attributes[key] = Expression.parse(value)
|
||||
end
|
||||
end
|
||||
@@ -58,7 +61,7 @@ module Liquid
|
||||
}
|
||||
|
||||
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
||||
if variable.is_a?(Array)
|
||||
if @for && variable.respond_to?(:each) && variable.respond_to?(:count)
|
||||
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
|
||||
variable.each { |var| render_partial_func.call(var, forloop) }
|
||||
else
|
||||
|
||||
@@ -2,17 +2,17 @@
|
||||
|
||||
module Liquid
|
||||
class TableRow < Block
|
||||
SYNTAX = /(\w+)\s+in\s+(#{QUOTED_FRAGMENT}+)/o
|
||||
Syntax = /(\w+)\s+in\s+(#{QuotedFragment}+)/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(TAG_ATTRIBUTES) do |key, value|
|
||||
markup.scan(TagAttributes) do |key, value|
|
||||
@attributes[key] = Expression.parse(value)
|
||||
end
|
||||
else
|
||||
|
||||
9
lib/liquid/template_factory.rb
Normal file
9
lib/liquid/template_factory.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
class TemplateFactory
|
||||
def for(_template_name)
|
||||
Liquid::Template.new
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -28,7 +28,7 @@ module Liquid
|
||||
|
||||
return @source.split("\n") if @for_liquid_tag
|
||||
|
||||
tokens = @source.split(TEMPLATE_PARSER)
|
||||
tokens = @source.split(TemplateParser)
|
||||
|
||||
# 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
|
||||
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
|
||||
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
|
||||
|
||||
attr_accessor :filters, :name, :line_number
|
||||
attr_reader :parse_context
|
||||
@@ -43,17 +43,17 @@ module Liquid
|
||||
|
||||
def lax_parse(markup)
|
||||
@filters = []
|
||||
return unless markup =~ MARKUP_WITH_QUOTED_FRAGMENT
|
||||
return unless markup =~ MarkupWithQuotedFragment
|
||||
|
||||
name_markup = Regexp.last_match(1)
|
||||
filter_markup = Regexp.last_match(2)
|
||||
@name = Expression.parse(name_markup)
|
||||
if filter_markup =~ FILTER_MARKUP_REGEX
|
||||
filters = Regexp.last_match(1).scan(FILTER_PARSER)
|
||||
if filter_markup =~ FilterMarkupRegex
|
||||
filters = Regexp.last_match(1).scan(FilterParser)
|
||||
filters.each do |f|
|
||||
next unless f =~ /\w+/
|
||||
filtername = Regexp.last_match(0)
|
||||
filterargs = f.scan(FILTER_ARGS_REGEX).flatten
|
||||
filterargs = f.scan(FilterArgsRegex).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(JUST_TAG_ATTRIBUTES))
|
||||
if (matches = a.match(JustTagAttributes))
|
||||
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 =~ QUOTED_FRAGMENT
|
||||
@markup =~ QuotedFragment
|
||||
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(VARIABLE_PARSER)
|
||||
lookups = markup.scan(VariableParser)
|
||||
|
||||
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::QUOTED_FRAGMENT})\s*(by\s*(\d+))?/
|
||||
Syntax = /(#{Liquid::QuotedFragment})\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::TAG_ATTRIBUTES) do |key, value|
|
||||
markup.scan(Liquid::TagAttributes) do |key, value|
|
||||
@attributes[key] = value
|
||||
end
|
||||
else
|
||||
|
||||
@@ -60,10 +60,10 @@ class CountingFileSystem
|
||||
end
|
||||
|
||||
class CustomInclude < Liquid::Tag
|
||||
SYNTAX = /(#{Liquid::QUOTED_FRAGMENT}+)(\s+(?:with|for)\s+(#{Liquid::QUOTED_FRAGMENT}+))?/o
|
||||
Syntax = /(#{Liquid::QuotedFragment}+)(\s+(?:with|for)\s+(#{Liquid::QuotedFragment}+))?/o
|
||||
|
||||
def initialize(tag_name, markup, tokens)
|
||||
markup =~ SYNTAX
|
||||
markup =~ Syntax
|
||||
@template_name = Regexp.last_match(1)
|
||||
super
|
||||
end
|
||||
|
||||
@@ -214,4 +214,22 @@ class RenderTagTest < Minitest::Test
|
||||
assert_template_result("Product: Draft 151cm first index:1 Product: Element 155cm last index:2 ",
|
||||
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||
end
|
||||
|
||||
def test_render_tag_for_drop
|
||||
Liquid::Template.file_system = StubFileSystem.new(
|
||||
'loop' => "{{ value.foo }}",
|
||||
)
|
||||
|
||||
assert_template_result("123",
|
||||
"{% render 'loop' for loop as value %}", "loop" => TestEnumerable.new)
|
||||
end
|
||||
|
||||
def test_render_tag_with_drop
|
||||
Liquid::Template.file_system = StubFileSystem.new(
|
||||
'loop' => "{{ value }}",
|
||||
)
|
||||
|
||||
assert_template_result("TestEnumerable",
|
||||
"{% render 'loop' with loop as value %}", "loop" => TestEnumerable.new)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -136,3 +136,16 @@ class StubFileSystem
|
||||
@values.fetch(template_path)
|
||||
end
|
||||
end
|
||||
|
||||
class StubTemplateFactory
|
||||
attr_reader :count
|
||||
|
||||
def initialize
|
||||
@count = 0
|
||||
end
|
||||
|
||||
def for(_template_name)
|
||||
@count += 1
|
||||
Liquid::Template.new
|
||||
end
|
||||
end
|
||||
|
||||
@@ -90,4 +90,39 @@ class PartialCacheUnitTest < Minitest::Test
|
||||
# but measuring file reads is an OK proxy for this.
|
||||
assert_equal(1, file_system.file_read_count)
|
||||
end
|
||||
|
||||
def test_uses_default_template_factory_when_no_template_factory_found_in_register
|
||||
context = Liquid::Context.build(
|
||||
registers: {
|
||||
file_system: StubFileSystem.new('my_partial' => 'my partial body'),
|
||||
}
|
||||
)
|
||||
|
||||
partial = Liquid::PartialCache.load(
|
||||
'my_partial',
|
||||
context: context,
|
||||
parse_context: Liquid::ParseContext.new
|
||||
)
|
||||
|
||||
assert_equal('my partial body', partial.render)
|
||||
end
|
||||
|
||||
def test_uses_template_factory_register_if_present
|
||||
template_factory = StubTemplateFactory.new
|
||||
context = Liquid::Context.build(
|
||||
registers: {
|
||||
file_system: StubFileSystem.new('my_partial' => 'my partial body'),
|
||||
template_factory: template_factory,
|
||||
}
|
||||
)
|
||||
|
||||
partial = Liquid::PartialCache.load(
|
||||
'my_partial',
|
||||
context: context,
|
||||
parse_context: Liquid::ParseContext.new
|
||||
)
|
||||
|
||||
assert_equal('my partial body', partial.render)
|
||||
assert_equal(1, template_factory.count)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,41 +6,41 @@ class RegexpUnitTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_empty
|
||||
assert_equal [], ''.scan(QUOTED_FRAGMENT)
|
||||
assert_equal([], ''.scan(QuotedFragment))
|
||||
end
|
||||
|
||||
def test_quote
|
||||
assert_equal ['"arg 1"'], '"arg 1"'.scan(QUOTED_FRAGMENT)
|
||||
assert_equal(['"arg 1"'], '"arg 1"'.scan(QuotedFragment))
|
||||
end
|
||||
|
||||
def test_words
|
||||
assert_equal ['arg1', 'arg2'], 'arg1 arg2'.scan(QUOTED_FRAGMENT)
|
||||
assert_equal(['arg1', 'arg2'], 'arg1 arg2'.scan(QuotedFragment))
|
||||
end
|
||||
|
||||
def test_tags
|
||||
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)
|
||||
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))
|
||||
end
|
||||
|
||||
def test_double_quoted_words
|
||||
assert_equal ['arg1', 'arg2', '"arg 3"'], 'arg1 arg2 "arg 3"'.scan(QUOTED_FRAGMENT)
|
||||
assert_equal(['arg1', 'arg2', '"arg 3"'], 'arg1 arg2 "arg 3"'.scan(QuotedFragment))
|
||||
end
|
||||
|
||||
def test_single_quoted_words
|
||||
assert_equal ['arg1', 'arg2', "'arg 3'"], 'arg1 arg2 \'arg 3\''.scan(QUOTED_FRAGMENT)
|
||||
assert_equal(['arg1', 'arg2', "'arg 3'"], 'arg1 arg2 \'arg 3\''.scan(QuotedFragment))
|
||||
end
|
||||
|
||||
def test_quoted_words_in_the_middle
|
||||
assert_equal ['arg1', 'arg2', '"arg 3"', 'arg4'], 'arg1 arg2 "arg 3" arg4 '.scan(QUOTED_FRAGMENT)
|
||||
assert_equal(['arg1', 'arg2', '"arg 3"', 'arg4'], 'arg1 arg2 "arg 3" arg4 '.scan(QuotedFragment))
|
||||
end
|
||||
|
||||
def test_variable_parser
|
||||
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)
|
||||
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))
|
||||
end
|
||||
end # RegexpTest
|
||||
|
||||
12
test/unit/template_factory_unit_test.rb
Normal file
12
test/unit/template_factory_unit_test.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
|
||||
class TemplateFactoryUnitTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_for_returns_liquid_template_instance
|
||||
template = TemplateFactory.new.for("anything")
|
||||
assert_instance_of(Liquid::Template, template)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user