mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 21:37:36 +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;
|
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
|
// https://dom.spec.whatwg.org/#dom-nodelist-length
|
||||||
u32 LiveNodeList::length() const
|
u32 LiveNodeList::length() const
|
||||||
{
|
{
|
||||||
|
|
|
@ -34,6 +34,8 @@ public:
|
||||||
protected:
|
protected:
|
||||||
LiveNodeList(JS::Realm&, Node& root, Scope, Function<bool(Node const&)> filter);
|
LiveNodeList(JS::Realm&, Node& root, Scope, Function<bool(Node const&)> filter);
|
||||||
|
|
||||||
|
Node* first_matching(Function<bool(Node const&)> const& filter) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void visit_edges(Cell::Visitor&) override;
|
virtual void visit_edges(Cell::Visitor&) override;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue