Skip to content

Truly independent web browser

LibWeb: Reuse in-flow block layouts next to floats

Aliaksandr Kalenik3dac8df4f83a
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index f1120b1960c..be61c29013d 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -605,6 +605,11 @@ enum InFlowReuse {
validity: fc_run_cache::FcRunCacheValidity,
abspos_containing_block_info_contributions_received: u64,
host_findings_noted_so_far: u64,
+ block_start_of_float_reads_before_child: Option<CssPixels>,
+ float_count_before_child: usize,
+ margins_before_box: CollapsedMargins,
+ flow_position_before_box: CssPixels,
+ containing_block_position_in_root: FfiCssPixelPoint,
},
}
@@ -614,6 +619,13 @@ enum FloatSide {
Right,
}
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) struct FloatInReach {
+ side: FloatSide,
+ margin_box_rect_from_box: CssPixelRect,
+ inline_intrusion_from_box: CssPixels,
+}
+
#[derive(Clone, Copy)]
pub(crate) struct FloatingBox {
box_: Node,
@@ -645,6 +657,17 @@ impl FloatingBox {
containing_block_rect.right() - self.offset_from_edge - self.left_margin_edge_to_content_edge
}
}
+
+ fn inline_intrusion_into_root(&self, root_content_inline_size: CssPixels) -> CssPixels {
+ let containing_block_rect = self.containing_block_rect_in_root_coordinate_space;
+ if self.side == FloatSide::Left {
+ containing_block_rect.x + self.offset_from_edge + self.left_content_edge_to_right_margin_edge
+ } else {
+ (root_content_inline_size - containing_block_rect.right())
+ + self.offset_from_edge
+ + self.left_margin_edge_to_content_edge
+ }
+ }
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -726,6 +749,7 @@ pub(crate) struct BlockFormattingContext<'pass> {
lowest_left_margin_edge: Cell<CssPixels>,
lowest_right_margin_edge: Cell<CssPixels>,
lowest_floating_descendant_bottom_margin_edge: Cell<Option<CssPixels>>,
+ block_start_of_float_reads_in_root: Cell<Option<CssPixels>>,
derived_baselines_of_root_box: Cell<DerivedBaselines>,
/// The trailing collapsed margin of the run, and the index in `root_flow_children` of the child it hangs below.
trailing_collapsed_margin: Cell<Option<(usize, CssPixels)>>,
@@ -887,6 +911,7 @@ impl<'pass> BlockFormattingContext<'pass> {
lowest_left_margin_edge: Cell::new(CssPixels::default()),
lowest_right_margin_edge: Cell::new(CssPixels::default()),
lowest_floating_descendant_bottom_margin_edge: Cell::new(None),
+ block_start_of_float_reads_in_root: Cell::new(None),
derived_baselines_of_root_box: Cell::new(DerivedBaselines::default()),
trailing_collapsed_margin: Cell::new(None),
table_box_in_wrapper_border_box_inline_size: Cell::new(None),
@@ -1927,7 +1952,17 @@ impl<'pass> BlockFormattingContext<'pass> {
self.bands.borrow()[self.band_index_at(block_offset)]
}
+ fn note_float_read_from(&self, block_position_in_root: CssPixels) {
+ let block_start_of_reads = self.block_start_of_float_reads_in_root.get();
+ self.block_start_of_float_reads_in_root.set(Some(
+ block_start_of_reads.map_or(block_position_in_root, |block_start| {
+ block_start.min(block_position_in_root)
+ }),
+ ));
+ }
+
pub(crate) fn next_float_band_block_start_after(&self, block_offset_in_root: CssPixels) -> Option<CssPixels> {
+ self.note_float_read_from(block_offset_in_root);
self.bands
.borrow()
.iter()
@@ -1962,6 +1997,7 @@ impl<'pass> BlockFormattingContext<'pass> {
}
fn merged_band(&self, block_start_in_root: CssPixels, block_end_in_root: CssPixels) -> FloatBand {
+ self.note_float_read_from(block_start_in_root);
let bands = self.bands.borrow();
assert!(!bands.is_empty());
let first_index = self.band_index_at(block_start_in_root);
@@ -2075,11 +2111,9 @@ impl<'pass> BlockFormattingContext<'pass> {
bands.push(band);
}
- fn add_float_to_bands(&self, floating_box: FloatingBox, mut containing_block_rect_in_root: CssPixelRect) {
+ fn add_float_to_bands(&self, floating_box: FloatingBox) {
let pending_adjustment =
self.block_offset_adjustment_from_pending_ancestor_block_start_margins(floating_box.box_);
- containing_block_rect_in_root.y += pending_adjustment;
- let root_content_inline_size = self.root_cells.content_inline_size;
let margin_box_rect = floating_box
.margin_box_rect_in_root_coordinate_space
.translated(CssPixels::default(), pending_adjustment);
@@ -2102,15 +2136,7 @@ impl<'pass> BlockFormattingContext<'pass> {
}
self.ensure_band_boundary(block_start);
self.ensure_band_boundary(block_end);
- let intrusion = if floating_box.side == FloatSide::Left {
- containing_block_rect_in_root.x
- + floating_box.offset_from_edge
- + floating_box.left_content_edge_to_right_margin_edge
- } else {
- (root_content_inline_size - containing_block_rect_in_root.right())
- + floating_box.offset_from_edge
- + floating_box.left_margin_edge_to_content_edge
- };
+ let intrusion = floating_box.inline_intrusion_into_root(self.root_cells.content_inline_size);
for band in self.bands.borrow_mut().iter_mut() {
if band.block_start < block_start {
continue;
@@ -2136,10 +2162,7 @@ impl<'pass> BlockFormattingContext<'pass> {
self.lowest_right_margin_edge.set(CssPixels::default());
let floats = self.floats.borrow();
for &floating_box in floats.iter() {
- self.add_float_to_bands(
- floating_box,
- floating_box.containing_block_rect_in_root_coordinate_space,
- );
+ self.add_float_to_bands(floating_box);
}
}
@@ -2428,6 +2451,17 @@ impl<'pass> BlockFormattingContext<'pass> {
containing_block_position_in_root: FfiCssPixelPoint,
) -> bool {
let style = self.style(node);
+ if style.clear() != clear::NONE {
+ let lowest_flow_position = match inline_context {
+ Some(_) => CssPixels::default(),
+ None => self.flow_position().min(CssPixels::default()),
+ };
+ self.note_float_read_from(
+ containing_block_position_in_root.y
+ + self.block_offset_adjustment_from_pending_ancestor_block_start_margins(node)
+ + lowest_flow_position,
+ );
+ }
let mut result = false;
let clear_to = |clearance_block_offset_in_root: CssPixels, result: &mut bool| {
if clearance_block_offset_in_root == CssPixels::default() {
@@ -3006,6 +3040,7 @@ impl<'pass> BlockFormattingContext<'pass> {
block_container_inline_size,
input.clone(),
containing_line_box_fragment,
+ containing_block_position_in_root,
) {
InFlowReuse::Hit { entry, fragment } => {
let (content_offset, margin_box_block_end) =
@@ -3705,7 +3740,8 @@ impl<'pass> BlockFormattingContext<'pass> {
}
/// Whether the layout of an in-flow child can be stored and reused by `InFlowBlockKey`: only a block container
- /// laid out in this run's own flow, where no float and no line clamp can read its position in the root.
+ /// laid out in this run's own flow, where no line clamp can read its position in the root, and where that position
+ /// is final if floats can read it.
fn in_flow_child_may_be_reused(
&self,
node: Node,
@@ -3719,7 +3755,7 @@ impl<'pass> BlockFormattingContext<'pass> {
&& self.run.fragments.is_some()
&& !self.run.should_collect_devtools_layout_data
&& containing_line_box_fragment.is_none()
- && self.floats.borrow().is_empty()
+ && (self.floats.borrow().is_empty() || !self.margin_state().has_open_top_margin_group())
&& !self.is_line_clamp_container
&& self.max_lines.get().is_none()
&& !self.laying_out_invisible_line_clamp_content.get()
@@ -3739,6 +3775,7 @@ impl<'pass> BlockFormattingContext<'pass> {
block_container_inline_size: CssPixels,
input: LayoutInput,
containing_line_box_fragment: Option<used_values::LineBoxFragmentCoordinate>,
+ containing_block_position_in_root: FfiCssPixelPoint,
) -> InFlowReuse {
if !self.in_flow_child_may_be_reused(node, block_container, containing_line_box_fragment) {
return InFlowReuse::Ineligible;
@@ -3770,8 +3807,12 @@ impl<'pass> BlockFormattingContext<'pass> {
fragment.identity,
)?;
Some((entry, fragment))
+ })
+ .filter(|(entry, _)| {
+ self.floats_read_by_contents_still_stand(node, entry, containing_block_position_in_root)
});
if let Some((entry, fragment)) = hit {
+ self.note_floats_read_by_reused_contents(node, &entry, containing_block_position_in_root);
return InFlowReuse::Hit { entry, fragment };
}
InFlowReuse::Store {
@@ -3780,9 +3821,124 @@ impl<'pass> BlockFormattingContext<'pass> {
abspos_containing_block_info_contributions_received: self
.abspos_containing_block_info_contributions_received(),
host_findings_noted_so_far: callbacks.host_findings_noted_so_far(),
+ block_start_of_float_reads_before_child: self.block_start_of_float_reads_in_root.take(),
+ float_count_before_child: self.floating_box_count(),
+ margins_before_box: self.margin_state().margins_since_reset,
+ flow_position_before_box: self.flow_position(),
+ containing_block_position_in_root,
}
}
+ fn border_box_block_start_of_reused_child(
+ &self,
+ node: Node,
+ floats_read: &in_flow_block_cache::FloatsReadByContents,
+ containing_block_position_in_root: FfiCssPixelPoint,
+ ) -> CssPixels {
+ containing_block_position_in_root.y
+ + self.block_offset_adjustment_from_pending_ancestor_block_start_margins(node)
+ + self.flow_position()
+ + floats_read.border_box_offset_from_flow_position
+ }
+
+ fn floats_read_by_contents_still_stand(
+ &self,
+ node: Node,
+ entry: &in_flow_block_cache::InFlowBlockEntry,
+ containing_block_position_in_root: FfiCssPixelPoint,
+ ) -> bool {
+ let floats_read = entry.floats_read_by_contents.as_ref();
+ if self.floats.borrow().is_empty() {
+ return floats_read.is_none_or(|floats_read| floats_read.floats_in_reach.is_empty());
+ }
+ let Some(floats_read) = floats_read else {
+ return false;
+ };
+ if floats_read.margins_before_box != self.margin_state().margins_since_reset {
+ return false;
+ }
+ self.rebuild_float_bands();
+ let Some(read_start_from_border_box) = floats_read.read_start_from_border_box else {
+ return true;
+ };
+ let border_box_block_start =
+ self.border_box_block_start_of_reused_child(node, floats_read, containing_block_position_in_root);
+ let lowest_margin_edge = self
+ .lowest_left_margin_edge
+ .get()
+ .max(self.lowest_right_margin_edge.get());
+ if lowest_margin_edge <= border_box_block_start + read_start_from_border_box {
+ return floats_read.floats_in_reach.is_empty();
+ }
+ self.floats_in_reach_of_contents_unless_above_root(
+ read_start_from_border_box,
+ border_box_block_start,
+ containing_block_position_in_root.x + entry.content_inline_offset,
+ )
+ .is_some_and(|floats_in_reach| floats_in_reach == floats_read.floats_in_reach)
+ }
+
+ fn note_floats_read_by_reused_contents(
+ &self,
+ node: Node,
+ entry: &in_flow_block_cache::InFlowBlockEntry,
+ containing_block_position_in_root: FfiCssPixelPoint,
+ ) {
+ if self.floats.borrow().is_empty() {
+ return;
+ }
+ let floats_read = entry
+ .floats_read_by_contents
+ .as_ref()
+ .expect("an in-flow child reused next to floats kept what its contents read of them");
+ if let Some(read_start_from_border_box) = floats_read.read_start_from_border_box {
+ self.note_float_read_from(
+ self.border_box_block_start_of_reused_child(node, floats_read, containing_block_position_in_root)
+ + read_start_from_border_box,
+ );
+ }
+ }
+
+ fn floats_in_reach_of_contents_unless_above_root(
+ &self,
+ read_start_from_border_box: CssPixels,
+ border_box_block_start: CssPixels,
+ content_inline_start: CssPixels,
+ ) -> Option<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;
+ let floats_in_reach: SmallVec<[FloatInReach; 2]> = self
+ .floats
+ .borrow()
+ .iter()
+ .filter_map(|floating_box| {
+ let margin_box_rect = floating_box.margin_box_rect_in_root_coordinate_space.translated(
+ CssPixels::default(),
+ self.block_offset_adjustment_from_pending_ancestor_block_start_margins(floating_box.box_),
+ );
+ if margin_box_rect.bottom() <= read_start_in_root {
+ return None;
+ }
+ let edge_of_box_from_root_edge = if floating_box.side == FloatSide::Left {
+ content_inline_start
+ } else {
+ root_content_inline_size - content_inline_start
+ };
+ Some(FloatInReach {
+ side: floating_box.side,
+ margin_box_rect_from_box: margin_box_rect
+ .translated(-content_inline_start, -border_box_block_start),
+ inline_intrusion_from_box: floating_box.inline_intrusion_into_root(root_content_inline_size)
+ - edge_of_box_from_root_edge,
+ })
+ })
+ .collect();
+ if !floats_in_reach.is_empty() && read_start_in_root < CssPixels::default() {
+ return None;
+ }
+ Some(floats_in_reach)
+ }
+
fn flow_position(&self) -> CssPixels {
self.block_offset_of_current_block_container
.get()
@@ -3886,10 +4042,19 @@ impl<'pass> BlockFormattingContext<'pass> {
validity,
abspos_containing_block_info_contributions_received,
host_findings_noted_so_far,
+ block_start_of_float_reads_before_child,
+ float_count_before_child,
+ margins_before_box,
+ flow_position_before_box,
+ containing_block_position_in_root,
} = attempt
else {
return;
};
+ let block_start_of_float_reads_of_contents = self.block_start_of_float_reads_in_root.get();
+ if let Some(block_start_of_reads) = block_start_of_float_reads_before_child {
+ self.note_float_read_from(block_start_of_reads);
+ }
let arena = self.run.callbacks.arena();
let Some((content_offset, placed)) = placed else {
return;
@@ -3898,7 +4063,7 @@ 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.floats.borrow().is_empty()
+ && 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
@@ -3906,6 +4071,37 @@ impl<'pass> BlockFormattingContext<'pass> {
if !reusable {
return;
}
+ let floats_read_by_contents = if float_count_before_child == 0 {
+ None
+ } else {
+ let border_box_offset_from_flow_position =
+ content_offset.y - used.border_box_top(false) - flow_position_before_box;
+ let border_box_block_start = containing_block_position_in_root.y
+ + self.block_offset_adjustment_from_pending_ancestor_block_start_margins(node)
+ + flow_position_before_box
+ + border_box_offset_from_flow_position;
+ let read_start_from_border_box = block_start_of_float_reads_of_contents
+ .map(|block_start_of_reads| block_start_of_reads - border_box_block_start);
+ let floats_in_reach = match read_start_from_border_box {
+ Some(read_start_from_border_box) => {
+ let Some(floats_in_reach) = self.floats_in_reach_of_contents_unless_above_root(
+ read_start_from_border_box,
+ border_box_block_start,
+ containing_block_position_in_root.x + content_offset.x,
+ ) else {
+ return;
+ };
+ floats_in_reach
+ }
+ None => SmallVec::new(),
+ };
+ Some(in_flow_block_cache::FloatsReadByContents {
+ margins_before_box,
+ border_box_offset_from_flow_position,
+ read_start_from_border_box,
+ floats_in_reach,
+ })
+ };
let entry = in_flow_block_cache::InFlowBlockEntry {
key,
validity,
@@ -3914,6 +4110,7 @@ impl<'pass> BlockFormattingContext<'pass> {
content_inline_offset: content_offset.x,
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,
outgoing_outside_marker_line: self.outside_marker_line.get(),
depends_on_percentage_block_size,
fragment_identity: placed.fragment.identity,
@@ -5719,7 +5916,7 @@ impl<'pass> BlockFormattingContext<'pass> {
margin_box_rect.x = floating_box.margin_box_left_in_root_coordinate_space();
floating_box.margin_box_rect_in_root_coordinate_space = margin_box_rect;
self.floats.borrow_mut().push(floating_box);
- self.add_float_to_bands(floating_box, containing_block_rect);
+ self.add_float_to_bands(floating_box);
(floating_box, content_block_offset)
}
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 83e5c9d5140..3335370b79f 100644
--- a/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
@@ -17,8 +17,9 @@ use super::*;
/// position, and collapses the margins its contents left with those now before it.
///
/// NB: Reuse is exact only while this key covers every such read. A new read of block formatting context or
-/// containing block state inside the in-flow child path must be added here. Floats and line clamping read the
-/// position in the formatting context root; neither may be present for an entry to be stored or used.
+/// containing block state inside the in-flow child path must be added here. Line clamping reads the position in
+/// the formatting context root, so it may not be present for an entry to be stored or used. What the subtree
+/// reads of floats is kept in the entry and has to hold where the box is placed now.
#[derive(Clone, PartialEq)]
pub(super) struct InFlowBlockKey {
pub(super) input: LayoutInput,
@@ -61,6 +62,14 @@ impl InFlowBlockKey {
}
}
+#[derive(Clone)]
+pub(super) struct FloatsReadByContents {
+ pub(super) margins_before_box: block_formatting_context::CollapsedMargins,
+ pub(super) border_box_offset_from_flow_position: CssPixels,
+ pub(super) read_start_from_border_box: Option<CssPixels>,
+ pub(super) floats_in_reach: smallvec::SmallVec<[block_formatting_context::FloatInReach; 2]>,
+}
+
/// What the layout of an in-flow block container leaves behind for its parent, apart from the flow position it started
/// at and the margins before it.
#[derive(Clone)]
@@ -75,6 +84,7 @@ pub(super) struct InFlowBlockEntry {
pub(super) content_inline_offset: CssPixels,
/// 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: Option<FloatsReadByContents>,
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
new file mode 100644
index 00000000000..a98816e4363
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/layout-in-flow-reuse-next-to-floats.txt
@@ -0,0 +1,23 @@
+a box below every float moves: reused [below-beside below-last], laid out again [], matches a fresh layout
+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 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
+only the intrusion of a float changes: reused [], laid out again [intrusion-beside], matches a fresh layout
+contents above their box read a float: reused [], laid out again [above-box-box], matches a fresh layout
+clearance after a negative flow offset: reused [], laid out again [negative-flow-box], matches a fresh layout
+clearance past a float without inline size: reused [], laid out again [zero-width-box], matches a fresh layout
+a margin before a box whose contents clear: reused [], laid out again [margin-clear-box], matches a fresh layout
+a margin before a list item: reused [], laid out again [margin-marker-box], matches a fresh layout
+floats move above the root: reused [], laid out again [negative-root-box], matches a fresh layout
+a box not stored still reads for its parent: reused [], laid out again [not-stored-box], matches a fresh layout
+a reused box inside a box laid out again: reused [replay-reused], laid out again [replay-box], matches a fresh layout
+then a float grows into the reused box: reused [], laid out again [replay-box replay-reused], matches a fresh layout
+a float widens beside a formatting context: reused [], laid out again [own-context-box], matches a fresh layout
+clearance in root inline content: reused [], laid out again [], matches a fresh layout
+a clearing float in root inline content: reused [], laid out again [], matches a fresh layout
+the root widens around a fixed box: reused [root-width-wrapped-last], laid out again [], matches a fresh layout
+the root widens around a float in it: reused [], laid out again [root-width-beside], 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
new file mode 100644
index 00000000000..bfce67697e2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/layout-in-flow-reuse-next-to-floats.html
@@ -0,0 +1,146 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<script src="layout-test.js"></script>
+<style>
+ body { margin: 0; font: 10px/10px SerenitySans; }
+ .scenario { display: flow-root; width: 200px; }
+ p { margin: 0; }
+ .float { float: left; }
+</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="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>
+<div class="scenario" id="removed"><div class="float" id="removed-float" style="width: 30px; height: 30px"></div><p id="removed-beside">one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen</p></div>
+<div class="scenario" id="into-reach"><div class="float" style="width: 20px; height: 30px"></div><div id="into-reach-spacer" style="height: 40px"></div><p id="into-reach-box">one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen</p></div>
+<div class="scenario" id="intrusion"><div class="float" id="intrusion-float" style="width: 50px; height: 50px"></div><p id="intrusion-beside">one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen</p></div>
+<div class="scenario" id="above-box"><div class="float" id="above-box-float" style="width: 20px; height: 30px"></div><div style="height: 40px"></div><div id="above-box-box" style="padding-top: 1px"><p style="margin-top: -35px">one two three four five six seven eight nine ten eleven twelve thirteen fourteen</p></div></div>
+<div class="scenario" id="negative-flow"><div class="float" id="negative-flow-float" style="width: 20px; height: 60px"></div><div style="height: 100px"></div><div id="negative-flow-box" style="padding-top: 1px"><div style="height: 10px; margin-top: -50px"></div><div style="clear: left">one</div></div></div>
+<div class="scenario" id="zero-width"><div class="float" id="zero-width-float" style="width: 0; height: 40px"></div><div style="height: 50px"></div><div id="zero-width-box" style="padding-top: 1px"><div style="clear: left">one</div></div></div>
+<div class="scenario" id="margin-clear"><div class="float" style="width: 20px; height: 35px"></div><div id="margin-clear-before" style="height: 10px; margin-bottom: 50px"></div><div id="margin-clear-box"><div style="clear: left; margin-top: 30px">one</div></div></div>
+<div class="scenario" id="margin-marker"><div class="float" style="width: 20px; height: 35px"></div><div id="margin-marker-before" style="height: 10px; margin-bottom: 50px"></div><div id="margin-marker-box" style="display: list-item; margin-left: 30px"><p style="margin-top: 30px">one</p></div></div>
+<div class="scenario" id="negative-root"><div id="negative-root-wrapper"><div class="float" style="width: 20px; height: 30px"></div><div style="height: 10px"></div><div id="negative-root-box"><div style="clear: left">one</div></div></div></div>
+<div class="scenario" id="not-stored"><div class="float" id="not-stored-float" style="width: 20px; height: 15px"></div><div id="not-stored-box" style="padding-bottom: 5px"><div style="height: 0">one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen</div></div></div>
+<div class="scenario" id="replay"><div class="float" id="replay-float" style="width: 20px; height: 15px"></div><div style="height: 10px"></div><div id="replay-box"><div id="replay-spacer" style="height: 20px"></div><p id="replay-reused">one two three four five six seven eight nine ten eleven twelve thirteen fourteen</p></div></div>
+<div class="scenario" id="own-context"><div class="float" id="own-context-float" style="width: 20px; height: 30px"></div><div id="own-context-box"><div style="overflow: hidden">one two three four five six seven eight nine ten eleven twelve thirteen fourteen</div></div></div>
+<div class="scenario" id="inline-clear">one<br style="clear: left">two</div>
+<div class="scenario" id="inline-float"><span id="inline-float-span" style="float: left; clear: left">one</span>two</div>
+<div class="scenario" id="root-width-wrapped"><div style="width: 150px"><div class="float" style="float: right; width: 20px; height: 40px"></div><p>one two three four five six seven eight nine ten eleven twelve</p><p id="root-width-wrapped-last">one two three four five six seven eight nine ten eleven twelve</p></div></div>
+<div class="scenario" id="root-width"><div class="float" style="float: right; width: 20px; height: 40px"></div><p id="root-width-beside">one two three four five six seven eight nine ten eleven twelve</p></div>
+<script>
+ function geometryOf(root) {
+ const origin = root.getBoundingClientRect();
+ const describe = rect => `${rect.left - origin.left},${rect.top - origin.top},${rect.width},${rect.height}`;
+ const geometry = [...root.querySelectorAll("*")].map(element => describe(element.getBoundingClientRect()));
+ const textNodes = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
+ while (textNodes.nextNode()) {
+ const range = document.createRange();
+ range.selectNodeContents(textNodes.currentNode);
+ geometry.push(...[...range.getClientRects()].map(describe));
+ }
+ return geometry.join(" ");
+ }
+
+ function matchesFreshLayout(root) {
+ const fresh = root.cloneNode(true);
+ root.after(fresh);
+ const matches = geometryOf(root) === geometryOf(fresh);
+ fresh.remove();
+ return matches;
+ }
+
+ promiseTest(async () => {
+ if (document.readyState !== "complete") {
+ await new Promise(resolve => window.addEventListener("load", resolve, { once: true }));
+ }
+ await document.fonts.ready;
+ const byId = id => document.getElementById(id);
+ const output = [];
+ const step = (name, rootId, watchedIds, mutate) => {
+ const work = layoutWork(mutate);
+ const reusedBoxes = work.boxes(event => event.event === "reuse in-flow subtree");
+ const isReused = id => reusedBoxes.some(box => box.endsWith(`#${id}`));
+ const reused = watchedIds.filter(isReused);
+ const laidOutAgain = watchedIds.filter(id => !isReused(id));
+ output.push(
+ `${name}: reused [${reused.join(" ")}], laid out again [${laidOutAgain.join(" ")}], ` +
+ `${matchesFreshLayout(byId(rootId)) ? "matches" : "DIFFERS FROM"} a fresh layout`
+ );
+ };
+
+ step("a box below every float moves", "below", ["below-beside", "below-last"], () => {
+ byId("below-spacer").style.height = "40px";
+ });
+ step("a box beside an unchanged float stays", "beside", ["beside-first", "beside-second"], () => {
+ byId("beside-second").firstChild.data = "one two three five";
+ });
+ step("boxes move with their float", "together", ["together-first", "together-last"], () => {
+ byId("together-spacer").style.height = "20px";
+ });
+ step("a later box in a nested flow", "nested", ["nested-first", "nested-last"], () => {
+ byId("nested-edited").firstChild.data = "one three";
+ });
+ step("a box that places a float", "places-float", ["places-float-box", "places-float-beside"], () => {
+ byId("places-float-edited").firstChild.data = "one three";
+ });
+ step("a float grows", "grow", ["grow-beside", "grow-below"], () => {
+ byId("grow-float").style.height = "35px";
+ });
+ step("a float is removed", "removed", ["removed-beside"], () => {
+ byId("removed-float").remove();
+ });
+ step("a box moves into the reach of a float", "into-reach", ["into-reach-box"], () => {
+ byId("into-reach-spacer").style.height = "10px";
+ });
+ step("only the intrusion of a float changes", "intrusion", ["intrusion-beside"], () => {
+ byId("intrusion-float").style.marginRight = "-10px";
+ });
+ step("contents above their box read a float", "above-box", ["above-box-box"], () => {
+ byId("above-box-float").style.width = "60px";
+ });
+ step("clearance after a negative flow offset", "negative-flow", ["negative-flow-box"], () => {
+ byId("negative-flow-float").style.height = "70px";
+ });
+ step("clearance past a float without inline size", "zero-width", ["zero-width-box"], () => {
+ byId("zero-width-float").style.height = "60px";
+ });
+ step("a margin before a box whose contents clear", "margin-clear", ["margin-clear-box"], () => {
+ byId("margin-clear-before").style.marginBottom = "0";
+ });
+ step("a margin before a list item", "margin-marker", ["margin-marker-box"], () => {
+ byId("margin-marker-before").style.marginBottom = "0";
+ });
+ step("floats move above the root", "negative-root", ["negative-root-box"], () => {
+ byId("negative-root-wrapper").style.marginTop = "-100px";
+ });
+ step("a box not stored still reads for its parent", "not-stored", ["not-stored-box"], () => {
+ byId("not-stored-float").style.width = "60px";
+ });
+ step("a reused box inside a box laid out again", "replay", ["replay-box", "replay-reused"], () => {
+ byId("replay-spacer").style.height = "21px";
+ });
+ step("then a float grows into the reused box", "replay", ["replay-box", "replay-reused"], () => {
+ byId("replay-float").style.height = "45px";
+ });
+ step("a float widens beside a formatting context", "own-context", ["own-context-box"], () => {
+ byId("own-context-float").style.width = "60px";
+ });
+ step("clearance in root inline content", "inline-clear", [], () => {
+ byId("inline-clear").firstChild.data = "three";
+ });
+ step("a clearing float in root inline content", "inline-float", [], () => {
+ byId("inline-float-span").firstChild.data = "three";
+ });
+ step("the root widens around a fixed box", "root-width-wrapped", ["root-width-wrapped-last"], () => {
+ byId("root-width-wrapped").style.width = "300px";
+ });
+ step("the root widens around a float in it", "root-width", ["root-width-beside"], () => {
+ byId("root-width").style.width = "300px";
+ });
+
+ for (const line of output)
+ println(line);
+ });
+</script>