|
| 1 | +| Title | Implement WHATWG URL Spec | |
| 2 | +|--------|-----------------------------| |
| 3 | +| Author | @jasnell | |
| 4 | +| Status | DRAFT | |
| 5 | +| Date | 2016-06-30T09:00:00-07:00 | |
| 6 | + |
| 7 | +## Description |
| 8 | + |
| 9 | +The WHATWG URL Standard specifies updated syntax, parsing and serialization of |
| 10 | +URLs as currently implemented by the main Web Browsers. The existing Node.js |
| 11 | +`url` module parsing and serialization implementation currently does not support |
| 12 | +the URL standard and fails to pass 160 of the WHATWG URL parsing tests. |
| 13 | + |
| 14 | +This proposal is to implement the WHATWG URL Standard by modifying the existing |
| 15 | +`url` module to provide an implementation of the `URL` object and associated |
| 16 | +APIs. Doing so improves the robustness of URL parsing, provides consistency |
| 17 | +with browser js code, and can eventually allow the reduction of node.js specific |
| 18 | +APIs. |
| 19 | + |
| 20 | +Initially, the implementation would be introduced as an undocumented |
| 21 | +experimental feature exposed via a new `URL` property in the `url` module. |
| 22 | + |
| 23 | +Because the existing `require('url')` module remains, there should be no |
| 24 | +backwards compatibility concerns. Once a decision is made to provide official, |
| 25 | +documented support for the new URL implementation, a decision would be made |
| 26 | +about whether to switch the internal uses of `require('url').parse()` to the |
| 27 | +new `URL` implementation. |
| 28 | + |
| 29 | +The current implementation can be found at: |
| 30 | + |
| 31 | +https://github.com/nodejs/node/pull/7448 |
| 32 | + |
| 33 | +## Example |
| 34 | + |
| 35 | +```js |
| 36 | +const URL = require('url').URL; |
| 37 | +const url = new URL('http://user:pass@example.org:1234/p/a/t/h?xyz=abc#hash'); |
| 38 | + |
| 39 | +console.log(url.protocol); // http: |
| 40 | +console.log(url.username); // user |
| 41 | +console.log(url.password); // password |
| 42 | +console.log(url.host); // example.org:1234 |
| 43 | +console.log(url.hostname); // example.org |
| 44 | +console.log(url.port); // 1234 |
| 45 | +console.log(url.pathname); // /p/a/t/h |
| 46 | +console.log(url.search); // ?xyz=abc |
| 47 | +console.log(url.searchParams); // SearchParams object |
| 48 | +console.log(url.hash); // hash |
| 49 | + |
| 50 | +// The SearchParams object is defined by the WHATWG spec also |
| 51 | +url.searchParams.append('key', 'value'); |
| 52 | + |
| 53 | +console.log(url); |
| 54 | + // http://user:pass@example.org:1234/p/a/t/h?xyz=abc&key=value#hash |
| 55 | + |
| 56 | + |
| 57 | +// Example using a base URL |
| 58 | +const url2 = new URL('/foo', url); |
| 59 | +console.log(url.protocol); // http: |
| 60 | +console.log(url.username); // user |
| 61 | +console.log(url.password); // password |
| 62 | +console.log(url.host); // example.org:1234 |
| 63 | +console.log(url.hostname); // example.org |
| 64 | +console.log(url.port); // 1234 |
| 65 | +console.log(url.pathname); // /foo |
| 66 | +console.log(url.search); // '' |
| 67 | +console.log(url.searchParams); // SearchParams object |
| 68 | +console.log(url.hash); // '' |
| 69 | +``` |
| 70 | + |
| 71 | +## APIs |
| 72 | + |
| 73 | +The public API would be as defined by the |
| 74 | +[WHATWG spec](https://url.spec.whatwg.org/#api). |
| 75 | + |
| 76 | +### `new URL(href, base)` |
| 77 | + |
| 78 | +The constructor implements the WHATWG basic parsing algorithm. Accessible via |
| 79 | +`const URL = require('url').URL` |
| 80 | + |
| 81 | +* `href` is a `string` containing the URL to parse. |
| 82 | +* `base` is either a `string` or a `URL` that contains the base URL to resolve |
| 83 | + against while parsing. |
| 84 | + |
| 85 | +See https://url.spec.whatwg.org/#urlutils-members for detail on the properties |
| 86 | +of the `URL` object. |
| 87 | + |
| 88 | +#### `url.protocol` |
| 89 | + |
| 90 | +* Returns a string |
| 91 | +* Getter/Setter |
| 92 | + |
| 93 | +#### `url.username` |
| 94 | + |
| 95 | +* Returns a string |
| 96 | +* Getter/Setter |
| 97 | + |
| 98 | +#### `url.password` |
| 99 | + |
| 100 | +* Returns a string |
| 101 | +* Getter/Setter |
| 102 | + |
| 103 | +#### `url.host` |
| 104 | + |
| 105 | +* Returns a string |
| 106 | +* Getter/Setter |
| 107 | + |
| 108 | +#### `url.hostname` |
| 109 | + |
| 110 | +* Returns a string |
| 111 | +* Getter/Setter |
| 112 | + |
| 113 | +#### `url.port` |
| 114 | + |
| 115 | +* Returns an integer |
| 116 | +* Getter/Setter |
| 117 | + |
| 118 | +#### `url.pathname` |
| 119 | + |
| 120 | +* Returns a string |
| 121 | +* Getter/Setter |
| 122 | + |
| 123 | +#### `url.search` |
| 124 | + |
| 125 | +* Returns a string |
| 126 | +* Getter/Setter |
| 127 | + |
| 128 | +#### `url.searchParams` |
| 129 | + |
| 130 | +* Returns a URLSearchParams object |
| 131 | +* Getter |
| 132 | + |
| 133 | +#### `url.hash` |
| 134 | + |
| 135 | +* Returns a string |
| 136 | +* Getter/Setter |
| 137 | + |
| 138 | +#### `url.origin` |
| 139 | + |
| 140 | +* Returns a string |
| 141 | +* Getter |
| 142 | + |
| 143 | +#### `url.href` |
| 144 | + |
| 145 | +* Returns a string |
| 146 | +* Getter |
| 147 | + |
| 148 | +#### `url.toString()` |
| 149 | + |
| 150 | +* Returns a string (same as `url.href`) |
| 151 | + |
| 152 | +### `URLSearchParams` |
| 153 | + |
| 154 | +Returned by the `url.searchParams` property and provides access read and write |
| 155 | +access to the search params. It is defined at |
| 156 | +https://url.spec.whatwg.org/#interface-urlsearchparams. |
| 157 | + |
| 158 | +Accessible via `require('url').URLSearchParams` |
| 159 | + |
| 160 | +#### `searchParams.append(name, value)` |
| 161 | +#### `searchParams.delete(name)` |
| 162 | +#### `searchParams.get(name)` |
| 163 | +#### `searchParams.getAll(name)` |
| 164 | +#### `searchParams.has(name)` |
| 165 | +#### `searchParams.set(name, value)` |
| 166 | +#### `searchParams.*[Symbol.iterator]()` |
| 167 | +#### `searchParams.toString()` |
| 168 | + |
| 169 | +### `URL.domainToASCII(domain)` |
| 170 | +### `URL.domainToUnicode(domain)` |
0 commit comments