`Location::query_map`: support repeated key (#4183)
This adds support for parsing e.g. `?foo=hello&foo=world`, returning both "hello" and "world" in `Location::query_map`
This commit is contained in:
parent
3258cd2a7f
commit
820fa3c43a
|
|
@ -758,8 +758,8 @@ pub struct Location {
|
||||||
|
|
||||||
/// The parsed "query" part of "www.example.com/index.html?query#fragment".
|
/// The parsed "query" part of "www.example.com/index.html?query#fragment".
|
||||||
///
|
///
|
||||||
/// "foo=42&bar%20" is parsed as `{"foo": "42", "bar ": ""}`
|
/// "foo=hello&bar%20&foo=world" is parsed as `{"bar ": [""], "foo": ["hello", "world"]}`
|
||||||
pub query_map: std::collections::BTreeMap<String, String>,
|
pub query_map: std::collections::BTreeMap<String, Vec<String>>,
|
||||||
|
|
||||||
/// `location.origin`
|
/// `location.origin`
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -117,21 +117,24 @@ pub fn web_location() -> epi::Location {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// query is percent-encoded
|
/// query is percent-encoded
|
||||||
fn parse_query_map(query: &str) -> BTreeMap<String, String> {
|
fn parse_query_map(query: &str) -> BTreeMap<String, Vec<String>> {
|
||||||
query
|
let mut map: BTreeMap<String, Vec<String>> = Default::default();
|
||||||
.split('&')
|
|
||||||
.filter_map(|pair| {
|
for pair in query.split('&') {
|
||||||
if pair.is_empty() {
|
if !pair.is_empty() {
|
||||||
None
|
if let Some((key, value)) = pair.split_once('=') {
|
||||||
|
map.entry(percent_decode(key))
|
||||||
|
.or_default()
|
||||||
|
.push(percent_decode(value));
|
||||||
} else {
|
} else {
|
||||||
Some(if let Some((key, value)) = pair.split_once('=') {
|
map.entry(percent_decode(pair))
|
||||||
(percent_decode(key), percent_decode(value))
|
.or_default()
|
||||||
} else {
|
.push(String::new());
|
||||||
(percent_decode(pair), String::new())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
.collect()
|
}
|
||||||
|
|
||||||
|
map
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(emilk): this test is never acgtually run, because this whole module is wasm32 only 🤦♂️
|
// TODO(emilk): this test is never acgtually run, because this whole module is wasm32 only 🤦♂️
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue