tags:

views:

81

answers:

3

Let's say I have some HTML like this:

<ol><li>a knock at the door, I'll be back in a second

which I display as inline HTML within a div that I produce. Is there any way to stop the inner, malformed HTML from screwing up the alignment of the entire document that comes after the HTML?

I realize that intelligently parsing it is one option (since I do have it on the server side), but I'm looking for a lighter solution.

Edit I know I could just replace the < and > with entities, but I want to keep the formatting (within reason).

+1  A: 

Replace < and > with ascii/unicode equivalents &#60; and &#62; OR &lt; and &gt; OR \u003c and \u003e

Midhat
Sorry, I can see the question wasn't clear. I could do that, but I want to keep the formatting if possible. Editing the question.
Yar
+1  A: 

Do you need the inline text to support HTML? If not, you could just strip all HTML tags, or replace <> with escapes. Or you could store the text on the server as Markdown or any other non-HTML language and generate good HTML on the fly.

Edit: If you really need HTML, I suggest that you run an XML validator on the HTML snippet. You don't actually need to look at the structure of the XML: if the snippet is well-formed then it's very unlikely that it will break the rest of the layout.

JSBangs
Yes, it has to support HTML, sorry I didn't mention that.
Yar
+4  A: 

Despite you looking for a 'lighter solution' than HTML parsing, you might want to consider using HTML Purifier or HTML Tidy, since that should take care of broken tags for you - that's essentially what they were made for.

pinkgothic
Could be. I was kind of hoping for a cheesy HTML solution, but I'll see which of these is easiest in Ruby, too. Thanks!
Yar
I imagine **HTML Tidy** would be easier to call from Ruby, but I have no Ruby experience at all. Whatever you do, though, good luck! :)
pinkgothic
Thanks @pinkgothic, since this problem was a one-off, I decided to just stip the `<` and `>`, but I was curious if there's something about HTMl that I don't know. Thanks.
Yar