|
1 | 1 | const assert = require('assert')
|
2 | 2 | const { atob } = require('buffer')
|
| 3 | +const { format } = require('url') |
3 | 4 | const { isValidHTTPToken, isomorphicDecode } = require('./util')
|
4 | 5 |
|
5 | 6 | const encoder = new TextEncoder()
|
@@ -111,73 +112,7 @@ function dataURLProcessor (dataURL) {
|
111 | 112 | * @param {boolean} excludeFragment
|
112 | 113 | */
|
113 | 114 | function URLSerializer (url, excludeFragment = false) {
|
114 |
| - // 1. Let output be url’s scheme and U+003A (:) concatenated. |
115 |
| - let output = url.protocol |
116 |
| - |
117 |
| - // 2. If url’s host is non-null: |
118 |
| - if (url.host.length > 0) { |
119 |
| - // 1. Append "//" to output. |
120 |
| - output += '//' |
121 |
| - |
122 |
| - // 2. If url includes credentials, then: |
123 |
| - if (url.username.length > 0 || url.password.length > 0) { |
124 |
| - // 1. Append url’s username to output. |
125 |
| - output += url.username |
126 |
| - |
127 |
| - // 2. If url’s password is not the empty string, then append U+003A (:), |
128 |
| - // followed by url’s password, to output. |
129 |
| - if (url.password.length > 0) { |
130 |
| - output += ':' + url.password |
131 |
| - } |
132 |
| - |
133 |
| - // 3. Append U+0040 (@) to output. |
134 |
| - output += '@' |
135 |
| - } |
136 |
| - |
137 |
| - // 3. Append url’s host, serialized, to output. |
138 |
| - output += decodeURIComponent(url.hostname) |
139 |
| - |
140 |
| - // 4. If url’s port is non-null, append U+003A (:) followed by url’s port, |
141 |
| - // serialized, to output. |
142 |
| - if (url.port.length > 0) { |
143 |
| - output += ':' + url.port |
144 |
| - } |
145 |
| - } |
146 |
| - |
147 |
| - // 3. If url’s host is null, url does not have an opaque path, |
148 |
| - // url’s path’s size is greater than 1, and url’s path[0] |
149 |
| - // is the empty string, then append U+002F (/) followed by |
150 |
| - // U+002E (.) to output. |
151 |
| - // Note: This prevents web+demo:/.//not-a-host/ or web+demo:/path/..//not-a-host/, |
152 |
| - // when parsed and then serialized, from ending up as web+demo://not-a-host/ |
153 |
| - // (they end up as web+demo:/.//not-a-host/). |
154 |
| - // Undici implementation note: url's path[0] can never be an |
155 |
| - // empty string, so we have to slightly alter what the spec says. |
156 |
| - if ( |
157 |
| - url.host.length === 0 && |
158 |
| - url.pathname.length > 1 && |
159 |
| - url.href.slice(url.protocol.length + 1)[0] === '.' |
160 |
| - ) { |
161 |
| - output += '/.' |
162 |
| - } |
163 |
| - |
164 |
| - // 4. Append the result of URL path serializing url to output. |
165 |
| - output += url.pathname |
166 |
| - |
167 |
| - // 5. If url’s query is non-null, append U+003F (?), |
168 |
| - // followed by url’s query, to output. |
169 |
| - if (url.search.length > 0) { |
170 |
| - output += url.search |
171 |
| - } |
172 |
| - |
173 |
| - // 6. If exclude fragment is false and url’s fragment is non-null, |
174 |
| - // then append U+0023 (#), followed by url’s fragment, to output. |
175 |
| - if (excludeFragment === false && url.hash.length > 0) { |
176 |
| - output += url.hash |
177 |
| - } |
178 |
| - |
179 |
| - // 7. Return output. |
180 |
| - return output |
| 115 | + return format(url, { fragment: !excludeFragment }) |
181 | 116 | }
|
182 | 117 |
|
183 | 118 | // https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
|
|
0 commit comments