// Copyright 2016 The rust-url developers. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
//! Getters and setters for URL components implemented per <https://url.spec.whatwg.org/#api> //! //! Unless you need to be interoperable with web browsers, //! you probably want to use `Url` method instead.
usecrate::parser::{default_port, Context, Input, Parser, SchemeType}; usecrate::{Host, ParseError, Position, Url}; use alloc::string::String; use alloc::string::ToString;
/// Setter for <https://url.spec.whatwg.org/#dom-url-protocol> #[allow(clippy::result_unit_err)] pubfn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> { // The scheme state in the spec ignores everything after the first `:`, // but `set_scheme` errors if there is more. iflet Some(position) = new_protocol.find(':') {
new_protocol = &new_protocol[..position];
}
url.set_scheme(new_protocol)
}
/// Setter for <https://url.spec.whatwg.org/#dom-url-host> #[allow(clippy::result_unit_err)] pubfn 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 url.cannot_be_a_base() { return Err(());
} // Host parsing rules are strict, // We don't want to trim the input let input = Input::new_no_trim(new_host); let host; let opt_port;
{ let scheme = url.scheme(); let scheme_type = SchemeType::from(scheme); if scheme_type == SchemeType::File && new_host.is_empty() {
url.set_host_internal(Host::Domain(String::new()), None); return Ok(());
}
iflet Ok((h, remaining)) = Parser::parse_host(input, scheme_type) {
host = h;
opt_port = iflet Some(remaining) = remaining.split_prefix(':') { if remaining.is_empty() {
None
} else {
Parser::parse_port(remaining, || default_port(scheme), Context::Setter)
.ok()
.map(|(port, _remaining)| port)
}
} else {
None
};
} else { return Err(());
}
} // Make sure we won't set an empty host to a url with a username or a port if host == Host::Domain("".to_string())
&& (!username(url).is_empty() || matches!(opt_port, Some(Some(_))) || url.port().is_some())
{ return Err(());
}
url.set_host_internal(host, opt_port);
Ok(())
}
/// Setter for <https://url.spec.whatwg.org/#dom-url-hash> pubfn set_hash(url: &mut Url, new_hash: &str) {
url.set_fragment(match new_hash { // If the given value is the empty string, // then set context object’s url’s fragment to null and return. "" => None, // Let input be the given value with a single leading U+0023 (#) removed, if any.
_ if new_hash.starts_with('#') => Some(&new_hash[1..]),
_ => Some(new_hash),
})
}
fn trim(s: &str) -> &str { if s.len() == 1 { ""
} else {
s
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.