ParentNode.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 <AK/NonnullRefPtrVector.h>
  8. #include <LibWeb/DOM/Node.h>
  9. namespace Web::DOM {
  10. class ParentNode : public 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. RefPtr<Element> first_element_child();
  17. RefPtr<Element> last_element_child();
  18. u32 child_element_count() const;
  19. ExceptionOr<RefPtr<Element>> query_selector(StringView);
  20. ExceptionOr<NonnullRefPtr<NodeList>> query_selector_all(StringView);
  21. NonnullRefPtr<HTMLCollection> children();
  22. NonnullRefPtr<HTMLCollection> get_elements_by_tag_name(FlyString const&);
  23. NonnullRefPtr<HTMLCollection> get_elements_by_tag_name_ns(FlyString const&, FlyString const&);
  24. ExceptionOr<void> prepend(Vector<Variant<NonnullRefPtr<Node>, String>> const& nodes);
  25. ExceptionOr<void> append(Vector<Variant<NonnullRefPtr<Node>, String>> const& nodes);
  26. ExceptionOr<void> replace_children(Vector<Variant<NonnullRefPtr<Node>, String>> const& nodes);
  27. protected:
  28. ParentNode(Document& document, NodeType type)
  29. : Node(document, type)
  30. {
  31. }
  32. };
  33. template<>
  34. inline bool Node::fast_is<ParentNode>() const { return is_parent_node(); }
  35. template<typename Callback>
  36. inline void ParentNode::for_each_child(Callback callback) const
  37. {
  38. for (auto* node = first_child(); node; node = node->next_sibling())
  39. callback(*node);
  40. }
  41. template<typename Callback>
  42. inline void ParentNode::for_each_child(Callback callback)
  43. {
  44. for (auto* node = first_child(); node; node = node->next_sibling())
  45. callback(*node);
  46. }
  47. }