Skip to content

Truly independent web browser

LibWeb: Stop a highlight at the end of an ancestor of its start

Andreas Klinge7d273458a92
diff --git a/Libraries/LibWeb/Painting/DocumentPaintState.cpp b/Libraries/LibWeb/Painting/DocumentPaintState.cpp
index 02d1ce7dd3c..ef3f7e8c08b 100644
--- a/Libraries/LibWeb/Painting/DocumentPaintState.cpp
+++ b/Libraries/LibWeb/Painting/DocumentPaintState.cpp
@@ -209,9 +209,9 @@ 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, nothing of what it holds is in the range, and we need to start
- // on the node right after it.
+ // on the node right after it. If the end container holds nothing after it, there is no node to mark.
if (!start_at) {
- for (auto* node = start_container.ptr(); node && !start_at; node = node->parent())
+ for (auto* node = start_container.ptr(); node && node != end_container.ptr() && !start_at; node = node->parent())
start_at = node->next_sibling();
}
diff --git a/Tests/LibWeb/Ref/expected/selection-ending-at-the-end-of-an-ancestor-of-its-start-ref.html b/Tests/LibWeb/Ref/expected/selection-ending-at-the-end-of-an-ancestor-of-its-start-ref.html
new file mode 100644
index 00000000000..08c3c592a68
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/selection-ending-at-the-end-of-an-ancestor-of-its-start-ref.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<style>
+ ::selection { color: black; background: lime; }
+</style>
+<p id="selected">selected text</p>
+<p>outside of the selection</p>
+<script>
+ getSelection().setBaseAndExtent(selected.firstChild, 3, selected.firstChild, 13);
+</script>
diff --git a/Tests/LibWeb/Ref/input/selection-ending-at-the-end-of-an-ancestor-of-its-start.html b/Tests/LibWeb/Ref/input/selection-ending-at-the-end-of-an-ancestor-of-its-start.html
new file mode 100644
index 00000000000..d2f2614915c
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/selection-ending-at-the-end-of-an-ancestor-of-its-start.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/selection-ending-at-the-end-of-an-ancestor-of-its-start-ref.html">
+<style>
+ ::selection { color: black; background: lime; }
+</style>
+<p id="selected">selected text</p>
+<p>outside of the selection</p>
+<script>
+ getSelection().setBaseAndExtent(selected.firstChild, 3, selected, 1);
+</script>