ParentNode.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/DOM/Node.h>
  8. namespace Web::DOM {
  9. class ParentNode : public Node {
  10. WEB_PLATFORM_OBJECT(ParentNode, Node);
  11. public:
  12. template<typename F>
  13. void for_each_child(F) const;
  14. template<typename F>
  15. void for_each_child(F);
  16. JS::GCPtr<Element> first_element_child();
  17. JS::GCPtr<Element> last_element_child();
  18. u32 child_element_count() const;
  19. WebIDL::ExceptionOr<JS::GCPtr<Element>> query_selector(StringView);
  20. WebIDL::ExceptionOr<JS::NonnullGCPtr<NodeList>> query_selector_all(StringView);
  21. JS::NonnullGCPtr<HTMLCollection> children();
  22. JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name(DeprecatedFlyString const&);
  23. JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name_ns(DeprecatedFlyString const&, DeprecatedFlyString const&);
  24. WebIDL::ExceptionOr<void> prepend(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
  25. WebIDL::ExceptionOr<void> append(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
  26. WebIDL::ExceptionOr<void> replace_children(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
  27. protected:
  28. ParentNode(JS::Realm& realm, Document& document, NodeType type)
  29. : Node(realm, document, type)
  30. {
  31. }
  32. ParentNode(Document& document, NodeType type)
  33. : Node(document, type)
  34. {
  35. }
  36. virtual void visit_edges(Cell::Visitor&) override;
  37. private:
  38. JS::GCPtr<HTMLCollection> m_children;
  39. };
  40. template<>
  41. inline bool Node::fast_is<ParentNode>() const { return is_parent_node(); }
  42. template<typename Callback>
  43. inline void ParentNode::for_each_child(Callback callback) const
  44. {
  45. for (auto* node = first_child(); node; node = node->next_sibling())
  46. callback(*node);
  47. }
  48. template<typename Callback>
  49. inline void ParentNode::for_each_child(Callback callback)
  50. {
  51. for (auto* node = first_child(); node; node = node->next_sibling())
  52. callback(*node);
  53. }
  54. }