DOMRectList.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2022, DerpyCrabs <derpycrabs@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Handle.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Geometry/DOMRect.h>
  9. #include <LibWeb/Geometry/DOMRectList.h>
  10. #include <LibWeb/WebIDL/ExceptionOr.h>
  11. namespace Web::Geometry {
  12. JS_DEFINE_ALLOCATOR(DOMRectList);
  13. JS::NonnullGCPtr<DOMRectList> DOMRectList::create(JS::Realm& realm, Vector<JS::Handle<DOMRect>> rect_handles)
  14. {
  15. Vector<JS::NonnullGCPtr<DOMRect>> rects;
  16. for (auto& rect : rect_handles)
  17. rects.append(*rect);
  18. return realm.heap().allocate<DOMRectList>(realm, realm, move(rects));
  19. }
  20. DOMRectList::DOMRectList(JS::Realm& realm, Vector<JS::NonnullGCPtr<DOMRect>> rects)
  21. : Bindings::PlatformObject(realm)
  22. , m_rects(move(rects))
  23. {
  24. m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
  25. }
  26. DOMRectList::~DOMRectList() = default;
  27. void DOMRectList::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(DOMRectList);
  31. }
  32. void DOMRectList::visit_edges(Cell::Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. for (auto& rect : m_rects)
  36. visitor.visit(rect);
  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. bool DOMRectList::is_supported_property_index(u32 index) const
  54. {
  55. return index < m_rects.size();
  56. }
  57. WebIDL::ExceptionOr<JS::Value> DOMRectList::item_value(size_t index) const
  58. {
  59. if (index >= m_rects.size())
  60. return JS::js_undefined();
  61. return m_rects[index].ptr();
  62. }
  63. }