Skip to content

Commit

Permalink
Merge pull request #1 from Shopify/master
Browse files Browse the repository at this point in the history
Pull inline with upstream
  • Loading branch information
evulse authored Aug 17, 2016
2 parents 19c6eb4 + 50c85af commit f41ed78
Show file tree
Hide file tree
Showing 6 changed files with 563 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ group :test do
gem 'rubocop', '0.34.2'

platform :mri do
gem 'liquid-c', github: 'Shopify/liquid-c', ref: '2570693d8d03faa0df9160ec74348a7149436df3'
gem 'liquid-c', github: 'Shopify/liquid-c', ref: '1fa04f1d3d4fdb5cc33bcc090334ab097ce2c10c'
end
end
3 changes: 2 additions & 1 deletion lib/liquid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Liquid
ArgumentSeparator = ','.freeze
FilterArgumentSeparator = ':'.freeze
VariableAttributeSeparator = '.'.freeze
WhitespaceControl = '-'.freeze
TagStart = /\{\%/
TagEnd = /\%\}/
VariableSignature = /\(?[\w\-\.\[\]]\)?/
Expand All @@ -34,7 +35,7 @@ module Liquid
QuotedString = /"[^"]*"|'[^']*'/
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/o
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/o
AnyStartingTag = /\{\{|\{\%/
AnyStartingTag = /#{TagStart}|#{VariableStart}/o
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/o
Expand Down
20 changes: 18 additions & 2 deletions lib/liquid/block_body.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Liquid
class BlockBody
FullToken = /\A#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
ContentOfVariable = /\A#{VariableStart}(.*)#{VariableEnd}\z/om
FullToken = /\A#{TagStart}#{WhitespaceControl}?\s*(\w+)\s*(.*?)#{WhitespaceControl}?#{TagEnd}\z/om
ContentOfVariable = /\A#{VariableStart}#{WhitespaceControl}?(.*?)#{WhitespaceControl}?#{VariableEnd}\z/om
TAGSTART = "{%".freeze
VARSTART = "{{".freeze

Expand All @@ -18,6 +18,7 @@ def parse(tokenizer, parse_context)
unless token.empty?
case
when token.start_with?(TAGSTART)
whitespace_handler(token, parse_context)
if token =~ FullToken
tag_name = $1
markup = $2
Expand All @@ -35,9 +36,14 @@ def parse(tokenizer, parse_context)
raise_missing_tag_terminator(token, parse_context)
end
when token.start_with?(VARSTART)
whitespace_handler(token, parse_context)
@nodelist << create_variable(token, parse_context)
@blank = false
else
if parse_context.trim_whitespace
token.lstrip!
end
parse_context.trim_whitespace = false
@nodelist << token
@blank &&= !!(token =~ /\A\s*\z/)
end
Expand All @@ -48,6 +54,16 @@ def parse(tokenizer, parse_context)
yield nil, nil
end

def whitespace_handler(token, parse_context)
if token[2] == WhitespaceControl
previous_token = @nodelist.last
if previous_token.is_a? String
previous_token.rstrip!
end
end
parse_context.trim_whitespace = (token[-3] == WhitespaceControl)
end

def blank?
@blank
end
Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/parse_context.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Liquid
class ParseContext
attr_accessor :locale, :line_number
attr_accessor :locale, :line_number, :trim_whitespace
attr_reader :partial, :warnings, :error_mode

def initialize(options = {})
Expand Down
16 changes: 16 additions & 0 deletions test/integration/error_handling_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ def test_with_line_numbers_adds_numbers_to_parser_errors
assert_match(/Liquid syntax error \(line 4\)/, err.message)
end

def test_with_line_numbers_adds_numbers_to_parser_errors_with_whitespace_trim
err = assert_raises(SyntaxError) do
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('
foobar
Expand Down
Loading

0 comments on commit f41ed78

Please sign in to comment.