Skip to content

Commit be083c8

Browse files
committed
Fix for stdweb
1 parent e48b803 commit be083c8

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

yew/src/virtual_dom/mod.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::rc::Rc;
2121
cfg_if! {
2222
if #[cfg(feature = "std_web")] {
2323
use crate::html::EventListener;
24-
use stdweb::web::{Element, Node};
24+
use stdweb::web::{Element, INode, Node};
2525
} else if #[cfg(feature = "web_sys")] {
2626
use gloo::events::EventListener;
2727
use web_sys::{Element, Node};
@@ -256,23 +256,47 @@ pub(crate) trait VDiff {
256256
) -> Node;
257257
}
258258

259-
fn insert_node(node: &Node, parent: &Node, node_position: &VDiffNodePosition) -> Node {
259+
#[cfg(feature = "web_sys")]
260+
fn insert_node(node: &Node, parent: &Element, node_position: &VDiffNodePosition) -> Node {
260261
match node_position {
261262
VDiffNodePosition::FirstChild => parent
262263
.insert_before(&node, parent.first_child().as_ref())
263-
.expect("can't insert tag before next sibling"),
264+
.expect("failed to insert tag before next sibling"),
264265
VDiffNodePosition::Before(next_sibling) => parent
265266
.insert_before(&node, Some(next_sibling))
266-
.expect("can't insert tag before next sibling"),
267+
.expect("failed to insert tag before next sibling"),
267268
VDiffNodePosition::After(previous_sibling) => parent
268269
.insert_before(&node, previous_sibling.next_sibling().as_ref())
269-
.expect("can't insert tag before next sibling"),
270-
VDiffNodePosition::LastChild => parent
271-
.append_child(&node)
272-
.expect("can't insert tag before next sibling"),
270+
.expect("failed to insert tag before next sibling"),
271+
VDiffNodePosition::LastChild => parent.append_child(node).expect("failed to append tag"),
273272
}
274273
}
275274

275+
#[cfg(feature = "std_web")]
276+
fn insert_node(node: &impl INode, parent: &impl INode, node_position: &VDiffNodePosition) -> Node {
277+
fn insert_before(node: &impl INode, parent: &impl INode, reference: Option<&Node>) {
278+
if let Some(reference) = reference {
279+
parent
280+
.insert_before(node, reference)
281+
.expect("failed to insert tag before next sibling");
282+
} else {
283+
parent.append_child(node);
284+
}
285+
}
286+
287+
match node_position {
288+
VDiffNodePosition::FirstChild => {
289+
insert_before(node, parent, parent.first_child().as_ref())
290+
}
291+
VDiffNodePosition::Before(next_sibling) => insert_before(node, parent, Some(next_sibling)),
292+
VDiffNodePosition::After(previous_sibling) => {
293+
insert_before(node, parent, previous_sibling.next_sibling().as_ref())
294+
}
295+
VDiffNodePosition::LastChild => parent.append_child(node),
296+
}
297+
node.as_node().clone()
298+
}
299+
276300
/// Transform properties to the expected type.
277301
pub trait Transformer<FROM, TO> {
278302
/// Transforms one type to another.

yew/src/virtual_dom/vcomp.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,16 @@ impl VDiff for VComp {
214214
let (mounted, node) = match reform {
215215
Reform::Keep(mounted) => {
216216
// Send properties update when the component is already rendered.
217-
let node = mounted.node_ref.get().expect("mounted VComp must have a node_ref");
217+
let node = mounted
218+
.node_ref
219+
.get()
220+
.expect("mounted VComp must have a node_ref");
218221
(this.replace(mounted), node)
219222
}
220223
Reform::Replace(position) => {
221224
// TODO: Revert dummy text for VComp
222225
let dummy_node = document().create_text_node("DUMMY NODE FOR COMPONENT");
223-
let node = (dummy_node.as_ref() as &Node).clone();
226+
let node: Node = dummy_node.clone().into();
224227

225228
super::insert_node(&dummy_node, parent, &position);
226229

yew/src/virtual_dom/vlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl VDiff for VList {
228228
"No node position to get node! children.len={}",
229229
self.children.len()
230230
);
231-
(parent.as_ref() as &Node).clone()
231+
parent.clone().into()
232232
}
233233
}
234234
}

yew/src/virtual_dom/vnode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl VNode {
4848
VNode::VComp(vcomp) => vcomp.node_ref.get(),
4949
VNode::VList(_) => None,
5050
VNode::VRef(node) => Some(node.clone()),
51-
VNode::VTag(vtag) => vtag.reference.as_deref().cloned(),
51+
VNode::VTag(vtag) => vtag.reference.as_ref().map(|element| element.clone().into()),
5252
VNode::VText(vtext) => vtext.reference.as_ref().map(|text_node| {
5353
cfg_match! {
5454
feature = "std_web" => text_node.as_node().clone(),

0 commit comments

Comments
 (0)