1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
* Copyright (C) 2018 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "FormattingContext.h"
#if ENABLE(LAYOUT_FORMATTING_CONTEXT)
#include "FormattingGeometry.h"
#include "FormattingQuirks.h"
#include "FormattingState.h"
#include "LayoutBox.h"
#include "LayoutBoxGeometry.h"
#include "LayoutContainerBox.h"
#include "LayoutContainingBlockChainIterator.h"
#include "LayoutContext.h"
#include "LayoutDescendantIterator.h"
#include "LayoutInitialContainingBlock.h"
#include "LayoutReplacedBox.h"
#include "LayoutState.h"
#include "Logging.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
namespace Layout {
WTF_MAKE_ISO_ALLOCATED_IMPL(FormattingContext);
FormattingContext::FormattingContext(const ContainerBox& formattingContextRoot, FormattingState& formattingState)
: m_root(formattingContextRoot)
, m_formattingState(formattingState)
{
ASSERT(formattingContextRoot.hasChild());
#ifndef NDEBUG
layoutState().registerFormattingContext(*this);
#endif
}
FormattingContext::~FormattingContext()
{
#ifndef NDEBUG
layoutState().deregisterFormattingContext(*this);
#endif
}
LayoutState& FormattingContext::layoutState() const
{
return m_formattingState.layoutState();
}
void FormattingContext::computeOutOfFlowHorizontalGeometry(const Box& layoutBox, const ConstraintsForOutOfFlowContent& constraints)
{
ASSERT(layoutBox.isOutOfFlowPositioned());
auto compute = [&](std::optional<LayoutUnit> usedWidth) {
return formattingGeometry().outOfFlowHorizontalGeometry(layoutBox, constraints.horizontal, constraints.vertical, { usedWidth, { } });
};
auto containingBlockWidth = constraints.horizontal.logicalWidth;
auto horizontalGeometry = compute({ });
if (auto maxWidth = formattingGeometry().computedMaxWidth(layoutBox, containingBlockWidth)) {
auto maxHorizontalGeometry = compute(maxWidth);
if (horizontalGeometry.contentWidthAndMargin.contentWidth > maxHorizontalGeometry.contentWidthAndMargin.contentWidth)
horizontalGeometry = maxHorizontalGeometry;
}
if (auto minWidth = formattingGeometry().computedMinWidth(layoutBox, containingBlockWidth)) {
auto minHorizontalGeometry = compute(minWidth);
if (horizontalGeometry.contentWidthAndMargin.contentWidth < minHorizontalGeometry.contentWidthAndMargin.contentWidth)
horizontalGeometry = minHorizontalGeometry;
}
auto& boxGeometry = formattingState().boxGeometry(layoutBox);
boxGeometry.setLogicalLeft(horizontalGeometry.left + horizontalGeometry.contentWidthAndMargin.usedMargin.start);
boxGeometry.setContentBoxWidth(horizontalGeometry.contentWidthAndMargin.contentWidth);
auto& usedHorizontalMargin = horizontalGeometry.contentWidthAndMargin.usedMargin;
boxGeometry.setHorizontalMargin({ usedHorizontalMargin.start, usedHorizontalMargin.end });
}
void FormattingContext::computeOutOfFlowVerticalGeometry(const Box& layoutBox, const ConstraintsForOutOfFlowContent& constraints)
{
ASSERT(layoutBox.isOutOfFlowPositioned());
auto compute = [&](std::optional<LayoutUnit> usedHeight) {
return formattingGeometry().outOfFlowVerticalGeometry(layoutBox, constraints.horizontal, constraints.vertical, { usedHeight });
};
auto containingBlockHeight = constraints.vertical.logicalHeight;
auto verticalGeometry = compute({ });
if (auto maxHeight = formattingGeometry().computedMaxHeight(layoutBox, containingBlockHeight)) {
auto maxVerticalGeometry = compute(maxHeight);
if (verticalGeometry.contentHeightAndMargin.contentHeight > maxVerticalGeometry.contentHeightAndMargin.contentHeight)
verticalGeometry = maxVerticalGeometry;
}
if (auto minHeight = formattingGeometry().computedMinHeight(layoutBox, containingBlockHeight)) {
auto minVerticalGeometry = compute(minHeight);
if (verticalGeometry.contentHeightAndMargin.contentHeight < minVerticalGeometry.contentHeightAndMargin.contentHeight)
verticalGeometry = minVerticalGeometry;
}
auto& boxGeometry = formattingState().boxGeometry(layoutBox);
auto nonCollapsedVerticalMargin = verticalGeometry.contentHeightAndMargin.nonCollapsedMargin;
boxGeometry.setLogicalTop(verticalGeometry.top + nonCollapsedVerticalMargin.before);
boxGeometry.setContentBoxHeight(verticalGeometry.contentHeightAndMargin.contentHeight);
// Margins of absolutely positioned boxes do not collapse.
boxGeometry.setVerticalMargin({ nonCollapsedVerticalMargin.before, nonCollapsedVerticalMargin.after });
}
void FormattingContext::computeBorderAndPadding(const Box& layoutBox, const HorizontalConstraints& horizontalConstraint)
{
auto& boxGeometry = formattingState().boxGeometry(layoutBox);
boxGeometry.setBorder(formattingGeometry().computedBorder(layoutBox));
boxGeometry.setPadding(formattingGeometry().computedPadding(layoutBox, horizontalConstraint.logicalWidth));
}
void FormattingContext::layoutOutOfFlowContent(const ConstraintsForOutOfFlowContent& constraints)
{
LOG_WITH_STREAM(FormattingContextLayout, stream << "Start: layout out-of-flow content -> context: " << &layoutState() << " root: " << &root());
collectOutOfFlowDescendantsIfNeeded();
auto constraintsForLayoutBox = [&] (const auto& outOfFlowBox) {
auto& containingBlock = outOfFlowBox->containingBlock();
return &containingBlock == &root() ? constraints : formattingGeometry().constraintsForOutOfFlowContent(containingBlock);
};
for (auto& outOfFlowBox : formattingState().outOfFlowBoxes()) {
ASSERT(outOfFlowBox->establishesFormattingContext());
auto containingBlockConstraints = constraintsForLayoutBox(outOfFlowBox);
auto horizontalConstraintsForBorderAndPadding = HorizontalConstraints { containingBlockConstraints.horizontal.logicalLeft, containingBlockConstraints.borderAndPaddingConstraints };
computeBorderAndPadding(outOfFlowBox, horizontalConstraintsForBorderAndPadding);
computeOutOfFlowHorizontalGeometry(outOfFlowBox, containingBlockConstraints);
auto outOfFlowBoxHasContent = is<ContainerBox>(outOfFlowBox.get()) && downcast<ContainerBox>(outOfFlowBox.get()).hasChild();
if (outOfFlowBoxHasContent) {
auto& containerBox = downcast<ContainerBox>(outOfFlowBox.get());
auto formattingContext = LayoutContext::createFormattingContext(containerBox, layoutState());
if (containerBox.hasInFlowOrFloatingChild())
formattingContext->layoutInFlowContent(formattingGeometry().constraintsForInFlowContent(containerBox));
computeOutOfFlowVerticalGeometry(containerBox, containingBlockConstraints);
formattingContext->layoutOutOfFlowContent(formattingGeometry().constraintsForOutOfFlowContent(containerBox));
} else
computeOutOfFlowVerticalGeometry(outOfFlowBox, containingBlockConstraints);
}
LOG_WITH_STREAM(FormattingContextLayout, stream << "End: layout out-of-flow content -> context: " << &layoutState() << " root: " << &root());
}
const BoxGeometry& FormattingContext::geometryForBox(const Box& layoutBox, std::optional<EscapeReason> escapeReason) const
{
UNUSED_PARAM(escapeReason);
#if ASSERT_ENABLED
auto isOkToAccessBoxGeometry = [&] {
if (!is<InitialContainingBlock>(layoutBox) && &layoutBox.formattingContextRoot() == &root()) {
// This is the non-escape case of accessing a box's geometry information within the same formatting context.
return true;
}
if (!escapeReason) {
// Any geometry access outside of the formatting context without a valid reason is considered an escape.
return false;
}
if (*escapeReason == EscapeReason::DocumentBoxStretchesToViewportQuirk) {
ASSERT(layoutState().inQuirksMode());
return is<InitialContainingBlock>(layoutBox);
}
if (*escapeReason == EscapeReason::BodyStretchesToViewportQuirk) {
ASSERT(layoutState().inQuirksMode());
return is<InitialContainingBlock>(layoutBox) || layoutBox.isDocumentBox();
}
if (*escapeReason == EscapeReason::TableQuirkNeedsGeometryFromEstablishedFormattingContext) {
// This is the case when the table formatting root collects geometry information from the cell's
// formatting context to be able to determine width/height. see shouldIgnoreChildContentVerticalMargin
ASSERT(root().establishesTableFormattingContext());
return &layoutBox.formattingContextRoot().formattingContextRoot() == &root();
}
if (*escapeReason == EscapeReason::OutOfFlowBoxNeedsInFlowGeometry) {
// When computing the static position of an out-of-flow box, we need to gather sibling/parent geometry information
// as if the out-of-flow box was a simple inflow box.
// Now since the out-of-flow and the sibling/parent boxes could very well be in different containing block subtrees
// the formatting context they live in could also be very different.
return true;
}
if (*escapeReason == EscapeReason::FloatBoxIsAlwaysRelativeToFloatStateRoot) {
// Float box top/left values are mapped relative to the FloatState's root. Inline formatting contexts(A) inherit floats from parent
// block formatting contexts(B). Floats in these inline formatting contexts(A) need to be mapped to the parent, block formatting context(B).
auto& formattingContextRoot = layoutBox.formattingContextRoot();
return &formattingContextRoot == &root() || &formattingContextRoot == &root().formattingContextRoot();
}
if (*escapeReason == EscapeReason::FindFixedHeightAncestorQuirk) {
ASSERT(layoutState().inQuirksMode());
// Find the first containing block with fixed height quirk. See Quirks::heightValueOfNearestContainingBlockWithFixedHeight.
// This is only to check if the targetFormattingRoot is an ancestor formatting root.
if (is<InitialContainingBlock>(layoutBox))
return true;
auto& targetFormattingRoot = layoutBox.formattingContextRoot();
auto* ancestorFormattingContextRoot = &root().formattingContextRoot();
while (true) {
if (&targetFormattingRoot == ancestorFormattingContextRoot)
return true;
ancestorFormattingContextRoot = &ancestorFormattingContextRoot->formattingContextRoot();
if (is<InitialContainingBlock>(*ancestorFormattingContextRoot))
return true;
}
return false;
}
if (*escapeReason == EscapeReason::TableNeedsAccessToTableWrapper) {
// Tables are wrapped in a 2 level formatting context structure. A <table> element initiates a block formatting context for its principal table box
// where the caption and the table content live. It also initiates a table wrapper box which establishes the table formatting context.
// In many cases the TFC needs access to the parent (generated) BFC.
return &layoutBox == &root().formattingContextRoot();
}
ASSERT_NOT_REACHED();
return false;
};
#endif
ASSERT(isOkToAccessBoxGeometry());
ASSERT(layoutState().hasBoxGeometry(layoutBox));
return layoutState().geometryForBox(layoutBox);
}
void FormattingContext::collectOutOfFlowDescendantsIfNeeded()
{
if (!formattingState().outOfFlowBoxes().isEmpty())
return;
auto& root = this->root();
if (!root.hasChild())
return;
if (!root.isPositioned() && !is<InitialContainingBlock>(root))
return;
// Collect the out-of-flow descendants at the formatting root level (as opposed to at the containing block level, though they might be the same).
// FIXME: Turn this into a register-self as boxes are being inserted.
for (auto& descendant : descendantsOfType<Box>(root)) {
if (!descendant.isOutOfFlowPositioned())
continue;
if (&descendant.formattingContextRoot() != &root)
continue;
formattingState().addOutOfFlowBox(descendant);
}
}
#ifndef NDEBUG
void FormattingContext::validateGeometryConstraintsAfterLayout() const
{
auto& formattingContextRoot = root();
// FIXME: add a descendantsOfType<> flavor that stops at nested formatting contexts
for (auto& layoutBox : descendantsOfType<Box>(formattingContextRoot)) {
if (&layoutBox.formattingContextRoot() != &formattingContextRoot)
continue;
auto& containingBlockGeometry = geometryForBox(layoutBox.containingBlock());
auto& boxGeometry = geometryForBox(layoutBox);
// 10.3.3 Block-level, non-replaced elements in normal flow
// 10.3.7 Absolutely positioned, non-replaced elements
if ((layoutBox.isBlockLevelBox() || layoutBox.isOutOfFlowPositioned()) && !layoutBox.isReplacedBox()) {
// margin-left + border-left-width + padding-left + width + padding-right + border-right-width + margin-right = width of containing block
auto containingBlockWidth = containingBlockGeometry.contentBoxWidth();
ASSERT(boxGeometry.horizontalMarginBorderAndPadding() + boxGeometry.contentBoxWidth() == containingBlockWidth);
}
// 10.6.4 Absolutely positioned, non-replaced elements
if (layoutBox.isOutOfFlowPositioned() && !layoutBox.isReplacedBox()) {
// top + margin-top + border-top-width + padding-top + height + padding-bottom + border-bottom-width + margin-bottom + bottom = height of containing block
auto containingBlockHeight = containingBlockGeometry.contentBoxHeight();
ASSERT(BoxGeometry::borderBoxTop(boxGeometry) + boxGeometry.marginBefore() + boxGeometry.borderBefore() + boxGeometry.paddingBefore().value_or(0) + boxGeometry.contentBoxHeight()
+ boxGeometry.paddingAfter().value_or(0) + boxGeometry.borderAfter() + boxGeometry.marginAfter() == containingBlockHeight);
}
}
}
#endif
}
}
#endif
|