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:
parent
191c87f1cd
commit
708263790a
2 changed files with 25 additions and 0 deletions
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue