treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Is there an alternative to replace < > with lt; and gt;

  • Im using prettyprint aka prettify to display code. Rather than having to convert all of my < and > entities i am using this function:
    String.prototype.escapeHTML = function() {
    return(
    this.replace(/</g,'&lt;').
    replace(/>/g,'&gt;').
    replace(/"/g,'&quot;')
    );
    };
    var conv = document.getElementById('conversion');
    if (conv) {
    convl.innerHTML = conv.innerHTML.escapeHTML();
    }

    This works quite well for preventing the html tags from rendering but when i use with prettify, it displays as a massive block of code without styles. Im sure there is something conflicting, but rather, are there any alternatives that are more popular for the fix im trying to attempt?

    jsfiddle: http://jsfiddle.net/john_motyl/2QFnB/ (swap the id to see what im talking about)
  • I'm not seeing the issue you are describing, but I would think if you escaped the HTML before running prettyPrint your problems should go away.

    Just to make sure you know that all syntax hilighting scripts add something like a span around bits of the code to colorize it, so it sounds like those elements are being converted by the escapeHTML script above and messing up the look.
  • @mottie, your absolutely correct. I was calling prettyprint() before trying to escape the html entities. Thanks for pointing that out, i totally overlooked that. Now i call that function inside my if statement and it works perfect. Thanks