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

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

Issue 2422803002: DevTools: prefix node.js scripts's sourceURL with "file://" to make them a valid url (Closed)
Patch Set: WI.ParsedURL.platformPathToURL Created 4 years, 2 months 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 *
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 var lastSlashIndex = this.path.lastIndexOf("/"); 67 var lastSlashIndex = this.path.lastIndexOf("/");
68 if (lastSlashIndex !== -1) { 68 if (lastSlashIndex !== -1) {
69 this.folderPathComponents = this.path.substring(0, lastSlashIndex); 69 this.folderPathComponents = this.path.substring(0, lastSlashIndex);
70 this.lastPathComponent = this.path.substring(lastSlashIndex + 1); 70 this.lastPathComponent = this.path.substring(lastSlashIndex + 1);
71 } else { 71 } else {
72 this.lastPathComponent = this.path; 72 this.lastPathComponent = this.path;
73 } 73 }
74 } 74 }
75 75
76 /** 76 /**
77 * @param {string} fileSystemPath
78 * @return {string}
79 */
80 WebInspector.ParsedURL.platformPathToURL = function(fileSystemPath)
81 {
82 fileSystemPath = fileSystemPath.replace(/\\/g, "/");
83 if (!fileSystemPath.startsWith("file://")) {
84 if (fileSystemPath.startsWith("/"))
85 fileSystemPath = "file://" + fileSystemPath;
86 else
87 fileSystemPath = "file:///" + fileSystemPath;
88 }
89 return fileSystemPath;
90 }
91
92 /**
77 * @return {!RegExp} 93 * @return {!RegExp}
78 */ 94 */
79 WebInspector.ParsedURL._urlRegex = function() 95 WebInspector.ParsedURL._urlRegex = function()
80 { 96 {
81 if (WebInspector.ParsedURL._urlRegexInstance) 97 if (WebInspector.ParsedURL._urlRegexInstance)
82 return WebInspector.ParsedURL._urlRegexInstance; 98 return WebInspector.ParsedURL._urlRegexInstance;
83 // RegExp groups: 99 // RegExp groups:
84 // 1 - scheme (using the RFC3986 grammar) 100 // 1 - scheme (using the RFC3986 grammar)
85 // 2 - hostname 101 // 2 - hostname
86 // 3 - ?port 102 // 3 - ?port
87 // 4 - ?path 103 // 4 - ?path
88 // 5 - ?query 104 // 5 - ?query
89 // 6 - ?fragment 105 // 6 - ?fragment
90 var schemeRegex = /([A-Za-z][A-Za-z0-9+.-]*):\/\//; 106 var schemeRegex = /([A-Za-z][A-Za-z0-9+.-]*):\/\//;
91 var hostRegex = /([^\s\/:]*)/; 107 var hostRegex = /([^\s\/:]*)/;
92 var portRegex = /(?::([\d]+))?/; 108 var portRegex = /(?::([\d]+))?/;
93 var pathRegex = /(\/[^#?]*)?/; 109 var pathRegex = /(\/[^#?]*)?/;
94 var queryRegex = /(?:\?([^#]*))?/; 110 var queryRegex = /(?:\?([^#]*))?/;
95 var fragmentRegex = /(?:#(.*))?/; 111 var fragmentRegex = /(?:#(.*))?/;
96 112
97 WebInspector.ParsedURL._urlRegexInstance = new RegExp("^" + schemeRegex.sour ce + hostRegex.source + portRegex.source + pathRegex.source + queryRegex.source + fragmentRegex.source + "$"); 113 WebInspector.ParsedURL._urlRegexInstance = new RegExp("^" + schemeRegex.sour ce + hostRegex.source + portRegex.source + pathRegex.source + queryRegex.source + fragmentRegex.source + "$");
98 return WebInspector.ParsedURL._urlRegexInstance; 114 return WebInspector.ParsedURL._urlRegexInstance;
99 } 115 }
100 116
101 /** 117 /**
102 * @param {string} url 118 * @param {string} url
103 * @return {!Array.<string>} 119 * @return {string}
104 */ 120 */
105 WebInspector.ParsedURL.splitURLIntoPathComponents = function(url) 121 WebInspector.ParsedURL.extractPath = function(url)
106 { 122 {
107 if (url.startsWith("/")) 123 var parsedURL = url.asParsedURL();
108 url = "file://" + url; 124 return parsedURL ? parsedURL.path : "";
109 var parsedURL = new WebInspector.ParsedURL(url);
110 var origin;
111 var folderPath;
112 var name;
113 if (parsedURL.isValid) {
114 origin = parsedURL.scheme + "://" + parsedURL.host;
115 if (parsedURL.port)
116 origin += ":" + parsedURL.port;
117 folderPath = parsedURL.folderPathComponents;
118 name = parsedURL.lastPathComponent;
119 if (parsedURL.queryParams)
120 name += "?" + parsedURL.queryParams;
121 } else {
122 origin = "";
123 folderPath = "";
124 name = url;
125 }
126 var result = [origin];
127 var splittedPath = folderPath.split("/");
128 for (var i = 1; i < splittedPath.length; ++i) {
129 if (!splittedPath[i])
130 continue;
131 result.push(splittedPath[i]);
132 }
133 result.push(name);
134 return result;
135 } 125 }
136 126
137 /** 127 /**
138 * @param {string} url 128 * @param {string} url
139 * @return {string} 129 * @return {string}
140 */ 130 */
141 WebInspector.ParsedURL.extractOrigin = function(url) 131 WebInspector.ParsedURL.extractOrigin = function(url)
142 { 132 {
143 var parsedURL = new WebInspector.ParsedURL(url); 133 var parsedURL = url.asParsedURL();
144 if (!parsedURL.isValid) 134 return parsedURL ? parsedURL.securityOrigin() : "";
145 return "";
146
147 var origin = parsedURL.scheme + "://" + parsedURL.host;
148 if (parsedURL.port)
149 origin += ":" + parsedURL.port;
150 return origin;
151 } 135 }
152 136
153 /** 137 /**
154 * @param {string} url 138 * @param {string} url
155 * @return {string} 139 * @return {string}
156 */ 140 */
157 WebInspector.ParsedURL.extractExtension = function(url) 141 WebInspector.ParsedURL.extractExtension = function(url)
158 { 142 {
159 var lastIndexOfDot = url.lastIndexOf("."); 143 var lastIndexOfDot = url.lastIndexOf(".");
160 var extension = lastIndexOfDot !== -1 ? url.substr(lastIndexOfDot + 1) : ""; 144 var extension = lastIndexOfDot !== -1 ? url.substr(lastIndexOfDot + 1) : "";
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 /** 332 /**
349 * @return {?WebInspector.ParsedURL} 333 * @return {?WebInspector.ParsedURL}
350 */ 334 */
351 String.prototype.asParsedURL = function() 335 String.prototype.asParsedURL = function()
352 { 336 {
353 var parsedURL = new WebInspector.ParsedURL(this.toString()); 337 var parsedURL = new WebInspector.ParsedURL(this.toString());
354 if (parsedURL.isValid) 338 if (parsedURL.isValid)
355 return parsedURL; 339 return parsedURL;
356 return null; 340 return null;
357 } 341 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698