ParentNode.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.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. JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name_ns(Optional<FlyString>, FlyString const&);
  25. WebIDL::ExceptionOr<void> prepend(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
  26. WebIDL::ExceptionOr<void> append(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
  27. WebIDL::ExceptionOr<void> replace_children(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
  28. JS::NonnullGCPtr<HTMLCollection> get_elements_by_class_name(StringView);
  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. if (callback(*node) == IterationDecision::Break)
  58. return;
  59. }
  60. }
  61. template<typename Callback>
  62. inline void ParentNode::for_each_child(Callback callback)
  63. {
  64. for (auto* node = first_child(); node; node = node->next_sibling()) {
  65. if (callback(*node) == IterationDecision::Break)
  66. return;
  67. }
  68. }
  69. }