Skip to content

Commit

Permalink
[html] CTRL-click should ignore query string. Fixes microsoft/vscode#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli authored Oct 2, 2023
1 parent 3584d32 commit 4ae534d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/services/htmlLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,27 @@ function createLink(document: TextDocument, documentContext: DocumentContext, at
endOffset--;
}
const workspaceUrl = getWorkspaceUrl(document.uri, tokenContent, documentContext, base);
if (!workspaceUrl || !isValidURI(workspaceUrl)) {
if (!workspaceUrl) {
return undefined;
}
const target = validateAndCleanURI(workspaceUrl);


return {
range: Range.create(document.positionAt(startOffset), document.positionAt(endOffset)),
target: workspaceUrl
target
};
}

function isValidURI(uri: string) {
function validateAndCleanURI(uriStr: string) : string | undefined {
try {
Uri.parse(uri);
return true;
const uri = Uri.parse(uriStr);
if (uri.query) {
return uri.with({ query: null }).toString(/* skipEncodig*/ true);
}
return uriStr;
} catch (e) {
return false;
return undefined;
}
}

Expand Down Expand Up @@ -153,4 +159,3 @@ export class HTMLDocumentLinks {
return newLinks;
}
}

3 changes: 2 additions & 1 deletion src/test/links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ suite('HTML Link Detection', () => {

testLinkDetection('<link rel="icon" type="image/x-icon" href="data:@file/x-icon;base64,AAABAAIAQEAAAAEAIAAoQgAAJgA">', []);
testLinkDetection('<blockquote cite="foo.png">', [{ offset: 18, length: 7, target: 'file:///test/data/abc/foo.png' }]);
testLinkDetection('<style src="styles.css?t=345">', [{ offset: 12, length: 16, target: 'file:///test/data/abc/styles.css' }]);
});

test('Local targets', () => {
testLinkDetection('<body><h1 id="title"></h1><a href="#title"</a></body>', [{ offset: 35, length: 6, target: 'file:///test/data/abc/test.html#1,14' }]);
testLinkDetection('<body><h1 id="title"></h1><a href="file:///test/data/abc/test.html#title"</a></body>', [{ offset: 35, length: 37, target: 'file:///test/data/abc/test.html#1,14' }]);
});

});
});

0 comments on commit 4ae534d

Please sign in to comment.