Compare commits

..

11 Commits

Author SHA1 Message Date
Dylan Thacker-Smith
25deb9e529 Update test to cover multiple consecutive dashes in identifiers. 2015-07-31 00:27:57 -04:00
Dylan Thacker-Smith
b140e91d8e Don't allow identifiers to end in a dash in strict parse mode. 2015-07-30 22:50:34 -04:00
Florian Weingarten
fc1c0d0d83 Merge pull request #632 from knu/fix_date_error
Properly rescue ::ArgumentError in the date filter
2015-07-24 10:50:52 -04:00
Akinori MUSHA
a215b70de9 Properly rescue ::ArgumentError in the date filter 2015-07-24 13:35:06 +09:00
Justin Li
1f70928f8a Update history to reflect merge of #631
[ci skip]
2015-07-23 17:07:40 -04:00
Justin Li
7713f6709d Update history for 3.0.5 2015-07-23 17:06:12 -04:00
Justin Li
239cf0e5f5 Update history for 2.6.3 2015-07-23 17:05:58 -04:00
Dylan Thacker-Smith
fa187665b3 Merge pull request #631 from Shopify/fix-tz-test-failure
Fix a timezone test failure.
2015-07-23 16:34:48 -04:00
Dylan Thacker-Smith
cd0c5e954c Fix a timezone test failure. 2015-07-23 16:19:59 -04:00
Florian Weingarten
490b457738 Merge pull request #626 from Shopify/fix_bracket_thing
Fix bracket thing
2015-07-17 17:19:06 +02:00
Florian Weingarten
4d6dec9b5a Fix chained access to multi-dimensional hash 2015-07-17 10:10:00 -04:00
10 changed files with 62 additions and 64 deletions

View File

@@ -15,6 +15,7 @@
* 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]
@@ -26,7 +27,15 @@
* 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.3 / 2015-05-28 / branch "3-0-stable"
## 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
* Fix condition parse order in strict mode (#569) [Justin Li]
@@ -74,7 +83,15 @@
* Make map filter work on enumerable drops (#233) [Florian Weingarten]
* Improved whitespace stripping for blank blocks, related to #216 [Florian Weingarten]
## 2.6.1 / 2014-01-10 / branch "2-6-stable"
## 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
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

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

View File

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

View File

@@ -3,7 +3,7 @@ module Liquid
def parse_with_selected_parser(markup)
case parse_context.error_mode
when :strict then strict_parse_with_error_context(markup)
when :lax, :lax_warn then lax_parse(markup)
when :lax then lax_parse(markup)
when :warn
begin
return strict_parse_with_error_context(markup)

View File

@@ -72,7 +72,7 @@ module Liquid
when String
Time.parse(obj)
end
rescue ArgumentError
rescue ::ArgumentError
nil
end
end

View File

@@ -10,16 +10,7 @@ module Liquid
# {{ user | link }}
#
class Variable
capture_ignored_variable_prefix = /([\s,\|'"]+?)??/
capture_expression = /(#{QuotedFragment})/o
capture_ignored_filter_prefix = /([^\|]+?)??/
capture_filters = /(#{FilterSeparator}.*)/o
VariableSyntax = /\A\s*#{capture_ignored_variable_prefix}\s*#{capture_expression}\s*(?:#{capture_ignored_filter_prefix}\s*#{capture_filters})?\z/om
capture_lax_separator = /(['"\|]+?)/
capture_filter = /((?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+)/o
FilterParser = /\s*(?:#{FilterSeparator}|#{capture_lax_separator})\s*#{capture_filter}/o
FilterParser = /(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o
attr_accessor :filters, :name, :line_number
attr_reader :parse_context
alias_method :options, :parse_context
@@ -44,19 +35,16 @@ module Liquid
def lax_parse(markup)
@filters = []
return unless markup =~ VariableSyntax
return unless markup =~ /(#{QuotedFragment})(.*)/om
add_syntax_warning("variable prefixed with ignored characters: #{$1.inspect}") if $1
name_markup = $2
add_syntax_warning("variable filter separator prefixed with ignored characters: #{$3.inspect}") if $3
filters_markup = $4
name_markup = $1
filter_markup = $2
@name = Expression.parse(name_markup)
if filters_markup
filters_markup.scan(FilterParser) do |lax_sep, f|
add_syntax_warning("unterminated quote or multiple pipe characters used as a filter separator: #{lax_sep.inspect}") if lax_sep
next unless f =~ /\A\s*(\W+)??(\w+)/
add_syntax_warning("ignored characters before filter name: #{$1.inspect}") if $1
filtername = $2
if filter_markup =~ /#{FilterSeparator}\s*(.*)/om
filters = $1.scan(FilterParser)
filters.each do |f|
next unless f =~ /\w+/
filtername = Regexp.last_match(0)
filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o).flatten
@filters << parse_filter_expressions(filtername, filterargs)
end
@@ -93,13 +81,6 @@ module Liquid
private
def add_syntax_warning(warning)
return unless parse_context.error_mode == :lax_warn
error = SyntaxError.new(warning)
error.line_number = parse_context.line_number
parse_context.warnings << error
end
def parse_filter_expressions(filter_name, unparsed_args)
filter_args = []
keyword_args = {}

View File

@@ -43,6 +43,14 @@ 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,8 +263,10 @@ class StandardFiltersTest < Minitest::Test
assert_equal '', @filters.date('', "%B")
assert_equal "07/05/2006", @filters.date(1152098955, "%m/%d/%Y")
assert_equal "07/05/2006", @filters.date("1152098955", "%m/%d/%Y")
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
end
def test_first_last
@@ -417,4 +419,19 @@ class StandardFiltersTest < Minitest::Test
def test_cannot_access_private_methods
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
end
end # StandardFiltersTest

View File

@@ -89,32 +89,4 @@ class VariableTest < Minitest::Test
def test_multiline_variable
assert_equal 'worked', Template.parse("{{\ntest\n}}").render!('test' => 'worked')
end
def test_lax_warnings_for_variable_ignored_prefix
ignored_chars = %(|,'")
template = Liquid::Template.parse("{{ #{ignored_chars}test }}", error_mode: :lax_warn)
assert_equal "works", template.render!('test' => 'works')
assert_equal [Liquid::SyntaxError.new("variable prefixed with ignored characters: #{ignored_chars.inspect}")], template.warnings
end
def test_lax_warnings_for_ignored_variable_filter_prefix
ignored_chars = ",wat? lax!"
template = Liquid::Template.parse("{{ test#{ignored_chars} | noop }}", error_mode: :lax_warn)
assert_equal "works", template.render!('test' => 'works')
assert_equal [Liquid::SyntaxError.new("variable filter separator prefixed with ignored characters: #{ignored_chars.inspect}")], template.warnings
end
def test_lax_warnings_for_weird_filter_chars
template = Liquid::Template.parse("{{ test | upcase \" prepend: 'it ' || append: ' surprisingly' }}", error_mode: :lax_warn)
assert_equal "it WORKS surprisingly", template.render!('test' => 'works')
expected_warnings = ['"', '||'].map{ |sep| Liquid::SyntaxError.new("unterminated quote or multiple pipe characters used as a filter separator: #{sep.inspect}") }
assert_equal expected_warnings, template.warnings
end
def test_lax_warnings_for_ignored_filter_name_prefix
ignored_chars = "'': '"
template = Liquid::Template.parse("{{ test | '': 'prepend ' }}", error_mode: :lax_warn)
assert_equal "prepend wat", template.render!('test' => 'wat')
assert_equal [Liquid::SyntaxError.new("ignored characters before filter name: #{ignored_chars.inspect}")], template.warnings
end
end

View File

@@ -36,6 +36,9 @@ 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