diff --git a/crates/xilem_html/src/element/attributes.rs b/crates/xilem_html/src/element/attribute_value.rs similarity index 69% rename from crates/xilem_html/src/element/attributes.rs rename to crates/xilem_html/src/element/attribute_value.rs index c4ac9ead9..050a3a683 100644 --- a/crates/xilem_html/src/element/attributes.rs +++ b/crates/xilem_html/src/element/attribute_value.rs @@ -1,8 +1,4 @@ -use std::borrow::Cow; - -use crate::vecmap::VecMap; - -type CowStr = Cow<'static, str>; +type CowStr = std::borrow::Cow<'static, str>; #[derive(PartialEq, Debug)] pub enum AttributeValue { @@ -94,33 +90,3 @@ impl IntoAttributeValue for &'static str { Some(AttributeValue::String(self.into())) } } - -#[derive(Default)] -pub struct Attributes(VecMap); - -impl<'a> IntoIterator for &'a Attributes { - type Item = (&'a CowStr, &'a AttributeValue); - - type IntoIter = <&'a VecMap as std::iter::IntoIterator>::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() - } -} - -impl Attributes { - // TODO return the previous attribute as an Option? - pub fn insert(&mut self, name: impl Into, value: impl IntoAttributeValue) { - let value = value.into_attribute_value(); - // This is a simple optimization in case this is the first attribute inserted to the map (saves an allocation for the Vec) - if let Some(value) = value.into_attribute_value() { - self.0.insert(name.into(), value); - } else { - self.0.remove(&name.into()); - } - } - - pub fn iter(&self) -> impl Iterator { - self.0.iter() - } -} diff --git a/crates/xilem_html/src/element/mod.rs b/crates/xilem_html/src/element/mod.rs index 88d3ccaa8..0dcc4b915 100644 --- a/crates/xilem_html/src/element/mod.rs +++ b/crates/xilem_html/src/element/mod.rs @@ -5,6 +5,7 @@ use crate::{ context::{ChangeFlags, Cx}, diff::{diff_kv_iterables, Diff}, + vecmap::VecMap, view::{DomElement, Pod, View, ViewMarker, ViewSequence}, }; @@ -16,7 +17,7 @@ pub mod attributes; #[cfg(feature = "typed")] pub mod elements; -pub use attributes::{AttributeValue, Attributes, IntoAttributeValue}; +pub use attributes::{AttributeValue, IntoAttributeValue}; type CowStr = Cow<'static, str>; @@ -25,7 +26,7 @@ type CowStr = Cow<'static, str>; /// If the element has no children, use the unit type (e.g. `let view = element("div", ())`). pub struct Element { name: CowStr, - attributes: Attributes, + attributes: VecMap, children: Children, #[allow(clippy::type_complexity)] after_update: Option>, @@ -62,7 +63,7 @@ pub struct ElementState { pub fn element(name: impl Into, children: ViewSeq) -> Element { Element { name: name.into(), - attributes: Attributes::default(), + attributes: Default::default(), children, after_update: None, } @@ -87,7 +88,12 @@ impl Element { /// If the name contains characters that are not valid in an attribute name, /// then the `View::build`/`View::rebuild` functions will panic for this view. pub fn set_attr(&mut self, name: impl Into, value: impl IntoAttributeValue) { - self.attributes.insert(name, value); + let name = name.into(); + if let Some(value) = value.into_attribute_value() { + self.attributes.insert(name, value); + } else { + self.attributes.remove(&name); + } } /// Set a function to run after the new view tree has been created.