From 40da61392fb4123daee75381dc7f311495ee3296 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Wed, 13 Feb 2008 20:13:12 -0800 Subject: [PATCH] Tweak the :ugly code a little. --- lib/haml/buffer.rb | 26 +++++++++++--------------- test/haml/engine_test.rb | 17 +++++++---------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb index edacc700..89d237b7 100644 --- a/lib/haml/buffer.rb +++ b/lib/haml/buffer.rb @@ -46,12 +46,12 @@ module Haml # Renders +text+ with the proper tabulation. This also deals with # making a possible one-line tag one line or not. def push_text(text, tab_change = 0) - if(@tabulation > 0 && !@options[:ugly]) + if @tabulation > 0 && !@options[:ugly] # Have to push every line in by the extra user set tabulation text.gsub!(/^/m, ' ' * @tabulation) end - @buffer << "#{text}" + @buffer << text @real_tabs += tab_change end @@ -70,20 +70,20 @@ module Haml result = result[0...-1] end - if close_tag && Buffer.one_liner?(result) - @buffer << result - @buffer << "\n" + if close_tag && (@options[:ugly] || Buffer.one_liner?(result)) + @buffer << "#{result}\n" @real_tabs -= 1 else if close_tag @buffer << "\n" end - (result = result.gsub(/^/m, tabs(tabulation))) unless @options[:ugly] + result = result.gsub(/^/m, tabs(tabulation)) unless @options[:ugly] @buffer << "#{result}\n" if close_tag - @buffer << (@options[:ugly] ? "\n" : "#{tabs(tabulation-1)}\n") + # We never get here if @options[:ugly] is true + @buffer << "#{tabs(tabulation-1)}\n" @real_tabs -= 1 end end @@ -109,16 +109,12 @@ module Haml else str = ">\n" end - if @options[:ugly] - @buffer << "<#{name}#{Precompiler.build_attributes(@options[:attr_wrapper], attributes)}#{str}" - else - @buffer << "#{tabs(tabulation)}<#{name}#{Precompiler.build_attributes(@options[:attr_wrapper], attributes)}#{str}" - end + + @buffer << "#{@options[:ugly] ? '' : tabs(tabulation)}<#{name}#{Precompiler.build_attributes(@options[:attr_wrapper], attributes)}#{str}" + if content - if Buffer.one_liner?(content) + if @options[:ugly] || Buffer.one_liner?(content) @buffer << "#{content}\n" - elsif @options[:ugly] - @buffer << "\n#{content}\n\n" else @buffer << "\n#{tabs(@real_tabs+1)}#{content}\n#{tabs(@real_tabs)}\n" end diff --git a/test/haml/engine_test.rb b/test/haml/engine_test.rb index 96fa79cb..7bc98428 100644 --- a/test/haml/engine_test.rb +++ b/test/haml/engine_test.rb @@ -385,16 +385,13 @@ class EngineTest < Test::Unit::TestCase end def test_ugly_true - input = "#outer\n #inner\n %p hello world" - actual = Haml::Engine.new(input, :ugly => true).render - expected = "
\n
\n

hello world

\n
\n
\n" - assert_equal(expected, actual) - end + assert_equal("
\n
\n

hello world

\n
\n
\n", + render("#outer\n #inner\n %p hello world", :ugly => true)) - def test_ugly_false - input = "#outer\n #inner\n %p hello world" - actual = Haml::Engine.new(input, :ugly => false).render - expected = "
\n
\n

hello world

\n
\n
\n" - assert_equal(expected, actual) + assert_equal("

#{'s' * 75}

\n", + render("%p #{'s' * 75}", :ugly => true)) + + assert_equal("

#{'s' * 75}

\n", + render("%p= 's' * 75", :ugly => true)) end end