1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:57:44 +00:00

LibWeb: Implement LiveNodeList::first_matching

This function returns the first element which matches both the filter
for the LiveNodeList collection itself, and a further filter that is
supplied as an argument to this function.
This commit is contained in:
Shannon Booth 2023-08-25 12:16:44 +12:00 committed by Andreas Kling
parent 191c87f1cd
commit 708263790a
2 changed files with 25 additions and 0 deletions

View file

@ -52,6 +52,29 @@ JS::MarkedVector<Node*> LiveNodeList::collection() const
return nodes;
}
Node* LiveNodeList::first_matching(Function<bool(Node const&)> const& filter) const
{
Node* matched_node = nullptr;
if (m_scope == Scope::Descendants) {
m_root->for_each_in_subtree([&](auto& node) {
if (m_filter(node) && filter(node)) {
matched_node = const_cast<Node*>(&node);
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
} else {
m_root->for_each_child([&](auto& node) {
if (m_filter(node) && filter(node)) {
matched_node = const_cast<Node*>(&node);
return IterationDecision::Break;
}
return IterationDecision::Continue;
});
}
return matched_node;
}
// https://dom.spec.whatwg.org/#dom-nodelist-length
u32 LiveNodeList::length() const
{