about summary refs log tree commit diff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs64
1 files changed, 63 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 3077220..8b5c47d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,5 +1,5 @@
 use core::fmt;
-use tag::Tag;
+pub use tag::{Tag, TagIterator, TagIteratorMut};
 
 //mod query;
 mod tag;
@@ -25,6 +25,68 @@ impl Html {
 		}
 	}
 
+	pub fn child_tags(&self) -> TagIterator {
+		TagIterator {
+			inner: self.nodes.iter(),
+		}
+	}
+
+	pub fn child_tags_mut(&mut self) -> TagIteratorMut {
+		TagIteratorMut {
+			inner: self.nodes.iter_mut(),
+		}
+	}
+
+	pub fn get_by_tag_name_mut(&mut self, looking: &str) -> Option<&mut Tag> {
+		// depth first
+		fn find_node<'a>(tag: &'a mut Tag, looking: &str) -> Option<&'a mut Tag> {
+			if tag.name.as_str() == looking {
+				return Some(tag);
+			}
+
+			for child in tag.child_tags_mut() {
+				if let Some(tag) = find_node(child, looking) {
+					return Some(tag);
+				}
+			}
+
+			None
+		}
+
+		for child in self.child_tags_mut() {
+			if let Some(tag) = find_node(child, looking) {
+				return Some(tag);
+			}
+		}
+
+		None
+	}
+
+	pub fn get_parent_that_contains_tag_name_mut(&mut self, looking: &str) -> Option<&mut Tag> {
+		// depth first
+		fn find_node<'a>(tag: &'a mut Tag, looking: &str) -> Option<&'a mut Tag> {
+			if tag.has_tag(looking) {
+				return Some(tag);
+			}
+
+			for child_tag in tag.child_tags_mut() {
+				if let Some(tag) = find_node(child_tag, looking) {
+					return Some(tag);
+				}
+			}
+
+			None
+		}
+
+		for child_tag in self.child_tags_mut() {
+			if let Some(tag) = find_node(child_tag, looking) {
+				return Some(tag);
+			}
+		}
+
+		None
+	}
+
 	fn parse_node(raw: &str) -> Consumed {
 		match Self::is_tag(raw) {
 			Some(_) => {