blob: d70961656cf7f2a0f2ebc9ac78078e1adda31809 [file] [log] [blame]
* Copyright (C) 1999 Lars Knoll (
* (C) 1999 Antti Koivisto (
* (C) 2007 David Smith (
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* Library General Public License for more details.
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
#include "config.h"
#include "RenderBlock.h"
#include "AXObjectCache.h"
#include "ColumnInfo.h"
#include "Document.h"
#include "Editor.h"
#include "Element.h"
#include "FloatQuad.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HTMLFormElement.h"
#include "HTMLNames.h"
#include "HitTestLocation.h"
#include "HitTestResult.h"
#include "InlineIterator.h"
#include "InlineTextBox.h"
#include "LayoutRepainter.h"
#include "LogicalSelectionOffsetCaches.h"
#include "OverflowEvent.h"
#include "PODFreeListArena.h"
#include "Page.h"
#include "PaintInfo.h"
#include "RenderBoxRegionInfo.h"
#include "RenderCombineText.h"
#include "RenderDeprecatedFlexibleBox.h"
#include "RenderFlexibleBox.h"
#include "RenderImage.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderMarquee.h"
#include "RenderNamedFlowThread.h"
#include "RenderRegion.h"
#include "RenderReplica.h"
#include "RenderTableCell.h"
#include "RenderTextFragment.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "SVGTextRunRenderingContext.h"
#include "Settings.h"
#include "ShadowRoot.h"
#include "TransformState.h"
#include <wtf/StdLibExtras.h>
#include "ExclusionShapeInsideInfo.h"
#include "ExclusionShapeOutsideInfo.h"
#include <wtf/StackStats.h>
#include <wtf/TemporaryChange.h>
using namespace std;
using namespace WTF;
using namespace Unicode;
namespace WebCore {
using namespace HTMLNames;
struct SameSizeAsRenderBlock : public RenderBox {
void* pointers[2];
RenderObjectChildList children;
RenderLineBoxList lineBoxes;
uint32_t bitfields;
COMPILE_ASSERT(sizeof(RenderBlock) == sizeof(SameSizeAsRenderBlock), RenderBlock_should_stay_small);
struct SameSizeAsFloatingObject {
void* pointers[2];
LayoutRect rect;
int paginationStrut;
uint32_t bitfields : 8;
COMPILE_ASSERT(sizeof(RenderBlock::MarginValues) == sizeof(LayoutUnit[4]), MarginValues_should_stay_small);
struct SameSizeAsMarginInfo {
uint32_t bitfields : 16;
LayoutUnit margins[2];
typedef WTF::HashMap<const RenderBox*, OwnPtr<ColumnInfo> > ColumnInfoMap;
static ColumnInfoMap* gColumnInfoMap = 0;
static TrackedDescendantsMap* gPositionedDescendantsMap = 0;
static TrackedDescendantsMap* gPercentHeightDescendantsMap = 0;
static TrackedContainerMap* gPositionedContainerMap = 0;
static TrackedContainerMap* gPercentHeightContainerMap = 0;
typedef WTF::HashMap<RenderBlock*, OwnPtr<ListHashSet<RenderInline*> > > ContinuationOutlineTableMap;
typedef WTF::HashSet<RenderBlock*> DelayedUpdateScrollInfoSet;
static int gDelayUpdateScrollInfo = 0;
static DelayedUpdateScrollInfoSet* gDelayedUpdateScrollInfoSet = 0;
static bool gColumnFlowSplitEnabled = true;
bool RenderBlock::s_canPropagateFloatIntoSibling = false;
// This class helps dispatching the 'overflow' event on layout change. overflow can be set on RenderBoxes, yet the existing code
// only works on RenderBlocks. If this change, this class should be shared with other RenderBoxes.
class OverflowEventDispatcher {
OverflowEventDispatcher(const RenderBlock* block)
: m_block(block)
, m_hadHorizontalLayoutOverflow(false)
, m_hadVerticalLayoutOverflow(false)
m_shouldDispatchEvent = !m_block->isAnonymous() && m_block->hasOverflowClip() && m_block->document()->hasListenerType(Document::OVERFLOWCHANGED_LISTENER);
if (m_shouldDispatchEvent) {
m_hadHorizontalLayoutOverflow = m_block->hasHorizontalLayoutOverflow();
m_hadVerticalLayoutOverflow = m_block->hasVerticalLayoutOverflow();
if (!m_shouldDispatchEvent)
bool hasHorizontalLayoutOverflow = m_block->hasHorizontalLayoutOverflow();
bool hasVerticalLayoutOverflow = m_block->hasVerticalLayoutOverflow();
bool horizontalLayoutOverflowChanged = hasHorizontalLayoutOverflow != m_hadHorizontalLayoutOverflow;
bool verticalLayoutOverflowChanged = hasVerticalLayoutOverflow != m_hadVerticalLayoutOverflow;
if (horizontalLayoutOverflowChanged || verticalLayoutOverflowChanged) {
if (FrameView* frameView = m_block->document()->view())
frameView->scheduleEvent(OverflowEvent::create(horizontalLayoutOverflowChanged, hasHorizontalLayoutOverflow, verticalLayoutOverflowChanged, hasVerticalLayoutOverflow), m_block->node());
const RenderBlock* m_block;
bool m_shouldDispatchEvent;
bool m_hadHorizontalLayoutOverflow;
bool m_hadVerticalLayoutOverflow;
// Our MarginInfo state used when laying out block children.
RenderBlock::MarginInfo::MarginInfo(RenderBlock* block, LayoutUnit beforeBorderPadding, LayoutUnit afterBorderPadding)
: m_atBeforeSideOfBlock(true)
, m_atAfterSideOfBlock(false)
, m_hasMarginBeforeQuirk(false)
, m_hasMarginAfterQuirk(false)
, m_determinedMarginBeforeQuirk(false)
, m_discardMargin(false)
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;
// If any height other than auto is specified in CSS, then we don't collapse our bottom
// margins with our children's margins. To do otherwise would be to risk odd visual
// effects when the children overflow out of the parent block and yet still collapse
// with it. We also don't collapse if we have any bottom border/padding.
m_canCollapseMarginAfterWithChildren = m_canCollapseWithChildren && (afterBorderPadding == 0) &&
(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();
// -------------------------------------------------------------------------------------------------------
RenderBlock::RenderBlock(ContainerNode* node)
: RenderBox(node)
, m_lineHeight(-1)
, m_hasMarginBeforeQuirk(false)
, m_hasMarginAfterQuirk(false)
, m_beingDestroyed(false)
, m_hasMarkupTruncation(false)
, m_hasBorderOrPaddingLogicalWidthChanged(false)
COMPILE_ASSERT(sizeof(RenderBlock::FloatingObject) == sizeof(SameSizeAsFloatingObject), FloatingObject_should_stay_small);
COMPILE_ASSERT(sizeof(RenderBlock::MarginInfo) == sizeof(SameSizeAsMarginInfo), MarginInfo_should_stay_small);
static void removeBlockFromDescendantAndContainerMaps(RenderBlock* block, TrackedDescendantsMap*& descendantMap, TrackedContainerMap*& containerMap)
if (OwnPtr<TrackedRendererListHashSet> descendantSet = descendantMap->take(block)) {
TrackedRendererListHashSet::iterator end = descendantSet->end();
for (TrackedRendererListHashSet::iterator descendant = descendantSet->begin(); descendant != end; ++descendant) {
TrackedContainerMap::iterator it = containerMap->find(*descendant);
ASSERT(it != containerMap->end());
if (it == containerMap->end())
HashSet<RenderBlock*>* containerSet = it->value.get();
if (containerSet->isEmpty())
if (m_floatingObjects)
if (hasColumns())
if (gPercentHeightDescendantsMap)
removeBlockFromDescendantAndContainerMaps(this, gPercentHeightDescendantsMap, gPercentHeightContainerMap);
if (gPositionedDescendantsMap)
removeBlockFromDescendantAndContainerMaps(this, gPositionedDescendantsMap, gPositionedContainerMap);
RenderBlock* RenderBlock::createAnonymous(Document* document)
RenderBlock* renderer = new (document->renderArena()) RenderBlock(0);
return renderer;
void RenderBlock::willBeDestroyed()
// Mark as being destroyed to avoid trouble with merges in removeChild().
m_beingDestroyed = true;
// Make sure to destroy anonymous children first while they are still connected to the rest of the tree, so that they will
// properly dirty line boxes that they are removed from. Effects that do :before/:after only on hover could crash otherwise.
// Destroy our continuation before anything other than anonymous children.
// The reason we don't destroy it before anonymous children is that they may
// have continuations of their own that are anonymous children of our continuation.
RenderBoxModelObject* continuation = this->continuation();
if (continuation) {
if (!documentBeingDestroyed()) {
if (firstLineBox()) {
// We can't wait for RenderBox::destroy to clear the selection,
// because by then we will have nuked the line boxes.
// FIXME: The FrameSelection should be responsible for this when it
// is notified of DOM mutations.
if (isSelectionBorder())
// If we are an anonymous block, then our line boxes might have children
// that will outlast this block. In the non-anonymous block case those
// children will be destroyed by the time we return from this function.
if (isAnonymousBlock()) {
for (InlineFlowBox* box = firstLineBox(); box; box = box->nextLineBox()) {
while (InlineBox* childBox = box->firstChild())
} else if (parent())
if (lineGridBox())
if (UNLIKELY(gDelayedUpdateScrollInfoSet != 0))
void RenderBlock::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
RenderStyle* oldStyle = style();
s_canPropagateFloatIntoSibling = oldStyle ? !isFloatingOrOutOfFlowPositioned() && !avoidsFloats() : false;
if (oldStyle && parent() && diff == StyleDifferenceLayout && oldStyle->position() != newStyle->position()) {
if (newStyle->position() == StaticPosition)
// Clear our positioned objects list. Our absolutely positioned descendants will be
// inserted into our containing block's positioned objects list during layout.
removePositionedObjects(0, NewContainingBlock);
else if (oldStyle->position() == StaticPosition) {
// Remove our absolutely positioned descendants from their current containing block.
// They will be inserted into our positioned objects list during layout.
RenderObject* cb = parent();
while (cb && (cb->style()->position() == StaticPosition || (cb->isInline() && !cb->isReplaced())) && !cb->isRenderView()) {
if (cb->style()->position() == RelativePosition && cb->isInline() && !cb->isReplaced()) {
cb = cb->containingBlock();
cb = cb->parent();
if (cb->isRenderBlock())
toRenderBlock(cb)->removePositionedObjects(this, NewContainingBlock);
if (containsFloats() && !isFloating() && !isOutOfFlowPositioned() && newStyle->hasOutOfFlowPosition())
RenderBox::styleWillChange(diff, newStyle);
static bool borderOrPaddingLogicalWidthChanged(const RenderStyle* oldStyle, const RenderStyle* newStyle)
if (newStyle->isHorizontalWritingMode())
return oldStyle->borderLeftWidth() != newStyle->borderLeftWidth()
|| oldStyle->borderRightWidth() != newStyle->borderRightWidth()
|| oldStyle->paddingLeft() != newStyle->paddingLeft()
|| oldStyle->paddingRight() != newStyle->paddingRight();
return oldStyle->borderTopWidth() != newStyle->borderTopWidth()
|| oldStyle->borderBottomWidth() != newStyle->borderBottomWidth()
|| oldStyle->paddingTop() != newStyle->paddingTop()
|| oldStyle->paddingBottom() != newStyle->paddingBottom();
void RenderBlock::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
RenderBox::styleDidChange(diff, oldStyle);
RenderStyle* newStyle = style();
// FIXME: Bug 89993: Style changes should affect the ExclusionShapeInsideInfos for other render blocks that
// share the same ExclusionShapeInsideInfo
updateExclusionShapeInsideInfoAfterStyleChange(newStyle->resolvedShapeInside(), oldStyle ? oldStyle->resolvedShapeInside() : 0);
if (!isAnonymousBlock()) {
// Ensure that all of our continuation blocks pick up the new style.
for (RenderBlock* currCont = blockElementContinuation(); currCont; currCont = currCont->blockElementContinuation()) {
RenderBoxModelObject* nextCont = currCont->continuation();
m_lineHeight = -1;
// After our style changed, if we lose our ability to propagate floats into next sibling
// blocks, then we need to find the top most parent containing that overhanging float and
// then mark its descendants with floats for layout and clear all floats from its next
// sibling blocks that exist in our floating objects list. See bug 56299 and 62875.
bool canPropagateFloatIntoSibling = !isFloatingOrOutOfFlowPositioned() && !avoidsFloats();
if (diff == StyleDifferenceLayout && s_canPropagateFloatIntoSibling && !canPropagateFloatIntoSibling && hasOverhangingFloats()) {
RenderBlock* parentBlock = this;
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
FloatingObjectSetIterator end = floatingObjectSet.end();
for (RenderObject* curr = parent(); curr && !curr->isRenderView(); curr = curr->parent()) {
if (curr->isRenderBlock()) {
RenderBlock* currBlock = toRenderBlock(curr);
if (currBlock->hasOverhangingFloats()) {
for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
RenderBox* renderer = (*it)->renderer();
if (currBlock->hasOverhangingFloat(renderer)) {
parentBlock = currBlock;
// It's possible for our border/padding to change, but for the overall logical width of the block to
// end up being the same. We keep track of this change so in layoutBlock, we can know to set relayoutChildren=true.
m_hasBorderOrPaddingLogicalWidthChanged = oldStyle && diff == StyleDifferenceLayout && needsLayout() && borderOrPaddingLogicalWidthChanged(oldStyle, newStyle);
RenderBlock* RenderBlock::continuationBefore(RenderObject* beforeChild)
if (beforeChild && beforeChild->parent() == this)
return this;
RenderBlock* curr = toRenderBlock(continuation());
RenderBlock* nextToLast = this;
RenderBlock* last = this;
while (curr) {
if (beforeChild && beforeChild->parent() == curr) {
if (curr->firstChild() == beforeChild)
return last;
return curr;
nextToLast = last;
last = curr;
curr = toRenderBlock(curr->continuation());
if (!beforeChild && !last->firstChild())
return nextToLast;
return last;
void RenderBlock::addChildToContinuation(RenderObject* newChild, RenderObject* beforeChild)
RenderBlock* flow = continuationBefore(beforeChild);
ASSERT(!beforeChild || beforeChild->parent()->isAnonymousColumnSpanBlock() || beforeChild->parent()->isRenderBlock());
RenderBoxModelObject* beforeChildParent = 0;
if (beforeChild)
beforeChildParent = toRenderBoxModelObject(beforeChild->parent());
else {
RenderBoxModelObject* cont = flow->continuation();
if (cont)
beforeChildParent = cont;
beforeChildParent = flow;
if (newChild->isFloatingOrOutOfFlowPositioned()) {
beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
// A continuation always consists of two potential candidates: a block or an anonymous
// column span box holding column span children.
bool childIsNormal = newChild->isInline() || !newChild->style()->columnSpan();
bool bcpIsNormal = beforeChildParent->isInline() || !beforeChildParent->style()->columnSpan();
bool flowIsNormal = flow->isInline() || !flow->style()->columnSpan();
if (flow == beforeChildParent) {
flow->addChildIgnoringContinuation(newChild, beforeChild);
// The goal here is to match up if we can, so that we can coalesce and create the
// minimal # of continuations needed for the inline.
if (childIsNormal == bcpIsNormal) {
beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
if (flowIsNormal == childIsNormal) {
flow->addChildIgnoringContinuation(newChild, 0); // Just treat like an append.
beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
void RenderBlock::addChildToAnonymousColumnBlocks(RenderObject* newChild, RenderObject* beforeChild)
ASSERT(!continuation()); // We don't yet support column spans that aren't immediate children of the multi-column block.
// The goal is to locate a suitable box in which to place our child.
RenderBlock* beforeChildParent = 0;
if (beforeChild) {
RenderObject* curr = beforeChild;
while (curr && curr->parent() != this)
curr = curr->parent();
beforeChildParent = toRenderBlock(curr);
ASSERT(beforeChildParent->isAnonymousColumnsBlock() || beforeChildParent->isAnonymousColumnSpanBlock());
} else
beforeChildParent = toRenderBlock(lastChild());
// If the new child is floating or positioned it can just go in that block.
if (newChild->isFloatingOrOutOfFlowPositioned()) {
beforeChildParent->addChildIgnoringAnonymousColumnBlocks(newChild, beforeChild);
// See if the child can be placed in the box.
bool newChildHasColumnSpan = newChild->style()->columnSpan() && !newChild->isInline();
bool beforeChildParentHoldsColumnSpans = beforeChildParent->isAnonymousColumnSpanBlock();
if (newChildHasColumnSpan == beforeChildParentHoldsColumnSpans) {
beforeChildParent->addChildIgnoringAnonymousColumnBlocks(newChild, beforeChild);
if (!beforeChild) {
// Create a new block of the correct type.
RenderBlock* newBox = newChildHasColumnSpan ? createAnonymousColumnSpanBlock() : createAnonymousColumnsBlock();
children()->appendChildNode(this, newBox);
newBox->addChildIgnoringAnonymousColumnBlocks(newChild, 0);
RenderObject* immediateChild = beforeChild;
bool isPreviousBlockViable = true;
while (immediateChild->parent() != this) {
if (isPreviousBlockViable)
isPreviousBlockViable = !immediateChild->previousSibling();
immediateChild = immediateChild->parent();
if (isPreviousBlockViable && immediateChild->previousSibling()) {
toRenderBlock(immediateChild->previousSibling())->addChildIgnoringAnonymousColumnBlocks(newChild, 0); // Treat like an append.
// Split our anonymous blocks.
RenderObject* newBeforeChild = splitAnonymousBoxesAroundChild(beforeChild);
// Create a new anonymous box of the appropriate type.
RenderBlock* newBox = newChildHasColumnSpan ? createAnonymousColumnSpanBlock() : createAnonymousColumnsBlock();
children()->insertChildNode(this, newBox, newBeforeChild);
newBox->addChildIgnoringAnonymousColumnBlocks(newChild, 0);
RenderBlock* RenderBlock::containingColumnsBlock(bool allowAnonymousColumnBlock)
RenderBlock* firstChildIgnoringAnonymousWrappers = 0;
for (RenderObject* curr = this; curr; curr = curr->parent()) {
if (!curr->isRenderBlock() || curr->isFloatingOrOutOfFlowPositioned() || curr->isTableCell() || curr->isRoot() || curr->isRenderView() || curr->hasOverflowClip()
|| curr->isInlineBlockOrInlineTable())
return 0;
// FIXME: Tables, RenderButtons, and RenderListItems all do special management
// of their children that breaks when the flow is split through them. Disabling
// multi-column for them to avoid this problem.
if (curr->isTable() || curr->isRenderButton() || curr->isListItem())
return 0;
RenderBlock* currBlock = toRenderBlock(curr);
if (!currBlock->createsAnonymousWrapper())
firstChildIgnoringAnonymousWrappers = currBlock;
if (currBlock->style()->specifiesColumns() && (allowAnonymousColumnBlock || !currBlock->isAnonymousColumnsBlock()))
return firstChildIgnoringAnonymousWrappers;
if (currBlock->isAnonymousColumnSpanBlock())
return 0;
return 0;
RenderBlock* RenderBlock::clone() const
RenderBlock* cloneBlock;
if (isAnonymousBlock()) {
cloneBlock = createAnonymousBlock();
else {
RenderObject* cloneRenderer = toElement(node())->createRenderer(renderArena(), style());
cloneBlock = toRenderBlock(cloneRenderer);
// This takes care of setting the right value of childrenInline in case
// generated content is added to cloneBlock and 'this' does not have
// generated content added yet.
cloneBlock->setChildrenInline(cloneBlock->firstChild() ? cloneBlock->firstChild()->isInline() : childrenInline());
return cloneBlock;
void RenderBlock::splitBlocks(RenderBlock* fromBlock, RenderBlock* toBlock,
RenderBlock* middleBlock,
RenderObject* beforeChild, RenderBoxModelObject* oldCont)
// Create a clone of this inline.
RenderBlock* cloneBlock = clone();
if (!isAnonymousBlock())
if (!beforeChild && isAfterContent(lastChild()))
beforeChild = lastChild();
// If we are moving inline children from |this| to cloneBlock, then we need
// to clear our line box tree.
if (beforeChild && childrenInline())
// Now take all of the children from beforeChild to the end and remove
// them from |this| and place them in the clone.
moveChildrenTo(cloneBlock, beforeChild, 0, true);
// Hook |clone| up as the continuation of the middle block.
if (!cloneBlock->isAnonymousBlock())
// We have been reparented and are now under the fromBlock. We need
// to walk up our block parent chain until we hit the containing anonymous columns block.
// Once we hit the anonymous columns block we're done.
RenderBoxModelObject* curr = toRenderBoxModelObject(parent());
RenderBoxModelObject* currChild = this;
RenderObject* currChildNextSibling = currChild->nextSibling();
while (curr && curr != fromBlock) {
RenderBlock* blockCurr = toRenderBlock(curr);
// Create a new clone.
RenderBlock* cloneChild = cloneBlock;
cloneBlock = blockCurr->clone();
// Insert our child clone as the first child.
cloneBlock->addChildIgnoringContinuation(cloneChild, 0);
// Hook the clone up as a continuation of |curr|. Note we do encounter
// anonymous blocks possibly as we walk up the block chain. When we split an
// anonymous block, there's no need to do any continuation hookup, since we haven't
// actually split a real element.
if (!blockCurr->isAnonymousBlock()) {
oldCont = blockCurr->continuation();
// Now we need to take all of the children starting from the first child
// *after* currChild and append them all to the clone.
blockCurr->moveChildrenTo(cloneBlock, currChildNextSibling, 0, true);
// Keep walking up the chain.
currChild = curr;
currChildNextSibling = currChild->nextSibling();
curr = toRenderBoxModelObject(curr->parent());
// Now we are at the columns block level. We need to put the clone into the toBlock.
toBlock->children()->appendChildNode(toBlock, cloneBlock);
// Now take all the children after currChild and remove them from the fromBlock
// and put them in the toBlock.
fromBlock->moveChildrenTo(toBlock, currChildNextSibling, 0, true);
void RenderBlock::splitFlow(RenderObject* beforeChild, RenderBlock* newBlockBox,
RenderObject* newChild, RenderBoxModelObject* oldCont)
RenderBlock* pre = 0;
RenderBlock* block = containingColumnsBlock();
// Delete our line boxes before we do the inline split into continuations.
bool madeNewBeforeBlock = false;
if (block->isAnonymousColumnsBlock()) {
// We can reuse this block and make it the preBlock of the next continuation.
pre = block;
block = toRenderBlock(block->parent());
} else {
// No anonymous block available for use. Make one.
pre = block->createAnonymousColumnsBlock();
madeNewBeforeBlock = true;
RenderBlock* post = block->createAnonymousColumnsBlock();
RenderObject* boxFirst = madeNewBeforeBlock ? block->firstChild() : pre->nextSibling();
if (madeNewBeforeBlock)
block->children()->insertChildNode(block, pre, boxFirst);
block->children()->insertChildNode(block, newBlockBox, boxFirst);
block->children()->insertChildNode(block, post, boxFirst);
if (madeNewBeforeBlock)
block->moveChildrenTo(pre, boxFirst, 0, true);
splitBlocks(pre, post, newBlockBox, beforeChild, oldCont);
// We already know the newBlockBox isn't going to contain inline kids, so avoid wasting
// time in makeChildrenNonInline by just setting this explicitly up front.
// We delayed adding the newChild until now so that the |newBlockBox| would be fully
// connected, thus allowing newChild access to a renderArena should it need
// to wrap itself in additional boxes (e.g., table construction).
// Always just do a full layout in order to ensure that line boxes (especially wrappers for images)
// get deleted properly. Because objects moves from the pre block into the post block, we want to
// make new line boxes instead of leaving the old line boxes around.
void RenderBlock::makeChildrenAnonymousColumnBlocks(RenderObject* beforeChild, RenderBlock* newBlockBox, RenderObject* newChild)
RenderBlock* pre = 0;
RenderBlock* post = 0;
RenderBlock* block = this; // Eventually block will not just be |this|, but will also be a block nested inside |this|. Assign to a variable
// so that we don't have to patch all of the rest of the code later on.
// Delete the block's line boxes before we do the split.
if (beforeChild && beforeChild->parent() != this)
beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
if (beforeChild != firstChild()) {
pre = block->createAnonymousColumnsBlock();
if (beforeChild) {
post = block->createAnonymousColumnsBlock();
RenderObject* boxFirst = block->firstChild();
if (pre)
block->children()->insertChildNode(block, pre, boxFirst);
block->children()->insertChildNode(block, newBlockBox, boxFirst);
if (post)
block->children()->insertChildNode(block, post, boxFirst);
// The pre/post blocks always have layers, so we know to always do a full insert/remove (so we pass true as the last argument).
block->moveChildrenTo(pre, boxFirst, beforeChild, true);
block->moveChildrenTo(post, beforeChild, 0, true);
// We already know the newBlockBox isn't going to contain inline kids, so avoid wasting
// time in makeChildrenNonInline by just setting this explicitly up front.
// We delayed adding the newChild until now so that the |newBlockBox| would be fully
// connected, thus allowing newChild access to a renderArena should it need
// to wrap itself in additional boxes (e.g., table construction).
// Always just do a full layout in order to ensure that line boxes (especially wrappers for images)
// get deleted properly. Because objects moved from the pre block into the post block, we want to
// make new line boxes instead of leaving the old line boxes around.
if (pre)
if (post)
RenderBlock* RenderBlock::columnsBlockForSpanningElement(RenderObject* newChild)
// FIXME: This function is the gateway for the addition of column-span support. It will
// be added to in three stages:
// (1) Immediate children of a multi-column block can span.
// (2) Nested block-level children with only block-level ancestors between them and the multi-column block can span.
// (3) Nested children with block or inline ancestors between them and the multi-column block can span (this is when we
// cross the streams and have to cope with both types of continuations mixed together).
// This function currently supports (1) and (2).
RenderBlock* columnsBlockAncestor = 0;
if (!newChild->isText() && newChild->style()->columnSpan() && !newChild->isBeforeOrAfterContent()
&& !newChild->isFloatingOrOutOfFlowPositioned() && !newChild->isInline() && !isAnonymousColumnSpanBlock()) {
columnsBlockAncestor = containingColumnsBlock(false);
if (columnsBlockAncestor) {
// Make sure that none of the parent ancestors have a continuation.
// If yes, we do not want split the block into continuations.
RenderObject* curr = this;
while (curr && curr != columnsBlockAncestor) {
if (curr->isRenderBlock() && toRenderBlock(curr)->continuation()) {
columnsBlockAncestor = 0;
curr = curr->parent();
return columnsBlockAncestor;
void RenderBlock::addChildIgnoringAnonymousColumnBlocks(RenderObject* newChild, RenderObject* beforeChild)
if (beforeChild && beforeChild->parent() != this) {
RenderObject* beforeChildContainer = beforeChild->parent();
while (beforeChildContainer->parent() != this)
beforeChildContainer = beforeChildContainer->parent();
if (beforeChildContainer->isAnonymous()) {
// If the requested beforeChild is not one of our children, then this is because
// there is an anonymous container within this object that contains the beforeChild.
RenderObject* beforeChildAnonymousContainer = beforeChildContainer;
if (beforeChildAnonymousContainer->isAnonymousBlock()
// Full screen renderers and full screen placeholders act as anonymous blocks, not tables:
|| beforeChildAnonymousContainer->isRenderFullScreen()
|| beforeChildAnonymousContainer->isRenderFullScreenPlaceholder()
) {
// Insert the child into the anonymous block box instead of here.
if (newChild->isInline() || beforeChild->parent()->firstChild() != beforeChild)
beforeChild->parent()->addChild(newChild, beforeChild);
addChild(newChild, beforeChild->parent());
if (newChild->isTablePart()) {
// Insert into the anonymous table.
beforeChildAnonymousContainer->addChild(newChild, beforeChild);
beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
ASSERT(beforeChild->parent() == this);
if (beforeChild->parent() != this) {
// We should never reach here. If we do, we need to use the
// safe fallback to use the topmost beforeChild container.
beforeChild = beforeChildContainer;
} else {
// We will reach here when beforeChild is a run-in element.
// If run-in element precedes a block-level element, it becomes the
// the first inline child of that block level element. The insertion
// point will be before that block-level element.
beforeChild = beforeChildContainer;
// Nothing goes before the intruded run-in.
if (beforeChild && beforeChild->isRunIn() && runInIsPlacedIntoSiblingBlock(beforeChild))
beforeChild = beforeChild->nextSibling();
// Check for a spanning element in columns.
if (gColumnFlowSplitEnabled) {
RenderBlock* columnsBlockAncestor = columnsBlockForSpanningElement(newChild);
if (columnsBlockAncestor) {
TemporaryChange<bool> columnFlowSplitEnabled(gColumnFlowSplitEnabled, false);
// We are placing a column-span element inside a block.
RenderBlock* newBox = createAnonymousColumnSpanBlock();
if (columnsBlockAncestor != this) {
// We are nested inside a multi-column element and are being split by the span. We have to break up
// our block into continuations.
RenderBoxModelObject* oldContinuation = continuation();
// When we split an anonymous block, there's no need to do any continuation hookup,
// since we haven't actually split a real element.
if (!isAnonymousBlock())
splitFlow(beforeChild, newBox, newChild, oldContinuation);
// We have to perform a split of this block's children. This involves creating an anonymous block box to hold
// the column-spanning |newChild|. We take all of the children from before |newChild| and put them into
// one anonymous columns block, and all of the children after |newChild| go into another anonymous block.
makeChildrenAnonymousColumnBlocks(beforeChild, newBox, newChild);
bool madeBoxesNonInline = false;
// A block has to either have all of its children inline, or all of its children as blocks.
// So, if our children are currently inline and a block child has to be inserted, we move all our
// inline children into anonymous block boxes.
if (childrenInline() && !newChild->isInline() && !newChild->isFloatingOrOutOfFlowPositioned()) {
// This is a block with inline content. Wrap the inline content in anonymous blocks.
madeBoxesNonInline = true;
if (beforeChild && beforeChild->parent() != this) {
beforeChild = beforeChild->parent();
ASSERT(beforeChild->parent() == this);
} else if (!childrenInline() && (newChild->isFloatingOrOutOfFlowPositioned() || newChild->isInline())) {
// If we're inserting an inline child but all of our children are blocks, then we have to make sure
// it is put into an anomyous block box. We try to use an existing anonymous box if possible, otherwise
// a new one is created and inserted into our list of children in the appropriate position.
RenderObject* afterChild = beforeChild ? beforeChild->previousSibling() : lastChild();
if (afterChild && afterChild->isAnonymousBlock()) {
if (newChild->isInline()) {
// No suitable existing anonymous box - create a new one.
RenderBlock* newBox = createAnonymousBlock();
RenderBox::addChild(newBox, beforeChild);
RenderBox::addChild(newChild, beforeChild);
// Handle placement of run-ins.
if (madeBoxesNonInline && parent() && isAnonymousBlock() && parent()->isRenderBlock())
// this object may be dead here
void RenderBlock::addChild(RenderObject* newChild, RenderObject* beforeChild)
if (continuation() && !isAnonymousBlock())
addChildToContinuation(newChild, beforeChild);
addChildIgnoringContinuation(newChild, beforeChild);
void RenderBlock::addChildIgnoringContinuation(RenderObject* newChild, RenderObject* beforeChild)
if (!isAnonymousBlock() && firstChild() && (firstChild()->isAnonymousColumnsBlock() || firstChild()->isAnonymousColumnSpanBlock()))
addChildToAnonymousColumnBlocks(newChild, beforeChild);
addChildIgnoringAnonymousColumnBlocks(newChild, beforeChild);
static void getInlineRun(RenderObject* start, RenderObject* boundary,
RenderObject*& inlineRunStart,
RenderObject*& inlineRunEnd)
// Beginning at |start| we find the largest contiguous run of inlines that
// we can. We denote the run with start and end points, |inlineRunStart|
// and |inlineRunEnd|. Note that these two values may be the same if
// we encounter only one inline.
// We skip any non-inlines we encounter as long as we haven't found any
// inlines yet.
// |boundary| indicates a non-inclusive boundary point. Regardless of whether |boundary|
// is inline or not, we will not include it in a run with inlines before it. It's as though we encountered
// a non-inline.
// Start by skipping as many non-inlines as we can.
RenderObject * curr = start;
bool sawInline;
do {
while (curr && !(curr->isInline() || curr->isFloatingOrOutOfFlowPositioned()))
curr = curr->nextSibling();
inlineRunStart = inlineRunEnd = curr;
if (!curr)
return; // No more inline children to be found.
sawInline = curr->isInline();
curr = curr->nextSibling();
while (curr && (curr->isInline() || curr->isFloatingOrOutOfFlowPositioned()) && (curr != boundary)) {
inlineRunEnd = curr;
if (curr->isInline())
sawInline = true;
curr = curr->nextSibling();
} while (!sawInline);
void RenderBlock::deleteLineBoxTree()
if (containsFloats()) {
// Clear references to originating lines, since the lines are being deleted
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
FloatingObjectSetIterator end = floatingObjectSet.end();
for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
ASSERT(!((*it)->m_originatingLine) || (*it)->m_originatingLine->renderer() == this);
(*it)->m_originatingLine = 0;
if (AXObjectCache* cache = document()->existingAXObjectCache())
RootInlineBox* RenderBlock::createRootInlineBox()
return new (renderArena()) RootInlineBox(this);
RootInlineBox* RenderBlock::createAndAppendRootInlineBox()
RootInlineBox* rootBox = createRootInlineBox();
if (UNLIKELY(AXObjectCache::accessibilityEnabled()) && m_lineBoxes.firstLineBox() == rootBox) {
if (AXObjectCache* cache = document()->existingAXObjectCache())
return rootBox;
void RenderBlock::makeChildrenNonInline(RenderObject *insertionPoint)
// makeChildrenNonInline takes a block whose children are *all* inline and it
// makes sure that inline children are coalesced under anonymous
// blocks. If |insertionPoint| is defined, then it represents the insertion point for
// the new block child that is causing us to have to wrap all the inlines. This
// means that we cannot coalesce inlines before |insertionPoint| with inlines following
// |insertionPoint|, because the new child is going to be inserted in between the inlines,
// splitting them.
ASSERT(isInlineBlockOrInlineTable() || !isInline());
ASSERT(!insertionPoint || insertionPoint->parent() == this);
RenderObject *child = firstChild();
if (!child)
// Since we are going to have block children, we have to move
// back the run-in to its original place.
if (child->isRunIn()) {
child = firstChild();
while (child) {
RenderObject *inlineRunStart, *inlineRunEnd;
getInlineRun(child, insertionPoint, inlineRunStart, inlineRunEnd);
if (!inlineRunStart)
child = inlineRunEnd->nextSibling();
RenderBlock* block = createAnonymousBlock();
children()->insertChildNode(this, block, inlineRunStart);
moveChildrenTo(block, inlineRunStart, child);
#ifndef NDEBUG
for (RenderObject *c = firstChild(); c; c = c->nextSibling())
void RenderBlock::removeLeftoverAnonymousBlock(RenderBlock* child)
if (child->continuation() || (child->firstChild() && (child->isAnonymousColumnSpanBlock() || child->isAnonymousColumnsBlock())))
RenderObject* firstAnChild = child->m_children.firstChild();
RenderObject* lastAnChild = child->m_children.lastChild();
if (firstAnChild) {
RenderObject* o = firstAnChild;
while (o) {
o = o->nextSibling();
if (child->previousSibling())
if (child->nextSibling())
if (child == m_children.firstChild())
if (child == m_children.lastChild())
} else {
if (child == m_children.firstChild())
if (child == m_children.lastChild())
if (child->previousSibling())
if (child->nextSibling())
child->m_next = 0;
// Remove all the information in the flow thread associated with the leftover anonymous block.
static bool canMergeContiguousAnonymousBlocks(RenderObject* oldChild, RenderObject* prev, RenderObject* next)
if (oldChild->documentBeingDestroyed() || oldChild->isInline() || oldChild->virtualContinuation())
return false;
if ((prev && (!prev->isAnonymousBlock() || toRenderBlock(prev)->continuation() || toRenderBlock(prev)->beingDestroyed()))
|| (next && (!next->isAnonymousBlock() || toRenderBlock(next)->continuation() || toRenderBlock(next)->beingDestroyed())))
return false;
// FIXME: This check isn't required when inline run-ins can't be split into continuations.
if (prev && prev->firstChild() && prev->firstChild()->isInline() && prev->firstChild()->isRunIn())
return false;
if ((prev && (prev->isRubyRun() || prev->isRubyBase()))
|| (next && (next->isRubyRun() || next->isRubyBase())))
return false;
if (!prev || !next)
return true;
// Make sure the types of the anonymous blocks match up.
return prev->isAnonymousColumnsBlock() == next->isAnonymousColumnsBlock()
&& prev->isAnonymousColumnSpanBlock() == next->isAnonymousColumnSpanBlock();
void RenderBlock::collapseAnonymousBoxChild(RenderBlock* parent, RenderObject* child)
RenderObject* nextSibling = child->nextSibling();
RenderFlowThread* childFlowThread = child->flowThreadContainingBlock();
CurrentRenderFlowThreadMaintainer flowThreadMaintainer(childFlowThread);
RenderBlock* anonBlock = toRenderBlock(parent->children()->removeChildNode(parent, child, child->hasLayer()));
anonBlock->moveAllChildrenTo(parent, nextSibling, child->hasLayer());
// Delete the now-empty block's lines and nuke it.
if (childFlowThread && childFlowThread->isRenderNamedFlowThread())
void RenderBlock::moveAllChildrenIncludingFloatsTo(RenderBlock* toBlock, bool fullRemoveInsert)
moveAllChildrenTo(toBlock, fullRemoveInsert);
// When a portion of the render tree is being detached, anonymous blocks
// will be combined as their children are deleted. In this process, the
// anonymous block later in the tree is merged into the one preceeding it.
// It can happen that the later block (this) contains floats that the
// previous block (toBlock) did not contain, and thus are not in the
// floating objects list for toBlock. This can result in toBlock containing
// floats that are not in it's floating objects list, but are in the
// floating objects lists of siblings and parents. This can cause problems
// when the float itself is deleted, since the deletion code assumes that
// if a float is not in it's containing block's floating objects list, it
// isn't in any floating objects list. In order to preserve this condition
// (removing it has serious performance implications), we need to copy the
// floating objects from the old block (this) to the new block (toBlock).
// The float's metrics will likely all be wrong, but since toBlock is
// already marked for layout, this will get fixed before anything gets
// displayed.
// See bug
if (m_floatingObjects) {
if (!toBlock->m_floatingObjects)
const FloatingObjectSet& fromFloatingObjectSet = m_floatingObjects->set();
FloatingObjectSetIterator end = fromFloatingObjectSet.end();
for (FloatingObjectSetIterator it = fromFloatingObjectSet.begin(); it != end; ++it) {
FloatingObject* floatingObject = *it;
// Don't insert the object again if it's already in the list
if (toBlock->containsFloat(floatingObject->renderer()))
void RenderBlock::removeChild(RenderObject* oldChild)
// No need to waste time in merging or removing empty anonymous blocks.
// We can just bail out if our document is getting destroyed.
if (documentBeingDestroyed()) {
// This protects against column split flows when anonymous blocks are getting merged.
TemporaryChange<bool> columnFlowSplitEnabled(gColumnFlowSplitEnabled, false);
// If this child is a block, and if our previous and next siblings are
// both anonymous blocks with inline content, then we can go ahead and
// fold the inline content back together.
RenderObject* prev = oldChild->previousSibling();
RenderObject* next = oldChild->nextSibling();
bool canMergeAnonymousBlocks = canMergeContiguousAnonymousBlocks(oldChild, prev, next);
if (canMergeAnonymousBlocks && prev && next) {
RenderBlock* nextBlock = toRenderBlock(next);
RenderBlock* prevBlock = toRenderBlock(prev);
if (prev->childrenInline() != next->childrenInline()) {
RenderBlock* inlineChildrenBlock = prev->childrenInline() ? prevBlock : nextBlock;
RenderBlock* blockChildrenBlock = prev->childrenInline() ? nextBlock : prevBlock;
// Place the inline children block inside of the block children block instead of deleting it.
// In order to reuse it, we have to reset it to just be a generic anonymous block. Make sure
// to clear out inherited column properties by just making a new style, and to also clear the
// column span flag if it is set.
RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(style(), BLOCK);
// Cache this value as it might get changed in setStyle() call.
bool inlineChildrenBlockHasLayer = inlineChildrenBlock->hasLayer();
children()->removeChildNode(this, inlineChildrenBlock, inlineChildrenBlockHasLayer);
// Now just put the inlineChildrenBlock inside the blockChildrenBlock.
blockChildrenBlock->children()->insertChildNode(blockChildrenBlock, inlineChildrenBlock, prev == inlineChildrenBlock ? blockChildrenBlock->firstChild() : 0,
inlineChildrenBlockHasLayer || blockChildrenBlock->hasLayer());
// inlineChildrenBlock got reparented to blockChildrenBlock, so it is no longer a child
// of "this". we null out prev or next so that is not used later in the function.
if (inlineChildrenBlock == prevBlock)
prev = 0;
next = 0;
} else {
// Take all the children out of the |next| block and put them in
// the |prev| block.
nextBlock->moveAllChildrenIncludingFloatsTo(prevBlock, nextBlock->hasLayer() || prevBlock->hasLayer());
// Delete the now-empty block's lines and nuke it.
next = 0;
RenderObject* child = prev ? prev : next;
if (canMergeAnonymousBlocks && child && !child->previousSibling() && !child->nextSibling() && canCollapseAnonymousBlockChild()) {
// The removal has knocked us down to containing only a single anonymous
// box. We can go ahead and pull the content right back up into our
// box.
collapseAnonymousBoxChild(this, child);
} else if (((prev && prev->isAnonymousBlock()) || (next && next->isAnonymousBlock())) && canCollapseAnonymousBlockChild()) {
// It's possible that the removal has knocked us down to a single anonymous
// block with pseudo-style element siblings (e.g. first-letter). If these
// are floating, then we need to pull the content up also.
RenderBlock* anonBlock = toRenderBlock((prev && prev->isAnonymousBlock()) ? prev : next);
if ((anonBlock->previousSibling() || anonBlock->nextSibling())
&& (!anonBlock->previousSibling() || (anonBlock->previousSibling()->style()->styleType() != NOPSEUDO && anonBlock->previousSibling()->isFloating() && !anonBlock->previousSibling()->previousSibling()))
&& (!anonBlock->nextSibling() || (anonBlock->nextSibling()->style()->styleType() != NOPSEUDO && anonBlock->nextSibling()->isFloating() && !anonBlock->nextSibling()->nextSibling()))) {
collapseAnonymousBoxChild(this, anonBlock);
if (!firstChild()) {
// If this was our last child be sure to clear out our line boxes.
if (childrenInline())
// If we are an empty anonymous block in the continuation chain,
// we need to remove ourself and fix the continuation chain.
if (!beingDestroyed() && isAnonymousBlockContinuation() && !oldChild->isListMarker()) {
RenderObject* containingBlockIgnoringAnonymous = containingBlock();
while (containingBlockIgnoringAnonymous && containingBlockIgnoringAnonymous->isAnonymousBlock())
containingBlockIgnoringAnonymous = containingBlockIgnoringAnonymous->containingBlock();
for (RenderObject* curr = this; curr; curr = curr->previousInPreOrder(containingBlockIgnoringAnonymous)) {
if (curr->virtualContinuation() != this)
// Found our previous continuation. We just need to point it to
// |this|'s next continuation.
RenderBoxModelObject* nextContinuation = continuation();
if (curr->isRenderInline())
else if (curr->isRenderBlock())
bool RenderBlock::isSelfCollapsingBlock() const
// We are not self-collapsing if we
// (a) have a non-zero height according to layout (an optimization to avoid wasting time)
// (b) are a table,
// (c) have border/padding,
// (d) have a min-height
// (e) have specified that one of our margins can't collapse using a CSS extension
if (logicalHeight() > 0
|| isTable() || borderAndPaddingLogicalHeight()
|| style()->logicalMinHeight().isPositive()
|| style()->marginBeforeCollapse() == MSEPARATE || style()->marginAfterCollapse() == MSEPARATE)
return false;
Length logicalHeightLength = style()->logicalHeight();
bool hasAutoHeight = logicalHeightLength.isAuto();
if (logicalHeightLength.isPercent() && !document()->inQuirksMode()) {
hasAutoHeight = true;
for (RenderBlock* cb = containingBlock(); !cb->isRenderView(); cb = cb->containingBlock()) {
if (cb->style()->logicalHeight().isFixed() || cb->isTableCell())
hasAutoHeight = false;
// If the height is 0 or auto, then whether or not we are a self-collapsing block depends
// on whether we have content that is all self-collapsing or not.
if (hasAutoHeight || ((logicalHeightLength.isFixed() || logicalHeightLength.isPercent()) && logicalHeightLength.isZero())) {
// If the block has inline children, see if we generated any line boxes. If we have any
// line boxes, then we can't be self-collapsing, since we have content.
if (childrenInline())
return !firstLineBox();
// Whether or not we collapse is dependent on whether all our normal flow children
// are also self-collapsing.
for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
if (child->isFloatingOrOutOfFlowPositioned())
if (!child->isSelfCollapsingBlock())
return false;
return true;
return false;
void RenderBlock::startDelayUpdateScrollInfo()
if (gDelayUpdateScrollInfo == 0) {
gDelayedUpdateScrollInfoSet = new DelayedUpdateScrollInfoSet;
void RenderBlock::finishDelayUpdateScrollInfo()
ASSERT(gDelayUpdateScrollInfo >= 0);
if (gDelayUpdateScrollInfo == 0) {
OwnPtr<DelayedUpdateScrollInfoSet> infoSet(adoptPtr(gDelayedUpdateScrollInfoSet));
gDelayedUpdateScrollInfoSet = 0;
for (DelayedUpdateScrollInfoSet::iterator it = infoSet->begin(); it != infoSet->end(); ++it) {
RenderBlock* block = *it;
if (block->hasOverflowClip()) {
void RenderBlock::updateScrollInfoAfterLayout()
if (hasOverflowClip()) {
if (style()->isFlippedBlocksWritingMode()) {
// Workaround for now. We cannot delay the scroll info for overflow
// for items with opposite writing directions, as the contents needs
// to overflow in that direction
if (gDelayUpdateScrollInfo)
void RenderBlock::layout()
StackStats::LayoutCheckPoint layoutCheckPoint;
OverflowEventDispatcher dispatcher(this);
// Update our first letter info now.
// Table cells call layoutBlock directly, so don't add any logic here. Put code into
// layoutBlock().
// It's safe to check for control clip here, since controls can never be table cells.
// If we have a lightweight clip, there can never be any overflow from children.
if (hasControlClip() && m_overflow)
void RenderBlock::updateExclusionShapeInsideInfoAfterStyleChange(const ExclusionShapeValue* shapeInside, const ExclusionShapeValue* oldShapeInside)
// FIXME: A future optimization would do a deep comparison for equality.
if (shapeInside == oldShapeInside)
ExclusionShapeInsideInfo* exclusionShapeInsideInfo;
if (shapeInside) {
exclusionShapeInsideInfo = ensureExclusionShapeInsideInfo();
} else if ((exclusionShapeInsideInfo = this->exclusionShapeInsideInfo(ShapePresentOrRemoved)))
static inline bool exclusionInfoRequiresRelayout(const RenderBlock* block)
return false;
ExclusionShapeInsideInfo* info = block->exclusionShapeInsideInfo(RenderBlock::ShapePresentOrRemoved);
if (info)
info->setNeedsLayout(info->shapeSizeDirty() || info->needsRemoval());
info = block->layoutExclusionShapeInsideInfo(RenderBlock::ShapePresentOrRemoved);
return info && info->needsLayout();
bool RenderBlock::updateRegionsAndExclusionsBeforeChildLayout(RenderFlowThread* flowThread)
if (!flowThread && !exclusionShapeInsideInfo())
if (!flowThread)
return exclusionInfoRequiresRelayout(this);
LayoutUnit oldHeight = logicalHeight();
LayoutUnit oldTop = logicalTop();
// Compute the maximum logical height content may cause this block to expand to
// FIXME: These should eventually use the const computeLogicalHeight rather than updateLogicalHeight
setLogicalHeight(LayoutUnit::max() / 2);
// Set our start and end regions. No regions above or below us will be considered by our children. They are
// effectively clamped to our region range.
return exclusionInfoRequiresRelayout(this);
void RenderBlock::computeExclusionShapeSize()
ExclusionShapeInsideInfo* exclusionShapeInsideInfo = this->exclusionShapeInsideInfo();
if (exclusionShapeInsideInfo) {
bool percentageLogicalHeightResolvable = percentageLogicalHeightIsResolvableFromBlock(this, false);
exclusionShapeInsideInfo->setShapeSize(logicalWidth(), percentageLogicalHeightResolvable ? logicalHeight() : LayoutUnit());
void RenderBlock::updateRegionsAndExclusionsAfterChildLayout(RenderFlowThread* flowThread, bool heightChanged)
ExclusionShapeInsideInfo* exclusionShapeInsideInfo = this->exclusionShapeInsideInfo(ShapePresentOrRemoved);
if (exclusionShapeInsideInfo && exclusionShapeInsideInfo->needsRemoval())
// A previous sibling has changed dimension, so we need to relayout the shape with the content
ExclusionShapeInsideInfo* shapeInsideInfo = layoutExclusionShapeInsideInfo();
if (heightChanged && shapeInsideInfo)
void RenderBlock::computeRegionRangeForBlock(RenderFlowThread* flowThread)
if (flowThread)
flowThread->setRegionRangeForBox(this, offsetFromLogicalTopOfFirstPage());
bool RenderBlock::updateLogicalWidthAndColumnWidth()
LayoutUnit oldWidth = logicalWidth();
LayoutUnit oldColumnWidth = desiredColumnWidth();
bool hasBorderOrPaddingLogicalWidthChanged = m_hasBorderOrPaddingLogicalWidthChanged;
m_hasBorderOrPaddingLogicalWidthChanged = false;
return oldWidth != logicalWidth() || oldColumnWidth != desiredColumnWidth() || hasBorderOrPaddingLogicalWidthChanged;
void RenderBlock::checkForPaginationLogicalHeightChange(LayoutUnit& pageLogicalHeight, bool& pageLogicalHeightChanged, bool& hasSpecifiedPageLogicalHeight)
ColumnInfo* colInfo = columnInfo();
if (hasColumns()) {
if (!pageLogicalHeight) {
// We need to go ahead and set our explicit page height if one exists, so that we can
// avoid doing two layout passes.
LayoutUnit columnHeight = contentLogicalHeight();
if (columnHeight > 0) {
pageLogicalHeight = columnHeight;
hasSpecifiedPageLogicalHeight = true;
if (colInfo->columnHeight() != pageLogicalHeight && everHadLayout())
pageLogicalHeightChanged = true;
if (!hasSpecifiedPageLogicalHeight && !pageLogicalHeight)
} else if (isRenderFlowThread()) {
pageLogicalHeight = 1; // This is just a hack to always make sure we have a page logical height.
pageLogicalHeightChanged = toRenderFlowThread(this)->pageLogicalSizeChanged();
void RenderBlock::layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight)
if (isInline() && !isInlineBlockOrInlineTable()) // Inline <form>s inside various table elements can
return; // cause us to come in here. Just bail.
if (!relayoutChildren && simplifiedLayout())
LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
if (updateLogicalWidthAndColumnWidth())
relayoutChildren = true;
LayoutUnit previousHeight = logicalHeight();
// FIXME: should this start out as borderAndPaddingLogicalHeight() + scrollbarLogicalHeight(),
// for consistency with other render classes?
bool pageLogicalHeightChanged = false;
bool hasSpecifiedPageLogicalHeight = false;
checkForPaginationLogicalHeightChange(pageLogicalHeight, pageLogicalHeightChanged, hasSpecifiedPageLogicalHeight);
RenderView* renderView = view();
RenderStyle* styleToUse = style();
LayoutStateMaintainer statePusher(renderView, this, locationOffset(), hasColumns() || hasTransform() || hasReflection() || styleToUse->isFlippedBlocksWritingMode(), pageLogicalHeight, pageLogicalHeightChanged, columnInfo());
// Regions changing widths can force us to relayout our children.
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (logicalWidthChangedInRegions(flowThread))
relayoutChildren = true;
if (updateRegionsAndExclusionsBeforeChildLayout(flowThread))
relayoutChildren = true;
// We use four values, maxTopPos, maxTopNeg, maxBottomPos, and maxBottomNeg, to track
// our current maximal positive and negative margins. These values are used when we
// are collapsed with adjacent blocks, so for example, if you have block A and B
// collapsing together, then you'd take the maximal positive margin from both A and B
// and subtract it from the maximal negative margin from both A and B to get the
// true collapsed margin. This algorithm is recursive, so when we finish layout()
// our block knows its current maximal positive/negative values.
// Start out by setting our margin values to our current margins. Table cells have
// no margins, so we don't fill in the values for table cells.
bool isCell = isTableCell();
if (!isCell) {
LayoutUnit repaintLogicalTop = 0;
LayoutUnit repaintLogicalBottom = 0;
LayoutUnit maxFloatLogicalBottom = 0;
if (!firstChild() && !isAnonymousBlock())
if (childrenInline())
layoutInlineChildren(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
layoutBlockChildren(relayoutChildren, maxFloatLogicalBottom);
// Expand our intrinsic height to encompass floats.
LayoutUnit toAdd = borderAfter() + paddingAfter() + scrollbarLogicalHeight();
if (lowestFloatLogicalBottom() > (logicalHeight() - toAdd) && expandsToEncloseOverhangingFloats())
setLogicalHeight(lowestFloatLogicalBottom() + toAdd);
if (relayoutForPagination(hasSpecifiedPageLogicalHeight, pageLogicalHeight, statePusher))
// Calculate our new height.
LayoutUnit oldHeight = logicalHeight();
LayoutUnit oldClientAfterEdge = clientLogicalBottom();
// Before updating the final size of the flow thread make sure a forced break is applied after the content.
// This ensures the size information is correctly computed for the last auto-height region receiving content.
if (isRenderFlowThread())
LayoutUnit newHeight = logicalHeight();
if (oldHeight != newHeight) {
if (oldHeight > newHeight && maxFloatLogicalBottom > newHeight && !childrenInline()) {
// One of our children's floats may have become an overhanging float for us. We need to look for it.
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->isBlockFlow() && !child->isFloatingOrOutOfFlowPositioned()) {
RenderBlock* block = toRenderBlock(child);
if (block->lowestFloatLogicalBottom() + block->logicalTop() > newHeight)
addOverhangingFloats(block, false);
bool heightChanged = (previousHeight != newHeight);
if (heightChanged)
relayoutChildren = true;
layoutPositionedObjects(relayoutChildren || isRoot());
updateRegionsAndExclusionsAfterChildLayout(flowThread, heightChanged);
// Add overflow from children (unless we're multi-column, since in that case all our child overflow is clipped anyway).
if (renderView->layoutState()->m_pageLogicalHeight)
setPageLogicalOffset(renderView->layoutState()->pageLogicalOffset(this, logicalTop()));
// Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
// we overflow or not.
// FIXME: This repaint logic should be moved into a separate helper function!
// Repaint with our new bounds if they are different from our old bounds.
bool didFullRepaint = repainter.repaintAfterLayout();
if (!didFullRepaint && repaintLogicalTop != repaintLogicalBottom && (styleToUse->visibility() == VISIBLE || enclosingLayer()->hasVisibleContent())) {
// FIXME: We could tighten up the left and right invalidation points if we let layoutInlineChildren fill them in based off the particular lines
// it had to lay out. We wouldn't need the hasOverflowClip() hack in that case either.
LayoutUnit repaintLogicalLeft = logicalLeftVisualOverflow();
LayoutUnit repaintLogicalRight = logicalRightVisualOverflow();
if (hasOverflowClip()) {
// If we have clipped overflow, we should use layout overflow as well, since visual overflow from lines didn't propagate to our block's overflow.
// Note the old code did this as well but even for overflow:visible. The addition of hasOverflowClip() at least tightens up the hack a bit.
// layoutInlineChildren should be patched to compute the entire repaint rect.
repaintLogicalLeft = min(repaintLogicalLeft, logicalLeftLayoutOverflow());
repaintLogicalRight = max(repaintLogicalRight, logicalRightLayoutOverflow());
LayoutRect repaintRect;
if (isHorizontalWritingMode())
repaintRect = LayoutRect(repaintLogicalLeft, repaintLogicalTop, repaintLogicalRight - repaintLogicalLeft, repaintLogicalBottom - repaintLogicalTop);
repaintRect = LayoutRect(repaintLogicalTop, repaintLogicalLeft, repaintLogicalBottom - repaintLogicalTop, repaintLogicalRight - repaintLogicalLeft);
// The repaint rect may be split across columns, in which case adjustRectForColumns() will return the union.
if (hasOverflowClip()) {
// Adjust repaint rect for scroll offset
// Don't allow this rect to spill out of our overflow box.
repaintRect.intersect(LayoutRect(LayoutPoint(), size()));
// Make sure the rect is still non-empty after intersecting for overflow above
if (!repaintRect.isEmpty()) {
repaintRectangle(repaintRect); // We need to do a partial repaint of our content.
if (hasReflection())
void RenderBlock::addOverflowFromChildren()
if (!hasColumns()) {
if (childrenInline())
} else {
ColumnInfo* colInfo = columnInfo();
if (columnCount(colInfo)) {
LayoutRect lastRect = columnRectAt(colInfo, columnCount(colInfo) - 1);
if (!hasOverflowClip())
void RenderBlock::computeOverflow(LayoutUnit oldClientAfterEdge, bool recomputeFloats)
// Add overflow from children.
if (!hasColumns() && (recomputeFloats || isRoot() || expandsToEncloseOverhangingFloats() || hasSelfPaintingLayer()))
// Add in the overflow from positioned objects.
if (hasOverflowClip()) {
// When we have overflow clip, propagate the original spillout since it will include collapsed bottom margins
// and bottom padding. Set the axis we don't care about to be 1, since we want this overflow to always
// be considered reachable.
LayoutRect clientRect(clientBoxRect());
LayoutRect rectToApply;
if (isHorizontalWritingMode())
rectToApply = LayoutRect(clientRect.x(), clientRect.y(), 1, max<LayoutUnit>(0, oldClientAfterEdge - clientRect.y()));
rectToApply = LayoutRect(clientRect.x(), clientRect.y(), max<LayoutUnit>(0, oldClientAfterEdge - clientRect.x()), 1);
if (hasRenderOverflow())
// Allow our overflow to catch cases where the caret in an empty editable element with negative text indent needs to get painted.
LayoutUnit textIndent = textIndentOffset();
if (textIndent < 0) {
LayoutRect clientRect(clientBoxRect());
LayoutRect rectToApply = LayoutRect(clientRect.x() + min<LayoutUnit>(0, textIndent), clientRect.y(), clientRect.width() - min<LayoutUnit>(0, textIndent), clientRect.height());
// Add visual overflow from box-shadow and border-image-outset.
// Add visual overflow from theme.
if (isRenderFlowThread())
void RenderBlock::addOverflowFromBlockChildren()
for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
if (!child->isFloatingOrOutOfFlowPositioned())
void RenderBlock::addOverflowFromFloats()
if (!m_floatingObjects)
const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
FloatingObjectSetIterator end = floatingObjectSet.end();
for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
FloatingObject* r = *it;
if (r->isDescendant())
addOverflowFromChild(r->m_renderer, IntSize(xPositionForFloatIncludingMargin(r), yPositionForFloatIncludingMargin(r)));
void RenderBlock::addOverflowFromPositionedObjects()
TrackedRendererListHashSet* positionedDescendants = positionedObjects();
if (!positionedDescendants)
RenderBox* positionedObject;
TrackedRendererListHashSet::iterator end = positionedDescendants->end();
for (TrackedRendererListHashSet::iterator it = positionedDescendants->begin(); it != end; ++it) {
positionedObject = *it;
// Fixed positioned elements don't contribute to layout overflow, since they don't scroll with the content.
if (positionedObject->style()->position() != FixedPosition) {
LayoutUnit x = positionedObject->x();
if (style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
x -= verticalScrollbarWidth();
addOverflowFromChild(positionedObject, LayoutSize(x, positionedObject->y()));
void RenderBlock::addVisualOverflowFromTheme()
if (!style()->hasAppearance())
IntRect inflatedRect = pixelSnappedBorderBoxRect();
theme()->adjustRepaintRect(this, inflatedRect);
bool RenderBlock::expandsToEncloseOverhangingFloats() const
return isInlineBlockOrInlineTable() || isFloatingOrOutOfFlowPositioned() || hasOverflowClip() || (parent() && parent()->isFlexibleBoxIncludingDeprecated())
|| hasColumns() || isTableCell() || isTableCaption() || isFieldset() || isWritingModeRoot() || isRoot();
void RenderBlock::adjustPositionedBlock(RenderBox* child, const MarginInfo& marginInfo)
bool isHorizontal = isHorizontalWritingMode();
bool hasStaticBlockPosition = child->style()->hasStaticBlockPosition(isHorizontal);
LayoutUnit logicalTop = logicalHeight();
updateStaticInlinePositionForChild(child, logicalTop);
if (!marginInfo.canCollapseWithMarginBefore()) {
// Positioned blocks don't collapse margins, so add the margin provided by
// the container now. The child's own margin is added later when calculating its logical top.
LayoutUnit collapsedBeforePos = marginInfo.positiveMargin();
LayoutUnit collapsedBeforeNeg = marginInfo.negativeMargin();
logicalTop += collapsedBeforePos - collapsedBeforeNeg;
RenderLayer* childLayer = child->layer();
if (childLayer->staticBlockPosition() != logicalTop) {
if (hasStaticBlockPosition)
child->setChildNeedsLayout(true, MarkOnlyThis);
void RenderBlock::adjustFloatingBlock(const MarginInfo& marginInfo)
// The float should be positioned taking into account the bottom margin
// of the previous flow. We add that margin into the height, get the
// float positioned properly, and then subtract the margin out of the
// height again. In the case of self-collapsing blocks, we always just
// use the top margins, since the self-collapsing block collapsed its
// own bottom margin into its top margin.
// Note also that the previous flow may collapse its margin into the top of
// our block. If this is the case, then we do not add the margin in to our
// height when computing the position of the float. This condition can be tested
// for by simply calling canCollapseWithMarginBefore. See
// for
// an example of this scenario.
LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
setLogicalHeight(logicalHeight() + marginOffset);
setLogicalHeight(logicalHeight() - marginOffset);
static void destroyRunIn(RenderBoxModelObject* runIn)
// Delete our line box tree. This is needed as our children got moved
// and our line box tree is no longer valid.
if (runIn->isRenderBlock())
else if (runIn->isRenderInline())
void RenderBlock::placeRunInIfNeeded(RenderObject* newChild)
if (newChild->isRunIn())
else if (RenderObject* prevSibling = newChild->previousSibling()) {
if (prevSibling->isRunIn())
RenderBoxModelObject* RenderBlock::createReplacementRunIn(RenderBoxModelObject* runIn)
RenderBoxModelObject* newRunIn = 0;
if (!runIn->isRenderBlock())
newRunIn = new (renderArena()) RenderBlock(runIn->node());
newRunIn = new (renderArena()) RenderInline(toElement(runIn->node()));
runIn->moveAllChildrenTo(newRunIn, true);
return newRunIn;
void RenderBlock::moveRunInUnderSiblingBlockIfNeeded(RenderObject* runIn)
// See if we have inline children. If the children aren't inline,
// then just treat the run-in as a normal block.
if (!runIn->childrenInline())
// FIXME: We don't handle non-block elements with run-in for now.
if (!runIn->isRenderBlock())
// FIXME: We don't support run-ins with or as part of a continuation
// as it makes the back-and-forth placing complex.
if (runIn->isElementContinuation() || runIn->virtualContinuation())
// Check if this node is allowed to run-in. E.g. <select> expects its renderer to
// be a RenderListBox or RenderMenuList, and hence cannot be a RenderInline run-in.
if (!runIn->canBeReplacedWithInlineRunIn())
RenderObject* curr = runIn->nextSibling();
if (!curr || !curr->isRenderBlock() || !curr->childrenInline())
// Per CSS3, "A run-in cannot run in to a block that already starts with a
// run-in or that itself is a run-in".
if (curr->isRunIn() || (curr->firstChild() && curr->firstChild()->isRunIn()))
if (curr->isAnonymous() || curr->isFloatingOrOutOfFlowPositioned())
RenderBoxModelObject* oldRunIn = toRenderBoxModelObject(runIn);
RenderBoxModelObject* newRunIn = createReplacementRunIn(oldRunIn);
// Now insert the new child under |curr| block. Use addChild instead of insertChildNode
// since it handles correct placement of the children, especially where we cannot insert
// anything before the first child. e.g. details tag. See
curr->addChild(newRunIn, curr->firstChild());
// Make sure that |this| get a layout since its run-in child moved.
bool RenderBlock::runInIsPlacedIntoSiblingBlock(RenderObject* runIn)
// If we don't have a parent, we can't be moved into our sibling block.
if (!parent())
return false;
// An intruded run-in needs to be an inline.
if (!runIn->isRenderInline())
return false;
return true;
void RenderBlock::moveRunInToOriginalPosition(RenderObject* runIn)
if (!runInIsPlacedIntoSiblingBlock(runIn))
// FIXME: Run-in that are now placed in sibling block can break up into continuation
// chains when new children are added to it. We cannot easily send them back to their
// original place since that requires writing integration logic with RenderInline::addChild
// and all other places that might cause continuations to be created (without blowing away
// |this|). Disabling this feature for now to prevent crashes.
if (runIn->isElementContinuation() || runIn->virtualContinuation())
RenderBoxModelObject* oldRunIn = toRenderBoxModelObject(runIn);
RenderBoxModelObject* newRunIn = createReplacementRunIn(oldRunIn);
// Add the run-in block as our previous sibling.
parent()->addChild(newRunIn, this);
// Make sure that the parent holding the new run-in gets layout.
LayoutUnit RenderBlock::collapseMargins(RenderBox* child, MarginInfo& marginInfo)
bool childDiscardMarginBefore = mustDiscardMarginBeforeForChild(child);
bool childDiscardMarginAfter = mustDiscardMarginAfterForChild(child);
bool childIsSelfCollapsing = child->isSelfCollapsingBlock();
// The child discards the before margin when the the after margin has discard in the case of a self collapsing block.
childDiscardMarginBefore = childDiscardMarginBefore || (childDiscardMarginAfter && childIsSelfCollapsing);
// Get the four margin values for the child and cache them.
const MarginValues childMargins = marginValuesForChild(child);
// Get our max pos and neg top margins.
LayoutUnit posTop = childMargins.positiveMarginBefore();
LayoutUnit negTop = childMargins.negativeMarginBefore();
// For self-collapsing blocks, collapse our bottom margins into our
// top to get new posTop and negTop values.
if (childIsSelfCollapsing) {
posTop = max(posTop, childMargins.positiveMarginAfter());
negTop = max(negTop, childMargins.negativeMarginAfter());
// See if the top margin is quirky. We only care if this child has
// margins that will collapse with us.
bool topQuirk = hasMarginBeforeQuirk(child);
if (marginInfo.canCollapseWithMarginBefore()) {
if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
// This child is collapsing with the top of the
// block. If it has larger margin values, then we need to update
// our own maximal values.
if (!document()->inQuirksMode() || !marginInfo.quirkContainer() || !topQuirk)
setMaxMarginBeforeValues(max(posTop, maxPositiveMarginBefore()), max(negTop, maxNegativeMarginBefore()));
// The minute any of the margins involved isn't a quirk, don't
// collapse it away, even if the margin is smaller (
// has an example of this, a <dt> with 0.8em author-specified inside
// a <dl> inside a <td>.
if (!marginInfo.determinedMarginBeforeQuirk() && !topQuirk && (posTop - negTop)) {
if (!marginInfo.determinedMarginBeforeQuirk() && topQuirk && !marginBefore())
// We have no top margin and our top child has a quirky margin.
// We will pick up this quirky margin and pass it through.
// This deals with the <td><div><p> case.
// Don't do this for a block that split two inlines though. You do
// still apply margins in this case.
} else
// The before margin of the container will also discard all the margins it is collapsing with.
// Once we find a child with discardMarginBefore all the margins collapsing with us must also discard.
if (childDiscardMarginBefore) {
if (marginInfo.quirkContainer() && marginInfo.atBeforeSideOfBlock() && (posTop - negTop))
LayoutUnit beforeCollapseLogicalTop = logicalHeight();
LayoutUnit logicalTop = beforeCollapseLogicalTop;
if (childIsSelfCollapsing) {
// For a self collapsing block both the before and after margins get discarded. The block doesn't contribute anything to the height of the block.
// Also, the child's top position equals the logical height of the container.
if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
// This child has no height. We need to compute our
// position before we collapse the child's margins together,
// so that we can get an accurate position for the zero-height block.
LayoutUnit collapsedBeforePos = max(marginInfo.positiveMargin(), childMargins.positiveMarginBefore());
LayoutUnit collapsedBeforeNeg = max(marginInfo.negativeMargin(), childMargins.negativeMarginBefore());
marginInfo.setMargin(collapsedBeforePos, collapsedBeforeNeg);
// Now collapse the child's margins together, which means examining our
// bottom margin values as well.
if (!marginInfo.canCollapseWithMarginBefore())
// We need to make sure that the position of the self-collapsing block
// is correct, since it could have overflowing content
// that needs to be positioned correctly (e.g., a block that
// had a specified height of 0 but that actually had subcontent).
logicalTop = logicalHeight() + collapsedBeforePos - collapsedBeforeNeg;
} else {
if (mustSeparateMarginBeforeForChild(child)) {
ASSERT(!marginInfo.discardMargin() || (marginInfo.discardMargin() && !marginInfo.margin()));
// If we are at the before side of the block and we collapse, ignore the computed margin
// and just add the child margin to the container height. This will correctly position
// the child inside the container.
LayoutUnit separateMargin = !marginInfo.canCollapseWithMarginBefore() ? marginInfo.margin() : LayoutUnit(0);
setLogicalHeight(logicalHeight() + separateMargin + marginBeforeForChild(child));
logicalTop = logicalHeight();
} else if (!marginInfo.discardMargin() && (!marginInfo.atBeforeSideOfBlock()
|| (!marginInfo.canCollapseMarginBeforeWithChildren()
&& (!document()->inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginBeforeQuirk())))) {
// We're collapsing with a previous sibling's margins and not
// with the top of the block.
setLogicalHeight(logicalHeight() + max(marginInfo.positiveMargin(), posTop) - max(marginInfo.negativeMargin(), negTop));
logicalTop = logicalHeight();
if (!marginInfo.discardMargin()) {
} else
if (marginInfo.margin())
// If margins would pull us past the top of the next page, then we need to pull back and pretend like the margins
// collapsed into the page edge.
LayoutState* layoutState = view()->layoutState();
if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTop > beforeCollapseLogicalTop
&& hasNextPage(beforeCollapseLogicalTop)) {
LayoutUnit oldLogicalTop = logicalTop;
logicalTop = min(logicalTop, nextPageLogicalTop(beforeCollapseLogicalTop));
setLogicalHeight(logicalHeight() + (logicalTop - oldLogicalTop));
// If we have collapsed into a previous sibling and so reduced the height of the parent, ensure any floats that now
// overhang from the previous sibling are added to our parent. If the child's previous sibling itself is a float the child will avoid
// or clear it anyway, so don't worry about any floating children it may contain.
LayoutUnit oldLogicalHeight = logicalHeight();
RenderObject* prev = child->previousSibling();
if (prev && prev->isBlockFlow() && !prev->isFloatingOrOutOfFlowPositioned()) {
RenderBlock* block = toRenderBlock(prev);
if (block->containsFloats() && !block->avoidsFloats() && (block->logicalTop() + block->lowestFloatLogicalBottom()) > logicalTop)
addOverhangingFloats(block, false);
return logicalTop;
LayoutUnit RenderBlock::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);
// For self-collapsing blocks that clear, they can still collapse their
// margins with following siblings. Reset the current margins to represent
// the self-collapsing block's margins only.
// If DISCARD is specified for -webkit-margin-collapse, reset the margin values.
if (!childDiscardMargin) {
MarginValues childMargins = marginValuesForChild(child);
marginInfo.setPositiveMargin(max(childMargins.positiveMarginBefore(), childMargins.positiveMarginAfter()));
marginInfo.setNegativeMargin(max(childMargins.negativeMarginBefore(), childMargins.negativeMarginAfter()));
} else
// CSS2.1 states:
// "If the top and bottom margins of an element with clearance are adjoining, its margins collapse with
// the adjoining margins of following siblings but that resulting margin does not collapse with the bottom margin of the parent block."
// So the parent's bottom margin cannot collapse through this block or any subsequent self-collapsing blocks. Check subsequent siblings
// for a block with height - if none is found then don't allow the margins to collapse with the parent.
bool wouldCollapseMarginsWithParent = marginInfo.canCollapseMarginAfterWithChildren();
for (RenderBox* curr = child->nextSiblingBox(); curr && wouldCollapseMarginsWithParent; curr = curr->nextSiblingBox()) {
if (!curr->isFloatingOrOutOfFlowPositioned() && !curr->isSelfCollapsingBlock())
wouldCollapseMarginsWithParent = false;
if (wouldCollapseMarginsWithParent)
// CSS2.1: "the amount of clearance is set so that clearance + margin-top = [height of float], i.e., clearance = [height of float] - margin-top"
// Move the top of the child box to the bottom of the float ignoring the child's top margin.
LayoutUnit collapsedMargin = collapsedMarginBeforeForChild(child);
setLogicalHeight(child->logicalTop() - collapsedMargin);
// A negative collapsed margin-top value cancels itself out as it has already been factored into |yPos| above.
heightIncrease -= max(LayoutUnit(), collapsedMargin);
} else
// Increase our height by the amount we had to clear.
setLogicalHeight(logicalHeight() + heightIncrease);
if (marginInfo.canCollapseWithMarginBefore()) {
// We can no longer collapse with the top of the block since a clear
// occurred. The empty blocks collapse into the cleared block.
// FIXME: This isn't quite correct. Need clarification for what to do
// if the height the cleared block is offset by is smaller than the
// margins involved.
setMaxMarginBeforeValues(oldTopPosMargin, oldTopNegMargin);
// In case the child discarded the before margin of the block we need to reset the mustDiscardMarginBefore flag to the initial value.
setMustDiscardMarginBefore(style()->marginBeforeCollapse() == MDISCARD);
LayoutUnit logicalTop = yPos + heightIncrease;
// After margin collapsing, one of our floats may now intrude into the child. If the child doesn't contain floats of its own it
// won't get picked up for relayout even though the logical top estimate was wrong - so add the newly intruding float now.
if (containsFloats() && child->isRenderBlock() && !toRenderBlock(child)->containsFloats() && !child->avoidsFloats() && lowestFloatLogicalBottom() > logicalTop)
toRenderBlock(child)->addIntrudingFloats(this, logicalLeftOffsetForContent(), logicalTop);
return logicalTop;
void RenderBlock::marginBeforeEstimateForChild(RenderBox* child, LayoutUnit& positiveMarginBefore, LayoutUnit& negativeMarginBefore, bool& discardMarginBefore) const
// Give up if in quirks mode and we're a body/table cell and the top margin of the child box is quirky.
// Give up if the child specified -webkit-margin-collapse: separate that prevents collapsing.
// FIXME: Use writing mode independent accessor for marginBeforeCollapse.
if ((document()->inQuirksMode() && hasMarginAfterQuirk(child) && (isTableCell() || isBody())) || child->style()->marginBeforeCollapse() == MSEPARATE)
// The margins are discarded by a child that specified -webkit-margin-collapse: discard.
// FIXME: Use writing mode independent accessor for marginBeforeCollapse.
if (child->style()->marginBeforeCollapse() == MDISCARD) {
positiveMarginBefore = 0;
negativeMarginBefore = 0;
discardMarginBefore = true;
LayoutUnit beforeChildMargin = marginBeforeForChild(child);
positiveMarginBefore = max(positiveMarginBefore, beforeChildMargin);
negativeMarginBefore = max(negativeMarginBefore, -beforeChildMargin);
if (!child->isRenderBlock())
RenderBlock* childBlock = toRenderBlock(child);
if (childBlock->childrenInline() || childBlock->isWritingModeRoot())
MarginInfo childMarginInfo(childBlock, childBlock->borderBefore() + childBlock->paddingBefore(), childBlock->borderAfter() + childBlock->paddingAfter());
if (!childMarginInfo.canCollapseMarginBeforeWithChildren())
RenderBox* grandchildBox = childBlock->firstChildBox();
for ( ; grandchildBox; grandchildBox = grandchildBox->nextSiblingBox()) {
if (!grandchildBox->isFloatingOrOutOfFlowPositioned())
// Give up if there is clearance on the box, since it probably won't collapse into us.
if (!grandchildBox || grandchildBox->style()->clear() != CNONE)
// Make sure to update the block margins now for the grandchild box so that we're looking at current values.
if (grandchildBox->needsLayout()) {
if (grandchildBox->isRenderBlock()) {
RenderBlock* grandchildBlock = toRenderBlock(grandchildBox);
// Collapse the margin of the grandchild box with our own to produce an estimate.
childBlock->marginBeforeEstimateForChild(grandchildBox, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
LayoutUnit RenderBlock::estimateLogicalTopPosition(RenderBox* child, const MarginInfo& marginInfo, LayoutUnit& estimateWithoutPagination)
// FIXME: We need to eliminate the estimation of vertical position, because when it's wrong we sometimes trigger a pathological
// relayout if there are intruding floats.
LayoutUnit logicalTopEstimate = logicalHeight();
if (!marginInfo.canCollapseWithMarginBefore()) {
LayoutUnit positiveMarginBefore = 0;
LayoutUnit negativeMarginBefore = 0;
bool discardMarginBefore = false;
if (child->selfNeedsLayout()) {
// Try to do a basic estimation of how the collapse is going to go.
marginBeforeEstimateForChild(child, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
} else {
// Use the cached collapsed margin values from a previous layout. Most of the time they
// will be right.
MarginValues marginValues = marginValuesForChild(child);
positiveMarginBefore = max(positiveMarginBefore, marginValues.positiveMarginBefore());
negativeMarginBefore = max(negativeMarginBefore, marginValues.negativeMarginBefore());
discardMarginBefore = mustDiscardMarginBeforeForChild(child);
// Collapse the result with our current margins.
if (!discardMarginBefore)
logicalTopEstimate += max(marginInfo.positiveMargin(), positiveMarginBefore) - max(marginInfo.negativeMargin(), negativeMarginBefore);
// Adjust logicalTopEstimate down to the next page if the margins are so large that we don't fit on the current
// page.
LayoutState* layoutState = view()->layoutState();
if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTopEstimate > logicalHeight()
&& hasNextPage(logicalHeight()))
logicalTopEstimate = min(logicalTopEstimate, nextPageLogicalTop(logicalHeight()));
logicalTopEstimate += getClearDelta(child, logicalTopEstimate);
estimateWithoutPagination = logicalTopEstimate;
if (layoutState->isPaginated()) {
// If the object has a page or column break value of "before", then we should shift to the top of the next page.
logicalTopEstimate = applyBeforeBreak(child, logicalTopEstimate);
// For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
logicalTopEstimate = adjustForUnsplittableChild(child, logicalTopEstimate);
if (!child->selfNeedsLayout() && child->isRenderBlock())
logicalTopEstimate += toRenderBlock(child)->paginationStrut();
return logicalTopEstimate;
LayoutUnit RenderBlock::computeStartPositionDeltaForChildAvoidingFloats(const RenderBox* child, LayoutUnit childMarginStart,
RenderRegion* region, LayoutUnit offsetFromLogicalTopOfFirstPage)
LayoutUnit startPosition = startOffsetForContent(region, offsetFromLogicalTopOfFirstPage);
// Add in our start margin.
LayoutUnit oldPosition = startPosition + childMarginStart;
LayoutUnit newPosition = oldPosition;
LayoutUnit blockOffset = logicalTopForChild(child);
if (region)
blockOffset = max(blockOffset, blockOffset + (region->logicalTopForFlowThreadContent() - offsetFromLogicalTopOfFirstPage));
LayoutUnit startOff = startOffsetForLine(blockOffset, false, region, offsetFromLogicalTopOfFirstPage, logicalHeightForChild(child));
if (style()->textAlign() != WEBKIT_CENTER && !child->style()->marginStartUsing(style()).isAuto()) {
if (childMarginStart < 0)
startOff += childMarginStart;
newPosition = max(newPosition, startOff); // Let the float sit in the child's margin if it can fit.
} else if (startOff != startPosition)
newPosition = startOff + childMarginStart;
return newPosition - oldPosition;
void RenderBlock::determineLogicalLeftPositionForChild(RenderBox* child, ApplyLayoutDeltaMode applyDelta)
LayoutUnit startPosition = borderStart() + paddingStart();
if (style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
startPosition -= verticalScrollbarWidth();
LayoutUnit totalAvailableLogicalWidth = borderAndPaddingLogicalWidth() + availableLogicalWidth();
// Add in our start margin.
LayoutUnit childMarginStart = marginStartForChild(child);
LayoutUnit newPosition = startPosition + childMarginStart;
// Some objects (e.g., tables, horizontal rules, overflow:auto blocks) avoid floats. They need
// to shift over as necessary to dodge any floats that might get in the way.
if (child->avoidsFloats() && containsFloats() && !flowThreadContainingBlock())
newPosition += computeStartPositionDeltaForChildAvoidingFloats(child, marginStartForChild(child));
setLogicalLeftForChild(child, style()->isLeftToRightDirection() ? newPosition : totalAvailableLogicalWidth - newPosition - logicalWidthForChild(child), applyDelta);
void RenderBlock::setCollapsedBottomMargin(const MarginInfo& marginInfo)
if (marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()) {
// Update the after side margin of the container to discard if the after margin of the last child also discards and we collapse with it.
// Don't update the max margin values because we won't need them anyway.
if (marginInfo.discardMargin()) {
// Update our max pos/neg bottom margins, since we collapsed our bottom margins
// with our children.
setMaxMarginAfterValues(max(maxPositiveMarginAfter(), marginInfo.positiveMargin()), max(maxNegativeMarginAfter(), marginInfo.negativeMargin()));
if (!marginInfo.hasMarginAfterQuirk())
if (marginInfo.hasMarginAfterQuirk() && !marginAfter())
// We have no bottom margin and our last child has a quirky margin.
// We will pick up this quirky margin and pass it through.
// This deals with the <td><div><p> case.
void RenderBlock::handleAfterSideOfBlock(LayoutUnit beforeSide, LayoutUnit afterSide, MarginInfo& marginInfo)
// If we can't collapse with children then go ahead and add in the bottom margin.
if (!marginInfo.discardMargin() && (!marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()
&& (!document()->inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginAfterQuirk())))
setLogicalHeight(logicalHeight() + marginInfo.margin());
// Now add in our bottom border/padding.
setLogicalHeight(logicalHeight() + afterSide);
// Negative margins can cause our height to shrink below our minimal height (border/padding).
// If this happens, ensure that the computed height is increased to the minimal height.
setLogicalHeight(max(logicalHeight(), beforeSide + afterSide));
// Update our bottom collapsed margin info.
void RenderBlock::setLogicalLeftForChild(RenderBox* child, LayoutUnit logicalLeft, ApplyLayoutDeltaMode applyDelta)
if (isHorizontalWritingMode()) {
if (applyDelta == ApplyLayoutDelta)
view()->addLayoutDelta(LayoutSize(child->x() - logicalLeft, 0));
} else {
if (applyDelta == ApplyLayoutDelta)
view()->addLayoutDelta(LayoutSize(0, child->y() - logicalLeft));
void RenderBlock::setLogicalTopForChild(RenderBox* child, LayoutUnit logicalTop, ApplyLayoutDeltaMode applyDelta)
if (isHorizontalWritingMode()) {
if (applyDelta == ApplyLayoutDelta)
view()->addLayoutDelta(LayoutSize(0, child->y() - logicalTop));
} else {
if (applyDelta == ApplyLayoutDelta)
view()->addLayoutDelta(LayoutSize(child->x() - logicalTop, 0));
void RenderBlock::updateBlockChildDirtyBitsBeforeLayout(bool relayoutChildren, RenderBox* child)
// FIXME: Technically percentage height objects only need a relayout if their percentage isn't going to be turned into
// an auto value. Add a method to determine this, so that we can avoid the relayout.
if (relayoutChildren || (child->hasRelativeLogicalHeight() && !isRenderView()) || child->hasViewportPercentageLogicalHeight())
child->setChildNeedsLayout(true, MarkOnlyThis);
// If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
if (relayoutChildren && child->needsPreferredWidthsRecalculation())
child->setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
void RenderBlock::layoutBlockChildren(bool relayoutChildren, LayoutUnit& maxFloatLogicalBottom)
if (gPercentHeightDescendantsMap) {
if (TrackedRendererListHashSet* descendants = gPercentHeightDescendantsMap->get(this)) {
TrackedRendererListHashSet::iterator end = descendants->end();
for (TrackedRendererListHashSet::iterator it = descendants->begin(); it != end; ++it) {
RenderBox* box = *it;
while (box != this) {
if (box->normalChildNeedsLayout())
box->setChildNeedsLayout(true, MarkOnlyThis);
box = box->containingBlock();
if (!box)
LayoutUnit beforeEdge = borderBefore() + paddingBefore();
LayoutUnit afterEdge = borderAfter() + paddingAfter() + scrollbarLogicalHeight();
// Lay out our hypothetical grid line as though it occurs at the top of the block.
if (view()->layoutState()->lineGrid() == this)
// The margin struct caches all our current margin collapsing state. The compact struct caches state when we encounter compacts,
MarginInfo marginInfo(this, beforeEdge, afterEdge);
// Fieldsets need to find their legend and position it inside the border of the object.
// The legend then gets skipped during normal layout. The same is true for ruby text.
// It doesn't get included in the normal layout process but is instead skipped.
RenderObject* childToExclude = layoutSpecialExcludedChild(relayoutChildren);
LayoutUnit previousFloatLogicalBottom = 0;
maxFloatLogicalBottom = 0;
RenderBox* next = firstChildBox();
while (next) {
RenderBox* child = next;
next = child->nextSiblingBox();
if (childToExclude == child)
continue; // Skip this child, since it will be positioned by the specialized subclass (fieldsets and ruby runs).
updateBlockChildDirtyBitsBeforeLayout(relayoutChildren, child);
if (child->isOutOfFlowPositioned()) {
adjustPositionedBlock(child, marginInfo);
if (child->isFloating()) {
// Lay out the child.
layoutBlockChild(child, marginInfo, previousFloatLogicalBottom, maxFloatLogicalBottom);
// Now do the handling of the bottom of the block, adding in our bottom border/padding and
// determining the correct collapsed bottom margin information.
handleAfterSideOfBlock(beforeEdge, afterEdge, marginInfo);
void RenderBlock::layoutBlockChild(RenderBox* child, MarginInfo& marginInfo, LayoutUnit& previousFloatLogicalBottom, LayoutUnit& maxFloatLogicalBottom)
LayoutUnit oldPosMarginBefore = maxPositiveMarginBefore();
LayoutUnit oldNegMarginBefore = maxNegativeMarginBefore();
// The child is a normal flow object. Compute the margins we will use for collapsing now.
// Try to guess our correct logical top position. In most cases this guess will
// be correct. Only if we're wrong (when we compute the real logical top position)
// will we have to potentially relayout.
LayoutUnit estimateWithoutPagination;
LayoutUnit logicalTopEstimate = estimateLogicalTopPosition(child, marginInfo, estimateWithoutPagination);
// Cache our old rect so that we can dirty the proper repaint rects if the child moves.