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

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -10,7 +11,6 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/MutationType.h>
#include <LibWeb/DOM/Range.h>
#include <LibWeb/DOM/StaticNodeList.h>
#include <LibWeb/Layout/TextNode.h>
namespace Web::DOM {
@ -105,31 +105,32 @@ WebIDL::ExceptionOr<void> CharacterData::replace_data(size_t offset, size_t coun
// NOTE: We do this later so that the mutation observer may notify UI clients of this node's new value.
queue_mutation_record(MutationType::characterData, {}, {}, old_data, {}, {}, nullptr, nullptr);
// 8. For each live range whose start node is node and start offset is greater than offset but less than or equal to offset plus count, set its start offset to offset.
for (auto& range : Range::live_ranges()) {
// 8. For each live range whose start node is node and start offset is greater than offset but less than or equal to
// offset plus count, set its start offset to offset.
for (auto* range : Range::live_ranges()) {
if (range->start_container() == this && range->start_offset() > offset && range->start_offset() <= (offset + count))
TRY(range->set_start(*range->start_container(), offset));
range->set_start_offset(offset);
}
// 9. For each live range whose end node is node and end offset is greater than offset but less than or equal to offset plus count, set its end offset to offset.
for (auto& range : Range::live_ranges()) {
// 9. For each live range whose end node is node and end offset is greater than offset but less than or equal to
// offset plus count, set its end offset to offset.
for (auto* range : Range::live_ranges()) {
if (range->end_container() == this && range->end_offset() > offset && range->end_offset() <= (offset + count))
TRY(range->set_end(*range->end_container(), offset));
range->set_end_offset(offset);
}
// 10. For each live range whose start node is node and start offset is greater than offset plus count, increase its start offset by datas length and decrease it by count.
for (auto& range : Range::live_ranges()) {
// 10. For each live range whose start node is node and start offset is greater than offset plus count, increase its
// start offset by datas length and decrease it by count.
for (auto* range : Range::live_ranges()) {
if (range->start_container() == this && range->start_offset() > (offset + count))
TRY(range->set_start(*range->start_container(), range->start_offset() + inserted_data_result.data.size() - count));
range->set_start_offset(range->start_offset() + inserted_data_result.data.size() - count);
}
// 11. For each live range whose end node is node and end offset is greater than offset plus count, increase its end offset by datas length and decrease it by count.
for (auto& range : Range::live_ranges()) {
if (range->end_container() == this && range->end_offset() > (offset + count)) {
// AD-HOC: Clamp offset to the end of the data if it's too large.
auto new_offset = min(range->end_offset() + inserted_data_result.data.size() - count, length_in_utf16_code_units());
TRY(range->set_end(*range->end_container(), new_offset));
}
// 11. For each live range whose end node is node and end offset is greater than offset plus count, increase its end
// offset by datas length and decrease it by count.
for (auto* range : Range::live_ranges()) {
if (range->end_container() == this && range->end_offset() > (offset + count))
range->set_end_offset(range->end_offset() + inserted_data_result.data.size() - count);
}
// 12. If nodes parent is non-null, then run the children changed steps for nodes parent.

View file

@ -2,7 +2,7 @@
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
* Copyright (c) 2024-2025, Jelle Raaijmakers <jelle@ladybird.org>
* Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -299,28 +299,40 @@ WebIDL::ExceptionOr<void> Node::normalize()
// 6. While currentNode is an exclusive Text node:
while (current_node && current_node->is_exclusive_text()) {
// 1. For each live range whose start node is currentNode, add length to its start offset and set its start node to node.
// 1. For each live range whose start node is currentNode, add length to its start offset and set its start
// node to node.
for (auto& range : Range::live_ranges()) {
if (range->start_container() == current_node)
TRY(range->set_start(node, range->start_offset() + length));
if (range->start_container() == current_node) {
range->increase_start_offset(length);
range->set_start_node(node);
}
}
// 2. For each live range whose end node is currentNode, add length to its end offset and set its end node to node.
// 2. For each live range whose end node is currentNode, add length to its end offset and set its end node
// to node.
for (auto& range : Range::live_ranges()) {
if (range->end_container() == current_node)
TRY(range->set_end(node, range->end_offset() + length));
if (range->end_container() == current_node) {
range->increase_end_offset(length);
range->set_end_node(node);
}
}
// 3. For each live range whose start node is currentNodes parent and start offset is currentNodes index, set its start node to node and its start offset to length.
// 3. For each live range whose start node is currentNodes parent and start offset is currentNodes index,
// set its start node to node and its start offset to length.
for (auto& range : Range::live_ranges()) {
if (range->start_container() == current_node->parent() && range->start_offset() == current_node->index())
TRY(range->set_start(node, length));
if (range->start_container() == current_node->parent() && range->start_offset() == current_node->index()) {
range->set_start_node(node);
range->set_start_offset(length);
}
}
// 4. For each live range whose end node is currentNodes parent and end offset is currentNodes index, set its end node to node and its end offset to length.
// 4. For each live range whose end node is currentNodes parent and end offset is currentNodes index, set
// its end node to node and its end offset to length.
for (auto& range : Range::live_ranges()) {
if (range->end_container() == current_node->parent() && range->end_offset() == current_node->index())
TRY(range->set_end(node, length));
if (range->end_container() == current_node->parent() && range->end_offset() == current_node->index()) {
range->set_end_node(node);
range->set_end_offset(length);
}
}
// 5. Add currentNodes length to length.
@ -681,16 +693,18 @@ void Node::insert_before(GC::Ref<Node> node, GC::Ptr<Node> child, bool suppress_
// 5. If child is non-null, then:
if (child) {
// 1. For each live range whose start node is parent and start offset is greater than childs index, increase its start offset by count.
// 1. For each live range whose start node is parent and start offset is greater than childs index, increase
// its start offset by count.
for (auto& range : Range::live_ranges()) {
if (range->start_container() == this && range->start_offset() > child->index())
range->increase_start_offset({}, count);
range->increase_start_offset(count);
}
// 2. For each live range whose end node is parent and end offset is greater than childs index, increase its end offset by count.
// 2. For each live range whose end node is parent and end offset is greater than childs index, increase its
// end offset by count.
for (auto& range : Range::live_ranges()) {
if (range->end_container() == this && range->end_offset() > child->index())
range->increase_end_offset({}, count);
range->increase_end_offset(count);
}
}
@ -866,27 +880,28 @@ void Node::live_range_pre_remove()
auto index = this->index();
// 4. For each live range whose start node is an inclusive descendant of node, set its start to (parent, index).
for (auto& range : Range::live_ranges()) {
for (auto* range : Range::live_ranges()) {
if (range->start_container()->is_inclusive_descendant_of(*this))
MUST(range->set_start(*parent, index));
}
// 5. For each live range whose end node is an inclusive descendant of node, set its end to (parent, index).
for (auto& range : Range::live_ranges()) {
for (auto* range : Range::live_ranges()) {
if (range->end_container()->is_inclusive_descendant_of(*this))
MUST(range->set_end(*parent, index));
}
// 6. For each live range whose start node is parent and start offset is greater than index, decrease its start offset by 1.
for (auto& range : Range::live_ranges()) {
// 6. For each live range whose start node is parent and start offset is greater than index, decrease its start
// offset by 1.
for (auto* range : Range::live_ranges()) {
if (range->start_container() == parent && range->start_offset() > index)
range->decrease_start_offset({}, 1);
range->decrease_start_offset(1);
}
// 7. For each live range whose end node is parent and end offset is greater than index, decrease its end offset by 1.
for (auto& range : Range::live_ranges()) {
for (auto* range : Range::live_ranges()) {
if (range->end_container() == parent && range->end_offset() > index)
range->decrease_end_offset({}, 1);
range->decrease_end_offset(1);
}
}
@ -1259,16 +1274,18 @@ WebIDL::ExceptionOr<void> Node::move_node(Node& new_parent, Node* child)
// 17. If child is non-null:
if (child) {
// 1. For each live range whose start node is newParent and start offset is greater than childs index, increase its start offset by 1.
// 1. For each live range whose start node is newParent and start offset is greater than childs index, increase
// its start offset by 1.
for (auto& range : Range::live_ranges()) {
if (range->start_container() == &new_parent && range->start_offset() > child->index())
range->increase_start_offset({}, 1);
range->increase_start_offset(1);
}
// 2. For each live range whose end node is newParent and end offset is greater than childs index, increase its end offset by 1.
// 2. For each live range whose end node is newParent and end offset is greater than childs index, increase its
// end offset by 1.
for (auto& range : Range::live_ranges()) {
if (range->end_container() == &new_parent && range->end_offset() > child->index())
range->increase_end_offset({}, 1);
range->increase_end_offset(1);
}
}

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;
}
}

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);

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -75,30 +76,37 @@ WebIDL::ExceptionOr<GC::Ref<Text>> Text::split_text(size_t offset)
// 1. Insert new node into parent before nodes next sibling.
parent->insert_before(*new_node, next_sibling());
// 2. For each live range whose start node is node and start offset is greater than offset, set its start node to new node and decrease its start offset by offset.
for (auto& range : Range::live_ranges()) {
if (range->start_container() == this && range->start_offset() > offset)
TRY(range->set_start(*new_node, range->start_offset() - offset));
}
// 3. For each live range whose end node is node and end offset is greater than offset, set its end node to new node and decrease its end offset by offset.
for (auto& range : Range::live_ranges()) {
if (range->end_container() == this && range->end_offset() > offset)
TRY(range->set_end(*new_node, range->end_offset() - offset));
}
// 4. For each live range whose start node is parent and start offset is equal to the index of node plus 1, increase its start offset by 1.
for (auto& range : Range::live_ranges()) {
if (range->start_container() == parent.ptr() && range->start_offset() == index() + 1)
TRY(range->set_start(*range->start_container(), range->start_offset() + 1));
}
// 5. For each live range whose end node is parent and end offset is equal to the index of node plus 1, increase its end offset by 1.
for (auto& range : Range::live_ranges()) {
if (range->end_container() == parent.ptr() && range->end_offset() == index() + 1) {
TRY(range->set_end(*range->end_container(), range->end_offset() + 1));
// 2. For each live range whose start node is node and start offset is greater than offset, set its start node
// to new node and decrease its start offset by offset.
for (auto* range : Range::live_ranges()) {
if (range->start_container() == this && range->start_offset() > offset) {
range->set_start_node(*new_node);
range->decrease_start_offset(offset);
}
}
// 3. For each live range whose end node is node and end offset is greater than offset, set its end node to new
// node and decrease its end offset by offset.
for (auto* range : Range::live_ranges()) {
if (range->end_container() == this && range->end_offset() > offset) {
range->set_end_node(*new_node);
range->decrease_end_offset(offset);
}
}
// 4. For each live range whose start node is parent and start offset is equal to the index of node plus 1,
// increase its start offset by 1.
for (auto* range : Range::live_ranges()) {
if (range->start_container() == parent.ptr() && range->start_offset() == index() + 1)
range->increase_start_offset(1);
}
// 5. For each live range whose end node is parent and end offset is equal to the index of node plus 1, increase
// its end offset by 1.
for (auto* range : Range::live_ranges()) {
if (range->end_container() == parent.ptr() && range->end_offset() == index() + 1)
range->increase_end_offset(1);
}
}
// 8. Replace data with node node, offset offset, count count, and data the empty string.