From f973ccef1b1419840db4aab8cdd062d0992471a4 Mon Sep 17 00:00:00 2001 From: Didier Lafforgue Date: Sat, 14 Apr 2012 17:35:11 +0200 Subject: [PATCH] preserve whitespace in textarea tags (HAML/Rails bug) --- config/initializers/haml.rb | 3 ++- lib/locomotive.rb | 1 + lib/locomotive/haml.rb | 34 ++++++++++++++++++++++++++ spec/dummy/config/initializers/haml.rb | 3 ++- 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 lib/locomotive/haml.rb diff --git a/config/initializers/haml.rb b/config/initializers/haml.rb index 31febf8b..c1a2c3ee 100644 --- a/config/initializers/haml.rb +++ b/config/initializers/haml.rb @@ -1 +1,2 @@ -# Haml::Template.options[:ugly] = true # improve performance in dev \ No newline at end of file +Haml::Template.options[:format] = :html5 +Haml::Template.options[:ugly] = true # improve performance in dev \ No newline at end of file diff --git a/lib/locomotive.rb b/lib/locomotive.rb index 02a6c73d..e15c1310 100644 --- a/lib/locomotive.rb +++ b/lib/locomotive.rb @@ -2,6 +2,7 @@ require 'locomotive/version' require 'locomotive/core_ext' require 'locomotive/configuration' require 'locomotive/logger' +require 'locomotive/haml' require 'locomotive/formtastic' require 'locomotive/dragonfly' require 'locomotive/kaminari' diff --git a/lib/locomotive/haml.rb b/lib/locomotive/haml.rb new file mode 100644 index 00000000..8071853e --- /dev/null +++ b/lib/locomotive/haml.rb @@ -0,0 +1,34 @@ +require 'haml/helpers/action_view_mods' + +module ActionView + module Helpers + module TagHelper + + # Only preserve whitespace in the tag's content: https://github.com/nex3/haml/pull/503 + def content_tag_with_haml_and_preserve(name, content_or_options_with_block = nil, *args, &block) + Rails.logger.debug("[content_tag_with_haml_and_preserve / ENGINE] #{name} / #{respond_to?(:content_tag_with_haml)} / #{respond_to?(:content_tag_without_haml)}") + + return content_tag_without_haml(name, content_or_options_with_block, *args, &block) unless is_haml? + + preserve = haml_buffer.options[:preserve].include?(name.to_s) + + if block_given? + if block_is_haml?(block) && preserve + content_tag_without_haml(name, content_or_options_with_block, *args) {preserve(&block)} + else + content_tag_without_haml(name, content_or_options_with_block, *args, &block) + end + else + if preserve && content_or_options_with_block + content_or_options_with_block = Haml::Helpers.preserve(content_or_options_with_block) + end + content_tag_without_haml(name, content_or_options_with_block, *args, &block) + end + end + + alias_method :content_tag_without_haml_and_preserve, :content_tag + alias_method :content_tag, :content_tag_with_haml_and_preserve + alias_method :content_tag_with_haml, :content_tag_with_haml_and_preserve + end + end +end \ No newline at end of file diff --git a/spec/dummy/config/initializers/haml.rb b/spec/dummy/config/initializers/haml.rb index df0b4de4..c1a2c3ee 100644 --- a/spec/dummy/config/initializers/haml.rb +++ b/spec/dummy/config/initializers/haml.rb @@ -1 +1,2 @@ -Haml::Template.options[:ugly] = true # improve performance in dev \ No newline at end of file +Haml::Template.options[:format] = :html5 +Haml::Template.options[:ugly] = true # improve performance in dev \ No newline at end of file