mirror of
https://github.com/SerenityOS/serenity
synced 2026-05-05 06:32:26 +02:00
LibWeb: Use IterationDecision in single level Node iteration methods
`Node::for_each_child()` and `Node::for_each_child_of_type()` callbacks now return an `IterationDecision`, which allows us to break early if required.
This commit is contained in:
committed by
Andrew Kaster
parent
b5bed37074
commit
c57d395a48
@@ -300,6 +300,7 @@ String Node::child_text_content() const
|
||||
if (maybe_content.has_value())
|
||||
builder.append(maybe_content.value());
|
||||
}
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
return MUST(builder.to_string());
|
||||
}
|
||||
@@ -903,6 +904,7 @@ JS::NonnullGCPtr<Node> Node::clone_node(Document* document, bool clone_children)
|
||||
if (clone_children) {
|
||||
for_each_child([&](auto& child) {
|
||||
MUST(copy->append_child(child.clone_node(document, true)));
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1032,6 +1034,7 @@ Vector<JS::Handle<Node>> Node::children_as_vector() const
|
||||
|
||||
for_each_child([&](auto& child) {
|
||||
nodes.append(JS::make_handle(child));
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
return nodes;
|
||||
@@ -1223,10 +1226,11 @@ void Node::serialize_tree_as_json(JsonObjectSerializer<StringBuilder>& object) c
|
||||
auto children = MUST(object.add_array("children"sv));
|
||||
auto add_child = [&children](DOM::Node const& child) {
|
||||
if (child.is_uninteresting_whitespace_node())
|
||||
return;
|
||||
return IterationDecision::Continue;
|
||||
JsonObjectSerializer<StringBuilder> child_object = MUST(children.add_object());
|
||||
child.serialize_tree_as_json(child_object);
|
||||
MUST(child_object.finish());
|
||||
return IterationDecision::Continue;
|
||||
};
|
||||
for_each_child(add_child);
|
||||
|
||||
@@ -1741,6 +1745,7 @@ void Node::build_accessibility_tree(AccessibilityTreeNode& parent)
|
||||
if (document_element->has_child_nodes())
|
||||
document_element->for_each_child([&parent](DOM::Node& child) {
|
||||
child.build_accessibility_tree(parent);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
} else if (is_element()) {
|
||||
@@ -1755,11 +1760,13 @@ void Node::build_accessibility_tree(AccessibilityTreeNode& parent)
|
||||
if (has_child_nodes()) {
|
||||
for_each_child([¤t_node](DOM::Node& child) {
|
||||
child.build_accessibility_tree(*current_node);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
} else if (has_child_nodes()) {
|
||||
for_each_child([&parent](DOM::Node& child) {
|
||||
child.build_accessibility_tree(parent);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
} else if (is_text()) {
|
||||
@@ -1767,6 +1774,7 @@ void Node::build_accessibility_tree(AccessibilityTreeNode& parent)
|
||||
if (has_child_nodes()) {
|
||||
for_each_child([&parent](DOM::Node& child) {
|
||||
child.build_accessibility_tree(parent);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1865,7 +1873,7 @@ ErrorOr<String> Node::name_or_description(NameOrDescription target, Document con
|
||||
element->for_each_child([&total_accumulated_text, current_node, target, &document, &visited_nodes](
|
||||
DOM::Node const& child_node) mutable {
|
||||
if (visited_nodes.contains(child_node.unique_id()))
|
||||
return;
|
||||
return IterationDecision::Continue;
|
||||
|
||||
// a. Set the current node to the child node.
|
||||
current_node = &child_node;
|
||||
@@ -1875,6 +1883,8 @@ ErrorOr<String> Node::name_or_description(NameOrDescription target, Document con
|
||||
|
||||
// c. Append the result to the accumulated text.
|
||||
total_accumulated_text.append(result);
|
||||
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
// iv. Return the accumulated text.
|
||||
return total_accumulated_text.to_string();
|
||||
|
||||
Reference in New Issue
Block a user