Compare commits

..

1 Commits

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

View File

@@ -13,9 +13,6 @@ Metrics/BlockNesting:
Metrics/ModuleLength:
Enabled: false
Metrics/ClassLength:
Enabled: false
Lint/AssignmentInCondition:
Enabled: false
@@ -121,3 +118,6 @@ Style/PerlBackrefs:
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,7 +6,7 @@ rvm:
- 2.2
- ruby-head
- jruby-head
# - rbx-2
- rbx-2
sudo: false

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.32.0'
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

@@ -15,7 +15,6 @@
* Remove support for `liquid_methods`
### Fixed
* 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]
@@ -27,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]
@@ -83,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

@@ -69,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
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
@@ -34,10 +33,6 @@ module Liquid
@filters = []
end
def warnings
@warnings ||= []
end
def strainer
@strainer ||= Strainer.create(self, @filters)
end
@@ -67,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
@@ -80,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

@@ -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

@@ -13,7 +13,7 @@ module Liquid
'?'.freeze => :question,
'-'.freeze => :dash
}
IDENTIFIER = /[a-zA-Z_](?:[\w-]*\w)?\??/
IDENTIFIER = /[a-zA-Z_][\w-]*\??/
SINGLE_STRING_LITERAL = /'[^\']*'/
DOUBLE_STRING_LITERAL = /"[^\"]*"/
NUMBER_LITERAL = /-?\d+(\.\d+)?/

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
CGI.escapeHTML(input).untaint rescue input
end
alias_method :h, :escape
@@ -46,8 +46,8 @@ module Liquid
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) || []
@@ -59,7 +59,7 @@ module Liquid
# Truncate a string down to x characters
def truncate(input, length = 50, truncate_string = "...".freeze)
return if input.nil?
length = Utils.to_integer(length)
length = to_integer(length)
l = length - truncate_string.length
l = 0 if l < 0
input.length > length ? input[0...l] + truncate_string : input
@@ -68,7 +68,7 @@ module Liquid
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
@@ -105,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)
@@ -255,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
@@ -307,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
@@ -316,13 +321,13 @@ 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
@@ -334,8 +339,51 @@ module Liquid
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

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

@@ -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

@@ -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
@@ -202,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
@@ -224,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
@@ -76,7 +74,7 @@ module Liquid
@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.tap{ |obj| taint_check(context, obj) }
end.tap{ |obj| taint_check(obj) }
end
private
@@ -108,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

@@ -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

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

@@ -263,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
@@ -420,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

@@ -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

@@ -36,9 +36,6 @@ class LexerUnitTest < Minitest::Test
tokens = Lexer.new('2foo').tokenize
assert_equal [[:number, '2'], [:id, 'foo'], [:end_of_string]], tokens
tokens = Lexer.new('foo-bar--baz-').tokenize
assert_equal [[:id, 'foo-bar--baz'], [:dash, "-"], [:end_of_string]], tokens
end
def test_whitespace

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