OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
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 | |
13 * distribution. | |
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. | |
17 * | |
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. | |
29 */ | |
30 | |
31 /** | |
32 * @constructor | |
33 * @extends {WebInspector.RequestView} | |
34 * @param {!WebInspector.NetworkRequest} request | |
35 * @param {!WebInspector.ParsedJSON} parsedJSON | |
36 */ | |
37 WebInspector.RequestJSONView = function(request, parsedJSON) | |
38 { | |
39 WebInspector.RequestView.call(this, request); | |
40 this._parsedJSON = parsedJSON; | |
41 this.element.classList.add("json"); | |
42 } | |
43 | |
44 // "false", "true", "null", ",", "{", "}", "[", "]", number, double-quoted strin
g. | |
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'); | |
46 | |
47 // Escaped unicode char. | |
48 WebInspector.RequestJSONView._escapedUnicode = new RegExp('\\\\(?:([^u])|u(.{4})
)', 'g'); | |
49 | |
50 // Map from escaped char to its literal value. | |
51 WebInspector.RequestJSONView._standardEscapes = {'"': '"', '/': '/', '\\': '\\',
'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t'}; | |
52 | |
53 /** | |
54 * @param {string} full | |
55 * @param {string} standard | |
56 * @param {string} unicode | |
57 * @return {string} | |
58 */ | |
59 WebInspector.RequestJSONView._unescape = function(full, standard, unicode) | |
60 { | |
61 return standard ? WebInspector.RequestJSONView._standardEscapes[standard] :
String.fromCharCode(parseInt(unicode, 16)); | |
62 } | |
63 | |
64 /** | |
65 * @param {string} text | |
66 * @return {string} | |
67 */ | |
68 WebInspector.RequestJSONView._unescapeString = function(text) | |
69 { | |
70 return text.indexOf("\\") === -1 ? text : text.replace(WebInspector.RequestJ
SONView._escapedUnicode, WebInspector.RequestJSONView._unescape); | |
71 } | |
72 | |
73 /** | |
74 * @return {*} | |
75 */ | |
76 WebInspector.RequestJSONView._buildObjectFromJSON = function(text) | |
77 { | |
78 var regExp = WebInspector.RequestJSONView._jsonToken; | |
79 regExp.lastIndex = 0; | |
80 var result = []; | |
81 var tip = result; | |
82 var stack = []; | |
83 var key = undefined; | |
84 var token = undefined; | |
85 var lastToken = undefined; | |
86 while (true) { | |
87 var match = regExp.exec(text); | |
88 if (match === null) | |
89 break; | |
90 lastToken = token; | |
91 token = match[0]; | |
92 var code = token.charCodeAt(0); | |
93 if ((code === 0x5b) || (code === 0x7b)) { // [ or { | |
94 var newTip = (code === 0x5b) ? [] : {}; | |
95 tip[key || tip.length] = newTip; | |
96 stack.push(tip); | |
97 tip = newTip; | |
98 } else if ((code === 0x5d) || (code === 0x7d)) { // ] or } | |
99 tip = stack.pop(); | |
100 if (!tip) | |
101 break; | |
102 } else if (code === 0x2C) { // , | |
103 if (Array.isArray(tip) && (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)); | |
107 if (!key) { | |
108 if (Array.isArray(tip)) { | |
109 key = tip.length; | |
110 } else { | |
111 key = token || ""; | |
112 continue; | |
113 } | |
114 } | |
115 tip[key] = token; | |
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 if (code === 0x2f || code === 0x2a || code === 0x26 || code === 0
x7c || code === 0x3b || code === 0x3d || code === 0x28 || code === 0x29) { // /*
&|;=() | |
123 // Looks like JavaScript | |
124 throw "Invalid JSON"; | |
125 } else { // sign or digit | |
126 tip[key || tip.length] = +(token); | |
127 } | |
128 key = undefined; | |
129 } | |
130 return (result.length > 1) ? result : result[0]; | |
131 } | |
132 | |
133 /** | |
134 * @param {string} text | |
135 * @return {?WebInspector.ParsedJSON} | |
136 */ | |
137 WebInspector.RequestJSONView.parseJSON = function(text) | |
138 { | |
139 // Do not treat HTML as JSON. | |
140 if (text.startsWith("<")) | |
141 return null; | |
142 var inner = WebInspector.RequestJSONView._findBrackets(text, "{", "}"); | |
143 var inner2 = WebInspector.RequestJSONView._findBrackets(text, "[", "]"); | |
144 inner = inner2.length > inner.length ? inner2 : inner; | |
145 | |
146 // Return on blank payloads or on payloads significantly smaller than origin
al text. | |
147 if (inner.length === -1 || text.length - inner.length > 80) | |
148 return null; | |
149 | |
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); | |
153 | |
154 // Only process valid JSONP. | |
155 if (suffix.trim().length && !(suffix.trim().startsWith(")") && prefix.trim()
.endsWith("("))) | |
156 return null; | |
157 | |
158 try { | |
159 return new WebInspector.ParsedJSON(WebInspector.RequestJSONView._buildOb
jectFromJSON(text), prefix, suffix); | |
160 } catch (e) { | |
161 return null; | |
162 } | |
163 } | |
164 | |
165 /** | |
166 * @param {string} text | |
167 * @param {string} open | |
168 * @param {string} close | |
169 * @return {{start: number, end: number, length: number}} | |
170 */ | |
171 WebInspector.RequestJSONView._findBrackets = function(text, open, close) | |
172 { | |
173 var start = text.indexOf(open); | |
174 var end = text.lastIndexOf(close); | |
175 var length = end - start - 1; | |
176 if (start == -1 || end == -1 || end < start) | |
177 length = -1; | |
178 return {start: start, end: end, length: length}; | |
179 } | |
180 | |
181 WebInspector.RequestJSONView.prototype = { | |
182 wasShown: function() | |
183 { | |
184 this._initialize(); | |
185 }, | |
186 | |
187 _initialize: function() | |
188 { | |
189 if (this._initialized) | |
190 return; | |
191 this._initialized = true; | |
192 | |
193 var obj = WebInspector.RemoteObject.fromLocalObject(this._parsedJSON.dat
a); | |
194 var title = this._parsedJSON.prefix + obj.description + this._parsedJSON
.suffix; | |
195 var section = new WebInspector.ObjectPropertiesSection(obj, title); | |
196 section.expand(); | |
197 section.editable = false; | |
198 this.element.appendChild(section.element); | |
199 }, | |
200 | |
201 __proto__: WebInspector.RequestView.prototype | |
202 } | |
203 | |
204 /** | |
205 * @constructor | |
206 */ | |
207 WebInspector.ParsedJSON = function(data, prefix, suffix) | |
208 { | |
209 this.data = data; | |
210 this.prefix = prefix; | |
211 this.suffix = suffix; | |
212 } | |
OLD | NEW |