diff --git a/url/src/lib.rs b/url/src/lib.rs index ad3c89001..4ae09c463 100644 --- a/url/src/lib.rs +++ b/url/src/lib.rs @@ -1552,12 +1552,12 @@ impl Url { parser::Input::trim_tab_and_newlines(input, vfn), ) }); + self.restore_already_parsed_fragment(fragment); } else { self.query_start = None; + self.restore_already_parsed_fragment(fragment); self.strip_trailing_spaces_from_opaque_path(); } - - self.restore_already_parsed_fragment(fragment); } /// Manipulate this URL’s query string, viewed as a sequence of name/value pairs diff --git a/url/tests/unit.rs b/url/tests/unit.rs index 6cb0f37fe..9c8b350ae 100644 --- a/url/tests/unit.rs +++ b/url/tests/unit.rs @@ -43,6 +43,15 @@ fn test_strip_trailing_spaces_from_opaque_path() { let mut url: Url = "data:space #hash".parse().unwrap(); url.set_fragment(None); assert_eq!(url.as_str(), "data:space"); + + // Strip spaces only when both query and fragment are null. + let mut url: Url = "data:space ?query#hash".parse().unwrap(); + url.set_query(None); + assert_eq!(url.as_str(), "data:space #hash"); + + let mut url: Url = "data:space ?query#hash".parse().unwrap(); + url.set_fragment(None); + assert_eq!(url.as_str(), "data:space ?query"); } #[test]