NonElementParentNode.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. return get_element_by_id(id.to_deprecated_fly_string());
  20. }
  21. JS::GCPtr<Element const> get_element_by_id(DeprecatedFlyString const& id) const
  22. {
  23. JS::GCPtr<Element const> found_element;
  24. static_cast<NodeType const*>(this)->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  25. if (element.deprecated_attribute(HTML::AttributeNames::id) == id) {
  26. found_element = &element;
  27. return IterationDecision::Break;
  28. }
  29. return IterationDecision::Continue;
  30. });
  31. return found_element;
  32. }
  33. JS::GCPtr<Element> get_element_by_id(FlyString const& id)
  34. {
  35. return get_element_by_id(id.to_deprecated_fly_string());
  36. }
  37. JS::GCPtr<Element> get_element_by_id(DeprecatedFlyString const& id)
  38. {
  39. JS::GCPtr<Element> found_element;
  40. static_cast<NodeType*>(this)->template for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  41. if (element.deprecated_attribute(HTML::AttributeNames::id) == id) {
  42. found_element = &element;
  43. return IterationDecision::Break;
  44. }
  45. return IterationDecision::Continue;
  46. });
  47. return found_element;
  48. }
  49. protected:
  50. NonElementParentNode() = default;
  51. };
  52. }