2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @extends {WebInspector.RequestView}
34 * @param {!WebInspector.NetworkRequest} request
35 * @param {!WebInspector.ParsedJSON} parsedJSON
37 WebInspector.RequestJSONView = function(request, parsedJSON)
39 WebInspector.RequestView.call(this, request);
40 this._parsedJSON = parsedJSON;
41 this.element.classList.add("json");
44 // "false", "true", "null", ",", "{", "}", "[", "]", number, double-quoted string.
45 WebInspector.RequestJSONView._jsonToken = new RegExp('(?:false|true|null|[,\\{\\}\\[\\]]|(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)|(?:\"(?:[^\\0-\\x08\\x0a-\\x1f\"\\\\]|\\\\(?:[\"/\\\\bfnrt]|u[0-9A-Fa-f]{4}))*\"))', 'g');
47 // Escaped unicode char.
48 WebInspector.RequestJSONView._escapedUnicode = new RegExp('\\\\(?:([^u])|u(.{4}))', 'g');
50 // Map from escaped char to its literal value.
51 WebInspector.RequestJSONView._standardEscapes = {'"': '"', '/': '/', '\\': '\\', 'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t'};
54 * @param {string} full
55 * @param {string} standard
56 * @param {string} unicode
59 WebInspector.RequestJSONView._unescape = function(full, standard, unicode)
61 return standard ? WebInspector.RequestJSONView._standardEscapes[standard] : String.fromCharCode(parseInt(unicode, 16));
65 * @param {string} text
68 WebInspector.RequestJSONView._unescapeString = function(text)
70 return text.indexOf("\\") === -1 ? text : text.replace(WebInspector.RequestJSONView._escapedUnicode, WebInspector.RequestJSONView._unescape);
76 WebInspector.RequestJSONView._buildObjectFromJSON = function(text)
78 var regExp = WebInspector.RequestJSONView._jsonToken;
84 var token = undefined;
85 var lastToken = undefined;
87 var match = regExp.exec(text);
92 var code = token.charCodeAt(0);
93 if ((code === 0x5b) || (code === 0x7b)) { // [ or {
94 var newTip = (code === 0x5b) ? [] : {};
95 tip[key || tip.length] = newTip;
98 } else if ((code === 0x5d) || (code === 0x7d)) { // ] or }
102 } else if (code === 0x2C) { // ,
103 if ((tip instanceof Array) && (lastToken === undefined || lastToken === "[" || lastToken === ","))
104 tip[tip.length] = undefined;
105 } else if (code === 0x22) { // "
106 token = WebInspector.RequestJSONView._unescapeString(token.substring(1, token.length - 1));
108 if (tip instanceof Array) {
116 } else if (code === 0x66) { // f
117 tip[key || tip.length] = false;
118 } else if (code === 0x6e) { // n
119 tip[key || tip.length] = null;
120 } else if (code === 0x74) { // t
121 tip[key || tip.length] = true;
122 } else { // sign or digit
123 tip[key || tip.length] = +(token);
127 return (result.length > 1) ? result : result[0];
131 * @param {string} text
132 * @return {?WebInspector.ParsedJSON}
134 WebInspector.RequestJSONView.parseJSON = function(text)
136 // Trim stubs like "while(1)", "for(;;)", weird numbers, etc. We need JSON start.
137 var inner = WebInspector.RequestJSONView._findBrackets(text, "{", "}");
138 var inner2 = WebInspector.RequestJSONView._findBrackets(text, "[", "]");
139 inner = inner2.length > inner.length ? inner2 : inner;
140 var inner3 = WebInspector.RequestJSONView._findBrackets(text, "(", ")");
141 if (inner3.length - 2 > inner.length) {
146 if (inner.length === -1)
150 var prefix = text.substring(0, inner.start);
151 var suffix = text.substring(inner.end + 1);
152 text = text.substring(inner.start, inner.end + 1);
155 return new WebInspector.ParsedJSON(WebInspector.RequestJSONView._buildObjectFromJSON(text), prefix, suffix);
162 * @param {string} text
163 * @param {string} open
164 * @param {string} close
165 * @return {{start: number, end: number, length: number}}
167 WebInspector.RequestJSONView._findBrackets = function(text, open, close)
169 var start = text.indexOf(open);
170 var end = text.lastIndexOf(close);
171 var length = end - start - 1;
172 if (start == -1 || end == -1 || end < start)
174 return {start: start, end: end, length: length};
177 WebInspector.RequestJSONView.prototype = {
183 _initialize: function()
185 if (this._initialized)
187 this._initialized = true;
189 var obj = WebInspector.RemoteObject.fromLocalObject(this._parsedJSON.data);
190 var title = this._parsedJSON.prefix + obj.description + this._parsedJSON.suffix;
191 var section = new WebInspector.ObjectPropertiesSection(obj, title);
193 section.editable = false;
194 this.element.appendChild(section.element);
197 __proto__: WebInspector.RequestView.prototype
203 WebInspector.ParsedJSON = function(data, prefix, suffix)
206 this.prefix = prefix;
207 this.suffix = suffix;