From 69772240594d6f4f9372941229a6daeb85096b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Wed, 28 Dec 2016 10:11:26 +0100 Subject: [PATCH] url: fix URL query update if searchParams changes If searchParams becomes empty, query must be set to null. Add missing update of context flags. Fixes: https://github.com/nodejs/node/issues/10480 PR-URL: https://github.com/nodejs/node/pull/10486 Reviewed-By: James M Snell --- lib/internal/url.js | 10 +++++++++- test/parallel/test-whatwg-url-searchparams-delete.js | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index ef22d2d4943f67..d7f859d95f0580 100755 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -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) { diff --git a/test/parallel/test-whatwg-url-searchparams-delete.js b/test/parallel/test-whatwg-url-searchparams-delete.js index bdccf49baf1cc4..b08a3b76867572 100644 --- a/test/parallel/test-whatwg-url-searchparams-delete.js +++ b/test/parallel/test-whatwg-url-searchparams-delete.js @@ -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/'); +}