Merge pull request #408 from leptos-rs/fix-boolean-attributes-ssr

Fix boolean attributes in `view` macro fast-path SSR
This commit is contained in:
Greg Johnston 2023-01-29 18:43:21 -05:00 committed by GitHub
commit 9a231ddef0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 10 deletions

View file

@ -49,9 +49,9 @@ impl Attribute {
/// Converts the attribute to its HTML value at that moment, not including
/// the attribute name, so it can be rendered on the server.
pub fn as_nameless_value_string(&self) -> String {
pub fn as_nameless_value_string(&self) -> Option<String> {
match self {
Attribute::String(value) => value.to_string(),
Attribute::String(value) => Some(value.to_string()),
Attribute::Fn(_, f) => {
let mut value = f();
while let Attribute::Fn(_, f) = value {
@ -59,11 +59,16 @@ impl Attribute {
}
value.as_nameless_value_string()
}
Attribute::Option(_, value) => value
.as_ref()
.map(|value| value.to_string())
.unwrap_or_default(),
Attribute::Bool(_) => String::new(),
Attribute::Option(_, value) => {
value.as_ref().map(|value| value.to_string())
}
Attribute::Bool(include) => {
if *include {
Some("".to_string())
} else {
None
}
}
}
}
}

View file

@ -435,10 +435,13 @@ fn attribute_to_tokens_ssr(
template.push_str(&value);
template.push('"');
} else {
template.push_str("=\"{}\"");
template.push_str("{}");
let value = value.as_ref();
holes.push(quote! {
leptos::escape_attr(&{#value}.into_attribute(#cx).as_nameless_value_string()),
&{#value}.into_attribute(#cx)
.as_nameless_value_string()
.map(|a| format!("{}=\"{}\"", #name, leptos::escape_attr(&a)))
.unwrap_or_default(),
})
}
}
@ -555,7 +558,9 @@ fn set_class_attribute_ssr(
template.push_str(" {}");
let value = value.as_ref();
holes.push(quote! {
leptos::escape_attr(&(cx, #value).into_attribute(#cx).as_nameless_value_string()),
&(cx, #value).into_attribute(#cx).as_nameless_value_string()
.map(|a| leptos::escape_attr(&a).to_string())
.unwrap_or_default(),
});
}
}