#include "config.h"
#include "RenderBox.h"
#include "CachedImage.h"
#include "Document.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "RenderArena.h"
#include "RenderView.h"
#include "RenderFlexibleBox.h"
#include "RenderTableCell.h"
#include "RenderTheme.h"
#include <assert.h>
#include <algorithm>
#include <math.h>
#if PLATFORM(MAC)
#include "FrameMac.h"
#endif
using namespace std;
namespace WebCore {
using namespace HTMLNames;
#define TABLECELLMARGIN -0x4000
RenderBox::RenderBox(Node* node)
: RenderObject(node)
{
m_minWidth = -1;
m_maxWidth = -1;
m_overrideSize = -1;
m_width = m_height = 0;
m_x = 0;
m_y = 0;
m_marginTop = 0;
m_marginBottom = 0;
m_marginLeft = 0;
m_marginRight = 0;
m_staticX = 0;
m_staticY = 0;
m_layer = 0;
m_inlineBoxWrapper = 0;
}
static RenderBlock* blockThatPaintsFloat(RenderObject* f)
{
RenderBlock* lastBlock = 0;
for (RenderObject* o = f->parent(); o; o = o->parent()) {
if (lastBlock && o->layer())
break;
if (o->isRenderBlock())
lastBlock = static_cast<RenderBlock*>(o);
}
return lastBlock;
}
void RenderBox::setStyle(RenderStyle *_style)
{
bool wasFloating = isFloating();
RenderObject::setStyle(_style);
if (isRoot())
setShouldPaintBackgroundOrBorder(true);
setInline(_style->isDisplayInlineType());
switch (_style->position()) {
case AbsolutePosition:
case FixedPosition:
setPositioned(true);
break;
default:
setPositioned(false);
if (_style->isFloating())
setFloating(true);
if (_style->position() == RelativePosition)
setRelPositioned(true);
}
if (!isRoot() && (!isBody() || !document()->isHTMLDocument()) && (isRenderBlock() || isTableRow() || isTableSection())) {
if (_style->overflowX() != OVISIBLE)
setHasOverflowClip();
}
if (requiresLayer()) {
if (!m_layer) {
if (wasFloating && isFloating()) {
if (RenderBlock* b = blockThatPaintsFloat(this))
b->setPaintsFloatingObject(this, false);
}
m_layer = new (renderArena()) RenderLayer(this);
m_layer->insertOnlyThisLayer();
if (parent() && !needsLayout() && containingBlock())
m_layer->updateLayerPositions();
}
} else if (m_layer && !isRoot() && !isRenderView()) {
assert(m_layer->parent());
RenderLayer* layer = m_layer;
m_layer = 0;
layer->removeOnlyThisLayer();
if (wasFloating && isFloating()) {
if (RenderBlock* b = blockThatPaintsFloat(this))
b->setPaintsFloatingObject(this, true);
}
}
if (m_layer)
m_layer->styleChanged();
if (isBody())
element()->document()->setTextColor(_style->color());
if (style()->outlineWidth() > 0 && style()->outlineSize() > maximalOutlineSize(PaintPhaseOutline))
static_cast<RenderView*>(document()->renderer())->setMaximalOutlineSize(style()->outlineSize());
}
RenderBox::~RenderBox()
{
}
void RenderBox::destroy()
{
RenderLayer* layer = m_layer;
RenderArena* arena = renderArena();
if (layer)
layer->clearClipRect();
RenderObject::destroy();
if (layer)
layer->destroy(arena);
}
int RenderBox::contentWidth() const
{
int w = m_width - (borderLeft() + borderRight() + paddingLeft() + paddingRight());
if (includeVerticalScrollbarSize())
w -= m_layer->verticalScrollbarWidth();
return w;
}
int RenderBox::contentHeight() const
{
int h = m_height - (borderTop() + borderBottom() + paddingTop() + paddingBottom());
if (includeHorizontalScrollbarSize())
h -= m_layer->horizontalScrollbarHeight();
return h;
}
int RenderBox::overrideWidth() const
{
return m_overrideSize == -1 ? m_width : m_overrideSize;
}
int RenderBox::overrideHeight() const
{
return m_overrideSize == -1 ? m_height : m_overrideSize;
}
void RenderBox::setPos(int xPos, int yPos)
{
if (xPos == m_x && yPos == m_y)
return;
m_x = xPos;
m_y = yPos;
}
int RenderBox::width() const
{
return m_width;
}
int RenderBox::height() const
{
return m_height;
}
int RenderBox::calcBorderBoxWidth(int w) const
{
int toAdd = borderLeft() + borderRight() + paddingLeft() + paddingRight();
if (style()->boxSizing() == CONTENT_BOX)
return w + toAdd;
return max(w, toAdd);
}
int RenderBox::calcBorderBoxHeight(int h) const
{
int toAdd = borderTop() + borderBottom() + paddingTop() + paddingBottom();
if (style()->boxSizing() == CONTENT_BOX)
return h + toAdd;
return max(h, toAdd);
}
int RenderBox::calcContentBoxWidth(int w) const
{
if (style()->boxSizing() == BORDER_BOX)
w -= (borderLeft() + borderRight() + paddingLeft() + paddingRight());
return max(0, w);
}
int RenderBox::calcContentBoxHeight(int h) const
{
if (style()->boxSizing() == BORDER_BOX)
h -= (borderTop() + borderBottom() + paddingTop() + paddingBottom());
return max(0, h);
}
bool RenderBox::nodeAtPoint(NodeInfo& info, int x, int y, int tx, int ty, HitTestAction action)
{
tx += m_x;
ty += m_y;
for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
if (!child->layer() && !child->isInlineFlow() && child->nodeAtPoint(info, x, y, tx, ty, action)) {
setInnerNode(info);
return true;
}
}
if (action == HitTestForeground && IntRect(tx, ty, m_width, m_height).contains(x, y)) {
setInnerNode(info);
return true;
}
return false;
}
void RenderBox::paint(PaintInfo& i, int _tx, int _ty)
{
_tx += m_x;
_ty += m_y;
for (RenderObject* child = firstChild(); child; child = child->nextSibling())
child->paint(i, _tx, _ty);
}
void RenderBox::paintRootBoxDecorations(PaintInfo& i, int _tx, int _ty)
{
const BackgroundLayer* bgLayer = style()->backgroundLayers();
Color bgColor = style()->backgroundColor();
if (document()->isHTMLDocument() && !style()->hasBackground()) {
HTMLElement* body = document()->body();
RenderObject* bodyObject = (body && body->hasLocalName(bodyTag)) ? body->renderer() : 0;
if (bodyObject) {
bgLayer = bodyObject->style()->backgroundLayers();
bgColor = bodyObject->style()->backgroundColor();
}
}
int w = width();
int h = height();
int rw, rh;
if (view()->frameView()) {
rw = view()->frameView()->contentsWidth();
rh = view()->frameView()->contentsHeight();
}
else {
rw = view()->width();
rh = view()->height();
}
int bx = _tx - marginLeft();
int by = _ty - marginTop();
int bw = max(w + marginLeft() + marginRight() + borderLeft() + borderRight(), rw);
int bh = max(h + marginTop() + marginBottom() + borderTop() + borderBottom(), rh);
int my = max(by, i.r.y());
paintBackgrounds(i.p, bgColor, bgLayer, my, i.r.height(), bx, by, bw, bh);
if (style()->hasBorder() && style()->display() != INLINE)
paintBorder( i.p, _tx, _ty, w, h, style() );
}
void RenderBox::paintBoxDecorations(PaintInfo& i, int _tx, int _ty)
{
if (!shouldPaintWithinRoot(i))
return;
if (isRoot())
return paintRootBoxDecorations(i, _tx, _ty);
int w = width();
int h = height() + borderTopExtra() + borderBottomExtra();
_ty -= borderTopExtra();
int my = max(_ty, i.r.y());
int mh;
if (_ty < i.r.y())
mh = max(0, h - (i.r.y() - _ty));
else
mh = min(i.r.height(), h);
bool themePainted = style()->hasAppearance() && !theme()->paint(this, i, IntRect(_tx, _ty, w, h));
if (!themePainted) {
if (!isBody() || !document()->isHTMLDocument() || document()->documentElement()->renderer()->style()->hasBackground())
paintBackgrounds(i.p, style()->backgroundColor(), style()->backgroundLayers(), my, mh, _tx, _ty, w, h);
if (style()->hasAppearance())
theme()->paintDecorations(this, i, IntRect(_tx, _ty, w, h));
}
if ((!style()->hasAppearance() || (!themePainted && theme()->paintBorderOnly(this, i, IntRect(_tx, _ty, w, h)))) && style()->hasBorder())
paintBorder(i.p, _tx, _ty, w, h, style());
}
void RenderBox::paintBackgrounds(GraphicsContext* p, const Color& c, const BackgroundLayer* bgLayer, int clipy, int cliph, int _tx, int _ty, int w, int height)
{
if (!bgLayer) return;
paintBackgrounds(p, c, bgLayer->next(), clipy, cliph, _tx, _ty, w, height);
paintBackground(p, c, bgLayer, clipy, cliph, _tx, _ty, w, height);
}
void RenderBox::paintBackground(GraphicsContext* p, const Color& c, const BackgroundLayer* bgLayer, int clipy, int cliph, int _tx, int _ty, int w, int height)
{
paintBackgroundExtended(p, c, bgLayer, clipy, cliph, _tx, _ty, w, height,
borderLeft(), borderRight(), paddingLeft(), paddingRight());
}
static void cacluateBackgroundSize(const BackgroundLayer* bgLayer, int& scaledWidth, int& scaledHeight)
{
CachedImage* bg = bgLayer->backgroundImage();
if (bgLayer->isBackgroundSizeSet()) {
Length bgWidth = bgLayer->backgroundSize().width;
Length bgHeight = bgLayer->backgroundSize().height;
if (bgWidth.isPercent())
scaledWidth = scaledWidth * bgWidth.value() / 100;
else if (bgWidth.isFixed())
scaledWidth = bgWidth.value();
else if (bgWidth.isAuto()) {
if (bgHeight.isPercent()) {
int scaledH = scaledHeight * bgHeight.value() / 100;
scaledWidth = bg->imageSize().width() * scaledH / bg->imageSize().height();
} else if (bgHeight.isFixed())
scaledWidth = bg->imageSize().width() * bgHeight.value() / bg->imageSize().height();
}
if (bgHeight.isPercent())
scaledHeight = scaledHeight * bgHeight.value() / 100;
else if (bgHeight.isFixed())
scaledHeight = bgHeight.value();
else if (bgHeight.isAuto()) {
if (bgWidth.isPercent())
scaledHeight = bg->imageSize().height() * scaledWidth / bg->imageSize().width();
else if (bgWidth.isFixed())
scaledHeight = bg->imageSize().height() * bgWidth.value() / bg->imageSize().width();
else if (bgWidth.isAuto()) {
scaledWidth = bg->imageSize().width();
scaledHeight = bg->imageSize().height();
}
}
} else {
scaledWidth = bg->imageSize().width();
scaledHeight = bg->imageSize().height();
}
}
void RenderBox::paintBackgroundExtended(GraphicsContext* p, const Color& c, const BackgroundLayer* bgLayer, int clipy, int cliph,
int _tx, int _ty, int w, int h,
int bleft, int bright, int pleft, int pright)
{
bool clippedToBorderRadius = false;
if (style()->hasBorderRadius()) {
p->save();
p->addRoundedRectClip(FloatRect(_tx + 0.5, _ty + 0.5, w - 1.0, h - 1.0),
FloatSize(style()->borderTopLeftRadius().width() - 0.5, style()->borderTopLeftRadius().height() - 0.5),
FloatSize(style()->borderTopRightRadius().width() - 0.5, style()->borderTopRightRadius().height() - 0.5),
FloatSize(style()->borderBottomLeftRadius().width() - 0.5, style()->borderBottomLeftRadius().height() - 0.5),
FloatSize(style()->borderBottomRightRadius().width() - 0.5, style()->borderBottomRightRadius().height() - 0.5));
clippedToBorderRadius = true;
}
if (bgLayer->backgroundClip() != BGBORDER) {
bool includePadding = bgLayer->backgroundClip() == BGCONTENT;
int x = _tx + bleft + (includePadding ? pleft : 0);
int y = _ty + borderTop() + (includePadding ? paddingTop() : 0);
int width = w - bleft - bright - (includePadding ? pleft + pright : 0);
int height = h - borderTop() - borderBottom() - (includePadding ? paddingTop() + paddingBottom() : 0);
p->save();
p->addClip(IntRect(x, y, width, height));
}
CachedImage* bg = bgLayer->backgroundImage();
bool shouldPaintBackgroundImage = bg && bg->canRender();
Color bgColor = c;
if (style()->forceBackgroundsToWhite()) {
bool shouldPaintBackgroundColor = !bgLayer->next() && bgColor.isValid() && bgColor.alpha() > 0;
if (shouldPaintBackgroundImage || shouldPaintBackgroundColor) {
bgColor = Color::white;
shouldPaintBackgroundImage = false;
}
}
if (!bgLayer->next() && isRoot() && !(bgColor.isValid() && bgColor.alpha() > 0) && view()->frameView()) {
bool isTransparent;
Node* elt = document()->ownerElement();
if (elt) {
if (elt->hasTagName(frameTag))
isTransparent = false;
else {
HTMLElement* body = document()->body();
isTransparent = !body || !body->hasLocalName(framesetTag); }
} else
isTransparent = view()->frameView()->isTransparent();
if (isTransparent)
view()->frameView()->useSlowRepaints(); else
bgColor = Color::white;
}
if (!bgLayer->next() && bgColor.isValid() && bgColor.alpha() > 0) {
IntRect rect(_tx, clipy, w, cliph);
if (bgColor.alpha() < 0xFF && isRoot() && !view()->frameView()->isTransparent())
p->fillRect(rect, Color(Color::white));
p->fillRect(rect, bgColor);
}
if (shouldPaintBackgroundImage) {
int sx = 0;
int sy = 0;
int cw,ch;
int cx,cy;
int scaledImageWidth, scaledImageHeight;
if (bgLayer->backgroundAttachment()) {
int hpab = 0, vpab = 0, left = 0, top = 0; if (bgLayer->backgroundOrigin() != BGBORDER) {
hpab += bleft + bright;
vpab += borderTop() + borderBottom();
left += bleft;
top += borderTop();
if (bgLayer->backgroundOrigin() == BGCONTENT) {
hpab += pleft + pright;
vpab += paddingTop() + paddingBottom();
left += pleft;
top += paddingTop();
}
}
int pw = w - hpab;
int ph = h - vpab;
scaledImageWidth = pw;
scaledImageHeight = ph;
cacluateBackgroundSize(bgLayer, scaledImageWidth, scaledImageHeight);
EBackgroundRepeat bgr = bgLayer->backgroundRepeat();
if ((bgr == NO_REPEAT || bgr == REPEAT_Y) && w > scaledImageWidth) {
cw = scaledImageWidth;
int xPosition = bgLayer->backgroundXPosition().calcMinValue(pw - scaledImageWidth);
if (xPosition >= 0)
cx = _tx + xPosition;
else {
cx = _tx;
if (scaledImageWidth > 0) {
sx = -xPosition;
cw += xPosition;
}
}
cx += left;
} else {
cw = w;
cx = _tx;
if (scaledImageWidth > 0) {
int xPosition = bgLayer->backgroundXPosition().calcMinValue(pw - scaledImageWidth);
if ((xPosition > 0) && (bgr == NO_REPEAT)) {
cx += xPosition;
cw -= xPosition;
} else {
sx = scaledImageWidth - (xPosition % scaledImageWidth);
sx -= left % scaledImageWidth;
}
}
}
if((bgr == NO_REPEAT || bgr == REPEAT_X) && h > scaledImageHeight) {
ch = scaledImageHeight;
int yPosition = bgLayer->backgroundYPosition().calcMinValue(ph - scaledImageHeight);
if (yPosition >= 0)
cy = _ty + yPosition;
else {
cy = _ty;
if (scaledImageHeight > 0) {
sy = -yPosition;
ch += yPosition;
}
}
cy += top;
} else {
ch = h;
cy = _ty;
if (scaledImageHeight > 0) {
int yPosition = bgLayer->backgroundYPosition().calcMinValue(ph - scaledImageHeight);
if ((yPosition > 0) && (bgr == NO_REPEAT)) {
cy += yPosition;
ch -= yPosition;
} else {
sy = scaledImageHeight - (yPosition % scaledImageHeight);
sy -= top % scaledImageHeight;
}
}
}
}
else {
IntRect vr = viewRect();
int pw = vr.width();
int ph = vr.height();
scaledImageWidth = pw;
scaledImageHeight = ph;
cacluateBackgroundSize(bgLayer, scaledImageWidth, scaledImageHeight);
EBackgroundRepeat bgr = bgLayer->backgroundRepeat();
if ((bgr == NO_REPEAT || bgr == REPEAT_Y) && pw > scaledImageWidth) {
cw = scaledImageWidth;
cx = vr.x() + bgLayer->backgroundXPosition().calcMinValue(pw - scaledImageWidth);
} else {
cw = pw;
cx = vr.x();
if (scaledImageWidth > 0)
sx = scaledImageWidth - bgLayer->backgroundXPosition().calcMinValue(pw - scaledImageWidth) % scaledImageWidth;
}
if ((bgr == NO_REPEAT || bgr == REPEAT_X) && ph > scaledImageHeight) {
ch = scaledImageHeight;
cy = vr.y() + bgLayer->backgroundYPosition().calcMinValue(ph - scaledImageHeight);
} else {
ch = ph;
cy = vr.y();
if (scaledImageHeight > 0)
sy = scaledImageHeight - bgLayer->backgroundYPosition().calcMinValue(ph - scaledImageHeight) % scaledImageHeight;
}
IntRect b = intersection(IntRect(cx, cy, cw, ch), IntRect(_tx, _ty, w, h));
sx += b.x() - cx;
sy += b.y() - cy;
cx = b.x();
cy = b.y();
cw = b.width();
ch = b.height();
}
if (cw > 0 && ch > 0) {
p->drawTiledImage(bg->image(), IntRect(cx, cy, cw, ch), IntPoint(sx, sy), IntSize(scaledImageWidth, scaledImageHeight),
bgLayer->backgroundComposite());
if (!p->paintingDisabled())
bg->liveResourceAccessed();
}
}
if (bgLayer->backgroundClip() != BGBORDER)
p->restore();
if (clippedToBorderRadius)
p->restore(); }
#if PLATFORM(MAC)
void RenderBox::paintCustomHighlight(int tx, int ty, const AtomicString& type, bool behindText)
{
InlineBox* boxWrap = inlineBoxWrapper();
RootInlineBox* r = boxWrap ? boxWrap->root() : 0;
if (r) {
FloatRect rootRect(tx + r->xPos(), ty + r->selectionTop(), r->width(), r->selectionHeight());
FloatRect imageRect(tx + m_x, rootRect.y(), width(), rootRect.height());
Mac(document()->frame())->paintCustomHighlight(type, imageRect, rootRect, behindText, false);
} else {
FloatRect imageRect(tx + m_x, ty + m_y, width(), height());
Mac(document()->frame())->paintCustomHighlight(type, imageRect, imageRect, behindText, false);
}
}
#endif
void RenderBox::outlineBox(GraphicsContext* p, int _tx, int _ty, const char* color)
{
p->setPen(Pen(Color(color), 1, Pen::DotLine));
p->setFillColor(Color::transparent);
p->drawRect(IntRect(_tx, _ty, m_width, m_height));
}
IntRect RenderBox::getOverflowClipRect(int tx, int ty)
{
int bl=borderLeft(),bt=borderTop(),bb=borderBottom(),br=borderRight();
int clipx = tx + bl;
int clipy = ty + bt;
int clipw = m_width - bl - br;
int cliph = m_height - bt - bb + borderTopExtra() + borderBottomExtra();
if (m_layer) {
clipw -= m_layer->verticalScrollbarWidth();
cliph -= m_layer->horizontalScrollbarHeight();
}
return IntRect(clipx,clipy,clipw,cliph);
}
IntRect RenderBox::getClipRect(int tx, int ty)
{
int clipx = tx;
int clipy = ty;
int clipw = m_width;
int cliph = m_height;
if (!style()->clipLeft().isAuto()) {
int c = style()->clipLeft().calcValue(m_width);
clipx += c;
clipw -= c;
}
if (!style()->clipRight().isAuto()) {
int w = style()->clipRight().calcValue(m_width);
clipw -= m_width - w;
}
if (!style()->clipTop().isAuto()) {
int c = style()->clipTop().calcValue(m_height);
clipy += c;
cliph -= c;
}
if (!style()->clipBottom().isAuto()) {
int h = style()->clipBottom().calcValue(m_height);
cliph -= m_height - h;
}
return IntRect(clipx, clipy, clipw, cliph);
}
int RenderBox::containingBlockWidth() const
{
RenderBlock* cb = containingBlock();
if (!cb)
return 0;
if (shrinkToAvoidFloats())
return cb->lineWidth(m_y);
else
return cb->contentWidth();
}
bool RenderBox::absolutePosition(int &xPos, int &yPos, bool f)
{
if (style()->position() == FixedPosition)
f = true;
RenderObject *o = container();
if (o && o->absolutePosition(xPos, yPos, f)) {
yPos += o->borderTopExtra();
if (style()->position() == AbsolutePosition && o->isRelPositioned() && o->isInlineFlow()) {
RenderFlow* flow = static_cast<RenderFlow*>(o);
int sx = 0;
int sy = 0;
if (flow->firstLineBox()) {
sx = flow->firstLineBox()->xPos();
sy = flow->firstLineBox()->yPos();
} else {
sx = flow->staticX();
sy = flow->staticY();
}
bool isInlineType = style()->isOriginalDisplayInlineType();
if (!hasStaticX())
xPos += sx;
if (hasStaticX() && !isInlineType)
xPos += sx - (containingBlock()->borderLeft() + containingBlock()->paddingLeft());
if (!hasStaticY())
yPos += sy;
}
if (o->hasOverflowClip())
o->layer()->subtractScrollOffset(xPos, yPos);
if (!isInline() || isReplaced()) {
xPos += m_x;
yPos += m_y;
}
if (isRelPositioned()) {
xPos += relativePositionOffsetX();
yPos += relativePositionOffsetY();
}
return true;
}
else {
xPos = yPos = 0;
return false;
}
}
void RenderBox::dirtyLineBoxes(bool fullLayout, bool)
{
if (m_inlineBoxWrapper) {
if (fullLayout) {
m_inlineBoxWrapper->destroy(renderArena());
m_inlineBoxWrapper = 0;
}
else
m_inlineBoxWrapper->dirtyLineBoxes();
}
}
void RenderBox::position(InlineBox* box, int from, int len, bool reverse, bool override)
{
if (isPositioned()) {
bool wasInline = style()->isOriginalDisplayInlineType();
if (wasInline && hasStaticX()) {
m_staticX = box->xPos();
}
else if (!wasInline && hasStaticY())
m_staticY = box->yPos();
box->remove();
box->destroy(renderArena());
}
else if (isReplaced()) {
m_x = box->xPos();
m_y = box->yPos();
m_inlineBoxWrapper = box;
}
}
InlineBox* RenderBox::inlineBoxWrapper() const
{
return m_inlineBoxWrapper;
}
void RenderBox::deleteLineBoxWrapper()
{
if (m_inlineBoxWrapper) {
if (!documentBeingDestroyed())
m_inlineBoxWrapper->remove();
m_inlineBoxWrapper->destroy(renderArena());
m_inlineBoxWrapper = 0;
}
}
void RenderBox::setInlineBoxWrapper(InlineBox* b)
{
m_inlineBoxWrapper = b;
}
IntRect RenderBox::getAbsoluteRepaintRect()
{
IntRect r = overflowRect(false);
if (style()) {
if (style()->hasAppearance())
theme()->adjustRepaintRect(this, r);
r.inflate(style()->outlineSize()); }
computeAbsoluteRepaintRect(r);
return r;
}
void RenderBox::computeAbsoluteRepaintRect(IntRect& r, bool f)
{
int x = r.x() + m_x;
int y = r.y() + m_y;
if (style()->position() == RelativePosition && m_layer)
m_layer->relativePositionOffset(x,y);
if (style()->position()==FixedPosition)
f = true;
RenderObject* o = container();
if (o) {
if (style()->position() == AbsolutePosition && o->isRelPositioned() && o->isInlineFlow()) {
RenderFlow* flow = static_cast<RenderFlow*>(o);
int sx = 0;
int sy = 0;
if (flow->firstLineBox()) {
sx = flow->firstLineBox()->xPos();
sy = flow->firstLineBox()->yPos();
} else {
sx = flow->staticX();
sy = flow->staticY();
}
bool isInlineType = style()->isOriginalDisplayInlineType();
if (!hasStaticX())
x += sx;
if (hasStaticX() && !isInlineType)
x += sx - (containingBlock()->borderLeft() + containingBlock()->paddingLeft());
if (!hasStaticY())
y += sy;
}
if (o->hasOverflowClip()) {
IntRect boxRect(0, 0, o->layer()->width(), o->layer()->height());
o->layer()->subtractScrollOffset(x,y); IntRect repaintRect(x, y, r.width(), r.height());
r = intersection(repaintRect, boxRect);
if (r.isEmpty())
return;
} else {
r.setX(x);
r.setY(y);
}
o->computeAbsoluteRepaintRect(r, f);
}
}
void RenderBox::repaintDuringLayoutIfMoved(int oldX, int oldY)
{
int newX = m_x;
int newY = m_y;
if (oldX != newX || oldY != newY) {
m_x = oldX; m_y = oldY;
repaint();
repaintOverhangingFloats(true);
m_x = newX; m_y = newY;
repaint();
repaintOverhangingFloats(true);
}
}
int RenderBox::relativePositionOffsetX() const
{
int tx = 0;
if(!style()->left().isAuto())
tx = style()->left().calcValue(containingBlockWidth());
else if(!style()->right().isAuto())
tx = -style()->right().calcValue(containingBlockWidth());
return tx;
}
int RenderBox::relativePositionOffsetY() const
{
int ty = 0;
if(!style()->top().isAuto())
{
if (!style()->top().isPercent()
|| containingBlock()->style()->height().isFixed())
ty = style()->top().calcValue(containingBlockHeight());
}
else if(!style()->bottom().isAuto())
{
if (!style()->bottom().isPercent()
|| containingBlock()->style()->height().isFixed())
ty = -style()->bottom().calcValue(containingBlockHeight());
}
return ty;
}
void RenderBox::calcWidth()
{
if (isPositioned()) {
calcAbsoluteHorizontal();
return;
}
if (node() && view()->frameView() && view()->frameView()->layoutRoot() == node())
return;
if (m_overrideSize != -1 && parent()->style()->boxOrient() == HORIZONTAL
&& parent()->isFlexibleBox() && parent()->isFlexingChildren()) {
m_width = m_overrideSize;
return;
}
bool inVerticalBox = parent()->isFlexibleBox() && (parent()->style()->boxOrient() == VERTICAL);
bool stretching = (parent()->style()->boxAlign() == BSTRETCH);
bool treatAsReplaced = isReplaced() && !isInlineBlockOrInlineTable() && (!inVerticalBox || !stretching);
Length width = (treatAsReplaced) ? Length(calcReplacedWidth(), Fixed) : style()->width();
RenderBlock* cb = containingBlock();
int containerWidth = max(0, containingBlockWidth());
Length marginLeft = style()->marginLeft();
Length marginRight = style()->marginRight();
if (isInline() && !isInlineBlockOrInlineTable()) {
m_marginLeft = marginLeft.calcMinValue(containerWidth);
m_marginRight = marginRight.calcMinValue(containerWidth);
if (treatAsReplaced)
m_width = max(width.value() + borderLeft() + borderRight() + paddingLeft() + paddingRight(), m_minWidth);
return;
}
if (treatAsReplaced)
m_width = width.value() + borderLeft() + borderRight() + paddingLeft() + paddingRight();
else {
m_width = calcWidthUsing(Width, containerWidth);
if (style()->maxWidth().value() != undefinedLength) {
int maxW = calcWidthUsing(MaxWidth, containerWidth);
if (m_width > maxW) {
m_width = maxW;
width = style()->maxWidth();
}
}
int minW = calcWidthUsing(MinWidth, containerWidth);
if (m_width < minW) {
m_width = minW;
width = style()->minWidth();
}
}
if (width.isAuto()) {
m_marginLeft = marginLeft.calcMinValue(containerWidth);
m_marginRight = marginRight.calcMinValue(containerWidth);
} else {
m_marginLeft = 0;
m_marginRight = 0;
calcHorizontalMargins(marginLeft, marginRight, containerWidth);
}
if (containerWidth && containerWidth != (m_width + m_marginLeft + m_marginRight)
&& !isFloating() && !isInline() && !cb->isFlexibleBox()) {
if (cb->style()->direction() == LTR)
m_marginRight = containerWidth - m_width - m_marginLeft;
else
m_marginLeft = containerWidth - m_width - m_marginRight;
}
}
int RenderBox::calcWidthUsing(WidthType widthType, int cw)
{
int width = m_width;
Length w;
if (widthType == Width)
w = style()->width();
else if (widthType == MinWidth)
w = style()->minWidth();
else
w = style()->maxWidth();
if (w.isIntrinsicOrAuto()) {
int marginLeft = style()->marginLeft().calcMinValue(cw);
int marginRight = style()->marginRight().calcMinValue(cw);
if (cw)
width = cw - marginLeft - marginRight;
if (sizesToIntrinsicWidth(widthType)) {
width = max(width, m_minWidth);
width = min(width, m_maxWidth);
}
} else
width = calcBorderBoxWidth(w.calcValue(cw));
return width;
}
bool RenderBox::sizesToIntrinsicWidth(WidthType widthType) const
{
if (isFloating() || (isCompact() && isInline()) ||
(isInlineBlockOrInlineTable() && !isHTMLMarquee()))
return true;
Length width = (widthType == MaxWidth) ? style()->maxWidth() : style()->width();
if (width.type() == Intrinsic)
return true;
if (parent()->style()->overflowX() == OMARQUEE) {
EMarqueeDirection dir = parent()->style()->marqueeDirection();
if (dir == MAUTO || dir == MFORWARD || dir == MBACKWARD || dir == MLEFT || dir == MRIGHT)
return true;
}
if (parent()->isFlexibleBox() &&
(parent()->style()->boxOrient() == HORIZONTAL || parent()->style()->boxAlign() != BSTRETCH))
return true;
return false;
}
void RenderBox::calcHorizontalMargins(const Length& ml, const Length& mr, int cw)
{
if (isFloating() || isInline()) {
m_marginLeft = ml.calcMinValue(cw);
m_marginRight = mr.calcMinValue(cw);
}
else
{
if ( (ml.isAuto() && mr.isAuto() && m_width<cw) ||
(!ml.isAuto() && !mr.isAuto() &&
containingBlock()->style()->textAlign() == WEBKIT_CENTER) )
{
m_marginLeft = (cw - m_width)/2;
if (m_marginLeft<0) m_marginLeft=0;
m_marginRight = cw - m_width - m_marginLeft;
}
else if ( (mr.isAuto() && m_width<cw) ||
(!ml.isAuto() && containingBlock()->style()->direction() == RTL &&
containingBlock()->style()->textAlign() == WEBKIT_LEFT))
{
m_marginLeft = ml.calcValue(cw);
m_marginRight = cw - m_width - m_marginLeft;
}
else if ( (ml.isAuto() && m_width<cw) ||
(!mr.isAuto() && containingBlock()->style()->direction() == LTR &&
containingBlock()->style()->textAlign() == WEBKIT_RIGHT))
{
m_marginRight = mr.calcValue(cw);
m_marginLeft = cw - m_width - m_marginRight;
}
else
{
m_marginLeft = ml.calcMinValue(cw);
m_marginRight = mr.calcMinValue(cw);
}
}
}
void RenderBox::calcHeight()
{
if (isTableCell() || (isInline() && !isReplaced()))
return;
if (isPositioned())
calcAbsoluteVertical();
else
{
calcVerticalMargins();
if (isTable())
return;
Length h;
bool inHorizontalBox = parent()->isFlexibleBox() && parent()->style()->boxOrient() == HORIZONTAL;
bool stretching = parent()->style()->boxAlign() == BSTRETCH;
bool treatAsReplaced = isReplaced() && !isInlineBlockOrInlineTable() && (!inHorizontalBox || !stretching);
bool checkMinMaxHeight = false;
if (m_overrideSize != -1 && parent()->isFlexibleBox() && parent()->style()->boxOrient() == VERTICAL
&& parent()->isFlexingChildren())
h = Length(m_overrideSize - borderTop() - borderBottom() - paddingTop() - paddingBottom(), Fixed);
else if (treatAsReplaced)
h = Length(calcReplacedHeight(), Fixed);
else {
h = style()->height();
checkMinMaxHeight = true;
}
if (h.isAuto() && parent()->isFlexibleBox() && parent()->style()->boxOrient() == HORIZONTAL
&& parent()->isStretchingChildren()) {
h = Length(parent()->contentHeight() - marginTop() - marginBottom() -
borderTop() - paddingTop() - borderBottom() - paddingBottom(), Fixed);
checkMinMaxHeight = false;
}
int height;
if (checkMinMaxHeight) {
height = calcHeightUsing(style()->height());
if (height == -1)
height = m_height;
int minH = calcHeightUsing(style()->minHeight()); int maxH = style()->maxHeight().value() == undefinedLength ? height : calcHeightUsing(style()->maxHeight());
if (maxH == -1)
maxH = height;
height = min(maxH, height);
height = max(minH, height);
}
else
height = h.value() + borderTop() + borderBottom() + paddingTop() + paddingBottom();
m_height = height;
}
if (stretchesToViewHeight()) {
int margins = collapsedMarginTop() + collapsedMarginBottom();
int visHeight = view()->frameView()->visibleHeight();
if (isRoot())
m_height = max(m_height, visHeight - margins);
else
m_height = max(m_height, visHeight -
(margins + parent()->marginTop() + parent()->marginBottom() +
parent()->borderTop() + parent()->borderBottom() +
parent()->paddingTop() + parent()->paddingBottom()));
}
}
int RenderBox::calcHeightUsing(const Length& h)
{
int height = -1;
if (!h.isAuto()) {
if (h.isFixed())
height = h.value();
else if (h.isPercent())
height = calcPercentageHeight(h);
if (height != -1) {
height = calcBorderBoxHeight(height);
return height;
}
}
return height;
}
int RenderBox::calcPercentageHeight(const Length& height)
{
int result = -1;
bool includeBorderPadding = isTable();
RenderBlock* cb = containingBlock();
if (style()->htmlHacks()) {
for ( ; !cb->isRenderView() && !cb->isBody() && !cb->isTableCell() && !cb->isPositioned() &&
cb->style()->height().isAuto(); cb = cb->containingBlock());
}
if (cb->isTableCell()) {
result = cb->overrideSize();
if (result == -1) {
RenderTableCell* cell = static_cast<RenderTableCell*>(cb);
if (scrollsOverflowY() && (!cell->style()->height().isAuto() || !cell->table()->style()->height().isAuto()))
return 0;
return -1;
}
includeBorderPadding = true;
}
else if (cb->style()->height().isFixed())
result = cb->calcContentBoxHeight(cb->style()->height().value());
else if (cb->style()->height().isPercent()) {
result = cb->calcPercentageHeight(cb->style()->height());
if (result != -1)
result = cb->calcContentBoxHeight(result);
} else if (cb->isRenderView() || (cb->isBody() && style()->htmlHacks()) ||
(cb->isPositioned() && !(cb->style()->top().isAuto() || cb->style()->bottom().isAuto()))) {
int oldHeight = cb->height();
cb->calcHeight();
result = cb->contentHeight();
cb->setHeight(oldHeight);
} else if (cb->isRoot() && isPositioned())
result = cb->calcContentBoxHeight(cb->availableHeight());
if (result != -1) {
result = height.calcValue(result);
if (includeBorderPadding) {
result -= (borderTop() + paddingTop() + borderBottom() + paddingBottom());
result = max(0, result);
}
}
return result;
}
int RenderBox::calcReplacedWidth() const
{
int width = calcReplacedWidthUsing(style()->width());
int minW = calcReplacedWidthUsing(style()->minWidth());
int maxW = style()->maxWidth().value() == undefinedLength ? width : calcReplacedWidthUsing(style()->maxWidth());
return max(minW, min(width, maxW));
}
int RenderBox::calcReplacedWidthUsing(Length width) const
{
switch (width.type()) {
case Fixed:
return calcContentBoxWidth(width.value());
case Percent: {
const int cw = containingBlockWidth();
if (cw > 0)
return calcContentBoxWidth(width.calcMinValue(cw));
}
default:
return intrinsicWidth();
}
}
int RenderBox::calcReplacedHeight() const
{
int height = calcReplacedHeightUsing(style()->height());
int minH = calcReplacedHeightUsing(style()->minHeight());
int maxH = style()->maxHeight().value() == undefinedLength ? height : calcReplacedHeightUsing(style()->maxHeight());
return max(minH, min(height, maxH));
}
int RenderBox::calcReplacedHeightUsing(Length height) const
{
switch (height.type()) {
case Fixed:
return calcContentBoxHeight(height.value());
case Percent:
{
RenderBlock* cb = containingBlock();
if (cb->isPositioned() && cb->style()->height().isAuto() && !(cb->style()->top().isAuto() || cb->style()->bottom().isAuto())) {
int oldHeight = cb->height();
cb->calcHeight();
int newHeight = cb->calcContentBoxHeight(cb->contentHeight());
cb->setHeight(oldHeight);
return calcContentBoxHeight(height.calcValue(newHeight));
}
if (cb->isTableCell() && (cb->style()->height().isAuto() || cb->style()->height().isPercent()))
return height.calcValue(cb->availableHeight() - (borderTop() + borderBottom()
+ paddingTop() + paddingBottom()));
return calcContentBoxHeight(height.calcValue(cb->availableHeight()));
}
default:
return intrinsicHeight();
}
}
int RenderBox::availableHeight() const
{
return availableHeightUsing(style()->height());
}
int RenderBox::availableHeightUsing(const Length& h) const
{
if (h.isFixed())
return calcContentBoxHeight(h.value());
if (isRenderView())
return static_cast<const RenderView*>(this)->frameView()->visibleHeight();
if (isTableCell() && (h.isAuto() || h.isPercent()))
return overrideSize() - (borderLeft() + borderRight() + paddingLeft() + paddingRight());
if (h.isPercent())
return calcContentBoxHeight(h.calcValue(containingBlock()->availableHeight()));
return containingBlock()->availableHeight();
}
void RenderBox::calcVerticalMargins()
{
if( isTableCell() ) {
m_marginTop = TABLECELLMARGIN;
m_marginBottom = TABLECELLMARGIN;
return;
}
Length tm = style()->marginTop();
Length bm = style()->marginBottom();
int cw = containingBlock()->contentWidth();
m_marginTop = tm.calcMinValue(cw);
m_marginBottom = bm.calcMinValue(cw);
}
void RenderBox::setStaticX(int staticX)
{
m_staticX = staticX;
}
void RenderBox::setStaticY(int staticY)
{
m_staticY = staticY;
}
int RenderBox::containingBlockWidthForPositioned(const RenderObject* containingBlock) const
{
if (containingBlock->isInline()) {
ASSERT(containingBlock->isRelPositioned());
const RenderFlow* flow = static_cast<const RenderFlow*>(containingBlock);
InlineFlowBox* first = flow->firstLineBox();
InlineFlowBox* last = flow->lastLineBox();
if (!first || !last)
return 0;
int fromLeft;
int fromRight;
if (containingBlock->style()->direction() == LTR) {
fromLeft = first->xPos() + first->borderLeft();
fromRight = last->xPos() + last->width() - last->borderRight();
} else {
fromRight = first->xPos() + first->width() - first->borderRight();
fromLeft = last->xPos() + last->borderLeft();
}
return max(0, (fromRight - fromLeft));
}
return containingBlock->width() - containingBlock->borderLeft() - containingBlock->borderRight();
}
int RenderBox::containingBlockHeightForPositioned(const RenderObject* containingBlock) const
{
if (containingBlock->isRoot())
return containingBlock->availableHeight();
return containingBlock->height() - containingBlock->borderTop() - containingBlock->borderBottom();
}
void RenderBox::calcAbsoluteHorizontal()
{
if (isReplaced()) {
calcAbsoluteHorizontalReplaced();
return;
}
const RenderObject* containerBlock = container();
const int containerWidth = containingBlockWidthForPositioned(containerBlock);
TextDirection containerDirection = (style()->htmlHacks()) ? parent()->style()->direction() : containerBlock->style()->direction();
const int bordersPlusPadding = borderLeft() + borderRight() + paddingLeft() + paddingRight();
const Length marginLeft = style()->marginLeft();
const Length marginRight = style()->marginRight();
Length left = style()->left();
Length right = style()->right();
if (left.isAuto() && right.isAuto()) {
if (containerDirection == LTR) {
int staticPosition = m_staticX - containerBlock->borderLeft();
for (RenderObject* po = parent(); po && po != containerBlock; po = po->parent())
staticPosition += po->xPos();
left.setValue(Fixed, staticPosition);
} else {
RenderObject* po = parent();
int staticPosition = m_staticX + containerWidth + containerBlock->borderRight() - po->width();
for (; po && po != containerBlock; po = po->parent())
staticPosition -= po->xPos();
right.setValue(Fixed, staticPosition);
}
}
calcAbsoluteHorizontalValues(style()->width(), containerBlock, containerDirection,
containerWidth, bordersPlusPadding,
left, right, marginLeft, marginRight,
m_width, m_marginLeft, m_marginRight, m_x);
if (style()->maxWidth().value() != undefinedLength) {
int maxWidth;
int maxMarginLeft;
int maxMarginRight;
int maxXPos;
calcAbsoluteHorizontalValues(style()->maxWidth(), containerBlock, containerDirection,
containerWidth, bordersPlusPadding,
left, right, marginLeft, marginRight,
maxWidth, maxMarginLeft, maxMarginRight, maxXPos);
if (m_width > maxWidth) {
m_width = maxWidth;
m_marginLeft = maxMarginLeft;
m_marginRight = maxMarginRight;
m_x = maxXPos;
}
}
if (style()->minWidth().value()) {
int minWidth;
int minMarginLeft;
int minMarginRight;
int minXPos;
calcAbsoluteHorizontalValues(style()->minWidth(), containerBlock, containerDirection,
containerWidth, bordersPlusPadding,
left, right, marginLeft, marginRight,
minWidth, minMarginLeft, minMarginRight, minXPos);
if (m_width < minWidth) {
m_width = minWidth;
m_marginLeft = minMarginLeft;
m_marginRight = minMarginRight;
m_x = minXPos;
}
}
m_width += bordersPlusPadding;
}
void RenderBox::calcAbsoluteHorizontalValues(Length width, const RenderObject* containerBlock, TextDirection containerDirection,
const int containerWidth, const int bordersPlusPadding,
const Length left, const Length right, const Length marginLeft, const Length marginRight,
int& widthValue, int& marginLeftValue, int& marginRightValue, int& xPos)
{
ASSERT(!(left.isAuto() && right.isAuto()));
int leftValue = 0;
bool widthIsAuto = width.isIntrinsicOrAuto();
bool leftIsAuto = left.isAuto();
bool rightIsAuto = right.isAuto();
if (!leftIsAuto && !widthIsAuto && !rightIsAuto) {
leftValue = left.calcValue(containerWidth);
widthValue = calcContentBoxWidth(width.calcValue(containerWidth));
const int availableSpace = containerWidth - (leftValue + widthValue + right.calcValue(containerWidth) + bordersPlusPadding);
if (marginLeft.isAuto() && marginRight.isAuto()) {
if (availableSpace >= 0) {
marginLeftValue = availableSpace / 2; marginRightValue = availableSpace - marginLeftValue; } else {
if (containerDirection == LTR) {
marginLeftValue = 0;
marginRightValue = availableSpace; } else {
marginLeftValue = availableSpace; marginRightValue = 0;
}
}
} else if (marginLeft.isAuto()) {
marginRightValue = marginRight.calcValue(containerWidth);
marginLeftValue = availableSpace - marginRightValue;
} else if (marginRight.isAuto()) {
marginLeftValue = marginLeft.calcValue(containerWidth);
marginRightValue = availableSpace - marginLeftValue;
} else {
marginLeftValue = marginLeft.calcValue(containerWidth);
marginRightValue = marginRight.calcValue(containerWidth);
if (containerDirection == RTL)
leftValue = (availableSpace + leftValue) - marginLeftValue - marginRightValue;
}
} else {
marginLeftValue = marginLeft.calcMinValue(containerWidth);
marginRightValue = marginRight.calcMinValue(containerWidth);
const int availableSpace = containerWidth - (marginLeftValue + marginRightValue + bordersPlusPadding);
if (leftIsAuto && widthIsAuto && !rightIsAuto) {
int rightValue = right.calcValue(containerWidth);
int preferredWidth = m_maxWidth - bordersPlusPadding;
int preferredMinWidth = m_minWidth - bordersPlusPadding;
int availableWidth = availableSpace - rightValue;
widthValue = min(max(preferredMinWidth, availableWidth), preferredWidth);
leftValue = availableSpace - (widthValue + rightValue);
} else if (!leftIsAuto && widthIsAuto && rightIsAuto) {
leftValue = left.calcValue(containerWidth);
int preferredWidth = m_maxWidth - bordersPlusPadding;
int preferredMinWidth = m_minWidth - bordersPlusPadding;
int availableWidth = availableSpace - leftValue;
widthValue = min(max(preferredMinWidth, availableWidth), preferredWidth);
} else if (leftIsAuto && !width.isAuto() && !rightIsAuto) {
widthValue = calcContentBoxWidth(width.calcValue(containerWidth));
leftValue = availableSpace - (widthValue + right.calcValue(containerWidth));
} else if (!leftIsAuto && widthIsAuto && !rightIsAuto) {
leftValue = left.calcValue(containerWidth);
widthValue = availableSpace - (leftValue + right.calcValue(containerWidth));
} else if (!leftIsAuto&& !widthIsAuto && rightIsAuto) {
leftValue = left.calcValue(containerWidth);
widthValue = calcContentBoxWidth(width.calcValue(containerWidth));
}
}
if (containerBlock->isInline() && containerBlock->style()->direction() == RTL) {
const RenderFlow* flow = static_cast<const RenderFlow*>(containerBlock);
InlineFlowBox* firstLine = flow->firstLineBox();
InlineFlowBox* lastLine = flow->lastLineBox();
if (firstLine && lastLine && firstLine != lastLine) {
xPos = leftValue + marginLeftValue + lastLine->borderLeft() + (lastLine->xPos() - firstLine->xPos());
return;
}
}
xPos = leftValue + marginLeftValue + containerBlock->borderLeft();
}
void RenderBox::calcAbsoluteVertical()
{
if (isReplaced()) {
calcAbsoluteVerticalReplaced();
return;
}
const RenderObject* containerBlock = container();
const int containerHeight = containingBlockHeightForPositioned(containerBlock);
const int bordersPlusPadding = borderTop() + borderBottom() + paddingTop() + paddingBottom();
const Length marginTop = style()->marginTop();
const Length marginBottom = style()->marginBottom();
Length top = style()->top();
Length bottom = style()->bottom();
if (top.isAuto() && bottom.isAuto()) {
int staticTop = m_staticY - containerBlock->borderTop();
for (RenderObject* po = parent(); po && po != containerBlock; po = po->parent()) {
if (!po->isTableRow())
staticTop += po->yPos();
}
top.setValue(Fixed, staticTop);
}
int height;
calcAbsoluteVerticalValues(style()->height(), containerBlock, containerHeight, bordersPlusPadding,
top, bottom, marginTop, marginBottom,
height, m_marginTop, m_marginBottom, m_y);
if (style()->maxHeight().value() != undefinedLength) {
int maxHeight;
int maxMarginTop;
int maxMarginBottom;
int maxYPos;
calcAbsoluteVerticalValues(style()->maxHeight(), containerBlock, containerHeight, bordersPlusPadding,
top, bottom, marginTop, marginBottom,
maxHeight, maxMarginTop, maxMarginBottom, maxYPos);
if (height > maxHeight) {
height = maxHeight;
m_marginTop = maxMarginTop;
m_marginBottom = maxMarginBottom;
m_y = maxYPos;
}
}
if (style()->minHeight().value()) {
int minHeight;
int minMarginTop;
int minMarginBottom;
int minYPos;
calcAbsoluteVerticalValues(style()->minHeight(), containerBlock, containerHeight, bordersPlusPadding,
top, bottom, marginTop, marginBottom,
minHeight, minMarginTop, minMarginBottom, minYPos);
if (height < minHeight) {
height = minHeight;
m_marginTop = minMarginTop;
m_marginBottom = minMarginBottom;
m_y = minYPos;
}
}
height += bordersPlusPadding;
if (m_height > height)
setOverflowHeight(m_height);
m_height = height;
}
void RenderBox::calcAbsoluteVerticalValues(Length height, const RenderObject* containerBlock,
const int containerHeight, const int bordersPlusPadding,
const Length top, const Length bottom, const Length marginTop, const Length marginBottom,
int& heightValue, int& marginTopValue, int& marginBottomValue, int& yPos)
{
ASSERT(!(top.isAuto() && bottom.isAuto()));
int contentHeight = m_height - bordersPlusPadding;
int topValue = 0;
bool heightIsAuto = height.isAuto();
bool topIsAuto = top.isAuto();
bool bottomIsAuto = bottom.isAuto();
if (isTable()) {
height.setValue(Fixed, contentHeight);
heightIsAuto = false;
}
if (!topIsAuto && !heightIsAuto && !bottomIsAuto) {
heightValue = calcContentBoxHeight(height.calcValue(containerHeight));
topValue = top.calcValue(containerHeight);
const int availableSpace = containerHeight - (topValue + heightValue + bottom.calcValue(containerHeight) + bordersPlusPadding);
if (marginTop.isAuto() && marginBottom.isAuto()) {
marginTopValue = availableSpace / 2; marginBottomValue = availableSpace - marginTopValue; } else if (marginTop.isAuto()) {
marginBottomValue = marginBottom.calcValue(containerHeight);
marginTopValue = availableSpace - marginBottomValue;
} else if (marginBottom.isAuto()) {
marginTopValue = marginTop.calcValue(containerHeight);
marginBottomValue = availableSpace - marginTopValue;
} else {
marginTopValue = marginTop.calcValue(containerHeight);
marginBottomValue = marginBottom.calcValue(containerHeight);
}
} else {
marginTopValue = marginTop.calcMinValue(containerHeight);
marginBottomValue = marginBottom.calcMinValue(containerHeight);
const int availableSpace = containerHeight - (marginTopValue + marginBottomValue + bordersPlusPadding);
if (topIsAuto && heightIsAuto && !bottomIsAuto) {
heightValue = contentHeight;
topValue = availableSpace - (heightValue + bottom.calcValue(containerHeight));
} else if (!topIsAuto && heightIsAuto && bottomIsAuto) {
topValue = top.calcValue(containerHeight);
heightValue = contentHeight;
} else if (topIsAuto && !heightIsAuto && !bottomIsAuto) {
heightValue = calcContentBoxHeight(height.calcValue(containerHeight));
topValue = availableSpace - (heightValue + bottom.calcValue(containerHeight));
} else if (!topIsAuto && heightIsAuto && !bottomIsAuto) {
topValue = top.calcValue(containerHeight);
heightValue = max(0, availableSpace - (topValue + bottom.calcValue(containerHeight)));
} else if (!topIsAuto && !heightIsAuto && bottomIsAuto) {
heightValue = calcContentBoxHeight(height.calcValue(containerHeight));
topValue = top.calcValue(containerHeight);
}
}
yPos = topValue + marginTopValue + containerBlock->borderTop();
}
void RenderBox::calcAbsoluteHorizontalReplaced()
{
const RenderObject* containerBlock = container();
const int containerWidth = containingBlockWidthForPositioned(containerBlock);
TextDirection containerDirection = (style()->htmlHacks()) ? parent()->style()->direction() : containerBlock->style()->direction();
Length left = style()->left();
Length right = style()->right();
Length marginLeft = style()->marginLeft();
Length marginRight = style()->marginRight();
m_width = calcReplacedWidth() + borderLeft() + borderRight() + paddingLeft() + paddingRight();
const int availableSpace = containerWidth - m_width;
if (left.isAuto() && right.isAuto()) {
if (containerDirection == LTR) {
int staticPosition = m_staticX - containerBlock->borderLeft();
for (RenderObject* po = parent(); po && po != containerBlock; po = po->parent())
staticPosition += po->xPos();
left.setValue(Fixed, staticPosition);
} else {
RenderObject* po = parent();
int staticPosition = m_staticX + containerWidth + containerBlock->borderRight() - po->width();
for (; po && po != containerBlock; po = po->parent())
staticPosition -= po->xPos();
right.setValue(Fixed, staticPosition);
}
}
if (left.isAuto() || right.isAuto()) {
if (marginLeft.isAuto())
marginLeft.setValue(Fixed, 0);
if (marginRight.isAuto())
marginRight.setValue(Fixed, 0);
}
int leftValue = 0;
int rightValue = 0;
if (marginLeft.isAuto() && marginRight.isAuto()) {
ASSERT(!(left.isAuto() && right.isAuto()));
leftValue = left.calcValue(containerWidth);
rightValue = right.calcValue(containerWidth);
int difference = availableSpace - (leftValue + rightValue);
if (difference > 0) {
m_marginLeft = difference / 2; m_marginRight = difference - m_marginLeft; } else {
if (containerDirection == LTR) {
m_marginLeft = 0;
m_marginRight = difference; } else {
m_marginLeft = difference; m_marginRight = 0;
}
}
} else if (left.isAuto()) {
m_marginLeft = marginLeft.calcValue(containerWidth);
m_marginRight = marginRight.calcValue(containerWidth);
rightValue = right.calcValue(containerWidth);
leftValue = availableSpace - (rightValue + m_marginLeft + m_marginRight);
} else if (right.isAuto()) {
m_marginLeft = marginLeft.calcValue(containerWidth);
m_marginRight = marginRight.calcValue(containerWidth);
leftValue = left.calcValue(containerWidth);
rightValue = availableSpace - (leftValue + m_marginLeft + m_marginRight);
} else if (marginLeft.isAuto()) {
m_marginRight = marginRight.calcValue(containerWidth);
leftValue = left.calcValue(containerWidth);
rightValue = right.calcValue(containerWidth);
m_marginLeft = availableSpace - (leftValue + rightValue + m_marginRight);
} else if (marginRight.isAuto()) {
m_marginLeft = marginLeft.calcValue(containerWidth);
leftValue = left.calcValue(containerWidth);
rightValue = right.calcValue(containerWidth);
m_marginRight = availableSpace - (leftValue + rightValue + m_marginLeft);
} else {
m_marginLeft = marginLeft.calcValue(containerWidth);
m_marginRight = marginRight.calcValue(containerWidth);
rightValue = right.calcValue(containerWidth);
leftValue = left.calcValue(containerWidth);
}
int totalWidth = m_width + leftValue + rightValue + m_marginLeft + m_marginRight;
if (totalWidth > containerWidth && (containerDirection == RTL))
leftValue = containerWidth - (totalWidth - leftValue);
if (containerBlock->isInline() && containerBlock->style()->direction() == RTL) {
const RenderFlow* flow = static_cast<const RenderFlow*>(containerBlock);
InlineFlowBox* firstLine = flow->firstLineBox();
InlineFlowBox* lastLine = flow->lastLineBox();
if (firstLine && lastLine && firstLine != lastLine) {
m_x = leftValue + m_marginLeft + lastLine->borderLeft() + (lastLine->xPos() - firstLine->xPos());
return;
}
}
m_x = leftValue + m_marginLeft + containerBlock->borderLeft();
}
void RenderBox::calcAbsoluteVerticalReplaced()
{
const RenderObject* containerBlock = container();
const int containerHeight = containingBlockHeightForPositioned(containerBlock);
Length top = style()->top();
Length bottom = style()->bottom();
Length marginTop = style()->marginTop();
Length marginBottom = style()->marginBottom();
m_height = calcReplacedHeight() + borderTop() + borderBottom() + paddingTop() + paddingBottom();
const int availableSpace = containerHeight - m_height;
if (top.isAuto() && bottom.isAuto()) {
int staticTop = m_staticY - containerBlock->borderTop();
for (RenderObject* po = parent(); po && po != containerBlock; po = po->parent()) {
if (!po->isTableRow())
staticTop += po->yPos();
}
top.setValue(Fixed, staticTop);
}
if (top.isAuto() || bottom.isAuto()) {
if (marginTop.isAuto())
marginTop.setValue(Fixed, 0);
if (marginBottom.isAuto())
marginBottom.setValue(Fixed, 0);
}
int topValue = 0;
int bottomValue = 0;
if (marginTop.isAuto() && marginBottom.isAuto()) {
ASSERT(!(top.isAuto() || bottom.isAuto()));
topValue = top.calcValue(containerHeight);
bottomValue = bottom.calcValue(containerHeight);
int difference = availableSpace - (topValue + bottomValue);
m_marginTop = difference / 2; m_marginBottom = difference - m_marginTop;
} else if (top.isAuto()) {
m_marginTop = marginTop.calcValue(containerHeight);
m_marginBottom = marginBottom.calcValue(containerHeight);
bottomValue = bottom.calcValue(containerHeight);
topValue = availableSpace - (bottomValue + m_marginTop + m_marginBottom);
} else if (bottom.isAuto()) {
m_marginTop = marginTop.calcValue(containerHeight);
m_marginBottom = marginBottom.calcValue(containerHeight);
topValue = top.calcValue(containerHeight);
} else if (marginTop.isAuto()) {
m_marginBottom = marginBottom.calcValue(containerHeight);
topValue = top.calcValue(containerHeight);
bottomValue = bottom.calcValue(containerHeight);
m_marginTop = availableSpace - (topValue + bottomValue + m_marginBottom);
} else if (marginBottom.isAuto()) {
m_marginTop = marginTop.calcValue(containerHeight);
topValue = top.calcValue(containerHeight);
bottomValue = bottom.calcValue(containerHeight);
m_marginBottom = availableSpace - (topValue + bottomValue + m_marginTop);
} else {
m_marginTop = marginTop.calcValue(containerHeight);
m_marginBottom = marginBottom.calcValue(containerHeight);
topValue = top.calcValue(containerHeight);
}
m_y = topValue + m_marginTop + containerBlock->borderTop();
}
IntRect RenderBox::caretRect(int offset, EAffinity affinity, int* extraWidthToEndOfLine)
{
const int caretWidth = 3;
IntRect rect(xPos(), yPos(), caretWidth, m_height);
if (offset != 0)
rect.move(IntSize(m_width - caretWidth, 0));
if (InlineBox* box = inlineBoxWrapper()) {
RootInlineBox* rootBox = box->root();
int top = rootBox->topOverflow();
rect.setY(top);
rect.setHeight(rootBox->bottomOverflow() - top);
}
int fontHeight = style()->font().height();
if (fontHeight > rect.height() || !isReplaced() && !isTable())
rect.setHeight(fontHeight);
RenderObject* cb = containingBlock();
int cbx, cby;
if (!cb || !cb->absolutePosition(cbx, cby))
return IntRect();
if (extraWidthToEndOfLine)
*extraWidthToEndOfLine = xPos() + m_width - rect.right();
rect.move(cbx, cby);
return rect;
}
int RenderBox::lowestPosition(bool includeOverflowInterior, bool includeSelf) const
{
if (!includeSelf || !m_width)
return 0;
int bottom = m_height;
if (isRelPositioned())
bottom += relativePositionOffsetY();
return bottom;
}
int RenderBox::rightmostPosition(bool includeOverflowInterior, bool includeSelf) const
{
if (!includeSelf || !m_height)
return 0;
int right = m_width;
if (isRelPositioned())
right += relativePositionOffsetX();
return right;
}
int RenderBox::leftmostPosition(bool includeOverflowInterior, bool includeSelf) const
{
if (!includeSelf || !m_height)
return m_width;
int left = 0;
if (isRelPositioned())
left += relativePositionOffsetX();
return left;
}
}