123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- use super::*;
- use proc_macro2::{Span, TokenStream as TokenStream2};
- use quote::{quote, ToTokens, TokenStreamExt};
- use syn::{
- parse::{Parse, ParseBuffer, ParseStream},
- Error, Expr, Ident, LitStr, Result, Token,
- };
- // =======================================
- // Parse the VNode::Element type
- // =======================================
- #[derive(PartialEq, Eq, Clone, Debug, Hash)]
- pub struct Element {
- pub name: Ident,
- pub key: Option<IfmtInput>,
- pub attributes: Vec<ElementAttrNamed>,
- pub children: Vec<BodyNode>,
- pub _is_static: bool,
- }
- impl Parse for Element {
- fn parse(stream: ParseStream) -> Result<Self> {
- let el_name = Ident::parse(stream)?;
- // parse the guts
- let content: ParseBuffer;
- syn::braced!(content in stream);
- let mut attributes: Vec<ElementAttrNamed> = vec![];
- let mut children: Vec<BodyNode> = vec![];
- let mut key = None;
- let mut _el_ref = None;
- // parse fields with commas
- // break when we don't get this pattern anymore
- // start parsing bodynodes
- // "def": 456,
- // abc: 123,
- loop {
- // Parse the raw literal fields
- if content.peek(LitStr) && content.peek2(Token![:]) && !content.peek3(Token![:]) {
- let name = content.parse::<LitStr>()?;
- let ident = name.clone();
- content.parse::<Token![:]>()?;
- if content.peek(LitStr) && content.peek2(Token![,]) {
- let value = content.parse()?;
- attributes.push(ElementAttrNamed {
- el_name: el_name.clone(),
- attr: ElementAttr::CustomAttrText { name, value },
- });
- } else {
- let value = content.parse::<Expr>()?;
- attributes.push(ElementAttrNamed {
- el_name: el_name.clone(),
- attr: ElementAttr::CustomAttrExpression { name, value },
- });
- }
- if content.is_empty() {
- break;
- }
- if content.parse::<Token![,]>().is_err() {
- missing_trailing_comma!(ident.span());
- }
- continue;
- }
- if content.peek(Ident) && content.peek2(Token![:]) && !content.peek3(Token![:]) {
- let name = content.parse::<Ident>()?;
- let ident = name.clone();
- let name_str = name.to_string();
- content.parse::<Token![:]>()?;
- if name_str.starts_with("on") {
- attributes.push(ElementAttrNamed {
- el_name: el_name.clone(),
- attr: ElementAttr::EventTokens {
- name,
- tokens: content.parse()?,
- },
- });
- } else {
- match name_str.as_str() {
- "key" => {
- key = Some(content.parse()?);
- }
- "classes" => todo!("custom class list not supported yet"),
- // "namespace" => todo!("custom namespace not supported yet"),
- "node_ref" => {
- _el_ref = Some(content.parse::<Expr>()?);
- }
- _ => {
- if content.peek(LitStr) {
- attributes.push(ElementAttrNamed {
- el_name: el_name.clone(),
- attr: ElementAttr::AttrText {
- name,
- value: content.parse()?,
- },
- });
- } else {
- attributes.push(ElementAttrNamed {
- el_name: el_name.clone(),
- attr: ElementAttr::AttrExpression {
- name,
- value: content.parse()?,
- },
- });
- }
- }
- }
- }
- if content.is_empty() {
- break;
- }
- // todo: add a message saying you need to include commas between fields
- if content.parse::<Token![,]>().is_err() {
- missing_trailing_comma!(ident.span());
- }
- continue;
- }
- break;
- }
- while !content.is_empty() {
- if (content.peek(LitStr) && content.peek2(Token![:])) && !content.peek3(Token![:]) {
- attr_after_element!(content.span());
- }
- if (content.peek(Ident) && content.peek2(Token![:])) && !content.peek3(Token![:]) {
- attr_after_element!(content.span());
- }
- children.push(content.parse::<BodyNode>()?);
- // consume comma if it exists
- // we don't actually care if there *are* commas after elements/text
- if content.peek(Token![,]) {
- let _ = content.parse::<Token![,]>();
- }
- }
- Ok(Self {
- key,
- name: el_name,
- attributes,
- children,
- _is_static: false,
- })
- }
- }
- impl ToTokens for Element {
- fn to_tokens(&self, tokens: &mut TokenStream2) {
- let name = &self.name;
- let children = &self.children;
- let key = match &self.key {
- Some(ty) => quote! { Some(#ty) },
- None => quote! { None },
- };
- let listeners = self
- .attributes
- .iter()
- .filter(|f| matches!(f.attr, ElementAttr::EventTokens { .. }));
- let attr = self
- .attributes
- .iter()
- .filter(|f| !matches!(f.attr, ElementAttr::EventTokens { .. }));
- tokens.append_all(quote! {
- __cx.element(
- dioxus_elements::#name,
- __cx.bump().alloc([ #(#listeners),* ]),
- __cx.bump().alloc([ #(#attr),* ]),
- __cx.bump().alloc([ #(#children),* ]),
- #key,
- )
- });
- }
- }
- #[derive(PartialEq, Eq, Clone, Debug, Hash)]
- pub enum ElementAttr {
- /// attribute: "valuee {}"
- AttrText { name: Ident, value: IfmtInput },
- /// attribute: true,
- AttrExpression { name: Ident, value: Expr },
- /// "attribute": "value {}"
- CustomAttrText { name: LitStr, value: IfmtInput },
- /// "attribute": true,
- CustomAttrExpression { name: LitStr, value: Expr },
- // /// onclick: move |_| {}
- // EventClosure { name: Ident, closure: ExprClosure },
- /// onclick: {}
- EventTokens { name: Ident, tokens: Expr },
- }
- impl ElementAttr {
- pub fn flart(&self) -> Span {
- match self {
- ElementAttr::AttrText { name, .. } => name.span(),
- ElementAttr::AttrExpression { name, .. } => name.span(),
- ElementAttr::CustomAttrText { name, .. } => name.span(),
- ElementAttr::CustomAttrExpression { name, .. } => name.span(),
- ElementAttr::EventTokens { name, .. } => name.span(),
- }
- }
- pub fn is_expr(&self) -> bool {
- matches!(
- self,
- ElementAttr::AttrExpression { .. }
- | ElementAttr::CustomAttrExpression { .. }
- | ElementAttr::EventTokens { .. }
- )
- }
- }
- #[derive(PartialEq, Eq, Clone, Debug, Hash)]
- pub struct ElementAttrNamed {
- pub el_name: Ident,
- pub attr: ElementAttr,
- }
- impl ToTokens for ElementAttrNamed {
- fn to_tokens(&self, tokens: &mut TokenStream2) {
- let ElementAttrNamed { el_name, attr } = self;
- tokens.append_all(match attr {
- ElementAttr::AttrText { name, value } => {
- quote! {
- __cx.attr(
- dioxus_elements::#el_name::#name.0,
- #value,
- None,
- false
- )
- }
- }
- ElementAttr::AttrExpression { name, value } => {
- quote! {
- __cx.attr(
- dioxus_elements::#el_name::#name.0,
- #value,
- None,
- false
- )
- }
- }
- ElementAttr::CustomAttrText { name, value } => {
- quote! {
- __cx.attr(
- dioxus_elements::#el_name::#name.0,
- #value,
- None,
- false
- )
- }
- }
- ElementAttr::CustomAttrExpression { name, value } => {
- quote! {
- __cx.attr(
- dioxus_elements::#el_name::#name.0,
- #value,
- None,
- false
- )
- }
- }
- ElementAttr::EventTokens { name, tokens } => {
- quote! {
- dioxus_elements::events::#name(__cx, #tokens)
- }
- }
- });
- }
- }
- // ::dioxus::core::Attribute {
- // name: stringify!(#name),
- // namespace: None,
- // volatile: false,
- // mounted_node: Default::default(),
- // value: ::dioxus::core::AttributeValue::Text(#value),
- // }
|