ParentNode.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(FlyString const&);
  23. JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name_ns(Optional<String> const&, FlyString const&);
  24. JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name(DeprecatedFlyString const&);
  25. JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name_ns(DeprecatedFlyString const&, DeprecatedFlyString 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. WebIDL::ExceptionOr<void> prepend(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
  30. WebIDL::ExceptionOr<void> append(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
  31. WebIDL::ExceptionOr<void> replace_children(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
  32. protected:
  33. ParentNode(JS::Realm& realm, Document& document, NodeType type)
  34. : Node(realm, document, type)
  35. {
  36. }
  37. ParentNode(Document& document, NodeType type)
  38. : Node(document, type)
  39. {
  40. }
  41. virtual void visit_edges(Cell::Visitor&) override;
  42. private:
  43. JS::GCPtr<HTMLCollection> m_children;
  44. };
  45. template<>
  46. inline bool Node::fast_is<ParentNode>() const { return is_parent_node(); }
  47. template<typename U>
  48. inline U* Node::shadow_including_first_ancestor_of_type()
  49. {
  50. for (auto* ancestor = parent_or_shadow_host(); ancestor; ancestor = ancestor->parent_or_shadow_host()) {
  51. if (is<U>(*ancestor))
  52. return &verify_cast<U>(*ancestor);
  53. }
  54. return nullptr;
  55. }
  56. template<typename Callback>
  57. inline void ParentNode::for_each_child(Callback callback) const
  58. {
  59. for (auto* node = first_child(); node; node = node->next_sibling())
  60. callback(*node);
  61. }
  62. template<typename Callback>
  63. inline void ParentNode::for_each_child(Callback callback)
  64. {
  65. for (auto* node = first_child(); node; node = node->next_sibling())
  66. callback(*node);
  67. }
  68. }