@@ -26,47 +26,47 @@ backwards compatibility with existing applications. New application code
26
26
should use the WHATWG API.
27
27
28
28
A comparison between the WHATWG and Legacy APIs is provided below. Above the URL
29
- ` 'http://user:pass@sub.host .com:8080/p/a/t/h?query=string#hash' ` , properties of
30
- an object returned by the legacy ` url.parse() ` are shown. Below it are
29
+ ` 'http://user:pass@sub.example .com:8080/p/a/t/h?query=string#hash' ` , properties
30
+ of an object returned by the legacy ` url.parse() ` are shown. Below it are
31
31
properties of a WHATWG ` URL ` object.
32
32
33
33
WHATWG URL's ` origin ` property includes ` protocol ` and ` host ` , but not
34
34
` username ` or ` password ` .
35
35
36
36
``` txt
37
- ┌─────────────────────────────────────────────────────────────────────────────────────────────┐
38
- │ href │
39
- ├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤
40
- │ protocol │ │ auth │ host │ path │ hash │
41
- │ │ │ ├──────────────┬──────┼──────────┬────────────────┤ │
42
- │ │ │ │ hostname │ port │ pathname │ search │ │
43
- │ │ │ │ │ │ ├─┬──────────────┤ │
44
- │ │ │ │ │ │ │ │ query │ │
45
- " https: // user : pass @ sub.host .com : 8080 /p/a/t/h ? query=string #hash "
46
- │ │ │ │ │ hostname │ port │ │ │ │
47
- │ │ │ │ ├──────────────┴──────┤ │ │ │
48
- │ protocol │ │ username │ password │ host │ │ │ │
49
- ├──────────┴──┼──────────┴──────────┼─────────────────────┤ │ │ │
50
- │ origin │ │ origin │ pathname │ search │ hash │
51
- ├─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────────┴───────┤
52
- │ href │
53
- └─────────────────────────────────────────────────────────────────────────────────────────────┘
37
+ ┌──────────────────────────────────────────────────────────────────────────────────────────────── ┐
38
+ │ href │
39
+ ├──────────┬──┬─────────────────────┬──────────────────────── ┬───────────────────────────┬───────┤
40
+ │ protocol │ │ auth │ host │ path │ hash │
41
+ │ │ │ ├───────────────── ┬──────┼──────────┬────────────────┤ │
42
+ │ │ │ │ hostname │ port │ pathname │ search │ │
43
+ │ │ │ │ │ │ ├─┬──────────────┤ │
44
+ │ │ │ │ │ │ │ │ query │ │
45
+ " https: // user : pass @ sub.example .com : 8080 /p/a/t/h ? query=string #hash "
46
+ │ │ │ │ │ hostname │ port │ │ │ │
47
+ │ │ │ │ ├───────────────── ┴──────┤ │ │ │
48
+ │ protocol │ │ username │ password │ host │ │ │ │
49
+ ├──────────┴──┼──────────┴──────────┼──────────────────────── ┤ │ │ │
50
+ │ origin │ │ origin │ pathname │ search │ hash │
51
+ ├─────────────┴─────────────────────┴──────────────────────── ┴──────────┴────────────────┴───────┤
52
+ │ href │
53
+ └──────────────────────────────────────────────────────────────────────────────────────────────── ┘
54
54
(all spaces in the "" line should be ignored — they are purely for formatting)
55
55
```
56
56
57
57
Parsing the URL string using the WHATWG API:
58
58
59
59
``` js
60
60
const myURL =
61
- new URL (' https://user:pass@sub.host .com:8080/p/a/t/h?query=string#hash' );
61
+ new URL (' https://user:pass@sub.example .com:8080/p/a/t/h?query=string#hash' );
62
62
```
63
63
64
64
Parsing the URL string using the Legacy API:
65
65
66
66
``` js
67
67
const url = require (' url' );
68
68
const myURL =
69
- url .parse (' https://user:pass@sub.host .com:8080/p/a/t/h?query=string#hash' );
69
+ url .parse (' https://user:pass@sub.example .com:8080/p/a/t/h?query=string#hash' );
70
70
```
71
71
72
72
## The WHATWG URL API
@@ -120,8 +120,8 @@ Unicode characters appearing within the hostname of `input` will be
120
120
automatically converted to ASCII using the [ Punycode] [ ] algorithm.
121
121
122
122
``` js
123
- const myURL = new URL (' https://你好你好 ' );
124
- // https://xn--6qqa088eba /
123
+ const myURL = new URL (' https://測試 ' );
124
+ // https://xn--g6w251d /
125
125
```
126
126
127
127
This feature is only available if the ` node ` executable was compiled with
@@ -132,23 +132,23 @@ and a `base` is provided, it is advised to validate that the `origin` of
132
132
the ` URL ` object is what is expected.
133
133
134
134
``` js
135
- let myURL = new URL (' http://anotherExample.org /' , ' https://example.org/' );
136
- // http://anotherexample.org /
135
+ let myURL = new URL (' http://Example.com /' , ' https://example.org/' );
136
+ // http://example.com /
137
137
138
- myURL = new URL (' https://anotherExample.org /' , ' https://example.org/' );
139
- // https://anotherexample.org /
138
+ myURL = new URL (' https://Example.com /' , ' https://example.org/' );
139
+ // https://example.com /
140
140
141
- myURL = new URL (' foo://anotherExample.org /' , ' https://example.org/' );
142
- // foo://anotherExample.org /
141
+ myURL = new URL (' foo://Example.com /' , ' https://example.org/' );
142
+ // foo://Example.com /
143
143
144
- myURL = new URL (' http:anotherExample.org /' , ' https://example.org/' );
145
- // http://anotherexample.org /
144
+ myURL = new URL (' http:Example.com /' , ' https://example.org/' );
145
+ // http://example.com /
146
146
147
- myURL = new URL (' https:anotherExample.org /' , ' https://example.org/' );
148
- // https://example.org/anotherExample.org /
147
+ myURL = new URL (' https:Example.com /' , ' https://example.org/' );
148
+ // https://example.org/Example.com /
149
149
150
- myURL = new URL (' foo:anotherExample.org /' , ' https://example.org/' );
151
- // foo:anotherExample.org /
150
+ myURL = new URL (' foo:Example.com /' , ' https://example.org/' );
151
+ // foo:Example.com /
152
152
```
153
153
154
154
#### url.hash
@@ -249,12 +249,12 @@ console.log(myURL.origin);
249
249
```
250
250
251
251
``` js
252
- const idnURL = new URL (' https://你好你好 ' );
252
+ const idnURL = new URL (' https://測試 ' );
253
253
console .log (idnURL .origin );
254
- // Prints https://xn--6qqa088eba
254
+ // Prints https://xn--g6w251d
255
255
256
256
console .log (idnURL .hostname );
257
- // Prints xn--6qqa088eba
257
+ // Prints xn--g6w251d
258
258
```
259
259
260
260
#### url.password
@@ -929,16 +929,16 @@ any way. The `url.format(URL[, options])` method allows for basic customization
929
929
of the output.
930
930
931
931
``` js
932
- const myURL = new URL (' https://a:b@你好你好 ?abc#foo' );
932
+ const myURL = new URL (' https://a:b@測試 ?abc#foo' );
933
933
934
934
console .log (myURL .href );
935
- // Prints https://a:b@xn--6qqa088eba /?abc#foo
935
+ // Prints https://a:b@xn--g6w251d /?abc#foo
936
936
937
937
console .log (myURL .toString ());
938
- // Prints https://a:b@xn--6qqa088eba /?abc#foo
938
+ // Prints https://a:b@xn--g6w251d /?abc#foo
939
939
940
940
console .log (url .format (myURL, { fragment: false , unicode: true , auth: false }));
941
- // Prints 'https://你好你好 /?abc'
941
+ // Prints 'https://測試 /?abc'
942
942
```
943
943
944
944
### url.pathToFileURL(path)
@@ -999,21 +999,21 @@ For example: `'#hash'`.
999
999
The ` host ` property is the full lower-cased host portion of the URL, including
1000
1000
the ` port ` if specified.
1001
1001
1002
- For example: ` 'sub.host .com:8080' ` .
1002
+ For example: ` 'sub.example .com:8080' ` .
1003
1003
1004
1004
#### urlObject.hostname
1005
1005
1006
1006
The ` hostname ` property is the lower-cased host name portion of the ` host `
1007
1007
component * without* the ` port ` included.
1008
1008
1009
- For example: ` 'sub.host .com' ` .
1009
+ For example: ` 'sub.example .com' ` .
1010
1010
1011
1011
#### urlObject.href
1012
1012
1013
1013
The ` href ` property is the full URL string that was parsed with both the
1014
1014
` protocol ` and ` host ` components converted to lower-case.
1015
1015
1016
- For example: ` 'http://user:pass@sub.host .com:8080/p/a/t/h?query=string#hash' ` .
1016
+ For example: ` 'http://user:pass@sub.example .com:8080/p/a/t/h?query=string#hash' ` .
1017
1017
1018
1018
#### urlObject.path
1019
1019
@@ -1282,11 +1282,11 @@ using the [Punycode][] algorithm. Note, however, that a hostname *may* contain
1282
1282
* both* Punycode encoded and percent-encoded characters:
1283
1283
1284
1284
``` js
1285
- const myURL = new URL (' https://%CF%80.com/foo' );
1285
+ const myURL = new URL (' https://%CF%80.example. com/foo' );
1286
1286
console .log (myURL .href );
1287
- // Prints https://xn--1xa.com/foo
1287
+ // Prints https://xn--1xa.example. com/foo
1288
1288
console .log (myURL .origin );
1289
- // Prints https://π.com
1289
+ // Prints https://π.example. com
1290
1290
```
1291
1291
1292
1292
[ `Error` ] : errors.html#errors_class_error
0 commit comments