Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sanitize HTML and close incomplete tags

sanitize() in ApplicationHelper doesn't close tags.

s = "<a href='http://example.com'>incomplete"
sanitize(s, :tags => ['a', 'p'])

The above snippet leaves the string as is. How could I force it to append a closing </a> or at least strip the <a> altogether?

like image 640
mahemoff Avatar asked Mar 29 '12 20:03

mahemoff


2 Answers

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.

like image 148
Niklas B. Avatar answered Sep 21 '22 16:09

Niklas B.


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
like image 41
jvnill Avatar answered Sep 23 '22 16:09

jvnill