-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add syntax to bind to properties instead of attributes #2819
Changes from 12 commits
971eaa8
2177f8c
27efd0a
866dbe6
b0c0126
0bb8cd5
66e714c
ea353cc
43ab9d0
9b418bf
5e8c4c4
00deee5
0107f03
1eea41a
3b9fa16
b8cf9b8
ec4f599
dfd2082
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,16 +14,18 @@ use crate::html_tree::HtmlDashedName; | |
use crate::stringify::Stringify; | ||
|
||
pub struct Prop { | ||
pub is_forced_attribute: bool, | ||
ranile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub label: HtmlDashedName, | ||
/// Punctuation between `label` and `value`. | ||
pub value: Expr, | ||
} | ||
impl Parse for Prop { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let at = input.parse::<Token![@]>().map(|_| true).unwrap_or(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's discuss some syntax and why I think using
My preferred solution: use Can we also support a syntax such as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #2819 (comment) |
||
if input.peek(Brace) { | ||
Self::parse_shorthand_prop_assignment(input) | ||
Self::parse_shorthand_prop_assignment(input, at) | ||
} else { | ||
Self::parse_prop_assignment(input) | ||
Self::parse_prop_assignment(input, at) | ||
} | ||
} | ||
} | ||
|
@@ -33,7 +35,10 @@ impl Prop { | |
/// Parse a prop using the shorthand syntax `{value}`, short for `value={value}` | ||
/// This only allows for labels with no hyphens, as it would otherwise create | ||
/// an ambiguity in the syntax | ||
fn parse_shorthand_prop_assignment(input: ParseStream) -> syn::Result<Self> { | ||
fn parse_shorthand_prop_assignment( | ||
input: ParseStream, | ||
is_forced_attribute: bool, | ||
) -> syn::Result<Self> { | ||
let value; | ||
let _brace = braced!(value in input); | ||
let expr = value.parse::<Expr>()?; | ||
|
@@ -44,7 +49,7 @@ impl Prop { | |
}) = expr | ||
{ | ||
if let (Some(ident), true) = (path.get_ident(), attrs.is_empty()) { | ||
syn::Result::Ok(HtmlDashedName::from(ident.clone())) | ||
Ok(HtmlDashedName::from(ident.clone())) | ||
} else { | ||
Err(syn::Error::new_spanned( | ||
path, | ||
|
@@ -59,11 +64,15 @@ impl Prop { | |
)); | ||
}?; | ||
|
||
Ok(Self { label, value: expr }) | ||
Ok(Self { | ||
label, | ||
value: expr, | ||
is_forced_attribute, | ||
}) | ||
} | ||
|
||
/// Parse a prop of the form `label={value}` | ||
fn parse_prop_assignment(input: ParseStream) -> syn::Result<Self> { | ||
fn parse_prop_assignment(input: ParseStream, is_forced_attribute: bool) -> syn::Result<Self> { | ||
let label = input.parse::<HtmlDashedName>()?; | ||
let equals = input.parse::<Token![=]>().map_err(|_| { | ||
syn::Error::new_spanned( | ||
|
@@ -83,7 +92,11 @@ impl Prop { | |
} | ||
|
||
let value = parse_prop_value(input)?; | ||
Ok(Self { label, value }) | ||
Ok(Self { | ||
label, | ||
value, | ||
is_forced_attribute, | ||
}) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For properties, maybe we should apply a snake_case to camelCase conversion here like
web_sys
.We only need to do this with
html!
macro as at runtime property names are set asstring
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is a good idea. We don't convert
snake_case
tokebab-case
for attributes so this will be just another differentiator between the two. We can add it for both though, but that is for a future PR