-
Notifications
You must be signed in to change notification settings - Fork 478
Expand file tree
/
Copy pathurl.js
More file actions
67 lines (52 loc) · 1.76 KB
/
url.js
File metadata and controls
67 lines (52 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { config } from "./config"
export function expandURL(locatable) {
return new URL(locatable.toString(), document.baseURI)
}
export function getAnchor(url) {
let anchorMatch
if (url.hash) {
return url.hash.slice(1)
// eslint-disable-next-line no-cond-assign
} else if ((anchorMatch = url.href.match(/#(.*)$/))) {
return anchorMatch[1]
}
}
export function getAction(form, submitter) {
const action = submitter?.getAttribute("formaction") || form.getAttribute("action") || form.action
return expandURL(action)
}
export function getExtension(url) {
return (getLastPathComponent(url).match(/\.[^.]*$/) || [])[0] || ""
}
export function isPrefixedBy(baseURL, url) {
const prefix = addTrailingSlash(url.origin + url.pathname)
return addTrailingSlash(baseURL.href) === prefix || baseURL.href.startsWith(prefix)
}
export function locationIsVisitable(location, rootLocation) {
return isPrefixedBy(location, rootLocation) && !config.drive.unvisitableExtensions.has(getExtension(location))
}
export function getLocationForLink(link) {
return expandURL(link.getAttribute("href") || "")
}
export function isHashLink(element) {
return element.getAttribute("href")?.startsWith("#") ?? false
}
export function getRequestURL(url) {
const anchor = getAnchor(url)
return anchor != null ? url.href.slice(0, -(anchor.length + 1)) : url.href
}
export function toCacheKey(url) {
return getRequestURL(url)
}
export function urlsAreEqual(left, right) {
return expandURL(left).href == expandURL(right).href
}
function getPathComponents(url) {
return url.pathname.split("/").slice(1)
}
function getLastPathComponent(url) {
return getPathComponents(url).slice(-1)[0]
}
function addTrailingSlash(value) {
return value.endsWith("/") ? value : value + "/"
}