From 407c925d9f614c6bb98947ed46e4ab481ebb764c Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Wed, 1 Feb 2017 09:29:12 -0800 Subject: [PATCH 1/3] url: fix setting `url.search` to the empty string Fixes: https://github.com/nodejs/node/issues/11101 Fixes: 98bb65f641 "url: improving URLSearchParams" --- lib/internal/url.js | 2 +- test/parallel/test-whatwg-url-searchparams.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index da4eb07a815c45..b140b2e8b03e0a 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -487,7 +487,7 @@ Object.defineProperties(URL.prototype, { if (!search) { ctx.query = null; ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY; - this[searchParams][searchParams] = {}; + this[searchParams][searchParams] = []; return; } if (search[0] === '?') search = search.slice(1); diff --git a/test/parallel/test-whatwg-url-searchparams.js b/test/parallel/test-whatwg-url-searchparams.js index 8b9e65123d9506..b3885a48be91e7 100644 --- a/test/parallel/test-whatwg-url-searchparams.js +++ b/test/parallel/test-whatwg-url-searchparams.js @@ -20,6 +20,10 @@ assert(sp.has('a')); assert.strictEqual(sp.get('a'), '[object Object]'); sp.delete('a'); assert(!sp.has('a')); + +m.search = ''; +assert.strictEqual(sp.toString(), ''); + values.forEach((i) => sp.append('a', i)); assert(sp.has('a')); assert.strictEqual(sp.getAll('a').length, 6); From 49b20270769c45d127e98d87d3cf69748c826c43 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Wed, 1 Feb 2017 11:34:37 -0800 Subject: [PATCH 2/3] url: do not assume searchParams data structure in search setter --- lib/internal/url.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index b140b2e8b03e0a..17534eb8260fe0 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -195,12 +195,7 @@ function onParseSearchComplete(flags, protocol, username, password, if (flags & binding.URL_FLAGS_FAILED) return; const ctx = this[context]; - if (query) { - ctx.query = query; - ctx.flags |= binding.URL_FLAGS_HAS_QUERY; - } else { - ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY; - } + ctx.query = query; } function onParseHashComplete(flags, protocol, username, password, @@ -487,13 +482,15 @@ Object.defineProperties(URL.prototype, { if (!search) { ctx.query = null; ctx.flags &= ~binding.URL_FLAGS_HAS_QUERY; - this[searchParams][searchParams] = []; - return; + } else { + if (search[0] === '?') search = search.slice(1); + ctx.query = ''; + ctx.flags |= binding.URL_FLAGS_HAS_QUERY; + if (search) { + binding.parse(search, binding.kQuery, null, ctx, + onParseSearchComplete.bind(this)); + } } - if (search[0] === '?') search = search.slice(1); - ctx.query = ''; - binding.parse(search, binding.kQuery, null, ctx, - onParseSearchComplete.bind(this)); initSearchParams(this[searchParams], search); } }, @@ -614,6 +611,10 @@ function update(url, params) { // Reused by the URL parse function invoked by // the href setter, and the URLSearchParams constructor function initSearchParams(url, init) { + if (!init) { + url[searchParams] = []; + return; + } url[searchParams] = getParamsFromObject(querystring.parse(init)); } From 320e5c489cbbdec1d051db7d5432457244b973ff Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Wed, 1 Feb 2017 22:31:42 -0800 Subject: [PATCH 3/3] remove outdated comment --- lib/internal/url.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 17534eb8260fe0..c286e49aa14294 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -608,8 +608,6 @@ function update(url, params) { } } -// Reused by the URL parse function invoked by -// the href setter, and the URLSearchParams constructor function initSearchParams(url, init) { if (!init) { url[searchParams] = [];