/* https://github.com/bhollis/jsonview/blob/master/src/components/jsonview.js MIT License Copyright (c) 2009 Benjamin Hollis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* * The JSONFormatter helper object. This contains two major functions, jsonToHTML and errorPage, * each of which returns an HTML document. */ function JSONFormatter(opt) { this.options = opt; } JSONFormatter.prototype = { htmlEncode: function (t) { return t != null ? t.toString().replace(/&/g,"&").replace(/"/g,""").replace(//g,">") : ''; }, // Completely escape strings, taking care to return common escape codes to their short forms jsString: function(s) { // the JSON serializer escapes everything to the long-form \uXXXX // escape code. This is a map of characters to return to the short-escaped // form after escaping. var has = { '\b': 'b', '\f': 'f', '\r': 'r', '\n': 'n', '\t': 't' }, ws; for (ws in has) { if (-1 === s.indexOf(ws)) { delete has[ws]; } } // The old nsIJSON can't encode just a string... s = JSON.stringify({a:s}); s = s.slice(6, -2); for (ws in has) { s = s.replace(new RegExp('\\\\u000' + (ws.charCodeAt().toString(16)), 'ig'), '\\' + has[ws]); } return this.htmlEncode(s); }, decorateWithSpan: function (value, className) { return '' + this.htmlEncode(value) + ''; }, // Convert a basic JSON datatype (number, string, boolean, null, object, array) into an HTML fragment. valueToHTML: function(value, path) { var valueType = typeof value; var output = ""; if (value == null) { output += this.decorateWithSpan('null', 'null'); } else if (value && value.constructor == Array) { output += this.arrayToHTML(value, path); } else if (valueType == 'object') { output += this.objectToHTML(value, path); } else if (valueType == 'number') { output += this.decorateWithSpan(value, 'num'); } else if (valueType == 'string') { if (/^(http|https):\/\/[^\s]+$/i.test(value)) { var prefix = ''; var dispValue = value; if ( this.options.baseUrl && value.indexOf(this.options.baseUrl) == 0) { prefix = '…'; dispValue = value.substr(this.options.baseUrl.length); } output += '"' + prefix + this.jsString(dispValue) + '"'; } else { output += '"' + this.jsString(value) + '"'; } } else if (valueType == 'boolean') { output += this.decorateWithSpan(value, 'bool'); } return output; }, // Convert an array into an HTML fragment arrayToHTML: function(json, in_path) { var hasContents = false; var output = ''; var cur = 0, total = 0; for (var prop in json ) { total++; } var path; for ( var prop in json ) { if ( ! json.hasOwnProperty(prop) ) continue; hasContents = true; path = (in_path||[]).slice(0); path.push(prop); output += '