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

Encode Path Parameters in yew-router #3187

Merged
merged 13 commits into from
Jun 21, 2023
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions packages/yew-router-macro/src/routable_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,15 @@ impl Routable {
Fields::Unit => quote! { Self::#ident },
Fields::Named(field) => {
let fields = field.named.iter().map(|it| {
//named fields have idents
// named fields have idents
it.ident.as_ref().unwrap()
});
quote! { Self::#ident { #(#fields: params.get(stringify!(#fields))?.parse().ok()?,)* } }
quote! { Self::#ident { #(#fields: {
let param = params.get(stringify!(#fields))?;
let param = &*::yew_router::__macro::decode_for_url(param).ok()?;
let param = param.parse().ok()?;
param
},)* } }
}
Fields::Unnamed(_) => unreachable!(), // already checked
};
Expand Down Expand Up @@ -176,7 +181,7 @@ impl Routable {
}

quote! {
Self::#ident { #(#fields),* } => ::std::format!(#right, #(#fields = #fields),*)
Self::#ident { #(#fields),* } => ::std::format!(#right, #(#fields = ::yew_router::__macro::encode_for_url(&::std::format!("{}", #fields))),*)
}
}
Fields::Unnamed(_) => unreachable!(), // already checked
Expand Down
1 change: 1 addition & 0 deletions packages/yew-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ route-recognizer = "0.3"
serde = "1"
serde_urlencoded = "0.7.1"
tracing = "0.1.37"
urlencoding = "2.1.2"

[dependencies.web-sys]
version = "0.3"
Expand Down
2 changes: 2 additions & 0 deletions packages/yew-router/src/macro_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub use urlencoding::{decode as decode_for_url, encode as encode_for_url};

use crate::utils::strip_slash_suffix;
use crate::Routable;

Expand Down
23 changes: 23 additions & 0 deletions packages/yew-router/tests/router_unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,26 @@ fn router_trailing_slash() {
AppRoute::recognize("/category/cooking-recipes/")
);
}

#[test]
fn router_url_encoding() {
#[derive(Routable, Debug, Clone, PartialEq)]
enum AppRoute {
#[at("/")]
Root,
#[at("/search/:query")]
Search { query: String },
}

assert_eq!(
yew_router::__macro::decode_for_url("/search/a%2Fb/").unwrap(),
"/search/a/b/"
);

assert_eq!(
Some(AppRoute::Search {
query: "a/b".to_string()
}),
AppRoute::recognize("/search/a%2Fb/")
);
}
63 changes: 63 additions & 0 deletions packages/yew-router/tests/url_encoded_routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::time::Duration;

use yew::platform::time::sleep;
use yew::prelude::*;
use yew_router::prelude::*;

mod utils;
use utils::*;
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
wasm_bindgen_test_configure!(run_in_browser);

#[derive(Routable, Debug, Clone, PartialEq)]
enum AppRoute {
#[at("/")]
Root,
#[at("/search/:query")]
Search { query: String },
}

#[function_component]
fn Comp() -> Html {
let switch = move |routes: AppRoute| match routes {
AppRoute::Root => html! {
<>
<h1>{ "Root" }</h1>
<Link<AppRoute> to={AppRoute::Search { query: "a/b".to_string() }}>
{"Click me"}
</Link<AppRoute>>
</>
},
AppRoute::Search { query } => html! {
<p id="q">{ query }</p>
},
};
html! {
<Switch<AppRoute> render={switch} />
}
}

#[function_component(Root)]
fn root() -> Html {
html! {
<BrowserRouter>
<Comp />
</BrowserRouter>
}
}

#[test]
async fn url_encoded_roundtrip() {
yew::Renderer::<Root>::with_root(gloo::utils::document().get_element_by_id("output").unwrap())
.render();
sleep(Duration::ZERO).await;
click("a");
sleep(Duration::ZERO).await;
let res = obtain_result_by_id("q");
assert_eq!(res, "a/b");

assert_eq!(
gloo::utils::window().location().pathname().unwrap(),
"/search/a%2Fb"
)
}