diff --git a/README.md b/README.md index b306dbbe8..f14755172 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ fn main() { } }; - render(root); + render(|| root); } ``` @@ -51,7 +51,7 @@ template! { // Attributes (including classes and ids) can also be specified. template! { - p(class="my-class", id="my-paragraph") + p(class="my-class", id="my-paragraph", aria-label="My paragraph") }; template! { diff --git a/maple-core-macro/src/attributes.rs b/maple-core-macro/src/attributes.rs index 0605cf60f..09347af3f 100644 --- a/maple-core-macro/src/attributes.rs +++ b/maple-core-macro/src/attributes.rs @@ -1,3 +1,5 @@ +use std::fmt; + use syn::ext::IdentExt; use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; @@ -6,7 +8,7 @@ use syn::{parenthesized, Expr, Ident, Result, Token}; pub enum AttributeType { /// Syntax: `name`. - DomAttribute { name: String }, + DomAttribute { name: AttributeName }, /// Syntax: `on:name`. Event { name: String }, /// Syntax: `ref`. @@ -15,7 +17,7 @@ pub enum AttributeType { impl Parse for AttributeType { fn parse(input: ParseStream) -> Result { - let ident = input.call(Ident::parse_any)?; + let ident: AttributeName = input.parse()?; let ident_str = ident.to_string(); if ident_str == "ref" { @@ -30,12 +32,12 @@ impl Parse for AttributeType { }) } _ => Err(syn::Error::new_spanned( - ident, + ident.tag, format!("unknown directive `{}`", ident_str), )), } } else { - Ok(Self::DomAttribute { name: ident_str }) + Ok(Self::DomAttribute { name: ident }) } } } @@ -74,3 +76,34 @@ impl Parse for AttributeList { }) } } + +/// Represents a html element tag (e.g. `div`, `custom-element` etc...). +pub struct AttributeName { + tag: Ident, + extended: Vec<(Token![-], Ident)>, +} + +impl Parse for AttributeName { + fn parse(input: ParseStream) -> Result { + let tag = input.call(Ident::parse_any)?; + let mut extended = Vec::new(); + while input.peek(Token![-]) { + extended.push((input.parse()?, input.parse()?)); + } + + Ok(Self { tag, extended }) + } +} + +impl fmt::Display for AttributeName { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let AttributeName { tag, extended } = self; + + write!(f, "{}", tag.to_string())?; + for (_, ident) in extended { + write!(f, "-{}", ident)?; + } + + Ok(()) + } +} diff --git a/maple-core-macro/src/component.rs b/maple-core-macro/src/component.rs index 7ecd7f577..ce20ae8bd 100644 --- a/maple-core-macro/src/component.rs +++ b/maple-core-macro/src/component.rs @@ -25,7 +25,11 @@ impl Parse for Component { impl ToTokens for Component { fn to_tokens(&self, tokens: &mut TokenStream) { - let Component { path, _paren: _, args } = self; + let Component { + path, + _paren: _, + args, + } = self; let quoted = quote! { ::maple_core::reactive::untrack(|| ::maple_core::TemplateResult::inner_element(&#path(#args))) }; diff --git a/maple-core-macro/src/element.rs b/maple-core-macro/src/element.rs index 6f1a8ee07..a9a3abacf 100644 --- a/maple-core-macro/src/element.rs +++ b/maple-core-macro/src/element.rs @@ -59,9 +59,14 @@ impl ToTokens for Element { match &attribute.ty { AttributeType::DomAttribute { name } => { + let attribute_name = name.to_string(); set_attributes.push(quote_spanned! { expr_span=> - ::maple_core::internal::attr(::std::convert::AsRef::as_ref(&element), #name, ::std::boxed::Box::new(move || ::std::format!("{}", #expr))); - }); + ::maple_core::internal::attr( + ::std::convert::AsRef::as_ref(&element), + #attribute_name, + ::std::boxed::Box::new(move || ::std::format!("{}", #expr)), + ); + }); } AttributeType::Event { name } => { set_event_listeners.push(quote_spanned! { expr_span=> diff --git a/maple-core-macro/tests/ui/element-pass.rs b/maple-core-macro/tests/ui/element-pass.rs index e1a0ce27a..d3ccd698d 100644 --- a/maple-core-macro/tests/ui/element-pass.rs +++ b/maple-core-macro/tests/ui/element-pass.rs @@ -11,6 +11,7 @@ fn compile_pass() { template! { p(class="my-class", id="my-id") }; template! { button(class="my-btn", on:click=|_| {}) }; + template! { button(class="my-btn", aria-hidden="true") }; } fn main() {} diff --git a/maple-core/Cargo.toml b/maple-core/Cargo.toml index c57d3478d..364f2429b 100644 --- a/maple-core/Cargo.toml +++ b/maple-core/Cargo.toml @@ -16,7 +16,7 @@ version = "0.4.2" [dependencies] maple-core-macro = {path = "../maple-core-macro", version = "0.4.2"} serde = {version = "1.0", optional = true} -wasm-bindgen = "0.2.72" +wasm-bindgen = "0.2" [dependencies.web-sys] features = [ @@ -31,7 +31,7 @@ features = [ "Text", "Window", ] -version = "0.3.48" +version = "0.3" [dev-dependencies] criterion = {version = "0.3", features = ["html_reports"]}