Repository: jaytaph/gosub-browser
File: src/render_tree.rs (Rust)

Code before:
            root: Rc::new(RefCell::new(Node::new())),
            position: Position::new(),
        }
    }

    pub fn build(&mut self) {
        // start with a clean root (if build is called multiple times)
        self.root = Rc::new(RefCell::new(Node::new()));

        let tree_iterator = document::TreeIterator::new(&self.document);
        let mut reference_element = Rc::clone(&self.root);

        for current_node_id in tree_iterator {
            let doc_read = self.document.get();
            if let Some(current_node) = doc_read.get_node_by_id(current_node_id) {
                match &current_node.data {
                    NodeData::Element(element) => {
                        let new_node = match element.name.as_str() {
                            "h1" => Node::new_heading1,
                            "h2" => Node::new_heading2,
                            "h3" => Node::new_heading3,
                            "h4" => Node::new_heading4,
                            "h5" => Node::new_heading5,
                            "h6" => Node::new_heading6,
                            "p" => Node::new_paragraph,
                            _ => todo!(), /* add more here, for now ignore */
                        }(&mut self.position);

                        let new_node = Rc::new(RefCell::new(new_node));

                        util::add_text_node(&reference_element, &new_node);
                        reference_element = Rc::clone(&new_node);
                    }
                    NodeData::Text(text) => {
                        let mut mut_element_ref = reference_element.as_ref().borrow_mut();
                        if let NodeType::Text(element_text) = &mut mut_element_ref.node_type {
                            element_text.value.push_str(text.value());
                        }
                    }
                    _ => { /* ignore */ }
                }
            }
        }
    }
}

/// An individual node that sits inside a RenderTree.
/// A RenderTree Node mimics a Node from the DOM but
/// contains more visual information such as width,
/// height, font sizes, colors, etc. A RenderTree Node
/// can have children just like regular DOM nodes.

Review comment (bug):
I really like this, but the only thing is I don't want `todo!()` since I believe it panics (correct me if I'm wrong.) This would break the rendering in the user agent if the element is not any of these types which I don't want; I just want to silently skip them

Code after:
            root: Rc::new(RefCell::new(Node::new())),
            position: Position::new(),
        }
    }

    pub fn build(&mut self) {
        // start with a clean root (if build is called multiple times)
        self.root = Rc::new(RefCell::new(Node::new()));

        let tree_iterator = document::TreeIterator::new(&self.document);
        let mut reference_element = Rc::clone(&self.root);

        for current_node_id in tree_iterator {
            let doc_read = self.document.get();
            if let Some(current_node) = doc_read.get_node_by_id(current_node_id) {
                match &current_node.data {
                    NodeData::Element(element) => {
                        let new_node = match element.name.as_str() {
                            "h1" => Node::new_heading1,
                            "h2" => Node::new_heading2,
                            "h3" => Node::new_heading3,
                            "h4" => Node::new_heading4,
                            "h5" => Node::new_heading5,
                            "h6" => Node::new_heading6,
                            "p" => Node::new_paragraph,
                            _ => continue, /* add more here, for now ignore */
                        }(&mut self.position);

                        let new_node = Rc::new(RefCell::new(new_node));

                        util::add_text_node(&reference_element, &new_node);
                        reference_element = Rc::clone(&new_node);
                    }
                    NodeData::Text(text) => {
                        let mut mut_element_ref = reference_element.as_ref().borrow_mut();
                        if let NodeType::Text(element_text) = &mut mut_element_ref.node_type {
                            element_text.value.push_str(text.value());
                        }
                    }
                    _ => { /* ignore */ }
                }
            }
        }
    }
}

/// An individual node that sits inside a RenderTree.
/// A RenderTree Node mimics a Node from the DOM but
/// contains more visual information such as width,
/// height, font sizes, colors, etc. A RenderTree Node
/// can have children just like regular DOM nodes.