LibWeb: Use GC::Ref<Node> in Range for start/end containers

Let's propagate the GC'ness of it all to the users of Range.
This commit is contained in:
Jelle Raaijmakers 2024-11-30 10:32:32 +01:00 committed by Andreas Kling
parent bf724ba2f6
commit c87960f8f3
Notes: github-actions[bot] 2024-11-30 16:37:32 +00:00
6 changed files with 92 additions and 99 deletions

View file

@ -11,8 +11,8 @@
namespace Web::DOM { namespace Web::DOM {
AbstractRange::AbstractRange(Node& start_container, WebIDL::UnsignedLong start_offset, Node& end_container, WebIDL::UnsignedLong end_offset) AbstractRange::AbstractRange(GC::Ref<Node> start_container, WebIDL::UnsignedLong start_offset, GC::Ref<Node> end_container, WebIDL::UnsignedLong end_offset)
: Bindings::PlatformObject(start_container.realm()) : Bindings::PlatformObject(start_container->realm())
, m_start_container(start_container) , m_start_container(start_container)
, m_start_offset(start_offset) , m_start_offset(start_offset)
, m_end_container(end_container) , m_end_container(end_container)

View file

@ -20,12 +20,10 @@ class AbstractRange : public Bindings::PlatformObject {
public: public:
virtual ~AbstractRange() override; virtual ~AbstractRange() override;
Node* start_container() { return m_start_container.ptr(); } GC::Ref<Node> start_container() const { return m_start_container; }
Node const* start_container() const { return m_start_container.ptr(); }
WebIDL::UnsignedLong start_offset() const { return m_start_offset; } WebIDL::UnsignedLong start_offset() const { return m_start_offset; }
Node* end_container() { return m_end_container.ptr(); } GC::Ref<Node> end_container() const { return m_end_container; }
Node const* end_container() const { return m_end_container.ptr(); }
WebIDL::UnsignedLong end_offset() const { return m_end_offset; } WebIDL::UnsignedLong end_offset() const { return m_end_offset; }
// https://dom.spec.whatwg.org/#range-collapsed // https://dom.spec.whatwg.org/#range-collapsed
@ -36,7 +34,7 @@ public:
} }
protected: protected:
AbstractRange(Node& start_container, WebIDL::UnsignedLong start_offset, Node& end_container, WebIDL::UnsignedLong end_offset); AbstractRange(GC::Ref<Node> start_container, WebIDL::UnsignedLong start_offset, GC::Ref<Node> end_container, WebIDL::UnsignedLong end_offset);
virtual void initialize(JS::Realm&) override; virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override; virtual void visit_edges(Cell::Visitor&) override;

View file

@ -48,9 +48,9 @@ GC::Ref<Range> Range::create(Document& document)
return realm.create<Range>(document); return realm.create<Range>(document);
} }
GC::Ref<Range> Range::create(Node& start_container, WebIDL::UnsignedLong start_offset, Node& end_container, WebIDL::UnsignedLong end_offset) GC::Ref<Range> Range::create(GC::Ref<Node> start_container, WebIDL::UnsignedLong start_offset, GC::Ref<Node> end_container, WebIDL::UnsignedLong end_offset)
{ {
auto& realm = start_container.realm(); auto& realm = start_container->realm();
return realm.create<Range>(start_container, start_offset, end_container, end_offset); return realm.create<Range>(start_container, start_offset, end_container, end_offset);
} }
@ -65,7 +65,7 @@ Range::Range(Document& document)
{ {
} }
Range::Range(Node& start_container, WebIDL::UnsignedLong start_offset, Node& end_container, WebIDL::UnsignedLong end_offset) Range::Range(GC::Ref<Node> start_container, WebIDL::UnsignedLong start_offset, GC::Ref<Node> end_container, WebIDL::UnsignedLong end_offset)
: AbstractRange(start_container, start_offset, end_container, end_offset) : AbstractRange(start_container, start_offset, end_container, end_offset)
{ {
live_ranges().set(this); live_ranges().set(this);
@ -110,25 +110,20 @@ void Range::update_associated_selection()
} }
// https://dom.spec.whatwg.org/#concept-range-root // https://dom.spec.whatwg.org/#concept-range-root
Node& Range::root() GC::Ref<Node> Range::root() const
{ {
// The root of a live range is the root of its start node. // The root of a live range is the root of its start node.
return m_start_container->root(); return m_start_container->root();
} }
Node const& Range::root() const
{
return m_start_container->root();
}
// https://dom.spec.whatwg.org/#concept-range-bp-position // https://dom.spec.whatwg.org/#concept-range-bp-position
RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(Node const& node_a, u32 offset_a, Node const& node_b, u32 offset_b) RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(GC::Ref<Node> node_a, u32 offset_a, GC::Ref<Node> node_b, u32 offset_b)
{ {
// 1. Assert: nodeA and nodeB have the same root. // 1. Assert: nodeA and nodeB have the same root.
VERIFY(&node_a.root() == &node_b.root()); VERIFY(&node_a->root() == &node_b->root());
// 2. If nodeA is nodeB, then return equal if offsetA is offsetB, before if offsetA is less than offsetB, and after if offsetA is greater than offsetB. // 2. If nodeA is nodeB, then return equal if offsetA is offsetB, before if offsetA is less than offsetB, and after if offsetA is greater than offsetB.
if (&node_a == &node_b) { if (node_a == node_b) {
if (offset_a == offset_b) if (offset_a == offset_b)
return RelativeBoundaryPointPosition::Equal; return RelativeBoundaryPointPosition::Equal;
@ -139,7 +134,7 @@ RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_bound
} }
// 3. If nodeA is following nodeB, then if the position of (nodeB, offsetB) relative to (nodeA, offsetA) is before, return after, and if it is after, return before. // 3. If nodeA is following nodeB, then if the position of (nodeB, offsetB) relative to (nodeA, offsetA) is before, return after, and if it is after, return before.
if (node_a.is_following(node_b)) { if (node_a->is_following(node_b)) {
auto relative_position = position_of_boundary_point_relative_to_other_boundary_point(node_b, offset_b, node_a, offset_a); auto relative_position = position_of_boundary_point_relative_to_other_boundary_point(node_b, offset_b, node_a, offset_a);
if (relative_position == RelativeBoundaryPointPosition::Before) if (relative_position == RelativeBoundaryPointPosition::Before)
@ -150,12 +145,12 @@ RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_bound
} }
// 4. If nodeA is an ancestor of nodeB: // 4. If nodeA is an ancestor of nodeB:
if (node_a.is_ancestor_of(node_b)) { if (node_a->is_ancestor_of(node_b)) {
// 1. Let child be nodeB. // 1. Let child be nodeB.
GC::Ref<Node const> child = node_b; GC::Ref<Node const> child = node_b;
// 2. While child is not a child of nodeA, set child to its parent. // 2. While child is not a child of nodeA, set child to its parent.
while (!node_a.is_parent_of(child)) { while (!node_a->is_parent_of(child)) {
auto* parent = child->parent(); auto* parent = child->parent();
VERIFY(parent); VERIFY(parent);
child = *parent; child = *parent;
@ -170,16 +165,16 @@ RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_bound
return RelativeBoundaryPointPosition::Before; return RelativeBoundaryPointPosition::Before;
} }
WebIDL::ExceptionOr<void> Range::set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end) WebIDL::ExceptionOr<void> Range::set_start_or_end(GC::Ref<Node> node, u32 offset, StartOrEnd start_or_end)
{ {
// To set the start or end of a range to a boundary point (node, offset), run these steps: // To set the start or end of a range to a boundary point (node, offset), run these steps:
// 1. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException. // 1. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
if (is<DocumentType>(node)) if (is<DocumentType>(*node))
return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType."_string); return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType."_string);
// 2. If offset is greater than nodes length, then throw an "IndexSizeError" DOMException. // 2. If offset is greater than nodes length, then throw an "IndexSizeError" DOMException.
if (offset > node.length()) if (offset > node->length())
return WebIDL::IndexSizeError::create(realm(), MUST(String::formatted("Node does not contain a child at offset {}", offset))); return WebIDL::IndexSizeError::create(realm(), MUST(String::formatted("Node does not contain a child at offset {}", offset)));
// 3. Let bp be the boundary point (node, offset). // 3. Let bp be the boundary point (node, offset).
@ -188,7 +183,7 @@ WebIDL::ExceptionOr<void> Range::set_start_or_end(Node& node, u32 offset, StartO
// -> If these steps were invoked as "set the start" // -> If these steps were invoked as "set the start"
// 1. If ranges root is not equal to nodes root, or if bp is after the ranges end, set ranges end to bp. // 1. If ranges root is not equal to nodes root, or if bp is after the ranges end, set ranges end to bp.
if (&root() != &node.root() || position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset) == RelativeBoundaryPointPosition::After) { if (root().ptr() != &node->root() || position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_end_container, m_end_offset) == RelativeBoundaryPointPosition::After) {
m_end_container = node; m_end_container = node;
m_end_offset = offset; m_end_offset = offset;
} }
@ -201,7 +196,7 @@ WebIDL::ExceptionOr<void> Range::set_start_or_end(Node& node, u32 offset, StartO
VERIFY(start_or_end == StartOrEnd::End); VERIFY(start_or_end == StartOrEnd::End);
// 1. If ranges root is not equal to nodes root, or if bp is before the ranges start, set ranges start to bp. // 1. If ranges root is not equal to nodes root, or if bp is before the ranges start, set ranges start to bp.
if (&root() != &node.root() || position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset) == RelativeBoundaryPointPosition::Before) { if (root().ptr() != &node->root() || position_of_boundary_point_relative_to_other_boundary_point(node, offset, m_start_container, m_start_offset) == RelativeBoundaryPointPosition::Before) {
m_start_container = node; m_start_container = node;
m_start_offset = offset; m_start_offset = offset;
} }
@ -216,72 +211,72 @@ WebIDL::ExceptionOr<void> Range::set_start_or_end(Node& node, u32 offset, StartO
} }
// https://dom.spec.whatwg.org/#concept-range-bp-set // https://dom.spec.whatwg.org/#concept-range-bp-set
WebIDL::ExceptionOr<void> Range::set_start(Node& node, WebIDL::UnsignedLong offset) WebIDL::ExceptionOr<void> Range::set_start(GC::Ref<Node> node, WebIDL::UnsignedLong offset)
{ {
// The setStart(node, offset) method steps are to set the start of this to boundary point (node, offset). // The setStart(node, offset) method steps are to set the start of this to boundary point (node, offset).
return set_start_or_end(node, offset, StartOrEnd::Start); return set_start_or_end(node, offset, StartOrEnd::Start);
} }
WebIDL::ExceptionOr<void> Range::set_end(Node& node, WebIDL::UnsignedLong offset) WebIDL::ExceptionOr<void> Range::set_end(GC::Ref<Node> node, WebIDL::UnsignedLong offset)
{ {
// The setEnd(node, offset) method steps are to set the end of this to boundary point (node, offset). // The setEnd(node, offset) method steps are to set the end of this to boundary point (node, offset).
return set_start_or_end(node, offset, StartOrEnd::End); return set_start_or_end(node, offset, StartOrEnd::End);
} }
// https://dom.spec.whatwg.org/#dom-range-setstartbefore // https://dom.spec.whatwg.org/#dom-range-setstartbefore
WebIDL::ExceptionOr<void> Range::set_start_before(Node& node) WebIDL::ExceptionOr<void> Range::set_start_before(GC::Ref<Node> node)
{ {
// 1. Let parent be nodes parent. // 1. Let parent be nodes parent.
auto* parent = node.parent(); auto* parent = node->parent();
// 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
if (!parent) if (!parent)
return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent."_string); return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent."_string);
// 3. Set the start of this to boundary point (parent, nodes index). // 3. Set the start of this to boundary point (parent, nodes index).
return set_start_or_end(*parent, node.index(), StartOrEnd::Start); return set_start_or_end(*parent, node->index(), StartOrEnd::Start);
} }
// https://dom.spec.whatwg.org/#dom-range-setstartafter // https://dom.spec.whatwg.org/#dom-range-setstartafter
WebIDL::ExceptionOr<void> Range::set_start_after(Node& node) WebIDL::ExceptionOr<void> Range::set_start_after(GC::Ref<Node> node)
{ {
// 1. Let parent be nodes parent. // 1. Let parent be nodes parent.
auto* parent = node.parent(); auto* parent = node->parent();
// 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
if (!parent) if (!parent)
return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent."_string); return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent."_string);
// 3. Set the start of this to boundary point (parent, nodes index plus 1). // 3. Set the start of this to boundary point (parent, nodes index plus 1).
return set_start_or_end(*parent, node.index() + 1, StartOrEnd::Start); return set_start_or_end(*parent, node->index() + 1, StartOrEnd::Start);
} }
// https://dom.spec.whatwg.org/#dom-range-setendbefore // https://dom.spec.whatwg.org/#dom-range-setendbefore
WebIDL::ExceptionOr<void> Range::set_end_before(Node& node) WebIDL::ExceptionOr<void> Range::set_end_before(GC::Ref<Node> node)
{ {
// 1. Let parent be nodes parent. // 1. Let parent be nodes parent.
auto* parent = node.parent(); auto* parent = node->parent();
// 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
if (!parent) if (!parent)
return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent."_string); return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent."_string);
// 3. Set the end of this to boundary point (parent, nodes index). // 3. Set the end of this to boundary point (parent, nodes index).
return set_start_or_end(*parent, node.index(), StartOrEnd::End); return set_start_or_end(*parent, node->index(), StartOrEnd::End);
} }
// https://dom.spec.whatwg.org/#dom-range-setendafter // https://dom.spec.whatwg.org/#dom-range-setendafter
WebIDL::ExceptionOr<void> Range::set_end_after(Node& node) WebIDL::ExceptionOr<void> Range::set_end_after(GC::Ref<Node> node)
{ {
// 1. Let parent be nodes parent. // 1. Let parent be nodes parent.
auto* parent = node.parent(); auto* parent = node->parent();
// 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
if (!parent) if (!parent)
return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent."_string); return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent."_string);
// 3. Set the end of this to boundary point (parent, nodes index plus 1). // 3. Set the end of this to boundary point (parent, nodes index plus 1).
return set_start_or_end(*parent, node.index() + 1, StartOrEnd::End); return set_start_or_end(*parent, node->index() + 1, StartOrEnd::End);
} }
// https://dom.spec.whatwg.org/#dom-range-compareboundarypoints // https://dom.spec.whatwg.org/#dom-range-compareboundarypoints
@ -297,7 +292,7 @@ WebIDL::ExceptionOr<WebIDL::Short> Range::compare_boundary_points(WebIDL::Unsign
return WebIDL::NotSupportedError::create(realm(), MUST(String::formatted("Expected 'how' to be one of START_TO_START (0), START_TO_END (1), END_TO_END (2) or END_TO_START (3), got {}", how))); return WebIDL::NotSupportedError::create(realm(), MUST(String::formatted("Expected 'how' to be one of START_TO_START (0), START_TO_END (1), END_TO_END (2) or END_TO_START (3), got {}", how)));
// 2. If thiss root is not the same as sourceRanges root, then throw a "WrongDocumentError" DOMException. // 2. If thiss root is not the same as sourceRanges root, then throw a "WrongDocumentError" DOMException.
if (&root() != &source_range.root()) if (root() != source_range.root())
return WebIDL::WrongDocumentError::create(realm(), "This range is not in the same tree as the source range."_string); return WebIDL::WrongDocumentError::create(realm(), "This range is not in the same tree as the source range."_string);
GC::Ptr<Node> this_point_node; GC::Ptr<Node> this_point_node;
@ -372,17 +367,17 @@ WebIDL::ExceptionOr<WebIDL::Short> Range::compare_boundary_points(WebIDL::Unsign
} }
// https://dom.spec.whatwg.org/#concept-range-select // https://dom.spec.whatwg.org/#concept-range-select
WebIDL::ExceptionOr<void> Range::select(Node& node) WebIDL::ExceptionOr<void> Range::select(GC::Ref<Node> node)
{ {
// 1. Let parent be nodes parent. // 1. Let parent be nodes parent.
auto* parent = node.parent(); auto* parent = node->parent();
// 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException. // 2. If parent is null, then throw an "InvalidNodeTypeError" DOMException.
if (!parent) if (!parent)
return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent."_string); return WebIDL::InvalidNodeTypeError::create(realm(), "Given node has no parent."_string);
// 3. Let index be nodes index. // 3. Let index be nodes index.
auto index = node.index(); auto index = node->index();
// 4. Set ranges start to boundary point (parent, index). // 4. Set ranges start to boundary point (parent, index).
m_start_container = *parent; m_start_container = *parent;
@ -397,7 +392,7 @@ WebIDL::ExceptionOr<void> Range::select(Node& node)
} }
// https://dom.spec.whatwg.org/#dom-range-selectnode // https://dom.spec.whatwg.org/#dom-range-selectnode
WebIDL::ExceptionOr<void> Range::select_node(Node& node) WebIDL::ExceptionOr<void> Range::select_node(GC::Ref<Node> node)
{ {
// The selectNode(node) method steps are to select node within this. // The selectNode(node) method steps are to select node within this.
return select(node); return select(node);
@ -418,14 +413,14 @@ void Range::collapse(bool to_start)
} }
// https://dom.spec.whatwg.org/#dom-range-selectnodecontents // https://dom.spec.whatwg.org/#dom-range-selectnodecontents
WebIDL::ExceptionOr<void> Range::select_node_contents(Node& node) WebIDL::ExceptionOr<void> Range::select_node_contents(GC::Ref<Node> node)
{ {
// 1. If node is a doctype, throw an "InvalidNodeTypeError" DOMException. // 1. If node is a doctype, throw an "InvalidNodeTypeError" DOMException.
if (is<DocumentType>(node)) if (is<DocumentType>(*node))
return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType."_string); return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType."_string);
// 2. Let length be the length of node. // 2. Let length be the length of node.
auto length = node.length(); auto length = node->length();
// 3. Set start to the boundary point (node, 0). // 3. Set start to the boundary point (node, 0).
m_start_container = node; m_start_container = node;
@ -481,21 +476,21 @@ GC::Ref<Node> Range::common_ancestor_container() const
} }
// https://dom.spec.whatwg.org/#dom-range-intersectsnode // https://dom.spec.whatwg.org/#dom-range-intersectsnode
bool Range::intersects_node(Node const& node) const bool Range::intersects_node(GC::Ref<Node> node) const
{ {
// 1. If nodes root is different from thiss root, return false. // 1. If nodes root is different from thiss root, return false.
if (&node.root() != &root()) if (&node->root() != root().ptr())
return false; return false;
// 2. Let parent be nodes parent. // 2. Let parent be nodes parent.
auto* parent = node.parent(); auto* parent = node->parent();
// 3. If parent is null, return true. // 3. If parent is null, return true.
if (!parent) if (!parent)
return true; return true;
// 4. Let offset be nodes index. // 4. Let offset be nodes index.
auto offset = node.index(); auto offset = node->index();
// 5. If (parent, offset) is before end and (parent, offset plus 1) is after start, return true // 5. If (parent, offset) is before end and (parent, offset plus 1) is after start, return true
auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(*parent, offset, m_end_container, m_end_offset); auto relative_position_to_end = position_of_boundary_point_relative_to_other_boundary_point(*parent, offset, m_end_container, m_end_offset);
@ -508,18 +503,18 @@ bool Range::intersects_node(Node const& node) const
} }
// https://dom.spec.whatwg.org/#dom-range-ispointinrange // https://dom.spec.whatwg.org/#dom-range-ispointinrange
WebIDL::ExceptionOr<bool> Range::is_point_in_range(Node const& node, WebIDL::UnsignedLong offset) const WebIDL::ExceptionOr<bool> Range::is_point_in_range(GC::Ref<Node> node, WebIDL::UnsignedLong offset) const
{ {
// 1. If nodes root is different from thiss root, return false. // 1. If nodes root is different from thiss root, return false.
if (&node.root() != &root()) if (&node->root() != root().ptr())
return false; return false;
// 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException. // 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
if (is<DocumentType>(node)) if (is<DocumentType>(*node))
return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType."_string); return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType."_string);
// 3. If offset is greater than nodes length, then throw an "IndexSizeError" DOMException. // 3. If offset is greater than nodes length, then throw an "IndexSizeError" DOMException.
if (offset > node.length()) if (offset > node->length())
return WebIDL::IndexSizeError::create(realm(), MUST(String::formatted("Node does not contain a child at offset {}", offset))); return WebIDL::IndexSizeError::create(realm(), MUST(String::formatted("Node does not contain a child at offset {}", offset)));
// 4. If (node, offset) is before start or after end, return false. // 4. If (node, offset) is before start or after end, return false.
@ -533,18 +528,18 @@ WebIDL::ExceptionOr<bool> Range::is_point_in_range(Node const& node, WebIDL::Uns
} }
// https://dom.spec.whatwg.org/#dom-range-comparepoint // https://dom.spec.whatwg.org/#dom-range-comparepoint
WebIDL::ExceptionOr<WebIDL::Short> Range::compare_point(Node const& node, WebIDL::UnsignedLong offset) const WebIDL::ExceptionOr<WebIDL::Short> Range::compare_point(GC::Ref<Node> node, WebIDL::UnsignedLong offset) const
{ {
// 1. If nodes root is different from thiss root, then throw a "WrongDocumentError" DOMException. // 1. If nodes root is different from thiss root, then throw a "WrongDocumentError" DOMException.
if (&node.root() != &root()) if (&node->root() != root().ptr())
return WebIDL::WrongDocumentError::create(realm(), "Given node is not in the same document as the range."_string); return WebIDL::WrongDocumentError::create(realm(), "Given node is not in the same document as the range."_string);
// 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException. // 2. If node is a doctype, then throw an "InvalidNodeTypeError" DOMException.
if (is<DocumentType>(node)) if (is<DocumentType>(*node))
return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType."_string); return WebIDL::InvalidNodeTypeError::create(realm(), "Node cannot be a DocumentType."_string);
// 3. If offset is greater than nodes length, then throw an "IndexSizeError" DOMException. // 3. If offset is greater than nodes length, then throw an "IndexSizeError" DOMException.
if (offset > node.length()) if (offset > node->length())
return WebIDL::IndexSizeError::create(realm(), MUST(String::formatted("Node does not contain a child at offset {}", offset))); return WebIDL::IndexSizeError::create(realm(), MUST(String::formatted("Node does not contain a child at offset {}", offset)));
// 4. If (node, offset) is before start, return 1. // 4. If (node, offset) is before start, return 1.
@ -581,7 +576,7 @@ String Range::to_string() const
} }
// 4. Append the concatenation of the data of all Text nodes that are contained in this, in tree order, to s. // 4. Append the concatenation of the data of all Text nodes that are contained in this, in tree order, to s.
for (Node const* node = start_container(); node != end_container()->next_sibling(); node = node->next_in_pre_order()) { for (GC::Ptr<Node> node = start_container(); node != end_container()->next_sibling(); node = node->next_in_pre_order()) {
if (is<Text>(*node) && contains_node(*node)) if (is<Text>(*node) && contains_node(*node))
builder.append(static_cast<Text const&>(*node).data()); builder.append(static_cast<Text const&>(*node).data());
} }
@ -790,10 +785,10 @@ WebIDL::ExceptionOr<GC::Ref<DocumentFragment>> Range::extract()
} }
// https://dom.spec.whatwg.org/#contained // https://dom.spec.whatwg.org/#contained
bool Range::contains_node(Node const& node) const bool Range::contains_node(GC::Ref<Node> node) const
{ {
// A node node is contained in a live range range if nodes root is ranges root, // A node node is contained in a live range range if nodes root is ranges root,
if (&node.root() != &root()) if (&node->root() != root().ptr())
return false; return false;
// and (node, 0) is after ranges start, // and (node, 0) is after ranges start,
@ -801,19 +796,19 @@ bool Range::contains_node(Node const& node) const
return false; return false;
// and (node, nodes length) is before ranges end. // and (node, nodes length) is before ranges end.
if (position_of_boundary_point_relative_to_other_boundary_point(node, node.length(), m_end_container, m_end_offset) != RelativeBoundaryPointPosition::Before) if (position_of_boundary_point_relative_to_other_boundary_point(node, node->length(), m_end_container, m_end_offset) != RelativeBoundaryPointPosition::Before)
return false; return false;
return true; return true;
} }
// https://dom.spec.whatwg.org/#partially-contained // https://dom.spec.whatwg.org/#partially-contained
bool Range::partially_contains_node(Node const& node) const bool Range::partially_contains_node(GC::Ref<Node> node) const
{ {
// A node is partially contained in a live range if its an inclusive ancestor of the live ranges start node but not its end node, or vice versa. // A node is partially contained in a live range if its an inclusive ancestor of the live ranges start node but not its end node, or vice versa.
if (node.is_inclusive_ancestor_of(m_start_container) && &node != m_end_container.ptr()) if (node->is_inclusive_ancestor_of(m_start_container) && node != m_end_container)
return true; return true;
if (node.is_inclusive_ancestor_of(m_end_container) && &node != m_start_container.ptr()) if (node->is_inclusive_ancestor_of(m_end_container) && node != m_start_container)
return true; return true;
return false; return false;
} }
@ -1109,9 +1104,9 @@ WebIDL::ExceptionOr<void> Range::delete_contents()
// 4. Let nodes to remove be a list of all the nodes that are contained in this, in tree order, omitting any node whose parent is also contained in this. // 4. Let nodes to remove be a list of all the nodes that are contained in this, in tree order, omitting any node whose parent is also contained in this.
GC::MarkedVector<Node*> nodes_to_remove(heap()); GC::MarkedVector<Node*> nodes_to_remove(heap());
for (Node const* node = start_container(); node != end_container()->next_sibling(); node = node->next_in_pre_order()) { for (GC::Ptr<Node> node = start_container(); node != end_container()->next_sibling(); node = node->next_in_pre_order()) {
if (contains_node(*node) && (!node->parent_node() || !contains_node(*node->parent_node()))) if (contains_node(*node) && (!node->parent_node() || !contains_node(*node->parent_node())))
nodes_to_remove.append(const_cast<Node*>(node)); nodes_to_remove.append(node);
} }
GC::Ptr<Node> new_node; GC::Ptr<Node> new_node;
@ -1168,17 +1163,17 @@ GC::Ref<Geometry::DOMRectList> Range::get_client_rects()
// FIXME: take Range collapsed into consideration // FIXME: take Range collapsed into consideration
// 2. Iterate the node included in Range // 2. Iterate the node included in Range
auto start_node = start_container(); auto start_node = start_container();
if (!is<DOM::Text>(*start_node))
start_node = *start_node->child_at_index(m_start_offset);
auto end_node = end_container(); auto end_node = end_container();
if (!is<DOM::Text>(start_node)) { if (!is<DOM::Text>(*end_node)) {
start_node = start_node->child_at_index(m_start_offset);
}
if (!is<DOM::Text>(end_node)) {
// end offset shouldn't be 0 // end offset shouldn't be 0
if (m_end_offset == 0) if (m_end_offset == 0)
return Geometry::DOMRectList::create(realm(), {}); return Geometry::DOMRectList::create(realm(), {});
end_node = end_node->child_at_index(m_end_offset - 1); end_node = *end_node->child_at_index(m_end_offset - 1);
} }
for (Node const* node = start_node; node && node != end_node->next_in_pre_order(); node = node->next_in_pre_order()) { for (GC::Ptr<Node> node = start_node; node && node != end_node->next_in_pre_order(); node = node->next_in_pre_order()) {
auto node_type = static_cast<NodeType>(node->node_type()); auto node_type = static_cast<NodeType>(node->node_type());
if (node_type == NodeType::ELEMENT_NODE) { if (node_type == NodeType::ELEMENT_NODE) {
// 1. For each element selected by the range, whose parent is not selected by the range, include the border // 1. For each element selected by the range, whose parent is not selected by the range, include the border

View file

@ -2,6 +2,7 @@
* Copyright (c) 2020, the SerenityOS developers. * Copyright (c) 2020, the SerenityOS developers.
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org> * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.org> * Copyright (c) 2022-2023, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -21,7 +22,7 @@ enum class RelativeBoundaryPointPosition {
}; };
// https://dom.spec.whatwg.org/#concept-range-bp-position // https://dom.spec.whatwg.org/#concept-range-bp-position
RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(Node const& node_a, u32 offset_a, Node const& node_b, u32 offset_b); RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_boundary_point(GC::Ref<Node> node_a, u32 offset_a, GC::Ref<Node> node_b, u32 offset_b);
class Range final : public AbstractRange { class Range final : public AbstractRange {
WEB_PLATFORM_OBJECT(Range, AbstractRange); WEB_PLATFORM_OBJECT(Range, AbstractRange);
@ -30,20 +31,20 @@ class Range final : public AbstractRange {
public: public:
[[nodiscard]] static GC::Ref<Range> create(Document&); [[nodiscard]] static GC::Ref<Range> create(Document&);
[[nodiscard]] static GC::Ref<Range> create(HTML::Window&); [[nodiscard]] static GC::Ref<Range> create(HTML::Window&);
[[nodiscard]] static GC::Ref<Range> create(Node& start_container, WebIDL::UnsignedLong start_offset, Node& end_container, WebIDL::UnsignedLong end_offset); [[nodiscard]] static GC::Ref<Range> create(GC::Ref<Node> start_container, WebIDL::UnsignedLong start_offset, GC::Ref<Node> end_container, WebIDL::UnsignedLong end_offset);
static WebIDL::ExceptionOr<GC::Ref<Range>> construct_impl(JS::Realm&); static WebIDL::ExceptionOr<GC::Ref<Range>> construct_impl(JS::Realm&);
virtual ~Range() override; virtual ~Range() override;
WebIDL::ExceptionOr<void> set_start(Node& node, WebIDL::UnsignedLong offset); WebIDL::ExceptionOr<void> set_start(GC::Ref<Node> node, WebIDL::UnsignedLong offset);
WebIDL::ExceptionOr<void> set_end(Node& node, WebIDL::UnsignedLong offset); WebIDL::ExceptionOr<void> set_end(GC::Ref<Node> node, WebIDL::UnsignedLong offset);
WebIDL::ExceptionOr<void> set_start_before(Node& node); WebIDL::ExceptionOr<void> set_start_before(GC::Ref<Node> node);
WebIDL::ExceptionOr<void> set_start_after(Node& node); WebIDL::ExceptionOr<void> set_start_after(GC::Ref<Node> node);
WebIDL::ExceptionOr<void> set_end_before(Node& node); WebIDL::ExceptionOr<void> set_end_before(GC::Ref<Node> node);
WebIDL::ExceptionOr<void> set_end_after(Node& node); WebIDL::ExceptionOr<void> set_end_after(GC::Ref<Node> node);
WebIDL::ExceptionOr<void> select_node(Node& node); WebIDL::ExceptionOr<void> select_node(GC::Ref<Node> node);
void collapse(bool to_start); void collapse(bool to_start);
WebIDL::ExceptionOr<void> select_node_contents(Node&); WebIDL::ExceptionOr<void> select_node_contents(GC::Ref<Node>);
void increase_start_offset(Badge<Node>, WebIDL::UnsignedLong); void increase_start_offset(Badge<Node>, WebIDL::UnsignedLong);
void increase_end_offset(Badge<Node>, WebIDL::UnsignedLong); void increase_end_offset(Badge<Node>, WebIDL::UnsignedLong);
@ -73,9 +74,9 @@ public:
// Note: Its functionality (disabling a Range object) was removed, but the method itself is preserved for compatibility. // Note: Its functionality (disabling a Range object) was removed, but the method itself is preserved for compatibility.
} }
bool intersects_node(Node const&) const; bool intersects_node(GC::Ref<Node>) const;
WebIDL::ExceptionOr<bool> is_point_in_range(Node const&, WebIDL::UnsignedLong offset) const; WebIDL::ExceptionOr<bool> is_point_in_range(GC::Ref<Node>, WebIDL::UnsignedLong offset) const;
WebIDL::ExceptionOr<WebIDL::Short> compare_point(Node const&, WebIDL::UnsignedLong offset) const; WebIDL::ExceptionOr<WebIDL::Short> compare_point(GC::Ref<Node>, WebIDL::UnsignedLong offset) const;
WebIDL::ExceptionOr<void> delete_contents(); WebIDL::ExceptionOr<void> delete_contents();
WebIDL::ExceptionOr<GC::Ref<DocumentFragment>> extract_contents(); WebIDL::ExceptionOr<GC::Ref<DocumentFragment>> extract_contents();
@ -91,7 +92,7 @@ public:
GC::Ref<Geometry::DOMRectList> get_client_rects(); GC::Ref<Geometry::DOMRectList> get_client_rects();
GC::Ref<Geometry::DOMRect> get_bounding_client_rect(); GC::Ref<Geometry::DOMRect> get_bounding_client_rect();
bool contains_node(Node const&) const; bool contains_node(GC::Ref<Node>) const;
void set_associated_selection(Badge<Selection::Selection>, GC::Ptr<Selection::Selection>); void set_associated_selection(Badge<Selection::Selection>, GC::Ptr<Selection::Selection>);
@ -99,13 +100,12 @@ public:
private: private:
explicit Range(Document&); explicit Range(Document&);
Range(Node& start_container, WebIDL::UnsignedLong start_offset, Node& end_container, WebIDL::UnsignedLong end_offset); Range(GC::Ref<Node> start_container, WebIDL::UnsignedLong start_offset, GC::Ref<Node> end_container, WebIDL::UnsignedLong end_offset);
virtual void initialize(JS::Realm&) override; virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override; virtual void visit_edges(Cell::Visitor&) override;
Node& root(); GC::Ref<Node> root() const;
Node const& root() const;
void update_associated_selection(); void update_associated_selection();
@ -114,14 +114,14 @@ private:
End, End,
}; };
WebIDL::ExceptionOr<void> set_start_or_end(Node& node, u32 offset, StartOrEnd start_or_end); WebIDL::ExceptionOr<void> set_start_or_end(GC::Ref<Node> node, u32 offset, StartOrEnd start_or_end);
WebIDL::ExceptionOr<void> select(Node& node); WebIDL::ExceptionOr<void> select(GC::Ref<Node> node);
WebIDL::ExceptionOr<GC::Ref<DocumentFragment>> extract(); WebIDL::ExceptionOr<GC::Ref<DocumentFragment>> extract();
WebIDL::ExceptionOr<GC::Ref<DocumentFragment>> clone_the_contents(); WebIDL::ExceptionOr<GC::Ref<DocumentFragment>> clone_the_contents();
WebIDL::ExceptionOr<void> insert(GC::Ref<Node>); WebIDL::ExceptionOr<void> insert(GC::Ref<Node>);
bool partially_contains_node(Node const&) const; bool partially_contains_node(GC::Ref<Node>) const;
GC::Ptr<Selection::Selection> m_associated_selection; GC::Ptr<Selection::Selection> m_associated_selection;
}; };

