diff options
author | silverwind <me@silverwind.io> | 2024-02-28 16:04:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-28 15:04:04 +0000 |
commit | 82405f808d7b50c3580f26e5ca645e2ed6d284ab (patch) | |
tree | f16241dd7850b677188149673d283c430ae93b7a /templates | |
parent | 71e0f185f9773d1cc4909867a10c86f74d12ce8d (diff) | |
download | gitea-82405f808d7b50c3580f26e5ca645e2ed6d284ab.tar.gz gitea-82405f808d7b50c3580f26e5ca645e2ed6d284ab.zip |
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
Diffstat (limited to 'templates')
-rw-r--r-- | templates/repo/clone_script.tmpl | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/templates/repo/clone_script.tmpl b/templates/repo/clone_script.tmpl index 0376da4a71..40dae76dc7 100644 --- a/templates/repo/clone_script.tmpl +++ b/templates/repo/clone_script.tmpl @@ -24,14 +24,22 @@ const btn = isSSH ? sshBtn : httpsBtn; if (!btn) return; - let link = btn.getAttribute('data-link'); - if (link.startsWith('http://') || link.startsWith('https://')) { - // use current protocol/host as the clone link - const url = new URL(link); - url.protocol = window.location.protocol; - url.host = window.location.host; - link = url.toString(); + // NOTE: Keep this function in sync with the one in the js folder + 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; } + const link = toOriginUrl(btn.getAttribute('data-link')); + for (const el of document.getElementsByClassName('js-clone-url')) { el[el.nodeName === 'INPUT' ? 'value' : 'textContent'] = link; } |