mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
LibWeb: Make DOMRect, DOMRectReadOnly and DOMRectList GC-allocated
This commit is contained in:
parent
44415af428
commit
57db2529cf
11 changed files with 125 additions and 79 deletions
|
@ -137,7 +137,9 @@ set(SOURCES
|
||||||
FontCache.cpp
|
FontCache.cpp
|
||||||
Geometry/DOMPoint.cpp
|
Geometry/DOMPoint.cpp
|
||||||
Geometry/DOMPointReadOnly.cpp
|
Geometry/DOMPointReadOnly.cpp
|
||||||
|
Geometry/DOMRect.cpp
|
||||||
Geometry/DOMRectList.cpp
|
Geometry/DOMRectList.cpp
|
||||||
|
Geometry/DOMRectReadOnly.cpp
|
||||||
HTML/AttributeNames.cpp
|
HTML/AttributeNames.cpp
|
||||||
HTML/BrowsingContext.cpp
|
HTML/BrowsingContext.cpp
|
||||||
HTML/BrowsingContextContainer.cpp
|
HTML/BrowsingContextContainer.cpp
|
||||||
|
|
|
@ -561,7 +561,7 @@ bool Element::serializes_as_void() const
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://drafts.csswg.org/cssom-view/#dom-element-getboundingclientrect
|
// https://drafts.csswg.org/cssom-view/#dom-element-getboundingclientrect
|
||||||
NonnullRefPtr<Geometry::DOMRect> Element::get_bounding_client_rect() const
|
JS::NonnullGCPtr<Geometry::DOMRect> Element::get_bounding_client_rect() const
|
||||||
{
|
{
|
||||||
// // NOTE: Ensure that layout is up-to-date before looking at metrics.
|
// // NOTE: Ensure that layout is up-to-date before looking at metrics.
|
||||||
const_cast<Document&>(document()).update_layout();
|
const_cast<Document&>(document()).update_layout();
|
||||||
|
@ -569,22 +569,22 @@ NonnullRefPtr<Geometry::DOMRect> Element::get_bounding_client_rect() const
|
||||||
// FIXME: Support inline layout nodes as well.
|
// FIXME: Support inline layout nodes as well.
|
||||||
auto* paint_box = this->paint_box();
|
auto* paint_box = this->paint_box();
|
||||||
if (!paint_box)
|
if (!paint_box)
|
||||||
return Geometry::DOMRect::create(0, 0, 0, 0);
|
return Geometry::DOMRect::create_with_global_object(window(), 0, 0, 0, 0);
|
||||||
|
|
||||||
VERIFY(document().browsing_context());
|
VERIFY(document().browsing_context());
|
||||||
auto viewport_offset = document().browsing_context()->viewport_scroll_offset();
|
auto viewport_offset = document().browsing_context()->viewport_scroll_offset();
|
||||||
|
|
||||||
return Geometry::DOMRect::create(paint_box->absolute_rect().translated(-viewport_offset.x(), -viewport_offset.y()));
|
return Geometry::DOMRect::create(window(), paint_box->absolute_rect().translated(-viewport_offset.x(), -viewport_offset.y()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://drafts.csswg.org/cssom-view/#dom-element-getclientrects
|
// https://drafts.csswg.org/cssom-view/#dom-element-getclientrects
|
||||||
NonnullRefPtr<Geometry::DOMRectList> Element::get_client_rects() const
|
JS::NonnullGCPtr<Geometry::DOMRectList> Element::get_client_rects() const
|
||||||
{
|
{
|
||||||
NonnullRefPtrVector<Geometry::DOMRect> rects;
|
Vector<JS::Handle<Geometry::DOMRect>> rects;
|
||||||
|
|
||||||
// 1. If the element on which it was invoked does not have an associated layout box return an empty DOMRectList object and stop this algorithm.
|
// 1. If the element on which it was invoked does not have an associated layout box return an empty DOMRectList object and stop this algorithm.
|
||||||
if (!layout_node() || !layout_node()->is_box())
|
if (!layout_node() || !layout_node()->is_box())
|
||||||
return Geometry::DOMRectList::create(move(rects));
|
return Geometry::DOMRectList::create(window(), move(rects));
|
||||||
|
|
||||||
// FIXME: 2. If the element has an associated SVG layout box return a DOMRectList object containing a single DOMRect object that describes
|
// FIXME: 2. If the element has an associated SVG layout box return a DOMRectList object containing a single DOMRect object that describes
|
||||||
// the bounding box of the element as defined by the SVG specification, applying the transforms that apply to the element and its ancestors.
|
// the bounding box of the element as defined by the SVG specification, applying the transforms that apply to the element and its ancestors.
|
||||||
|
@ -597,8 +597,8 @@ NonnullRefPtr<Geometry::DOMRectList> Element::get_client_rects() const
|
||||||
// - Replace each anonymous block box with its child box(es) and repeat this until no anonymous block boxes are left in the final list.
|
// - Replace each anonymous block box with its child box(es) and repeat this until no anonymous block boxes are left in the final list.
|
||||||
|
|
||||||
auto bounding_rect = get_bounding_client_rect();
|
auto bounding_rect = get_bounding_client_rect();
|
||||||
rects.append(bounding_rect);
|
rects.append(*bounding_rect);
|
||||||
return Geometry::DOMRectList::create(move(rects));
|
return Geometry::DOMRectList::create(window(), move(rects));
|
||||||
}
|
}
|
||||||
|
|
||||||
int Element::client_top() const
|
int Element::client_top() const
|
||||||
|
|
|
@ -125,8 +125,8 @@ public:
|
||||||
bool is_void_element() const;
|
bool is_void_element() const;
|
||||||
bool serializes_as_void() const;
|
bool serializes_as_void() const;
|
||||||
|
|
||||||
NonnullRefPtr<Geometry::DOMRect> get_bounding_client_rect() const;
|
JS::NonnullGCPtr<Geometry::DOMRect> get_bounding_client_rect() const;
|
||||||
NonnullRefPtr<Geometry::DOMRectList> get_client_rects() const;
|
JS::NonnullGCPtr<Geometry::DOMRectList> get_client_rects() const;
|
||||||
|
|
||||||
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>);
|
virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>);
|
||||||
|
|
||||||
|
|
|
@ -451,9 +451,6 @@ namespace Web::Bindings {
|
||||||
class BlobWrapper;
|
class BlobWrapper;
|
||||||
class CryptoWrapper;
|
class CryptoWrapper;
|
||||||
class DOMExceptionWrapper;
|
class DOMExceptionWrapper;
|
||||||
class DOMRectListWrapper;
|
|
||||||
class DOMRectReadOnlyWrapper;
|
|
||||||
class DOMRectWrapper;
|
|
||||||
class FileWrapper;
|
class FileWrapper;
|
||||||
class HeadersWrapper;
|
class HeadersWrapper;
|
||||||
class HeadersIteratorWrapper;
|
class HeadersIteratorWrapper;
|
||||||
|
|
30
Userland/Libraries/LibWeb/Geometry/DOMRect.cpp
Normal file
30
Userland/Libraries/LibWeb/Geometry/DOMRect.cpp
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibWeb/Geometry/DOMRect.h>
|
||||||
|
#include <LibWeb/HTML/Window.h>
|
||||||
|
|
||||||
|
namespace Web::Geometry {
|
||||||
|
|
||||||
|
JS::NonnullGCPtr<DOMRect> DOMRect::create_with_global_object(HTML::Window& window, double x, double y, double width, double height)
|
||||||
|
{
|
||||||
|
return *window.heap().allocate<DOMRect>(window.realm(), window, x, y, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
JS::NonnullGCPtr<DOMRect> DOMRect::create(HTML::Window& window, Gfx::FloatRect const& rect)
|
||||||
|
{
|
||||||
|
return create_with_global_object(window, rect.x(), rect.y(), rect.width(), rect.height());
|
||||||
|
}
|
||||||
|
|
||||||
|
DOMRect::DOMRect(HTML::Window& window, double x, double y, double width, double height)
|
||||||
|
: DOMRectReadOnly(window, x, y, width, height)
|
||||||
|
{
|
||||||
|
set_prototype(&window.cached_web_prototype("DOMRect"));
|
||||||
|
}
|
||||||
|
|
||||||
|
DOMRect::~DOMRect() = default;
|
||||||
|
|
||||||
|
}
|
|
@ -11,25 +11,14 @@
|
||||||
namespace Web::Geometry {
|
namespace Web::Geometry {
|
||||||
|
|
||||||
// https://drafts.fxtf.org/geometry/#DOMRect
|
// https://drafts.fxtf.org/geometry/#DOMRect
|
||||||
class DOMRect final
|
class DOMRect final : public DOMRectReadOnly {
|
||||||
: public DOMRectReadOnly {
|
WEB_PLATFORM_OBJECT(DOMRect, DOMRectReadOnly);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using WrapperType = Bindings::DOMRectWrapper;
|
static JS::NonnullGCPtr<DOMRect> create_with_global_object(HTML::Window&, double x = 0, double y = 0, double width = 0, double height = 0);
|
||||||
|
static JS::NonnullGCPtr<DOMRect> create(HTML::Window&, Gfx::FloatRect const&);
|
||||||
|
|
||||||
static NonnullRefPtr<DOMRect> create_with_global_object(HTML::Window&, double x = 0, double y = 0, double width = 0, double height = 0)
|
virtual ~DOMRect() override;
|
||||||
{
|
|
||||||
return DOMRect::create(x, y, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
static NonnullRefPtr<DOMRect> create(double x = 0, double y = 0, double width = 0, double height = 0)
|
|
||||||
{
|
|
||||||
return adopt_ref(*new DOMRect(x, y, width, height));
|
|
||||||
}
|
|
||||||
|
|
||||||
static NonnullRefPtr<DOMRect> create(Gfx::FloatRect const& rect)
|
|
||||||
{
|
|
||||||
return adopt_ref(*new DOMRect(rect.x(), rect.y(), rect.width(), rect.height()));
|
|
||||||
}
|
|
||||||
|
|
||||||
double x() const { return m_rect.x(); }
|
double x() const { return m_rect.x(); }
|
||||||
double y() const { return m_rect.y(); }
|
double y() const { return m_rect.y(); }
|
||||||
|
@ -42,9 +31,9 @@ public:
|
||||||
void set_height(double height) { m_rect.set_height(height); }
|
void set_height(double height) { m_rect.set_height(height); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DOMRect(float x, float y, float width, float height)
|
DOMRect(HTML::Window&, double x, double y, double width, double height);
|
||||||
: DOMRectReadOnly(x, y, width, height)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WRAPPER_HACK(DOMRect, Web::Geometry)
|
||||||
|
|
|
@ -4,16 +4,29 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Heap/Handle.h>
|
||||||
#include <LibWeb/Geometry/DOMRect.h>
|
#include <LibWeb/Geometry/DOMRect.h>
|
||||||
#include <LibWeb/Geometry/DOMRectList.h>
|
#include <LibWeb/Geometry/DOMRectList.h>
|
||||||
|
#include <LibWeb/HTML/Window.h>
|
||||||
|
|
||||||
namespace Web::Geometry {
|
namespace Web::Geometry {
|
||||||
|
|
||||||
DOMRectList::DOMRectList(NonnullRefPtrVector<DOMRect>&& rects)
|
JS::NonnullGCPtr<DOMRectList> DOMRectList::create(HTML::Window& window, Vector<JS::Handle<DOMRect>> rect_handles)
|
||||||
: m_rects(move(rects))
|
{
|
||||||
|
Vector<JS::NonnullGCPtr<DOMRect>> rects;
|
||||||
|
for (auto& rect : rect_handles)
|
||||||
|
rects.append(*rect);
|
||||||
|
return *window.heap().allocate<DOMRectList>(window.realm(), window, move(rects));
|
||||||
|
}
|
||||||
|
|
||||||
|
DOMRectList::DOMRectList(HTML::Window& window, Vector<JS::NonnullGCPtr<DOMRect>> rects)
|
||||||
|
: Bindings::LegacyPlatformObject(window.cached_web_prototype("DOMRectList"))
|
||||||
|
, m_rects(move(rects))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DOMRectList::~DOMRectList() = default;
|
||||||
|
|
||||||
// https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
|
// https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
|
||||||
u32 DOMRectList::length() const
|
u32 DOMRectList::length() const
|
||||||
{
|
{
|
||||||
|
@ -28,7 +41,7 @@ DOMRect const* DOMRectList::item(u32 index) const
|
||||||
// Otherwise, the DOMRect object at index must be returned. Indices are zero-based.
|
// Otherwise, the DOMRect object at index must be returned. Indices are zero-based.
|
||||||
if (index >= m_rects.size())
|
if (index >= m_rects.size())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
return &m_rects[index];
|
return m_rects[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DOMRectList::is_supported_property_index(u32 index) const
|
bool DOMRectList::is_supported_property_index(u32 index) const
|
||||||
|
@ -36,4 +49,12 @@ bool DOMRectList::is_supported_property_index(u32 index) const
|
||||||
return index < m_rects.size();
|
return index < m_rects.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JS::Value DOMRectList::item_value(size_t index) const
|
||||||
|
{
|
||||||
|
if (index >= m_rects.size())
|
||||||
|
return JS::js_undefined();
|
||||||
|
|
||||||
|
return m_rects[index].ptr();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,42 +6,33 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Noncopyable.h>
|
|
||||||
#include <AK/NonnullRefPtrVector.h>
|
|
||||||
#include <AK/RefCounted.h>
|
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibWeb/Bindings/Wrappable.h>
|
#include <LibWeb/Bindings/LegacyPlatformObject.h>
|
||||||
#include <LibWeb/Forward.h>
|
|
||||||
#include <LibWeb/Geometry/DOMRect.h>
|
#include <LibWeb/Geometry/DOMRect.h>
|
||||||
|
|
||||||
namespace Web::Geometry {
|
namespace Web::Geometry {
|
||||||
|
|
||||||
// https://drafts.fxtf.org/geometry-1/#DOMRectList
|
// https://drafts.fxtf.org/geometry-1/#DOMRectList
|
||||||
class DOMRectList final
|
class DOMRectList final : public Bindings::LegacyPlatformObject {
|
||||||
: public RefCounted<DOMRectList>
|
WEB_PLATFORM_OBJECT(DOMRectList, Bindings::LegacyPlatformObject);
|
||||||
, public Bindings::Wrappable {
|
|
||||||
AK_MAKE_NONCOPYABLE(DOMRectList);
|
|
||||||
AK_MAKE_NONMOVABLE(DOMRectList);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using WrapperType = Bindings::DOMRectListWrapper;
|
static JS::NonnullGCPtr<DOMRectList> create(HTML::Window&, Vector<JS::Handle<DOMRect>>);
|
||||||
|
|
||||||
static NonnullRefPtr<DOMRectList> create(NonnullRefPtrVector<DOMRect>&& rects)
|
virtual ~DOMRectList() override;
|
||||||
{
|
|
||||||
return adopt_ref(*new DOMRectList(move(rects)));
|
|
||||||
}
|
|
||||||
|
|
||||||
~DOMRectList() = default;
|
|
||||||
|
|
||||||
u32 length() const;
|
u32 length() const;
|
||||||
DOMRect const* item(u32 index) const;
|
DOMRect const* item(u32 index) const;
|
||||||
|
|
||||||
bool is_supported_property_index(u32) const;
|
virtual bool is_supported_property_index(u32) const override;
|
||||||
|
virtual JS::Value item_value(size_t index) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DOMRectList(NonnullRefPtrVector<DOMRect>&& rects);
|
DOMRectList(HTML::Window&, Vector<JS::NonnullGCPtr<DOMRect>>);
|
||||||
|
|
||||||
NonnullRefPtrVector<DOMRect> m_rects;
|
Vector<JS::NonnullGCPtr<DOMRect>> m_rects;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WRAPPER_HACK(DOMRectList, Web::Geometry)
|
||||||
|
|
26
Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.cpp
Normal file
26
Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.cpp
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibWeb/Geometry/DOMRectReadOnly.h>
|
||||||
|
#include <LibWeb/HTML/Window.h>
|
||||||
|
|
||||||
|
namespace Web::Geometry {
|
||||||
|
|
||||||
|
JS::NonnullGCPtr<DOMRectReadOnly> DOMRectReadOnly::create_with_global_object(HTML::Window& window, double x, double y, double width, double height)
|
||||||
|
{
|
||||||
|
return *window.heap().allocate<DOMRectReadOnly>(window.realm(), window, x, y, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
DOMRectReadOnly::DOMRectReadOnly(HTML::Window& window, double x, double y, double width, double height)
|
||||||
|
: PlatformObject(window.realm())
|
||||||
|
, m_rect(x, y, width, height)
|
||||||
|
{
|
||||||
|
set_prototype(&window.cached_web_prototype("DOMRectReadOnly"));
|
||||||
|
}
|
||||||
|
|
||||||
|
DOMRectReadOnly::~DOMRectReadOnly() = default;
|
||||||
|
|
||||||
|
}
|
|
@ -6,29 +6,20 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/RefCounted.h>
|
|
||||||
#include <LibGfx/Rect.h>
|
#include <LibGfx/Rect.h>
|
||||||
#include <LibWeb/Bindings/Wrappable.h>
|
#include <LibWeb/Bindings/PlatformObject.h>
|
||||||
#include <LibWeb/Forward.h>
|
#include <LibWeb/Forward.h>
|
||||||
|
|
||||||
namespace Web::Geometry {
|
namespace Web::Geometry {
|
||||||
|
|
||||||
// https://drafts.fxtf.org/geometry/#domrectreadonly
|
// https://drafts.fxtf.org/geometry/#domrectreadonly
|
||||||
class DOMRectReadOnly
|
class DOMRectReadOnly : public Bindings::PlatformObject {
|
||||||
: public RefCounted<DOMRectReadOnly>
|
WEB_PLATFORM_OBJECT(DOMRectReadOnly, Bindings::PlatformObject);
|
||||||
, public Bindings::Wrappable {
|
|
||||||
public:
|
public:
|
||||||
using WrapperType = Bindings::DOMRectReadOnlyWrapper;
|
static JS::NonnullGCPtr<DOMRectReadOnly> create_with_global_object(HTML::Window&, double x = 0, double y = 0, double width = 0, double height = 0);
|
||||||
|
|
||||||
static NonnullRefPtr<DOMRectReadOnly> create_with_global_object(HTML::Window&, double x = 0, double y = 0, double width = 0, double height = 0)
|
virtual ~DOMRectReadOnly() override;
|
||||||
{
|
|
||||||
return DOMRectReadOnly::create(x, y, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
static NonnullRefPtr<DOMRectReadOnly> create(double x = 0, double y = 0, double width = 0, double height = 0)
|
|
||||||
{
|
|
||||||
return adopt_ref(*new DOMRectReadOnly(x, y, width, height));
|
|
||||||
}
|
|
||||||
|
|
||||||
double x() const { return m_rect.x(); }
|
double x() const { return m_rect.x(); }
|
||||||
double y() const { return m_rect.y(); }
|
double y() const { return m_rect.y(); }
|
||||||
|
@ -41,11 +32,10 @@ public:
|
||||||
double left() const { return min(x(), x() + width()); }
|
double left() const { return min(x(), x() + width()); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
DOMRectReadOnly(float x, float y, float width, float height)
|
DOMRectReadOnly(HTML::Window&, double x, double y, double width, double height);
|
||||||
: m_rect(x, y, width, height)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Gfx::FloatRect m_rect;
|
Gfx::FloatRect m_rect;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WRAPPER_HACK(DOMRectReadOnly, Web::Geometry)
|
||||||
|
|
|
@ -58,9 +58,9 @@ libweb_js_wrapper(FileAPI/Blob)
|
||||||
libweb_js_wrapper(FileAPI/File)
|
libweb_js_wrapper(FileAPI/File)
|
||||||
libweb_js_wrapper(Geometry/DOMPoint NO_INSTANCE)
|
libweb_js_wrapper(Geometry/DOMPoint NO_INSTANCE)
|
||||||
libweb_js_wrapper(Geometry/DOMPointReadOnly NO_INSTANCE)
|
libweb_js_wrapper(Geometry/DOMPointReadOnly NO_INSTANCE)
|
||||||
libweb_js_wrapper(Geometry/DOMRect)
|
libweb_js_wrapper(Geometry/DOMRect NO_INSTANCE)
|
||||||
libweb_js_wrapper(Geometry/DOMRectList)
|
libweb_js_wrapper(Geometry/DOMRectList NO_INSTANCE)
|
||||||
libweb_js_wrapper(Geometry/DOMRectReadOnly)
|
libweb_js_wrapper(Geometry/DOMRectReadOnly NO_INSTANCE)
|
||||||
libweb_js_wrapper(HTML/CanvasGradient NO_INSTANCE)
|
libweb_js_wrapper(HTML/CanvasGradient NO_INSTANCE)
|
||||||
libweb_js_wrapper(HTML/CanvasRenderingContext2D NO_INSTANCE)
|
libweb_js_wrapper(HTML/CanvasRenderingContext2D NO_INSTANCE)
|
||||||
libweb_js_wrapper(HTML/CloseEvent NO_INSTANCE)
|
libweb_js_wrapper(HTML/CloseEvent NO_INSTANCE)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue