HTMLbeautifier for AJAX

a common JavaScript problem is, if you always work with runtime generated HTML (which is normal in AJAX) you have no real control over it. so handgestrickt made a layer that shows the documents HTML (document.body.innerHTML). for better overview and readability we wanted it indented and colourized like in a source code editor. this is why we wrote HTMLbeautifier. it needs Prototype.js' String.escapeHTML() function and uses our splitFix() function we posted earlier.

<html>
<head>
 <title>HTMLbeautifier example</title>
 <script type="text/javascript" src="prototype.js"></script>
 <style type="text/css">
#debug {
        position: fixed;
        bottom: 0px;
        right: 0px;
        margin: 0px;
        padding: 0px;
        display: block;
        width: 50%;
        height: 300px;
        overflow: auto;
        border: 1px solid red;
        background-color: white;
        z-index: 1000;
        font-size: 13px;
        line-height: 17px;
}

#debug .tag {
        color: blue;
}

#debug .attribute {
        color: red;
}

#debug .comment {
        color: #00aaaa;
}
 </style>
 <script type="text/javascript">
var foo = {
        showDebug: function() {
                $('debug').innerHTML = this.HTMLbeautify(document.body.innerHTML,'debug');
                $('debug').style.display = 'block';
        },

        HTMLbeautify: function(HTML,id) {
                var result = '';
                HTML = HTML.replace(/\r?\n/g,'');
                HTML = HTML.replace(/\s+</g,'<');
                HTML = HTML.replace(/>\s+/g,'>');
                eval("HTML = HTML.replace(/<pre[^>]+id=[\\"\']?"+id+"[\\"\']?.*?<\/pre>/i,'');");
                var pieces = this.splitFix(HTML,/(<[^>]+>)/);
                var indent = 0;
                for(var a=0;a<pieces.length;a++) {
                        if(pieces[a].search(/<\/\w/) != -1) indent--;
                        if(pieces[a] != '') {
                                var piece = pieces[a].escapeHTML();
                                if(piece.search(/&lt;\w/) != -1) {
                                        piece = piece.replace(/(\s)(\w+=)([\"\'\w])/g,'<span class="attribute"></span>');
                                        piece = piece.replace(/(&lt;\w+)/g,'<span class="tag"></span>');
                                        piece = piece.replace(/(\/?&gt;)/g,'<span class="tag"></span>');
                                }
                                else if(piece.search(/&lt;\/\w/) != -1) piece = '<span class="tag">'+piece+'</span>';
                                else if(piece.search(/&lt;!--.*--&gt;/) != -1) piece = '<span class="comment">'+piece+'</span>';
                                result += ((indent > 0) ? this.str_repeat('&nbsp;',indent) : '')+piece+"<br />";
                        }
                        if(
                                pieces[a].search(/<[a-zA-Z]/) != -1 &&
                                pieces[a].search(/<(?:and|area|atop|audioscope|base|basefont|bgsound|br|choose|col|embed|frame|hr|img|input|isindex|keygen|left|limittext|link|meta|nextid|of|over|param|range|right|spacer|spot|tab|wbr)\W/i) == -1
                        ) indent++;
                }
                return result;
        },

        splitFix: function(string,regexp) {
                var matches = [];
                if(!document.all || window.opera) matches = string.split(regexp);
                else {
                        var delim = '<#%fuckIE!>'; //lol
                        var prnth = String(regexp).replace(/\(\?/g,'');
                        prnth = prnth.replace(/[^\(]+/g,'').length;
                        var repl = delim;
                        for(var a=0;a<prnth;a++) repl += '$<span>'</span>+(a+1)+delim;
                        eval("string = string.replace("+regexp+"g,repl);");
                        matches = string.split(delim);
                }
                return matches;
        },

        str_repeat: function(string,length) {
                var result = '';
                for(var a=0;a<length;a++) result += string;
                return result;
        }
}
 </script>
</head>
<body onload="foo.showDebug();">

<!--...Your HTML here...-->

<pre id="debug"><!--show debug information here...--></pre>
</body>
</html>

Saturday, 17. March 2007 • trackback url

Add Comment

( to reply to a comment, click the reply link next to the comment )

Comment Title:
Your Name:
Email Address:
Make Public?
Website:
Make Public?

Comment:


Allowed XHTML tags : a, b, i, strong, code, acrynom, blockquote, abbr. Linebreaks will be converted automatically.


Captcha:

captcha image

please type the content of the above image into the following form-field: