fix: sync SvelteURL port when protocol setter clears it (#18705)

Per the WHATWG URL spec, assigning a new protocol can clear the URL's port when the current port equals the new scheme's default port. Therefore also `set` the port when the protocol is updated.
pull/18710/head
Jawad Ali 4 days ago committed by GitHub
parent 6b16b7f73f
commit c894afd8c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: sync `SvelteURL` port signal when the protocol setter clears the port

@ -163,6 +163,8 @@ export class SvelteURL extends URL {
set protocol(value) { set protocol(value) {
super.protocol = value; super.protocol = value;
set(this.#protocol, super.protocol); set(this.#protocol, super.protocol);
// changing the protocol can clear the port when it matches the new scheme's default
set(this.#port, super.port);
} }
get search() { get search() {

@ -240,3 +240,25 @@ test('url.searchParams.forEach re-runs when the search string changes via the UR
cleanup(); cleanup();
}); });
test('url.port is updated when the protocol change clears the port', () => {
const url = new SvelteURL('http://example.com:443/');
const log: any = [];
const cleanup = effect_root(() => {
render_effect(() => {
log.push(url.port);
});
});
flushSync(() => {
// 443 is the default port for https, so it gets stripped
url.protocol = 'https:';
});
assert.equal(url.port, '');
assert.equal(url.href, 'https://example.com/');
assert.deepEqual(log, ['443', '']);
cleanup();
});

Loading…
Cancel
Save