Skip to content

Instantly share code, notes, and snippets.

@Fjan
Created November 17, 2010 09:04
Show Gist options
  • Save Fjan/703175 to your computer and use it in GitHub Desktop.
Save Fjan/703175 to your computer and use it in GitHub Desktop.
Add this to your test_helper.rb to detect HTML escaping issues
# add gem 'tidy' to your Gem file
# drop this snippet in class ActiveSupport::TestCase in test_helper.rb
# then just put "assert_valid_markup" in various places in your tests
# HTML tidy needs to be installed on your system (Snow Leopard has it installed by default)
def assert_valid_markup(markup=@response.body)
errors = []
Tidy.path='/usr/lib/libtidy.dylib' # This is were it lives by default on OS X
Tidy.open(:input_xml => true) do |tidy|
tidy.options.char_encoding = 'utf8'
tidy.clean(markup)
errors.concat(tidy.errors)
end
errors << "Double escaped HTML in text (&#{$1};)" if markup =~ /&amp;(raquo|lt|gt|nbsp|amp|ldquo|rdquo)/
errors << "Escaped HTML in text <#{$1}>" if markup =~ /&lt;([a-z]+)/
errors << "Unescaped ampersand in text" if markup =~ /& /
unless errors.empty?
error_str = ''
errors.each do |e|
error_str += e.gsub(/\n/, "\n ")
end
error_str = "XHTML Validation Failed:\n #{error_str}"
assert_block(error_str) { false }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment