Compare commits

...

74 Commits

Author SHA1 Message Date
Florian Weingarten
d6db28c854 Revert "Merge pull request #463 from Shopify/stricter-identifiers"
This reverts commit a056f6521c, reversing
changes made to 7843bcca8d.
2014-11-07 01:49:01 +00:00
Florian Weingarten
475ea51f1f Revert "Merge pull request #466 from Shopify/remove-expression-cache"
This reverts commit d9ae36ec40, reversing
changes made to 2da9d49478.
2014-11-07 01:48:51 +00:00
Florian Weingarten
9c33e9601b Revert "Merge pull request #476 from Shopify/missing-variable-name-error"
This reverts commit 4dc682313f, reversing
changes made to a8f60ff6b1.
2014-11-07 01:48:16 +00:00
Florian Weingarten
b242a7273a Revert "Merge pull request #478 from Shopify/numbers-in-identifiers"
This reverts commit 263e90e772, reversing
changes made to 4dc682313f.
2014-11-07 01:48:05 +00:00
Florian Weingarten
4b1835e3c0 Revert "Merge pull request #458 from Shopify/block-body"
This reverts commit 12d526a05c, reversing
changes made to 263e90e772.

Conflicts:
	lib/liquid/block_body.rb
2014-11-07 01:47:47 +00:00
Florian Weingarten
2fe3a21a5d Revert "Merge pull request #479 from Shopify/tweaks-for-c"
This reverts commit aa182f64b4, reversing
changes made to 70c45f8cd8.
2014-11-07 01:46:59 +00:00
Justin Li
76ef675eb2 Merge pull request #481 from Shopify/fix-nil-blank
Coerce regex @blank output to a boolean
2014-11-06 13:03:15 -05:00
Justin Li
e5fd4d929f Coerce regex @blank output to a boolean 2014-11-05 20:44:06 -05:00
Justin Li
2e42c7be1f Merge pull request #480 from Shopify/number_variables
Add quirks test for variables with number prefixes
2014-11-05 12:05:21 -05:00
Justin Li
95b031ee04 Add quirks test for extra dots in ranges 2014-11-05 11:41:12 -05:00
Justin Li
4d97a714a9 Add quirks test for variables with number prefixes 2014-11-05 10:56:58 -05:00
Justin Li
aa182f64b4 Merge pull request #479 from Shopify/tweaks-for-c
Tweaks for C
2014-11-04 14:02:14 -05:00
Justin Li
4e870302b1 Add env var for saving stackprof graphviz output 2014-11-04 18:38:14 +00:00
Justin Li
098c89b5f5 Convenience methods for raising terminator syntax errors 2014-11-04 18:38:08 +00:00
Justin Li
70c45f8cd8 Use SVG badge URLs
[ci skip]
2014-11-03 17:41:42 -05:00
Justin Li
12d526a05c Merge pull request #458 from Shopify/block-body
Create a BlockBody class to decouple block body parsing from tags.
2014-11-03 17:34:39 -05:00
Dylan Thacker-Smith
2fd8ad08c0 Remove unused local variable that was accidentally added. 2014-11-03 17:07:42 -05:00
Dylan Thacker-Smith
15e1d46125 Avoid storing options instance variable in BlockBody.
There is no need to pass parse options to the BlockBody initializer, since
it does all the parsing in the parse method, unlike tags which parse the
tag markup in the initializer.
2014-11-03 17:07:42 -05:00
Dylan Thacker-Smith
73fcd42403 Create a BlockBody class to decouple block body parsing from tags. 2014-11-03 17:07:42 -05:00
Justin Li
263e90e772 Merge pull request #478 from Shopify/numbers-in-identifiers
Use a single token for identifiers
2014-10-30 21:59:26 -04:00
Justin Li
81770f094d Remove unnecessary + 2014-10-29 13:39:43 -04:00
Justin Li
dd5ee81089 Disallow number and dash identifier prefixes 2014-10-29 12:08:00 -04:00
Justin Li
a07e382617 Use a single token for identifiers 2014-10-29 11:28:41 -04:00
Justin Li
4dc682313f Merge pull request #476 from Shopify/missing-variable-name-error
Disallow filters with no variable in strict mode
2014-10-27 13:56:11 -04:00
Justin Li
5616ddf00e Remove obsolete comment 2014-10-27 13:44:14 -04:00
Justin Li
fcb23a4cd2 Disallow filters with no variable in strict mode 2014-10-27 13:34:27 -04:00
Justin Li
a8f60ff6b1 Merge pull request #472 from Shopify/fix-leaky-test
Fix test leaking error_mode, fix equality check for VariableLookup
2014-10-23 10:12:41 -04:00
Justin Li
a206c8301d Fix test leaking error_mode, fix equality check for VariableLookup 2014-10-22 15:40:41 -04:00
Justin Li
ee0de01480 Merge pull request #469 from Shopify/falsy-variable-fix
Fix case where a variable name is falsy
2014-10-21 15:06:34 -04:00
Justin Li
887b05e6ed Clarify test name 2014-10-21 14:06:30 -04:00
Justin Li
5d68e8803f Ensure nil works as a variable name 2014-10-21 14:03:10 -04:00
Justin Li
dedd1d3dc0 Fix case where a variable name is falsy 2014-10-21 12:09:26 -04:00
Dylan Thacker-Smith
d9ae36ec40 Merge pull request #466 from Shopify/remove-expression-cache
Remove expression cache
2014-10-20 13:57:17 -04:00
Dylan Thacker-Smith
b9ac3fef8f Remove the quotes from the partial string in the profiler timing objects. 2014-10-18 16:26:18 -04:00
Dylan Thacker-Smith
f5faa4858c Remove parsed expression cache. 2014-10-18 15:03:40 -04:00
Dylan Thacker-Smith
bc5e444d04 Use Expression.parse and Context#evaluate in the Include class. 2014-10-18 15:03:40 -04:00
Dylan Thacker-Smith
3a4b63f37e Use Expression.parse and Context#evaluate in the TableRow class. 2014-10-18 15:03:40 -04:00
Dylan Thacker-Smith
a1a128db19 Refactor Condition so that it takes a parsed expression. 2014-10-18 15:03:40 -04:00
Dylan Thacker-Smith
d502b9282a Use Expression.parse and Context#evaluate in the For class. 2014-10-18 15:03:36 -04:00
Dylan Thacker-Smith
fee8e41466 Use Expression.parse and Context#evaluate in the Cycle class. 2014-10-18 14:27:58 -04:00
Dylan Thacker-Smith
37260f17ff Use Expression.parse and Context#evaluate in the Condition class. 2014-10-18 14:27:58 -04:00
Florian Weingarten
2da9d49478 Merge pull request #465 from Shopify/avoid_multi_assigns
Avoid parallel assignments
2014-10-18 16:19:02 +02:00
Florian Weingarten
7196a2d58e Avoid parallel assignments 2014-10-18 13:58:32 +00:00
Justin Li
a056f6521c Merge pull request #463 from Shopify/stricter-identifiers
Separate ? and - into special tokens
2014-10-17 13:45:48 -04:00
Justin Li
de16db9b72 Don't allow - to end a variable name 2014-10-17 13:38:07 -04:00
Justin Li
b4ea483c4e Separate ? and - into special tokens 2014-10-17 13:30:54 -04:00
Justin Li
7843bcca8d Merge pull request #443 from Shopify/completely-parse-variables
Parse expressions in Liquid::Variable#parse.
2014-10-17 13:12:46 -04:00
Florian Weingarten
76ea5596ff Merge pull request #462 from Shopify/flat_map
nodelist flat_map over map.flatten
2014-10-17 18:32:00 +02:00
Florian Weingarten
f9318e8c93 flat_map 2014-10-17 16:11:12 +00:00
Florian Weingarten
71253ec6f9 Merge pull request #459 from Shopify/pop_vs_shift
Use pop over shift to avoid reverse
2014-10-15 21:42:53 +02:00
Florian Weingarten
0fa075b879 Use pop over shift to avoid reverse 2014-10-15 19:26:39 +00:00
Dylan Thacker-Smith
6d080afd22 Merge pull request #446 from Shopify/remove-end-tag
Remove unused Block#end_tag method.
2014-10-14 03:03:31 -04:00
Dylan Thacker-Smith
a67e2a0a00 Remove unused Block#end_tag method.
Although the method is called, it is defined with an empty body and not
overridden to do anything else.
2014-10-14 02:58:11 -04:00
Dylan Thacker-Smith
f387508666 Parse expressions in Liquid::Variable#parse. 2014-10-08 21:06:59 -04:00
Florian Weingarten
632b1fb702 Merge pull request #455 from Shopify/parse_error_line_numbers
Line numbers for all parse errors
2014-10-04 17:53:30 +02:00
Dylan Thacker-Smith
d84870d7a5 Test line number of errors in nested blocks. 2014-10-03 16:25:12 -05:00
Florian Weingarten
584b492e70 Line numbers for all parse errors 2014-10-03 21:00:31 +00:00
Dylan Thacker-Smith
b79c9cb9bf Merge pull request #453 from Shopify/no-modify-default-resource-limit
Avoid modifying the default resources limits hash.
2014-10-01 19:02:09 -05:00
Dylan Thacker-Smith
cf5ccede50 Avoid modifying the default resources limits hash. 2014-10-01 18:51:06 -05:00
Evan Huus
23622a9739 Merge pull request #440 from Shopify/drop-tainting
Variable tainting
2014-09-22 13:43:35 -04:00
Florian Weingarten
7ba5a6ab75 Merge pull request #450 from Shopify/additional_test_for_includes
Regression test for including assignments
2014-09-18 21:05:37 +02:00
Florian Weingarten
be3d261e11 Regression test for including assignments 2014-09-18 10:37:44 +00:00
Evan Huus
eeb061ef44 Address code review comments
- clean up comment wording
- fix potentially leaky tests
2014-09-16 17:23:26 +00:00
Evan Huus
67b2c320a1 Add tainting tests 2014-09-16 17:23:26 +00:00
Evan Huus
1d151885be Auto-untaint variables passed through 'escape' 2014-09-16 17:23:26 +00:00
Evan Huus
e836024dd9 Check and handle when a tainted variable is used 2014-09-16 17:23:26 +00:00
Dylan Thacker-Smith
638455ed92 Merge pull request #448 from Shopify/remove-unused-filter-not-found-error
Remove Liquid::FilterNotFoundError since it is never raised.
2014-09-15 17:43:33 -04:00
Dylan Thacker-Smith
b2a74883e9 Remove Liquid::FilterNotFoundError since it is never raised. 2014-09-15 17:42:07 -04:00
Dylan Thacker-Smith
6875e5e16f Merge pull request #449 from Shopify/fix-flaky-total-render-time-test
Fix flaky test which assumes total_render_time can't be 0.
2014-09-15 17:41:41 -04:00
Dylan Thacker-Smith
a5717a3f8d Fix flaky test which assumes total_render_time can't be 0.
jruby has millisecond precision for Time.now, so total_render_time can be 0
due to this lack of precision.
2014-09-15 17:26:55 -04:00
Dylan Thacker-Smith
804fcfebd1 Merge pull request #444 from Shopify/remove-block-children
Avoid keeping track of two lists of nodes during parsing.
2014-09-15 09:56:08 -04:00
Dylan Thacker-Smith
b37ee5684a Merge pull request #445 from Shopify/prefer-super-over-render-all
Use super rather than render_all in single block render classes.
2014-09-15 09:53:54 -04:00
Dylan Thacker-Smith
0573b63b4c Use super rather than render_all in single block render classes. 2014-09-12 16:58:07 -04:00
Dylan Thacker-Smith
29c21d7867 Avoid keeping track of two lists of nodes during parsing. 2014-09-12 16:43:00 -04:00
30 changed files with 455 additions and 141 deletions

View File

@@ -3,6 +3,7 @@
## 3.0.0 / not yet released / branch "master"
* ...
* Removed Block#end_tag. Instead, override parse with `super` followed by your code. See #446 [Dylan Thacker-Smith, dylanahsmith]
* Fixed condition with wrong data types, see #423 [Bogdan Gusiev]
* Add url_encode to standard filters, see #421 [Derrick Reimer, djreimer]
* Add uniq to standard filters [Florian Weingarten, fw42]

View File

@@ -1,5 +1,5 @@
[![Build Status](https://secure.travis-ci.org/Shopify/liquid.png?branch=master)](http://travis-ci.org/Shopify/liquid)
[![Inline docs](http://inch-ci.org/github/Shopify/liquid.png)](http://inch-ci.org/github/Shopify/liquid)
[![Build Status](https://api.travis-ci.org/Shopify/liquid.svg?branch=master)](http://travis-ci.org/Shopify/liquid)
[![Inline docs](http://inch-ci.org/github/Shopify/liquid.svg?branch=master)](http://inch-ci.org/github/Shopify/liquid)
# Liquid template engine

View File

@@ -11,7 +11,8 @@ class LiquidServlet < WEBrick::HTTPServlet::AbstractServlet
private
def handle(type, req, res)
@request, @response = req, res
@request = req
@response = res
@request.path_info =~ /(\w+)\z/
@action = $1 || 'index'

View File

@@ -14,48 +14,45 @@ module Liquid
@nodelist ||= []
@nodelist.clear
# All child tags of the current block.
@children = []
while token = tokens.shift
unless token.empty?
case
when token.start_with?(TAGSTART)
if token =~ FullToken
begin
unless token.empty?
case
when token.start_with?(TAGSTART)
if token =~ FullToken
# if we found the proper block delimiter just end parsing here and let the outer block
# proceed
if block_delimiter == $1
end_tag
return
end
# if we found the proper block delimiter just end parsing here and let the outer block
# proceed
return if block_delimiter == $1
# fetch the tag from registered blocks
if tag = Template.tags[$1]
markup = token.is_a?(Token) ? token.child($2) : $2
new_tag = tag.parse($1, markup, tokens, @options)
new_tag.line_number = token.line_number if token.is_a?(Token)
@blank &&= new_tag.blank?
@nodelist << new_tag
@children << new_tag
# fetch the tag from registered blocks
if tag = Template.tags[$1]
markup = token.is_a?(Token) ? token.child($2) : $2
new_tag = tag.parse($1, markup, tokens, @options)
new_tag.line_number = token.line_number if token.is_a?(Token)
@blank &&= new_tag.blank?
@nodelist << new_tag
else
# this tag is not registered with the system
# pass it to the current block for special handling or error reporting
unknown_tag($1, $2, tokens)
end
else
# this tag is not registered with the system
# pass it to the current block for special handling or error reporting
unknown_tag($1, $2, tokens)
raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination".freeze, :token => token, :tag_end => TagEnd.inspect))
end
when token.start_with?(VARSTART)
new_var = create_variable(token)
new_var.line_number = token.line_number if token.is_a?(Token)
@nodelist << new_var
@blank = false
else
raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination".freeze, :token => token, :tag_end => TagEnd.inspect))
@nodelist << token
@blank &&= (token =~ /\A\s*\z/)
end
when token.start_with?(VARSTART)
new_var = create_variable(token)
new_var.line_number = token.line_number if token.is_a?(Token)
@nodelist << new_var
@children << new_var
@blank = false
else
@nodelist << token
@blank &&= (token =~ /\A\s*\z/)
end
rescue SyntaxError => e
e.set_line_number_from_token(token)
raise
end
end
@@ -70,16 +67,13 @@ module Liquid
all_warnings = []
all_warnings.concat(@warnings) if @warnings
(@children || []).each do |node|
all_warnings.concat(node.warnings || [])
(nodelist || []).each do |node|
all_warnings.concat(node.warnings || []) if node.respond_to?(:warnings)
end
all_warnings
end
def end_tag
end
def unknown_tag(tag, params, tokens)
case tag
when 'else'.freeze

123
lib/liquid/block_body.rb Normal file
View File

@@ -0,0 +1,123 @@
module Liquid
class BlockBody
FullToken = /\A#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
ContentOfVariable = /\A#{VariableStart}(.*)#{VariableEnd}\z/om
TAGSTART = "{%".freeze
VARSTART = "{{".freeze
attr_reader :nodelist
def initialize
@nodelist = []
@blank = true
end
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 = Template.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
raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination".freeze, :token => token, :tag_end => TagEnd.inspect))
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
@nodelist << token
@blank &&= !!(token =~ /\A\s*\z/)
end
end
rescue SyntaxError => e
e.set_line_number_from_token(token)
raise
end
end
yield nil, nil
end
def blank?
@blank
end
def warnings
all_warnings = []
nodelist.each do |node|
all_warnings.concat(node.warnings) if node.respond_to?(:warnings) && node.warnings
end
all_warnings
end
def render(context)
output = []
context.resource_limits[:render_length_current] = 0
context.resource_limits[:render_score_current] += @nodelist.length
@nodelist.each do |token|
# Break out if we have any unhanded interrupts.
break if context.has_interrupt?
begin
# If we get an Interrupt that means the block must stop processing. An
# Interrupt is any command that stops block execution such as {% break %}
# or {% continue %}
if token.is_a?(Continue) or token.is_a?(Break)
context.push_interrupt(token.interrupt)
break
end
token_output = render_token(token, context)
unless token.is_a?(Block) && token.blank?
output << token_output
end
rescue MemoryError => e
raise e
rescue ::StandardError => e
output << context.handle_error(e, token)
end
end
output.join
end
private
def render_token(token, context)
token_output = (token.respond_to?(:render) ? token.render(context) : token)
context.increment_used_resources(:render_length_current, token_output)
if context.resource_limits_reached?
context.resource_limits[:reached] = true
raise MemoryError.new("Memory limits exceeded".freeze)
end
token_output
end
def create_variable(token, options)
token.scan(ContentOfVariable) do |content|
markup = token.is_a?(Token) ? token.child(content.first) : content.first
return Variable.new(markup, options)
end
raise SyntaxError.new(options[:locale].t("errors.syntax.variable_termination".freeze, :token => token, :tag_end => VariableEnd.inspect))
end
end
end

View File

@@ -28,7 +28,9 @@ module Liquid
attr_accessor :left, :operator, :right
def initialize(left = nil, operator = nil, right = nil)
@left, @operator, @right = left, operator, right
@left = left
@operator = operator
@right = right
@child_relation = nil
@child_condition = nil
end
@@ -47,11 +49,13 @@ module Liquid
end
def or(condition)
@child_relation, @child_condition = :or, condition
@child_relation = :or
@child_condition = condition
end
def and(condition)
@child_relation, @child_condition = :and, condition
@child_relation = :and
@child_condition = condition
end
def attach(attachment)
@@ -94,7 +98,8 @@ module Liquid
# return this as the result.
return context[left] if op == nil
left, right = context[left], context[right]
left = context[left]
right = context[right]
operation = self.class.operators[op] || raise(Liquid::ArgumentError.new("Unknown operator #{op}"))

View File

@@ -21,7 +21,7 @@ module Liquid
@scopes = [(outer_scope || {})]
@registers = registers
@errors = []
@resource_limits = resource_limits || Template.default_resource_limits
@resource_limits = resource_limits || Template.default_resource_limits.dup
@resource_limits[:render_score_current] = 0
@resource_limits[:assign_score_current] = 0
@parsed_expression = Hash.new{ |cache, markup| cache[markup] = Expression.parse(markup) }

View File

@@ -18,6 +18,7 @@ module Liquid
def set_line_number_from_token(token)
return unless token.respond_to?(:line_number)
return if self.line_number
self.line_number = token.line_number
end
@@ -50,10 +51,10 @@ module Liquid
class ArgumentError < Error; end
class ContextError < Error; end
class FilterNotFound < Error; end
class FileSystemError < Error; end
class StandardError < Error; end
class SyntaxError < Error; end
class StackLevelError < Error; end
class TaintedError < Error; end
class MemoryError < Error; end
end

View File

@@ -34,7 +34,7 @@ module Liquid
end
def escape(input)
CGI.escapeHTML(input) rescue input
CGI.escapeHTML(input).untaint rescue input
end
alias_method :h, :escape

View File

@@ -15,7 +15,7 @@ module Liquid
end
def nodelist
@blocks.map(&:attachment).flatten
@blocks.flat_map(&:attachment)
end
def unknown_tag(tag, markup, tokens)

View File

@@ -21,7 +21,7 @@ module Liquid
end
def nodelist
@blocks.map(&:attachment).flatten
@blocks.flat_map(&:attachment)
end
def unknown_tag(tag, markup, tokens)
@@ -57,15 +57,15 @@ module Liquid
end
def lax_parse(markup)
expressions = markup.scan(ExpressionsAndOperators).reverse
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.shift =~ Syntax
expressions = markup.scan(ExpressionsAndOperators)
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.pop =~ Syntax
condition = Condition.new($1, $2, $3)
while not expressions.empty?
operator = (expressions.shift).to_s.strip
operator = expressions.pop.to_s.strip
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.shift.to_s =~ Syntax
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.pop.to_s =~ Syntax
new_condition = Condition.new($1, $2, $3)
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless BOOLEAN_OPERATORS.include?(operator)

View File

@@ -4,7 +4,7 @@ module Liquid
def render(context)
context.stack do
output = render_all(@nodelist, context)
output = super
if output != context.registers[:ifchanged]
context.registers[:ifchanged] = output

View File

@@ -8,10 +8,7 @@ module Liquid
while token = tokens.shift
if token =~ FullTokenPossiblyInvalid
@nodelist << $1 if $1 != "".freeze
if block_delimiter == $2
end_tag
return
end
return if block_delimiter == $2
end
@nodelist << token if not token.empty?
end

View File

@@ -54,7 +54,7 @@ module Liquid
col += 1
result << "<td class=\"col#{col}\">" << render_all(@nodelist, context) << '</td>'
result << "<td class=\"col#{col}\">" << super << '</td>'
if col == cols and (index != length - 1)
col = 0

View File

@@ -60,6 +60,12 @@ module Liquid
# :strict will enforce correct syntax.
attr_writer :error_mode
# Sets how strict the taint checker should be.
# :lax is the default, and ignores the taint flag completely
# :warn adds a warning, but does not interrupt the rendering
# :error raises an error when tainted output is used
attr_writer :taint_mode
def file_system
@@file_system
end
@@ -80,6 +86,10 @@ module Liquid
@error_mode || :lax
end
def taint_mode
@taint_mode || :lax
end
# Pass a module with filter methods which should be available
# to all liquid views. Good for registering the standard library
def register_filter(mod)

View File

@@ -35,15 +35,17 @@ module Liquid
def lax_parse(markup)
@filters = []
if markup =~ /\s*(#{QuotedFragment})(.*)/om
@name = Regexp.last_match(1)
if Regexp.last_match(2) =~ /#{FilterSeparator}\s*(.*)/om
filters = Regexp.last_match(1).scan(FilterParser)
if markup =~ /(#{QuotedFragment})(.*)/om
name_markup = $1
filter_markup = $2
@name = Expression.parse(name_markup)
if filter_markup =~ /#{FilterSeparator}\s*(.*)/om
filters = $1.scan(FilterParser)
filters.each do |f|
if f =~ /\w+/
filtername = Regexp.last_match(0)
filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o).flatten
@filters << [filtername, filterargs]
@filters << parse_filter_expressions(filtername, filterargs)
end
end
end
@@ -53,7 +55,7 @@ module Liquid
def strict_parse(markup)
# Very simple valid cases
if markup =~ EasyParse
@name = $1
@name = Expression.parse($1)
@filters = []
return
end
@@ -61,11 +63,11 @@ module Liquid
@filters = []
p = Parser.new(markup)
# Could be just filters with no input
@name = p.look(:pipe) ? ''.freeze : p.expression
@name = p.look(:pipe) ? nil : Expression.parse(p.expression)
while p.consume?(:pipe)
filtername = p.consume(:id)
filterargs = p.consume?(:colon) ? parse_filterargs(p) : []
@filters << [filtername, filterargs]
@filters << parse_filter_expressions(filtername, filterargs)
end
p.consume(:end_of_string)
end
@@ -81,22 +83,51 @@ module Liquid
end
def render(context)
return ''.freeze if @name.nil?
@filters.inject(context[@name]) do |output, filter|
filterargs = []
keyword_args = {}
filter[1].to_a.each do |a|
if matches = a.match(/\A#{TagAttributes}\z/o)
keyword_args[matches[1]] = context[matches[2]]
else
filterargs << context[a]
end
@filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)|
filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
output = context.invoke(filter_name, output, *filter_args)
end.tap{ |obj| taint_check(obj) }
end
private
def parse_filter_expressions(filter_name, unparsed_args)
filter_args = []
keyword_args = {}
unparsed_args.each do |a|
if matches = a.match(/\A#{TagAttributes}\z/o)
keyword_args[matches[1]] = Expression.parse(matches[2])
else
filter_args << Expression.parse(a)
end
filterargs << keyword_args unless keyword_args.empty?
begin
output = context.invoke(filter[0], output, *filterargs)
rescue FilterNotFound
raise FilterNotFound, "Error - filter '#{filter[0]}' in '#{@markup.strip}' could not be found."
end
result = [filter_name, filter_args]
result << keyword_args unless keyword_args.empty?
result
end
def evaluate_filter_expressions(context, filter_args, filter_kwargs)
parsed_args = filter_args.map{ |expr| context.evaluate(expr) }
if filter_kwargs
parsed_kwargs = {}
filter_kwargs.each do |key, expr|
parsed_kwargs[key] = context.evaluate(expr)
end
parsed_args << parsed_kwargs
end
parsed_args
end
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

View File

@@ -64,5 +64,15 @@ module Liquid
object
end
def ==(other)
self.class == other.class && self.state == other.state
end
protected
def state
[@name, @lookups, @command_flags]
end
end
end

View File

@@ -4,8 +4,6 @@ class Paginate < Liquid::Block
def initialize(tag_name, markup, options)
super
@nodelist = []
if markup =~ Syntax
@collection_name = $1
@page_size = if $2
@@ -73,7 +71,7 @@ class Paginate < Liquid::Block
end
end
render_all(@nodelist, context)
super
end
end

View File

@@ -23,12 +23,10 @@ class ContextTest < Minitest::Test
end
def test_has_key_will_not_add_an_error_for_missing_keys
Template.error_mode = :strict
context = Context.new
context.has_key?('unknown')
assert_empty context.errors
with_error_mode :strict do
context = Context.new
context.has_key?('unknown')
assert_empty context.errors
end
end
end

View File

@@ -48,6 +48,10 @@ class ProductDrop < Liquid::Drop
ContextDrop.new
end
def user_input
"foo".taint
end
protected
def callmenot
"protected"
@@ -108,6 +112,30 @@ class DropsTest < Minitest::Test
assert_equal ' ', tpl.render!('product' => ProductDrop.new)
end
def test_rendering_raises_on_tainted_attr
with_taint_mode(:error) do
tpl = Liquid::Template.parse('{{ product.user_input }}')
assert_raises TaintedError do
tpl.render!('product' => ProductDrop.new)
end
end
end
def test_rendering_warns_on_tainted_attr
with_taint_mode(:warn) do
tpl = Liquid::Template.parse('{{ product.user_input }}')
tpl.render!('product' => ProductDrop.new)
assert_match /tainted/, tpl.warnings.first
end
end
def test_rendering_doesnt_raise_on_escaped_tainted_attr
with_taint_mode(:error) do
tpl = Liquid::Template.parse('{{ product.user_input | escape }}')
tpl.render!('product' => ProductDrop.new)
end
end
def test_drop_does_only_respond_to_whitelisted_methods
assert_equal "", Liquid::Template.parse("{{ product.inspect }}").render!('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse("{{ product.pretty_inspect }}").render!('product' => ProductDrop.new)

View File

@@ -100,6 +100,73 @@ class ErrorHandlingTest < Minitest::Test
assert_equal Liquid::ArgumentError, template.errors.first.class
end
def test_with_line_numbers_adds_numbers_to_parser_errors
err = assert_raises(SyntaxError) do
template = Liquid::Template.parse(%q{
foobar
{% "cat" | foobar %}
bla
},
:line_numbers => true
)
end
assert_match /Liquid syntax error \(line 4\)/, err.message
end
def test_parsing_warn_with_line_numbers_adds_numbers_to_lexer_errors
template = Liquid::Template.parse(%q{
foobar
{% if 1 =! 2 %}ok{% endif %}
bla
},
:error_mode => :warn,
:line_numbers => true
)
assert_equal ['Liquid syntax error (line 4): Unexpected character = in "1 =! 2"'],
template.warnings.map(&:message)
end
def test_parsing_strict_with_line_numbers_adds_numbers_to_lexer_errors
err = assert_raises(SyntaxError) do
Liquid::Template.parse(%q{
foobar
{% if 1 =! 2 %}ok{% endif %}
bla
},
:error_mode => :strict,
:line_numbers => true
)
end
assert_equal 'Liquid syntax error (line 4): Unexpected character = in "1 =! 2"', err.message
end
def test_syntax_errors_in_nested_blocks_have_correct_line_number
err = assert_raises(SyntaxError) do
Liquid::Template.parse(%q{
foobar
{% if 1 != 2 %}
{% foo %}
{% endif %}
bla
},
:line_numbers => true
)
end
assert_equal "Liquid syntax error (line 5): Unknown tag 'foo'", err.message
end
def test_strict_error_messages
err = assert_raises(SyntaxError) do
Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ', :error_mode => :strict)

View File

@@ -100,4 +100,17 @@ class ParsingQuirksTest < Minitest::Test
end
end
def test_invalid_variables_work
with_error_mode(:lax) do
assert_template_result('bar', "{% assign 123foo = 'bar' %}{{ 123foo }}")
assert_template_result('123', "{% assign 123 = 'bar' %}{{ 123 }}")
end
end
def test_extra_dots_in_ranges
with_error_mode(:lax) do
assert_template_result('12345', "{% for i in (1...5) %}{{ i }}{% endfor %}")
end
end
end # ParsingQuirksTest

View File

@@ -72,7 +72,7 @@ class RenderProfilingTest < Minitest::Test
t = Template.parse("{% include 'a_template' %}", :profile => true)
t.render!
assert t.profiler.total_render_time > 0, "Total render time was not calculated"
assert t.profiler.total_render_time >= 0, "Total render time was not calculated"
end
def test_profiling_uses_include_to_mark_children

View File

@@ -10,6 +10,11 @@ class IfElseTagTest < Minitest::Test
assert_template_result(' you rock ?','{% if false %} you suck {% endif %} {% if true %} you rock {% endif %}?')
end
def test_literal_comparisons
assert_template_result(' NO ','{% assign v = false %}{% if v %} YES {% else %} NO {% endif %}')
assert_template_result(' YES ','{% assign v = nil %}{% if v == nil %} YES {% else %} NO {% endif %}')
end
def test_if_else
assert_template_result(' YES ','{% if false %} NO {% else %} YES {% endif %}')
assert_template_result(' YES ','{% if true %} YES {% else %} NO {% endif %}')

View File

@@ -27,6 +27,9 @@ class TestFileSystem
when "pick_a_source"
"from TestFileSystem"
when 'assignments'
"{% assign foo = 'bar' %}"
else
template_path
end
@@ -108,6 +111,10 @@ class IncludeTagTest < Minitest::Test
'echo1' => 'test123', 'more_echos' => { "echo2" => 'test321'}
end
def test_included_templates_assigns_variables
assert_template_result "bar", "{% include 'assignments' %}{{ foo }}"
end
def test_nested_include_tag
assert_template_result "body body_detail", "{% include 'body' %}"

View File

@@ -135,6 +135,18 @@ class TemplateTest < Minitest::Test
assert t.resource_limits[:render_length_current] > 0
end
def test_default_resource_limits_unaffected_by_render_with_context
context = Context.new
t = Template.parse("{% for a in (1..100) %} {% assign foo = 1 %} {% endfor %}")
t.render!(context)
assert context.resource_limits[:assign_score_current] > 0
assert context.resource_limits[:render_score_current] > 0
assert context.resource_limits[:render_length_current] > 0
refute Template.default_resource_limits.key?(:assign_score_current)
refute Template.default_resource_limits.key?(:render_score_current)
refute Template.default_resource_limits.key?(:render_length_current)
end
def test_can_use_drop_as_context
t = Template.new
t.registers['lulz'] = 'haha'

View File

@@ -31,6 +31,12 @@ class VariableTest < Minitest::Test
def test_false_renders_as_false
assert_equal 'false', Template.parse("{{ foo }}").render!('foo' => false)
assert_equal 'false', Template.parse("{{ false }}").render!
end
def test_nil_renders_as_empty_string
assert_equal '', Template.parse("{{ nil }}").render!
assert_equal 'cat', Template.parse("{{ nil | append: 'cat' }}").render!
end
def test_preset_assigns

View File

@@ -57,6 +57,14 @@ module Minitest
Liquid::Strainer.class_variable_set(:@@filters, original_filters)
end
def with_taint_mode(mode)
old_mode = Liquid::Template.taint_mode
Liquid::Template.taint_mode = mode
yield
ensure
Liquid::Template.taint_mode = old_mode
end
def with_error_mode(mode)
old_mode = Liquid::Template.error_mode
Liquid::Template.error_mode = mode

View File

@@ -57,7 +57,8 @@ class StrainerUnitTest < Minitest::Test
end
def test_strainer_uses_a_class_cache_to_avoid_method_cache_invalidation
a, b = Module.new, Module.new
a = Module.new
b = Module.new
strainer = Strainer.create(nil, [a,b])
assert_kind_of Strainer, strainer
assert_kind_of a, strainer

View File

@@ -5,125 +5,123 @@ class VariableUnitTest < Minitest::Test
def test_variable
var = Variable.new('hello')
assert_equal 'hello', var.name
assert_equal VariableLookup.new('hello'), var.name
end
def test_filters
var = Variable.new('hello | textileze')
assert_equal 'hello', var.name
assert_equal [["textileze",[]]], var.filters
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['textileze',[]]], var.filters
var = Variable.new('hello | textileze | paragraph')
assert_equal 'hello', var.name
assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['textileze',[]], ['paragraph',[]]], var.filters
var = Variable.new(%! hello | strftime: '%Y'!)
assert_equal 'hello', var.name
assert_equal [["strftime",["'%Y'"]]], var.filters
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['strftime',['%Y']]], var.filters
var = Variable.new(%! 'typo' | link_to: 'Typo', true !)
assert_equal %!'typo'!, var.name
assert_equal [["link_to",["'Typo'", "true"]]], var.filters
assert_equal 'typo', var.name
assert_equal [['link_to',['Typo', true]]], var.filters
var = Variable.new(%! 'typo' | link_to: 'Typo', false !)
assert_equal %!'typo'!, var.name
assert_equal [["link_to",["'Typo'", "false"]]], var.filters
assert_equal 'typo', var.name
assert_equal [['link_to',['Typo', false]]], var.filters
var = Variable.new(%! 'foo' | repeat: 3 !)
assert_equal %!'foo'!, var.name
assert_equal [["repeat",["3"]]], var.filters
assert_equal 'foo', var.name
assert_equal [['repeat',[3]]], var.filters
var = Variable.new(%! 'foo' | repeat: 3, 3 !)
assert_equal %!'foo'!, var.name
assert_equal [["repeat",["3","3"]]], var.filters
assert_equal 'foo', var.name
assert_equal [['repeat',[3,3]]], var.filters
var = Variable.new(%! 'foo' | repeat: 3, 3, 3 !)
assert_equal %!'foo'!, var.name
assert_equal [["repeat",["3","3","3"]]], var.filters
assert_equal 'foo', var.name
assert_equal [['repeat',[3,3,3]]], var.filters
var = Variable.new(%! hello | strftime: '%Y, okay?'!)
assert_equal 'hello', var.name
assert_equal [["strftime",["'%Y, okay?'"]]], var.filters
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['strftime',['%Y, okay?']]], var.filters
var = Variable.new(%! hello | things: "%Y, okay?", 'the other one'!)
assert_equal 'hello', var.name
assert_equal [["things",["\"%Y, okay?\"","'the other one'"]]], var.filters
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 = Variable.new(%! '2006-06-06' | date: "%m/%d/%Y"!)
assert_equal "'2006-06-06'", var.name
assert_equal [["date",["\"%m/%d/%Y\""]]], var.filters
assert_equal '2006-06-06', var.name
assert_equal [['date',['%m/%d/%Y']]], var.filters
end
def test_filters_without_whitespace
var = Variable.new('hello | textileze | paragraph')
assert_equal 'hello', var.name
assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['textileze',[]], ['paragraph',[]]], var.filters
var = Variable.new('hello|textileze|paragraph')
assert_equal 'hello', var.name
assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['textileze',[]], ['paragraph',[]]], var.filters
var = Variable.new("hello|replace:'foo','bar'|textileze")
assert_equal 'hello', var.name
assert_equal [["replace", ["'foo'", "'bar'"]], ["textileze", []]], var.filters
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['replace', ['foo', 'bar']], ['textileze', []]], var.filters
end
def test_symbol
var = Variable.new("http://disney.com/logo.gif | image: 'med' ", :error_mode => :lax)
assert_equal "http://disney.com/logo.gif", var.name
assert_equal [["image",["'med'"]]], var.filters
assert_equal VariableLookup.new('http://disney.com/logo.gif'), var.name
assert_equal [['image',['med']]], var.filters
end
def test_string_to_filter
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
assert_equal 'http://disney.com/logo.gif', var.name
assert_equal [['image',['med']]], var.filters
end
def test_string_single_quoted
var = Variable.new(%| "hello" |)
assert_equal '"hello"', var.name
assert_equal 'hello', var.name
end
def test_string_double_quoted
var = Variable.new(%| 'hello' |)
assert_equal "'hello'", var.name
assert_equal 'hello', var.name
end
def test_integer
var = Variable.new(%| 1000 |)
assert_equal "1000", var.name
assert_equal 1000, var.name
end
def test_float
var = Variable.new(%| 1000.01 |)
assert_equal "1000.01", var.name
assert_equal 1000.01, var.name
end
def test_string_with_special_chars
var = Variable.new(%| 'hello! $!@.;"ddasd" ' |)
assert_equal %|'hello! $!@.;"ddasd" '|, var.name
assert_equal 'hello! $!@.;"ddasd" ', var.name
end
def test_string_dot
var = Variable.new(%| test.test |)
assert_equal 'test.test', var.name
assert_equal VariableLookup.new('test.test'), var.name
end
def test_filter_with_keyword_arguments
var = Variable.new(%! hello | things: greeting: "world", farewell: 'goodbye'!)
assert_equal 'hello', var.name
assert_equal [['things',["greeting: \"world\"","farewell: 'goodbye'"]]], var.filters
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['things', [], { 'greeting' => 'world', 'farewell' => 'goodbye' }]], var.filters
end
def test_lax_filter_argument_parsing
var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax)
assert_equal 'number_of_comments', var.name
assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters
assert_equal VariableLookup.new('number_of_comments'), var.name
assert_equal [['pluralize',['comment','comments']]], var.filters
end
def test_strict_filter_argument_parsing