diff --git a/Userland/Libraries/LibWeb/Geometry/DOMRect.cpp b/Userland/Libraries/LibWeb/Geometry/DOMRect.cpp index db39cda450..c68db13834 100644 --- a/Userland/Libraries/LibWeb/Geometry/DOMRect.cpp +++ b/Userland/Libraries/LibWeb/Geometry/DOMRect.cpp @@ -20,6 +20,13 @@ WebIDL::ExceptionOr> 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> DOMRect::from_rect(JS::VM& vm, Geometry::DOMRectInit const& other) +{ + auto& realm = *vm.current_realm(); + return MUST_OR_THROW_OOM(realm.heap().allocate(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) { diff --git a/Userland/Libraries/LibWeb/Geometry/DOMRect.h b/Userland/Libraries/LibWeb/Geometry/DOMRect.h index 407b21b940..a6ea4900e5 100644 --- a/Userland/Libraries/LibWeb/Geometry/DOMRect.h +++ b/Userland/Libraries/LibWeb/Geometry/DOMRect.h @@ -17,6 +17,7 @@ class DOMRect final : public DOMRectReadOnly { public: static WebIDL::ExceptionOr> construct_impl(JS::Realm&, double x = 0, double y = 0, double width = 0, double height = 0); static WebIDL::ExceptionOr> create(JS::Realm&, Gfx::FloatRect const&); + static WebIDL::ExceptionOr> from_rect(JS::VM&, DOMRectInit const&); virtual ~DOMRect() override; diff --git a/Userland/Libraries/LibWeb/Geometry/DOMRect.idl b/Userland/Libraries/LibWeb/Geometry/DOMRect.idl index a364dba2a0..82e8e21bce 100644 --- a/Userland/Libraries/LibWeb/Geometry/DOMRect.idl +++ b/Userland/Libraries/LibWeb/Geometry/DOMRect.idl @@ -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; diff --git a/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.cpp b/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.cpp index f17c66c9f3..ea4aae386a 100644 --- a/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.cpp +++ b/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.cpp @@ -15,6 +15,13 @@ WebIDL::ExceptionOr> DOMRectReadOnly::construc return MUST_OR_THROW_OOM(realm.heap().allocate(realm, realm, x, y, width, height)); } +// https://drafts.fxtf.org/geometry/#create-a-domrect-from-the-dictionary +WebIDL::ExceptionOr> DOMRectReadOnly::from_rect(JS::VM& vm, Geometry::DOMRectInit const& other) +{ + auto& realm = *vm.current_realm(); + return MUST_OR_THROW_OOM(realm.heap().allocate(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) diff --git a/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.h b/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.h index 8b78a2dd8b..84ec341e91 100644 --- a/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.h +++ b/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.h @@ -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> construct_impl(JS::Realm&, double x = 0, double y = 0, double width = 0, double height = 0); + static WebIDL::ExceptionOr> from_rect(JS::VM&, DOMRectInit const&); virtual ~DOMRectReadOnly() override; diff --git a/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.idl b/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.idl index aa77d24e4b..aa4da63178 100644 --- a/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.idl +++ b/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.idl @@ -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; +};