Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support attributes with '-' in name #79

Merged
merged 3 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
}
};

render(root);
render(|| root);
}
```

Expand All @@ -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! {
Expand Down
41 changes: 37 additions & 4 deletions maple-core-macro/src/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

use syn::ext::IdentExt;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
Expand All @@ -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`.
Expand All @@ -15,7 +17,7 @@ pub enum AttributeType {

impl Parse for AttributeType {
fn parse(input: ParseStream) -> Result<Self> {
let ident = input.call(Ident::parse_any)?;
let ident: AttributeName = input.parse()?;
let ident_str = ident.to_string();

if ident_str == "ref" {
Expand All @@ -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 })
}
}
}
Expand Down Expand Up @@ -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<Self> {
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(())
}
}
6 changes: 5 additions & 1 deletion maple-core-macro/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))) };

Expand Down
9 changes: 7 additions & 2 deletions maple-core-macro/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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=>
Expand Down
1 change: 1 addition & 0 deletions maple-core-macro/tests/ui/element-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
4 changes: 2 additions & 2 deletions maple-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -31,7 +31,7 @@ features = [
"Text",
"Window",
]
version = "0.3.48"
version = "0.3"

[dev-dependencies]
criterion = {version = "0.3", features = ["html_reports"]}
Expand Down