rice-framework/krad-web/src/main/webapp/krad/scripts/krad.utility.js
/**
 * Formats an unformated html string by adding appropriate line breaks and spaces for indentation
 *
 * <p> Modified solution based on: http://stackoverflow.com/questions/376373/pretty-printing-xml-with-javascript </p>
 *
 * @param html the html string to format
 * @returns {string} a formatted html string which can be use in pre tags
 */
Formats an unformated html string by adding appropriate line breaks and spaces for indentation



@param html the html string to format
@returns {string} a formatted html string which can be use in pre tags
Formats an unformated html string by adding appropriate line breaks and spaces for indentation @param html the html string to format @returns { string } a formatted html string which can be use in pre tags
function formatHtml(html) {
    var reg = /(>)(<)(\/*)/g;
    var wsexp = / *(.*) +\n/g;
    var contexp = /(<.+>)(.+\n)/g;
    html = html.replace(reg, '$1\n$2$3').replace(wsexp, '$1\n').replace(contexp, '$1\n$2');
    var formatted = '';
    var lines = html.split('\n');
    var indent = 0;
    var lastType = 'other';
    var transitions = {
        'single->single': 0,
        'single->closing': -1,
        'single->opening': 0,
        'single->other': 0,
        'closing->single': 0,
        'closing->closing': -1,
        'closing->opening': 0,
        'closing->other': 0,
        'opening->single': 1,
        'opening->closing': 0,
        'opening->opening': 1,
        'opening->other': 1,
        'other->single': 0,
        'other->closing': -1,
        'other->opening': 0,
        'other->other': 0
    };
    for (var i = 0; i < lines.length; i++) {
        var ln = lines[i];
        var single = Boolean(ln.match(/<.+\/>/)) || ln.indexOf("<input") != -1;
        var closing = Boolean(ln.match(/<\/.+>/));
        var opening = Boolean(ln.match(/<[^!].*>/));
        var type = single ? 'single' : closing ? 'closing' : opening ? 'opening' : 'other';
        var fromTo = lastType + '->' + type;
        lastType = type;
        var padding = '';
        indent += transitions[fromTo];
        for (var j = 0; j < indent; j++) {
            padding += '   ';
        }
        if (fromTo == 'opening->closing')
            formatted = formatted.substr(0, formatted.length - 1) + ln + '\n';
        else
            formatted += padding + ln + '\n';
    }
    return formatted;
}
function formatHtml ( html ) { var reg = / (>)(<)(\/*) / g ; var wsexp = /  *(.*) +\n / g ; var contexp = / (<.+>)(.+\n) / g ; html = html . replace ( reg , '$1\n$2$3' ) . replace ( wsexp , '$1\n' ) . replace ( contexp , '$1\n$2' ) ; var formatted = '' ; var lines = html . split ( '\n' ) ; var indent = 0 ; var lastType = 'other' ; var transitions = { 'single->single' : 0 , 'single->closing' : - 1 , 'single->opening' : 0 , 'single->other' : 0 , 'closing->single' : 0 , 'closing->closing' : - 1 , 'closing->opening' : 0 , 'closing->other' : 0 , 'opening->single' : 1 , 'opening->closing' : 0 , 'opening->opening' : 1 , 'opening->other' : 1 , 'other->single' : 0 , 'other->closing' : - 1 , 'other->opening' : 0 , 'other->other' : 0 } ; for ( var i = 0 ; i < lines . length ; i ++ ) { var ln = lines [ i ] ; var single = Boolean ( ln . match ( / <.+\/> / ) ) || ln . indexOf ( "<input" ) != - 1 ; var closing = Boolean ( ln . match ( / <\/.+> / ) ) ; var opening = Boolean ( ln . match ( / <[^!].*> / ) ) ; var type = single ? 'single' : closing ? 'closing' : opening ? 'opening' : 'other' ; var fromTo = lastType + '->' + type ; lastType = type ; var padding = '' ; indent += transitions [ fromTo ] ; for ( var j = 0 ; j < indent ; j ++ ) { padding += '   ' ; } if ( fromTo == 'opening->closing' ) formatted = formatted . substr ( 0 , formatted . length - 1 ) + ln + '\n' ; else formatted += padding + ln + '\n' ; } return formatted ; }
Formats an unformated html string by adding appropriate line breaks and spaces for indentation
Formats an unformated html string by adding appropriate line breaks and spaces for indentation
// 4 types of tags - single, closing, opening, other (text, doctype, comment) - 4*4 = 16 transitions
 // is this line a single tag? ex. <br />
 // is this a closing tag? ex. </a>
 // is this even a tag (that's not <!something>)
 // substr removes line break (\n) from prev loop
