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

@ -47,11 +47,6 @@ public:
void collapse(bool to_start);
WebIDL::ExceptionOr<void> select_node_contents(GC::Ref<Node>);
void increase_start_offset(Badge<Node>, WebIDL::UnsignedLong);
void increase_end_offset(Badge<Node>, WebIDL::UnsignedLong);
void decrease_start_offset(Badge<Node>, WebIDL::UnsignedLong);
void decrease_end_offset(Badge<Node>, WebIDL::UnsignedLong);
// https://dom.spec.whatwg.org/#dom-range-start_to_start
enum HowToCompareBoundaryPoints : WebIDL::UnsignedShort {
START_TO_START = 0,
@ -123,6 +118,10 @@ public:
}
private:
friend class CharacterData;
friend class Node;
friend class Text;
explicit Range(Document&);
Range(GC::Ref<Node> start_container, WebIDL::UnsignedLong start_offset, GC::Ref<Node> end_container, WebIDL::UnsignedLong end_offset);
@ -138,6 +137,16 @@ private:
End,
};
void set_start_node(GC::Ref<Node> node) { m_start_container = node; }
void set_start_offset(WebIDL::UnsignedLong offset) { m_start_offset = offset; }
void set_end_node(GC::Ref<Node> node) { m_end_container = node; }
void set_end_offset(WebIDL::UnsignedLong offset) { m_end_offset = offset; }
void increase_start_offset(WebIDL::UnsignedLong count) { m_start_offset += count; }
void increase_end_offset(WebIDL::UnsignedLong count) { m_end_offset += count; }
void decrease_start_offset(WebIDL::UnsignedLong count) { m_start_offset -= count; }
void decrease_end_offset(WebIDL::UnsignedLong count) { m_end_offset -= count; }
WebIDL::ExceptionOr<void> set_start_or_end(GC::Ref<Node> node, u32 offset, StartOrEnd start_or_end);
WebIDL::ExceptionOr<void> select(GC::Ref<Node> node);