ParentNode.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. protected:
  25. ParentNode(Document& document, NodeType type)
  26. : Node(document, type)
  27. {
  28. }
  29. };
  30. template<typename Callback>
  31. inline void ParentNode::for_each_child(Callback callback) const
  32. {
  33. for (auto* node = first_child(); node; node = node->next_sibling())
  34. callback(*node);
  35. }
  36. template<typename Callback>
  37. inline void ParentNode::for_each_child(Callback callback)
  38. {
  39. for (auto* node = first_child(); node; node = node->next_sibling())
  40. callback(*node);
  41. }
  42. }