1use crate::dom::{
2 search_by_attr::{search_all_element_by_attr, search_element_by_attr},
3 search_by_tag_name::{search_all_element_by_tag_name, search_element_by_tag_name},
4};
5
6#[derive(Debug, PartialEq, Clone)]
7pub enum Node {
8 Element(ElementNode),
9 Text(String),
10}
11
12#[derive(Debug, PartialEq, Clone)]
13pub struct ElementNode {
14 pub tag_name: String,
15 pub attributes: Vec<(String, String)>,
16 pub children: Vec<Node>,
17}
18
19impl ElementNode {
20 pub fn get_by_id(&self, id_value: &str) -> Option<&ElementNode> {
21 for node in &self.children {
22 if let Some(found) = search_element_by_attr(node, "id", id_value) {
23 return Some(found);
24 }
25 }
26 None
27 }
28
29 pub fn get_all_by_id(&self, id_value: &str) -> Vec<&ElementNode> {
30 let mut list_found = vec![];
31 for node in &self.children {
32 list_found.extend(search_all_element_by_attr(node, "id", id_value));
33 }
34 list_found
35 }
36
37 pub fn get_by_class(&self, value: &str) -> Option<&ElementNode> {
38 for node in &self.children {
39 if let Some(found) = search_element_by_attr(node, "class", value) {
40 return Some(found);
41 }
42 }
43 None
44 }
45
46 pub fn get_all_by_class(&self, value: &str) -> Vec<&ElementNode> {
47 let mut list_found = vec![];
48 for node in &self.children {
49 list_found.extend(search_all_element_by_attr(node, "class", value));
50 }
51 list_found
52 }
53
54 pub fn get_by_tag_name(&self, tag_name: &str) -> Option<&ElementNode> {
55 for node in &self.children {
56 if let Some(found) = search_element_by_tag_name(node, tag_name) {
57 return Some(found);
58 }
59 }
60 None
61 }
62
63 pub fn get_all_by_tag_name(&self, tag_name: &str) -> Vec<&ElementNode> {
64 let mut list_found = vec![];
65 for node in &self.children {
66 list_found.extend(search_all_element_by_tag_name(node, tag_name));
67 }
68 list_found
69 }
70
71 pub fn get_by_attr(
72 &self,
73 attributes_name: &str,
74 attributes_value: &str,
75 ) -> Option<&ElementNode> {
76 for node in &self.children {
77 if let Some(found) = search_element_by_attr(node, attributes_name, attributes_value) {
78 return Some(found);
79 }
80 }
81 None
82 }
83
84 pub fn get_all_by_attr(
85 &self,
86 attributes_name: &str,
87 attributes_value: &str,
88 ) -> Vec<&ElementNode> {
89 let mut list_found = vec![];
90 for node in &self.children {
91 list_found.extend(search_all_element_by_attr(
92 node,
93 attributes_name,
94 attributes_value,
95 ));
96 }
97 list_found
98 }
99}