Skip to content

Commit 6ef3876

Browse files
authored
Merge pull request servo#813 from CYBAI/strip-for-opaque
Implement potentially strip spaces for opaque paths
2 parents dbe60a4 + fdbe16c commit 6ef3876

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

url/src/lib.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,32 @@ impl Url {
322322
url
323323
}
324324

325+
/// https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path
326+
fn strip_trailing_spaces_from_opaque_path(&mut self) {
327+
if !self.cannot_be_a_base() {
328+
return;
329+
}
330+
331+
if self.fragment_start.is_some() {
332+
return;
333+
}
334+
335+
if self.query_start.is_some() {
336+
return;
337+
}
338+
339+
let trailing_space_count = self
340+
.serialization
341+
.chars()
342+
.rev()
343+
.take_while(|c| *c == ' ')
344+
.count();
345+
346+
let start = self.serialization.len() - trailing_space_count;
347+
348+
self.serialization.truncate(start);
349+
}
350+
325351
/// Parse a string as an URL, with this URL as the base URL.
326352
///
327353
/// The inverse of this is [`make_relative`].
@@ -1434,7 +1460,8 @@ impl Url {
14341460
self.serialization.push('#');
14351461
self.mutate(|parser| parser.parse_fragment(parser::Input::no_trim(input)))
14361462
} else {
1437-
self.fragment_start = None
1463+
self.fragment_start = None;
1464+
self.strip_trailing_spaces_from_opaque_path();
14381465
}
14391466
}
14401467

@@ -1497,6 +1524,9 @@ impl Url {
14971524
parser::Input::trim_tab_and_newlines(input, vfn),
14981525
)
14991526
});
1527+
} else {
1528+
self.query_start = None;
1529+
self.strip_trailing_spaces_from_opaque_path();
15001530
}
15011531

15021532
self.restore_already_parsed_fragment(fragment);

url/tests/unit.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ fn test_relative_empty() {
3434
assert_eq!(url.as_str(), "sc://%C3%B1");
3535
}
3636

37+
#[test]
38+
fn test_strip_trailing_spaces_from_opaque_path() {
39+
let mut url: Url = "data:space ?query".parse().unwrap();
40+
url.set_query(None);
41+
assert_eq!(url.as_str(), "data:space");
42+
43+
let mut url: Url = "data:space #hash".parse().unwrap();
44+
url.set_fragment(None);
45+
assert_eq!(url.as_str(), "data:space");
46+
}
47+
3748
#[test]
3849
fn test_set_empty_host() {
3950
let mut base: Url = "moz://foo:bar@servo/baz".parse().unwrap();

0 commit comments

Comments
 (0)