Html

class
 new Html() 

Option name Type Description

Helper class with HTML manipulation features

class Html {

htmlEncode

method
 Html.htmlEncode() 

Option name Type Description
string String

the string to encode

return String

the encoded string

HTML encode a string for security purposes. Should prevent XSS attacks

static htmlEncode(string) {
    return $('<div/>').text(string).html();
}

jsonSyntaxHighlight

method
 Html.jsonSyntaxHighlight() 

Option name Type Description
json String, JSON

the json string or element to highlight

return String

and html string of the json with enabled css highlighting functionality.

Json highlighter

static jsonSyntaxHighlight(json) {
    if (typeof json != 'string') {
         json = JSON.stringify(json, undefined, 2);
    }
    json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
        let cls = 'number';
        if (/^"/.test(match)) {
            if (/:$/.test(match)) {
                cls = 'key';
            } else {
                cls = 'string';
            }
        } else if (/true|false/.test(match)) {
            cls = 'boolean';
        } else if (/null/.test(match)) {
            cls = 'null';
        }
        return '<span class="' + cls + '">' + match + '</span>';
    });
}
}

export default Html;