diff --git a/History.md b/History.md index 2f869195..2dc8f3d0 100644 --- a/History.md +++ b/History.md @@ -24,6 +24,8 @@ * Liquid::Template.register_filter raises when the module overrides registered public methods as private or protected (#705) [Gaurav Chande] ### Fixed + +* Fix variable names being detected as an operator when starting with contains (#788) [Michael Angell] * Fix include tag used with strict_variables (#828) [QuickPay] * Fix map filter when value is a Proc (#672) [Guillaume Malette] * Fix truncate filter when value is not a string (#672) [Guillaume Malette] diff --git a/lib/liquid/lexer.rb b/lib/liquid/lexer.rb index b9f2422e..dee07f02 100644 --- a/lib/liquid/lexer.rb +++ b/lib/liquid/lexer.rb @@ -18,10 +18,10 @@ module Liquid DOUBLE_STRING_LITERAL = /"[^\"]*"/ NUMBER_LITERAL = /-?\d+(\.\d+)?/ DOTDOT = /\.\./ - COMPARISON_OPERATOR = /==|!=|<>|<=?|>=?|contains/ + COMPARISON_OPERATOR = /==|!=|<>|<=?|>=?|contains(?=\s)/ def initialize(input) - @ss = StringScanner.new(input.rstrip) + @ss = StringScanner.new(input) end def tokenize @@ -29,6 +29,7 @@ module Liquid until @ss.eos? @ss.skip(/\s*/) + break if @ss.eos? tok = case when t = @ss.scan(COMPARISON_OPERATOR) then [:comparison, t] when t = @ss.scan(SINGLE_STRING_LITERAL) then [:string, t] diff --git a/test/integration/parsing_quirks_test.rb b/test/integration/parsing_quirks_test.rb index 3531318c..23742dc7 100644 --- a/test/integration/parsing_quirks_test.rb +++ b/test/integration/parsing_quirks_test.rb @@ -115,4 +115,8 @@ class ParsingQuirksTest < Minitest::Test assert_template_result('12345', "{% for i in (1...5) %}{{ i }}{% endfor %}") end end + + def test_contains_in_id + assert_template_result(' YES ', '{% if containsallshipments == true %} YES {% endif %}', 'containsallshipments' => true) + end end # ParsingQuirksTest diff --git a/test/unit/lexer_unit_test.rb b/test/unit/lexer_unit_test.rb index af9c267b..5adcf2bd 100644 --- a/test/unit/lexer_unit_test.rb +++ b/test/unit/lexer_unit_test.rb @@ -19,7 +19,7 @@ class LexerUnitTest < Minitest::Test end def test_comparison - tokens = Lexer.new('== <> contains').tokenize + tokens = Lexer.new('== <> contains ').tokenize assert_equal [[:comparison, '=='], [:comparison, '<>'], [:comparison, 'contains'], [:end_of_string]], tokens end