Skip to content

Commit

Permalink
correctly format true boolean when spreding attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptistemontan committed Oct 4, 2023
1 parent e0d15c1 commit e51b5a0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
46 changes: 46 additions & 0 deletions leptos_dom/src/macro_helpers/into_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,52 @@ impl Attribute {
}
}
}

#[doc(hidden)]
pub fn format_attrs<'a, T: AsRef<[(&'a str, Self)]> + 'a>(
attrs: T,
) -> String {
// this monstrosity is a signature move by @baptistemontan
fn format_inner<'a>(
s: &mut String,
mut iter: impl Iterator<Item = (&'a str, Option<Oco<'a, str>>)>,
) -> core::fmt::Result {
use crate::ssr::escape_attr;
use core::fmt::Write;
let Some((first_attr_name, first_attr)) = iter.next() else {
return Ok(());
};
s.push_str(first_attr_name);
if let Some(attr) = first_attr {
write!(s, "=\"{}\"", &escape_attr(&attr))?;
}
for (attr_name, attr) in iter {
match attr {
None => write!(s, " {}", attr_name)?,
Some(attr) => {
write!(s, " {}=\"{}\"", attr_name, &escape_attr(&attr))?
}
}
}
Ok(())
}
let iter =
attrs
.as_ref()
.iter()
.filter_map(|(attr_name, attr)| match attr {
Attribute::Bool(true) => Some((*attr_name, None)),
attr => {
let as_nameless_value_string =
attr.as_nameless_value_string()?;
Some((*attr_name, Some(as_nameless_value_string)))
}
});
let mut s = String::new();
format_inner(&mut s, iter)
.expect("Unexpected error while writing to a String.");
s
}
}

impl PartialEq for Attribute {
Expand Down
4 changes: 1 addition & 3 deletions leptos_macro/src/view/server_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,7 @@ fn element_to_tokens_ssr(
// the template
template.push_str(" {}");
holes.push(quote! {
{#end}.into_iter().filter_map(|(name, attr)| {
Some(format!("{}=\"{}\"", name, ::leptos::leptos_dom::ssr::escape_attr(&attr.as_nameless_value_string()?)))
}).collect::<Vec<_>>().join(" ")
::leptos::Attribute::format_attrs(#end)
});
};
}
Expand Down

0 comments on commit e51b5a0

Please sign in to comment.