Serenity Operating System
1/*
2 * Copyright (c) 2022, DerpyCrabs <derpycrabs@gmail.com>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibJS/Heap/Handle.h>
8#include <LibWeb/Bindings/Intrinsics.h>
9#include <LibWeb/Geometry/DOMRect.h>
10#include <LibWeb/Geometry/DOMRectList.h>
11#include <LibWeb/WebIDL/ExceptionOr.h>
12
13namespace Web::Geometry {
14
15WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRectList>> DOMRectList::create(JS::Realm& realm, Vector<JS::Handle<DOMRect>> rect_handles)
16{
17 Vector<JS::NonnullGCPtr<DOMRect>> rects;
18 for (auto& rect : rect_handles)
19 rects.append(*rect);
20 return MUST_OR_THROW_OOM(realm.heap().allocate<DOMRectList>(realm, realm, move(rects)));
21}
22
23DOMRectList::DOMRectList(JS::Realm& realm, Vector<JS::NonnullGCPtr<DOMRect>> rects)
24 : Bindings::LegacyPlatformObject(realm)
25 , m_rects(move(rects))
26{
27}
28
29DOMRectList::~DOMRectList() = default;
30
31JS::ThrowCompletionOr<void> DOMRectList::initialize(JS::Realm& realm)
32{
33 MUST_OR_THROW_OOM(Base::initialize(realm));
34 set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMRectListPrototype>(realm, "DOMRectList"));
35
36 return {};
37}
38
39// https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
40u32 DOMRectList::length() const
41{
42 return m_rects.size();
43}
44
45// https://drafts.fxtf.org/geometry-1/#dom-domrectlist-item
46DOMRect const* DOMRectList::item(u32 index) const
47{
48 // The item(index) method, when invoked, must return null when
49 // index is greater than or equal to the number of DOMRect objects associated with the DOMRectList.
50 // Otherwise, the DOMRect object at index must be returned. Indices are zero-based.
51 if (index >= m_rects.size())
52 return nullptr;
53 return m_rects[index];
54}
55
56bool DOMRectList::is_supported_property_index(u32 index) const
57{
58 return index < m_rects.size();
59}
60
61WebIDL::ExceptionOr<JS::Value> DOMRectList::item_value(size_t index) const
62{
63 if (index >= m_rects.size())
64 return JS::js_undefined();
65
66 return m_rects[index].ptr();
67}
68
69}