diff --git a/src/virtual_dom/mod.rs b/src/virtual_dom/mod.rs index 5d558213455..81aaa8f0ce2 100644 --- a/src/virtual_dom/mod.rs +++ b/src/virtual_dom/mod.rs @@ -58,7 +58,9 @@ impl Classes { /// /// Prevents duplication of class names. pub fn push(&mut self, class: &str) { - self.set.insert(class.into()); + if !class.is_empty() { + self.set.insert(class.into()); + } } /// Check the set contains a class. @@ -66,6 +68,11 @@ impl Classes { self.set.contains(class) } + /// Check the set is empty. + pub fn is_empty(&self) -> bool { + self.set.is_empty() + } + /// Adds other classes to this set of classes; returning itself. /// /// Takes the logical union of both `Classes`. @@ -89,28 +96,44 @@ impl ToString for Classes { impl From<&str> for Classes { fn from(t: &str) -> Self { - let set = t.split_whitespace().map(String::from).collect(); + let set = t + .split_whitespace() + .map(String::from) + .filter(|c| !c.is_empty()) + .collect(); Self { set } } } impl From for Classes { fn from(t: String) -> Self { - let set = t.split_whitespace().map(String::from).collect(); + let set = t + .split_whitespace() + .map(String::from) + .filter(|c| !c.is_empty()) + .collect(); Self { set } } } impl From<&String> for Classes { fn from(t: &String) -> Self { - let set = t.split_whitespace().map(String::from).collect(); + let set = t + .split_whitespace() + .map(String::from) + .filter(|c| !c.is_empty()) + .collect(); Self { set } } } impl> From> for Classes { fn from(t: Vec) -> Self { - let set = t.iter().map(|x| x.as_ref().to_string()).collect(); + let set = t + .iter() + .map(|x| x.as_ref().to_string()) + .filter(|c| !c.is_empty()) + .collect(); Self { set } } } diff --git a/tests/vtag_test.rs b/tests/vtag_test.rs index 9135ee27aae..e55e77c2153 100644 --- a/tests/vtag_test.rs +++ b/tests/vtag_test.rs @@ -228,6 +228,33 @@ fn supports_multiple_classes_vec() { } } +#[test] +fn filter_empty_string_classes_vec() { + let mut classes = vec![""]; + classes.push("class-2"); + let a: VNode = html! {
}; + let b: VNode = html! {
}; + let c: VNode = html! {
}; + + if let VNode::VTag(vtag) = a { + assert!(vtag.classes.is_empty()); + } else { + panic!("vtag expected"); + } + + if let VNode::VTag(vtag) = b { + assert!(vtag.classes.is_empty()); + } else { + panic!("vtag expected"); + } + + if let VNode::VTag(vtag) = c { + assert!(vtag.classes.is_empty()); + } else { + panic!("vtag expected"); + } +} + fn assert_vtag(node: &mut VNode) -> &mut VTag { if let VNode::VTag(vtag) = node { return vtag;