Dont write empty strings in link

This commit is contained in:
Jonathan Kelley 2024-01-31 13:15:33 -08:00
parent a7a3513b3f
commit 72a40e41e6
No known key found for this signature in database
GPG key ID: 1FBB50F7EB0A08BE
2 changed files with 19 additions and 34 deletions

View file

@ -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}
}

View file

@ -74,12 +74,9 @@ fn href_internal() {
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {rel} {target}>Link</a>",
"<h1>App</h1><a {href} {default}>Link</a>",
href = r#"href="/test""#,
default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="""#,
rel = r#"rel="""#,
target = r#"target="""#
);
assert_eq!(prepare::<Route>(), expected);
@ -111,12 +108,10 @@ fn href_external() {
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {rel} {target}>Link</a>",
"<h1>App</h1><a {href} {default} {rel}>Link</a>",
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::<Route>(), expected);
@ -149,12 +144,10 @@ fn with_class() {
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {rel} {target}>Link</a>",
"<h1>App</h1><a {href} {default} {class}>Link</a>",
href = r#"href="/test""#,
default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="test_class""#,
rel = r#"rel="""#,
target = r#"target="""#
);
assert_eq!(prepare::<Route>(), expected);
@ -181,12 +174,10 @@ fn with_active_class_active() {
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {rel} {target}>Link</a>",
"<h1>App</h1><a {href} {default} {class}>Link</a>",
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::<Route>(), expected);
@ -220,12 +211,10 @@ fn with_active_class_inactive() {
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {rel} {target}>Link</a>",
"<h1>App</h1><a {href} {default} {class}>Link</a>",
href = r#"href="/test""#,
default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="test_class""#,
rel = r#"rel="""#,
target = r#"target="""#
);
assert_eq!(prepare::<Route>(), expected);
@ -258,13 +247,10 @@ fn with_id() {
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {rel} {target} {id}>Link</a>",
"<h1>App</h1><a {href} {default} {id}>Link</a>",
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::<Route>(), expected);
@ -297,11 +283,9 @@ fn with_new_tab() {
}
let expected = format!(
"<h1>App</h1><a {href} {default} {class} {rel} {target}>Link</a>",
"<h1>App</h1><a {href} {default} {target}>Link</a>",
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!(
"<h1>App</h1><a {href} {default} {class} {rel} {target}>Link</a>",
"<h1>App</h1><a {href} {default} {rel} {target}>Link</a>",
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!(
"<h1>App</h1><a {href} {default} {class} {rel} {target}>Link</a>",
"<h1>App</h1><a {href} {default} {rel}>Link</a>",
href = r#"href="/test""#,
default = r#"dioxus-prevent-default="onclick""#,
class = r#"class="""#,
rel = r#"rel="test_rel""#,
target = r#"target="""#
);
assert_eq!(prepare::<Route>(), expected);