Reputation: 91
Is there any way to close html tags if a user forgets to? E.g. when the user input is:
<b>small</b><i>test
Is there a way in Rails to automatically add the closing </i>
tag, so that all the following html won't be italic?
I used .html_safe
to interpret everything as html, but I would like to terminate <i>
too.
Upvotes: 2
Views: 541
Reputation: 31077
Rails doesn't have any built in capability to do this however you have a couple of options:
gem install nokogiri
)Using nokogori you can simply do:
html = "<b>small</b><i>test"
clean = Nokogiri::HTML::DocumentFragment.parse(html).to_html
# clean = "<b>small</b><i>test</i>"
Upvotes: 4
Reputation: 3834
A decent way to do this would be to feed the input to a dom parser and then have that parser output correct HTML.
Note that this isn't fool proof and if the user makes too many mistakes, the parser won't know what to do.
I'd suggest Nokogiri
Upvotes: 0
Reputation: 46914
Rails can't do that to you.
You can use some template system like Haml or Slim to don't forget that.
You can do it by your own editor too.
Upvotes: 0