Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement potentially strip spaces for opaque paths #813

Merged
merged 1 commit into from
Feb 20, 2023
Merged
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
32 changes: 31 additions & 1 deletion url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,32 @@ 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 @@ -1434,7 +1460,8 @@ impl Url {
self.serialization.push('#');
self.mutate(|parser| parser.parse_fragment(parser::Input::no_trim(input)))
} else {
self.fragment_start = None
self.fragment_start = None;
self.strip_trailing_spaces_from_opaque_path();
}
}

Expand Down Expand Up @@ -1497,6 +1524,9 @@ impl Url {
parser::Input::trim_tab_and_newlines(input, vfn),
)
});
} else {
self.query_start = None;
self.strip_trailing_spaces_from_opaque_path();
}

self.restore_already_parsed_fragment(fragment);
Expand Down
11 changes: 11 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ 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