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.
28 #if ENABLE(LAYOUT_FORMATTING_CONTEXT)
30 #include "FormattingContext.h"
31 #include <wtf/IsoMalloc.h>
39 class BlockFormattingState;
41 class FloatingContext;
43 // This class implements the layout logic for block formatting contexts.
44 // https://www.w3.org/TR/CSS22/visuren.html#block-formatting
45 class BlockFormattingContext : public FormattingContext {
46 WTF_MAKE_ISO_ALLOCATED(BlockFormattingContext);
48 BlockFormattingContext(const Box& formattingContextRoot, FormattingState& formattingState);
50 void layout() const override;
53 void layoutFormattingContextRoot(FloatingContext&, const Box&) const;
54 void placeInFlowPositionedChildren(const Container&) const;
56 void computeWidthAndMargin(const Box&) const;
57 void computeHeightAndMargin(const Box&) const;
59 void computeStaticPosition(const Box&) const;
60 void computeFloatingPosition(const FloatingContext&, const Box&) const;
61 void computePositionToAvoidFloats(const FloatingContext&, const Box&) const;
62 void computeVerticalPositionForFloatClear(const FloatingContext&, const Box&) const;
64 void computeEstimatedMarginTopForAncestors(const Box&) const;
65 void computeEstimatedMarginTop(const Box&) const;
67 void precomputeVerticalPositionForFormattingRootIfNeeded(const Box&) const;
69 InstrinsicWidthConstraints instrinsicWidthConstraints() const override;
71 // This class implements positioning and sizing for boxes participating in a block formatting context.
72 class Geometry : public FormattingContext::Geometry {
74 static HeightAndMargin inFlowHeightAndMargin(const LayoutState&, const Box&, std::optional<LayoutUnit> usedHeight = { });
75 static WidthAndMargin inFlowWidthAndMargin(const LayoutState&, const Box&, std::optional<LayoutUnit> usedWidth = { });
77 static Position staticPosition(const LayoutState&, const Box&);
79 static bool instrinsicWidthConstraintsNeedChildrenWidth(const Box&);
80 static InstrinsicWidthConstraints instrinsicWidthConstraints(const LayoutState&, const Box&);
82 static LayoutUnit estimatedMarginTop(const LayoutState&, const Box&);
85 // This class implements margin collapsing for block formatting context.
86 class MarginCollapse {
88 static LayoutUnit marginTop(const LayoutState&, const Box&);
89 static LayoutUnit marginBottom(const LayoutState&, const Box&);
91 static bool isMarginBottomCollapsedWithParent(const LayoutState&, const Box&);
92 static bool isMarginTopCollapsedWithParentMarginBottom(const Box&);
95 static LayoutUnit collapsedMarginBottomFromLastChild(const LayoutState&, const Box&);
96 static LayoutUnit nonCollapsedMarginBottom(const LayoutState&, const Box&);
98 static LayoutUnit computedNonCollapsedMarginTop(const LayoutState&, const Box&);
99 static LayoutUnit computedNonCollapsedMarginBottom(const LayoutState&, const Box&);
101 static LayoutUnit collapsedMarginTopFromFirstChild(const LayoutState&, const Box&);
102 static LayoutUnit nonCollapsedMarginTop(const LayoutState&, const Box&);
105 static HeightAndMargin inFlowNonReplacedHeightAndMargin(const LayoutState&, const Box&, std::optional<LayoutUnit> usedHeight = { });
106 static WidthAndMargin inFlowNonReplacedWidthAndMargin(const LayoutState&, const Box&, std::optional<LayoutUnit> usedWidth = { });
107 static WidthAndMargin inFlowReplacedWidthAndMargin(const LayoutState&, const Box&, std::optional<LayoutUnit> usedWidth = { });
108 static Position staticPositionForOutOfFlowPositioned(const LayoutState&, const Box&);