Skip to content

Commit

Permalink
Merge pull request #3 from snatchev/master
Browse files Browse the repository at this point in the history
fix for haml/haml#486 - indentation issue on comments
  • Loading branch information
norman committed Jun 13, 2012
2 parents d093812 + 592d148 commit fe26b97
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
13 changes: 11 additions & 2 deletions lib/html2haml/html/erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def add_text(src, text)
# @param src [String] The source buffer
# @param code [String] The Ruby statement to add to the buffer
def add_stmt(src, code)
src << '</haml:block>' if block_closer?(code) || mid_block?(code)
src << '</haml:block>' if has_code?(code) && block_closer?(code) || mid_block?(code)
src << '<haml:silent>' << h(code) << '</haml:silent>' unless code.strip == "end"
src << '<haml:block>' if block_opener?(code) || mid_block?(code)
src << '<haml:block>' if has_code?(code) && block_opener?(code) || mid_block?(code)
end

# Concatenates a Ruby expression that's printed to the document
Expand Down Expand Up @@ -103,6 +103,15 @@ def valid_ruby?(code)
false
end

# Returns whether the code has any content
# This is used to test whether lines have been removed by erubis, such as comments
#
# @param code [String] Ruby code to check
# @return [Boolean]
def has_code?(code)
code != "\n"
end

# Checks if a string of Ruby code opens a block.
# This could either be something like `foo do |a|`
# or a keyword that requires a matching `end`
Expand Down
39 changes: 38 additions & 1 deletion test/erb_tests.rb → test/erb_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module ErbTests
require 'test_helper'

class ErbTest < MiniTest::Unit::TestCase
def test_erb
assert_equal '- foo = bar', render_erb('<% foo = bar %>')
assert_equal '- foo = bar', render_erb('<% foo = bar -%>')
Expand Down Expand Up @@ -437,4 +439,39 @@ def test_silent_inside_block_inside_tag
</table>
ERB
end

def test_commented_erb_should_not_cause_indentation
assert_equal(<<HAML.rstrip, render_erb(<<ERB))
%title
html2haml and multiline titles
= # stylesheet_link_tag :first
= stylesheet_link_tag 'another file'
HAML
<title>
html2haml and multiline titles
</title>
<%=# stylesheet_link_tag :first %>
<%#= stylesheet_link_tag :second %>
<%# stylesheet_link_tag :third %>
<%= stylesheet_link_tag 'another file' %>
ERB
end

def test_should_wrap_in_silent
assert_equal(<<HTML.rstrip, Haml::HTML::ERB.new(<<ERB).src)
<haml:silent> some_variable_or_function \n</haml:silent>
HTML
<% some_variable_or_function %>
ERB
end

#comment content is removed by erubis
def test_should_wrap_process_comments_as_empty_lines
assert_equal(<<HTML.rstrip, Haml::HTML::ERB.new(<<ERB).src)
<haml:silent>\n</haml:silent>
HTML
<%# some_variable_or_function %>
ERB
end

end
19 changes: 0 additions & 19 deletions test/html2haml_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'test_helper'
require 'erb_tests'
require 'html2haml/html'

class Html2HamlTest < MiniTest::Unit::TestCase
def test_empty_render_should_remain_empty
Expand Down Expand Up @@ -292,13 +290,6 @@ def test_comma_post_tag_with_text_before
HTML
end

begin
require 'html2haml/html/erb'
include ErbTests
rescue LoadError => e
puts "\n** Couldn't require #{e.message[/-- (.*)$/, 1]}, skipping some tests"
end

# Encodings

unless RUBY_VERSION < "1.9"
Expand Down Expand Up @@ -329,14 +320,4 @@ def test_xhtml_strict_doctype
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
HTML
end

protected

def render(text, options = {})
Haml::HTML.new(text, options).render.rstrip
end

def render_erb(text)
render(text, :erb => true)
end
end
15 changes: 14 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@

require "bundler/setup"
require "minitest/autorun"
require "html2haml"
require "html2haml"
require 'html2haml/html'
require 'html2haml/html/erb'

class MiniTest::Unit::TestCase
protected
def render(text, options = {})
Haml::HTML.new(text, options).render.rstrip
end

def render_erb(text)
render(text, :erb => true)
end
end

0 comments on commit fe26b97

Please sign in to comment.