Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/DOM/Node.h (C/C++)

Code before:

    template<typename U, typename Callback>
    WebIDL::ExceptionOr<void> for_each_child_of_type_fallible(Callback callback)
    {
        for (auto* node = first_child(); node; node = node->next_sibling()) {
            if (auto* maybe_node_of_type = as_if<U>(node)) {
                if (TRY(callback(*maybe_node_of_type)) == IterationDecision::Break)
                    return {};
            }
        }
        return {};
    }
    template<typename U>
    U const* shadow_including_first_ancestor_of_type() const
    {
        return const_cast<Node*>(this)->template shadow_including_first_ancestor_of_type<U>();
    }

    template<typename U>
    U* shadow_including_first_ancestor_of_type();

    template<typename Predicate>
    requires requires(Predicate& predicate, Node const& node) { { predicate(node) } -> ConvertibleTo<bool>; }
    Node const* find_in_shadow_including_ancestry(Predicate&& predicate) const
    {
        for (Node const* it = this; it && it->is_connected(); it = it->parent_or_shadow_host_node()) {
            if (predicate(*it))
                return it;
        }
        return nullptr;
    }

    ErrorOr<String> accessible_name(Document const&, ShouldComputeRole = ShouldComputeRole::Yes) const;
    ErrorOr<String> accessible_description(Document const&) const;

    Optional<String> locate_a_namespace(Optional<String> const& prefix) const;
    Optional<String> lookup_namespace_uri(Optional<String> prefix) const;
    Optional<String> lookup_prefix(Optional<String> namespace_) const;
    bool is_default_namespace(Optional<String> namespace_) const;
    Vector<FlyString> get_in_scope_prefixes() const;

    bool is_inert() const;

    bool has_inclusive_ancestor_with_display_none();

    GC::Ptr<ShadowRoot> containing_shadow_root();
    GC::Ptr<ShadowRoot const> containing_shadow_root() const
    {
        return const_cast<Node*>(this)->containing_shadow_root();
    }


Review comment (performance):
When would it not be connected in the tree if the parent is not? It seem inefficient to check this. And actually, is node being connected necessary as a predicate in the first place - I suppose it could also be valid to call a predicated function like this for a disconnected tree

Code after:

    Slottable as_slottable();

    template<typename U, typename Callback>
    WebIDL::ExceptionOr<void> for_each_child_of_type_fallible(Callback callback)
    {
        for (auto* node = first_child(); node; node = node->next_sibling()) {
            if (auto* maybe_node_of_type = as_if<U>(node)) {
                if (TRY(callback(*maybe_node_of_type)) == IterationDecision::Break)
                    return {};
            }
        }
        return {};
    }
    template<typename U>
    U const* shadow_including_first_ancestor_of_type() const
    {
        return const_cast<Node*>(this)->template shadow_including_first_ancestor_of_type<U>();
    }

    template<typename U>
    U* shadow_including_first_ancestor_of_type();

    template<typename Predicate>
    requires requires(Predicate& predicate, Node const& node) { { predicate(node) } -> ConvertibleTo<bool>; }
    Node const* find_in_shadow_including_ancestry(Predicate&& predicate) const
    {
        for (Node const* it = this; it; it = it->parent_or_shadow_host_node()) {
            if (predicate(*it))
                return it;
        }
        return nullptr;
    }

    ErrorOr<String> accessible_name(Document const&, ShouldComputeRole = ShouldComputeRole::Yes) const;
    ErrorOr<String> accessible_description(Document const&) const;

    Optional<String> locate_a_namespace(Optional<String> const& prefix) const;
    Optional<String> lookup_namespace_uri(Optional<String> prefix) const;
    Optional<String> lookup_prefix(Optional<String> namespace_) const;
    bool is_default_namespace(Optional<String> namespace_) const;
    Vector<FlyString> get_in_scope_prefixes() const;

    bool is_inert() const;

    bool has_inclusive_ancestor_with_display_none();

    GC::Ptr<ShadowRoot> containing_shadow_root();
    GC::Ptr<ShadowRoot const> containing_shadow_root() const
    {
        return const_cast<Node*>(this)->containing_shadow_root();