Compare commits

...

1 Commits
4239 ... 3221

Author SHA1 Message Date
Greg Johnston
a10ab613b8 fix: support complex punctuated attribute keys in attr: syntax (closes #3221) 2024-11-08 15:53:08 -05:00

View File

@@ -23,8 +23,11 @@ use std::{
collections::{HashMap, HashSet, VecDeque},
};
use syn::{
spanned::Spanned, Expr, Expr::Tuple, ExprArray, ExprLit, ExprRange, Lit,
LitStr, RangeLimits, Stmt,
punctuated::Pair::{End, Punctuated},
spanned::Spanned,
Expr,
Expr::Tuple,
ExprArray, ExprLit, ExprRange, Lit, LitStr, RangeLimits, Stmt,
};
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -998,10 +1001,10 @@ pub(crate) fn attribute_absolute(
) -> Option<TokenStream> {
let key = node.key.to_string();
let contains_dash = key.contains('-');
let attr_aira = key.starts_with("attr:aria-");
let attr_colon = key.starts_with("attr:");
// anything that follows the x:y pattern
match &node.key {
NodeName::Punctuated(parts) if !contains_dash || attr_aira => {
NodeName::Punctuated(parts) if !contains_dash || attr_colon => {
if parts.len() >= 2 {
let id = &parts[0];
match id {
@@ -1010,7 +1013,8 @@ pub(crate) fn attribute_absolute(
if id == "let" || id == "clone" {
None
} else if id == "attr" {
let value = attribute_value(node, true);
let value = attribute_value(node, true);
let multipart = parts.len() > 2;
let key = &parts[1];
let key_name = key.to_string();
if key_name == "class" || key_name == "style" {
@@ -1026,6 +1030,15 @@ pub(crate) fn attribute_absolute(
Some(
quote! { ::leptos::tachys::html::attribute::#key(#value) },
)
} else if multipart {
// e.g., attr:data-foo="bar"
let key_name = parts.pairs().skip(1).map(|p| match p {
Punctuated(n, p) => format!("{n}{p}"),
End(n) => n.to_string(),
}).collect::<String>();
Some(
quote! { ::leptos::tachys::html::attribute::custom::custom_attribute(#key_name, #value) },
)
} else {
Some(
quote! { ::leptos::tachys::html::attribute::#key(#value) },