Skip to content

Commit 508927c

Browse files
committed
LibWeb: Take floats into account when measuring automatic width of IFC
When there are floats present inside an IFC, we must coordinate with the parent BFC to calculate the automatic width of the IFC's block box. This is because the IFC is not directly aware of floats. Only the BFC knows enough about them to account for them in automatic sizing.
1 parent 610a760 commit 508927c

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Viewport <#document> at (0,0) content-size 800x600 children: not-inline
2+
BlockContainer <html> at (0,0) content-size 800x0 [BFC] children: not-inline
3+
BlockContainer <body> at (8,8) content-size 140x100 positioned [BFC] children: not-inline
4+
BlockContainer <div.black> at (58,58) content-size 40x0 children: inline
5+
BlockContainer <div.green> at (78,78) content-size 0x0 floating [BFC] children: not-inline
6+
TextNode <#text>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html><style>
2+
* {
3+
}
4+
body {
5+
position: absolute;
6+
}
7+
.black {
8+
background: magenta;
9+
border: 50px solid black;
10+
}
11+
.green {
12+
float: left;
13+
border: 20px solid lime;
14+
}
15+
</style><body><div class="black"><div class="green"></div>

Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,13 +985,19 @@ CSSPixels BlockFormattingContext::greatest_child_width(Box const& box) const
985985
CSSPixels width_here = line_box.width();
986986
CSSPixels extra_width_from_left_floats = 0;
987987
for (auto& left_float : m_left_floats.all_boxes) {
988+
// NOTE: Floats directly affect the automatic size of their containing block, but only indirectly anything above in the tree.
989+
if (left_float->box->containing_block() != &box)
990+
continue;
988991
if (line_box.baseline() >= left_float->top_margin_edge.value() || line_box.baseline() <= left_float->bottom_margin_edge.value()) {
989992
auto const& left_float_state = m_state.get(left_float->box);
990993
extra_width_from_left_floats = max(extra_width_from_left_floats, left_float->offset_from_edge + left_float_state.content_width() + left_float_state.margin_box_right());
991994
}
992995
}
993996
CSSPixels extra_width_from_right_floats = 0;
994997
for (auto& right_float : m_right_floats.all_boxes) {
998+
// NOTE: Floats directly affect the automatic size of their containing block, but only indirectly anything above in the tree.
999+
if (right_float->box->containing_block() != &box)
1000+
continue;
9951001
if (line_box.baseline() >= right_float->top_margin_edge.value() || line_box.baseline() <= right_float->bottom_margin_edge.value()) {
9961002
auto const& right_float_state = m_state.get(right_float->box);
9971003
extra_width_from_right_floats = max(extra_width_from_right_floats, right_float->offset_from_edge + right_float_state.margin_box_left());

Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ void InlineFormattingContext::run(Box const&, LayoutMode layout_mode, AvailableS
7777
m_available_space = available_space;
7878
generate_line_boxes(layout_mode);
7979

80-
CSSPixels max_line_width = 0;
8180
CSSPixels content_height = 0;
8281

8382
for (auto& line_box : m_containing_block_state.line_boxes) {
84-
max_line_width = max(max_line_width, line_box.width());
8583
content_height += line_box.height();
8684
}
8785

88-
m_automatic_content_width = max_line_width;
86+
// NOTE: We ask the parent BFC to calculate the automatic content width of this IFC.
87+
// This ensures that any floated boxes are taken into account.
88+
m_automatic_content_width = parent().greatest_child_width(containing_block());
8989
m_automatic_content_height = content_height;
9090
}
9191

0 commit comments

Comments
 (0)