From 5ea6a13ead2a5cab0f578c8af5f0e0be88c3a08c Mon Sep 17 00:00:00 2001 From: gennyble Date: Wed, 18 Dec 2024 00:27:34 -0600 Subject: Ahhhhh --- src/lib.rs | 29 +++++++++++++++++++++++++---- src/query.rs | 0 src/tag.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) mode change 100644 => 100755 src/lib.rs mode change 100644 => 100755 src/query.rs mode change 100644 => 100755 src/tag.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs old mode 100644 new mode 100755 index 8b5c47d..bc8b629 --- a/src/lib.rs +++ b/src/lib.rs @@ -87,6 +87,31 @@ impl Html { None } + pub fn get_by_id(&self, id: &str) -> Option<&Tag> { + // depth first + fn find_node<'a>(tag: &'a Tag, id: &str) -> Option<&'a Tag> { + if tag.id().unwrap_or_default() == id { + return Some(tag); + } + + for child in tag.child_tags() { + if let Some(tag) = find_node(child, id) { + return Some(tag); + } + } + + None + } + + for child in self.child_tags() { + if let Some(tag) = find_node(child, id) { + return Some(tag); + } + } + + None + } + fn parse_node(raw: &str) -> Consumed { match Self::is_tag(raw) { Some(_) => { @@ -133,14 +158,11 @@ impl Html { }; } - println!("Looking for {}", root_tag.name); - loop { // Special case