ladybird/Libraries/LibWeb/DOM/ParentNode.h
Andreas Kling bae4434780 LibWeb: Avoid sibling walks for last-child invalidation
Track last-child and backward positional selector dependencies
separately on parent nodes. A last-child or only-child selector can only
change the element at the trailing edge, so insertions and removals can
invalidate that element directly instead of walking every previous
sibling.

Keep the previous-sibling walk for selectors such as nth-last-child and
last-of-type, where every previous element's from-end position may
change.
2026-06-08 17:16:18 +02:00

101 lines
3.3 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/DOM/Node.h>
#include <LibWeb/Export.h>
namespace Web::DOM {
class WEB_API ParentNode : public Node {
WEB_NON_IDL_PLATFORM_OBJECT(ParentNode, Node);
GC_DECLARE_ALLOCATOR(ParentNode);
public:
template<typename F>
void for_each_child(F) const;
template<typename F>
void for_each_child(F);
GC::Ptr<Element> first_element_child();
GC::Ptr<Element> last_element_child();
u32 child_element_count() const;
WebIDL::ExceptionOr<GC::Ptr<Element>> query_selector(StringView);
WebIDL::ExceptionOr<GC::Ref<NodeList>> query_selector_all(StringView);
GC::Ref<HTMLCollection> children();
GC::Ref<HTMLCollection> get_elements_by_tag_name(FlyString const&);
GC::Ref<HTMLCollection> get_elements_by_tag_name_ns(Optional<FlyString>, FlyString const&);
WebIDL::ExceptionOr<void> prepend(ReadonlySpan<Variant<GC::Ref<Node>, Utf16String>> const& nodes);
WebIDL::ExceptionOr<void> append(ReadonlySpan<Variant<GC::Ref<Node>, Utf16String>> const& nodes);
WebIDL::ExceptionOr<void> replace_children(ReadonlySpan<Variant<GC::Ref<Node>, Utf16String>> const& nodes);
WebIDL::ExceptionOr<void> move_before(GC::Ref<Node> node, GC::Ptr<Node> child);
GC::Ref<HTMLCollection> get_elements_by_class_name(StringView);
GC::Ptr<Element> get_element_by_id(FlyString const& id) const;
bool has_child_affected_by_last_child_pseudo_class() const { return m_has_child_affected_by_last_child_pseudo_class; }
void set_has_child_affected_by_last_child_pseudo_class(bool value) { m_has_child_affected_by_last_child_pseudo_class = value; }
bool has_child_affected_by_backward_positional_pseudo_class() const { return m_has_child_affected_by_backward_positional_pseudo_class; }
void set_has_child_affected_by_backward_positional_pseudo_class(bool value) { m_has_child_affected_by_backward_positional_pseudo_class = value; }
protected:
ParentNode(JS::Realm& realm, Document& document, NodeType type)
: Node(realm, document, type)
{
}
ParentNode(Document& document, NodeType type)
: Node(document, type)
{
}
virtual void visit_edges(Cell::Visitor&) override;
private:
GC::Ptr<HTMLCollection> m_children;
bool m_has_child_affected_by_last_child_pseudo_class { false };
bool m_has_child_affected_by_backward_positional_pseudo_class { false };
};
template<>
inline bool Node::fast_is<ParentNode>() const { return is_parent_node(); }
template<typename U>
inline U* Node::first_flat_tree_ancestor_of_type()
{
for (auto* ancestor = flat_tree_parent(); ancestor; ancestor = ancestor->flat_tree_parent()) {
if (is<U>(*ancestor))
return &as<U>(*ancestor);
}
return nullptr;
}
template<typename Callback>
inline void ParentNode::for_each_child(Callback callback) const
{
for (auto* node = first_child(); node; node = node->next_sibling()) {
if (callback(*node) == IterationDecision::Break)
return;
}
}
template<typename Callback>
inline void ParentNode::for_each_child(Callback callback)
{
for (auto* node = first_child(); node; node = node->next_sibling()) {
if (callback(*node) == IterationDecision::Break)
return;
}
}
}