Skip to content

Commit a8e8930

Browse files
committed
Document trailing space handling in opaque URL paths
1 parent 6d36361 commit a8e8930

File tree

1 file changed

+37
-1
lines changed
  • files/en-us/web/api/url/pathname

1 file changed

+37
-1
lines changed

files/en-us/web/api/url/pathname/index.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ The **`pathname`** property of the {{domxref("URL")}} interface represents a loc
1313
HTTPS, HTTP, or other URLs with [hierarchical schemes](https://www.rfc-editor.org/rfc/rfc3986#section-1.2.3) (which the URL standard calls "[special schemes](https://url.spec.whatwg.org/#special-scheme)") always have at least one (invisible) path segment: the empty string.
1414
The `pathname` value for such URLs will therefore always have at least one `/` character.
1515

16-
For non-hierarchical schemes, if the URL has no path segments, the value of its `pathname` property will be the empty string.
16+
For non-hierarchical schemes, the pathname is known as an _opaque path_ (meaning, the URL parser does not try to split it into a list of segments). In this case, an empty path results in the `pathname` property being the empty string. Trailing spaces in opaque paths are stripped during initial parsing if the `hash` and `search` are both empty; otherwise, they are percent-encoded as `%20` even when `hash` and `search` are later set to empty strings.
17+
18+
> [!NOTE]
19+
> Percent-encoding trailing spaces in opaque paths is not widely implemented. Some browsers implement the old behavior of stripping trailing spaces from `pathname` whenever the `hash` and `search` properties are both empty strings. In these browsers, setting `hash` or `search` may change the `pathname` as well.
1720
1821
## Value
1922

@@ -58,6 +61,39 @@ const url = new URL(
5861
console.log(url.pathname); // Logs "/articles/this-that-other-outre-collection"
5962
```
6063

64+
### Pathname with opaque path
65+
66+
When the URL uses a non-hierarchical scheme, the `pathname` property behaves slightly differently. The following example shows a `data:` URL with no path at all, in which case the `pathname` is the empty string.
67+
68+
```js
69+
const url = new URL("data:");
70+
console.log(JSON.stringify(url.pathname)); // ""
71+
```
72+
73+
Browsers always strip trailing spaces from `pathname`.
74+
75+
```js
76+
const url = new URL("data:text/plain,Hello ");
77+
console.log(JSON.stringify(url.pathname)); // "text/plain,Hello"
78+
```
79+
80+
However, if the hash or search are not empty during initial parsing, the trailing space is either preserved (old behavior) or percent-encoded (new behavior).
81+
82+
```js
83+
const url = new URL("data:text/plain,Hello #frag");
84+
console.log(JSON.stringify(url.pathname)); // "text/plain,Hello " (old) or "text/plain,Hello%20" (new)
85+
```
86+
87+
If they are later set to empty strings, the trailing space is either removed (old behavior) or remains percent-encoded (new behavior).
88+
89+
```js
90+
const url = new URL("data:text/plain,Hello #frag");
91+
url.hash = "";
92+
console.log(JSON.stringify(url.pathname)); // "text/plain,Hello" (old) or "text/plain,Hello%20" (new)
93+
```
94+
95+
Both behaviors ensure that serializing and parsing the URL round-trip; that is, `new URL(url.href).href` is always equal to `url.href`. If the trailing space remains as-is after removing the hash, then `new URL()` would strip it.
96+
6197
## Specifications
6298

6399
{{Specifications}}

0 commit comments

Comments
 (0)