2 * Copyright (C) 2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "BlockFormattingContext.h"
29 #if ENABLE(LAYOUT_FORMATTING_CONTEXT)
31 #include "BlockFormattingState.h"
32 #include "DisplayBox.h"
33 #include "FloatingContext.h"
34 #include "FloatingState.h"
35 #include "LayoutBox.h"
36 #include "LayoutContainer.h"
37 #include "LayoutContext.h"
38 #include <wtf/IsoMallocInlines.h>
43 WTF_MAKE_ISO_ALLOCATED_IMPL(BlockFormattingContext);
45 BlockFormattingContext::BlockFormattingContext(const Box& formattingContextRoot, LayoutContext& layoutContext)
46 : FormattingContext(formattingContextRoot, layoutContext)
50 void BlockFormattingContext::layout(LayoutContext& layoutContext, FormattingState& formattingState) const
52 // 9.4.1 Block formatting contexts
53 // In a block formatting context, boxes are laid out one after the other, vertically, beginning at the top of a containing block.
54 // The vertical distance between two sibling boxes is determined by the 'margin' properties.
55 // Vertical margins between adjacent block-level boxes in a block formatting context collapse.
56 if (!is<Container>(root()))
58 auto& formattingRoot = downcast<Container>(root());
59 LayoutQueue layoutQueue;
60 FloatingContext floatingContext(formattingState.floatingState());
61 // This is a post-order tree traversal layout.
62 // 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.
63 if (auto* firstChild = formattingRoot.firstInFlowOrFloatingChild())
64 layoutQueue.append(std::make_unique<LayoutPair>(LayoutPair {*firstChild, layoutContext.createDisplayBox(*firstChild)}));
65 // 1. Go all the way down to the leaf node
66 // 2. Compute static position and width as we traverse down
67 // 3. As we climb back on the tree, compute height and finialize position
68 // (Any subtrees with new formatting contexts need to layout synchronously)
69 while (!layoutQueue.isEmpty()) {
70 // Traverse down on the descendants and compute width/static position until we find a leaf node.
72 auto& layoutPair = *layoutQueue.last();
73 auto& layoutBox = layoutPair.layoutBox;
74 auto& displayBox = layoutPair.displayBox;
76 computeWidth(layoutBox, displayBox);
77 computeStaticPosition(layoutBox, layoutPair.displayBox);
78 if (layoutBox.establishesFormattingContext()) {
79 auto formattingContext = layoutContext.formattingContext(layoutBox);
80 formattingContext->layout(layoutContext, layoutContext.establishedFormattingState(layoutBox, *formattingContext));
83 if (!is<Container>(layoutBox) || !downcast<Container>(layoutBox).hasInFlowOrFloatingChild())
85 auto& firstChild = *downcast<Container>(layoutBox).firstInFlowOrFloatingChild();
86 layoutQueue.append(std::make_unique<LayoutPair>(LayoutPair {firstChild, layoutContext.createDisplayBox(firstChild)}));
89 // Climb back on the ancestors and compute height/final position.
90 while (!layoutQueue.isEmpty()) {
91 // All inflow descendants (if there are any) are laid out by now. Let's compute the box's height.
92 auto layoutPair = layoutQueue.takeLast();
93 auto& layoutBox = layoutPair->layoutBox;
94 auto& displayBox = layoutPair->displayBox;
96 computeHeight(layoutBox, displayBox);
97 // Adjust position now that we have all the previous floats placed in this context -if needed.
98 floatingContext.computePosition(layoutBox, displayBox);
99 if (!is<Container>(layoutBox))
101 auto& container = downcast<Container>(layoutBox);
102 // Move in-flow positioned children to their final position.
103 placeInFlowPositionedChildren(container);
104 if (auto* nextSibling = container.nextInFlowOrFloatingSibling()) {
105 layoutQueue.append(std::make_unique<LayoutPair>(LayoutPair {*nextSibling, layoutContext.createDisplayBox(*nextSibling)}));
110 // Place the inflow positioned children.
111 placeInFlowPositionedChildren(formattingRoot);
112 // And take care of out-of-flow boxes as the final step.
113 layoutOutOfFlowDescendants();
116 std::unique_ptr<FormattingState> BlockFormattingContext::createFormattingState(Ref<FloatingState>&& floatingState) const
118 return std::make_unique<BlockFormattingState>(WTFMove(floatingState));
121 Ref<FloatingState> BlockFormattingContext::createOrFindFloatingState() const
123 // Block formatting context always establishes a new floating state.
124 return FloatingState::create();
127 void BlockFormattingContext::computeStaticPosition(const Box&, Display::Box&) const
131 void BlockFormattingContext::computeInFlowWidth(const Box&, Display::Box&) const
135 void BlockFormattingContext::computeInFlowHeight(const Box&, Display::Box&) const
139 LayoutUnit BlockFormattingContext::marginTop(const Box&) const
144 LayoutUnit BlockFormattingContext::marginBottom(const Box&) const