DOMRectList.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2022, DerpyCrabs <derpycrabs@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGC/Root.h>
  7. #include <LibWeb/Bindings/DOMRectListPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Geometry/DOMRect.h>
  10. #include <LibWeb/Geometry/DOMRectList.h>
  11. #include <LibWeb/WebIDL/ExceptionOr.h>
  12. namespace Web::Geometry {
  13. GC_DEFINE_ALLOCATOR(DOMRectList);
  14. GC::Ref<DOMRectList> DOMRectList::create(JS::Realm& realm, Vector<GC::Root<DOMRect>> rect_handles)
  15. {
  16. Vector<GC::Ref<DOMRect>> rects;
  17. for (auto& rect : rect_handles)
  18. rects.append(*rect);
  19. return realm.create<DOMRectList>(realm, move(rects));
  20. }
  21. DOMRectList::DOMRectList(JS::Realm& realm, Vector<GC::Ref<DOMRect>> rects)
  22. : Bindings::PlatformObject(realm)
  23. , m_rects(move(rects))
  24. {
  25. m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
  26. }
  27. DOMRectList::~DOMRectList() = default;
  28. void DOMRectList::initialize(JS::Realm& realm)
  29. {
  30. Base::initialize(realm);
  31. WEB_SET_PROTOTYPE_FOR_INTERFACE(DOMRectList);
  32. }
  33. void DOMRectList::visit_edges(Cell::Visitor& visitor)
  34. {
  35. Base::visit_edges(visitor);
  36. visitor.visit(m_rects);
  37. }
  38. // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
  39. u32 DOMRectList::length() const
  40. {
  41. return m_rects.size();
  42. }
  43. // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-item
  44. DOMRect const* DOMRectList::item(u32 index) const
  45. {
  46. // The item(index) method, when invoked, must return null when
  47. // index is greater than or equal to the number of DOMRect objects associated with the DOMRectList.
  48. // Otherwise, the DOMRect object at index must be returned. Indices are zero-based.
  49. if (index >= m_rects.size())
  50. return nullptr;
  51. return m_rects[index];
  52. }
  53. Optional<JS::Value> DOMRectList::item_value(size_t index) const
  54. {
  55. if (index >= m_rects.size())
  56. return {};
  57. return m_rects[index].ptr();
  58. }
  59. }