module Haml::RailsHelpers

def find_and_preserve(input = nil, tags = DEFAULT_PRESERVE_TAGS, &block)

def find_and_preserve(input = nil, tags = DEFAULT_PRESERVE_TAGS, &block)
  return find_and_preserve(capture_haml(&block), input || tags) if block
  tags = tags.each_with_object('') do |t, s|
    s << '|' unless s.empty?
    s << Regexp.escape(t)
  end
  re = /<(#{tags})([^>]*)>(.*?)(<\/\1>)/im
  input.to_s.gsub(re) do |s|
    s =~ re # Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible
    "<#{$1}#{$2}>#{preserve($3)}</#{$1}>"
  end
end