|
@@ -6,7 +6,7 @@
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
// except according to those terms.
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
-//! Getters and setters for URL components implemented per https://url.spec.whatwg.org/#api
|
|
|
|
|
|
|
+//! Getters and setters for URL components implemented per <https://url.spec.whatwg.org/#api>
|
|
|
//!
|
|
//!
|
|
|
//! Unless you need to be interoperable with web browsers,
|
|
//! Unless you need to be interoperable with web browsers,
|
|
|
//! you probably want to use `Url` method instead.
|
|
//! you probably want to use `Url` method instead.
|
|
@@ -57,7 +57,7 @@ pub fn internal_components(url: &Url) -> InternalComponents {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// https://url.spec.whatwg.org/#dom-url-domaintoascii
|
|
|
|
|
|
|
+/// <https://url.spec.whatwg.org/#dom-url-domaintoascii>
|
|
|
pub fn domain_to_ascii(domain: &str) -> String {
|
|
pub fn domain_to_ascii(domain: &str) -> String {
|
|
|
match Host::parse(domain) {
|
|
match Host::parse(domain) {
|
|
|
Ok(Host::Domain(domain)) => domain,
|
|
Ok(Host::Domain(domain)) => domain,
|
|
@@ -65,7 +65,7 @@ pub fn domain_to_ascii(domain: &str) -> String {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// https://url.spec.whatwg.org/#dom-url-domaintounicode
|
|
|
|
|
|
|
+/// <https://url.spec.whatwg.org/#dom-url-domaintounicode>
|
|
|
pub fn domain_to_unicode(domain: &str) -> String {
|
|
pub fn domain_to_unicode(domain: &str) -> String {
|
|
|
match Host::parse(domain) {
|
|
match Host::parse(domain) {
|
|
|
Ok(Host::Domain(ref domain)) => {
|
|
Ok(Host::Domain(ref domain)) => {
|
|
@@ -76,29 +76,29 @@ pub fn domain_to_unicode(domain: &str) -> String {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-href
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-href>
|
|
|
pub fn href(url: &Url) -> &str {
|
|
pub fn href(url: &Url) -> &str {
|
|
|
url.as_str()
|
|
url.as_str()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Setter for https://url.spec.whatwg.org/#dom-url-href
|
|
|
|
|
|
|
+/// Setter for <https://url.spec.whatwg.org/#dom-url-href>
|
|
|
pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> {
|
|
pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> {
|
|
|
*url = Url::parse(value)?;
|
|
*url = Url::parse(value)?;
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-origin
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-origin>
|
|
|
pub fn origin(url: &Url) -> String {
|
|
pub fn origin(url: &Url) -> String {
|
|
|
url.origin().ascii_serialization()
|
|
url.origin().ascii_serialization()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-protocol
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-protocol>
|
|
|
#[inline]
|
|
#[inline]
|
|
|
pub fn protocol(url: &Url) -> &str {
|
|
pub fn protocol(url: &Url) -> &str {
|
|
|
&url.as_str()[..url.scheme().len() + ":".len()]
|
|
&url.as_str()[..url.scheme().len() + ":".len()]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Setter for https://url.spec.whatwg.org/#dom-url-protocol
|
|
|
|
|
|
|
+/// Setter for <https://url.spec.whatwg.org/#dom-url-protocol>
|
|
|
#[allow(clippy::result_unit_err)]
|
|
#[allow(clippy::result_unit_err)]
|
|
|
pub fn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> {
|
|
pub fn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> {
|
|
|
// The scheme state in the spec ignores everything after the first `:`,
|
|
// The scheme state in the spec ignores everything after the first `:`,
|
|
@@ -109,25 +109,25 @@ pub fn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> {
|
|
|
url.set_scheme(new_protocol)
|
|
url.set_scheme(new_protocol)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-username
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-username>
|
|
|
#[inline]
|
|
#[inline]
|
|
|
pub fn username(url: &Url) -> &str {
|
|
pub fn username(url: &Url) -> &str {
|
|
|
url.username()
|
|
url.username()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Setter for https://url.spec.whatwg.org/#dom-url-username
|
|
|
|
|
|
|
+/// Setter for <https://url.spec.whatwg.org/#dom-url-username>
|
|
|
#[allow(clippy::result_unit_err)]
|
|
#[allow(clippy::result_unit_err)]
|
|
|
pub fn set_username(url: &mut Url, new_username: &str) -> Result<(), ()> {
|
|
pub fn set_username(url: &mut Url, new_username: &str) -> Result<(), ()> {
|
|
|
url.set_username(new_username)
|
|
url.set_username(new_username)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-password
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-password>
|
|
|
#[inline]
|
|
#[inline]
|
|
|
pub fn password(url: &Url) -> &str {
|
|
pub fn password(url: &Url) -> &str {
|
|
|
url.password().unwrap_or("")
|
|
url.password().unwrap_or("")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Setter for https://url.spec.whatwg.org/#dom-url-password
|
|
|
|
|
|
|
+/// Setter for <https://url.spec.whatwg.org/#dom-url-password>
|
|
|
#[allow(clippy::result_unit_err)]
|
|
#[allow(clippy::result_unit_err)]
|
|
|
pub fn set_password(url: &mut Url, new_password: &str) -> Result<(), ()> {
|
|
pub fn set_password(url: &mut Url, new_password: &str) -> Result<(), ()> {
|
|
|
url.set_password(if new_password.is_empty() {
|
|
url.set_password(if new_password.is_empty() {
|
|
@@ -137,13 +137,13 @@ pub fn set_password(url: &mut Url, new_password: &str) -> Result<(), ()> {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-host
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-host>
|
|
|
#[inline]
|
|
#[inline]
|
|
|
pub fn host(url: &Url) -> &str {
|
|
pub fn host(url: &Url) -> &str {
|
|
|
&url[Position::BeforeHost..Position::AfterPort]
|
|
&url[Position::BeforeHost..Position::AfterPort]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Setter for https://url.spec.whatwg.org/#dom-url-host
|
|
|
|
|
|
|
+/// Setter for <https://url.spec.whatwg.org/#dom-url-host>
|
|
|
#[allow(clippy::result_unit_err)]
|
|
#[allow(clippy::result_unit_err)]
|
|
|
pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
|
|
pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
|
|
|
// If context object’s url’s cannot-be-a-base-URL flag is set, then return.
|
|
// If context object’s url’s cannot-be-a-base-URL flag is set, then return.
|
|
@@ -190,13 +190,13 @@ pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-hostname
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-hostname>
|
|
|
#[inline]
|
|
#[inline]
|
|
|
pub fn hostname(url: &Url) -> &str {
|
|
pub fn hostname(url: &Url) -> &str {
|
|
|
url.host_str().unwrap_or("")
|
|
url.host_str().unwrap_or("")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Setter for https://url.spec.whatwg.org/#dom-url-hostname
|
|
|
|
|
|
|
+/// Setter for <https://url.spec.whatwg.org/#dom-url-hostname>
|
|
|
#[allow(clippy::result_unit_err)]
|
|
#[allow(clippy::result_unit_err)]
|
|
|
pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
|
|
pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
|
|
|
if url.cannot_be_a_base() {
|
|
if url.cannot_be_a_base() {
|
|
@@ -232,13 +232,13 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-port
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-port>
|
|
|
#[inline]
|
|
#[inline]
|
|
|
pub fn port(url: &Url) -> &str {
|
|
pub fn port(url: &Url) -> &str {
|
|
|
&url[Position::BeforePort..Position::AfterPort]
|
|
&url[Position::BeforePort..Position::AfterPort]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Setter for https://url.spec.whatwg.org/#dom-url-port
|
|
|
|
|
|
|
+/// Setter for <https://url.spec.whatwg.org/#dom-url-port>
|
|
|
#[allow(clippy::result_unit_err)]
|
|
#[allow(clippy::result_unit_err)]
|
|
|
pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
|
|
pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
|
|
|
let result;
|
|
let result;
|
|
@@ -262,13 +262,13 @@ pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-pathname
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-pathname>
|
|
|
#[inline]
|
|
#[inline]
|
|
|
pub fn pathname(url: &Url) -> &str {
|
|
pub fn pathname(url: &Url) -> &str {
|
|
|
url.path()
|
|
url.path()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Setter for https://url.spec.whatwg.org/#dom-url-pathname
|
|
|
|
|
|
|
+/// Setter for <https://url.spec.whatwg.org/#dom-url-pathname>
|
|
|
pub fn set_pathname(url: &mut Url, new_pathname: &str) {
|
|
pub fn set_pathname(url: &mut Url, new_pathname: &str) {
|
|
|
if url.cannot_be_a_base() {
|
|
if url.cannot_be_a_base() {
|
|
|
return;
|
|
return;
|
|
@@ -291,12 +291,12 @@ pub fn set_pathname(url: &mut Url, new_pathname: &str) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-search
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-search>
|
|
|
pub fn search(url: &Url) -> &str {
|
|
pub fn search(url: &Url) -> &str {
|
|
|
trim(&url[Position::AfterPath..Position::AfterQuery])
|
|
trim(&url[Position::AfterPath..Position::AfterQuery])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Setter for https://url.spec.whatwg.org/#dom-url-search
|
|
|
|
|
|
|
+/// Setter for <https://url.spec.whatwg.org/#dom-url-search>
|
|
|
pub fn set_search(url: &mut Url, new_search: &str) {
|
|
pub fn set_search(url: &mut Url, new_search: &str) {
|
|
|
url.set_query(match new_search {
|
|
url.set_query(match new_search {
|
|
|
"" => None,
|
|
"" => None,
|
|
@@ -305,12 +305,12 @@ pub fn set_search(url: &mut Url, new_search: &str) {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Getter for https://url.spec.whatwg.org/#dom-url-hash
|
|
|
|
|
|
|
+/// Getter for <https://url.spec.whatwg.org/#dom-url-hash>
|
|
|
pub fn hash(url: &Url) -> &str {
|
|
pub fn hash(url: &Url) -> &str {
|
|
|
trim(&url[Position::AfterQuery..])
|
|
trim(&url[Position::AfterQuery..])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// Setter for https://url.spec.whatwg.org/#dom-url-hash
|
|
|
|
|
|
|
+/// Setter for <https://url.spec.whatwg.org/#dom-url-hash>
|
|
|
pub fn set_hash(url: &mut Url, new_hash: &str) {
|
|
pub fn set_hash(url: &mut Url, new_hash: &str) {
|
|
|
url.set_fragment(match new_hash {
|
|
url.set_fragment(match new_hash {
|
|
|
// If the given value is the empty string,
|
|
// If the given value is the empty string,
|