From 92f4ea58886e9e60111cf8959289d592c6b71379 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Tue, 8 Nov 2022 07:15:26 -0500 Subject: [PATCH] Fixes issue #60 --- leptos_dom/src/render.rs | 2 +- leptos_macro/src/view.rs | 47 ++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/leptos_dom/src/render.rs b/leptos_dom/src/render.rs index 16f5cda2c..0ca985a06 100644 --- a/leptos_dom/src/render.rs +++ b/leptos_dom/src/render.rs @@ -273,7 +273,7 @@ pub fn insert_expression( Child::Nodes(new_nodes.to_vec()) } } else { - clean_children(&parent, Child::Null, &Marker::NoChildren, None); + clean_children(&parent, Child::Null, before, None); append_nodes(parent, new_nodes.to_vec(), before.as_some_node().cloned()); Child::Nodes(new_nodes.to_vec()) } diff --git a/leptos_macro/src/view.rs b/leptos_macro/src/view.rs index f2ede523c..1af305e84 100644 --- a/leptos_macro/src/view.rs +++ b/leptos_macro/src/view.rs @@ -661,20 +661,25 @@ fn child_to_tokens( .map(|val| val.span()) .unwrap_or_else(Span::call_site); - *next_el_id += 1; - let name = child_ident(*next_el_id, node); - let location = if let Some(sibling) = &prev_sib { - quote_spanned! { - span => //log::debug!("-> next sibling"); - let #name = #sibling.next_sibling().unwrap_throw(); - //log::debug!("\tnext sibling = {}", #name.node_name()); - } + let (name, location) = if is_first_child && mode == Mode::Client { + (None, quote! { }) } else { - quote_spanned! { - span => //log::debug!("\\|/ first child on {}", #parent.node_name()); - let #name = #parent.first_child().unwrap_throw(); - //log::debug!("\tfirst child = {}", #name.node_name()); - } + *next_el_id += 1; + let name = child_ident(*next_el_id, node); + let location = if let Some(sibling) = &prev_sib { + quote_spanned! { + span => //log::debug!("-> next sibling"); + let #name = #sibling.next_sibling().unwrap_throw(); + //log::debug!("\tnext sibling = {}", #name.node_name()); + } + } else { + quote_spanned! { + span => //log::debug!("\\|/ first child on {}", #parent.node_name()); + let #name = #parent.first_child().unwrap_throw(); + //log::debug!("\tfirst child = {}", #name.node_name()); + } + }; + (Some(name), location) }; let before = match &next_sib { @@ -700,13 +705,19 @@ fn child_to_tokens( template.push_str(&v); } - PrevSibChange::Sib(name) + if let Some(name) = name { + PrevSibChange::Sib(name) + } else { + PrevSibChange::Parent + } } else { // these markers are one of the primary templating differences across modes match mode { // in CSR, simply insert a comment node: it will be picked up and replaced with the value Mode::Client => { - template.push_str(""); + if !is_first_child { + template.push_str(""); + } navigations.push(location); let current = match current { @@ -759,7 +770,11 @@ fn child_to_tokens( }), } - PrevSibChange::Sib(name) + if let Some(name) = name { + PrevSibChange::Sib(name) + } else { + PrevSibChange::Parent + } } } _ => panic!("unexpected child node type"),