Skip to content

Commit

Permalink
Fix braw detach
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza1311 committed Sep 3, 2022
1 parent 9a1547f commit a7ea1fc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/yew/src/dom_bundle/bnode.rs
@@ -1,5 +1,6 @@
//! This module contains the bundle version of an abstract node [BNode]

use fmt::Debug;
use std::fmt;

use web_sys::{Element, Node};
Expand Down Expand Up @@ -241,7 +242,7 @@ impl From<BRaw> for BNode {
}
}

impl fmt::Debug for BNode {
impl Debug for BNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::Tag(ref vtag) => vtag.fmt(f),
Expand All @@ -251,7 +252,7 @@ impl fmt::Debug for BNode {
Self::Ref(ref vref) => write!(f, "VRef ( \"{}\" )", crate::utils::print_node(vref)),
Self::Portal(ref vportal) => vportal.fmt(f),
Self::Suspense(ref bsusp) => bsusp.fmt(f),
Self::Raw(ref braw) => write!(f, "VRaw {{ {} }}", braw.html),
Self::Raw(ref braw) => braw.fmt(f),
}
}
}
Expand Down
36 changes: 31 additions & 5 deletions packages/yew/src/dom_bundle/braw.rs
Expand Up @@ -7,11 +7,12 @@ use crate::dom_bundle::utils::insert_node;
use crate::dom_bundle::BSubtree;
use crate::html::AnyScope;
use crate::virtual_dom::VRaw;
use crate::{AttrValue, NodeRef};
use crate::{NodeRef};

#[derive(Debug)]
pub struct BRaw {
pub html: AttrValue,
reference: NodeRef,
children_count: usize
}

impl BRaw {
Expand All @@ -30,9 +31,20 @@ impl BRaw {

fn detach_bundle(&self, parent: &Element) {
if let Some(node) = self.reference.cast::<Element>() {
let mut next_sibling = node.unchecked_ref::<Element>().next_sibling();

parent
.remove_child(&node)
.expect("failed to remove braw node");
let len = self.children_count - 1;
for _ in 0..len {
if let Some(node) = next_sibling {
next_sibling = node.clone().unchecked_ref::<Element>().next_sibling();
parent
.remove_child(&node)
.expect("failed to remove braw node");
}
}
}
}
}
Expand Down Expand Up @@ -73,13 +85,14 @@ impl Reconcilable for VRaw {
return (
next_sibling.clone(),
BRaw {
html: self.html,
reference: next_sibling,
children_count: 0,
},
);
}
let node_ref = NodeRef::default();

let count = elements.len();
let mut iter = elements.into_iter();
let first = iter.next().unwrap();
insert_node(&first, parent, next_sibling.get().as_ref());
Expand All @@ -90,8 +103,8 @@ impl Reconcilable for VRaw {
(
node_ref.clone(),
BRaw {
html: self.html,
reference: node_ref,
children_count: count,
},
)
}
Expand Down Expand Up @@ -167,7 +180,7 @@ mod tests {
fn braw_works_one_node_nested() {
let (root, scope, parent) = setup_parent();

const HTML: &str = r#"<p>one <a href="https://yew.rs">link</a> more paragraph</p>"#;
const HTML: &str = r#"<p>one <a href="https://yew.rs">link</a> more paragraph</p><div>here</div>"#;
let elem = VNode::from_raw_html(HTML.into());
let (_, mut elem) = elem.attach(&root, &scope, &parent, NodeRef::default());
assert_braw(&mut elem);
Expand All @@ -184,6 +197,19 @@ mod tests {
assert_eq!(parent.inner_html(), HTML)
}

#[test]
fn braw_detach_works() {
let (root, scope, parent) = setup_parent();

const HTML: &str = r#"<p>paragraph</p><a href="https://yew.rs">link</a>"#;
let elem = VNode::from_raw_html(HTML.into());
let (_, mut elem) = elem.attach(&root, &scope, &parent, NodeRef::default());
assert_braw(&mut elem);
assert_eq!(parent.inner_html(), HTML);
elem.detach(&root, &parent, false);
assert_eq!(parent.inner_html(), "");
}

fn assert_braw(node: &mut BNode) -> &mut BRaw {
if let BNode::Raw(braw) = node {
return braw;
Expand Down

0 comments on commit a7ea1fc

Please sign in to comment.