Skip to content

Commit 0a37870

Browse files
silverwindGiteaBot
authored andcommitted
Fix gitea-origin-url with default ports (go-gitea#29085)
When setting `url.host` on a URL object with no port specified (like is the case of default port), the resulting URL's port will not change. Workaround this quirk in the URL standard by explicitely setting port for the http and https protocols. Extracted the logic to a function for the purpose of testing. Initially I wanted to have the function in utils.js, but it turns out esbuild can not treeshake the unused functions which would result in the webcomponents chunk having all 2kB utils.js inlined, so it seemed not worth. Fixes: go-gitea#29084
1 parent c9b2aae commit 0a37870

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

.eslintrc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ rules:
743743
wc/no-constructor-params: [2]
744744
wc/no-constructor: [2]
745745
wc/no-customized-built-in-elements: [2]
746-
wc/no-exports-with-element: [2]
746+
wc/no-exports-with-element: [0]
747747
wc/no-invalid-element-name: [2]
748748
wc/no-invalid-extends: [2]
749749
wc/no-method-prefixed-with-on: [2]
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
// Convert an absolute or relative URL to an absolute URL with the current origin
2+
export function toOriginUrl(urlStr) {
3+
try {
4+
// only process absolute HTTP/HTTPS URL or relative URLs ('/xxx' or '//host/xxx')
5+
if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
6+
const {origin, protocol, hostname, port} = window.location;
7+
const url = new URL(urlStr, origin);
8+
url.protocol = protocol;
9+
url.hostname = hostname;
10+
url.port = port || (protocol === 'https:' ? '443' : '80');
11+
return url.toString();
12+
}
13+
} catch {}
14+
return urlStr;
15+
}
16+
217
window.customElements.define('gitea-origin-url', class extends HTMLElement {
318
connectedCallback() {
4-
const urlStr = this.getAttribute('data-url');
5-
try {
6-
// only process absolute HTTP/HTTPS URL or relative URLs ('/xxx' or '//host/xxx')
7-
if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
8-
const url = new URL(urlStr, window.origin);
9-
url.protocol = window.location.protocol;
10-
url.host = window.location.host;
11-
this.textContent = url.toString();
12-
return;
13-
}
14-
} catch {}
15-
this.textContent = urlStr;
19+
this.textContent = toOriginUrl(this.getAttribute('data-url'));
1620
}
1721
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {toOriginUrl} from './GiteaOriginUrl.js';
2+
3+
test('toOriginUrl', () => {
4+
const oldLocation = window.location;
5+
for (const origin of ['https://example.com', 'https://example.com:3000']) {
6+
window.location = new URL(`${origin}/`);
7+
expect(toOriginUrl('/')).toEqual(`${origin}/`);
8+
expect(toOriginUrl('/org/repo.git')).toEqual(`${origin}/org/repo.git`);
9+
expect(toOriginUrl('https://another.com')).toEqual(`${origin}/`);
10+
expect(toOriginUrl('https://another.com/')).toEqual(`${origin}/`);
11+
expect(toOriginUrl('https://another.com/org/repo.git')).toEqual(`${origin}/org/repo.git`);
12+
expect(toOriginUrl('https://another.com:4000')).toEqual(`${origin}/`);
13+
expect(toOriginUrl('https://another.com:4000/')).toEqual(`${origin}/`);
14+
expect(toOriginUrl('https://another.com:4000/org/repo.git')).toEqual(`${origin}/org/repo.git`);
15+
}
16+
window.location = oldLocation;
17+
});

0 commit comments

Comments
 (0)