Truly independent web browser
LibWeb: Give a collapsed range in text the rect of its caret
Aliaksandr Kalenikcdddba3a36f8
diff --git a/Libraries/LibWeb/DOM/Range.cpp b/Libraries/LibWeb/DOM/Range.cppindex 910196ab296..8862930fc1b 100644--- a/Libraries/LibWeb/DOM/Range.cpp+++ b/Libraries/LibWeb/DOM/Range.cpp@@ -1330,7 +1330,6 @@ Vector<Gfx::DoubleRect> Range::client_rects()Vector<Gfx::DoubleRect> rects;Optional<Layout::RustFFI::FfiViewportMapping> viewport_mapping;- // FIXME: take Range collapsed into consideration// 2. Iterate the node included in Rangeauto next_after_subtree = [](Node& node) -> GC::Ptr<Node> {for (auto* current = &node; current; current = current->parent_node()) {@@ -1370,10 +1369,7 @@ Vector<Gfx::DoubleRect> Range::client_rects()auto const is_end_container = node.ptr() == m_end_container.ptr();auto selection_state = Painting::SelectionState::Full;if (is_start_container && is_end_container) {- if (m_start_offset == m_end_offset)- selection_state = Painting::SelectionState::None;- else- selection_state = Painting::SelectionState::StartAndEnd;+ selection_state = Painting::SelectionState::StartAndEnd;} else if (is_start_container) {selection_state = Painting::SelectionState::Start;} else if (is_end_container) {@@ -1394,8 +1390,6 @@ Vector<Gfx::DoubleRect> Range::client_rects()// 2. For each Text node selected or partially selected by the range (including when the boundary-points// are identical), include scaled DOMRect object (for the part that is selected, not the whole line box).auto const& text = static_cast<DOM::Text const&>(*node);- if (selection_state == Painting::SelectionState::None)- continue;// A text node without a box renders nothing, so it has no rects.auto const* layout_node = text.layout_node(read);diff --git a/Libraries/LibWeb/Rust/src/layout/script_entries.rs b/Libraries/LibWeb/Rust/src/layout/script_entries.rsindex 61317dfa086..664e6082197 100644--- a/Libraries/LibWeb/Rust/src/layout/script_entries.rs+++ b/Libraries/LibWeb/Rust/src/layout/script_entries.rs@@ -13,7 +13,7 @@ use super::FfiCssPixelRect;use super::node_data::NodeSlotId;use super::query::inline_cursor::{self, CaretPosition, InlineCursor};use super::used_values::{FfiCssPixelPoint, FfiCssPixelSize};-use crate::css::css_pixels::CssPixelRect;+use crate::css::css_pixels::{CssPixelRect, CssPixels};use crate::css::ffi_support::FfiUtf16View;use crate::css::style::tree::StyleNodeID;use crate::layout::text_queries::FfiDomTextRange;@@ -155,6 +155,38 @@ pub unsafe extern "C" fn layout_script_text_range_rects(let paintable_rows = arena;let viewport_mapping = viewport_mapping.mapping();+ // A collapsed range has the rect of the caret it is in text, with no width.+ if selection_state == crate::painting::paintable_data::SELECTION_STATE_START_AND_END+ && range_start_offset == range_end_offset+ {+ let cursor = InlineCursor::new(arena);+ if let Some(position) = cursor.line_of(primary, range_start_offset, true)+ && let Some(index) = position.fragment+ {+ let root = position.line.root;+ let side = arena.committed_side_data(root);+ let fragment = &side.fragments()[index as usize];+ let mut rect = cursor+ .caret_rect_in_fragment(root, fragment, range_start_offset, true)+ .rect;+ if crate::painting::text_fragment::is_horizontal(fragment) {+ rect.width = CssPixels::default();+ } else {+ rect.height = CssPixels::default();+ }+ let rect = viewport_mapping.line_content_rect_to_viewport(+ arena,+ root,+ fragment.layout_node,+ rect,+ ViewportSpace::LayoutViewport,+ );+ // SAFETY: The consumer copies the plain-data rect synchronously.+ unsafe { push_rect(context, rect.into()) };+ }+ return;+ }+let fragments = arena.text_fragments(primary);let node_slots = fragments.as_slice();crate::painting::text_fragment::for_each_fragment_of_nodes(paintable_rows, node_slots, |block, _, fragment| {diff --git a/Tests/LibWeb/Text/expected/range-client-rects-collapsed.txt b/Tests/LibWeb/Text/expected/range-client-rects-collapsed.txtnew file mode 100644index 00000000000..a5f85b2364c--- /dev/null+++ b/Tests/LibWeb/Text/expected/range-client-rects-collapsed.txt@@ -0,0 +1,14 @@+wrapped 0: 10,15 0x20, bounding 10,15 0x20+wrapped 4: 10,45 0x20, bounding 10,45 0x20+wrapped 9: 110,45 0x20, bounding 110,45 0x20+wrapped 10: 10,75 0x20, bounding 10,75 0x20+wrapped 13: 70,75 0x20, bounding 70,75 0x20+pre 0: 200,15 0x20, bounding 200,15 0x20+pre 3: 260,15 0x20, bounding 260,15 0x20+pre 4: none, bounding 0,0 0x0+pre 5: 200,75 0x20, bounding 200,75 0x20+pre 10: 300,75 0x20, bounding 300,75 0x20+vertical 0: 435,10 20x0, bounding 435,10 20x0+vertical 2: 435,50 20x0, bounding 435,50 20x0+vertical 7: 405,70 20x0, bounding 405,70 20x0+scroller 1: 30,245 0x20, bounding 30,245 0x20diff --git a/Tests/LibWeb/Text/input/range-client-rects-collapsed.html b/Tests/LibWeb/Text/input/range-client-rects-collapsed.htmlnew file mode 100644index 00000000000..56ef60373fc--- /dev/null+++ b/Tests/LibWeb/Text/input/range-client-rects-collapsed.html@@ -0,0 +1,35 @@+<!DOCTYPE html>+<style>+body { margin: 0; }+div { position: absolute; font: 20px/30px Ahem; }+#wrapped { left: 10px; top: 10px; width: 120px; }+#pre { left: 200px; top: 10px; white-space: pre; }+#vertical { left: 400px; top: 10px; writing-mode: vertical-rl; height: 120px; }+#scroller { left: 10px; top: 200px; width: 200px; height: 60px; overflow: scroll; }+</style>+<div id="wrapped">aaa bbbbb ccc</div>+<div id="pre">one++three</div>+<div id="vertical">abc def</div>+<div id="scroller">one<br>two<br>three<br>four<br>five</div>+<script src="include.js"></script>+<script>+ // A collapsed range in text has a zero-width rect where its caret is: at the start of the next line at a soft wrap,+ // and none on a line with no text.+ test(() => {+ const format = rect => `${Math.round(rect.left)},${Math.round(rect.top)} ${Math.round(rect.width)}x${Math.round(rect.height)}`;+ const collapsedRects = (text, offset) => {+ const range = document.createRange();+ range.setStart(text, offset);+ const rects = [...range.getClientRects()].map(format).join(" ") || "none";+ return `${rects}, bounding ${format(range.getBoundingClientRect())}`;+ };+ document.getElementById("scroller").scrollTop = 20;+ for (const [id, offsets, child] of [["wrapped", [0, 4, 9, 10, 13]], ["pre", [0, 3, 4, 5, 10]], ["vertical", [0, 2, 7]], ["scroller", [1], 4]]) {+ const text = document.getElementById(id).childNodes[child ?? 0];+ for (const offset of offsets)+ println(`${id} ${offset}: ${collapsedRects(text, offset)}`);+ }+ });+</script>