NonElementParentNode.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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/Forward.h>
  8. #include <LibJS/Heap/GCPtr.h>
  9. #include <LibWeb/Forward.h>
  10. #include <LibWeb/HTML/AttributeNames.h>
  11. #include <LibWeb/TreeNode.h>
  12. namespace Web::DOM {
  13. template<typename NodeType>
  14. class NonElementParentNode {
  15. public:
  16. JS::GCPtr<Element const> get_element_by_id(DeprecatedFlyString const& id) const
  17. {
  18. JS::GCPtr<Element const> found_element;
  19. static_cast<NodeType const*>(this)->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  20. if (element.attribute(HTML::AttributeNames::id) == id) {
  21. found_element = &element;
  22. return IterationDecision::Break;
  23. }
  24. return IterationDecision::Continue;
  25. });
  26. return found_element;
  27. }
  28. JS::GCPtr<Element> get_element_by_id(DeprecatedFlyString const& id)
  29. {
  30. JS::GCPtr<Element> found_element;
  31. static_cast<NodeType*>(this)->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  32. if (element.attribute(HTML::AttributeNames::id) == id) {
  33. found_element = &element;
  34. return IterationDecision::Break;
  35. }
  36. return IterationDecision::Continue;
  37. });
  38. return found_element;
  39. }
  40. protected:
  41. NonElementParentNode() = default;
  42. };
  43. }