Skip to content

Commit c69f774

Browse files
committed
Fix gitea-origin-url with default ports
When setting `url.host` on a URL object with no port specified, the resulting URL's protocol 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 it. 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 of utils.js inlined. Fixes: #29084
1 parent 5c0fc90 commit c69f774

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
@@ -811,7 +811,7 @@ rules:
811811
wc/no-constructor-params: [2]
812812
wc/no-constructor: [2]
813813
wc/no-customized-built-in-elements: [2]
814-
wc/no-exports-with-element: [2]
814+
wc/no-exports-with-element: [0]
815815
wc/no-invalid-element-name: [2]
816816
wc/no-invalid-extends: [2]
817817
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 url = new URL(urlStr, window.origin);
7+
const {protocol, hostname, port} = window.location;
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)