views:

120

answers:

3

I'm getting html data from a database which has been sanitised.

Basically what I'm getting is something like this:

<div class="someclass"><blockquote>
  <p>something here.</p>
</blockquote>

And so on. So if I try to display it, it is displaying as

<div class="someclass"><blockquote> <p>something here</p> </blockquote>

What I want is to convert it to proper html before displaying, so that the content displays properly, without the tags.

What's the easiest way to do this using javascript?

Just want to note here that I'm working with in Adobe AIR. So I don't have any alternatives.

A: 

I'm not sure why you would want to do this with JavaScript, unless it's server-side JS... but in any case, you could just replalce &gt; and &lt; with their equivalents using the string's replace function.

However, this may lead to problems if you have used those two in some text, say you wrote an HTML tutorial or whatever. This is why in cases like this you may want to instead store the unsanitized HTML in your database, because converting it may be tricky to do correctly.

Jani Hartikainen
It is an adobe air application. So javascript is the only thing that is available. And I'm getting the escaped html from another server over which I don't have control over.
trex279
+2  A: 

This could help in a snap:

String.prototype.deentitize = function() {
    var ret = this.replace(/&gt;/g, '>');
    ret = ret.replace(/&lt;/g, '<');
    ret = ret.replace(/&quot;/g, '"');
    ret = ret.replace(/&apos;/g, "'");
    ret = ret.replace(/&amp;/g, '&');
    return ret;
};
steve_c
+7  A: 

You could create an element, assign the encoded HTML to its innerHTML and retrieve the nodeValue from the text node created on the insertion.

function htmlDecode(input){
  var e = document.createElement('div');
  e.innerHTML = input;
  return e.childNodes[0].nodeValue;
}

htmlDecode('&lt;div class="someclass"&gt;&lt;blockquote&gt; &lt;p&gt;&quot; ' +
           'something&quot;&nbsp;here.&lt;/p&gt;Q&lt;/blockquote&gt;')

// returns :
// "<div class="someclass"><blockquote> <p>"something" here.</p>Q</blockquote>"

Note that this method should work with all the HTML Character Entities.

CMS
Really like this method. With steve_c's method, you'll need to keep adding htmlentites to the replace function as you come across them. This handles all HTML straight out of the box.
pmckenna
Thank you. I was looking for something on these lines. A brilliant solution.
trex279
This is actually a much more robust solution than the function below. Plus working with the DOM is much nicer than messing around with strings imo.
steve_c
Thanks @trex279 and @steve_c, glad you both like it!
CMS