Skip to content

Commit

Permalink
Comments and nits fixups.
Browse files Browse the repository at this point in the history
  • Loading branch information
o0Ignition0o committed Dec 9, 2019
1 parent 2103271 commit 3b394f6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2058,12 +2058,12 @@ impl Url {
let new_scheme_type = SchemeType::from(&parser.serialization);
let old_scheme_type = SchemeType::from(self.scheme());
// If url’s scheme is a special scheme and buffer is not a special scheme, then return.
if new_scheme_type.is_special() && !old_scheme_type.is_special() ||
if (new_scheme_type.is_special() && !old_scheme_type.is_special()) ||
// If url’s scheme is not a special scheme and buffer is a special scheme, then return.
!new_scheme_type.is_special() && old_scheme_type.is_special() ||
(!new_scheme_type.is_special() && old_scheme_type.is_special()) ||
// If url includes credentials or has a non-null port, and buffer is "file", then return.
// If url’s scheme is "file" and its host is an empty host or null, then return.
new_scheme_type.is_file() && self.has_authority()
(new_scheme_type.is_file() && self.has_authority())
{
return Err(());
}
Expand Down Expand Up @@ -2095,8 +2095,8 @@ impl Url {

// Update the port so it can be removed
// If it is the scheme's default
// We don't mind it silently failing
// If there was no port in the first place
// we don't mind it silently failing
// if there was no port in the first place
let previous_port = self.port();
let _ = self.set_port(previous_port);

Expand Down Expand Up @@ -2575,7 +2575,7 @@ fn file_url_segments_to_pathbuf(
}
// A windows drive letter must end with a slash.
if bytes.len() > 2 {
if matches!(bytes[bytes.len() -2], b'a'..=b'z' | b'A'..=b'Z')
if matches!(bytes[bytes.len() - 2], b'a'..=b'z' | b'A'..=b'Z')
&& matches!(bytes[bytes.len() - 1], b':' | b'|')
{
bytes.push(b'/');
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ impl<'a> Parser<'a> {
// url is special and c is U+005C (\)
// If @ flag is set and buffer is the empty string, validation error, return failure.
if let (Some(c), _) = remaining.split_first() {
if c == '/' || c == '?' || c == '#' || scheme_type.is_special() && c == '\\' {
if c == '/' || c == '?' || c == '#' || (scheme_type.is_special() && c == '\\') {
return Err(ParseError::EmptyHost);
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/quirks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
if url.cannot_be_a_base() {
return Err(());
}
// Host parsing rules are strict,
// We don't want to trim the input
// Host parsing rules are strict we don't want to trim the input
let input = Input::no_trim(new_hostname);
let scheme_type = SchemeType::from(url.scheme());
if let Ok((host, _remaining)) = Parser::parse_host(input, scheme_type) {
Expand All @@ -168,7 +167,7 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
if SchemeType::from(url.scheme()) == SchemeType::SpecialNotFile
// Port with an empty host
||!port(&url).is_empty()
// Empty host with includes credentials
// Empty host that includes credentials
|| !url.username().is_empty()
|| !url.password().unwrap_or(&"").is_empty()
{
Expand Down Expand Up @@ -224,9 +223,9 @@ pub fn set_pathname(url: &mut Url, new_pathname: &str) {
return;
}
if Some('/') == new_pathname.chars().nth(0)
|| SchemeType::from(url.scheme()).is_special()
// \ is a segment delimiter for 'special' URLs"
&& Some('\\') == new_pathname.chars().nth(0)
|| (SchemeType::from(url.scheme()).is_special()
// \ is a segment delimiter for 'special' URLs"
&& Some('\\') == new_pathname.chars().nth(0))
{
url.set_path(new_pathname)
} else {
Expand Down
29 changes: 29 additions & 0 deletions tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,35 @@ fn test_relative_empty() {
assert_eq!(url.as_str(), "sc://%C3%B1");
}

#[test]
fn test_set_empty_host() {
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap();
base.set_username("").unwrap();
assert_eq!(base.as_str(), "moz://:bar@servo/baz");
base.set_host(None).unwrap();
assert_eq!(base.as_str(), "moz:/baz");
base.set_host(Some("servo")).unwrap();
assert_eq!(base.as_str(), "moz://servo/baz");
}

#[test]
fn test_set_empty_hostname() {
use url::quirks;
let mut base: Url = "moz://foo@servo/baz".parse().unwrap();
assert!(
quirks::set_hostname(&mut base, "").is_err(),
"setting an empty hostname to a url with a username should fail"
);
base = "moz://:pass@servo/baz".parse().unwrap();
assert!(
quirks::set_hostname(&mut base, "").is_err(),
"setting an empty hostname to a url with a password should fail"
);
base = "moz://servo/baz".parse().unwrap();
quirks::set_hostname(&mut base, "").unwrap();
assert_eq!(base.as_str(), "moz:///baz");
}

macro_rules! assert_from_file_path {
($path: expr) => {
assert_from_file_path!($path, $path)
Expand Down

0 comments on commit 3b394f6

Please sign in to comment.