Skip to content

Commit

Permalink
fix: support nested urls inside query params (#1581)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Nov 19, 2024
1 parent 1e59470 commit e071abb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
34 changes: 28 additions & 6 deletions projects/editor/components/edit-link/utils/edit-link-parse-url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {tuiIsValidUrl} from '@taiga-ui/cdk';
import {
TUI_EDITOR_LINK_HASH_PREFIX,
TUI_EDITOR_LINK_OSI_PROTOCOL_DIVIDER,
Expand All @@ -10,7 +9,8 @@ interface TuiEditLinkParsed {
prefix: string;
}

function splitOsiProtocol(url = ''): Array<string | undefined> {
function splitOsiProtocol(rawUrl = ''): Array<string | undefined> {
const [url = '', queryParams = ''] = rawUrl.split(/\?/) ?? [];
const protocolPosition = url.indexOf(TUI_EDITOR_LINK_OSI_PROTOCOL_DIVIDER) ?? -1;
const [prefix, path] =
protocolPosition > -1
Expand All @@ -25,18 +25,25 @@ function splitOsiProtocol(url = ''): Array<string | undefined> {
),
]
: ['', url];
const result = [prefix, path].filter(Boolean);

return path?.includes('://') && result.length > 1 ? splitOsiProtocol(path) : result;
const pathWithQueries = path + (queryParams.length ? `?${queryParams}` : '');
const result = [prefix, pathWithQueries].filter(Boolean);

return path?.includes('://') && result.length > 1
? splitOsiProtocol(pathWithQueries)
: result;
}

function splitSimpleProtocol(url = ''): Array<string | undefined> {
function splitSimpleProtocol(rawUrl = ''): Array<string | undefined> {
const [url = '', queryParams = ''] = rawUrl.split(/\?/) ?? [];
const [prefix, path] = url.split(/:/).slice(-2).filter(Boolean);
const hardUrl = // https://domain.com/path:some:schema:data:test
(url.includes('/') && url.lastIndexOf(':') > url.indexOf('/')) ||
(url.includes('?') && url.lastIndexOf(':') > url.indexOf('?'));

return !hardUrl && prefix && path && !tuiIsValidUrl(url) ? [`${prefix}:`, path] : [];
return !hardUrl && prefix && path && !isValidUrl(url)
? [`${prefix}:`, path + (queryParams.length ? `?${queryParams}` : '')]
: [];
}

export function tuiEditLinkParseUrl(url = ''): TuiEditLinkParsed {
Expand Down Expand Up @@ -66,3 +73,18 @@ export function tuiEditLinkParseUrl(url = ''): TuiEditLinkParsed {

return {prefix, path: prefix === '' ? url : path};
}

/**
* @internal
*/
function isValidUrl(url: string): boolean {
return new RegExp(
String.raw`^([a-zA-Z]+:\/\/)?` + // protocol
String.raw`((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|localhost|` + // domain name
String.raw`((\d{1,3}\.){3}\d{1,3}))` + // OR IP (v4) address
String.raw`(\:\d+)?(\/[-a-z\d%_.~+\:]*)*` + // port and path
String.raw`(\?[)(;&a-z\d%_.~+=-]*)?` + // query string
String.raw`(\#[-a-z\d_]*)?$`, // fragment locator
'i',
).test(url);
}
9 changes: 9 additions & 0 deletions projects/editor/test/edit-link-parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,13 @@ describe('tuiEditLinkParseUrl', () => {
path: 'hello/world',
});
});

it('nested', () => {
expect(
tuiEditLinkParseUrl('https://google.com?utm_source=https://yahoo.com'),
).toEqual({
prefix: 'https://',
path: 'google.com?utm_source=https://yahoo.com',
});
});
});

0 comments on commit e071abb

Please sign in to comment.