1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#include "config.h"
27#include "BlockFormattingContext.h"
28
29#if ENABLE(LAYOUT_FORMATTING_CONTEXT)
30
31#include "LayoutBox.h"
32#include "LayoutContainer.h"
33#include "LayoutState.h"
34
35namespace WebCore {
36namespace Layout {
37
38static const Container& initialContainingBlock(const Box& layoutBox)
39{
40 auto* containingBlock = layoutBox.containingBlock();
41 while (containingBlock->containingBlock())
42 containingBlock = containingBlock->containingBlock();
43 return *containingBlock;
44}
45
46static bool isQuirkContainer(const Box& layoutBox)
47{
48 return layoutBox.isBodyBox() || layoutBox.isDocumentBox() || layoutBox.isTableCell();
49}
50
51bool BlockFormattingContext::Quirks::needsStretching(const LayoutState& layoutState, const Box& layoutBox)
52{
53 // In quirks mode, body stretches to html and html to the initial containing block (height: auto only).
54 if (!layoutState.inQuirksMode())
55 return false;
56
57 if (!layoutBox.isDocumentBox() && !layoutBox.isBodyBox())
58 return false;
59
60 return layoutBox.style().logicalHeight().isAuto();
61}
62
63HeightAndMargin BlockFormattingContext::Quirks::stretchedInFlowHeight(const LayoutState& layoutState, const Box& layoutBox, HeightAndMargin heightAndMargin)
64{
65 ASSERT(layoutBox.isInFlow());
66 ASSERT(layoutBox.isDocumentBox() || layoutBox.isBodyBox());
67
68 auto& documentBox = layoutBox.isDocumentBox() ? layoutBox : *layoutBox.parent();
69 auto& documentBoxDisplayBox = layoutState.displayBoxForLayoutBox(documentBox);
70
71 auto& initialContainingBlockDisplayBox = layoutState.displayBoxForLayoutBox(initialContainingBlock(layoutBox));
72 auto strechedHeight = initialContainingBlockDisplayBox.contentBoxHeight();
73 strechedHeight -= documentBoxDisplayBox.verticalBorder() + documentBoxDisplayBox.verticalPadding().valueOr(0);
74
75 LayoutUnit totalVerticalMargin;
76 if (layoutBox.isDocumentBox()) {
77 // Document box's margins do not collapse.
78 auto verticalMargin = heightAndMargin.nonCollapsedMargin;
79 totalVerticalMargin = verticalMargin.before + verticalMargin.after;
80 } else if (layoutBox.isBodyBox()) {
81 // Here is the quirky part for body box:
82 // Stretch the body using the initial containing block's height and shrink it with document box's margin/border/padding.
83 // This looks extremely odd when html has non-auto height.
84 auto documentBoxVerticalMargin = Geometry::computedVerticalMargin(documentBox, UsedHorizontalValues { initialContainingBlockDisplayBox.contentBoxWidth() });
85 strechedHeight -= (documentBoxVerticalMargin.before.valueOr(0) + documentBoxVerticalMargin.after.valueOr(0));
86
87 auto& bodyBoxDisplayBox = layoutState.displayBoxForLayoutBox(layoutBox);
88 strechedHeight -= bodyBoxDisplayBox.verticalBorder() + bodyBoxDisplayBox.verticalPadding().valueOr(0);
89
90 auto nonCollapsedMargin = heightAndMargin.nonCollapsedMargin;
91 auto collapsedMargin = MarginCollapse::collapsedVerticalValues(layoutState, layoutBox, nonCollapsedMargin);
92 totalVerticalMargin = collapsedMargin.before.valueOr(nonCollapsedMargin.before);
93 totalVerticalMargin += collapsedMargin.isCollapsedThrough ? nonCollapsedMargin.after : collapsedMargin.after.valueOr(nonCollapsedMargin.after);
94 }
95
96 // Stretch but never overstretch with the margins.
97 if (heightAndMargin.height + totalVerticalMargin < strechedHeight)
98 heightAndMargin.height = strechedHeight - totalVerticalMargin;
99
100 return heightAndMargin;
101}
102
103bool BlockFormattingContext::Quirks::shouldIgnoreCollapsedQuirkMargin(const LayoutState& layoutState, const Box& layoutBox)
104{
105 return layoutState.inQuirksMode() && isQuirkContainer(layoutBox);
106}
107
108}
109}
110
111#endif
112