Skip to content

Truly independent web browser

LibWeb: Place a caret before a <br> on the empty line it ends

Aliaksandr Kalenik0a7c682ca9e3
diff --git a/Libraries/LibWeb/Painting/BoxViews.cpp b/Libraries/LibWeb/Painting/BoxViews.cpp
index 1340cf068e1..9b5b34781e3 100644
--- a/Libraries/LibWeb/Painting/BoxViews.cpp
+++ b/Libraries/LibWeb/Painting/BoxViews.cpp
@@ -450,6 +450,12 @@ CSSPixelRect caret_rect_for_child_offset(Layout::BegunRead const& read, Layout::
if (!child || !is<HTML::HTMLBRElement>(*child))
return rect;
+ if (auto const* break_layout_node = child->unsafe_layout_node(read)) {
+ CSSPixelPoint line_position;
+ if (Layout::RustFFI::layout_script_empty_line_ended_by_forced_break_position(block.document_host(), committed_row_slot(block), Layout::Node::slot_id(break_layout_node), &line_position))
+ return caret_rect_for_empty_line(styled_block, line_position);
+ }
+
// A caret parked before a <br> sits on the line below the content preceding the <br>. Layout produces no
// fragments for <br>, so start below the fragments of any preceding content, and add one line height for each
// empty line rendered by earlier <br>s.
diff --git a/Libraries/LibWeb/Rust/src/layout/script_entries.rs b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
index d5968583f8c..2c0cda09a70 100644
--- a/Libraries/LibWeb/Rust/src/layout/script_entries.rs
+++ b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
@@ -396,6 +396,33 @@ pub extern "C" fn layout_script_paintable_unbroken_border_box_size(
});
}
+#[unsafe(no_mangle)]
+pub extern "C" fn layout_script_empty_line_ended_by_forced_break_position(
+ host: &DocumentHost,
+ block: NodeSlotId,
+ forced_break: NodeSlotId,
+ out: &mut super::used_values::FfiCssPixelPoint,
+) -> bool {
+ let position = read_rows(host, |arena| {
+ if !arena.paintable_row_is_populated(block) {
+ return None;
+ }
+ let side = arena.committed_side_data(block);
+ let line = side
+ .lines()
+ .iter()
+ .find(|line| line.forced_break_node == forced_break && line.fragment_count == 0)?;
+ let root_position =
+ crate::painting::paintable_geometry::LineRootPositions::new(arena, block).of_fragment(line.box_fragment);
+ Some(CssPixelRect::from(line.rect).translated_by(root_position).location())
+ });
+ let Some(position) = position else {
+ return false;
+ };
+ *out = position.into();
+ true
+}
+
#[unsafe(no_mangle)]
pub extern "C" fn layout_script_paintable_box_model(
host: &DocumentHost,
diff --git a/Tests/LibWeb/Text/expected/Editing/caret-before-br-on-empty-line.txt b/Tests/LibWeb/Text/expected/Editing/caret-before-br-on-empty-line.txt
new file mode 100644
index 00000000000..00d8c32b649
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/caret-before-br-on-empty-line.txt
@@ -0,0 +1,3 @@
+editor 0: caret before <br> matches text there: true
+editor 1: caret before <br> matches text there: true
+editor 2: caret before <br> matches text there: true
diff --git a/Tests/LibWeb/Text/input/Editing/caret-before-br-on-empty-line.html b/Tests/LibWeb/Text/input/Editing/caret-before-br-on-empty-line.html
new file mode 100644
index 00000000000..090ec22deca
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/caret-before-br-on-empty-line.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .editor { width: 300px; font: 20px/40px monospace; padding: 15px 0 0 25px; }
+ .float { float: left; width: 10px; height: 100px; }
+ .tall { font-size: 40px; line-height: 80px; }
+</style>
+<div class="editor" contenteditable><br></div>
+<div class="editor" contenteditable><div class="float"></div>x<br style="clear: left"><br></div>
+<div class="editor" contenteditable>x<br><span class="tall">y</span><br><br></div>
+<script>
+ test(() => {
+ const selection = getSelection();
+ function rectAt(node, offset) {
+ selection.collapse(node, offset);
+ return internals.currentCaretRect();
+ }
+ for (const [index, editor] of [...document.querySelectorAll(".editor")].entries()) {
+ editor.focus();
+ const breaks = editor.querySelectorAll("br");
+ const lastBreak = breaks[breaks.length - 1];
+ const beforeBreak = rectAt(editor, [...editor.childNodes].indexOf(lastBreak));
+ const text = document.createTextNode("a");
+ editor.insertBefore(text, lastBreak);
+ const inText = rectAt(text, 0);
+ text.remove();
+ println(`editor ${index}: caret before <br> matches text there: ${beforeBreak.x === inText.x && beforeBreak.y === inText.y}`);
+ }
+ });
+</script>