1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:27:43 +00:00

LibWeb: Change DOM::Position to be GC-allocated

This commit is contained in:
Aliaksandr Kalenik 2023-09-26 19:34:21 +02:00 committed by Andreas Kling
parent 35623ad52e
commit 46254101f7
10 changed files with 72 additions and 62 deletions

View file

@ -13,8 +13,8 @@
namespace Web::DOM {
Position::Position(Node& node, unsigned offset)
: m_node(JS::make_handle(node))
Position::Position(JS::GCPtr<Node> node, unsigned offset)
: m_node(node)
, m_offset(offset)
{
}
@ -23,7 +23,7 @@ ErrorOr<String> Position::to_string() const
{
if (!node())
return String::formatted("DOM::Position(nullptr, {})", offset());
return String::formatted("DOM::Position({} ({})), {})", node()->node_name(), node(), offset());
return String::formatted("DOM::Position({} ({})), {})", node()->node_name(), node().ptr(), offset());
}
bool Position::increment_offset()

View file

@ -10,21 +10,23 @@
#include <AK/Error.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <LibJS/Heap/Handle.h>
#include <LibJS/Heap/Heap.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/Forward.h>
namespace Web::DOM {
class Position {
class Position final : public JS::Cell {
JS_CELL(Position, JS::Cell);
public:
Position() = default;
Position(Node&, unsigned offset);
[[nodiscard]] static JS::NonnullGCPtr<Position> create(JS::Realm& realm, JS::NonnullGCPtr<Node> node, unsigned offset)
{
return realm.heap().allocate<Position>(realm, node, offset);
}
bool is_valid() const { return m_node.ptr(); }
Node* node() { return m_node.cell(); }
Node const* node() const { return m_node.cell(); }
JS::GCPtr<Node> node() { return m_node; }
JS::GCPtr<Node const> node() const { return m_node; }
unsigned offset() const { return m_offset; }
bool offset_is_at_end_of_node() const;
@ -32,15 +34,17 @@ public:
bool increment_offset();
bool decrement_offset();
bool operator==(Position const& other) const
bool equals(JS::NonnullGCPtr<Position> other) const
{
return m_node.ptr() == other.m_node.ptr() && m_offset == other.m_offset;
return m_node.ptr() == other->m_node.ptr() && m_offset == other->m_offset;
}
ErrorOr<String> to_string() const;
private:
JS::Handle<Node> m_node;
Position(JS::GCPtr<Node>, unsigned offset);
JS::GCPtr<Node> m_node;
unsigned m_offset { 0 };
};