Skip to content

Truly independent web browser

LibWeb: Find a node's fragments by index in long inline content

Andreas Kling6dc90892e827
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_content.rs b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
index c236aacd270..8a53be57e01 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_content.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
@@ -29,6 +29,26 @@ pub struct InlineContent {
pub writing_mode: u8,
/// The pieces of each inline box, by box.
pub inline_boxes: InlineBoxIndex,
+ /// The fragments of each node, by node.
+ pub text_fragments: TextFragmentIndex,
+}
+
+/// The indices of each node's fragments in its line root's content, which queries that start from a node look up in a
+/// line root with many fragments. Most line roots are never asked, so the index is built the first time one is.
+#[derive(Default)]
+pub struct TextFragmentIndex(std::sync::OnceLock<InlineBoxIndex>);
+
+// NB: The index follows from the fragments, so a copy builds its own, and it does not tell two contents apart.
+impl Clone for TextFragmentIndex {
+ fn clone(&self) -> Self {
+ Self::default()
+ }
+}
+
+impl PartialEq for TextFragmentIndex {
+ fn eq(&self, _: &Self) -> bool {
+ true
+ }
}
/// The indices of each inline box's pieces in its line root's content, in line order, which painting and geometry
@@ -53,11 +73,14 @@ fn inline_box_order(node: NodeSlotId) -> (u32, u8) {
impl InlineBoxIndex {
fn of(pieces: &[InlineBoxPieceRecord]) -> Self {
- let mut pieces_by_box: Vec<(NodeSlotId, u32)> = pieces
- .iter()
+ Self::of_nodes(pieces.iter().map(|piece| piece.node))
+ }
+
+ fn of_nodes(nodes: impl Iterator<Item = NodeSlotId>) -> Self {
+ let mut pieces_by_box: Vec<(NodeSlotId, u32)> = nodes
.enumerate()
- .filter(|(_, piece)| !piece.node.is_invalid())
- .map(|(index, piece)| (piece.node, index as u32))
+ .filter(|(_, node)| !node.is_invalid())
+ .map(|(index, node)| (node, index as u32))
.collect();
pieces_by_box.sort_unstable_by_key(|&(node, index)| (inline_box_order(node), index));
let mut index = Self {
@@ -279,6 +302,7 @@ impl InlineContent {
lines,
fragments,
inline_boxes: InlineBoxIndex::of(&pieces),
+ text_fragments: TextFragmentIndex::default(),
inline_box_pieces: pieces,
items: Vec::new(),
lines_after_clamp_point_rect: data.lines_after_clamp_point_rect,
@@ -356,6 +380,15 @@ impl InlineContent {
/// Indexes the pieces of each inline box again, after they were renumbered or given to other boxes.
pub(crate) fn index_inline_boxes(&mut self) {
self.inline_boxes = InlineBoxIndex::of(&self.inline_box_pieces);
+ self.text_fragments = TextFragmentIndex::default();
+ }
+
+ /// The indices of the fragments of `node`, in fragment order.
+ pub(crate) fn fragment_indices_of(&self, node: NodeSlotId) -> &[u32] {
+ self.text_fragments
+ .0
+ .get_or_init(|| InlineBoxIndex::of_nodes(self.fragments.iter().map(|fragment| fragment.layout_node)))
+ .piece_indices_of(node)
}
fn drop_repeated_placeholder_pieces(&mut self) {
diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_data.rs b/Libraries/LibWeb/Rust/src/painting/paintable_data.rs
index 2b315f2a7bf..9ccc12ffe68 100644
--- a/Libraries/LibWeb/Rust/src/painting/paintable_data.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paintable_data.rs
@@ -276,6 +276,12 @@ impl CommittedSideData {
pub(crate) fn fragments(&self) -> &[FragmentRecord] {
self.inline_content.as_ref().map_or(&[], |content| &content.fragments)
}
+ /// For a line root, the indices of the fragments of `node` among its fragments, in fragment order.
+ pub(crate) fn fragment_indices_of(&self, node: NodeSlotId) -> &[u32] {
+ self.inline_content
+ .as_ref()
+ .map_or(&[], |content| content.fragment_indices_of(node))
+ }
pub(crate) fn inline_box_pieces(&self) -> &[InlineBoxPieceRecord] {
self.inline_content
.as_ref()
diff --git a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs b/Libraries/LibWeb/Rust/src/painting/text_fragment.rs
index a0fa71e44fc..75e6553f57e 100644
--- a/Libraries/LibWeb/Rust/src/painting/text_fragment.rs
+++ b/Libraries/LibWeb/Rust/src/painting/text_fragment.rs
@@ -177,15 +177,25 @@ pub(crate) fn for_each_fragment_of_nodes(
node_slots: &[NodeSlotId],
mut callback: impl FnMut(NodeSlotId, u32, &FragmentRecord) -> bool,
) {
+ // OPTIMIZATION: Up to this many fragments, a scan costs less than building an index would.
+ const MAX_FRAGMENTS_TO_SCAN: usize = 32;
+
for &node in node_slots {
let Some(block) = containing_block_paintable_of_node(layout_arena, node) else {
continue;
};
- for (index, fragment) in layout_arena.committed_side_data(block).fragments().iter().enumerate() {
- if fragment.layout_node != node {
- continue;
+ let side_data = layout_arena.committed_side_data(block);
+ let fragments = side_data.fragments();
+ if fragments.len() <= MAX_FRAGMENTS_TO_SCAN {
+ for (index, fragment) in fragments.iter().enumerate() {
+ if fragment.layout_node == node && !callback(block, index as u32, fragment) {
+ return;
+ }
}
- if !callback(block, index as u32, fragment) {
+ continue;
+ }
+ for &index in side_data.fragment_indices_of(node) {
+ if !callback(block, index, &fragments[index as usize]) {
return;
}
}
diff --git a/Tests/LibWeb/Text/expected/range-client-rects-in-block-with-many-fragments.txt b/Tests/LibWeb/Text/expected/range-client-rects-in-block-with-many-fragments.txt
new file mode 100644
index 00000000000..a795ab84337
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/range-client-rects-in-block-with-many-fragments.txt
@@ -0,0 +1,4 @@
+first span: 8,8,55.859375,16
+last span: 8,264,64.1875,16
+long text: 131.53125,104,78.203125,16 8,120,197.015625,16 8,136,207.140625,16 8,152,106.34375,16
+whole block: 84 rects
diff --git a/Tests/LibWeb/Text/input/range-client-rects-in-block-with-many-fragments.html b/Tests/LibWeb/Text/input/range-client-rects-in-block-with-many-fragments.html
new file mode 100644
index 00000000000..1ab9ade4015
--- /dev/null
+++ b/Tests/LibWeb/Text/input/range-client-rects-in-block-with-many-fragments.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<style>
+ #block {
+ font: 16px SerenitySans;
+ width: 200px;
+ }
+</style>
+<div id="block"></div>
+<script src="include.js"></script>
+<script>
+ test(() => {
+ const block = document.getElementById("block");
+ for (let i = 0; i < 40; ++i) {
+ const span = document.createElement("span");
+ span.textContent = `word${i} `;
+ block.appendChild(span);
+ }
+ const longText = document.createTextNode("this text is long enough to wrap over more than one line of the block ");
+ block.insertBefore(longText, block.children[20]);
+
+ function rects(node) {
+ const range = document.createRange();
+ range.selectNodeContents(node);
+ return Array.from(range.getClientRects()).map(rect => `${rect.x},${rect.y},${rect.width},${rect.height}`).join(" ");
+ }
+
+ println(`first span: ${rects(block.children[0].firstChild)}`);
+ println(`last span: ${rects(block.children[39].firstChild)}`);
+ println(`long text: ${rects(longText)}`);
+ println(`whole block: ${rects(block).split(" ").length} rects`);
+ });
+</script>