ParentNode.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. JS_DECLARE_ALLOCATOR(ParentNode);
  12. public:
  13. template<typename F>
  14. void for_each_child(F) const;
  15. template<typename F>
  16. void for_each_child(F);
  17. JS::GCPtr<Element> first_element_child();
  18. JS::GCPtr<Element> last_element_child();
  19. u32 child_element_count() const;
  20. WebIDL::ExceptionOr<JS::GCPtr<Element>> query_selector(StringView);
  21. WebIDL::ExceptionOr<JS::NonnullGCPtr<NodeList>> query_selector_all(StringView);
  22. JS::NonnullGCPtr<HTMLCollection> children();
  23. JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name(FlyString const&);
  24. // FIXME: This should take an Optional<FlyString>
  25. JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name_ns(Optional<String> const&, FlyString const&);
  26. WebIDL::ExceptionOr<void> prepend(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
  27. WebIDL::ExceptionOr<void> append(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
  28. WebIDL::ExceptionOr<void> replace_children(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
  29. protected:
  30. ParentNode(JS::Realm& realm, Document& document, NodeType type)
  31. : Node(realm, document, type)
  32. {
  33. }
  34. ParentNode(Document& document, NodeType type)
  35. : Node(document, type)
  36. {
  37. }
  38. virtual void visit_edges(Cell::Visitor&) override;
  39. private:
  40. JS::GCPtr<HTMLCollection> m_children;
  41. };
  42. template<>
  43. inline bool Node::fast_is<ParentNode>() const { return is_parent_node(); }
  44. template<typename U>
  45. inline U* Node::shadow_including_first_ancestor_of_type()
  46. {
  47. for (auto* ancestor = parent_or_shadow_host(); ancestor; ancestor = ancestor->parent_or_shadow_host()) {
  48. if (is<U>(*ancestor))
  49. return &verify_cast<U>(*ancestor);
  50. }
  51. return nullptr;
  52. }
  53. template<typename Callback>
  54. inline void ParentNode::for_each_child(Callback callback) const
  55. {
  56. for (auto* node = first_child(); node; node = node->next_sibling())
  57. callback(*node);
  58. }
  59. template<typename Callback>
  60. inline void ParentNode::for_each_child(Callback callback)
  61. {
  62. for (auto* node = first_child(); node; node = node->next_sibling())
  63. callback(*node);
  64. }
  65. }