DOMRectList.cpp 1.6 KB

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