Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xilem_html: Experiment with Strong typed attributes/event-listeners owned by the concrete element type #119

2 changes: 1 addition & 1 deletion crates/xilem_html/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typed = [
"web-sys/HtmlTableSectionElement", "web-sys/HtmlTemplateElement", "web-sys/HtmlTextAreaElement",
"web-sys/HtmlTimeElement", "web-sys/HtmlTrackElement", "web-sys/HtmlUListElement",
"web-sys/HtmlVideoElement", "web-sys/InputEvent", "web-sys/KeyboardEvent", "web-sys/MouseEvent",
"web-sys/PointerEvent", "web-sys/WheelEvent",
"web-sys/PointerEvent", "web-sys/WheelEvent", "web-sys/TouchEvent",
]

[dependencies]
Expand Down
25 changes: 25 additions & 0 deletions crates/xilem_html/src/element/attribute_value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
use std::collections::BTreeSet;

type CowStr = std::borrow::Cow<'static, str>;

// TODO not sure how useful an extra enum for attribute keys is (comparison is probably a little bit faster...)
// #[derive(PartialEq, Eq)]
// enum AttrKey {
// Width,
// Height,
// Class,
// Untyped(Box<Cow<'static, str>>),
// }

#[derive(PartialEq, Debug)]
pub enum AttributeValue {
True, // for the boolean true, this serializes to an empty string (e.g. for <input checked>)
Expand All @@ -8,6 +19,7 @@ pub enum AttributeValue {
F32(f32),
F64(f64),
String(CowStr),
Classes(BTreeSet<CowStr>),
}

impl AttributeValue {
Expand All @@ -19,6 +31,19 @@ impl AttributeValue {
AttributeValue::F32(n) => n.to_string().into(),
AttributeValue::F64(n) => n.to_string().into(),
AttributeValue::String(s) => s.clone(),
// TODO maybe use Vec as backend (should probably be more performant for few classes, which seems to be the average case)
AttributeValue::Classes(set) => set
.iter()
.fold(String::new(), |mut acc, s| {
if !acc.is_empty() {
acc += " ";
}
if !s.is_empty() {
acc += s;
}
acc
})
.into(),
}
}
}
Expand Down
Loading