Quellcode durchsuchen

LibWeb: Add {,de}serialization steps for DOMRect

Kenneth Myhra vor 1 Jahr
Ursprung
Commit
94c6389fc0

+ 2 - 0
Tests/LibWeb/Text/expected/HTML/StructuredClone-serializable-objects.txt

@@ -18,3 +18,5 @@ instanceOf DOMPoint: true
 DOMPoint: {"x":10,"y":20,"z":30,"w":40}
 instanceOf DOMRectReadOnly: true
 DOMRectReadOnly: {"x":10,"y":20,"width":30,"height":40,"top":20,"right":40,"bottom":60,"left":10}
+instanceOf DOMRect: true
+DOMRect: {"x":10,"y":20,"width":30,"height":40,"top":20,"right":40,"bottom":60,"left":10}

+ 4 - 0
Tests/LibWeb/Text/input/HTML/StructuredClone-serializable-objects.html

@@ -39,6 +39,10 @@
         println(`instanceOf DOMRectReadOnly: ${domRectReadOnly instanceof DOMRectReadOnly}`);
         println(`DOMRectReadOnly: ${JSON.stringify(domRectReadOnly)}`);
 
+        let domRect = structuredClone(new DOMRect(10, 20, 30, 40));
+        println(`instanceOf DOMRect: ${domRect instanceof DOMRect}`);
+        println(`DOMRect: ${JSON.stringify(domRect)}`);
+
         done();
     });
 </script>

+ 10 - 0
Userland/Libraries/LibWeb/Geometry/DOMRect.cpp

@@ -22,6 +22,11 @@ JS::NonnullGCPtr<DOMRect> DOMRect::create(JS::Realm& realm, Gfx::FloatRect const
     return realm.heap().allocate<DOMRect>(realm, realm, rect.x(), rect.y(), rect.width(), rect.height());
 }
 
+JS::NonnullGCPtr<DOMRect> DOMRect::create(JS::Realm& realm)
+{
+    return realm.heap().allocate<DOMRect>(realm, realm);
+}
+
 // https://drafts.fxtf.org/geometry/#create-a-domrect-from-the-dictionary
 JS::NonnullGCPtr<DOMRect> DOMRect::from_rect(JS::VM& vm, Geometry::DOMRectInit const& other)
 {
@@ -34,6 +39,11 @@ DOMRect::DOMRect(JS::Realm& realm, double x, double y, double width, double heig
 {
 }
 
+DOMRect::DOMRect(JS::Realm& realm)
+    : DOMRectReadOnly(realm)
+{
+}
+
 DOMRect::~DOMRect() = default;
 
 void DOMRect::initialize(JS::Realm& realm)

+ 4 - 0
Userland/Libraries/LibWeb/Geometry/DOMRect.h

@@ -18,6 +18,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);
     [[nodiscard]] static JS::NonnullGCPtr<DOMRect> create(JS::Realm&, Gfx::FloatRect const&);
+    [[nodiscard]] static JS::NonnullGCPtr<DOMRect> create(JS::Realm&);
     [[nodiscard]] static JS::NonnullGCPtr<DOMRect> from_rect(JS::VM&, DOMRectInit const&);
 
     virtual ~DOMRect() override;
@@ -32,8 +33,11 @@ public:
     void set_width(double width) { m_rect.set_width(width); }
     void set_height(double height) { m_rect.set_height(height); }
 
+    virtual StringView interface_name() const override { return "DOMRect"sv; }
+
 private:
     DOMRect(JS::Realm&, double x, double y, double width, double height);
+    explicit DOMRect(JS::Realm&);
 
     virtual void initialize(JS::Realm&) override;
 };

+ 3 - 0
Userland/Libraries/LibWeb/HTML/StructuredSerialize.cpp

@@ -39,6 +39,7 @@
 #include <LibWeb/Geometry/DOMMatrixReadOnly.h>
 #include <LibWeb/Geometry/DOMPoint.h>
 #include <LibWeb/Geometry/DOMPointReadOnly.h>
+#include <LibWeb/Geometry/DOMRect.h>
 #include <LibWeb/Geometry/DOMRectReadOnly.h>
 #include <LibWeb/HTML/MessagePort.h>
 #include <LibWeb/HTML/StructuredSerialize.h>
@@ -978,6 +979,8 @@ private:
             return Geometry::DOMPoint::create(realm);
         if (interface_name == "DOMRectReadOnly"sv)
             return Geometry::DOMRectReadOnly::create(realm);
+        if (interface_name == "DOMRect"sv)
+            return Geometry::DOMRect::create(realm);
 
         VERIFY_NOT_REACHED();
     }