From 339e450027b4a5d2e1317e13863cd1b2e7ab5853 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Wed, 27 Oct 2021 16:58:20 -0400 Subject: [PATCH] wip: fix some bugs around the rsx macro --- packages/core-macro/src/rsx/element.rs | 98 ++++++++++++++------------ packages/core/examples/expand.rs | 1 + packages/core/examples/jsframework.rs | 4 +- packages/core/examples/syntax.rs | 23 +----- 4 files changed, 57 insertions(+), 69 deletions(-) create mode 100644 packages/core/examples/expand.rs diff --git a/packages/core-macro/src/rsx/element.rs b/packages/core-macro/src/rsx/element.rs index 493dc7887..4c37b0b21 100644 --- a/packages/core-macro/src/rsx/element.rs +++ b/packages/core-macro/src/rsx/element.rs @@ -14,51 +14,57 @@ use syn::{ pub struct Element { name: Ident, key: Option, - attributes: Vec, - listeners: Vec, + attributes: Vec, + listeners: Vec, children: Vec, _is_static: bool, } impl Parse for Element { fn parse(stream: ParseStream) -> Result { - let name = Ident::parse(stream)?; + let el_name = Ident::parse(stream)?; // parse the guts let content: ParseBuffer; syn::braced!(content in stream); - let mut attributes: Vec = vec![]; - let mut listeners: Vec = vec![]; + let mut attributes: Vec = vec![]; + let mut listeners: Vec = vec![]; let mut children: Vec = vec![]; let mut key = None; let mut el_ref = None; while !content.is_empty() { if content.peek(Ident) && content.peek2(Token![:]) && !content.peek3(Token![:]) { - let name = Ident::parse_any(stream)?; + let name = content.parse::()?; let name_str = name.to_string(); - stream.parse::()?; + content.parse::()?; if name_str.starts_with("on") { - if stream.peek(token::Brace) { - let content; - syn::braced!(content in stream); + if content.peek(token::Brace) { + let mycontent; + syn::braced!(mycontent in content); - listeners.push(ElementAttr::EventTokens { - name, - tokens: content.parse()?, + listeners.push(ElementAttrNamed { + el_name: el_name.clone(), + attr: ElementAttr::EventTokens { + name, + tokens: mycontent.parse()?, + }, }); } else { - listeners.push(ElementAttr::EventClosure { - name, - closure: content.parse()?, + listeners.push(ElementAttrNamed { + el_name: el_name.clone(), + attr: ElementAttr::EventClosure { + name, + closure: content.parse()?, + }, }); }; } else { match name_str.as_str() { "key" => { - key = Some(stream.parse()?); + key = Some(content.parse()?); } "classes" => { todo!("custom class list not supported") @@ -67,18 +73,24 @@ impl Parse for Element { todo!("custom namespace not supported") } "node_ref" => { - el_ref = Some(stream.parse::()?); + el_ref = Some(content.parse::()?); } _ => { - if stream.peek(LitStr) { - listeners.push(ElementAttr::AttrText { - name, - value: content.parse()?, + if content.peek(LitStr) { + attributes.push(ElementAttrNamed { + el_name: el_name.clone(), + attr: ElementAttr::AttrText { + name, + value: content.parse()?, + }, }); } else { - listeners.push(ElementAttr::AttrExpression { - name, - value: content.parse()?, + attributes.push(ElementAttrNamed { + el_name: el_name.clone(), + attr: ElementAttr::AttrExpression { + name, + value: content.parse()?, + }, }); } } @@ -90,10 +102,16 @@ impl Parse for Element { if content.peek(LitStr) { let value = content.parse::()?; - attributes.push(ElementAttr::CustomAttrText { name, value }); + attributes.push(ElementAttrNamed { + el_name: el_name.clone(), + attr: ElementAttr::CustomAttrText { name, value }, + }); } else { let value = content.parse::()?; - attributes.push(ElementAttr::CustomAttrExpression { name, value }); + attributes.push(ElementAttrNamed { + el_name: el_name.clone(), + attr: ElementAttr::CustomAttrExpression { name, value }, + }); } } else { children.push(content.parse::()?); @@ -108,7 +126,7 @@ impl Parse for Element { Ok(Self { key, - name, + name: el_name, attributes, children, listeners, @@ -121,12 +139,9 @@ impl ToTokens for Element { fn to_tokens(&self, tokens: &mut TokenStream2) { let name = &self.name; let childs = &self.children; - let listeners = &self.listeners; - let attr = self.attributes.iter().map(|x| ElementAttrNamed { - attr: x, - el_name: name, - }); + let listeners = &self.listeners; + let attr = &self.attributes; let key = match &self.key { Some(ty) => quote! { Some(format_args_f!(#ty)) }, @@ -165,21 +180,14 @@ enum ElementAttr { EventTokens { name: Ident, tokens: Expr }, } -impl ToTokens for ElementAttr { - fn to_tokens(&self, tokens: &mut TokenStream2) { - // weird requirment - todo!() - } +struct ElementAttrNamed { + el_name: Ident, + attr: ElementAttr, } -struct ElementAttrNamed<'a> { - el_name: &'a Ident, - attr: &'a ElementAttr, -} - -impl ToTokens for ElementAttrNamed<'_> { +impl ToTokens for ElementAttrNamed { fn to_tokens(&self, tokens: &mut TokenStream2) { - let ElementAttrNamed { el_name, attr } = *self; + let ElementAttrNamed { el_name, attr } = self; let toks = match attr { ElementAttr::AttrText { name, value } => { diff --git a/packages/core/examples/expand.rs b/packages/core/examples/expand.rs new file mode 100644 index 000000000..1b041b70c --- /dev/null +++ b/packages/core/examples/expand.rs @@ -0,0 +1 @@ +fn main( ) {} diff --git a/packages/core/examples/jsframework.rs b/packages/core/examples/jsframework.rs index 3a9d73681..2b2939a15 100644 --- a/packages/core/examples/jsframework.rs +++ b/packages/core/examples/jsframework.rs @@ -48,11 +48,11 @@ fn Row((cx, props): Component) -> DomTree { cx.render(rsx! { tr { td { class:"col-md-1", "{props.row_id}" } - td { class:"col-md-1", on_click: move |_| { /* run onselect */ } + td { class:"col-md-1", onclick: move |_| { /* run onselect */ } a { class: "lbl", "{props.label}" } } td { class: "col-md-1" - a { class: "remove", on_click: {handler} + a { class: "remove", onclick: {handler} span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" } } } diff --git a/packages/core/examples/syntax.rs b/packages/core/examples/syntax.rs index 0c1674c8d..a54a3c218 100644 --- a/packages/core/examples/syntax.rs +++ b/packages/core/examples/syntax.rs @@ -8,31 +8,10 @@ use dioxus_html as dioxus_elements; fn main() {} fn html_usage() { - let r = html! { -
- "hello world" -
-
-
- "hello world" - - -
- }; let r = rsx! { div { + onclick: move |_| {} "hello world" } }; } - -fn rsx_uage() { - // let r = html! { - // - // "hello world" - // "hello world" - // "hello world" - // "hello world" - // - // }; -}