LibWeb: Implement DOMRect(ReadOnly)#fromRect

This will also be used by IntersectionObserver.
This commit is contained in:
Luke Wilde 2023-07-06 23:21:20 +01:00 committed by Andreas Kling
parent 1d426df262
commit 6f8afd8cd9
Notes: sideshowbarker 2024-07-17 03:10:07 +09:00
6 changed files with 35 additions and 0 deletions

View file

@ -20,6 +20,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRect>> DOMRect::create(JS::Realm& realm,
return construct_impl(realm, rect.x(), rect.y(), rect.width(), rect.height());
}
// https://drafts.fxtf.org/geometry/#create-a-domrect-from-the-dictionary
WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRect>> DOMRect::from_rect(JS::VM& vm, Geometry::DOMRectInit const& other)
{
auto& realm = *vm.current_realm();
return MUST_OR_THROW_OOM(realm.heap().allocate<DOMRect>(realm, realm, other.x, other.y, other.width, other.height));
}
DOMRect::DOMRect(JS::Realm& realm, double x, double y, double width, double height)
: DOMRectReadOnly(realm, x, y, width, height)
{

View file

@ -17,6 +17,7 @@ class DOMRect final : public DOMRectReadOnly {
public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRect>> construct_impl(JS::Realm&, double x = 0, double y = 0, double width = 0, double height = 0);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRect>> create(JS::Realm&, Gfx::FloatRect const&);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRect>> from_rect(JS::VM&, DOMRectInit const&);
virtual ~DOMRect() override;

View file

@ -6,6 +6,8 @@ interface DOMRect : DOMRectReadOnly {
constructor(optional double x = 0, optional double y = 0, optional double width = 0, optional double height = 0);
[NewObject] static DOMRect fromRect(optional DOMRectInit other = {});
attribute double x;
attribute double y;
attribute double width;

View file

@ -15,6 +15,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRectReadOnly>> DOMRectReadOnly::construc
return MUST_OR_THROW_OOM(realm.heap().allocate<DOMRectReadOnly>(realm, realm, x, y, width, height));
}
// https://drafts.fxtf.org/geometry/#create-a-domrect-from-the-dictionary
WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRectReadOnly>> DOMRectReadOnly::from_rect(JS::VM& vm, Geometry::DOMRectInit const& other)
{
auto& realm = *vm.current_realm();
return MUST_OR_THROW_OOM(realm.heap().allocate<DOMRectReadOnly>(realm, realm, other.x, other.y, other.width, other.height));
}
DOMRectReadOnly::DOMRectReadOnly(JS::Realm& realm, double x, double y, double width, double height)
: PlatformObject(realm)
, m_rect(x, y, width, height)

View file

@ -12,12 +12,21 @@
namespace Web::Geometry {
// https://drafts.fxtf.org/geometry/#dictdef-domrectinit
struct DOMRectInit {
double x { 0.0 };
double y { 0.0 };
double width { 0.0 };
double height { 0.0 };
};
// https://drafts.fxtf.org/geometry/#domrectreadonly
class DOMRectReadOnly : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(DOMRectReadOnly, Bindings::PlatformObject);
public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRectReadOnly>> construct_impl(JS::Realm&, double x = 0, double y = 0, double width = 0, double height = 0);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRectReadOnly>> from_rect(JS::VM&, DOMRectInit const&);
virtual ~DOMRectReadOnly() override;

View file

@ -4,6 +4,8 @@ interface DOMRectReadOnly {
constructor(optional double x = 0, optional double y = 0, optional double width = 0, optional double height = 0);
[NewObject] static DOMRectReadOnly fromRect(optional DOMRectInit other = {});
readonly attribute double x;
readonly attribute double y;
readonly attribute double width;
@ -15,3 +17,10 @@ interface DOMRectReadOnly {
readonly attribute double left;
};
dictionary DOMRectInit {
unrestricted double x = 0;
unrestricted double y = 0;
unrestricted double width = 0;
unrestricted double height = 0;
};