RenderBlockFlow.cpp [plain text]
#include "config.h"
#include "RenderBlockFlow.h"
#include "Editor.h"
#include "FloatingObjects.h"
#include "Frame.h"
#include "HTMLElement.h"
#include "HitTestLocation.h"
#include "InlineTextBox.h"
#include "LayoutRepainter.h"
#include "RenderFlowThread.h"
#include "RenderIterator.h"
#include "RenderLayer.h"
#include "RenderListItem.h"
#include "RenderMarquee.h"
#include "RenderMultiColumnFlowThread.h"
#include "RenderMultiColumnSet.h"
#include "RenderNamedFlowFragment.h"
#include "RenderTableCell.h"
#include "RenderText.h"
#include "RenderView.h"
#include "SimpleLineLayoutFunctions.h"
#include "VerticalPositionCache.h"
#include "VisiblePosition.h"
namespace WebCore {
bool RenderBlock::s_canPropagateFloatIntoSibling = false;
struct SameSizeAsMarginInfo {
uint32_t bitfields : 16;
LayoutUnit margins[2];
};
COMPILE_ASSERT(sizeof(RenderBlockFlow::MarginValues) == sizeof(LayoutUnit[4]), MarginValues_should_stay_small);
COMPILE_ASSERT(sizeof(RenderBlockFlow::MarginInfo) == sizeof(SameSizeAsMarginInfo), MarginInfo_should_stay_small);
RenderBlockFlow::MarginInfo::MarginInfo(RenderBlockFlow& block, LayoutUnit beforeBorderPadding, LayoutUnit afterBorderPadding)
: m_atBeforeSideOfBlock(true)
, m_atAfterSideOfBlock(false)
, m_hasMarginBeforeQuirk(false)
, m_hasMarginAfterQuirk(false)
, m_determinedMarginBeforeQuirk(false)
, m_discardMargin(false)
{
const RenderStyle& blockStyle = block.style();
ASSERT(block.isRenderView() || block.parent());
m_canCollapseWithChildren = !block.isRenderView() && !block.isRoot() && !block.isOutOfFlowPositioned()
&& !block.isFloating() && !block.isTableCell() && !block.hasOverflowClip() && !block.isInlineBlockOrInlineTable()
&& !block.isRenderFlowThread() && !block.isWritingModeRoot() && !block.parent()->isFlexibleBox()
&& blockStyle.hasAutoColumnCount() && blockStyle.hasAutoColumnWidth() && !blockStyle.columnSpan();
m_canCollapseMarginBeforeWithChildren = m_canCollapseWithChildren && !beforeBorderPadding && blockStyle.marginBeforeCollapse() != MSEPARATE;
m_canCollapseMarginAfterWithChildren = m_canCollapseWithChildren && !afterBorderPadding
&& (blockStyle.logicalHeight().isAuto() && !blockStyle.logicalHeight().value()) && blockStyle.marginAfterCollapse() != MSEPARATE;
m_quirkContainer = block.isTableCell() || block.isBody();
m_discardMargin = m_canCollapseMarginBeforeWithChildren && block.mustDiscardMarginBefore();
m_positiveMargin = (m_canCollapseMarginBeforeWithChildren && !block.mustDiscardMarginBefore()) ? block.maxPositiveMarginBefore() : LayoutUnit();
m_negativeMargin = (m_canCollapseMarginBeforeWithChildren && !block.mustDiscardMarginBefore()) ? block.maxNegativeMarginBefore() : LayoutUnit();
}
RenderBlockFlow::RenderBlockFlow(Element& element, PassRef<RenderStyle> style)
: RenderBlock(element, WTF::move(style), RenderBlockFlowFlag)
#if ENABLE(IOS_TEXT_AUTOSIZING)
, m_widthForTextAutosizing(-1)
, m_lineCountForTextAutosizing(NOT_SET)
#endif
{
setChildrenInline(true);
}
RenderBlockFlow::RenderBlockFlow(Document& document, PassRef<RenderStyle> style)
: RenderBlock(document, WTF::move(style), RenderBlockFlowFlag)
#if ENABLE(IOS_TEXT_AUTOSIZING)
, m_widthForTextAutosizing(-1)
, m_lineCountForTextAutosizing(NOT_SET)
#endif
{
setChildrenInline(true);
}
RenderBlockFlow::~RenderBlockFlow()
{
}
void RenderBlockFlow::createMultiColumnFlowThread()
{
RenderMultiColumnFlowThread* flowThread = new RenderMultiColumnFlowThread(document(), RenderStyle::createAnonymousStyleWithDisplay(&style(), BLOCK));
flowThread->initializeStyle();
setChildrenInline(false); RenderBlock::addChild(flowThread);
flowThread->populate(); setMultiColumnFlowThread(flowThread);
}
void RenderBlockFlow::destroyMultiColumnFlowThread()
{
multiColumnFlowThread()->evacuateAndDestroy();
ASSERT(!multiColumnFlowThread());
}
void RenderBlockFlow::insertedIntoTree()
{
RenderBlock::insertedIntoTree();
createRenderNamedFlowFragmentIfNeeded();
}
void RenderBlockFlow::willBeDestroyed()
{
m_beingDestroyed = true;
if (renderNamedFlowFragment())
setRenderNamedFlowFragment(0);
destroyLeftoverChildren();
RenderBoxModelObject* continuation = this->continuation();
if (continuation) {
continuation->destroy();
setContinuation(0);
}
if (!documentBeingDestroyed()) {
if (firstRootBox()) {
if (isSelectionBorder())
view().clearSelection();
if (isAnonymousBlock()) {
for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
while (auto childBox = box->firstChild())
childBox->removeFromParent();
}
}
} else if (parent())
parent()->dirtyLinesFromChangedChild(this);
}
m_lineBoxes.deleteLineBoxes();
removeFromUpdateScrollInfoAfterLayoutTransaction();
RenderBox::willBeDestroyed();
}
void RenderBlockFlow::rebuildFloatingObjectSetFromIntrudingFloats()
{
if (m_floatingObjects)
m_floatingObjects->setHorizontalWritingMode(isHorizontalWritingMode());
HashSet<RenderBox*> oldIntrudingFloatSet;
if (!childrenInline() && m_floatingObjects) {
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto end = floatingObjectSet.end();
for (auto it = floatingObjectSet.begin(); it != end; ++it) {
FloatingObject* floatingObject = it->get();
if (!floatingObject->isDescendant())
oldIntrudingFloatSet.add(&floatingObject->renderer());
}
}
if (avoidsFloats() || isRoot() || isRenderView() || isFloatingOrOutOfFlowPositioned() || isTableCell()) {
if (m_floatingObjects)
m_floatingObjects->clear();
if (!oldIntrudingFloatSet.isEmpty())
markAllDescendantsWithFloatsForLayout();
return;
}
RendererToFloatInfoMap floatMap;
if (m_floatingObjects) {
if (childrenInline())
m_floatingObjects->moveAllToFloatInfoMap(floatMap);
else
m_floatingObjects->clear();
}
if (!parent() || !parent()->isRenderBlockFlow())
return;
RenderBlockFlow* parentBlock = toRenderBlockFlow(parent());
bool parentHasFloats = false;
RenderObject* prev = previousSibling();
while (prev && (prev->isFloatingOrOutOfFlowPositioned() || !prev->isBox() || !prev->isRenderBlockFlow() || toRenderBlockFlow(prev)->avoidsFloats())) {
if (prev->isFloating())
parentHasFloats = true;
prev = prev->previousSibling();
}
LayoutUnit logicalTopOffset = logicalTop();
if (parentHasFloats || (parentBlock->lowestFloatLogicalBottom() > logicalTopOffset && prev && toRenderBlockFlow(prev)->isSelfCollapsingBlock()))
addIntrudingFloats(parentBlock, parentBlock->logicalLeftOffsetForContent(), logicalTopOffset);
LayoutUnit logicalLeftOffset = 0;
if (prev)
logicalTopOffset -= toRenderBox(prev)->logicalTop();
else {
prev = parentBlock;
logicalLeftOffset += parentBlock->logicalLeftOffsetForContent();
}
RenderBlockFlow* block = toRenderBlockFlow(prev);
if (block->m_floatingObjects && block->lowestFloatLogicalBottom() > logicalTopOffset)
addIntrudingFloats(block, logicalLeftOffset, logicalTopOffset);
if (childrenInline()) {
LayoutUnit changeLogicalTop = LayoutUnit::max();
LayoutUnit changeLogicalBottom = LayoutUnit::min();
if (m_floatingObjects) {
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto end = floatingObjectSet.end();
for (auto it = floatingObjectSet.begin(); it != end; ++it) {
FloatingObject* floatingObject = it->get();
std::unique_ptr<FloatingObject> oldFloatingObject = floatMap.take(&floatingObject->renderer());
LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
if (oldFloatingObject) {
LayoutUnit oldLogicalBottom = logicalBottomForFloat(oldFloatingObject.get());
if (logicalWidthForFloat(floatingObject) != logicalWidthForFloat(oldFloatingObject.get()) || logicalLeftForFloat(floatingObject) != logicalLeftForFloat(oldFloatingObject.get())) {
changeLogicalTop = 0;
changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalBottom, oldLogicalBottom));
} else {
if (logicalBottom != oldLogicalBottom) {
changeLogicalTop = std::min(changeLogicalTop, std::min(logicalBottom, oldLogicalBottom));
changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalBottom, oldLogicalBottom));
}
LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
LayoutUnit oldLogicalTop = logicalTopForFloat(oldFloatingObject.get());
if (logicalTop != oldLogicalTop) {
changeLogicalTop = std::min(changeLogicalTop, std::min(logicalTop, oldLogicalTop));
changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalTop, oldLogicalTop));
}
}
if (oldFloatingObject->originatingLine() && !selfNeedsLayout()) {
ASSERT(&oldFloatingObject->originatingLine()->renderer() == this);
oldFloatingObject->originatingLine()->markDirty();
}
} else {
changeLogicalTop = 0;
changeLogicalBottom = std::max(changeLogicalBottom, logicalBottom);
}
}
}
auto end = floatMap.end();
for (auto it = floatMap.begin(); it != end; ++it) {
FloatingObject* floatingObject = it->value.get();
if (!floatingObject->isDescendant()) {
changeLogicalTop = 0;
changeLogicalBottom = std::max(changeLogicalBottom, logicalBottomForFloat(floatingObject));
}
}
markLinesDirtyInBlockRange(changeLogicalTop, changeLogicalBottom);
} else if (!oldIntrudingFloatSet.isEmpty()) {
if (m_floatingObjects->set().size() < oldIntrudingFloatSet.size())
markAllDescendantsWithFloatsForLayout();
else {
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto end = floatingObjectSet.end();
for (auto it = floatingObjectSet.begin(); it != end && !oldIntrudingFloatSet.isEmpty(); ++it)
oldIntrudingFloatSet.remove(&(*it)->renderer());
if (!oldIntrudingFloatSet.isEmpty())
markAllDescendantsWithFloatsForLayout();
}
}
}
void RenderBlockFlow::adjustIntrinsicLogicalWidthsForColumns(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
{
if (!style().hasAutoColumnCount() || !style().hasAutoColumnWidth()) {
int columnCount = style().hasAutoColumnCount() ? 1 : style().columnCount();
LayoutUnit columnWidth;
LayoutUnit colGap = columnGap();
LayoutUnit gapExtra = (columnCount - 1) * colGap;
if (style().hasAutoColumnWidth())
minLogicalWidth = minLogicalWidth * columnCount + gapExtra;
else {
columnWidth = style().columnWidth();
minLogicalWidth = std::min(minLogicalWidth, columnWidth);
}
maxLogicalWidth = std::max(maxLogicalWidth, columnWidth) * columnCount + gapExtra;
}
}
void RenderBlockFlow::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
{
if (childrenInline()) {
const_cast<RenderBlockFlow*>(this)->computeInlinePreferredLogicalWidths(minLogicalWidth, maxLogicalWidth);
} else
computeBlockPreferredLogicalWidths(minLogicalWidth, maxLogicalWidth);
maxLogicalWidth = std::max(minLogicalWidth, maxLogicalWidth);
adjustIntrinsicLogicalWidthsForColumns(minLogicalWidth, maxLogicalWidth);
if (!style().autoWrap() && childrenInline()) {
if (layer() && layer()->marquee() && layer()->marquee()->isHorizontal())
minLogicalWidth = 0;
}
if (isTableCell()) {
Length tableCellWidth = toRenderTableCell(this)->styleOrColLogicalWidth();
if (tableCellWidth.isFixed() && tableCellWidth.value() > 0)
maxLogicalWidth = std::max(minLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(tableCellWidth.value()));
}
int scrollbarWidth = instrinsicScrollbarLogicalWidth();
maxLogicalWidth += scrollbarWidth;
minLogicalWidth += scrollbarWidth;
}
bool RenderBlockFlow::recomputeLogicalWidthAndColumnWidth()
{
bool changed = recomputeLogicalWidth();
LayoutUnit oldColumnWidth = computedColumnWidth();
computeColumnCountAndWidth();
return changed || oldColumnWidth != computedColumnWidth();
}
LayoutUnit RenderBlockFlow::columnGap() const
{
if (style().hasNormalColumnGap())
return style().fontDescription().computedPixelSize(); return style().columnGap();
}
void RenderBlockFlow::computeColumnCountAndWidth()
{
unsigned desiredColumnCount = 1;
LayoutUnit desiredColumnWidth = contentLogicalWidth();
if (document().paginated() || (style().hasAutoColumnCount() && style().hasAutoColumnWidth()) || !style().hasInlineColumnAxis()) {
setComputedColumnCountAndWidth(desiredColumnCount, desiredColumnWidth);
return;
}
LayoutUnit availWidth = desiredColumnWidth;
LayoutUnit colGap = columnGap();
LayoutUnit colWidth = std::max<LayoutUnit>(LayoutUnit::fromPixel(1), LayoutUnit(style().columnWidth()));
int colCount = std::max<int>(1, style().columnCount());
if (style().hasAutoColumnWidth() && !style().hasAutoColumnCount()) {
desiredColumnCount = colCount;
desiredColumnWidth = std::max<LayoutUnit>(0, (availWidth - ((desiredColumnCount - 1) * colGap)) / desiredColumnCount);
} else if (!style().hasAutoColumnWidth() && style().hasAutoColumnCount()) {
desiredColumnCount = std::max<LayoutUnit>(1, (availWidth + colGap) / (colWidth + colGap));
desiredColumnWidth = ((availWidth + colGap) / desiredColumnCount) - colGap;
} else {
desiredColumnCount = std::max<LayoutUnit>(std::min<LayoutUnit>(colCount, (availWidth + colGap) / (colWidth + colGap)), 1);
desiredColumnWidth = ((availWidth + colGap) / desiredColumnCount) - colGap;
}
setComputedColumnCountAndWidth(desiredColumnCount, desiredColumnWidth);
}
void RenderBlockFlow::layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight)
{
ASSERT(needsLayout());
if (!relayoutChildren && simplifiedLayout())
return;
LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
if (recomputeLogicalWidthAndColumnWidth())
relayoutChildren = true;
rebuildFloatingObjectSetFromIntrudingFloats();
LayoutUnit previousHeight = logicalHeight();
setLogicalHeight(0);
bool pageLogicalHeightChanged = false;
checkForPaginationLogicalHeightChange(relayoutChildren, pageLogicalHeight, pageLogicalHeightChanged);
const RenderStyle& styleToUse = style();
LayoutStateMaintainer statePusher(view(), *this, locationOffset(), hasTransform() || hasReflection() || styleToUse.isFlippedBlocksWritingMode(), pageLogicalHeight, pageLogicalHeightChanged);
preparePaginationBeforeBlockLayout(relayoutChildren);
if (!relayoutChildren)
relayoutChildren = namedFlowFragmentNeedsUpdate();
bool isCell = isTableCell();
if (!isCell) {
initMaxMarginValues();
setHasMarginBeforeQuirk(styleToUse.hasMarginBeforeQuirk());
setHasMarginAfterQuirk(styleToUse.hasMarginAfterQuirk());
setPaginationStrut(0);
}
LayoutUnit repaintLogicalTop = 0;
LayoutUnit repaintLogicalBottom = 0;
LayoutUnit maxFloatLogicalBottom = 0;
if (!firstChild() && !isAnonymousBlock())
setChildrenInline(true);
if (childrenInline())
layoutInlineChildren(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
else
layoutBlockChildren(relayoutChildren, maxFloatLogicalBottom);
LayoutUnit toAdd = borderAndPaddingAfter() + scrollbarLogicalHeight();
if (lowestFloatLogicalBottom() > (logicalHeight() - toAdd) && expandsToEncloseOverhangingFloats())
setLogicalHeight(lowestFloatLogicalBottom() + toAdd);
if (relayoutForPagination(statePusher) || relayoutToAvoidWidows(statePusher)) {
ASSERT(!shouldBreakAtLineToAvoidWidow());
return;
}
LayoutUnit oldHeight = logicalHeight();
LayoutUnit oldClientAfterEdge = clientLogicalBottom();
if (isRenderFlowThread())
toRenderFlowThread(this)->applyBreakAfterContent(oldClientAfterEdge);
updateLogicalHeight();
LayoutUnit newHeight = logicalHeight();
if (oldHeight != newHeight) {
if (oldHeight > newHeight && maxFloatLogicalBottom > newHeight && !childrenInline()) {
for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
if (blockFlow.isFloatingOrOutOfFlowPositioned())
continue;
if (blockFlow.lowestFloatLogicalBottom() + blockFlow.logicalTop() > newHeight)
addOverhangingFloats(blockFlow, false);
}
}
}
bool heightChanged = (previousHeight != newHeight);
if (heightChanged)
relayoutChildren = true;
layoutPositionedObjects(relayoutChildren || isRoot());
computeOverflow(oldClientAfterEdge);
statePusher.pop();
fitBorderToLinesIfNeeded();
if (view().layoutState()->m_pageLogicalHeight)
setPageLogicalOffset(view().layoutState()->pageLogicalOffset(this, logicalTop()));
updateLayerTransform();
updateScrollInfoAfterLayout();
bool didFullRepaint = repainter.repaintAfterLayout();
if (!didFullRepaint && repaintLogicalTop != repaintLogicalBottom && (styleToUse.visibility() == VISIBLE || enclosingLayer()->hasVisibleContent())) {
LayoutUnit repaintLogicalLeft = logicalLeftVisualOverflow();
LayoutUnit repaintLogicalRight = logicalRightVisualOverflow();
if (hasOverflowClip()) {
repaintLogicalLeft = std::min(repaintLogicalLeft, logicalLeftLayoutOverflow());
repaintLogicalRight = std::max(repaintLogicalRight, logicalRightLayoutOverflow());
}
LayoutRect repaintRect;
if (isHorizontalWritingMode())
repaintRect = LayoutRect(repaintLogicalLeft, repaintLogicalTop, repaintLogicalRight - repaintLogicalLeft, repaintLogicalBottom - repaintLogicalTop);
else
repaintRect = LayoutRect(repaintLogicalTop, repaintLogicalLeft, repaintLogicalBottom - repaintLogicalTop, repaintLogicalRight - repaintLogicalLeft);
repaintRect.inflate(maximalOutlineSize(PaintPhaseOutline));
if (hasOverflowClip()) {
repaintRect.move(-scrolledContentOffset());
repaintRect.intersect(LayoutRect(LayoutPoint(), size()));
}
if (!repaintRect.isEmpty()) {
repaintRectangle(repaintRect); if (hasReflection())
repaintRectangle(reflectedRect(repaintRect));
}
}
clearNeedsLayout();
}
void RenderBlockFlow::layoutBlockChildren(bool relayoutChildren, LayoutUnit& maxFloatLogicalBottom)
{
dirtyForLayoutFromPercentageHeightDescendants();
LayoutUnit beforeEdge = borderAndPaddingBefore();
LayoutUnit afterEdge = borderAndPaddingAfter() + scrollbarLogicalHeight();
setLogicalHeight(beforeEdge);
if (view().layoutState()->lineGrid() == this)
layoutLineGridBox();
MarginInfo marginInfo(*this, beforeEdge, afterEdge);
RenderObject* childToExclude = layoutSpecialExcludedChild(relayoutChildren);
LayoutUnit previousFloatLogicalBottom = 0;
maxFloatLogicalBottom = 0;
RenderBox* next = firstChildBox();
while (next) {
RenderBox& child = *next;
next = child.nextSiblingBox();
if (childToExclude == &child)
continue;
updateBlockChildDirtyBitsBeforeLayout(relayoutChildren, child);
if (child.isOutOfFlowPositioned()) {
child.containingBlock()->insertPositionedObject(child);
adjustPositionedBlock(child, marginInfo);
continue;
}
if (child.isFloating()) {
insertFloatingObject(child);
adjustFloatingBlock(marginInfo);
continue;
}
layoutBlockChild(child, marginInfo, previousFloatLogicalBottom, maxFloatLogicalBottom);
}
handleAfterSideOfBlock(beforeEdge, afterEdge, marginInfo);
}
void RenderBlockFlow::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
{
if (m_lineLayoutPath == UndeterminedPath)
m_lineLayoutPath = SimpleLineLayout::canUseFor(*this) ? SimpleLinesPath : LineBoxesPath;
if (m_lineLayoutPath == SimpleLinesPath) {
deleteLineBoxesBeforeSimpleLineLayout();
layoutSimpleLines(repaintLogicalTop, repaintLogicalBottom);
return;
}
m_simpleLineLayout = nullptr;
layoutLineBoxes(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
}
void RenderBlockFlow::layoutBlockChild(RenderBox& child, MarginInfo& marginInfo, LayoutUnit& previousFloatLogicalBottom, LayoutUnit& maxFloatLogicalBottom)
{
LayoutUnit oldPosMarginBefore = maxPositiveMarginBefore();
LayoutUnit oldNegMarginBefore = maxNegativeMarginBefore();
child.computeAndSetBlockDirectionMargins(this);
LayoutUnit estimateWithoutPagination;
LayoutUnit logicalTopEstimate = estimateLogicalTopPosition(child, marginInfo, estimateWithoutPagination);
LayoutRect oldRect = child.frameRect();
LayoutUnit oldLogicalTop = logicalTopForChild(child);
#if !ASSERT_DISABLED
LayoutSize oldLayoutDelta = view().layoutDelta();
#endif
setLogicalTopForChild(child, logicalTopEstimate, ApplyLayoutDelta);
estimateRegionRangeForBoxChild(child);
RenderBlockFlow* childBlockFlow = child.isRenderBlockFlow() ? toRenderBlockFlow(&child) : nullptr;
bool markDescendantsWithFloats = false;
if (logicalTopEstimate != oldLogicalTop && !child.avoidsFloats() && childBlockFlow && childBlockFlow->containsFloats())
markDescendantsWithFloats = true;
#if ENABLE(SUBPIXEL_LAYOUT)
else if (UNLIKELY(logicalTopEstimate.mightBeSaturated()))
markDescendantsWithFloats = true;
#endif
else if (!child.avoidsFloats() || child.shrinkToAvoidFloats()) {
LayoutUnit fb = std::max(previousFloatLogicalBottom, lowestFloatLogicalBottom());
if (fb > logicalTopEstimate)
markDescendantsWithFloats = true;
}
if (childBlockFlow) {
if (markDescendantsWithFloats)
childBlockFlow->markAllDescendantsWithFloatsForLayout();
if (!child.isWritingModeRoot())
previousFloatLogicalBottom = std::max(previousFloatLogicalBottom, oldLogicalTop + childBlockFlow->lowestFloatLogicalBottom());
}
if (!child.needsLayout())
child.markForPaginationRelayoutIfNeeded();
bool childHadLayout = child.everHadLayout();
bool childNeededLayout = child.needsLayout();
if (childNeededLayout)
child.layout();
bool atBeforeSideOfBlock = marginInfo.atBeforeSideOfBlock();
LayoutUnit logicalTopBeforeClear = collapseMargins(child, marginInfo);
LayoutUnit logicalTopAfterClear = clearFloatsIfNeeded(child, marginInfo, oldPosMarginBefore, oldNegMarginBefore, logicalTopBeforeClear);
bool paginated = view().layoutState()->isPaginated();
if (paginated)
logicalTopAfterClear = adjustBlockChildForPagination(logicalTopAfterClear, estimateWithoutPagination, child, atBeforeSideOfBlock && logicalTopBeforeClear == logicalTopAfterClear);
setLogicalTopForChild(child, logicalTopAfterClear, ApplyLayoutDelta);
if (logicalTopAfterClear != logicalTopEstimate || child.needsLayout() || (paginated && childBlockFlow && childBlockFlow->shouldBreakAtLineToAvoidWidow())) {
if (child.shrinkToAvoidFloats()) {
child.setChildNeedsLayout(MarkOnlyThis);
}
if (childBlockFlow) {
if (!child.avoidsFloats() && childBlockFlow->containsFloats())
childBlockFlow->markAllDescendantsWithFloatsForLayout();
if (!child.needsLayout())
child.markForPaginationRelayoutIfNeeded();
}
}
if (updateRegionRangeForBoxChild(child))
child.setNeedsLayout(MarkOnlyThis);
child.layoutIfNeeded();
if (marginInfo.atBeforeSideOfBlock() && !child.isSelfCollapsingBlock())
marginInfo.setAtBeforeSideOfBlock(false);
determineLogicalLeftPositionForChild(child, ApplyLayoutDelta);
setLogicalHeight(logicalHeight() + logicalHeightForChildForFragmentation(child));
if (mustSeparateMarginAfterForChild(child)) {
setLogicalHeight(logicalHeight() + marginAfterForChild(child));
marginInfo.clearMargin();
}
if (childBlockFlow && childBlockFlow->containsFloats())
maxFloatLogicalBottom = std::max(maxFloatLogicalBottom, addOverhangingFloats(*childBlockFlow, !childNeededLayout));
LayoutSize childOffset = child.location() - oldRect.location();
if (childOffset.width() || childOffset.height()) {
view().addLayoutDelta(childOffset);
if (childHadLayout && !selfNeedsLayout() && child.checkForRepaintDuringLayout())
child.repaintDuringLayoutIfMoved(oldRect);
}
if (!childHadLayout && child.checkForRepaintDuringLayout()) {
child.repaint();
child.repaintOverhangingFloats(true);
}
if (paginated) {
if (RenderFlowThread* flowThread = flowThreadContainingBlock())
flowThread->flowThreadDescendantBoxLaidOut(&child);
LayoutUnit newHeight = applyAfterBreak(child, logicalHeight(), marginInfo);
if (newHeight != height())
setLogicalHeight(newHeight);
}
ASSERT(view().layoutDeltaMatches(oldLayoutDelta));
}
void RenderBlockFlow::adjustPositionedBlock(RenderBox& child, const MarginInfo& marginInfo)
{
bool isHorizontal = isHorizontalWritingMode();
bool hasStaticBlockPosition = child.style().hasStaticBlockPosition(isHorizontal);
LayoutUnit logicalTop = logicalHeight();
updateStaticInlinePositionForChild(child, logicalTop);
if (!marginInfo.canCollapseWithMarginBefore()) {
LayoutUnit collapsedBeforePos = marginInfo.positiveMargin();
LayoutUnit collapsedBeforeNeg = marginInfo.negativeMargin();
logicalTop += collapsedBeforePos - collapsedBeforeNeg;
}
RenderLayer* childLayer = child.layer();
if (childLayer->staticBlockPosition() != logicalTop) {
childLayer->setStaticBlockPosition(logicalTop);
if (hasStaticBlockPosition)
child.setChildNeedsLayout(MarkOnlyThis);
}
}
LayoutUnit RenderBlockFlow::marginOffsetForSelfCollapsingBlock()
{
ASSERT(isSelfCollapsingBlock());
RenderBlockFlow* parentBlock = toRenderBlockFlow(parent());
if (parentBlock && style().clear() && parentBlock->getClearDelta(*this, logicalHeight()))
return marginValuesForChild(*this).positiveMarginBefore();
return LayoutUnit();
}
void RenderBlockFlow::determineLogicalLeftPositionForChild(RenderBox& child, ApplyLayoutDeltaMode applyDelta)
{
LayoutUnit startPosition = borderStart() + paddingStart();
if (style().shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
startPosition -= verticalScrollbarWidth();
LayoutUnit totalAvailableLogicalWidth = borderAndPaddingLogicalWidth() + availableLogicalWidth();
LayoutUnit childMarginStart = marginStartForChild(child);
LayoutUnit newPosition = startPosition + childMarginStart;
if (child.avoidsFloats() && containsFloats() && !flowThreadContainingBlock())
newPosition += computeStartPositionDeltaForChildAvoidingFloats(child, marginStartForChild(child));
setLogicalLeftForChild(child, style().isLeftToRightDirection() ? newPosition : totalAvailableLogicalWidth - newPosition - logicalWidthForChild(child), applyDelta);
}
void RenderBlockFlow::adjustFloatingBlock(const MarginInfo& marginInfo)
{
LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
setLogicalHeight(logicalHeight() + marginOffset);
positionNewFloats();
setLogicalHeight(logicalHeight() - marginOffset);
}
void RenderBlockFlow::updateStaticInlinePositionForChild(RenderBox& child, LayoutUnit logicalTop)
{
if (child.style().isOriginalDisplayInlineType())
setStaticInlinePositionForChild(child, logicalTop, startAlignedOffsetForLine(logicalTop, false));
else
setStaticInlinePositionForChild(child, logicalTop, startOffsetForContent(logicalTop));
}
void RenderBlockFlow::setStaticInlinePositionForChild(RenderBox& child, LayoutUnit blockOffset, LayoutUnit inlinePosition)
{
if (flowThreadContainingBlock()) {
inlinePosition += startOffsetForContent() - startOffsetForContent(blockOffset);
}
child.layer()->setStaticInlinePosition(inlinePosition);
}
RenderBlockFlow::MarginValues RenderBlockFlow::marginValuesForChild(RenderBox& child) const
{
LayoutUnit childBeforePositive = 0;
LayoutUnit childBeforeNegative = 0;
LayoutUnit childAfterPositive = 0;
LayoutUnit childAfterNegative = 0;
LayoutUnit beforeMargin = 0;
LayoutUnit afterMargin = 0;
RenderBlockFlow* childRenderBlock = child.isRenderBlockFlow() ? toRenderBlockFlow(&child) : nullptr;
if (!child.isWritingModeRoot()) {
if (childRenderBlock) {
childBeforePositive = childRenderBlock->maxPositiveMarginBefore();
childBeforeNegative = childRenderBlock->maxNegativeMarginBefore();
childAfterPositive = childRenderBlock->maxPositiveMarginAfter();
childAfterNegative = childRenderBlock->maxNegativeMarginAfter();
} else {
beforeMargin = child.marginBefore();
afterMargin = child.marginAfter();
}
} else if (child.isHorizontalWritingMode() == isHorizontalWritingMode()) {
if (childRenderBlock) {
childBeforePositive = childRenderBlock->maxPositiveMarginAfter();
childBeforeNegative = childRenderBlock->maxNegativeMarginAfter();
childAfterPositive = childRenderBlock->maxPositiveMarginBefore();
childAfterNegative = childRenderBlock->maxNegativeMarginBefore();
} else {
beforeMargin = child.marginAfter();
afterMargin = child.marginBefore();
}
} else {
beforeMargin = marginBeforeForChild(child);
afterMargin = marginAfterForChild(child);
}
if (beforeMargin) {
if (beforeMargin > 0)
childBeforePositive = beforeMargin;
else
childBeforeNegative = -beforeMargin;
}
if (afterMargin) {
if (afterMargin > 0)
childAfterPositive = afterMargin;
else
childAfterNegative = -afterMargin;
}
return MarginValues(childBeforePositive, childBeforeNegative, childAfterPositive, childAfterNegative);
}
LayoutUnit RenderBlockFlow::collapseMargins(RenderBox& child, MarginInfo& marginInfo)
{
bool childDiscardMarginBefore = mustDiscardMarginBeforeForChild(child);
bool childDiscardMarginAfter = mustDiscardMarginAfterForChild(child);
bool childIsSelfCollapsing = child.isSelfCollapsingBlock();
childDiscardMarginBefore = childDiscardMarginBefore || (childDiscardMarginAfter && childIsSelfCollapsing);
const MarginValues childMargins = marginValuesForChild(child);
LayoutUnit posTop = childMargins.positiveMarginBefore();
LayoutUnit negTop = childMargins.negativeMarginBefore();
if (childIsSelfCollapsing) {
posTop = std::max(posTop, childMargins.positiveMarginAfter());
negTop = std::max(negTop, childMargins.negativeMarginAfter());
}
bool topQuirk = hasMarginBeforeQuirk(child);
if (marginInfo.canCollapseWithMarginBefore()) {
if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
if (!document().inQuirksMode() || !marginInfo.quirkContainer() || !topQuirk)
setMaxMarginBeforeValues(std::max(posTop, maxPositiveMarginBefore()), std::max(negTop, maxNegativeMarginBefore()));
if (!marginInfo.determinedMarginBeforeQuirk() && !topQuirk && (posTop - negTop)) {
setHasMarginBeforeQuirk(false);
marginInfo.setDeterminedMarginBeforeQuirk(true);
}
if (!marginInfo.determinedMarginBeforeQuirk() && topQuirk && !marginBefore())
setHasMarginBeforeQuirk(true);
} else
setMustDiscardMarginBefore();
}
if (childDiscardMarginBefore) {
marginInfo.setDiscardMargin(true);
marginInfo.clearMargin();
}
if (marginInfo.quirkContainer() && marginInfo.atBeforeSideOfBlock() && (posTop - negTop))
marginInfo.setHasMarginBeforeQuirk(topQuirk);
LayoutUnit beforeCollapseLogicalTop = logicalHeight();
LayoutUnit logicalTop = beforeCollapseLogicalTop;
LayoutUnit clearanceForSelfCollapsingBlock;
RenderObject* prev = child.previousSibling();
if (!marginInfo.canCollapseWithMarginBefore() && prev && prev->isRenderBlockFlow() && toRenderBlockFlow(prev)->isSelfCollapsingBlock()) {
clearanceForSelfCollapsingBlock = toRenderBlockFlow(prev)->marginOffsetForSelfCollapsingBlock();
setLogicalHeight(logicalHeight() - clearanceForSelfCollapsingBlock);
}
if (childIsSelfCollapsing) {
if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
LayoutUnit collapsedBeforePos = std::max(marginInfo.positiveMargin(), childMargins.positiveMarginBefore());
LayoutUnit collapsedBeforeNeg = std::max(marginInfo.negativeMargin(), childMargins.negativeMarginBefore());
marginInfo.setMargin(collapsedBeforePos, collapsedBeforeNeg);
marginInfo.setPositiveMarginIfLarger(childMargins.positiveMarginAfter());
marginInfo.setNegativeMarginIfLarger(childMargins.negativeMarginAfter());
if (!marginInfo.canCollapseWithMarginBefore())
logicalTop = logicalHeight() + collapsedBeforePos - collapsedBeforeNeg;
}
} else {
if (mustSeparateMarginBeforeForChild(child)) {
ASSERT(!marginInfo.discardMargin() || (marginInfo.discardMargin() && !marginInfo.margin()));
LayoutUnit separateMargin = !marginInfo.canCollapseWithMarginBefore() ? marginInfo.margin() : LayoutUnit::fromPixel(0);
setLogicalHeight(logicalHeight() + separateMargin + marginBeforeForChild(child));
logicalTop = logicalHeight();
} else if (!marginInfo.discardMargin() && (!marginInfo.atBeforeSideOfBlock()
|| (!marginInfo.canCollapseMarginBeforeWithChildren()
&& (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginBeforeQuirk())))) {
setLogicalHeight(logicalHeight() + std::max(marginInfo.positiveMargin(), posTop) - std::max(marginInfo.negativeMargin(), negTop));
logicalTop = logicalHeight();
}
marginInfo.setDiscardMargin(childDiscardMarginAfter);
if (!marginInfo.discardMargin()) {
marginInfo.setPositiveMargin(childMargins.positiveMarginAfter());
marginInfo.setNegativeMargin(childMargins.negativeMarginAfter());
} else
marginInfo.clearMargin();
if (marginInfo.margin())
marginInfo.setHasMarginAfterQuirk(hasMarginAfterQuirk(child));
}
LayoutState* layoutState = view().layoutState();
if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTop > beforeCollapseLogicalTop
&& hasNextPage(beforeCollapseLogicalTop)) {
LayoutUnit oldLogicalTop = logicalTop;
logicalTop = std::min(logicalTop, nextPageLogicalTop(beforeCollapseLogicalTop));
setLogicalHeight(logicalHeight() + (logicalTop - oldLogicalTop));
}
if (prev && prev->isRenderBlockFlow() && !prev->isFloatingOrOutOfFlowPositioned()) {
RenderBlockFlow& block = toRenderBlockFlow(*prev);
LayoutUnit oldLogicalHeight = logicalHeight();
setLogicalHeight(logicalTop);
if (block.containsFloats() && !block.avoidsFloats() && (block.logicalTop() + block.lowestFloatLogicalBottom()) > logicalTop)
addOverhangingFloats(block, false);
setLogicalHeight(oldLogicalHeight);
bool logicalTopIntrudesIntoFloat = clearanceForSelfCollapsingBlock > 0 && logicalTop < beforeCollapseLogicalTop;
if (logicalTopIntrudesIntoFloat && containsFloats() && !child.avoidsFloats() && lowestFloatLogicalBottom() > logicalTop)
child.setNeedsLayout();
}
return logicalTop;
}
LayoutUnit RenderBlockFlow::clearFloatsIfNeeded(RenderBox& child, MarginInfo& marginInfo, LayoutUnit oldTopPosMargin, LayoutUnit oldTopNegMargin, LayoutUnit yPos)
{
LayoutUnit heightIncrease = getClearDelta(child, yPos);
if (!heightIncrease)
return yPos;
if (child.isSelfCollapsingBlock()) {
bool childDiscardMargin = mustDiscardMarginBeforeForChild(child) || mustDiscardMarginAfterForChild(child);
MarginValues childMargins = marginValuesForChild(child);
if (!childDiscardMargin) {
marginInfo.setPositiveMargin(std::max(childMargins.positiveMarginBefore(), childMargins.positiveMarginAfter()));
marginInfo.setNegativeMargin(std::max(childMargins.negativeMarginBefore(), childMargins.negativeMarginAfter()));
} else
marginInfo.clearMargin();
marginInfo.setDiscardMargin(childDiscardMargin);
bool wouldCollapseMarginsWithParent = marginInfo.canCollapseMarginAfterWithChildren();
for (RenderBox* curr = child.nextSiblingBox(); curr && wouldCollapseMarginsWithParent; curr = curr->nextSiblingBox()) {
if (!curr->isFloatingOrOutOfFlowPositioned() && !curr->isSelfCollapsingBlock())
wouldCollapseMarginsWithParent = false;
}
if (wouldCollapseMarginsWithParent)
marginInfo.setCanCollapseMarginAfterWithChildren(false);
setLogicalHeight(child.logicalTop() + childMargins.negativeMarginBefore());
} else
setLogicalHeight(logicalHeight() + heightIncrease);
if (marginInfo.canCollapseWithMarginBefore()) {
setMaxMarginBeforeValues(oldTopPosMargin, oldTopNegMargin);
marginInfo.setAtBeforeSideOfBlock(false);
setMustDiscardMarginBefore(style().marginBeforeCollapse() == MDISCARD);
}
return yPos + heightIncrease;
}
void RenderBlockFlow::marginBeforeEstimateForChild(RenderBox& child, LayoutUnit& positiveMarginBefore, LayoutUnit& negativeMarginBefore, bool& discardMarginBefore) const
{
if ((document().inQuirksMode() && hasMarginAfterQuirk(child) && (isTableCell() || isBody())) || child.style().marginBeforeCollapse() == MSEPARATE)
return;
if (child.style().marginBeforeCollapse() == MDISCARD) {
positiveMarginBefore = 0;
negativeMarginBefore = 0;
discardMarginBefore = true;
return;
}
LayoutUnit beforeChildMargin = marginBeforeForChild(child);
positiveMarginBefore = std::max(positiveMarginBefore, beforeChildMargin);
negativeMarginBefore = std::max(negativeMarginBefore, -beforeChildMargin);
if (!child.isRenderBlockFlow())
return;
RenderBlockFlow& childBlock = toRenderBlockFlow(child);
if (childBlock.childrenInline() || childBlock.isWritingModeRoot())
return;
MarginInfo childMarginInfo(childBlock, childBlock.borderAndPaddingBefore(), childBlock.borderAndPaddingAfter());
if (!childMarginInfo.canCollapseMarginBeforeWithChildren())
return;
RenderBox* grandchildBox = childBlock.firstChildBox();
for (; grandchildBox; grandchildBox = grandchildBox->nextSiblingBox()) {
if (!grandchildBox->isFloatingOrOutOfFlowPositioned())
break;
}
if (!grandchildBox || grandchildBox->style().clear() != CNONE)
return;
if (grandchildBox->needsLayout()) {
grandchildBox->computeAndSetBlockDirectionMargins(this);
if (grandchildBox->isRenderBlock()) {
RenderBlock* grandchildBlock = toRenderBlock(grandchildBox);
grandchildBlock->setHasMarginBeforeQuirk(grandchildBox->style().hasMarginBeforeQuirk());
grandchildBlock->setHasMarginAfterQuirk(grandchildBox->style().hasMarginAfterQuirk());
}
}
childBlock.marginBeforeEstimateForChild(*grandchildBox, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
}
LayoutUnit RenderBlockFlow::estimateLogicalTopPosition(RenderBox& child, const MarginInfo& marginInfo, LayoutUnit& estimateWithoutPagination)
{
LayoutUnit logicalTopEstimate = logicalHeight();
if (!marginInfo.canCollapseWithMarginBefore()) {
LayoutUnit positiveMarginBefore = 0;
LayoutUnit negativeMarginBefore = 0;
bool discardMarginBefore = false;
if (child.selfNeedsLayout()) {
marginBeforeEstimateForChild(child, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
} else {
MarginValues marginValues = marginValuesForChild(child);
positiveMarginBefore = std::max(positiveMarginBefore, marginValues.positiveMarginBefore());
negativeMarginBefore = std::max(negativeMarginBefore, marginValues.negativeMarginBefore());
discardMarginBefore = mustDiscardMarginBeforeForChild(child);
}
if (!discardMarginBefore)
logicalTopEstimate += std::max(marginInfo.positiveMargin(), positiveMarginBefore) - std::max(marginInfo.negativeMargin(), negativeMarginBefore);
}
LayoutState* layoutState = view().layoutState();
if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTopEstimate > logicalHeight()
&& hasNextPage(logicalHeight()))
logicalTopEstimate = std::min(logicalTopEstimate, nextPageLogicalTop(logicalHeight()));
logicalTopEstimate += getClearDelta(child, logicalTopEstimate);
estimateWithoutPagination = logicalTopEstimate;
if (layoutState->isPaginated()) {
logicalTopEstimate = applyBeforeBreak(child, logicalTopEstimate);
logicalTopEstimate = adjustForUnsplittableChild(child, logicalTopEstimate);
if (!child.selfNeedsLayout() && child.isRenderBlock())
logicalTopEstimate += toRenderBlock(child).paginationStrut();
}
return logicalTopEstimate;
}
void RenderBlockFlow::setCollapsedBottomMargin(const MarginInfo& marginInfo)
{
if (marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()) {
if (marginInfo.discardMargin()) {
setMustDiscardMarginAfter();
return;
}
setMaxMarginAfterValues(std::max(maxPositiveMarginAfter(), marginInfo.positiveMargin()), std::max(maxNegativeMarginAfter(), marginInfo.negativeMargin()));
if (!marginInfo.hasMarginAfterQuirk())
setHasMarginAfterQuirk(false);
if (marginInfo.hasMarginAfterQuirk() && !marginAfter())
setHasMarginAfterQuirk(true);
}
}
void RenderBlockFlow::handleAfterSideOfBlock(LayoutUnit beforeSide, LayoutUnit afterSide, MarginInfo& marginInfo)
{
marginInfo.setAtAfterSideOfBlock(true);
RenderObject* lastBlock = lastChild();
if (lastBlock && lastBlock->isRenderBlockFlow() && toRenderBlockFlow(lastBlock)->isSelfCollapsingBlock())
setLogicalHeight(logicalHeight() - toRenderBlockFlow(lastBlock)->marginOffsetForSelfCollapsingBlock());
if (!marginInfo.discardMargin() && (!marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()
&& (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginAfterQuirk())))
setLogicalHeight(logicalHeight() + marginInfo.margin());
setLogicalHeight(logicalHeight() + afterSide);
setLogicalHeight(std::max(logicalHeight(), beforeSide + afterSide));
setCollapsedBottomMargin(marginInfo);
}
void RenderBlockFlow::setMaxMarginBeforeValues(LayoutUnit pos, LayoutUnit neg)
{
if (!hasRareBlockFlowData()) {
if (pos == RenderBlockFlowRareData::positiveMarginBeforeDefault(*this) && neg == RenderBlockFlowRareData::negativeMarginBeforeDefault(*this))
return;
materializeRareBlockFlowData();
}
rareBlockFlowData()->m_margins.setPositiveMarginBefore(pos);
rareBlockFlowData()->m_margins.setNegativeMarginBefore(neg);
}
void RenderBlockFlow::setMaxMarginAfterValues(LayoutUnit pos, LayoutUnit neg)
{
if (!hasRareBlockFlowData()) {
if (pos == RenderBlockFlowRareData::positiveMarginAfterDefault(*this) && neg == RenderBlockFlowRareData::negativeMarginAfterDefault(*this))
return;
materializeRareBlockFlowData();
}
rareBlockFlowData()->m_margins.setPositiveMarginAfter(pos);
rareBlockFlowData()->m_margins.setNegativeMarginAfter(neg);
}
void RenderBlockFlow::setMustDiscardMarginBefore(bool value)
{
if (style().marginBeforeCollapse() == MDISCARD) {
ASSERT(value);
return;
}
if (!hasRareBlockFlowData()) {
if (!value)
return;
materializeRareBlockFlowData();
}
rareBlockFlowData()->m_discardMarginBefore = value;
}
void RenderBlockFlow::setMustDiscardMarginAfter(bool value)
{
if (style().marginAfterCollapse() == MDISCARD) {
ASSERT(value);
return;
}
if (!hasRareBlockFlowData()) {
if (!value)
return;
materializeRareBlockFlowData();
}
rareBlockFlowData()->m_discardMarginAfter = value;
}
bool RenderBlockFlow::mustDiscardMarginBefore() const
{
return style().marginBeforeCollapse() == MDISCARD || (hasRareBlockFlowData() && rareBlockFlowData()->m_discardMarginBefore);
}
bool RenderBlockFlow::mustDiscardMarginAfter() const
{
return style().marginAfterCollapse() == MDISCARD || (hasRareBlockFlowData() && rareBlockFlowData()->m_discardMarginAfter);
}
bool RenderBlockFlow::mustDiscardMarginBeforeForChild(const RenderBox& child) const
{
ASSERT(!child.selfNeedsLayout());
if (!child.isWritingModeRoot())
return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginBefore() : (child.style().marginBeforeCollapse() == MDISCARD);
if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginAfter() : (child.style().marginAfterCollapse() == MDISCARD);
return false;
}
bool RenderBlockFlow::mustDiscardMarginAfterForChild(const RenderBox& child) const
{
ASSERT(!child.selfNeedsLayout());
if (!child.isWritingModeRoot())
return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginAfter() : (child.style().marginAfterCollapse() == MDISCARD);
if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginBefore() : (child.style().marginBeforeCollapse() == MDISCARD);
return false;
}
bool RenderBlockFlow::mustSeparateMarginBeforeForChild(const RenderBox& child) const
{
ASSERT(!child.selfNeedsLayout());
const RenderStyle& childStyle = child.style();
if (!child.isWritingModeRoot())
return childStyle.marginBeforeCollapse() == MSEPARATE;
if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
return childStyle.marginAfterCollapse() == MSEPARATE;
return false;
}
bool RenderBlockFlow::mustSeparateMarginAfterForChild(const RenderBox& child) const
{
ASSERT(!child.selfNeedsLayout());
const RenderStyle& childStyle = child.style();
if (!child.isWritingModeRoot())
return childStyle.marginAfterCollapse() == MSEPARATE;
if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
return childStyle.marginBeforeCollapse() == MSEPARATE;
return false;
}
static bool inNormalFlow(RenderBox& child)
{
RenderBlock* curr = child.containingBlock();
while (curr && curr != &child.view()) {
if (curr->isRenderFlowThread())
return true;
if (curr->isFloatingOrOutOfFlowPositioned())
return false;
curr = curr->containingBlock();
}
return true;
}
LayoutUnit RenderBlockFlow::applyBeforeBreak(RenderBox& child, LayoutUnit logicalOffset)
{
RenderFlowThread* flowThread = flowThreadContainingBlock();
bool isInsideMulticolFlowThread = flowThread && !flowThread->isRenderNamedFlowThread();
bool checkColumnBreaks = flowThread && flowThread->shouldCheckColumnBreaks();
bool checkPageBreaks = !checkColumnBreaks && view().layoutState()->m_pageLogicalHeight; bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
bool checkBeforeAlways = (checkColumnBreaks && child.style().columnBreakBefore() == PBALWAYS)
|| (checkPageBreaks && child.style().pageBreakBefore() == PBALWAYS)
|| (checkRegionBreaks && child.style().regionBreakBefore() == PBALWAYS);
if (checkBeforeAlways && inNormalFlow(child) && hasNextPage(logicalOffset, IncludePageBoundary)) {
if (checkColumnBreaks) {
if (isInsideMulticolFlowThread)
checkRegionBreaks = true;
}
if (checkRegionBreaks) {
LayoutUnit offsetBreakAdjustment = 0;
if (flowThread->addForcedRegionBreak(this, offsetFromLogicalTopOfFirstPage() + logicalOffset, &child, true, &offsetBreakAdjustment))
return logicalOffset + offsetBreakAdjustment;
}
return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
}
return logicalOffset;
}
LayoutUnit RenderBlockFlow::applyAfterBreak(RenderBox& child, LayoutUnit logicalOffset, MarginInfo& marginInfo)
{
RenderFlowThread* flowThread = flowThreadContainingBlock();
bool isInsideMulticolFlowThread = flowThread && !flowThread->isRenderNamedFlowThread();
bool checkColumnBreaks = flowThread && flowThread->shouldCheckColumnBreaks();
bool checkPageBreaks = !checkColumnBreaks && view().layoutState()->m_pageLogicalHeight; bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
bool checkAfterAlways = (checkColumnBreaks && child.style().columnBreakAfter() == PBALWAYS)
|| (checkPageBreaks && child.style().pageBreakAfter() == PBALWAYS)
|| (checkRegionBreaks && child.style().regionBreakAfter() == PBALWAYS);
if (checkAfterAlways && inNormalFlow(child) && hasNextPage(logicalOffset, IncludePageBoundary)) {
LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
marginInfo.clearMargin();
if (checkColumnBreaks) {
if (isInsideMulticolFlowThread)
checkRegionBreaks = true;
}
if (checkRegionBreaks) {
LayoutUnit offsetBreakAdjustment = 0;
if (flowThread->addForcedRegionBreak(this, offsetFromLogicalTopOfFirstPage() + logicalOffset + marginOffset, &child, false, &offsetBreakAdjustment))
return logicalOffset + marginOffset + offsetBreakAdjustment;
}
return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
}
return logicalOffset;
}
LayoutUnit RenderBlockFlow::adjustBlockChildForPagination(LayoutUnit logicalTopAfterClear, LayoutUnit estimateWithoutPagination, RenderBox& child, bool atBeforeSideOfBlock)
{
RenderBlock* childRenderBlock = child.isRenderBlock() ? toRenderBlock(&child) : nullptr;
if (estimateWithoutPagination != logicalTopAfterClear) {
setLogicalHeight(logicalTopAfterClear);
setLogicalTopForChild(child, logicalTopAfterClear, ApplyLayoutDelta);
if (child.shrinkToAvoidFloats()) {
child.setChildNeedsLayout(MarkOnlyThis);
}
if (childRenderBlock) {
if (!child.avoidsFloats() && childRenderBlock->containsFloats())
toRenderBlockFlow(childRenderBlock)->markAllDescendantsWithFloatsForLayout();
if (!child.needsLayout())
child.markForPaginationRelayoutIfNeeded();
}
child.layoutIfNeeded();
}
LayoutUnit oldTop = logicalTopAfterClear;
LayoutUnit result = applyBeforeBreak(child, logicalTopAfterClear);
if (pageLogicalHeightForOffset(result)) {
LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(result, ExcludePageBoundary);
LayoutUnit spaceShortage = child.logicalHeight() - remainingLogicalHeight;
if (spaceShortage > 0) {
setPageBreak(result, spaceShortage);
}
}
LayoutUnit logicalTopBeforeUnsplittableAdjustment = result;
LayoutUnit logicalTopAfterUnsplittableAdjustment = adjustForUnsplittableChild(child, result);
LayoutUnit paginationStrut = 0;
LayoutUnit unsplittableAdjustmentDelta = logicalTopAfterUnsplittableAdjustment - logicalTopBeforeUnsplittableAdjustment;
if (unsplittableAdjustmentDelta)
paginationStrut = unsplittableAdjustmentDelta;
else if (childRenderBlock && childRenderBlock->paginationStrut())
paginationStrut = childRenderBlock->paginationStrut();
if (paginationStrut) {
if (atBeforeSideOfBlock && oldTop == result && !isOutOfFlowPositioned() && !isTableCell()) {
setPaginationStrut(result + paginationStrut);
if (childRenderBlock)
childRenderBlock->setPaginationStrut(0);
} else
result += paginationStrut;
}
setLogicalHeight(logicalHeight() + (result - oldTop));
return result;
}
static inline LayoutUnit calculateMinimumPageHeight(RenderStyle* renderStyle, RootInlineBox* lastLine, LayoutUnit lineTop, LayoutUnit lineBottom)
{
unsigned lineCount = std::max<unsigned>(renderStyle->hasAutoOrphans() ? 1 : renderStyle->orphans(), renderStyle->hasAutoWidows() ? 1 : renderStyle->widows());
if (lineCount > 1) {
RootInlineBox* line = lastLine;
for (unsigned i = 1; i < lineCount && line->prevRootBox(); i++)
line = line->prevRootBox();
LayoutRect overflow = line->logicalVisualOverflowRect(line->lineTop(), line->lineBottom());
lineTop = std::min(line->lineTopWithLeading(), overflow.y());
}
return lineBottom - lineTop;
}
void RenderBlockFlow::adjustLinePositionForPagination(RootInlineBox* lineBox, LayoutUnit& delta, bool& overflowsRegion, RenderFlowThread* flowThread)
{
overflowsRegion = false;
LayoutRect logicalVisualOverflow = lineBox->logicalVisualOverflowRect(lineBox->lineTop(), lineBox->lineBottom());
LayoutUnit logicalOffset = std::min(lineBox->lineTopWithLeading(), logicalVisualOverflow.y());
LayoutUnit logicalBottom = std::max(lineBox->lineBottomWithLeading(), logicalVisualOverflow.maxY());
LayoutUnit lineHeight = logicalBottom - logicalOffset;
updateMinimumPageHeight(logicalOffset, calculateMinimumPageHeight(&style(), lineBox, logicalOffset, logicalBottom));
logicalOffset += delta;
lineBox->setPaginationStrut(0);
lineBox->setIsFirstAfterPageBreak(false);
LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
if (!pageLogicalHeight || (hasUniformPageLogicalHeight && logicalVisualOverflow.height() > pageLogicalHeight)
|| !hasNextPage(logicalOffset))
return;
LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
overflowsRegion = (lineHeight > remainingLogicalHeight);
int lineIndex = lineCount(lineBox);
if (remainingLogicalHeight < lineHeight || (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex)) {
if (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex) {
clearShouldBreakAtLineToAvoidWidow();
setDidBreakAtLineToAvoidWidow();
}
if (!hasUniformPageLogicalHeight && !pushToNextPageWithMinimumLogicalHeight(remainingLogicalHeight, logicalOffset, lineHeight))
return;
if (lineHeight > pageLogicalHeight) {
remainingLogicalHeight -= std::min(lineHeight - pageLogicalHeight, std::max<LayoutUnit>(0, logicalVisualOverflow.y() - lineBox->lineTopWithLeading()));
}
LayoutUnit remainingLogicalHeightAtNewOffset = pageRemainingLogicalHeightForOffset(logicalOffset + remainingLogicalHeight, ExcludePageBoundary);
overflowsRegion = (lineHeight > remainingLogicalHeightAtNewOffset);
LayoutUnit totalLogicalHeight = lineHeight + std::max<LayoutUnit>(0, logicalOffset);
LayoutUnit pageLogicalHeightAtNewOffset = hasUniformPageLogicalHeight ? pageLogicalHeight : pageLogicalHeightForOffset(logicalOffset + remainingLogicalHeight);
setPageBreak(logicalOffset, lineHeight - remainingLogicalHeight);
if (((lineBox == firstRootBox() && totalLogicalHeight < pageLogicalHeightAtNewOffset) || (!style().hasAutoOrphans() && style().orphans() >= lineIndex))
&& !isOutOfFlowPositioned() && !isTableCell())
setPaginationStrut(remainingLogicalHeight + std::max<LayoutUnit>(0, logicalOffset));
else {
delta += remainingLogicalHeight;
lineBox->setPaginationStrut(remainingLogicalHeight);
lineBox->setIsFirstAfterPageBreak(true);
}
} else if (remainingLogicalHeight == pageLogicalHeight) {
if (lineBox != firstRootBox())
lineBox->setIsFirstAfterPageBreak(true);
if (lineBox != firstRootBox() || offsetFromLogicalTopOfFirstPage())
setPageBreak(logicalOffset, lineHeight);
}
}
void RenderBlockFlow::setBreakAtLineToAvoidWidow(int lineToBreak)
{
ASSERT(lineToBreak >= 0);
ASSERT(!ensureRareBlockFlowData().m_didBreakAtLineToAvoidWidow);
ensureRareBlockFlowData().m_lineBreakToAvoidWidow = lineToBreak;
}
void RenderBlockFlow::setDidBreakAtLineToAvoidWidow()
{
ASSERT(!shouldBreakAtLineToAvoidWidow());
if (!hasRareBlockFlowData())
return;
rareBlockFlowData()->m_didBreakAtLineToAvoidWidow = true;
}
void RenderBlockFlow::clearDidBreakAtLineToAvoidWidow()
{
if (!hasRareBlockFlowData())
return;
rareBlockFlowData()->m_didBreakAtLineToAvoidWidow = false;
}
void RenderBlockFlow::clearShouldBreakAtLineToAvoidWidow() const
{
ASSERT(shouldBreakAtLineToAvoidWidow());
if (!hasRareBlockFlowData())
return;
rareBlockFlowData()->m_lineBreakToAvoidWidow = -1;
}
bool RenderBlockFlow::relayoutToAvoidWidows(LayoutStateMaintainer& statePusher)
{
if (!shouldBreakAtLineToAvoidWidow())
return false;
statePusher.pop();
setEverHadLayout(true);
layoutBlock(false);
return true;
}
bool RenderBlockFlow::hasNextPage(LayoutUnit logicalOffset, PageBoundaryRule pageBoundaryRule) const
{
ASSERT(view().layoutState() && view().layoutState()->isPaginated());
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (!flowThread)
return true;
LayoutUnit pageOffset = offsetFromLogicalTopOfFirstPage() + logicalOffset;
RenderRegion* region = flowThread->regionAtBlockOffset(this, pageOffset, true);
if (!region)
return false;
if (region->isLastRegion())
return region->isRenderRegionSet() || region->style().regionFragment() == BreakRegionFragment
|| (pageBoundaryRule == IncludePageBoundary && pageOffset == region->logicalTopForFlowThreadContent());
RenderRegion* startRegion = nullptr;
RenderRegion* endRegion = nullptr;
flowThread->getRegionRangeForBox(this, startRegion, endRegion);
return (endRegion && region != endRegion);
}
LayoutUnit RenderBlockFlow::adjustForUnsplittableChild(RenderBox& child, LayoutUnit logicalOffset, bool includeMargins)
{
if (!childBoxIsUnsplittableForFragmentation(child))
return logicalOffset;
RenderFlowThread* flowThread = flowThreadContainingBlock();
LayoutUnit childLogicalHeight = logicalHeightForChild(child) + (includeMargins ? marginBeforeForChild(child) + marginAfterForChild(child) : LayoutUnit());
LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
updateMinimumPageHeight(logicalOffset, childLogicalHeight);
if (!pageLogicalHeight || (hasUniformPageLogicalHeight && childLogicalHeight > pageLogicalHeight)
|| !hasNextPage(logicalOffset))
return logicalOffset;
LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
if (remainingLogicalHeight < childLogicalHeight) {
if (!hasUniformPageLogicalHeight && !pushToNextPageWithMinimumLogicalHeight(remainingLogicalHeight, logicalOffset, childLogicalHeight))
return logicalOffset;
return logicalOffset + remainingLogicalHeight;
}
return logicalOffset;
}
bool RenderBlockFlow::pushToNextPageWithMinimumLogicalHeight(LayoutUnit& adjustment, LayoutUnit logicalOffset, LayoutUnit minimumLogicalHeight) const
{
bool checkRegion = false;
for (LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset + adjustment); pageLogicalHeight;
pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset + adjustment)) {
if (minimumLogicalHeight <= pageLogicalHeight)
return true;
if (!hasNextPage(logicalOffset + adjustment))
return false;
adjustment += pageLogicalHeight;
checkRegion = true;
}
return !checkRegion;
}
void RenderBlockFlow::setPageBreak(LayoutUnit offset, LayoutUnit spaceShortage)
{
if (RenderFlowThread* flowThread = flowThreadContainingBlock())
flowThread->setPageBreak(this, offsetFromLogicalTopOfFirstPage() + offset, spaceShortage);
}
void RenderBlockFlow::updateMinimumPageHeight(LayoutUnit offset, LayoutUnit minHeight)
{
if (RenderFlowThread* flowThread = flowThreadContainingBlock())
flowThread->updateMinimumPageHeight(this, offsetFromLogicalTopOfFirstPage() + offset, minHeight);
}
LayoutUnit RenderBlockFlow::nextPageLogicalTop(LayoutUnit logicalOffset, PageBoundaryRule pageBoundaryRule) const
{
LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
if (!pageLogicalHeight)
return logicalOffset;
LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset);
if (pageBoundaryRule == ExcludePageBoundary)
return logicalOffset + (remainingLogicalHeight ? remainingLogicalHeight : pageLogicalHeight);
return logicalOffset + remainingLogicalHeight;
}
LayoutUnit RenderBlockFlow::pageLogicalTopForOffset(LayoutUnit offset) const
{
LayoutUnit firstPageLogicalTop = isHorizontalWritingMode() ? view().layoutState()->m_pageOffset.height() : view().layoutState()->m_pageOffset.width();
LayoutUnit blockLogicalTop = isHorizontalWritingMode() ? view().layoutState()->m_layoutOffset.height() : view().layoutState()->m_layoutOffset.width();
LayoutUnit cumulativeOffset = offset + blockLogicalTop;
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (!flowThread) {
LayoutUnit pageLogicalHeight = view().layoutState()->pageLogicalHeight();
if (!pageLogicalHeight)
return 0;
return cumulativeOffset - roundToInt(cumulativeOffset - firstPageLogicalTop) % roundToInt(pageLogicalHeight);
}
return firstPageLogicalTop + flowThread->pageLogicalTopForOffset(cumulativeOffset - firstPageLogicalTop);
}
LayoutUnit RenderBlockFlow::pageLogicalHeightForOffset(LayoutUnit offset) const
{
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (!flowThread)
return view().layoutState()->m_pageLogicalHeight;
return flowThread->pageLogicalHeightForOffset(offset + offsetFromLogicalTopOfFirstPage());
}
LayoutUnit RenderBlockFlow::pageRemainingLogicalHeightForOffset(LayoutUnit offset, PageBoundaryRule pageBoundaryRule) const
{
offset += offsetFromLogicalTopOfFirstPage();
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (!flowThread) {
LayoutUnit pageLogicalHeight = view().layoutState()->m_pageLogicalHeight;
LayoutUnit remainingHeight = pageLogicalHeight - intMod(offset, pageLogicalHeight);
if (pageBoundaryRule == IncludePageBoundary) {
remainingHeight = intMod(remainingHeight, pageLogicalHeight);
}
return remainingHeight;
}
return flowThread->pageRemainingLogicalHeightForOffset(offset, pageBoundaryRule);
}
LayoutUnit RenderBlockFlow::logicalHeightForChildForFragmentation(const RenderBox& child) const
{
if (!flowThreadContainingBlock() || !flowThreadContainingBlock()->isRenderNamedFlowThread())
return logicalHeightForChild(child);
if (!childBoxIsUnsplittableForFragmentation(child) || !pageLogicalHeightForOffset(logicalTopForChild(child)))
return logicalHeightForChild(child);
LayoutUnit childLogicalTop = logicalTopForChild(child);
if (!hasNextPage(childLogicalTop))
return logicalHeightForChild(child);
LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(childLogicalTop, ExcludePageBoundary);
return std::min(child.logicalHeight(), remainingLogicalHeight);
}
void RenderBlockFlow::layoutLineGridBox()
{
if (style().lineGrid() == RenderStyle::initialLineGrid()) {
setLineGridBox(0);
return;
}
setLineGridBox(0);
auto lineGridBox = std::make_unique<RootInlineBox>(*this);
lineGridBox->setHasTextChildren(); lineGridBox->setConstructed();
GlyphOverflowAndFallbackFontsMap textBoxDataMap;
VerticalPositionCache verticalPositionCache;
lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
setLineGridBox(WTF::move(lineGridBox));
}
bool RenderBlockFlow::containsFloat(RenderBox& renderer) const
{
return m_floatingObjects && m_floatingObjects->set().contains<RenderBox&, FloatingObjectHashTranslator>(renderer);
}
void RenderBlockFlow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBlock::styleDidChange(diff, oldStyle);
bool canPropagateFloatIntoSibling = !isFloatingOrOutOfFlowPositioned() && !avoidsFloats();
if (diff == StyleDifferenceLayout && s_canPropagateFloatIntoSibling && !canPropagateFloatIntoSibling && hasOverhangingFloats()) {
RenderBlockFlow* parentBlock = this;
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
for (auto& ancestor : ancestorsOfType<RenderBlockFlow>(*this)) {
if (ancestor.isRenderView())
break;
if (ancestor.hasOverhangingFloats()) {
for (auto it = floatingObjectSet.begin(), end = floatingObjectSet.end(); it != end; ++it) {
RenderBox& renderer = (*it)->renderer();
if (ancestor.hasOverhangingFloat(renderer)) {
parentBlock = &ancestor;
break;
}
}
}
}
parentBlock->markAllDescendantsWithFloatsForLayout();
parentBlock->markSiblingsWithFloatsForLayout();
}
if (auto fragment = renderNamedFlowFragment())
fragment->setStyle(RenderNamedFlowFragment::createStyle(style()));
if (diff >= StyleDifferenceRepaint) {
if (selfNeedsLayout() || !m_simpleLineLayout || !SimpleLineLayout::canUseFor(*this))
invalidateLineLayoutPath();
}
if (multiColumnFlowThread())
updateStylesForColumnChildren();
}
void RenderBlockFlow::updateStylesForColumnChildren()
{
for (auto child = firstChildBox(); child && (child->isInFlowRenderFlowThread() || child->isRenderMultiColumnSet()); child = child->nextSiblingBox())
child->setStyle(RenderStyle::createAnonymousStyleWithDisplay(&style(), BLOCK));
}
void RenderBlockFlow::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
{
const RenderStyle* oldStyle = hasInitializedStyle() ? &style() : nullptr;
s_canPropagateFloatIntoSibling = oldStyle ? !isFloatingOrOutOfFlowPositioned() && !avoidsFloats() : false;
if (oldStyle) {
EPosition oldPosition = oldStyle->position();
EPosition newPosition = newStyle.position();
if (parent() && diff == StyleDifferenceLayout && oldPosition != newPosition) {
if (containsFloats() && !isFloating() && !isOutOfFlowPositioned() && newStyle.hasOutOfFlowPosition())
markAllDescendantsWithFloatsForLayout();
if (RenderFlowThread* flowThread = flowThreadContainingBlock(SkipFlowThreadCache)) {
if (flowThread->isRenderMultiColumnFlowThread() && !isOutOfFlowPositioned() && (newPosition == AbsolutePosition || newPosition == FixedPosition))
flowThread->removeFlowChildInfo(this);
}
}
}
RenderBlock::styleWillChange(diff, newStyle);
}
void RenderBlockFlow::deleteLines()
{
if (containsFloats())
m_floatingObjects->clearLineBoxTreePointers();
if (m_simpleLineLayout) {
ASSERT(!m_lineBoxes.firstLineBox());
m_simpleLineLayout = nullptr;
} else
m_lineBoxes.deleteLineBoxTree();
RenderBlock::deleteLines();
}
void RenderBlockFlow::moveFloatsTo(RenderBlockFlow* toBlockFlow)
{
if (m_floatingObjects) {
if (!toBlockFlow->m_floatingObjects)
toBlockFlow->createFloatingObjects();
const FloatingObjectSet& fromFloatingObjectSet = m_floatingObjects->set();
auto end = fromFloatingObjectSet.end();
for (auto it = fromFloatingObjectSet.begin(); it != end; ++it) {
FloatingObject* floatingObject = it->get();
if (toBlockFlow->containsFloat(floatingObject->renderer()))
continue;
toBlockFlow->m_floatingObjects->add(floatingObject->unsafeClone());
}
}
}
void RenderBlockFlow::moveAllChildrenIncludingFloatsTo(RenderBlock* toBlock, bool fullRemoveInsert)
{
RenderBlockFlow* toBlockFlow = toRenderBlockFlow(toBlock);
moveAllChildrenTo(toBlockFlow, fullRemoveInsert);
moveFloatsTo(toBlockFlow);
}
void RenderBlockFlow::addOverflowFromFloats()
{
if (!m_floatingObjects)
return;
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto end = floatingObjectSet.end();
for (auto it = floatingObjectSet.begin(); it != end; ++it) {
FloatingObject* r = it->get();
if (r->isDescendant())
addOverflowFromChild(&r->renderer(), IntSize(xPositionForFloatIncludingMargin(r), yPositionForFloatIncludingMargin(r)));
}
}
void RenderBlockFlow::computeOverflow(LayoutUnit oldClientAfterEdge, bool recomputeFloats)
{
RenderBlock::computeOverflow(oldClientAfterEdge, recomputeFloats);
if (!multiColumnFlowThread() && (recomputeFloats || isRoot() || expandsToEncloseOverhangingFloats() || hasSelfPaintingLayer()))
addOverflowFromFloats();
}
void RenderBlockFlow::repaintOverhangingFloats(bool paintAllDescendants)
{
if (!hasOverhangingFloats())
return;
LayoutStateDisabler layoutStateDisabler(&view());
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto end = floatingObjectSet.end();
for (auto it = floatingObjectSet.begin(); it != end; ++it) {
FloatingObject* floatingObject = it->get();
if (logicalBottomForFloat(floatingObject) > logicalHeight()
&& !floatingObject->renderer().hasSelfPaintingLayer()
&& (floatingObject->shouldPaint() || (paintAllDescendants && floatingObject->renderer().isDescendantOf(this)))) {
floatingObject->renderer().repaint();
floatingObject->renderer().repaintOverhangingFloats(false);
}
}
}
void RenderBlockFlow::paintColumnRules(PaintInfo& paintInfo, const LayoutPoint& point)
{
RenderBlock::paintColumnRules(paintInfo, point);
if (!multiColumnFlowThread() || paintInfo.context->paintingDisabled())
return;
for (auto& columnSet : childrenOfType<RenderMultiColumnSet>(*this)) {
LayoutPoint childPoint = columnSet.location() + flipForWritingModeForChild(&columnSet, point);
columnSet.paintColumnRules(paintInfo, childPoint);
}
}
void RenderBlockFlow::paintFloats(PaintInfo& paintInfo, const LayoutPoint& paintOffset, bool preservePhase)
{
if (!m_floatingObjects)
return;
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto end = floatingObjectSet.end();
for (auto it = floatingObjectSet.begin(); it != end; ++it) {
FloatingObject* r = it->get();
if (r->shouldPaint() && !r->renderer().hasSelfPaintingLayer()) {
PaintInfo currentPaintInfo(paintInfo);
currentPaintInfo.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
LayoutPoint childPoint = flipFloatForWritingModeForChild(r, LayoutPoint(paintOffset.x() + xPositionForFloatIncludingMargin(r) - r->renderer().x(), paintOffset.y() + yPositionForFloatIncludingMargin(r) - r->renderer().y()));
r->renderer().paint(currentPaintInfo, childPoint);
if (!preservePhase) {
currentPaintInfo.phase = PaintPhaseChildBlockBackgrounds;
r->renderer().paint(currentPaintInfo, childPoint);
currentPaintInfo.phase = PaintPhaseFloat;
r->renderer().paint(currentPaintInfo, childPoint);
currentPaintInfo.phase = PaintPhaseForeground;
r->renderer().paint(currentPaintInfo, childPoint);
currentPaintInfo.phase = PaintPhaseOutline;
r->renderer().paint(currentPaintInfo, childPoint);
}
}
}
}
void RenderBlockFlow::clipOutFloatingObjects(RenderBlock& rootBlock, const PaintInfo* paintInfo, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock)
{
if (m_floatingObjects) {
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto end = floatingObjectSet.end();
for (auto it = floatingObjectSet.begin(); it != end; ++it) {
FloatingObject* floatingObject = it->get();
LayoutRect floatBox(offsetFromRootBlock.width() + xPositionForFloatIncludingMargin(floatingObject),
offsetFromRootBlock.height() + yPositionForFloatIncludingMargin(floatingObject),
floatingObject->renderer().width(), floatingObject->renderer().height());
rootBlock.flipForWritingMode(floatBox);
floatBox.move(rootBlockPhysicalPosition.x(), rootBlockPhysicalPosition.y());
paintInfo->context->clipOut(pixelSnappedIntRect(floatBox));
}
}
}
void RenderBlockFlow::createFloatingObjects()
{
m_floatingObjects = std::make_unique<FloatingObjects>(*this);
}
void RenderBlockFlow::removeFloatingObjects()
{
if (!m_floatingObjects)
return;
markSiblingsWithFloatsForLayout();
m_floatingObjects->clear();
}
FloatingObject* RenderBlockFlow::insertFloatingObject(RenderBox& floatBox)
{
ASSERT(floatBox.isFloating());
if (!m_floatingObjects)
createFloatingObjects();
else {
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(floatBox);
if (it != floatingObjectSet.end())
return it->get();
}
std::unique_ptr<FloatingObject> floatingObject = FloatingObject::create(floatBox);
bool isChildRenderBlock = floatBox.isRenderBlock();
if (isChildRenderBlock && !floatBox.needsLayout() && view().layoutState()->pageLogicalHeightChanged())
floatBox.setChildNeedsLayout(MarkOnlyThis);
bool needsBlockDirectionLocationSetBeforeLayout = isChildRenderBlock && view().layoutState()->needsBlockDirectionLocationSetBeforeLayout();
if (!needsBlockDirectionLocationSetBeforeLayout || isWritingModeRoot()) floatBox.layoutIfNeeded();
else {
floatBox.updateLogicalWidth();
floatBox.computeAndSetBlockDirectionMargins(this);
}
setLogicalWidthForFloat(floatingObject.get(), logicalWidthForChild(floatBox) + marginStartForChild(floatBox) + marginEndForChild(floatBox));
return m_floatingObjects->add(WTF::move(floatingObject));
}
void RenderBlockFlow::removeFloatingObject(RenderBox& floatBox)
{
if (m_floatingObjects) {
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(floatBox);
if (it != floatingObjectSet.end()) {
FloatingObject* floatingObject = it->get();
if (childrenInline()) {
LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
if (logicalBottom < 0 || logicalBottom < logicalTop || logicalTop == LayoutUnit::max())
logicalBottom = LayoutUnit::max();
else {
logicalBottom = std::max(logicalBottom, logicalTop + 1);
}
if (floatingObject->originatingLine()) {
if (!selfNeedsLayout()) {
ASSERT(&floatingObject->originatingLine()->renderer() == this);
floatingObject->originatingLine()->markDirty();
}
#if !ASSERT_DISABLED
floatingObject->setOriginatingLine(0);
#endif
}
markLinesDirtyInBlockRange(0, logicalBottom);
}
m_floatingObjects->remove(floatingObject);
}
}
}
void RenderBlockFlow::removeFloatingObjectsBelow(FloatingObject* lastFloat, int logicalOffset)
{
if (!containsFloats())
return;
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
FloatingObject* curr = floatingObjectSet.last().get();
while (curr != lastFloat && (!curr->isPlaced() || logicalTopForFloat(curr) >= logicalOffset)) {
m_floatingObjects->remove(curr);
if (floatingObjectSet.isEmpty())
break;
curr = floatingObjectSet.last().get();
}
}
LayoutUnit RenderBlockFlow::logicalLeftOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
{
LayoutUnit offset = fixedOffset;
if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
offset = m_floatingObjects->logicalLeftOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
return adjustLogicalLeftOffsetForLine(offset, applyTextIndent);
}
LayoutUnit RenderBlockFlow::logicalRightOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
{
LayoutUnit offset = fixedOffset;
if (m_floatingObjects && m_floatingObjects->hasRightObjects())
offset = m_floatingObjects->logicalRightOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
return adjustLogicalRightOffsetForLine(offset, applyTextIndent);
}
LayoutPoint RenderBlockFlow::computeLogicalLocationForFloat(const FloatingObject* floatingObject, LayoutUnit logicalTopOffset) const
{
RenderBox& childBox = floatingObject->renderer();
LayoutUnit logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); LayoutUnit logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset);
LayoutUnit floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
LayoutUnit floatLogicalLeft;
bool insideFlowThread = flowThreadContainingBlock();
if (childBox.style().floating() == LeftFloat) {
LayoutUnit heightRemainingLeft = 1;
LayoutUnit heightRemainingRight = 1;
floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
while (logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight) - floatLogicalLeft < floatLogicalWidth) {
logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
if (insideFlowThread) {
logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
}
}
floatLogicalLeft = std::max(logicalLeftOffset - borderAndPaddingLogicalLeft(), floatLogicalLeft);
} else {
LayoutUnit heightRemainingLeft = 1;
LayoutUnit heightRemainingRight = 1;
floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
while (floatLogicalLeft - logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft) < floatLogicalWidth) {
logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
if (insideFlowThread) {
logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
}
}
floatLogicalLeft -= logicalWidthForFloat(floatingObject);
}
return LayoutPoint(floatLogicalLeft, logicalTopOffset);
}
bool RenderBlockFlow::positionNewFloats()
{
if (!m_floatingObjects)
return false;
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
if (floatingObjectSet.isEmpty())
return false;
if (floatingObjectSet.last()->isPlaced())
return false;
auto it = floatingObjectSet.end();
--it; auto begin = floatingObjectSet.begin();
FloatingObject* lastPlacedFloatingObject = 0;
while (it != begin) {
--it;
if ((*it)->isPlaced()) {
lastPlacedFloatingObject = it->get();
++it;
break;
}
}
LayoutUnit logicalTop = logicalHeight();
if (lastPlacedFloatingObject)
logicalTop = std::max(logicalTopForFloat(lastPlacedFloatingObject), logicalTop);
auto end = floatingObjectSet.end();
for (; it != end; ++it) {
FloatingObject* floatingObject = it->get();
if (floatingObject->renderer().containingBlock() != this)
continue;
RenderBox& childBox = floatingObject->renderer();
LayoutUnit childLogicalLeftMargin = style().isLeftToRightDirection() ? marginStartForChild(childBox) : marginEndForChild(childBox);
LayoutRect oldRect = childBox.frameRect();
if (childBox.style().clear() & CLEFT)
logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatLeft), logicalTop);
if (childBox.style().clear() & CRIGHT)
logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatRight), logicalTop);
LayoutPoint floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, logicalTop);
setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
estimateRegionRangeForBoxChild(childBox);
LayoutState* layoutState = view().layoutState();
bool isPaginated = layoutState->isPaginated();
if (isPaginated && !childBox.needsLayout())
childBox.markForPaginationRelayoutIfNeeded();
childBox.layoutIfNeeded();
if (isPaginated) {
LayoutUnit newLogicalTop = adjustForUnsplittableChild(childBox, floatLogicalLocation.y(), true);
RenderBlock* childBlock = childBox.isRenderBlock() ? toRenderBlock(&childBox) : nullptr;
if (childBlock && childBlock->paginationStrut()) {
newLogicalTop += childBlock->paginationStrut();
childBlock->setPaginationStrut(0);
}
if (newLogicalTop != floatLogicalLocation.y()) {
floatingObject->setPaginationStrut(newLogicalTop - floatLogicalLocation.y());
floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, newLogicalTop);
setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
if (childBlock)
childBlock->setChildNeedsLayout(MarkOnlyThis);
childBox.layoutIfNeeded();
}
if (updateRegionRangeForBoxChild(childBox)) {
childBox.setNeedsLayout(MarkOnlyThis);
childBox.layoutIfNeeded();
}
}
setLogicalTopForFloat(floatingObject, floatLogicalLocation.y());
setLogicalHeightForFloat(floatingObject, logicalHeightForChildForFragmentation(childBox) + marginBeforeForChild(childBox) + marginAfterForChild(childBox));
m_floatingObjects->addPlacedObject(floatingObject);
#if ENABLE(CSS_SHAPES)
if (ShapeOutsideInfo* shapeOutside = childBox.shapeOutsideInfo())
shapeOutside->setReferenceBoxLogicalSize(logicalSizeForChild(childBox));
#endif
if (childBox.checkForRepaintDuringLayout())
childBox.repaintDuringLayoutIfMoved(oldRect);
}
return true;
}
void RenderBlockFlow::clearFloats(EClear clear)
{
positionNewFloats();
LayoutUnit newY = 0;
switch (clear) {
case CLEFT:
newY = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
break;
case CRIGHT:
newY = lowestFloatLogicalBottom(FloatingObject::FloatRight);
break;
case CBOTH:
newY = lowestFloatLogicalBottom();
break;
default:
break;
}
if (height() < newY)
setLogicalHeight(newY);
}
LayoutUnit RenderBlockFlow::logicalLeftFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
{
if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
return m_floatingObjects->logicalLeftOffset(fixedOffset, logicalTop, logicalHeight);
return fixedOffset;
}
LayoutUnit RenderBlockFlow::logicalRightFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
{
if (m_floatingObjects && m_floatingObjects->hasRightObjects())
return m_floatingObjects->logicalRightOffset(fixedOffset, logicalTop, logicalHeight);
return fixedOffset;
}
LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelow(LayoutUnit logicalHeight) const
{
if (!m_floatingObjects)
return logicalHeight;
return m_floatingObjects->findNextFloatLogicalBottomBelow(logicalHeight);
}
LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelowForBlock(LayoutUnit logicalHeight) const
{
if (!m_floatingObjects)
return logicalHeight;
return m_floatingObjects->findNextFloatLogicalBottomBelowForBlock(logicalHeight);
}
LayoutUnit RenderBlockFlow::lowestFloatLogicalBottom(FloatingObject::Type floatType) const
{
if (!m_floatingObjects)
return 0;
LayoutUnit lowestFloatBottom = 0;
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto end = floatingObjectSet.end();
for (auto it = floatingObjectSet.begin(); it != end; ++it) {
FloatingObject* floatingObject = it->get();
if (floatingObject->isPlaced() && floatingObject->type() & floatType)
lowestFloatBottom = std::max(lowestFloatBottom, logicalBottomForFloat(floatingObject));
}
return lowestFloatBottom;
}
LayoutUnit RenderBlockFlow::addOverhangingFloats(RenderBlockFlow& child, bool makeChildPaintOtherFloats)
{
if (child.hasOverflowClip() || !child.containsFloats() || child.isRoot() || child.isWritingModeRoot() || child.isRenderFlowThread() || child.isRenderRegion())
return 0;
LayoutUnit childLogicalTop = child.logicalTop();
LayoutUnit childLogicalLeft = child.logicalLeft();
LayoutUnit lowestFloatLogicalBottom = 0;
auto childEnd = child.m_floatingObjects->set().end();
for (auto childIt = child.m_floatingObjects->set().begin(); childIt != childEnd; ++childIt) {
FloatingObject* floatingObject = childIt->get();
LayoutUnit floatLogicalBottom = std::min(logicalBottomForFloat(floatingObject), LayoutUnit::max() - childLogicalTop);
LayoutUnit logicalBottom = childLogicalTop + floatLogicalBottom;
lowestFloatLogicalBottom = std::max(lowestFloatLogicalBottom, logicalBottom);
if (logicalBottom > logicalHeight()) {
if (!containsFloat(floatingObject->renderer())) {
LayoutSize offset = isHorizontalWritingMode() ? LayoutSize(-childLogicalLeft, -childLogicalTop) : LayoutSize(-childLogicalTop, -childLogicalLeft);
bool shouldPaint = false;
if (floatingObject->renderer().enclosingFloatPaintingLayer() == enclosingFloatPaintingLayer()) {
floatingObject->setShouldPaint(false);
shouldPaint = true;
}
if (!m_floatingObjects)
createFloatingObjects();
m_floatingObjects->add(floatingObject->copyToNewContainer(offset, shouldPaint, true));
}
} else {
if (makeChildPaintOtherFloats && !floatingObject->shouldPaint() && !floatingObject->renderer().hasSelfPaintingLayer()
&& floatingObject->renderer().isDescendantOf(&child) && floatingObject->renderer().enclosingFloatPaintingLayer() == child.enclosingFloatPaintingLayer()) {
floatingObject->setShouldPaint(true);
}
if (floatingObject->isDescendant())
child.addOverflowFromChild(&floatingObject->renderer(), LayoutSize(xPositionForFloatIncludingMargin(floatingObject), yPositionForFloatIncludingMargin(floatingObject)));
}
}
return lowestFloatLogicalBottom;
}
bool RenderBlockFlow::hasOverhangingFloat(RenderBox& renderer)
{
if (!m_floatingObjects || !parent())
return false;
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(renderer);
if (it == floatingObjectSet.end())
return false;
return logicalBottomForFloat(it->get()) > logicalHeight();
}
void RenderBlockFlow::addIntrudingFloats(RenderBlockFlow* prev, LayoutUnit logicalLeftOffset, LayoutUnit logicalTopOffset)
{
ASSERT(!avoidsFloats());
if (!prev->m_floatingObjects)
return;
logicalLeftOffset += marginLogicalLeft();
const FloatingObjectSet& prevSet = prev->m_floatingObjects->set();
auto prevEnd = prevSet.end();
for (auto prevIt = prevSet.begin(); prevIt != prevEnd; ++prevIt) {
FloatingObject* floatingObject = prevIt->get();
if (logicalBottomForFloat(floatingObject) > logicalTopOffset) {
if (!m_floatingObjects || !m_floatingObjects->set().contains<FloatingObject&, FloatingObjectHashTranslator>(*floatingObject)) {
if (!m_floatingObjects)
createFloatingObjects();
LayoutSize offset = isHorizontalWritingMode()
? LayoutSize(logicalLeftOffset - (prev != parent() ? prev->marginLeft() : LayoutUnit()), logicalTopOffset)
: LayoutSize(logicalTopOffset, logicalLeftOffset - (prev != parent() ? prev->marginTop() : LayoutUnit()));
m_floatingObjects->add(floatingObject->copyToNewContainer(offset));
}
}
}
}
void RenderBlockFlow::markAllDescendantsWithFloatsForLayout(RenderBox* floatToRemove, bool inLayout)
{
if (!everHadLayout() && !containsFloats())
return;
MarkingBehavior markParents = inLayout ? MarkOnlyThis : MarkContainingBlockChain;
setChildNeedsLayout(markParents);
if (floatToRemove)
removeFloatingObject(*floatToRemove);
if (childrenInline())
return;
for (auto& block : childrenOfType<RenderBlock>(*this)) {
if (!floatToRemove && block.isFloatingOrOutOfFlowPositioned())
continue;
if (!block.isRenderBlockFlow()) {
if (block.shrinkToAvoidFloats() && block.everHadLayout())
block.setChildNeedsLayout(markParents);
continue;
}
auto& blockFlow = toRenderBlockFlow(block);
if ((floatToRemove ? blockFlow.containsFloat(*floatToRemove) : blockFlow.containsFloats()) || blockFlow.shrinkToAvoidFloats())
blockFlow.markAllDescendantsWithFloatsForLayout(floatToRemove, inLayout);
}
}
void RenderBlockFlow::markSiblingsWithFloatsForLayout(RenderBox* floatToRemove)
{
if (!m_floatingObjects)
return;
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto end = floatingObjectSet.end();
for (RenderObject* next = nextSibling(); next; next = next->nextSibling()) {
if (!next->isRenderBlockFlow() || next->isFloatingOrOutOfFlowPositioned() || toRenderBlock(next)->avoidsFloats())
continue;
RenderBlockFlow* nextBlock = toRenderBlockFlow(next);
for (auto it = floatingObjectSet.begin(); it != end; ++it) {
RenderBox& floatingBox = (*it)->renderer();
if (floatToRemove && &floatingBox != floatToRemove)
continue;
if (nextBlock->containsFloat(floatingBox))
nextBlock->markAllDescendantsWithFloatsForLayout(&floatingBox);
}
}
}
LayoutPoint RenderBlockFlow::flipFloatForWritingModeForChild(const FloatingObject* child, const LayoutPoint& point) const
{
if (!style().isFlippedBlocksWritingMode())
return point;
if (isHorizontalWritingMode())
return LayoutPoint(point.x(), point.y() + height() - child->renderer().height() - 2 * yPositionForFloatIncludingMargin(child));
return LayoutPoint(point.x() + width() - child->renderer().width() - 2 * xPositionForFloatIncludingMargin(child), point.y());
}
LayoutUnit RenderBlockFlow::getClearDelta(RenderBox& child, LayoutUnit logicalTop)
{
if (!containsFloats())
return 0;
bool clearSet = child.style().clear() != CNONE;
LayoutUnit logicalBottom = 0;
switch (child.style().clear()) {
case CNONE:
break;
case CLEFT:
logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
break;
case CRIGHT:
logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatRight);
break;
case CBOTH:
logicalBottom = lowestFloatLogicalBottom();
break;
}
LayoutUnit result = clearSet ? std::max<LayoutUnit>(0, logicalBottom - logicalTop) : LayoutUnit();
if (!result && child.avoidsFloats()) {
LayoutUnit newLogicalTop = logicalTop;
while (true) {
LayoutUnit availableLogicalWidthAtNewLogicalTopOffset = availableLogicalWidthForLine(newLogicalTop, false, logicalHeightForChild(child));
if (availableLogicalWidthAtNewLogicalTopOffset == availableLogicalWidthForContent(newLogicalTop))
return newLogicalTop - logicalTop;
RenderRegion* region = regionAtBlockOffset(logicalTopForChild(child));
LayoutRect borderBox = child.borderBoxRectInRegion(region, DoNotCacheRenderBoxRegionInfo);
LayoutUnit childLogicalWidthAtOldLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
LayoutUnit childOldLogicalWidth = child.logicalWidth();
LayoutUnit childOldMarginLeft = child.marginLeft();
LayoutUnit childOldMarginRight = child.marginRight();
LayoutUnit childOldLogicalTop = child.logicalTop();
child.setLogicalTop(newLogicalTop);
child.updateLogicalWidth();
region = regionAtBlockOffset(logicalTopForChild(child));
borderBox = child.borderBoxRectInRegion(region, DoNotCacheRenderBoxRegionInfo);
LayoutUnit childLogicalWidthAtNewLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
child.setLogicalTop(childOldLogicalTop);
child.setLogicalWidth(childOldLogicalWidth);
child.setMarginLeft(childOldMarginLeft);
child.setMarginRight(childOldMarginRight);
if (childLogicalWidthAtNewLogicalTopOffset <= availableLogicalWidthAtNewLogicalTopOffset) {
if (childLogicalWidthAtOldLogicalTopOffset != childLogicalWidthAtNewLogicalTopOffset)
child.setChildNeedsLayout(MarkOnlyThis);
return newLogicalTop - logicalTop;
}
newLogicalTop = nextFloatLogicalBottomBelowForBlock(newLogicalTop);
ASSERT(newLogicalTop >= logicalTop);
if (newLogicalTop < logicalTop)
break;
}
ASSERT_NOT_REACHED();
}
return result;
}
bool RenderBlockFlow::hitTestFloats(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset)
{
if (!m_floatingObjects)
return false;
LayoutPoint adjustedLocation = accumulatedOffset;
if (isRenderView())
adjustedLocation += toLayoutSize(toRenderView(*this).frameView().scrollPosition());
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto begin = floatingObjectSet.begin();
for (auto it = floatingObjectSet.end(); it != begin;) {
--it;
FloatingObject* floatingObject = it->get();
if (floatingObject->shouldPaint() && !floatingObject->renderer().hasSelfPaintingLayer()) {
LayoutUnit xOffset = xPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer().x();
LayoutUnit yOffset = yPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer().y();
LayoutPoint childPoint = flipFloatForWritingModeForChild(floatingObject, adjustedLocation + LayoutSize(xOffset, yOffset));
if (floatingObject->renderer().hitTest(request, result, locationInContainer, childPoint)) {
updateHitTestResult(result, locationInContainer.point() - toLayoutSize(childPoint));
return true;
}
}
}
return false;
}
bool RenderBlockFlow::hitTestInlineChildren(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
{
ASSERT(childrenInline());
if (auto simpleLineLayout = this->simpleLineLayout())
return SimpleLineLayout::hitTestFlow(*this, *simpleLineLayout, request, result, locationInContainer, accumulatedOffset, hitTestAction);
return m_lineBoxes.hitTest(this, request, result, locationInContainer, accumulatedOffset, hitTestAction);
}
void RenderBlockFlow::adjustForBorderFit(LayoutUnit x, LayoutUnit& left, LayoutUnit& right) const
{
if (style().visibility() != VISIBLE)
return;
if (childrenInline()) {
const_cast<RenderBlockFlow&>(*this).ensureLineBoxes();
for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
if (box->firstChild())
left = std::min(left, x + LayoutUnit(box->firstChild()->x()));
if (box->lastChild())
right = std::max(right, x + LayoutUnit(ceilf(box->lastChild()->logicalRight())));
}
} else {
for (RenderBox* obj = firstChildBox(); obj; obj = obj->nextSiblingBox()) {
if (!obj->isFloatingOrOutOfFlowPositioned()) {
if (obj->isRenderBlockFlow() && !obj->hasOverflowClip())
toRenderBlockFlow(obj)->adjustForBorderFit(x + obj->x(), left, right);
else if (obj->style().visibility() == VISIBLE) {
left = std::min(left, x + obj->x());
right = std::max(right, x + obj->x() + obj->width());
}
}
}
}
if (m_floatingObjects) {
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
auto end = floatingObjectSet.end();
for (auto it = floatingObjectSet.begin(); it != end; ++it) {
FloatingObject* r = it->get();
if (r->shouldPaint()) {
LayoutUnit floatLeft = xPositionForFloatIncludingMargin(r) - r->renderer().x();
LayoutUnit floatRight = floatLeft + r->renderer().width();
left = std::min(left, floatLeft);
right = std::max(right, floatRight);
}
}
}
}
void RenderBlockFlow::fitBorderToLinesIfNeeded()
{
if (style().borderFit() == BorderFitBorder || hasOverrideWidth())
return;
LayoutUnit left = LayoutUnit::max();
LayoutUnit right = LayoutUnit::min();
LayoutUnit oldWidth = contentWidth();
adjustForBorderFit(0, left, right);
LayoutUnit leftEdge = borderLeft() + paddingLeft();
LayoutUnit rightEdge = leftEdge + oldWidth;
left = std::min(rightEdge, std::max(leftEdge, left));
right = std::max(leftEdge, std::min(rightEdge, right));
LayoutUnit newContentWidth = right - left;
if (newContentWidth == oldWidth)
return;
setOverrideLogicalContentWidth(newContentWidth);
layoutBlock(false);
clearOverrideLogicalContentWidth();
}
void RenderBlockFlow::markLinesDirtyInBlockRange(LayoutUnit logicalTop, LayoutUnit logicalBottom, RootInlineBox* highest)
{
if (logicalTop >= logicalBottom)
return;
if (m_simpleLineLayout) {
invalidateLineLayoutPath();
return;
}
RootInlineBox* lowestDirtyLine = lastRootBox();
RootInlineBox* afterLowest = lowestDirtyLine;
while (lowestDirtyLine && lowestDirtyLine->lineBottomWithLeading() >= logicalBottom && logicalBottom < LayoutUnit::max()) {
afterLowest = lowestDirtyLine;
lowestDirtyLine = lowestDirtyLine->prevRootBox();
}
while (afterLowest && afterLowest != highest && (afterLowest->lineBottomWithLeading() >= logicalTop || afterLowest->lineBottomWithLeading() < 0)) {
afterLowest->markDirty();
afterLowest = afterLowest->prevRootBox();
}
}
int RenderBlockFlow::firstLineBaseline() const
{
if (isWritingModeRoot() && !isRubyRun())
return -1;
if (!childrenInline())
return RenderBlock::firstLineBaseline();
if (!hasLines())
return -1;
if (auto simpleLineLayout = this->simpleLineLayout())
return SimpleLineLayout::computeFlowFirstLineBaseline(*this, *simpleLineLayout);
ASSERT(firstRootBox());
return firstRootBox()->logicalTop() + firstLineStyle().fontMetrics().ascent(firstRootBox()->baselineType());
}
int RenderBlockFlow::inlineBlockBaseline(LineDirectionMode lineDirection) const
{
if (isWritingModeRoot() && !isRubyRun())
return -1;
if (!childrenInline())
return RenderBlock::inlineBlockBaseline(lineDirection);
if (!hasLines()) {
if (!hasLineIfEmpty())
return -1;
const FontMetrics& fontMetrics = firstLineStyle().fontMetrics();
return fontMetrics.ascent()
+ (lineHeight(true, lineDirection, PositionOfInteriorLineBoxes) - fontMetrics.height()) / 2
+ (lineDirection == HorizontalLine ? borderTop() + paddingTop() : borderRight() + paddingRight());
}
if (auto simpleLineLayout = this->simpleLineLayout())
return SimpleLineLayout::computeFlowLastLineBaseline(*this, *simpleLineLayout);
bool isFirstLine = lastRootBox() == firstRootBox();
const RenderStyle& style = isFirstLine ? firstLineStyle() : this->style();
return lastRootBox()->logicalTop() + style.fontMetrics().ascent(lastRootBox()->baselineType());
}
GapRects RenderBlockFlow::inlineSelectionGaps(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
{
ASSERT(!m_simpleLineLayout);
GapRects result;
bool containsStart = selectionState() == SelectionStart || selectionState() == SelectionBoth;
if (!hasLines()) {
if (containsStart) {
lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalHeight();
lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, logicalHeight(), cache);
lastLogicalRight = logicalRightSelectionOffset(rootBlock, logicalHeight(), cache);
}
return result;
}
RootInlineBox* lastSelectedLine = 0;
RootInlineBox* curr;
for (curr = firstRootBox(); curr && !curr->hasSelectedChildren(); curr = curr->nextRootBox()) { }
for (; curr && curr->hasSelectedChildren(); curr = curr->nextRootBox()) {
LayoutUnit selTop = curr->selectionTopAdjustedForPrecedingBlock();
LayoutUnit selHeight = curr->selectionHeightAdjustedForPrecedingBlock();
if (!containsStart && !lastSelectedLine &&
selectionState() != SelectionStart && selectionState() != SelectionBoth)
result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop, lastLogicalLeft, lastLogicalRight, selTop, cache, paintInfo));
LayoutRect logicalRect(curr->logicalLeft(), selTop, curr->logicalWidth(), selTop + selHeight);
logicalRect.move(isHorizontalWritingMode() ? offsetFromRootBlock : offsetFromRootBlock.transposedSize());
LayoutRect physicalRect = rootBlock.logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
if (!paintInfo || (isHorizontalWritingMode() && physicalRect.y() < paintInfo->rect.maxY() && physicalRect.maxY() > paintInfo->rect.y())
|| (!isHorizontalWritingMode() && physicalRect.x() < paintInfo->rect.maxX() && physicalRect.maxX() > paintInfo->rect.x()))
result.unite(curr->lineSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, selTop, selHeight, cache, paintInfo));
lastSelectedLine = curr;
}
if (containsStart && !lastSelectedLine)
lastSelectedLine = lastRootBox();
if (lastSelectedLine && selectionState() != SelectionEnd && selectionState() != SelectionBoth) {
lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + lastSelectedLine->selectionBottom();
lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
lastLogicalRight = logicalRightSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
}
return result;
}
void RenderBlockFlow::createRenderNamedFlowFragmentIfNeeded()
{
if (!document().cssRegionsEnabled() || renderNamedFlowFragment() || isRenderNamedFlowFragment())
return;
if (style().isDisplayRegionType() && style().hasFlowFrom()) {
RenderNamedFlowFragment* flowFragment = new RenderNamedFlowFragment(document(), RenderNamedFlowFragment::createStyle(style()));
flowFragment->initializeStyle();
setRenderNamedFlowFragment(flowFragment);
addChild(renderNamedFlowFragment());
}
}
bool RenderBlockFlow::needsLayoutAfterRegionRangeChange() const
{
if (!containsFloats() || expandsToEncloseOverhangingFloats())
return false;
return true;
}
bool RenderBlockFlow::canHaveChildren() const
{
return !renderNamedFlowFragment() ? RenderBlock::canHaveChildren() : renderNamedFlowFragment()->canHaveChildren();
}
bool RenderBlockFlow::canHaveGeneratedChildren() const
{
return !renderNamedFlowFragment() ? RenderBlock::canHaveGeneratedChildren() : renderNamedFlowFragment()->canHaveGeneratedChildren();
}
bool RenderBlockFlow::namedFlowFragmentNeedsUpdate() const
{
if (!isRenderNamedFlowFragmentContainer())
return false;
return hasRelativeLogicalHeight() && !isRenderView();
}
void RenderBlockFlow::updateLogicalHeight()
{
RenderBlock::updateLogicalHeight();
if (renderNamedFlowFragment()) {
renderNamedFlowFragment()->setLogicalHeight(std::max<LayoutUnit>(0, logicalHeight() - borderAndPaddingLogicalHeight()));
renderNamedFlowFragment()->invalidateRegionIfNeeded();
}
}
void RenderBlockFlow::setRenderNamedFlowFragment(RenderNamedFlowFragment* flowFragment)
{
RenderBlockFlowRareData& rareData = ensureRareBlockFlowData();
if (rareData.m_renderNamedFlowFragment)
rareData.m_renderNamedFlowFragment->destroy();
rareData.m_renderNamedFlowFragment = flowFragment;
}
void RenderBlockFlow::setMultiColumnFlowThread(RenderMultiColumnFlowThread* flowThread)
{
if (flowThread || hasRareBlockFlowData()) {
RenderBlockFlowRareData& rareData = ensureRareBlockFlowData();
rareData.m_multiColumnFlowThread = flowThread;
}
}
static bool shouldCheckLines(const RenderBlockFlow& blockFlow)
{
return !blockFlow.isFloatingOrOutOfFlowPositioned() && blockFlow.style().height().isAuto();
}
RootInlineBox* RenderBlockFlow::lineAtIndex(int i) const
{
ASSERT(i >= 0);
if (style().visibility() != VISIBLE)
return nullptr;
if (childrenInline()) {
for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
if (!i--)
return box;
}
return nullptr;
}
for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
if (!shouldCheckLines(blockFlow))
continue;
if (RootInlineBox* box = blockFlow.lineAtIndex(i))
return box;
}
return nullptr;
}
int RenderBlockFlow::lineCount(const RootInlineBox* stopRootInlineBox, bool* found) const
{
if (style().visibility() != VISIBLE)
return 0;
int count = 0;
if (childrenInline()) {
if (auto simpleLineLayout = this->simpleLineLayout()) {
ASSERT(!stopRootInlineBox);
return simpleLineLayout->lineCount();
}
for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
count++;
if (box == stopRootInlineBox) {
if (found)
*found = true;
break;
}
}
return count;
}
for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
if (!shouldCheckLines(blockFlow))
continue;
bool recursiveFound = false;
count += blockFlow.lineCount(stopRootInlineBox, &recursiveFound);
if (recursiveFound) {
if (found)
*found = true;
break;
}
}
return count;
}
static int getHeightForLineCount(const RenderBlockFlow& block, int lineCount, bool includeBottom, int& count)
{
if (block.style().visibility() != VISIBLE)
return -1;
if (block.childrenInline()) {
for (auto box = block.firstRootBox(); box; box = box->nextRootBox()) {
if (++count == lineCount)
return box->lineBottom() + (includeBottom ? (block.borderBottom() + block.paddingBottom()) : LayoutUnit());
}
} else {
RenderBox* normalFlowChildWithoutLines = 0;
for (auto obj = block.firstChildBox(); obj; obj = obj->nextSiblingBox()) {
if (obj->isRenderBlockFlow() && shouldCheckLines(toRenderBlockFlow(*obj))) {
int result = getHeightForLineCount(toRenderBlockFlow(*obj), lineCount, false, count);
if (result != -1)
return result + obj->y() + (includeBottom ? (block.borderBottom() + block.paddingBottom()) : LayoutUnit());
} else if (!obj->isFloatingOrOutOfFlowPositioned())
normalFlowChildWithoutLines = obj;
}
if (normalFlowChildWithoutLines && !lineCount)
return normalFlowChildWithoutLines->y() + normalFlowChildWithoutLines->height();
}
return -1;
}
int RenderBlockFlow::heightForLineCount(int lineCount)
{
int count = 0;
return getHeightForLineCount(*this, lineCount, true, count);
}
void RenderBlockFlow::clearTruncation()
{
if (style().visibility() != VISIBLE)
return;
if (childrenInline() && hasMarkupTruncation()) {
ensureLineBoxes();
setHasMarkupTruncation(false);
for (auto box = firstRootBox(); box; box = box->nextRootBox())
box->clearTruncation();
return;
}
for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
if (shouldCheckLines(blockFlow))
blockFlow.clearTruncation();
}
}
bool RenderBlockFlow::containsNonZeroBidiLevel() const
{
for (auto root = firstRootBox(); root; root = root->nextRootBox()) {
for (auto box = root->firstLeafChild(); box; box = box->nextLeafChild()) {
if (box->bidiLevel())
return true;
}
}
return false;
}
Position RenderBlockFlow::positionForBox(InlineBox *box, bool start) const
{
if (!box)
return Position();
if (!box->renderer().nonPseudoNode())
return createLegacyEditingPosition(nonPseudoElement(), start ? caretMinOffset() : caretMaxOffset());
if (!box->isInlineTextBox())
return createLegacyEditingPosition(box->renderer().nonPseudoNode(), start ? box->renderer().caretMinOffset() : box->renderer().caretMaxOffset());
InlineTextBox* textBox = toInlineTextBox(box);
return createLegacyEditingPosition(box->renderer().nonPseudoNode(), start ? textBox->start() : textBox->start() + textBox->len());
}
VisiblePosition RenderBlockFlow::positionForPointWithInlineChildren(const LayoutPoint& pointInLogicalContents, const RenderRegion* region)
{
ASSERT(childrenInline());
ensureLineBoxes();
if (!firstRootBox())
return createVisiblePosition(0, DOWNSTREAM);
bool linesAreFlipped = style().isFlippedLinesWritingMode();
bool blocksAreFlipped = style().isFlippedBlocksWritingMode();
InlineBox* closestBox = 0;
RootInlineBox* firstRootBoxWithChildren = 0;
RootInlineBox* lastRootBoxWithChildren = 0;
for (RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox()) {
if (region && root->containingRegion() != region)
continue;
if (!root->firstLeafChild())
continue;
if (!firstRootBoxWithChildren)
firstRootBoxWithChildren = root;
if (!linesAreFlipped && root->isFirstAfterPageBreak() && (pointInLogicalContents.y() < root->lineTopWithLeading()
|| (blocksAreFlipped && pointInLogicalContents.y() == root->lineTopWithLeading())))
break;
lastRootBoxWithChildren = root;
if (pointInLogicalContents.y() < root->selectionBottom() || (blocksAreFlipped && pointInLogicalContents.y() == root->selectionBottom())) {
if (linesAreFlipped) {
RootInlineBox* nextRootBoxWithChildren = root->nextRootBox();
while (nextRootBoxWithChildren && !nextRootBoxWithChildren->firstLeafChild())
nextRootBoxWithChildren = nextRootBoxWithChildren->nextRootBox();
if (nextRootBoxWithChildren && nextRootBoxWithChildren->isFirstAfterPageBreak() && (pointInLogicalContents.y() > nextRootBoxWithChildren->lineTopWithLeading()
|| (!blocksAreFlipped && pointInLogicalContents.y() == nextRootBoxWithChildren->lineTopWithLeading())))
continue;
}
closestBox = root->closestLeafChildForLogicalLeftPosition(pointInLogicalContents.x());
if (closestBox)
break;
}
}
bool moveCaretToBoundary = frame().editor().behavior().shouldMoveCaretToHorizontalBoundaryWhenPastTopOrBottom();
if (!moveCaretToBoundary && !closestBox && lastRootBoxWithChildren) {
closestBox = lastRootBoxWithChildren->closestLeafChildForLogicalLeftPosition(pointInLogicalContents.x());
}
if (closestBox) {
if (moveCaretToBoundary) {
LayoutUnit firstRootBoxWithChildrenTop = std::min<LayoutUnit>(firstRootBoxWithChildren->selectionTop(), firstRootBoxWithChildren->logicalTop());
if (pointInLogicalContents.y() < firstRootBoxWithChildrenTop
|| (blocksAreFlipped && pointInLogicalContents.y() == firstRootBoxWithChildrenTop)) {
InlineBox* box = firstRootBoxWithChildren->firstLeafChild();
if (box->isLineBreak()) {
if (InlineBox* newBox = box->nextLeafChildIgnoringLineBreak())
box = newBox;
}
return VisiblePosition(positionForBox(box, true), DOWNSTREAM);
}
}
LayoutPoint point(pointInLogicalContents.x(), closestBox->root().blockDirectionPointInLine());
if (!isHorizontalWritingMode())
point = point.transposedPoint();
if (closestBox->renderer().isReplaced())
return positionForPointRespectingEditingBoundaries(*this, toRenderBox(closestBox->renderer()), point);
return closestBox->renderer().positionForPoint(point, nullptr);
}
if (lastRootBoxWithChildren) {
ASSERT(moveCaretToBoundary);
InlineBox* logicallyLastBox;
if (lastRootBoxWithChildren->getLogicalEndBoxWithNode(logicallyLastBox))
return VisiblePosition(positionForBox(logicallyLastBox, false), DOWNSTREAM);
}
return createVisiblePosition(0, DOWNSTREAM);
}
VisiblePosition RenderBlockFlow::positionForPoint(const LayoutPoint& point, const RenderRegion* region)
{
if (auto fragment = renderNamedFlowFragment())
return fragment->positionForPoint(point, region);
return RenderBlock::positionForPoint(point, region);
}
void RenderBlockFlow::addFocusRingRectsForInlineChildren(Vector<IntRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject*)
{
ASSERT(childrenInline());
ensureLineBoxes();
for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
LayoutUnit top = std::max<LayoutUnit>(curr->lineTop(), curr->top());
LayoutUnit bottom = std::min<LayoutUnit>(curr->lineBottom(), curr->top() + curr->height());
LayoutRect rect(additionalOffset.x() + curr->x(), additionalOffset.y() + top, curr->width(), bottom - top);
if (!rect.isEmpty())
rects.append(pixelSnappedIntRect(rect));
}
}
void RenderBlockFlow::paintInlineChildren(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
ASSERT(childrenInline());
if (auto simpleLineLayout = this->simpleLineLayout()) {
SimpleLineLayout::paintFlow(*this, *simpleLineLayout, paintInfo, paintOffset);
return;
}
m_lineBoxes.paint(this, paintInfo, paintOffset);
}
bool RenderBlockFlow::relayoutForPagination(LayoutStateMaintainer& statePusher)
{
if (!multiColumnFlowThread() || !multiColumnFlowThread()->shouldRelayoutForPagination())
return false;
multiColumnFlowThread()->setNeedsHeightsRecalculation(false);
multiColumnFlowThread()->setInBalancingPass(true);
bool needsRelayout;
bool neededRelayout = false;
bool firstPass = true;
do {
needsRelayout = false;
for (RenderMultiColumnSet* multicolSet = multiColumnFlowThread()->firstMultiColumnSet(); multicolSet; multicolSet = multicolSet->nextSiblingMultiColumnSet()) {
if (multicolSet->recalculateColumnHeight(firstPass))
needsRelayout = true;
if (needsRelayout) {
multicolSet->setChildNeedsLayout(MarkOnlyThis);
}
}
if (needsRelayout) {
neededRelayout = true;
multiColumnFlowThread()->setChildNeedsLayout(MarkOnlyThis);
setChildNeedsLayout(MarkOnlyThis);
if (firstPass)
statePusher.pop();
layoutBlock(false);
}
firstPass = false;
} while (needsRelayout);
multiColumnFlowThread()->setInBalancingPass(false);
return neededRelayout;
}
bool RenderBlockFlow::hasLines() const
{
ASSERT(childrenInline());
if (auto simpleLineLayout = this->simpleLineLayout())
return simpleLineLayout->lineCount();
return lineBoxes().firstLineBox();
}
void RenderBlockFlow::invalidateLineLayoutPath()
{
switch (m_lineLayoutPath) {
case UndeterminedPath:
case ForceLineBoxesPath:
ASSERT(!m_simpleLineLayout);
return;
case LineBoxesPath:
ASSERT(!m_simpleLineLayout);
m_lineLayoutPath = UndeterminedPath;
return;
case SimpleLinesPath:
m_simpleLineLayout = nullptr;
setNeedsLayout();
m_lineLayoutPath = UndeterminedPath;
return;
}
ASSERT_NOT_REACHED();
}
void RenderBlockFlow::layoutSimpleLines(LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
{
ASSERT(!m_lineBoxes.firstLineBox());
m_simpleLineLayout = SimpleLineLayout::create(*this);
LayoutUnit lineLayoutHeight = SimpleLineLayout::computeFlowHeight(*this, *m_simpleLineLayout);
LayoutUnit lineLayoutTop = borderAndPaddingBefore();
repaintLogicalTop = lineLayoutTop;
repaintLogicalBottom = lineLayoutTop + lineLayoutHeight;
setLogicalHeight(lineLayoutTop + lineLayoutHeight + borderAndPaddingAfter());
}
void RenderBlockFlow::deleteLineBoxesBeforeSimpleLineLayout()
{
ASSERT(m_lineLayoutPath == SimpleLinesPath);
lineBoxes().deleteLineBoxes();
toRenderText(firstChild())->deleteLineBoxesBeforeSimpleLineLayout();
}
void RenderBlockFlow::ensureLineBoxes()
{
m_lineLayoutPath = ForceLineBoxesPath;
if (!m_simpleLineLayout)
return;
m_simpleLineLayout = nullptr;
#if !ASSERT_DISABLED
LayoutUnit oldHeight = logicalHeight();
#endif
bool didNeedLayout = needsLayout();
bool relayoutChildren = false;
LayoutUnit repaintLogicalTop;
LayoutUnit repaintLogicalBottom;
layoutLineBoxes(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
updateLogicalHeight();
ASSERT(didNeedLayout || logicalHeight() == oldHeight);
if (!didNeedLayout)
clearNeedsLayout();
}
#ifndef NDEBUG
void RenderBlockFlow::showLineTreeAndMark(const InlineBox* markedBox1, const char* markedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const RenderObject* obj) const
{
RenderBlock::showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLabel2, obj);
for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox())
root->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLabel2, obj, 1);
}
#endif
RenderBlockFlow::RenderBlockFlowRareData& RenderBlockFlow::ensureRareBlockFlowData()
{
if (hasRareBlockFlowData())
return *m_rareBlockFlowData;
materializeRareBlockFlowData();
return *m_rareBlockFlowData;
}
void RenderBlockFlow::materializeRareBlockFlowData()
{
ASSERT(!hasRareBlockFlowData());
m_rareBlockFlowData = std::make_unique<RenderBlockFlow::RenderBlockFlowRareData>(*this);
}
#if ENABLE(IOS_TEXT_AUTOSIZING)
inline static bool isVisibleRenderText(RenderObject* renderer)
{
if (!renderer->isText())
return false;
RenderText* renderText = toRenderText(renderer);
return !renderText->linesBoundingBox().isEmpty() && !renderText->text()->containsOnlyWhitespace();
}
inline static bool resizeTextPermitted(RenderObject* render)
{
auto renderer = render->parent();
while (renderer) {
if (renderer->element() && renderer->element()->isHTMLElement() && !renderer->element()->isInShadowTree()) {
const HTMLElement& element = toHTMLElement(*renderer->element());
return !isHTMLInputElement(element) && !isHTMLTextAreaElement(element);
}
renderer = renderer->parent();
}
return true;
}
int RenderBlockFlow::lineCountForTextAutosizing()
{
if (style().visibility() != VISIBLE)
return 0;
if (childrenInline())
return lineCount();
int count = 0;
for (auto& listItem : childrenOfType<RenderListItem>(*this))
count += listItem.lineCount();
return count;
}
static bool isNonBlocksOrNonFixedHeightListItems(const RenderObject* render)
{
if (!render->isRenderBlock())
return true;
if (render->isListItem())
return render->style().height().type() != Fixed;
return false;
}
static inline float oneLineTextMultiplier(float specifiedSize)
{
return std::max((1.0f / log10f(specifiedSize) * 1.7f), 1.0f);
}
static inline float textMultiplier(float specifiedSize)
{
return std::max((1.0f / log10f(specifiedSize) * 1.95f), 1.0f);
}
void RenderBlockFlow::adjustComputedFontSizes(float size, float visibleWidth)
{
if (visibleWidth >= width())
return;
unsigned lineCount;
if (m_lineCountForTextAutosizing == NOT_SET) {
int count = lineCountForTextAutosizing();
if (!count)
lineCount = NO_LINE;
else if (count == 1)
lineCount = ONE_LINE;
else
lineCount = MULTI_LINE;
} else
lineCount = m_lineCountForTextAutosizing;
ASSERT(lineCount != NOT_SET);
if (lineCount == NO_LINE)
return;
float actualWidth = m_widthForTextAutosizing != -1 ? static_cast<float>(m_widthForTextAutosizing) : static_cast<float>(width());
float scale = visibleWidth / actualWidth;
float minFontSize = roundf(size / scale);
for (RenderObject* descendent = traverseNext(this, isNonBlocksOrNonFixedHeightListItems); descendent; descendent = descendent->traverseNext(this, isNonBlocksOrNonFixedHeightListItems)) {
if (isVisibleRenderText(descendent) && resizeTextPermitted(descendent)) {
RenderText* text = toRenderText(descendent);
RenderStyle& oldStyle = text->style();
FontDescription fontDescription = oldStyle.fontDescription();
float specifiedSize = fontDescription.specifiedSize();
float scaledSize = roundf(specifiedSize * scale);
if (scaledSize > 0 && scaledSize < minFontSize) {
if (m_lineCountForTextAutosizing == NOT_SET)
m_lineCountForTextAutosizing = lineCount;
if (m_widthForTextAutosizing == -1)
m_widthForTextAutosizing = actualWidth;
float candidateNewSize = 0;
float lineTextMultiplier = lineCount == ONE_LINE ? oneLineTextMultiplier(specifiedSize) : textMultiplier(specifiedSize);
candidateNewSize = roundf(std::min(minFontSize, specifiedSize * lineTextMultiplier));
if (candidateNewSize > specifiedSize && candidateNewSize != fontDescription.computedSize() && text->textNode() && oldStyle.textSizeAdjust().isAuto())
document().addAutoSizingNode(text->textNode(), candidateNewSize);
}
}
}
}
#endif // ENABLE(IOS_TEXT_AUTOSIZING)
RenderObject* RenderBlockFlow::layoutSpecialExcludedChild(bool relayoutChildren)
{
RenderMultiColumnFlowThread* flowThread = multiColumnFlowThread();
if (!flowThread)
return nullptr;
setLogicalTopForChild(*flowThread, borderAndPaddingBefore());
if (relayoutChildren)
flowThread->setChildNeedsLayout(MarkOnlyThis);
if (flowThread->needsLayout()) {
for (RenderMultiColumnSet* columnSet = flowThread->firstMultiColumnSet(); columnSet; columnSet = columnSet->nextSiblingMultiColumnSet())
columnSet->prepareForLayout(!flowThread->inBalancingPass());
flowThread->invalidateRegions();
flowThread->setNeedsHeightsRecalculation(true);
flowThread->layout();
} else {
flowThread->setNeedsHeightsRecalculation(false);
}
determineLogicalLeftPositionForChild(*flowThread);
return flowThread;
}
void RenderBlockFlow::addChild(RenderObject* newChild, RenderObject* beforeChild)
{
if (multiColumnFlowThread())
return multiColumnFlowThread()->addChild(newChild, beforeChild);
if (beforeChild) {
if (RenderFlowThread* containingFlowThread = flowThreadContainingBlock())
beforeChild = containingFlowThread->resolveMovedChild(beforeChild);
}
RenderBlock::addChild(newChild, beforeChild);
}
RenderObject* RenderBlockFlow::removeChild(RenderObject& oldChild)
{
if (!documentBeingDestroyed()) {
RenderFlowThread* flowThread = multiColumnFlowThread();
if (flowThread && flowThread != &oldChild)
flowThread->flowThreadRelativeWillBeRemoved(&oldChild);
}
return RenderBlock::removeChild(oldChild);
}
void RenderBlockFlow::checkForPaginationLogicalHeightChange(bool& relayoutChildren, LayoutUnit& pageLogicalHeight, bool& pageLogicalHeightChanged)
{
if (!isRenderFlowThread() && !multiColumnFlowThread())
return;
if (RenderMultiColumnFlowThread* flowThread = multiColumnFlowThread()) {
LogicalExtentComputedValues computedValues;
computeLogicalHeight(LayoutUnit(), logicalTop(), computedValues);
LayoutUnit columnHeight = computedValues.m_extent - borderAndPaddingLogicalHeight() - scrollbarLogicalHeight();
LayoutUnit oldHeightAvailable = flowThread->columnHeightAvailable();
flowThread->setColumnHeightAvailable(std::max<LayoutUnit>(columnHeight, 0));
if (oldHeightAvailable != flowThread->columnHeightAvailable())
relayoutChildren = true;
} else if (isRenderFlowThread()) {
RenderFlowThread* flowThread = toRenderFlowThread(this);
pageLogicalHeight = flowThread->isPageLogicalHeightKnown() ? LayoutUnit(1) : LayoutUnit(0);
pageLogicalHeightChanged = flowThread->pageLogicalSizeChanged();
}
}
bool RenderBlockFlow::requiresColumns(int desiredColumnCount) const
{
bool isPaginated = (style().overflowY() == OPAGEDX || style().overflowY() == OPAGEDY) && !(isRoot() || isBody());
return firstChild() && (desiredColumnCount != 1 || !style().hasAutoColumnWidth() || !style().hasInlineColumnAxis() || isPaginated);
}
void RenderBlockFlow::setComputedColumnCountAndWidth(int count, LayoutUnit width)
{
bool destroyColumns = !requiresColumns(count);
if (destroyColumns) {
if (multiColumnFlowThread())
destroyMultiColumnFlowThread();
} else {
if (!multiColumnFlowThread())
createMultiColumnFlowThread();
multiColumnFlowThread()->setColumnCountAndWidth(count, width);
multiColumnFlowThread()->setProgressionIsInline(style().hasInlineColumnAxis());
multiColumnFlowThread()->setProgressionIsReversed(style().columnProgression() == ReverseColumnProgression);
}
}
void RenderBlockFlow::updateColumnProgressionFromStyle(RenderStyle* style)
{
if (!multiColumnFlowThread())
return;
bool needsLayout = false;
bool oldProgressionIsInline = multiColumnFlowThread()->progressionIsInline();
bool newProgressionIsInline = style->hasInlineColumnAxis();
if (oldProgressionIsInline != newProgressionIsInline) {
multiColumnFlowThread()->setProgressionIsInline(newProgressionIsInline);
needsLayout = true;
}
bool oldProgressionIsReversed = multiColumnFlowThread()->progressionIsReversed();
bool newProgressionIsReversed = style->columnProgression() == ReverseColumnProgression;
if (oldProgressionIsReversed != newProgressionIsReversed) {
multiColumnFlowThread()->setProgressionIsReversed(newProgressionIsReversed);
needsLayout = true;
}
if (needsLayout)
setNeedsLayoutAndPrefWidthsRecalc();
}
LayoutUnit RenderBlockFlow::computedColumnWidth() const
{
if (multiColumnFlowThread())
return multiColumnFlowThread()->computedColumnWidth();
return contentLogicalWidth();
}
unsigned RenderBlockFlow::computedColumnCount() const
{
if (multiColumnFlowThread())
return multiColumnFlowThread()->computedColumnCount();
return 1;
}
bool RenderBlockFlow::isTopLayoutOverflowAllowed() const
{
bool hasTopOverflow = RenderBlock::isTopLayoutOverflowAllowed();
if (!multiColumnFlowThread() || style().columnProgression() == NormalColumnProgression)
return hasTopOverflow;
if (!(isHorizontalWritingMode() ^ !style().hasInlineColumnAxis()))
hasTopOverflow = !hasTopOverflow;
return hasTopOverflow;
}
bool RenderBlockFlow::isLeftLayoutOverflowAllowed() const
{
bool hasLeftOverflow = RenderBlock::isLeftLayoutOverflowAllowed();
if (!multiColumnFlowThread() || style().columnProgression() == NormalColumnProgression)
return hasLeftOverflow;
if (isHorizontalWritingMode() ^ !style().hasInlineColumnAxis())
hasLeftOverflow = !hasLeftOverflow;
return hasLeftOverflow;
}
}