Skip to content

Commit

Permalink
Handle parser edge case with '!' and '&'
Browse files Browse the repository at this point in the history
Treat '!' and '&' as operators when immediately followed by
interpolation.

Resolves haml#841
  • Loading branch information
norman committed Jun 8, 2015
1 parent 1f8821c commit 1e5a4ba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/haml/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def process_line(line)
return push plain(line.strip!(3), :escape_html) if line.text[1, 2] == '=='
return push script(line.strip!(2), :escape_html) if line.text[1] == SCRIPT
return push flat_script(line.strip!(2), :escape_html) if line.text[1] == FLAT_SCRIPT
return push plain(line.strip!(1), :escape_html) if line.text[1] == ?\s
return push plain(line.strip!(1), :escape_html) if line.text[1] == ?\s || line.text[1..2] == '#{'
push plain(line)
when SCRIPT
return push plain(line.strip!(2)) if line.text[1] == SCRIPT
Expand All @@ -252,7 +252,7 @@ def process_line(line)
return push plain(line.strip!(3), false) if line.text[1, 2] == '=='
return push script(line.strip!(2), false) if line.text[1] == SCRIPT
return push flat_script(line.strip!(2), false) if line.text[1] == FLAT_SCRIPT
return push plain(line.strip!(1), false) if line.text[1] == ?\s
return push plain(line.strip!(1), false) if line.text[1] == ?\s || line.text[1..2] == '#{'
push plain(line)
when ESCAPE
line.text = line.text[1..-1]
Expand Down
10 changes: 10 additions & 0 deletions test/engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1032,11 +1032,21 @@ def test_escaped_string_interpolation
assert_equal("<p>\n 4&&lt;\n</p>\n", render("%p\n & \#{2+2}&\#{'<'}", :escape_html => false))
end

def test_escaped_string_interpolation_with_no_space
assert_equal("&lt;br&gt;\n", render('&#{"<br>"}'))
assert_equal("<span>&lt;br&gt;</span>\n", render('%span&#{"<br>"}'))
end

def test_unescaped_string_interpolation
assert_equal("<p>\n 4&<\n</p>\n", render("%p\n ! \#{2+2}&\#{'<'}", :escape_html => true))
assert_equal("<p>\n 4&<\n</p>\n", render("%p\n ! \#{2+2}&\#{'<'}", :escape_html => false))
end

def test_unescaped_string_interpolation_with_no_space
assert_equal("<br>\n", render('!#{"<br>"}'))
assert_equal("<span><br></span>\n", render('%span!#{"<br>"}'))
end

def test_scripts_should_respect_escape_html_option
assert_equal("<p>\n foo &amp; bar\n</p>\n", render("%p\n = 'foo & bar'", :escape_html => true))
assert_equal("<p>\n foo & bar\n</p>\n", render("%p\n = 'foo & bar'", :escape_html => false))
Expand Down

0 comments on commit 1e5a4ba

Please sign in to comment.