Filter empty string classes (#770)

* Filter empty string classes

* cargo fmt

* feedback

* Add tests
This commit is contained in:
Justin Starry 2019-12-03 23:06:35 -05:00 committed by GitHub
parent cb671ad4d2
commit fd8c3ee665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 5 deletions

View File

@ -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<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<&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<T: AsRef<str>> From<Vec<T>> for Classes {
fn from(t: Vec<T>) -> 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 }
}
}

View File

@ -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<Comp> = html! { <div class=vec![""]></div> };
let b: VNode<Comp> = html! { <div class=("")></div> };
let c: VNode<Comp> = html! { <div class=""></div> };
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<Comp>) -> &mut VTag<Comp> {
if let VNode::VTag(vtag) = node {
return vtag;