mirror of
https://github.com/kemko/liquid.git
synced 2026-01-08 03:05:42 +03:00
Compare commits
7 Commits
fix-consta
...
render-wit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9973a2ec96 | ||
|
|
545312de6d | ||
|
|
60b4182b7e | ||
|
|
efaec29fa6 | ||
|
|
e6a6d2e813 | ||
|
|
25019e68d1 | ||
|
|
ae809b6290 |
@@ -892,7 +892,7 @@ Lint/FormatParameterMismatch:
|
|||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
Lint/HandleExceptions:
|
Lint/HandleExceptions:
|
||||||
AllowComments: true
|
Enabled: true
|
||||||
|
|
||||||
Lint/ImplicitStringConcatenation:
|
Lint/ImplicitStringConcatenation:
|
||||||
Description: Checks for adjacent string literals on the same line, which could
|
Description: Checks for adjacent string literals on the same line, which could
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -85,5 +85,3 @@ 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
|
||||||
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
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ module Liquid
|
|||||||
@left = left
|
@left = left
|
||||||
@operator = operator
|
@operator = operator
|
||||||
@right = right
|
@right = right
|
||||||
|
|
||||||
@child_relation = nil
|
@child_relation = nil
|
||||||
@child_condition = nil
|
@child_condition = nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -7,10 +7,8 @@ module Liquid
|
|||||||
|
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@template_options = options ? options.dup : {}
|
@template_options = options ? options.dup : {}
|
||||||
|
|
||||||
@locale = @template_options[:locale] ||= I18n.new
|
@locale = @template_options[:locale] ||= I18n.new
|
||||||
@warnings = []
|
@warnings = []
|
||||||
|
|
||||||
self.depth = 0
|
self.depth = 0
|
||||||
self.partial = false
|
self.partial = false
|
||||||
end
|
end
|
||||||
@@ -22,7 +20,6 @@ module Liquid
|
|||||||
def partial=(value)
|
def partial=(value)
|
||||||
@partial = value
|
@partial = value
|
||||||
@options = value ? partial_options : @template_options
|
@options = value ? partial_options : @template_options
|
||||||
|
|
||||||
@error_mode = @options[:error_mode] || Template.error_mode
|
@error_mode = @options[:error_mode] || Template.error_mode
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ module Liquid
|
|||||||
|
|
||||||
file_system = (context.registers[:file_system] ||= Liquid::Template.file_system)
|
file_system = (context.registers[:file_system] ||= Liquid::Template.file_system)
|
||||||
source = file_system.read_template_file(template_name)
|
source = file_system.read_template_file(template_name)
|
||||||
|
|
||||||
parse_context.partial = true
|
parse_context.partial = true
|
||||||
|
|
||||||
partial = Liquid::Template.parse(source, parse_context)
|
partial = Liquid::Template.parse(source, parse_context)
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ module Liquid
|
|||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
class Timing
|
class Timing
|
||||||
attr_reader :code, :partial, :line_number, :children, :total_time, :self_time
|
attr_reader :code, :partial, :line_number, :children
|
||||||
|
|
||||||
def initialize(node, partial)
|
def initialize(node, partial)
|
||||||
@code = node.respond_to?(:raw) ? node.raw : node
|
@code = node.respond_to?(:raw) ? node.raw : node
|
||||||
@@ -65,17 +65,6 @@ module Liquid
|
|||||||
|
|
||||||
def finish
|
def finish
|
||||||
@end_time = Time.now
|
@end_time = Time.now
|
||||||
@total_time = @end_time - @start_time
|
|
||||||
|
|
||||||
if @children.empty?
|
|
||||||
@self_time = @total_time
|
|
||||||
else
|
|
||||||
total_children_time = 0
|
|
||||||
@children.each do |child|
|
|
||||||
total_children_time += child.total_time
|
|
||||||
end
|
|
||||||
@self_time = @total_time - total_children_time
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_time
|
def render_time
|
||||||
@@ -109,8 +98,8 @@ module Liquid
|
|||||||
Thread.current[:liquid_profiler]
|
Thread.current[:liquid_profiler]
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(partial_name = "<root>")
|
def initialize
|
||||||
@partial_stack = [partial_name]
|
@partial_stack = ["<root>"]
|
||||||
|
|
||||||
@root_timing = Timing.new("", current_partial)
|
@root_timing = Timing.new("", current_partial)
|
||||||
@timing_stack = [@root_timing]
|
@timing_stack = [@root_timing]
|
||||||
|
|||||||
@@ -78,12 +78,9 @@ module Liquid
|
|||||||
return if input.nil?
|
return if input.nil?
|
||||||
input_str = input.to_s
|
input_str = input.to_s
|
||||||
length = Utils.to_integer(length)
|
length = Utils.to_integer(length)
|
||||||
|
|
||||||
truncate_string_str = truncate_string.to_s
|
truncate_string_str = truncate_string.to_s
|
||||||
|
|
||||||
l = length - truncate_string_str.length
|
l = length - truncate_string_str.length
|
||||||
l = 0 if l < 0
|
l = 0 if l < 0
|
||||||
|
|
||||||
input_str.length > length ? input_str[0...l].concat(truncate_string_str) : input_str
|
input_str.length > length ? input_str[0...l].concat(truncate_string_str) : input_str
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -91,10 +88,8 @@ module Liquid
|
|||||||
return if input.nil?
|
return if input.nil?
|
||||||
wordlist = input.to_s.split
|
wordlist = input.to_s.split
|
||||||
words = Utils.to_integer(words)
|
words = Utils.to_integer(words)
|
||||||
|
|
||||||
l = words - 1
|
l = words - 1
|
||||||
l = 0 if l < 0
|
l = 0 if l < 0
|
||||||
|
|
||||||
wordlist.length > l ? wordlist[0..l].join(" ").concat(truncate_string.to_s) : input
|
wordlist.length > l ? wordlist[0..l].join(" ").concat(truncate_string.to_s) : input
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -133,13 +128,13 @@ module Liquid
|
|||||||
|
|
||||||
# Join elements of the array with certain character between them
|
# Join elements of the array with certain character between them
|
||||||
def join(input, glue = ' ')
|
def join(input, glue = ' ')
|
||||||
InputIterator.new(input, context).join(glue)
|
InputIterator.new(input).join(glue)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sort elements of the array
|
# Sort elements of the array
|
||||||
# provide optional property with which to sort an array of hashes or drops
|
# provide optional property with which to sort an array of hashes or drops
|
||||||
def sort(input, property = nil)
|
def sort(input, property = nil)
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input)
|
||||||
|
|
||||||
return [] if ary.empty?
|
return [] if ary.empty?
|
||||||
|
|
||||||
@@ -159,7 +154,7 @@ module Liquid
|
|||||||
# Sort elements of an array ignoring case if strings
|
# Sort elements of an array ignoring case if strings
|
||||||
# provide optional property with which to sort an array of hashes or drops
|
# provide optional property with which to sort an array of hashes or drops
|
||||||
def sort_natural(input, property = nil)
|
def sort_natural(input, property = nil)
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input)
|
||||||
|
|
||||||
return [] if ary.empty?
|
return [] if ary.empty?
|
||||||
|
|
||||||
@@ -179,7 +174,7 @@ module Liquid
|
|||||||
# Filter the elements of an array to those with a certain property value.
|
# Filter the elements of an array to those with a certain property value.
|
||||||
# By default the target is any truthy value.
|
# By default the target is any truthy value.
|
||||||
def where(input, property, target_value = nil)
|
def where(input, property, target_value = nil)
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input)
|
||||||
|
|
||||||
if ary.empty?
|
if ary.empty?
|
||||||
[]
|
[]
|
||||||
@@ -201,7 +196,7 @@ module Liquid
|
|||||||
# Remove duplicate elements from an array
|
# Remove duplicate elements from an array
|
||||||
# provide optional property with which to determine uniqueness
|
# provide optional property with which to determine uniqueness
|
||||||
def uniq(input, property = nil)
|
def uniq(input, property = nil)
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input)
|
||||||
|
|
||||||
if property.nil?
|
if property.nil?
|
||||||
ary.uniq
|
ary.uniq
|
||||||
@@ -218,13 +213,13 @@ module Liquid
|
|||||||
|
|
||||||
# Reverse the elements of an array
|
# Reverse the elements of an array
|
||||||
def reverse(input)
|
def reverse(input)
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input)
|
||||||
ary.reverse
|
ary.reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
# map/collect on a given property
|
# map/collect on a given property
|
||||||
def map(input, property)
|
def map(input, property)
|
||||||
InputIterator.new(input, context).map do |e|
|
InputIterator.new(input).map do |e|
|
||||||
e = e.call if e.is_a?(Proc)
|
e = e.call if e.is_a?(Proc)
|
||||||
|
|
||||||
if property == "to_liquid"
|
if property == "to_liquid"
|
||||||
@@ -241,7 +236,7 @@ module Liquid
|
|||||||
# Remove nils within an array
|
# Remove nils within an array
|
||||||
# provide optional property with which to check for nil
|
# provide optional property with which to check for nil
|
||||||
def compact(input, property = nil)
|
def compact(input, property = nil)
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input)
|
||||||
|
|
||||||
if property.nil?
|
if property.nil?
|
||||||
ary.compact
|
ary.compact
|
||||||
@@ -285,7 +280,7 @@ module Liquid
|
|||||||
unless array.respond_to?(:to_ary)
|
unless array.respond_to?(:to_ary)
|
||||||
raise ArgumentError, "concat filter requires an array argument"
|
raise ArgumentError, "concat filter requires an array argument"
|
||||||
end
|
end
|
||||||
InputIterator.new(input, context).concat(array)
|
InputIterator.new(input).concat(array)
|
||||||
end
|
end
|
||||||
|
|
||||||
# prepend a string to another
|
# prepend a string to another
|
||||||
@@ -426,26 +421,17 @@ module Liquid
|
|||||||
result.is_a?(BigDecimal) ? result.to_f : result
|
result.is_a?(BigDecimal) ? result.to_f : result
|
||||||
end
|
end
|
||||||
|
|
||||||
# Set a default value when the input is nil, false or empty
|
def default(input, default_value = '')
|
||||||
#
|
if !input || input.respond_to?(:empty?) && input.empty?
|
||||||
# Example:
|
Usage.increment("default_filter_received_false_value") if input == false # See https://github.com/Shopify/liquid/issues/1127
|
||||||
# {{ product.title | default: "No Title" }}
|
default_value
|
||||||
#
|
else
|
||||||
# Use `allow_false` when an input should only be tested against nil or empty and not false.
|
input
|
||||||
#
|
end
|
||||||
# Example:
|
|
||||||
# {{ product.title | default: "No Title", allow_false: true }}
|
|
||||||
#
|
|
||||||
def default(input, default_value = '', options = {})
|
|
||||||
options = {} unless options.is_a?(Hash)
|
|
||||||
false_check = options['allow_false'] ? input.nil? : !input
|
|
||||||
false_check || (input.respond_to?(:empty?) && input.empty?) ? default_value : input
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
attr_reader :context
|
|
||||||
|
|
||||||
def raise_property_error(property)
|
def raise_property_error(property)
|
||||||
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
||||||
end
|
end
|
||||||
@@ -474,8 +460,7 @@ module Liquid
|
|||||||
class InputIterator
|
class InputIterator
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
def initialize(input, context)
|
def initialize(input)
|
||||||
@context = context
|
|
||||||
@input = if input.is_a?(Array)
|
@input = if input.is_a?(Array)
|
||||||
input.flatten
|
input.flatten
|
||||||
elsif input.is_a?(Hash)
|
elsif input.is_a?(Hash)
|
||||||
@@ -514,7 +499,6 @@ module Liquid
|
|||||||
|
|
||||||
def each
|
def each
|
||||||
@input.each do |e|
|
@input.each do |e|
|
||||||
e.context = @context if e.respond_to?(:context=)
|
|
||||||
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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,7 +20,7 @@ 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
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -51,8 +51,8 @@ module Liquid
|
|||||||
|
|
||||||
iteration += 1
|
iteration += 1
|
||||||
iteration = 0 if iteration >= @variables.size
|
iteration = 0 if iteration >= @variables.size
|
||||||
|
|
||||||
context.registers[:cycle][key] = iteration
|
context.registers[:cycle][key] = iteration
|
||||||
|
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -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
|
||||||
@@ -105,11 +105,9 @@ module Liquid
|
|||||||
p = Parser.new(markup)
|
p = Parser.new(markup)
|
||||||
@variable_name = p.consume(:id)
|
@variable_name = p.consume(:id)
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in')
|
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in')
|
||||||
|
|
||||||
collection_name = p.expression
|
collection_name = p.expression
|
||||||
@collection_name = Expression.parse(collection_name)
|
|
||||||
|
|
||||||
@name = "#{@variable_name}-#{collection_name}"
|
@name = "#{@variable_name}-#{collection_name}"
|
||||||
|
@collection_name = Expression.parse(collection_name)
|
||||||
@reversed = p.id?('reversed')
|
@reversed = p.id?('reversed')
|
||||||
|
|
||||||
while p.look(:id) && p.look(:colon, 1)
|
while p.look(:id) && p.look(:colon, 1)
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ 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
|
||||||
|
|||||||
@@ -21,7 +21,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
|
||||||
end
|
end
|
||||||
@@ -43,27 +43,19 @@ module Liquid
|
|||||||
|
|
||||||
context_variable_name = @alias_name || template_name.split('/').last
|
context_variable_name = @alias_name || template_name.split('/').last
|
||||||
|
|
||||||
render_partial_func = ->(var, forloop) {
|
render_partial_func = ->(var) {
|
||||||
inner_context = context.new_isolated_subcontext
|
inner_context = context.new_isolated_subcontext
|
||||||
inner_context.template_name = template_name
|
inner_context.template_name = template_name
|
||||||
inner_context.partial = true
|
inner_context.partial = true
|
||||||
inner_context['forloop'] = forloop if forloop
|
|
||||||
|
|
||||||
@attributes.each do |key, value|
|
@attributes.each do |key, value|
|
||||||
inner_context[key] = context.evaluate(value)
|
inner_context[key] = context.evaluate(value)
|
||||||
end
|
end
|
||||||
inner_context[context_variable_name] = var unless var.nil?
|
inner_context[context_variable_name] = var unless var.nil?
|
||||||
partial.render_to_output_buffer(inner_context, output)
|
partial.render_to_output_buffer(inner_context, output)
|
||||||
forloop&.send(:increment!)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
variable.is_a?(Array) ? variable.each(&render_partial_func) : render_partial_func.call(variable)
|
||||||
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
|
|
||||||
variable.each { |var| render_partial_func.call(var, forloop) }
|
|
||||||
else
|
|
||||||
render_partial_func.call(variable, nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -27,6 +27,7 @@ module Liquid
|
|||||||
to = @attributes.key?('limit') ? from + context.evaluate(@attributes['limit']).to_i : nil
|
to = @attributes.key?('limit') ? from + context.evaluate(@attributes['limit']).to_i : nil
|
||||||
|
|
||||||
collection = Utils.slice_collection(collection, from, to)
|
collection = Utils.slice_collection(collection, from, to)
|
||||||
|
|
||||||
length = collection.length
|
length = collection.length
|
||||||
|
|
||||||
cols = context.evaluate(@attributes['cols']).to_i
|
cols = context.evaluate(@attributes['cols']).to_i
|
||||||
|
|||||||
@@ -254,7 +254,7 @@ module Liquid
|
|||||||
if @profiling && !context.partial
|
if @profiling && !context.partial
|
||||||
raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler)
|
raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler)
|
||||||
|
|
||||||
@profiler = Profiler.new(context.template_name)
|
@profiler = Profiler.new
|
||||||
@profiler.start
|
@profiler.start
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -179,11 +179,6 @@ class DropsTest < Minitest::Test
|
|||||||
assert_equal(' carrot ', output)
|
assert_equal(' carrot ', output)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_context_drop_array_with_map
|
|
||||||
output = Liquid::Template.parse(' {{ contexts | map: "bar" }} ').render!('contexts' => [ContextDrop.new, ContextDrop.new], 'bar' => "carrot")
|
|
||||||
assert_equal(' carrotcarrot ', output)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_nested_context_drop
|
def test_nested_context_drop
|
||||||
output = Liquid::Template.parse(' {{ product.context.foo }} ').render!('product' => ProductDrop.new, 'foo' => "monkey")
|
output = Liquid::Template.parse(' {{ product.context.foo }} ').render!('product' => ProductDrop.new, 'foo' => "monkey")
|
||||||
assert_equal(' monkey ', output)
|
assert_equal(' monkey ', output)
|
||||||
|
|||||||
@@ -252,7 +252,6 @@ class ErrorHandlingTest < Minitest::Test
|
|||||||
|
|
||||||
begin
|
begin
|
||||||
Liquid::Template.file_system = TestFileSystem.new
|
Liquid::Template.file_system = TestFileSystem.new
|
||||||
|
|
||||||
template = Liquid::Template.parse("Argument error:\n{% include 'product' %}", line_numbers: true)
|
template = Liquid::Template.parse("Argument error:\n{% include 'product' %}", line_numbers: true)
|
||||||
page = template.render('errors' => ErrorDrop.new)
|
page = template.render('errors' => ErrorDrop.new)
|
||||||
ensure
|
ensure
|
||||||
|
|||||||
@@ -153,19 +153,4 @@ class RenderProfilingTest < Minitest::Test
|
|||||||
# Will profile each invocation of the for block
|
# Will profile each invocation of the for block
|
||||||
assert_equal(2, t.profiler[0].children.length)
|
assert_equal(2, t.profiler[0].children.length)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_profiling_supports_self_time
|
|
||||||
t = Template.parse("{% for item in collection %} {{ item }} {% endfor %}", profile: true)
|
|
||||||
t.render!("collection" => ["one", "two"])
|
|
||||||
leaf = t.profiler[0].children[0]
|
|
||||||
|
|
||||||
assert_operator leaf.self_time, :>, 0
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_profiling_supports_total_time
|
|
||||||
t = Template.parse("{% if true %} {% increment test %} {{ test }} {% endif %}", profile: true)
|
|
||||||
t.render!
|
|
||||||
|
|
||||||
assert_operator t.profiler[0].total_time, :>, 0
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -685,17 +685,6 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal("bar", @filters.default(false, "bar"))
|
assert_equal("bar", @filters.default(false, "bar"))
|
||||||
assert_equal("bar", @filters.default([], "bar"))
|
assert_equal("bar", @filters.default([], "bar"))
|
||||||
assert_equal("bar", @filters.default({}, "bar"))
|
assert_equal("bar", @filters.default({}, "bar"))
|
||||||
assert_template_result('bar', "{{ false | default: 'bar' }}")
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_default_handle_false
|
|
||||||
assert_equal("foo", @filters.default("foo", "bar", "allow_false" => true))
|
|
||||||
assert_equal("bar", @filters.default(nil, "bar", "allow_false" => true))
|
|
||||||
assert_equal("bar", @filters.default("", "bar", "allow_false" => true))
|
|
||||||
assert_equal(false, @filters.default(false, "bar", "allow_false" => true))
|
|
||||||
assert_equal("bar", @filters.default([], "bar", "allow_false" => true))
|
|
||||||
assert_equal("bar", @filters.default({}, "bar", "allow_false" => true))
|
|
||||||
assert_template_result('false', "{{ false | default: 'bar', allow_false: true }}")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_cannot_access_private_methods
|
def test_cannot_access_private_methods
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -205,13 +205,4 @@ class RenderTagTest < Minitest::Test
|
|||||||
assert_template_result("Product: Draft 151cm Product: Element 155cm ",
|
assert_template_result("Product: Draft 151cm Product: Element 155cm ",
|
||||||
"{% 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_forloop
|
|
||||||
Liquid::Template.file_system = StubFileSystem.new(
|
|
||||||
'product' => "Product: {{ product.title }} {% if forloop.first %}first{% endif %} {% if forloop.last %}last{% endif %} index:{{ forloop.index }} ",
|
|
||||||
)
|
|
||||||
|
|
||||||
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
|
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user