From 72a40e41e69735739bf535557d50e23772f5963b Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 31 Jan 2024 13:15:33 -0800 Subject: [PATCH] Dont write empty strings in link --- packages/router/src/components/link.rs | 16 ++++++----- packages/router/tests/via_ssr/link.rs | 37 +++++++------------------- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/packages/router/src/components/link.rs b/packages/router/src/components/link.rs index fd3c7abd6..1645f8d0d 100644 --- a/packages/router/src/components/link.rs +++ b/packages/router/src/components/link.rs @@ -244,14 +244,18 @@ pub fn Link(props: LinkProps) -> Element { } } - let tag_target = new_tab.then_some("_blank").unwrap_or_default(); + let class = if class_.is_empty() { + None + } else { + Some(class_) + }; + + let tag_target = new_tab.then_some("_blank"); let is_external = matches!(parsed_route, NavigationTarget::External(_)); let is_router_nav = !is_external && !new_tab; let prevent_default = is_router_nav.then_some("onclick").unwrap_or_default(); - let rel = rel - .or_else(|| is_external.then_some("noopener noreferrer".to_string())) - .unwrap_or_default(); + let rel = rel.or_else(|| is_external.then_some("noopener noreferrer".to_string())); let do_default = onclick.is_none() || !onclick_only; @@ -270,9 +274,9 @@ pub fn Link(props: LinkProps) -> Element { onclick: action, href, prevent_default, - class: class_, + class, rel, - target: "{tag_target}", + target: tag_target, ..attributes, {children} } diff --git a/packages/router/tests/via_ssr/link.rs b/packages/router/tests/via_ssr/link.rs index 26907a307..3a8f93a3d 100644 --- a/packages/router/tests/via_ssr/link.rs +++ b/packages/router/tests/via_ssr/link.rs @@ -74,12 +74,9 @@ fn href_internal() { } let expected = format!( - "

App

Link", + "

App

Link", href = r#"href="/test""#, default = r#"dioxus-prevent-default="onclick""#, - class = r#"class="""#, - rel = r#"rel="""#, - target = r#"target="""# ); assert_eq!(prepare::(), expected); @@ -111,12 +108,10 @@ fn href_external() { } let expected = format!( - "

App

Link", + "

App

Link", href = r#"href="https://dioxuslabs.com/""#, default = r#"dioxus-prevent-default="""#, - class = r#"class="""#, rel = r#"rel="noopener noreferrer""#, - target = r#"target="""# ); assert_eq!(prepare::(), expected); @@ -149,12 +144,10 @@ fn with_class() { } let expected = format!( - "

App

Link", + "

App

Link", href = r#"href="/test""#, default = r#"dioxus-prevent-default="onclick""#, class = r#"class="test_class""#, - rel = r#"rel="""#, - target = r#"target="""# ); assert_eq!(prepare::(), expected); @@ -181,12 +174,10 @@ fn with_active_class_active() { } let expected = format!( - "

App

Link", + "

App

Link", href = r#"href="/""#, default = r#"dioxus-prevent-default="onclick""#, class = r#"class="test_class active_class""#, - rel = r#"rel="""#, - target = r#"target="""# ); assert_eq!(prepare::(), expected); @@ -220,12 +211,10 @@ fn with_active_class_inactive() { } let expected = format!( - "

App

Link", + "

App

Link", href = r#"href="/test""#, default = r#"dioxus-prevent-default="onclick""#, class = r#"class="test_class""#, - rel = r#"rel="""#, - target = r#"target="""# ); assert_eq!(prepare::(), expected); @@ -258,13 +247,10 @@ fn with_id() { } let expected = format!( - "

App

Link", + "

App

Link", href = r#"href="/test""#, default = r#"dioxus-prevent-default="onclick""#, - class = r#"class="""#, id = r#"id="test_id""#, - rel = r#"rel="""#, - target = r#"target="""# ); assert_eq!(prepare::(), expected); @@ -297,11 +283,9 @@ fn with_new_tab() { } let expected = format!( - "

App

Link", + "

App

Link", href = r#"href="/test""#, default = r#"dioxus-prevent-default="""#, - class = r#"class="""#, - rel = r#"rel="""#, target = r#"target="_blank""# ); @@ -328,10 +312,9 @@ fn with_new_tab_external() { } let expected = format!( - "

App

Link", + "

App

Link", href = r#"href="https://dioxuslabs.com/""#, default = r#"dioxus-prevent-default="""#, - class = r#"class="""#, rel = r#"rel="noopener noreferrer""#, target = r#"target="_blank""# ); @@ -366,12 +349,10 @@ fn with_rel() { } let expected = format!( - "

App

Link", + "

App

Link", href = r#"href="/test""#, default = r#"dioxus-prevent-default="onclick""#, - class = r#"class="""#, rel = r#"rel="test_rel""#, - target = r#"target="""# ); assert_eq!(prepare::(), expected);