Skip to content

Commit 4464840

Browse files
committed
Comments and nits fixups.
1 parent 925ec94 commit 4464840

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2058,12 +2058,12 @@ impl Url {
20582058
let new_scheme_type = SchemeType::from(&parser.serialization);
20592059
let old_scheme_type = SchemeType::from(self.scheme());
20602060
// If url’s scheme is a special scheme and buffer is not a special scheme, then return.
2061-
if new_scheme_type.is_special() && !old_scheme_type.is_special() ||
2061+
if (new_scheme_type.is_special() && !old_scheme_type.is_special()) ||
20622062
// If url’s scheme is not a special scheme and buffer is a special scheme, then return.
2063-
!new_scheme_type.is_special() && old_scheme_type.is_special() ||
2063+
(!new_scheme_type.is_special() && old_scheme_type.is_special()) ||
20642064
// If url includes credentials or has a non-null port, and buffer is "file", then return.
20652065
// If url’s scheme is "file" and its host is an empty host or null, then return.
2066-
new_scheme_type.is_file() && self.has_authority()
2066+
(new_scheme_type.is_file() && self.has_authority())
20672067
{
20682068
return Err(());
20692069
}
@@ -2095,8 +2095,8 @@ impl Url {
20952095

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

@@ -2575,7 +2575,7 @@ fn file_url_segments_to_pathbuf(
25752575
}
25762576
// A windows drive letter must end with a slash.
25772577
if bytes.len() > 2 {
2578-
if matches!(bytes[bytes.len() -2], b'a'..=b'z' | b'A'..=b'Z')
2578+
if matches!(bytes[bytes.len() - 2], b'a'..=b'z' | b'A'..=b'Z')
25792579
&& matches!(bytes[bytes.len() - 1], b':' | b'|')
25802580
{
25812581
bytes.push(b'/');

src/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ impl<'a> Parser<'a> {
908908
// url is special and c is U+005C (\)
909909
// If @ flag is set and buffer is the empty string, validation error, return failure.
910910
if let (Some(c), _) = remaining.split_first() {
911-
if c == '/' || c == '?' || c == '#' || scheme_type.is_special() && c == '\\' {
911+
if c == '/' || c == '?' || c == '#' || (scheme_type.is_special() && c == '\\') {
912912
return Err(ParseError::EmptyHost);
913913
}
914914
}

src/quirks.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
157157
if url.cannot_be_a_base() {
158158
return Err(());
159159
}
160-
// Host parsing rules are strict,
161-
// We don't want to trim the input
160+
// Host parsing rules are strict we don't want to trim the input
162161
let input = Input::no_trim(new_hostname);
163162
let scheme_type = SchemeType::from(url.scheme());
164163
if let Ok((host, _remaining)) = Parser::parse_host(input, scheme_type) {
@@ -168,7 +167,7 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
168167
if SchemeType::from(url.scheme()) == SchemeType::SpecialNotFile
169168
// Port with an empty host
170169
||!port(&url).is_empty()
171-
// Empty host with includes credentials
170+
// Empty host that includes credentials
172171
|| !url.username().is_empty()
173172
|| !url.password().unwrap_or(&"").is_empty()
174173
{
@@ -224,9 +223,9 @@ pub fn set_pathname(url: &mut Url, new_pathname: &str) {
224223
return;
225224
}
226225
if Some('/') == new_pathname.chars().nth(0)
227-
|| SchemeType::from(url.scheme()).is_special()
228-
// \ is a segment delimiter for 'special' URLs"
229-
&& Some('\\') == new_pathname.chars().nth(0)
226+
|| (SchemeType::from(url.scheme()).is_special()
227+
// \ is a segment delimiter for 'special' URLs"
228+
&& Some('\\') == new_pathname.chars().nth(0))
230229
{
231230
url.set_path(new_pathname)
232231
} else {

tests/unit.rs

+29
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ fn test_relative_empty() {
3737
assert_eq!(url.as_str(), "sc://%C3%B1");
3838
}
3939

40+
#[test]
41+
fn test_set_empty_host() {
42+
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap();
43+
base.set_username("").unwrap();
44+
assert_eq!(base.as_str(), "moz://:bar@servo/baz");
45+
base.set_host(None).unwrap();
46+
assert_eq!(base.as_str(), "moz:/baz");
47+
base.set_host(Some("servo")).unwrap();
48+
assert_eq!(base.as_str(), "moz://servo/baz");
49+
}
50+
51+
#[test]
52+
fn test_set_empty_hostname() {
53+
use url::quirks;
54+
let mut base: Url = "moz://foo@servo/baz".parse().unwrap();
55+
assert!(
56+
quirks::set_hostname(&mut base, "").is_err(),
57+
"setting an empty hostname to a url with a username should fail"
58+
);
59+
base = "moz://:pass@servo/baz".parse().unwrap();
60+
assert!(
61+
quirks::set_hostname(&mut base, "").is_err(),
62+
"setting an empty hostname to a url with a password should fail"
63+
);
64+
base = "moz://servo/baz".parse().unwrap();
65+
quirks::set_hostname(&mut base, "").unwrap();
66+
assert_eq!(base.as_str(), "moz:///baz");
67+
}
68+
4069
macro_rules! assert_from_file_path {
4170
($path: expr) => {
4271
assert_from_file_path!($path, $path)

0 commit comments

Comments
 (0)