mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 16:25:42 +03:00
Compare commits
15 Commits
styling-fi
...
sort-numer
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9bb533985e | ||
|
|
1c6a5d91fe | ||
|
|
1aa7d3d2ba | ||
|
|
0db9c56f34 | ||
|
|
f4d134cd5c | ||
|
|
b667bcb48b | ||
|
|
2c14e0b2ba | ||
|
|
ca207ed93f | ||
|
|
ef13343591 | ||
|
|
adb40c41b7 | ||
|
|
d8403af515 | ||
|
|
0d26f05bb8 | ||
|
|
1dcad34b06 | ||
|
|
c0ffee5919 | ||
|
|
2f0a2c66f3 |
@@ -1,5 +1,5 @@
|
|||||||
inherit_from:
|
inherit_from:
|
||||||
- https://shopify.github.io/ruby-style-guide/rubocop.yml
|
- 'https://shopify.github.io/ruby-style-guide/rubocop.yml'
|
||||||
- .rubocop_todo.yml
|
- .rubocop_todo.yml
|
||||||
|
|
||||||
require: rubocop-performance
|
require: rubocop-performance
|
||||||
@@ -8,9 +8,10 @@ Performance:
|
|||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
AllCops:
|
AllCops:
|
||||||
|
TargetRubyVersion: 2.4
|
||||||
Exclude:
|
Exclude:
|
||||||
- 'vendor/bundle/**/*'
|
- 'vendor/bundle/**/*'
|
||||||
|
|
||||||
Naming/MethodName:
|
Naming/MethodName:
|
||||||
Exclude:
|
Exclude:
|
||||||
- 'example/server/liquid_servlet.rb'
|
- 'example/server/liquid_servlet.rb'
|
||||||
|
|||||||
@@ -6,26 +6,6 @@
|
|||||||
# Note that changes in the inspected code, or installation of new
|
# Note that changes in the inspected code, or installation of new
|
||||||
# versions of RuboCop, may require this file to be generated again.
|
# versions of RuboCop, may require this file to be generated again.
|
||||||
|
|
||||||
# Offense count: 2
|
|
||||||
Lint/AmbiguousOperator:
|
|
||||||
Exclude:
|
|
||||||
- 'test/unit/condition_unit_test.rb'
|
|
||||||
|
|
||||||
# Offense count: 21
|
|
||||||
# Configuration parameters: AllowSafeAssignment.
|
|
||||||
Lint/AssignmentInCondition:
|
|
||||||
Exclude:
|
|
||||||
- 'lib/liquid/block_body.rb'
|
|
||||||
- 'lib/liquid/lexer.rb'
|
|
||||||
- 'lib/liquid/standardfilters.rb'
|
|
||||||
- 'lib/liquid/tags/for.rb'
|
|
||||||
- 'lib/liquid/tags/if.rb'
|
|
||||||
- 'lib/liquid/tags/raw.rb'
|
|
||||||
- 'lib/liquid/variable.rb'
|
|
||||||
- 'performance/profile.rb'
|
|
||||||
- 'test/test_helper.rb'
|
|
||||||
- 'test/unit/tokenizer_unit_test.rb'
|
|
||||||
|
|
||||||
# Offense count: 2
|
# Offense count: 2
|
||||||
# Cop supports --auto-correct.
|
# Cop supports --auto-correct.
|
||||||
# Configuration parameters: EnforcedStyle.
|
# Configuration parameters: EnforcedStyle.
|
||||||
@@ -34,17 +14,6 @@ Lint/InheritException:
|
|||||||
Exclude:
|
Exclude:
|
||||||
- 'lib/liquid/interrupts.rb'
|
- 'lib/liquid/interrupts.rb'
|
||||||
|
|
||||||
# Offense count: 2
|
|
||||||
Lint/UselessAssignment:
|
|
||||||
Exclude:
|
|
||||||
- 'performance/shopify/database.rb'
|
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Configuration parameters: CheckForMethodsWithNoSideEffects.
|
|
||||||
Lint/Void:
|
|
||||||
Exclude:
|
|
||||||
- 'lib/liquid/parse_context.rb'
|
|
||||||
|
|
||||||
# Offense count: 98
|
# Offense count: 98
|
||||||
# Cop supports --auto-correct.
|
# Cop supports --auto-correct.
|
||||||
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
||||||
@@ -76,19 +45,4 @@ Style/ClassVars:
|
|||||||
Exclude:
|
Exclude:
|
||||||
- 'lib/liquid/condition.rb'
|
- 'lib/liquid/condition.rb'
|
||||||
- 'lib/liquid/strainer.rb'
|
- 'lib/liquid/strainer.rb'
|
||||||
- 'lib/liquid/template.rb'
|
- 'lib/liquid/template.rb'
|
||||||
|
|
||||||
# Offense count: 1
|
|
||||||
# Configuration parameters: AllowCoercion.
|
|
||||||
Style/DateTime:
|
|
||||||
Exclude:
|
|
||||||
- 'test/unit/context_unit_test.rb'
|
|
||||||
|
|
||||||
# Offense count: 9
|
|
||||||
# Cop supports --auto-correct.
|
|
||||||
# Configuration parameters: AllowAsExpressionSeparator.
|
|
||||||
Style/Semicolon:
|
|
||||||
Exclude:
|
|
||||||
- 'test/integration/error_handling_test.rb'
|
|
||||||
- 'test/integration/template_test.rb'
|
|
||||||
- 'test/unit/context_unit_test.rb'
|
|
||||||
@@ -7,8 +7,6 @@ rvm:
|
|||||||
- &latest_ruby 2.6
|
- &latest_ruby 2.6
|
||||||
- 2.7
|
- 2.7
|
||||||
- ruby-head
|
- ruby-head
|
||||||
- jruby-head
|
|
||||||
- truffleruby
|
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
@@ -17,8 +15,6 @@ matrix:
|
|||||||
name: Profiling Memory Usage
|
name: Profiling Memory Usage
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- rvm: ruby-head
|
- rvm: ruby-head
|
||||||
- rvm: jruby-head
|
|
||||||
- rvm: truffleruby
|
|
||||||
|
|
||||||
branches:
|
branches:
|
||||||
only:
|
only:
|
||||||
|
|||||||
@@ -78,7 +78,10 @@ require 'liquid/tokenizer'
|
|||||||
require 'liquid/parse_context'
|
require 'liquid/parse_context'
|
||||||
require 'liquid/partial_cache'
|
require 'liquid/partial_cache'
|
||||||
require 'liquid/usage'
|
require 'liquid/usage'
|
||||||
|
require 'liquid/register'
|
||||||
|
require 'liquid/static_registers'
|
||||||
|
|
||||||
# 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 }
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
private def parse_for_liquid_tag(tokenizer, parse_context)
|
private def parse_for_liquid_tag(tokenizer, parse_context)
|
||||||
while token = tokenizer.shift
|
while (token = tokenizer.shift)
|
||||||
unless token.empty? || token =~ WhitespaceOrNothing
|
unless token.empty? || token =~ WhitespaceOrNothing
|
||||||
unless token =~ LiquidTagToken
|
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
|
||||||
@@ -36,7 +36,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
tag_name = Regexp.last_match(1)
|
tag_name = Regexp.last_match(1)
|
||||||
markup = Regexp.last_match(2)
|
markup = Regexp.last_match(2)
|
||||||
unless tag = registered_tags[tag_name]
|
unless (tag = registered_tags[tag_name])
|
||||||
# end parsing if we reach an unknown tag and let the caller decide
|
# end parsing if we reach an unknown tag and let the caller decide
|
||||||
# determine how to proceed
|
# determine how to proceed
|
||||||
return yield tag_name, markup
|
return yield tag_name, markup
|
||||||
@@ -52,7 +52,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
private def parse_for_document(tokenizer, parse_context, &block)
|
private def parse_for_document(tokenizer, parse_context, &block)
|
||||||
while token = tokenizer.shift
|
while (token = tokenizer.shift)
|
||||||
next if token.empty?
|
next if token.empty?
|
||||||
case
|
case
|
||||||
when token.start_with?(TAGSTART)
|
when token.start_with?(TAGSTART)
|
||||||
@@ -74,7 +74,7 @@ module Liquid
|
|||||||
next parse_for_liquid_tag(liquid_tag_tokenizer, parse_context, &block)
|
next parse_for_liquid_tag(liquid_tag_tokenizer, parse_context, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
unless tag = registered_tags[tag_name]
|
unless (tag = registered_tags[tag_name])
|
||||||
# end parsing if we reach an unknown tag and let the caller decide
|
# end parsing if we reach an unknown tag and let the caller decide
|
||||||
# determine how to proceed
|
# determine how to proceed
|
||||||
return yield tag_name, markup
|
return yield tag_name, markup
|
||||||
@@ -122,7 +122,7 @@ module Liquid
|
|||||||
context.resource_limits.render_score += @nodelist.length
|
context.resource_limits.render_score += @nodelist.length
|
||||||
|
|
||||||
idx = 0
|
idx = 0
|
||||||
while node = @nodelist[idx]
|
while (node = @nodelist[idx])
|
||||||
previous_output_size = output.bytesize
|
previous_output_size = output.bytesize
|
||||||
|
|
||||||
case node
|
case node
|
||||||
@@ -154,7 +154,13 @@ module Liquid
|
|||||||
private
|
private
|
||||||
|
|
||||||
def render_node(context, output, node)
|
def render_node(context, output, node)
|
||||||
node.render_to_output_buffer(context, output)
|
if node.disabled?(context)
|
||||||
|
output << node.disabled_error_message
|
||||||
|
return
|
||||||
|
end
|
||||||
|
disable_tags(context, node.disabled_tags) do
|
||||||
|
node.render_to_output_buffer(context, output)
|
||||||
|
end
|
||||||
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
rescue UndefinedVariable, UndefinedDropMethod, UndefinedFilter => e
|
||||||
context.handle_error(e, node.line_number)
|
context.handle_error(e, node.line_number)
|
||||||
rescue ::StandardError => e
|
rescue ::StandardError => e
|
||||||
@@ -162,6 +168,11 @@ module Liquid
|
|||||||
output << context.handle_error(e, line_number)
|
output << context.handle_error(e, line_number)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disable_tags(context, tags, &block)
|
||||||
|
return yield if tags.empty?
|
||||||
|
context.registers[:disabled_tags].disable(tags, &block)
|
||||||
|
end
|
||||||
|
|
||||||
def raise_if_resource_limits_reached(context, length)
|
def raise_if_resource_limits_reached(context, length)
|
||||||
context.resource_limits.render_length += length
|
context.resource_limits.render_length += length
|
||||||
return unless context.resource_limits.reached?
|
return unless context.resource_limits.reached?
|
||||||
|
|||||||
@@ -18,18 +18,17 @@ module Liquid
|
|||||||
attr_accessor :exception_renderer, :template_name, :partial, :global_filter, :strict_variables, :strict_filters
|
attr_accessor :exception_renderer, :template_name, :partial, :global_filter, :strict_variables, :strict_filters
|
||||||
|
|
||||||
# rubocop:disable Metrics/ParameterLists
|
# rubocop:disable Metrics/ParameterLists
|
||||||
def self.build(environments: {}, outer_scope: {}, registers: {}, rethrow_errors: false, resource_limits: nil, static_registers: {}, static_environments: {})
|
def self.build(environments: {}, outer_scope: {}, registers: {}, rethrow_errors: false, resource_limits: nil, static_environments: {})
|
||||||
new(environments, outer_scope, registers, rethrow_errors, resource_limits, static_registers, static_environments)
|
new(environments, outer_scope, registers, rethrow_errors, resource_limits, static_environments)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil, static_registers = {}, static_environments = {})
|
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil, static_environments = {})
|
||||||
@environments = [environments]
|
@environments = [environments]
|
||||||
@environments.flatten!
|
@environments.flatten!
|
||||||
|
|
||||||
@static_environments = [static_environments].flat_map(&:freeze).freeze
|
@static_environments = [static_environments].flat_map(&:freeze).freeze
|
||||||
@scopes = [(outer_scope || {})]
|
@scopes = [(outer_scope || {})]
|
||||||
@registers = registers
|
@registers = registers
|
||||||
@static_registers = static_registers.freeze
|
|
||||||
@errors = []
|
@errors = []
|
||||||
@partial = false
|
@partial = false
|
||||||
@strict_variables = false
|
@strict_variables = false
|
||||||
@@ -137,7 +136,7 @@ module Liquid
|
|||||||
Context.build(
|
Context.build(
|
||||||
resource_limits: resource_limits,
|
resource_limits: resource_limits,
|
||||||
static_environments: static_environments,
|
static_environments: static_environments,
|
||||||
static_registers: static_registers
|
registers: StaticRegisters.new(registers)
|
||||||
).tap do |subcontext|
|
).tap do |subcontext|
|
||||||
subcontext.base_scope_depth = base_scope_depth + 1
|
subcontext.base_scope_depth = base_scope_depth + 1
|
||||||
subcontext.exception_renderer = exception_renderer
|
subcontext.exception_renderer = exception_renderer
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ module Liquid
|
|||||||
|
|
||||||
if include?(Enumerable)
|
if include?(Enumerable)
|
||||||
blacklist += Enumerable.public_instance_methods
|
blacklist += Enumerable.public_instance_methods
|
||||||
blacklist -= [:sort, :count, :first, :min, :max, :include?]
|
blacklist -= [:sort, :count, :first, :min, :max]
|
||||||
end
|
end
|
||||||
|
|
||||||
whitelist = [:to_liquid] + (public_instance_methods - blacklist)
|
whitelist = [:to_liquid] + (public_instance_methods - blacklist)
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def full_path(template_path)
|
def full_path(template_path)
|
||||||
raise FileSystemError, "Illegal template name '#{template_path}'" unless template_path =~ %r{\A[^./][a-zA-Z0-9_/]+\z}
|
raise FileSystemError, "Illegal template name '#{template_path}'" unless %r{\A[^./][a-zA-Z0-9_/]+\z}.match?(template_path)
|
||||||
|
|
||||||
full_path = if template_path.include?('/')
|
full_path = if template_path.include?('/')
|
||||||
File.join(root, File.dirname(template_path), @pattern % File.basename(template_path))
|
File.join(root, File.dirname(template_path), @pattern % File.basename(template_path))
|
||||||
|
|||||||
@@ -33,15 +33,21 @@ module Liquid
|
|||||||
until @ss.eos?
|
until @ss.eos?
|
||||||
@ss.skip(WHITESPACE_OR_NOTHING)
|
@ss.skip(WHITESPACE_OR_NOTHING)
|
||||||
break if @ss.eos?
|
break if @ss.eos?
|
||||||
tok = if t = @ss.scan(COMPARISON_OPERATOR) then [:comparison, t]
|
tok = if (t = @ss.scan(COMPARISON_OPERATOR))
|
||||||
elsif t = @ss.scan(SINGLE_STRING_LITERAL) then [:string, t]
|
[:comparison, t]
|
||||||
elsif t = @ss.scan(DOUBLE_STRING_LITERAL) then [:string, t]
|
elsif (t = @ss.scan(SINGLE_STRING_LITERAL))
|
||||||
elsif t = @ss.scan(NUMBER_LITERAL) then [:number, t]
|
[:string, t]
|
||||||
elsif t = @ss.scan(IDENTIFIER) then [:id, t]
|
elsif (t = @ss.scan(DOUBLE_STRING_LITERAL))
|
||||||
elsif t = @ss.scan(DOTDOT) then [:dotdot, t]
|
[:string, t]
|
||||||
|
elsif (t = @ss.scan(NUMBER_LITERAL))
|
||||||
|
[:number, t]
|
||||||
|
elsif (t = @ss.scan(IDENTIFIER))
|
||||||
|
[:id, t]
|
||||||
|
elsif (t = @ss.scan(DOTDOT))
|
||||||
|
[:dotdot, t]
|
||||||
else
|
else
|
||||||
c = @ss.getch
|
c = @ss.getch
|
||||||
if s = SPECIALS[c]
|
if (s = SPECIALS[c])
|
||||||
[s, c]
|
[s, c]
|
||||||
else
|
else
|
||||||
raise SyntaxError, "Unexpected character #{c}"
|
raise SyntaxError, "Unexpected character #{c}"
|
||||||
|
|||||||
@@ -25,3 +25,5 @@
|
|||||||
render: "Syntax error in tag 'render' - Template name must be a quoted string"
|
render: "Syntax error in tag 'render' - Template name must be a quoted string"
|
||||||
argument:
|
argument:
|
||||||
include: "Argument error in tag 'include' - Illegal template name"
|
include: "Argument error in tag 'include' - Illegal template name"
|
||||||
|
disabled:
|
||||||
|
tag: "usage is not allowed in this context"
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ module Liquid
|
|||||||
@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
|
||||||
value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def partial_options
|
def partial_options
|
||||||
|
|||||||
6
lib/liquid/register.rb
Normal file
6
lib/liquid/register.rb
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Liquid
|
||||||
|
class Register
|
||||||
|
end
|
||||||
|
end
|
||||||
32
lib/liquid/registers/disabled_tags.rb
Normal file
32
lib/liquid/registers/disabled_tags.rb
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
module Liquid
|
||||||
|
class DisabledTags < Register
|
||||||
|
def initialize
|
||||||
|
@disabled_tags = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def disabled?(tag)
|
||||||
|
@disabled_tags.key?(tag) && @disabled_tags[tag] > 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def disable(tags)
|
||||||
|
tags.each(&method(:increment))
|
||||||
|
yield
|
||||||
|
ensure
|
||||||
|
tags.each(&method(:decrement))
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def increment(tag)
|
||||||
|
@disabled_tags[tag] ||= 0
|
||||||
|
@disabled_tags[tag] += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def decrement(tag)
|
||||||
|
@disabled_tags[tag] -= 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Template.add_register(:disabled_tags, DisabledTags.new)
|
||||||
|
end
|
||||||
@@ -193,6 +193,23 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Sort elements of an array in numeric order
|
||||||
|
# provide optional property with which to sort an array of hashes or drops
|
||||||
|
def sort_numeric(input, property = nil)
|
||||||
|
ary = InputIterator.new(input)
|
||||||
|
if property.nil?
|
||||||
|
ary.sort do |a, b|
|
||||||
|
Utils.to_number(a) <=> Utils.to_number(b)
|
||||||
|
end
|
||||||
|
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||||
|
[]
|
||||||
|
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
|
||||||
|
ary.sort do |a, b|
|
||||||
|
Utils.to_number(a[property]) <=> Utils.to_number(b[property])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# 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)
|
||||||
@@ -327,7 +344,7 @@ module Liquid
|
|||||||
def date(input, format)
|
def date(input, format)
|
||||||
return input if format.to_s.empty?
|
return input if format.to_s.empty?
|
||||||
|
|
||||||
return input unless date = Utils.to_date(input)
|
return input unless (date = Utils.to_date(input))
|
||||||
|
|
||||||
date.strftime(format.to_s)
|
date.strftime(format.to_s)
|
||||||
end
|
end
|
||||||
|
|||||||
36
lib/liquid/static_registers.rb
Normal file
36
lib/liquid/static_registers.rb
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Liquid
|
||||||
|
class StaticRegisters
|
||||||
|
attr_reader :static, :registers
|
||||||
|
|
||||||
|
def initialize(registers = {})
|
||||||
|
@static = registers.is_a?(StaticRegisters) ? registers.static : registers
|
||||||
|
@registers = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def []=(key, value)
|
||||||
|
@registers[key] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
def [](key)
|
||||||
|
if @registers.key?(key)
|
||||||
|
@registers[key]
|
||||||
|
else
|
||||||
|
@static[key]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete(key)
|
||||||
|
@registers.delete(key)
|
||||||
|
end
|
||||||
|
|
||||||
|
def fetch(key, default = nil)
|
||||||
|
key?(key) ? self[key] : default
|
||||||
|
end
|
||||||
|
|
||||||
|
def key?(key)
|
||||||
|
@registers.key?(key) || @static.key?(key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -13,7 +13,15 @@ module Liquid
|
|||||||
tag
|
tag
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disable_tags(*tags)
|
||||||
|
disabled_tags.push(*tags)
|
||||||
|
end
|
||||||
|
|
||||||
private :new
|
private :new
|
||||||
|
|
||||||
|
def disabled_tags
|
||||||
|
@disabled_tags ||= []
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(tag_name, markup, parse_context)
|
def initialize(tag_name, markup, parse_context)
|
||||||
@@ -38,6 +46,14 @@ module Liquid
|
|||||||
''
|
''
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disabled?(context)
|
||||||
|
context.registers[:disabled_tags].disabled?(tag_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def disabled_error_message
|
||||||
|
"#{tag_name} #{options[:locale].t('errors.disabled.tag')}"
|
||||||
|
end
|
||||||
|
|
||||||
# For backwards compatibility with custom tags. In a future release, the semantics
|
# For backwards compatibility with custom tags. In a future release, the semantics
|
||||||
# of the `render_to_output_buffer` method will become the default and the `render`
|
# of the `render_to_output_buffer` method will become the default and the `render`
|
||||||
# method will be removed.
|
# method will be removed.
|
||||||
@@ -49,5 +65,9 @@ module Liquid
|
|||||||
def blank?
|
def blank?
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disabled_tags
|
||||||
|
self.class.disabled_tags
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ module Liquid
|
|||||||
@reversed = p.id?('reversed')
|
@reversed = p.id?('reversed')
|
||||||
|
|
||||||
while p.look(:id) && p.look(:colon, 1)
|
while p.look(:id) && p.look(:colon, 1)
|
||||||
unless attribute = p.id?('limit') || p.id?('offset')
|
unless (attribute = p.id?('limit') || p.id?('offset'))
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_attribute")
|
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_attribute")
|
||||||
end
|
end
|
||||||
p.consume
|
p.consume
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ module Liquid
|
|||||||
def parse_binary_comparisons(p)
|
def parse_binary_comparisons(p)
|
||||||
condition = parse_comparison(p)
|
condition = parse_comparison(p)
|
||||||
first_condition = condition
|
first_condition = condition
|
||||||
while op = (p.id?('and') || p.id?('or'))
|
while (op = (p.id?('and') || p.id?('or')))
|
||||||
child_condition = parse_comparison(p)
|
child_condition = parse_comparison(p)
|
||||||
condition.send(op, child_condition)
|
condition.send(op, child_condition)
|
||||||
condition = child_condition
|
condition = child_condition
|
||||||
@@ -104,7 +104,7 @@ module Liquid
|
|||||||
|
|
||||||
def parse_comparison(p)
|
def parse_comparison(p)
|
||||||
a = Expression.parse(p.expression)
|
a = Expression.parse(p.expression)
|
||||||
if op = p.consume?(:comparison)
|
if (op = p.consume?(:comparison))
|
||||||
b = Expression.parse(p.expression)
|
b = Expression.parse(p.expression)
|
||||||
Condition.new(a, op, b)
|
Condition.new(a, op, b)
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ module Liquid
|
|||||||
|
|
||||||
def parse(tokens)
|
def parse(tokens)
|
||||||
@body = +''
|
@body = +''
|
||||||
while token = tokens.shift
|
while (token = tokens.shift)
|
||||||
if token =~ FullTokenPossiblyInvalid
|
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)
|
||||||
@@ -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 markup =~ Syntax
|
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
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ module Liquid
|
|||||||
class Render < Tag
|
class Render < Tag
|
||||||
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
||||||
|
|
||||||
|
disable_tags "include"
|
||||||
|
|
||||||
attr_reader :template_name_expr, :attributes
|
attr_reader :template_name_expr, :attributes
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def initialize(tag_name, markup, options)
|
||||||
@@ -22,6 +24,10 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
|
render_tag(context, output)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_tag(context, output)
|
||||||
# Though we evaluate this here we will only ever parse it as a string literal.
|
# Though we evaluate this here we will only ever parse it as a string literal.
|
||||||
template_name = context.evaluate(@template_name_expr)
|
template_name = context.evaluate(@template_name_expr)
|
||||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
||||||
|
|||||||
@@ -92,6 +92,14 @@ module Liquid
|
|||||||
@tags ||= TagRegistry.new
|
@tags ||= TagRegistry.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_register(name, klass)
|
||||||
|
registers[name.to_sym] = klass
|
||||||
|
end
|
||||||
|
|
||||||
|
def registers
|
||||||
|
@registers ||= {}
|
||||||
|
end
|
||||||
|
|
||||||
def error_mode
|
def error_mode
|
||||||
@error_mode ||= :lax
|
@error_mode ||= :lax
|
||||||
end
|
end
|
||||||
@@ -191,18 +199,26 @@ module Liquid
|
|||||||
|
|
||||||
output = nil
|
output = nil
|
||||||
|
|
||||||
|
context_register = context.registers.is_a?(StaticRegisters) ? context.registers.static : context.registers
|
||||||
|
|
||||||
case args.last
|
case args.last
|
||||||
when Hash
|
when Hash
|
||||||
options = args.pop
|
options = args.pop
|
||||||
output = options[:output] if options[:output]
|
output = options[:output] if options[:output]
|
||||||
|
|
||||||
registers.merge!(options[:registers]) if options[:registers].is_a?(Hash)
|
options[:registers]&.each do |key, register|
|
||||||
|
context_register[key] = register
|
||||||
|
end
|
||||||
|
|
||||||
apply_options_to_context(context, options)
|
apply_options_to_context(context, options)
|
||||||
when Module, Array
|
when Module, Array
|
||||||
context.add_filters(args.pop)
|
context.add_filters(args.pop)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Template.registers.each do |key, register|
|
||||||
|
context_register[key] = register
|
||||||
|
end
|
||||||
|
|
||||||
# Retrying a render resets resource usage
|
# Retrying a render resets resource usage
|
||||||
context.resource_limits.reset
|
context.resource_limits.reset
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ module Liquid
|
|||||||
when Numeric
|
when Numeric
|
||||||
obj
|
obj
|
||||||
when String
|
when String
|
||||||
obj.strip =~ /\A-?\d+\.\d+\z/ ? BigDecimal(obj) : obj.to_i
|
/\A-?\d+\.\d+\z/.match?(obj.strip) ? BigDecimal(obj) : obj.to_i
|
||||||
else
|
else
|
||||||
if obj.respond_to?(:to_number)
|
if obj.respond_to?(:to_number)
|
||||||
obj.to_number
|
obj.to_number
|
||||||
|
|||||||
@@ -104,13 +104,21 @@ module Liquid
|
|||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def disabled?(_context)
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def disabled_tags
|
||||||
|
[]
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def parse_filter_expressions(filter_name, unparsed_args)
|
def parse_filter_expressions(filter_name, unparsed_args)
|
||||||
filter_args = []
|
filter_args = []
|
||||||
keyword_args = nil
|
keyword_args = nil
|
||||||
unparsed_args.each do |a|
|
unparsed_args.each do |a|
|
||||||
if matches = a.match(JustTagAttributes)
|
if (matches = a.match(JustTagAttributes))
|
||||||
keyword_args ||= {}
|
keyword_args ||= {}
|
||||||
keyword_args[matches[1]] = Expression.parse(matches[2])
|
keyword_args[matches[1]] = Expression.parse(matches[2])
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ profiler.run
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if profile_type == :cpu && graph_filename = ENV['GRAPH_FILENAME']
|
if profile_type == :cpu && (graph_filename = ENV['GRAPH_FILENAME'])
|
||||||
File.open(graph_filename, 'w') do |f|
|
File.open(graph_filename, 'w') do |f|
|
||||||
StackProf::Report.new(results).print_graphviz(nil, f)
|
StackProf::Report.new(results).print_graphviz(nil, f)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ module Database
|
|||||||
db['article'] = db['blog']['articles'].first
|
db['article'] = db['blog']['articles'].first
|
||||||
|
|
||||||
db['cart'] = {
|
db['cart'] = {
|
||||||
'total_price' => db['line_items'].values.inject(0) { |sum, item| sum += item['line_price'] * item['quantity'] },
|
'total_price' => db['line_items'].values.inject(0) { |sum, item| sum + item['line_price'] * item['quantity'] },
|
||||||
'item_count' => db['line_items'].values.inject(0) { |sum, item| sum += item['quantity'] },
|
'item_count' => db['line_items'].values.inject(0) { |sum, item| sum + item['quantity'] },
|
||||||
'items' => db['line_items'].values,
|
'items' => db['line_items'].values,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -272,4 +272,11 @@ class DropsTest < Minitest::Test
|
|||||||
assert_equal 'ProductDrop', Liquid::Template.parse("{{ product }}").render!('product' => ProductDrop.new)
|
assert_equal 'ProductDrop', Liquid::Template.parse("{{ product }}").render!('product' => ProductDrop.new)
|
||||||
assert_equal 'EnumerableDrop', Liquid::Template.parse('{{ collection }}').render!('collection' => EnumerableDrop.new)
|
assert_equal 'EnumerableDrop', Liquid::Template.parse('{{ collection }}').render!('collection' => EnumerableDrop.new)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_invokable_methods
|
||||||
|
assert_equal %w(to_liquid catchall user_input context texts).to_set, ProductDrop.invokable_methods
|
||||||
|
assert_equal %w(to_liquid scopes_as_array loop_pos scopes).to_set, ContextDrop.invokable_methods
|
||||||
|
assert_equal %w(to_liquid size max min first count).to_set, EnumerableDrop.invokable_methods
|
||||||
|
assert_equal %w(to_liquid max min sort count first).to_set, RealEnumerableDrop.invokable_methods
|
||||||
|
end
|
||||||
end # DropsTest
|
end # DropsTest
|
||||||
|
|||||||
@@ -211,7 +211,10 @@ class ErrorHandlingTest < Minitest::Test
|
|||||||
def test_setting_default_exception_renderer
|
def test_setting_default_exception_renderer
|
||||||
old_exception_renderer = Liquid::Template.default_exception_renderer
|
old_exception_renderer = Liquid::Template.default_exception_renderer
|
||||||
exceptions = []
|
exceptions = []
|
||||||
Liquid::Template.default_exception_renderer = ->(e) { exceptions << e; '' }
|
Liquid::Template.default_exception_renderer = ->(e) {
|
||||||
|
exceptions << e
|
||||||
|
''
|
||||||
|
}
|
||||||
template = Liquid::Template.parse('This is a runtime error: {{ errors.argument_error }}')
|
template = Liquid::Template.parse('This is a runtime error: {{ errors.argument_error }}')
|
||||||
|
|
||||||
output = template.render('errors' => ErrorDrop.new)
|
output = template.render('errors' => ErrorDrop.new)
|
||||||
@@ -225,7 +228,10 @@ class ErrorHandlingTest < Minitest::Test
|
|||||||
def test_exception_renderer_exposing_non_liquid_error
|
def test_exception_renderer_exposing_non_liquid_error
|
||||||
template = Liquid::Template.parse('This is a runtime error: {{ errors.runtime_error }}', line_numbers: true)
|
template = Liquid::Template.parse('This is a runtime error: {{ errors.runtime_error }}', line_numbers: true)
|
||||||
exceptions = []
|
exceptions = []
|
||||||
handler = ->(e) { exceptions << e; e.cause }
|
handler = ->(e) {
|
||||||
|
exceptions << e
|
||||||
|
e.cause
|
||||||
|
}
|
||||||
|
|
||||||
output = template.render({ 'errors' => ErrorDrop.new }, exception_renderer: handler)
|
output = template.render({ 'errors' => ErrorDrop.new }, exception_renderer: handler)
|
||||||
|
|
||||||
|
|||||||
27
test/integration/registers/disabled_tags_test.rb
Normal file
27
test/integration/registers/disabled_tags_test.rb
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class DisabledTagsTest < Minitest::Test
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
class DisableRaw < Block
|
||||||
|
disable_tags "raw"
|
||||||
|
end
|
||||||
|
|
||||||
|
class DisableRawEcho < Block
|
||||||
|
disable_tags "raw", "echo"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_disables_raw
|
||||||
|
with_custom_tag('disable', DisableRaw) do
|
||||||
|
assert_template_result 'raw usage is not allowed in this contextfoo', '{% disable %}{% raw %}Foobar{% endraw %}{% echo "foo" %}{% enddisable %}'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_disables_echo_and_raw
|
||||||
|
with_custom_tag('disable', DisableRawEcho) do
|
||||||
|
assert_template_result 'raw usage is not allowed in this contextecho usage is not allowed in this context', '{% disable %}{% raw %}Foobar{% endraw %}{% echo "foo" %}{% enddisable %}'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -198,6 +198,12 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a")
|
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_sort_numeric
|
||||||
|
assert_equal ['1', '2', '3', '10'], @filters.sort_numeric(['10', '3', '2', '1'])
|
||||||
|
assert_equal [{ "a" => '1' }, { "a" => '2' }, { "a" => '3' }, { "a" => '10' }],
|
||||||
|
@filters.sort_numeric([{ "a" => '10' }, { "a" => '3' }, { "a" => '1' }, { "a" => '2' }], "a")
|
||||||
|
end
|
||||||
|
|
||||||
def test_sort_with_nils
|
def test_sort_with_nils
|
||||||
assert_equal [1, 2, 3, 4, nil], @filters.sort([nil, 4, 3, 2, 1])
|
assert_equal [1, 2, 3, 4, nil], @filters.sort([nil, 4, 3, 2, 1])
|
||||||
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }, {}], @filters.sort([{ "a" => 4 }, { "a" => 3 }, {}, { "a" => 1 }, { "a" => 2 }], "a")
|
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }, {}], @filters.sort([{ "a" => 4 }, { "a" => 3 }, {}, { "a" => 1 }, { "a" => 2 }], "a")
|
||||||
@@ -292,6 +298,10 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal [], @filters.sort_natural([], "a")
|
assert_equal [], @filters.sort_natural([], "a")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_sort_numeric_empty_array
|
||||||
|
assert_equal [], @filters.sort_numeric([], "a")
|
||||||
|
end
|
||||||
|
|
||||||
def test_sort_natural_invalid_property
|
def test_sort_natural_invalid_property
|
||||||
foo = [
|
foo = [
|
||||||
[1],
|
[1],
|
||||||
|
|||||||
@@ -89,14 +89,12 @@ class RenderTagTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_includes_and_renders_count_towards_the_same_recursion_limit
|
def test_sub_contexts_count_towards_the_same_recursion_limit
|
||||||
Liquid::Template.file_system = StubFileSystem.new(
|
Liquid::Template.file_system = StubFileSystem.new(
|
||||||
'loop_render' => '{% render "loop_include" %}',
|
'loop_render' => '{% render "loop_render" %}',
|
||||||
'loop_include' => '{% include "loop_render" %}'
|
|
||||||
)
|
)
|
||||||
|
assert_raises Liquid::StackLevelError do
|
||||||
assert_raises Liquid::StackLevelError do
|
Template.parse('{% render "loop_render" %}').render!
|
||||||
Template.parse('{% render "loop_include" %}').render!
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -148,4 +146,23 @@ class RenderTagTest < Minitest::Test
|
|||||||
Liquid::Template.file_system = StubFileSystem.new('decr' => '{% decrement %}')
|
Liquid::Template.file_system = StubFileSystem.new('decr' => '{% decrement %}')
|
||||||
assert_template_result '-1-2-1', '{% decrement %}{% decrement %}{% render "decr" %}'
|
assert_template_result '-1-2-1', '{% decrement %}{% decrement %}{% render "decr" %}'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_includes_will_not_render_inside_render_tag
|
||||||
|
Liquid::Template.file_system = StubFileSystem.new(
|
||||||
|
'foo' => 'bar',
|
||||||
|
'test_include' => '{% include "foo" %}'
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_template_result 'include usage is not allowed in this context', '{% render "test_include" %}'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_includes_will_not_render_inside_nested_sibling_tags
|
||||||
|
Liquid::Template.file_system = StubFileSystem.new(
|
||||||
|
'foo' => 'bar',
|
||||||
|
'nested_render_with_sibling_include' => '{% render "test_include" %}{% include "foo" %}',
|
||||||
|
'test_include' => '{% include "foo" %}'
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_template_result 'include usage is not allowed in this contextinclude usage is not allowed in this context', '{% render "nested_render_with_sibling_include" %}'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -81,7 +81,10 @@ class TemplateTest < Minitest::Test
|
|||||||
|
|
||||||
def test_lambda_is_called_once_from_persistent_assigns_over_multiple_parses_and_renders
|
def test_lambda_is_called_once_from_persistent_assigns_over_multiple_parses_and_renders
|
||||||
t = Template.new
|
t = Template.new
|
||||||
t.assigns['number'] = -> { @global ||= 0; @global += 1 }
|
t.assigns['number'] = -> {
|
||||||
|
@global ||= 0
|
||||||
|
@global += 1
|
||||||
|
}
|
||||||
assert_equal '1', t.parse("{{number}}").render!
|
assert_equal '1', t.parse("{{number}}").render!
|
||||||
assert_equal '1', t.parse("{{number}}").render!
|
assert_equal '1', t.parse("{{number}}").render!
|
||||||
assert_equal '1', t.render!
|
assert_equal '1', t.render!
|
||||||
@@ -90,7 +93,10 @@ class TemplateTest < Minitest::Test
|
|||||||
|
|
||||||
def test_lambda_is_called_once_from_custom_assigns_over_multiple_parses_and_renders
|
def test_lambda_is_called_once_from_custom_assigns_over_multiple_parses_and_renders
|
||||||
t = Template.new
|
t = Template.new
|
||||||
assigns = { 'number' => -> { @global ||= 0; @global += 1 } }
|
assigns = { 'number' => -> {
|
||||||
|
@global ||= 0
|
||||||
|
@global += 1
|
||||||
|
} }
|
||||||
assert_equal '1', t.parse("{{number}}").render!(assigns)
|
assert_equal '1', t.parse("{{number}}").render!(assigns)
|
||||||
assert_equal '1', t.parse("{{number}}").render!(assigns)
|
assert_equal '1', t.parse("{{number}}").render!(assigns)
|
||||||
assert_equal '1', t.render!(assigns)
|
assert_equal '1', t.render!(assigns)
|
||||||
@@ -237,7 +243,10 @@ class TemplateTest < Minitest::Test
|
|||||||
|
|
||||||
def test_exception_renderer_that_returns_string
|
def test_exception_renderer_that_returns_string
|
||||||
exception = nil
|
exception = nil
|
||||||
handler = ->(e) { exception = e; '<!-- error -->' }
|
handler = ->(e) {
|
||||||
|
exception = e
|
||||||
|
'<!-- error -->'
|
||||||
|
}
|
||||||
|
|
||||||
output = Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_renderer: handler)
|
output = Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_renderer: handler)
|
||||||
|
|
||||||
@@ -248,7 +257,10 @@ class TemplateTest < Minitest::Test
|
|||||||
def test_exception_renderer_that_raises
|
def test_exception_renderer_that_raises
|
||||||
exception = nil
|
exception = nil
|
||||||
assert_raises(Liquid::ZeroDivisionError) do
|
assert_raises(Liquid::ZeroDivisionError) do
|
||||||
Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_renderer: ->(e) { exception = e; raise })
|
Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_renderer: ->(e) {
|
||||||
|
exception = e
|
||||||
|
raise
|
||||||
|
})
|
||||||
end
|
end
|
||||||
assert exception.is_a?(Liquid::ZeroDivisionError)
|
assert exception.is_a?(Liquid::ZeroDivisionError)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ require 'liquid.rb'
|
|||||||
require 'liquid/profiler'
|
require 'liquid/profiler'
|
||||||
|
|
||||||
mode = :strict
|
mode = :strict
|
||||||
if env_mode = ENV['LIQUID_PARSER_MODE']
|
if (env_mode = ENV['LIQUID_PARSER_MODE'])
|
||||||
puts "-- #{env_mode.upcase} ERROR MODE"
|
puts "-- #{env_mode.upcase} ERROR MODE"
|
||||||
mode = env_mode.to_sym
|
mode = env_mode.to_sym
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -26,9 +26,9 @@ class ConditionUnitTest < Minitest::Test
|
|||||||
assert_evaluates_true 1, '<=', 1
|
assert_evaluates_true 1, '<=', 1
|
||||||
# negative numbers
|
# negative numbers
|
||||||
assert_evaluates_true 1, '>', -1
|
assert_evaluates_true 1, '>', -1
|
||||||
assert_evaluates_true -1, '<', 1
|
assert_evaluates_true(-1, '<', 1)
|
||||||
assert_evaluates_true 1.0, '>', -1.0
|
assert_evaluates_true 1.0, '>', -1.0
|
||||||
assert_evaluates_true -1.0, '<', 1.0
|
assert_evaluates_true(-1.0, '<', 1.0)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_default_operators_evalute_false
|
def test_default_operators_evalute_false
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ class ContextUnitTest < Minitest::Test
|
|||||||
@context['date'] = Date.today
|
@context['date'] = Date.today
|
||||||
assert_equal Date.today, @context['date']
|
assert_equal Date.today, @context['date']
|
||||||
|
|
||||||
now = DateTime.now
|
now = Time.now
|
||||||
@context['datetime'] = now
|
@context['datetime'] = now
|
||||||
assert_equal now, @context['datetime']
|
assert_equal now, @context['datetime']
|
||||||
|
|
||||||
@@ -405,7 +405,11 @@ class ContextUnitTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_lambda_is_called_once
|
def test_lambda_is_called_once
|
||||||
@context['callcount'] = proc { @global ||= 0; @global += 1; @global.to_s }
|
@context['callcount'] = proc {
|
||||||
|
@global ||= 0
|
||||||
|
@global += 1
|
||||||
|
@global.to_s
|
||||||
|
}
|
||||||
|
|
||||||
assert_equal '1', @context['callcount']
|
assert_equal '1', @context['callcount']
|
||||||
assert_equal '1', @context['callcount']
|
assert_equal '1', @context['callcount']
|
||||||
@@ -415,7 +419,11 @@ class ContextUnitTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_nested_lambda_is_called_once
|
def test_nested_lambda_is_called_once
|
||||||
@context['callcount'] = { "lambda" => proc { @global ||= 0; @global += 1; @global.to_s } }
|
@context['callcount'] = { "lambda" => proc {
|
||||||
|
@global ||= 0
|
||||||
|
@global += 1
|
||||||
|
@global.to_s
|
||||||
|
} }
|
||||||
|
|
||||||
assert_equal '1', @context['callcount.lambda']
|
assert_equal '1', @context['callcount.lambda']
|
||||||
assert_equal '1', @context['callcount.lambda']
|
assert_equal '1', @context['callcount.lambda']
|
||||||
@@ -425,7 +433,11 @@ class ContextUnitTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_lambda_in_array_is_called_once
|
def test_lambda_in_array_is_called_once
|
||||||
@context['callcount'] = [1, 2, proc { @global ||= 0; @global += 1; @global.to_s }, 4, 5]
|
@context['callcount'] = [1, 2, proc {
|
||||||
|
@global ||= 0
|
||||||
|
@global += 1
|
||||||
|
@global.to_s
|
||||||
|
}, 4, 5]
|
||||||
|
|
||||||
assert_equal '1', @context['callcount[2]']
|
assert_equal '1', @context['callcount[2]']
|
||||||
assert_equal '1', @context['callcount[2]']
|
assert_equal '1', @context['callcount[2]']
|
||||||
@@ -518,15 +530,23 @@ class ContextUnitTest < Minitest::Test
|
|||||||
registers = {
|
registers = {
|
||||||
my_register: :my_value,
|
my_register: :my_value,
|
||||||
}
|
}
|
||||||
super_context = Context.new({}, {}, registers)
|
super_context = Context.new({}, {}, StaticRegisters.new(registers))
|
||||||
|
super_context.registers[:my_register] = :my_alt_value
|
||||||
subcontext = super_context.new_isolated_subcontext
|
subcontext = super_context.new_isolated_subcontext
|
||||||
assert_nil subcontext.registers[:my_register]
|
assert_equal :my_value, subcontext.registers[:my_register]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_new_isolated_subcontext_inherits_static_registers
|
def test_new_isolated_subcontext_inherits_static_registers
|
||||||
super_context = Context.build(static_registers: { my_register: :my_value })
|
super_context = Context.build(registers: { my_register: :my_value })
|
||||||
subcontext = super_context.new_isolated_subcontext
|
subcontext = super_context.new_isolated_subcontext
|
||||||
assert_equal :my_value, subcontext.static_registers[:my_register]
|
assert_equal :my_value, subcontext.registers[:my_register]
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_new_isolated_subcontext_registers_do_not_pollute_context
|
||||||
|
super_context = Context.build(registers: { my_register: :my_value })
|
||||||
|
subcontext = super_context.new_isolated_subcontext
|
||||||
|
subcontext.registers[:my_register] = :my_alt_value
|
||||||
|
assert_equal :my_value, super_context.registers[:my_register]
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_new_isolated_subcontext_inherits_filters
|
def test_new_isolated_subcontext_inherits_filters
|
||||||
|
|||||||
36
test/unit/registers/disabled_tags_unit_test.rb
Normal file
36
test/unit/registers/disabled_tags_unit_test.rb
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class DisabledTagsUnitTest < Minitest::Test
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def test_disables_tag_specified
|
||||||
|
register = DisabledTags.new
|
||||||
|
register.disable(%w(foo bar)) do
|
||||||
|
assert_equal true, register.disabled?("foo")
|
||||||
|
assert_equal true, register.disabled?("bar")
|
||||||
|
assert_equal false, register.disabled?("unknown")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_disables_nested_tags
|
||||||
|
register = DisabledTags.new
|
||||||
|
register.disable(["foo"]) do
|
||||||
|
register.disable(["foo"]) do
|
||||||
|
assert_equal true, register.disabled?("foo")
|
||||||
|
assert_equal false, register.disabled?("bar")
|
||||||
|
end
|
||||||
|
register.disable(["bar"]) do
|
||||||
|
assert_equal true, register.disabled?("foo")
|
||||||
|
assert_equal true, register.disabled?("bar")
|
||||||
|
register.disable(["foo"]) do
|
||||||
|
assert_equal true, register.disabled?("foo")
|
||||||
|
assert_equal true, register.disabled?("bar")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal true, register.disabled?("foo")
|
||||||
|
assert_equal false, register.disabled?("bar")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
248
test/unit/static_registers_unit_test.rb
Normal file
248
test/unit/static_registers_unit_test.rb
Normal file
@@ -0,0 +1,248 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class StaticRegistersUnitTest < Minitest::Test
|
||||||
|
include Liquid
|
||||||
|
|
||||||
|
def set
|
||||||
|
static_register = StaticRegisters.new
|
||||||
|
static_register[nil] = true
|
||||||
|
static_register[1] = :one
|
||||||
|
static_register[:one] = "one"
|
||||||
|
static_register["two"] = "three"
|
||||||
|
static_register["two"] = 3
|
||||||
|
static_register[false] = nil
|
||||||
|
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.registers)
|
||||||
|
|
||||||
|
static_register
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_get
|
||||||
|
static_register = set
|
||||||
|
|
||||||
|
assert_equal true, static_register[nil]
|
||||||
|
assert_equal :one, static_register[1]
|
||||||
|
assert_equal "one", static_register[:one]
|
||||||
|
assert_equal 3, static_register["two"]
|
||||||
|
assert_nil static_register[false]
|
||||||
|
assert_nil static_register["unknown"]
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_delete
|
||||||
|
static_register = set
|
||||||
|
|
||||||
|
assert_equal true, static_register.delete(nil)
|
||||||
|
assert_equal :one, static_register.delete(1)
|
||||||
|
assert_equal "one", static_register.delete(:one)
|
||||||
|
assert_equal 3, static_register.delete("two")
|
||||||
|
assert_nil static_register.delete(false)
|
||||||
|
assert_nil static_register.delete("unknown")
|
||||||
|
|
||||||
|
assert_equal({}, static_register.registers)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fetch
|
||||||
|
static_register = set
|
||||||
|
|
||||||
|
assert_equal true, static_register.fetch(nil)
|
||||||
|
assert_equal :one, static_register.fetch(1)
|
||||||
|
assert_equal "one", static_register.fetch(:one)
|
||||||
|
assert_equal 3, static_register.fetch("two")
|
||||||
|
assert_nil static_register.fetch(false)
|
||||||
|
assert_nil static_register.fetch("unknown")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fetch_default
|
||||||
|
static_register = StaticRegisters.new
|
||||||
|
|
||||||
|
assert_equal true, static_register.fetch(nil, true)
|
||||||
|
assert_equal :one, static_register.fetch(1, :one)
|
||||||
|
assert_equal "one", static_register.fetch(:one, "one")
|
||||||
|
assert_equal 3, static_register.fetch("two", 3)
|
||||||
|
assert_nil static_register.fetch(false, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_key
|
||||||
|
static_register = set
|
||||||
|
|
||||||
|
assert_equal true, static_register.key?(nil)
|
||||||
|
assert_equal true, static_register.key?(1)
|
||||||
|
assert_equal true, static_register.key?(:one)
|
||||||
|
assert_equal true, static_register.key?("two")
|
||||||
|
assert_equal true, static_register.key?(false)
|
||||||
|
assert_equal false, static_register.key?("unknown")
|
||||||
|
assert_equal false, static_register.key?(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_with_static
|
||||||
|
static_register = StaticRegisters.new(nil => true, 1 => :one, :one => "one", "two" => 3, false => nil)
|
||||||
|
static_register[nil] = false
|
||||||
|
static_register["two"] = 4
|
||||||
|
static_register[true] = "foo"
|
||||||
|
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static)
|
||||||
|
assert_equal({ nil => false, "two" => 4, true => "foo" }, static_register.registers)
|
||||||
|
|
||||||
|
static_register
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_get_with_static
|
||||||
|
static_register = set_with_static
|
||||||
|
|
||||||
|
assert_equal false, static_register[nil]
|
||||||
|
assert_equal :one, static_register[1]
|
||||||
|
assert_equal "one", static_register[:one]
|
||||||
|
assert_equal 4, static_register["two"]
|
||||||
|
assert_equal "foo", static_register[true]
|
||||||
|
assert_nil static_register[false]
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_delete_with_static
|
||||||
|
static_register = set_with_static
|
||||||
|
|
||||||
|
assert_equal false, static_register.delete(nil)
|
||||||
|
assert_equal 4, static_register.delete("two")
|
||||||
|
assert_equal "foo", static_register.delete(true)
|
||||||
|
assert_nil static_register.delete("unknown")
|
||||||
|
assert_nil static_register.delete(:one)
|
||||||
|
|
||||||
|
assert_equal({}, static_register.registers)
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fetch_with_static
|
||||||
|
static_register = set_with_static
|
||||||
|
|
||||||
|
assert_equal false, static_register.fetch(nil)
|
||||||
|
assert_equal :one, static_register.fetch(1)
|
||||||
|
assert_equal "one", static_register.fetch(:one)
|
||||||
|
assert_equal 4, static_register.fetch("two")
|
||||||
|
assert_equal "foo", static_register.fetch(true)
|
||||||
|
assert_nil static_register.fetch(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_key_with_static
|
||||||
|
static_register = set_with_static
|
||||||
|
|
||||||
|
assert_equal true, static_register.key?(nil)
|
||||||
|
assert_equal true, static_register.key?(1)
|
||||||
|
assert_equal true, static_register.key?(:one)
|
||||||
|
assert_equal true, static_register.key?("two")
|
||||||
|
assert_equal true, static_register.key?(false)
|
||||||
|
assert_equal false, static_register.key?("unknown")
|
||||||
|
assert_equal true, static_register.key?(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_static_register_can_be_frozen
|
||||||
|
static_register = set_with_static
|
||||||
|
|
||||||
|
static = static_register.static.freeze
|
||||||
|
|
||||||
|
assert_raises(RuntimeError) do
|
||||||
|
static["two"] = "foo"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_raises(RuntimeError) do
|
||||||
|
static["unknown"] = "foo"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_raises(RuntimeError) do
|
||||||
|
static.delete("two")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_new_static_retains_static
|
||||||
|
static_register = StaticRegisters.new(nil => true, 1 => :one, :one => "one", "two" => 3, false => nil)
|
||||||
|
static_register["one"] = 1
|
||||||
|
static_register["two"] = 2
|
||||||
|
static_register["three"] = 3
|
||||||
|
|
||||||
|
new_register = StaticRegisters.new(static_register)
|
||||||
|
assert_equal({}, new_register.registers)
|
||||||
|
|
||||||
|
new_register["one"] = 4
|
||||||
|
new_register["two"] = 5
|
||||||
|
new_register["three"] = 6
|
||||||
|
|
||||||
|
newest_register = StaticRegisters.new(new_register)
|
||||||
|
assert_equal({}, newest_register.registers)
|
||||||
|
|
||||||
|
newest_register["one"] = 7
|
||||||
|
newest_register["two"] = 8
|
||||||
|
newest_register["three"] = 9
|
||||||
|
|
||||||
|
assert_equal({ "one" => 1, "two" => 2, "three" => 3 }, static_register.registers)
|
||||||
|
assert_equal({ "one" => 4, "two" => 5, "three" => 6 }, new_register.registers)
|
||||||
|
assert_equal({ "one" => 7, "two" => 8, "three" => 9 }, newest_register.registers)
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static)
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, new_register.static)
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, newest_register.static)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_multiple_instances_are_unique
|
||||||
|
static_register = StaticRegisters.new(nil => true, 1 => :one, :one => "one", "two" => 3, false => nil)
|
||||||
|
static_register["one"] = 1
|
||||||
|
static_register["two"] = 2
|
||||||
|
static_register["three"] = 3
|
||||||
|
|
||||||
|
new_register = StaticRegisters.new(foo: :bar)
|
||||||
|
assert_equal({}, new_register.registers)
|
||||||
|
|
||||||
|
new_register["one"] = 4
|
||||||
|
new_register["two"] = 5
|
||||||
|
new_register["three"] = 6
|
||||||
|
|
||||||
|
newest_register = StaticRegisters.new(bar: :foo)
|
||||||
|
assert_equal({}, newest_register.registers)
|
||||||
|
|
||||||
|
newest_register["one"] = 7
|
||||||
|
newest_register["two"] = 8
|
||||||
|
newest_register["three"] = 9
|
||||||
|
|
||||||
|
assert_equal({ "one" => 1, "two" => 2, "three" => 3 }, static_register.registers)
|
||||||
|
assert_equal({ "one" => 4, "two" => 5, "three" => 6 }, new_register.registers)
|
||||||
|
assert_equal({ "one" => 7, "two" => 8, "three" => 9 }, newest_register.registers)
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static)
|
||||||
|
assert_equal({ foo: :bar }, new_register.static)
|
||||||
|
assert_equal({ bar: :foo }, newest_register.static)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_can_update_static_directly_and_updates_all_instances
|
||||||
|
static_register = StaticRegisters.new(nil => true, 1 => :one, :one => "one", "two" => 3, false => nil)
|
||||||
|
static_register["one"] = 1
|
||||||
|
static_register["two"] = 2
|
||||||
|
static_register["three"] = 3
|
||||||
|
|
||||||
|
new_register = StaticRegisters.new(static_register)
|
||||||
|
assert_equal({}, new_register.registers)
|
||||||
|
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil }, static_register.static)
|
||||||
|
|
||||||
|
new_register["one"] = 4
|
||||||
|
new_register["two"] = 5
|
||||||
|
new_register["three"] = 6
|
||||||
|
new_register.static["four"] = 10
|
||||||
|
|
||||||
|
newest_register = StaticRegisters.new(new_register)
|
||||||
|
assert_equal({}, newest_register.registers)
|
||||||
|
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil, "four" => 10 }, new_register.static)
|
||||||
|
|
||||||
|
newest_register["one"] = 7
|
||||||
|
newest_register["two"] = 8
|
||||||
|
newest_register["three"] = 9
|
||||||
|
new_register.static["four"] = 5
|
||||||
|
new_register.static["five"] = 15
|
||||||
|
|
||||||
|
assert_equal({ "one" => 1, "two" => 2, "three" => 3 }, static_register.registers)
|
||||||
|
assert_equal({ "one" => 4, "two" => 5, "three" => 6 }, new_register.registers)
|
||||||
|
assert_equal({ "one" => 7, "two" => 8, "three" => 9 }, newest_register.registers)
|
||||||
|
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil, "four" => 5, "five" => 15 }, newest_register.static)
|
||||||
|
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil, "four" => 5, "five" => 15 }, static_register.static)
|
||||||
|
assert_equal({ nil => true, 1 => :one, :one => "one", "two" => 3, false => nil, "four" => 5, "five" => 15 }, new_register.static)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -35,7 +35,7 @@ class TokenizerTest < Minitest::Test
|
|||||||
def tokenize(source)
|
def tokenize(source)
|
||||||
tokenizer = Liquid::Tokenizer.new(source)
|
tokenizer = Liquid::Tokenizer.new(source)
|
||||||
tokens = []
|
tokens = []
|
||||||
while t = tokenizer.shift
|
while (t = tokenizer.shift)
|
||||||
tokens << t
|
tokens << t
|
||||||
end
|
end
|
||||||
tokens
|
tokens
|
||||||
|
|||||||
Reference in New Issue
Block a user