ParentNode.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // FIXME: This should take an Optional<FlyString>
  24. JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name_ns(Optional<String> const&, 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. protected:
  29. ParentNode(JS::Realm& realm, Document& document, NodeType type)
  30. : Node(realm, document, type)
  31. {
  32. }
  33. ParentNode(Document& document, NodeType type)
  34. : Node(document, type)
  35. {
  36. }
  37. virtual void visit_edges(Cell::Visitor&) override;
  38. private:
  39. JS::GCPtr<HTMLCollection> m_children;
  40. };
  41. template<>
  42. inline bool Node::fast_is<ParentNode>() const { return is_parent_node(); }
  43. template<typename U>
  44. inline U* Node::shadow_including_first_ancestor_of_type()
  45. {
  46. for (auto* ancestor = parent_or_shadow_host(); ancestor; ancestor = ancestor->parent_or_shadow_host()) {
  47. if (is<U>(*ancestor))
  48. return &verify_cast<U>(*ancestor);
  49. }
  50. return nullptr;
  51. }
  52. template<typename Callback>
  53. inline void ParentNode::for_each_child(Callback callback) const
  54. {
  55. for (auto* node = first_child(); node; node = node->next_sibling())
  56. callback(*node);
  57. }
  58. template<typename Callback>
  59. inline void ParentNode::for_each_child(Callback callback)
  60. {
  61. for (auto* node = first_child(); node; node = node->next_sibling())
  62. callback(*node);
  63. }
  64. }