From 16067daa9517ddbda56720c077875f522202ff34 Mon Sep 17 00:00:00 2001 From: Amanda Stjerna Date: Sat, 2 Dec 2023 21:16:30 +0100 Subject: [PATCH] Recursion -> loop --- src/parser/tag.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/parser/tag.rs b/src/parser/tag.rs index 94b0e23..feaff35 100644 --- a/src/parser/tag.rs +++ b/src/parser/tag.rs @@ -590,18 +590,22 @@ impl<'a, 'b> ChildrenMut<'a, 'b> { /// Attempts to find the very last node handle that is contained in the given tag fn find_last_node_handle<'a>(tag: &HTMLTag<'a>, parser: &Parser<'a>) -> Option { - let last_handle = tag._children.as_slice().last().copied()?; - - let child = last_handle - .get(parser) - .expect("Failed to get child node, please open a bug report") // this shouldn't happen - .as_tag(); - - if let Some(child) = child { - // Recursively call this function to get to the innermost node - find_last_node_handle(child, parser).or(Some(last_handle)) - } else { - Some(last_handle) + let mut tag = tag; + let mut last_handle = None; + + loop { + if let Some(last_child_handle) = tag._children.as_slice().last().copied() { + last_handle = Some(last_child_handle); + if let Some(child) = last_child_handle + .get(parser) + .expect("Failed to get child node, please open a bug report") // this shouldn't happen + .as_tag() + { + tag = child; // Continue looking at the child + continue; + } + }; + break last_handle; } }