GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — doc: doc and test URLSearchParams discrepancy · nodejs/node@25443fa

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • url.md
  • test/parallel
    • test-whatwg-url-custom-searchparams-stringifier.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/url.md‎

Copy file name to clipboardExpand all lines: doc/api/url.md
+21-3Lines changed: 21 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,27 @@ and [`url.format()`][] methods would produce.
472472
* {URLSearchParams}
473473

474474
Gets the [`URLSearchParams`][] object representing the query parameters of the
475-
URL. This property is read-only; to replace the entirety of query parameters of
476-
the URL, use the [`url.search`][] setter. See [`URLSearchParams`][]
477-
documentation for details.
475+
URL. This property is read-only but the `URLSearchParams` object it provides
476+
can be used to mutate the URL instance; to replace the entirety of query
477+
parameters of the URL, use the [`url.search`][] setter. See
478+
[`URLSearchParams`][] documentation for details.
479+
480+
Use care when using `.searchParams` to modify the `URL` because,
481+
per the WHATWG specification, the `URLSearchParams` object uses
482+
different rules to determine which characters to percent-encode. For
483+
instance, the `URL` object will not percent encode the ASCII tilde (`~`)
484+
character, while `URLSearchParams` will always encode it:
485+
486+
```js
487+
const myUrl = new URL('https://example.org/abc?foo=~bar');
488+
489+
console.log(myUrl.search); // prints ?foo=~bar
490+
491+
// Modify the URL via searchParams...
492+
myUrl.searchParams.sort();
493+
494+
console.log(myUrl.search); // prints ?foo=%7Ebar
495+
```
478496

479497
#### `url.username`
480498

Collapse file

‎test/parallel/test-whatwg-url-custom-searchparams-stringifier.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-whatwg-url-custom-searchparams-stringifier.js
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ const URLSearchParams = require('url').URLSearchParams;
1616
message: 'Value of "this" must be of type URLSearchParams'
1717
});
1818
}
19+
20+
// The URLSearchParams stringifier mutates the base URL using
21+
// different percent-encoding rules than the URL itself.
22+
{
23+
const myUrl = new URL('https://example.org?foo=~bar');
24+
assert.strictEqual(myUrl.search, '?foo=~bar');
25+
myUrl.searchParams.sort();
26+
assert.strictEqual(myUrl.search, '?foo=%7Ebar');
27+
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.