html!: fix explicit return types in callbacks

### Problem
The html! macro didn't properly handle explicit return types in
callbacks, considering the '>' in '->' as the end of the HTML tag.

```rust
html! {
    <div onblur=|_| -> u32 0 />
    //               ^ here
}
```

Fixes: #560
This commit is contained in:
Thomas Lacroix 2019-08-05 15:33:23 +02:00
parent fea73fe941
commit c6e1a395d1
No known key found for this signature in database
GPG Key ID: F2B5DB09C8BC0CD4
2 changed files with 14 additions and 2 deletions

View File

@ -32,9 +32,9 @@ impl Parse for HtmlProp {
}
pub struct HtmlPropSuffix {
pub stream: TokenStream,
pub div: Option<Token![/]>,
pub gt: Token![>],
pub stream: TokenStream,
}
impl Parse for HtmlPropSuffix {
@ -67,6 +67,14 @@ impl Parse for HtmlPropSuffix {
break;
}
}
'-' => {
if input.peek(Token![>]) {
// Handle explicit return types in callbacks (#560)
// We increase angle_count here in order to ignore
// the following >.
angle_count += 1;
}
}
_ => {}
};
}
@ -77,6 +85,6 @@ impl Parse for HtmlPropSuffix {
let stream: proc_macro2::TokenStream = trees.into_iter().collect();
let stream = TokenStream::from(stream);
Ok(HtmlPropSuffix { div, gt, stream })
Ok(HtmlPropSuffix { stream, div, gt })
}
}

View File

@ -380,6 +380,10 @@ fn it_checks_mixed_closing_tags() {
let b: VNode<CompInt> = html! { <div> <div onblur=|_| 3></div> </div> };
assert_eq!(a, b); // NB: assert_eq! doesn't (cannot) compare the closures
let b: VNode<CompInt> = html! { <div> <a onblur=|_| 0></a> </div> };
let a: VNode<CompInt> = html! { <div> <a onblur=|_| -> u32 { 0 } /> </div> };
assert_eq!(a, b); // NB: assert_eq! doesn't (cannot) compare the closures
// This is a known limitation of the html! macro:
//
// html! { <div> <img onblur=|_| 2 > 1 /> </div> }