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: nodejs#10480
PR-URL: nodejs#10486
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
targos committed Jan 1, 2017
1 parent db18dd8 commit b465cd0
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 b465cd0

Please sign in to comment.