Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/network/JSONView.js

Issue 1468633002: DevTools: Prettify websocket frames preview on Network pane. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update tests. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 12 matching lines...) Expand all
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 /** 31 /**
32 * @constructor 32 * @constructor
33 * @extends {WebInspector.RequestView} 33 * @extends {WebInspector.VBox}
34 * @param {!WebInspector.NetworkRequest} request
35 * @param {!WebInspector.ParsedJSON} parsedJSON 34 * @param {!WebInspector.ParsedJSON} parsedJSON
36 */ 35 */
37 WebInspector.RequestJSONView = function(request, parsedJSON) 36 WebInspector.JSONView = function(parsedJSON)
38 { 37 {
39 WebInspector.RequestView.call(this, request); 38 WebInspector.VBox.call(this);
40 this._parsedJSON = parsedJSON; 39 this._parsedJSON = parsedJSON;
41 this.element.classList.add("json"); 40 this.element.classList.add("json-view");
42 } 41 }
43 42
44 // "false", "true", "null", ",", "{", "}", "[", "]", number, double-quoted strin g. 43 // "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'); 44 WebInspector.JSONView._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 45
47 // Escaped unicode char. 46 // Escaped unicode char.
48 WebInspector.RequestJSONView._escapedUnicode = new RegExp('\\\\(?:([^u])|u(.{4}) )', 'g'); 47 WebInspector.JSONView._escapedUnicode = new RegExp('\\\\(?:([^u])|u(.{4}))', 'g' );
49 48
50 // Map from escaped char to its literal value. 49 // Map from escaped char to its literal value.
51 WebInspector.RequestJSONView._standardEscapes = {'"': '"', '/': '/', '\\': '\\', 'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t'}; 50 WebInspector.JSONView._standardEscapes = {'"': '"', '/': '/', '\\': '\\', 'b': ' \b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t'};
52 51
53 /** 52 /**
54 * @param {string} full 53 * @param {string} full
55 * @param {string} standard 54 * @param {string} standard
56 * @param {string} unicode 55 * @param {string} unicode
57 * @return {string} 56 * @return {string}
58 */ 57 */
59 WebInspector.RequestJSONView._unescape = function(full, standard, unicode) 58 WebInspector.JSONView._unescape = function(full, standard, unicode)
60 { 59 {
61 return standard ? WebInspector.RequestJSONView._standardEscapes[standard] : String.fromCharCode(parseInt(unicode, 16)); 60 return standard ? WebInspector.JSONView._standardEscapes[standard] : String. fromCharCode(parseInt(unicode, 16));
62 } 61 }
63 62
64 /** 63 /**
65 * @param {string} text 64 * @param {string} text
66 * @return {string} 65 * @return {string}
67 */ 66 */
68 WebInspector.RequestJSONView._unescapeString = function(text) 67 WebInspector.JSONView._unescapeString = function(text)
69 { 68 {
70 return text.indexOf("\\") === -1 ? text : text.replace(WebInspector.RequestJ SONView._escapedUnicode, WebInspector.RequestJSONView._unescape); 69 return text.indexOf("\\") === -1 ? text : text.replace(WebInspector.JSONView ._escapedUnicode, WebInspector.JSONView._unescape);
71 } 70 }
72 71
73 /** 72 /**
74 * @return {*} 73 * @return {*}
75 */ 74 */
76 WebInspector.RequestJSONView._buildObjectFromJSON = function(text) 75 WebInspector.JSONView._buildObjectFromJSON = function(text)
77 { 76 {
78 var regExp = WebInspector.RequestJSONView._jsonToken; 77 var regExp = WebInspector.JSONView._jsonToken;
79 regExp.lastIndex = 0; 78 regExp.lastIndex = 0;
80 var result = []; 79 var result = [];
81 var tip = result; 80 var tip = result;
82 var stack = []; 81 var stack = [];
83 var key = undefined; 82 var key = undefined;
84 var token = undefined; 83 var token = undefined;
85 var lastToken = undefined; 84 var lastToken = undefined;
86 while (true) { 85 while (true) {
87 var match = regExp.exec(text); 86 var match = regExp.exec(text);
88 if (match === null) 87 if (match === null)
89 break; 88 break;
90 lastToken = token; 89 lastToken = token;
91 token = match[0]; 90 token = match[0];
92 var code = token.charCodeAt(0); 91 var code = token.charCodeAt(0);
93 if ((code === 0x5b) || (code === 0x7b)) { // [ or { 92 if ((code === 0x5b) || (code === 0x7b)) { // [ or {
94 var newTip = (code === 0x5b) ? [] : {}; 93 var newTip = (code === 0x5b) ? [] : {};
95 tip[key || tip.length] = newTip; 94 tip[key || tip.length] = newTip;
96 stack.push(tip); 95 stack.push(tip);
97 tip = newTip; 96 tip = newTip;
98 } else if ((code === 0x5d) || (code === 0x7d)) { // ] or } 97 } else if ((code === 0x5d) || (code === 0x7d)) { // ] or }
99 tip = stack.pop(); 98 tip = stack.pop();
100 if (!tip) 99 if (!tip)
101 break; 100 break;
102 } else if (code === 0x2C) { // , 101 } else if (code === 0x2C) { // ,
103 if (Array.isArray(tip) && (lastToken === undefined || lastToken === "[" || lastToken === ",")) 102 if (Array.isArray(tip) && (lastToken === undefined || lastToken === "[" || lastToken === ","))
104 tip[tip.length] = undefined; 103 tip[tip.length] = undefined;
105 } else if (code === 0x22) { // " 104 } else if (code === 0x22) { // "
106 token = WebInspector.RequestJSONView._unescapeString(token.substring (1, token.length - 1)); 105 token = WebInspector.JSONView._unescapeString(token.substring(1, tok en.length - 1));
107 if (!key) { 106 if (!key) {
108 if (Array.isArray(tip)) { 107 if (Array.isArray(tip)) {
109 key = tip.length; 108 key = tip.length;
110 } else { 109 } else {
111 key = token || ""; 110 key = token || "";
112 continue; 111 continue;
113 } 112 }
114 } 113 }
115 tip[key] = token; 114 tip[key] = token;
116 } else if (code === 0x66) { // f 115 } else if (code === 0x66) { // f
(...skipping 10 matching lines...) Expand all
127 } 126 }
128 key = undefined; 127 key = undefined;
129 } 128 }
130 return (result.length > 1) ? result : result[0]; 129 return (result.length > 1) ? result : result[0];
131 } 130 }
132 131
133 /** 132 /**
134 * @param {string} text 133 * @param {string} text
135 * @return {?WebInspector.ParsedJSON} 134 * @return {?WebInspector.ParsedJSON}
136 */ 135 */
137 WebInspector.RequestJSONView.parseJSON = function(text) 136 WebInspector.JSONView.parseJSON = function(text)
138 { 137 {
139 // Do not treat HTML as JSON. 138 // Do not treat HTML as JSON.
140 if (text.startsWith("<")) 139 if (text.startsWith("<"))
141 return null; 140 return null;
142 var inner = WebInspector.RequestJSONView._findBrackets(text, "{", "}"); 141 var inner = WebInspector.JSONView._findBrackets(text, "{", "}");
143 var inner2 = WebInspector.RequestJSONView._findBrackets(text, "[", "]"); 142 var inner2 = WebInspector.JSONView._findBrackets(text, "[", "]");
144 inner = inner2.length > inner.length ? inner2 : inner; 143 inner = inner2.length > inner.length ? inner2 : inner;
145 144
146 // Return on blank payloads or on payloads significantly smaller than origin al text. 145 // Return on blank payloads or on payloads significantly smaller than origin al text.
147 if (inner.length === -1 || text.length - inner.length > 80) 146 if (inner.length === -1 || text.length - inner.length > 80)
148 return null; 147 return null;
149 148
150 var prefix = text.substring(0, inner.start); 149 var prefix = text.substring(0, inner.start);
151 var suffix = text.substring(inner.end + 1); 150 var suffix = text.substring(inner.end + 1);
152 text = text.substring(inner.start, inner.end + 1); 151 text = text.substring(inner.start, inner.end + 1);
153 152
154 // Only process valid JSONP. 153 // Only process valid JSONP.
155 if (suffix.trim().length && !(suffix.trim().startsWith(")") && prefix.trim() .endsWith("("))) 154 if (suffix.trim().length && !(suffix.trim().startsWith(")") && prefix.trim() .endsWith("(")))
156 return null; 155 return null;
157 156
158 try { 157 try {
159 return new WebInspector.ParsedJSON(WebInspector.RequestJSONView._buildOb jectFromJSON(text), prefix, suffix); 158 return new WebInspector.ParsedJSON(WebInspector.JSONView._buildObjectFro mJSON(text), prefix, suffix);
160 } catch (e) { 159 } catch (e) {
161 return null; 160 return null;
162 } 161 }
163 } 162 }
164 163
165 /** 164 /**
166 * @param {string} text 165 * @param {string} text
167 * @param {string} open 166 * @param {string} open
168 * @param {string} close 167 * @param {string} close
169 * @return {{start: number, end: number, length: number}} 168 * @return {{start: number, end: number, length: number}}
170 */ 169 */
171 WebInspector.RequestJSONView._findBrackets = function(text, open, close) 170 WebInspector.JSONView._findBrackets = function(text, open, close)
172 { 171 {
173 var start = text.indexOf(open); 172 var start = text.indexOf(open);
174 var end = text.lastIndexOf(close); 173 var end = text.lastIndexOf(close);
175 var length = end - start - 1; 174 var length = end - start - 1;
176 if (start == -1 || end == -1 || end < start) 175 if (start == -1 || end == -1 || end < start)
177 length = -1; 176 length = -1;
178 return {start: start, end: end, length: length}; 177 return {start: start, end: end, length: length};
179 } 178 }
180 179
181 WebInspector.RequestJSONView.prototype = { 180 WebInspector.JSONView.prototype = {
182 wasShown: function() 181 wasShown: function()
183 { 182 {
184 this._initialize(); 183 this._initialize();
185 }, 184 },
186 185
187 _initialize: function() 186 _initialize: function()
188 { 187 {
189 if (this._initialized) 188 if (this._initialized)
190 return; 189 return;
191 this._initialized = true; 190 this._initialized = true;
192 191
193 var obj = WebInspector.RemoteObject.fromLocalObject(this._parsedJSON.dat a); 192 var obj = WebInspector.RemoteObject.fromLocalObject(this._parsedJSON.dat a);
194 var title = this._parsedJSON.prefix + obj.description + this._parsedJSON .suffix; 193 var title = this._parsedJSON.prefix + obj.description + this._parsedJSON .suffix;
195 var section = new WebInspector.ObjectPropertiesSection(obj, title); 194 var section = new WebInspector.ObjectPropertiesSection(obj, title);
196 section.expand(); 195 section.expand();
197 section.editable = false; 196 section.editable = false;
198 this.element.appendChild(section.element); 197 this.element.appendChild(section.element);
199 }, 198 },
200 199
201 __proto__: WebInspector.RequestView.prototype 200 __proto__: WebInspector.VBox.prototype
202 } 201 }
203 202
204 /** 203 /**
205 * @constructor 204 * @constructor
205 * @param {*} data
206 * @param {string} prefix
207 * @param {string} suffix
206 */ 208 */
207 WebInspector.ParsedJSON = function(data, prefix, suffix) 209 WebInspector.ParsedJSON = function(data, prefix, suffix)
208 { 210 {
209 this.data = data; 211 this.data = data;
210 this.prefix = prefix; 212 this.prefix = prefix;
211 this.suffix = suffix; 213 this.suffix = suffix;
212 } 214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698