From 7465f695e162856f3f1882a4ace5ae3084e0c53f Mon Sep 17 00:00:00 2001 From: gennyble Date: Tue, 9 Jan 2024 18:48:26 -0600 Subject: add some search functions --- src/lib.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) (limited to 'src/lib.rs') 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(_) => { -- cgit 1.4.1-3-g733a5