Skip to content

Truly independent web browser

LibWeb: Reuse in-flow blocks that place floats

Aliaksandr Kalenikbd171f6d7111
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index e9380e93749..0e9b6e22066 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -684,6 +684,13 @@ pub(crate) struct FloatingBox {
}
impl FloatingBox {
+ fn translated(mut self, x: CssPixels, y: CssPixels) -> Self {
+ self.margin_box_rect_in_root_coordinate_space = self.margin_box_rect_in_root_coordinate_space.translated(x, y);
+ self.containing_block_rect_in_root_coordinate_space =
+ self.containing_block_rect_in_root_coordinate_space.translated(x, y);
+ self
+ }
+
fn margin_box_left_in_root_coordinate_space(&self) -> CssPixels {
let containing_block_rect = self.containing_block_rect_in_root_coordinate_space;
if self.side == FloatSide::Left {
@@ -2231,12 +2238,7 @@ impl<'pass> BlockFormattingContext<'pass> {
if !self.is_ancestor_of(ancestor, floating_box.box_) {
continue;
}
- floating_box.margin_box_rect_in_root_coordinate_space = floating_box
- .margin_box_rect_in_root_coordinate_space
- .translated(delta.x, delta.y);
- floating_box.containing_block_rect_in_root_coordinate_space = floating_box
- .containing_block_rect_in_root_coordinate_space
- .translated(delta.x, delta.y);
+ *floating_box = floating_box.translated(delta.x, delta.y);
any_float_moved = true;
}
if !any_float_moved {
@@ -3090,8 +3092,13 @@ impl<'pass> BlockFormattingContext<'pass> {
) {
InFlowReuse::Hit { entry, fragment } => {
let in_open_top_margin_group = self.margin_state().has_open_top_margin_group();
- let (content_offset, margin_box_block_end) =
- self.reuse_in_flow_child(sized_child, block_container, &entry, fragment);
+ let (content_offset, margin_box_block_end) = self.reuse_in_flow_child(
+ sized_child,
+ block_container,
+ &entry,
+ fragment,
+ containing_block_position_in_root,
+ );
self.note_floats_read_by_reused_contents(
node,
&entry,
@@ -3922,6 +3929,7 @@ impl<'pass> BlockFormattingContext<'pass> {
return floats_read.floats_in_reach.is_empty();
}
self.floats_in_reach_of_contents(
+ self.floating_box_count(),
read_start_from_border_box,
border_box_block_start,
containing_block_position_in_root.x + entry.content_inline_offset,
@@ -3952,16 +3960,18 @@ impl<'pass> BlockFormattingContext<'pass> {
}
}
+ /// Which of the first `float_count` floats reach the contents of a box, which read floats from
+ /// `read_start_from_border_box` down, relative to the box.
fn floats_in_reach_of_contents(
&self,
+ float_count: usize,
read_start_from_border_box: CssPixels,
border_box_block_start: CssPixels,
content_inline_start: CssPixels,
) -> SmallVec<[FloatInReach; 2]> {
let read_start_in_root = border_box_block_start + read_start_from_border_box;
let root_content_inline_size = self.root_cells.content_inline_size;
- self.floats
- .borrow()
+ self.floats.borrow()[..float_count]
.iter()
.filter_map(|floating_box| {
let margin_box_rect = floating_box.margin_box_rect_in_root_coordinate_space.translated(
@@ -4005,6 +4015,7 @@ impl<'pass> BlockFormattingContext<'pass> {
block_container: BlockContainer,
entry: &in_flow_block_cache::InFlowBlockEntry,
fragment: std::sync::Arc<fragment_tree::Fragment>,
+ containing_block_position_in_root: FfiCssPixelPoint,
) -> (FfiCssPixelPoint, CssPixels) {
let node = sized_child.node;
self.run.callbacks.layout_trace().reused_in_flow_subtree(node);
@@ -4042,6 +4053,18 @@ impl<'pass> BlockFormattingContext<'pass> {
None,
Some(fragment_tree::RunRootFragment::propagating_nothing(fragment)),
);
+ self.append_floating_boxes(
+ entry
+ .floats_placed_by_contents
+ .iter()
+ .map(|floating_box| {
+ floating_box.translated(
+ containing_block_position_in_root.x + content_offset.x,
+ containing_block_position_in_root.y + content_offset.y,
+ )
+ })
+ .collect(),
+ );
self.block_offset_of_current_block_container.set(Some(
content_offset.y
+ metrics.content_block_size()
@@ -4106,7 +4129,6 @@ impl<'pass> BlockFormattingContext<'pass> {
// NB: A reused child tells its parent that margins do not collapse through it, which holds only for a child
// whose border box has a block size.
let reusable = !placed.exports_payloads
- && self.floating_box_count() == float_count_before_child
&& self.abspos_containing_block_info_contributions_received()
== abspos_containing_block_info_contributions_received
&& self.run.callbacks.host_findings_noted_so_far() == host_findings_noted_so_far
@@ -4128,6 +4150,7 @@ impl<'pass> BlockFormattingContext<'pass> {
read_start_from_border_box,
floats_in_reach: read_start_from_border_box.map_or_else(SmallVec::new, |read_start_from_border_box| {
self.floats_in_reach_of_contents(
+ float_count_before_child,
read_start_from_border_box,
border_box_block_start,
containing_block_position_in_root.x + content_offset.x,
@@ -4143,6 +4166,15 @@ impl<'pass> BlockFormattingContext<'pass> {
margins_left_by_contents: margins_left_by_contents
.expect("the contents of an in-flow child collapse their margins in a margin state of their own"),
floats_read_by_contents,
+ floats_placed_by_contents: self.floats.borrow()[float_count_before_child..]
+ .iter()
+ .map(|floating_box| {
+ floating_box.translated(
+ -(containing_block_position_in_root.x + content_offset.x),
+ -(containing_block_position_in_root.y + content_offset.y),
+ )
+ })
+ .collect(),
outgoing_outside_marker_line: self.outside_marker_line.get(),
depends_on_percentage_block_size,
fragment_identity: placed.fragment.identity,
@@ -5900,6 +5932,9 @@ impl<'pass> BlockFormattingContext<'pass> {
} else {
block_offset
};
+ // NB: A float that ends above this position cannot reach the float being placed, whichever edge of it the steps
+ // below compare with.
+ self.note_float_read_from(containing_block_rect_now.y + margin_box_ceiling);
// https://www.w3.org/TR/CSS2/visuren.html#flow-control
// A float clears the floats of the sides its 'clear' names, whichever side it floats to itself.
if matches!(style.clear(), clear::LEFT | clear::BOTH | clear::INLINE_START)
diff --git a/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs b/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
index 71a8c73ec78..4dc1c2d02ae 100644
--- a/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
@@ -88,6 +88,8 @@ pub(super) struct InFlowBlockEntry {
/// What the child's contents left in their margin state, with the top margin group they collapsed into.
pub(super) margins_left_by_contents: block_formatting_context::BlockMarginState,
pub(super) floats_read_by_contents: FloatsReadByContents,
+ /// The floats the child's contents placed, relative to the child's content box.
+ pub(super) floats_placed_by_contents: Vec<block_formatting_context::FloatingBox>,
pub(super) outgoing_outside_marker_line: Option<block_formatting_context::OutsideMarkerLine>,
pub(super) depends_on_percentage_block_size: bool,
/// The fragment the stored layout placed. The committed subtree is reused only while it is still that fragment.
diff --git a/Tests/LibWeb/Text/expected/layout-in-flow-reuse-next-to-floats.txt b/Tests/LibWeb/Text/expected/layout-in-flow-reuse-next-to-floats.txt
index d3db0dc35a2..35fe9617c33 100644
--- a/Tests/LibWeb/Text/expected/layout-in-flow-reuse-next-to-floats.txt
+++ b/Tests/LibWeb/Text/expected/layout-in-flow-reuse-next-to-floats.txt
@@ -2,7 +2,7 @@ a box below every float moves: reused [below-beside below-last], laid out again
a box beside an unchanged float stays: reused [beside-first], laid out again [beside-second], matches a fresh layout
boxes move with their float: reused [together-last], laid out again [together-first], matches a fresh layout
a later box in a nested flow: reused [nested-last], laid out again [nested-first], matches a fresh layout
-a box that places a float: reused [places-float-beside], laid out again [places-float-box], matches a fresh layout
+a box that places a float: reused [places-float-box places-float-beside], laid out again [], matches a fresh layout
a float grows: reused [], laid out again [grow-beside grow-below], matches a fresh layout
a float is removed: reused [], laid out again [removed-beside], matches a fresh layout
a box moves into the reach of a float: reused [], laid out again [into-reach-box], matches a fresh layout
@@ -25,3 +25,11 @@ a box stored without floats, below a new float: reused [new-float-below], laid o
a box reused without floats: reused [read-through-first], laid out again [read-through-box], matches a fresh layout
then a new float reaches it: reused [], laid out again [read-through-box read-through-first], matches a fresh layout
a margin before a box that reads where it settled: reused [settled-box], laid out again [], matches a fresh layout
+a box carries the float it placed: reused [carries-box carries-beside], laid out again [carries-clear], matches a fresh layout
+a box read the float a reused box placed: reused [reader-box], laid out again [reader-beside], matches a fresh layout
+a reused box's float sizes its root: reused [grows-root-box], laid out again [], matches a fresh layout
+the root widens around a reused box with a float: reused [right-of-root-box], laid out again [], matches a fresh layout
+a float reaches a box that placed one: reused [], laid out again [in-reach-box], matches a fresh layout
+floats placed through a reused child: reused [nested-floats-inner], laid out again [nested-floats-outer], matches a fresh layout
+then the box holding them moves: reused [nested-floats-outer], laid out again [nested-floats-inner], matches a fresh layout
+a float in a top margin group: reused [in-margin-group-box], laid out again [in-margin-group-outer], matches a fresh layout
diff --git a/Tests/LibWeb/Text/input/layout-in-flow-reuse-next-to-floats.html b/Tests/LibWeb/Text/input/layout-in-flow-reuse-next-to-floats.html
index dbd61313b3c..eba7e5e4806 100644
--- a/Tests/LibWeb/Text/input/layout-in-flow-reuse-next-to-floats.html
+++ b/Tests/LibWeb/Text/input/layout-in-flow-reuse-next-to-floats.html
@@ -9,7 +9,7 @@
</style>
<div class="scenario" id="below"><div class="float" style="width: 20px; height: 20px"></div><div id="below-beside">one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen</div><div id="below-spacer" style="height: 30px"></div><p id="below-last">one two three four five six seven eight nine ten</p></div>
<div class="scenario" id="beside"><div class="float" style="width: 40px; height: 40px"></div><p id="beside-first">one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen</p><p id="beside-second">one two three four</p></div>
-<div class="scenario" id="together"><div id="together-spacer" style="height: 10px"></div><div><div class="float" style="width: 20px; height: 60px"></div><p id="together-first">one two three four five six seven eight nine ten eleven twelve</p><p id="together-last">one two three four five six seven eight nine ten eleven twelve</p></div></div>
+<div class="scenario" id="together"><div id="together-spacer" style="height: 10px"></div><div><div class="float" style="width: 20px; height: 60px"></div><p id="together-first">one two three four five six seven eight nine ten eleven twelve</p><p id="together-last">one two three four five six seven eight nine ten eleven twelve</p><p id="together-edited">one two</p></div></div>
<div class="scenario" id="nested"><div><div><div class="float" style="width: 20px; height: 10px"></div><p id="nested-first">one two three four five six seven eight nine ten eleven twelve</p><p id="nested-edited">one two</p><p id="nested-last">one two three four</p></div></div></div>
<div class="scenario" id="places-float"><div id="places-float-box"><div class="float" style="width: 20px; height: 30px"></div>one two three</div><p id="places-float-beside">one two three four five six seven eight nine ten eleven twelve</p><p id="places-float-edited">one two</p></div>
<div class="scenario" id="grow"><div class="float" id="grow-float" style="width: 20px; height: 15px"></div><p id="grow-beside">one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen</p><p id="grow-below">one two three four</p></div>
@@ -32,6 +32,13 @@
<div class="scenario" id="new-float"><div id="new-float-spacer" style="height: 30px"></div><p id="new-float-below">one two three four five six seven eight nine ten eleven twelve</p></div>
<div class="scenario" id="read-through"><div id="read-through-box"><p id="read-through-first">one two three four five six seven eight nine ten eleven twelve</p><div id="read-through-spacer" style="height: 10px"></div></div></div>
<div class="scenario" id="settled"><div class="float" style="width: 20px; height: 15px"></div><div id="settled-before" style="height: 20px; margin-bottom: 10px"></div><div id="settled-box" style="padding-top: 1px">one two three four five six seven eight nine ten eleven twelve</div></div>
+<div class="scenario" id="carries"><div id="carries-spacer" style="height: 10px"></div><div id="carries-box"><div class="float" style="width: 20px; height: 30px"></div>one two three</div><p id="carries-beside">one two three four five six seven eight nine ten eleven twelve</p><p id="carries-clear" style="clear: left">one two</p></div>
+<div class="scenario" id="reader"><div id="reader-box"><div class="float" style="width: 20px; height: 30px"></div><div class="float" style="float: right; width: 30px; height: 30px"></div>one two three</div><p id="reader-beside">one two</p></div>
+<div class="scenario" id="grows-root"><div class="scenario" id="grows-root-inner"><div id="grows-root-box"><div class="float" style="width: 20px; height: 60px"></div>one</div><p id="grows-root-edited">one two</p></div><p>after</p></div>
+<div class="scenario" id="right-of-root"><div style="width: 150px"><div id="right-of-root-box"><div class="float" style="float: right; width: 20px; height: 30px"></div>one</div><p id="right-of-root-beside">one two three four five six seven eight nine ten eleven twelve</p></div></div>
+<div class="scenario" id="in-reach"><div class="float" id="in-reach-float" style="width: 20px; height: 10px"></div><div id="in-reach-box" style="padding-top: 1px"><div class="float" style="width: 20px; height: 10px"></div>one</div></div>
+<div class="scenario" id="nested-floats"><div id="nested-floats-spacer" style="height: 10px"></div><div id="nested-floats-outer"><div id="nested-floats-inner"><div class="float" style="width: 20px; height: 30px"></div>one</div><div id="nested-floats-edited" style="height: 10px"></div></div><p id="nested-floats-beside">one two three four five six seven eight nine ten eleven twelve</p></div>
+<div class="scenario" id="in-margin-group"><div id="in-margin-group-outer" style="margin-top: 20px"><div id="in-margin-group-box" style="margin-top: 30px"><div class="float" style="width: 20px; height: 30px"></div>one</div></div><p id="in-margin-group-beside">one two three four five six seven eight nine ten eleven twelve</p></div>
<script>
function geometryOf(root) {
const origin = root.getBoundingClientRect();
@@ -81,6 +88,7 @@
});
step("boxes move with their float", "together", ["together-first", "together-last"], () => {
byId("together-spacer").style.height = "20px";
+ byId("together-edited").firstChild.data = "one three";
});
step("a later box in a nested flow", "nested", ["nested-first", "nested-last"], () => {
byId("nested-edited").firstChild.data = "one three";
@@ -161,6 +169,30 @@
step("a margin before a box that reads where it settled", "settled", ["settled-box"], () => {
byId("settled-before").style.marginBottom = "30px";
});
+ step("a box carries the float it placed", "carries", ["carries-box", "carries-beside", "carries-clear"], () => {
+ byId("carries-spacer").style.height = "25px";
+ });
+ step("a box read the float a reused box placed", "reader", ["reader-box", "reader-beside"], () => {
+ byId("reader-beside").firstChild.data = "one two three four five six seven eight nine ten eleven twelve";
+ });
+ step("a reused box's float sizes its root", "grows-root", ["grows-root-box"], () => {
+ byId("grows-root-edited").firstChild.data = "one three";
+ });
+ step("the root widens around a reused box with a float", "right-of-root", ["right-of-root-box"], () => {
+ byId("right-of-root").style.width = "300px";
+ });
+ step("a float reaches a box that placed one", "in-reach", ["in-reach-box"], () => {
+ byId("in-reach-float").style.height = "20px";
+ });
+ step("floats placed through a reused child", "nested-floats", ["nested-floats-outer", "nested-floats-inner"], () => {
+ byId("nested-floats-edited").style.height = "20px";
+ });
+ step("then the box holding them moves", "nested-floats", ["nested-floats-outer", "nested-floats-inner"], () => {
+ byId("nested-floats-spacer").style.height = "20px";
+ });
+ step("a float in a top margin group", "in-margin-group", ["in-margin-group-outer", "in-margin-group-box"], () => {
+ byId("in-margin-group-outer").style.marginTop = "40px";
+ });
for (const line of output)
println(line);