Skip to content

Truly independent web browser

LibWeb: Clear selection states without walking the paint tree

Aliaksandr Kaleniked3f445984ab
diff --git a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
index f3458068c43..ed819c7845d 100644
--- a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
+++ b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
@@ -748,15 +748,14 @@ mod tests {
use crate::painting::paintable_data::{FfiSelectionEntry, SELECTION_STATE_START_AND_END};
let mut arena = LayoutNodeArena::new();
- let viewport = arena.allocate_for_test().slot;
- arena.populate_paintable_row(viewport);
let (first, remainder) = first_letter_slices(&mut arena, 1, 4);
let entries = [FfiSelectionEntry {
is_text_node_entry: true,
layout_node: remainder,
state: SELECTION_STATE_START_AND_END,
}];
- let states = crate::painting::selection::apply(&mut arena.paintable_rows_mut(), viewport, &entries);
+ let selection = crate::painting::selection::apply(&mut arena.paintable_rows_mut(), &entries, 0, 4);
+ let states = &selection.text_states;
assert_eq!(states.len(), 2);
assert_eq!(states.get(&first), Some(&SELECTION_STATE_START_AND_END));
assert_eq!(states.get(&remainder), Some(&SELECTION_STATE_START_AND_END));
diff --git a/Libraries/LibWeb/Rust/src/painting/paint_changes.rs b/Libraries/LibWeb/Rust/src/painting/paint_changes.rs
index 7eef70d63c8..1e75b86bcc7 100644
--- a/Libraries/LibWeb/Rust/src/painting/paint_changes.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paint_changes.rs
@@ -144,16 +144,13 @@ impl PaintChange {
if !arena.paintable_row_is_populated(viewport) {
return;
}
- let text_states = super::selection::apply(&mut arena.paintable_rows_mut(), viewport, &entries);
- arena.paint_state().borrow_mut().selection = Some(Arc::new(super::selection::SelectionRange {
- start_offset,
- end_offset,
- text_states,
- }));
+ let selection =
+ super::selection::apply(&mut arena.paintable_rows_mut(), &entries, start_offset, end_offset);
+ arena.paint_state().borrow_mut().selection = Some(Arc::new(selection));
}
Self::ClearSelection { viewport } => {
if arena.paintable_row_is_populated(viewport) {
- super::selection::clear(&mut arena.paintable_rows_mut(), viewport);
+ super::selection::clear(&mut arena.paintable_rows_mut());
}
}
Self::ApplySearchText {
diff --git a/Libraries/LibWeb/Rust/src/painting/selection.rs b/Libraries/LibWeb/Rust/src/painting/selection.rs
index 24b583e5487..8a05cf9ad6b 100644
--- a/Libraries/LibWeb/Rust/src/painting/selection.rs
+++ b/Libraries/LibWeb/Rust/src/painting/selection.rs
@@ -65,6 +65,7 @@ pub(crate) struct SelectionRange {
pub start_offset: usize,
pub end_offset: usize,
pub text_states: std::collections::HashMap<NodeSlotId, u8>,
+ pub boxes_with_selection_state: Vec<NodeSlotId>,
}
fn invalidate_text_node(layout_arena: &PaintableRowsMut<'_>, node: NodeSlotId) {
@@ -76,32 +77,32 @@ fn invalidate_text_node(layout_arena: &PaintableRowsMut<'_>, node: NodeSlotId) {
}
}
-pub(crate) fn clear(layout_arena: &mut PaintableRowsMut<'_>, viewport: NodeSlotId) {
- let previous = layout_arena.paint_state().borrow_mut().selection.take();
- if let Some(previous) = previous {
- for node in previous.text_states.keys() {
- invalidate_text_node(layout_arena, *node);
- }
+pub(crate) fn clear(layout_arena: &mut PaintableRowsMut<'_>) {
+ let Some(previous) = layout_arena.paint_state().borrow_mut().selection.take() else {
+ return;
+ };
+ for node in previous.text_states.keys() {
+ invalidate_text_node(layout_arena, *node);
}
- let mut slots = Vec::new();
- crate::painting::paint_order::for_each_in_paint_subtree(layout_arena, viewport, |slot| {
- slots.push(slot);
- });
- for current in slots {
- if layout_arena.paintable_data(current).selection_state != SELECTION_STATE_NONE {
- layout_arena.paintable_data_mut(current).selection_state = SELECTION_STATE_NONE;
- layout_arena.push_paint_damage(current, PaintDamage::ALL_DRAW);
+ for &row in &previous.boxes_with_selection_state {
+ if layout_arena.paintable_row_is_populated(row)
+ && layout_arena.paintable_data(row).selection_state != SELECTION_STATE_NONE
+ {
+ layout_arena.paintable_data_mut(row).selection_state = SELECTION_STATE_NONE;
+ layout_arena.push_paint_damage(row, PaintDamage::ALL_DRAW);
}
}
}
pub(crate) fn apply(
layout_arena: &mut PaintableRowsMut<'_>,
- viewport: NodeSlotId,
entries: &[FfiSelectionEntry],
-) -> std::collections::HashMap<NodeSlotId, u8> {
- clear(layout_arena, viewport);
+ start_offset: usize,
+ end_offset: usize,
+) -> SelectionRange {
+ clear(layout_arena);
let mut text_states = std::collections::HashMap::new();
+ let mut boxes_with_selection_state = Vec::new();
for entry in entries {
if entry.is_text_node_entry {
for &node in layout_arena.text_fragments(entry.layout_node).as_slice() {
@@ -115,10 +116,16 @@ pub(crate) fn apply(
if layout_arena.paintable_data(entry.layout_node).selection_state != entry.state {
layout_arena.paintable_data_mut(entry.layout_node).selection_state = entry.state;
layout_arena.push_paint_damage(entry.layout_node, PaintDamage::ALL_DRAW);
+ boxes_with_selection_state.push(entry.layout_node);
}
}
}
- text_states
+ SelectionRange {
+ start_offset,
+ end_offset,
+ text_states,
+ boxes_with_selection_state,
+ }
}
#[derive(Debug)]
diff --git a/Tests/LibWeb/Ref/expected/selection-with-image-moves-away-after-paint-ref.html b/Tests/LibWeb/Ref/expected/selection-with-image-moves-away-after-paint-ref.html
new file mode 100644
index 00000000000..0fc15c5bd7a
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/selection-with-image-moves-away-after-paint-ref.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<style>
+ img {
+ width: 50px;
+ height: 50px;
+ vertical-align: middle;
+ }
+</style>
+<p>before image <img src="../data/smiley.png"> after image</p>
+<p id="other">The selection ends up here</p>
+<script>
+ getSelection().selectAllChildren(document.getElementById("other"));
+</script>
diff --git a/Tests/LibWeb/Ref/input/selection-with-image-moves-away-after-paint.html b/Tests/LibWeb/Ref/input/selection-with-image-moves-away-after-paint.html
new file mode 100644
index 00000000000..1d2c6c6ece3
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/selection-with-image-moves-away-after-paint.html
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/selection-with-image-moves-away-after-paint-ref.html">
+<style>
+ img {
+ width: 50px;
+ height: 50px;
+ vertical-align: middle;
+ }
+</style>
+<p>before image <img src="../data/smiley.png"> after image</p>
+<p id="other">The selection ends up here</p>
+<script>
+ function afterNextFrame(callback) {
+ requestAnimationFrame(() => requestAnimationFrame(callback));
+ }
+
+ window.addEventListener("load", () => {
+ const selection = getSelection();
+ // The image paints selected first, then the selection is removed and later placed where it does not cover it.
+ selection.selectAllChildren(document.body);
+ afterNextFrame(() => {
+ selection.removeAllRanges();
+ afterNextFrame(() => {
+ selection.selectAllChildren(document.getElementById("other"));
+ afterNextFrame(() => {
+ if (window.internals)
+ internals.signalTestIsDone("PASS");
+ else
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+ });
+ });
+</script>
+</html>