Browse Source

url: fix setting `url.search` to the empty string

PR-URL: https://github.com/nodejs/node/pull/11105
Fixes: https://github.com/nodejs/node/issues/11101
Fixes: 98bb65f641 "url: improving URLSearchParams"
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
v6
Timothy Gu 8 years ago
parent
commit
0792d452e8
  1. 19
      lib/internal/url.js
  2. 4
      test/parallel/test-whatwg-url-searchparams.js

19
lib/internal/url.js

@ -188,12 +188,7 @@ function onParseSearchComplete(flags, protocol, username, password,
if (flags & binding.URL_FLAGS_FAILED) if (flags & binding.URL_FLAGS_FAILED)
return; return;
const ctx = this[context]; const ctx = this[context];
if (query) {
ctx.query = query; ctx.query = query;
ctx.flags |= binding.URL_FLAGS_HAS_QUERY;
} else {
ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY;
}
} }
function onParseHashComplete(flags, protocol, username, password, function onParseHashComplete(flags, protocol, username, password,
@ -486,13 +481,15 @@ Object.defineProperties(URL.prototype, {
if (!search) { if (!search) {
ctx.query = null; ctx.query = null;
ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY; ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY;
this[searchParams][searchParams] = {}; } else {
return;
}
if (search[0] === '?') search = search.slice(1); if (search[0] === '?') search = search.slice(1);
ctx.query = ''; ctx.query = '';
ctx.flags |= binding.URL_FLAGS_HAS_QUERY;
if (search) {
binding.parse(search, binding.kQuery, null, ctx, binding.parse(search, binding.kQuery, null, ctx,
onParseSearchComplete.bind(this)); onParseSearchComplete.bind(this));
}
}
initSearchParams(this[searchParams], search); initSearchParams(this[searchParams], search);
} }
}, },
@ -610,9 +607,11 @@ function update(url, params) {
} }
} }
// Reused by the URL parse function invoked by
// the href setter, and the URLSearchParams constructor
function initSearchParams(url, init) { function initSearchParams(url, init) {
if (!init) {
url[searchParams] = [];
return;
}
url[searchParams] = getParamsFromObject(querystring.parse(init)); url[searchParams] = getParamsFromObject(querystring.parse(init));
} }

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

@ -21,6 +21,10 @@ assert(sp.has('a'));
assert.strictEqual(sp.get('a'), '[object Object]'); assert.strictEqual(sp.get('a'), '[object Object]');
sp.delete('a'); sp.delete('a');
assert(!sp.has('a')); assert(!sp.has('a'));
m.search = '';
assert.strictEqual(sp.toString(), '');
values.forEach((i) => sp.append('a', i)); values.forEach((i) => sp.append('a', i));
assert(sp.has('a')); assert(sp.has('a'));
assert.strictEqual(sp.getAll('a').length, 6); assert.strictEqual(sp.getAll('a').length, 6);

Loading…
Cancel
Save