diff --git a/src/dom/attributes.rs b/src/dom/attributes.rs index 1ddfaff..c1cbe7e 100644 --- a/src/dom/attributes.rs +++ b/src/dom/attributes.rs @@ -222,6 +222,52 @@ pub trait AttributeSetter { .expect_throw("Unable to set string attribute"); } } + + fn add_class(&mut self, class_name: &str) { + self.ws_element() + .class_list() + .add_1(class_name) + .expect_throw("Unable to add new class"); + } + + fn remove_class(&mut self, class_name: &str) { + self.ws_element() + .class_list() + .remove_1(class_name) + .expect_throw("Unable to remove old class"); + } + + fn _class(&mut self, class_name: &str) { + let (changed, old_value) = self.check_str_attribute_and_return_old_value(class_name); + if let Some(old_value) = old_value { + self.remove_class(&old_value); + } + if changed { + self.add_class(class_name); + } + } + + fn _class_if(&mut self, class_name: &str, class_on: bool) { + if self.check_bool_attribute(class_on) { + if class_on { + self.add_class(class_name); + } else { + self.remove_class(class_name); + } + } + } + + fn _class_or(&mut self, first: bool, first_class: &str, second_class: &str) { + if self.check_bool_attribute(first) { + if first { + self.add_class(first_class); + self.remove_class(second_class); + } else { + self.remove_class(first_class); + self.add_class(second_class); + } + } + } } macro_rules! create_methods_for_events { diff --git a/src/dom/html/attributes.rs b/src/dom/html/attributes.rs index 0f958e1..6b128ce 100644 --- a/src/dom/html/attributes.rs +++ b/src/dom/html/attributes.rs @@ -192,36 +192,18 @@ where } fn class(mut self, class_name: &str) -> Self { - let (changed, old_value) = self.check_str_attribute_and_return_old_value(class_name); - if let Some(old_value) = old_value { - self.ws_element() - .class_list() - .remove_1(&old_value) - .expect_throw("Unable to remove old class"); - } - if changed { - self.ws_element() - .class_list() - .add_1(class_name) - .expect_throw("Unable to add new class"); - } + self._class(class_name); self } fn class_if(mut self, class_name: &str, class_on: bool) -> Self { - if self.check_bool_attribute(class_on) { - if class_on { - self.ws_element() - .class_list() - .add_1(class_name) - .expect_throw("Unable to add class"); - } else { - self.ws_element() - .class_list() - .remove_1(class_name) - .expect_throw("Unable to remove class"); - } - } + self._class_if(class_name, class_on); + self + } + + /// Set the `first_class` if `first` is true, otherwise, set the `second_class` + fn class_or(mut self, first: bool, first_class: &str, second_class: &str) -> Self { + self._class_or(first, first_class, second_class); self } diff --git a/src/dom/svg/attributes.rs b/src/dom/svg/attributes.rs index 7ecca9d..fb103f0 100644 --- a/src/dom/svg/attributes.rs +++ b/src/dom/svg/attributes.rs @@ -1,4 +1,4 @@ -use wasm_bindgen::UnwrapThrowExt; +//use wasm_bindgen::UnwrapThrowExt; pub trait SvgAttributeSetter: Sized + crate::dom::attributes::AttributeSetter { fn bool_attr(mut self, name: &str, value: bool) -> Self { @@ -283,41 +283,19 @@ pub trait SvgAttributeSetter: Sized + crate::dom::attributes::AttributeSetter str zoom_and_pan "zoomAndPan" } - // Copied from crate::dom::html::attributes - // TODO: Simimilar code should be reuse fn class(mut self, class_name: &str) -> Self { - let (changed, old_value) = self.check_str_attribute_and_return_old_value(class_name); - if let Some(old_value) = old_value { - self.ws_element() - .class_list() - .remove_1(&old_value) - .expect_throw("Unable to remove old class"); - } - if changed { - self.ws_element() - .class_list() - .add_1(class_name) - .expect_throw("Unable to add new class"); - } + self._class(class_name); self } - // Copied from crate::dom::html::attributes - // TODO: Simimilar code should be reuse fn class_if(mut self, class_name: &str, class_on: bool) -> Self { - if self.check_bool_attribute(class_on) { - if class_on { - self.ws_element() - .class_list() - .add_1(class_name) - .expect_throw("Unable to add class"); - } else { - self.ws_element() - .class_list() - .remove_1(class_name) - .expect_throw("Unable to remove class"); - } - } + self._class_if(class_name, class_on); + self + } + + /// Set the `first_class` if `first` is true, otherwise, set the `second_class`. + fn class_or(mut self, first: bool, first_class: &str, second_class: &str) -> Self { + self._class_or(first, first_class, second_class); self } }