Skip to content

Commit

Permalink
Special handling for href
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed May 28, 2019
1 parent 4011a7c commit 58c69d3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/macro-impl/src/html_tree/html_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ impl ToTokens for HtmlTag {
checked,
disabled,
selected,
href,
listeners,
} = &open.attributes;
let attr_names = attributes.iter().map(|attr| attr.name.to_string());
Expand Down Expand Up @@ -130,6 +131,12 @@ impl ToTokens for HtmlTag {
}
}
});
let add_href = href.iter().map(|href| {
quote! {
let __yew_href: $crate::html::Href = #href.into();
__yew_vtag.add_attribute("href", &__yew_href);
}
});
let set_classes = classes.iter().map(|classes_form| match classes_form {
ClassesForm::Tuple(classes) => quote! { #(__yew_vtag.add_class(&(#classes));)* },
ClassesForm::Single(classes) => quote! {
Expand All @@ -147,6 +154,7 @@ impl ToTokens for HtmlTag {
#(#set_checked)*
#(#add_disabled)*
#(#add_selected)*
#(#add_href)*
#(__yew_vtag.add_child(#children);)*
__yew_vtag
}});
Expand Down Expand Up @@ -219,6 +227,7 @@ struct TagAttributes {
checked: Option<Expr>,
disabled: Option<Expr>,
selected: Option<Expr>,
href: Option<Expr>,
}

enum ClassesForm {
Expand Down Expand Up @@ -394,6 +403,7 @@ impl Parse for TagAttributes {
let checked = TagAttributes::remove_attr(&mut attributes, "checked");
let disabled = TagAttributes::remove_attr(&mut attributes, "disabled");
let selected = TagAttributes::remove_attr(&mut attributes, "selected");
let href = TagAttributes::remove_attr(&mut attributes, "href");

Ok(TagAttributes {
attributes,
Expand All @@ -404,6 +414,7 @@ impl Parse for TagAttributes {
checked,
disabled,
selected,
href,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/macro/tests/html-tag-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ test_html! { |t31| <input disabled=1 /> }
test_html! { |t32| <option selected=1 /> }
test_html! { |t33| <input type=() /> }
test_html! { |t34| <input value=() /> }
test_html! { |t35| <a href=() /> }

fn main() {}
12 changes: 12 additions & 0 deletions crates/macro/tests/html-tag-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,17 @@ error[E0277]: `()` doesn't implement `std::fmt::Display`
= note: required because of the requirements on the impl of `std::string::ToString` for `()`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0277]: the trait bound `yew_shared::html::Href: std::convert::From<()>` is not satisfied
--> $DIR/html-tag-fail.rs:26:1
|
26 | test_html! { |t35| <a href=() /> }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<()>` is not implemented for `yew_shared::html::Href`
|
= help: the following implementations were found:
<yew_shared::html::Href as std::convert::From<&'a str>>
<yew_shared::html::Href as std::convert::From<std::string::String>>
= note: required because of the requirements on the impl of `std::convert::Into<yew_shared::html::Href>` for `()`
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

Some errors occurred: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
1 change: 1 addition & 0 deletions crates/macro/tests/html-tag-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test_html! { |t1|
<img class=("avatar", "hidden") src="http://pic.com" />
<img class="avatar hidden", />
<button onclick=|e| panic!(e) />
<a href="http://google.com" />
</div>
}

Expand Down

0 comments on commit 58c69d3

Please sign in to comment.