Skip to content

Ensure html_nested returns the inner type #3493

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions packages/yew-macro/src/html_tree/html_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,42 +330,36 @@ impl ToTokens for HtmlElement {
let node = match &*name {
"input" => {
quote! {
::std::convert::Into::<::yew::virtual_dom::VNode>::into(
::yew::virtual_dom::VTag::__new_input(
#value,
#checked,
#node_ref,
#key,
#attributes,
#listeners,
),
)
}
}
"textarea" => {
quote! {
::std::convert::Into::<::yew::virtual_dom::VNode>::into(
::yew::virtual_dom::VTag::__new_textarea(
#value,
#node_ref,
#key,
#attributes,
#listeners,
),
)
}
}
_ => {
quote! {
::std::convert::Into::<::yew::virtual_dom::VNode>::into(
::yew::virtual_dom::VTag::__new_other(
::yew::virtual_dom::AttrValue::Static(#name),
#node_ref,
#key,
#attributes,
#listeners,
#children,
),
)
}
}
Expand Down
4 changes: 1 addition & 3 deletions packages/yew-macro/src/html_tree/html_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ impl ToTokens for HtmlList {
};

tokens.extend(quote_spanned! {spanned.span()=>
::yew::virtual_dom::VNode::VList(::std::rc::Rc::new(
::yew::virtual_dom::VList::with_children(#children, #key)
))
::yew::virtual_dom::VList::with_children(#children, #key)
});
}
}
Expand Down
7 changes: 3 additions & 4 deletions packages/yew-macro/src/html_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ impl ToTokens for HtmlTree {
fn to_tokens(&self, tokens: &mut TokenStream) {
lint::lint_all(self);
match self {
// this is consistent with VNode::default()
HtmlTree::Empty => tokens.extend(quote! {
<::yew::virtual_dom::VNode as ::std::default::Default>::default()
::yew::virtual_dom::VList::new()
}),
HtmlTree::Component(comp) => comp.to_tokens(tokens),
HtmlTree::Element(tag) => tag.to_tokens(tokens),
Expand Down Expand Up @@ -375,9 +376,7 @@ impl ToTokens for HtmlRootBraced {

tokens.extend(quote_spanned! {brace.span.span()=>
{
::yew::virtual_dom::VNode::VList(::std::rc::Rc::new(
::yew::virtual_dom::VList::with_children(#children, ::std::option::Option::None)
))
::yew::virtual_dom::VList::with_children(#children, ::std::option::Option::None)
}
});
}
Expand Down
16 changes: 15 additions & 1 deletion packages/yew/src/html/conversion/into_prop_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use implicit_clone::ImplicitClone;

use crate::callback::Callback;
use crate::html::{BaseComponent, ChildrenRenderer, Component, NodeRef, Scope};
use crate::virtual_dom::{AttrValue, VChild, VList, VNode, VText};
use crate::virtual_dom::{AttrValue, VChild, VList, VNode, VTag, VText};

impl ImplicitClone for NodeRef {}
impl<Comp: Component> ImplicitClone for Scope<Comp> {}
Expand Down Expand Up @@ -153,6 +153,13 @@ impl IntoPropValue<VNode> for VText {
}
}

impl IntoPropValue<VNode> for VTag {
#[inline]
fn into_prop_value(self) -> VNode {
VNode::VTag(Rc::new(self))
}
}

impl IntoPropValue<VNode> for () {
#[inline]
fn into_prop_value(self) -> VNode {
Expand Down Expand Up @@ -181,6 +188,13 @@ impl IntoPropValue<ChildrenRenderer<VNode>> for VText {
}
}

impl IntoPropValue<ChildrenRenderer<VNode>> for VTag {
#[inline]
fn into_prop_value(self) -> ChildrenRenderer<VNode> {
ChildrenRenderer::new(vec![self.into()])
}
}

impl IntoPropValue<VList> for ChildrenRenderer<VNode> {
#[inline]
fn into_prop_value(self) -> VList {
Expand Down
34 changes: 34 additions & 0 deletions packages/yew/src/virtual_dom/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,37 @@ mod feat_ssr {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{function_component, html_nested, Html};

#[function_component]
fn Comp() -> Html {
todo!()
}

#[test]
fn html_nested_types() {
let _vlist: VList = html_nested! {
<>
<div>{"Hello"}</div>
<div>{"World"}</div>
</>
};
let _vlist2: VList = html_nested! {};
let _vtext: VText = html_nested!("");
let _vcomp: VChild<Comp> = html_nested! { <Comp /> };
let _vtag: VTag = html_nested! { <div></div> };
let _vtag: VTag = html_nested! { <textarea /> };
let _vtag: VTag = html_nested! { <input /> };

let _vif: VList = html_nested! {
if true {
<div>
</div>
}
};
}
}