Skip to content

Truly independent web browser

LibWeb: Place a float-avoiding box from its right edge in RTL

sideshowbarkerb60895341c68
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 2e8e874e9a1..2bc9cbbd38e 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -1119,16 +1119,17 @@ impl<'pass> BlockFormattingContext<'pass> {
box_avoids_floats_because_it_establishes_formatting_context(&self.run.callbacks, node)
}
- // The definite inline space left for this box after the float bands that
- // intrude at its position are subtracted, including the negative-margin
- // adjustment; None when the space is not definite or the box may overlap
- // floats. This is the parent-owned half of block-level inline sizing —
- // it needs the flow position and the live float bands.
+ /// The definite inline space left for this box after the float bands that intrude at its position are subtracted,
+ /// including the negative-margin adjustment; None when the space is not definite or the box may overlap floats.
+ /// This is the parent-owned half of block-level inline sizing — it needs the flow position and the live float
+ /// bands. With `measure_over_border_box`, `content_position_in_root` is the box's own content position, and the
+ /// floats beside its border box count, as the check that moves it below them measures them.
fn float_reduced_inline_opportunity(
&self,
sized_child: ChildSizedHere<'_>,
available_space: AvailableSpace,
content_position_in_root: FfiCssPixelPoint,
+ measure_over_border_box: bool,
) -> Option<CssPixels> {
let node = sized_child.node;
if !matches!(available_space.inline_size, AvailableSize::Definite(_))
@@ -1138,14 +1139,33 @@ impl<'pass> BlockFormattingContext<'pass> {
}
let style = self.style(node);
let available_inline_size = available_space.inline_size.to_px_or_zero();
- let box_in_root_rect = CssPixelRect {
- x: content_position_in_root.x,
- y: content_position_in_root.y,
- width: available_inline_size,
- height: self.metrics_of(sized_child).content_block_size(),
+ let metrics = self.metrics_of(sized_child);
+ let box_in_root_rect = if measure_over_border_box {
+ CssPixelRect {
+ x: content_position_in_root.x,
+ y: content_position_in_root.y - metrics.border_box_top(false),
+ width: available_inline_size,
+ height: metrics.border_box_block_size(false),
+ }
+ } else {
+ CssPixelRect {
+ x: content_position_in_root.x,
+ y: content_position_in_root.y,
+ width: available_inline_size,
+ height: metrics.content_block_size(),
+ }
};
// NB: The box avoids every float beside it, so this takes the deepest intrusions over its block size.
- let band = self.merged_band(box_in_root_rect.y, box_in_root_rect.bottom());
+ let band = if measure_over_border_box {
+ self.band_beside_border_box(
+ box_in_root_rect.y,
+ box_in_root_rect.bottom(),
+ content_position_in_root.y,
+ !metrics.has_definite_block_size(),
+ )
+ } else {
+ self.merged_band(box_in_root_rect.y, box_in_root_rect.bottom())
+ };
let intrusion = self.intrusions_for_band_into_rect(band, box_in_root_rect);
if intrusion.left <= CssPixels::default() && intrusion.right <= CssPixels::default() {
return Some(available_inline_size);
@@ -1172,7 +1192,7 @@ impl<'pass> BlockFormattingContext<'pass> {
content_position_in_root: FfiCssPixelPoint,
) {
let float_avoidance_inline_size =
- self.float_reduced_inline_opportunity(sized_child, available_space, content_position_in_root);
+ self.float_reduced_inline_opportunity(sized_child, available_space, content_position_in_root, false);
let content_inline_size = self.resolve_root_inline_metrics_and_content_size(
sized_child,
available_space,
@@ -1726,6 +1746,31 @@ impl<'pass> BlockFormattingContext<'pass> {
}
/// The deepest intrusion on each side of the bands between the two block offsets.
+ /// The floats beside the border box of a box that avoids floats. While the box's block size is unresolved and its
+ /// border box ends at its content edge, the floats at that edge count too, since the box's content may still reach
+ /// the floats that start there.
+ fn band_beside_border_box(
+ &self,
+ border_box_block_start_in_root: CssPixels,
+ border_box_block_end_in_root: CssPixels,
+ content_block_start_in_root: CssPixels,
+ 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)
+ {
+ return band;
+ }
+ let at_content_edge = self.merged_band(content_block_start_in_root, content_block_start_in_root);
+ band.left_intrusion = band.left_intrusion.max(at_content_edge.left_intrusion);
+ band.right_intrusion = band.right_intrusion.max(at_content_edge.right_intrusion);
+ band
+ }
+
fn merged_band(&self, block_start_in_root: CssPixels, block_end_in_root: CssPixels) -> FloatBand {
let bands = self.bands.borrow();
assert!(!bands.is_empty());
@@ -1942,22 +1987,67 @@ impl<'pass> BlockFormattingContext<'pass> {
self.rebuild_float_bands();
}
+ /// The left edge of the border box of a box that avoids floats, in its containing block's content box.
fn border_box_left_of_box_avoiding_floats(
&self,
node: Node,
metrics: &impl BoxMetricCells,
space_used_by_floats: inline_formatting_context::SpaceUsedByFloats,
+ border_box_inline_size: CssPixels,
+ available_inline_size: CssPixels,
+ ) -> CssPixels {
+ let style = self.style(node);
+ // NB: A box placed from its right edge has its right margin and the floats on the right decide where it goes, as
+ // the left ones do for a box placed from its left edge.
+ if self.box_avoiding_floats_is_placed_from_right(node) {
+ let border_box_right_inset = Self::border_box_inset_beside_floats(
+ style.margin_right().is_auto(),
+ metrics.margin_right(),
+ space_used_by_floats.right,
+ space_used_by_floats.left,
+ );
+ return available_inline_size - border_box_right_inset - border_box_inline_size;
+ }
+ Self::border_box_inset_beside_floats(
+ style.margin_left().is_auto(),
+ metrics.margin_left(),
+ space_used_by_floats.left,
+ space_used_by_floats.right,
+ )
+ }
+
+ /// Whether a box that avoids floats is placed from the right edge of its containing block: In a right-to-left one,
+ /// unless a legacy alignment value (from an HTML align attribute, e.g.) on the containing block places it instead.
+ fn box_avoiding_floats_is_placed_from_right(&self, node: Node) -> bool {
+ let containing_block_style = self.style(self.containing_block(node));
+ containing_block_style.direction() == direction::RTL
+ && !matches!(
+ containing_block_style.text_align(),
+ text_align::_LIBWEB_CENTER
+ | text_align::_LIBWEB_INHERIT_OR_CENTER
+ | text_align::_LIBWEB_LEFT
+ | text_align::_LIBWEB_RIGHT
+ )
+ }
+
+ /// How far the border box of a box that avoids floats sits from the edge of its containing block it's placed from,
+ /// given its margin on that side and the floats intruding there: A margin can overlap the floats on its own side.
+ fn border_box_inset_beside_floats(
+ margin_is_auto: bool,
+ margin: CssPixels,
+ intrusion_on_this_side: CssPixels,
+ intrusion_on_other_side: CssPixels,
) -> CssPixels {
- if self.style(node).margin_left().is_auto() {
- return space_used_by_floats.left + metrics.margin_left();
+ if margin_is_auto {
+ return intrusion_on_this_side + margin;
}
- if metrics.margin_left() >= CssPixels::default() {
- return space_used_by_floats.left.max(metrics.margin_left());
+ if margin >= CssPixels::default() {
+ return intrusion_on_this_side.max(margin);
}
- if space_used_by_floats.left > CssPixels::default() || space_used_by_floats.right > CssPixels::default() {
- return space_used_by_floats.left;
+ if intrusion_on_this_side > CssPixels::default() || intrusion_on_other_side > CssPixels::default() {
+ return intrusion_on_this_side;
}
- space_used_by_floats.left + metrics.margin_left()
+ intrusion_on_this_side + margin
}
fn avoid_float_intrusions(
@@ -1993,14 +2083,30 @@ impl<'pass> BlockFormattingContext<'pass> {
..containing_block_rect_in_root
};
let space = self.intrusions_for_band_into_rect(
- self.merged_band(border_box_block_offset_in_root, band_rect.bottom()),
+ self.band_beside_border_box(
+ border_box_block_offset_in_root,
+ band_rect.bottom(),
+ border_box_block_offset_in_root + metrics.border_box_top(false),
+ !metrics.has_definite_block_size(),
+ ),
band_rect,
);
let constrained = space.left > CssPixels::default() || space.right > CssPixels::default();
- let border_box_left = self.border_box_left_of_box_avoiding_floats(node, &metrics, space);
+ let available_inline_size = available_space.inline_size.to_px_or_zero();
+ let border_box_left = self.border_box_left_of_box_avoiding_floats(
+ node,
+ &metrics,
+ space,
+ candidate_border_box_inline_size,
+ available_inline_size,
+ );
+ // NB: The box has to clear the floats if its border box is too wide for the space beside them, or if where
+ // it's placed overlaps the floats on either side. A margin may still carry it past an edge with no float.
let mut must_clear = constrained
- && border_box_left + candidate_border_box_inline_size
- > available_space.inline_size.to_px_or_zero() - space.right;
+ && (candidate_border_box_inline_size > available_inline_size - space.left - space.right
+ || (space.left > CssPixels::default() && border_box_left < space.left)
+ || (space.right > CssPixels::default()
+ && border_box_left + candidate_border_box_inline_size > available_inline_size - space.right));
if !must_clear {
let border_rect = CssPixelRect {
x: band_rect.x + border_box_left,
@@ -2031,7 +2137,7 @@ impl<'pass> BlockFormattingContext<'pass> {
};
// Deliberately re-run inline sizing after every band descent,
// without committing: only the winning candidate is committed.
- probe.opportunity = self.float_reduced_inline_opportunity(sized_child, available_space, position);
+ probe.opportunity = self.float_reduced_inline_opportunity(sized_child, available_space, position, true);
probe.content_inline_size = self.resolve_root_inline_metrics_and_content_size(
sized_child,
available_space,
@@ -2166,20 +2272,40 @@ impl<'pass> BlockFormattingContext<'pass> {
let mut inline_offset = CssPixels::default();
let mut available_inline_size_within_containing_block = available_space.inline_size.to_px_or_zero();
if self.box_should_avoid_floats_because_it_establishes_fc(node) {
- let space = self.intrusion_by_floats_into_rect(
- CssPixelRect {
- x: content_position_in_root.x,
- y: content_position_in_root.y,
- width: content_inline_size,
- height: metrics.content_block_size(),
- }
- .into(),
- CssPixels::default(),
- metrics.content_block_size(),
+ // NB: The floats the box avoids are the ones beside its border box, as the check that moves it below them
+ // measures them. And a box placed from the right has the floats on the right measured across the whole
+ // line, not just over its own inline size from the left.
+ let border_box_rect = CssPixelRect {
+ x: content_position_in_root.x,
+ y: content_position_in_root.y - metrics.border_box_top(false),
+ width: if self.box_avoiding_floats_is_placed_from_right(node) {
+ available_inline_size_within_containing_block
+ } else {
+ content_inline_size
+ },
+ height: metrics.border_box_block_size(false),
+ };
+ let space = self.intrusions_for_band_into_rect(
+ self.band_beside_border_box(
+ border_box_rect.y,
+ border_box_rect.bottom(),
+ content_position_in_root.y,
+ !metrics.has_definite_block_size(),
+ ),
+ border_box_rect,
+ );
+ let border_box_inline_size =
+ content_inline_size + metrics.border_box_left(false) + metrics.border_box_right(false);
+ let border_box_left = self.border_box_left_of_box_avoiding_floats(
+ node,
+ &metrics,
+ space,
+ border_box_inline_size,
+ available_inline_size_within_containing_block,
);
available_inline_size_within_containing_block -= space.left + space.right;
// Subtracting the left margin here because it is applied again when the margin box offset is added below.
- inline_offset = self.border_box_left_of_box_avoiding_floats(node, &metrics, space) - metrics.margin_left();
+ inline_offset = border_box_left - metrics.margin_left();
}
let containing_block = self.containing_block(node);
@@ -2787,6 +2913,7 @@ impl<'pass> BlockFormattingContext<'pass> {
sized_child,
available_space,
content_position_in_root_now(content_block_offset),
+ true,
);
let mut probe = FloatAvoidanceProbe {
opportunity,
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 79e2bb69c3f..2a07e38d127 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1321,8 +1321,6 @@ Ref/input/wpt-import/css/CSS2/floats/floats-wrap-bfc-003-right-table.xht
Ref/input/wpt-import/css/CSS2/floats/floats-wrap-bfc-004.xht
Ref/input/wpt-import/css/CSS2/floats/floats-wrap-bfc-006.xht
Ref/input/wpt-import/css/CSS2/floats/floats-wrap-bfc-with-margin-002.tentative.html
-Ref/input/wpt-import/css/CSS2/floats/floats-wrap-bfc-with-margin-003.tentative.html
-Ref/input/wpt-import/css/CSS2/floats/floats-wrap-bfc-with-margin-005.html
Ref/input/wpt-import/css/CSS2/floats/floats-wrap-bfc-with-margin-006.tentative.html
Ref/input/wpt-import/css/CSS2/floats/floats-wrap-top-below-bfc-001r.xht
Ref/input/wpt-import/css/CSS2/floats/floats-zero-height-wrap-002.xht
diff --git a/Tests/LibWeb/Text/expected/float-avoiding-box-beside-floats.txt b/Tests/LibWeb/Text/expected/float-avoiding-box-beside-floats.txt
new file mode 100644
index 00000000000..ccd55592099
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/float-avoiding-box-beside-floats.txt
@@ -0,0 +1,26 @@
+right float, margin-right 30px: left=350 top=0 width=100
+right float, margin-right 80px: left=320 top=0 width=100
+left float, margin-right 30px: left=370 top=0 width=100
+left float, margin-left 80px: left=370 top=0 width=100
+floats on both sides: left=350 top=0 width=100
+right float, negative margin-right: left=350 top=0 width=100
+right float, auto margins: left=175 top=0 width=100
+right float, auto width: left=0 top=0 width=450
+right float, margin-right past the left edge: left=-50 top=0 width=100
+right float, thick top border: left=350 top=0 width=100
+left float, margin-right reaching into it: left=-50 top=30 width=100
+ltr, left float, margin-left past the right edge: left=450 top=0 width=100
+right float, too wide to fit beside it: left=20 top=30 width=480
+right float, thick top border, auto margins: left=175 top=0 width=100
+right float, thick top border, auto width: left=0 top=0 width=450
+ltr, left float, thick top border: left=50 top=0 width=100
+ltr, left float, thick top border, auto width: left=50 top=0 width=450
+ltr, left float, thick top border, auto margins: left=225 top=0 width=100
+ltr, thick top border, auto height, float at its content edge: left=0 top=140 width=100
+ltr, thick top border, zero height, float at its content edge: left=100 top=0 width=100
+ltr, lifted above a float, thick top border, auto height: left=0 top=10 width=100
+ltr, floated stretch canvas below an earlier float: left=0 top=40 width=500
+left float, legacy center alignment: left=175 top=0 width=100
+no floats: left=370 top=0 width=100
+ltr, left float, margin-left 30px: left=50 top=0 width=100
+ltr, right float, margin-left 30px: left=30 top=0 width=100
diff --git a/Tests/LibWeb/Text/input/float-avoiding-box-beside-floats.html b/Tests/LibWeb/Text/input/float-avoiding-box-beside-floats.html
new file mode 100644
index 00000000000..e93e8b5c596
--- /dev/null
+++ b/Tests/LibWeb/Text/input/float-avoiding-box-beside-floats.html
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ body { margin: 0; }
+ .container { direction: rtl; display: flow-root; width: 500px; }
+ .float { width: 50px; height: 30px; }
+ .box { overflow: hidden; width: 100px; height: 10px; }
+</style>
+<!-- A box that establishes a formatting context sits beside the floats. In a right-to-left container, its right margin
+ can overlap a float on the right, as its left margin can overlap a float on the left in a left-to-right one. And
+ the floats it avoids are the ones beside its border box, so a thick top border beside a float counts too. -->
+<div class="container" data-case="right float, margin-right 30px"><div class="float" style="float: right"></div><div class="box" style="margin-left: 20px; margin-right: 30px"></div></div>
+<div class="container" data-case="right float, margin-right 80px"><div class="float" style="float: right"></div><div class="box" style="margin-left: 20px; margin-right: 80px"></div></div>
+<div class="container" data-case="left float, margin-right 30px"><div class="float" style="float: left"></div><div class="box" style="margin-left: 20px; margin-right: 30px"></div></div>
+<div class="container" data-case="left float, margin-left 80px"><div class="float" style="float: left"></div><div class="box" style="margin-left: 80px; margin-right: 30px"></div></div>
+<div class="container" data-case="floats on both sides"><div class="float" style="float: left"></div><div class="float" style="float: right"></div><div class="box" style="margin-left: 20px; margin-right: 30px"></div></div>
+<div class="container" data-case="right float, negative margin-right"><div class="float" style="float: right"></div><div class="box" style="margin-right: -20px"></div></div>
+<div class="container" data-case="right float, auto margins"><div class="float" style="float: right"></div><div class="box" style="margin: 0 auto"></div></div>
+<div class="container" data-case="right float, auto width"><div class="float" style="float: right"></div><div class="box" style="width: auto"></div></div>
+<div class="container" data-case="right float, margin-right past the left edge"><div class="float" style="float: right"></div><div class="box" style="margin-right: 450px"></div></div>
+<div class="container" data-case="right float, thick top border"><div class="float" style="float: right"></div><div class="box" style="border-top: 40px solid transparent"></div></div>
+<div class="container" data-case="left float, margin-right reaching into it"><div class="float" style="float: left"></div><div class="box" style="margin-right: 450px"></div></div>
+<div class="container" style="direction: ltr" data-case="ltr, left float, margin-left past the right edge"><div class="float" style="float: left"></div><div class="box" style="margin-left: 450px"></div></div>
+<div class="container" data-case="right float, too wide to fit beside it"><div class="float" style="float: right"></div><div class="box" style="width: 480px"></div></div>
+<div class="container" data-case="right float, thick top border, auto margins"><div class="float" style="float: right"></div><div class="box" style="border-top: 40px solid transparent; margin: 0 auto"></div></div>
+<div class="container" data-case="right float, thick top border, auto width"><div class="float" style="float: right"></div><div class="box" style="border-top: 40px solid transparent; width: auto"></div></div>
+<div class="container" style="direction: ltr" data-case="ltr, left float, thick top border"><div class="float" style="float: left"></div><div class="box" style="border-top: 40px solid transparent"></div></div>
+<div class="container" style="direction: ltr" data-case="ltr, left float, thick top border, auto width"><div class="float" style="float: left"></div><div class="box" style="border-top: 40px solid transparent; width: auto"></div></div>
+<div class="container" style="direction: ltr" data-case="ltr, left float, thick top border, auto margins"><div class="float" style="float: left"></div><div class="box" style="border-top: 40px solid transparent; margin: 0 auto"></div></div>
+<div class="container" style="direction: ltr" data-case="ltr, thick top border, auto height, float at its content edge"><div class="float" style="float: left; width: 100px; height: 40px"></div><div class="float" style="float: left; width: 450px; height: 100px"></div><div class="box" style="height: auto; border-top: 40px solid transparent"><div style="height: 50px"></div></div></div>
+<div class="container" style="direction: ltr" data-case="ltr, thick top border, zero height, float at its content edge"><div class="float" style="float: left; width: 100px; height: 40px"></div><div class="float" style="float: left; width: 450px; height: 100px"></div><div class="box" style="height: 0; border-top: 40px solid transparent"></div></div>
+<div class="container" style="direction: ltr; height: 120px; padding-top: 110px" data-case="ltr, lifted above a float, thick top border, auto height"><div class="float" style="float: left; width: 450px; height: 30px"></div><div class="box" style="height: auto; margin-top: -100px; border-top: 40px solid transparent"><div style="height: 10px"></div></div></div>
+<div class="container" style="direction: ltr" data-case="ltr, floated stretch canvas below an earlier float"><div class="float" style="float: left"></div><div style="padding-top: 40px"><canvas class="box" style="float: left; width: stretch; border-top: 40px solid"></canvas></div></div>
+<div class="container" align="center" data-case="left float, legacy center alignment"><div class="float" style="float: left"></div><div class="box" style="margin-right: 100px"></div></div>
+<div class="container" data-case="no floats"><div class="box" style="margin-left: 20px; margin-right: 30px"></div></div>
+<div class="container" style="direction: ltr" data-case="ltr, left float, margin-left 30px"><div class="float" style="float: left"></div><div class="box" style="margin-left: 30px"></div></div>
+<div class="container" style="direction: ltr" data-case="ltr, right float, margin-left 30px"><div class="float" style="float: right"></div><div class="box" style="margin-left: 30px; margin-right: 20px"></div></div>
+<script>
+ test(() => {
+ for (const container of document.querySelectorAll(".container")) {
+ const rect = container.querySelector(".box").getBoundingClientRect();
+ const top = rect.top - container.getBoundingClientRect().top;
+ println(`${container.dataset.case}: left=${rect.left} top=${top} width=${rect.width}`);
+ }
+ });
+</script>