From abd621e972cfac28d2e4d9184c059df7d310aeae Mon Sep 17 00:00:00 2001 From: John-Mark Bell Date: Sun, 21 Jan 2024 22:58:44 +0000 Subject: HTML: rework iterators --- content/handlers/html/box/manager.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/content/handlers/html/box/manager.c b/content/handlers/html/box/manager.c index 6d558e41e..c70e747bf 100644 --- a/content/handlers/html/box/manager.c +++ b/content/handlers/html/box/manager.c @@ -189,24 +189,29 @@ _find_parent_element_or_document(dom_node *target) do { exc = dom_node_get_parent_node(target, &parent); dom_node_unref(target); + target = NULL; + if (exc == DOM_NO_ERR && parent != NULL) { dom_node_type node_type; - exc = dom_node_get_node_type(parent, &node_type); + + target = parent; + parent = NULL; + + exc = dom_node_get_node_type(target, &node_type); if (exc == DOM_NO_ERR && (node_type == DOM_ELEMENT_NODE || node_type == DOM_DOCUMENT_NODE)) { break; } - target = parent; } - } while (exc == DOM_NO_ERR && parent != NULL); + } while (exc == DOM_NO_ERR && target != NULL); if (exc != DOM_NO_ERR) { - dom_node_unref(parent); - parent = NULL; + dom_node_unref(target); + target = NULL; } - return parent; + return target; } static dom_exception @@ -216,22 +221,25 @@ _foreach_element_sibling(dom_node *cur, dom_node *sibling = NULL; dom_exception exc; + /* Take ref on cur (we'll release it in the loop) */ dom_node_ref(cur); do { exc = dom_node_get_next_sibling(cur, &sibling); dom_node_unref(cur); - cur = sibling; - sibling = NULL; + cur = NULL; - if (exc == DOM_NO_ERR && cur != NULL) { + if (exc == DOM_NO_ERR && sibling != NULL) { dom_node_type sibling_type; - exc = dom_node_get_node_type(cur, &sibling_type); + exc = dom_node_get_node_type(sibling, &sibling_type); if (exc == DOM_NO_ERR && sibling_type == DOM_ELEMENT_NODE) { - exc = cb(cur, pw); + exc = cb(sibling, pw); } + + cur = sibling; + sibling = NULL; } } while (exc == DOM_NO_ERR && cur != NULL); -- cgit v1.2.3