Skip to content

Commit e883366

Browse files
marcopiracciniaduh95
authored andcommitted
url: fix URLSearchParams(null) to prudce null= per spec
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com> PR-URL: #63782 Fixes: #63559 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 10272a9 commit e883366

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

lib/internal/url.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ class URLSearchParams {
337337
constructor(init = undefined) {
338338
markTransferMode(this, false, false);
339339

340-
if (init == null) {
340+
if (init === undefined) {
341341
// Do nothing
342-
} else if (typeof init === 'object' || typeof init === 'function') {
342+
} else if (init !== null && (typeof init === 'object' || typeof init === 'function')) {
343343
const method = init[SymbolIterator];
344344
if (method === this[SymbolIterator] && #searchParams in init) {
345345
// While the spec does not have this branch, we can use it as a

test/parallel/test-whatwg-url-custom-searchparams-constructor.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ function makeIterableFunc(array) {
2828
let params;
2929
params = new URLSearchParams(undefined);
3030
assert.strictEqual(params.toString(), '');
31+
// Per WebIDL union resolution, null is coerced to the USVString "null".
32+
// Refs: https://url.spec.whatwg.org/#interface-urlsearchparams
3133
params = new URLSearchParams(null);
32-
assert.strictEqual(params.toString(), '');
34+
assert.strictEqual(params.toString(), 'null=');
35+
params = new URLSearchParams(false);
36+
assert.strictEqual(params.toString(), 'false=');
37+
params = new URLSearchParams(0);
38+
assert.strictEqual(params.toString(), '0=');
3339
params = new URLSearchParams(
3440
makeIterableFunc([['key', 'val'], ['key2', 'val2']])
3541
);

0 commit comments

Comments
 (0)