Compare commits

..

1 Commits

Author SHA1 Message Date
Justin Li
74f9bad513 Add spaceless tag 2015-06-26 10:00:34 -07:00
52 changed files with 523 additions and 763 deletions

View File

@@ -13,9 +13,6 @@ Metrics/BlockNesting:
Metrics/ModuleLength:
Enabled: false
Metrics/ClassLength:
Enabled: false
Lint/AssignmentInCondition:
Enabled: false
@@ -118,8 +115,9 @@ Style/ClassVars:
Style/PerlBackrefs:
Enabled: false
Style/TrivialAccessors:
AllowPredicates: true
Style/WordArray:
Enabled: false
Style/ModuleLength:
Exclude:
- lib/liquid/standardfilters.rb

View File

@@ -13,6 +13,11 @@ Lint/NestedMethodDefinition:
Metrics/AbcSize:
Max: 58
# Offense count: 16
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 314
# Offense count: 12
Metrics/CyclomaticComplexity:
Max: 15
@@ -27,6 +32,11 @@ Metrics/LineLength:
Metrics/MethodLength:
Max: 46
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 235
# Offense count: 6
Metrics/PerceivedComplexity:
Max: 13

View File

@@ -6,15 +6,10 @@ rvm:
- 2.2
- ruby-head
- jruby-head
# - rbx-2
- rbx-2
sudo: false
addons:
apt:
packages:
- libgmp3-dev
matrix:
allow_failures:
- rvm: jruby-head

View File

@@ -6,9 +6,9 @@ gem 'stackprof', platforms: :mri_21
group :test do
gem 'spy', '0.4.1'
gem 'benchmark-ips'
gem 'rubocop', '0.34.2'
gem 'rubocop'
platform :mri do
gem 'liquid-c', github: 'Shopify/liquid-c', ref: '2570693d8d03faa0df9160ec74348a7149436df3'
gem 'liquid-c', github: 'Shopify/liquid-c', ref: '35e9aee48d639ae1d3ac9ba77616aca9800eab7d'
end
end

View File

