OLD | NEW |
---|---|
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 * |
(...skipping 30 matching lines...) Expand all Loading... | |
41 this.port = ''; | 41 this.port = ''; |
42 this.path = ''; | 42 this.path = ''; |
43 this.queryParams = ''; | 43 this.queryParams = ''; |
44 this.fragment = ''; | 44 this.fragment = ''; |
45 this.folderPathComponents = ''; | 45 this.folderPathComponents = ''; |
46 this.lastPathComponent = ''; | 46 this.lastPathComponent = ''; |
47 | 47 |
48 var match = url.match(Common.ParsedURL._urlRegex()); | 48 var match = url.match(Common.ParsedURL._urlRegex()); |
49 if (match) { | 49 if (match) { |
50 this.isValid = true; | 50 this.isValid = true; |
51 this.scheme = match[1].toLowerCase(); | 51 this.scheme = match[2].toLowerCase(); |
52 this.host = match[2]; | 52 this.host = match[3]; |
53 this.port = match[3]; | 53 this.port = match[4]; |
54 this.path = match[4] || '/'; | 54 this.path = match[5] || '/'; |
55 this.queryParams = match[5] || ''; | 55 this.queryParams = match[6] || ''; |
56 this.fragment = match[6]; | 56 this.fragment = match[7]; |
57 } else { | 57 } else { |
58 if (this.url.startsWith('data:')) { | 58 if (this.url.startsWith('data:')) { |
59 this.scheme = 'data'; | 59 this.scheme = 'data'; |
60 return; | 60 return; |
61 } | 61 } |
62 if (this.url === 'about:blank') { | 62 if (this.url === 'about:blank') { |
63 this.scheme = 'about'; | 63 this.scheme = 'about'; |
64 return; | 64 return; |
65 } | 65 } |
66 this.path = this.url; | 66 this.path = this.url; |
(...skipping 23 matching lines...) Expand all Loading... | |
90 return fileSystemPath; | 90 return fileSystemPath; |
91 } | 91 } |
92 | 92 |
93 /** | 93 /** |
94 * @return {!RegExp} | 94 * @return {!RegExp} |
95 */ | 95 */ |
96 static _urlRegex() { | 96 static _urlRegex() { |
97 if (Common.ParsedURL._urlRegexInstance) | 97 if (Common.ParsedURL._urlRegexInstance) |
98 return Common.ParsedURL._urlRegexInstance; | 98 return Common.ParsedURL._urlRegexInstance; |
99 // RegExp groups: | 99 // RegExp groups: |
100 // 1 - scheme (using the RFC3986 grammar) | 100 // 1 - scheme, hostname, ?port |
101 // 2 - hostname | 101 // 2 - scheme (using the RFC3986 grammar) |
102 // 3 - ?port | 102 // 3 - hostname |
103 // 4 - ?path | 103 // 4 - ?port |
104 // 5 - ?query | 104 // 5 - ?path |
105 // 6 - ?fragment | 105 // 6 - ?query |
106 // 7 - ?fragment | |
106 var schemeRegex = /([A-Za-z][A-Za-z0-9+.-]*):\/\//; | 107 var schemeRegex = /([A-Za-z][A-Za-z0-9+.-]*):\/\//; |
107 var hostRegex = /([^\s\/:]*)/; | 108 var hostRegex = /([^\s\/:]*)/; |
108 var portRegex = /(?::([\d]+))?/; | 109 var portRegex = /(?::([\d]+))?/; |
109 var pathRegex = /(\/[^#?]*)?/; | 110 var pathRegex = /(\/[^#?]*)?/; |
110 var queryRegex = /(?:\?([^#]*))?/; | 111 var queryRegex = /(?:\?([^#]*))?/; |
111 var fragmentRegex = /(?:#(.*))?/; | 112 var fragmentRegex = /(?:#(.*))?/; |
112 | 113 |
113 Common.ParsedURL._urlRegexInstance = new RegExp( | 114 Common.ParsedURL._urlRegexInstance = new RegExp( |
114 '^' + schemeRegex.source + hostRegex.source + portRegex.source + pathReg ex.source + queryRegex.source + | 115 '^(' + schemeRegex.source + hostRegex.source + portRegex.source + ')' + pathRegex.source + queryRegex.source + |
115 fragmentRegex.source + '$'); | 116 fragmentRegex.source + '$'); |
116 return Common.ParsedURL._urlRegexInstance; | 117 return Common.ParsedURL._urlRegexInstance; |
117 } | 118 } |
118 | 119 |
119 /** | 120 /** |
120 * @param {string} url | 121 * @param {string} url |
121 * @return {string} | 122 * @return {string} |
122 */ | 123 */ |
123 static extractPath(url) { | 124 static extractPath(url) { |
124 var parsedURL = url.asParsedURL(); | 125 var parsedURL = url.asParsedURL(); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 if (hrefPath.charAt(0) !== '/') | 204 if (hrefPath.charAt(0) !== '/') |
204 hrefPath = parsedURL.folderPathComponents + '/' + hrefPath; | 205 hrefPath = parsedURL.folderPathComponents + '/' + hrefPath; |
205 return securityOrigin + Runtime.normalizePath(hrefPath) + hrefSuffix; | 206 return securityOrigin + Runtime.normalizePath(hrefPath) + hrefSuffix; |
206 } | 207 } |
207 | 208 |
208 /** | 209 /** |
209 * @param {string} string | 210 * @param {string} string |
210 * @return {!{url: string, lineNumber: (number|undefined), columnNumber: (numb er|undefined)}} | 211 * @return {!{url: string, lineNumber: (number|undefined), columnNumber: (numb er|undefined)}} |
211 */ | 212 */ |
212 static splitLineAndColumn(string) { | 213 static splitLineAndColumn(string) { |
214 // Only look for line and column numbers in the path to avoid matching port numbers. | |
215 var beforePathMatch = string.match(Common.ParsedURL._urlRegex()); | |
alph
2017/04/26 22:00:05
Can't you just use lastPathComponentWithFragment()
luoe
2017/04/26 23:45:05
It doesn't include queryParams, but we could add i
| |
216 var beforePath = ''; | |
217 var pathAndAfter = string; | |
218 if (beforePathMatch) { | |
219 beforePath = beforePathMatch[1]; | |
220 pathAndAfter = string.substring(beforePathMatch[1].length); | |
221 } | |
222 | |
213 var lineColumnRegEx = /(?::(\d+))?(?::(\d+))?$/; | 223 var lineColumnRegEx = /(?::(\d+))?(?::(\d+))?$/; |
214 var lineColumnMatch = lineColumnRegEx.exec(string); | 224 var lineColumnMatch = lineColumnRegEx.exec(pathAndAfter); |
215 var lineNumber; | 225 var lineNumber; |
216 var columnNumber; | 226 var columnNumber; |
217 console.assert(lineColumnMatch); | 227 console.assert(lineColumnMatch); |
218 | 228 |
219 if (typeof(lineColumnMatch[1]) === 'string') { | 229 if (typeof(lineColumnMatch[1]) === 'string') { |
220 lineNumber = parseInt(lineColumnMatch[1], 10); | 230 lineNumber = parseInt(lineColumnMatch[1], 10); |
221 // Immediately convert line and column to 0-based numbers. | 231 // Immediately convert line and column to 0-based numbers. |
222 lineNumber = isNaN(lineNumber) ? undefined : lineNumber - 1; | 232 lineNumber = isNaN(lineNumber) ? undefined : lineNumber - 1; |
223 } | 233 } |
224 if (typeof(lineColumnMatch[2]) === 'string') { | 234 if (typeof(lineColumnMatch[2]) === 'string') { |
225 columnNumber = parseInt(lineColumnMatch[2], 10); | 235 columnNumber = parseInt(lineColumnMatch[2], 10); |
226 columnNumber = isNaN(columnNumber) ? undefined : columnNumber - 1; | 236 columnNumber = isNaN(columnNumber) ? undefined : columnNumber - 1; |
227 } | 237 } |
228 | 238 |
229 return { | 239 return { |
230 url: string.substring(0, string.length - lineColumnMatch[0].length), | 240 url: beforePath + pathAndAfter.substring(0, pathAndAfter.length - lineColu mnMatch[0].length), |
231 lineNumber: lineNumber, | 241 lineNumber: lineNumber, |
232 columnNumber: columnNumber | 242 columnNumber: columnNumber |
233 }; | 243 }; |
234 } | 244 } |
235 | 245 |
236 /** | 246 /** |
237 * @param {string} url | 247 * @param {string} url |
238 * @return {boolean} | 248 * @return {boolean} |
239 */ | 249 */ |
240 static isRelativeURL(url) { | 250 static isRelativeURL(url) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 | 332 |
323 /** | 333 /** |
324 * @return {?Common.ParsedURL} | 334 * @return {?Common.ParsedURL} |
325 */ | 335 */ |
326 String.prototype.asParsedURL = function() { | 336 String.prototype.asParsedURL = function() { |
327 var parsedURL = new Common.ParsedURL(this.toString()); | 337 var parsedURL = new Common.ParsedURL(this.toString()); |
328 if (parsedURL.isValid) | 338 if (parsedURL.isValid) |
329 return parsedURL; | 339 return parsedURL; |
330 return null; | 340 return null; |
331 }; | 341 }; |
OLD | NEW |