Skip to content

Truly independent web browser

LibWeb: Rebase glyph cluster starts when splitting text items

Aliaksandr Kalenik128dcfa3d6e6
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index 7bd098199a4..b592d6405b8 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -396,10 +396,12 @@ impl Item {
at_split
})?;
let split_x = run.glyphs[split_glyph_index].x;
+ let split_cluster_start = run.glyphs[split_glyph_index].cluster_start;
let glyphs = run.glyphs[split_glyph_index..]
.iter()
.map(|glyph| libgfx_rust::text_layout::DrawGlyph {
x: glyph.x - split_x,
+ cluster_start: glyph.cluster_start.saturating_sub(split_cluster_start),
..*glyph
})
.collect();
@@ -482,9 +484,11 @@ impl Item {
let mut prefix = self.take_owned_glyph_run(prepared);
let split_x = prefix.glyphs[split_glyph_index].x;
+ let split_cluster_start = prefix.glyphs[split_glyph_index].cluster_start;
let mut remainder_glyphs = prefix.glyphs.split_off(split_glyph_index);
for glyph in &mut remainder_glyphs {
glyph.x -= split_x;
+ glyph.cluster_start = glyph.cluster_start.saturating_sub(split_cluster_start);
}
let remainder = OwnedGlyphRun {
glyphs: remainder_glyphs,
diff --git a/Tests/LibWeb/Text/expected/Editing/caret-in-word-broken-across-lines.txt b/Tests/LibWeb/Text/expected/Editing/caret-in-word-broken-across-lines.txt
new file mode 100644
index 00000000000..0c7a52d2eac
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Editing/caret-in-word-broken-across-lines.txt
@@ -0,0 +1,2 @@
+Lines: several
+Offsets with a misplaced caret: none
diff --git a/Tests/LibWeb/Text/input/Editing/caret-in-word-broken-across-lines.html b/Tests/LibWeb/Text/input/Editing/caret-in-word-broken-across-lines.html
new file mode 100644
index 00000000000..55358e238f7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Editing/caret-in-word-broken-across-lines.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ #editor {
+ font: 20px monospace;
+ width: 5ch;
+ overflow-wrap: anywhere;
+ }
+</style>
+<div id="editor" contenteditable="true">abcdefghijklmnopqrstuvwxyz</div>
+<script>
+ test(() => {
+ const editor = document.getElementById("editor");
+ const text = editor.firstChild;
+ const selection = getSelection();
+ editor.focus();
+
+ const rectOfCharacter = index => {
+ const range = document.createRange();
+ range.setStart(text, index);
+ range.setEnd(text, index + 1);
+ return range.getBoundingClientRect();
+ };
+
+ const offsetsWithMisplacedCaret = [];
+ for (let offset = 0; offset < text.length; ++offset) {
+ selection.setBaseAndExtent(text, offset, text, offset);
+ const caret = internals.currentCaretRect();
+ const character = rectOfCharacter(offset);
+ if (caret.left !== character.left || caret.top !== character.top)
+ offsetsWithMisplacedCaret.push(offset);
+ }
+
+ println(`Lines: ${new Set([...text.data].map((_, index) => rectOfCharacter(index).top)).size > 1 ? "several" : "one"}`);
+ println(`Offsets with a misplaced caret: ${offsetsWithMisplacedCaret.join(", ") || "none"}`);
+ });
+</script>