Skip to content

Truly independent web browser

LibWeb: Scroll the caret on an empty line into view

Andreas Kling02a049ee0677
diff --git a/Libraries/LibWeb/Rust/src/layout/script_entries.rs b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
index 78f3e49a0b2..c15b56c3abd 100644
--- a/Libraries/LibWeb/Rust/src/layout/script_entries.rs
+++ b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
@@ -302,26 +302,13 @@ pub extern "C" fn layout_script_paintable_empty_line_caret_rect(
style_source: NodeSlotId::INVALID,
};
*out = read_rows(host, |arena| {
- let paintable_rows = arena;
- if !paintable_rows.paintable_row_is_populated(block) {
- return result;
- }
let fragments = arena.text_fragments(primary);
- let node_slots = fragments.as_slice();
- let side = arena.committed_side_data(block);
- let Some(first_fragment) = side.fragments().first() else {
- return result;
- };
- if !node_slots.contains(&first_fragment.layout_node) {
- return result;
- }
- for target in crate::painting::visual_lines::empty_line_caret_targets(paintable_rows, block) {
- if target.offset == offset {
- result.has_value = true;
- result.rect = target.rect.into();
- result.style_source = crate::painting::text_fragment::style_source(paintable_rows, first_fragment);
- break;
- }
+ if let Some(caret) =
+ crate::painting::caret::caret_rect_on_empty_line(arena, block, fragments.as_slice(), offset)
+ {
+ result.has_value = true;
+ result.rect = caret.rect.into();
+ result.style_source = caret.style_source;
}
result
});
diff --git a/Libraries/LibWeb/Rust/src/painting/caret.rs b/Libraries/LibWeb/Rust/src/painting/caret.rs
index 1a708a42e75..6b4d9a70776 100644
--- a/Libraries/LibWeb/Rust/src/painting/caret.rs
+++ b/Libraries/LibWeb/Rust/src/painting/caret.rs
@@ -9,6 +9,7 @@ use crate::layout::node_data::NodeSlotId;
use crate::painting::paint_read::PaintRead;
use crate::painting::paintable_data::{FragmentRecord, SELECTION_STATE_START_AND_END};
use crate::painting::text_fragment::{self, CaretMatch};
+use crate::painting::visual_lines;
pub(crate) struct CaretRectResult {
pub rect: CssPixelRect,
@@ -80,6 +81,33 @@ pub(crate) fn caret_rect_for_position(
))
}
+/// The caret on an empty line of `block`, such as the line after a trailing newline, at `offset` in the text laid out
+/// in the fragments of `node_slots`. No fragment holds such a position.
+pub(crate) fn caret_rect_on_empty_line(
+ layout_arena: &impl PaintRead,
+ block: NodeSlotId,
+ node_slots: &[NodeSlotId],
+ offset: usize,
+) -> Option<CaretRectResult> {
+ if !layout_arena.paintable_row_is_populated(block) {
+ return None;
+ }
+ let side = layout_arena.committed_side_data(block);
+ let first_fragment = side.fragments().first()?;
+ if !node_slots.contains(&first_fragment.layout_node) {
+ return None;
+ }
+ let target = visual_lines::empty_line_caret_targets(layout_arena, block)
+ .into_iter()
+ .find(|target| target.offset == offset)?;
+ Some(CaretRectResult {
+ rect: target.rect,
+ style_source: text_fragment::style_source(layout_arena, first_fragment),
+ owner: block,
+ node: first_fragment.layout_node,
+ })
+}
+
pub(crate) fn caret_rect_in_dom_range(
layout_arena: &impl PaintRead,
node_slots: &[NodeSlotId],
diff --git a/Libraries/LibWeb/Rust/src/painting/scroll_chain.rs b/Libraries/LibWeb/Rust/src/painting/scroll_chain.rs
index b006844a054..6a6a7d67709 100644
--- a/Libraries/LibWeb/Rust/src/painting/scroll_chain.rs
+++ b/Libraries/LibWeb/Rust/src/painting/scroll_chain.rs
@@ -154,7 +154,13 @@ pub(crate) fn scroll_target_for_text_position(
scroll_block_axis: bool,
) -> Option<(NodeSlotId, CssPixelPoint)> {
let fragments = arena.text_fragments(text);
- let caret = caret::caret_rect_for_position(arena, fragments.as_slice(), offset, affinity_is_downstream)?;
+ let node_slots = fragments.as_slice();
+ let caret = match caret::caret_rect_for_position(arena, node_slots, offset, affinity_is_downstream) {
+ Some(caret) => caret,
+ // No fragment holds the position: the caret sits on an empty line of the block that lays the text out.
+ None => std::iter::successors(arena.node_parent_if_live(text), |&node| arena.node_parent_if_live(node))
+ .find_map(|block| caret::caret_rect_on_empty_line(arena, block, node_slots, offset))?,
+ };
let style = arena.node_style_if_live(caret.style_source)?;
let horizontal = style.writing_mode() == writing_mode::HORIZONTAL_TB;
let one = CssPixels::from_integer(1);
diff --git a/Tests/LibWeb/Text/expected/textarea-empty-line-caret.txt b/Tests/LibWeb/Text/expected/textarea-empty-line-caret.txt
index 484c28ab4b9..8b14c0dc7dd 100644
--- a/Tests/LibWeb/Text/expected/textarea-empty-line-caret.txt
+++ b/Tests/LibWeb/Text/expected/textarea-empty-line-caret.txt
@@ -1,6 +1,6 @@
a trailing newline adds a line: false
a trailing newline adds a line with white-space: pre-line: false
typed: caret on the empty last line is visible: true
-execCommand: caret on the empty last line is visible: false
-caret moved onto an empty middle line is visible: false
+execCommand: caret on the empty last line is visible: true
+caret moved onto an empty middle line is visible: true
caret on an empty first line is painted: true