ladybird/Userland/Libraries/LibWeb/Geometry/DOMRectList.cpp
Andrew Kaster 521ed0e911 LibWeb: Delete LegacyPlatformObject and move behavior to PlatformObject
We have two known PlatformObjects that need to implement some of the
behavior of LegacyPlatformObjects to date: Window, and HTMLFormElement.

To make this not require double (or virtual) inheritance of
PlatformObject, move the behavior of LegacyPlatformObject into
PlatformObject. The selection of LegacyPlatformObject behavior is done
with a new bitfield of feature flags instead of a dozen virtual
functions that return bool. This change simplifies every class involved
in the diff with the notable exception of Window, which now needs some
ugly const casts to implement named property access.
2024-01-12 09:11:18 +01:00

78 lines
2.1 KiB
C++

/*
* Copyright (c) 2022, DerpyCrabs <derpycrabs@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Heap/Handle.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Geometry/DOMRect.h>
#include <LibWeb/Geometry/DOMRectList.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::Geometry {
JS_DEFINE_ALLOCATOR(DOMRectList);
JS::NonnullGCPtr<DOMRectList> DOMRectList::create(JS::Realm& realm, Vector<JS::Handle<DOMRect>> rect_handles)
{
Vector<JS::NonnullGCPtr<DOMRect>> rects;
for (auto& rect : rect_handles)
rects.append(*rect);
return realm.heap().allocate<DOMRectList>(realm, realm, move(rects));
}
DOMRectList::DOMRectList(JS::Realm& realm, Vector<JS::NonnullGCPtr<DOMRect>> rects)
: Bindings::PlatformObject(realm)
, m_rects(move(rects))
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
}
DOMRectList::~DOMRectList() = default;
void DOMRectList::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMRectListPrototype>(realm, "DOMRectList"_fly_string));
}
void DOMRectList::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
for (auto& rect : m_rects)
visitor.visit(rect);
}
// https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
u32 DOMRectList::length() const
{
return m_rects.size();
}
// https://drafts.fxtf.org/geometry-1/#dom-domrectlist-item
DOMRect const* DOMRectList::item(u32 index) const
{
// The item(index) method, when invoked, must return null when
// index is greater than or equal to the number of DOMRect objects associated with the DOMRectList.
// Otherwise, the DOMRect object at index must be returned. Indices are zero-based.
if (index >= m_rects.size())
return nullptr;
return m_rects[index];
}
bool DOMRectList::is_supported_property_index(u32 index) const
{
return index < m_rects.size();
}
WebIDL::ExceptionOr<JS::Value> DOMRectList::item_value(size_t index) const
{
if (index >= m_rects.size())
return JS::js_undefined();
return m_rects[index].ptr();
}
}