Skip to content

Truly independent web browser

LibWeb: Read floats the same wherever a flow starts

Aliaksandr Kalenik1db002bfe27d
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index be61c29013d..d0b81d7ed9e 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -190,8 +190,8 @@ impl LaidOutBlockLevelBox {
pub(crate) struct StateBeforeFindingLineBreaks {
float_count: usize,
bands: SmallVec<[FloatBand; 4]>,
- lowest_left_margin_edge: CssPixels,
- lowest_right_margin_edge: CssPixels,
+ lowest_left_margin_edge: Option<CssPixels>,
+ lowest_right_margin_edge: Option<CssPixels>,
margin_state: BlockMarginState,
}
@@ -679,6 +679,17 @@ pub(crate) struct FloatBand {
pub(crate) right_intrusion: Option<CssPixels>,
}
+impl FloatBand {
+ /// The first band of a flow, which starts above every position in the flow and which no float touches until one is
+ /// placed at its start.
+ fn above_every_float() -> Self {
+ Self {
+ block_start: CssPixels::from_raw(i32::MIN),
+ ..Self::default()
+ }
+ }
+}
+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct FloatPlacement {
block_start: CssPixels,
@@ -746,8 +757,8 @@ pub(crate) struct BlockFormattingContext<'pass> {
outside_marker_line_box_block_size_of_root: Cell<Option<CssPixels>>,
floats: RefCell<Vec<FloatingBox>>,
bands: RefCell<SmallVec<[FloatBand; 4]>>,
- lowest_left_margin_edge: Cell<CssPixels>,
- lowest_right_margin_edge: Cell<CssPixels>,
+ lowest_left_margin_edge: Cell<Option<CssPixels>>,
+ lowest_right_margin_edge: Cell<Option<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>,
@@ -907,9 +918,9 @@ impl<'pass> BlockFormattingContext<'pass> {
outside_marker_line: Cell::new(None),
outside_marker_line_box_block_size_of_root: Cell::new(None),
floats: RefCell::new(Vec::new()),
- bands: RefCell::new(smallvec![FloatBand::default()]),
- lowest_left_margin_edge: Cell::new(CssPixels::default()),
- lowest_right_margin_edge: Cell::new(CssPixels::default()),
+ bands: RefCell::new(smallvec![FloatBand::above_every_float()]),
+ lowest_left_margin_edge: Cell::new(None),
+ lowest_right_margin_edge: Cell::new(None),
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()),
@@ -1982,12 +1993,7 @@ impl<'pass> BlockFormattingContext<'pass> {
block_size_is_unresolved: bool,
) -> FloatBand {
let mut band = self.merged_band(border_box_block_start_in_root, border_box_block_end_in_root);
- // NB: A content edge above the first band has no floats at it; a query there would return the first band.
- let first_band_block_start = self.bands.borrow().first().map(|band| band.block_start);
- if !block_size_is_unresolved
- || border_box_block_end_in_root > content_block_start_in_root
- || first_band_block_start.is_some_and(|block_start| content_block_start_in_root < block_start)
- {
+ if !block_size_is_unresolved || border_box_block_end_in_root > content_block_start_in_root {
return band;
}
let at_content_edge = self.merged_band(content_block_start_in_root, content_block_start_in_root);
@@ -2096,11 +2102,7 @@ impl<'pass> BlockFormattingContext<'pass> {
return;
}
if bands[index].block_start > block_start {
- let mut band = if index == 0 {
- FloatBand::default()
- } else {
- bands[index - 1]
- };
+ let mut band = bands[index - 1];
band.block_start = block_start;
bands.insert(index, band);
return;
@@ -2121,10 +2123,10 @@ impl<'pass> BlockFormattingContext<'pass> {
let block_end = margin_box_rect.bottom();
if floating_box.side == FloatSide::Left {
self.lowest_left_margin_edge
- .set(self.lowest_left_margin_edge.get().max(block_end));
+ .set(self.lowest_left_margin_edge.get().max(Some(block_end)));
} else {
self.lowest_right_margin_edge
- .set(self.lowest_right_margin_edge.get().max(block_end));
+ .set(self.lowest_right_margin_edge.get().max(Some(block_end)));
}
if block_end <= block_start {
return;
@@ -2156,10 +2158,10 @@ impl<'pass> BlockFormattingContext<'pass> {
{
let mut bands = self.bands.borrow_mut();
bands.clear();
- bands.push(FloatBand::default());
+ bands.push(FloatBand::above_every_float());
}
- self.lowest_left_margin_edge.set(CssPixels::default());
- self.lowest_right_margin_edge.set(CssPixels::default());
+ self.lowest_left_margin_edge.set(None);
+ self.lowest_right_margin_edge.set(None);
let floats = self.floats.borrow();
for &floating_box in floats.iter() {
self.add_float_to_bands(floating_box);
@@ -2463,10 +2465,10 @@ impl<'pass> BlockFormattingContext<'pass> {
);
}
let mut result = false;
- let clear_to = |clearance_block_offset_in_root: CssPixels, result: &mut bool| {
- if clearance_block_offset_in_root == CssPixels::default() {
+ let clear_to = |lowest_margin_edge_in_root: Option<CssPixels>, result: &mut bool| {
+ let Some(clearance_block_offset_in_root) = lowest_margin_edge_in_root else {
return;
- }
+ };
// NOTE: Floating boxes are globally relevant within this BFC, *but* their offset coordinates
// are relative to their containing block.
// This means that we have to first convert to a root-space block offset before clearing,
@@ -3867,15 +3869,14 @@ impl<'pass> BlockFormattingContext<'pass> {
.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 {
+ if lowest_margin_edge.is_none_or(|edge| 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(
+ self.floats_in_reach_of_contents(
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)
+ ) == floats_read.floats_in_reach
}
fn note_floats_read_by_reused_contents(
@@ -3899,16 +3900,15 @@ impl<'pass> BlockFormattingContext<'pass> {
}
}
- fn floats_in_reach_of_contents_unless_above_root(
+ fn floats_in_reach_of_contents(
&self,
read_start_from_border_box: CssPixels,
border_box_block_start: CssPixels,
content_inline_start: CssPixels,
- ) -> Option<SmallVec<[FloatInReach; 2]>> {
+ ) -> 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
+ self.floats
.borrow()
.iter()
.filter_map(|floating_box| {
@@ -3932,11 +3932,7 @@ impl<'pass> BlockFormattingContext<'pass> {
- 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)
+ .collect()
}
fn flow_position(&self) -> CssPixels {
@@ -4082,19 +4078,13 @@ impl<'pass> BlockFormattingContext<'pass> {
+ 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(),
- };
+ let floats_in_reach = read_start_from_border_box.map_or_else(SmallVec::new, |read_start_from_border_box| {
+ self.floats_in_reach_of_contents(
+ read_start_from_border_box,
+ border_box_block_start,
+ containing_block_position_in_root.x + content_offset.x,
+ )
+ });
Some(in_flow_block_cache::FloatsReadByContents {
margins_before_box,
border_box_offset_from_flow_position,
@@ -5870,13 +5860,15 @@ impl<'pass> BlockFormattingContext<'pass> {
};
// 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) {
- margin_box_ceiling =
- margin_box_ceiling.max(self.lowest_left_margin_edge.get() - containing_block_rect_now.y);
+ if matches!(style.clear(), clear::LEFT | clear::BOTH | clear::INLINE_START)
+ && let Some(lowest_left_margin_edge) = self.lowest_left_margin_edge.get()
+ {
+ margin_box_ceiling = margin_box_ceiling.max(lowest_left_margin_edge - containing_block_rect_now.y);
}
- if matches!(style.clear(), clear::RIGHT | clear::BOTH | clear::INLINE_END) {
- margin_box_ceiling =
- margin_box_ceiling.max(self.lowest_right_margin_edge.get() - containing_block_rect_now.y);
+ if matches!(style.clear(), clear::RIGHT | clear::BOTH | clear::INLINE_END)
+ && let Some(lowest_right_margin_edge) = self.lowest_right_margin_edge.get()
+ {
+ margin_box_ceiling = margin_box_ceiling.max(lowest_right_margin_edge - containing_block_rect_now.y);
}
let mut ceiling_in_root = containing_block_rect_now.y + margin_box_ceiling;
if let Some(last) = self.floats.borrow().last().copied() {
diff --git a/Tests/LibWeb/Text/expected/floats-above-block-formatting-context-root.txt b/Tests/LibWeb/Text/expected/floats-above-block-formatting-context-root.txt
new file mode 100644
index 00000000000..da2a8b854c8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/floats-above-block-formatting-context-root.txt
@@ -0,0 +1,6 @@
+box-above-float: x=0, y=-20
+box-above-float-above-root: x=0, y=-60
+clear-past-float-above-root: x=0, y=-30
+clearing-float-above-root: x=0, y=-30
+clear-without-floats: x=0, y=-40
+clearing-float-without-floats: x=0, y=-40
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 a98816e4363..97c1b49b1cc 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
@@ -12,7 +12,7 @@ clearance after a negative flow offset: reused [], laid out again [negative-flow
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
+floats move above the root: reused [negative-root-box], laid out again [], 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
diff --git a/Tests/LibWeb/Text/input/floats-above-block-formatting-context-root.html b/Tests/LibWeb/Text/input/floats-above-block-formatting-context-root.html
new file mode 100644
index 00000000000..aa9cc64a9a6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/floats-above-block-formatting-context-root.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ body { margin: 0; font: 10px/10px SerenitySans; }
+ .scenario { display: flow-root; position: relative; width: 200px; height: 100px; margin-top: 200px; }
+ .float { float: left; width: 20px; }
+</style>
+<div class="scenario" id="box-above-float">
+ <div class="float" style="height: 20px"></div>
+ <div id="box-above-float-probe" style="overflow: hidden; height: 0; width: 50px; margin-top: -20px"></div>
+</div>
+<div class="scenario" id="box-above-float-above-root">
+ <div style="height: 10px; margin-top: -50px"></div>
+ <div class="float" style="height: 10px"></div>
+ <div id="box-above-float-above-root-probe" style="overflow: hidden; height: 0; width: 50px; margin-top: -20px"></div>
+</div>
+<div class="scenario" id="clear-past-float-above-root">
+ <div style="height: 10px; margin-top: -50px"></div>
+ <div class="float" style="height: 10px"></div>
+ <div id="clear-past-float-above-root-probe" style="clear: left; height: 10px"></div>
+</div>
+<div class="scenario" id="clearing-float-above-root">
+ <div style="height: 10px; margin-top: -50px"></div>
+ <div class="float" style="height: 10px"></div>
+ <div id="clearing-float-above-root-probe" class="float" style="clear: left; height: 10px"></div>
+</div>
+<div class="scenario" id="clear-without-floats">
+ <div style="height: 10px; margin-top: -50px"></div>
+ <div id="clear-without-floats-probe" style="clear: left; height: 10px"></div>
+</div>
+<div class="scenario" id="clearing-float-without-floats">
+ <div style="height: 10px; margin-top: -50px"></div>
+ <div id="clearing-float-without-floats-probe" class="float" style="clear: left; height: 10px"></div>
+</div>
+<script>
+ test(() => {
+ for (const scenario of document.querySelectorAll(".scenario")) {
+ const origin = scenario.getBoundingClientRect();
+ const probe = document.getElementById(`${scenario.id}-probe`).getBoundingClientRect();
+ println(`${scenario.id}: x=${probe.left - origin.left}, y=${probe.top - origin.top}`);
+ }
+ });
+</script>