incomplete" sanitize...">

HTML sanitize and close partial tags

sanitize() in ApplicationHelper does not close tags.

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

The above snippet leaves the string as is. How can I make it add a closure </a> or at least delete <a> altogether?

+6
source share
2 answers

Updated Answer

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

You can use the correct HTML parser for this. I would recommend Nokogiri for work:

 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 always returns valid XML. Of course, you can pack them into your own helper method for ease of use.

+5
source

Source: https://habr.com/ru/post/911979/


All Articles