parse_html/
node.rs

1use crate::dom::{search_by_attr::search_node_attr, search_by_tag_name::search_node_by_tag_name};
2
3#[derive(Debug, PartialEq)]
4pub enum Node {
5    Element(ElementNode),
6    Text(String),
7}
8
9#[derive(Debug, PartialEq)]
10pub struct ElementNode {
11    pub tag_name: String,
12    pub attributes: Vec<(String, String)>,
13    pub children: Vec<Node>,
14}
15
16impl ElementNode {
17    pub fn get_by_id(&self, id_value: &str) -> Option<&ElementNode> {
18        for node in &self.children {
19            if let Some(found) = search_node_attr(node, "id", id_value) {
20                return Some(found);
21            }
22        }
23        None
24    }
25
26    pub fn get_by_class(&self, value: &str) -> Option<&ElementNode> {
27        for node in &self.children {
28            if let Some(found) = search_node_attr(node, "class", value) {
29                return Some(found);
30            }
31        }
32        None
33    }
34
35    pub fn get_by_tag_name(&self, tag_name: &str) -> Option<&ElementNode> {
36        for node in &self.children {
37            if let Some(found) = search_node_by_tag_name(node, tag_name) {
38                return Some(found);
39            }
40        }
41        None
42    }
43}