Skip to content

Truly independent web browser

LibWeb: Index links held apart from their containing blocks

Aliaksandr Kalenikbdf304d67f09
diff --git a/Libraries/LibWeb/Rust/src/cow_map.rs b/Libraries/LibWeb/Rust/src/cow_map.rs
index 2f36bfa1095..fc0876946c8 100644
--- a/Libraries/LibWeb/Rust/src/cow_map.rs
+++ b/Libraries/LibWeb/Rust/src/cow_map.rs
@@ -414,10 +414,6 @@ impl<T: Clone + Eq + Hash> CowSet<T> {
self.0.heap_size()
}
- pub(crate) fn is_empty(&self) -> bool {
- self.0.is_empty()
- }
-
pub(crate) fn clear(&mut self) {
self.0.clear();
}
@@ -551,6 +547,6 @@ mod tests {
assert!(set.contains(&1));
assert!(set.remove(&1));
assert!(!set.remove(&1));
- assert!(set.is_empty());
+ assert_eq!(set.iter().count(), 0);
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs b/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
index f8d0fcd6a0f..ddcddfaac21 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+use crate::cow_map::CowMap;
use crate::css::css_enums::{overflow, positioning, writing_mode};
use crate::css::css_pixels::{CssPixelPoint, CssPixelRect, CssPixels};
use crate::css::display::FfiDisplay;
@@ -21,6 +22,7 @@ use crate::painting::scrollable_overflow::physical_overflow_directions_of_style;
use crate::painting::visual_context::node_values;
use crate::painting::{node_painting, scroll_snap, style_queries};
use libgfx_rust::AffineTransform;
+use smallvec::SmallVec;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct FragmentScrollableOverflow {
@@ -630,6 +632,60 @@ pub(crate) fn scrollable_overflow_of<'link>(
}
}
+/// The boxes whose committed links a box other than their containing block holds, by the containing block they were
+/// committed with, which a containing block reads its own from without walking every other one's.
+#[derive(Clone, Default)]
+pub(crate) struct LinksHeldApartFromTheirContainingBlocks {
+ containing_block_of: CowMap<NodeSlotId, NodeSlotId>,
+ by_containing_block: CowMap<NodeSlotId, SmallVec<[NodeSlotId; 4]>>,
+}
+
+impl LinksHeldApartFromTheirContainingBlocks {
+ fn insert(&mut self, node: NodeSlotId, containing_block: NodeSlotId) {
+ match self.containing_block_of.insert(node, containing_block) {
+ Some(previous) if previous == containing_block => return,
+ Some(previous) => self.remove_from(previous, node),
+ None => {}
+ }
+ self.by_containing_block
+ .get_or_insert_with(containing_block, SmallVec::new)
+ .push(node);
+ }
+
+ fn remove(&mut self, node: NodeSlotId) {
+ if let Some(containing_block) = self.containing_block_of.remove(&node) {
+ self.remove_from(containing_block, node);
+ }
+ }
+
+ fn remove_from(&mut self, containing_block: NodeSlotId, node: NodeSlotId) {
+ let Some(nodes) = self.by_containing_block.get_mut(&containing_block) else {
+ return;
+ };
+ if let Some(position) = nodes.iter().position(|held| *held == node) {
+ nodes.swap_remove(position);
+ }
+ if nodes.is_empty() {
+ self.by_containing_block.remove(&containing_block);
+ }
+ }
+
+ fn held_apart_from(&self, containing_block: NodeSlotId) -> &[NodeSlotId] {
+ self.by_containing_block
+ .get(&containing_block)
+ .map_or(&[], |nodes| nodes.as_slice())
+ }
+
+ fn is_empty(&self) -> bool {
+ self.containing_block_of.is_empty()
+ }
+
+ pub(crate) fn clear(&mut self) {
+ self.containing_block_of.clear();
+ self.by_containing_block.clear();
+ }
+}
+
impl LayoutNodeArena {
pub(crate) fn request_fragment_overflow_recalculation(&mut self, node: NodeSlotId) {
if !node.is_invalid() {
@@ -644,10 +700,11 @@ impl LayoutNodeArena {
is_held_apart: bool,
) {
if is_held_apart {
- self.links_held_apart_from_their_containing_blocks.insert(node);
+ self.links_held_apart_from_their_containing_blocks
+ .insert(node, containing_block);
self.request_fragment_overflow_recalculation(containing_block);
} else if !self.links_held_apart_from_their_containing_blocks.is_empty() {
- self.links_held_apart_from_their_containing_blocks.remove(&node);
+ self.links_held_apart_from_their_containing_blocks.remove(node);
}
}
@@ -707,7 +764,12 @@ impl LayoutNodeArena {
let candidates = contained_links(&link.fragment)
.map(|contained_link| contained_link.fragment.node)
.chain(links_held_beside)
- .chain(self.links_held_apart_from_their_containing_blocks.iter().copied());
+ .chain(
+ self.links_held_apart_from_their_containing_blocks
+ .held_apart_from(node)
+ .iter()
+ .copied(),
+ );
for candidate in candidates {
if met.insert(candidate)
&& self.slot_is_live(candidate)
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index ad34e8749e0..4faf6a7e64d 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -1231,7 +1231,8 @@ pub(crate) struct LayoutNodeArena {
committed_link_holders: ForkVec<CommittedLinkHolder>,
/// The boxes whose committed links a full commit met at the top of its pass, which no fragment holds.
pass_level_committed_links: Vec<NodeSlotId>,
- pub(crate) links_held_apart_from_their_containing_blocks: CowSet<NodeSlotId>,
+ pub(crate) links_held_apart_from_their_containing_blocks:
+ super::fragment_overflow::LinksHeldApartFromTheirContainingBlocks,
/// Whether a node left the document since a layout round last left layout up to date. Its boxes leave the rows at
/// once, while the committed layout of the boxes around them names them until that round lays those out again.
boxes_removed_ahead_of_layout: bool,