Skip to content

Commit 8547871

Browse files
TimothyGuitaloacasas
authored andcommitted
url: fix setting url.search to the empty string
PR-URL: #11105 Fixes: #11101 Fixes: 98bb65f "url: improving URLSearchParams" Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent 94555c9 commit 8547871

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

lib/internal/url.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,7 @@ function onParseSearchComplete(flags, protocol, username, password,
188188
if (flags & binding.URL_FLAGS_FAILED)
189189
return;
190190
const ctx = this[context];
191-
if (query) {
192-
ctx.query = query;
193-
ctx.flags |= binding.URL_FLAGS_HAS_QUERY;
194-
} else {
195-
ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY;
196-
}
191+
ctx.query = query;
197192
}
198193

199194
function onParseHashComplete(flags, protocol, username, password,
@@ -486,13 +481,15 @@ Object.defineProperties(URL.prototype, {
486481
if (!search) {
487482
ctx.query = null;
488483
ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY;
489-
this[searchParams][searchParams] = {};
490-
return;
484+
} else {
485+
if (search[0] === '?') search = search.slice(1);
486+
ctx.query = '';
487+
ctx.flags |= binding.URL_FLAGS_HAS_QUERY;
488+
if (search) {
489+
binding.parse(search, binding.kQuery, null, ctx,
490+
onParseSearchComplete.bind(this));
491+
}
491492
}
492-
if (search[0] === '?') search = search.slice(1);
493-
ctx.query = '';
494-
binding.parse(search, binding.kQuery, null, ctx,
495-
onParseSearchComplete.bind(this));
496493
initSearchParams(this[searchParams], search);
497494
}
498495
},
@@ -610,9 +607,11 @@ function update(url, params) {
610607
}
611608
}
612609

613-
// Reused by the URL parse function invoked by
614-
// the href setter, and the URLSearchParams constructor
615610
function initSearchParams(url, init) {
611+
if (!init) {
612+
url[searchParams] = [];
613+
return;
614+
}
616615
url[searchParams] = getParamsFromObject(querystring.parse(init));
617616
}
618617

test/parallel/test-whatwg-url-searchparams.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ assert(sp.has('a'));
2020
assert.strictEqual(sp.get('a'), '[object Object]');
2121
sp.delete('a');
2222
assert(!sp.has('a'));
23+
24+
m.search = '';
25+
assert.strictEqual(sp.toString(), '');
26+
2327
values.forEach((i) => sp.append('a', i));
2428
assert(sp.has('a'));
2529
assert.strictEqual(sp.getAll('a').length, 6);

0 commit comments

Comments
 (0)