LibWeb: Modify range start & end directly where applicable

We were calling into `Range::set_start_or_end()` indirectly through
`::set_start()` and `::set_end()`, but that algorithm only calls for an
invocation whenever the start or end of a range needs to be set to a
boundary point. If an algorithm step calls for setting the node or
offset, we should directly modify the range.

The problem with calling into `::set_start_or_end()` is that this
algorithm potentially modifies _both_ the start and end of the range,
but algorithms trying to update a range's start or end often have
explicit steps to take both the start and end into account and end up
overcompensating for the start or end offset resulting in an invalid
range (e.g. with an end offset beyond a node's length).

This makes updating a range's start/end a bit more efficient and removes
a piece of ad-hoc code in CharacterData needed to make it work before.
This commit is contained in:
Jelle Raaijmakers 2025-05-14 12:56:03 +02:00 committed by Tim Ledbetter
commit 0d83426a49
Notes: github-actions[bot] 2025-05-15 10:45:47 +00:00
5 changed files with 112 additions and 95 deletions

View file

@ -39,7 +39,7 @@ HashTable<Range*>& Range::live_ranges()
GC::Ref<Range> Range::create(HTML::Window& window)
{
return Range::create(window.associated_document());
return create(window.associated_document());
}
GC::Ref<Range> Range::create(Document& document)
@ -57,7 +57,7 @@ GC::Ref<Range> Range::create(GC::Ref<Node> start_container, WebIDL::UnsignedLong
WebIDL::ExceptionOr<GC::Ref<Range>> Range::construct_impl(JS::Realm& realm)
{
auto& window = as<HTML::Window>(realm.global_object());
return Range::create(window);
return create(window);
}
Range::Range(Document& document)
@ -167,6 +167,7 @@ RelativeBoundaryPointPosition position_of_boundary_point_relative_to_other_bound
return RelativeBoundaryPointPosition::Before;
}
// https://dom.spec.whatwg.org/#concept-range-bp-set
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:
@ -212,13 +213,14 @@ WebIDL::ExceptionOr<void> Range::set_start_or_end(GC::Ref<Node> node, u32 offset
return {};
}
// https://dom.spec.whatwg.org/#concept-range-bp-set
// https://dom.spec.whatwg.org/#dom-range-setstart
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).
return set_start_or_end(node, offset, StartOrEnd::Start);
}
// https://dom.spec.whatwg.org/#dom-range-setend
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).
@ -1291,24 +1293,4 @@ WebIDL::ExceptionOr<GC::Ref<DocumentFragment>> Range::create_contextual_fragment
return fragment_node;
}
void Range::increase_start_offset(Badge<Node>, WebIDL::UnsignedLong count)
{
m_start_offset += count;
}
void Range::increase_end_offset(Badge<Node>, WebIDL::UnsignedLong count)
{
m_end_offset += count;
}
void Range::decrease_start_offset(Badge<Node>, WebIDL::UnsignedLong count)
{
m_start_offset -= count;
}
void Range::decrease_end_offset(Badge<Node>, WebIDL::UnsignedLong count)
{
m_end_offset -= count;
}
}