Skip to content

Commit

Permalink
Merge pull request xtermjs#4937 from szymonkaliski/sk/fix-url-parsing
Browse files Browse the repository at this point in the history
fix weblinks addon url parsing for url.encoded urls
  • Loading branch information
jerch authored Jan 11, 2024
2 parents 1940bbe + f03d870 commit 99df13b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
23 changes: 15 additions & 8 deletions addons/addon-web-links/src/WebLinkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ export class WebLinkProvider implements ILinkProvider {
}
}

function baseUrlString(url: URL): string {
if (url.password && url.username) {
return `${url.protocol}//${url.username}:${url.password}@${url.host}`;
}

if (url.username) {
return `${url.protocol}//${url.username}@${url.host}`;
}

return `${url.protocol}//${url.host}`;
}

export class LinkComputer {
public static computeLink(y: number, regex: RegExp, terminal: Terminal, activate: (event: MouseEvent, uri: string) => void): ILink[] {
const rex = new RegExp(regex.source, (regex.flags || '') + 'g');
Expand All @@ -56,16 +68,11 @@ export class LinkComputer {

// check via URL if the matched text would form a proper url
// NOTE: This outsources the ugly url parsing to the browser.
// To avoid surprising auto expansion from URL we additionally
// check afterwards if the provided string resembles the parsed
// one close enough:
// - decodeURI decode path segement back to byte repr
// to detect unicode auto conversion correctly
// - append / also match domain urls w'o any path notion
// we check if the provided string resembles the URL-parsed one
// up to the end of the domain name (ignoring path and params)
try {
const url = new URL(text);
const urlText = decodeURI(url.toString());
if (text !== urlText && text + '/' !== urlText) {
if (!text.startsWith(baseUrlString(url))) {
continue;
}
} catch (e) {
Expand Down
7 changes: 7 additions & 0 deletions addons/addon-web-links/test/WebLinksAddon.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ describe('WebLinksAddon', () => {
await resetAndHover(5, 1);
await evalLinkStateData('http://test:[email protected]/some_path', { start: { x: 12, y: 1 }, end: { x: 13, y: 2 } });
});
it('url encoded params work properly', async () => {
await writeSync(page, '¥¥¥cafe\u0301 http://test:[email protected]/some_path?param=1%202%3');
await resetAndHover(12, 0);
await evalLinkStateData('http://test:[email protected]/some_path?param=1%202%3', { start: { x: 12, y: 1 }, end: { x: 27, y: 2 } });
await resetAndHover(5, 1);
await evalLinkStateData('http://test:[email protected]/some_path?param=1%202%3', { start: { x: 12, y: 1 }, end: { x: 27, y: 2 } });
});
});
});

Expand Down

0 comments on commit 99df13b

Please sign in to comment.