diff --git a/macros/src/html.rs b/macros/src/html.rs index a1db35d..cef7ce6 100644 --- a/macros/src/html.rs +++ b/macros/src/html.rs @@ -262,7 +262,7 @@ impl Element { let key = TokenTree::Ident(key.clone()); let value = process_value(value); body.extend(quote!( - element.events.#key = Some(typed_html::events::IntoEventHandler::into_event_handler(#value)); + element.events.#key = Some(#value.into()); )); } diff --git a/typed-html/src/events.rs b/typed-html/src/events.rs index aa1ac4f..5ac38c6 100644 --- a/typed-html/src/events.rs +++ b/typed-html/src/events.rs @@ -29,52 +29,17 @@ pub trait IntoEventHandler { fn into_event_handler(self) -> Box>; } -/// An uninhabited event type for string handlers. -pub enum StringEvent {} - -impl EventHandler for &'static str { - fn attach(&mut self, _target: &mut ::EventTarget) { - panic!("Silly wabbit, strings as event handlers are only for printing."); - } - - fn render(&self) -> Option { - Some(self.to_string()) - } -} - -impl IntoEventHandler for &'static str { - fn into_event_handler(self) -> Box> { - Box::new(self) - } -} - -impl EventHandler for String { - fn attach(&mut self, _target: &mut ::EventTarget) { - panic!("Silly wabbit, strings as event handlers are only for printing."); - } - - fn render(&self) -> Option { - Some(self.clone()) - } -} - -impl IntoEventHandler for String { - fn into_event_handler(self) -> Box> { - Box::new(self) - } -} - -macro_rules! declare_string_events { +macro_rules! declare_events_struct { ($($name:ident,)*) => { - pub struct StringEvents { + pub struct Events { $( - pub $name: Option>>, + pub $name: Option, )* } - impl Default for StringEvents { + impl Default for Events { fn default() -> Self { - StringEvents { + Events { $( $name: None, )* @@ -82,12 +47,12 @@ macro_rules! declare_string_events { } } - impl Display for StringEvents { + impl Display for Events { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { $( if let Some(ref value) = self.$name { - write!(f, " on{}=\"{}\"", stringify!($name), - encode_attribute(value.render().unwrap().as_str()))?; + let attribute = encode_attribute(&value.to_string()); + write!(f, " on{}=\"{}\"", stringify!($name), attribute)?; } )* Ok(()) @@ -96,7 +61,7 @@ macro_rules! declare_string_events { } } -declare_string_events! { +declare_events_struct! { abort, autocomplete, autocompleteerror, diff --git a/typed-html/src/lib.rs b/typed-html/src/lib.rs index 40b4dc0..8c8bfd4 100644 --- a/typed-html/src/lib.rs +++ b/typed-html/src/lib.rs @@ -221,7 +221,7 @@ pub trait OutputType { /// String output impl OutputType for String { - type Events = events::StringEvents; + type Events = events::Events; type EventTarget = (); type EventListenerHandle = (); }