Skip to content

Commit

Permalink
Merge pull request #770 from haml/escape-filter-interpolation
Browse files Browse the repository at this point in the history
HTML escape interpolated code in filters
  • Loading branch information
teeparham committed Apr 19, 2014
2 parents 800095b + 8591704 commit 2630344
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/haml/filters.rb
Expand Up @@ -163,7 +163,7 @@ def compile(compiler, text)
if contains_interpolation?(text)
return if options[:suppress_eval]

text = unescape_interpolation(text).gsub(/(\\+)n/) do |s|
text = unescape_interpolation(text, options[:escape_html]).gsub(/(\\+)n/) do |s|
escapes = $1.size
next s if escapes % 2 == 0
"#{'\\' * (escapes - 1)}\n"
Expand Down
9 changes: 7 additions & 2 deletions test/filters_test.rb
Expand Up @@ -109,6 +109,11 @@ def evaluate(scope, locals, &block)
end
end

test "interpolated code should use be escaped in escape_html is set" do
assert_equal "<script>evil</script>\n",
render(":plain\n \#{'<script>evil</script>'}", :escape_html => true)
end

end

class ErbFilterTest < MiniTest::Unit::TestCase
Expand Down Expand Up @@ -140,8 +145,8 @@ class JavascriptFilterTest < MiniTest::Unit::TestCase
assert_match(/bar/, html)
end

test "should never HTML-escape ampersands" do
html = "<script>\n & < > &\n</script>\n"
test "should never HTML-escape non-interpolated ampersands" do
html = "<script>\n & < > &amp;\n</script>\n"
haml = %Q{:javascript\n & < > \#{"&"}}
assert_equal(html, render(haml, :escape_html => true))
end
Expand Down

0 comments on commit 2630344

Please sign in to comment.