#include "config.h"
#include "RenderBlock.h"
#include "Document.h"
#include "Element.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HTMLNames.h"
#include "HitTestResult.h"
#include "InlineTextBox.h"
#include "RenderImage.h"
#include "RenderInline.h"
#include "RenderMarquee.h"
#include "RenderReplica.h"
#include "RenderTableCell.h"
#include "RenderTextFragment.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "SelectionController.h"
#include <wtf/StdLibExtras.h>
using namespace std;
using namespace WTF;
using namespace Unicode;
namespace WebCore {
const int verticalLineClickFudgeFactor= 3;
using namespace HTMLNames;
struct ColumnInfo {
ColumnInfo()
: m_desiredColumnWidth(0)
, m_desiredColumnCount(1)
{ }
int m_desiredColumnWidth;
unsigned m_desiredColumnCount;
Vector<IntRect> m_columnRects;
};
typedef WTF::HashMap<const RenderBox*, ColumnInfo*> ColumnInfoMap;
static ColumnInfoMap* gColumnInfoMap = 0;
typedef WTF::HashMap<const RenderBlock*, HashSet<RenderBox*>*> PercentHeightDescendantsMap;
static PercentHeightDescendantsMap* gPercentHeightDescendantsMap = 0;
typedef WTF::HashMap<const RenderBox*, HashSet<RenderBlock*>*> PercentHeightContainerMap;
static PercentHeightContainerMap* gPercentHeightContainerMap = 0;
typedef WTF::HashMap<RenderBlock*, ListHashSet<RenderFlow*>*> ContinuationOutlineTableMap;
RenderBlock::MarginInfo::MarginInfo(RenderBlock* block, int top, int bottom)
{
m_canCollapseWithChildren = !block->isRenderView() && !block->isRoot() && !block->isPositioned() &&
!block->isFloating() && !block->isTableCell() && !block->hasOverflowClip() && !block->isInlineBlockOrInlineTable();
m_canCollapseTopWithChildren = m_canCollapseWithChildren && (top == 0) && block->style()->marginTopCollapse() != MSEPARATE;
m_canCollapseBottomWithChildren = m_canCollapseWithChildren && (bottom == 0) &&
(block->style()->height().isAuto() && block->style()->height().value() == 0) && block->style()->marginBottomCollapse() != MSEPARATE;
m_quirkContainer = block->isTableCell() || block->isBody() || block->style()->marginTopCollapse() == MDISCARD ||
block->style()->marginBottomCollapse() == MDISCARD;
m_atTopOfBlock = true;
m_atBottomOfBlock = false;
m_posMargin = m_canCollapseTopWithChildren ? block->maxTopMargin(true) : 0;
m_negMargin = m_canCollapseTopWithChildren ? block->maxTopMargin(false) : 0;
m_selfCollapsingBlockClearedFloat = false;
m_topQuirk = m_bottomQuirk = m_determinedTopQuirk = false;
}
RenderBlock::RenderBlock(Node* node)
: RenderFlow(node)
, m_floatingObjects(0)
, m_positionedObjects(0)
, m_maxMargin(0)
, m_overflowHeight(0)
, m_overflowWidth(0)
, m_overflowLeft(0)
, m_overflowTop(0)
, m_widthForTextAutosizing(-1)
{
}
RenderBlock::~RenderBlock()
{
delete m_floatingObjects;
delete m_positionedObjects;
delete m_maxMargin;
if (m_hasColumns)
delete gColumnInfoMap->take(this);
if (gPercentHeightDescendantsMap) {
if (HashSet<RenderBox*>* descendantSet = gPercentHeightDescendantsMap->take(this)) {
HashSet<RenderBox*>::iterator end = descendantSet->end();
for (HashSet<RenderBox*>::iterator descendant = descendantSet->begin(); descendant != end; ++descendant) {
HashSet<RenderBlock*>* containerSet = gPercentHeightContainerMap->get(*descendant);
ASSERT(containerSet);
if (!containerSet)
continue;
ASSERT(containerSet->contains(this));
containerSet->remove(this);
if (containerSet->isEmpty()) {
gPercentHeightContainerMap->remove(*descendant);
delete containerSet;
}
}
delete descendantSet;
}
}
}
void RenderBlock::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
{
setReplaced(newStyle->isDisplayReplacedType());
if (style() && parent() && diff == StyleDifferenceLayout && style()->position() != newStyle->position()) {
if (newStyle->position() == StaticPosition)
removePositionedObjects(0);
else if (style()->position() == StaticPosition) {
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();
break;
}
cb = cb->parent();
}
if (cb->isRenderBlock())
static_cast<RenderBlock*>(cb)->removePositionedObjects(this);
}
}
RenderFlow::styleWillChange(diff, newStyle);
}
void RenderBlock::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderFlow::styleDidChange(diff, oldStyle);
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->isAnonymousBlock()) {
RefPtr<RenderStyle> newStyle = RenderStyle::create();
newStyle->inheritFrom(style());
newStyle->setDisplay(BLOCK);
child->setStyle(newStyle.release());
}
}
m_lineHeight = -1;
if (!isAnonymous() && document()->usesBeforeAfterRules() && canHaveChildren()) {
updateBeforeAfterContent(RenderStyle::BEFORE);
updateBeforeAfterContent(RenderStyle::AFTER);
}
updateFirstLetter();
}
void RenderBlock::addChildToFlow(RenderObject* newChild, RenderObject* beforeChild)
{
if (!beforeChild && isAfterContent(lastChild()))
beforeChild = lastChild();
bool madeBoxesNonInline = false;
if (beforeChild && beforeChild->parent() != this) {
RenderObject* anonymousChild = beforeChild->parent();
ASSERT(anonymousChild);
while (anonymousChild->parent() != this)
anonymousChild = anonymousChild->parent();
ASSERT(anonymousChild->isAnonymous());
if (anonymousChild->isAnonymousBlock()) {
if (newChild->isInline() || beforeChild->parent()->firstChild() != beforeChild)
beforeChild->parent()->addChild(newChild, beforeChild);
else
addChildToFlow(newChild, beforeChild->parent());
return;
}
ASSERT(anonymousChild->isTable());
if (newChild->isTableCol() && newChild->style()->display() == TABLE_COLUMN_GROUP
|| newChild->isRenderBlock() && newChild->style()->display() == TABLE_CAPTION
|| newChild->isTableSection()
|| newChild->isTableRow()
|| newChild->isTableCell()) {
anonymousChild->addChild(newChild, beforeChild);
return;
}
beforeChild = anonymousChild;
}
if (m_childrenInline && !newChild->isInline() && !newChild->isFloatingOrPositioned()) {
makeChildrenNonInline(beforeChild);
madeBoxesNonInline = true;
if (beforeChild && beforeChild->parent() != this) {
beforeChild = beforeChild->parent();
ASSERT(beforeChild->isAnonymousBlock());
ASSERT(beforeChild->parent() == this);
}
} else if (!m_childrenInline && (newChild->isFloatingOrPositioned() || newChild->isInline())) {
RenderObject* afterChild = beforeChild ? beforeChild->previousSibling() : lastChild();
if (afterChild && afterChild->isAnonymousBlock()) {
afterChild->addChild(newChild);
return;
}
if (newChild->isInline()) {
RenderBlock* newBox = createAnonymousBlock();
RenderContainer::addChild(newBox, beforeChild);
newBox->addChild(newChild);
return;
}
}
RenderContainer::addChild(newChild, beforeChild);
if (madeBoxesNonInline && parent() && isAnonymousBlock())
parent()->removeLeftoverAnonymousBlock(this);
}
static void getInlineRun(RenderObject* start, RenderObject* boundary,
RenderObject*& inlineRunStart,
RenderObject*& inlineRunEnd)
{
RenderObject * curr = start;
bool sawInline;
do {
while (curr && !(curr->isInline() || curr->isFloatingOrPositioned()))
curr = curr->nextSibling();
inlineRunStart = inlineRunEnd = curr;
if (!curr)
return;
sawInline = curr->isInline();
curr = curr->nextSibling();
while (curr && (curr->isInline() || curr->isFloatingOrPositioned()) && (curr != boundary)) {
inlineRunEnd = curr;
if (curr->isInline())
sawInline = true;
curr = curr->nextSibling();
}
} while (!sawInline);
}
void RenderBlock::deleteLineBoxTree()
{
InlineFlowBox* line = m_firstLineBox;
InlineFlowBox* nextLine;
while (line) {
nextLine = line->nextFlowBox();
line->deleteLine(renderArena());
line = nextLine;
}
m_firstLineBox = m_lastLineBox = 0;
}
void RenderBlock::makeChildrenNonInline(RenderObject *insertionPoint)
{
ASSERT(isInlineBlockOrInlineTable() || !isInline());
ASSERT(!insertionPoint || insertionPoint->parent() == this);
m_childrenInline = false;
RenderObject *child = firstChild();
if (!child)
return;
deleteLineBoxTree();
while (child) {
RenderObject *inlineRunStart, *inlineRunEnd;
getInlineRun(child, insertionPoint, inlineRunStart, inlineRunEnd);
if (!inlineRunStart)
break;
child = inlineRunEnd->nextSibling();
RenderBlock* box = createAnonymousBlock();
insertChildNode(box, inlineRunStart);
RenderObject* o = inlineRunStart;
while(o != inlineRunEnd)
{
RenderObject* no = o;
o = no->nextSibling();
box->moveChildNode(no);
}
box->moveChildNode(inlineRunEnd);
}
#ifndef NDEBUG
for (RenderObject *c = firstChild(); c; c = c->nextSibling())
ASSERT(!c->isInline());
#endif
repaint();
}
void RenderBlock::removeChild(RenderObject *oldChild)
{
RenderObject* prev = oldChild->previousSibling();
RenderObject* next = oldChild->nextSibling();
bool canDeleteAnonymousBlocks = !documentBeingDestroyed() && !isInline() && !oldChild->isInline() &&
!oldChild->virtualContinuation() &&
(!prev || (prev->isAnonymousBlock() && prev->childrenInline())) &&
(!next || (next->isAnonymousBlock() && next->childrenInline()));
if (canDeleteAnonymousBlocks && prev && next) {
prev->setNeedsLayoutAndPrefWidthsRecalc();
RenderObject* o = next->firstChild();
while (o) {
RenderObject* no = o;
o = no->nextSibling();
prev->moveChildNode(no);
}
RenderBlock* nextBlock = static_cast<RenderBlock*>(next);
nextBlock->deleteLineBoxTree();
next->destroy();
}
RenderFlow::removeChild(oldChild);
RenderObject* child = prev ? prev : next;
if (canDeleteAnonymousBlocks && child && !child->previousSibling() && !child->nextSibling() && !isFlexibleBox()) {
setNeedsLayoutAndPrefWidthsRecalc();
RenderBlock* anonBlock = static_cast<RenderBlock*>(removeChildNode(child, false));
m_childrenInline = true;
RenderObject* o = anonBlock->firstChild();
while (o) {
RenderObject* no = o;
o = no->nextSibling();
moveChildNode(no);
}
anonBlock->deleteLineBoxTree();
anonBlock->destroy();
}
}
int RenderBlock::overflowHeight(bool includeInterior) const
{
if (!includeInterior && hasOverflowClip()) {
int shadowHeight = 0;
for (ShadowData* boxShadow = style()->boxShadow(); boxShadow; boxShadow = boxShadow->next)
shadowHeight = max(boxShadow->y + boxShadow->blur, shadowHeight);
int inflatedHeight = height() + shadowHeight;
if (hasReflection())
inflatedHeight = max(inflatedHeight, reflectionBox().bottom());
return inflatedHeight;
}
return m_overflowHeight;
}
int RenderBlock::overflowWidth(bool includeInterior) const
{
if (!includeInterior && hasOverflowClip()) {
int shadowWidth = 0;
for (ShadowData* boxShadow = style()->boxShadow(); boxShadow; boxShadow = boxShadow->next)
shadowWidth = max(boxShadow->x + boxShadow->blur, shadowWidth);
int inflatedWidth = width() + shadowWidth;
if (hasReflection())
inflatedWidth = max(inflatedWidth, reflectionBox().right());
return inflatedWidth;
}
return m_overflowWidth;
}
int RenderBlock::overflowLeft(bool includeInterior) const
{
if (!includeInterior && hasOverflowClip()) {
int shadowLeft = 0;
for (ShadowData* boxShadow = style()->boxShadow(); boxShadow; boxShadow = boxShadow->next)
shadowLeft = min(boxShadow->x - boxShadow->blur, shadowLeft);
int left = shadowLeft;
if (hasReflection())
left = min(left, reflectionBox().x());
return left;
}
return m_overflowLeft;
}
int RenderBlock::overflowTop(bool includeInterior) const
{
if (!includeInterior && hasOverflowClip()) {
int shadowTop = 0;
for (ShadowData* boxShadow = style()->boxShadow(); boxShadow; boxShadow = boxShadow->next)
shadowTop = min(boxShadow->y - boxShadow->blur, shadowTop);
int top = shadowTop;
if (hasReflection())
top = min(top, reflectionBox().y());
return top;
}
return m_overflowTop;
}
IntRect RenderBlock::overflowRect(bool includeInterior) const
{
if (!includeInterior && hasOverflowClip()) {
IntRect box = borderBoxRect();
int shadowLeft = 0;
int shadowRight = 0;
int shadowTop = 0;
int shadowBottom = 0;
for (ShadowData* boxShadow = style()->boxShadow(); boxShadow; boxShadow = boxShadow->next) {
shadowLeft = min(boxShadow->x - boxShadow->blur, shadowLeft);
shadowRight = max(boxShadow->x + boxShadow->blur, shadowRight);
shadowTop = min(boxShadow->y - boxShadow->blur, shadowTop);
shadowBottom = max(boxShadow->y + boxShadow->blur, shadowBottom);
}
box.move(shadowLeft, shadowTop);
box.setWidth(box.width() - shadowLeft + shadowRight);
box.setHeight(box.height() - shadowTop + shadowBottom);
if (hasReflection()) {
IntRect reflection(reflectionBox());
int reflectTop = min(box.y(), reflection.y());
int reflectBottom = max(box.bottom(), reflection.bottom());
box.setHeight(reflectBottom - reflectTop);
box.setY(reflectTop);
int reflectLeft = min(box.x(), reflection.x());
int reflectRight = max(box.right(), reflection.right());
box.setWidth(reflectRight - reflectLeft);
box.setX(reflectLeft);
}
return box;
}
if (!includeInterior && hasOverflowClip())
return borderBoxRect();
int l = overflowLeft(includeInterior);
int t = overflowTop(includeInterior);
return IntRect(l, t, overflowWidth(includeInterior) - l, max(overflowHeight(includeInterior), height()) - t);
}
bool RenderBlock::isSelfCollapsingBlock() const
{
if (height() > 0 ||
isTable() || (borderBottom() + paddingBottom() + borderTop() + paddingTop()) != 0 ||
style()->minHeight().isPositive() ||
style()->marginTopCollapse() == MSEPARATE || style()->marginBottomCollapse() == MSEPARATE)
return false;
bool hasAutoHeight = style()->height().isAuto();
if (style()->height().isPercent() && !style()->htmlHacks()) {
hasAutoHeight = true;
for (RenderBlock* cb = containingBlock(); !cb->isRenderView(); cb = cb->containingBlock()) {
if (cb->style()->height().isFixed() || cb->isTableCell())
hasAutoHeight = false;
}
}
if (hasAutoHeight || ((style()->height().isFixed() || style()->height().isPercent()) && style()->height().isZero())) {
if (childrenInline())
return !firstLineBox();
for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
if (child->isFloatingOrPositioned())
continue;
if (!child->isSelfCollapsingBlock())
return false;
}
return true;
}
return false;
}
void RenderBlock::layout()
{
updateFirstLetter();
layoutBlock(false);
if (hasControlClip()) {
m_overflowWidth = width();
m_overflowHeight = height();
m_overflowLeft = 0;
m_overflowTop = 0;
}
}
void RenderBlock::layoutBlock(bool relayoutChildren)
{
ASSERT(needsLayout());
if (isInline() && !isInlineBlockOrInlineTable()) return;
if (!relayoutChildren && layoutOnlyPositionedObjects())
return;
LayoutRepainter repainter(*this, m_everHadLayout && checkForRepaintDuringLayout());
LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), m_hasColumns || hasTransform() || hasReflection());
int oldWidth = width();
int oldColumnWidth = desiredColumnWidth();
calcWidth();
calcColumnWidth();
m_overflowWidth = width();
m_overflowLeft = 0;
if (oldWidth != width() || oldColumnWidth != desiredColumnWidth())
relayoutChildren = true;
clearFloats();
int previousHeight = height();
setHeight(0);
m_overflowHeight = 0;
bool isCell = isTableCell();
if (!isCell) {
initMaxMarginValues();
m_topMarginQuirk = style()->marginTop().quirk();
m_bottomMarginQuirk = style()->marginBottom().quirk();
Node* node = element();
if (node && node->hasTagName(formTag) && static_cast<HTMLFormElement*>(node)->isMalformed()) {
setMaxBottomMargins(0, 0);
}
}
if (scrollsOverflow()) {
if (style()->overflowX() == OSCROLL)
m_layer->setHasHorizontalScrollbar(true);
if (style()->overflowY() == OSCROLL)
m_layer->setHasVerticalScrollbar(true);
}
int repaintTop = 0;
int repaintBottom = 0;
int maxFloatBottom = 0;
if (childrenInline())
layoutInlineChildren(relayoutChildren, repaintTop, repaintBottom);
else
layoutBlockChildren(relayoutChildren, maxFloatBottom);
int toAdd = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
if (floatBottom() > (height() - toAdd) && expandsToEncloseOverhangingFloats())
setHeight(floatBottom() + toAdd);
int singleColumnBottom = layoutColumns();
int oldHeight = height();
calcHeight();
if (oldHeight != height()) {
if (oldHeight > height() && maxFloatBottom > height() && !childrenInline()) {
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->isBlockFlow() && !child->isFloatingOrPositioned()) {
RenderBlock* block = static_cast<RenderBlock*>(child);
if (block->floatBottom() + block->y() > height())
addOverhangingFloats(block, -block->x(), -block->y(), false);
}
}
}
layoutColumns(singleColumnBottom);
if (m_overflowHeight > height())
m_overflowHeight -= toAdd;
if (m_overflowHeight < height())
m_overflowHeight = height();
}
if (previousHeight != height())
relayoutChildren = true;
if ((isCell || isInline() || isFloatingOrPositioned() || isRoot()) && !hasOverflowClip() && !hasControlClip())
addVisualOverflow(floatRect());
layoutPositionedObjects(relayoutChildren || isRoot());
positionListMarker();
m_overflowWidth = max(m_overflowWidth, width());
m_overflowHeight = max(m_overflowHeight, height());
if (!hasOverflowClip()) {
for (ShadowData* boxShadow = style()->boxShadow(); boxShadow; boxShadow = boxShadow->next) {
m_overflowLeft = min(m_overflowLeft, boxShadow->x - boxShadow->blur);
m_overflowWidth = max(m_overflowWidth, width() + boxShadow->x + boxShadow->blur);
m_overflowTop = min(m_overflowTop, boxShadow->y - boxShadow->blur);
m_overflowHeight = max(m_overflowHeight, height() + boxShadow->y + boxShadow->blur);
}
if (hasReflection()) {
m_overflowTop = min(m_overflowTop, reflectionBox().y());
m_overflowHeight = max(m_overflowHeight, reflectionBox().bottom());
}
}
statePusher.pop();
if (hasOverflowClip())
m_layer->updateScrollInfoAfterLayout();
bool didFullRepaint = repainter.repaintAfterLayout();
if (!didFullRepaint && repaintTop != repaintBottom && (style()->visibility() == VISIBLE || enclosingLayer()->hasVisibleContent())) {
IntRect repaintRect(m_overflowLeft, repaintTop, m_overflowWidth - m_overflowLeft, repaintBottom - repaintTop);
repaintRect.inflate(maximalOutlineSize(PaintPhaseOutline));
if (hasOverflowClip()) {
int x = repaintRect.x();
int y = repaintRect.y();
layer()->subtractScrolledContentOffset(x, y);
repaintRect.setX(x);
repaintRect.setY(y);
repaintRect.intersect(IntRect(0, 0, width(), height()));
}
if (!repaintRect.isEmpty()) {
repaintRectangle(repaintRect); if (hasReflection())
repaintRectangle(reflectedRect(repaintRect));
}
}
setNeedsLayout(false);
}
bool RenderBlock::expandsToEncloseOverhangingFloats() const
{
return isInlineBlockOrInlineTable() || isFloatingOrPositioned() || hasOverflowClip() || (parent() && parent()->isFlexibleBox()) || m_hasColumns || isTableCell() || isFieldset();
}
void RenderBlock::adjustPositionedBlock(RenderBox* child, const MarginInfo& marginInfo)
{
if (child->hasStaticX()) {
if (style()->direction() == LTR)
child->setStaticX(borderLeft() + paddingLeft());
else
child->setStaticX(borderRight() + paddingRight());
}
if (child->hasStaticY()) {
int y = height();
if (!marginInfo.canCollapseWithTop()) {
child->calcVerticalMargins();
int marginTop = child->marginTop();
int collapsedTopPos = marginInfo.posMargin();
int collapsedTopNeg = marginInfo.negMargin();
if (marginTop > 0) {
if (marginTop > collapsedTopPos)
collapsedTopPos = marginTop;
} else {
if (-marginTop > collapsedTopNeg)
collapsedTopNeg = -marginTop;
}
y += (collapsedTopPos - collapsedTopNeg) - marginTop;
}
child->setStaticY(y);
}
}
void RenderBlock::adjustFloatingBlock(const MarginInfo& marginInfo)
{
int marginOffset = marginInfo.canCollapseWithTop() ? 0 : marginInfo.margin();
setHeight(height() + marginOffset);
positionNewFloats();
setHeight(height() - marginOffset);
}
RenderBox* RenderBlock::handleSpecialChild(RenderBox* child, const MarginInfo& marginInfo, bool& handled)
{
RenderBox* next = handlePositionedChild(child, marginInfo, handled);
if (handled) return next;
next = handleFloatingChild(child, marginInfo, handled);
if (handled) return next;
return handleRunInChild(child, handled);
}
RenderBox* RenderBlock::handlePositionedChild(RenderBox* child, const MarginInfo& marginInfo, bool& handled)
{
if (child->isPositioned()) {
handled = true;
child->containingBlock()->insertPositionedObject(child);
adjustPositionedBlock(child, marginInfo);
return child->nextSiblingBox();
}
return 0;
}
RenderBox* RenderBlock::handleFloatingChild(RenderBox* child, const MarginInfo& marginInfo, bool& handled)
{
if (child->isFloating()) {
handled = true;
insertFloatingObject(child);
adjustFloatingBlock(marginInfo);
return child->nextSiblingBox();
}
return 0;
}
RenderBox* RenderBlock::handleRunInChild(RenderBox* blockRunIn, bool& handled)
{
if (blockRunIn->isRunIn() && (blockRunIn->childrenInline() || blockRunIn->isReplaced())) {
RenderObject* curr = blockRunIn->nextSibling();
while (curr && curr->isFloatingOrPositioned())
curr = curr->nextSibling();
if (curr && (curr->isRenderBlock() && curr->childrenInline() && !curr->isRunIn())) {
handled = true;
RenderBox* next = blockRunIn->nextSiblingBox();
removeChildNode(blockRunIn);
RenderInline* inlineRunIn = new (renderArena()) RenderInline(blockRunIn->node());
inlineRunIn->setStyle(blockRunIn->style());
for (RenderObject* runInChild = blockRunIn->firstChild(); runInChild; runInChild = runInChild->nextSibling()) {
if (runInChild->style()->styleType() != RenderStyle::BEFORE && runInChild->style()->styleType() != RenderStyle::AFTER) {
blockRunIn->removeChildNode(runInChild, false);
inlineRunIn->addChild(runInChild); }
}
curr->insertChildNode(inlineRunIn, curr->firstChild());
if (blockRunIn->element())
blockRunIn->element()->setRenderer(inlineRunIn);
blockRunIn->destroy();
return next;
}
}
return 0;
}
void RenderBlock::collapseMargins(RenderBox* child, MarginInfo& marginInfo, int yPosEstimate)
{
int posTop = child->maxTopMargin(true);
int negTop = child->maxTopMargin(false);
if (child->isSelfCollapsingBlock()) {
posTop = max(posTop, child->maxBottomMargin(true));
negTop = max(negTop, child->maxBottomMargin(false));
}
bool topQuirk = child->isTopMarginQuirk() || style()->marginTopCollapse() == MDISCARD;
if (marginInfo.canCollapseWithTop()) {
if (!style()->htmlHacks() || !marginInfo.quirkContainer() || !topQuirk)
setMaxTopMargins(max(posTop, maxTopPosMargin()), max(negTop, maxTopNegMargin()));
if (!marginInfo.determinedTopQuirk() && !topQuirk && (posTop-negTop)) {
m_topMarginQuirk = false;
marginInfo.setDeterminedTopQuirk(true);
}
if (!marginInfo.determinedTopQuirk() && topQuirk && marginTop() == 0)
m_topMarginQuirk = true;
}
if (marginInfo.quirkContainer() && marginInfo.atTopOfBlock() && (posTop - negTop))
marginInfo.setTopQuirk(topQuirk);
int ypos = height();
if (child->isSelfCollapsingBlock()) {
int collapsedTopPos = max(marginInfo.posMargin(), child->maxTopMargin(true));
int collapsedTopNeg = max(marginInfo.negMargin(), child->maxTopMargin(false));
marginInfo.setMargin(collapsedTopPos, collapsedTopNeg);
marginInfo.setPosMarginIfLarger(child->maxBottomMargin(true));
marginInfo.setNegMarginIfLarger(child->maxBottomMargin(false));
if (!marginInfo.canCollapseWithTop())
ypos = height() + collapsedTopPos - collapsedTopNeg;
}
else {
if (child->style()->marginTopCollapse() == MSEPARATE) {
setHeight(height() + marginInfo.margin() + child->marginTop());
ypos = height();
}
else if (!marginInfo.atTopOfBlock() ||
(!marginInfo.canCollapseTopWithChildren()
&& (!style()->htmlHacks() || !marginInfo.quirkContainer() || !marginInfo.topQuirk()))) {
setHeight(height() + max(marginInfo.posMargin(), posTop) - max(marginInfo.negMargin(), negTop));
ypos = height();
}
marginInfo.setPosMargin(child->maxBottomMargin(true));
marginInfo.setNegMargin(child->maxBottomMargin(false));
if (marginInfo.margin())
marginInfo.setBottomQuirk(child->isBottomMarginQuirk() || style()->marginBottomCollapse() == MDISCARD);
marginInfo.setSelfCollapsingBlockClearedFloat(false);
}
view()->addLayoutDelta(IntSize(0, yPosEstimate - ypos));
child->setLocation(child->x(), ypos);
if (ypos != yPosEstimate) {
if (child->shrinkToAvoidFloats())
child->setChildNeedsLayout(true, false);
if (!child->avoidsFloats() && child->containsFloats())
static_cast<RenderBlock*>(child)->markAllDescendantsWithFloatsForLayout();
child->layoutIfNeeded();
}
}
void RenderBlock::clearFloatsIfNeeded(RenderBox* child, MarginInfo& marginInfo, int oldTopPosMargin, int oldTopNegMargin)
{
int heightIncrease = getClearDelta(child);
if (!heightIncrease)
return;
view()->addLayoutDelta(IntSize(0, -heightIncrease));
child->setLocation(child->x(), child->y() + heightIncrease);
if (child->isSelfCollapsingBlock()) {
marginInfo.setPosMargin(max(child->maxTopMargin(true), child->maxBottomMargin(true)));
marginInfo.setNegMargin(max(child->maxTopMargin(false), child->maxBottomMargin(false)));
setHeight(child->y() - max(0, marginInfo.margin()));
marginInfo.setSelfCollapsingBlockClearedFloat(true);
} else
setHeight(height() + heightIncrease);
if (marginInfo.canCollapseWithTop()) {
setMaxTopMargins(oldTopPosMargin, oldTopNegMargin);
marginInfo.setAtTopOfBlock(false);
}
if (child->shrinkToAvoidFloats())
child->setChildNeedsLayout(true, false);
if (!child->avoidsFloats() && child->containsFloats())
static_cast<RenderBlock*>(child)->markAllDescendantsWithFloatsForLayout();
child->layoutIfNeeded();
}
int RenderBlock::estimateVerticalPosition(RenderBox* child, const MarginInfo& marginInfo)
{
int yPosEstimate = height();
if (!marginInfo.canCollapseWithTop()) {
int childMarginTop = child->selfNeedsLayout() ? child->marginTop() : child->collapsedMarginTop();
yPosEstimate += max(marginInfo.margin(), childMarginTop);
}
return yPosEstimate;
}
void RenderBlock::determineHorizontalPosition(RenderBox* child)
{
if (style()->direction() == LTR) {
int xPos = borderLeft() + paddingLeft();
int chPos = xPos + child->marginLeft();
if (child->avoidsFloats()) {
int leftOff = leftOffset(height());
if (style()->textAlign() != WEBKIT_CENTER && child->style()->marginLeft().type() != Auto) {
if (child->marginLeft() < 0)
leftOff += child->marginLeft();
chPos = max(chPos, leftOff); }
else if (leftOff != xPos) {
child->calcHorizontalMargins(child->style()->marginLeft(), child->style()->marginRight(), lineWidth(child->y()));
chPos = leftOff + child->marginLeft();
}
}
view()->addLayoutDelta(IntSize(child->x() - chPos, 0));
child->setLocation(chPos, child->y());
} else {
int xPos = width() - borderRight() - paddingRight() - verticalScrollbarWidth();
int chPos = xPos - (child->width() + child->marginRight());
if (child->avoidsFloats()) {
int rightOff = rightOffset(height());
if (style()->textAlign() != WEBKIT_CENTER && child->style()->marginRight().type() != Auto) {
if (child->marginRight() < 0)
rightOff -= child->marginRight();
chPos = min(chPos, rightOff - child->width()); } else if (rightOff != xPos) {
toRenderBox(child)->calcHorizontalMargins(child->style()->marginLeft(), child->style()->marginRight(), lineWidth(child->y()));
chPos = rightOff - child->marginRight() - child->width();
}
}
view()->addLayoutDelta(IntSize(child->x() - chPos, 0));
child->setLocation(chPos, child->y());
}
}
void RenderBlock::setCollapsedBottomMargin(const MarginInfo& marginInfo)
{
if (marginInfo.canCollapseWithBottom() && !marginInfo.canCollapseWithTop()) {
setMaxBottomMargins(max(maxBottomPosMargin(), marginInfo.posMargin()), max(maxBottomNegMargin(), marginInfo.negMargin()));
if (!marginInfo.bottomQuirk())
m_bottomMarginQuirk = false;
if (marginInfo.bottomQuirk() && marginBottom() == 0)
m_bottomMarginQuirk = true;
}
}
void RenderBlock::handleBottomOfBlock(int top, int bottom, MarginInfo& marginInfo)
{
if (!marginInfo.selfCollapsingBlockClearedFloat())
marginInfo.setAtBottomOfBlock(true);
else {
if (marginInfo.margin() < 0)
marginInfo.clearMargin();
}
if (!marginInfo.canCollapseWithBottom() && !marginInfo.canCollapseWithTop()
&& (!style()->htmlHacks() || !marginInfo.quirkContainer() || !marginInfo.bottomQuirk()))
setHeight(height() + marginInfo.margin());
setHeight(height() + bottom);
setHeight(max(height(), top + bottom));
m_overflowHeight = max(height(), m_overflowHeight);
setCollapsedBottomMargin(marginInfo);
}
void RenderBlock::layoutBlockChildren(bool relayoutChildren, int& maxFloatBottom)
{
if (gPercentHeightDescendantsMap) {
if (HashSet<RenderBox*>* descendants = gPercentHeightDescendantsMap->get(this)) {
HashSet<RenderBox*>::iterator end = descendants->end();
for (HashSet<RenderBox*>::iterator it = descendants->begin(); it != end; ++it) {
RenderBox* box = *it;
while (box != this) {
if (box->normalChildNeedsLayout())
break;
box->setChildNeedsLayout(true, false);
box = box->containingBlock();
ASSERT(box);
if (!box)
break;
}
}
}
}
int top = borderTop() + paddingTop();
int bottom = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
m_overflowHeight = top;
setHeight(m_overflowHeight);
MarginInfo marginInfo(this, top, bottom);
RenderObject* legend = layoutLegend(relayoutChildren);
int previousFloatBottom = 0;
maxFloatBottom = 0;
RenderBox* child = firstChildBox();
while (child) {
if (legend == child) {
child = child->nextSiblingBox();
continue; }
int oldTopPosMargin = maxTopPosMargin();
int oldTopNegMargin = maxTopNegMargin();
if (relayoutChildren || ((child->style()->height().isPercent() || child->style()->minHeight().isPercent() || child->style()->maxHeight().isPercent()) && !isRenderView()))
child->setChildNeedsLayout(true, false);
if (relayoutChildren && (child->style()->paddingLeft().isPercent() || child->style()->paddingRight().isPercent()))
child->setPrefWidthsDirty(true, false);
bool handled = false;
RenderBox* next = handleSpecialChild(child, marginInfo, handled);
if (handled) {
child = next;
continue;
}
child->calcVerticalMargins();
if (child->style()->marginTopCollapse() == MSEPARATE) {
marginInfo.setAtTopOfBlock(false);
marginInfo.clearMargin();
}
int yPosEstimate = estimateVerticalPosition(child, marginInfo);
IntRect oldRect(child->x(), child->y() , child->width(), child->height());
#ifndef NDEBUG
IntSize oldLayoutDelta = view()->layoutDelta();
#endif
view()->addLayoutDelta(IntSize(0, child->y() - yPosEstimate));
child->setLocation(child->x(), yPosEstimate);
bool markDescendantsWithFloats = false;
if (yPosEstimate != oldRect.y() && !child->avoidsFloats() && child->containsFloats())
markDescendantsWithFloats = true;
else if (!child->avoidsFloats() || child->shrinkToAvoidFloats()) {
int fb = max(previousFloatBottom, floatBottom());
if (fb > height() || fb > yPosEstimate)
markDescendantsWithFloats = true;
}
if (markDescendantsWithFloats)
static_cast<RenderBlock*>(child)->markAllDescendantsWithFloatsForLayout();
if (child->isRenderBlock())
previousFloatBottom = max(previousFloatBottom, oldRect.y() + static_cast<RenderBlock*>(child)->floatBottom());
bool childHadLayout = child->m_everHadLayout;
bool childNeededLayout = child->needsLayout();
if (childNeededLayout)
child->layout();
collapseMargins(child, marginInfo, yPosEstimate);
clearFloatsIfNeeded(child, marginInfo, oldTopPosMargin, oldTopNegMargin);
if (marginInfo.atTopOfBlock() && !child->isSelfCollapsingBlock())
marginInfo.setAtTopOfBlock(false);
determineHorizontalPosition(child);
setHeight(height() + child->height());
if (child->style()->marginBottomCollapse() == MSEPARATE) {
setHeight(height() + child->marginBottom());
marginInfo.clearMargin();
}
maxFloatBottom = max(maxFloatBottom, addOverhangingFloats(static_cast<RenderBlock*>(child), -child->x(), -child->y(), !childNeededLayout));
m_overflowTop = min(m_overflowTop, child->y() + child->overflowTop(false));
m_overflowHeight = max(m_overflowHeight, height() + child->overflowHeight(false) - child->height());
m_overflowWidth = max(child->x() + child->overflowWidth(false), m_overflowWidth);
m_overflowLeft = min(child->x() + child->overflowLeft(false), m_overflowLeft);
IntSize childOffset(child->x() - oldRect.x(), child->y() - oldRect.y());
if (childOffset.width() || childOffset.height()) {
view()->addLayoutDelta(childOffset);
if (childHadLayout && !selfNeedsLayout() && child->checkForRepaintDuringLayout())
child->repaintDuringLayoutIfMoved(oldRect);
}
if (!childHadLayout && child->checkForRepaintDuringLayout())
child->repaint();
ASSERT(oldLayoutDelta == view()->layoutDelta());
child = child->nextSiblingBox();
}
handleBottomOfBlock(top, bottom, marginInfo);
}
bool RenderBlock::layoutOnlyPositionedObjects()
{
if (!posChildNeedsLayout() || normalChildNeedsLayout() || selfNeedsLayout())
return false;
LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), m_hasColumns || hasTransform() || hasReflection());
if (needsPositionedMovementLayout()) {
tryLayoutDoingPositionedMovementOnly();
if (needsLayout())
return false;
}
layoutPositionedObjects(false);
statePusher.pop();
if (hasOverflowClip())
m_layer->updateScrollInfoAfterLayout();
setNeedsLayout(false);
return true;
}
void RenderBlock::layoutPositionedObjects(bool relayoutChildren)
{
if (m_positionedObjects) {
RenderBox* r;
Iterator end = m_positionedObjects->end();
for (Iterator it = m_positionedObjects->begin(); it != end; ++it) {
r = *it;
if (relayoutChildren || (r->hasStaticY() && r->parent() != this && r->parent()->isBlockFlow()))
r->setChildNeedsLayout(true, false);
if (relayoutChildren && (r->style()->paddingLeft().isPercent() || r->style()->paddingRight().isPercent()))
r->setPrefWidthsDirty(true, false);
if (r->needsPositionedMovementLayoutOnly())
r->tryLayoutDoingPositionedMovementOnly();
r->layoutIfNeeded();
}
}
}
void RenderBlock::markPositionedObjectsForLayout()
{
if (m_positionedObjects) {
RenderBox* r;
Iterator end = m_positionedObjects->end();
for (Iterator it = m_positionedObjects->begin(); it != end; ++it) {
r = *it;
r->setChildNeedsLayout(true);
}
}
}
void RenderBlock::repaintOverhangingFloats(bool paintAllDescendants)
{
if (hasOverhangingFloats()) {
ASSERT(m_floatingObjects);
if (!m_floatingObjects)
return;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
view()->disableLayoutState();
for ( ; (r = it.current()); ++it) {
if (r->m_bottom > height() && (paintAllDescendants && r->m_renderer->isDescendantOf(this) || r->m_shouldPaint) && !r->m_renderer->hasSelfPaintingLayer()) {
r->m_renderer->repaint();
r->m_renderer->repaintOverhangingFloats();
}
}
view()->enableLayoutState();
}
}
void RenderBlock::paint(PaintInfo& paintInfo, int tx, int ty)
{
tx += x();
ty += y();
PaintPhase phase = paintInfo.phase;
if (!isRoot()) {
IntRect overflowBox = overflowRect(false);
overflowBox.inflate(maximalOutlineSize(paintInfo.phase));
overflowBox.move(tx, ty);
if (!overflowBox.intersects(paintInfo.rect))
return;
}
bool pushedClip = pushContentsClip(paintInfo, tx, ty);
paintObject(paintInfo, tx, ty);
if (pushedClip)
popContentsClip(paintInfo, phase, tx, ty);
if (hasOverflowClip() && style()->visibility() == VISIBLE && (phase == PaintPhaseBlockBackground || phase == PaintPhaseChildBlockBackground))
layer()->paintOverflowControls(paintInfo.context, tx, ty, paintInfo.rect);
}
void RenderBlock::paintColumns(PaintInfo& paintInfo, int tx, int ty, bool paintingFloats)
{
GraphicsContext* context = paintInfo.context;
int currXOffset = 0;
int currYOffset = 0;
int ruleAdd = borderLeft() + paddingLeft();
int ruleX = 0;
int colGap = columnGap();
const Color& ruleColor = style()->columnRuleColor();
bool ruleTransparent = style()->columnRuleIsTransparent();
EBorderStyle ruleStyle = style()->columnRuleStyle();
int ruleWidth = style()->columnRuleWidth();
bool renderRule = !paintingFloats && ruleStyle > BHIDDEN && !ruleTransparent && ruleWidth <= colGap;
Vector<IntRect>* colRects = columnRects();
unsigned colCount = colRects->size();
for (unsigned i = 0; i < colCount; i++) {
IntRect colRect = colRects->at(i);
colRect.move(tx, ty);
context->save();
context->clip(colRect);
PaintInfo info(paintInfo);
info.rect.intersect(colRect);
int finalX = tx + currXOffset;
int finalY = ty + currYOffset;
if (paintingFloats)
paintFloats(info, finalX, finalY, paintInfo.phase == PaintPhaseSelection || paintInfo.phase == PaintPhaseTextClip);
else
paintContents(info, finalX, finalY);
if (style()->direction() == LTR) {
ruleX += colRect.width() + colGap / 2;
currXOffset += colRect.width() + colGap;
} else {
ruleX -= (colRect.width() + colGap / 2);
currXOffset -= (colRect.width() + colGap);
}
currYOffset -= colRect.height();
context->restore();
if (renderRule && paintInfo.phase == PaintPhaseForeground && i < colCount - 1) {
int ruleStart = ruleX - ruleWidth / 2 + ruleAdd;
int ruleEnd = ruleStart + ruleWidth;
drawBorder(paintInfo.context, tx + ruleStart, ty + borderTop() + paddingTop(), tx + ruleEnd, ty + borderTop() + paddingTop() + contentHeight(),
style()->direction() == LTR ? BSLeft : BSRight, ruleColor, style()->color(), ruleStyle, 0, 0);
}
ruleX = currXOffset;
}
}
void RenderBlock::paintContents(PaintInfo& paintInfo, int tx, int ty)
{
if (document()->didLayoutWithPendingStylesheets() && !isRenderView())
return;
if (childrenInline())
paintLines(paintInfo, tx, ty);
else
paintChildren(paintInfo, tx, ty);
}
void RenderBlock::paintChildren(PaintInfo& paintInfo, int tx, int ty)
{
PaintPhase newPhase = (paintInfo.phase == PaintPhaseChildOutlines) ? PaintPhaseOutline : paintInfo.phase;
newPhase = (newPhase == PaintPhaseChildBlockBackgrounds) ? PaintPhaseChildBlockBackground : newPhase;
PaintInfo info(paintInfo);
info.phase = newPhase;
info.paintingRoot = paintingRootForChildren(paintInfo);
bool isPrinting = document()->printing();
for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
if (isPrinting && !childrenInline() && child->style()->pageBreakBefore() == PBALWAYS &&
inRootBlockContext() && (ty + child->y()) > paintInfo.rect.y() &&
(ty + child->y()) < paintInfo.rect.bottom()) {
view()->setBestTruncatedAt(ty + child->y(), this, true);
return;
}
if (!child->hasSelfPaintingLayer() && !child->isFloating())
child->paint(info, tx, ty);
if (isPrinting && !childrenInline() && child->style()->pageBreakAfter() == PBALWAYS &&
inRootBlockContext() && (ty + child->y() + child->height()) > paintInfo.rect.y() &&
(ty + child->y() + child->height()) < paintInfo.rect.bottom()) {
view()->setBestTruncatedAt(ty + child->y() + child->height() + max(0, child->collapsedMarginBottom()), this, true);
return;
}
}
}
void RenderBlock::paintCaret(PaintInfo& paintInfo, int tx, int ty, CaretType type)
{
SelectionController* selection = type == CursorCaret ? document()->frame()->selection() : document()->frame()->dragCaretController();
RenderObject* caretPainter = selection->caretRenderer();
if (caretPainter == this && selection->isContentEditable()) {
offsetForContents(tx, ty);
if (type == CursorCaret)
document()->frame()->paintCaret(paintInfo.context, tx, ty, paintInfo.rect);
else
document()->frame()->paintDragCaret(paintInfo.context, tx, ty, paintInfo.rect);
}
}
void RenderBlock::paintObject(PaintInfo& paintInfo, int tx, int ty)
{
PaintPhase paintPhase = paintInfo.phase;
if ((paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) &&
hasBoxDecorations() && style()->visibility() == VISIBLE) {
paintBoxDecorations(paintInfo, tx, ty);
}
if (paintPhase == PaintPhaseMask && style()->visibility() == VISIBLE) {
paintMask(paintInfo, tx, ty);
return;
}
if (paintPhase == PaintPhaseBlockBackground)
return;
int scrolledX = tx;
int scrolledY = ty;
if (hasOverflowClip())
m_layer->subtractScrolledContentOffset(scrolledX, scrolledY);
if (paintPhase != PaintPhaseSelfOutline) {
if (m_hasColumns)
paintColumns(paintInfo, scrolledX, scrolledY);
else
paintContents(paintInfo, scrolledX, scrolledY);
}
bool isPrinting = document()->printing();
if (!isPrinting && !m_hasColumns)
paintSelection(paintInfo, scrolledX, scrolledY);
if (paintPhase == PaintPhaseFloat || paintPhase == PaintPhaseSelection || paintPhase == PaintPhaseTextClip) {
if (m_hasColumns)
paintColumns(paintInfo, scrolledX, scrolledY, true);
else
paintFloats(paintInfo, scrolledX, scrolledY, paintPhase == PaintPhaseSelection || paintPhase == PaintPhaseTextClip);
}
if ((paintPhase == PaintPhaseOutline || paintPhase == PaintPhaseSelfOutline) && hasOutline() && style()->visibility() == VISIBLE)
RenderBox::paintOutline(paintInfo.context, tx, ty, width(), height(), style());
if ((paintPhase == PaintPhaseOutline || paintPhase == PaintPhaseChildOutlines)) {
if (continuation() && continuation()->hasOutline() && continuation()->style()->visibility() == VISIBLE) {
RenderFlow* inlineFlow = static_cast<RenderFlow*>(continuation()->element()->renderer());
if (!inlineFlow->hasLayer())
containingBlock()->addContinuationWithOutline(inlineFlow);
else if (!inlineFlow->firstLineBox())
inlineFlow->paintOutline(paintInfo.context, tx - x() + inlineFlow->containingBlock()->x(),
ty - y() + inlineFlow->containingBlock()->y());
}
paintContinuationOutlines(paintInfo, tx, ty);
}
if (paintPhase == PaintPhaseForeground) {
paintCaret(paintInfo, scrolledX, scrolledY, CursorCaret);
paintCaret(paintInfo, scrolledX, scrolledY, DragCaret);
}
}
void RenderBlock::paintFloats(PaintInfo& paintInfo, int tx, int ty, bool preservePhase)
{
if (!m_floatingObjects)
return;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for (; (r = it.current()); ++it) {
if (r->m_shouldPaint && !r->m_renderer->hasSelfPaintingLayer()) {
PaintInfo currentPaintInfo(paintInfo);
currentPaintInfo.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
int currentTX = tx + r->m_left - r->m_renderer->x() + r->m_renderer->marginLeft();
int currentTY = ty + r->m_top - r->m_renderer->y() + r->m_renderer->marginTop();
r->m_renderer->paint(currentPaintInfo, currentTX, currentTY);
if (!preservePhase) {
currentPaintInfo.phase = PaintPhaseChildBlockBackgrounds;
r->m_renderer->paint(currentPaintInfo, currentTX, currentTY);
currentPaintInfo.phase = PaintPhaseFloat;
r->m_renderer->paint(currentPaintInfo, currentTX, currentTY);
currentPaintInfo.phase = PaintPhaseForeground;
r->m_renderer->paint(currentPaintInfo, currentTX, currentTY);
currentPaintInfo.phase = PaintPhaseOutline;
r->m_renderer->paint(currentPaintInfo, currentTX, currentTY);
}
}
}
}
void RenderBlock::paintEllipsisBoxes(PaintInfo& paintInfo, int tx, int ty)
{
if (!shouldPaintWithinRoot(paintInfo) || !firstLineBox())
return;
if (style()->visibility() == VISIBLE && paintInfo.phase == PaintPhaseForeground) {
int yPos = ty + firstLineBox()->yPos();
int h = lastLineBox()->yPos() + lastLineBox()->height() - firstLineBox()->yPos();
if (yPos >= paintInfo.rect.bottom() || yPos + h <= paintInfo.rect.y())
return;
for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
yPos = ty + curr->yPos();
h = curr->height();
if (curr->ellipsisBox() && yPos < paintInfo.rect.bottom() && yPos + h > paintInfo.rect.y())
curr->paintEllipsisBox(paintInfo, tx, ty);
}
}
}
static ContinuationOutlineTableMap* continuationOutlineTable()
{
DEFINE_STATIC_LOCAL(ContinuationOutlineTableMap, table, ());
return &table;
}
void RenderBlock::addContinuationWithOutline(RenderFlow* flow)
{
ASSERT(!flow->layer());
ContinuationOutlineTableMap* table = continuationOutlineTable();
ListHashSet<RenderFlow*>* continuations = table->get(this);
if (!continuations) {
continuations = new ListHashSet<RenderFlow*>;
table->set(this, continuations);
}
continuations->add(flow);
}
void RenderBlock::paintContinuationOutlines(PaintInfo& info, int tx, int ty)
{
ContinuationOutlineTableMap* table = continuationOutlineTable();
if (table->isEmpty())
return;
ListHashSet<RenderFlow*>* continuations = table->get(this);
if (!continuations)
return;
ListHashSet<RenderFlow*>::iterator end = continuations->end();
for (ListHashSet<RenderFlow*>::iterator it = continuations->begin(); it != end; ++it) {
RenderFlow* flow = *it;
RenderBlock* block = flow->containingBlock();
for ( ; block && block != this; block = block->containingBlock()) {
tx += block->x();
ty += block->y();
}
ASSERT(block);
flow->paintOutline(info.context, tx, ty);
}
delete continuations;
table->remove(this);
}
void RenderBlock::setSelectionState(SelectionState s)
{
if (selectionState() == s)
return;
if (s == SelectionInside && selectionState() != SelectionNone)
return;
if ((s == SelectionStart && selectionState() == SelectionEnd) ||
(s == SelectionEnd && selectionState() == SelectionStart))
m_selectionState = SelectionBoth;
else
m_selectionState = s;
RenderBlock* cb = containingBlock();
if (cb && !cb->isRenderView())
cb->setSelectionState(s);
}
bool RenderBlock::shouldPaintSelectionGaps() const
{
return m_selectionState != SelectionNone && style()->visibility() == VISIBLE && isSelectionRoot();
}
bool RenderBlock::isSelectionRoot() const
{
if (!element())
return false;
if (isTable())
return false;
if (isBody() || isRoot() || hasOverflowClip() || isRelPositioned() ||
isFloatingOrPositioned() || isTableCell() || isInlineBlockOrInlineTable() || hasTransform() ||
hasReflection() || hasMask())
return true;
if (view() && view()->selectionStart()) {
Node* startElement = view()->selectionStart()->element();
if (startElement && startElement->rootEditableElement() == element())
return true;
}
return false;
}
GapRects RenderBlock::selectionGapRectsForRepaint(RenderBox* )
{
ASSERT(!needsLayout());
if (!shouldPaintSelectionGaps())
return GapRects();
FloatPoint absContentPoint = localToAbsolute(FloatPoint());
if (hasOverflowClip())
absContentPoint -= layer()->scrolledContentOffset();
int lastTop = 0;
int lastLeft = leftSelectionOffset(this, lastTop);
int lastRight = rightSelectionOffset(this, lastTop);
return fillSelectionGaps(this, absContentPoint.x(), absContentPoint.y(), absContentPoint.x(), absContentPoint.y(), lastTop, lastLeft, lastRight);
}
void RenderBlock::paintSelection(PaintInfo& paintInfo, int tx, int ty)
{
UNUSED_PARAM(paintInfo);
UNUSED_PARAM(tx);
UNUSED_PARAM(ty);
return;
}
static void clipOutPositionedObjects(const RenderObject::PaintInfo* paintInfo, int tx, int ty, ListHashSet<RenderBox*>* positionedObjects)
{
if (!positionedObjects)
return;
ListHashSet<RenderBox*>::const_iterator end = positionedObjects->end();
for (ListHashSet<RenderBox*>::const_iterator it = positionedObjects->begin(); it != end; ++it) {
RenderBox* r = *it;
paintInfo->context->clipOut(IntRect(tx + r->x(), ty + r->y(), r->width(), r->height()));
}
}
GapRects RenderBlock::fillSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
int& lastTop, int& lastLeft, int& lastRight, const PaintInfo* paintInfo)
{
if (paintInfo) {
clipOutPositionedObjects(paintInfo, tx, ty, m_positionedObjects);
if (isBody() || isRoot()) for (RenderBlock* cb = containingBlock(); cb && !cb->isRenderView(); cb = cb->containingBlock())
clipOutPositionedObjects(paintInfo, cb->x(), cb->y(), cb->m_positionedObjects);
if (m_floatingObjects) {
for (DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects); it.current(); ++it) {
FloatingObject* r = it.current();
paintInfo->context->clipOut(IntRect(tx + r->m_left + r->m_renderer->marginLeft(),
ty + r->m_top + r->m_renderer->marginTop(),
r->m_renderer->width(), r->m_renderer->height()));
}
}
}
GapRects result;
if (!isBlockFlow()) return result;
if (m_hasColumns || hasTransform()) {
lastTop = (ty - blockY) + height();
lastLeft = leftSelectionOffset(rootBlock, height());
lastRight = rightSelectionOffset(rootBlock, height());
return result;
}
if (childrenInline())
result = fillInlineSelectionGaps(rootBlock, blockX, blockY, tx, ty, lastTop, lastLeft, lastRight, paintInfo);
else
result = fillBlockSelectionGaps(rootBlock, blockX, blockY, tx, ty, lastTop, lastLeft, lastRight, paintInfo);
if (rootBlock == this && (m_selectionState != SelectionBoth && m_selectionState != SelectionEnd))
result.uniteCenter(fillVerticalSelectionGap(lastTop, lastLeft, lastRight, ty + height(),
rootBlock, blockX, blockY, paintInfo));
return result;
}
GapRects RenderBlock::fillInlineSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
int& lastTop, int& lastLeft, int& lastRight, const PaintInfo* paintInfo)
{
GapRects result;
bool containsStart = selectionState() == SelectionStart || selectionState() == SelectionBoth;
if (!firstLineBox()) {
if (containsStart) {
lastTop = (ty - blockY) + height();
lastLeft = leftSelectionOffset(rootBlock, height());
lastRight = rightSelectionOffset(rootBlock, height());
}
return result;
}
RootInlineBox* lastSelectedLine = 0;
RootInlineBox* curr;
for (curr = firstRootBox(); curr && !curr->hasSelectedChildren(); curr = curr->nextRootBox()) { }
for (; curr && curr->hasSelectedChildren(); curr = curr->nextRootBox()) {
int selTop = curr->selectionTop();
int selHeight = curr->selectionHeight();
if (!containsStart && !lastSelectedLine &&
selectionState() != SelectionStart && selectionState() != SelectionBoth)
result.uniteCenter(fillVerticalSelectionGap(lastTop, lastLeft, lastRight, ty + selTop,
rootBlock, blockX, blockY, paintInfo));
if (!paintInfo || ty + selTop < paintInfo->rect.bottom() && ty + selTop + selHeight > paintInfo->rect.y())
result.unite(curr->fillLineSelectionGap(selTop, selHeight, rootBlock, blockX, blockY, tx, ty, paintInfo));
lastSelectedLine = curr;
}
if (containsStart && !lastSelectedLine)
lastSelectedLine = lastRootBox();
if (lastSelectedLine && selectionState() != SelectionEnd && selectionState() != SelectionBoth) {
lastTop = (ty - blockY) + lastSelectedLine->bottomOverflow();
lastLeft = leftSelectionOffset(rootBlock, lastSelectedLine->bottomOverflow());
lastRight = rightSelectionOffset(rootBlock, lastSelectedLine->bottomOverflow());
}
return result;
}
GapRects RenderBlock::fillBlockSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
int& lastTop, int& lastLeft, int& lastRight, const PaintInfo* paintInfo)
{
GapRects result;
RenderBox* curr;
for (curr = firstChildBox(); curr && curr->selectionState() == SelectionNone; curr = curr->nextSiblingBox()) { }
for (bool sawSelectionEnd = false; curr && !sawSelectionEnd; curr = curr->nextSiblingBox()) {
SelectionState childState = curr->selectionState();
if (childState == SelectionBoth || childState == SelectionEnd)
sawSelectionEnd = true;
if (curr->isFloatingOrPositioned())
continue;
if (curr->isRelPositioned() && curr->hasLayer()) {
IntSize relOffset = curr->layer()->relativePositionOffset();
if (relOffset.width() || relOffset.height())
continue;
}
bool paintsOwnSelection = curr->shouldPaintSelectionGaps() || curr->isTable(); bool fillBlockGaps = paintsOwnSelection || (curr->canBeSelectionLeaf() && childState != SelectionNone);
if (fillBlockGaps) {
if (childState == SelectionEnd || childState == SelectionInside)
result.uniteCenter(fillVerticalSelectionGap(lastTop, lastLeft, lastRight,
ty + curr->y(), rootBlock, blockX, blockY, paintInfo));
if (paintsOwnSelection && (childState == SelectionStart || sawSelectionEnd))
childState = SelectionNone;
bool leftGap, rightGap;
getHorizontalSelectionGapInfo(childState, leftGap, rightGap);
if (leftGap)
result.uniteLeft(fillLeftSelectionGap(this, curr->x(), curr->y(), curr->height(), rootBlock, blockX, blockY, tx, ty, paintInfo));
if (rightGap)
result.uniteRight(fillRightSelectionGap(this, curr->x() + curr->width(), curr->y(), curr->height(), rootBlock, blockX, blockY, tx, ty, paintInfo));
lastTop = (ty - blockY) + (curr->y() + curr->height());
lastLeft = leftSelectionOffset(rootBlock, curr->y() + curr->height());
lastRight = rightSelectionOffset(rootBlock, curr->y() + curr->height());
} else if (childState != SelectionNone)
result.unite(static_cast<RenderBlock*>(curr)->fillSelectionGaps(rootBlock, blockX, blockY, tx + curr->x(), ty + curr->y(),
lastTop, lastLeft, lastRight, paintInfo));
}
return result;
}
IntRect RenderBlock::fillHorizontalSelectionGap(RenderObject* selObj, int xPos, int yPos, int actualWidth, int height, const PaintInfo* paintInfo)
{
if (actualWidth <= 0 || height <= 0)
return IntRect();
IntRect gapRect(xPos, yPos, actualWidth, height);
if (paintInfo && selObj->style()->visibility() == VISIBLE)
paintInfo->context->fillRect(gapRect, selObj->selectionBackgroundColor());
return gapRect;
}
IntRect RenderBlock::fillVerticalSelectionGap(int lastTop, int lastLeft, int lastRight, int bottomY, RenderBlock* rootBlock,
int blockX, int blockY, const PaintInfo* paintInfo)
{
int top = blockY + lastTop;
int height = bottomY - top;
if (height <= 0)
return IntRect();
int left = blockX + max(lastLeft, leftSelectionOffset(rootBlock, bottomY));
int right = blockX + min(lastRight, rightSelectionOffset(rootBlock, bottomY));
int actualWidth = right - left;
if (actualWidth <= 0)
return IntRect();
IntRect gapRect(left, top, actualWidth, height);
if (paintInfo)
paintInfo->context->fillRect(gapRect, selectionBackgroundColor());
return gapRect;
}
IntRect RenderBlock::fillLeftSelectionGap(RenderObject* selObj, int xPos, int yPos, int height, RenderBlock* rootBlock,
int blockX, int , int tx, int ty, const PaintInfo* paintInfo)
{
int top = yPos + ty;
int left = blockX + max(leftSelectionOffset(rootBlock, yPos), leftSelectionOffset(rootBlock, yPos + height));
int right = min(xPos + tx, blockX + min(rightSelectionOffset(rootBlock, yPos), rightSelectionOffset(rootBlock, yPos + height)));
int actualWidth = right - left;
if (actualWidth <= 0)
return IntRect();
IntRect gapRect(left, top, actualWidth, height);
if (paintInfo)
paintInfo->context->fillRect(gapRect, selObj->selectionBackgroundColor());
return gapRect;
}
IntRect RenderBlock::fillRightSelectionGap(RenderObject* selObj, int xPos, int yPos, int height, RenderBlock* rootBlock,
int blockX, int , int tx, int ty, const PaintInfo* paintInfo)
{
int left = max(xPos + tx, blockX + max(leftSelectionOffset(rootBlock, yPos), leftSelectionOffset(rootBlock, yPos + height)));
int top = yPos + ty;
int right = blockX + min(rightSelectionOffset(rootBlock, yPos), rightSelectionOffset(rootBlock, yPos + height));
int actualWidth = right - left;
if (actualWidth <= 0)
return IntRect();
IntRect gapRect(left, top, actualWidth, height);
if (paintInfo)
paintInfo->context->fillRect(gapRect, selObj->selectionBackgroundColor());
return gapRect;
}
void RenderBlock::getHorizontalSelectionGapInfo(SelectionState state, bool& leftGap, bool& rightGap)
{
bool ltr = style()->direction() == LTR;
leftGap = (state == RenderObject::SelectionInside) ||
(state == RenderObject::SelectionEnd && ltr) ||
(state == RenderObject::SelectionStart && !ltr);
rightGap = (state == RenderObject::SelectionInside) ||
(state == RenderObject::SelectionStart && ltr) ||
(state == RenderObject::SelectionEnd && !ltr);
}
int RenderBlock::leftSelectionOffset(RenderBlock* rootBlock, int yPos)
{
int left = leftOffset(yPos);
if (left == borderLeft() + paddingLeft()) {
if (rootBlock != this)
return containingBlock()->leftSelectionOffset(rootBlock, yPos + y());
return left;
}
else {
RenderBlock* cb = this;
while (cb != rootBlock) {
left += cb->x();
cb = cb->containingBlock();
}
}
return left;
}
int RenderBlock::rightSelectionOffset(RenderBlock* rootBlock, int yPos)
{
int right = rightOffset(yPos);
if (right == (contentWidth() + (borderLeft() + paddingLeft()))) {
if (rootBlock != this)
return containingBlock()->rightSelectionOffset(rootBlock, yPos + y());
return right;
}
else {
RenderBlock* cb = this;
while (cb != rootBlock) {
right += cb->x();
cb = cb->containingBlock();
}
}
return right;
}
void RenderBlock::insertPositionedObject(RenderBox* o)
{
if (!m_positionedObjects)
m_positionedObjects = new ListHashSet<RenderBox*>;
m_positionedObjects->add(o);
}
void RenderBlock::removePositionedObject(RenderBox* o)
{
if (m_positionedObjects)
m_positionedObjects->remove(o);
}
void RenderBlock::removePositionedObjects(RenderBlock* o)
{
if (!m_positionedObjects)
return;
RenderBox* r;
Iterator end = m_positionedObjects->end();
Vector<RenderBox*, 16> deadObjects;
for (Iterator it = m_positionedObjects->begin(); it != end; ++it) {
r = *it;
if (!o || r->isDescendantOf(o)) {
if (o)
r->setChildNeedsLayout(true, false);
RenderObject* p = r->parent();
while (p && !p->isRenderBlock())
p = p->parent();
if (p)
p->setChildNeedsLayout(true);
deadObjects.append(r);
}
}
for (unsigned i = 0; i < deadObjects.size(); i++)
m_positionedObjects->remove(deadObjects.at(i));
}
void RenderBlock::insertFloatingObject(RenderBox* o)
{
ASSERT(o->isFloating());
if (!m_floatingObjects) {
m_floatingObjects = new DeprecatedPtrList<FloatingObject>;
m_floatingObjects->setAutoDelete(true);
} else {
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
FloatingObject* f;
while ( (f = it.current()) ) {
if (f->m_renderer == o) return;
++it;
}
}
o->layoutIfNeeded();
FloatingObject* newObj = new FloatingObject(o->style()->floating() == FLEFT ? FloatingObject::FloatLeft : FloatingObject::FloatRight);
newObj->m_top = -1;
newObj->m_bottom = -1;
newObj->m_width = o->width() + o->marginLeft() + o->marginRight();
newObj->m_shouldPaint = !o->hasSelfPaintingLayer(); newObj->m_isDescendant = true;
newObj->m_renderer = o;
m_floatingObjects->append(newObj);
}
void RenderBlock::removeFloatingObject(RenderBox* o)
{
if (m_floatingObjects) {
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
while (it.current()) {
if (it.current()->m_renderer == o) {
if (childrenInline())
markLinesDirtyInVerticalRange(0, it.current()->m_bottom);
m_floatingObjects->removeRef(it.current());
}
++it;
}
}
}
bool RenderBlock::positionNewFloats()
{
if (!m_floatingObjects)
return false;
FloatingObject* f = m_floatingObjects->last();
if (!f || f->m_top != -1)
return false;
FloatingObject* lastFloat = m_floatingObjects->getPrev();
while (lastFloat && lastFloat->m_top == -1) {
f = m_floatingObjects->prev();
lastFloat = m_floatingObjects->getPrev();
}
int y = height();
if (lastFloat)
y = max(lastFloat->m_top, y);
while (f) {
if (f->m_renderer->containingBlock() != this) {
f = m_floatingObjects->next();
continue;
}
RenderBox* o = f->m_renderer;
int _height = o->height() + o->marginTop() + o->marginBottom();
int ro = rightOffset(); int lo = leftOffset(); int fwidth = f->m_width; if (ro - lo < fwidth)
fwidth = ro - lo;
IntRect oldRect(o->x(), o->y() , o->width(), o->height());
if (o->style()->clear() & CLEFT)
y = max(leftBottom(), y);
if (o->style()->clear() & CRIGHT)
y = max(rightBottom(), y);
if (o->style()->floating() == FLEFT) {
int heightRemainingLeft = 1;
int heightRemainingRight = 1;
int fx = leftRelOffset(y,lo, false, &heightRemainingLeft);
while (rightRelOffset(y,ro, false, &heightRemainingRight)-fx < fwidth) {
y += min(heightRemainingLeft, heightRemainingRight);
fx = leftRelOffset(y,lo, false, &heightRemainingLeft);
}
fx = max(0, fx);
f->m_left = fx;
o->setLocation(fx + o->marginLeft(), y + o->marginTop());
} else {
int heightRemainingLeft = 1;
int heightRemainingRight = 1;
int fx = rightRelOffset(y,ro, false, &heightRemainingRight);
while (fx - leftRelOffset(y,lo, false, &heightRemainingLeft) < fwidth) {
y += min(heightRemainingLeft, heightRemainingRight);
fx = rightRelOffset(y, ro, false, &heightRemainingRight);
}
f->m_left = fx - f->m_width;
o->setLocation(fx - o->marginRight() - o->width(), y + o->marginTop());
}
f->m_top = y;
f->m_bottom = f->m_top + _height;
if (o->checkForRepaintDuringLayout())
o->repaintDuringLayoutIfMoved(oldRect);
f = m_floatingObjects->next();
}
return true;
}
void RenderBlock::newLine(EClear clear)
{
positionNewFloats();
int newY = 0;
switch(clear)
{
case CLEFT:
newY = leftBottom();
break;
case CRIGHT:
newY = rightBottom();
break;
case CBOTH:
newY = floatBottom();
default:
break;
}
if (height() < newY)
setHeight(newY);
}
void RenderBlock::addPercentHeightDescendant(RenderBox* descendant)
{
if (!gPercentHeightDescendantsMap) {
gPercentHeightDescendantsMap = new PercentHeightDescendantsMap;
gPercentHeightContainerMap = new PercentHeightContainerMap;
}
HashSet<RenderBox*>* descendantSet = gPercentHeightDescendantsMap->get(this);
if (!descendantSet) {
descendantSet = new HashSet<RenderBox*>;
gPercentHeightDescendantsMap->set(this, descendantSet);
}
bool added = descendantSet->add(descendant).second;
if (!added) {
ASSERT(gPercentHeightContainerMap->get(descendant));
ASSERT(gPercentHeightContainerMap->get(descendant)->contains(this));
return;
}
HashSet<RenderBlock*>* containerSet = gPercentHeightContainerMap->get(descendant);
if (!containerSet) {
containerSet = new HashSet<RenderBlock*>;
gPercentHeightContainerMap->set(descendant, containerSet);
}
ASSERT(!containerSet->contains(this));
containerSet->add(this);
}
void RenderBlock::removePercentHeightDescendant(RenderBox* descendant)
{
if (!gPercentHeightContainerMap)
return;
HashSet<RenderBlock*>* containerSet = gPercentHeightContainerMap->take(descendant);
if (!containerSet)
return;
HashSet<RenderBlock*>::iterator end = containerSet->end();
for (HashSet<RenderBlock*>::iterator it = containerSet->begin(); it != end; ++it) {
RenderBlock* container = *it;
HashSet<RenderBox*>* descendantSet = gPercentHeightDescendantsMap->get(container);
ASSERT(descendantSet);
if (!descendantSet)
continue;
ASSERT(descendantSet->contains(descendant));
descendantSet->remove(descendant);
if (descendantSet->isEmpty()) {
gPercentHeightDescendantsMap->remove(container);
delete descendantSet;
}
}
delete containerSet;
}
int
RenderBlock::leftOffset() const
{
return borderLeft()+paddingLeft();
}
int
RenderBlock::leftRelOffset(int y, int fixedOffset, bool applyTextIndent,
int *heightRemaining ) const
{
int left = fixedOffset;
if (m_floatingObjects) {
if ( heightRemaining ) *heightRemaining = 1;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
{
if (r->m_top <= y && r->m_bottom > y &&
r->type() == FloatingObject::FloatLeft &&
r->m_left + r->m_width > left) {
left = r->m_left + r->m_width;
if ( heightRemaining ) *heightRemaining = r->m_bottom - y;
}
}
}
if (applyTextIndent && m_firstLine && style()->direction() == LTR) {
int cw = 0;
if (style()->textIndent().isPercent())
cw = containingBlock()->availableWidth();
left += style()->textIndent().calcMinValue(cw);
}
return left;
}
int
RenderBlock::rightOffset() const
{
return borderLeft() + paddingLeft() + availableWidth();
}
int
RenderBlock::rightRelOffset(int y, int fixedOffset, bool applyTextIndent,
int *heightRemaining ) const
{
int right = fixedOffset;
if (m_floatingObjects) {
if (heightRemaining) *heightRemaining = 1;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
{
if (r->m_top <= y && r->m_bottom > y &&
r->type() == FloatingObject::FloatRight &&
r->m_left < right) {
right = r->m_left;
if ( heightRemaining ) *heightRemaining = r->m_bottom - y;
}
}
}
if (applyTextIndent && m_firstLine && style()->direction() == RTL) {
int cw = 0;
if (style()->textIndent().isPercent())
cw = containingBlock()->availableWidth();
right -= style()->textIndent().calcMinValue(cw);
}
return right;
}
int
RenderBlock::lineWidth(int y) const
{
int result = rightOffset(y) - leftOffset(y);
return (result < 0) ? 0 : result;
}
int RenderBlock::nextFloatBottomBelow(int height) const
{
if (!m_floatingObjects)
return 0;
int bottom = INT_MAX;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it) {
if (r->m_bottom > height)
bottom = min(r->m_bottom, bottom);
}
return bottom == INT_MAX ? 0 : bottom;
}
int
RenderBlock::floatBottom() const
{
if (!m_floatingObjects) return 0;
int bottom=0;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
if (r->m_bottom>bottom)
bottom=r->m_bottom;
return bottom;
}
IntRect RenderBlock::floatRect() const
{
IntRect result;
if (!m_floatingObjects || hasOverflowClip())
return result;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for (; (r = it.current()); ++it) {
if (r->m_shouldPaint && !r->m_renderer->hasSelfPaintingLayer()) {
IntRect childRect = r->m_renderer->overflowRect(false);
childRect.move(r->m_left + r->m_renderer->marginLeft(), r->m_top + r->m_renderer->marginTop());
result.unite(childRect);
}
}
return result;
}
int RenderBlock::lowestPosition(bool includeOverflowInterior, bool includeSelf) const
{
if (!includeOverflowInterior && (hasOverflowClip() || hasControlClip()))
return includeSelf && width() > 0 ? overflowHeight(false) : 0;
int bottom = includeSelf && width() > 0 ? height() : 0;
if (!hasColumns()) {
for (RenderObject* c = firstChild(); c; c = c->nextSibling()) {
if (!c->isFloatingOrPositioned() && !c->isText() && !c->isRenderInline())
bottom = max(bottom, toRenderBox(c)->y() + c->lowestPosition(false));
}
}
if (includeSelf && isRelPositioned())
bottom += relativePositionOffsetY();
if (!includeOverflowInterior && hasOverflowClip())
return bottom;
int relativeOffset = includeSelf && isRelPositioned() ? relativePositionOffsetY() : 0;
if (includeSelf)
bottom = max(bottom, m_overflowHeight + relativeOffset);
if (m_positionedObjects) {
RenderBox* r;
Iterator end = m_positionedObjects->end();
for (Iterator it = m_positionedObjects->begin(); it != end; ++it) {
r = *it;
if (r->style()->position() != FixedPosition) {
if (!isRenderView() || r->x() + r->width() > 0 || r->x() + r->rightmostPosition(false) > 0) {
int lp = r->y() + r->lowestPosition(false);
bottom = max(bottom, lp + relativeOffset);
}
}
}
}
if (m_hasColumns) {
Vector<IntRect>* colRects = columnRects();
for (unsigned i = 0; i < colRects->size(); i++)
bottom = max(bottom, colRects->at(i).bottom() + relativeOffset);
return bottom;
}
if (m_floatingObjects) {
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it ) {
if (r->m_shouldPaint || r->m_renderer->hasSelfPaintingLayer()) {
int lp = r->m_top + r->m_renderer->marginTop() + r->m_renderer->lowestPosition(false);
bottom = max(bottom, lp + relativeOffset);
}
}
}
if (!includeSelf && lastLineBox()) {
int lp = lastLineBox()->yPos() + lastLineBox()->height();
bottom = max(bottom, lp);
}
return bottom;
}
int RenderBlock::rightmostPosition(bool includeOverflowInterior, bool includeSelf) const
{
if (!includeOverflowInterior && (hasOverflowClip() || hasControlClip()))
return includeSelf && height() > 0 ? overflowWidth(false) : 0;
int right = includeSelf && height() > 0 ? width() : 0;
if (!hasColumns()) {
for (RenderObject* c = firstChild(); c; c = c->nextSibling()) {
if (!c->isFloatingOrPositioned() && c->isBox() && !c->isRenderInline())
right = max(right, toRenderBox(c)->x() + c->rightmostPosition(false));
}
}
if (includeSelf && isRelPositioned())
right += relativePositionOffsetX();
if (!includeOverflowInterior && hasOverflowClip())
return right;
int relativeOffset = includeSelf && isRelPositioned() ? relativePositionOffsetX() : 0;
if (includeSelf)
right = max(right, m_overflowWidth + relativeOffset);
if (m_positionedObjects) {
RenderBox* r;
Iterator end = m_positionedObjects->end();
for (Iterator it = m_positionedObjects->begin() ; it != end; ++it) {
r = *it;
if (r->style()->position() != FixedPosition) {
if (!isRenderView() || r->y() + r->height() > 0 || r->y() + r->lowestPosition(false) > 0) {
int rp = r->x() + r->rightmostPosition(false);
right = max(right, rp + relativeOffset);
}
}
}
}
if (m_hasColumns) {
if (style()->direction() == LTR)
right = max(columnRects()->last().right() + relativeOffset, right);
return right;
}
if (m_floatingObjects) {
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it ) {
if (r->m_shouldPaint || r->m_renderer->hasSelfPaintingLayer()) {
int rp = r->m_left + r->m_renderer->marginLeft() + r->m_renderer->rightmostPosition(false);
right = max(right, rp + relativeOffset);
}
}
}
if (!includeSelf && firstLineBox()) {
for (InlineRunBox* currBox = firstLineBox(); currBox; currBox = currBox->nextLineBox()) {
int rp = currBox->xPos() + currBox->width();
if (node()->isContentEditable() && node() == node()->rootEditableElement() && style()->direction() == LTR)
rp += 1;
right = max(right, rp);
}
}
return right;
}
int RenderBlock::leftmostPosition(bool includeOverflowInterior, bool includeSelf) const
{
if (!includeOverflowInterior && (hasOverflowClip() || hasControlClip()))
return includeSelf && height() > 0 ? overflowLeft(false) : width();
int left = includeSelf && height() > 0 ? 0 : width();
if (!hasColumns()) {
for (RenderObject* c = firstChild(); c; c = c->nextSibling()) {
if (!c->isFloatingOrPositioned() && c->isBox() && !c->isRenderInline())
left = min(left, toRenderBox(c)->x() + c->leftmostPosition(false));
}
}
if (includeSelf && isRelPositioned())
left += relativePositionOffsetX();
if (!includeOverflowInterior && hasOverflowClip())
return left;
int relativeOffset = includeSelf && isRelPositioned() ? relativePositionOffsetX() : 0;
if (includeSelf)
left = min(left, m_overflowLeft + relativeOffset);
if (m_positionedObjects) {
RenderBox* r;
Iterator end = m_positionedObjects->end();
for (Iterator it = m_positionedObjects->begin(); it != end; ++it) {
r = *it;
if (r->style()->position() != FixedPosition) {
if (!isRenderView() || r->y() + r->height() > 0 || r->y() + r->lowestPosition(false) > 0) {
int lp = r->x() + r->leftmostPosition(false);
left = min(left, lp + relativeOffset);
}
}
}
}
if (m_hasColumns) {
if (style()->direction() == RTL)
left = min(columnRects()->last().x() + relativeOffset, left);
return left;
}
if (m_floatingObjects) {
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it ) {
if (r->m_shouldPaint || r->m_renderer->hasSelfPaintingLayer()) {
int lp = r->m_left + r->m_renderer->marginLeft() + r->m_renderer->leftmostPosition(false);
left = min(left, lp + relativeOffset);
}
}
}
if (!includeSelf && firstLineBox()) {
for (InlineRunBox* currBox = firstLineBox(); currBox; currBox = currBox->nextLineBox())
left = min(left, (int)currBox->xPos());
}
return left;
}
int
RenderBlock::leftBottom()
{
if (!m_floatingObjects) return 0;
int bottom=0;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
if (r->m_bottom > bottom && r->type() == FloatingObject::FloatLeft)
bottom=r->m_bottom;
return bottom;
}
int
RenderBlock::rightBottom()
{
if (!m_floatingObjects) return 0;
int bottom=0;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
if (r->m_bottom>bottom && r->type() == FloatingObject::FloatRight)
bottom=r->m_bottom;
return bottom;
}
void RenderBlock::markLinesDirtyInVerticalRange(int top, int bottom)
{
if (top >= bottom)
return;
RootInlineBox* lowestDirtyLine = lastRootBox();
RootInlineBox* afterLowest = lowestDirtyLine;
while (lowestDirtyLine && lowestDirtyLine->blockHeight() >= bottom) {
afterLowest = lowestDirtyLine;
lowestDirtyLine = lowestDirtyLine->prevRootBox();
}
while (afterLowest && afterLowest->blockHeight() >= top) {
afterLowest->markDirty();
afterLowest = afterLowest->prevRootBox();
}
}
void RenderBlock::clearFloats()
{
if (avoidsFloats() || isRoot() || isRenderView() || isFloatingOrPositioned() || isTableCell()) {
if (m_floatingObjects)
m_floatingObjects->clear();
return;
}
typedef HashMap<RenderObject*, FloatingObject*> RendererToFloatInfoMap;
RendererToFloatInfoMap floatMap;
if (m_floatingObjects) {
if (childrenInline()) {
m_floatingObjects->first();
while (FloatingObject* f = m_floatingObjects->take())
floatMap.add(f->m_renderer, f);
} else
m_floatingObjects->clear();
}
bool parentHasFloats = false;
RenderObject* prev = previousSibling();
while (prev && (!prev->isBox() || !prev->isRenderBlock() || prev->avoidsFloats() || prev->isFloatingOrPositioned())) {
if (prev->isFloating())
parentHasFloats = true;
prev = prev->previousSibling();
}
int offset = y();
if (parentHasFloats) {
RenderBlock* parentBlock = static_cast<RenderBlock *>(parent());
addIntrudingFloats(parentBlock, parentBlock->borderLeft() + parentBlock->paddingLeft(), offset);
}
int xoffset = 0;
if (prev)
offset -= toRenderBox(prev)->y();
else if (parent()->isBox()) {
prev = parent();
xoffset += toRenderBox(prev)->borderLeft() + toRenderBox(prev)->paddingLeft();
}
if (!prev || !prev->isRenderBlock())
return;
RenderBlock* block = static_cast<RenderBlock *>(prev);
if (block->m_floatingObjects && block->floatBottom() > offset)
addIntrudingFloats(block, xoffset, offset);
if (childrenInline()) {
int changeTop = INT_MAX;
int changeBottom = INT_MIN;
if (m_floatingObjects) {
for (FloatingObject* f = m_floatingObjects->first(); f; f = m_floatingObjects->next()) {
FloatingObject* oldFloatingObject = floatMap.get(f->m_renderer);
if (oldFloatingObject) {
if (f->m_width != oldFloatingObject->m_width || f->m_left != oldFloatingObject->m_left) {
changeTop = 0;
changeBottom = max(changeBottom, max(f->m_bottom, oldFloatingObject->m_bottom));
} else if (f->m_bottom != oldFloatingObject->m_bottom) {
changeTop = min(changeTop, min(f->m_bottom, oldFloatingObject->m_bottom));
changeBottom = max(changeBottom, max(f->m_bottom, oldFloatingObject->m_bottom));
}
floatMap.remove(f->m_renderer);
delete oldFloatingObject;
} else {
changeTop = 0;
changeBottom = max(changeBottom, f->m_bottom);
}
}
}
RendererToFloatInfoMap::iterator end = floatMap.end();
for (RendererToFloatInfoMap::iterator it = floatMap.begin(); it != end; ++it) {
FloatingObject* floatingObject = (*it).second;
if (!floatingObject->m_isDescendant) {
changeTop = 0;
changeBottom = max(changeBottom, floatingObject->m_bottom);
}
}
deleteAllValues(floatMap);
markLinesDirtyInVerticalRange(changeTop, changeBottom);
}
}
int RenderBlock::addOverhangingFloats(RenderBlock* child, int xoff, int yoff, bool makeChildPaintOtherFloats)
{
if (child->hasOverflowClip() || !child->containsFloats() || child->isRoot())
return 0;
int lowestFloatBottom = 0;
IntRect floatsOverflowRect;
DeprecatedPtrListIterator<FloatingObject> it(*child->m_floatingObjects);
for (FloatingObject* r; (r = it.current()); ++it) {
int bottom = child->y() + r->m_bottom;
lowestFloatBottom = max(lowestFloatBottom, bottom);
if (bottom > height()) {
if (!containsFloat(r->m_renderer)) {
FloatingObject *floatingObj = new FloatingObject(r->type());
floatingObj->m_top = r->m_top - yoff;
floatingObj->m_bottom = r->m_bottom - yoff;
floatingObj->m_left = r->m_left - xoff;
floatingObj->m_width = r->m_width;
floatingObj->m_renderer = r->m_renderer;
if (r->m_renderer->enclosingLayer() == enclosingLayer())
r->m_shouldPaint = false;
else
floatingObj->m_shouldPaint = false;
if (!m_floatingObjects) {
m_floatingObjects = new DeprecatedPtrList<FloatingObject>;
m_floatingObjects->setAutoDelete(true);
}
m_floatingObjects->append(floatingObj);
}
} else if (makeChildPaintOtherFloats && !r->m_shouldPaint && !r->m_renderer->hasSelfPaintingLayer() &&
r->m_renderer->isDescendantOf(child) && r->m_renderer->enclosingLayer() == child->enclosingLayer())
r->m_shouldPaint = true;
if (r->m_shouldPaint && !r->m_renderer->hasSelfPaintingLayer()) {
IntRect floatOverflowRect = r->m_renderer->overflowRect(false);
floatOverflowRect.move(r->m_left + r->m_renderer->marginLeft(), r->m_top + r->m_renderer->marginTop());
floatsOverflowRect.unite(floatOverflowRect);
}
}
child->addVisualOverflow(floatsOverflowRect);
return lowestFloatBottom;
}
void RenderBlock::addIntrudingFloats(RenderBlock* prev, int xoff, int yoff)
{
if (!prev->m_floatingObjects)
return;
DeprecatedPtrListIterator<FloatingObject> it(*prev->m_floatingObjects);
for (FloatingObject *r; (r = it.current()); ++it) {
if (r->m_bottom > yoff) {
FloatingObject* f = 0;
if (m_floatingObjects) {
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
while ((f = it.current())) {
if (f->m_renderer == r->m_renderer) break;
++it;
}
}
if (!f) {
FloatingObject *floatingObj = new FloatingObject(r->type());
floatingObj->m_top = r->m_top - yoff;
floatingObj->m_bottom = r->m_bottom - yoff;
floatingObj->m_left = r->m_left - xoff;
if (prev != parent())
floatingObj->m_left += prev->marginLeft();
floatingObj->m_left -= marginLeft();
floatingObj->m_shouldPaint = false; floatingObj->m_width = r->m_width;
floatingObj->m_renderer = r->m_renderer;
if (!m_floatingObjects) {
m_floatingObjects = new DeprecatedPtrList<FloatingObject>;
m_floatingObjects->setAutoDelete(true);
}
m_floatingObjects->append(floatingObj);
}
}
}
}
bool RenderBlock::avoidsFloats() const
{
return RenderFlow::avoidsFloats() || !style()->hasAutoColumnCount() || !style()->hasAutoColumnWidth();
}
bool RenderBlock::containsFloat(RenderObject* o)
{
if (m_floatingObjects) {
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
while (it.current()) {
if (it.current()->m_renderer == o)
return true;
++it;
}
}
return false;
}
void RenderBlock::markAllDescendantsWithFloatsForLayout(RenderBox* floatToRemove, bool inLayout)
{
setChildNeedsLayout(true, !inLayout);
if (floatToRemove)
removeFloatingObject(floatToRemove);
if (!childrenInline()) {
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->isRenderBlock() && !child->isFloatingOrPositioned() &&
((floatToRemove ? child->containsFloat(floatToRemove) : child->containsFloats()) || child->shrinkToAvoidFloats()))
static_cast<RenderBlock*>(child)->markAllDescendantsWithFloatsForLayout(floatToRemove, inLayout);
}
}
}
int RenderBlock::getClearDelta(RenderBox* child)
{
if (!containsFloats())
return 0;
bool clearSet = child->style()->clear() != CNONE;
int bottom = 0;
switch (child->style()->clear()) {
case CNONE:
break;
case CLEFT:
bottom = leftBottom();
break;
case CRIGHT:
bottom = rightBottom();
break;
case CBOTH:
bottom = floatBottom();
break;
}
int result = clearSet ? max(0, bottom - child->y()) : 0;
if (!result && child->avoidsFloats() && child->style()->width().isFixed() &&
child->minPrefWidth() > lineWidth(child->y()) && child->minPrefWidth() <= availableWidth() &&
document()->inStrictMode())
result = max(0, floatBottom() - child->y());
return result;
}
void RenderBlock::addVisualOverflow(const IntRect& r)
{
if (r.isEmpty())
return;
m_overflowLeft = min(m_overflowLeft, r.x());
m_overflowWidth = max(m_overflowWidth, r.right());
m_overflowTop = min(m_overflowTop, r.y());
m_overflowHeight = max(m_overflowHeight, r.bottom());
}
bool RenderBlock::isPointInOverflowControl(HitTestResult& result, int, int, int, int)
{
if (!scrollsOverflow())
return false;
return layer()->hitTestOverflowControls(result);
}
bool RenderBlock::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, int _x, int _y, int _tx, int _ty, HitTestAction hitTestAction)
{
int tx = _tx + x();
int ty = _ty + y();
if (!isRenderView()) {
IntRect overflowBox = overflowRect(false);
overflowBox.move(tx, ty);
if (!overflowBox.contains(_x, _y))
return false;
}
if ((hitTestAction == HitTestBlockBackground || hitTestAction == HitTestChildBlockBackground) && isPointInOverflowControl(result, _x, _y, tx, ty)) {
updateHitTestResult(result, IntPoint(_x - tx, _y - ty));
return true;
}
bool useOverflowClip = hasOverflowClip() && !hasSelfPaintingLayer();
bool useClip = (hasControlClip() || useOverflowClip);
bool checkChildren = !useClip || (hasControlClip() ? controlClipRect(tx, ty).contains(_x, _y) : getOverflowClipRect(tx, ty).contains(_x, _y));
if (checkChildren) {
int scrolledX = tx;
int scrolledY = ty;
if (hasOverflowClip())
m_layer->subtractScrolledContentOffset(scrolledX, scrolledY);
if (!m_hasColumns && hitTestContents(request, result, _x, _y, scrolledX, scrolledY, hitTestAction))
return true;
if (m_hasColumns && hitTestColumns(request, result, _x, _y, scrolledX, scrolledY, hitTestAction))
return true;
if (hitTestAction == HitTestFloat && m_floatingObjects) {
if (isRenderView()) {
scrolledX += static_cast<RenderView*>(this)->frameView()->scrollX();
scrolledY += static_cast<RenderView*>(this)->frameView()->scrollY();
}
FloatingObject* o;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for (it.toLast(); (o = it.current()); --it) {
if (o->m_shouldPaint && !o->m_renderer->hasSelfPaintingLayer()) {
int xoffset = scrolledX + o->m_left + o->m_renderer->marginLeft() - o->m_renderer->x();
int yoffset = scrolledY + o->m_top + o->m_renderer->marginTop() - o->m_renderer->y();
if (o->m_renderer->hitTest(request, result, IntPoint(_x, _y), xoffset, yoffset)) {
updateHitTestResult(result, IntPoint(_x - xoffset, _y - yoffset));
return true;
}
}
}
}
}
if (hitTestAction == HitTestBlockBackground || hitTestAction == HitTestChildBlockBackground) {
IntRect boundsRect(tx, ty, width(), height());
if (visibleToHitTesting() && boundsRect.contains(_x, _y)) {
updateHitTestResult(result, IntPoint(_x - tx, _y - ty));
return true;
}
}
return false;
}
bool RenderBlock::hitTestColumns(const HitTestRequest& request, HitTestResult& result, int x, int y, int tx, int ty, HitTestAction hitTestAction)
{
int currXOffset = 0;
int currYOffset = 0;
int colGap = columnGap();
Vector<IntRect>* colRects = columnRects();
for (unsigned i = 0; i < colRects->size(); i++) {
IntRect colRect = colRects->at(i);
colRect.move(tx, ty);
if (colRect.contains(x, y)) {
int finalX = tx + currXOffset;
int finalY = ty + currYOffset;
return hitTestContents(request, result, x, y, finalX, finalY, hitTestAction);
}
if (style()->direction() == LTR)
currXOffset += colRect.width() + colGap;
else
currXOffset -= (colRect.width() + colGap);
currYOffset -= colRect.height();
}
return false;
}
bool RenderBlock::hitTestContents(const HitTestRequest& request, HitTestResult& result, int x, int y, int tx, int ty, HitTestAction hitTestAction)
{
if (childrenInline() && !isTable()) {
if (hitTestLines(request, result, x, y, tx, ty, hitTestAction)) {
updateHitTestResult(result, IntPoint(x - tx, y - ty));
return true;
}
} else {
HitTestAction childHitTest = hitTestAction;
if (hitTestAction == HitTestChildBlockBackgrounds)
childHitTest = HitTestChildBlockBackground;
for (RenderBox* child = lastChildBox(); child; child = child->previousSiblingBox()) {
if (!child->hasSelfPaintingLayer() && !child->isFloating() && child->nodeAtPoint(request, result, x, y, tx, ty, childHitTest)) {
updateHitTestResult(result, IntPoint(x - tx, y - ty));
return true;
}
}
}
return false;
}
Position RenderBlock::positionForBox(InlineBox *box, bool start) const
{
if (!box)
return Position();
if (!box->object()->element())
return Position(element(), start ? caretMinOffset() : caretMaxOffset());
if (!box->isInlineTextBox())
return Position(box->object()->element(), start ? box->object()->caretMinOffset() : box->object()->caretMaxOffset());
InlineTextBox *textBox = static_cast<InlineTextBox *>(box);
return Position(box->object()->element(), start ? textBox->start() : textBox->start() + textBox->len());
}
Position RenderBlock::positionForRenderer(RenderObject* renderer, bool start) const
{
if (!renderer)
return Position(element(), 0);
Node* node = renderer->element() ? renderer->element() : element();
if (!node)
return Position();
ASSERT(renderer == node->renderer());
int offset = start ? renderer->caretMinOffset() : renderer->caretMaxOffset();
ASSERT(!node->isCharacterDataNode() || renderer->isText());
return Position(node, offset);
}
VisiblePosition RenderBlock::positionForCoordinates(int x, int y)
{
if (isTable())
return RenderFlow::positionForCoordinates(x, y);
int top = borderTop();
int bottom = top + paddingTop() + contentHeight() + paddingBottom();
int left = borderLeft();
int right = left + paddingLeft() + contentWidth() + paddingRight();
Node* n = element();
int contentsX = x;
int contentsY = y;
offsetForContents(contentsX, contentsY);
if (isReplaced()) {
if (y < 0 || y < height() && x < 0)
return VisiblePosition(n, caretMinOffset(), DOWNSTREAM);
if (y >= height() || y >= 0 && x >= width())
return VisiblePosition(n, caretMaxOffset(), DOWNSTREAM);
}
right = right;
if (y < top) {
if (childrenInline()) {
if (!firstRootBox())
return VisiblePosition(n, 0, DOWNSTREAM);
InlineBox* closestBox = firstRootBox()->closestLeafChildForXPos(x);
if (closestBox)
return closestBox->object()->positionForCoordinates(contentsX, closestBox->m_y);
}
else {
if (firstChild()) {
RenderObject* c = firstChild();
return c->positionForCoordinates(contentsX - toRenderBox(c)->x(), contentsY - toRenderBox(c)->y());
}
}
}
else if (y >= bottom) {
if (childrenInline()) {
if (!lastRootBox())
return VisiblePosition(n, 0, DOWNSTREAM);
InlineBox* closestBox = lastRootBox()->closestLeafChildForXPos(x);
if (closestBox) {
if (document()->frame()->singleLineSelectionBehavior() || y - bottom < 8) {
return closestBox->object()->positionForCoordinates(contentsX, closestBox->m_y);
}
else {
return closestBox->object()->positionForCoordinates(contentsX, y);
}
}
}
else {
if (lastChild()) {
RenderObject* c = firstChild();
return lastChild()->positionForCoordinates(contentsX - toRenderBox(c)->x(), contentsY - toRenderBox(c)->y());
}
}
}
if (childrenInline()) {
if (!firstRootBox())
return VisiblePosition(n, 0, DOWNSTREAM);
if (contentsY < firstRootBox()->topOverflow() - verticalLineClickFudgeFactor)
{
InlineBox* closestBox = firstRootBox()->closestLeafChildForXPos(x);
if (closestBox)
return closestBox->object()->positionForCoordinates(contentsX, closestBox->m_y);
}
for (RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox()) {
if (root->nextRootBox())
bottom = root->nextRootBox()->topOverflow();
else
bottom = root->bottomOverflow() + verticalLineClickFudgeFactor;
if (contentsY < bottom && root->firstChild()) {
InlineBox* closestBox = root->closestLeafChildForXPos(x);
if (closestBox)
return closestBox->object()->positionForCoordinates(contentsX, closestBox->m_y);
}
}
if (lastRootBox())
{
InlineBox* closestBox = lastRootBox()->closestLeafChildForXPos(x);
if (closestBox) {
return closestBox->object()->positionForCoordinates(contentsX, closestBox->m_y);
}
}
return VisiblePosition(n, 0, DOWNSTREAM);
}
if (firstChildBox() && contentsY < firstChildBox()->y())
return VisiblePosition(n, 0, DOWNSTREAM);
for (RenderBox* renderer = firstChildBox(); renderer; renderer = renderer->nextSiblingBox()) {
if (renderer->height() == 0 || renderer->style()->visibility() != VISIBLE || renderer->isFloatingOrPositioned())
continue;
RenderBox* next = renderer->nextSiblingBox();
while (next && next->isFloatingOrPositioned())
next = next->nextSiblingBox();
if (next)
bottom = next->y();
else
bottom = top + scrollHeight();
if (contentsY >= renderer->y() && contentsY < bottom)
return renderer->positionForCoordinates(contentsX - renderer->x(), contentsY - renderer->y());
}
return RenderFlow::positionForCoordinates(x, y);
}
void RenderBlock::offsetForContents(int& tx, int& ty) const
{
if (hasOverflowClip())
m_layer->addScrolledContentOffset(tx, ty);
if (m_hasColumns) {
IntPoint contentsPoint(tx, ty);
adjustPointToColumnContents(contentsPoint);
tx = contentsPoint.x();
ty = contentsPoint.y();
}
}
int RenderBlock::availableWidth() const
{
if (m_hasColumns)
return desiredColumnWidth();
return contentWidth();
}
int RenderBlock::columnGap() const
{
if (style()->hasNormalColumnGap())
return style()->fontDescription().computedPixelSize(); return static_cast<int>(style()->columnGap());
}
void RenderBlock::calcColumnWidth()
{
unsigned desiredColumnCount = 1;
int desiredColumnWidth = contentWidth();
if (document()->printing() || (style()->hasAutoColumnCount() && style()->hasAutoColumnWidth())) {
setDesiredColumnCountAndWidth(desiredColumnCount, desiredColumnWidth);
return;
}
int availWidth = desiredColumnWidth;
int colGap = columnGap();
int colWidth = max(1, static_cast<int>(style()->columnWidth()));
int colCount = max(1, static_cast<int>(style()->columnCount()));
if (style()->hasAutoColumnWidth()) {
if ((colCount - 1) * colGap < availWidth) {
desiredColumnCount = colCount;
desiredColumnWidth = (availWidth - (desiredColumnCount - 1) * colGap) / desiredColumnCount;
} else if (colGap < availWidth) {
desiredColumnCount = availWidth / colGap;
desiredColumnWidth = (availWidth - (desiredColumnCount - 1) * colGap) / desiredColumnCount;
}
} else if (style()->hasAutoColumnCount()) {
if (colWidth < availWidth) {
desiredColumnCount = (availWidth + colGap) / (colWidth + colGap);
desiredColumnWidth = (availWidth - (desiredColumnCount - 1) * colGap) / desiredColumnCount;
}
} else {
if (colCount * colWidth + (colCount - 1) * colGap <= availWidth) {
desiredColumnCount = colCount;
desiredColumnWidth = colWidth;
} else if (colWidth < availWidth) {
desiredColumnCount = (availWidth + colGap) / (colWidth + colGap);
desiredColumnWidth = (availWidth - (desiredColumnCount - 1) * colGap) / desiredColumnCount;
}
}
setDesiredColumnCountAndWidth(desiredColumnCount, desiredColumnWidth);
}
void RenderBlock::setDesiredColumnCountAndWidth(int count, int actualWidth)
{
if (count == 1) {
if (m_hasColumns) {
delete gColumnInfoMap->take(this);
m_hasColumns = false;
}
} else {
ColumnInfo* info;
if (m_hasColumns)
info = gColumnInfoMap->get(this);
else {
if (!gColumnInfoMap)
gColumnInfoMap = new ColumnInfoMap;
info = new ColumnInfo;
gColumnInfoMap->add(this, info);
m_hasColumns = true;
}
info->m_desiredColumnCount = count;
info->m_desiredColumnWidth = actualWidth;
}
}
int RenderBlock::desiredColumnWidth() const
{
if (!m_hasColumns)
return contentWidth();
return gColumnInfoMap->get(this)->m_desiredColumnWidth;
}
unsigned RenderBlock::desiredColumnCount() const
{
if (!m_hasColumns)
return 1;
return gColumnInfoMap->get(this)->m_desiredColumnCount;
}
Vector<IntRect>* RenderBlock::columnRects() const
{
if (!m_hasColumns)
return 0;
return &gColumnInfoMap->get(this)->m_columnRects;
}
int RenderBlock::layoutColumns(int endOfContent)
{
if (!m_hasColumns)
return -1;
ColumnInfo* info = gColumnInfoMap->get(this);
int desiredColumnWidth = info->m_desiredColumnWidth;
int desiredColumnCount = info->m_desiredColumnCount;
Vector<IntRect>* columnRects = &info->m_columnRects;
bool computeIntrinsicHeight = (endOfContent == -1);
int availableHeight = contentHeight();
int colHeight = computeIntrinsicHeight ? availableHeight / desiredColumnCount : availableHeight;
int columnSlop = lineHeight(false) / 2;
int remainingSlopSpace = columnSlop * desiredColumnCount;
if (computeIntrinsicHeight)
colHeight += columnSlop;
int colGap = columnGap();
columnRects->clear();
RenderView* v = view();
int left = borderLeft() + paddingLeft();
int top = borderTop() + paddingTop();
int currX = style()->direction() == LTR ? borderLeft() + paddingLeft() : borderLeft() + paddingLeft() + contentWidth() - desiredColumnWidth;
int currY = top;
unsigned colCount = desiredColumnCount;
int maxColBottom = borderTop() + paddingTop();
int contentBottom = top + availableHeight;
for (unsigned i = 0; i < colCount; i++) {
if (computeIntrinsicHeight && i == colCount - 1)
colHeight = availableHeight;
IntRect colRect(currX, top, desiredColumnWidth, colHeight);
IntRect pageRect(left, currY, desiredColumnWidth, colHeight);
v->setPrintRect(pageRect);
v->setTruncatedAt(currY + colHeight);
GraphicsContext context((PlatformGraphicsContext*)0);
RenderObject::PaintInfo paintInfo(&context, pageRect, PaintPhaseForeground, false, 0, 0);
m_hasColumns = false;
paintObject(paintInfo, 0, 0);
m_hasColumns = true;
int adjustedBottom = v->bestTruncatedAt();
if (adjustedBottom <= currY)
adjustedBottom = currY + colHeight;
colRect.setHeight(adjustedBottom - currY);
if (computeIntrinsicHeight) {
int lostSpace = colHeight - colRect.height();
if (lostSpace > remainingSlopSpace) {
int spaceToRedistribute = lostSpace - remainingSlopSpace;
int remainingColumns = colCount - i + 1;
colHeight += spaceToRedistribute / remainingColumns;
}
remainingSlopSpace = max(0, remainingSlopSpace - lostSpace);
}
if (style()->direction() == LTR)
currX += desiredColumnWidth + colGap;
else
currX -= (desiredColumnWidth + colGap);
currY += colRect.height();
availableHeight -= colRect.height();
maxColBottom = max(colRect.bottom(), maxColBottom);
columnRects->append(colRect);
if (currY < endOfContent && i == colCount - 1 && (computeIntrinsicHeight || contentHeight()))
colCount++;
}
m_overflowWidth = max(width(), currX - colGap);
m_overflowLeft = min(0, currX + desiredColumnWidth + colGap);
m_overflowHeight = maxColBottom;
int toAdd = borderBottom() + paddingBottom() + horizontalScrollbarHeight();
if (computeIntrinsicHeight)
setHeight(m_overflowHeight + toAdd);
v->setPrintRect(IntRect());
v->setTruncatedAt(0);
ASSERT(colCount == columnRects->size());
return contentBottom;
}
void RenderBlock::adjustPointToColumnContents(IntPoint& point) const
{
if (!m_hasColumns)
return;
Vector<IntRect>* colRects = columnRects();
int colGap = columnGap();
int leftGap = colGap / 2;
IntPoint columnPoint(colRects->at(0).location());
int yOffset = 0;
for (unsigned i = 0; i < colRects->size(); i++) {
IntRect colRect = colRects->at(i);
IntRect gapAndColumnRect(colRect.x() - leftGap, colRect.y(), colRect.width() + colGap, colRect.height());
if (gapAndColumnRect.contains(point)) {
point.move(columnPoint.x() - colRect.x(), yOffset);
return;
}
yOffset += colRect.height();
}
}
void RenderBlock::adjustRectForColumns(IntRect& r) const
{
if (!m_hasColumns)
return;
Vector<IntRect>* colRects = columnRects();
IntRect result;
int currXOffset = 0;
int currYOffset = 0;
int colGap = columnGap();
for (unsigned i = 0; i < colRects->size(); i++) {
IntRect colRect = colRects->at(i);
IntRect repaintRect = r;
repaintRect.move(currXOffset, currYOffset);
repaintRect.intersect(colRect);
result.unite(repaintRect);
if (style()->direction() == LTR)
currXOffset += colRect.width() + colGap;
else
currXOffset -= (colRect.width() + colGap);
currYOffset -= colRect.height();
}
r = result;
}
void RenderBlock::calcPrefWidths()
{
ASSERT(prefWidthsDirty());
updateFirstLetter();
if (!isTableCell() && style()->width().isFixed() && style()->width().value() > 0)
m_minPrefWidth = m_maxPrefWidth = calcContentBoxWidth(style()->width().value());
else {
m_minPrefWidth = 0;
m_maxPrefWidth = 0;
if (childrenInline())
calcInlinePrefWidths();
else
calcBlockPrefWidths();
m_maxPrefWidth = max(m_minPrefWidth, m_maxPrefWidth);
if (!style()->autoWrap() && childrenInline()) {
m_minPrefWidth = m_maxPrefWidth;
if (m_layer && m_layer->marquee() && m_layer->marquee()->isHorizontal())
m_minPrefWidth = 0;
}
if (isTableCell()) {
Length w = static_cast<const RenderTableCell*>(this)->styleOrColWidth();
if (w.isFixed() && w.value() > 0)
m_maxPrefWidth = max(m_minPrefWidth, calcContentBoxWidth(w.value()));
}
}
if (style()->minWidth().isFixed() && style()->minWidth().value() > 0) {
m_maxPrefWidth = max(m_maxPrefWidth, calcContentBoxWidth(style()->minWidth().value()));
m_minPrefWidth = max(m_minPrefWidth, calcContentBoxWidth(style()->minWidth().value()));
}
if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength) {
m_maxPrefWidth = min(m_maxPrefWidth, calcContentBoxWidth(style()->maxWidth().value()));
m_minPrefWidth = min(m_minPrefWidth, calcContentBoxWidth(style()->maxWidth().value()));
}
int toAdd = 0;
toAdd = borderLeft() + borderRight() + paddingLeft() + paddingRight();
m_minPrefWidth += toAdd;
m_maxPrefWidth += toAdd;
setPrefWidthsDirty(false);
}
struct InlineMinMaxIterator
{
RenderObject* parent;
RenderObject* current;
bool endOfInline;
InlineMinMaxIterator(RenderObject* p, bool end = false)
:parent(p), current(p), endOfInline(end) {}
RenderObject* next();
};
RenderObject* InlineMinMaxIterator::next()
{
RenderObject* result = 0;
bool oldEndOfInline = endOfInline;
endOfInline = false;
while (current || current == parent) {
if (!oldEndOfInline &&
(current == parent ||
(!current->isFloating() && !current->isReplaced() && !current->isPositioned())))
result = current->firstChild();
if (!result) {
if (!oldEndOfInline && current->isRenderInline()) {
result = current;
endOfInline = true;
break;
}
while (current && current != parent) {
result = current->nextSibling();
if (result) break;
current = current->parent();
if (current && current != parent && current->isRenderInline()) {
result = current;
endOfInline = true;
break;
}
}
}
if (!result)
break;
if (!result->isPositioned() && (result->isText() || result->isFloating() || result->isReplaced() || result->isRenderInline()))
break;
current = result;
result = 0;
}
current = result;
return current;
}
static int getBPMWidth(int childValue, Length cssUnit)
{
if (cssUnit.type() != Auto)
return (cssUnit.isFixed() ? cssUnit.value() : childValue);
return 0;
}
static int getBorderPaddingMargin(const RenderBox* child, bool endOfInline)
{
RenderStyle* cstyle = child->style();
int result = 0;
bool leftSide = (cstyle->direction() == LTR) ? !endOfInline : endOfInline;
result += getBPMWidth((leftSide ? child->marginLeft() : child->marginRight()),
(leftSide ? cstyle->marginLeft() :
cstyle->marginRight()));
result += getBPMWidth((leftSide ? child->paddingLeft() : child->paddingRight()),
(leftSide ? cstyle->paddingLeft() :
cstyle->paddingRight()));
result += leftSide ? child->borderLeft() : child->borderRight();
return result;
}
static inline void stripTrailingSpace(int& inlineMax, int& inlineMin,
RenderObject* trailingSpaceChild)
{
if (trailingSpaceChild && trailingSpaceChild->isText()) {
RenderText* t = toRenderText(trailingSpaceChild);
const UChar space = ' ';
const Font& font = t->style()->font(); int spaceWidth = font.width(TextRun(&space, 1));
inlineMax -= spaceWidth + font.wordSpacing();
if (inlineMin > inlineMax)
inlineMin = inlineMax;
}
}
void RenderBlock::calcInlinePrefWidths()
{
int inlineMax = 0;
int inlineMin = 0;
int cw = containingBlock()->contentWidth();
bool stripFrontSpaces = true;
RenderObject* trailingSpaceChild = 0;
bool allowImagesToBreak = !style()->htmlHacks() || !isTableCell() || !style()->width().isIntrinsicOrAuto();
bool autoWrap, oldAutoWrap;
autoWrap = oldAutoWrap = style()->autoWrap();
InlineMinMaxIterator childIterator(this);
bool addedTextIndent = false; RenderObject* prevFloat = 0;
RenderObject* previousLeaf = 0;
while (RenderObject* child = childIterator.next()) {
autoWrap = child->isReplaced() ? child->parent()->style()->autoWrap() :
child->style()->autoWrap();
if (!child->isBR()) {
RenderStyle* cstyle = child->style();
int childMin = 0;
int childMax = 0;
if (!child->isText()) {
if (child->isRenderInline()) {
int bpm = getBorderPaddingMargin(static_cast<RenderFlow*>(child), childIterator.endOfInline);
childMin += bpm;
childMax += bpm;
inlineMin += childMin;
inlineMax += childMax;
child->setPrefWidthsDirty(false);
} else {
int margins = 0;
Length leftMargin = cstyle->marginLeft();
Length rightMargin = cstyle->marginRight();
if (leftMargin.isFixed())
margins += leftMargin.value();
if (rightMargin.isFixed())
margins += rightMargin.value();
childMin += margins;
childMax += margins;
}
}
if (!child->isRenderInline() && !child->isText()) {
childMin += child->minPrefWidth();
childMax += child->maxPrefWidth();
bool clearPreviousFloat;
if (child->isFloating()) {
clearPreviousFloat = (prevFloat
&& (prevFloat->style()->floating() == FLEFT && (child->style()->clear() & CLEFT)
|| prevFloat->style()->floating() == FRIGHT && (child->style()->clear() & CRIGHT)));
prevFloat = child;
} else
clearPreviousFloat = false;
bool canBreakReplacedElement = !child->isImage() || allowImagesToBreak;
if (canBreakReplacedElement && (autoWrap || oldAutoWrap) || clearPreviousFloat) {
m_minPrefWidth = max(inlineMin, m_minPrefWidth);
inlineMin = 0;
}
if (clearPreviousFloat) {
m_maxPrefWidth = max(inlineMax, m_maxPrefWidth);
inlineMax = 0;
}
int ti = 0;
if (!addedTextIndent) {
addedTextIndent = true;
ti = style()->textIndent().calcMinValue(cw);
childMin+=ti;
childMax+=ti;
}
inlineMax += childMax;
if (!autoWrap || !canBreakReplacedElement) {
if (child->isFloating())
m_minPrefWidth = max(childMin, m_minPrefWidth);
else
inlineMin += childMin;
} else {
m_minPrefWidth = max(childMin, m_minPrefWidth);
inlineMin = 0;
}
if (!child->isFloating()) {
stripFrontSpaces = false;
trailingSpaceChild = 0;
}
} else if (child->isText()) {
RenderText* t = toRenderText(child);
if (t->isWordBreak()) {
m_minPrefWidth = max(inlineMin, m_minPrefWidth);
inlineMin = 0;
continue;
}
bool hasBreakableChar, hasBreak;
int beginMin, endMin;
bool beginWS, endWS;
int beginMax, endMax;
t->trimmedPrefWidths(inlineMax, beginMin, beginWS, endMin, endWS,
hasBreakableChar, hasBreak, beginMax, endMax,
childMin, childMax, stripFrontSpaces);
if (!hasBreak && childMax == 0) {
if (autoWrap && (beginWS || endWS)) {
m_minPrefWidth = max(inlineMin, m_minPrefWidth);
inlineMin = 0;
}
continue;
}
if (stripFrontSpaces)
trailingSpaceChild = child;
else
trailingSpaceChild = 0;
int ti = 0;
if (!addedTextIndent) {
addedTextIndent = true;
ti = style()->textIndent().calcMinValue(cw);
childMin+=ti; beginMin += ti;
childMax+=ti; beginMax += ti;
}
if (!hasBreakableChar) {
inlineMin += childMin;
} else {
if (beginWS)
m_minPrefWidth = max(inlineMin, m_minPrefWidth);
else {
inlineMin += beginMin;
m_minPrefWidth = max(inlineMin, m_minPrefWidth);
childMin -= ti;
}
inlineMin = childMin;
if (endWS) {
m_minPrefWidth = max(inlineMin, m_minPrefWidth);
inlineMin = 0;
} else {
m_minPrefWidth = max(inlineMin, m_minPrefWidth);
inlineMin = endMin;
}
}
if (hasBreak) {
inlineMax += beginMax;
m_maxPrefWidth = max(inlineMax, m_maxPrefWidth);
m_maxPrefWidth = max(childMax, m_maxPrefWidth);
inlineMax = endMax;
} else
inlineMax += childMax;
}
} else {
m_minPrefWidth = max(inlineMin, m_minPrefWidth);
m_maxPrefWidth = max(inlineMax, m_maxPrefWidth);
inlineMin = inlineMax = 0;
stripFrontSpaces = true;
trailingSpaceChild = 0;
}
oldAutoWrap = autoWrap;
if (!child->isRenderInline())
previousLeaf = child;
}
if (style()->collapseWhiteSpace())
stripTrailingSpace(inlineMax, inlineMin, trailingSpaceChild);
m_minPrefWidth = max(inlineMin, m_minPrefWidth);
m_maxPrefWidth = max(inlineMax, m_maxPrefWidth);
}
#define BLOCK_MAX_WIDTH 15000
void RenderBlock::calcBlockPrefWidths()
{
bool nowrap = style()->whiteSpace() == NOWRAP;
RenderObject *child = firstChild();
int floatLeftWidth = 0, floatRightWidth = 0;
while (child) {
if (child->isPositioned()) {
child = child->nextSibling();
continue;
}
if (child->isFloating() || child->avoidsFloats()) {
int floatTotalWidth = floatLeftWidth + floatRightWidth;
if (child->style()->clear() & CLEFT) {
m_maxPrefWidth = max(floatTotalWidth, m_maxPrefWidth);
floatLeftWidth = 0;
}
if (child->style()->clear() & CRIGHT) {
m_maxPrefWidth = max(floatTotalWidth, m_maxPrefWidth);
floatRightWidth = 0;
}
}
Length ml = child->style()->marginLeft();
Length mr = child->style()->marginRight();
int margin = 0, marginLeft = 0, marginRight = 0;
if (ml.isFixed())
marginLeft += ml.value();
if (mr.isFixed())
marginRight += mr.value();
margin = marginLeft + marginRight;
int w = child->minPrefWidth() + margin;
m_minPrefWidth = max(w, m_minPrefWidth);
if (nowrap && !child->isTable())
m_maxPrefWidth = max(w, m_maxPrefWidth);
w = child->maxPrefWidth() + margin;
if (!child->isFloating()) {
if (child->avoidsFloats()) {
int maxLeft = marginLeft > 0 ? max(floatLeftWidth, marginLeft) : floatLeftWidth + marginLeft;
int maxRight = marginRight > 0 ? max(floatRightWidth, marginRight) : floatRightWidth + marginRight;
w = child->maxPrefWidth() + maxLeft + maxRight;
w = max(w, floatLeftWidth + floatRightWidth);
}
else
m_maxPrefWidth = max(floatLeftWidth + floatRightWidth, m_maxPrefWidth);
floatLeftWidth = floatRightWidth = 0;
}
if (child->isFloating()) {
if (style()->floating() == FLEFT)
floatLeftWidth += w;
else
floatRightWidth += w;
} else
m_maxPrefWidth = max(w, m_maxPrefWidth);
if (style()->htmlHacks() && child->style()->width().isPercent() &&
!isTableCell() && child->isTable() && m_maxPrefWidth < BLOCK_MAX_WIDTH) {
RenderBlock* cb = containingBlock();
while (!cb->isRenderView() && !cb->isTableCell())
cb = cb->containingBlock();
if (!cb->isTableCell())
m_maxPrefWidth = BLOCK_MAX_WIDTH;
}
child = child->nextSibling();
}
m_minPrefWidth = max(0, m_minPrefWidth);
m_maxPrefWidth = max(0, m_maxPrefWidth);
m_maxPrefWidth = max(floatLeftWidth + floatRightWidth, m_maxPrefWidth);
}
bool RenderBlock::hasLineIfEmpty() const
{
return element() && (element()->isContentEditable() && element()->rootEditableElement() == element() ||
element()->isShadowNode() && element()->shadowParentNode()->hasTagName(inputTag));
}
int RenderBlock::lineHeight(bool b, bool isRootLineBox) const
{
if (isReplaced() && !isRootLineBox)
return height() + marginTop() + marginBottom();
return RenderFlow::lineHeight(b, isRootLineBox);
}
int RenderBlock::baselinePosition(bool b, bool isRootLineBox) const
{
if (isReplaced() && !isRootLineBox) {
if (style()->hasAppearance() && !theme()->isControlContainer(style()->appearance()))
return theme()->baselinePosition(this);
int baselinePos = (m_layer && (m_layer->marquee() || m_layer->verticalScrollbar() || m_layer->scrollYOffset() != 0)) ? -1 : getBaselineOfLastLineBox();
if (baselinePos != -1 && baselinePos <= borderTop() + paddingTop() + contentHeight())
return marginTop() + baselinePos;
return height() + marginTop() + marginBottom();
}
return RenderFlow::baselinePosition(b, isRootLineBox);
}
int RenderBlock::getBaselineOfFirstLineBox() const
{
if (!isBlockFlow())
return RenderFlow::getBaselineOfFirstLineBox();
if (childrenInline()) {
if (firstLineBox())
return firstLineBox()->yPos() + firstLineBox()->baseline();
else
return -1;
}
else {
for (RenderBox* curr = firstChildBox(); curr; curr = curr->nextSiblingBox()) {
if (!curr->isFloatingOrPositioned()) {
int result = curr->getBaselineOfFirstLineBox();
if (result != -1)
return curr->y() + result; }
}
}
return -1;
}
int RenderBlock::getBaselineOfLastLineBox() const
{
if (!isBlockFlow())
return RenderFlow::getBaselineOfLastLineBox();
if (childrenInline()) {
if (!firstLineBox() && hasLineIfEmpty())
return RenderFlow::baselinePosition(true, true) + borderTop() + paddingTop();
if (lastLineBox())
return lastLineBox()->yPos() + lastLineBox()->baseline();
return -1;
}
else {
bool haveNormalFlowChild = false;
for (RenderBox* curr = lastChildBox(); curr; curr = curr->previousSiblingBox()) {
if (!curr->isFloatingOrPositioned()) {
haveNormalFlowChild = true;
int result = curr->getBaselineOfLastLineBox();
if (result != -1)
return curr->y() + result; }
}
if (!haveNormalFlowChild && hasLineIfEmpty())
return RenderFlow::baselinePosition(true, true) + borderTop() + paddingTop();
}
return -1;
}
bool RenderBlock::containsNonZeroBidiLevel() const
{
for (RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox()) {
for (InlineBox* box = root->firstLeafChild(); box; box = box->nextLeafChild()) {
if (box->bidiLevel())
return true;
}
}
return false;
}
RenderBlock* RenderBlock::firstLineBlock() const
{
RenderBlock* firstLineBlock = const_cast<RenderBlock*>(this);
bool hasPseudo = false;
while (true) {
hasPseudo = firstLineBlock->style()->hasPseudoStyle(RenderStyle::FIRST_LINE);
if (hasPseudo)
break;
RenderObject* parentBlock = firstLineBlock->parent();
if (firstLineBlock->isReplaced() || firstLineBlock->isFloating() ||
!parentBlock || parentBlock->firstChild() != firstLineBlock || !parentBlock->isBlockFlow())
break;
ASSERT(parentBlock->isRenderBlock());
firstLineBlock = static_cast<RenderBlock*>(parentBlock);
}
if (!hasPseudo)
return 0;
return firstLineBlock;
}
void RenderBlock::updateFirstLetter()
{
if (!document()->usesFirstLetterRules())
return;
if (style()->styleType() == RenderStyle::FIRST_LETTER)
return;
RenderObject* firstLetterBlock = this;
bool hasPseudoStyle = false;
while (true) {
hasPseudoStyle = firstLetterBlock->style()->hasPseudoStyle(RenderStyle::FIRST_LETTER)
&& firstLetterBlock->canHaveChildren();
if (hasPseudoStyle)
break;
RenderObject* parentBlock = firstLetterBlock->parent();
if (firstLetterBlock->isReplaced() || !parentBlock || parentBlock->firstChild() != firstLetterBlock ||
!parentBlock->isBlockFlow())
break;
firstLetterBlock = parentBlock;
}
if (!hasPseudoStyle)
return;
RenderObject* currChild = firstLetterBlock->firstChild();
while (currChild && currChild->needsLayout() && (!currChild->isReplaced() || currChild->isFloatingOrPositioned()) && !currChild->isText()) {
if (currChild->isFloatingOrPositioned()) {
if (currChild->style()->styleType() == RenderStyle::FIRST_LETTER)
break;
currChild = currChild->nextSibling();
} else
currChild = currChild->firstChild();
}
while (currChild && currChild->isListMarker())
currChild = currChild->nextSibling();
if (!currChild)
return;
RenderObject* firstLetterContainer = currChild->parent();
if (currChild->style()->styleType() == RenderStyle::FIRST_LETTER) {
RenderStyle* pseudo = firstLetterBlock->getCachedPseudoStyle(RenderStyle::FIRST_LETTER,
firstLetterContainer->firstLineStyle());
currChild->setStyle(pseudo);
for (RenderObject* genChild = currChild->firstChild(); genChild; genChild = genChild->nextSibling()) {
if (genChild->isText())
genChild->setStyle(pseudo);
}
return;
}
if (currChild->isText() && !currChild->isBR() && currChild->parent()->style()->styleType() != RenderStyle::FIRST_LETTER) {
view()->disableLayoutState();
RenderText* textObj = toRenderText(currChild);
RenderStyle* pseudoStyle = firstLetterBlock->getCachedPseudoStyle(RenderStyle::FIRST_LETTER,
firstLetterContainer->firstLineStyle());
pseudoStyle->setDisplay( pseudoStyle->isFloating() ? BLOCK : INLINE);
pseudoStyle->setPosition( StaticPosition );
RenderObject* firstLetter = RenderFlow::createAnonymousFlow(document(), pseudoStyle); firstLetterContainer->addChild(firstLetter, currChild);
RefPtr<StringImpl> oldText = textObj->originalText();
ASSERT(oldText);
if (oldText && oldText->length() > 0) {
unsigned int length = 0;
while (length < oldText->length() && (isSpaceOrNewline((*oldText)[length]) || Unicode::isPunct((*oldText)[length])))
length++;
length++;
RenderTextFragment* remainingText =
new (renderArena()) RenderTextFragment(textObj->node(), oldText.get(), length, oldText->length() - length);
remainingText->setStyle(textObj->style());
if (remainingText->element())
remainingText->element()->setRenderer(remainingText);
RenderObject* nextObj = textObj->nextSibling();
firstLetterContainer->removeChild(textObj);
firstLetterContainer->addChild(remainingText, nextObj);
remainingText->setFirstLetter(firstLetter);
RenderTextFragment* letter =
new (renderArena()) RenderTextFragment(remainingText->node(), oldText.get(), 0, length);
RefPtr<RenderStyle> newStyle = RenderStyle::create();
newStyle->inheritFrom(pseudoStyle);
letter->setStyle(newStyle.release());
firstLetter->addChild(letter);
textObj->destroy();
}
view()->enableLayoutState();
}
}
bool RenderBlock::inRootBlockContext() const
{
if (isTableCell() || isFloatingOrPositioned() || hasOverflowClip())
return false;
if (isRoot() || isRenderView())
return true;
return containingBlock()->inRootBlockContext();
}
static bool shouldCheckLines(RenderObject* obj)
{
return !obj->isFloatingOrPositioned() && !obj->isRunIn() &&
obj->isBlockFlow() && obj->style()->height().isAuto() &&
(!obj->isFlexibleBox() || obj->style()->boxOrient() == VERTICAL);
}
static RootInlineBox* getLineAtIndex(RenderBlock* block, int i, int& count)
{
if (block->style()->visibility() == VISIBLE) {
if (block->childrenInline()) {
for (RootInlineBox* box = block->firstRootBox(); box; box = box->nextRootBox()) {
if (count++ == i)
return box;
}
}
else {
for (RenderObject* obj = block->firstChild(); obj; obj = obj->nextSibling()) {
if (shouldCheckLines(obj)) {
RootInlineBox *box = getLineAtIndex(static_cast<RenderBlock*>(obj), i, count);
if (box)
return box;
}
}
}
}
return 0;
}
static int getHeightForLineCount(RenderBlock* block, int l, bool includeBottom, int& count)
{
if (block->style()->visibility() == VISIBLE) {
if (block->childrenInline()) {
for (RootInlineBox* box = block->firstRootBox(); box; box = box->nextRootBox()) {
if (++count == l)
return box->bottomOverflow() + (includeBottom ? (block->borderBottom() + block->paddingBottom()) : 0);
}
}
else {
RenderBox* normalFlowChildWithoutLines = 0;
for (RenderBox* obj = block->firstChildBox(); obj; obj = obj->nextSiblingBox()) {
if (shouldCheckLines(obj)) {
int result = getHeightForLineCount(static_cast<RenderBlock*>(obj), l, false, count);
if (result != -1)
return result + obj->y() + (includeBottom ? (block->borderBottom() + block->paddingBottom()) : 0);
}
else if (!obj->isFloatingOrPositioned() && !obj->isRunIn())
normalFlowChildWithoutLines = obj;
}
if (normalFlowChildWithoutLines && l == 0)
return normalFlowChildWithoutLines->y() + normalFlowChildWithoutLines->height();
}
}
return -1;
}
RootInlineBox* RenderBlock::lineAtIndex(int i)
{
int count = 0;
return getLineAtIndex(this, i, count);
}
int RenderBlock::lineCount()
{
int count = 0;
if (style()->visibility() == VISIBLE) {
if (childrenInline())
for (RootInlineBox* box = firstRootBox(); box; box = box->nextRootBox())
count++;
else
for (RenderObject* obj = firstChild(); obj; obj = obj->nextSibling())
if (shouldCheckLines(obj))
count += static_cast<RenderBlock*>(obj)->lineCount();
}
return count;
}
int RenderBlock::heightForLineCount(int l)
{
int count = 0;
return getHeightForLineCount(this, l, true, count);
}
void RenderBlock::adjustForBorderFit(int x, int& left, int& right) const
{
if (style()->visibility() == VISIBLE) {
if (childrenInline()) {
for (RootInlineBox* box = firstRootBox(); box; box = box->nextRootBox()) {
if (box->firstChild())
left = min(left, x + box->firstChild()->xPos());
if (box->lastChild())
right = max(right, x + box->lastChild()->xPos() + box->lastChild()->width());
}
}
else {
for (RenderBox* obj = firstChildBox(); obj; obj = obj->nextSiblingBox()) {
if (!obj->isFloatingOrPositioned()) {
if (obj->isBlockFlow() && !obj->hasOverflowClip())
static_cast<RenderBlock*>(obj)->adjustForBorderFit(x + obj->x(), left, right);
else if (obj->style()->visibility() == VISIBLE) {
left = min(left, x + obj->x());
right = max(right, x + obj->x() + obj->width());
}
}
}
}
if (m_floatingObjects) {
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for (; (r = it.current()); ++it) {
if (r->m_shouldPaint) {
int floatLeft = r->m_left - r->m_renderer->x() + r->m_renderer->marginLeft();
int floatRight = floatLeft + r->m_renderer->width();
left = min(left, floatLeft);
right = max(right, floatRight);
}
}
}
}
}
void RenderBlock::borderFitAdjust(int& x, int& w) const
{
if (style()->borderFit() == BorderFitBorder)
return;
int left = INT_MAX;
int right = INT_MIN;
int oldWidth = w;
adjustForBorderFit(0, left, right);
if (left != INT_MAX) {
left -= (borderLeft() + paddingLeft());
if (left > 0) {
x += left;
w -= left;
}
}
if (right != INT_MIN) {
right += (borderRight() + paddingRight());
if (right < oldWidth)
w -= (oldWidth - right);
}
}
void RenderBlock::clearTruncation()
{
if (style()->visibility() == VISIBLE) {
if (childrenInline() && hasMarkupTruncation()) {
setHasMarkupTruncation(false);
for (RootInlineBox* box = firstRootBox(); box; box = box->nextRootBox())
box->clearTruncation();
}
else
for (RenderObject* obj = firstChild(); obj; obj = obj->nextSibling())
if (shouldCheckLines(obj))
static_cast<RenderBlock*>(obj)->clearTruncation();
}
}
void RenderBlock::setMaxTopMargins(int pos, int neg)
{
if (!m_maxMargin) {
if (pos == MaxMargin::topPosDefault(this) && neg == MaxMargin::topNegDefault(this))
return;
m_maxMargin = new MaxMargin(this);
}
m_maxMargin->m_topPos = pos;
m_maxMargin->m_topNeg = neg;
}
void RenderBlock::setMaxBottomMargins(int pos, int neg)
{
if (!m_maxMargin) {
if (pos == MaxMargin::bottomPosDefault(this) && neg == MaxMargin::bottomNegDefault(this))
return;
m_maxMargin = new MaxMargin(this);
}
m_maxMargin->m_bottomPos = pos;
m_maxMargin->m_bottomNeg = neg;
}
const char* RenderBlock::renderName() const
{
if (isBody())
return "RenderBody";
if (isFloating())
return "RenderBlock (floating)";
if (isPositioned())
return "RenderBlock (positioned)";
if (isAnonymousBlock())
return "RenderBlock (anonymous)";
else if (isAnonymous())
return "RenderBlock (generated)";
if (isRelPositioned())
return "RenderBlock (relative positioned)";
if (isRunIn())
return "RenderBlock (run-in)";
return "RenderBlock";
}
#define ParagraphMinWordCount 6
#define ParagraphMinLineCount 2
#define ParagraphMaxWidth 0.9
inline static bool isVisibleRenderText(RenderObject *renderer)
{
if (!renderer->isText())
return false;
RenderText *renderText = toRenderText(renderer);
IntRect boundingBox = renderText->linesBoundingBox();
return boundingBox.width() != 0 && boundingBox.height() != 0 && !renderText->text()->containsOnlyWhitespace();
}
inline static bool resizeTextPermitted(RenderObject *render) {
RenderObject* renderer = render->parent();
while (renderer) {
if (renderer->node() && renderer->node()->isHTMLElement() && !renderer->node()->isShadowNode())
return !static_cast<HTMLElement*>(renderer->node())->hasTagName(inputTag);
renderer = renderer->parent();
}
return true;
}
int RenderBlock::immediateLineCount()
{
int count = 0;
if (style()->visibility() == VISIBLE) {
if (childrenInline())
for (RootInlineBox* box = firstRootBox(); box; box = box->nextRootBox())
count++;
else
for (RenderObject* obj = firstChild(); obj; obj = obj->nextSibling())
if (obj->isListItem())
count += static_cast<RenderBlock*>(obj)->lineCount();
}
return count;
}
static bool includeNonBlocksOrListItems(const RenderObject *render)
{
return !render->isRenderBlock() || render->isListItem();
}
#define ONE_LINE_TEXT_MULTIPLIER (MAX((1.0f/log10f(specifiedSize) * 1.7f),1.0f))
#define TEXT_MULTIPLIER (MAX((1.0f/log10f(specifiedSize) * 1.95f),1.0f))
void RenderBlock::adjustComputedFontSizes(float size, float visibleWidth)
{
if (visibleWidth >= width())
return;
unsigned lineCount;
if (m_lineCountForTextAutosizing == NOT_SET) {
int count = immediateLineCount();
if (count == 0)
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, includeNonBlocksOrListItems); descendent; descendent = descendent->traverseNext(this, includeNonBlocksOrListItems)) {
if (isVisibleRenderText(descendent) && resizeTextPermitted(descendent)) {
RenderText *text = static_cast<RenderText *>(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;
if (lineCount == ONE_LINE)
candidateNewSize = roundf(MIN(minFontSize, specifiedSize * ONE_LINE_TEXT_MULTIPLIER));
else
candidateNewSize = roundf(MIN(minFontSize, specifiedSize * TEXT_MULTIPLIER));
if (candidateNewSize > specifiedSize && candidateNewSize != fontDescription.computedSize() && text->element() && (!oldStyle || oldStyle->textSizeAdjust().isAuto()))
document()->addAutoSizingNode (text->element(), candidateNewSize);
}
}
}
}
}