[LFC] Add FormattingContext::computeWidth/computeHeight logic.
[WebKit-https.git] / Source / WebCore / layout / blockformatting / BlockFormattingContext.cpp
index d37e181..0809426 100644 (file)
 #if ENABLE(LAYOUT_FORMATTING_CONTEXT)
 
 #include "BlockFormattingState.h"
+#include "FloatingContext.h"
+#include "FloatingState.h"
+#include "LayoutBox.h"
+#include "LayoutContainer.h"
+#include "LayoutContext.h"
 #include <wtf/IsoMallocInlines.h>
 
 namespace WebCore {
@@ -36,29 +41,90 @@ namespace Layout {
 
 WTF_MAKE_ISO_ALLOCATED_IMPL(BlockFormattingContext);
 
-BlockFormattingContext::BlockFormattingContext(Box& formattingContextRoot)
-    : FormattingContext(formattingContextRoot)
+BlockFormattingContext::BlockFormattingContext(const Box& formattingContextRoot, LayoutContext& layoutContext)
+    : FormattingContext(formattingContextRoot, layoutContext)
 {
 }
 
-void BlockFormattingContext::layout(FormattingState&)
+void BlockFormattingContext::layout(LayoutContext& layoutContext, FormattingState& formattingState) const
 {
+    // 9.4.1 Block formatting contexts
+    // In a block formatting context, boxes are laid out one after the other, vertically, beginning at the top of a containing block.
+    // The vertical distance between two sibling boxes is determined by the 'margin' properties.
+    // Vertical margins between adjacent block-level boxes in a block formatting context collapse.
+    if (!is<Container>(root()))
+        return;
+    auto& formattingRoot = downcast<Container>(root());
+    Vector<const Box*> layoutQueue;
+    FloatingContext floatingContext(formattingState.floatingState());
+    // This is a post-order tree traversal layout.
+    // The root container layout is done in the formatting context it lives in, not that one it creates, so let's start with the first child.
+    if (formattingRoot.hasInFlowOrFloatingChild())
+        layoutQueue.append(formattingRoot.firstInFlowOrFloatingChild());
+    // 1. Go all the way down to the leaf node
+    // 2. Compute static position and width as we traverse down
+    // 3. As we climb back on the tree, compute height and finialize position
+    // (Any subtrees with new formatting contexts need to layout synchronously)
+    while (!layoutQueue.isEmpty()) {
+        // Traverse down on the descendants and compute width/static position until we find a leaf node.
+        while (true) {
+            auto& layoutBox = *layoutQueue.last();
+            computeWidth(layoutBox);
+            computeStaticPosition(layoutBox);
+            if (layoutBox.establishesFormattingContext()) {
+                auto formattingContext = layoutContext.formattingContext(layoutBox);
+                formattingContext->layout(layoutContext, layoutContext.establishedFormattingState(layoutBox, *formattingContext));
+                break;
+            }
+            if (!is<Container>(layoutBox) || !downcast<Container>(layoutBox).hasInFlowOrFloatingChild())
+                break;
+            layoutQueue.append(downcast<Container>(layoutBox).firstInFlowOrFloatingChild());
+        }
+
+        // Climb back on the ancestors and compute height/final position.
+        while (!layoutQueue.isEmpty()) {
+            // All inflow descendants (if there are any) are laid out by now. Let's compute the box's height.
+            auto& layoutBox = *layoutQueue.takeLast();
+            computeHeight(layoutBox);
+            // Adjust position now that we have all the previous floats placed in this context -if needed.
+            floatingContext.computePosition(layoutBox);
+            if (!is<Container>(layoutBox))
+                continue;
+            auto& container = downcast<Container>(layoutBox);
+            // Move in-flow positioned children to their final position.
+            placeInFlowPositionedChildren(container);
+            if (auto* nextSibling = container.nextInFlowOrFloatingSibling()) {
+                layoutQueue.append(nextSibling);
+                break;
+            }
+        }
+    }
+    // Place the inflow positioned children.
+    placeInFlowPositionedChildren(formattingRoot);
+    // And take care of out-of-flow boxes as the final step.
+    layoutOutOfFlowDescendants();
+}
+
+std::unique_ptr<FormattingState> BlockFormattingContext::createFormattingState(Ref<FloatingState>&& floatingState) const
+{
+    return std::make_unique<BlockFormattingState>(WTFMove(floatingState));
 }
 
-std::unique_ptr<FormattingState> BlockFormattingContext::formattingState() const
+Ref<FloatingState> BlockFormattingContext::createOrFindFloatingState() const
 {
-    return std::make_unique<BlockFormattingState>();
+    // Block formatting context always establishes a new floating state.
+    return FloatingState::create();
 }
 
 void BlockFormattingContext::computeStaticPosition(const Box&) const
 {
 }
 
-void BlockFormattingContext::computeWidth(const Box&) const
+void BlockFormattingContext::computeInFlowWidth(const Box&) const
 {
 }
 
-void BlockFormattingContext::computeHeight(const Box&) const
+void BlockFormattingContext::computeInFlowHeight(const Box&) const
 {
 }