Skip to content

Commit e838d89

Browse files
committed
don't use js snippets lol
1 parent 7d6e02e commit e838d89

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

packages/yew-router/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ route-recognizer = "0.3.0"
2424
version = "0.3"
2525
features = [
2626
"Attr",
27+
"Document",
2728
"History",
2829
"HtmlBaseElement",
2930
"Event",
3031
"NamedNodeMap",
32+
"Url",
33+
"UrlSearchParams",
3134
"Window",
3235
]

packages/yew-router/src/lib.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -254,36 +254,21 @@ fn build_path_with_base(to: &str) -> String {
254254
}
255255

256256
fn get_query_params() -> HashMap<String, String> {
257-
#[wasm_bindgen(inline_js = r#"
258-
export function params() {
259-
let entries = (new URL(document.URL)).searchParams.entries()
260-
let list = []
261-
for (const [key, value] of entries) {
262-
list.push({key, value})
263-
}
264-
return list
265-
}
266-
"#)]
267-
extern "C" {
268-
fn params() -> js_sys::Array;
269-
}
270-
271-
#[wasm_bindgen]
272-
extern "C" {
273-
#[derive(Debug)]
274-
type Param;
275-
276-
#[wasm_bindgen(getter, method)]
277-
fn key(this: &Param) -> String;
278-
279-
#[wasm_bindgen(getter, method)]
280-
fn value(this: &Param) -> String;
281-
}
257+
let url = web_sys::Url::new(&yew::utils::document().url().unwrap()).unwrap();
258+
259+
let iter = js_sys::try_iter(&JsValue::from(&url.search_params()))
260+
.expect("try_iter failed")
261+
.expect("try_iter failed")
262+
.into_iter()
263+
.map(|it| it.unwrap().unchecked_into::<js_sys::Array>().to_vec())
264+
.map(|it| {
265+
let mut iter = it.into_iter();
266+
// unwraps are unreachable
267+
// there will be at least 2 values here
268+
// both of them will be strings
269+
(iter.next().unwrap().as_string().unwrap(), iter.next().unwrap().as_string().unwrap())
270+
});
282271

283-
let iter = params().to_vec().into_iter().map(|value: JsValue| {
284-
let param = value.unchecked_into::<Param>();
285-
(param.key(), param.value())
286-
});
287272
let mut map = HashMap::new();
288273

289274
for (k, v) in iter {

0 commit comments

Comments
 (0)