Instantly share code, notes, and snippets.

Embed
What would you like to do?
if Rails::VERSION::MAJOR == 2
require "active_support/core_ext/string/output_safety"
class ERB
module Util
# see https://github.com/rails/rails/issues/7430
def html_escape(s)
s = s.to_s
if s.html_safe?
s
else
s.gsub(/[&"'><]/, HTML_ESCAPE).html_safe
end
end
alias h html_escape
singleton_class.send(:remove_method, :html_escape)
module_function :html_escape, :h
end
end
else
puts "You can delete this file+test now #{__FILE__}"
end
# encoding: utf-8
require File.expand_path("../../../helpers/test_helper", File.dirname(__FILE__))
class ErbUtilSilenceTest < ActiveSupport::TestCase
def record_stderr
$stderr = StringIO.new
yield
$stderr.string
ensure
$stderr = STDERR
end
context ERB::Util do
context "#html_escape" do
should "be silent" do
recorded = record_stderr do
assert_equal "åß∂åß∂&lt;&gt;", ERB::Util.html_escape("åß∂åß∂<>")
end
assert_equal "", recorded
end
end
end
end
@foobear

This comment has been minimized.

Copy link

foobear commented Feb 22, 2013

Thanks, this is awesome!

FYI: I've converted the test into a spec on my project: https://gist.github.com/foobear/0a49a76db93c5b7c9dda

@goodsimon

This comment has been minimized.

Copy link

goodsimon commented Sep 2, 2014

Ditto - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment