diff options
Diffstat (limited to 'src/lib.rs')
-rwxr-xr-x[-rw-r--r--] | src/lib.rs | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/lib.rs b/src/lib.rs index 8b5c47d..bc8b629 100644..100755 --- 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 <script> and <style> if root_tag.name == "script" && tag.get_attribute("src").is_none() || root_tag.name == "style" { - println!("\tparse special"); let special = Self::special_parse(rest.unwrap(), root_tag.name); match special { @@ -166,7 +188,6 @@ impl Html { // Find the closing end of out root_tag if let Some((parsed, remaining)) = Self::is_tag(rest.unwrap()) { if parsed.closing && parsed.name == root_tag.name { - println!("\tclosed {}", parsed.name); break Consumed { node: Node::Tag(tag), remaining, |