diff --git a/extensions/html/server/src/service/services/htmlLinks.ts b/extensions/html/server/src/service/services/htmlLinks.ts index e5051bdd60f8d0d602bf8f540bb74a4422a47b2b..18e8c921af553355c19604acdfaffe9c13d66935 100644 --- a/extensions/html/server/src/service/services/htmlLinks.ts +++ b/extensions/html/server/src/service/services/htmlLinks.ts @@ -18,7 +18,7 @@ function _stripQuotes(url: string): string { .replace(/^"([^"]+)"$/,(substr, match1) => match1); } -export function _getWorkspaceUrl(modelAbsoluteUri: Uri, rootAbsoluteUrlStr: string, tokenContent: string): string { +export function _getWorkspaceUrl(modelAbsoluteUri: Uri, rootAbsoluteUrl: Uri, tokenContent: string): string { tokenContent = _stripQuotes(tokenContent); if (/^\s*javascript\:/i.test(tokenContent) || /^\s*\#/i.test(tokenContent)) { @@ -47,9 +47,10 @@ export function _getWorkspaceUrl(modelAbsoluteUri: Uri, rootAbsoluteUrlStr: stri alternativeResultPath = paths.join(modelPath, tokenContent); alternativeResultPath = alternativeResultPath.replace(/^(\/\.\.)+/, ''); } - let potentialResult = modelAbsoluteUri.with({ path: alternativeResultPath }).toString(); + let potentialResult = modelAbsoluteUri.with({ path: alternativeResultPath }).toString(true); - if (rootAbsoluteUrlStr && strings.startsWith(modelAbsoluteUri.toString(), rootAbsoluteUrlStr)) { + let rootAbsoluteUrlStr = rootAbsoluteUrl && rootAbsoluteUrl.toString(true); + if (rootAbsoluteUrlStr && strings.startsWith(modelAbsoluteUri.toString(true), rootAbsoluteUrlStr)) { // The `rootAbsoluteUrl` is set and matches our current model // We need to ensure that this `potentialResult` does not escape `rootAbsoluteUrl` @@ -63,7 +64,7 @@ export function _getWorkspaceUrl(modelAbsoluteUri: Uri, rootAbsoluteUrlStr: stri return potentialResult; } -function createLink(document: TextDocument, rootAbsoluteUrl: string, tokenContent: string, startOffset: number, endOffset: number): DocumentLink { +function createLink(document: TextDocument, rootAbsoluteUrl: Uri, tokenContent: string, startOffset: number, endOffset: number): DocumentLink { let documentUri = Uri.parse(document.uri); let workspaceUrl = _getWorkspaceUrl(documentUri, rootAbsoluteUrl, tokenContent); if (!workspaceUrl) { @@ -78,15 +79,13 @@ function createLink(document: TextDocument, rootAbsoluteUrl: string, tokenConten export function provideLinks(document: TextDocument, workspacePath:string): DocumentLink[] { let newLinks: DocumentLink[] = []; - let rootAbsoluteUrl: string = null; + let rootAbsoluteUrl: Uri = null; if (workspacePath) { // The workspace can be null in the no folder opened case - let strRootAbsoluteUrl = workspacePath; - if (strRootAbsoluteUrl.charAt(strRootAbsoluteUrl.length - 1) === '/') { - rootAbsoluteUrl = strRootAbsoluteUrl; - } else { - rootAbsoluteUrl = strRootAbsoluteUrl + '/'; + if (workspacePath.charAt(workspacePath.length - 1) !== '/') { + workspacePath = workspacePath + '/'; } + rootAbsoluteUrl = Uri.parse(workspacePath); } let scanner = createScanner(document.getText(), 0); diff --git a/extensions/html/server/src/service/test/links.test.ts b/extensions/html/server/src/service/test/links.test.ts index a41b020006d3e34904932cd19042e26724b68356..66cabef6ce022082001a28f9f008d3ac5553d604 100644 --- a/extensions/html/server/src/service/test/links.test.ts +++ b/extensions/html/server/src/service/test/links.test.ts @@ -14,7 +14,7 @@ suite('HTML Link Detection', () => { function testLinkCreation(modelUrl:string, rootUrl:string, tokenContent:string, expected:string): void { var _modelUrl = Uri.parse(modelUrl); - var actual = htmlLinks._getWorkspaceUrl(_modelUrl, rootUrl, tokenContent); + var actual = htmlLinks._getWorkspaceUrl(_modelUrl, Uri.parse(rootUrl), tokenContent); assert.equal(actual, expected); } @@ -44,7 +44,7 @@ suite('HTML Link Detection', () => { testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, 'http://www.microsoft.com/', 'http://www.microsoft.com/'); testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, 'https://www.microsoft.com/', 'https://www.microsoft.com/'); testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, ' //www.microsoft.com/', 'http://www.microsoft.com/'); - //testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, 'a.js', 'file:///C:/Alex/src/path/to/a.js'); + testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, 'a.js', 'file:///c:/Alex/src/path/to/a.js'); testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, '/a.js', 'file:///a.js'); testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', 'javascript:void;', null); @@ -55,17 +55,17 @@ suite('HTML Link Detection', () => { testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', 'https://www.microsoft.com/', 'https://www.microsoft.com/'); testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', 'https://www.microsoft.com/?q=1#h', 'https://www.microsoft.com/?q=1#h'); testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', ' //www.microsoft.com/', 'http://www.microsoft.com/'); - //testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', 'a.js', 'file:///C:/Alex/src/path/to/a.js'); - //testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', '/a.js', 'file:///C:/Alex/src/a.js'); + testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', 'a.js', 'file:///c:/Alex/src/path/to/a.js'); + testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', '/a.js', 'file:///c:/Alex/src/a.js'); testLinkCreation('https://www.test.com/path/to/file.txt', null, 'file:///C:\\Alex\\src\\path\\to\\file.txt', 'file:///C:\\Alex\\src\\path\\to\\file.txt'); testLinkCreation('https://www.test.com/path/to/file.txt', null, '//www.microsoft.com/', 'https://www.microsoft.com/'); testLinkCreation('https://www.test.com/path/to/file.txt', 'https://www.test.com', '//www.microsoft.com/', 'https://www.microsoft.com/'); // invalid uris don't throw - testLinkCreation('https://www.test.com/path/to/file.txt', 'https://www.test.com', '%', 'https://www.test.com/path/to/%25'); + testLinkCreation('https://www.test.com/path/to/file.txt', 'https://www.test.com', '%', 'https://www.test.com/path/to/%'); // Bug #18314: Ctrl + Click does not open existing file if folder's name starts with 'c' character - // testLinkCreation('file:///c:/Alex/working_dir/18314-link-detection/test.html', 'file:///c:/Alex/working_dir/18314-link-detection/', '/class/class.js', 'file:///c:/Alex/working_dir/18314-link-detection/class/class.js'); + testLinkCreation('file:///c:/Alex/working_dir/18314-link-detection/test.html', 'file:///c:/Alex/working_dir/18314-link-detection/', '/class/class.js', 'file:///c:/Alex/working_dir/18314-link-detection/class/class.js'); }); }); \ No newline at end of file