Liking cljdoc? Tell your friends :D

dk.simongray.whatwg-url

Parse, resolve, write and compare URLs the way browsers do, by the URL Standard.

A URL is a plain map of the parts it has:

  • :scheme, in lower case, e.g. "https"
  • :username and :password, percent-encoded, when they aren't empty
  • :host, as text, e.g. "example.com", "127.0.0.1" or "[::1]" (it can be empty, as in file:///etc/hosts)
  • :port, a number, unless it's the default of the scheme
  • :path, a vector of segments, or a string for an opaque path like that of mailto:me@example.com
  • :query and :fragment, without their ? and # (both can be empty)

Every function that takes a URL also takes its text (e.g. a string or a java.net.URI) and parses it as an absolute URL first. For text that doesn't parse, it returns nil.

The functions named after the attributes of JavaScript's URL class (e.g. hostname and pathname) return their values. To set them the way that class's setters do, use with and update.

Parse, resolve, write and compare URLs the way browsers do, by the URL
Standard.

A URL is a plain map of the parts it has:

- :scheme, in lower case, e.g. "https"
- :username and :password, percent-encoded, when they aren't empty
- :host, as text, e.g. "example.com", "127.0.0.1" or "[::1]" (it
  can be empty, as in file:///etc/hosts)
- :port, a number, unless it's the default of the scheme
- :path, a vector of segments, or a string for an opaque path like that
  of mailto:me@example.com
- :query and :fragment, without their ? and # (both can be empty)

Every function that takes a URL also takes its text (e.g. a string or a
java.net.URI) and parses it as an absolute URL first. For text that
doesn't parse, it returns nil.

The functions named after the attributes of JavaScript's URL class (e.g.
hostname and pathname) return their values. To set them the way that
class's setters do, use with and update.
raw docstring

equivalent?clj/s

(equivalent? a b)
(equivalent? a b opts)

Whether the URLs a and b have the same text, with opts:

  • :fragment?, false to compare them without their fragments
Whether the URLs `a` and `b` have the same text, with `opts`:

- :fragment?, false to compare them without their fragments
sourceraw docstring

hashclj/s

(hash url)

The fragment of the url with its #, or an empty string when the fragment is empty or there's none.

The fragment of the `url` with its #, or an empty string when the
fragment is empty or there's none.
sourceraw docstring

hostclj/s

(host url)

The host and port of the url, e.g. example.com:8443, or an empty string when it has no host.

The host and port of the `url`, e.g. example.com:8443, or an empty
string when it has no host.
sourceraw docstring

hostnameclj/s

(hostname url)

The host of the url, e.g. example.com, or an empty string when it has none.

The host of the `url`, e.g. example.com, or an empty string when it has
none.
sourceraw docstring

hrefclj/s

(href url)
(href url {:keys [fragment?] :or {fragment? true}})

The text of the url, with opts:

  • :fragment?, false to leave out the fragment

    (href {:scheme "https" :host "example.com" :path [""]}) ;; => "https://example.com/"

The text of the `url`, with `opts`:

- :fragment?, false to leave out the fragment

    (href {:scheme "https" :host "example.com" :path [""]})
    ;; => "https://example.com/"
sourceraw docstring

originclj/s

(origin url)

The origin of the url as text, e.g. https://example.com:8443, or "null" when it's opaque, e.g. for a file URL.

The origin of a blob URL is that of the URL in its path.

The origin of the `url` as text, e.g. https://example.com:8443, or
"null" when it's opaque, e.g. for a file URL.

The origin of a blob URL is that of the URL in its path.
sourceraw docstring

parseclj/s

(parse s)
(parse s base)

The URL in the text s, resolved against the URL base when given, or nil when s doesn't parse. The base is a map or text.

(parse "../c?q#f" "https://EXAMPLE.com/a/b")
;; => {:scheme "https" :host "example.com" :path ["c"]
;;     :query "q" :fragment "f"}
The URL in the text `s`, resolved against the URL `base` when given, or
nil when `s` doesn't parse. The base is a map or text.

    (parse "../c?q#f" "https://EXAMPLE.com/a/b")
    ;; => {:scheme "https" :host "example.com" :path ["c"]
    ;;     :query "q" :fragment "f"}
sourceraw docstring

passwordclj/s

(password url)

The password of the url, percent-encoded, or an empty string.

The password of the `url`, percent-encoded, or an empty string.
sourceraw docstring

path-starts-with?clj/s

(path-starts-with? url prefix)

Whether the path of the url starts with the segments of the path of prefix, the way cookies compare paths. So /a/b starts with /a, but /ab doesn't. An opaque path starts with nothing.

Whether the path of the `url` starts with the segments of the path of
`prefix`, the way cookies compare paths. So /a/b starts with /a, but /ab
doesn't. An opaque path starts with nothing.
sourceraw docstring

pathnameclj/s

(pathname url)

The path of the url as text, e.g. /a/b.

The path of the `url` as text, e.g. /a/b.
sourceraw docstring

portclj/s

(port url)

The port of the url as text, e.g. 8443, or an empty string when it's the scheme's default or there's none.

The port of the `url` as text, e.g. 8443, or an empty string when it's
the scheme's default or there's none.
sourceraw docstring

protocolclj/s

(protocol url)

The scheme of the url with a colon, e.g. https:.

The scheme of the `url` with a colon, e.g. https:.
sourceraw docstring

resolveclj/s

(resolve base s)

The text of the URL s resolved against the URL base, or nil when it doesn't parse. This is how a browser resolves a link against the URL of its page. With a nil base, only an absolute URL parses.

(resolve "https://example.com/blog/post" "../about")
;; => "https://example.com/about"

For many URLs against the same base, use resolver, which parses the base only once.

The text of the URL `s` resolved against the URL `base`, or nil when it
doesn't parse. This is how a browser resolves a link against the URL of
its page. With a nil base, only an absolute URL parses.

    (resolve "https://example.com/blog/post" "../about")
    ;; => "https://example.com/about"

For many URLs against the same base, use resolver, which parses the base
only once.
sourceraw docstring

resolverclj/s

(resolver base)

A function that resolves a URL against the URL base as resolve does, but parses the base only once, e.g. for the links of a page.

(map (resolver "https://example.com/blog/post") ["a" "/b"])
;; => ("https://example.com/blog/a" "https://example.com/b")
A function that resolves a URL against the URL `base` as resolve does,
but parses the base only once, e.g. for the links of a page.

    (map (resolver "https://example.com/blog/post") ["a" "/b"])
    ;; => ("https://example.com/blog/a" "https://example.com/b")
sourceraw docstring

same-origin?clj/s

(same-origin? a b)

Whether the URLs a and b have the same scheme, host and port. An opaque origin, e.g. of a file URL, is the same as no other.

Whether the URLs `a` and `b` have the same scheme, host and port. An
opaque origin, e.g. of a file URL, is the same as no other.
sourceraw docstring

schemeclj/s

(scheme url)

The scheme of the url, in lower case, e.g. https.

The scheme of the `url`, in lower case, e.g. https.
sourceraw docstring

(search url)

The query of the url with its ?, or an empty string when the query is empty or there's none.

The query of the `url` with its ?, or an empty string when the query is
empty or there's none.
sourceraw docstring

search-paramsclj/s

(search-params url)

The name-value pairs of the query of the url, as the params namespace takes them.

The name-value pairs of the query of the `url`, as the params namespace
takes them.
sourceraw docstring

special?clj/s

(special? url)

Whether the url has a special scheme: ftp, file, http, https, ws or wss.

Whether the `url` has a special scheme: ftp, file, http, https, ws or
wss.
sourceraw docstring

updateclj/s

(update url k f & args)

The url with the attribute k set to what f returns for its value and the args, as with sets it. The attributes are those of with.

(update "https://example.com/?q=a" :search-params params/set "page" 2)
;; => {:scheme "https" :host "example.com" :path [""]
;;     :query "q=a&page=2"}
The `url` with the attribute `k` set to what `f` returns for its value
and the `args`, as with sets it. The attributes are those of with.

    (update "https://example.com/?q=a" :search-params params/set "page" 2)
    ;; => {:scheme "https" :host "example.com" :path [""]
    ;;     :query "q=a&page=2"}
sourceraw docstring

usernameclj/s

(username url)

The username of the url, percent-encoded, or an empty string.

The username of the `url`, percent-encoded, or an empty string.
sourceraw docstring

valid?clj/s

(valid? s)
(valid? s base)

Whether the text s is a valid URL string, resolved against base: one that parses with no validation error.

Whether the text `s` is a valid URL string, resolved against `base`:
one that parses with no validation error.
sourceraw docstring

validation-errorsclj/s

(validation-errors s)
(validation-errors s base)

The validation errors of the text s as a URL resolved against base, or nil when the base doesn't parse.

The errors are keywords in the order they're found, named as in section 1.1 of the URL Standard, e.g. :invalid-credentials. When s doesn't parse, the last error is the one that made it fail (if there is one).

(validation-errors "https://user@example.org/a b")
;; => [:invalid-credentials :invalid-url-unit]
The validation errors of the text `s` as a URL resolved against `base`,
or nil when the base doesn't parse.

The errors are keywords in the order they're found, named as in section
1.1 of the URL Standard, e.g. :invalid-credentials. When `s` doesn't
parse, the last error is the one that made it fail (if there is one).

    (validation-errors "https://user@example.org/a b")
    ;; => [:invalid-credentials :invalid-url-unit]
sourceraw docstring

withclj/s

(with url k v & kvs)

The url with each attribute k set to its value v in order, as the setters of the URL class set them. A setter that refuses a value leaves the URL as it is.

The attributes :href, :protocol, :username, :password, :host, :hostname, :port, :pathname, :search and :hash take text. The attribute :search-params takes name-value pairs.

(with "https://example.com/a?b" :pathname "/c d" :search "")
;; => {:scheme "https" :host "example.com" :path ["c%20d"]}
The `url` with each attribute `k` set to its value `v` in order, as the
setters of the URL class set them. A setter that refuses a value leaves
the URL as it is.

The attributes :href, :protocol, :username, :password, :host, :hostname,
:port, :pathname, :search and :hash take text. The attribute
:search-params takes name-value pairs.

    (with "https://example.com/a?b" :pathname "/c d" :search "")
    ;; => {:scheme "https" :host "example.com" :path ["c%20d"]}
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close