[Haml] [html2haml] Support :javascript filters.

This commit is contained in:
Nathan Weizenbaum 2009-10-05 17:53:35 -07:00
parent a630df6860
commit d851ff5b19
3 changed files with 90 additions and 0 deletions

View File

@ -54,6 +54,23 @@ including the line number and the offending character.
Foo #{bar} baz! Foo #{bar} baz!
Flip #{bang}. Flip #{bang}.
* `<script>` tags are now transformed into `:javascript` filters,
and indentation is preserved.
For example:
<script type="text/javascript">
function foo() {
return 12;
}
</script>
is now transformed into:
:javascript
function foo() {
return 12;
}
* Attributes are now output in a more-standard format, * Attributes are now output in a more-standard format,
without spaces within the curly braces without spaces within the curly braces
(e.g. `%p{:foo => "bar"}` as opposed to `%p{ :foo => "bar" }`). (e.g. `%p{:foo => "bar"}` as opposed to `%p{ :foo => "bar" }`).

View File

@ -66,6 +66,9 @@ module Haml
parse_text_with_interpolation(uninterp(text), tabs) parse_text_with_interpolation(uninterp(text), tabs)
end end
def parse_text_with_erb(text, tabs, options)
end
def parse_text_with_interpolation(text, tabs) def parse_text_with_interpolation(text, tabs)
text.strip! text.strip!
return "" if text.empty? return "" if text.empty?
@ -210,6 +213,11 @@ module Haml
# @see Haml::HTML::Node#to_haml # @see Haml::HTML::Node#to_haml
def to_haml(tabs, options) def to_haml(tabs, options)
return "" if converted_to_haml return "" if converted_to_haml
if name == "script" &&
(attributes['type'].nil? || attributes['type'] == "text/javascript") &&
(attributes.keys - ['type']).empty?
return script_to_haml(tabs, options)
end
output = "#{tabulate(tabs)}" output = "#{tabulate(tabs)}"
if options[:erb] && name[0...5] == 'haml:' if options[:erb] && name[0...5] == 'haml:'
@ -264,6 +272,24 @@ module Haml
end end
end end
def script_to_haml(tabs, options)
content =
if children.first.is_a?(::Hpricot::CData)
children.first.content
else
CGI.unescapeHTML(self.innerText)
end
content = erb_to_interpolation(content, options)
content.gsub!(/\A\s*\n(\s*)/, '\1')
original_indent = content[/\A(\s*)/, 1]
if content.split("\n").all? {|l| l.strip.empty? || l =~ /^#{original_indent}/}
content.gsub!(/^#{original_indent}/, tabulate(tabs + 1))
end
"#{tabulate(tabs)}:javascript\n#{content}"
end
def haml_tag_loud(text) def haml_tag_loud(text)
"= #{text.gsub(/\n\s*/, ' ').strip}\n" "= #{text.gsub(/\n\s*/, ' ').strip}\n"
end end

View File

@ -84,6 +84,38 @@ HAML
HTML HTML
end end
def test_script_tag
assert_equal(<<HAML.rstrip, render(<<HTML))
:javascript
function foo() {
return "12" & "13";
}
HAML
<script type="text/javascript">
function foo() {
return "12" &amp; "13";
}
</script>
HTML
end
def test_script_tag_with_cdata
assert_equal(<<HAML.rstrip, render(<<HTML))
:javascript
function foo() {
return "&amp;";
}
HAML
<script type="text/javascript">
<![CDATA[
function foo() {
return "&amp;";
}
]]>
</script>
HTML
end
## ERB ## ERB
def test_erb def test_erb
@ -131,6 +163,21 @@ HAML
HTML HTML
end end
def test_erb_in_script
assert_equal(<<HAML.rstrip, render_erb(<<HTML))
:javascript
function foo() {
return \#{foo.to_json};
}
HAML
<script type="text/javascript">
function foo() {
return <%= foo.to_json %>;
}
</script>
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.')