Sanitize HTML and close incomplete tags

You can use a proper HTML parser to do this. I'd recommend Nokogiri for the job:

require 'nokogiri'
# ...
s = "<a href='http://example.com'>incomplete"
Nokogiri::HTML::fragment(sanitize(s, :tags => ['a', 'p'])).to_xml
# => "<a href=\"http://example.com\">incomplete</a>"

This will always return valid XML. Of course you can package that into your own helper method for easier usage.


The updated answer is

 s = "<a href='http://example.com'>incomplete"
 html = sanitize(s, tags: %w[a p])
 Nokogiri::HTML::DocumentFragment.parse(html).to_html