DOMRectList.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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::NonnullGCPtr<DOMRectList> DOMRectList::create(JS::Realm& realm, Vector<JS::Handle<DOMRect>> rect_handles)
  13. {
  14. Vector<JS::NonnullGCPtr<DOMRect>> rects;
  15. for (auto& rect : rect_handles)
  16. rects.append(*rect);
  17. return realm.heap().allocate<DOMRectList>(realm, realm, move(rects));
  18. }
  19. DOMRectList::DOMRectList(JS::Realm& realm, Vector<JS::NonnullGCPtr<DOMRect>> rects)
  20. : Bindings::LegacyPlatformObject(realm)
  21. , m_rects(move(rects))
  22. {
  23. }
  24. DOMRectList::~DOMRectList() = default;
  25. void DOMRectList::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMRectListPrototype>(realm, "DOMRectList"));
  29. }
  30. // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
  31. u32 DOMRectList::length() const
  32. {
  33. return m_rects.size();
  34. }
  35. // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-item
  36. DOMRect const* DOMRectList::item(u32 index) const
  37. {
  38. // The item(index) method, when invoked, must return null when
  39. // index is greater than or equal to the number of DOMRect objects associated with the DOMRectList.
  40. // Otherwise, the DOMRect object at index must be returned. Indices are zero-based.
  41. if (index >= m_rects.size())
  42. return nullptr;
  43. return m_rects[index];
  44. }
  45. bool DOMRectList::is_supported_property_index(u32 index) const
  46. {
  47. return index < m_rects.size();
  48. }
  49. WebIDL::ExceptionOr<JS::Value> DOMRectList::item_value(size_t index) const
  50. {
  51. if (index >= m_rects.size())
  52. return JS::js_undefined();
  53. return m_rects[index].ptr();
  54. }
  55. }