Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 7 additions & 30 deletions url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions url/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions url/tests/expected_failures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
<non-spec:/> set pathname to <//p>
<non-spec:/.//> set pathname to <p>
<file:///w|/m>
<non-special:opaque ?hi>
<non-special:opaque #hi>
<non-special:opaque \t\t \t#hi>
<non-special:opaque \t\t #hi>
<non-special:opaque\t\t \r #hi>
<foo://host/ !\"$%&\'()*+,-./:;<=>@[\\]^_`{|}~>
<wss://host/ !\"$%&\'()*+,-./:;<=>@[\\]^_`{|}~>
<///test> against <http://example.org/>
Expand All @@ -62,12 +57,4 @@
<///example.org/../path/../../path> against <http://example.org/>
</\\//\\/a/../> against <file:///>
<a:/> set pathname to <\u{0}\u{1}\t\n\r\u{1f} !\"#$%&\'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~\u{7f}\u{80}\u{81}\u{c9}\u{e9}>
<data:space #fragment> set hash to <>
<sc:space #fragment> set hash to <>
<data:space ?query#fragment> set hash to <>
<sc:space ?query#fragment> set hash to <>
<data:space ?query> set search to <>
<sc:space ?query> set search to <>
<data:space ?query#fragment> set search to <>
<sc:space ?query#fragment> set search to <>
<https://domain.com:3000> set port to <\n\n\t\t>
11 changes: 0 additions & 11 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,6 @@ fn test_relative_empty() {
assert_eq!(url.as_str(), "sc://%C3%B1");
}

#[test]
fn test_strip_trailing_spaces_from_opaque_path() {
let mut url: Url = "data:space ?query".parse().unwrap();
url.set_query(None);
assert_eq!(url.as_str(), "data:space");

let mut url: Url = "data:space #hash".parse().unwrap();
url.set_fragment(None);
assert_eq!(url.as_str(), "data:space");
}

#[test]
fn test_set_empty_host() {
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap();
Expand Down