# Best way to close unclosed tags in user input?

**URL:** <https://www.ruby-forum.com/t/best-way-to-close-unclosed-tags-in-user-input/120449>\
**Category:** Rails\
**Created:** [November 17, 2007, 12:20am UTC](https://www.ruby-forum.com/t/best-way-to-close-unclosed-tags-in-user-input/120449 "2007-11-17T00:20:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![skaufman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@skaufman](https://www.ruby-forum.com/u/skaufman)\
**Post date:** [November 17, 2007, 12:20am UTC](https://www.ruby-forum.com/t/best-way-to-close-unclosed-tags-in-user-input/120449/1 "2007-11-17T00:20:25Z")

</div>

This seems like such a common situation that I can’t believe I haven’t  
found an easy recipe. What is the best way to close an unclosed tag in  
user-submitted input? It doesn’t appear that HPricot has a built-in  
method for this, though HPricot looks like the most likely tool to  
use. White\_list and RedCloth also don’t appear to solve this. What  
else should I investigate? TIA!

---

<div class="post-metadata">

**Author:** ![skaufman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@skaufman](https://www.ruby-forum.com/u/skaufman)\
**Post date:** [November 17, 2007, 1:33am UTC](https://www.ruby-forum.com/t/best-way-to-close-unclosed-tags-in-user-input/120449/2 "2007-11-17T01:33:29Z")

</div>

On Nov 16, 2007 5:19 PM, Stan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This seems like such a common situation that I can’t believe I haven’t  
> found an easy recipe. What is the best way to close an unclosed tag in  
> user-submitted input? It doesn’t appear that HPricot has a built-in  
> method for this, though HPricot looks like the most likely tool to  
> use. White\_list and RedCloth also don’t appear to solve this. What  
> else should I investigate? TIA!

You can run the html through Tidy. Using Mac ports, it’d be `port install tidy`.

Tidy also fits nicely into functional tests for your own html. I’ve  
used it in continuous builder setups where we needed the whole team,  
some of them not very html savvy but still good programmers otherwise,  
to contribute valid html.

One problem I encountered however was how to validate partials. To  
solve that I would sample the first tag from a partial and then wrap  
it with whatever was required to become (minimally) valid HTML. With  
a little recursion you can go upward from a partial to a valid?  
html document, then run that against tidy.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![skaufman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e7dd6/32.png) [@skaufman](https://www.ruby-forum.com/u/skaufman)\
**Post date:** [November 17, 2007, 4:09am UTC](https://www.ruby-forum.com/t/best-way-to-close-unclosed-tags-in-user-input/120449/3 "2007-11-17T04:09:21Z")

</div>

On Nov 16, 3:19 pm, Stan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It doesn’t appear that HPricot has a built-in  
> method for this

I should have looked a bit longer. HPricot makes this trivial:

> > Hpricot(“A **closed** tag and an unclosed tag”).to\_html  
> > =\> “A **closed** tag and an unclosed tag”
