Skip to content

Commit

Permalink
Support hyphenated attribute as second (#200)
Browse files Browse the repository at this point in the history
This commit adds support for when a hyphenated attribute is the second
attribute.

```rust
html! {
  <path
    d="..."
    stroke-linejoin="miter"
  />
}
```

Before this commit the `html!` procedural macro would error if a
hyphenated attribute was present and was not the first attribute.
  • Loading branch information
chinedufn authored Jun 3, 2024
1 parent e8a1933 commit c6d8553
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions crates/html-macro/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ fn parse_attributes(input: &mut ParseStream) -> Result<Vec<Attr>> {
{
let (key, key_span) = parse_attribute_key(input)?;

println!("PARSING EQUALS");
// =
input.parse::<Token![=]>()?;
println!("PARSED EQUALS");

// Continue parsing tokens until we see the next attribute or a closing > tag
let mut value_tokens = TokenStream::new();
Expand All @@ -174,13 +176,23 @@ fn parse_attributes(input: &mut ParseStream) -> Result<Vec<Attr>> {
let tt: TokenTree = input.parse()?;
value_tokens.extend(Some(tt));

let has_attrib_key = input.peek(Ident)
let next_token_is_attrib_key = input.peek(Ident)
|| input.peek(Token![as])
|| input.peek(Token![async])
|| input.peek(Token![for])
|| input.peek(Token![loop])
|| input.peek(Token![type]);
let peek_start_of_next_attr = has_attrib_key && input.peek2(Token![=]);

// The `=` in:
// class="hello-world"
//
// Or the `-` in:
// http-equiv="refresh"
let next_next_token_is_equals_or_hyphen =
input.peek2(Token![=]) || input.peek2(Token![-]);

let peek_start_of_next_attr =
next_token_is_attrib_key && next_next_token_is_equals_or_hyphen;

let peek_end_of_tag = input.peek(Token![>]);

Expand Down Expand Up @@ -431,6 +443,21 @@ mod tests {
is_self_closing: true,
},
),
// Verify that we can parse an element that has a hyphenated attribute as its second
// attribute.
(
quote! {
<path
d="M1,5 a2,2"
stroke-linejoin="miter"
/>
},
ExpectedTag {
name: "path",
attributes: vec![("d", "M1,5 a2,2"), ("stroke-linejoin", "miter")],
is_self_closing: true,
},
),
];

for (tokens, expected_tag) in tests {
Expand Down

0 comments on commit c6d8553

Please sign in to comment.