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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/common/ParsedURL.js

Issue 2466123002: DevTools: reformat front-end code to match chromium style. (Closed)
Patch Set: all done Created 4 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) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 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 * 1. Redistributions of source code must retain the above copyright 8 * 1. 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 * 10 *
11 * 2. Redistributions in binary form must reproduce the above 11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer 12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the 13 * in the documentation and/or other materials provided with the
14 * distribution. 14 * distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS 16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28
29 /** 28 /**
30 * @constructor 29 * @unrestricted
31 * @param {string} url
32 */ 30 */
33 WebInspector.ParsedURL = function(url) 31 WebInspector.ParsedURL = class {
34 { 32 /**
33 * @param {string} url
34 */
35 constructor(url) {
35 this.isValid = false; 36 this.isValid = false;
36 this.url = url; 37 this.url = url;
37 this.scheme = ""; 38 this.scheme = '';
38 this.host = ""; 39 this.host = '';
39 this.port = ""; 40 this.port = '';
40 this.path = ""; 41 this.path = '';
41 this.queryParams = ""; 42 this.queryParams = '';
42 this.fragment = ""; 43 this.fragment = '';
43 this.folderPathComponents = ""; 44 this.folderPathComponents = '';
44 this.lastPathComponent = ""; 45 this.lastPathComponent = '';
45 46
46 var match = url.match(WebInspector.ParsedURL._urlRegex()); 47 var match = url.match(WebInspector.ParsedURL._urlRegex());
47 if (match) { 48 if (match) {
48 this.isValid = true; 49 this.isValid = true;
49 this.scheme = match[1].toLowerCase(); 50 this.scheme = match[1].toLowerCase();
50 this.host = match[2]; 51 this.host = match[2];
51 this.port = match[3]; 52 this.port = match[3];
52 this.path = match[4] || "/"; 53 this.path = match[4] || '/';
53 this.queryParams = match[5] || ""; 54 this.queryParams = match[5] || '';
54 this.fragment = match[6]; 55 this.fragment = match[6];
55 } else { 56 } else {
56 if (this.url.startsWith("data:")) { 57 if (this.url.startsWith('data:')) {
57 this.scheme = "data"; 58 this.scheme = 'data';
58 return; 59 return;
59 } 60 }
60 if (this.url === "about:blank") { 61 if (this.url === 'about:blank') {
61 this.scheme = "about"; 62 this.scheme = 'about';
62 return; 63 return;
63 } 64 }
64 this.path = this.url; 65 this.path = this.url;
65 } 66 }
66 67
67 var lastSlashIndex = this.path.lastIndexOf("/"); 68 var lastSlashIndex = this.path.lastIndexOf('/');
68 if (lastSlashIndex !== -1) { 69 if (lastSlashIndex !== -1) {
69 this.folderPathComponents = this.path.substring(0, lastSlashIndex); 70 this.folderPathComponents = this.path.substring(0, lastSlashIndex);
70 this.lastPathComponent = this.path.substring(lastSlashIndex + 1); 71 this.lastPathComponent = this.path.substring(lastSlashIndex + 1);
71 } else { 72 } else {
72 this.lastPathComponent = this.path; 73 this.lastPathComponent = this.path;
73 } 74 }
74 }; 75 }
75 76
76 /** 77 /**
77 * @param {string} fileSystemPath 78 * @param {string} fileSystemPath
78 * @return {string} 79 * @return {string}
79 */ 80 */
80 WebInspector.ParsedURL.platformPathToURL = function(fileSystemPath) 81 static platformPathToURL(fileSystemPath) {
81 { 82 fileSystemPath = fileSystemPath.replace(/\\/g, '/');
82 fileSystemPath = fileSystemPath.replace(/\\/g, "/"); 83 if (!fileSystemPath.startsWith('file://')) {
83 if (!fileSystemPath.startsWith("file://")) { 84 if (fileSystemPath.startsWith('/'))
84 if (fileSystemPath.startsWith("/")) 85 fileSystemPath = 'file://' + fileSystemPath;
85 fileSystemPath = "file://" + fileSystemPath; 86 else
86 else 87 fileSystemPath = 'file:///' + fileSystemPath;
87 fileSystemPath = "file:///" + fileSystemPath;
88 } 88 }
89 return fileSystemPath; 89 return fileSystemPath;
90 }; 90 }
91 91
92 /** 92 /**
93 * @return {!RegExp} 93 * @return {!RegExp}
94 */ 94 */
95 WebInspector.ParsedURL._urlRegex = function() 95 static _urlRegex() {
96 {
97 if (WebInspector.ParsedURL._urlRegexInstance) 96 if (WebInspector.ParsedURL._urlRegexInstance)
98 return WebInspector.ParsedURL._urlRegexInstance; 97 return WebInspector.ParsedURL._urlRegexInstance;
99 // RegExp groups: 98 // RegExp groups:
100 // 1 - scheme (using the RFC3986 grammar) 99 // 1 - scheme (using the RFC3986 grammar)
101 // 2 - hostname 100 // 2 - hostname
102 // 3 - ?port 101 // 3 - ?port
103 // 4 - ?path 102 // 4 - ?path
104 // 5 - ?query 103 // 5 - ?query
105 // 6 - ?fragment 104 // 6 - ?fragment
106 var schemeRegex = /([A-Za-z][A-Za-z0-9+.-]*):\/\//; 105 var schemeRegex = /([A-Za-z][A-Za-z0-9+.-]*):\/\//;
107 var hostRegex = /([^\s\/:]*)/; 106 var hostRegex = /([^\s\/:]*)/;
108 var portRegex = /(?::([\d]+))?/; 107 var portRegex = /(?::([\d]+))?/;
109 var pathRegex = /(\/[^#?]*)?/; 108 var pathRegex = /(\/[^#?]*)?/;
110 var queryRegex = /(?:\?([^#]*))?/; 109 var queryRegex = /(?:\?([^#]*))?/;
111 var fragmentRegex = /(?:#(.*))?/; 110 var fragmentRegex = /(?:#(.*))?/;
112 111
113 WebInspector.ParsedURL._urlRegexInstance = new RegExp("^" + schemeRegex.sour ce + hostRegex.source + portRegex.source + pathRegex.source + queryRegex.source + fragmentRegex.source + "$"); 112 WebInspector.ParsedURL._urlRegexInstance = new RegExp(
113 '^' + schemeRegex.source + hostRegex.source + portRegex.source + pathReg ex.source + queryRegex.source +
114 fragmentRegex.source + '$');
114 return WebInspector.ParsedURL._urlRegexInstance; 115 return WebInspector.ParsedURL._urlRegexInstance;
115 }; 116 }
116 117
117 /** 118 /**
118 * @param {string} url 119 * @param {string} url
119 * @return {string} 120 * @return {string}
120 */ 121 */
121 WebInspector.ParsedURL.extractPath = function(url) 122 static extractPath(url) {
122 {
123 var parsedURL = url.asParsedURL(); 123 var parsedURL = url.asParsedURL();
124 return parsedURL ? parsedURL.path : ""; 124 return parsedURL ? parsedURL.path : '';
125 }; 125 }
126 126
127 /** 127 /**
128 * @param {string} url 128 * @param {string} url
129 * @return {string} 129 * @return {string}
130 */ 130 */
131 WebInspector.ParsedURL.extractOrigin = function(url) 131 static extractOrigin(url) {
132 {
133 var parsedURL = url.asParsedURL(); 132 var parsedURL = url.asParsedURL();
134 return parsedURL ? parsedURL.securityOrigin() : ""; 133 return parsedURL ? parsedURL.securityOrigin() : '';
135 }; 134 }
136 135
137 /** 136 /**
138 * @param {string} url 137 * @param {string} url
139 * @return {string} 138 * @return {string}
140 */ 139 */
141 WebInspector.ParsedURL.extractExtension = function(url) 140 static extractExtension(url) {
142 { 141 var lastIndexOfDot = url.lastIndexOf('.');
143 var lastIndexOfDot = url.lastIndexOf("."); 142 var extension = lastIndexOfDot !== -1 ? url.substr(lastIndexOfDot + 1) : '';
144 var extension = lastIndexOfDot !== -1 ? url.substr(lastIndexOfDot + 1) : ""; 143 var indexOfQuestionMark = extension.indexOf('?');
145 var indexOfQuestionMark = extension.indexOf("?");
146 if (indexOfQuestionMark !== -1) 144 if (indexOfQuestionMark !== -1)
147 extension = extension.substr(0, indexOfQuestionMark); 145 extension = extension.substr(0, indexOfQuestionMark);
148 return extension; 146 return extension;
149 }; 147 }
150 148
151 /** 149 /**
152 * @param {string} url 150 * @param {string} url
153 * @return {string} 151 * @return {string}
154 */ 152 */
155 WebInspector.ParsedURL.extractName = function(url) 153 static extractName(url) {
156 { 154 var index = url.lastIndexOf('/');
157 var index = url.lastIndexOf("/");
158 return index !== -1 ? url.substr(index + 1) : url; 155 return index !== -1 ? url.substr(index + 1) : url;
159 }; 156 }
160 157
161 /** 158 /**
162 * @param {string} baseURL 159 * @param {string} baseURL
163 * @param {string} href 160 * @param {string} href
164 * @return {?string} 161 * @return {?string}
165 */ 162 */
166 WebInspector.ParsedURL.completeURL = function(baseURL, href) 163 static completeURL(baseURL, href) {
167 {
168 // Return special URLs as-is. 164 // Return special URLs as-is.
169 var trimmedHref = href.trim(); 165 var trimmedHref = href.trim();
170 if (trimmedHref.startsWith("data:") || trimmedHref.startsWith("blob:") || tr immedHref.startsWith("javascript:")) 166 if (trimmedHref.startsWith('data:') || trimmedHref.startsWith('blob:') || tr immedHref.startsWith('javascript:'))
171 return href; 167 return href;
172 168
173 // Return absolute URLs as-is. 169 // Return absolute URLs as-is.
174 var parsedHref = trimmedHref.asParsedURL(); 170 var parsedHref = trimmedHref.asParsedURL();
175 if (parsedHref && parsedHref.scheme) 171 if (parsedHref && parsedHref.scheme)
176 return trimmedHref; 172 return trimmedHref;
177 173
178 var parsedURL = baseURL.asParsedURL(); 174 var parsedURL = baseURL.asParsedURL();
179 if (!parsedURL) 175 if (!parsedURL)
180 return null; 176 return null;
181 177
182 if (parsedURL.isDataURL()) 178 if (parsedURL.isDataURL())
183 return href; 179 return href;
184 180
185 if (href.length > 1 && href.charAt(0) === "/" && href.charAt(1) === "/") { 181 if (href.length > 1 && href.charAt(0) === '/' && href.charAt(1) === '/') {
186 // href starts with "//" which is a full URL with the protocol dropped ( use the baseURL protocol). 182 // href starts with "//" which is a full URL with the protocol dropped (us e the baseURL protocol).
187 return parsedURL.scheme + ":" + href; 183 return parsedURL.scheme + ':' + href;
188 } 184 }
189 185
190 var securityOrigin = parsedURL.securityOrigin(); 186 var securityOrigin = parsedURL.securityOrigin();
191 var pathText = parsedURL.path; 187 var pathText = parsedURL.path;
192 var queryText = parsedURL.queryParams ? "?" + parsedURL.queryParams : ""; 188 var queryText = parsedURL.queryParams ? '?' + parsedURL.queryParams : '';
193 189
194 // Empty href resolves to a URL without fragment. 190 // Empty href resolves to a URL without fragment.
195 if (!href.length) 191 if (!href.length)
196 return securityOrigin + pathText + queryText; 192 return securityOrigin + pathText + queryText;
197 193
198 if (href.charAt(0) === "#") 194 if (href.charAt(0) === '#')
199 return securityOrigin + pathText + queryText + href; 195 return securityOrigin + pathText + queryText + href;
200 196
201 if (href.charAt(0) === "?") 197 if (href.charAt(0) === '?')
202 return securityOrigin + pathText + href; 198 return securityOrigin + pathText + href;
203 199
204 var hrefPath = href.match(/^[^#?]*/)[0]; 200 var hrefPath = href.match(/^[^#?]*/)[0];
205 var hrefSuffix = href.substring(hrefPath.length); 201 var hrefSuffix = href.substring(hrefPath.length);
206 if (hrefPath.charAt(0) !== "/") 202 if (hrefPath.charAt(0) !== '/')
207 hrefPath = parsedURL.folderPathComponents + "/" + hrefPath; 203 hrefPath = parsedURL.folderPathComponents + '/' + hrefPath;
208 return securityOrigin + Runtime.normalizePath(hrefPath) + hrefSuffix; 204 return securityOrigin + Runtime.normalizePath(hrefPath) + hrefSuffix;
209 }; 205 }
210 206
211 WebInspector.ParsedURL.prototype = { 207 /**
212 get displayName() 208 * @param {string} string
213 { 209 * @return {!{url: string, lineNumber: (number|undefined), columnNumber: (numb er|undefined)}}
214 if (this._displayName) 210 */
215 return this._displayName; 211 static splitLineAndColumn(string) {
216
217 if (this.isDataURL())
218 return this.dataURLDisplayName();
219 if (this.isAboutBlank())
220 return this.url;
221
222 this._displayName = this.lastPathComponent;
223 if (!this._displayName)
224 this._displayName = (this.host || "") + "/";
225 if (this._displayName === "/")
226 this._displayName = this.url;
227 return this._displayName;
228 },
229
230 /**
231 * @return {string}
232 */
233 dataURLDisplayName: function()
234 {
235 if (this._dataURLDisplayName)
236 return this._dataURLDisplayName;
237 if (!this.isDataURL())
238 return "";
239 this._dataURLDisplayName = this.url.trimEnd(20);
240 return this._dataURLDisplayName;
241 },
242
243 /**
244 * @return {boolean}
245 */
246 isAboutBlank: function()
247 {
248 return this.url === "about:blank";
249 },
250
251 /**
252 * @return {boolean}
253 */
254 isDataURL: function()
255 {
256 return this.scheme === "data";
257 },
258
259 /**
260 * @return {string}
261 */
262 lastPathComponentWithFragment: function()
263 {
264 return this.lastPathComponent + (this.fragment ? "#" + this.fragment : " ");
265 },
266
267 /**
268 * @return {string}
269 */
270 domain: function()
271 {
272 if (this.isDataURL())
273 return "data:";
274 return this.host + (this.port ? ":" + this.port : "");
275 },
276
277 /**
278 * @return {string}
279 */
280 securityOrigin: function()
281 {
282 if (this.isDataURL())
283 return "data:";
284 return this.scheme + "://" + this.domain();
285 },
286
287 /**
288 * @return {string}
289 */
290 urlWithoutScheme: function()
291 {
292 if (this.scheme && this.url.startsWith(this.scheme + "://"))
293 return this.url.substring(this.scheme.length + 3);
294 return this.url;
295 },
296 };
297
298 /**
299 * @param {string} string
300 * @return {!{url: string, lineNumber: (number|undefined), columnNumber: (number |undefined)}}
301 */
302 WebInspector.ParsedURL.splitLineAndColumn = function(string)
303 {
304 var lineColumnRegEx = /(?::(\d+))?(?::(\d+))?$/; 212 var lineColumnRegEx = /(?::(\d+))?(?::(\d+))?$/;
305 var lineColumnMatch = lineColumnRegEx.exec(string); 213 var lineColumnMatch = lineColumnRegEx.exec(string);
306 var lineNumber; 214 var lineNumber;
307 var columnNumber; 215 var columnNumber;
308 console.assert(lineColumnMatch); 216 console.assert(lineColumnMatch);
309 217
310 if (typeof(lineColumnMatch[1]) === "string") { 218 if (typeof(lineColumnMatch[1]) === 'string') {
311 lineNumber = parseInt(lineColumnMatch[1], 10); 219 lineNumber = parseInt(lineColumnMatch[1], 10);
312 // Immediately convert line and column to 0-based numbers. 220 // Immediately convert line and column to 0-based numbers.
313 lineNumber = isNaN(lineNumber) ? undefined : lineNumber - 1; 221 lineNumber = isNaN(lineNumber) ? undefined : lineNumber - 1;
314 } 222 }
315 if (typeof(lineColumnMatch[2]) === "string") { 223 if (typeof(lineColumnMatch[2]) === 'string') {
316 columnNumber = parseInt(lineColumnMatch[2], 10); 224 columnNumber = parseInt(lineColumnMatch[2], 10);
317 columnNumber = isNaN(columnNumber) ? undefined : columnNumber - 1; 225 columnNumber = isNaN(columnNumber) ? undefined : columnNumber - 1;
318 } 226 }
319 227
320 return {url: string.substring(0, string.length - lineColumnMatch[0].length), lineNumber: lineNumber, columnNumber: columnNumber}; 228 return {
229 url: string.substring(0, string.length - lineColumnMatch[0].length),
230 lineNumber: lineNumber,
231 columnNumber: columnNumber
232 };
233 }
234
235 /**
236 * @param {string} url
237 * @return {boolean}
238 */
239 static isRelativeURL(url) {
240 return !(/^[A-Za-z][A-Za-z0-9+.-]*:/.test(url));
241 }
242
243 get displayName() {
244 if (this._displayName)
245 return this._displayName;
246
247 if (this.isDataURL())
248 return this.dataURLDisplayName();
249 if (this.isAboutBlank())
250 return this.url;
251
252 this._displayName = this.lastPathComponent;
253 if (!this._displayName)
254 this._displayName = (this.host || '') + '/';
255 if (this._displayName === '/')
256 this._displayName = this.url;
257 return this._displayName;
258 }
259
260 /**
261 * @return {string}
262 */
263 dataURLDisplayName() {
264 if (this._dataURLDisplayName)
265 return this._dataURLDisplayName;
266 if (!this.isDataURL())
267 return '';
268 this._dataURLDisplayName = this.url.trimEnd(20);
269 return this._dataURLDisplayName;
270 }
271
272 /**
273 * @return {boolean}
274 */
275 isAboutBlank() {
276 return this.url === 'about:blank';
277 }
278
279 /**
280 * @return {boolean}
281 */
282 isDataURL() {
283 return this.scheme === 'data';
284 }
285
286 /**
287 * @return {string}
288 */
289 lastPathComponentWithFragment() {
290 return this.lastPathComponent + (this.fragment ? '#' + this.fragment : '');
291 }
292
293 /**
294 * @return {string}
295 */
296 domain() {
297 if (this.isDataURL())
298 return 'data:';
299 return this.host + (this.port ? ':' + this.port : '');
300 }
301
302 /**
303 * @return {string}
304 */
305 securityOrigin() {
306 if (this.isDataURL())
307 return 'data:';
308 return this.scheme + '://' + this.domain();
309 }
310
311 /**
312 * @return {string}
313 */
314 urlWithoutScheme() {
315 if (this.scheme && this.url.startsWith(this.scheme + '://'))
316 return this.url.substring(this.scheme.length + 3);
317 return this.url;
318 }
321 }; 319 };
322 320
323 /**
324 * @param {string} url
325 * @return {boolean}
326 */
327 WebInspector.ParsedURL.isRelativeURL = function(url)
328 {
329 return !(/^[A-Za-z][A-Za-z0-9+.-]*:/.test(url));
330 };
331 321
332 /** 322 /**
333 * @return {?WebInspector.ParsedURL} 323 * @return {?WebInspector.ParsedURL}
334 */ 324 */
335 String.prototype.asParsedURL = function() 325 String.prototype.asParsedURL = function() {
336 { 326 var parsedURL = new WebInspector.ParsedURL(this.toString());
337 var parsedURL = new WebInspector.ParsedURL(this.toString()); 327 if (parsedURL.isValid)
338 if (parsedURL.isValid) 328 return parsedURL;
339 return parsedURL; 329 return null;
340 return null;
341 }; 330 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698