LibWeb: Add Node::for_each(_inclusive)_ancestor()

Allows for easy iteration over the chain of ancestors for a node.
This commit is contained in:
Jelle Raaijmakers 2025-01-09 10:54:45 +01:00 committed by Andreas Kling
commit d08febcf67
Notes: github-actions[bot] 2025-01-10 22:39:07 +00:00
3 changed files with 83 additions and 53 deletions

View file

@ -589,6 +589,36 @@ public:
return TraversalDecision::Continue;
}
template<typename Callback>
void for_each_ancestor(Callback callback) const
{
return const_cast<Node*>(this)->for_each_ancestor(move(callback));
}
template<typename Callback>
void for_each_ancestor(Callback callback)
{
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
if (callback(*ancestor) == IterationDecision::Break)
break;
}
}
template<typename Callback>
void for_each_inclusive_ancestor(Callback callback) const
{
return const_cast<Node*>(this)->for_each_inclusive_ancestor(move(callback));
}
template<typename Callback>
void for_each_inclusive_ancestor(Callback callback)
{
for (auto* ancestor = this; ancestor; ancestor = ancestor->parent()) {
if (callback(*ancestor) == IterationDecision::Break)
break;
}
}
template<typename Callback>
void for_each_child(Callback callback) const
{