* Given two URLs that are assumed to be on the same * protocol/host/user/password build a relative URL from the * path, params, and hash values. * * @param rootURL The root URL that the target will be relative to. * @param targetURL The target that the relative URL points to. * @return A rootU
(rootURL, targetURL)
| 253 | * @return A rootURL-relative, normalized URL value. |
| 254 | */ |
| 255 | function computeRelativeURL(rootURL, targetURL) { |
| 256 | if (typeof rootURL === "string") rootURL = new URL(rootURL); |
| 257 | if (typeof targetURL === "string") targetURL = new URL(targetURL); |
| 258 | |
| 259 | const targetParts = targetURL.pathname.split("/"); |
| 260 | const rootParts = rootURL.pathname.split("/"); |
| 261 | |
| 262 | // If we've got a URL path ending with a "/", we remove it since we'd |
| 263 | // otherwise be relative to the wrong location. |
| 264 | if (rootParts.length > 0 && !rootParts[rootParts.length - 1]) { |
| 265 | rootParts.pop(); |
| 266 | } |
| 267 | |
| 268 | while ( |
| 269 | targetParts.length > 0 && |
| 270 | rootParts.length > 0 && |
| 271 | targetParts[0] === rootParts[0] |
| 272 | ) { |
| 273 | targetParts.shift(); |
| 274 | rootParts.shift(); |
| 275 | } |
| 276 | |
| 277 | const relativePath = rootParts |
| 278 | .map(() => "..") |
| 279 | .concat(targetParts) |
| 280 | .join("/"); |
| 281 | |
| 282 | return relativePath + targetURL.search + targetURL.hash; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Given a URL, ensure that it is treated as a directory URL. |
no test coverage detected
searching dependent graphs…