use heck::*; use quote::{format_ident, quote}; pub fn element(input: proc_macro2::TokenStream) -> syn::Result { let element: Element = syn::parse2(input)?; let code = quote! { #element }; Ok(code) } struct Element { attrs: Vec, namespace: Namespace, tag: String, kind: Option, element: Option, attributes: Option, events: Option, } enum Namespace { Html, Svg, MathMl, } struct Attributes(Vec); struct Attribute { attrs: Vec, name: String, #[allow(dead_code)] is_void: bool, } struct Events(Vec); struct Event { attrs: Vec, name: String, ty: syn::Type, } impl syn::parse::Parse for Element { fn parse(input: syn::parse::ParseStream) -> syn::Result { let attrs = input.call(syn::Attribute::parse_outer)?; let mut namespace = None; let mut tag = None; let mut kind = None; let mut element = None; let mut attributes = None; let mut events = None; while !input.is_empty() { let ident = input.parse::()?; input.parse::()?; let ident_string = ident.to_string(); match ident_string.as_str() { "namespace" => { namespace = Some(match input.parse::()?.value().as_str() { "html" => Namespace::Html, "svg" => Namespace::Svg, "mathml" => Namespace::MathMl, _ => return Err(input.error("unexpected namespace")), }); } "tag" => { tag = Some(input.parse::()?.value()); } "kind" => { kind = Some(input.parse()?); } "element" => { element = Some(input.parse()?); } "attributes" => { let content; syn::braced!(content in input); attributes = Some(content.parse()?); } "events" => { let content; syn::braced!(content in input); events = Some(content.parse()?) } _ => return Err(input.error("unexpected key")), } input.parse::().ok(); } Ok(Element { attrs, namespace: namespace.unwrap(), tag: tag.unwrap(), kind, element, attributes, events, }) } } impl syn::parse::Parse for Attributes { fn parse(input: syn::parse::ParseStream) -> syn::Result { let attributes = input.parse_terminated::<_, syn::Token![,]>(Attribute::parse)?; let attributes = attributes.into_iter().collect(); Ok(Attributes(attributes)) } } impl syn::parse::Parse for Attribute { fn parse(input: syn::parse::ParseStream) -> syn::Result { let attrs = input.call(syn::Attribute::parse_outer)?; let name = input.parse::()?.to_string(); let is_void = input.parse::().ok().is_some(); Ok(Attribute { attrs, name, is_void, }) } } impl syn::parse::Parse for Events { fn parse(input: syn::parse::ParseStream) -> syn::Result { let events = input.parse_terminated::<_, syn::Token![,]>(Event::parse)?; let events = events.into_iter().collect(); Ok(Events(events)) } } impl syn::parse::Parse for Event { fn parse(input: syn::parse::ParseStream) -> syn::Result { let attrs = input.call(syn::Attribute::parse_outer)?; let name = input.parse::()?.to_string(); input.parse::()?; let ty = input.parse()?; Ok(Event { attrs, name, ty }) } } impl quote::ToTokens for Element { fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { let doc = &self.attrs; let tag = &self.tag; let namespace = match self.namespace { Namespace::Html => quote! { crate::Namespace::Html }, Namespace::Svg => quote! { crate::Namespace::Svg }, Namespace::MathMl => quote! { crate::Namespace::MathMl }, }; let kind = match self.namespace { Namespace::Html => self .kind .as_ref() .map(|kind| quote! { Some(#kind) }) .unwrap_or_else(|| quote! { Some(crate::HtmlElementKind::Normal) }), _ => quote! { None }, }; let fn_ident = format_ident!("{}", tag); let element_ident = format_ident!("{}Element", tag.to_camel_case()); let dom_element_ty = self .element .as_ref() .map(|element| quote! { #element }) .unwrap_or_else(|| quote! { web_sys::Element }); let basic_impl = quote! { impl #element_ident { pub fn new() -> Self { #element_ident(crate::Element::new(#tag, #namespace, #kind)) } pub fn future(mut self, f: impl FnOnce(&crate::Element) -> F ) -> Self where F: 'static + std::future::Future { self.0 = self.0.future(f); self } pub fn attribute(mut self, name: impl Into>, value: T) -> Self where T: crate::attribute_value::IntoAttributeValue, { self.0 = self.0.attribute(name, value); self } pub fn attribute_signal(mut self, name: impl Into>, value: S) -> Self where T: crate::attribute_value::IntoAttributeValue, S: 'static + Unpin + futures_signals::signal::Signal, { self.0 = self.0.attribute_signal(name, value); self } pub fn class(mut self, value: T) -> Self where T: crate::option_string_value::IntoOptionStringValue, { self.0 = self.0.class(value); self } pub fn class_signal(mut self, value: S) -> Self where S: 'static + Unpin + futures_signals::signal::Signal, T: crate::option_string_value::IntoOptionStringValue, { self.0 = self.0.class_signal(value); self } pub fn style(mut self, name: impl Into>, value: T) -> Self where T: crate::option_string_value::IntoOptionStringValue, { self.0 = self.0.style(name, value); self } pub fn style_signal(mut self, name: impl Into>, value: S) -> Self where S: 'static + Unpin + futures_signals::signal::Signal, T: crate::option_string_value::IntoOptionStringValue, { self.0 = self.0.style_signal(name, value); self } pub fn event( mut self, name: impl Into>, closure: impl 'static + FnMut(wasm_bindgen::JsValue), ) -> Self { self.0 = self.0.event(name, closure); self } pub fn child(mut self, child: T) -> Self where T: Into, { self.0 = self.0.child(child); self } pub fn children(mut self, children: I) -> Self where T: Into, I: IntoIterator, { for child in children.into_iter() { self.0 = self.0.child(child); } self } pub fn child_signal(mut self, signal: S) -> Self where T: Into, S: 'static + Unpin + futures_signals::signal::Signal, { self.0 = self.0.child_signal(signal); self } pub fn child_signal_vec(mut self, signal_vec: S) -> Self where T: Into, S: 'static + Unpin + futures_signals::signal_vec::SignalVec, { self.0 = self.0.child_signal_vec(signal_vec); self } pub fn inner_html(mut self, value: impl crate::string_value::IntoStringValue) -> Self { self.0 = self.0.inner_html(value); self } #[cfg(target_arch = "wasm32")] pub fn dom_element(&self) -> #dom_element_ty { wasm_bindgen::JsCast::unchecked_into(self.0.dom_element()) } } }; let global_attributes = match self.namespace { Namespace::Html => html_element_attributes(), _ => Attributes(Vec::new()), }; let empty_attributes = Attributes(Vec::new()); let attributes = self.attributes.as_ref().unwrap_or(&empty_attributes); let attribute_fns = global_attributes .0 .iter() .chain(attributes.0.iter()) .map(|attribute| { let attribute_attrs = &attribute.attrs; let attribute_name = attribute.name.to_string(); let attribute_ident = format_ident!("{}", attribute_name); let attribute_signal_ident = format_ident!("{}_signal", attribute_name); quote! { #(#attribute_attrs)* pub fn #attribute_ident(mut self, value: T) -> Self where T: crate::attribute_value::IntoAttributeValue, { self.0 = self.0.attribute(#attribute_name, value); self } pub fn #attribute_signal_ident(mut self, value: S) -> Self where T: crate::attribute_value::IntoAttributeValue, S: 'static + Unpin + futures_signals::signal::Signal, { self.0 = self.0.attribute_signal(#attribute_name, value); self } } }); let attributes_impl = quote! { impl #element_ident { #(#attribute_fns)* } }; let global_events = match self.namespace { Namespace::Html => html_element_events(), _ => Events(Vec::new()), }; let empty_events = Events(Vec::new()); let events = self.events.as_ref().unwrap_or(&empty_events); let event_fns = global_events.0.iter().chain(events.0.iter()).map(|event| { let event_attrs = &event.attrs; let event_name = event.name.to_string(); let event_fn_ident = format_ident!("on{}", event_name); let event_ty = &event.ty; quote! { #(#event_attrs)* pub fn #event_fn_ident( mut self, mut closure: impl 'static + FnMut(web_sys::#event_ty), ) -> Self { self.0 = self.0.event( #event_name, move |event| closure(wasm_bindgen::JsCast::unchecked_into(event)), ); self } } }); let events_impl = quote! { impl #element_ident { #(#event_fns)* } }; let from_impls = quote! { impl crate::component::Component for #element_ident { fn into_node(self) -> crate::Node { crate::Node::Element(self.0) } } }; let code = quote! { #(#doc)* pub fn #fn_ident() -> #element_ident { #element_ident::new() } #(#doc)* pub struct #element_ident(crate::Element); #basic_impl #attributes_impl #events_impl #from_impls }; code.to_tokens(tokens); } } fn html_element_attributes() -> Attributes { syn::parse2(quote!( /// Provides a hint for generating a keyboard shortcut for the current element. This attribute consists of a space-separated list of characters. The browser should use the first one that exists on the computer keyboard layout. accesskey, /** Controls whether and how text input is automatically capitalized as it is entered/edited by the user. It can have the following values: * off or none, no autocapitalization is applied (all letters default to lowercase) * on or sentences, the first letter of each sentence defaults to a capital letter; all other letters default to lowercase * words, the first letter of each word defaults to a capital letter; all other letters default to lowercase * characters, all letters should default to uppercase */ autocapitalize, /// A space-separated list of the classes of the element. Classes allows CSS and JavaScript to select and access specific elements via the class selectors or functions like the method `Document.getElementsByClassName()`. // class, /** An enumerated attribute indicating if the element should be editable by the user. If so, the browser modifies its widget to allow editing. The attribute must take one of the following values: true or the empty string, which indicates that the element must be editable; false, which indicates that the element must not be editable. */ contenteditable, /// The id of a `` to use as the contextual menu for this element. contextmenu, /** An enumerated attribute indicating the directionality of the element's text. It can have the following values: * ltr, which means left to right and is to be used for languages that are written from the left to the right (like English); * rtl, which means right to left and is to be used for languages that are written from the right to the left (like Arabic); * auto, which lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then it applies that directionality to the whole element. */ dir, /** An enumerated attribute indicating whether the element can be dragged, using the Drag and Drop API. It can have the following values: * true, which indicates that the element may be dragged * false, which indicates that the element may not be dragged. */ draggable, /// Hints what action label (or icon) to present for the enter key on virtual keyboards. enterkeyhint, /// Used to transitively export shadow parts from a nested shadow tree into a containing light tree. exportparts, /// A Boolean attribute indicates that the element is not yet, or is no longer, relevant. For example, it can be used to hide elements of the page that can't be used until the login process has been completed. The browser won't render such elements. This attribute must not be used to hide content that could legitimately be shown. hidden?, /// Defines a unique identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS). id, /// Provides a hint to browsers as to the type of virtual keyboard configuration to use when editing this element or its contents. Used primarily on `` elements, but is usable on any element while in contenteditable mode. inputmode, /// Allows you to specify that a standard HTML element should behave like a registered custom built-in element (see Using custom elements for more details). is, /// The unique, global identifier of an item. itemid, /// Used to add properties to an item. Every HTML element may have an itemprop attribute specified, where an itemprop consists of a name and value pair. itemprop, /// Properties that are not descendants of an element with the itemscope attribute can be associated with the item using an itemref. It provides a list of element ids (not itemids) with additional properties elsewhere in the document. itemref, /// itemscope (usually) works along with itemtype to specify that the HTML contained in a block is about a particular item. itemscope creates the Item and defines the scope of the itemtype associated with it. itemtype is a valid URL of a vocabulary (such as schema.org) that describes the item and its properties context. itemscope?, /// Specifies the URL of the vocabulary that will be used to define itemprops (item properties) in the data structure. itemscope is used to set the scope of where in the data structure the vocabulary set by itemtype will be active. itemtype, /// Helps define the language of an element: the language that non-editable elements are in, or the language that editable elements should be written in by the user. The attribute contains one \u201clanguage tag\u201d (made of hyphen-separated \u201clanguage subtags\u201d) in the format defined in Tags for Identifying Languages (BCP47). xml:lang has priority over it. lang, /// A cryptographic nonce ("number used once") which can be used by Content Security Policy to determine whether or not a given fetch will be allowed to proceed. nonce, /// A space-separated list of the part names of the element. Part names allows CSS to select and style specific elements in a shadow tree via the ::part pseudo-element. part, /// Assigns a slot in a shadow DOM shadow tree to an element: An element with a slot attribute is assigned to the slot created by the `` element whose name attribute's value matches that slot attribute's value. slot, /** An enumerated attribute defines whether the element may be checked for spelling errors. It may have the following values: * true, which indicates that the element should be, if possible, checked for spelling errors; * false, which indicates that the element should not be checked for spelling errors. */ spellcheck, // /// Contains CSS styling declarations to be applied to the element. Note that it is recommended for styles to be defined in a separate file or files. This attribute and the `