Skip to content

Commit

Permalink
url: fix URL query update if searchParams changes
Browse files Browse the repository at this point in the history
If searchParams becomes empty, query must be set to null.
Add missing update of context flags.

Fixes: #10480
PR-URL: #10486
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
targos authored and evanlucas committed Jan 3, 2017
1 parent 711dad2 commit c66870b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,15 @@ function update(url, params) {
if (!url)
return;

url[context].query = params.toString();
const ctx = url[context];
const serializedParams = params.toString();
if (serializedParams) {
ctx.query = serializedParams;
ctx.flags |= binding.URL_FLAGS_HAS_QUERY;
} else {
ctx.query = null;
ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY;
}
}

function getSearchParamPairs(target) {
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ params.append('first', 10);
params.delete('first');
assert.strictEqual(false, params.has('first'),
'Search params object has no "first" name');

// https://github.com/nodejs/node/issues/10480
// Emptying searchParams should correctly update url's query
{
const url = new URL('http://domain?var=1&var=2&var=3');
for (const param of url.searchParams.keys()) {
url.searchParams.delete(param);
}
assert.strictEqual(url.searchParams.toString(), '');
assert.strictEqual(url.search, '');
assert.strictEqual(url.href, 'http://domain/');
}

0 comments on commit c66870b

Please sign in to comment.