@@ -1,7 +1,8 @@
11use super::change_list::ChangeList;
2−use super::node::{Attribute, Node, NodeData, NodeRef};
2+use super::node::{Attribute, ElementNode, Node, TextNode};
33use super::Render;
44use bumpalo::Bump;
5+use log::*;
56use std::cmp;
67use std::mem;
78
@@ -11,10 +12,10 @@ pub struct Vdom {
1112 container: web_sys::Element,
1213
1314 // Actually a reference into `self.dom_buffers[0]`.
14− current_root: Option<NodeRef<'static>>,
15+ current_root: Option<Node<'static>>,
1516}
1617
17−unsafe fn extend_node_lifetime<'a>(node: NodeRef<'a>) -> NodeRef<'static> {
18+unsafe fn extend_node_lifetime<'a>(node: Node<'a>) -> Node<'static> {
1819 mem::transmute(node)
1920}
2021
@@ -30,7 +31,7 @@ impl Vdom {
3031 container.set_inner_html("");
3132
3233 // Create a dummy `<div/>` in our container.
33− let current_root: NodeRef = dom_buffers[0].alloc(Node::element("div", [], [])).into();
34+ let current_root = Node::element(&dom_buffers[0], "div", [], []);
3435 let current_root = Some(unsafe { extend_node_lifetime(current_root) });
3536 let window = web_sys::window().expect("should have acess to the Window");
3637 let document = window
@@ -85,53 +86,55 @@ impl Vdom {
8586 mem::swap(&mut first[0], &mut second[0]);
8687 }
8788
88− unsafe fn set_current_root(&mut self, current: NodeRef<'static>) {
89+ unsafe fn set_current_root(&mut self, current: Node<'static>) {
8990 debug_assert!(self.current_root.is_none());
9091 self.current_root = Some(current);
9192 }
9293
93− fn diff(&self, old: NodeRef, new: NodeRef) {
94− debug!("---------------------------------------------------------");
95− debug!("dodrio::Vdom::diff");
96− debug!(" old = {:#?}", old);
97− debug!(" new = {:#?}", new);
98− match (new.data, old.data) {
99− (NodeData::Text { text: new_text }, NodeData::Text { text: old_text }) => {
94+ fn diff(&self, old: Node, new: Node) {
95+ // debug!("---------------------------------------------------------");
96+ // debug!("dodrio::Vdom::diff");
97+ // debug!(" old = {:#?}", old);
98+ // debug!(" new = {:#?}", new);
99+ match (&new, old) {
100+ (&Node::Text(TextNode { text: new_text }), Node::Text(TextNode { text: old_text })) => {
100101 debug!(" both are text nodes");
101102 if new_text != old_text {
102103 debug!(" text needs updating");
103104 self.change_list.emit_set_text(new_text);
104105 }
105106 }
106− (NodeData::Text { .. }, NodeData::Element { .. }) => {
107+ (&Node::Text(TextNode { .. }), Node::Element(ElementNode { .. })) => {
107108 debug!(" replacing a text node with an element");
108109 self.create(new);
109110 self.change_list.emit_replace_with();
110111 }
111− (NodeData::Element { .. }, NodeData::Text { .. }) => {
112+ (&Node::Element(ElementNode { .. }), Node::Text(TextNode { .. })) => {
112113 debug!(" replacing an element with a text node");
113114 self.create(new);
114115 self.change_list.emit_replace_with();
115116 }
116117 (
117− NodeData::Element {
118+ &Node::Element(ElementNode {
118119 tag_name: new_tag_name,
119− },
120− NodeData::Element {
120+ attributes: new_attributes,
121+ children: new_children,
122+ }),
123+ Node::Element(ElementNode {
121124 tag_name: old_tag_name,
122− },
125+ attributes: old_attributes,
126+ children: old_children,
127+ }),
123128 ) => {
124129 debug!(" updating an element");
125−
126130 if new_tag_name != old_tag_name {
127131 debug!(" different tag names; creating new element and replacing old element");
128132 self.create(new);
129133 self.change_list.emit_replace_with();
130134 return;
131135 }
132−
133− self.diff_attributes(old.attributes, new.attributes);
134− self.diff_children(old.children, new.children);
136+ self.diff_attributes(old_attributes, new_attributes);
137+ self.diff_children(old_children, new_children);
135138 }
136139 }
137140 }
@@ -165,7 +168,7 @@ impl Vdom {
165168 }
166169 }
167170
168− fn diff_children(&self, old: &[NodeRef], new: &[NodeRef]) {
171+ fn diff_children(&self, old: &[Node], new: &[Node]) {
169172 debug!(" updating children shared by old and new");
170173
171174 let num_children_to_diff = cmp::min(new.len(), old.len());
@@ -217,18 +220,22 @@ impl Vdom {
217220 }
218221 }
219222
220− fn create(&self, node: NodeRef) {
221− debug!("dodrio::Vdom::create({:#?})", node);
222− match node.data {
223− NodeData::Text { text } => {
223+ fn create(&self, node: Node) {
224+ // debug!("dodrio::Vdom::create({:#?})", node);
225+ match node {
226+ Node::Text(TextNode { text }) => {
224227 self.change_list.emit_create_text_node(text);
225228 }
226− NodeData::Element { tag_name } => {
229+ Node::Element(ElementNode {
230+ tag_name,
231+ attributes,
232+ children,
233+ }) => {
227234 self.change_list.emit_create_element(tag_name);
228− for attr in node.attributes {
235+ for attr in attributes {
229236 self.change_list.emit_set_attribute(&attr.name, &attr.value);
230237 }
231− for child in node.children {
238+ for child in children {
232239 self.create(child.clone());
233240 self.change_list.emit_append_child();
234241 }