whatwg/url

`URLSearchParams.set()` accepts an array as value in Firefox & Chrome but this is nowhere documented

Closed this issue · 3 comments

What is the issue with the URL Standard?

As the title says, Firefox 129 and Chrome 124 seem to support set()ting a query parameter to a list of values:

const url = new URL('https://www.example.com');
url.searchParams.set('param', ['foo', 'bar', 'baz']);
console.log(url.toString()); // https://www.example.com/?param=foo%2Cbar%2Cbaz

However, I can't find anything about this feature – neither on MDN nor on the Chrome Developer blog, nor in the spec. Moreover, TypeScript's lib.dom.d.ts defines the signature of set() as

set(name: string, value: string): void;

How come this is not documented anywhere? I suppose this is not an official feature? What other browsers support this (and if so, which versions)? Would it make sense to add it to the spec?

They do not accept arrays. JavaScript just converts ["foo", "bar", "baz"] to the string foo,bar,baz which percent-encodes to foo%2Cbar%2Cbaz.

This is documented although not in the sense you would expect.

The second argument of set() is of type USVString as per https://url.spec.whatwg.org/#interface-urlsearchparams which per step 1 of https://webidl.spec.whatwg.org/#js-USVString means that through some indirection https://tc39.es/ecma262/multipage/abstract-operations.html#sec-tostring gets invoked on the value passed as second argument. Which is what you are observing.

Ahh, looks like I got bitten by JavaScript's weak typing. Thank you, and sorry for the noise!