1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Add option to always remove whitespace.

Closes #550
Closes #528

Signed-off-by: Norman Clarke <norman@njclarke.com>
This commit is contained in:
Tim van der Horst 2012-06-04 23:14:03 -03:00 committed by Norman Clarke
parent 27a670ea27
commit a836bf2f02
4 changed files with 35 additions and 1 deletions

View file

@ -160,6 +160,13 @@ Available options are:
Defaults to `true` in Rails production mode, and `false`
everywhere else.
{#remove_whitespace-option} `:remove_whitespace`
: If set to `true`, all tags are treated as if
both [whitespace removal](#whitespace_removal__and_)
options were present. Use with caution as this
may cause whitespace-related formatting errors.
Defaults to `false`.
{#suppress_eval-option} `:suppress_eval`
: Whether or not attribute hashes and Ruby scripts
designated by `=` or `~` should be

View file

@ -13,6 +13,7 @@ module Haml
:line => 1,
:mime_type => nil,
:preserve => %w(textarea pre code),
:remove_whitespace => false,
:suppress_eval => false,
:ugly => false
}
@ -49,6 +50,7 @@ module Haml
attr_reader :encoding
attr_reader :format
attr_reader :mime_type
attr_reader :remove_whitespace
def initialize(values = {}, &block)
@ -64,7 +66,8 @@ module Haml
send "#{key}=", value
end
[:escape_attrs, :hyphenate_data_attrs, :suppress_eval, :ugly].each do |method|
[:escape_attrs, :hyphenate_data_attrs, :remove_whitespace, :suppress_eval,
:ugly].each do |method|
class_eval(<<-END)
def #{method}?
!! @#{method}
@ -106,6 +109,11 @@ module Haml
end
@format = value
end
def remove_whitespace=(value)
@ugly = true if value
@remove_whitespace = value
end
if RUBY_VERSION < "1.9"
attr_writer :encoding

View file

@ -505,6 +505,11 @@ END
nuke_inner_whitespace = nuke_whitespace.include? '<'
end
if @options[:remove_whitespace]
nuke_outer_whitespace = true
nuke_inner_whitespace = true
end
value = value.to_s.strip
[tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
nuke_inner_whitespace, action, value, last_line || @index]

View file

@ -1331,6 +1331,20 @@ HAML
render("%p= 's' * 75", :ugly => true))
end
def test_remove_whitespace_true
assert_equal("<div id='outer'><div id='inner'><p>hello world</p></div></div>",
render("#outer\n #inner\n %p hello world", :remove_whitespace => true))
assert_equal("<p>hello world<pre>foo bar\nbaz</pre></p>", render(<<HAML, :remove_whitespace => true))
%p
hello world
%pre
foo bar
baz
HAML
assert_equal("<div><span>foo</span> <span>bar</span></div>",
render('%div <span>foo</span> <span>bar</span>', :remove_whitespace => true))
end
def test_auto_preserve_unless_ugly
assert_equal("<pre>foo&#x000A;bar</pre>\n", render('%pre="foo\nbar"'))
assert_equal("<pre>foo\nbar</pre>\n", render("%pre\n foo\n bar"))