View file

@ -2173,7 +2173,7 @@ static String visible_text_in_range(DOM::Range const& range)
if (is<DOM::Text>(*range.start_container()) && range.start_container()->layout_node()) if (is<DOM::Text>(*range.start_container()) && range.start_container()->layout_node())
builder.append(static_cast<DOM::Text const&>(*range.start_container()).data().bytes_as_string_view().substring_view(range.start_offset())); builder.append(static_cast<DOM::Text const&>(*range.start_container()).data().bytes_as_string_view().substring_view(range.start_offset()));
for (DOM::Node const* node = range.start_container(); node != range.end_container()->next_sibling(); node = node->next_in_pre_order()) { for (GC::Ptr<DOM::Node> node = range.start_container(); node != range.end_container()->next_sibling(); node = node->next_in_pre_order()) {
if (is<DOM::Text>(*node) && range.contains_node(*node) && node->layout_node()) if (is<DOM::Text>(*node) && range.contains_node(*node) && node->layout_node())
builder.append(static_cast<DOM::Text const&>(*node).data()); builder.append(static_cast<DOM::Text const&>(*node).data());
} }

View file

@ -286,8 +286,8 @@ void ViewportPaintable::recompute_selection_states(DOM::Range& range)
return TraversalDecision::Continue; return TraversalDecision::Continue;
}); });
auto* start_container = range.start_container(); auto start_container = range.start_container();
auto* end_container = range.end_container(); auto end_container = range.end_container();
// 2. If the selection starts and ends in the same node: // 2. If the selection starts and ends in the same node:
if (start_container == end_container) { if (start_container == end_container) {