Skip to content
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 safe first_node fn #2094

Merged
merged 1 commit into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/yew/src/virtual_dom/vcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ mod tests {
let test_node: Node = document().create_text_node("test").into();
let test_node_ref = NodeRef::new(test_node);
let check_node_ref = |vnode: VNode| {
assert_eq!(vnode.first_node(), test_node_ref.get().unwrap());
assert_eq!(vnode.unchecked_first_node(), test_node_ref.get().unwrap());
};

let props = Props {
Expand Down
25 changes: 22 additions & 3 deletions packages/yew/src/virtual_dom/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use gloo::console;
use std::cmp::PartialEq;
use std::fmt;
use std::iter::FromIterator;
use wasm_bindgen::JsCast;

use web_sys::{Element, Node};

Expand Down Expand Up @@ -45,8 +46,23 @@ impl VNode {
}
}

/// Returns the first DOM node if available
pub(crate) fn first_node(&self) -> Option<Node> {
match self {
VNode::VTag(vtag) => vtag.reference().cloned().map(JsCast::unchecked_into),
VNode::VText(vtext) => vtext
.reference
.as_ref()
.cloned()
.map(JsCast::unchecked_into),
VNode::VComp(vcomp) => vcomp.node_ref.get(),
VNode::VList(vlist) => vlist.get(0).and_then(VNode::first_node),
VNode::VRef(node) => Some(node.clone()),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VNode::VList(vlist) => vlist.get(0).and_then(VNode::first_node)

would vlist be empty and cause out of index range error?

Copy link
Member

@siku2 siku2 Dec 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VList::get is the same as Vec::get, so it returns Option::None when the index is out of bounds.

}
}

/// Returns the first DOM node that is used to designate the position of the virtual DOM node.
pub(crate) fn first_node(&self) -> Node {
pub(crate) fn unchecked_first_node(&self) -> Node {
match self {
VNode::VTag(vtag) => vtag
.reference()
Expand All @@ -67,7 +83,10 @@ impl VNode {
crate::virtual_dom::vcomp::get_event_log(vcomp.id),
);
}),
VNode::VList(vlist) => vlist.get(0).expect("VList is not mounted").first_node(),
VNode::VList(vlist) => vlist
.get(0)
.expect("VList is not mounted")
.unchecked_first_node(),
VNode::VRef(node) => node.clone(),
}
}
Expand All @@ -85,7 +104,7 @@ impl VNode {
.expect("VComp has no root vnode")
.move_before(parent, next_sibling);
}
_ => super::insert_node(&self.first_node(), parent, next_sibling.as_ref()),
_ => super::insert_node(&self.unchecked_first_node(), parent, next_sibling.as_ref()),
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ impl VDiff for VTag {
}
} else {
let el = self.create_element(parent);
super::insert_node(&el, parent, Some(&ancestor.first_node()));
super::insert_node(&el, parent, ancestor.first_node().as_ref());
ancestor.detach(parent);
(None, el)
}
Expand Down