[Haml] [html2haml] Expand ERB-to-interpolation to include CDATA.

This commit is contained in:
Nathan Weizenbaum 2009-10-05 17:12:56 -07:00
parent 0138b212c5
commit a630df6860
2 changed files with 25 additions and 1 deletions

View File

@ -41,6 +41,19 @@ module Haml
private private
def erb_to_interpolation(text, options)
return text unless options[:erb]
text = CGI.escapeHTML(uninterp(text))
%w[<haml:loud> </haml:loud>].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 # <haml:loud> element
str + '#{' + CGI.unescapeHTML(elem.innerText.strip) + '}'
end
end
end
def tabulate(tabs) def tabulate(tabs)
' ' * tabs ' ' * tabs
end end
@ -147,7 +160,9 @@ module Haml
class ::Hpricot::CData class ::Hpricot::CData
# @see Haml::HTML::Node#to_haml # @see Haml::HTML::Node#to_haml
def to_haml(tabs, options) 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
end end

View File

@ -122,6 +122,15 @@ HAML
HTML HTML
end end
def test_erb_in_cdata
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
:cdata
Foo \#{bar} baz
HAML
<![CDATA[Foo <%= bar %> baz]]>
HTML
end
def test_erb_in_line def test_erb_in_line
assert_equal 'foo bar #{baz}', render_erb('foo bar <%= baz %>') assert_equal 'foo bar #{baz}', render_erb('foo bar <%= baz %>')
assert_equal 'foo bar #{baz}! Bang.', render_erb('foo bar <%= baz %>! Bang.') assert_equal 'foo bar #{baz}! Bang.', render_erb('foo bar <%= baz %>! Bang.')