Revert refactor for removing params and fragments

The refactored version was not equivalent. It could not handle
fragments containing a question mark.
See 67268ed598 (r703400238)
This commit is contained in:
Matthias 2021-09-08 00:29:30 +02:00
parent 67268ed598
commit ffab0343fc

View file

@ -3,12 +3,13 @@ use linkify::LinkFinder;
/// Remove all GET parameters from a URL.
/// The link is not a URL but a String as it may not have a base domain.
pub(crate) fn remove_get_params_and_fragment(url: &str) -> &str {
let path = match url.split_once('?') {
let path = match url.split_once('#') {
Some((path_without_fragment, _fragment)) => path_without_fragment,
None => url,
};
let path = match path.split_once('?') {
Some((path_without_params, _params)) => path_without_params,
None => match url.split_once('#') {
Some((path_without_fragment, _fragment)) => path_without_fragment,
None => url,
},
None => path,
};
path
}
@ -80,5 +81,13 @@ mod test_fs_tree {
remove_get_params_and_fragment("test.png?foo=bar#anchor"),
"test.png"
);
assert_eq!(
remove_get_params_and_fragment("test.png#anchor?anchor!?"),
"test.png"
);
assert_eq!(
remove_get_params_and_fragment("test.png?foo=bar#anchor?anchor!"),
"test.png"
);
}
}