#include "config.h"
#include "RenderBlock.h"
#include "Document.h"
#include "Element.h"
#include "Frame.h"
#include "GraphicsContext.h"
#include "InlineTextBox.h"
#include "RenderTableCell.h"
#include "RenderTextFragment.h"
#include "SelectionController.h"
#include "HTMLNames.h"
#include "RenderView.h"
#include "RenderTheme.h"
#include "TextStream.h"
using namespace std;
namespace WebCore {
const int verticalLineClickFudgeFactor= 3;
using namespace HTMLNames;
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;
}
enum LineCount
{
NOT_SET = 0, NO_LINE = 1, ONE_LINE = 2, MULTI_LINE = 3
};
RenderBlock::RenderBlock(Node* node)
:RenderFlow(node)
{
m_childrenInline = true;
m_floatingObjects = 0;
m_positionedObjects = 0;
m_firstLine = false;
m_hasMarkupTruncation = false;
m_selectionState = SelectionNone;
m_clearStatus = CNONE;
m_maxTopPosMargin = m_maxTopNegMargin = m_maxBottomPosMargin = m_maxBottomNegMargin = 0;
m_topMarginQuirk = m_bottomMarginQuirk = false;
m_overflowHeight = m_overflowWidth = 0;
m_overflowLeft = m_overflowTop = 0;
m_tabWidth = -1;
m_widthForTextAutosizing = -1;
m_lineCountForTextAutosizing = NOT_SET;
}
RenderBlock::~RenderBlock()
{
delete m_floatingObjects;
delete m_positionedObjects;
}
void RenderBlock::setStyle(RenderStyle* _style)
{
setReplaced(_style->isDisplayReplacedType());
RenderFlow::setStyle(_style);
RenderObject *child = firstChild();
while (child != 0)
{
if (child->isAnonymousBlock())
{
RenderStyle* newStyle = new (renderArena()) RenderStyle();
newStyle->inheritFrom(style());
newStyle->setDisplay(BLOCK);
child->setStyle(newStyle);
}
child = child->nextSibling();
}
m_lineHeight = -1;
m_tabWidth = -1;
updatePseudoChild(RenderStyle::BEFORE);
updatePseudoChild(RenderStyle::AFTER);
}
void RenderBlock::addChildToFlow(RenderObject* newChild, RenderObject* beforeChild)
{
if (!beforeChild && lastChild() && lastChild()->style()->styleType() == RenderStyle::AFTER)
beforeChild = lastChild();
bool madeBoxesNonInline = false;
if (beforeChild && beforeChild->parent() != this) {
ASSERT(beforeChild->parent());
ASSERT(beforeChild->parent()->isAnonymousBlock());
if (newChild->isInline()) {
beforeChild->parent()->addChild(newChild,beforeChild);
return;
}
else if (beforeChild->parent()->firstChild() != beforeChild)
return beforeChild->parent()->addChild(newChild, beforeChild);
else
return addChildToFlow(newChild, beforeChild->parent());
}
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())
{
if (newChild->isInline()) {
if (beforeChild) {
if (beforeChild->previousSibling() && beforeChild->previousSibling()->isAnonymousBlock()) {
beforeChild->previousSibling()->addChild(newChild);
return;
}
}
else {
if (m_last && m_last->isAnonymousBlock()) {
m_last->addChild(newChild);
return;
}
}
RenderBlock* newBox = createAnonymousBlock();
RenderContainer::addChild(newBox,beforeChild);
newBox->addChild(newChild);
return;
}
}
RenderContainer::addChild(newChild,beforeChild);
if ( madeBoxesNonInline )
removeLeftoverAnonymousBoxes();
}
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;
deleteLineBoxTree();
RenderObject *child = firstChild();
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->appendChildNode(removeChildNode(no));
}
box->appendChildNode(removeChildNode(inlineRunEnd));
}
#ifndef NDEBUG
for (RenderObject *c = firstChild(); c; c = c->nextSibling())
ASSERT(!c->isInline());
#endif
}
void RenderBlock::removeChild(RenderObject *oldChild)
{
RenderObject* prev = oldChild->previousSibling();
RenderObject* next = oldChild->nextSibling();
bool canDeleteAnonymousBlocks = !documentBeingDestroyed() && !isInline() && !oldChild->isInline() &&
!oldChild->continuation() &&
(!prev || (prev->isAnonymousBlock() && prev->childrenInline())) &&
(!next || (next->isAnonymousBlock() && next->childrenInline()));
if (canDeleteAnonymousBlocks && prev && next) {
prev->setNeedsLayoutAndMinMaxRecalc();
RenderObject* o = next->firstChild();
while (o) {
RenderObject* no = o;
o = no->nextSibling();
prev->appendChildNode(next->removeChildNode(no));
no->setNeedsLayoutAndMinMaxRecalc();
}
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()) {
setNeedsLayoutAndMinMaxRecalc();
RenderBlock* anonBlock = static_cast<RenderBlock*>(removeChildNode(child));
m_childrenInline = true;
RenderObject* o = anonBlock->firstChild();
while (o) {
RenderObject* no = o;
o = no->nextSibling();
appendChildNode(anonBlock->removeChildNode(no));
no->setNeedsLayoutAndMinMaxRecalc();
}
anonBlock->deleteLineBoxTree();
anonBlock->destroy();
}
}
int RenderBlock::overflowHeight(bool includeInterior) const
{
return (!includeInterior && hasOverflowClip()) ? m_height : m_overflowHeight;
}
int RenderBlock::overflowWidth(bool includeInterior) const
{
return (!includeInterior && hasOverflowClip()) ? m_width : m_overflowWidth;
}
int RenderBlock::overflowLeft(bool includeInterior) const
{
return (!includeInterior && hasOverflowClip()) ? 0 : m_overflowLeft;
}
int RenderBlock::overflowTop(bool includeInterior) const
{
return (!includeInterior && hasOverflowClip()) ? 0 : m_overflowTop;
}
IntRect RenderBlock::overflowRect(bool includeInterior) const
{
if (!includeInterior && hasOverflowClip())
return borderBox();
int l = overflowLeft(includeInterior);
int t = min(overflowTop(includeInterior), -borderTopExtra());
return IntRect(l, t, overflowWidth(includeInterior) - l, max(overflowHeight(includeInterior), height() + borderBottomExtra()) - t);
}
bool RenderBlock::isSelfCollapsingBlock() const
{
if (m_height > 0 ||
isTable() || (borderBottom() + paddingBottom() + borderTop() + paddingTop()) != 0 ||
style()->minHeight().value() > 0 ||
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().value() == 0)) {
if (childrenInline())
return !firstLineBox();
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (child->isFloatingOrPositioned())
continue;
if (!child->isSelfCollapsingBlock())
return false;
}
return true;
}
return false;
}
void RenderBlock::layout()
{
layoutBlock(false);
}
void RenderBlock::layoutBlock(bool relayoutChildren)
{
ASSERT(needsLayout());
ASSERT(minMaxKnown());
if (isInline() && !isInlineBlockOrInlineTable()) return;
if (!relayoutChildren && posChildNeedsLayout() && !normalChildNeedsLayout() && !selfNeedsLayout()) {
layoutPositionedObjects(false);
if (hasOverflowClip())
m_layer->updateScrollInfoAfterLayout();
setNeedsLayout(false);
return;
}
IntRect oldBounds, oldFullBounds;
bool checkForRepaint = checkForRepaintDuringLayout();
if (checkForRepaint) {
getAbsoluteRepaintRectIncludingFloats(oldBounds, oldFullBounds);
oldBounds.move(view()->layoutDelta());
oldFullBounds.move(view()->layoutDelta());
}
int oldWidth = m_width;
calcWidth();
m_overflowWidth = m_width;
if (oldWidth != m_width)
relayoutChildren = true;
clearFloats();
int previousHeight = m_height;
m_height = 0;
m_overflowHeight = 0;
m_clearStatus = CNONE;
if (!isTableCell()) {
initMaxMarginValues();
m_topMarginQuirk = style()->marginTop().quirk();
m_bottomMarginQuirk = style()->marginBottom().quirk();
if (element() && element()->hasTagName(formTag) && element()->isMalformed())
m_maxBottomPosMargin = m_maxBottomNegMargin = 0;
}
if (scrollsOverflow()) {
if (style()->overflowX() == OSCROLL)
m_layer->setHasHorizontalScrollbar(true);
if (style()->overflowY() == OSCROLL)
m_layer->setHasVerticalScrollbar(true);
}
IntRect repaintRect;
if (childrenInline())
repaintRect = layoutInlineChildren(relayoutChildren);
else
layoutBlockChildren(relayoutChildren);
int toAdd = borderBottom() + paddingBottom();
if (includeHorizontalScrollbarSize())
toAdd += m_layer->horizontalScrollbarHeight();
if (floatBottom() > (m_height - toAdd) && (isInlineBlockOrInlineTable() || isFloatingOrPositioned() || hasOverflowClip() ||
(parent() && parent()->isFlexibleBox())))
m_height = floatBottom() + toAdd;
int oldHeight = m_height;
calcHeight();
if (oldHeight != m_height) {
if (m_overflowHeight > m_height)
m_overflowHeight -= paddingBottom() + borderBottom();
if (m_overflowHeight < m_height)
m_overflowHeight = m_height;
}
if (previousHeight != m_height)
relayoutChildren = true;
if (isTableCell()) {
if (lastChild() && lastChild()->hasOverhangingFloats()) {
ASSERT(lastChild()->isRenderBlock());
m_height = lastChild()->yPos() + static_cast<RenderBlock*>(lastChild())->floatBottom();
m_height += borderBottom() + paddingBottom();
}
if (m_overflowHeight > m_height && !hasOverflowClip())
m_height = m_overflowHeight + borderBottom() + paddingBottom();
}
if (hasOverhangingFloats() && ((isFloating() && style()->height().isAuto()) || isTableCell())) {
m_height = floatBottom();
m_height += borderBottom() + paddingBottom();
}
layoutPositionedObjects(relayoutChildren || isRoot());
positionListMarker();
m_overflowWidth = max(m_overflowWidth, m_width);
m_overflowHeight = max(m_overflowHeight, m_height);
RenderObject* flexbox = view()->flexBoxInFirstLayout();
if (hasOverflowClip() && !(flexbox && flexbox != this && hasAncestor(flexbox)))
m_layer->updateScrollInfoAfterLayout();
bool didFullRepaint = false;
if (checkForRepaint)
didFullRepaint = repaintAfterLayoutIfNeeded(oldBounds, oldFullBounds);
if (!didFullRepaint && !repaintRect.isEmpty()) {
RenderView* v = view();
if (v && v->frameView()) {
repaintRect.inflate(maximalOutlineSize(PaintPhaseOutline));
v->frameView()->addRepaintInfo(this, repaintRect); }
}
setNeedsLayout(false);
}
void RenderBlock::adjustPositionedBlock(RenderObject* 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 = m_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();
m_height += marginOffset;
positionNewFloats();
m_height -= marginOffset;
}
RenderObject* RenderBlock::handleSpecialChild(RenderObject* child, const MarginInfo& marginInfo, CompactInfo& compactInfo, bool& handled)
{
RenderObject* next = handlePositionedChild(child, marginInfo, handled);
if (handled) return next;
next = handleFloatingChild(child, marginInfo, handled);
if (handled) return next;
next = handleCompactChild(child, compactInfo, handled);
if (handled) return next;
return handleRunInChild(child, handled);
}
RenderObject* RenderBlock::handlePositionedChild(RenderObject* child, const MarginInfo& marginInfo, bool& handled)
{
if (child->isPositioned()) {
handled = true;
child->containingBlock()->insertPositionedObject(child);
adjustPositionedBlock(child, marginInfo);
return child->nextSibling();
}
return 0;
}
RenderObject* RenderBlock::handleFloatingChild(RenderObject* child, const MarginInfo& marginInfo, bool& handled)
{
if (child->isFloating()) {
handled = true;
insertFloatingObject(child);
adjustFloatingBlock(marginInfo);
return child->nextSibling();
}
return 0;
}
RenderObject* RenderBlock::handleCompactChild(RenderObject* child, CompactInfo& compactInfo, bool& handled)
{
if (child->isCompact() && !compactInfo.compact() && (child->childrenInline() || child->isReplaced())) {
RenderObject* next = child->nextSibling();
RenderObject* curr = next;
while (curr && curr->isFloatingOrPositioned())
curr = curr->nextSibling();
if (curr && curr->isRenderBlock() && !curr->isCompact() && !curr->isRunIn()) {
curr->calcWidth();
child->setInline(true); child->calcWidth();
int childMargins = child->marginLeft() + child->marginRight();
int margin = style()->direction() == LTR ? curr->marginLeft() : curr->marginRight();
if (margin >= (childMargins + child->maxWidth())) {
handled = true;
compactInfo.set(child, curr);
child->setPos(0,0);
RenderObject* next = child->nextSibling();
removeChildNode(child);
curr->insertChildNode(child, curr->firstChild());
return next;
}
else
child->setInline(false); }
}
return 0;
}
void RenderBlock::insertCompactIfNeeded(RenderObject* child, CompactInfo& compactInfo)
{
if (compactInfo.matches(child)) {
RenderObject* compactChild = compactInfo.compact();
int compactXPos = borderLeft() + paddingLeft() + compactChild->marginLeft();
if (style()->direction() == RTL) {
compactChild->calcWidth(); compactXPos = width() - borderRight() - paddingRight() - marginRight() -
compactChild->width() - compactChild->marginRight();
}
compactXPos -= child->xPos(); compactChild->setPos(compactXPos, compactChild->yPos()); compactInfo.clear();
}
}
RenderObject* RenderBlock::handleRunInChild(RenderObject* child, bool& handled)
{
if (child->isRunIn() && (child->childrenInline() || child->isReplaced())) {
RenderObject* curr = child->nextSibling();
while (curr && curr->isFloatingOrPositioned())
curr = curr->nextSibling();
if (curr && (curr->isRenderBlock() && curr->childrenInline() && !curr->isCompact() && !curr->isRunIn())) {
handled = true;
child->setInline(true);
child->setPos(0,0);
RenderObject* next = child->nextSibling();
removeChildNode(child);
curr->insertChildNode(child, curr->firstChild());
return next;
}
}
return 0;
}
void RenderBlock::collapseMargins(RenderObject* 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) {
m_maxTopPosMargin = max(posTop, m_maxTopPosMargin);
m_maxTopNegMargin = max(negTop, m_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 = m_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 = m_height + collapsedTopPos - collapsedTopNeg;
}
else {
if (child->style()->marginTopCollapse() == MSEPARATE) {
m_height += marginInfo.margin() + child->marginTop();
ypos = m_height;
}
else if (!marginInfo.atTopOfBlock() ||
(!marginInfo.canCollapseTopWithChildren()
&& (!style()->htmlHacks() || !marginInfo.quirkContainer() || !marginInfo.topQuirk()))) {
m_height += max(marginInfo.posMargin(), posTop) - max(marginInfo.negMargin(), negTop);
ypos = m_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->setPos(child->xPos(), ypos);
if (ypos != yPosEstimate) {
if (child->shrinkToAvoidFloats())
child->setChildNeedsLayout(true);
if (!child->avoidsFloats() && child->containsFloats())
child->markAllDescendantsWithFloatsForLayout();
child->layoutIfNeeded();
}
}
void RenderBlock::clearFloatsIfNeeded(RenderObject* child, MarginInfo& marginInfo, int oldTopPosMargin, int oldTopNegMargin)
{
int heightIncrease = getClearDelta(child);
if (heightIncrease) {
view()->addLayoutDelta(IntSize(0, -heightIncrease));
child->setPos(child->xPos(), child->yPos() + heightIncrease);
if (!child->isSelfCollapsingBlock())
m_height += heightIncrease;
else {
marginInfo.setPosMargin(max(0, child->yPos() - m_height));
marginInfo.setNegMargin(0);
marginInfo.setSelfCollapsingBlockClearedFloat(true);
}
if (marginInfo.canCollapseWithTop()) {
m_maxTopPosMargin = oldTopPosMargin;
m_maxTopNegMargin = oldTopNegMargin;
marginInfo.setAtTopOfBlock(false);
}
if (child->shrinkToAvoidFloats())
child->setChildNeedsLayout(true);
if (!child->avoidsFloats() && child->containsFloats())
child->markAllDescendantsWithFloatsForLayout();
child->layoutIfNeeded();
}
}
int RenderBlock::estimateVerticalPosition(RenderObject* child, const MarginInfo& marginInfo)
{
int yPosEstimate = m_height;
if (!marginInfo.canCollapseWithTop()) {
int childMarginTop = child->selfNeedsLayout() ? child->marginTop() : child->collapsedMarginTop();
yPosEstimate += max(marginInfo.margin(), childMarginTop);
}
return yPosEstimate;
}
void RenderBlock::determineHorizontalPosition(RenderObject* child)
{
if (style()->direction() == LTR) {
int xPos = borderLeft() + paddingLeft();
int chPos = xPos + child->marginLeft();
if (child->avoidsFloats()) {
int leftOff = leftOffset(m_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) {
static_cast<RenderBox*>(child)->calcHorizontalMargins(child->style()->marginLeft(), child->style()->marginRight(), lineWidth(child->yPos()));
chPos = leftOff + child->marginLeft();
}
}
view()->addLayoutDelta(IntSize(child->xPos() - chPos, 0));
child->setPos(chPos, child->yPos());
} else {
int xPos = m_width - borderRight() - paddingRight() - (includeVerticalScrollbarSize() ? m_layer->verticalScrollbarWidth() : 0);
int chPos = xPos - (child->width() + child->marginRight());
if (child->avoidsFloats()) {
int rightOff = rightOffset(m_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) {
static_cast<RenderBox*>(child)->calcHorizontalMargins(child->style()->marginLeft(), child->style()->marginRight(), lineWidth(child->yPos()));
chPos = rightOff - child->marginRight() - child->width();
}
}
view()->addLayoutDelta(IntSize(child->xPos() - chPos, 0));
child->setPos(chPos, child->yPos());
}
}
void RenderBlock::setCollapsedBottomMargin(const MarginInfo& marginInfo)
{
if (marginInfo.canCollapseWithBottom() && !marginInfo.canCollapseWithTop()) {
m_maxBottomPosMargin = max(m_maxBottomPosMargin, marginInfo.posMargin());
m_maxBottomNegMargin = max(m_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);
if (!marginInfo.canCollapseWithBottom() && !marginInfo.canCollapseWithTop()
&& (!style()->htmlHacks() || !marginInfo.quirkContainer() || !marginInfo.bottomQuirk()))
m_height += marginInfo.margin();
m_height += bottom;
m_height = max(m_height, top + bottom);
m_overflowHeight = max(m_height, m_overflowHeight);
setCollapsedBottomMargin(marginInfo);
}
void RenderBlock::layoutBlockChildren(bool relayoutChildren)
{
int top = borderTop() + paddingTop();
int bottom = borderBottom() + paddingBottom() + (includeHorizontalScrollbarSize() ? m_layer->horizontalScrollbarHeight() : 0);
m_height = m_overflowHeight = top;
MarginInfo marginInfo(this, top, bottom);
CompactInfo compactInfo;
RenderObject* legend = layoutLegend(relayoutChildren);
RenderObject* child = firstChild();
while (child) {
if (legend == child) {
child = child->nextSibling();
continue; }
int oldTopPosMargin = m_maxTopPosMargin;
int oldTopNegMargin = m_maxTopNegMargin;
if (relayoutChildren || (child->style()->height().isPercent() || child->style()->minHeight().isPercent() || child->style()->maxHeight().isPercent()))
child->setChildNeedsLayout(true);
bool handled = false;
RenderObject* next = handleSpecialChild(child, marginInfo, compactInfo, handled);
if (handled) { child = next; continue; }
child->calcVerticalMargins();
if (child->style()->marginTopCollapse() == MSEPARATE) {
marginInfo.setAtTopOfBlock(false);
marginInfo.clearMargin();
}
int yPosEstimate = estimateVerticalPosition(child, marginInfo);
if (!child->avoidsFloats() || child->shrinkToAvoidFloats()) {
int fb = floatBottom();
if (fb > m_height || fb > yPosEstimate)
child->setChildNeedsLayout(true);
}
int oldChildX = child->xPos();
int oldChildY = child->yPos();
view()->addLayoutDelta(IntSize(0, child->yPos() - yPosEstimate));
child->setPos(child->xPos(), yPosEstimate);
if (yPosEstimate != oldChildY && !child->avoidsFloats() && child->containsFloats())
child->markAllDescendantsWithFloatsForLayout();
child->layoutIfNeeded();
collapseMargins(child, marginInfo, yPosEstimate);
int postCollapseChildY = child->yPos();
clearFloatsIfNeeded(child, marginInfo, oldTopPosMargin, oldTopNegMargin);
if (marginInfo.atTopOfBlock() && !child->isSelfCollapsingBlock())
marginInfo.setAtTopOfBlock(false);
determineHorizontalPosition(child);
m_overflowTop = min(m_overflowTop, child->yPos() + child->overflowTop(false));
m_height += child->height();
if (child->style()->marginBottomCollapse() == MSEPARATE) {
m_height += child->marginBottom();
marginInfo.clearMargin();
}
int overflowDelta = child->overflowHeight(false) - child->height();
if (m_height + overflowDelta > m_overflowHeight)
m_overflowHeight = m_height + overflowDelta;
addOverhangingFloats(static_cast<RenderBlock *>(child), -child->xPos(), -child->yPos());
m_overflowWidth = max(child->xPos() + child->overflowWidth(false), m_overflowWidth);
m_overflowLeft = min(child->xPos() + child->overflowLeft(false), m_overflowLeft);
insertCompactIfNeeded(child, compactInfo);
view()->addLayoutDelta(IntSize(child->xPos() - oldChildX, child->yPos() - oldChildY));
if (!selfNeedsLayout() && child->checkForRepaintDuringLayout()) {
int finalChildX = child->xPos();
int finalChildY = child->yPos();
if (finalChildX != oldChildX || finalChildY != oldChildY)
child->repaintDuringLayoutIfMoved(oldChildX, oldChildY);
else if (finalChildY != yPosEstimate || finalChildY != postCollapseChildY) {
child->repaint();
child->repaintOverhangingFloats();
}
}
child = child->nextSibling();
}
handleBottomOfBlock(top, bottom, marginInfo);
setNeedsLayout(false);
}
void RenderBlock::layoutPositionedObjects(bool relayoutChildren)
{
if (m_positionedObjects) {
RenderObject* r;
DeprecatedPtrListIterator<RenderObject> it(*m_positionedObjects);
for ( ; (r = it.current()); ++it ) {
if (relayoutChildren || (r->hasStaticY() && r->parent() != this && r->parent()->isBlockFlow()))
r->setChildNeedsLayout(true);
r->layoutIfNeeded();
}
}
}
void RenderBlock::markPositionedObjectsForLayout()
{
if (m_positionedObjects) {
RenderObject* r;
DeprecatedPtrListIterator<RenderObject> it(*m_positionedObjects);
for (; (r = it.current()); ++it)
r->setChildNeedsLayout(true);
}
}
void RenderBlock::getAbsoluteRepaintRectIncludingFloats(IntRect& bounds, IntRect& fullBounds)
{
bounds = fullBounds = getAbsoluteRepaintRect();
if (hasOverhangingFloats() && m_floatingObjects) {
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it) {
if (!r->noPaint && !r->node->layer()) {
IntRect childRect, childFullRect;
r->node->getAbsoluteRepaintRectIncludingFloats(childRect, childFullRect);
fullBounds.unite(childFullRect);
}
}
}
}
void RenderBlock::repaintOverhangingFloats(bool paintAllDescendants)
{
if (hasOverhangingFloats()) {
ASSERT(m_floatingObjects);
if (!m_floatingObjects)
return;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it) {
if (r->endY > m_height && (paintAllDescendants && r->node->hasAncestor(this) || !r->noPaint) && !r->node->layer()) {
r->node->repaint();
r->node->repaintOverhangingFloats();
}
}
}
}
void RenderBlock::repaintObjectsBeforeLayout()
{
RenderFlow::repaintObjectsBeforeLayout();
if (!needsLayout())
return;
if (m_positionedObjects) {
RenderObject* r;
DeprecatedPtrListIterator<RenderObject> it(*m_positionedObjects);
for ( ; (r = it.current()); ++it )
r->repaintObjectsBeforeLayout();
}
}
void RenderBlock::paint(PaintInfo& i, int _tx, int _ty)
{
_tx += m_x;
_ty += m_y;
if (!isInlineFlow() && !isRoot()) {
IntRect overflowBox = overflowRect(false);
overflowBox.inflate(maximalOutlineSize(i.phase));
overflowBox.move(_tx, _ty);
bool intersectsOverflowBox = overflowBox.intersects(i.r);
if (!intersectsOverflowBox) {
if (i.phase != PaintPhaseFloat)
return;
IntRect floatBox = floatRect();
floatBox.inflate(maximalOutlineSize(i.phase));
floatBox.move(_tx, _ty);
if (!floatBox.intersects(i.r))
return;
}
}
return paintObject(i, _tx, _ty);
}
void RenderBlock::paintChildren(PaintInfo& i, int _tx, int _ty)
{
PaintPhase newPhase = (i.phase == PaintPhaseChildOutlines) ? PaintPhaseOutline : i.phase;
newPhase = (newPhase == PaintPhaseChildBlockBackgrounds) ? PaintPhaseChildBlockBackground : newPhase;
PaintInfo paintInfo(i);
paintInfo.phase = newPhase;
bool isPrinting = document()->printing();
for (RenderObject *child = firstChild(); child; child = child->nextSibling()) {
if (isPrinting && !childrenInline() && child->style()->pageBreakBefore() == PBALWAYS &&
inRootBlockContext() && (_ty + child->yPos()) > i.r.y() &&
(_ty + child->yPos()) < i.r.bottom()) {
view()->setBestTruncatedAt(_ty + child->yPos(), this, true);
return;
}
if (!child->layer() && !child->isFloating())
child->paint(paintInfo, _tx, _ty);
if (isPrinting && !childrenInline() && child->style()->pageBreakAfter() == PBALWAYS &&
inRootBlockContext() && (_ty + child->yPos() + child->height()) > i.r.y() &&
(_ty + child->yPos() + child->height()) < i.r.bottom()) {
view()->setBestTruncatedAt(_ty + child->yPos() + child->height() + child->collapsedMarginBottom(), this, true);
return;
}
}
}
void RenderBlock::paintCaret(PaintInfo& i, CaretType type)
{
const SelectionController &s = type == CursorCaret ? document()->frame()->selection() : document()->frame()->dragCaret();
Node *caretNode = s.start().node();
RenderObject *renderer = caretNode ? caretNode->renderer() : 0;
if (renderer && (renderer == this || renderer->containingBlock() == this) && caretNode && caretNode->isContentEditable()) {
if (type == CursorCaret) {
document()->frame()->paintCaret(i.p, i.r);
} else {
document()->frame()->paintDragCaret(i.p, i.r);
}
}
}
void RenderBlock::paintObject(PaintInfo& i, int _tx, int _ty)
{
PaintPhase paintPhase = i.phase;
bool inlineFlow = isInlineFlow();
if (!inlineFlow &&
(paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) &&
shouldPaintBackgroundOrBorder() && style()->visibility() == VISIBLE) {
paintBoxDecorations(i, _tx, _ty);
}
if (paintPhase == PaintPhaseBlockBackground)
return;
int scrolledX = _tx;
int scrolledY = _ty;
if (hasOverflowClip())
m_layer->subtractScrollOffset(scrolledX, scrolledY);
if (paintPhase != PaintPhaseSelfOutline) {
if (childrenInline())
paintLines(i, scrolledX, scrolledY);
else
paintChildren(i, scrolledX, scrolledY);
}
bool isPrinting = document()->printing();
if (!inlineFlow && !isPrinting)
paintSelection(i, scrolledX, scrolledY);
if (!inlineFlow && (paintPhase == PaintPhaseFloat || paintPhase == PaintPhaseSelection))
paintFloats(i, scrolledX, scrolledY, paintPhase == PaintPhaseSelection);
if (!inlineFlow && (paintPhase == PaintPhaseOutline || paintPhase == PaintPhaseSelfOutline)
&& style()->outlineWidth() && style()->visibility() == VISIBLE)
paintOutline(i.p, _tx, _ty, width(), height(), style());
if (!inlineFlow && paintPhase == PaintPhaseForeground) {
paintCaret(i, CursorCaret);
paintCaret(i, DragCaret);
}
#ifdef BOX_DEBUG
if ( style() && style()->visibility() == VISIBLE ) {
if(isAnonymous())
outlineBox(i.p, _tx, _ty, "green");
if(isFloating())
outlineBox(i.p, _tx, _ty, "yellow");
else
outlineBox(i.p, _tx, _ty);
}
#endif
}
void RenderBlock::paintFloats(PaintInfo& i, int _tx, int _ty, bool paintSelection)
{
if (!m_floatingObjects)
return;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it) {
if (!r->noPaint && !r->node->layer()) {
PaintInfo info(i);
info.phase = paintSelection ? PaintPhaseSelection : PaintPhaseBlockBackground;
int tx = _tx + r->left - r->node->xPos() + r->node->marginLeft();
int ty = _ty + r->startY - r->node->yPos() + r->node->marginTop();
r->node->paint(info, tx, ty);
if (!paintSelection) {
info.phase = PaintPhaseChildBlockBackgrounds;
r->node->paint(info, tx, ty);
info.phase = PaintPhaseFloat;
r->node->paint(info, tx, ty);
info.phase = PaintPhaseForeground;
r->node->paint(info, tx, ty);
info.phase = PaintPhaseOutline;
r->node->paint(info, tx, ty);
}
}
}
}
void RenderBlock::paintEllipsisBoxes(PaintInfo& i, int _tx, int _ty)
{
if (!shouldPaintWithinRoot(i) || !firstLineBox())
return;
if (style()->visibility() == VISIBLE && i.phase == PaintPhaseForeground) {
int yPos = _ty + firstLineBox()->yPos();;
int h = lastLineBox()->yPos() + lastLineBox()->height() - firstLineBox()->yPos();
if (yPos >= i.r.bottom() || yPos + h <= i.r.y())
return;
for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
yPos = _ty + curr->yPos();
h = curr->height();
if (curr->ellipsisBox() && yPos < i.r.bottom() && yPos + h > i.r.y())
curr->paintEllipsisBox(i, _tx, _ty);
}
}
}
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())
return true;
if (view() && view()->selectionStart()) {
Node* startElement = view()->selectionStart()->element();
if (startElement && startElement->rootEditableElement() == element())
return true;
}
return false;
}
GapRects RenderBlock::selectionGapRects()
{
if (!shouldPaintSelectionGaps())
return GapRects();
int tx, ty;
absolutePositionForContent(tx, ty);
int lastTop = -borderTopExtra();
int lastLeft = leftSelectionOffset(this, lastTop);
int lastRight = rightSelectionOffset(this, lastTop);
return fillSelectionGaps(this, tx, ty, tx, ty, lastTop, lastLeft, lastRight);
}
void RenderBlock::paintSelection(PaintInfo& i, int tx, int ty)
{
if (shouldPaintSelectionGaps() && i.phase == PaintPhaseForeground) {
return;
}
}
GapRects RenderBlock::fillSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty, int& lastTop, int& lastLeft, int& lastRight,
const PaintInfo* i)
{
GapRects result;
if (!isBlockFlow())
return result;
if (childrenInline())
result = fillInlineSelectionGaps(rootBlock, blockX, blockY, tx, ty, lastTop, lastLeft, lastRight, i);
else
result = fillBlockSelectionGaps(rootBlock, blockX, blockY, tx, ty, lastTop, lastLeft, lastRight, i);
if (rootBlock == this && (m_selectionState != SelectionBoth && m_selectionState != SelectionEnd))
result.uniteCenter(fillVerticalSelectionGap(lastTop, lastLeft, lastRight, ty + height() + borderBottomExtra(),
rootBlock, blockX, blockY, i));
return result;
}
GapRects RenderBlock::fillInlineSelectionGaps(RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty,
int& lastTop, int& lastLeft, int& lastRight, const PaintInfo* i)
{
GapRects result;
RenderObject* selStart = view()->selectionStart();
bool containsStart = (selStart && (selStart == this || selStart->containingBlock() == this));
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, i));
if (!i || ty + selTop < i->r.bottom() && ty + selTop + selHeight > i->r.y())
result.unite(curr->fillLineSelectionGap(selTop, selHeight, rootBlock, blockX, blockY, tx, ty, i));
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* i)
{
GapRects result;
RenderObject* curr;
for (curr = firstChild(); curr && curr->selectionState() == SelectionNone; curr = curr->nextSibling());
for (bool sawSelectionEnd = false; curr && !sawSelectionEnd; curr = curr->nextSibling()) {
SelectionState childState = curr->selectionState();
if (childState == SelectionBoth || childState == SelectionEnd)
sawSelectionEnd = true;
if (curr->isFloatingOrPositioned())
continue;
if (curr->isRelPositioned() && curr->layer()) {
int x = 0;
int y = 0;
curr->layer()->relativePositionOffset(x, y);
if (x || y)
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->yPos(), rootBlock, blockX, blockY, i));
if (paintsOwnSelection && (childState == SelectionStart || sawSelectionEnd))
childState = SelectionNone;
bool leftGap, rightGap;
getHorizontalSelectionGapInfo(childState, leftGap, rightGap);
if (leftGap)
result.uniteLeft(fillLeftSelectionGap(this, curr->xPos(), curr->yPos(), curr->height(), rootBlock, blockX, blockY, tx, ty, i));
if (rightGap)
result.uniteRight(fillRightSelectionGap(this, curr->xPos() + curr->width(), curr->yPos(), curr->height(), rootBlock, blockX, blockY, tx, ty, i));
lastTop = (ty - blockY) + (curr->yPos() + curr->height());
lastLeft = leftSelectionOffset(rootBlock, curr->yPos() + curr->height());
lastRight = rightSelectionOffset(rootBlock, curr->yPos() + curr->height());
}
else if (childState != SelectionNone)
result.unite(static_cast<RenderBlock*>(curr)->fillSelectionGaps(rootBlock, blockX, blockY, tx + curr->xPos(), ty + curr->yPos(),
lastTop, lastLeft, lastRight, i));
}
return result;
}
IntRect RenderBlock::fillHorizontalSelectionGap(RenderObject* selObj, int xPos, int yPos, int width, int height,
const PaintInfo* i)
{
if (width <= 0 || height <= 0)
return IntRect();
IntRect gapRect(xPos, yPos, width, height);
if (i)
i->p->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* i)
{
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 width = right - left;
if (width <= 0)
return IntRect();
IntRect gapRect(left, top, width, height);
if (i)
i->p->fillRect(gapRect, selectionBackgroundColor());
return gapRect;
}
IntRect RenderBlock::fillLeftSelectionGap(RenderObject* selObj, int xPos, int yPos, int height, RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty, const PaintInfo* i)
{
int top = yPos + ty;
int left = blockX + max(leftSelectionOffset(rootBlock, yPos), leftSelectionOffset(rootBlock, yPos + height));
int width = tx + xPos - left;
if (width <= 0)
return IntRect();
IntRect gapRect(left, top, width, height);
if (i)
i->p->fillRect(gapRect, selObj->selectionBackgroundColor());
return gapRect;
}
IntRect RenderBlock::fillRightSelectionGap(RenderObject* selObj, int xPos, int yPos, int height, RenderBlock* rootBlock, int blockX, int blockY, int tx, int ty, const PaintInfo* i)
{
int left = xPos + tx;
int top = yPos + ty;
int right = blockX + min(rightSelectionOffset(rootBlock, yPos), rightSelectionOffset(rootBlock, yPos + height));
int width = right - left;
if (width <= 0)
return IntRect();
IntRect gapRect(left, top, width, height);
if (i)
i->p->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 y)
{
int left = leftOffset(y);
if (left == borderLeft() + paddingLeft()) {
if (rootBlock != this)
return containingBlock()->leftSelectionOffset(rootBlock, y + yPos());
return 0;
}
else {
RenderBlock* cb = this;
while (cb != rootBlock) {
left += cb->xPos();
cb = cb->containingBlock();
}
}
return left;
}
int RenderBlock::rightSelectionOffset(RenderBlock* rootBlock, int y)
{
int right = rightOffset(y);
if (right == (contentWidth() + (borderLeft() + paddingLeft()))) {
if (rootBlock != this)
return containingBlock()->rightSelectionOffset(rootBlock, y + yPos());
return width();
}
else {
RenderBlock* cb = this;
while (cb != rootBlock) {
right += cb->xPos();
cb = cb->containingBlock();
}
}
return right;
}
void RenderBlock::insertPositionedObject(RenderObject *o)
{
if (!m_positionedObjects) {
m_positionedObjects = new DeprecatedPtrList<RenderObject>;
m_positionedObjects->setAutoDelete(false);
}
else {
DeprecatedPtrListIterator<RenderObject> it(*m_positionedObjects);
RenderObject* f;
while ( (f = it.current()) ) {
if (f == o) return;
++it;
}
}
m_positionedObjects->append(o);
}
void RenderBlock::removePositionedObject(RenderObject *o)
{
if (m_positionedObjects) {
DeprecatedPtrListIterator<RenderObject> it(*m_positionedObjects);
while (it.current()) {
if (it.current() == o) {
m_positionedObjects->removeRef(it.current());
return;
}
++it;
}
}
}
void RenderBlock::removePositionedObjects(RenderBlock* o)
{
if (!m_positionedObjects)
return;
DeprecatedPtrListIterator<RenderObject> it(*m_positionedObjects);
while (it.current()) {
if (!o || it.current()->hasAncestor(o)) {
if (o)
it.current()->setChildNeedsLayout(true, false);
m_positionedObjects->removeRef(it.current());
} else
++it;
}
}
void RenderBlock::insertFloatingObject(RenderObject *o)
{
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->node == o) return;
++it;
}
}
FloatingObject *newObj;
if (o->isFloating()) {
o->layoutIfNeeded();
if(o->style()->floating() == FLEFT)
newObj = new FloatingObject(FloatingObject::FloatLeft);
else
newObj = new FloatingObject(FloatingObject::FloatRight);
newObj->startY = -1;
newObj->endY = -1;
newObj->width = o->width() + o->marginLeft() + o->marginRight();
newObj->noPaint = o->layer(); }
else {
ASSERT(false);
newObj = 0; }
newObj->node = o;
m_floatingObjects->append(newObj);
}
void RenderBlock::removeFloatingObject(RenderObject *o)
{
if (m_floatingObjects) {
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
while (it.current()) {
if (it.current()->node == o)
m_floatingObjects->removeRef(it.current());
++it;
}
}
}
void RenderBlock::setPaintsFloatingObject(RenderObject* o, bool b)
{
if (!m_floatingObjects)
return;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
while (it.current()) {
if (it.current()->node == o) {
it.current()->noPaint = !b;
setChildNeedsLayout(true);
return;
}
++it;
}
}
void RenderBlock::positionNewFloats()
{
if(!m_floatingObjects) return;
FloatingObject *f = m_floatingObjects->last();
if(!f || f->startY != -1) return;
FloatingObject *lastFloat;
while(1)
{
lastFloat = m_floatingObjects->getPrev();
if (!lastFloat || lastFloat->startY != -1)
break;
f = m_floatingObjects->prev();
}
int y = m_height;
if(lastFloat && lastFloat->startY > y)
y = lastFloat->startY;
while(f)
{
if (f->node->containingBlock()!=this)
{
f = m_floatingObjects->next();
continue;
}
RenderObject *o = f->node;
int _height = o->height() + o->marginTop() + o->marginBottom();
int ro = rightOffset(); int lo = leftOffset(); int fwidth = f->width; if (ro - lo < fwidth)
fwidth = ro - lo;
int oldChildX = o->xPos();
int oldChildY = o->yPos();
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);
}
if (fx<0) fx=0;
f->left = fx;
o->setPos(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);
}
if (fx<f->width) fx=f->width;
f->left = fx - f->width;
o->setPos(fx - o->marginRight() - o->width(), y + o->marginTop());
}
f->startY = y;
f->endY = f->startY + _height;
if (o->checkForRepaintDuringLayout())
o->repaintDuringLayoutIfMoved(oldChildX, oldChildY);
f = m_floatingObjects->next();
}
}
void RenderBlock::newLine()
{
positionNewFloats();
int newY = 0;
switch(m_clearStatus)
{
case CLEFT:
newY = leftBottom();
break;
case CRIGHT:
newY = rightBottom();
break;
case CBOTH:
newY = floatBottom();
default:
break;
}
if (m_height < newY)
m_height = newY;
m_clearStatus = CNONE;
}
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->startY <= y && r->endY > y &&
r->type() == FloatingObject::FloatLeft &&
r->left + r->width > left) {
left = r->left + r->width;
if ( heightRemaining ) *heightRemaining = r->endY - y;
}
}
}
if (applyTextIndent && m_firstLine && style()->direction() == LTR) {
int cw=0;
if (style()->textIndent().isPercent())
cw = containingBlock()->contentWidth();
left += style()->textIndent().calcMinValue(cw);
}
return left;
}
int
RenderBlock::rightOffset() const
{
int right = m_width - borderRight() - paddingRight();
if (includeVerticalScrollbarSize())
right -= m_layer->verticalScrollbarWidth();
return right;
}
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->startY <= y && r->endY > y &&
r->type() == FloatingObject::FloatRight &&
r->left < right) {
right = r->left;
if ( heightRemaining ) *heightRemaining = r->endY - y;
}
}
}
if (applyTextIndent && m_firstLine && style()->direction() == RTL) {
int cw=0;
if (style()->textIndent().isPercent())
cw = containingBlock()->contentWidth();
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::nearestFloatBottom(int height) const
{
if (!m_floatingObjects) return 0;
int bottom = 0;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it )
if (r->endY>height && (r->endY<bottom || bottom==0))
bottom=r->endY;
return max(bottom, height);
}
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->endY>bottom)
bottom=r->endY;
return bottom;
}
IntRect RenderBlock::floatRect() const
{
IntRect result(borderBox());
if (!m_floatingObjects || hasOverflowClip())
return result;
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for (; (r = it.current()); ++it) {
if (!r->noPaint && !r->node->layer()) {
IntRect childRect = unionRect(r->node->floatRect(), r->node->overflowRect());
childRect.move(r->left + r->node->marginLeft(), r->startY + r->node->marginTop());
result.unite(childRect);
}
}
return result;
}
int
RenderBlock::lowestPosition(bool includeOverflowInterior, bool includeSelf) const
{
int bottom = RenderFlow::lowestPosition(includeOverflowInterior, includeSelf);
if (!includeOverflowInterior && hasOverflowClip())
return bottom;
if (includeSelf && m_overflowHeight > bottom)
bottom = m_overflowHeight;
if (m_floatingObjects) {
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it ) {
if (!r->noPaint || r->node->layer()) {
int lp = r->startY + r->node->marginTop() + r->node->lowestPosition(false);
bottom = max(bottom, lp);
}
}
}
if (m_positionedObjects && !isRenderView()) {
RenderObject* r;
DeprecatedPtrListIterator<RenderObject> it(*m_positionedObjects);
for ( ; (r = it.current()); ++it ) {
int lp = r->yPos() + r->lowestPosition(false);
bottom = max(bottom, lp);
}
}
if (!includeSelf && lastLineBox()) {
int lp = lastLineBox()->yPos() + lastLineBox()->height();
bottom = max(bottom, lp);
}
return bottom;
}
int RenderBlock::rightmostPosition(bool includeOverflowInterior, bool includeSelf) const
{
int right = RenderFlow::rightmostPosition(includeOverflowInterior, includeSelf);
if (!includeOverflowInterior && hasOverflowClip())
return right;
if (includeSelf && m_overflowWidth > right)
right = m_overflowWidth;
if (m_floatingObjects) {
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it ) {
if (!r->noPaint || r->node->layer()) {
int rp = r->left + r->node->marginLeft() + r->node->rightmostPosition(false);
right = max(right, rp);
}
}
}
if (m_positionedObjects && !isRenderView()) {
RenderObject* r;
DeprecatedPtrListIterator<RenderObject> it(*m_positionedObjects);
for ( ; (r = it.current()); ++it ) {
int rp = r->xPos() + r->rightmostPosition(false);
right = max(right, rp);
}
}
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
{
int left = RenderFlow::leftmostPosition(includeOverflowInterior, includeSelf);
if (!includeOverflowInterior && hasOverflowClip())
return left;
if (includeSelf && m_overflowLeft < left)
left = m_overflowLeft;
if (m_floatingObjects) {
FloatingObject* r;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for ( ; (r = it.current()); ++it ) {
if (!r->noPaint || r->node->layer()) {
int lp = r->left + r->node->marginLeft() + r->node->leftmostPosition(false);
left = min(left, lp);
}
}
}
if (m_positionedObjects && !isRenderView()) {
RenderObject* r;
DeprecatedPtrListIterator<RenderObject> it(*m_positionedObjects);
for ( ; (r = it.current()); ++it ) {
int lp = r->xPos() + r->leftmostPosition(false);
left = min(left, lp);
}
}
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->endY > bottom && r->type() == FloatingObject::FloatLeft)
bottom=r->endY;
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->endY>bottom && r->type() == FloatingObject::FloatRight)
bottom=r->endY;
return bottom;
}
void
RenderBlock::clearFloats()
{
if (m_floatingObjects)
m_floatingObjects->clear();
if (avoidsFloats() || isRoot() || isRenderView() || isFloatingOrPositioned() || isTableCell())
return;
bool parentHasFloats = false;
RenderObject *prev = previousSibling();
while (prev && (!prev->isRenderBlock() || prev->avoidsFloats() || prev->isFloatingOrPositioned())) {
if (prev->isFloating())
parentHasFloats = true;
prev = prev->previousSibling();
}
int offset = m_y;
if (parentHasFloats)
addIntrudingFloats(static_cast<RenderBlock *>(parent()),
parent()->borderLeft() + parent()->paddingLeft(), offset);
int xoffset = 0;
if (prev)
offset -= prev->yPos();
else {
prev = parent();
xoffset += prev->borderLeft() + prev->paddingLeft();
}
if (!prev->isRenderBlock()) return;
RenderBlock* block = static_cast<RenderBlock *>(prev);
if (!block->m_floatingObjects) return;
if (block->floatBottom() > offset)
addIntrudingFloats(block, xoffset, offset);
}
void RenderBlock::addOverhangingFloats(RenderBlock* child, int xoff, int yoff)
{
if (child->hasOverflowClip() || !child->hasOverhangingFloats() || child->isRoot())
return;
ASSERT(child->m_floatingObjects);
if (!child->m_floatingObjects)
return;
DeprecatedPtrListIterator<FloatingObject> it(*child->m_floatingObjects);
for (FloatingObject *r; (r = it.current()); ++it) {
if (child->yPos() + r->endY > height()) {
FloatingObject* f = 0;
if (m_floatingObjects) {
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
while ((f = it.current())) {
if (f->node == r->node) break;
++it;
}
}
if (!f) {
FloatingObject *floatingObj = new FloatingObject(r->type());
floatingObj->startY = r->startY - yoff;
floatingObj->endY = r->endY - yoff;
floatingObj->left = r->left - xoff;
floatingObj->width = r->width;
floatingObj->node = r->node;
if (r->node->enclosingLayer() == enclosingLayer())
r->noPaint = true;
else
floatingObj->noPaint = true;
if (!m_floatingObjects) {
m_floatingObjects = new DeprecatedPtrList<FloatingObject>;
m_floatingObjects->setAutoDelete(true);
}
m_floatingObjects->append(floatingObj);
}
}
}
}
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->endY > yoff) {
FloatingObject* f = 0;
if (m_floatingObjects) {
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
while ((f = it.current())) {
if (f->node == r->node) break;
++it;
}
}
if (!f) {
FloatingObject *floatingObj = new FloatingObject(r->type());
floatingObj->startY = r->startY - yoff;
floatingObj->endY = r->endY - yoff;
floatingObj->left = r->left - xoff;
if (prev != parent())
floatingObj->left += prev->marginLeft();
floatingObj->left -= marginLeft();
floatingObj->noPaint = true; floatingObj->width = r->width;
floatingObj->node = r->node;
if (!m_floatingObjects) {
m_floatingObjects = new DeprecatedPtrList<FloatingObject>;
m_floatingObjects->setAutoDelete(true);
}
m_floatingObjects->append(floatingObj);
}
}
}
}
bool RenderBlock::containsFloat(RenderObject* o)
{
if (m_floatingObjects) {
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
while (it.current()) {
if (it.current()->node == o)
return true;
++it;
}
}
return false;
}
void RenderBlock::markAllDescendantsWithFloatsForLayout(RenderObject* floatToRemove)
{
setChildNeedsLayout(true);
if (floatToRemove)
removeFloatingObject(floatToRemove);
if (!childrenInline()) {
for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
if (isBlockFlow() && !child->isFloatingOrPositioned() &&
((floatToRemove ? child->containsFloat(floatToRemove) : child->containsFloats()) || child->shrinkToAvoidFloats()))
child->markAllDescendantsWithFloatsForLayout(floatToRemove);
}
}
}
int RenderBlock::getClearDelta(RenderObject *child)
{
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->yPos()) : 0;
if (!result && child->avoidsFloats() && child->style()->width().isFixed() &&
child->minWidth() > lineWidth(child->yPos()) && child->minWidth() <= contentWidth() &&
document()->inStrictMode())
result = max(0, floatBottom() - child->yPos());
return result;
}
bool RenderBlock::isPointInScrollbar(int _x, int _y, int _tx, int _ty)
{
if (!scrollsOverflow())
return false;
if (m_layer->verticalScrollbarWidth()) {
IntRect vertRect(_tx + width() - borderRight() - m_layer->verticalScrollbarWidth(),
_ty + borderTop() - borderTopExtra(),
m_layer->verticalScrollbarWidth(),
height() + borderTopExtra() + borderBottomExtra() - borderTop() - borderBottom() - m_layer->horizontalScrollbarHeight());
if (vertRect.contains(_x, _y)) {
RenderLayer::gScrollBar = m_layer->verticalScrollbar();
return true;
}
}
if (m_layer->horizontalScrollbarHeight()) {
IntRect horizRect(_tx + borderLeft(),
_ty + height() + borderBottomExtra() - m_layer->horizontalScrollbarHeight() - borderBottom(),
width() - borderLeft() - borderRight() - m_layer->verticalScrollbarWidth(),
m_layer->horizontalScrollbarHeight());
if (horizRect.contains(_x, _y)) {
RenderLayer::gScrollBar = m_layer->horizontalScrollbar();
return true;
}
}
return false;
}
bool RenderBlock::nodeAtPoint(NodeInfo& info, int _x, int _y, int _tx, int _ty, HitTestAction hitTestAction)
{
bool inlineFlow = isInlineFlow();
int tx = _tx + m_x;
int ty = _ty + m_y + borderTopExtra();
if (!inlineFlow && !isRoot()) {
IntRect overflowBox = overflowRect(false);
overflowBox.move(tx, ty);
bool insideOverflowBox = overflowBox.contains(_x, _y);
if (!insideOverflowBox) {
if (hitTestAction != HitTestFloat)
return false;
IntRect floatBox = floatRect();
floatBox.move(tx, ty);
if (!floatBox.contains(_x, _y))
return false;
}
}
if (isPointInScrollbar(_x, _y, tx, ty)) {
if (hitTestAction == HitTestBlockBackground) {
setInnerNode(info);
return true;
}
return false;
}
int scrolledX = tx;
int scrolledY = ty;
if (hasOverflowClip())
m_layer->subtractScrollOffset(scrolledX, scrolledY);
if (childrenInline() && !isTable()) {
if (hitTestLines(info, _x, _y, scrolledX, scrolledY, hitTestAction)) {
setInnerNode(info);
return true;
}
}
else {
HitTestAction childHitTest = hitTestAction;
if (hitTestAction == HitTestChildBlockBackgrounds)
childHitTest = HitTestChildBlockBackground;
for (RenderObject* child = lastChild(); child; child = child->previousSibling())
if (!child->layer() && !child->isFloating() && !child->isInlineFlow() && child->nodeAtPoint(info, _x, _y, scrolledX, scrolledY, childHitTest)) {
setInnerNode(info);
return true;
}
}
if (hitTestAction == HitTestFloat && m_floatingObjects) {
if (isRenderView()) {
scrolledX += static_cast<RenderView*>(this)->frameView()->contentsX();
scrolledY += static_cast<RenderView*>(this)->frameView()->contentsY();
}
FloatingObject* o;
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
for (it.toLast(); (o = it.current()); --it)
if (!o->noPaint && !o->node->layer() && o->node->hitTest(info, _x, _y,
scrolledX + o->left + o->node->marginLeft() - o->node->xPos(),
scrolledY + o->startY + o->node->marginTop() - o->node->yPos())) {
setInnerNode(info);
return true;
}
}
if (!inlineFlow && (hitTestAction == HitTestBlockBackground || hitTestAction == HitTestChildBlockBackground)) {
int topExtra = borderTopExtra();
IntRect boundsRect(tx, ty - topExtra, m_width, m_height + topExtra + borderBottomExtra());
if (isRoot() || (style()->visibility() == VISIBLE && boundsRect.contains(_x, _y))) {
setInnerNode(info);
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();
int offset = start ? node->caretMinOffset() : node->caretMaxOffset();
return Position(node, offset);
}
VisiblePosition RenderBlock::positionForCoordinates(int x, int y)
{
if (isTable())
return RenderFlow::positionForCoordinates(x, y);
int absx, absy;
absolutePositionForContent(absx, absy);
int top = absy + borderTop() + paddingTop();
int bottom = top + contentHeight();
Node* n = element();
if (y < top) {
if (childrenInline()) {
if (!firstRootBox())
return VisiblePosition(n, 0, DOWNSTREAM);
InlineBox* closestBox = firstRootBox()->closestLeafChildForXPos(x, absx);
if (closestBox)
return closestBox->object()->positionForCoordinates(x, absy + closestBox->m_y);
}
else {
if (firstChild())
return firstChild()->positionForCoordinates(x, y);
}
}
else if (y >= bottom) {
if (childrenInline()) {
if (!lastRootBox())
return VisiblePosition(n, 0, DOWNSTREAM);
InlineBox* closestBox = lastRootBox()->closestLeafChildForXPos(x, absx);
if (closestBox) {
if (document()->frame()->singleLineSelectionBehavior()) {
return closestBox->object()->positionForCoordinates(x, absy + closestBox->m_y);
}
else {
return closestBox->object()->positionForCoordinates(x, y);
}
}
}
else {
if (lastChild())
return lastChild()->positionForCoordinates(x, y);
}
}
if (childrenInline()) {
if (!firstRootBox())
return VisiblePosition(n, 0, DOWNSTREAM);
int contentsX = absx;
int contentsY = absy;
if (hasOverflowClip())
layer()->subtractScrollOffset(contentsX, contentsY);
if (y < contentsY + firstRootBox()->topOverflow() - verticalLineClickFudgeFactor)
{
InlineBox* closestBox = firstRootBox()->closestLeafChildForXPos(x, absx);
if (closestBox)
return closestBox->object()->positionForCoordinates(x, absy + closestBox->m_y);
}
for (RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox()) {
if (root->nextRootBox())
bottom = contentsY + root->nextRootBox()->topOverflow();
else
bottom = contentsY + root->bottomOverflow() + verticalLineClickFudgeFactor;
if (y < bottom && root->firstChild()) {
InlineBox* closestBox = root->closestLeafChildForXPos(x, contentsX);
if (closestBox)
return closestBox->object()->positionForCoordinates(x, contentsY + closestBox->m_y);
}
}
if (lastRootBox())
{
InlineBox* closestBox = lastRootBox()->closestLeafChildForXPos(x, absx);
if (closestBox)
return closestBox->object()->positionForCoordinates(x, absy + closestBox->m_y);
}
return VisiblePosition(n, 0, DOWNSTREAM);
}
for (RenderObject* renderer = firstChild(); renderer; renderer = renderer->nextSibling()) {
if (renderer->height() == 0 || renderer->style()->visibility() != VISIBLE || renderer->isFloatingOrPositioned())
continue;
renderer->absolutePositionForContent(absx, top);
RenderObject* next = renderer->nextSibling();
while (next && next->isFloatingOrPositioned())
next = next->nextSibling();
if (next)
next->absolutePositionForContent(absx, bottom);
else
bottom = top + contentHeight();
if (y >= top && y < bottom)
return renderer->positionForCoordinates(x, y);
}
return RenderFlow::positionForCoordinates(x, y);
}
void RenderBlock::calcMinMaxWidth()
{
ASSERT( !minMaxKnown() );
#ifdef DEBUG_LAYOUT
kdDebug( 6040 ) << renderName() << "(RenderBlock)::calcMinMaxWidth() this=" << this << endl;
#endif
m_minWidth = 0;
m_maxWidth = 0;
if (childrenInline())
calcInlineMinMaxWidth();
else
calcBlocminMaxWidth();
if(m_maxWidth < m_minWidth) m_maxWidth = m_minWidth;
if (!style()->autoWrap() && childrenInline()) {
m_minWidth = m_maxWidth;
if (m_layer && m_layer->marquee() && m_layer->marquee()->isHorizontal())
m_minWidth = 0;
}
if (isTableCell()) {
Length w = static_cast<RenderTableCell*>(this)->styleOrColWidth();
if (w.isFixed() && w.value() > 0)
m_maxWidth = max(m_minWidth, calcContentBoxWidth(w.value()));
} else if (style()->width().isFixed() && style()->width().value() > 0)
m_minWidth = m_maxWidth = calcContentBoxWidth(style()->width().value());
if (style()->minWidth().isFixed() && style()->minWidth().value() > 0) {
m_maxWidth = max(m_maxWidth, calcContentBoxWidth(style()->minWidth().value()));
m_minWidth = max(m_minWidth, calcContentBoxWidth(style()->minWidth().value()));
}
if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength) {
m_maxWidth = min(m_maxWidth, calcContentBoxWidth(style()->maxWidth().value()));
m_minWidth = min(m_minWidth, calcContentBoxWidth(style()->maxWidth().value()));
}
int toAdd = 0;
toAdd = borderLeft() + borderRight() + paddingLeft() + paddingRight();
m_minWidth += toAdd;
m_maxWidth += toAdd;
setMinMaxKnown();
}
struct InlineMinMaxIterator
{
RenderObject* parent;
RenderObject* current;
bool endOfInline;
InlineMinMaxIterator(RenderObject* p, RenderObject* o, bool end = false)
:parent(p), current(o), endOfInline(end) {}
RenderObject* next();
};
RenderObject* InlineMinMaxIterator::next()
{
RenderObject* result = 0;
bool oldEndOfInline = endOfInline;
endOfInline = false;
while (current != 0 || (current == parent))
{
if (!oldEndOfInline &&
(current == parent ||
(!current->isFloating() && !current->isReplaced() && !current->isPositioned())))
result = current->firstChild();
if (!result) {
if (!oldEndOfInline && current->isInlineFlow()) {
result = current;
endOfInline = true;
break;
}
while (current && current != parent) {
result = current->nextSibling();
if (result) break;
current = current->parent();
if (current && current != parent && current->isInlineFlow()) {
result = current;
endOfInline = true;
break;
}
}
}
if (!result) break;
if (!result->isPositioned() && (result->isText() || result->isBR() ||
result->isFloating() || result->isReplaced() ||
result->isInlineFlow()))
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(RenderObject* 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 = static_cast<RenderText *>(trailingSpaceChild);
const Font *f = t->font(false); const UChar space = ' ';
int spaceWidth = f->width(TextRun(&space, 1));
inlineMax -= spaceWidth;
if (inlineMin > inlineMax)
inlineMin = inlineMax;
}
}
void RenderBlock::calcInlineMinMaxWidth()
{
int inlineMax=0;
int inlineMin=0;
int cw = containingBlock()->contentWidth();
bool stripFrontSpaces = true;
RenderObject* trailingSpaceChild = 0;
bool autoWrap, oldAutoWrap;
autoWrap = oldAutoWrap = style()->autoWrap();
InlineMinMaxIterator childIterator(this, this);
bool addedTextIndent = false; RenderObject* prevFloat = 0;
while (RenderObject* child = childIterator.next())
{
autoWrap = child->style()->autoWrap();
if (!child->isBR()) {
RenderStyle* cstyle = child->style();
int childMin = 0;
int childMax = 0;
if (!child->isText()) {
if (child->isInlineFlow()) {
int bpm = getBorderPaddingMargin(child, childIterator.endOfInline);
childMin += bpm;
childMax += bpm;
inlineMin += childMin;
inlineMax += childMax;
}
else {
int margins = 0;
Length leftMargin = cstyle->marginLeft();
Length rightMargin = cstyle->marginRight();
bool useCalculatedWidths = child->isListMarker();
if (leftMargin.isPercent() || rightMargin.isPercent() || useCalculatedWidths)
child->calcWidth();
if (useCalculatedWidths || leftMargin.isPercent())
margins += child->marginLeft();
else if (leftMargin.isFixed())
margins += leftMargin.value();
if (useCalculatedWidths || rightMargin.isPercent())
margins += child->marginRight();
else if (rightMargin.isFixed())
margins += rightMargin.value();
childMin += margins;
childMax += margins;
}
}
if (!child->isRenderInline() && !child->isText()) {
childMin += child->minWidth();
childMax += child->maxWidth();
if (autoWrap || oldAutoWrap) {
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
inlineMin = 0;
}
if (child->isFloating()) {
if (prevFloat &&
((prevFloat->style()->floating() == FLEFT && (child->style()->clear() & CLEFT)) ||
(prevFloat->style()->floating() == FRIGHT && (child->style()->clear() & CRIGHT)))) {
m_maxWidth = max(inlineMax, m_maxWidth);
inlineMax = 0;
}
prevFloat = child;
}
int ti = 0;
if (!addedTextIndent) {
addedTextIndent = true;
ti = style()->textIndent().calcMinValue(cw);
childMin+=ti;
childMax+=ti;
}
inlineMax += childMax;
if (!autoWrap)
inlineMin += childMin;
else {
inlineMin = childMin;
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
inlineMin = 0;
}
if (!child->isFloating()) {
stripFrontSpaces = false;
trailingSpaceChild = 0;
}
}
else if (child->isText())
{
RenderText* t = static_cast<RenderText *>(child);
bool hasBreakableChar, hasBreak;
int beginMin, endMin;
bool beginWS, endWS;
int beginMax, endMax;
t->trimmedMinMaxWidth(inlineMax, beginMin, beginWS, endMin, endWS,
hasBreakableChar, hasBreak, beginMax, endMax,
childMin, childMax, stripFrontSpaces);
if (!hasBreak && childMax == 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) {
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
}
else {
inlineMin += beginMin;
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
childMin -= ti;
}
inlineMin = childMin;
if (endWS) {
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
inlineMin = 0;
}
else {
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
inlineMin = endMin;
}
}
if (hasBreak) {
inlineMax += beginMax;
if (m_maxWidth < inlineMax) m_maxWidth = inlineMax;
if (m_maxWidth < childMax) m_maxWidth = childMax;
inlineMax = endMax;
}
else
inlineMax += childMax;
}
}
else
{
if(m_minWidth < inlineMin) m_minWidth = inlineMin;
if(m_maxWidth < inlineMax) m_maxWidth = inlineMax;
inlineMin = inlineMax = 0;
stripFrontSpaces = true;
trailingSpaceChild = 0;
}
oldAutoWrap = autoWrap;
}
if (style()->collapseWhiteSpace())
stripTrailingSpace(inlineMax, inlineMin, trailingSpaceChild);
m_minWidth = max(inlineMin, m_minWidth);
m_maxWidth = max(inlineMax, m_maxWidth);
}
#define BLOCK_MAX_WIDTH 15000
void RenderBlock::calcBlocminMaxWidth()
{
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_maxWidth = max(floatTotalWidth, m_maxWidth);
floatLeftWidth = 0;
}
if (child->style()->clear() & CRIGHT) {
m_maxWidth = max(floatTotalWidth, m_maxWidth);
floatRightWidth = 0;
}
}
Length ml = child->style()->marginLeft();
Length mr = child->style()->marginRight();
if (ml.isPercent() || mr.isPercent())
calcWidth();
int margin = 0, marginLeft = 0, marginRight = 0;
if (ml.isFixed())
marginLeft += ml.value();
else if (ml.isPercent())
marginLeft += child->marginLeft();
if (mr.isFixed())
marginRight += mr.value();
else if (mr.isPercent())
marginRight += child->marginRight();
margin = marginLeft + marginRight;
int w = child->minWidth() + margin;
if (m_minWidth < w) m_minWidth = w;
if (nowrap && !child->isTable() && m_maxWidth < w)
m_maxWidth = w;
w = child->maxWidth() + 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->maxWidth() + maxLeft + maxRight;
w = max(w, floatLeftWidth + floatRightWidth);
}
else
m_maxWidth = max(floatLeftWidth + floatRightWidth, m_maxWidth);
floatLeftWidth = floatRightWidth = 0;
}
if (child->isFloating()) {
if (style()->floating() == FLEFT)
floatLeftWidth += w;
else
floatRightWidth += w;
}
else if (m_maxWidth < w)
m_maxWidth = w;
if (style()->htmlHacks() && child->style()->width().isPercent() &&
!isTableCell() && child->isTable() && m_maxWidth < BLOCK_MAX_WIDTH) {
RenderBlock* cb = containingBlock();
while (!cb->isRenderView() && !cb->isTableCell())
cb = cb->containingBlock();
if (!cb->isTableCell())
m_maxWidth = BLOCK_MAX_WIDTH;
}
child = child->nextSibling();
}
m_minWidth = max(0, m_minWidth);
m_maxWidth = max(0, m_maxWidth);
m_maxWidth = max(floatLeftWidth + floatRightWidth, m_maxWidth);
}
bool RenderBlock::hasLineIfEmpty() const
{
return element() && (element()->isContentEditable() && element()->rootEditableElement() == element() ||
element()->isShadowNode() && element()->shadowParentNode()->hasTagName(inputTag));
}
short RenderBlock::lineHeight(bool b, bool isRootLineBox) const
{
if (isReplaced() && !isRootLineBox)
return height()+marginTop()+marginBottom();
return RenderFlow::lineHeight(b, isRootLineBox);
}
short 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 (m_firstLineBox)
return m_firstLineBox->yPos() + m_firstLineBox->baseline();
else
return -1;
}
else {
for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling()) {
if (!curr->isFloatingOrPositioned()) {
int result = curr->getBaselineOfFirstLineBox();
if (result != -1)
return curr->yPos() + result; }
}
}
return -1;
}
int RenderBlock::getBaselineOfLastLineBox() const
{
if (!isBlockFlow())
return RenderFlow::getBaselineOfLastLineBox();
if (childrenInline()) {
if (!firstLineBox() && hasLineIfEmpty())
return RenderFlow::baselinePosition(true) + borderTop() + paddingTop();
if (m_lastLineBox)
return m_lastLineBox->yPos() + m_lastLineBox->baseline();
return -1;
}
else {
for (RenderObject* curr = lastChild(); curr; curr = curr->previousSibling()) {
if (!curr->isFloatingOrPositioned()) {
int result = curr->getBaselineOfLastLineBox();
if (result != -1)
return curr->yPos() + result; }
}
}
return -1;
}
RenderBlock* RenderBlock::firstLineBlock() const
{
const RenderObject* firstLineBlock = 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;
firstLineBlock = parentBlock;
}
if (!hasPseudo)
return 0;
return (RenderBlock*)(firstLineBlock);
}
void RenderBlock::updateFirstLetter()
{
RenderObject* firstLetterBlock = this;
bool hasPseudoStyle = false;
while (true) {
hasPseudoStyle = firstLetterBlock->style()->hasPseudoStyle(RenderStyle::FIRST_LETTER);
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->isText())
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->getPseudoStyle(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) {
RenderText* textObj = static_cast<RenderText*>(currChild);
RenderStyle* pseudoStyle = firstLetterBlock->getPseudoStyle(RenderStyle::FIRST_LETTER,
firstLetterContainer->firstLineStyle());
pseudoStyle->setDisplay( pseudoStyle->isFloating() ? BLOCK : INLINE);
pseudoStyle->setPosition( StaticPosition );
RenderObject* firstLetter = RenderFlow::createAnonymousFlow(document(), pseudoStyle); firstLetterContainer->addChild(firstLetter, firstLetterContainer->firstChild());
RefPtr<StringImpl> oldText = textObj->originalString();
ASSERT(oldText);
if (oldText && oldText->length() > 0) {
unsigned int length = 0;
while (length < oldText->length() && (DeprecatedChar((*oldText)[length]).isSpace() || u_ispunct((*oldText)[length])))
length++;
length++;
RenderTextFragment* remainingText =
new (renderArena()) RenderTextFragment(textObj->node(), oldText.get(), length, oldText->length() - length, firstLetter);
remainingText->setStyle(textObj->style());
if (remainingText->element())
remainingText->element()->setRenderer(remainingText);
RenderObject* nextObj = textObj->nextSibling();
firstLetterContainer->removeChild(textObj);
firstLetterContainer->addChild(remainingText, nextObj);
RenderTextFragment* letter =
new (renderArena()) RenderTextFragment(remainingText->node(), oldText.get(), 0, length);
RenderStyle* newStyle = new (renderArena()) RenderStyle();
newStyle->inheritFrom(pseudoStyle);
letter->setStyle(newStyle);
firstLetter->addChild(letter);
textObj->destroy();
}
}
}
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->isCompact() && !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;
}
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 {
RenderObject* normalFlowChildWithoutLines = 0;
for (RenderObject* obj = block->firstChild(); obj; obj = obj->nextSibling()) {
if (shouldCheckLines(obj)) {
int result = getHeightForLineCount(static_cast<RenderBlock*>(obj), l, false, count);
if (result != -1)
return result + obj->yPos() + (includeBottom ? (block->borderBottom() + block->paddingBottom()) : 0);
}
else if (!obj->isFloatingOrPositioned() && !obj->isCompact() && !obj->isRunIn())
normalFlowChildWithoutLines = obj;
}
if (normalFlowChildWithoutLines && l == 0)
return normalFlowChildWithoutLines->yPos() + 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::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();
}
}
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 (isCompact())
return "RenderBlock (compact)";
if (isRunIn())
return "RenderBlock (run-in)";
return "RenderBlock";
}
#ifndef NDEBUG
void RenderBlock::dump(TextStream *stream, DeprecatedString ind) const
{
if (m_childrenInline) { *stream << " childrenInline"; }
if (m_firstLine) { *stream << " firstLine"; }
if (m_floatingObjects && !m_floatingObjects->isEmpty())
{
*stream << " special(";
DeprecatedPtrListIterator<FloatingObject> it(*m_floatingObjects);
FloatingObject *r;
bool first = true;
for ( ; (r = it.current()); ++it )
{
if (!first)
*stream << ",";
*stream << r->node->renderName();
first = false;
}
*stream << ")";
}
RenderFlow::dump(stream,ind);
}
#endif
#define ParagraphMinWordCount 6
#define ParagraphMinLineCount 2
#define ParagraphMaxWidth 0.9
inline static bool isVisibleRenderText(RenderObject *render)
{
return render->isText() && render->width() != 0 && render->height() != 0 && !static_cast<RenderText *>(render)->string()->containsOnlyWhitespace();
}
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.0/log10(specifiedSize) * 1.7),1.0))
#define TEXT_MULTIPLIER (MAX((1.0/log10(specifiedSize) * 1.95),1.0))
void RenderBlock::adjustComputedFontSizes(float size, float visibleWidth)
{
if (visibleWidth >= m_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 width = m_widthForTextAutosizing != -1 ? (float)m_widthForTextAutosizing : (float)m_width;
float scale = visibleWidth / width;
float minFontSize = roundf(size / scale);
for (RenderObject *descendent = traverseNext(this, includeNonBlocksOrListItems); descendent; descendent = descendent->traverseNext(this, includeNonBlocksOrListItems)) {
if (isVisibleRenderText(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 = width;
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);
}
}
}
}
}