Get rid of the 50-char one-liner rule in Haml.

This commit is contained in:
Nathan Weizenbaum 2008-04-24 19:14:52 -07:00
parent 22a22689e5
commit a241cd4eb4
7 changed files with 12 additions and 47 deletions

View File

@ -6,11 +6,6 @@ module Haml
class Buffer
include Haml::Helpers
# Set the maximum length for a line to be considered a one-liner.
# Lines <= the maximum will be rendered on one line,
# i.e. <tt><p>Hello world</p></tt>
ONE_LINER_LENGTH = 50
# The string that holds the compiled XHTML. This is aliased as
# _erbout for compatibility with ERB-specific code.
attr_accessor :buffer
@ -118,7 +113,7 @@ module Haml
result = html_escape(result) if escape_html
if close_tag && (@options[:ugly] || Buffer.one_liner?(result) || preserve_tag)
if close_tag && (@options[:ugly] || !result.include?("\n") || preserve_tag)
@buffer << "#{result}</#{close_tag}>\n"
@real_tabs -= 1
else
@ -162,7 +157,7 @@ module Haml
@buffer << "#{@options[:ugly] ? '' : tabs(tabulation)}<#{name}#{attributes}#{str}"
if content
if @options[:ugly] || Buffer.one_liner?(content)
if @options[:ugly] || !content.include?("\n")
@buffer << "#{content}</#{name}>\n"
else
@buffer << "\n#{tabs(@real_tabs+1)}#{content}\n#{tabs(@real_tabs)}</#{name}>\n"
@ -189,17 +184,11 @@ module Haml
to.merge!(from)
end
private
# Some of these methods are exposed as public class methods
# so they can be re-used in helpers.
# Returns whether or not the given value is short enough to be rendered
# on one line.
def self.one_liner?(value)
value.length <= ONE_LINER_LENGTH && value.scan(/\n/).empty?
end
private
@@tab_cache = {}
# Gets <tt>count</tt> tabs. Mostly for internal use.
def tabs(count)

View File

@ -284,13 +284,11 @@ END
def push_merged_text(text, tab_change = 0, try_one_liner = false)
@merged_text << (@options[:ugly] ? text : "#{' ' * @output_tabs}#{text}")
@tab_change += tab_change
@try_one_liner = try_one_liner
end
# Concatenate <tt>text</tt> to <tt>@buffer</tt> without tabulation.
def concat_merged_text(text)
@merged_text << text
@try_one_liner = false
end
def push_text(text, tab_change = 0, try_one_liner = false)
@ -301,11 +299,10 @@ END
return if @merged_text.empty?
@precompiled << "_hamlout.push_text(#{@merged_text.dump}"
@precompiled << ", #{@tab_change}" if @tab_change != 0 || @try_one_liner
@precompiled << ", #{@tab_change}" if @tab_change != 0
@precompiled << ");"
@merged_text = ''
@tab_change = 0
@try_one_liner = false
end
# Renders a block of text as plain text.
@ -559,10 +556,9 @@ END
self_closing ||= !!( !@block_opened && value.empty? && @options[:autoclose].include?(tag_name) )
one_liner = Buffer.one_liner?(value) || preserve_tag
if object_ref == "nil" && attributes_hash.nil? && !preserve_script && (parse || one_liner)
if object_ref == "nil" && attributes_hash.nil? && !preserve_script
# This means that we can render the tag directly to text and not process it in the buffer
tag_closed = !value.empty? && one_liner && !parse
tag_closed = !value.empty? && !parse
open_tag = prerender_tag(tag_name, self_closing, attributes)
open_tag << "#{value}</#{tag_name}>" if tag_closed
@ -610,7 +606,7 @@ END
open = "<!--#{conditional} "
# Render it statically if possible
if !content.empty? && Buffer.one_liner?(content)
unless content.empty?
return push_text("#{open}#{content} #{conditional ? "<![endif]-->" : "-->"}")
end

View File

@ -67,20 +67,8 @@ END
assert_equal("<p>Hello</p>", render('%p Hello').chomp)
end
def test_long_liner_should_not_print_on_one_line
assert_equal("<div>\n #{'x' * 51}\n</div>", render("%div #{'x' * 51}").chomp)
end
def test_non_prerendered_one_liner
assert_equal("<p class='awesome'>One line</p>\n", render("%p{:class => c} One line", :locals => {:c => 'awesome'}))
end
def test_non_prerendered_script_one_liner
assert_equal("<p class='awesome'>One line</p>\n", render("%p{:class => c}= 'One line'", :locals => {:c => 'awesome'}))
end
def test_non_prerendered_long_script_one_liner
assert_equal("<p class='awesome'>\n #{'x' * 60}\n</p>\n", render("%p{:class => c}= 'x' * 60", :locals => {:c => 'awesome'}))
def test_one_liner_with_newline_shouldnt_be_one_line
assert_equal("<p>\n foo\n bar\n</p>", render('%p= "foo\nbar"').chomp)
end
def test_multi_render

View File

@ -24,9 +24,7 @@
</p>
</div>
<p>foo</p>
<p>
reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
</p>
<p>reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong</p>
<div class='woah'>
<div id='funky'>
<div>

View File

@ -27,9 +27,6 @@ stuff followed by whitespace
yee\ha
</p>
<!-- Short comment -->
<!--
This is a really long comment look how long it is it should be on a line of its own don't you think?
-->
<!--
This is a block comment
cool, huh?

View File

@ -4,9 +4,7 @@
<title>Stop. haml time</title>
<div id='content'>
<h1>This is a title!</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit</p>
<p class='foo'>Cigarettes!</p>
<h2>Man alive!</h2>
<ul class='things'>

View File

@ -31,7 +31,6 @@
\%p foo
\yee\ha
/ Short comment
/ This is a really long comment look how long it is it should be on a line of its own don't you think?
/
This is a block comment
cool, huh?