Skip to content

Truly independent web browser

LibWeb: Leave a node out of a selection that starts past its end

Andreas Klinge5213a82950c
diff --git a/Libraries/LibWeb/Painting/DocumentPaintState.cpp b/Libraries/LibWeb/Painting/DocumentPaintState.cpp
index e5bb0343493..02d1ce7dd3c 100644
--- a/Libraries/LibWeb/Painting/DocumentPaintState.cpp
+++ b/Libraries/LibWeb/Painting/DocumentPaintState.cpp
@@ -208,13 +208,11 @@ static void for_each_node_in_highlight_range(Layout::BegunRead const& read, DOM:
// 4. Mark the nodes between the start and end of the selection as Full.
auto* start_at = start_container->child_at_index(range.start_offset());
- // If the start container has no child at that index, we need to start on the node right after the start container.
+ // If the start container has no child at that index, nothing of what it holds is in the range, and we need to start
+ // on the node right after it.
if (!start_at) {
- if (auto* last_child = start_container->last_child()) {
- start_at = last_child->next_in_pre_order();
- } else {
- start_at = start_container->next_in_pre_order();
- }
+ for (auto* node = start_container.ptr(); node && !start_at; node = node->parent())
+ start_at = node->next_sibling();
}
DOM::Node* stop_at = end_container->child_at_index(range.end_offset());
diff --git a/Tests/LibWeb/Ref/expected/selection-starting-past-the-end-of-a-node-ref.html b/Tests/LibWeb/Ref/expected/selection-starting-past-the-end-of-a-node-ref.html
new file mode 100644
index 00000000000..af84b4193ca
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/selection-starting-past-the-end-of-a-node-ref.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<style>
+ ::selection { color: black; background: lime; }
+ span { display: inline-block; }
+</style>
+<p id="start"><span>outside of the selection</span></p>
+<p id="end">selected text</p>
+<script>
+ getSelection().setBaseAndExtent(end.firstChild, 0, end.firstChild, 8);
+</script>
diff --git a/Tests/LibWeb/Ref/input/selection-starting-past-the-end-of-a-node.html b/Tests/LibWeb/Ref/input/selection-starting-past-the-end-of-a-node.html
new file mode 100644
index 00000000000..ff4004eae03
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/selection-starting-past-the-end-of-a-node.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/selection-starting-past-the-end-of-a-node-ref.html">
+<style>
+ ::selection { color: black; background: lime; }
+ span { display: inline-block; }
+</style>
+<p id="start"><span>outside of the selection</span></p>
+<p id="end">selected text</p>
+<script>
+ getSelection().setBaseAndExtent(start, 1, end.firstChild, 8);
+</script>