@@ -3,10 +3,6 @@
## 4.0.0 / not yet released / branch "master"
### Changed
* Improve loop performance (#681) [Florian Weingarten]
* Rename Drop method `before_method` to `liquid_method_missing` (#661) [Thierry Joyal]
* Add url_decode filter to invert url_encode (#645) [Larry Archer]
* Add global_filter to apply a filter to all output (#610) [Loren Hale]
* Add compact filter (#600) [Carson Reinke]
* Rename deprecated "has_key?" and "has_interrupt?" methods (#593) [Florian Weingarten]
* Include template name with line numbers in render errors (574) [Dylan Thacker-Smith]
@@ -19,11 +15,6 @@
* Remove support for `liquid_methods`
### Fixed
* Fix map filter when value is a Proc (#672) [Guillaume Malette]
* Fix truncate filter when value is not a string (#672) [Guillaume Malette]
* Fix behaviour of escape filter when input is nil (#665) [Tanel Jakobsoo]
* Fix sort filter behaviour with empty array input (#652) [Marcel Cary]
* Fix test failure under certain timezones (#631) [Dylan Thacker-Smith]
* Fix bug in uniq filter (#595) [Florian Weingarten]
* Fix bug when "blank" and "empty" are used as variable names (#592) [Florian Weingarten]
* Fix condition parse order in strict mode (#569) [Justin Li]
@@ -35,15 +26,7 @@
* Disallow variable names in the strict parser that are not valid in the lax parser (#463) [Justin Li]
* Fix BlockBody#warnings taking exponential time to compute (#486) [Justin Li]
## 3.0.5 / 2015-07-23 / branch "3-0-stable"
* Fix test failure under certain timezones [Dylan Thacker-Smith]
## 3.0.4 / 2015-07-17
* Fix chained access to multi-dimensional hashes [Florian Weingarten]
## 3.0.3 / 2015-05-28
## 3.0.3 / 2015-05-28 / branch "3-0-stable"
* Fix condition parse order in strict mode (#569) [Justin Li]
@@ -91,15 +74,7 @@
* Make map filter work on enumerable drops (#233) [Florian Weingarten]
* Improved whitespace stripping for blank blocks, related to #216 [Florian Weingarten]
## 2.6.3 / 2015-07-23 / branch "2-6-stable"
* Fix test failure under certain timezones [Dylan Thacker-Smith]
## 2.6.2 / 2015-01-23
* Remove duplicate hash key [Parker Moore]
## 2.6.1 / 2014-01-10
## 2.6.1 / 2014-01-10 / branch "2-6-stable"
Security fix, cherry-picked from master (4e14a65):
* Don't call to_sym when creating conditions for security reasons (#273) [Bouke van der Bijl]

View File

@@ -48,8 +48,6 @@ require 'liquid/lexer'
require 'liquid/parser'
require 'liquid/i18n'
require 'liquid/drop'
require 'liquid/tablerowloop_drop'
require 'liquid/forloop_drop'
require 'liquid/extensions'
require 'liquid/errors'
require 'liquid/interrupts'
@@ -71,7 +69,7 @@ require 'liquid/standardfilters'
require 'liquid/condition'
require 'liquid/utils'
require 'liquid/tokenizer'
require 'liquid/parse_context'
require 'liquid/token'
# Load all the tags of the standard library
#

View File

@@ -23,17 +23,29 @@ module Liquid
@body.nodelist
end
# warnings of this block and all sub-tags
def warnings
all_warnings = []
all_warnings.concat(@warnings) if @warnings
(nodelist || []).each do |node|
all_warnings.concat(node.warnings || []) if node.respond_to?(:warnings)
end
all_warnings
end
def unknown_tag(tag, _params, _tokens)
case tag
when 'else'.freeze
raise SyntaxError.new(parse_context.locale.t("errors.syntax.unexpected_else".freeze,
raise SyntaxError.new(options[:locale].t("errors.syntax.unexpected_else".freeze,
block_name: block_name))
when 'end'.freeze
raise SyntaxError.new(parse_context.locale.t("errors.syntax.invalid_delimiter".freeze,
raise SyntaxError.new(options[:locale].t("errors.syntax.invalid_delimiter".freeze,
block_name: block_name,
block_delimiter: block_delimiter))
else
raise SyntaxError.new(parse_context.locale.t("errors.syntax.unknown_tag".freeze, tag: tag))
raise SyntaxError.new(options[:locale].t("errors.syntax.unknown_tag".freeze, tag: tag))
end
end
@@ -48,12 +60,12 @@ module Liquid
protected
def parse_body(body, tokens)
body.parse(tokens, parse_context) do |end_tag_name, end_tag_params|
body.parse(tokens, options) do |end_tag_name, end_tag_params|
@blank &&= body.blank?
return false if end_tag_name == block_delimiter
unless end_tag_name
raise SyntaxError.new(parse_context.locale.t("errors.syntax.tag_never_closed".freeze, block_name: block_name))
raise SyntaxError.new(@options[:locale].t("errors.syntax.tag_never_closed".freeze, block_name: block_name))
end
# this tag is not registered with the system

View File

@@ -12,37 +12,44 @@ module Liquid
@blank = true
end
def parse(tokenizer, parse_context)
parse_context.line_number = tokenizer.line_number
while token = tokenizer.shift
unless token.empty?
case
when token.start_with?(TAGSTART)
if token =~ FullToken
tag_name = $1
markup = $2
# fetch the tag from registered blocks
if tag = registered_tags[tag_name]
new_tag = tag.parse(tag_name, markup, tokenizer, parse_context)
@blank &&= new_tag.blank?
@nodelist << new_tag
def parse(tokens, options)
while token = tokens.shift
begin
unless token.empty?
case
when token.start_with?(TAGSTART)
if token =~ FullToken
tag_name = $1
markup = $2
# fetch the tag from registered blocks
if tag = registered_tags[tag_name]
markup = token.child(markup) if token.is_a?(Token)
new_tag = tag.parse(tag_name, markup, tokens, options)
new_tag.line_number = token.line_number if token.is_a?(Token)
@blank &&= new_tag.blank?
@nodelist << new_tag
else
# end parsing if we reach an unknown tag and let the caller decide
# determine how to proceed
return yield tag_name, markup
end
else
# end parsing if we reach an unknown tag and let the caller decide
# determine how to proceed
return yield tag_name, markup
raise_missing_tag_terminator(token, options)
end
when token.start_with?(VARSTART)
new_var = create_variable(token, options)
new_var.line_number = token.line_number if token.is_a?(Token)
@nodelist << new_var
@blank = false
else
raise_missing_tag_terminator(token, parse_context)
@nodelist << token
@blank &&= !!(token =~ /\A\s*\z/)
end
when token.start_with?(VARSTART)
@nodelist << create_variable(token, parse_context)
@blank = false
else
@nodelist << token
@blank &&= !!(token =~ /\A\s*\z/)
end
rescue SyntaxError => e
e.set_line_number_from_token(token)
raise
end
parse_context.line_number = tokenizer.line_number
end
yield nil, nil
@@ -52,6 +59,14 @@ module Liquid
@blank
end
def warnings
all_warnings = []
nodelist.each do |node|
all_warnings.concat(node.warnings || []) if node.respond_to?(:warnings)
end
all_warnings
end
def render(context)
output = []
context.resource_limits.render_score += @nodelist.length
@@ -69,15 +84,15 @@ module Liquid
break
end
node_output = render_node(token, context)
token_output = render_token(token, context)
unless token.is_a?(Block) && token.blank?
output << node_output
output << token_output
end
rescue MemoryError => e
raise e
rescue ::StandardError => e
output << context.handle_error(e, token.line_number)
output << context.handle_error(e, token)
end
end
@@ -86,31 +101,31 @@ module Liquid
private
def render_node(node, context)
node_output = (node.respond_to?(:render) ? node.render(context) : node)
node_output = node_output.is_a?(Array) ? node_output.join : node_output.to_s
def render_token(token, context)
token_output = (token.respond_to?(:render) ? token.render(context) : token)
token_str = token_output.is_a?(Array) ? token_output.join : token_output.to_s
context.resource_limits.render_length += node_output.length
context.resource_limits.render_length += token_str.length
if context.resource_limits.reached?
raise MemoryError.new("Memory limits exceeded".freeze)
end
node_output
token_str
end
def create_variable(token, parse_context)
def create_variable(token, options)
token.scan(ContentOfVariable) do |content|
markup = content.first
return Variable.new(markup, parse_context)
markup = token.is_a?(Token) ? token.child(content.first) : content.first
return Variable.new(markup, options)
end
raise_missing_variable_terminator(token, parse_context)
raise_missing_variable_terminator(token, options)
end
def raise_missing_tag_terminator(token, parse_context)
raise SyntaxError.new(parse_context.locale.t("errors.syntax.tag_termination".freeze, token: token, tag_end: TagEnd.inspect))
def raise_missing_tag_terminator(token, options)
raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination".freeze, token: token, tag_end: TagEnd.inspect))
end
def raise_missing_variable_terminator(token, parse_context)
raise SyntaxError.new(parse_context.locale.t("errors.syntax.variable_termination".freeze, token: token, tag_end: VariableEnd.inspect))
def raise_missing_variable_terminator(token, options)
raise SyntaxError.new(options[:locale].t("errors.syntax.variable_termination".freeze, token: token, tag_end: VariableEnd.inspect))
end
def registered_tags

View File

@@ -13,14 +13,13 @@ module Liquid
# context['bob'] #=> nil class Context
class Context
attr_reader :scopes, :errors, :registers, :environments, :resource_limits
attr_accessor :exception_handler, :template_name, :partial, :global_filter
attr_accessor :exception_handler, :template_name
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil)
@environments = [environments].flatten
@scopes = [(outer_scope || {})]
@registers = registers
@errors = []
@partial = false
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
squash_instance_assigns_with_environments
@@ -32,11 +31,6 @@ module Liquid
@interrupts = []
@filters = []
@global_filter = nil
end
def warnings
@warnings ||= []
end
def strainer
@@ -53,10 +47,6 @@ module Liquid
@strainer = nil
end
def apply_global_filter(obj)
global_filter.nil? ? obj : global_filter.call(obj)
end
# are there any not handled interrupts?
def interrupt?
!@interrupts.empty?
@@ -72,10 +62,10 @@ module Liquid
@interrupts.pop
end
def handle_error(e, line_number = nil)
def handle_error(e, token = nil)
if e.is_a?(Liquid::Error)
e.template_name ||= template_name
e.line_number ||= line_number
e.template_name = template_name
e.set_line_number_from_token(token)
end
output = nil
@@ -85,10 +75,7 @@ module Liquid
case result
when Exception
e = result
if e.is_a?(Liquid::Error)
e.template_name ||= template_name
e.line_number ||= line_number
end
e.set_line_number_from_token(token) if e.is_a?(Liquid::Error)
when String
output = result
else

View File

@@ -1,26 +1,27 @@
module Liquid
class Document < BlockBody
def self.parse(tokens, parse_context)
DEFAULT_OPTIONS = {
locale: I18n.new
}
def self.parse(tokens, options)
doc = new
doc.parse(tokens, parse_context)
doc.parse(tokens, DEFAULT_OPTIONS.merge(options))
doc
end
def parse(tokens, parse_context)
def parse(tokens, options)
super do |end_tag_name, end_tag_params|
unknown_tag(end_tag_name, parse_context) if end_tag_name
unknown_tag(end_tag_name, options) if end_tag_name
end
rescue SyntaxError => e
e.line_number ||= parse_context.line_number
raise
end
def unknown_tag(tag, parse_context)
def unknown_tag(tag, options)
case tag
when 'else'.freeze, 'end'.freeze
raise SyntaxError.new(parse_context.locale.t("errors.syntax.unexpected_outer_tag".freeze, tag: tag))
raise SyntaxError.new(options[:locale].t("errors.syntax.unexpected_outer_tag".freeze, tag: tag))
else
raise SyntaxError.new(parse_context.locale.t("errors.syntax.unknown_tag".freeze, tag: tag))
raise SyntaxError.new(options[:locale].t("errors.syntax.unknown_tag".freeze, tag: tag))
end
end
end

View File

@@ -18,22 +18,24 @@ module Liquid
# tmpl = Liquid::Template.parse( ' {% for product in product.top_sales %} {{ product.name }} {%endfor%} ' )
# tmpl.render('product' => ProductDrop.new ) # will invoke top_sales query.
#
# Your drop can either implement the methods sans any parameters
# or implement the liquid_method_missing(name) method which is a catch all.
# Your drop can either implement the methods sans any parameters or implement the before_method(name) method which is a
# catch all.
class Drop
attr_writer :context
EMPTY_STRING = ''.freeze
# Catch all for the method
def liquid_method_missing(_method)
def before_method(_method)
nil
end
# called by liquid to invoke a drop
def invoke_drop(method_or_key)
if self.class.invokable?(method_or_key)
if method_or_key && method_or_key != EMPTY_STRING && self.class.invokable?(method_or_key)
send(method_or_key)
else
liquid_method_missing(method_or_key)
before_method(method_or_key)
end
end

View File

@@ -17,6 +17,12 @@ module Liquid
str
end
def set_line_number_from_token(token)
return unless token.respond_to?(:line_number)
return if line_number
self.line_number = token.line_number
end
def self.render(e)
if e.is_a?(Liquid::Error)
e.to_s

View File

@@ -7,44 +7,44 @@ class String # :nodoc:
end
end
class Array # :nodoc:
class Array # :nodoc:
def to_liquid
self
end
end
class Hash # :nodoc:
class Hash # :nodoc:
def to_liquid
self
end
end
class Numeric # :nodoc:
class Numeric # :nodoc:
def to_liquid
self
end
end
class Time # :nodoc:
class Time # :nodoc:
def to_liquid
self
end
end
class DateTime < Date # :nodoc:
class DateTime < Date # :nodoc:
def to_liquid
self
end
end
class Date # :nodoc:
class Date # :nodoc:
def to_liquid
self
end
end
class TrueClass
def to_liquid # :nodoc:
def to_liquid # :nodoc:
self
end
end
@@ -60,9 +60,3 @@ class NilClass
self
end
end
class Proc
def to_liquid # :nodoc:
self
end
end

View File

@@ -1,42 +0,0 @@
module Liquid
class ForloopDrop < Drop
def initialize(name, length, parentloop)
@name = name
@length = length
@parentloop = parentloop
@index = 0
end
attr_reader :name, :length, :parentloop
def index
@index + 1
end
def index0
@index
end
def rindex
@length - @index
end
def rindex0
@length - @index - 1
end
def first
@index == 0
end
def last
@index == @length - 1
end
protected
def increment!
@index += 1
end
end
end

View File

@@ -1,37 +0,0 @@
module Liquid
class ParseContext
attr_accessor :partial, :locale, :line_number
attr_reader :warnings, :error_mode
def initialize(options = {})
@template_options = options ? options.dup : {}
@locale = @template_options[:locale] ||= I18n.new
@warnings = []
self.partial = false
end
def [](option_key)
@options[option_key]
end
def partial=(value)
@partial = value
@options = value ? partial_options : @template_options
@error_mode = @options[:error_mode] || Template.error_mode
value
end
def partial_options
@partial_options ||= begin
dont_pass = @template_options[:include_options_blacklist]
if dont_pass == true
{ locale: locale }
elsif dont_pass.is_a?(Array)
@template_options.reject { |k, v| dont_pass.include?(k) }
else
@template_options
end
end
end
end
end

View File

@@ -75,7 +75,7 @@ module Liquid
def variable_signature
str = consume(:id)
while look(:open_square)
if look(:open_square)
str << consume
str << expression
str << consume(:close_square)

View File

@@ -1,14 +1,16 @@
module Liquid
module ParserSwitching
def parse_with_selected_parser(markup)
case parse_context.error_mode
case @options[:error_mode] || Template.error_mode
when :strict then strict_parse_with_error_context(markup)
when :lax then lax_parse(markup)
when :warn
begin
return strict_parse_with_error_context(markup)
rescue SyntaxError => e
parse_context.warnings << e
e.set_line_number_from_token(markup)
@warnings ||= []
@warnings << e
return lax_parse(markup)
end
end
@@ -19,7 +21,6 @@ module Liquid
def strict_parse_with_error_context(markup)
strict_parse(markup)
rescue SyntaxError => e
e.line_number = line_number
e.markup_context = markup_context(markup)
raise e
end

View File

@@ -19,7 +19,7 @@ module Liquid
# inside of <tt>{% include %}</tt> tags.
#
# profile.each do |node|
# # Access to the node itself
# # Access to the token itself
# node.code
#
# # Which template and line number of this node.
@@ -46,15 +46,15 @@ module Liquid
class Timing
attr_reader :code, :partial, :line_number, :children
def initialize(node, partial)
@code = node.respond_to?(:raw) ? node.raw : node
def initialize(token, partial)
@code = token.respond_to?(:raw) ? token.raw : token
@partial = partial
@line_number = node.respond_to?(:line_number) ? node.line_number : nil
@line_number = token.respond_to?(:line_number) ? token.line_number : nil
@children = []
end
def self.start(node, partial)
new(node, partial).tap(&:start)
def self.start(token, partial)
new(token, partial).tap(&:start)
end
def start
@@ -70,11 +70,11 @@ module Liquid
end
end
def self.profile_node_render(node)
if Profiler.current_profile && node.respond_to?(:render)
Profiler.current_profile.start_node(node)
def self.profile_token_render(token)
if Profiler.current_profile && token.respond_to?(:render)
Profiler.current_profile.start_token(token)
output = yield
Profiler.current_profile.end_node(node)
Profiler.current_profile.end_token(token)
output
else
yield
@@ -132,11 +132,11 @@ module Liquid
@root_timing.children.length
end
def start_node(node)
@timing_stack.push(Timing.start(node, current_partial))
def start_token(token)
@timing_stack.push(Timing.start(token, current_partial))
end
def end_node(_node)
def end_token(_token)
timing = @timing_stack.pop
timing.finish

View File

@@ -1,13 +1,13 @@
module Liquid
class BlockBody
def render_node_with_profiling(node, context)
Profiler.profile_node_render(node) do
render_node_without_profiling(node, context)
def render_token_with_profiling(token, context)
Profiler.profile_token_render(token) do
render_token_without_profiling(token, context)
end
end
alias_method :render_node_without_profiling, :render_node
alias_method :render_node, :render_node_with_profiling
alias_method :render_token_without_profiling, :render_token
alias_method :render_token, :render_token_with_profiling
end
class Include < Tag

View File

@@ -16,22 +16,7 @@ module Liquid
end
def evaluate(context)
start_int = to_integer(context.evaluate(@start_obj))
end_int = to_integer(context.evaluate(@end_obj))
start_int..end_int
end
private
def to_integer(input)
case input
when Integer
input
when NilClass, String
input.to_i
else
Utils.to_integer(input)
end
context.evaluate(@start_obj).to_i..context.evaluate(@end_obj).to_i
end
end
end

View File

@@ -33,7 +33,7 @@ module Liquid
end
def escape(input)
CGI.escapeHTML(input).untaint unless input.nil?
CGI.escapeHTML(input).untaint rescue input
end
alias_method :h, :escape
@@ -42,16 +42,12 @@ module Liquid
end
def url_encode(input)
CGI.escape(input) unless input.nil?
end
def url_decode(input)
CGI.unescape(input) unless input.nil?
CGI.escape(input) rescue input
end
def slice(input, offset, length = nil)
offset = Utils.to_integer(offset)
length = length ? Utils.to_integer(length) : 1
offset = to_integer(offset)
length = length ? to_integer(length) : 1
if input.is_a?(Array)
input.slice(offset, length) || []
@@ -63,17 +59,16 @@ module Liquid
# Truncate a string down to x characters
def truncate(input, length = 50, truncate_string = "...".freeze)
return if input.nil?
input_str = input.to_s
length = Utils.to_integer(length)
length = to_integer(length)
l = length - truncate_string.length
l = 0 if l < 0
input_str.length > length ? input_str[0...l] + truncate_string : input_str
input.length > length ? input[0...l] + truncate_string : input
end
def truncatewords(input, words = 15, truncate_string = "...".freeze)
return if input.nil?
wordlist = input.to_s.split
words = Utils.to_integer(words)
words = to_integer(words)
l = words - 1
l = 0 if l < 0
wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string : input
@@ -110,6 +105,11 @@ module Liquid
input.to_s.gsub(/\r?\n/, ''.freeze)
end
# Remove whitespace between HTML tags
def strip_html_whitespace(input)
Spaceless.strip_html_whitespace(input.to_s)
end
# Join elements of the array with certain character between them
def join(input, glue = ' '.freeze)
InputIterator.new(input).join(glue)
@@ -121,8 +121,6 @@ module Liquid
ary = InputIterator.new(input)
if property.nil?
ary.sort
elsif ary.empty? # The next two cases assume a non-empty array.
[]
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
ary.sort { |a, b| a[property] <=> b[property] }
elsif ary.first.respond_to?(property)
@@ -137,8 +135,6 @@ module Liquid
if property.nil?
ary.sort { |a, b| a.casecmp(b) }
elsif ary.empty? # The next two cases assume a non-empty array.
[]
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
ary.sort { |a, b| a[property].casecmp(b[property]) }
elsif ary.first.respond_to?(property)
@@ -153,8 +149,6 @@ module Liquid
if property.nil?
ary.uniq
elsif ary.empty? # The next two cases assume a non-empty array.
[]
elsif ary.first.respond_to?(:[])
ary.uniq{ |a| a[property] }
end
@@ -174,8 +168,7 @@ module Liquid
if property == "to_liquid".freeze
e
elsif e.respond_to?(:[])
r = e[property]
r.is_a?(Proc) ? r.call : r
e[property]
end
end
end
@@ -187,8 +180,6 @@ module Liquid
if property.nil?
ary.compact
elsif ary.empty? # The next two cases assume a non-empty array.
[]
elsif ary.first.respond_to?(:[])
ary.reject{ |a| a[property].nil? }
elsif ary.first.respond_to?(property)
@@ -269,7 +260,7 @@ module Liquid
def date(input, format)
return input if format.to_s.empty?
return input unless date = Utils.to_date(input)
return input unless date = to_date(input)
date.strftime(format.to_s)
end
@@ -321,7 +312,7 @@ module Liquid
end
def round(input, n = 0)
result = Utils.to_number(input).round(Utils.to_number(n))
result = to_number(input).round(to_number(n))
result = result.to_f if result.is_a?(BigDecimal)
result = result.to_i if n == 0
result
@@ -330,29 +321,69 @@ module Liquid
end
def ceil(input)
Utils.to_number(input).ceil.to_i
to_number(input).ceil.to_i
rescue ::FloatDomainError => e
raise Liquid::FloatDomainError, e.message
end
def floor(input)
Utils.to_number(input).floor.to_i
to_number(input).floor.to_i
rescue ::FloatDomainError => e
raise Liquid::FloatDomainError, e.message
end
def default(input, default_value = ''.freeze)
if !input || input.respond_to?(:empty?) && input.empty?
default_value
else
input
end
def default(input, default_value = "".freeze)
is_blank = input.respond_to?(:empty?) ? input.empty? : !input
is_blank ? default_value : input
end
private
def to_integer(num)
return num if num.is_a?(Integer)
num = num.to_s
begin
Integer(num)
rescue ::ArgumentError
raise Liquid::ArgumentError, "invalid integer"
end
end
def to_number(obj)
case obj
when Float
BigDecimal.new(obj.to_s)
when Numeric
obj
when String
(obj.strip =~ /\A\d+\.\d+\z/) ? BigDecimal.new(obj) : obj.to_i
else
0
end
end
def to_date(obj)
return obj if obj.respond_to?(:strftime)
if obj.is_a?(String)
return nil if obj.empty?
obj = obj.downcase
end
case obj
when 'now'.freeze, 'today'.freeze
Time.now
when /\A\d+\z/, Integer
Time.at(obj.to_i)
when String
Time.parse(obj)
end
rescue ArgumentError
nil
end
def apply_operation(input, operand, operation)
result = Utils.to_number(input).send(operation, Utils.to_number(operand))
result = to_number(input).send(operation, to_number(operand))
result.is_a?(BigDecimal) ? result.to_f : result
end
@@ -391,14 +422,9 @@ module Liquid
to_a.compact
end
def empty?
@input.each { return false }
true
end
def each
@input.each do |e|
yield(e.to_liquid)
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
end
end
end

View File

@@ -52,7 +52,7 @@ module Liquid
args.first
end
rescue ::ArgumentError => e
raise Liquid::ArgumentError, e.message, e.backtrace
raise Liquid::ArgumentError.new(e.message)
end
end
end

View File

@@ -1,62 +0,0 @@
module Liquid
class TablerowloopDrop < Drop
def initialize(length, cols)
@length = length
@row = 1
@col = 1
@cols = cols
@index = 0
end
attr_reader :length, :col, :row
def index
@index + 1
end
def index0
@index
end
def col0
@col - 1
end
def rindex
@length - @index
end
def rindex0
@length - @index - 1
end
def first
@index == 0
end
def last
@index == @length - 1
end
def col_first
@col == 1
end
def col_last
@col == @cols
end
protected
def increment!
@index += 1
if @col == @cols
@col = 1
@row += 1
else
@col += 1
end
end
end
end

View File

@@ -1,24 +1,23 @@
module Liquid
class Tag
attr_reader :nodelist, :tag_name, :line_number, :parse_context
alias_method :options, :parse_context
attr_accessor :options, :line_number
attr_reader :nodelist, :warnings
include ParserSwitching
class << self
def parse(tag_name, markup, tokenizer, options)
def parse(tag_name, markup, tokens, options)
tag = new(tag_name, markup, options)
tag.parse(tokenizer)
tag.parse(tokens)
tag
end
private :new
end
def initialize(tag_name, markup, parse_context)
def initialize(tag_name, markup, options)
@tag_name = tag_name
@markup = markup
@parse_context = parse_context
@line_number = parse_context.line_number
@options = options
end
def parse(_tokens)

View File

@@ -15,6 +15,7 @@ module Liquid
if markup =~ Syntax
@to = $1
@from = Variable.new($2, options)
@from.line_number = line_number
else
raise SyntaxError.new options[:locale].t("errors.syntax.assign".freeze)
end

View File

@@ -37,7 +37,7 @@ module Liquid
iteration = context.registers[:cycle][key]
result = context.evaluate(@variables[iteration])
iteration += 1
iteration = 0 if iteration >= @variables.size
iteration = 0 if iteration >= @variables.size
context.registers[:cycle][key] = iteration
result
end

View File

@@ -67,13 +67,69 @@ module Liquid
end
def render(context)
segment = collection_segment(context)
for_offsets = context.registers[:for] ||= Hash.new(0)
for_stack = context.registers[:for_stack] ||= []
if segment.empty?
render_else(context)
parent_loop = for_stack.last
for_stack.push(nil)
collection = context.evaluate(@collection_name)
collection = collection.to_a if collection.is_a?(Range)
from = if @from == :continue
for_offsets[@name].to_i
else
render_segment(context, segment)
context.evaluate(@from).to_i
end
limit = context.evaluate(@limit)
to = limit ? limit.to_i + from : nil
segment = Utils.slice_collection(collection, from, to)
return render_else(context) if segment.empty?
segment.reverse! if @reversed
result = ''
length = segment.length
# Store our progress through the collection for the continue flag
for_offsets[@name] = from + segment.length
context.stack do
segment.each_with_index do |item, index|
context[@variable_name] = item
loop_vars = {
'name'.freeze => @name,
'length'.freeze => length,
'index'.freeze => index + 1,
'index0'.freeze => index,
'rindex'.freeze => length - index,
'rindex0'.freeze => length - index - 1,
'first'.freeze => (index == 0),
'last'.freeze => (index == length - 1),
'parentloop'.freeze => parent_loop
}
context['forloop'.freeze] = loop_vars
for_stack[-1] = loop_vars
result << @for_block.render(context)
# Handle any interrupts if they exist.
if context.interrupt?
interrupt = context.pop_interrupt
break if interrupt.is_a? BreakInterrupt
next if interrupt.is_a? ContinueInterrupt
end
end
end
result
ensure
for_stack.pop
end
protected
@@ -96,7 +152,7 @@ module Liquid
def strict_parse(markup)
p = Parser.new(markup)
@variable_name = p.consume(:id)
raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_in".freeze)) unless p.id?('in'.freeze)
raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_in".freeze)) unless p.id?('in'.freeze)
collection_name = p.expression
@name = "#{@variable_name}-#{collection_name}"
@collection_name = Expression.parse(collection_name)
@@ -114,63 +170,6 @@ module Liquid
private
def collection_segment(context)
offsets = context.registers[:for] ||= Hash.new(0)
from = if @from == :continue
offsets[@name].to_i
else
context.evaluate(@from).to_i
end
collection = context.evaluate(@collection_name)
collection = collection.to_a if collection.is_a?(Range)
limit = context.evaluate(@limit)
to = limit ? limit.to_i + from : nil
segment = Utils.slice_collection(collection, from, to)
segment.reverse! if @reversed
offsets[@name] = from + segment.length
segment
end
def render_segment(context, segment)
for_stack = context.registers[:for_stack] ||= []
length = segment.length
result = ''
context.stack do
loop_vars = Liquid::ForloopDrop.new(@name, length, for_stack[-1])
for_stack.push(loop_vars)
begin
context['forloop'.freeze] = loop_vars
segment.each_with_index do |item, index|
context[@variable_name] = item
result << @for_block.render(context)
loop_vars.send(:increment!)
# Handle any interrupts if they exist.
if context.interrupt?
interrupt = context.pop_interrupt
break if interrupt.is_a? BreakInterrupt
next if interrupt.is_a? ContinueInterrupt
end
end
ensure
for_stack.pop
end
end
result
end
def set_attribute(key, expr)
case key
when 'offset'.freeze

View File

@@ -53,10 +53,8 @@ module Liquid
end
old_template_name = context.template_name
old_partial = context.partial
begin
context.template_name = template_name
context.partial = true
context.stack do
@attributes.each do |key, value|
context[key] = context.evaluate(value)
@@ -74,15 +72,11 @@ module Liquid
end
ensure
context.template_name = old_template_name
context.partial = old_partial
end
end
private
alias_method :parse_context, :options
private :parse_context
def load_cached_partial(template_name, context)
cached_partials = context.registers[:cached_partials] || {}
@@ -90,12 +84,7 @@ module Liquid
return cached
end
source = read_template_from_file_system(context)
begin
parse_context.partial = true
partial = Liquid::Template.parse(source, parse_context)
ensure
parse_context.partial = false
end
partial = Liquid::Template.parse(source, pass_options)
cached_partials[template_name] = partial
context.registers[:cached_partials] = cached_partials
partial
@@ -106,6 +95,16 @@ module Liquid
file_system.read_template_file(context.evaluate(@template_name_expr))
end
def pass_options
dont_pass = @options[:include_options_blacklist]
return { locale: @options[:locale] } if dont_pass == true
opts = @options.merge(included: true, include_options_blacklist: false)
if dont_pass.is_a?(Array)
dont_pass.each { |o| opts.delete(o) }
end
opts
end
end
Template.register_tag('include'.freeze, Include)

View File

@@ -3,11 +3,11 @@ module Liquid
Syntax = /\A\s*\z/
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
def initialize(tag_name, markup, parse_context)
def initialize(tag_name, markup, options)
super
unless markup =~ Syntax
raise SyntaxError.new(parse_context.locale.t("errors.syntax.tag_unexpected_args".freeze, tag: tag_name))
raise SyntaxError.new(@options[:locale].t("errors.syntax.tag_unexpected_args".freeze, tag: tag_name))
end
end
@@ -21,7 +21,7 @@ module Liquid
@body << token unless token.empty?
end
raise SyntaxError.new(parse_context.locale.t("errors.syntax.tag_never_closed".freeze, block_name: block_name))
raise SyntaxError.new(@options[:locale].t("errors.syntax.tag_never_closed".freeze, block_name: block_name))
end
def render(_context)

View File

@@ -0,0 +1,24 @@
module Liquid
# The spaceless tag strips whitespace between HTML tags using a simple regex.
#
# == Usage:
# {% spaceless %}
# <h1>
# Hello
# </h1>
# {% endspaceless %}
#
class Spaceless < Block
HTML_STRIP_SPACE_REGEXP = />\s+</
def render(context)
self.class.strip_html_whitespace(super)
end
def self.strip_html_whitespace(input)
input.gsub(HTML_STRIP_SPACE_REGEXP, '><')
end
end
Template.register_tag('spaceless'.freeze, Spaceless)
end

View File

@@ -28,21 +28,36 @@ module Liquid
cols = context.evaluate(@attributes['cols'.freeze]).to_i
row = 1
col = 0
result = "<tr class=\"row1\">\n"
context.stack do
tablerowloop = Liquid::TablerowloopDrop.new(length, cols)
context['tablerowloop'.freeze] = tablerowloop
collection.each_with_index do |item, index|
context[@variable_name] = item
context['tablerowloop'.freeze] = {
'length'.freeze => length,
'index'.freeze => index + 1,
'index0'.freeze => index,
'col'.freeze => col + 1,
'col0'.freeze => col,
'rindex'.freeze => length - index,
'rindex0'.freeze => length - index - 1,
'first'.freeze => (index == 0),
'last'.freeze => (index == length - 1),
'col_first'.freeze => (col == 0),
'col_last'.freeze => (col == cols - 1)
}
result << "<td class=\"col#{tablerowloop.col}\">" << super << '</td>'
col += 1
if tablerowloop.col_last && !tablerowloop.last
result << "</tr>\n<tr class=\"row#{tablerowloop.row + 1}\">"
result << "<td class=\"col#{col}\">" << super << '</td>'
if col == cols && (index != length - 1)
col = 0
row += 1
result << "</tr>\n<tr class=\"row#{row}\">"
end
tablerowloop.send(:increment!)
end
end
result << "</tr>\n"

View File

@@ -14,7 +14,7 @@ module Liquid
#
class Template
attr_accessor :root
attr_reader :resource_limits, :warnings
attr_reader :resource_limits
@@file_system = BlankFileSystem.new
@@ -116,12 +116,16 @@ module Liquid
@options = options
@profiling = options[:profile]
@line_numbers = options[:line_numbers] || @profiling
parse_context = options.is_a?(ParseContext) ? options : ParseContext.new(options)
@root = Document.parse(tokenize(source), parse_context)
@warnings = parse_context.warnings
@root = Document.parse(tokenize(source), options)
@warnings = nil
self
end
def warnings
return [] unless @root
@warnings ||= @root.warnings
end
def registers
@registers ||= {}
end
@@ -179,15 +183,20 @@ module Liquid
when Hash
options = args.pop
registers.merge!(options[:registers]) if options[:registers].is_a?(Hash)
if options[:registers].is_a?(Hash)
registers.merge!(options[:registers])
end
context.add_filters(options[:filters]) if options[:filters]
if options[:filters]
context.add_filters(options[:filters])
end
context.global_filter = options[:global_filter] if options[:global_filter]
context.exception_handler = options[:exception_handler] if options[:exception_handler]
when Module, Array
if options[:exception_handler]
context.exception_handler = options[:exception_handler]
end
when Module
context.add_filters(args.pop)
when Array
context.add_filters(args.pop)
end
@@ -197,7 +206,7 @@ module Liquid
begin
# render the nodelist.
# for performance reasons we get an array back here. join will make a string out of it.
result = with_profiling(context) do
result = with_profiling do
@root.render(context)
end
result.respond_to?(:join) ? result.join : result
@@ -219,8 +228,8 @@ module Liquid
Tokenizer.new(source, @line_numbers)
end
def with_profiling(context)
if @profiling && !context.partial
def with_profiling
if @profiling && !@options[:included]
raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler)
@profiler = Profiler.new

18
lib/liquid/token.rb Normal file
View File

@@ -0,0 +1,18 @@
module Liquid
class Token < String
attr_reader :line_number
def initialize(content, line_number)
super(content)
@line_number = line_number
end
def raw
"<raw>"
end
def child(string)
Token.new(string, @line_number)
end
end
end

View File

@@ -1,17 +1,13 @@
module Liquid
class Tokenizer
attr_reader :line_number
def initialize(source, line_numbers = false)
@source = source
@line_number = 1 if line_numbers
@line_numbers = line_numbers
@tokens = tokenize
end
def shift
token = @tokens.shift
@line_number += token.count("\n") if @line_number && token
token
@tokens.shift
end
private
@@ -21,11 +17,21 @@ module Liquid
return [] if @source.to_s.empty?
tokens = @source.split(TemplateParser)
tokens = @line_numbers ? calculate_line_numbers(tokens) : tokens
# removes the rogue empty element at the beginning of the array
tokens.shift if tokens[0] && tokens[0].empty?
tokens
end
def calculate_line_numbers(tokens)
current_line = 1
tokens.map do |token|
Token.new(token, current_line).tap do
current_line += token.count("\n")
end
end
end
end
end

View File

@@ -32,48 +32,5 @@ module Liquid
segments
end
def self.to_integer(num)
return num if num.is_a?(Integer)
num = num.to_s
begin
Integer(num)
rescue ::ArgumentError
raise Liquid::ArgumentError, "invalid integer"
end
end
def self.to_number(obj)
case obj
when Float
BigDecimal.new(obj.to_s)
when Numeric
obj
when String
(obj.strip =~ /\A\d+\.\d+\z/) ? BigDecimal.new(obj) : obj.to_i
else
0
end
end
def self.to_date(obj)
return obj if obj.respond_to?(:strftime)
if obj.is_a?(String)
return nil if obj.empty?
obj = obj.downcase
end
case obj
when 'now'.freeze, 'today'.freeze
Time.now
when /\A\d+\z/, Integer
Time.at(obj.to_i)
when String
Time.parse(obj)
end
rescue ::ArgumentError
nil
end
end
end

View File

@@ -11,16 +11,14 @@ module Liquid
#
class Variable
FilterParser = /(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o
attr_accessor :filters, :name, :line_number
attr_reader :parse_context
alias_method :options, :parse_context
attr_accessor :filters, :name, :warnings
attr_accessor :line_number
include ParserSwitching
def initialize(markup, parse_context)
def initialize(markup, options = {})
@markup = markup
@name = nil
@parse_context = parse_context
@line_number = parse_context.line_number
@options = options || {}
parse_with_selected_parser(markup)
end
@@ -73,16 +71,10 @@ module Liquid
end
def render(context)
obj = @filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)|
@filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)|
filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
context.invoke(filter_name, output, *filter_args)
end
obj = context.apply_global_filter(obj)
taint_check(context, obj)
obj
end.tap{ |obj| taint_check(obj) }
end
private
@@ -114,22 +106,17 @@ module Liquid
parsed_args
end
def taint_check(context, obj)
return unless obj.tainted?
return if Template.taint_mode == :lax
@markup =~ QuotedFragment
name = Regexp.last_match(0)
error = TaintedError.new("variable '#{name}' is tainted and was not escaped")
error.line_number = line_number
error.template_name = context.template_name
case Template.taint_mode
when :warn
context.warnings << error
when :error
raise error
def taint_check(obj)
if obj.tainted?
@markup =~ QuotedFragment
name = Regexp.last_match(0)
case Template.taint_mode
when :warn
@warnings ||= []
@warnings << "variable '#{name}' is tainted and was not escaped"
when :error
raise TaintedError, "Error - variable '#{name}' is tainted and was not escaped"
end
end
end
end

View File

@@ -1,4 +1,4 @@
# encoding: utf-8
module Liquid
VERSION = "4.0.0.rc1"
VERSION = "4.0.0.alpha"
end

View File

@@ -18,9 +18,9 @@ Gem::Specification.new do |s|
s.required_rubygems_version = ">= 1.3.7"
s.test_files = Dir.glob("{test}/**/*")
s.files = Dir.glob("{lib}/**/*") + %w(LICENSE README.md)
s.files = Dir.glob("{lib}/**/*") + %w(MIT-LICENSE README.md)
s.extra_rdoc_files = ["History.md", "README.md"]
s.extra_rdoc_files = ["History.md", "README.md"]
s.require_path = "lib"

View File

@@ -13,7 +13,7 @@ class ContextDrop < Liquid::Drop
@context['forloop.index']
end
def liquid_method_missing(method)
def before_method(method)
@context[method]
end
end
@@ -30,8 +30,8 @@ class ProductDrop < Liquid::Drop
end
class CatchallDrop < Liquid::Drop
def liquid_method_missing(method)
'catchall_method: ' << method.to_s
def before_method(method)
'method: ' << method.to_s
end
end
@@ -59,7 +59,7 @@ class ProductDrop < Liquid::Drop
end
class EnumerableDrop < Liquid::Drop
def liquid_method_missing(method)
def before_method(method)
method
end
@@ -93,7 +93,7 @@ end
class RealEnumerableDrop < Liquid::Drop
include Enumerable
def liquid_method_missing(method)
def before_method(method)
method
end
@@ -124,10 +124,8 @@ class DropsTest < Minitest::Test
def test_rendering_warns_on_tainted_attr
with_taint_mode(:warn) do
tpl = Liquid::Template.parse('{{ product.user_input }}')
context = Context.new('product' => ProductDrop.new)
tpl.render!(context)
assert_equal [Liquid::TaintedError], context.warnings.map(&:class)
assert_equal "variable 'product.user_input' is tainted and was not escaped", context.warnings.first.to_s(false)
tpl.render!('product' => ProductDrop.new)
assert_match /tainted/, tpl.warnings.first
end
end
@@ -157,14 +155,14 @@ class DropsTest < Minitest::Test
assert_equal ' text1 ', output
end
def test_catchall_unknown_method
def test_unknown_method
output = Liquid::Template.parse(' {{ product.catchall.unknown }} ').render!('product' => ProductDrop.new)
assert_equal ' catchall_method: unknown ', output
assert_equal ' method: unknown ', output
end
def test_catchall_integer_argument_drop
def test_integer_argument_drop
output = Liquid::Template.parse(' {{ product.catchall[8] }} ').render!('product' => ProductDrop.new)
assert_equal ' catchall_method: 8 ', output
assert_equal ' method: 8 ', output
end
def test_text_array_drop
@@ -231,7 +229,7 @@ class DropsTest < Minitest::Test
assert_equal '3', Liquid::Template.parse('{{collection.size}}').render!('collection' => EnumerableDrop.new)
end
def test_enumerable_drop_will_invoke_liquid_method_missing_for_clashing_method_names
def test_enumerable_drop_will_invoke_before_method_for_clashing_method_names
["select", "each", "map", "cycle"].each do |method|
assert_equal method.to_s, Liquid::Template.parse("{{collection.#{method}}}").render!('collection' => EnumerableDrop.new)
assert_equal method.to_s, Liquid::Template.parse("{{collection[\"#{method}\"]}}").render!('collection' => EnumerableDrop.new)

View File

@@ -39,13 +39,13 @@ class FiltersTest < Minitest::Test
@context['var'] = 1000
@context.add_filters(MoneyFilter)
assert_equal ' 1000$ ', Template.parse("{{var | money}}").render(@context)
assert_equal ' 1000$ ', Variable.new("var | money").render(@context)
end
def test_underscore_in_filter_name
@context['var'] = 1000
@context.add_filters(MoneyFilter)
assert_equal ' 1000$ ', Template.parse("{{var | money_with_underscore}}").render(@context)
assert_equal ' 1000$ ', Variable.new("var | money_with_underscore").render(@context)
end
def test_second_filter_overwrites_first
@@ -53,20 +53,20 @@ class FiltersTest < Minitest::Test
@context.add_filters(MoneyFilter)
@context.add_filters(CanadianMoneyFilter)
assert_equal ' 1000$ CAD ', Template.parse("{{var | money}}").render(@context)
assert_equal ' 1000$ CAD ', Variable.new("var | money").render(@context)
end
def test_size
@context['var'] = 'abcd'
@context.add_filters(MoneyFilter)
assert_equal '4', Template.parse("{{var | size}}").render(@context)
assert_equal 4, Variable.new("var | size").render(@context)
end
def test_join
@context['var'] = [1, 2, 3, 4]
assert_equal "1 2 3 4", Template.parse("{{var | join}}").render(@context)
assert_equal "1 2 3 4", Variable.new("var | join").render(@context)
end
def test_sort
@@ -76,11 +76,11 @@ class FiltersTest < Minitest::Test
@context['arrays'] = ['flower', 'are']
@context['case_sensitive'] = ['sensitive', 'Expected', 'case']
assert_equal '1 2 3 4', Template.parse("{{numbers | sort | join}}").render(@context)
assert_equal 'alphabetic as expected', Template.parse("{{words | sort | join}}").render(@context)
assert_equal '3', Template.parse("{{value | sort}}").render(@context)
assert_equal 'are flower', Template.parse("{{arrays | sort | join}}").render(@context)
assert_equal 'Expected case sensitive', Template.parse("{{case_sensitive | sort | join}}").render(@context)
assert_equal [1, 2, 3, 4], Variable.new("numbers | sort").render(@context)
assert_equal ['alphabetic', 'as', 'expected'], Variable.new("words | sort").render(@context)
assert_equal [3], Variable.new("value | sort").render(@context)
assert_equal ['are', 'flower'], Variable.new("arrays | sort").render(@context)
assert_equal ['Expected', 'case', 'sensitive'], Variable.new("case_sensitive | sort").render(@context)
end
def test_sort_natural
@@ -89,13 +89,19 @@ class FiltersTest < Minitest::Test
@context['objects'] = [TestObject.new('A'), TestObject.new('b'), TestObject.new('C')]
# Test strings
assert_equal 'Assert case Insensitive', Template.parse("{{words | sort_natural | join}}").render(@context)
assert_equal ['Assert', 'case', 'Insensitive'], Variable.new("words | sort_natural").render(@context)
# Test hashes
assert_equal 'A b C', Template.parse("{{hashes | sort_natural: 'a' | map: 'a' | join}}").render(@context)
sorted = Variable.new("hashes | sort_natural: 'a'").render(@context)
assert_equal sorted[0]['a'], 'A'
assert_equal sorted[1]['a'], 'b'
assert_equal sorted[2]['a'], 'C'
# Test objects
assert_equal 'A b C', Template.parse("{{objects | sort_natural: 'a' | map: 'a' | join}}").render(@context)
sorted = Variable.new("objects | sort_natural: 'a'").render(@context)
assert_equal sorted[0].a, 'A'
assert_equal sorted[1].a, 'b'
assert_equal sorted[2].a, 'C'
end
def test_compact
@@ -104,44 +110,49 @@ class FiltersTest < Minitest::Test
@context['objects'] = [TestObject.new('A'), TestObject.new(nil), TestObject.new('C')]
# Test strings
assert_equal 'a b c', Template.parse("{{words | compact | join}}").render(@context)
assert_equal ['a', 'b', 'c'], Variable.new("words | compact").render(@context)
# Test hashes
assert_equal 'A C', Template.parse("{{hashes | compact: 'a' | map: 'a' | join}}").render(@context)
sorted = Variable.new("hashes | compact: 'a'").render(@context)
assert_equal sorted[0]['a'], 'A'
assert_equal sorted[1]['a'], 'C'
assert_nil sorted[2]
# Test objects
assert_equal 'A C', Template.parse("{{objects | compact: 'a' | map: 'a' | join}}").render(@context)
sorted = Variable.new("objects | compact: 'a'").render(@context)
assert_equal sorted[0].a, 'A'
assert_equal sorted[1].a, 'C'
assert_nil sorted[2]
end
def test_strip_html
@context['var'] = "<b>bla blub</a>"
assert_equal "bla blub", Template.parse("{{ var | strip_html }}").render(@context)
assert_equal "bla blub", Variable.new("var | strip_html").render(@context)
end
def test_strip_html_ignore_comments_with_html
@context['var'] = "<!-- split and some <ul> tag --><b>bla blub</a>"
assert_equal "bla blub", Template.parse("{{ var | strip_html }}").render(@context)
assert_equal "bla blub", Variable.new("var | strip_html").render(@context)
end
def test_capitalize
@context['var'] = "blub"
assert_equal "Blub", Template.parse("{{ var | capitalize }}").render(@context)
assert_equal "Blub", Variable.new("var | capitalize").render(@context)
end
def test_nonexistent_filter_is_ignored
@context['var'] = 1000
assert_equal '1000', Template.parse("{{ var | xyzzy }}").render(@context)
assert_equal 1000, Variable.new("var | xyzzy").render(@context)
end
def test_filter_with_keyword_arguments
@context['surname'] = 'john'
@context['input'] = 'hello %{first_name}, %{last_name}'
@context.add_filters(SubstituteFilter)
output = Template.parse(%({{ input | substitute: first_name: surname, last_name: 'doe' }})).render(@context)
output = Variable.new(%( 'hello %{first_name}, %{last_name}' | substitute: first_name: surname, last_name: 'doe' )).render(@context)
assert_equal 'hello john, doe', output
end
@@ -170,7 +181,7 @@ class FiltersInTemplate < Minitest::Test
end
end # FiltersTest
class TestObject < Liquid::Drop
class TestObject
attr_accessor :a
def initialize(a)
@a = a

View File

@@ -43,14 +43,6 @@ class OutputTest < Minitest::Test
assert_equal expected, Template.parse(text).render!(@assigns)
end
def test_variable_traversing_with_two_brackets
text = %({{ site.data.menu[include.menu][include.locale] }})
assert_equal "it works!", Template.parse(text).render!(
"site" => { "data" => { "menu" => { "foo" => { "bar" => "it works!" } } } },
"include" => { "menu" => "foo", "locale" => "bar" }
)
end
def test_variable_traversing
text = %( {{car.bmw}} {{car.gm}} {{car.bmw}} )

View File

@@ -118,7 +118,6 @@ class StandardFiltersTest < Minitest::Test
def test_escape
assert_equal '&lt;strong&gt;', @filters.escape('<strong>')
assert_equal nil, @filters.escape(nil)
assert_equal '&lt;strong&gt;', @filters.h('<strong>')
end
@@ -131,13 +130,6 @@ class StandardFiltersTest < Minitest::Test
assert_equal nil, @filters.url_encode(nil)
end
def test_url_decode
assert_equal 'foo bar', @filters.url_decode('foo+bar')
assert_equal 'foo bar', @filters.url_decode('foo%20bar')
assert_equal 'foo+1@example.com', @filters.url_decode('foo%2B1%40example.com')
assert_equal nil, @filters.url_decode(nil)
end
def test_truncatewords
assert_equal 'one two three', @filters.truncatewords('one two three', 4)
assert_equal 'one two...', @filters.truncatewords('one two three', 2)
@@ -166,14 +158,6 @@ 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")
end
def test_sort_empty_array
assert_equal [], @filters.sort([], "a")
end
def test_sort_natural_empty_array
assert_equal [], @filters.sort_natural([], "a")
end
def test_legacy_sort_hash
assert_equal [{ a: 1, b: 2 }], @filters.sort({ a: 1, b: 2 })
end
@@ -193,14 +177,6 @@ class StandardFiltersTest < Minitest::Test
assert_equal [testdrop], @filters.uniq([testdrop, TestDrop.new], 'test')
end
def test_uniq_empty_array
assert_equal [], @filters.uniq([], "a")
end
def test_compact_empty_array
assert_equal [], @filters.compact([], "a")
end
def test_reverse
assert_equal [4, 3, 2, 1], @filters.reverse([1, 2, 3, 4])
end
@@ -249,19 +225,6 @@ class StandardFiltersTest < Minitest::Test
assert_template_result "testfoo", templ, "procs" => [p]
end
def test_map_over_drops_returning_procs
drops = [
{
"proc" => ->{ "foo" },
},
{
"proc" => ->{ "bar" },
},
]
templ = '{{ drops | map: "proc" }}'
assert_template_result "foobar", templ, "drops" => drops
end
def test_map_works_on_enumerables
assert_template_result "123", '{{ foo | map: "foo" }}', "foo" => TestEnumerable.new
end
@@ -275,10 +238,6 @@ class StandardFiltersTest < Minitest::Test
assert_template_result 'foobar', '{{ foo | last }}', 'foo' => [ThingWithToLiquid.new]
end
def test_truncate_calls_to_liquid
assert_template_result "wo...", '{{ foo | truncate: 5 }}', "foo" => TestThing.new
end
def test_date
assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B")
assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B")
@@ -304,10 +263,8 @@ class StandardFiltersTest < Minitest::Test
assert_equal '', @filters.date('', "%B")
with_timezone("UTC") do
assert_equal "07/05/2006", @filters.date(1152098955, "%m/%d/%Y")
assert_equal "07/05/2006", @filters.date("1152098955", "%m/%d/%Y")
end
assert_equal "07/05/2006", @filters.date(1152098955, "%m/%d/%Y")
assert_equal "07/05/2006", @filters.date("1152098955", "%m/%d/%Y")
end
def test_first_last
@@ -461,18 +418,8 @@ class StandardFiltersTest < Minitest::Test
assert_template_result('a', "{{ 'a' | to_number }}")
end
def test_date_raises_nothing
assert_template_result('', "{{ '' | date: '%D' }}")
assert_template_result('abc', "{{ 'abc' | date: '%D' }}")
end
private
def with_timezone(tz)
old_tz = ENV['TZ']
ENV['TZ'] = tz
yield
ensure
ENV['TZ'] = old_tz
def test_strip_html_whitespace
html = "<a>\n\n <b> \n <c>a</c> \r\n </b> </a>"
assert_template_result('<a><b><c>a</c></b></a>', "{{ a | strip_html_whitespace }}", { 'a' => html })
end
end # StandardFiltersTest

View File

@@ -38,12 +38,6 @@ HERE
def test_for_with_range
assert_template_result(' 1 2 3 ', '{%for item in (1..3) %} {{item}} {%endfor%}')
assert_raises(Liquid::ArgumentError) do
Template.parse('{% for i in (a..2) %}{% endfor %}').render!("a" => [1, 2])
end
assert_template_result(' 0 1 2 3 ', '{% for item in (a..3) %} {{item}} {% endfor %}', "a" => "invalid integer")
end
def test_for_with_variable_range

View File

@@ -29,10 +29,10 @@ class IfElseTagTest < Minitest::Test
assert_template_result(' YES ', '{% if a or b %} YES {% endif %}', 'a' => true, 'b' => true)
assert_template_result(' YES ', '{% if a or b %} YES {% endif %}', 'a' => true, 'b' => false)
assert_template_result(' YES ', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => true)
assert_template_result('', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => false)
assert_template_result('', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => false)
assert_template_result(' YES ', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => true)
assert_template_result('', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => false)
assert_template_result('', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => false)
end
def test_if_or_with_operators

View File

@@ -293,4 +293,9 @@ class StandardTagTest < Minitest::Test
def test_multiline_tag
assert_template_result '0 1 2 3', "0{%\nfor i in (1..3)\n%} {{\ni\n}}{%\nendfor\n%}"
end
def test_spaceless
html = "<a>\n\n <b> \n <c>a</c> \r\n </b> </a>"
assert_template_result '<a><b><c>a</c></b></a>', "{% spaceless %}#{html}{% endspaceless %}"
end
end # StandardTagTest

View File

@@ -2,7 +2,7 @@ require 'test_helper'
require 'timeout'
class TemplateContextDrop < Liquid::Drop
def liquid_method_missing(method)
def before_method(method)
method
end
@@ -211,18 +211,4 @@ class TemplateTest < Minitest::Test
end
assert exception.is_a?(Liquid::ZeroDivisionError)
end
def test_global_filter_option_on_render
global_filter_proc = ->(output) { "#{output} filtered" }
rendered_template = Template.parse("{{name}}").render({ "name" => "bob" }, global_filter: global_filter_proc)
assert_equal 'bob filtered', rendered_template
end
def test_global_filter_option_when_native_filters_exist
global_filter_proc = ->(output) { "#{output} filtered" }
rendered_template = Template.parse("{{name | upcase}}").render({ "name" => "bob" }, global_filter: global_filter_proc)
assert_equal 'BOB filtered', rendered_template
end
end

View File

@@ -267,7 +267,7 @@ class ContextUnitTest < Minitest::Test
def test_access_hashes_with_hash_notation
@context['products'] = { 'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
@context['product'] = { 'variants' => [ { 'title' => 'draft151cm' }, { 'title' => 'element151cm' } ] }
@context['product'] = { 'variants' => [ { 'title' => 'draft151cm' }, { 'title' => 'element151cm' } ] }
assert_equal 5, @context['products["count"]']
assert_equal 'deepsnow', @context['products["tags"][0]']
@@ -305,7 +305,7 @@ class ContextUnitTest < Minitest::Test
end
def test_first_can_appear_in_middle_of_callchain
@context['product'] = { 'variants' => [ { 'title' => 'draft151cm' }, { 'title' => 'element151cm' } ] }
@context['product'] = { 'variants' => [ { 'title' => 'draft151cm' }, { 'title' => 'element151cm' } ] }
assert_equal 'draft151cm', @context['product.variants[0].title']
assert_equal 'element151cm', @context['product.variants[1].title']
@@ -466,18 +466,4 @@ class ContextUnitTest < Minitest::Test
assert contx
assert_nil contx['poutine']
end
def test_apply_global_filter
global_filter_proc = ->(output) { "#{output} filtered" }
context = Context.new
context.global_filter = global_filter_proc
assert_equal 'hi filtered', context.apply_global_filter('hi')
end
def test_apply_global_filter_when_no_global_filter_exist
context = Context.new
assert_equal 'hi', context.apply_global_filter('hi')
end
end # ContextTest

View File

@@ -29,18 +29,6 @@ class StrainerUnitTest < Minitest::Test
end
end
def test_stainer_argument_error_contains_backtrace
strainer = Strainer.create(nil)
begin
strainer.invoke("public_filter", 1)
rescue Liquid::ArgumentError => e
assert_match(
/\ALiquid error: wrong number of arguments \((1 for 0|given 1, expected 0)\)\z/,
e.message)
assert_equal e.backtrace[0].split(':')[0], __FILE__
end
end
def test_strainer_only_invokes_public_filter_methods
strainer = Strainer.create(nil)
assert_equal false, strainer.class.invokable?('__test__')

View File

@@ -4,18 +4,13 @@ class TagUnitTest < Minitest::Test
include Liquid
def test_tag
tag = Tag.parse('tag', "", Tokenizer.new(""), ParseContext.new)
tag = Tag.parse('tag', [], [], {})
assert_equal 'liquid::tag', tag.name
assert_equal '', tag.render(Context.new)
end
def test_return_raw_text_of_tag
tag = Tag.parse("long_tag", "param1, param2, param3", Tokenizer.new(""), ParseContext.new)
tag = Tag.parse("long_tag", "param1, param2, param3", [], {})
assert_equal("long_tag param1, param2, param3", tag.raw)
end
def test_tag_name_should_return_name_of_the_tag
tag = Tag.parse("some_tag", "", Tokenizer.new(""), ParseContext.new)
assert_equal 'some_tag', tag.tag_name
end
end

View File

@@ -22,34 +22,20 @@ class TokenizerTest < Minitest::Test
end
def test_calculate_line_numbers_per_token_with_profiling
assert_equal [1], tokenize_line_numbers("{{funk}}")
assert_equal [1, 1, 1], tokenize_line_numbers(" {{funk}} ")
assert_equal [1, 2, 2], tokenize_line_numbers("\n{{funk}}\n")
assert_equal [1, 1, 3], tokenize_line_numbers(" {{\n funk \n}} ")
assert_equal [1], tokenize("{{funk}}", true).map(&:line_number)
assert_equal [1, 1, 1], tokenize(" {{funk}} ", true).map(&:line_number)
assert_equal [1, 2, 2], tokenize("\n{{funk}}\n", true).map(&:line_number)
assert_equal [1, 1, 3], tokenize(" {{\n funk \n}} ", true).map(&:line_number)
end
private
def tokenize(source)
tokenizer = Liquid::Tokenizer.new(source)
def tokenize(source, line_numbers = false)
tokenizer = Liquid::Tokenizer.new(source, line_numbers)
tokens = []
while t = tokenizer.shift
tokens << t
end
tokens
end
def tokenize_line_numbers(source)
tokenizer = Liquid::Tokenizer.new(source, true)
line_numbers = []
loop do
line_number = tokenizer.line_number
if tokenizer.shift
line_numbers << line_number
else
break
end
end
line_numbers
end
end

View File

@@ -4,133 +4,133 @@ class VariableUnitTest < Minitest::Test
include Liquid
def test_variable
var = create_variable('hello')
var = Variable.new('hello')
assert_equal VariableLookup.new('hello'), var.name
end
def test_filters
var = create_variable('hello | textileze')
var = Variable.new('hello | textileze')
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['textileze', []]], var.filters
var = create_variable('hello | textileze | paragraph')
var = Variable.new('hello | textileze | paragraph')
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['textileze', []], ['paragraph', []]], var.filters
var = create_variable(%( hello | strftime: '%Y'))
var = Variable.new(%( hello | strftime: '%Y'))
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['strftime', ['%Y']]], var.filters
var = create_variable(%( 'typo' | link_to: 'Typo', true ))
var = Variable.new(%( 'typo' | link_to: 'Typo', true ))
assert_equal 'typo', var.name
assert_equal [['link_to', ['Typo', true]]], var.filters
var = create_variable(%( 'typo' | link_to: 'Typo', false ))
var = Variable.new(%( 'typo' | link_to: 'Typo', false ))
assert_equal 'typo', var.name
assert_equal [['link_to', ['Typo', false]]], var.filters
var = create_variable(%( 'foo' | repeat: 3 ))
var = Variable.new(%( 'foo' | repeat: 3 ))
assert_equal 'foo', var.name
assert_equal [['repeat', [3]]], var.filters
var = create_variable(%( 'foo' | repeat: 3, 3 ))
var = Variable.new(%( 'foo' | repeat: 3, 3 ))
assert_equal 'foo', var.name
assert_equal [['repeat', [3, 3]]], var.filters
var = create_variable(%( 'foo' | repeat: 3, 3, 3 ))
var = Variable.new(%( 'foo' | repeat: 3, 3, 3 ))
assert_equal 'foo', var.name
assert_equal [['repeat', [3, 3, 3]]], var.filters
var = create_variable(%( hello | strftime: '%Y, okay?'))
var = Variable.new(%( hello | strftime: '%Y, okay?'))
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['strftime', ['%Y, okay?']]], var.filters
var = create_variable(%( hello | things: "%Y, okay?", 'the other one'))
var = Variable.new(%( hello | things: "%Y, okay?", 'the other one'))
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['things', ['%Y, okay?', 'the other one']]], var.filters
end
def test_filter_with_date_parameter
var = create_variable(%( '2006-06-06' | date: "%m/%d/%Y"))
var = Variable.new(%( '2006-06-06' | date: "%m/%d/%Y"))
assert_equal '2006-06-06', var.name
assert_equal [['date', ['%m/%d/%Y']]], var.filters
end
def test_filters_without_whitespace
var = create_variable('hello | textileze | paragraph')
var = Variable.new('hello | textileze | paragraph')
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['textileze', []], ['paragraph', []]], var.filters
var = create_variable('hello|textileze|paragraph')
var = Variable.new('hello|textileze|paragraph')
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['textileze', []], ['paragraph', []]], var.filters
var = create_variable("hello|replace:'foo','bar'|textileze")
var = Variable.new("hello|replace:'foo','bar'|textileze")
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['replace', ['foo', 'bar']], ['textileze', []]], var.filters
end
def test_symbol
var = create_variable("http://disney.com/logo.gif | image: 'med' ", error_mode: :lax)
var = Variable.new("http://disney.com/logo.gif | image: 'med' ", error_mode: :lax)
assert_equal VariableLookup.new('http://disney.com/logo.gif'), var.name
assert_equal [['image', ['med']]], var.filters
end
def test_string_to_filter
var = create_variable("'http://disney.com/logo.gif' | image: 'med' ")
var = Variable.new("'http://disney.com/logo.gif' | image: 'med' ")
assert_equal 'http://disney.com/logo.gif', var.name
assert_equal [['image', ['med']]], var.filters
end
def test_string_single_quoted
var = create_variable(%( "hello" ))
var = Variable.new(%( "hello" ))
assert_equal 'hello', var.name
end
def test_string_double_quoted
var = create_variable(%( 'hello' ))
var = Variable.new(%( 'hello' ))
assert_equal 'hello', var.name
end
def test_integer
var = create_variable(%( 1000 ))
var = Variable.new(%( 1000 ))
assert_equal 1000, var.name
end
def test_float
var = create_variable(%( 1000.01 ))
var = Variable.new(%( 1000.01 ))
assert_equal 1000.01, var.name
end
def test_dashes
assert_equal VariableLookup.new('foo-bar'), create_variable('foo-bar').name
assert_equal VariableLookup.new('foo-bar-2'), create_variable('foo-bar-2').name
assert_equal VariableLookup.new('foo-bar'), Variable.new('foo-bar').name
assert_equal VariableLookup.new('foo-bar-2'), Variable.new('foo-bar-2').name
with_error_mode :strict do
assert_raises(Liquid::SyntaxError) { create_variable('foo - bar') }
assert_raises(Liquid::SyntaxError) { create_variable('-foo') }
assert_raises(Liquid::SyntaxError) { create_variable('2foo') }
assert_raises(Liquid::SyntaxError) { Variable.new('foo - bar') }
assert_raises(Liquid::SyntaxError) { Variable.new('-foo') }
assert_raises(Liquid::SyntaxError) { Variable.new('2foo') }
end
end
def test_string_with_special_chars
var = create_variable(%( 'hello! $!@.;"ddasd" ' ))
var = Variable.new(%( 'hello! $!@.;"ddasd" ' ))
assert_equal 'hello! $!@.;"ddasd" ', var.name
end
def test_string_dot
var = create_variable(%( test.test ))
var = Variable.new(%( test.test ))
assert_equal VariableLookup.new('test.test'), var.name
end
def test_filter_with_keyword_arguments
var = create_variable(%( hello | things: greeting: "world", farewell: 'goodbye'))
var = Variable.new(%( hello | things: greeting: "world", farewell: 'goodbye'))
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['things', [], { 'greeting' => 'world', 'farewell' => 'goodbye' }]], var.filters
end
def test_lax_filter_argument_parsing
var = create_variable(%( number_of_comments | pluralize: 'comment': 'comments' ), error_mode: :lax)
var = Variable.new(%( number_of_comments | pluralize: 'comment': 'comments' ), error_mode: :lax)
assert_equal VariableLookup.new('number_of_comments'), var.name
assert_equal [['pluralize', ['comment', 'comments']]], var.filters
end
@@ -138,13 +138,13 @@ class VariableUnitTest < Minitest::Test
def test_strict_filter_argument_parsing
with_error_mode(:strict) do
assert_raises(SyntaxError) do
create_variable(%( number_of_comments | pluralize: 'comment': 'comments' ))
Variable.new(%( number_of_comments | pluralize: 'comment': 'comments' ))
end
end
end
def test_output_raw_source_of_variable
var = create_variable(%( name_of_variable | upcase ))
var = Variable.new(%( name_of_variable | upcase ))
assert_equal " name_of_variable | upcase ", var.raw
end
@@ -153,10 +153,4 @@ class VariableUnitTest < Minitest::Test
assert_equal 'a', lookup.name
assert_equal ['b', 'c'], lookup.lookups
end
private
def create_variable(markup, options = {})
Variable.new(markup, ParseContext.new(options))
end
end