ParentNode.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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<NonnullRefPtrVector<Element>> query_selector_all(StringView);
  21. NonnullRefPtr<HTMLCollection> children();
  22. protected:
  23. ParentNode(Document& document, NodeType type)
  24. : Node(document, type)
  25. {
  26. }
  27. };
  28. template<typename Callback>
  29. inline void ParentNode::for_each_child(Callback callback) const
  30. {
  31. for (auto* node = first_child(); node; node = node->next_sibling())
  32. callback(*node);
  33. }
  34. template<typename Callback>
  35. inline void ParentNode::for_each_child(Callback callback)
  36. {
  37. for (auto* node = first_child(); node; node = node->next_sibling())
  38. callback(*node);
  39. }
  40. }