From 9358f108c4a090bc14858666a2ea7ebf15b7f727 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 17 Jan 2022 11:38:14 +0100 Subject: [PATCH] LibWeb: Add TreeNode::next_in_pre_order(T* stay_within) variant This is a scoped variant of the pre-order traversal helper that aborts when attempting to leave the `stay_within` node. --- Userland/Libraries/LibWeb/TreeNode.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Userland/Libraries/LibWeb/TreeNode.h b/Userland/Libraries/LibWeb/TreeNode.h index 2a8da45b8d..8a9cc61bb8 100644 --- a/Userland/Libraries/LibWeb/TreeNode.h +++ b/Userland/Libraries/LibWeb/TreeNode.h @@ -140,11 +140,31 @@ public: return node; } + T* next_in_pre_order(T const* stay_within) + { + if (first_child()) + return first_child(); + + T* node = static_cast(this); + T* next = nullptr; + while (!(next = node->next_sibling())) { + node = node->parent(); + if (!node || node == stay_within) + return nullptr; + } + return next; + } + T const* next_in_pre_order() const { return const_cast(this)->next_in_pre_order(); } + T const* next_in_pre_order(T const* stay_within) const + { + return const_cast(this)->next_in_pre_order(stay_within); + } + T* previous_in_pre_order() { if (auto* node = previous_sibling()) {