diff options
author | gennyble <gen@nyble.dev> | 2024-01-09 18:48:26 -0600 |
---|---|---|
committer | gennyble <gen@nyble.dev> | 2024-01-09 18:48:26 -0600 |
commit | 7465f695e162856f3f1882a4ace5ae3084e0c53f (patch) | |
tree | db793ca6c230779b3042b22c621d66afd4d2d6ab /src/lib.rs | |
parent | aea63e598fb9cfd02f9397b1db0deaea968a21b9 (diff) | |
download | cutie-7465f695e162856f3f1882a4ace5ae3084e0c53f.tar.gz cutie-7465f695e162856f3f1882a4ace5ae3084e0c53f.zip |
add some search functions
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 64 |
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(_) => { |