NonElementParentNode.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/Forward.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibWeb/Forward.h>
  11. #include <LibWeb/HTML/AttributeNames.h>
  12. #include <LibWeb/TreeNode.h>
  13. namespace Web::DOM {
  14. template<typename NodeType>
  15. class NonElementParentNode {
  16. public:
  17. JS::GCPtr<Element const> get_element_by_id(FlyString const& id) const
  18. {
  19. JS::GCPtr<Element const> found_element;
  20. static_cast<NodeType const*>(this)->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  21. if (element.id() == id) {
  22. found_element = &element;
  23. return IterationDecision::Break;
  24. }
  25. return IterationDecision::Continue;
  26. });
  27. return found_element;
  28. }
  29. JS::GCPtr<Element> get_element_by_id(FlyString const& id)
  30. {
  31. JS::GCPtr<Element> found_element;
  32. static_cast<NodeType*>(this)->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  33. if (element.id() == id) {
  34. found_element = &element;
  35. return IterationDecision::Break;
  36. }
  37. return IterationDecision::Continue;
  38. });
  39. return found_element;
  40. }
  41. protected:
  42. NonElementParentNode() = default;
  43. };
  44. }