2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-06-15 18:55:47 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/DOM/Node.h>
|
2025-07-19 19:35:33 -07:00
|
|
|
#include <LibWeb/Export.h>
|
2019-06-15 18:55:47 +02:00
|
|
|
|
2020-07-26 19:37:56 +02:00
|
|
|
namespace Web::DOM {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2025-07-19 19:35:33 -07:00
|
|
|
class WEB_API ParentNode : public Node {
|
2022-08-28 13:42:07 +02:00
|
|
|
WEB_PLATFORM_OBJECT(ParentNode, Node);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(ParentNode);
|
2022-08-28 13:42:07 +02:00
|
|
|
|
2019-06-15 18:55:47 +02:00
|
|
|
public:
|
2020-09-18 09:49:51 +02:00
|
|
|
template<typename F>
|
|
|
|
void for_each_child(F) const;
|
|
|
|
template<typename F>
|
|
|
|
void for_each_child(F);
|
2019-06-15 18:55:47 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<Element> first_element_child();
|
|
|
|
GC::Ptr<Element> last_element_child();
|
2021-04-11 17:13:10 +01:00
|
|
|
u32 child_element_count() const;
|
2020-11-21 18:49:09 +00:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
WebIDL::ExceptionOr<GC::Ptr<Element>> query_selector(StringView);
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<NodeList>> query_selector_all(StringView);
|
2020-08-17 19:14:30 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<HTMLCollection> children();
|
2021-09-13 23:01:17 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<HTMLCollection> get_elements_by_tag_name(FlyString const&);
|
|
|
|
GC::Ref<HTMLCollection> get_elements_by_tag_name_ns(Optional<FlyString>, FlyString const&);
|
2023-09-15 21:42:32 +12:00
|
|
|
|
2025-07-24 12:05:52 -04:00
|
|
|
WebIDL::ExceptionOr<void> prepend(Vector<Variant<GC::Root<Node>, Utf16String>> const& nodes);
|
|
|
|
WebIDL::ExceptionOr<void> append(Vector<Variant<GC::Root<Node>, Utf16String>> const& nodes);
|
|
|
|
WebIDL::ExceptionOr<void> replace_children(Vector<Variant<GC::Root<Node>, Utf16String>> const& nodes);
|
2025-03-08 12:45:26 +13:00
|
|
|
WebIDL::ExceptionOr<void> move_before(GC::Ref<Node> node, GC::Ptr<Node> child);
|
2023-09-11 18:36:23 +12:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<HTMLCollection> get_elements_by_class_name(StringView);
|
2024-07-22 20:38:36 +01:00
|
|
|
|
2025-03-25 17:30:52 +00:00
|
|
|
GC::Ptr<Element> get_element_by_id(FlyString const& id) const;
|
|
|
|
|
2019-06-15 18:55:47 +02:00
|
|
|
protected:
|
2022-08-28 13:42:07 +02:00
|
|
|
ParentNode(JS::Realm& realm, Document& document, NodeType type)
|
|
|
|
: Node(realm, document, type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-17 19:14:30 +01:00
|
|
|
ParentNode(Document& document, NodeType type)
|
2019-09-29 11:43:07 +02:00
|
|
|
: Node(document, type)
|
2019-06-15 18:55:47 +02:00
|
|
|
{
|
|
|
|
}
|
2022-09-18 00:42:33 +02:00
|
|
|
|
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
|
|
|
|
|
|
|
private:
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ptr<HTMLCollection> m_children;
|
2019-06-15 18:55:47 +02:00
|
|
|
};
|
|
|
|
|
2022-03-14 14:40:42 +01:00
|
|
|
template<>
|
|
|
|
inline bool Node::fast_is<ParentNode>() const { return is_parent_node(); }
|
|
|
|
|
2023-05-28 18:27:35 +01:00
|
|
|
template<typename U>
|
|
|
|
inline U* Node::shadow_including_first_ancestor_of_type()
|
|
|
|
{
|
|
|
|
for (auto* ancestor = parent_or_shadow_host(); ancestor; ancestor = ancestor->parent_or_shadow_host()) {
|
|
|
|
if (is<U>(*ancestor))
|
2025-01-21 09:12:05 -05:00
|
|
|
return &as<U>(*ancestor);
|
2023-05-28 18:27:35 +01:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-15 22:49:44 +02:00
|
|
|
template<typename Callback>
|
|
|
|
inline void ParentNode::for_each_child(Callback callback) const
|
2019-06-15 18:55:47 +02:00
|
|
|
{
|
2024-05-04 14:59:52 +01:00
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling()) {
|
|
|
|
if (callback(*node) == IterationDecision::Break)
|
|
|
|
return;
|
|
|
|
}
|
2019-06-15 18:55:47 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 22:49:44 +02:00
|
|
|
template<typename Callback>
|
|
|
|
inline void ParentNode::for_each_child(Callback callback)
|
|
|
|
{
|
2024-05-04 14:59:52 +01:00
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling()) {
|
|
|
|
if (callback(*node) == IterationDecision::Break)
|
|
|
|
return;
|
|
|
|
}
|
2019-06-15 22:49:44 +02:00
|
|
|
}
|
2020-03-07 10:27:02 +01:00
|
|
|
|
|
|
|
}
|