Skip to content

Truly independent web browser

LibWeb: Clear floats on the other side when placing a float among blocks

Aliaksandr Kalenikd0f5ff52369b
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 3c241fe85e2..aed26c5e5ac 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -5277,11 +5277,13 @@ impl<'pass> BlockFormattingContext<'pass> {
} else {
block_offset
};
- if side == FloatSide::Left && matches!(style.clear(), clear::LEFT | clear::BOTH | clear::INLINE_START) {
+ // 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 side == FloatSide::Right && matches!(style.clear(), clear::RIGHT | clear::BOTH | clear::INLINE_END) {
+ 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);
}
diff --git a/Tests/LibWeb/Text/expected/css/float-clears-floats-on-the-other-side.txt b/Tests/LibWeb/Text/expected/css/float-clears-floats-on-the-other-side.txt
new file mode 100644
index 00000000000..50118e06020
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/float-clears-floats-on-the-other-side.txt
@@ -0,0 +1,3 @@
+clears-left: 50px below the top of its container
+clears-right: 50px below the top of its container
+clears-both: 50px below the top of its container
diff --git a/Tests/LibWeb/Text/input/css/float-clears-floats-on-the-other-side.html b/Tests/LibWeb/Text/input/css/float-clears-floats-on-the-other-side.html
new file mode 100644
index 00000000000..6f1f605850a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/float-clears-floats-on-the-other-side.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .container { display: flow-root; width: 200px; }
+ .container > div { width: 50px; height: 50px; }
+</style>
+<!-- No white space between the floats, so they are block-level siblings. -->
+<div class="container"><div style="float: left"></div><div id="clears-left" style="float: right; clear: left"></div></div>
+<div class="container"><div style="float: right"></div><div id="clears-right" style="float: left; clear: right"></div></div>
+<div class="container"><div style="float: left"></div><div id="clears-both" style="float: right; clear: both"></div></div>
+<script>
+ test(() => {
+ for (const id of ["clears-left", "clears-right", "clears-both"]) {
+ const float = document.getElementById(id);
+ const offset = float.getBoundingClientRect().top - float.parentElement.getBoundingClientRect().top;
+ println(`${id}: ${offset}px below the top of its container`);
+ }
+ });
+</script>