mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
[Haml] [html2haml] Support :javascript filters.
This commit is contained in:
parent
a630df6860
commit
d851ff5b19
3 changed files with 90 additions and 0 deletions
|
@ -54,6 +54,23 @@ including the line number and the offending character.
|
|||
Foo #{bar} baz!
|
||||
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,
|
||||
without spaces within the curly braces
|
||||
(e.g. `%p{:foo => "bar"}` as opposed to `%p{ :foo => "bar" }`).
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -84,6 +84,38 @@ HAML
|
|||
HTML
|
||||
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" & "13";
|
||||
}
|
||||
</script>
|
||||
HTML
|
||||
end
|
||||
|
||||
def test_script_tag_with_cdata
|
||||
assert_equal(<<HAML.rstrip, render(<<HTML))
|
||||
:javascript
|
||||
function foo() {
|
||||
return "&";
|
||||
}
|
||||
HAML
|
||||
<script type="text/javascript">
|
||||
<![CDATA[
|
||||
function foo() {
|
||||
return "&";
|
||||
}
|
||||
]]>
|
||||
</script>
|
||||
HTML
|
||||
end
|
||||
|
||||
## ERB
|
||||
|
||||
def test_erb
|
||||
|
@ -131,6 +163,21 @@ HAML
|
|||
HTML
|
||||
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
|
||||
assert_equal 'foo bar #{baz}', render_erb('foo bar <%= baz %>')
|
||||
assert_equal 'foo bar #{baz}! Bang.', render_erb('foo bar <%= baz %>! Bang.')
|
||||
|
|
Loading…
Add table
Reference in a new issue