@@ -1,7 +1,8 @@
1use super::change_list::ChangeList;
1use super::change_list::ChangeList;
2−use super::node::{Attribute, Node, NodeData, NodeRef};
2+use super::node::{Attribute, ElementNode, Node, TextNode};
3use super::Render;
3use super::Render;
4use bumpalo::Bump;
4use bumpalo::Bump;
5use std::cmp;
6use std::cmp;
6use std::mem;
7use std::mem;
@@ -11,10 +12,10 @@ pub struct Vdom {
11 container: web_sys::Element,
12 container: web_sys::Element,
13 // Actually a reference into `self.dom_buffers[0]`.
14 // Actually a reference into `self.dom_buffers[0]`.
14− current_root: Option<NodeRef<'static>>,
15+ current_root: Option<Node<'static>>,
17−unsafe fn extend_node_lifetime<'a>(node: NodeRef<'a>) -> NodeRef<'static> {
18+unsafe fn extend_node_lifetime<'a>(node: Node<'a>) -> Node<'static> {
18 mem::transmute(node)
19 mem::transmute(node)
@@ -30,7 +31,7 @@ impl Vdom {
30 container.set_inner_html("");
31 container.set_inner_html("");
32 // Create a dummy `<div/>` in our container.
33 // 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", [], []);
34 let current_root = Some(unsafe { extend_node_lifetime(current_root) });
35 let current_root = Some(unsafe { extend_node_lifetime(current_root) });
35 let window = web_sys::window().expect("should have acess to the Window");
36 let window = web_sys::window().expect("should have acess to the Window");
36 let document = window
37 let document = window
@@ -85,53 +86,55 @@ impl Vdom {
85 mem::swap(&mut first[0], &mut second[0]);
86 mem::swap(&mut first[0], &mut second[0]);
88− unsafe fn set_current_root(&mut self, current: NodeRef<'static>) {
89+ unsafe fn set_current_root(&mut self, current: Node<'static>) {
89 debug_assert!(self.current_root.is_none());
90 debug_assert!(self.current_root.is_none());
90 self.current_root = Some(current);
91 self.current_root = Some(current);
93− fn diff(&self, old: NodeRef, new: NodeRef) {
94+ fn diff(&self, old: Node, new: Node) {
94− debug!("---------------------------------------------------------");
95+ // debug!("---------------------------------------------------------");
95− debug!("dodrio::Vdom::diff");
96+ // debug!("dodrio::Vdom::diff");
96− debug!(" old = {:#?}", old);
97+ // debug!(" old = {:#?}", old);
97− debug!(" new = {:#?}", new);
98+ // debug!(" new = {:#?}", new);
98− match (new.data, old.data) {
99+ match (&new, old) {
99− (NodeData::Text { text: new_text }, NodeData::Text { text: old_text }) => {
100+ (&Node::Text(TextNode { text: new_text }), Node::Text(TextNode { text: old_text })) => {
100 debug!(" both are text nodes");
101 debug!(" both are text nodes");
101 if new_text != old_text {
102 if new_text != old_text {
102 debug!(" text needs updating");
103 debug!(" text needs updating");
103 self.change_list.emit_set_text(new_text);
104 self.change_list.emit_set_text(new_text);
106− (NodeData::Text { .. }, NodeData::Element { .. }) => {
107+ (&Node::Text(TextNode { .. }), Node::Element(ElementNode { .. })) => {
107 debug!(" replacing a text node with an element");
108 debug!(" replacing a text node with an element");
108 self.create(new);
109 self.create(new);
109 self.change_list.emit_replace_with();
110 self.change_list.emit_replace_with();
111− (NodeData::Element { .. }, NodeData::Text { .. }) => {
112+ (&Node::Element(ElementNode { .. }), Node::Text(TextNode { .. })) => {
112 debug!(" replacing an element with a text node");
113 debug!(" replacing an element with a text node");
113 self.create(new);
114 self.create(new);
114 self.change_list.emit_replace_with();
115 self.change_list.emit_replace_with();
117− NodeData::Element {
118+ &Node::Element(ElementNode {
118 tag_name: new_tag_name,
119 tag_name: new_tag_name,
120+ attributes: new_attributes,
121+ children: new_children,
120− NodeData::Element {
123+ Node::Element(ElementNode {
121 tag_name: old_tag_name,
124 tag_name: old_tag_name,
125+ attributes: old_attributes,
126+ children: old_children,
124 debug!(" updating an element");
129 debug!(" updating an element");
126 if new_tag_name != old_tag_name {
130 if new_tag_name != old_tag_name {
127 debug!(" different tag names; creating new element and replacing old element");
131 debug!(" different tag names; creating new element and replacing old element");
128 self.create(new);
132 self.create(new);
129 self.change_list.emit_replace_with();
133 self.change_list.emit_replace_with();
133− self.diff_attributes(old.attributes, new.attributes);
136+ self.diff_attributes(old_attributes, new_attributes);
134− self.diff_children(old.children, new.children);
137+ self.diff_children(old_children, new_children);
@@ -165,7 +168,7 @@ impl Vdom {
168− fn diff_children(&self, old: &[NodeRef], new: &[NodeRef]) {
171+ fn diff_children(&self, old: &[Node], new: &[Node]) {
169 debug!(" updating children shared by old and new");
172 debug!(" updating children shared by old and new");
171 let num_children_to_diff = cmp::min(new.len(), old.len());
174 let num_children_to_diff = cmp::min(new.len(), old.len());
@@ -217,18 +220,22 @@ impl Vdom {
220− fn create(&self, node: NodeRef) {
223+ fn create(&self, node: Node) {
221− debug!("dodrio::Vdom::create({:#?})", node);
224+ // debug!("dodrio::Vdom::create({:#?})", node);
222− match node.data {
225+ match node {
223− NodeData::Text { text } => {
226+ Node::Text(TextNode { text }) => {
224 self.change_list.emit_create_text_node(text);
227 self.change_list.emit_create_text_node(text);
229+ Node::Element(ElementNode {
226− NodeData::Element { tag_name } => {
230+ tag_name,
227 self.change_list.emit_create_element(tag_name);
234 self.change_list.emit_create_element(tag_name);
228− for attr in node.attributes {
235+ for attr in attributes {
229 self.change_list.emit_set_attribute(&attr.name, &attr.value);
236 self.change_list.emit_set_attribute(&attr.name, &attr.value);
231− for child in node.children {
238+ for child in children {
232 self.create(child.clone());
239 self.create(child.clone());
233 self.change_list.emit_append_child();
240 self.change_list.emit_append_child();