DOMRectList.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. visitor.visit(m_rects);
  36. }
  37. // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
  38. u32 DOMRectList::length() const
  39. {
  40. return m_rects.size();
  41. }
  42. // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-item
  43. DOMRect const* DOMRectList::item(u32 index) const
  44. {
  45. // The item(index) method, when invoked, must return null when
  46. // index is greater than or equal to the number of DOMRect objects associated with the DOMRectList.
  47. // Otherwise, the DOMRect object at index must be returned. Indices are zero-based.
  48. if (index >= m_rects.size())
  49. return nullptr;
  50. return m_rects[index];
  51. }
  52. bool DOMRectList::is_supported_property_index(u32 index) const
  53. {
  54. return index < m_rects.size();
  55. }
  56. WebIDL::ExceptionOr<JS::Value> DOMRectList::item_value(size_t index) const
  57. {
  58. if (index >= m_rects.size())
  59. return JS::js_undefined();
  60. return m_rects[index].ptr();
  61. }
  62. }