Skip to content

Commit

Permalink
Remove Attributes wrapper type and cleanup a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp-M committed Aug 16, 2023
1 parent 8cdf4b9 commit 6d7e05c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -94,33 +90,3 @@ impl IntoAttributeValue for &'static str {
Some(AttributeValue::String(self.into()))
}
}

#[derive(Default)]
pub struct Attributes(VecMap<CowStr, AttributeValue>);

impl<'a> IntoIterator for &'a Attributes {
type Item = (&'a CowStr, &'a AttributeValue);

type IntoIter = <&'a VecMap<CowStr, AttributeValue> 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<CowStr>, 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<Item = (&CowStr, &AttributeValue)> {
self.0.iter()
}
}
14 changes: 10 additions & 4 deletions crates/xilem_html/src/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::{
context::{ChangeFlags, Cx},
diff::{diff_kv_iterables, Diff},
vecmap::VecMap,
view::{DomElement, Pod, View, ViewMarker, ViewSequence},
};

Expand All @@ -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>;

Expand All @@ -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<El, Children = ()> {
name: CowStr,
attributes: Attributes,
attributes: VecMap<CowStr, AttributeValue>,
children: Children,
#[allow(clippy::type_complexity)]
after_update: Option<Box<dyn Fn(&El)>>,
Expand Down Expand Up @@ -62,7 +63,7 @@ pub struct ElementState<ViewSeqState> {
pub fn element<El, ViewSeq>(name: impl Into<CowStr>, children: ViewSeq) -> Element<El, ViewSeq> {
Element {
name: name.into(),
attributes: Attributes::default(),
attributes: Default::default(),
children,
after_update: None,
}
Expand All @@ -87,7 +88,12 @@ impl<El, ViewSeq> Element<El, ViewSeq> {
/// 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<CowStr>, 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.
Expand Down

0 comments on commit 6d7e05c

Please sign in to comment.