瀏覽代碼

LibWeb: Move box floatation out of normal flow layout

Layout of floating children now places the child in the normal flow and
then floats it left or right afterwards.
Andreas Kling 4 年之前
父節點
當前提交
b638e74b68
共有 1 個文件被更改,包括 24 次插入16 次删除
  1. 24 16
      Libraries/LibWeb/Layout/BlockFormattingContext.cpp

+ 24 - 16
Libraries/LibWeb/Layout/BlockFormattingContext.cpp

@@ -587,22 +587,6 @@ void BlockFormattingContext::place_block_level_non_replaced_element_in_normal_fl
         }
         }
     }
     }
 
 
-    if (box.style().float_() == CSS::Float::Left) {
-        if (!m_left_floating_boxes.is_empty()) {
-            auto& previous_floating_box = *m_left_floating_boxes.last();
-            x = previous_floating_box.effective_offset().x() + previous_floating_box.width();
-        }
-        m_left_floating_boxes.append(&box);
-    } else if (box.style().float_() == CSS::Float::Right) {
-        if (!m_right_floating_boxes.is_empty()) {
-            auto& previous_floating_box = *m_right_floating_boxes.last();
-            x = previous_floating_box.effective_offset().x() - box.width();
-        } else {
-            x = containing_block.width() - box.width();
-        }
-        m_right_floating_boxes.append(&box);
-    }
-
     box.set_offset(x, y);
     box.set_offset(x, y);
 }
 }
 
 
@@ -668,7 +652,31 @@ void BlockFormattingContext::layout_floating_child(Box& box)
     layout_inside(box, LayoutMode::Default);
     layout_inside(box, LayoutMode::Default);
     compute_height(box);
     compute_height(box);
 
 
+    // First we place the box normally (to get the right y coordinate.)
     place_block_level_non_replaced_element_in_normal_flow(box);
     place_block_level_non_replaced_element_in_normal_flow(box);
+
+    // Then we float it to the left or right.
+
+    float x = box.effective_offset().x();
+    float y = box.effective_offset().y();
+
+    if (box.style().float_() == CSS::Float::Left) {
+        if (!m_left_floating_boxes.is_empty()) {
+            auto& previous_floating_box = *m_left_floating_boxes.last();
+            x = previous_floating_box.effective_offset().x() + previous_floating_box.width();
+        }
+        m_left_floating_boxes.append(&box);
+    } else if (box.style().float_() == CSS::Float::Right) {
+        if (!m_right_floating_boxes.is_empty()) {
+            auto& previous_floating_box = *m_right_floating_boxes.last();
+            x = previous_floating_box.effective_offset().x() - box.width();
+        } else {
+            x = context_box().width() - box.width();
+        }
+        m_right_floating_boxes.append(&box);
+    }
+
+    box.set_offset(x, y);
 }
 }
 
 
 void BlockFormattingContext::layout_absolutely_positioned_descendant(Box& box)
 void BlockFormattingContext::layout_absolutely_positioned_descendant(Box& box)