WHATWG URL
Elide implements the WHATWG [URL Standard][spec] URL global backed by the Servo
[url][rs-url] crate — the reference Rust implementation of the spec. This means the
parser, the host parsing (IDNA / IPv4 / IPv6), every percent-encoding code-point set, and
the per-setter basic-URL-parser state overrides all match the spec by construction.
[spec]: https://url.spec.whatwg.org/ [rs-url]: https://crates.io/crates/url
Construction
new URL(input) // absolute URL
new URL(input, base) // input resolved against baseBoth arguments are coerced to strings. Parsing failure raises TypeError.
URL(input) called without new (as a plain function) is a constructor-call error per
WebIDL — use URL.parse if you want a non-throwing variant.
Static methods
| Method | Returns | Notes | |
|---|---|---|---|
URL.parse(input, base?) | `URL \ | null` | Non-throwing — returns null instead of raising. |
URL.canParse(input, base?) | boolean | Just the parseability check; no object allocated. |
Component accessors
Every component is both a getter and a setter exceptorigin, which is read-only. Setter
semantics follow the WHATWG spec's basic URL parser with state overrides — invalid inputs
are silently ignored (e.g. setting a non-numeric port) except for href which throws
TypeError on parse failure.
| Component | Getter returns | Setter notes |
|---|---|---|
href | Full serialisation (same as toString()). | Reparses the URL — throws TypeError on failure. |
origin | ASCII-serialised tuple origin (scheme://host[:port]) for tuple-origin schemes; the literal string "null" for opaque origins (file:, data:, etc.). Always a string. | Read-only. |
protocol | Scheme plus trailing :. | Trailing : optional; silently ignored on forbidden transitions. |
username | Raw username component. | Percent-encoded via the userinfo set. |
password | Raw password component. | Empty string clears the password. |
host | hostname + : + port if a port is present. | Accepts either "host" or "host:port". |
hostname | Host portion (bracketed for IPv6). | Empty string clears the host. |
port | Stringified port or "" for the default. | Non-numeric input silently ignored; "" clears. |
pathname | Path (always starts with / for authority URLs). | Normalised per the spec (dot segments resolved). |
search | "?" + query or "". | Leading ? optional; empty string clears the query. |
searchParams | URLSearchParams — see below. | Read-only. Live-bound (see below). |
hash | "#" + fragment or "". | Leading # optional; empty string clears the fragment. |
searchParams live binding
URL.searchParams returns the same URLSearchParams object on every access for a given
URL instance (WebIDL [SameObject]), and the two stay in sync in both directions:
const u = new URL("https:<<>>
u.searchParams.append(" b", "2");
u.href; <<>>
u.search = " ?c=3"; // replaces the query
u.searchParams.size; // 1 — the previously-cached view reflects the new stateEvery mutation on the returned URLSearchParams is re-serialised and fed into
URL.search; reassigning URL.href or URL.search refreshes the cached view's pair
list from the URL's current query.
Origin serialisation
URL.origin serialises per WHATWG spec:
http/https/ws/wss/ftp:"scheme://host[:port]".file::"null"(opaque origin).- Other non-special schemes:
"null"(opaque origin).
Examples
const u = new URL("https:<<>>
u.protocol; <<>>
u.host; <<>>
u.origin; <<>>
u.pathname; <<>>
<<>>
new URL(" https://日本.jp/").hostname; // "xn--wgv71a.jp"
// IPv6 literal, brackets preserved.
new URL("http:<<>>
<<>>
URL.canParse(" ::::bad"); <<>>
URL.parse(" https://x/").href; // "https://x/"Compatibility notes
URLis installed as a global on every JavaScript realm in Elide.- Implementation is backed by the
urlRust crate, so behaviour matches Firefox / Servo and is broadly compatible with Node.js, Deno, and Chromium. - Native allocations backing each
URLhandle are released by a JVM Cleaner when the JS object becomes unreachable; no explicit.dispose()is required.