public struct JSModel
URL class described in the URL Standard.
The other APIs exposed by
WebURL are preferred over this interface, as they are designed to better match the expectations of Swift developers.
It is also subject to legacy and browser interoperability considerations which do not apply to the
The primary purpose of this API is to facilitate testing (this API is tested directly against the common web-platform-tests used by major browsers to
The main differences between the Swift
URL class are:
nilto represent not-present values, rather than using empty strings.
searchproperty, even though the "?" query delimiter is present in one of the strings and not in the other. This has secondary effects, such as
url.search = url.searchpotentially changing the serialized URL string.
WebURLmodels the former as
nil(to mean "not present"), and the latter as an empty string, to more accurately describe the structure of the URL. URLs are confusing and complex enough.
WebURLdoes not include delimiters in its components.
.searchproperty, but not part of
.queryproperty. We assume that most consumers of the query or fragment strings are going to drop this delimiter as the first thing that they do, and it also happens to match the behaviour of Foundation's
WebURLdoes not - the value you provide is the value that will be set, with percent-encoding if necessary to make it work.
WebURLdoes not filter ASCII whitespace characters in component setters.
url.search = "\t\thello\n", and the tabs and newlines will be silently removed.
WebURLpercent-encodes those characters, like it does for other disallowed characters, so they are maintained in the component's value.
WebURLdoes not ignore trailing content when setting a component.
var url = new URL("http://example.com/hello/world?weather=sunny"); url.hostname = "test.com/some/path?query"; url.href; // "http://test.com/hello/world?weather=sunny"
Continuing with the theme of
WebURLcomponent setters being stricter about setting precisely the string that you give them, this operation would fail if setting
WebURL.hostname, as "/" is a forbidden host code-point.
WebURL interface instead.
A Uniform Resource Locator (URL) is a universal identifier, which often describes the location of a resource.
public init?(_ string: String, base: String?)
Constructs a new URL by parsing
string against the given
base URL string.
nil, this is equivalent to
WebURL(string). Otherwise, it is equivalent to
public var swiftModel: WebURL
WebURL interface to this URL, with properties and functionality tailored to Swift developers.
public var description: String
public var href: String
Gets and sets the serialized URL.
public var origin: String
Gets the read-only serialization of the URL's origin.
public var username: String
Gets and sets the username portion of the URL.
public var password: String
Gets and sets the password portion of the URL.
public var scheme: String
Gets and sets the protocol portion of the URL.
public var hostname: String
Gets and sets the host name portion of the URL.
The key difference between
url.hostname is that
url.hostname does not include the port.
public var host: String
Gets the host portion of the URL.
public var port: String
Gets and sets the port portion of the URL.
public var pathname: String
Gets and sets the path portion of the URL.
public var search: String
Gets and sets the serialized query portion of the URL.
public var hash: String
Gets and sets the fragment portion of the URL.
public func hash(into hasher: inout Hasher)
public static func == (lhs: Self, rhs: Self) -> Bool