diff --git a/lib/haml/html.rb b/lib/haml/html.rb index 59fdfcad..48063640 100644 --- a/lib/haml/html.rb +++ b/lib/haml/html.rb @@ -41,6 +41,19 @@ module Haml private + def erb_to_interpolation(text, options) + return text unless options[:erb] + text = CGI.escapeHTML(uninterp(text)) + %w[ ].each {|str| text.gsub!(CGI.escapeHTML(str), str)} + ::Hpricot::XML(text).children.inject("") do |str, elem| + if elem.is_a?(::Hpricot::Text) + str + CGI.unescapeHTML(elem.to_s) + else # element + str + '#{' + CGI.unescapeHTML(elem.innerText.strip) + '}' + end + end + end + def tabulate(tabs) ' ' * tabs end @@ -147,7 +160,9 @@ module Haml class ::Hpricot::CData # @see Haml::HTML::Node#to_haml def to_haml(tabs, options) - "#{tabulate(tabs)}:cdata\n#{parse_text(self.content, tabs + 1)}" + content = parse_text_with_interpolation( + erb_to_interpolation(self.content, options), tabs + 1) + "#{tabulate(tabs)}:cdata\n#{content}" end end diff --git a/test/haml/html2haml_test.rb b/test/haml/html2haml_test.rb index 770e7163..62f895f5 100644 --- a/test/haml/html2haml_test.rb +++ b/test/haml/html2haml_test.rb @@ -122,6 +122,15 @@ HAML HTML end + def test_erb_in_cdata + assert_equal(< baz]]> +HTML + end + def test_erb_in_line assert_equal 'foo bar #{baz}', render_erb('foo bar <%= baz %>') assert_equal 'foo bar #{baz}! Bang.', render_erb('foo bar <%= baz %>! Bang.')