diff --git a/doc-src/HAML_CHANGELOG.md b/doc-src/HAML_CHANGELOG.md
index abfbb601..7d5f6eaf 100644
--- a/doc-src/HAML_CHANGELOG.md
+++ b/doc-src/HAML_CHANGELOG.md
@@ -54,6 +54,23 @@ including the line number and the offending character.
Foo #{bar} baz!
Flip #{bang}.
+* `
+
+ is now transformed into:
+
+ :javascript
+ function foo() {
+ return 12;
+ }
+
* 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" }`).
diff --git a/lib/haml/html.rb b/lib/haml/html.rb
index 48063640..da32bcb4 100644
--- a/lib/haml/html.rb
+++ b/lib/haml/html.rb
@@ -66,6 +66,9 @@ module Haml
parse_text_with_interpolation(uninterp(text), tabs)
end
+ def parse_text_with_erb(text, tabs, options)
+ end
+
def parse_text_with_interpolation(text, tabs)
text.strip!
return "" if text.empty?
@@ -210,6 +213,11 @@ module Haml
# @see Haml::HTML::Node#to_haml
def to_haml(tabs, options)
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)}"
if options[:erb] && name[0...5] == 'haml:'
@@ -264,6 +272,24 @@ module Haml
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)
"= #{text.gsub(/\n\s*/, ' ').strip}\n"
end
diff --git a/test/haml/html2haml_test.rb b/test/haml/html2haml_test.rb
index 62f895f5..eeee4414 100644
--- a/test/haml/html2haml_test.rb
+++ b/test/haml/html2haml_test.rb
@@ -84,6 +84,38 @@ HAML
HTML
end
+ def test_script_tag
+ assert_equal(<
+ function foo() {
+ return "12" & "13";
+ }
+
+HTML
+ end
+
+ def test_script_tag_with_cdata
+ assert_equal(<
+
+
+HTML
+ end
+
## ERB
def test_erb
@@ -131,6 +163,21 @@ HAML
HTML
end
+ def test_erb_in_script
+ assert_equal(<
+ function foo() {
+ return <%= foo.to_json %>;
+ }
+
+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.')