DOMRectList.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. WebIDL::ExceptionOr<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 MUST_OR_THROW_OOM(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. JS::ThrowCompletionOr<void> DOMRectList::initialize(JS::Realm& realm)
  26. {
  27. MUST_OR_THROW_OOM(Base::initialize(realm));
  28. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMRectListPrototype>(realm, "DOMRectList"));
  29. return {};
  30. }
  31. // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
  32. u32 DOMRectList::length() const
  33. {
  34. return m_rects.size();
  35. }
  36. // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-item
  37. DOMRect const* DOMRectList::item(u32 index) const
  38. {
  39. // The item(index) method, when invoked, must return null when
  40. // index is greater than or equal to the number of DOMRect objects associated with the DOMRectList.
  41. // Otherwise, the DOMRect object at index must be returned. Indices are zero-based.
  42. if (index >= m_rects.size())
  43. return nullptr;
  44. return m_rects[index];
  45. }
  46. bool DOMRectList::is_supported_property_index(u32 index) const
  47. {
  48. return index < m_rects.size();
  49. }
  50. WebIDL::ExceptionOr<JS::Value> DOMRectList::item_value(size_t index) const
  51. {
  52. if (index >= m_rects.size())
  53. return JS::js_undefined();
  54. return m_rects[index].ptr();
  55. }
  56. }