diff --git a/web_src/js/features/repo-common.ts b/web_src/js/features/repo-common.ts index e8fb257c18..81dd42619f 100644 --- a/web_src/js/features/repo-common.ts +++ b/web_src/js/features/repo-common.ts @@ -4,7 +4,6 @@ import {showErrorToast} from '../modules/toast.ts'; import {sleep} from '../utils.ts'; import RepoActivityTopAuthors from '../components/RepoActivityTopAuthors.vue'; import {createApp} from 'vue'; -import {toOriginUrl} from '../utils/url.ts'; import {createTippy} from '../modules/tippy.ts'; import {localUserSettings} from '../modules/user-settings.ts'; @@ -78,7 +77,8 @@ function initCloneSchemeUrlSelection(parent: Element) { const isTea = scheme === 'tea'; if (tabHttps) { - tabHttps.textContent = window.origin.split(':')[0].toUpperCase(); // show "HTTP" or "HTTPS" + const link = tabHttps.getAttribute('data-link')!; + tabHttps.textContent = link.split(':')[0].toUpperCase(); // show "HTTP" or "HTTPS" tabHttps.classList.toggle('active', isHttps); } if (tabSsh) { @@ -98,7 +98,7 @@ function initCloneSchemeUrlSelection(parent: Element) { } if (!tab) return; - const link = toOriginUrl(tab.getAttribute('data-link')!); + const link = tab.getAttribute('data-link')!; for (const el of document.querySelectorAll('.js-clone-url')) { if (el.nodeName === 'INPUT') { diff --git a/web_src/js/utils/url.test.ts b/web_src/js/utils/url.test.ts index a2e7339375..0a7e27ca61 100644 --- a/web_src/js/utils/url.test.ts +++ b/web_src/js/utils/url.test.ts @@ -1,4 +1,4 @@ -import {linkifyURLs, pathEscape, pathEscapeSegments, toOriginUrl, urlQueryEscape} from './url.ts'; +import {linkifyURLs, pathEscape, pathEscapeSegments, urlQueryEscape} from './url.ts'; describe('escape', () => { const queryNonAscii = " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; @@ -45,19 +45,3 @@ test('linkifyURLs', () => { expect(linkifyURLs('https://evil.com/\nonclick=alert(1)')).toEqual(`${link('https://evil.com/')}\nonclick=alert(1)`); expect(linkifyURLs('https://evil.com/"onmouseover=alert(1)')).toEqual(`${link('https://evil.com/"onmouseover=alert')}(1)`); }); - -test('toOriginUrl', () => { - const oldLocation = String(window.location); - for (const origin of ['https://example.com', 'https://example.com:3000']) { - window.location.assign(`${origin}/`); - expect(toOriginUrl('/')).toEqual(`${origin}/`); - expect(toOriginUrl('/org/repo.git')).toEqual(`${origin}/org/repo.git`); - expect(toOriginUrl('https://another.com')).toEqual(`${origin}/`); - expect(toOriginUrl('https://another.com/')).toEqual(`${origin}/`); - expect(toOriginUrl('https://another.com/org/repo.git')).toEqual(`${origin}/org/repo.git`); - expect(toOriginUrl('https://another.com:4000')).toEqual(`${origin}/`); - expect(toOriginUrl('https://another.com:4000/')).toEqual(`${origin}/`); - expect(toOriginUrl('https://another.com:4000/org/repo.git')).toEqual(`${origin}/org/repo.git`); - } - window.location.assign(oldLocation); -}); diff --git a/web_src/js/utils/url.ts b/web_src/js/utils/url.ts index 927f0937a4..84328faf24 100644 --- a/web_src/js/utils/url.ts +++ b/web_src/js/utils/url.ts @@ -57,19 +57,3 @@ export function linkifyURLs(html: string): string { return `${cleanUrl}${trailing}`; // eslint-disable-line github/unescaped-html-literal }); } - -/** Convert an absolute or relative URL to an absolute URL with the current origin. It only - * processes absolute HTTP/HTTPS URLs or relative URLs like '/xxx' or '//host/xxx'. */ -export function toOriginUrl(urlStr: string) { - try { - if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) { - const {origin, protocol, hostname, port} = window.location; - const url = new URL(urlStr, origin); - url.protocol = protocol; - url.hostname = hostname; - url.port = port || (protocol === 'https:' ? '443' : '80'); - return url.toString(); - } - } catch {} - return urlStr; -}