diff --git a/REFERENCE.md b/REFERENCE.md index 949fdfdc..32bd5ce6 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -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 diff --git a/lib/haml/options.rb b/lib/haml/options.rb index ee07b6f4..74ce1fdb 100644 --- a/lib/haml/options.rb +++ b/lib/haml/options.rb @@ -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 diff --git a/lib/haml/parser.rb b/lib/haml/parser.rb index d67950cb..788e4052 100644 --- a/lib/haml/parser.rb +++ b/lib/haml/parser.rb @@ -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] diff --git a/test/engine_test.rb b/test/engine_test.rb index 06c294cc..b977262f 100644 --- a/test/engine_test.rb +++ b/test/engine_test.rb @@ -1331,6 +1331,20 @@ HAML render("%p= 's' * 75", :ugly => true)) end + def test_remove_whitespace_true + assert_equal("
hello world
hello world
foo bar\nbaz", render(<
foo bar\n", render('%pre="foo\nbar"')) assert_equal("
foo\nbar\n", render("%pre\n foo\n bar"))