Grow your CSS skills. Land your dream job.

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

  • # August 14, 2012 at 5:53 am

    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(/ replace(/>/g,'>').
    replace(/"/g,'"')
    );
    };
    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)

    # August 14, 2012 at 11:59 am

    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.

    # August 14, 2012 at 8:52 pm

    @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

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".