diff --git a/front_end/core/common/ParsedURL.ts b/front_end/core/common/ParsedURL.ts index ca6882b4f1..e1f8cab22a 100644 --- a/front_end/core/common/ParsedURL.ts +++ b/front_end/core/common/ParsedURL.ts @@ -168,17 +168,16 @@ export class ParsedURL { } static rawPathToUrlString(fileSystemPath: Platform.DevToolsPath.RawPathString): Platform.DevToolsPath.UrlString { - let preEncodedPath: string = ParsedURL.preEncodeSpecialCharactersInPath( - fileSystemPath.replace(/\\/g, '/') as Platform.DevToolsPath.RawPathString); - preEncodedPath = preEncodedPath.replace(/\\/g, '/'); - if (!preEncodedPath.startsWith('file://')) { - if (preEncodedPath.startsWith('/')) { - preEncodedPath = 'file://' + preEncodedPath; + let rawPath: string = fileSystemPath; + rawPath = rawPath.replace(/\\/g, '/'); + if (!rawPath.startsWith('file://')) { + if (rawPath.startsWith('/')) { + rawPath = 'file://' + rawPath; } else { - preEncodedPath = 'file:///' + preEncodedPath; + rawPath = 'file:///' + rawPath; } } - return new URL(preEncodedPath).toString() as Platform.DevToolsPath.UrlString; + return rawPath as Platform.DevToolsPath.UrlString; } static relativePathToUrlString(relativePath: string, baseURL: Platform.DevToolsPath.UrlString): @@ -188,14 +187,13 @@ export class ParsedURL { return new URL(preEncodedPath, baseURL).toString() as Platform.DevToolsPath.UrlString; } - static urlToRawPathString(fileURL: Platform.DevToolsPath.UrlString, isWindows?: boolean): + static capFilePrefix(fileURL: Platform.DevToolsPath.UrlString, isWindows?: boolean): Platform.DevToolsPath.RawPathString { console.assert(fileURL.startsWith('file://'), 'This must be a file URL.'); - const decodedFileURL = decodeURIComponent(fileURL); if (isWindows) { - return decodedFileURL.substr('file:///'.length).replace(/\//g, '\\') as Platform.DevToolsPath.RawPathString; + return fileURL.substr('file:///'.length).replace(/\//g, '\\') as Platform.DevToolsPath.RawPathString; } - return decodedFileURL.substr('file://'.length) as Platform.DevToolsPath.RawPathString; + return fileURL.substr('file://'.length) as Platform.DevToolsPath.RawPathString; } static urlWithoutHash(url: string): string { diff --git a/front_end/core/sdk/DebuggerModel.ts b/front_end/core/sdk/DebuggerModel.ts index 5b7727fa6c..59a6532e40 100644 --- a/front_end/core/sdk/DebuggerModel.ts +++ b/front_end/core/sdk/DebuggerModel.ts @@ -425,7 +425,7 @@ export class DebuggerModel extends SDKModel { if (this.target().type() === Type.Node && url.startsWith('file://')) { // TODO(crbug.com/1253323): Cast to UrlString will be removed when migration to branded types is complete. const platformPath = - Common.ParsedURL.ParsedURL.urlToRawPathString(url as Platform.DevToolsPath.UrlString, Host.Platform.isWin()); + Common.ParsedURL.ParsedURL.capFilePrefix(url as Platform.DevToolsPath.UrlString, Host.Platform.isWin()); urlRegex = `${Platform.StringUtilities.escapeForRegExp(platformPath)}|${Platform.StringUtilities.escapeForRegExp(url)}`; } diff --git a/front_end/models/persistence/IsolatedFileSystem.ts b/front_end/models/persistence/IsolatedFileSystem.ts index 6d27fdcee1..e8fc84ea69 100644 --- a/front_end/models/persistence/IsolatedFileSystem.ts +++ b/front_end/models/persistence/IsolatedFileSystem.ts @@ -176,7 +176,7 @@ export class IsolatedFileSystem extends PlatformFileSystem { this.initialGitFoldersInternal.add(parentFolder); } if (this.isFileExcluded(entry.fullPath + '/')) { - this.excludedEmbedderFolders.push(Common.ParsedURL.ParsedURL.urlToRawPathString( + this.excludedEmbedderFolders.push(Common.ParsedURL.ParsedURL.capFilePrefix( this.path() + entry.fullPath as Platform.DevToolsPath.UrlString, Host.Platform.isWin())); continue; } @@ -555,8 +555,7 @@ export class IsolatedFileSystem extends PlatformFileSystem { tooltipForURL(url: string): string { const path = Platform.StringUtilities.trimMiddle( - Common.ParsedURL.ParsedURL.urlToRawPathString(url as Platform.DevToolsPath.UrlString, Host.Platform.isWin()), - 150); + Common.ParsedURL.ParsedURL.capFilePrefix(url as Platform.DevToolsPath.UrlString, Host.Platform.isWin()), 150); return i18nString(UIStrings.linkedToS, {PH1: path}); } diff --git a/front_end/models/persistence/PersistenceActions.ts b/front_end/models/persistence/PersistenceActions.ts index 2ebcc13c19..23f9b7adbb 100644 --- a/front_end/models/persistence/PersistenceActions.ts +++ b/front_end/models/persistence/PersistenceActions.ts @@ -94,8 +94,8 @@ export class ContextMenuProvider implements UI.ContextMenu.Provider { const fileURL = binding ? binding.fileSystem.contentURL() : contentProvider.contentURL(); if (fileURL.startsWith('file://')) { // TODO(crbug.com/1253323): Cast to UrlString will be removed when migration to branded types is complete. - const path = Common.ParsedURL.ParsedURL.urlToRawPathString( - fileURL as Platform.DevToolsPath.UrlString, Host.Platform.isWin()); + const path = + Common.ParsedURL.ParsedURL.capFilePrefix(fileURL as Platform.DevToolsPath.UrlString, Host.Platform.isWin()); contextMenu.revealSection().appendItem( i18nString(UIStrings.openInContainingFolder), () => Host.InspectorFrontendHost.InspectorFrontendHostInstance.showItemInFolder(path)); diff --git a/front_end/panels/sources/NavigatorView.ts b/front_end/panels/sources/NavigatorView.ts index f5abdbd49d..c8838d943a 100644 --- a/front_end/panels/sources/NavigatorView.ts +++ b/front_end/panels/sources/NavigatorView.ts @@ -849,7 +849,7 @@ export class NavigatorView extends UI.Widget.VBox implements SDK.TargetManager.O if (project.type() === Workspace.Workspace.projectTypes.FileSystem) { // TODO(crbug.com/1253323): Cast to RawPathString will be removed when migration to branded types is complete. - const folderPath = Common.ParsedURL.ParsedURL.urlToRawPathString( + const folderPath = Common.ParsedURL.ParsedURL.capFilePrefix( Persistence.FileSystemWorkspaceBinding.FileSystemWorkspaceBinding.completeURL(project, path) as Platform.DevToolsPath.UrlString, Host.Platform.isWin()); diff --git a/test/unittests/front_end/core/common/ParsedURL_test.ts b/test/unittests/front_end/core/common/ParsedURL_test.ts index 4c13da9445..699435edb7 100644 --- a/test/unittests/front_end/core/common/ParsedURL_test.ts +++ b/test/unittests/front_end/core/common/ParsedURL_test.ts @@ -121,13 +121,13 @@ describe('Parsed URL', () => { it('converts path that starts with "file://" to a platform path', () => { const pathTest = 'file://usr/lib' as Platform.DevToolsPath.UrlString; - const convertedPath = ParsedURL.urlToRawPathString(pathTest); + const convertedPath = ParsedURL.capFilePrefix(pathTest); assert.strictEqual(convertedPath, 'usr/lib', 'URL was not converted successfully'); }); it('converts path that starts with "file:///" to a platform path on Windows', () => { const pathTest = 'file:///usr/lib' as Platform.DevToolsPath.UrlString; - const convertedPath = ParsedURL.urlToRawPathString(pathTest, true); + const convertedPath = ParsedURL.capFilePrefix(pathTest, true); assert.strictEqual(convertedPath, 'usr\\lib', 'URL was not converted successfully'); }); @@ -570,33 +570,4 @@ describe('Parsed URL', () => { const convertedUrl = ParsedURL.relativePathToUrlString(relativePath, baseUrl); assert.strictEqual(convertedUrl, 'http://localhost:8080/my%20folder/new%20spaced%2520name'); }); - - it('converts URL to a platform path that includes drive letter and spaces on Windows', () => { - const urlTest = 'file:///C:/Program%20Files/Google' as Platform.DevToolsPath.UrlString; - const convertedUrl = ParsedURL.urlToRawPathString(urlTest, true); - assert.strictEqual(convertedUrl, 'C:\\Program Files\\Google', 'URL was not converted successfully'); - }); - - it('converts URL to a platform path that includes spaces and percents', () => { - const urlTest = 'file:///home/user/with%20space/with%2520escape' as Platform.DevToolsPath.UrlString; - const convertedUrl = ParsedURL.urlToRawPathString(urlTest, false); - assert.strictEqual(convertedUrl, '/home/user/with space/with%20escape', 'URL was not converted successfully'); - }); - - it('converts Windows platform path with spaces and percents to file url', () => { - const urlTest = 'C:\\Program Files\\with%20escape' as Platform.DevToolsPath.RawPathString; - const convertedUrl = ParsedURL.rawPathToUrlString(urlTest); - assert.strictEqual( - convertedUrl, 'file:///C:/Program%20Files/with%2520escape', 'URL was not converted successfully'); - }); - - it('converts platform path with variety of special characters to URL and back consistently with Chrome', () => { - const platformPathTest = - '/home/a:b@c(d, e+f)=&g;#h$' as Platform.DevToolsPath.RawPathString; // Valid filename on unix - const urlTest = 'file:///home/a:b@c(d,%20e+f)=&g%3B%23h$' as - Platform.DevToolsPath.UrlString; // URL in Chrome address bar if you open that file - assert.strictEqual(ParsedURL.rawPathToUrlString(platformPathTest), urlTest); - assert.strictEqual(ParsedURL.urlToRawPathString(urlTest), platformPathTest); - }); - }); diff --git a/test/unittests/front_end/core/protocol_client/NodeURL_test.ts b/test/unittests/front_end/core/protocol_client/NodeURL_test.ts index 9d4480e369..d2c945b270 100644 --- a/test/unittests/front_end/core/protocol_client/NodeURL_test.ts +++ b/test/unittests/front_end/core/protocol_client/NodeURL_test.ts @@ -23,16 +23,15 @@ describe('NodeURL', () => { }); describe('patch', () => { - const url = Host.Platform.isWin() ? 'C:\\prog\\foobar.js' : '/usr/local/home/prog/foobar.js'; - const validPatchedUrl = - Host.Platform.isWin() ? 'file:///C:/prog/foobar.js' : 'file:///usr/local/home/prog/foobar.js'; + const url = Host.Platform.isWin() ? 'c:\\prog\\foobar.js' : '/usr/local/home/prog/foobar.js'; + const patchedUrl = Host.Platform.isWin() ? 'file:///c:/prog/foobar.js' : 'file:///usr/local/home/prog/foobar.js'; it('does patch url fields', () => { const object = {url, result: null}; ProtocolClient.NodeURL.NodeURL.patch(object); - assert.strictEqual(object.url, validPatchedUrl); + assert.strictEqual(object.url, patchedUrl); }); it('does not patch the url of the result', () => { @@ -68,8 +67,8 @@ describe('NodeURL', () => { ProtocolClient.NodeURL.NodeURL.patch(object as unknown as {url: string}); - assert.strictEqual(object.exceptionDetails.url, validPatchedUrl); - assert.strictEqual(object.exceptionDetails.stackTrace.callFrames[0].url, validPatchedUrl); + assert.strictEqual(object.exceptionDetails.url, patchedUrl); + assert.strictEqual(object.exceptionDetails.stackTrace.callFrames[0].url, patchedUrl); }); }); });