diff --git a/url/src/lib.rs b/url/src/lib.rs
index fa2803681..3e3a4d42d 100644
--- a/url/src/lib.rs
+++ b/url/src/lib.rs
@@ -383,32 +383,6 @@ impl Url {
url
}
- /// https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path
- fn strip_trailing_spaces_from_opaque_path(&mut self) {
- if !self.cannot_be_a_base() {
- return;
- }
-
- if self.fragment_start.is_some() {
- return;
- }
-
- if self.query_start.is_some() {
- return;
- }
-
- let trailing_space_count = self
- .serialization
- .chars()
- .rev()
- .take_while(|c| *c == ' ')
- .count();
-
- let start = self.serialization.len() - trailing_space_count;
-
- self.serialization.truncate(start);
- }
-
/// Parse a string as an URL, with this URL as the base URL.
///
/// The inverse of this is [`make_relative`].
@@ -1591,7 +1565,6 @@ impl Url {
self.mutate(|parser| parser.parse_fragment(parser::Input::new_no_trim(input)))
} else {
self.fragment_start = None;
- self.strip_trailing_spaces_from_opaque_path();
}
}
@@ -1657,9 +1630,6 @@ impl Url {
});
} else {
self.query_start = None;
- if fragment.is_none() {
- self.strip_trailing_spaces_from_opaque_path();
- }
}
self.restore_already_parsed_fragment(fragment);
@@ -1759,6 +1729,13 @@ impl Url {
/// assert_eq!(url.as_str(), "https://example.com/api/some%20comments");
/// assert_eq!(url.path(), "/api/some%20comments");
///
+ /// // `set_path` will encode leading `/` and trailing ` ` in cannot-be-a-base paths.
+ /// let mut url = Url::parse("non-special: ?")?;
+ /// assert_eq!(url.path(), " %20");
+ /// url.set_query(None);
+ /// url.set_path("/ ");
+ /// assert_eq!(url.path(), "%2F %20");
+ ///
/// # Ok(())
/// # }
/// # run().unwrap();
diff --git a/url/src/parser.rs b/url/src/parser.rs
index dbdf9b906..2bd7963af 100644
--- a/url/src/parser.rs
+++ b/url/src/parser.rs
@@ -1431,6 +1431,10 @@ impl Parser<'_> {
Some(('?', _)) | Some(('#', _)) if self.context == Context::UrlParser => {
return input_before_c
}
+ Some((' ', _)) if input.starts_with('?') || input.starts_with('#') || input.is_empty() => {
+ self.serialization.push_str("%20");
+ return input;
+ }
Some((c, utf8_c)) => {
self.check_url_code_point(c, &input);
self.serialization
diff --git a/url/tests/expected_failures.txt b/url/tests/expected_failures.txt
index 8d4407c45..5fb63d339 100644
--- a/url/tests/expected_failures.txt
+++ b/url/tests/expected_failures.txt
@@ -46,11 +46,6 @@