[Haml] [html2haml] Properly parse conditional comments.

This commit is contained in:
Nathan Weizenbaum 2009-10-10 17:42:57 -07:00
parent d18f8bfdcd
commit 4ccb75c4df
3 changed files with 32 additions and 3 deletions

View File

@ -90,6 +90,8 @@ including the line number and the offending character.
* Self-closing tags (such as `<br />`) are now transformed into
self-closing Haml tags (like `%br/`).
* IE conditional comments are now properly parsed.
* Attributes are now output in a more-standard format,
without spaces within the curly braces
(e.g. `%p{:foo => "bar"}` as opposed to `%p{ :foo => "bar" }`).

View File

@ -204,10 +204,16 @@ module Haml
class ::Hpricot::Comment
# @see Haml::HTML::Node#to_haml
def to_haml(tabs, options)
if self.content.include?("\n")
"#{tabulate(tabs)}/\n#{parse_text(self.content, tabs + 1)}"
content = self.content
if content =~ /\A(\[[^\]]+\])>(.*)<!\[endif\]\z/m
condition = $1
content = $2
end
if content.include?("\n")
"#{tabulate(tabs)}/#{condition}\n#{parse_text(content, tabs + 1)}"
else
"#{tabulate(tabs)}/ #{self.content.strip}"
"#{tabulate(tabs)}/#{condition} #{content.strip}"
end
end
end

View File

@ -173,6 +173,27 @@ baz</code>
HTML
end
def test_conditional_comment
assert_equal(<<HAML.rstrip, render(<<HTML))
/[if foo]
bar
baz
HAML
<!--[if foo]>
bar
baz
<![endif]-->
HTML
end
def test_inline_conditional_comment
assert_equal(<<HAML.rstrip, render(<<HTML))
/[if foo] bar baz
HAML
<!--[if foo]> bar baz <![endif]-->
HTML
end
## ERB
def test_erb