[Haml] [html2haml] Add support for the :css filter as well.

This commit is contained in:
Nathan Weizenbaum 2009-10-05 18:03:35 -07:00
parent d851ff5b19
commit 68bf3acfdb
3 changed files with 23 additions and 3 deletions

View File

@ -55,6 +55,7 @@ including the line number and the offending character.
Flip #{bang}.
* `<script>` tags are now transformed into `:javascript` filters,
and `<style>` tags into `:css` filters.
and indentation is preserved.
For example:

View File

@ -216,7 +216,11 @@ module Haml
if name == "script" &&
(attributes['type'].nil? || attributes['type'] == "text/javascript") &&
(attributes.keys - ['type']).empty?
return script_to_haml(tabs, options)
return to_haml_filter(:javascript, tabs, options)
elsif name == "style" &&
(attributes['type'].nil? || attributes['type'] == "text/css") &&
(attributes.keys - ['type']).empty?
return to_haml_filter(:css, tabs, options)
end
output = "#{tabulate(tabs)}"
@ -272,7 +276,7 @@ module Haml
end
end
def script_to_haml(tabs, options)
def to_haml_filter(filter, tabs, options)
content =
if children.first.is_a?(::Hpricot::CData)
children.first.content
@ -287,7 +291,7 @@ module Haml
content.gsub!(/^#{original_indent}/, tabulate(tabs + 1))
end
"#{tabulate(tabs)}:javascript\n#{content}"
"#{tabulate(tabs)}:#{filter}\n#{content}"
end
def haml_tag_loud(text)

View File

@ -178,6 +178,21 @@ HAML
HTML
end
def test_erb_in_style
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
:css
foo {
bar: baz;
}
HAML
<style type="text/css">
foo {
bar: baz;
}
</style>
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.')