gitea/web_src/js/webcomponents/GiteaOriginUrl.js
silverwind 82405f808d
Fix URL calculation in clone input box (#29470)
Ported the function as-is and added comments so we don't forget about
this in the future.

Fixes: https://github.com/go-gitea/gitea/issues/29462
2024-02-28 15:04:04 +00:00

23 lines
859 B
JavaScript

// 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'.
// NOTE: Keep this function in sync with clone_script.tmpl
export function toOriginUrl(urlStr) {
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;
}
window.customElements.define('gitea-origin-url', class extends HTMLElement {
connectedCallback() {
this.textContent = toOriginUrl(this.getAttribute('data-url'));
}
});