mirror of
https://github.com/kemko/liquid.git
synced 2026-01-06 18:25:41 +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:
|
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:
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ matrix:
|
|||||||
name: Profiling Memory Usage
|
name: Profiling Memory Usage
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- rvm: ruby-head
|
- rvm: ruby-head
|
||||||
|
- rvm: 2.7
|
||||||
|
|
||||||
branches:
|
branches:
|
||||||
only:
|
only:
|
||||||
|
|||||||
@@ -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
|
||||||
FILTER_SEPARATOR = /\|/
|
FilterSeparator = /\|/
|
||||||
ARGUMENT_SEPARATOR = ','
|
ArgumentSeparator = ','
|
||||||
FILTER_ARGUMENT_SEPARATOR = ':'
|
FilterArgumentSeparator = ':'
|
||||||
VARIABLE_ATTRIBUTE_SEPARATOR = '.'
|
VariableAttributeSeparator = '.'
|
||||||
WHITESPACE_CONTROL = '-'
|
WhitespaceControl = '-'
|
||||||
TAG_START = /\{\%/
|
TagStart = /\{\%/
|
||||||
TAG_END = /\%\}/
|
TagEnd = /\%\}/
|
||||||
VARIABLE_SIGNATURE = /\(?[\w\-\.\[\]]\)?/
|
VariableSignature = /\(?[\w\-\.\[\]]\)?/
|
||||||
VARIABLE_SEGMENT = /[\w\-]/
|
VariableSegment = /[\w\-]/
|
||||||
VARIABLE_START = /\{\{/
|
VariableStart = /\{\{/
|
||||||
VARIABLE_END = /\}\}/
|
VariableEnd = /\}\}/
|
||||||
VARIABLE_INCOMPLETE_END = /\}\}?/
|
VariableIncompleteEnd = /\}\}?/
|
||||||
QUOTED_STRING = /"[^"]*"|'[^']*'/
|
QuotedString = /"[^"]*"|'[^']*'/
|
||||||
QUOTED_FRAGMENT = /#{QUOTED_STRING}|(?:[^\s,\|'"]|#{QUOTED_STRING})+/o
|
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/o
|
||||||
TAG_ATTRIBUTES = /(\w+)\s*\:\s*(#{QUOTED_FRAGMENT})/o
|
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/o
|
||||||
ANY_STARTING_TAG = /#{TAG_START}|#{VARIABLE_START}/o
|
AnyStartingTag = /#{TagStart}|#{VariableStart}/o
|
||||||
PARTIAL_TEMPLATE_PARSER = /#{TAG_START}.*?#{TAG_END}|#{VARIABLE_START}.*?#{VARIABLE_INCOMPLETE_END}/om
|
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
|
||||||
TEMPLATE_PARSER = /(#{PARTIAL_TEMPLATE_PARSER}|#{ANY_STARTING_TAG})/om
|
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
|
||||||
VARIABLE_PARSER = /\[[^\]]+\]|#{VARIABLE_SEGMENT}+\??/o
|
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/o
|
||||||
|
|
||||||
singleton_class.send(:attr_accessor, :cache_classes)
|
singleton_class.send(:attr_accessor, :cache_classes)
|
||||||
self.cache_classes = true
|
self.cache_classes = true
|
||||||
@@ -80,10 +80,9 @@ require 'liquid/partial_cache'
|
|||||||
require 'liquid/usage'
|
require 'liquid/usage'
|
||||||
require 'liquid/register'
|
require 'liquid/register'
|
||||||
require 'liquid/static_registers'
|
require 'liquid/static_registers'
|
||||||
|
require 'liquid/template_factory'
|
||||||
|
|
||||||
# Load all the tags of the standard library
|
# Load all the tags of the standard library
|
||||||
#
|
#
|
||||||
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
|
||||||
LIQUID_TAG_TOKEN = /\A\s*(\w+)\s*(.*?)\z/o
|
LiquidTagToken = /\A\s*(\w+)\s*(.*?)\z/o
|
||||||
FULL_TOKEN = /\A#{TAG_START}#{WHITESPACE_CONTROL}?(\s*)(\w+)(\s*)(.*?)#{WHITESPACE_CONTROL}?#{TAG_END}\z/om
|
FullToken = /\A#{TagStart}#{WhitespaceControl}?(\s*)(\w+)(\s*)(.*?)#{WhitespaceControl}?#{TagEnd}\z/om
|
||||||
CONTENT_OF_VARIABLE = /\A#{VARIABLE_START}#{WHITESPACE_CONTROL}?(.*?)#{WHITESPACE_CONTROL}?#{VARIABLE_END}\z/om
|
ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
|
||||||
WHITESPACE_OR_NOTHING = /\A\s*\z/
|
WhitespaceOrNothing = /\A\s*\z/
|
||||||
TAG_START_STRING = "{%"
|
TAGSTART = "{%"
|
||||||
VAR_START_STRING = "{{"
|
VARSTART = "{{"
|
||||||
|
|
||||||
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 =~ WHITESPACE_OR_NOTHING
|
unless token.empty? || token =~ WhitespaceOrNothing
|
||||||
unless token =~ LIQUID_TAG_TOKEN
|
unless token =~ LiquidTagToken
|
||||||
# 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?(TAG_START_STRING)
|
when token.start_with?(TAGSTART)
|
||||||
whitespace_handler(token, parse_context)
|
whitespace_handler(token, parse_context)
|
||||||
unless token =~ FULL_TOKEN
|
unless token =~ FullToken
|
||||||
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?(VAR_START_STRING)
|
when token.start_with?(VARSTART)
|
||||||
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 =~ WHITESPACE_OR_NOTHING)
|
@blank &&= !!(token =~ WhitespaceOrNothing)
|
||||||
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] == WHITESPACE_CONTROL
|
if token[2] == WhitespaceControl
|
||||||
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] == WHITESPACE_CONTROL)
|
parse_context.trim_whitespace = (token[-3] == WhitespaceControl)
|
||||||
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(CONTENT_OF_VARIABLE) do |content|
|
token.scan(ContentOfVariable) 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: TAG_END.inspect)
|
raise SyntaxError, parse_context.locale.t("errors.syntax.tag_termination", token: token, tag_end: TagEnd.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: VARIABLE_END.inspect)
|
raise SyntaxError, parse_context.locale.t("errors.syntax.variable_termination", token: token, tag_end: VariableEnd.inspect)
|
||||||
end
|
end
|
||||||
|
|
||||||
def registered_tags
|
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
|
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
|
cached_partials[template_name] = partial
|
||||||
ensure
|
ensure
|
||||||
parse_context.partial = false
|
parse_context.partial = false
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ module Liquid
|
|||||||
# {{ foo }}
|
# {{ foo }}
|
||||||
#
|
#
|
||||||
class Assign < Tag
|
class Assign < Tag
|
||||||
SYNTAX = /(#{VARIABLE_SIGNATURE}+)\s*=\s*(.*)\s*/om
|
Syntax = /(#{VariableSignature}+)\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 = /(#{VARIABLE_SIGNATURE}+)/o
|
Syntax = /(#{VariableSignature}+)/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 = /(#{QUOTED_FRAGMENT})/o
|
Syntax = /(#{QuotedFragment})/o
|
||||||
WHEN_SYNTAX = /(#{QUOTED_FRAGMENT})(?:(?:\s+or\s+|\s*\,\s*)(#{QUOTED_FRAGMENT}.*))?/om
|
WhenSyntax = /(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/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 =~ WHEN_SYNTAX
|
unless markup =~ WhenSyntax
|
||||||
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
|
||||||
SIMPLE_SYNTAX = /\A#{QUOTED_FRAGMENT}+/o
|
SimpleSyntax = /\A#{QuotedFragment}+/o
|
||||||
NAMED_SYNTAX = /\A(#{QUOTED_FRAGMENT})\s*\:\s*(.*)/om
|
NamedSyntax = /\A(#{QuotedFragment})\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 NAMED_SYNTAX
|
when NamedSyntax
|
||||||
@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 SIMPLE_SYNTAX
|
when SimpleSyntax
|
||||||
@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*(#{QUOTED_FRAGMENT})\s*/o
|
var =~ /\s*(#{QuotedFragment})\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(#{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
|
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(TAG_ATTRIBUTES) do |key, value|
|
markup.scan(TagAttributes) 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 = /(#{QUOTED_FRAGMENT})\s*([=!<>a-z_]+)?\s*(#{QUOTED_FRAGMENT})?/o
|
Syntax = /(#{QuotedFragment})\s*([=!<>a-z_]+)?\s*(#{QuotedFragment})?/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
|
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
|
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(EXPRESSIONS_AND_OPERATORS)
|
expressions = markup.scan(ExpressionsAndOperators)
|
||||||
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,6 +17,7 @@ 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
|
||||||
|
|
||||||
@@ -33,7 +34,7 @@ module Liquid
|
|||||||
@template_name_expr = Expression.parse(template_name)
|
@template_name_expr = Expression.parse(template_name)
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
|
|
||||||
markup.scan(TAG_ATTRIBUTES) do |key, value|
|
markup.scan(TagAttributes) 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/
|
||||||
FULL_TOKEN_POSSIBLY_INVALID = /\A(.*)#{TAG_START}\s*(\w+)\s*(.*)?#{TAG_END}\z/om
|
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\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 =~ FULL_TOKEN_POSSIBLY_INVALID
|
if token =~ FullTokenPossiblyInvalid
|
||||||
@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
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
class Render < Tag
|
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"
|
disable_tags "include"
|
||||||
|
|
||||||
@@ -14,14 +15,16 @@ module Liquid
|
|||||||
raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
|
raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
|
||||||
|
|
||||||
template_name = Regexp.last_match(1)
|
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
|
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
|
||||||
@template_name_expr = Expression.parse(template_name)
|
@template_name_expr = Expression.parse(template_name)
|
||||||
|
@for = (with_or_for == FOR)
|
||||||
|
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
markup.scan(TAG_ATTRIBUTES) do |key, value|
|
markup.scan(TagAttributes) do |key, value|
|
||||||
@attributes[key] = Expression.parse(value)
|
@attributes[key] = Expression.parse(value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -58,7 +61,7 @@ module Liquid
|
|||||||
}
|
}
|
||||||
|
|
||||||
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
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)
|
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
|
||||||
variable.each { |var| render_partial_func.call(var, forloop) }
|
variable.each { |var| render_partial_func.call(var, forloop) }
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -2,17 +2,17 @@
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
class TableRow < Block
|
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
|
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(TAG_ATTRIBUTES) do |key, value|
|
markup.scan(TagAttributes) do |key, value|
|
||||||
@attributes[key] = Expression.parse(value)
|
@attributes[key] = Expression.parse(value)
|
||||||
end
|
end
|
||||||
else
|
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
|
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
|
# 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
|
||||||
FILTER_MARKUP_REGEX = /#{FILTER_SEPARATOR}\s*(.*)/om
|
FilterMarkupRegex = /#{FilterSeparator}\s*(.*)/om
|
||||||
FILTER_PARSER = /(?:\s+|#{QUOTED_FRAGMENT}|#{ARGUMENT_SEPARATOR})+/o
|
FilterParser = /(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o
|
||||||
FILTER_ARGS_REGEX . = /(?:#{FILTER_ARGUMENT_SEPARATOR}|#{ARGUMENT_SEPARATOR})\s*((?:\w+\s*\:\s*)?#{QUOTED_FRAGMENT})/o
|
FilterArgsRegex = /(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o
|
||||||
JUST_TAG_ATTRIBUTES = /\A#{TAG_ATTRIBUTES}\z/o
|
JustTagAttributes = /\A#{TagAttributes}\z/o
|
||||||
MARKUP_WITH_QUOTED_FRAGMENT = /(#{QUOTED_FRAGMENT})(.*)/om
|
MarkupWithQuotedFragment = /(#{QuotedFragment})(.*)/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 =~ MARKUP_WITH_QUOTED_FRAGMENT
|
return unless markup =~ MarkupWithQuotedFragment
|
||||||
|
|
||||||
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 =~ FILTER_MARKUP_REGEX
|
if filter_markup =~ FilterMarkupRegex
|
||||||
filters = Regexp.last_match(1).scan(FILTER_PARSER)
|
filters = Regexp.last_match(1).scan(FilterParser)
|
||||||
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(FILTER_ARGS_REGEX).flatten
|
filterargs = f.scan(FilterArgsRegex).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(JUST_TAG_ATTRIBUTES))
|
if (matches = a.match(JustTagAttributes))
|
||||||
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 =~ QUOTED_FRAGMENT
|
@markup =~ QuotedFragment
|
||||||
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(VARIABLE_PARSER)
|
lookups = markup.scan(VariableParser)
|
||||||
|
|
||||||
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::QUOTED_FRAGMENT})\s*(by\s*(\d+))?/
|
Syntax = /(#{Liquid::QuotedFragment})\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::TAG_ATTRIBUTES) do |key, value|
|
markup.scan(Liquid::TagAttributes) 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::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)
|
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
|
||||||
|
|||||||
@@ -214,4 +214,22 @@ class RenderTagTest < Minitest::Test
|
|||||||
assert_template_result("Product: Draft 151cm first index:1 Product: Element 155cm last index:2 ",
|
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' }])
|
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -136,3 +136,16 @@ class StubFileSystem
|
|||||||
@values.fetch(template_path)
|
@values.fetch(template_path)
|
||||||
end
|
end
|
||||||
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.
|
# but measuring file reads is an OK proxy for this.
|
||||||
assert_equal(1, file_system.file_read_count)
|
assert_equal(1, file_system.file_read_count)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -6,41 +6,41 @@ class RegexpUnitTest < Minitest::Test
|
|||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
def test_empty
|
def test_empty
|
||||||
assert_equal [], ''.scan(QUOTED_FRAGMENT)
|
assert_equal([], ''.scan(QuotedFragment))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_quote
|
def test_quote
|
||||||
assert_equal ['"arg 1"'], '"arg 1"'.scan(QUOTED_FRAGMENT)
|
assert_equal(['"arg 1"'], '"arg 1"'.scan(QuotedFragment))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_words
|
def test_words
|
||||||
assert_equal ['arg1', 'arg2'], 'arg1 arg2'.scan(QUOTED_FRAGMENT)
|
assert_equal(['arg1', 'arg2'], 'arg1 arg2'.scan(QuotedFragment))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_tags
|
def test_tags
|
||||||
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(['<tr></tr>'], '<tr></tr>'.scan(QuotedFragment))
|
||||||
assert_equal ['<style', 'class="hello">', '</style>'], %(<style class="hello">' </style>).scan(QUOTED_FRAGMENT)
|
assert_equal(['<style', 'class="hello">', '</style>'], %(<style class="hello">' </style>).scan(QuotedFragment))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_double_quoted_words
|
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
|
end
|
||||||
|
|
||||||
def test_single_quoted_words
|
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
|
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(QUOTED_FRAGMENT)
|
assert_equal(['arg1', 'arg2', '"arg 3"', 'arg4'], 'arg1 arg2 "arg 3" arg4 '.scan(QuotedFragment))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_variable_parser
|
def test_variable_parser
|
||||||
assert_equal ['var'], 'var'.scan(VARIABLE_PARSER)
|
assert_equal(['var'], 'var'.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]'], 'var[method]'.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]'], 'var["method"][0]'.scan(VariableParser))
|
||||||
assert_equal ['var', '[method]', '[0]', 'method'], 'var[method][0].method'.scan(VARIABLE_PARSER)
|
assert_equal(['var', '[method]', '[0]', 'method'], 'var[method][0].method'.scan(VariableParser))
|
||||||
end
|
end
|
||||||
end # RegexpTest
|
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