diff --git a/url/src/lib.rs b/url/src/lib.rs
index 3cbf9ee8..4eae5ad2 100644
--- a/url/src/lib.rs
+++ b/url/src/lib.rs
@@ -1709,6 +1709,32 @@ impl Url {
let old_after_path_pos = to_u32(self.serialization.len()).unwrap();
let cannot_be_a_base = self.cannot_be_a_base();
let scheme_type = SchemeType::from(self.scheme());
+ let mut path_empty = false;
+ let mut has_host = if let Some(index) = self.serialization.find(":/") {
+ let rest = &self.serialization[(index + ":/".len())..];
+ let host_part = rest.split('/').next().unwrap_or("");
+ path_empty = rest.is_empty();
+ !host_part.is_empty() && !host_part.contains('@')
+ } else {
+ false
+ };
+
+ // Ensure the path length is greater than 1 to account
+ // for cases where "/." is already appended from serialization
+ // If we set path, then we already checked the other two conditions:
+ // 1. The host is null
+ // 2. the first segment of the URL's path is an empty string
+ // https://url.spec.whatwg.org/#url-serializing
+ if path.len() > 1 {
+ if let Some(index) = self.serialization.find(":") {
+ let removal_start = index + ":".len();
+ if self.serialization[removal_start..].starts_with("/.") {
+ self.serialization.replace_range(removal_start..removal_start + "/.".len(), "");
+ self.path_start -= "/.".len() as u32;
+ }
+ }
+ }
+
self.serialization.truncate(self.path_start as usize);
self.mutate(|parser| {
if cannot_be_a_base {
@@ -1718,7 +1744,6 @@ impl Url {
}
parser.parse_cannot_be_a_base_path(parser::Input::new_no_trim(path));
} else {
- let mut has_host = true; // FIXME
parser.parse_path_start(
scheme_type,
&mut has_host,
@@ -1726,6 +1751,21 @@ impl Url {
);
}
});
+
+ // For cases where normalization is applied across both the serialization and the path.
+ // Append "/." immediately after the scheme (up to ":")
+ // This is done if three conditions are met.
+ // https://url.spec.whatwg.org/#url-serializing
+ // 1. The host is null
+ // 2. The url's path length is greater than 1
+ // 3. the first segment of the URL's path is an empty string
+ if !has_host && path.len() > 1 && path_empty {
+ if let Some(index) = self.serialization.find("://") {
+ self.serialization.insert_str(index + ":".len(), "/.");
+ self.path_start += "/.".len() as u32;
+ }
+ }
+
self.restore_after_path(old_after_path_pos, &after_path);
}
diff --git a/url/tests/expected_failures.txt b/url/tests/expected_failures.txt
index 899e7f70..d1ed726c 100644
--- a/url/tests/expected_failures.txt
+++ b/url/tests/expected_failures.txt
@@ -43,7 +43,3 @@
set pathname to <\\\\>
set pathname to /\\/>
set pathname to /monkey/..//>
- set pathname to
- set pathname to
- set pathname to /p>
- set pathname to