Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: doc and test URLSearchParams discrepancy #33236

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions doc/api/url.md
Expand Up @@ -472,9 +472,27 @@ and [`url.format()`][] methods would produce.
* {URLSearchParams}

Gets the [`URLSearchParams`][] object representing the query parameters of the
URL. This property is read-only; to replace the entirety of query parameters of
the URL, use the [`url.search`][] setter. See [`URLSearchParams`][]
documentation for details.
URL. This property is read-only but the `URLSearchParams` object it provides
can be used to mutate the URL instance; to replace the entirety of query
parameters of the URL, use the [`url.search`][] setter. See
[`URLSearchParams`][] documentation for details.

Care must be taken when using `.searchParams` to modify the `URL` because,
Trott marked this conversation as resolved.
Show resolved Hide resolved
per the WHATWG specification, the `URLSearchParams` object uses slightly
Trott marked this conversation as resolved.
Show resolved Hide resolved
different rules to determine which characters should be percent-encoded. For
Trott marked this conversation as resolved.
Show resolved Hide resolved
instance, the `URL` object will not percent encode the ASCII tilde (`~`)
character, while `URLSearchParams` will always encode it:

```js
const myUrl = new URL('https://example.org/abc?foo=~bar');

console.log(myUrl.search); // prints ?foo=~bar

// Modify the URL via searchParams...
myUrl.searchParams.sort();

console.log(myUrl.search); // prints ?foo=%7Ebar
```

#### `url.username`

Expand Down
Expand Up @@ -16,3 +16,12 @@ const URLSearchParams = require('url').URLSearchParams;
message: 'Value of "this" must be of type URLSearchParams'
});
}

// The URLSearchParams stringifier mutates the base URL using
// different percent-encoding rules than the URL itself.
{
const myUrl = new URL('https://example.org?foo=~bar');
assert.strictEqual(myUrl.search, '?foo=~bar');
myUrl.searchParams.sort();
assert.strictEqual(myUrl.search, '?foo=%7Ebar');
}