#include "config.h"
#include "RenderBox.h"
#include "CachedImage.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "Document.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "htmlediting.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "ImageBuffer.h"
#include "FloatQuad.h"
#include "Frame.h"
#include "Page.h"
#include "PaintInfo.h"
#include "RenderArena.h"
#include "RenderBoxRegionInfo.h"
#include "RenderFlexibleBox.h"
#include "RenderFlowThread.h"
#include "RenderGeometryMap.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderPart.h"
#include "RenderRegion.h"
#include "RenderTableCell.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "ScrollbarTheme.h"
#include "TransformState.h"
#include <algorithm>
#include <math.h>
#include "Settings.h"
using namespace std;
namespace WebCore {
using namespace HTMLNames;
typedef WTF::HashMap<const RenderBox*, LayoutUnit> OverrideSizeMap;
static OverrideSizeMap* gOverrideHeightMap = 0;
static OverrideSizeMap* gOverrideWidthMap = 0;
bool RenderBox::s_hadOverflowClip = false;
RenderBox::RenderBox(Node* node)
: RenderBoxModelObject(node)
, m_marginLeft(0)
, m_marginRight(0)
, m_marginTop(0)
, m_marginBottom(0)
, m_minPreferredLogicalWidth(-1)
, m_maxPreferredLogicalWidth(-1)
, m_inlineBoxWrapper(0)
{
setIsBox();
}
RenderBox::~RenderBox()
{
}
LayoutUnit RenderBox::marginBefore() const
{
switch (style()->writingMode()) {
case TopToBottomWritingMode:
return m_marginTop;
case BottomToTopWritingMode:
return m_marginBottom;
case LeftToRightWritingMode:
return m_marginLeft;
case RightToLeftWritingMode:
return m_marginRight;
}
ASSERT_NOT_REACHED();
return m_marginTop;
}
LayoutUnit RenderBox::marginAfter() const
{
switch (style()->writingMode()) {
case TopToBottomWritingMode:
return m_marginBottom;
case BottomToTopWritingMode:
return m_marginTop;
case LeftToRightWritingMode:
return m_marginRight;
case RightToLeftWritingMode:
return m_marginLeft;
}
ASSERT_NOT_REACHED();
return m_marginBottom;
}
LayoutUnit RenderBox::marginStart() const
{
if (isHorizontalWritingMode())
return style()->isLeftToRightDirection() ? m_marginLeft : m_marginRight;
return style()->isLeftToRightDirection() ? m_marginTop : m_marginBottom;
}
LayoutUnit RenderBox::marginEnd() const
{
if (isHorizontalWritingMode())
return style()->isLeftToRightDirection() ? m_marginRight : m_marginLeft;
return style()->isLeftToRightDirection() ? m_marginBottom : m_marginTop;
}
void RenderBox::setMarginStart(LayoutUnit margin)
{
if (isHorizontalWritingMode()) {
if (style()->isLeftToRightDirection())
m_marginLeft = margin;
else
m_marginRight = margin;
} else {
if (style()->isLeftToRightDirection())
m_marginTop = margin;
else
m_marginBottom = margin;
}
}
void RenderBox::setMarginEnd(LayoutUnit margin)
{
if (isHorizontalWritingMode()) {
if (style()->isLeftToRightDirection())
m_marginRight = margin;
else
m_marginLeft = margin;
} else {
if (style()->isLeftToRightDirection())
m_marginBottom = margin;
else
m_marginTop = margin;
}
}
void RenderBox::setMarginBefore(LayoutUnit margin)
{
switch (style()->writingMode()) {
case TopToBottomWritingMode:
m_marginTop = margin;
break;
case BottomToTopWritingMode:
m_marginBottom = margin;
break;
case LeftToRightWritingMode:
m_marginLeft = margin;
break;
case RightToLeftWritingMode:
m_marginRight = margin;
break;
}
}
void RenderBox::setMarginAfter(LayoutUnit margin)
{
switch (style()->writingMode()) {
case TopToBottomWritingMode:
m_marginBottom = margin;
break;
case BottomToTopWritingMode:
m_marginTop = margin;
break;
case LeftToRightWritingMode:
m_marginRight = margin;
break;
case RightToLeftWritingMode:
m_marginLeft = margin;
break;
}
}
LayoutRect RenderBox::borderBoxRectInRegion(RenderRegion* region, LayoutUnit offsetFromTopOfFirstPage, RenderBoxRegionInfoFlags cacheFlag) const
{
if (!region)
return borderBoxRect();
RenderBoxRegionInfo* boxInfo = renderBoxRegionInfo(region, offsetFromTopOfFirstPage, cacheFlag);
if (!boxInfo)
return borderBoxRect();
LayoutUnit logicalWidth = boxInfo->logicalWidth();
LayoutUnit logicalLeft = boxInfo->logicalLeft();
const RenderBlock* currentBox = containingBlock();
offsetFromTopOfFirstPage -= logicalTop();
RenderBoxRegionInfo* currentBoxInfo = currentBox->renderBoxRegionInfo(region, offsetFromTopOfFirstPage);
while (currentBoxInfo && currentBoxInfo->isShifted()) {
if (currentBox->style()->direction() == LTR)
logicalLeft += currentBoxInfo->logicalLeft();
else
logicalLeft -= (currentBox->logicalWidth() - currentBoxInfo->logicalWidth()) - currentBoxInfo->logicalLeft();
offsetFromTopOfFirstPage -= logicalTop();
currentBox = currentBox->containingBlock();
region = currentBox->clampToStartAndEndRegions(region);
currentBoxInfo = currentBox->renderBoxRegionInfo(region, offsetFromTopOfFirstPage);
}
if (cacheFlag == DoNotCacheRenderBoxRegionInfo)
delete boxInfo;
if (isHorizontalWritingMode())
return LayoutRect(logicalLeft, 0, logicalWidth, height());
return LayoutRect(0, logicalLeft, width(), logicalWidth);
}
void RenderBox::clearRenderBoxRegionInfo()
{
if (!inRenderFlowThread() || isRenderFlowThread())
return;
RenderFlowThread* flowThread = enclosingRenderFlowThread();
flowThread->removeRenderBoxRegionInfo(this);
}
void RenderBox::willBeDestroyed()
{
clearOverrideSize();
if (style()) {
RenderBlock::removePercentHeightDescendantIfNeeded(this);
}
RenderBoxModelObject::willBeDestroyed();
}
void RenderBox::removeFloatingOrPositionedChildFromBlockLists()
{
ASSERT(isFloatingOrOutOfFlowPositioned());
if (documentBeingDestroyed())
return;
if (isFloating()) {
RenderBlock* parentBlock = 0;
for (RenderObject* curr = parent(); curr && !curr->isRenderView(); curr = curr->parent()) {
if (curr->isRenderBlock()) {
RenderBlock* currBlock = toRenderBlock(curr);
if (!parentBlock || currBlock->containsFloat(this))
parentBlock = currBlock;
}
}
if (parentBlock) {
RenderObject* parent = parentBlock->parent();
if (parent && parent->isFlexibleBoxIncludingDeprecated())
parentBlock = toRenderBlock(parent);
parentBlock->markSiblingsWithFloatsForLayout(this);
parentBlock->markAllDescendantsWithFloatsForLayout(this, false);
}
}
if (isOutOfFlowPositioned())
RenderBlock::removePositionedObject(this);
}
void RenderBox::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
{
s_hadOverflowClip = hasOverflowClip();
RenderStyle* oldStyle = style();
if (oldStyle) {
if (diff >= StyleDifferenceRepaint && node() &&
(node()->hasTagName(htmlTag) || node()->hasTagName(bodyTag)))
view()->repaint();
if (diff == StyleDifferenceLayout && parent() && oldStyle->position() != newStyle->position()) {
markContainingBlocksForLayout();
if (oldStyle->position() == StaticPosition)
repaint();
else if (newStyle->hasOutOfFlowPosition())
parent()->setChildNeedsLayout(true);
if (isFloating() && !isOutOfFlowPositioned() && newStyle->hasOutOfFlowPosition())
removeFloatingOrPositionedChildFromBlockLists();
}
} else if (newStyle && isBody())
view()->repaint();
RenderBoxModelObject::styleWillChange(diff, newStyle);
}
void RenderBox::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
bool oldHorizontalWritingMode = isHorizontalWritingMode();
RenderBoxModelObject::styleDidChange(diff, oldStyle);
RenderStyle* newStyle = style();
if (needsLayout() && oldStyle) {
RenderBlock::removePercentHeightDescendantIfNeeded(this);
if (isOutOfFlowPositioned() && newStyle->hasStaticBlockPosition(isHorizontalWritingMode()) && oldStyle->marginBefore() != newStyle->marginBefore()
&& parent() && !parent()->normalChildNeedsLayout())
parent()->setChildNeedsLayout(true);
}
if (RenderBlock::hasPercentHeightContainerMap() && firstChild()
&& oldHorizontalWritingMode != isHorizontalWritingMode())
RenderBlock::clearPercentHeightDescendantsFrom(this);
if (hasOverflowClip() && oldStyle && newStyle && oldStyle->effectiveZoom() != newStyle->effectiveZoom()) {
if (int left = layer()->scrollXOffset()) {
left = (left / oldStyle->effectiveZoom()) * newStyle->effectiveZoom();
layer()->scrollToXOffset(left);
}
if (int top = layer()->scrollYOffset()) {
top = (top / oldStyle->effectiveZoom()) * newStyle->effectiveZoom();
layer()->scrollToYOffset(top);
}
}
bool isBodyRenderer = isBody();
bool isRootRenderer = isRoot();
if (isBodyRenderer)
document()->setTextColor(newStyle->visitedDependentColor(CSSPropertyColor));
if (isRootRenderer || isBodyRenderer) {
RenderView* viewRenderer = view();
RenderStyle* viewStyle = viewRenderer->style();
if (viewStyle->direction() != newStyle->direction() && (isRootRenderer || !document()->directionSetOnDocumentElement())) {
viewStyle->setDirection(newStyle->direction());
if (isBodyRenderer)
document()->documentElement()->renderer()->style()->setDirection(newStyle->direction());
setNeedsLayoutAndPrefWidthsRecalc();
}
if (viewStyle->writingMode() != newStyle->writingMode() && (isRootRenderer || !document()->writingModeSetOnDocumentElement())) {
viewStyle->setWritingMode(newStyle->writingMode());
viewRenderer->setHorizontalWritingMode(newStyle->isHorizontalWritingMode());
if (isBodyRenderer) {
document()->documentElement()->renderer()->style()->setWritingMode(newStyle->writingMode());
document()->documentElement()->renderer()->setHorizontalWritingMode(newStyle->isHorizontalWritingMode());
}
setNeedsLayoutAndPrefWidthsRecalc();
}
frame()->view()->recalculateScrollbarOverlayStyle();
}
}
void RenderBox::updateBoxModelInfoFromStyle()
{
RenderBoxModelObject::updateBoxModelInfoFromStyle();
RenderStyle* styleToUse = style();
bool isRootObject = isRoot();
bool isViewObject = isRenderView();
if (isRootObject || isViewObject)
setHasBoxDecorations(true);
setPositioned(styleToUse->hasOutOfFlowPosition());
setFloating(!isOutOfFlowPositioned() && styleToUse->isFloating());
if (styleToUse->overflowX() != OVISIBLE && !isRootObject && (isRenderBlock() || isTableRow() || isTableSection())) {
bool boxHasOverflowClip = true;
if (isBody()) {
if (document()->documentElement()->hasTagName(htmlTag) &&
document()->body() == node() &&
document()->documentElement()->renderer()->style()->overflowX() == OVISIBLE)
boxHasOverflowClip = false;
}
if (boxHasOverflowClip) {
if (!s_hadOverflowClip)
repaint();
setHasOverflowClip();
}
}
setHasTransform(styleToUse->hasTransformRelatedProperty());
setHasReflection(styleToUse->boxReflect());
}
void RenderBox::layout()
{
ASSERT(needsLayout());
RenderObject* child = firstChild();
if (!child) {
setNeedsLayout(false);
return;
}
LayoutStateMaintainer statePusher(view(), this, locationOffset(), style()->isFlippedBlocksWritingMode());
while (child) {
child->layoutIfNeeded();
ASSERT(!child->needsLayout());
child = child->nextSibling();
}
statePusher.pop();
setNeedsLayout(false);
}
LayoutUnit RenderBox::clientWidth() const
{
return width() - borderLeft() - borderRight() - verticalScrollbarWidth();
}
LayoutUnit RenderBox::clientHeight() const
{
return height() - borderTop() - borderBottom() - horizontalScrollbarHeight();
}
int RenderBox::pixelSnappedClientWidth() const
{
return snapSizeToPixel(clientWidth(), x() + clientLeft());
}
int RenderBox::pixelSnappedClientHeight() const
{
return snapSizeToPixel(clientHeight(), y() + clientTop());
}
int RenderBox::scrollWidth() const
{
if (hasOverflowClip())
return layer()->scrollWidth();
if (style()->isLeftToRightDirection())
return snapSizeToPixel(max(clientWidth(), maxXLayoutOverflow() - borderLeft()), clientLeft());
return clientWidth() - min(ZERO_LAYOUT_UNIT, minXLayoutOverflow() - borderLeft());
}
int RenderBox::scrollHeight() const
{
if (hasOverflowClip())
return layer()->scrollHeight();
return snapSizeToPixel(max(clientHeight(), maxYLayoutOverflow() - borderTop()), clientTop());
}
int RenderBox::scrollLeft() const
{
return hasOverflowClip() ? layer()->scrollXOffset() : 0;
}
int RenderBox::scrollTop() const
{
return hasOverflowClip() ? layer()->scrollYOffset() : 0;
}
void RenderBox::setScrollLeft(int newLeft)
{
if (hasOverflowClip())
layer()->scrollToXOffset(newLeft, RenderLayer::ScrollOffsetClamped);
}
void RenderBox::setScrollTop(int newTop)
{
if (hasOverflowClip())
layer()->scrollToYOffset(newTop, RenderLayer::ScrollOffsetClamped);
}
void RenderBox::absoluteRects(Vector<IntRect>& rects, const LayoutPoint& accumulatedOffset) const
{
rects.append(pixelSnappedIntRect(accumulatedOffset, size()));
}
void RenderBox::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
{
quads.append(localToAbsoluteQuad(FloatRect(0, 0, width(), height()), false, wasFixed));
}
void RenderBox::updateLayerTransform()
{
if (hasLayer())
layer()->updateTransform();
}
IntRect RenderBox::absoluteContentBox() const
{
IntRect rect = pixelSnappedIntRect(contentBoxRect());
FloatPoint absPos = localToAbsolute(FloatPoint());
rect.move(absPos.x(), absPos.y());
return rect;
}
FloatQuad RenderBox::absoluteContentQuad() const
{
LayoutRect rect = contentBoxRect();
return localToAbsoluteQuad(FloatRect(rect));
}
LayoutRect RenderBox::outlineBoundsForRepaint(RenderBoxModelObject* repaintContainer, LayoutPoint* cachedOffsetToRepaintContainer) const
{
LayoutRect box = borderBoundingBox();
adjustRectForOutlineAndShadow(box);
FloatQuad containerRelativeQuad = FloatRect(box);
if (cachedOffsetToRepaintContainer)
containerRelativeQuad.move(cachedOffsetToRepaintContainer->x(), cachedOffsetToRepaintContainer->y());
else
containerRelativeQuad = localToContainerQuad(containerRelativeQuad, repaintContainer);
box = containerRelativeQuad.enclosingBoundingBox();
box.move(view()->layoutDelta());
return box;
}
void RenderBox::addFocusRingRects(Vector<IntRect>& rects, const LayoutPoint& additionalOffset)
{
if (!size().isEmpty())
rects.append(pixelSnappedIntRect(additionalOffset, size()));
}
LayoutRect RenderBox::reflectionBox() const
{
LayoutRect result;
if (!style()->boxReflect())
return result;
LayoutRect box = borderBoxRect();
result = box;
switch (style()->boxReflect()->direction()) {
case ReflectionBelow:
result.move(0, box.height() + reflectionOffset());
break;
case ReflectionAbove:
result.move(0, -box.height() - reflectionOffset());
break;
case ReflectionLeft:
result.move(-box.width() - reflectionOffset(), 0);
break;
case ReflectionRight:
result.move(box.width() + reflectionOffset(), 0);
break;
}
return result;
}
int RenderBox::reflectionOffset() const
{
if (!style()->boxReflect())
return 0;
RenderView* renderView = view();
if (style()->boxReflect()->direction() == ReflectionLeft || style()->boxReflect()->direction() == ReflectionRight)
return valueForLength(style()->boxReflect()->offset(), borderBoxRect().width(), renderView);
return valueForLength(style()->boxReflect()->offset(), borderBoxRect().height(), renderView);
}
LayoutRect RenderBox::reflectedRect(const LayoutRect& r) const
{
if (!style()->boxReflect())
return LayoutRect();
LayoutRect box = borderBoxRect();
LayoutRect result = r;
switch (style()->boxReflect()->direction()) {
case ReflectionBelow:
result.setY(box.maxY() + reflectionOffset() + (box.maxY() - r.maxY()));
break;
case ReflectionAbove:
result.setY(box.y() - reflectionOffset() - box.height() + (box.maxY() - r.maxY()));
break;
case ReflectionLeft:
result.setX(box.x() - reflectionOffset() - box.width() + (box.maxX() - r.maxX()));
break;
case ReflectionRight:
result.setX(box.maxX() + reflectionOffset() + (box.maxX() - r.maxX()));
break;
}
return result;
}
bool RenderBox::fixedElementLaysOutRelativeToFrame(Frame* frame, FrameView* frameView) const
{
return style() && style()->position() == FixedPosition && container()->isRenderView() && frame && frameView && frameView->fixedElementsLayoutRelativeToFrame();
}
bool RenderBox::includeVerticalScrollbarSize() const
{
return hasOverflowClip() && !layer()->hasOverlayScrollbars()
&& (style()->overflowY() == OSCROLL || style()->overflowY() == OAUTO);
}
bool RenderBox::includeHorizontalScrollbarSize() const
{
return hasOverflowClip() && !layer()->hasOverlayScrollbars()
&& (style()->overflowX() == OSCROLL || style()->overflowX() == OAUTO);
}
int RenderBox::verticalScrollbarWidth() const
{
return includeVerticalScrollbarSize() ? layer()->verticalScrollbarWidth() : 0;
}
int RenderBox::horizontalScrollbarHeight() const
{
return includeHorizontalScrollbarSize() ? layer()->horizontalScrollbarHeight() : 0;
}
bool RenderBox::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
{
RenderLayer* l = layer();
if (l && l->scroll(direction, granularity, multiplier)) {
if (stopNode)
*stopNode = node();
return true;
}
if (stopNode && *stopNode && *stopNode == node())
return true;
RenderBlock* b = containingBlock();
if (b && !b->isRenderView())
return b->scroll(direction, granularity, multiplier, stopNode);
return false;
}
bool RenderBox::logicalScroll(ScrollLogicalDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
{
bool scrolled = false;
RenderLayer* l = layer();
if (l) {
#if PLATFORM(MAC)
if (granularity == ScrollByDocument)
scrolled = l->scroll(logicalToPhysical(ScrollInlineDirectionBackward, isHorizontalWritingMode(), style()->isFlippedBlocksWritingMode()), ScrollByDocument, multiplier);
#endif
if (l->scroll(logicalToPhysical(direction, isHorizontalWritingMode(), style()->isFlippedBlocksWritingMode()), granularity, multiplier))
scrolled = true;
if (scrolled) {
if (stopNode)
*stopNode = node();
return true;
}
}
if (stopNode && *stopNode && *stopNode == node())
return true;
RenderBlock* b = containingBlock();
if (b && !b->isRenderView())
return b->logicalScroll(direction, granularity, multiplier, stopNode);
return false;
}
bool RenderBox::canBeScrolledAndHasScrollableArea() const
{
return canBeProgramaticallyScrolled() && (scrollHeight() != clientHeight() || scrollWidth() != clientWidth());
}
bool RenderBox::canBeProgramaticallyScrolled() const
{
return (hasOverflowClip() && (scrollsOverflow() || (node() && node()->rendererIsEditable()))) || (node() && node()->isDocumentNode());
}
void RenderBox::autoscroll()
{
if (layer())
layer()->autoscroll();
}
void RenderBox::panScroll(const IntPoint& source)
{
if (layer())
layer()->panScrollFromPoint(source);
}
bool RenderBox::needsPreferredWidthsRecalculation() const
{
return style()->paddingStart().isPercent() || style()->paddingEnd().isPercent();
}
IntSize RenderBox::scrolledContentOffset() const
{
ASSERT(hasOverflowClip());
ASSERT(hasLayer());
return layer()->scrolledContentOffset();
}
LayoutSize RenderBox::cachedSizeForOverflowClip() const
{
ASSERT(hasOverflowClip());
ASSERT(hasLayer());
return layer()->size();
}
LayoutUnit RenderBox::minPreferredLogicalWidth() const
{
if (preferredLogicalWidthsDirty())
const_cast<RenderBox*>(this)->computePreferredLogicalWidths();
return m_minPreferredLogicalWidth;
}
LayoutUnit RenderBox::maxPreferredLogicalWidth() const
{
if (preferredLogicalWidthsDirty())
const_cast<RenderBox*>(this)->computePreferredLogicalWidths();
return m_maxPreferredLogicalWidth;
}
bool RenderBox::hasOverrideHeight() const
{
return gOverrideHeightMap && gOverrideHeightMap->contains(this);
}
bool RenderBox::hasOverrideWidth() const
{
return gOverrideWidthMap && gOverrideWidthMap->contains(this);
}
void RenderBox::setOverrideHeight(LayoutUnit height)
{
if (!gOverrideHeightMap)
gOverrideHeightMap = new OverrideSizeMap();
gOverrideHeightMap->set(this, height);
}
void RenderBox::setOverrideWidth(LayoutUnit width)
{
if (!gOverrideWidthMap)
gOverrideWidthMap = new OverrideSizeMap();
gOverrideWidthMap->set(this, width);
}
void RenderBox::clearOverrideSize()
{
if (gOverrideHeightMap)
gOverrideHeightMap->remove(this);
if (gOverrideWidthMap)
gOverrideWidthMap->remove(this);
}
LayoutUnit RenderBox::overrideWidth() const
{
return hasOverrideWidth() ? gOverrideWidthMap->get(this) : width();
}
LayoutUnit RenderBox::overrideHeight() const
{
return hasOverrideHeight() ? gOverrideHeightMap->get(this) : height();
}
LayoutUnit RenderBox::computeBorderBoxLogicalWidth(LayoutUnit width) const
{
LayoutUnit bordersPlusPadding = borderAndPaddingLogicalWidth();
if (style()->boxSizing() == CONTENT_BOX)
return width + bordersPlusPadding;
return max(width, bordersPlusPadding);
}
LayoutUnit RenderBox::computeBorderBoxLogicalHeight(LayoutUnit height) const
{
LayoutUnit bordersPlusPadding = borderAndPaddingLogicalHeight();
if (style()->boxSizing() == CONTENT_BOX)
return height + bordersPlusPadding;
return max(height, bordersPlusPadding);
}
LayoutUnit RenderBox::computeContentBoxLogicalWidth(LayoutUnit width) const
{
if (style()->boxSizing() == BORDER_BOX)
width -= borderAndPaddingLogicalWidth();
return max<LayoutUnit>(0, width);
}
LayoutUnit RenderBox::computeContentBoxLogicalHeight(LayoutUnit height) const
{
if (style()->boxSizing() == BORDER_BOX)
height -= borderAndPaddingLogicalHeight();
return max<LayoutUnit>(0, height);
}
bool RenderBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
{
LayoutPoint adjustedLocation = accumulatedOffset + location();
for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
if (!child->hasLayer() && child->nodeAtPoint(request, result, pointInContainer, adjustedLocation, action)) {
updateHitTestResult(result, pointInContainer - toLayoutSize(adjustedLocation));
return true;
}
}
LayoutRect boundsRect = borderBoxRectInRegion(result.region());
boundsRect.moveBy(adjustedLocation);
if (visibleToHitTesting() && action == HitTestForeground && boundsRect.intersects(result.rectForPoint(pointInContainer))) {
updateHitTestResult(result, pointInContainer - toLayoutSize(adjustedLocation));
if (!result.addNodeToRectBasedTestResult(node(), pointInContainer, boundsRect))
return true;
}
return false;
}
void RenderBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
LayoutPoint adjustedPaintOffset = paintOffset + location();
PaintInfo childInfo(paintInfo);
childInfo.updatePaintingRootForChildren(this);
for (RenderObject* child = firstChild(); child; child = child->nextSibling())
child->paint(childInfo, adjustedPaintOffset);
}
void RenderBox::paintRootBoxFillLayers(const PaintInfo& paintInfo)
{
RenderObject* rootBackgroundRenderer = rendererForRootBackground();
const FillLayer* bgLayer = rootBackgroundRenderer->style()->backgroundLayers();
Color bgColor = rootBackgroundRenderer->style()->visitedDependentColor(CSSPropertyBackgroundColor);
paintFillLayers(paintInfo, bgColor, bgLayer, view()->backgroundRect(this), BackgroundBleedNone, CompositeSourceOver, rootBackgroundRenderer);
}
BackgroundBleedAvoidance RenderBox::determineBackgroundBleedAvoidance(GraphicsContext* context) const
{
if (context->paintingDisabled())
return BackgroundBleedNone;
const RenderStyle* style = this->style();
if (!style->hasBackground() || !style->hasBorder() || !style->hasBorderRadius() || borderImageIsLoadedAndCanBeRendered())
return BackgroundBleedNone;
AffineTransform ctm = context->getCTM();
FloatSize contextScaling(static_cast<float>(ctm.xScale()), static_cast<float>(ctm.yScale()));
if (borderObscuresBackgroundEdge(contextScaling))
return BackgroundBleedShrinkBackground;
return BackgroundBleedUseTransparencyLayer;
}
void RenderBox::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (!paintInfo.shouldPaintWithinRoot(this))
return;
LayoutRect paintRect = borderBoxRectInRegion(paintInfo.renderRegion);
paintRect.moveBy(paintOffset);
borderFitAdjust(paintRect);
if (style()->appearance() == CheckboxPart || style()->appearance() == RadioPart) {
int width = min(paintRect.width(), paintRect.height());
int height = width;
int ty = paintRect.y() + (this->height() - height) / 2;
paintRect = IntRect(paintRect.x(), ty, width, height);
}
BackgroundBleedAvoidance bleedAvoidance = determineBackgroundBleedAvoidance(paintInfo.context);
if (!boxShadowShouldBeAppliedToBackground(bleedAvoidance))
paintBoxShadow(paintInfo, paintRect, style(), Normal);
GraphicsContextStateSaver stateSaver(*paintInfo.context, false);
if (bleedAvoidance == BackgroundBleedUseTransparencyLayer) {
RoundedRect border = style()->getRoundedBorderFor(paintRect, view());
stateSaver.save();
paintInfo.context->addRoundedRectClip(border);
paintInfo.context->beginTransparencyLayer(1);
}
IntRect snappedPaintRect(pixelSnappedIntRect(paintRect));
bool themePainted = style()->hasAppearance() && !theme()->paint(this, paintInfo, snappedPaintRect);
if (!themePainted) {
paintBackground(paintInfo, paintRect, bleedAvoidance);
if (style()->hasAppearance())
theme()->paintDecorations(this, paintInfo, snappedPaintRect);
}
paintBoxShadow(paintInfo, paintRect, style(), Inset);
if ((!style()->hasAppearance() || (!themePainted && theme()->paintBorderOnly(this, paintInfo, snappedPaintRect))) && style()->hasBorder())
paintBorder(paintInfo, paintRect, style(), bleedAvoidance);
if (bleedAvoidance == BackgroundBleedUseTransparencyLayer)
paintInfo.context->endTransparencyLayer();
}
void RenderBox::paintBackground(const PaintInfo& paintInfo, const LayoutRect& paintRect, BackgroundBleedAvoidance bleedAvoidance)
{
if (isRoot())
paintRootBoxFillLayers(paintInfo);
else if (!isBody()
|| (document()->documentElement()->renderer() && document()->documentElement()->renderer()->hasBackground())
|| (document()->documentElement()->renderer() != parent())) {
if (!backgroundIsObscured())
paintFillLayers(paintInfo, style()->visitedDependentColor(CSSPropertyBackgroundColor), style()->backgroundLayers(), paintRect, bleedAvoidance);
}
}
void RenderBox::paintMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (!paintInfo.shouldPaintWithinRoot(this) || style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask || paintInfo.context->paintingDisabled())
return;
LayoutRect paintRect = LayoutRect(paintOffset, size());
borderFitAdjust(paintRect);
paintMaskImages(paintInfo, paintRect);
}
void RenderBox::paintMaskImages(const PaintInfo& paintInfo, const LayoutRect& paintRect)
{
bool pushTransparencyLayer = false;
bool compositedMask = hasLayer() && layer()->hasCompositedMask();
bool flattenCompositingLayers = view()->frameView() && view()->frameView()->paintBehavior() & PaintBehaviorFlattenCompositingLayers;
CompositeOperator compositeOp = CompositeSourceOver;
bool allMaskImagesLoaded = true;
if (!compositedMask || flattenCompositingLayers) {
pushTransparencyLayer = true;
StyleImage* maskBoxImage = style()->maskBoxImage().image();
const FillLayer* maskLayers = style()->maskLayers();
if (maskBoxImage)
allMaskImagesLoaded &= maskBoxImage->isLoaded();
if (maskLayers)
allMaskImagesLoaded &= maskLayers->imagesAreLoaded();
paintInfo.context->setCompositeOperation(CompositeDestinationIn);
paintInfo.context->beginTransparencyLayer(1);
compositeOp = CompositeSourceOver;
}
if (allMaskImagesLoaded) {
paintFillLayers(paintInfo, Color(), style()->maskLayers(), paintRect, BackgroundBleedNone, compositeOp);
paintNinePieceImage(paintInfo.context, paintRect, style(), style()->maskBoxImage(), compositeOp);
}
if (pushTransparencyLayer)
paintInfo.context->endTransparencyLayer();
}
LayoutRect RenderBox::maskClipRect()
{
const NinePieceImage& maskBoxImage = style()->maskBoxImage();
if (maskBoxImage.image()) {
LayoutRect borderImageRect = borderBoxRect();
LayoutUnit topOutset;
LayoutUnit rightOutset;
LayoutUnit bottomOutset;
LayoutUnit leftOutset;
style()->getMaskBoxImageOutsets(topOutset, rightOutset, bottomOutset, leftOutset);
borderImageRect.setX(borderImageRect.x() - leftOutset);
borderImageRect.setY(borderImageRect.y() - topOutset);
borderImageRect.setWidth(borderImageRect.width() + leftOutset + rightOutset);
borderImageRect.setHeight(borderImageRect.height() + topOutset + bottomOutset);
return borderImageRect;
}
LayoutRect result;
LayoutRect borderBox = borderBoxRect();
for (const FillLayer* maskLayer = style()->maskLayers(); maskLayer; maskLayer = maskLayer->next()) {
if (maskLayer->image()) {
BackgroundImageGeometry geometry;
calculateBackgroundImageGeometry(maskLayer, borderBox, geometry);
result.unite(geometry.destRect());
}
}
return result;
}
void RenderBox::paintFillLayers(const PaintInfo& paintInfo, const Color& c, const FillLayer* fillLayer, const LayoutRect& rect,
BackgroundBleedAvoidance bleedAvoidance, CompositeOperator op, RenderObject* backgroundObject)
{
if (!fillLayer)
return;
paintFillLayers(paintInfo, c, fillLayer->next(), rect, bleedAvoidance, op, backgroundObject);
paintFillLayer(paintInfo, c, fillLayer, rect, bleedAvoidance, op, backgroundObject);
}
void RenderBox::paintFillLayer(const PaintInfo& paintInfo, const Color& c, const FillLayer* fillLayer, const LayoutRect& rect,
BackgroundBleedAvoidance bleedAvoidance, CompositeOperator op, RenderObject* backgroundObject)
{
paintFillLayerExtended(paintInfo, c, fillLayer, rect, bleedAvoidance, 0, LayoutSize(), op, backgroundObject);
}
#if USE(ACCELERATED_COMPOSITING)
static bool layersUseImage(WrappedImagePtr image, const FillLayer* layers)
{
for (const FillLayer* curLayer = layers; curLayer; curLayer = curLayer->next()) {
if (curLayer->image() && image == curLayer->image()->data())
return true;
}
return false;
}
#endif
void RenderBox::imageChanged(WrappedImagePtr image, const IntRect*)
{
if (!parent())
return;
if ((style()->borderImage().image() && style()->borderImage().image()->data() == image) ||
(style()->maskBoxImage().image() && style()->maskBoxImage().image()->data() == image)) {
repaint();
return;
}
bool didFullRepaint = repaintLayerRectsForImage(image, style()->backgroundLayers(), true);
if (!didFullRepaint)
repaintLayerRectsForImage(image, style()->maskLayers(), false);
#if USE(ACCELERATED_COMPOSITING)
if (hasLayer() && layer()->hasCompositedMask() && layersUseImage(image, style()->maskLayers()))
layer()->contentChanged(MaskImageChanged);
#endif
}
bool RenderBox::repaintLayerRectsForImage(WrappedImagePtr image, const FillLayer* layers, bool drawingBackground)
{
LayoutRect rendererRect;
RenderBox* layerRenderer = 0;
for (const FillLayer* curLayer = layers; curLayer; curLayer = curLayer->next()) {
if (curLayer->image() && image == curLayer->image()->data() && curLayer->image()->canRender(this, style()->effectiveZoom())) {
if (!layerRenderer) {
bool drawingRootBackground = drawingBackground && (isRoot() || (isBody() && !document()->documentElement()->renderer()->hasBackground()));
if (drawingRootBackground) {
layerRenderer = view();
LayoutUnit rw;
LayoutUnit rh;
if (FrameView* frameView = toRenderView(layerRenderer)->frameView()) {
rw = frameView->contentsWidth();
rh = frameView->contentsHeight();
} else {
rw = layerRenderer->width();
rh = layerRenderer->height();
}
rendererRect = LayoutRect(-layerRenderer->marginLeft(),
-layerRenderer->marginTop(),
max(layerRenderer->width() + layerRenderer->marginWidth() + layerRenderer->borderLeft() + layerRenderer->borderRight(), rw),
max(layerRenderer->height() + layerRenderer->marginHeight() + layerRenderer->borderTop() + layerRenderer->borderBottom(), rh));
} else {
layerRenderer = this;
rendererRect = borderBoxRect();
}
}
BackgroundImageGeometry geometry;
layerRenderer->calculateBackgroundImageGeometry(curLayer, rendererRect, geometry);
layerRenderer->repaintRectangle(geometry.destRect());
if (geometry.destRect() == rendererRect)
return true;
}
}
return false;
}
#if PLATFORM(MAC)
void RenderBox::paintCustomHighlight(const LayoutPoint& paintOffset, const AtomicString& type, bool behindText)
{
Frame* frame = this->frame();
if (!frame)
return;
Page* page = frame->page();
if (!page)
return;
InlineBox* boxWrap = inlineBoxWrapper();
RootInlineBox* r = boxWrap ? boxWrap->root() : 0;
if (r) {
FloatRect rootRect(paintOffset.x() + r->x(), paintOffset.y() + r->selectionTop(), r->logicalWidth(), r->selectionHeight());
FloatRect imageRect(paintOffset.x() + x(), rootRect.y(), width(), rootRect.height());
page->chrome()->client()->paintCustomHighlight(node(), type, imageRect, rootRect, behindText, false);
} else {
FloatRect imageRect(paintOffset.x() + x(), paintOffset.y() + y(), width(), height());
page->chrome()->client()->paintCustomHighlight(node(), type, imageRect, imageRect, behindText, false);
}
}
#endif
bool RenderBox::pushContentsClip(PaintInfo& paintInfo, const LayoutPoint& accumulatedOffset)
{
if (paintInfo.phase == PaintPhaseBlockBackground || paintInfo.phase == PaintPhaseSelfOutline || paintInfo.phase == PaintPhaseMask)
return false;
bool isControlClip = hasControlClip();
bool isOverflowClip = hasOverflowClip() && !layer()->isSelfPaintingLayer();
if (!isControlClip && !isOverflowClip)
return false;
if (paintInfo.phase == PaintPhaseOutline)
paintInfo.phase = PaintPhaseChildOutlines;
else if (paintInfo.phase == PaintPhaseChildBlockBackground) {
paintInfo.phase = PaintPhaseBlockBackground;
paintObject(paintInfo, accumulatedOffset);
paintInfo.phase = PaintPhaseChildBlockBackgrounds;
}
IntRect clipRect = pixelSnappedIntRect(isControlClip ? controlClipRect(accumulatedOffset) : overflowClipRect(accumulatedOffset, paintInfo.renderRegion));
paintInfo.context->save();
if (style()->hasBorderRadius())
paintInfo.context->addRoundedRectClip(style()->getRoundedInnerBorderFor(LayoutRect(accumulatedOffset, size())));
paintInfo.context->clip(clipRect);
return true;
}
void RenderBox::popContentsClip(PaintInfo& paintInfo, PaintPhase originalPhase, const LayoutPoint& accumulatedOffset)
{
ASSERT(hasControlClip() || (hasOverflowClip() && !layer()->isSelfPaintingLayer()));
paintInfo.context->restore();
if (originalPhase == PaintPhaseOutline) {
paintInfo.phase = PaintPhaseSelfOutline;
paintObject(paintInfo, accumulatedOffset);
paintInfo.phase = originalPhase;
} else if (originalPhase == PaintPhaseChildBlockBackground)
paintInfo.phase = originalPhase;
}
LayoutRect RenderBox::overflowClipRect(const LayoutPoint& location, RenderRegion* region, OverlayScrollbarSizeRelevancy relevancy)
{
LayoutRect clipRect = borderBoxRectInRegion(region);
clipRect.setLocation(location + clipRect.location() + LayoutSize(borderLeft(), borderTop()));
clipRect.setSize(clipRect.size() - LayoutSize(borderLeft() + borderRight(), borderTop() + borderBottom()));
if (layer()) {
if (style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
clipRect.move(layer()->verticalScrollbarWidth(relevancy), 0);
clipRect.contract(layer()->verticalScrollbarWidth(relevancy), layer()->horizontalScrollbarHeight(relevancy));
}
return clipRect;
}
LayoutRect RenderBox::clipRect(const LayoutPoint& location, RenderRegion* region)
{
LayoutRect borderBoxRect = borderBoxRectInRegion(region);
LayoutRect clipRect = LayoutRect(borderBoxRect.location() + location, borderBoxRect.size());
RenderView* renderView = view();
if (!style()->clipLeft().isAuto()) {
LayoutUnit c = valueForLength(style()->clipLeft(), borderBoxRect.width(), renderView);
clipRect.move(c, 0);
clipRect.contract(c, 0);
}
if (!style()->clipRight().isAuto())
clipRect.contract(width() - valueForLength(style()->clipRight(), width(), renderView), 0);
if (!style()->clipTop().isAuto()) {
LayoutUnit c = valueForLength(style()->clipTop(), borderBoxRect.height(), renderView);
clipRect.move(0, c);
clipRect.contract(0, c);
}
if (!style()->clipBottom().isAuto())
clipRect.contract(0, height() - valueForLength(style()->clipBottom(), height(), renderView));
return clipRect;
}
LayoutUnit RenderBox::shrinkLogicalWidthToAvoidFloats(LayoutUnit childMarginStart, LayoutUnit childMarginEnd, const RenderBlock* cb, RenderRegion* region, LayoutUnit offsetFromLogicalTopOfFirstPage)
{
RenderRegion* containingBlockRegion = 0;
LayoutUnit logicalTopPosition = logicalTop();
LayoutUnit adjustedPageOffsetForContainingBlock = offsetFromLogicalTopOfFirstPage - logicalTop();
if (region) {
LayoutUnit offsetFromLogicalTopOfRegion = region ? region->offsetFromLogicalTopOfFirstPage() - offsetFromLogicalTopOfFirstPage : ZERO_LAYOUT_UNIT;
logicalTopPosition = max(logicalTopPosition, logicalTopPosition + offsetFromLogicalTopOfRegion);
containingBlockRegion = cb->clampToStartAndEndRegions(region);
}
LayoutUnit result = cb->availableLogicalWidthForLine(logicalTopPosition, false, containingBlockRegion, adjustedPageOffsetForContainingBlock) - childMarginStart - childMarginEnd;
if (childMarginStart > 0) {
LayoutUnit startContentSide = cb->startOffsetForContent(containingBlockRegion, adjustedPageOffsetForContainingBlock);
LayoutUnit startContentSideWithMargin = startContentSide + childMarginStart;
LayoutUnit startOffset = cb->startOffsetForLine(logicalTopPosition, false, containingBlockRegion, adjustedPageOffsetForContainingBlock);
if (startOffset > startContentSideWithMargin)
result += childMarginStart;
else
result += startOffset - startContentSide;
}
if (childMarginEnd > 0) {
LayoutUnit endContentSide = cb->endOffsetForContent(containingBlockRegion, adjustedPageOffsetForContainingBlock);
LayoutUnit endContentSideWithMargin = endContentSide + childMarginEnd;
LayoutUnit endOffset = cb->endOffsetForLine(logicalTopPosition, false, containingBlockRegion, adjustedPageOffsetForContainingBlock);
if (endOffset > endContentSideWithMargin)
result += childMarginEnd;
else
result += endOffset - endContentSide;
}
return result;
}
LayoutUnit RenderBox::containingBlockLogicalWidthForContent() const
{
RenderBlock* cb = containingBlock();
return cb->availableLogicalWidth();
}
LayoutUnit RenderBox::containingBlockLogicalWidthForContentInRegion(RenderRegion* region, LayoutUnit offsetFromLogicalTopOfFirstPage) const
{
if (!region)
return containingBlockLogicalWidthForContent();
RenderBlock* cb = containingBlock();
RenderRegion* containingBlockRegion = cb->clampToStartAndEndRegions(region);
LayoutUnit result = cb->availableLogicalWidth();
RenderBoxRegionInfo* boxInfo = cb->renderBoxRegionInfo(containingBlockRegion, offsetFromLogicalTopOfFirstPage - logicalTop());
if (!boxInfo)
return result;
return max<LayoutUnit>(0, result - (cb->logicalWidth() - boxInfo->logicalWidth()));
}
LayoutUnit RenderBox::containingBlockAvailableLineWidthInRegion(RenderRegion* region, LayoutUnit offsetFromLogicalTopOfFirstPage) const
{
RenderBlock* cb = containingBlock();
RenderRegion* containingBlockRegion = 0;
LayoutUnit logicalTopPosition = logicalTop();
LayoutUnit adjustedPageOffsetForContainingBlock = offsetFromLogicalTopOfFirstPage - logicalTop();
if (region) {
LayoutUnit offsetFromLogicalTopOfRegion = region ? region->offsetFromLogicalTopOfFirstPage() - offsetFromLogicalTopOfFirstPage : ZERO_LAYOUT_UNIT;
logicalTopPosition = max(logicalTopPosition, logicalTopPosition + offsetFromLogicalTopOfRegion);
containingBlockRegion = cb->clampToStartAndEndRegions(region);
}
return cb->availableLogicalWidthForLine(logicalTopPosition, false, containingBlockRegion, adjustedPageOffsetForContainingBlock);
}
LayoutUnit RenderBox::perpendicularContainingBlockLogicalHeight() const
{
RenderBlock* cb = containingBlock();
RenderStyle* containingBlockStyle = cb->style();
Length logicalHeightLength = containingBlockStyle->logicalHeight();
if (!logicalHeightLength.isFixed()) {
return containingBlockStyle->isHorizontalWritingMode() ? view()->frameView()->visibleHeight() : view()->frameView()->visibleWidth();
}
return cb->computeContentBoxLogicalHeight(logicalHeightLength.value());
}
void RenderBox::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState, ApplyContainerFlipOrNot, bool* wasFixed) const
{
if (repaintContainer == this)
return;
if (RenderView* v = view()) {
if (v->layoutStateEnabled() && !repaintContainer) {
LayoutState* layoutState = v->layoutState();
LayoutSize offset = layoutState->m_paintOffset + locationOffset();
if (style()->hasInFlowPosition() && layer())
offset += layer()->offsetForInFlowPosition();
transformState.move(offset);
return;
}
}
bool containerSkipped;
RenderObject* o = container(repaintContainer, &containerSkipped);
if (!o)
return;
bool isFixedPos = style()->position() == FixedPosition;
bool hasTransform = hasLayer() && layer()->transform();
if (hasTransform && !isFixedPos)
fixed = false;
else if (isFixedPos)
fixed = true;
if (wasFixed)
*wasFixed = fixed;
LayoutSize containerOffset = offsetFromContainer(o, roundedLayoutPoint(transformState.mappedPoint()));
bool preserve3D = useTransforms && (o->style()->preserves3D() || style()->preserves3D());
if (useTransforms && shouldUseTransformFromContainer(o)) {
TransformationMatrix t;
getTransformFromContainer(o, containerOffset, t);
transformState.applyTransform(t, preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
} else
transformState.move(containerOffset.width(), containerOffset.height(), preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
if (containerSkipped) {
LayoutSize containerOffset = repaintContainer->offsetFromAncestorContainer(o);
transformState.move(-containerOffset.width(), -containerOffset.height(), preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
return;
}
if (o->isRenderFlowThread()) {
RenderRegion* region = toRenderFlowThread(o)->mapFromFlowToRegion(transformState);
if (region)
region->mapLocalToContainer(region->containerForRepaint(), fixed, useTransforms, transformState, DoNotApplyContainerFlip, wasFixed);
return;
}
o->mapLocalToContainer(repaintContainer, fixed, useTransforms, transformState, DoNotApplyContainerFlip, wasFixed);
}
const RenderObject* RenderBox::pushMappingToContainer(const RenderBoxModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
{
ASSERT(ancestorToStopAt != this);
bool ancestorSkipped;
RenderObject* container = this->container(ancestorToStopAt, &ancestorSkipped);
if (!container)
return 0;
bool isFixedPos = style()->position() == FixedPosition;
bool hasTransform = hasLayer() && layer()->transform();
LayoutSize adjustmentForSkippedAncestor;
if (ancestorSkipped) {
adjustmentForSkippedAncestor = -ancestorToStopAt->offsetFromAncestorContainer(container);
}
bool offsetDependsOnPoint = false;
LayoutSize containerOffset = offsetFromContainer(container, LayoutPoint(), &offsetDependsOnPoint);
if (container->isRenderFlowThread())
offsetDependsOnPoint = true;
bool preserve3D = container->style()->preserves3D() || style()->preserves3D();
if (shouldUseTransformFromContainer(container)) {
TransformationMatrix t;
getTransformFromContainer(container, containerOffset, t);
t.translateRight(adjustmentForSkippedAncestor.width(), adjustmentForSkippedAncestor.height());
geometryMap.push(this, t, preserve3D, offsetDependsOnPoint, isFixedPos, hasTransform);
} else {
containerOffset += adjustmentForSkippedAncestor;
geometryMap.push(this, containerOffset, preserve3D, offsetDependsOnPoint, isFixedPos, hasTransform);
}
return ancestorSkipped ? ancestorToStopAt : container;
}
void RenderBox::mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState& transformState) const
{
ASSERT(!view() || !view()->layoutStateEnabled());
bool isFixedPos = style()->position() == FixedPosition;
bool hasTransform = hasLayer() && layer()->transform();
if (hasTransform) {
fixed &= isFixedPos;
} else
fixed |= isFixedPos;
RenderBoxModelObject::mapAbsoluteToLocalPoint(fixed, useTransforms, transformState);
}
LayoutSize RenderBox::offsetFromContainer(RenderObject* o, const LayoutPoint& point, bool* offsetDependsOnPoint) const
{
ASSERT(o == container());
LayoutSize offset;
if (isInFlowPositioned())
offset += offsetForInFlowPosition();
if (!isInline() || isReplaced()) {
if (!style()->hasOutOfFlowPosition() && o->hasColumns()) {
RenderBlock* block = toRenderBlock(o);
LayoutRect columnRect(frameRect());
block->adjustStartEdgeForWritingModeIncludingColumns(columnRect);
offset += toSize(columnRect.location());
LayoutPoint columnPoint = block->flipForWritingModeIncludingColumns(point + offset);
offset = toLayoutSize(block->flipForWritingModeIncludingColumns(toLayoutPoint(offset)));
o->adjustForColumns(offset, columnPoint);
offset = block->flipForWritingMode(offset);
if (offsetDependsOnPoint)
*offsetDependsOnPoint = true;
} else
offset += topLeftLocationOffset();
}
if (o->hasOverflowClip())
offset -= toRenderBox(o)->scrolledContentOffset();
if (style()->position() == AbsolutePosition && o->isInFlowPositioned() && o->isRenderInline())
offset += toRenderInline(o)->offsetForInFlowPositionedInline(this);
return offset;
}
InlineBox* RenderBox::createInlineBox()
{
return new (renderArena()) InlineBox(this);
}
void RenderBox::dirtyLineBoxes(bool fullLayout)
{
if (m_inlineBoxWrapper) {
if (fullLayout) {
m_inlineBoxWrapper->destroy(renderArena());
m_inlineBoxWrapper = 0;
} else
m_inlineBoxWrapper->dirtyLineBoxes();
}
}
void RenderBox::positionLineBox(InlineBox* box)
{
if (isOutOfFlowPositioned()) {
bool wasInline = style()->isOriginalDisplayInlineType();
if (wasInline) {
RootInlineBox* root = box->root();
root->block()->setStaticInlinePositionForChild(this, root->lineTopWithLeading(), roundedLayoutUnit(box->logicalLeft()));
if (style()->hasStaticInlinePosition(box->isHorizontal()))
setChildNeedsLayout(true, MarkOnlyThis); } else {
layer()->setStaticBlockPosition(box->logicalTop());
if (style()->hasStaticBlockPosition(box->isHorizontal()))
setChildNeedsLayout(true, MarkOnlyThis); }
box->remove();
box->destroy(renderArena());
} else if (isReplaced()) {
setLocation(roundedLayoutPoint(box->topLeft()));
ASSERT(!m_inlineBoxWrapper);
if (m_inlineBoxWrapper)
deleteLineBoxWrapper();
m_inlineBoxWrapper = box;
}
}
void RenderBox::deleteLineBoxWrapper()
{
if (m_inlineBoxWrapper) {
if (!documentBeingDestroyed())
m_inlineBoxWrapper->remove();
m_inlineBoxWrapper->destroy(renderArena());
m_inlineBoxWrapper = 0;
}
}
LayoutRect RenderBox::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer) const
{
if (style()->visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
return LayoutRect();
LayoutRect r = visualOverflowRect();
RenderView* v = view();
if (v) {
r.move(v->layoutDelta());
}
if (style()) {
if (v) {
ASSERT(style()->outlineSize() <= v->maximalOutlineSize());
r.inflate(v->maximalOutlineSize());
}
}
computeRectForRepaint(repaintContainer, r);
return r;
}
void RenderBox::computeRectForRepaint(RenderBoxModelObject* repaintContainer, LayoutRect& rect, bool fixed) const
{
RenderStyle* styleToUse = style();
if (RenderView* v = view()) {
if (v->layoutStateEnabled() && !repaintContainer && styleToUse->position() != FixedPosition) {
LayoutState* layoutState = v->layoutState();
if (layer() && layer()->transform())
rect = layer()->transform()->mapRect(rect);
if (styleToUse->hasInFlowPosition() && layer())
rect.move(layer()->offsetForInFlowPosition());
rect.moveBy(location());
rect.move(layoutState->m_paintOffset);
if (layoutState->m_clipped)
rect.intersect(layoutState->m_clipRect);
return;
}
}
if (hasReflection())
rect.unite(reflectedRect(rect));
if (repaintContainer == this) {
if (repaintContainer->style()->isFlippedBlocksWritingMode())
flipForWritingMode(rect);
return;
}
bool containerSkipped;
RenderObject* o = container(repaintContainer, &containerSkipped);
if (!o)
return;
if (isWritingModeRoot() && !isOutOfFlowPositioned())
flipForWritingMode(rect);
LayoutPoint topLeft = rect.location();
topLeft.move(locationOffset());
EPosition position = styleToUse->position();
if (layer() && layer()->transform()) {
fixed = position == FixedPosition;
rect = layer()->transform()->mapRect(rect);
topLeft = rect.location();
topLeft.move(locationOffset());
} else if (position == FixedPosition)
fixed = true;
if (position == AbsolutePosition && o->isInFlowPositioned() && o->isRenderInline())
topLeft += toRenderInline(o)->offsetForInFlowPositionedInline(this);
else if ((position == RelativePosition || position == StickyPosition) && layer()) {
topLeft += layer()->offsetForInFlowPosition();
}
if (o->isBlockFlow() && position != AbsolutePosition && position != FixedPosition) {
RenderBlock* cb = toRenderBlock(o);
if (cb->hasColumns()) {
LayoutRect repaintRect(topLeft, rect.size());
cb->adjustRectForColumns(repaintRect);
topLeft = repaintRect.location();
rect = repaintRect;
}
}
if (o->hasOverflowClip()) {
RenderBox* containerBox = toRenderBox(o);
topLeft -= containerBox->scrolledContentOffset();
if (!containerBox->layer() || !containerBox->layer()->hasAcceleratedTouchScrolling()) {
LayoutRect repaintRect(topLeft, rect.size());
LayoutRect boxRect(LayoutPoint(), containerBox->cachedSizeForOverflowClip());
rect = intersection(repaintRect, boxRect);
if (rect.isEmpty())
return;
} else
rect.setLocation(topLeft);
} else
rect.setLocation(topLeft);
if (containerSkipped) {
LayoutSize containerOffset = repaintContainer->offsetFromAncestorContainer(o);
rect.move(-containerOffset);
return;
}
o->computeRectForRepaint(repaintContainer, rect, fixed);
}
void RenderBox::repaintDuringLayoutIfMoved(const LayoutRect& rect)
{
LayoutUnit newX = x();
LayoutUnit newY = y();
LayoutUnit newWidth = width();
LayoutUnit newHeight = height();
if (rect.x() != newX || rect.y() != newY) {
m_frameRect = rect;
repaint();
repaintOverhangingFloats(true);
m_frameRect = LayoutRect(newX, newY, newWidth, newHeight);
repaint();
repaintOverhangingFloats(true);
}
}
void RenderBox::computeLogicalWidth()
{
computeLogicalWidthInRegion();
}
void RenderBox::computeLogicalWidthInRegion(RenderRegion* region, LayoutUnit offsetFromLogicalTopOfFirstPage)
{
if (isOutOfFlowPositioned()) {
computePositionedLogicalWidth(region, offsetFromLogicalTopOfFirstPage);
return;
}
if (node() && view()->frameView() && view()->frameView()->layoutRoot(true) == this)
return;
if (hasOverrideWidth() && parent()->isFlexibleBoxIncludingDeprecated()) {
setLogicalWidth(overrideWidth());
return;
}
bool inVerticalBox = parent()->isDeprecatedFlexibleBox() && (parent()->style()->boxOrient() == VERTICAL);
bool stretching = (parent()->style()->boxAlign() == BSTRETCH);
bool treatAsReplaced = shouldComputeSizeAsReplaced() && (!inVerticalBox || !stretching);
RenderStyle* styleToUse = style();
Length logicalWidthLength = (treatAsReplaced) ? Length(computeReplacedLogicalWidth(), Fixed) : styleToUse->logicalWidth();
RenderBlock* cb = containingBlock();
LayoutUnit containerLogicalWidth = max<LayoutUnit>(0, containingBlockLogicalWidthForContentInRegion(region, offsetFromLogicalTopOfFirstPage));
bool hasPerpendicularContainingBlock = cb->isHorizontalWritingMode() != isHorizontalWritingMode();
LayoutUnit containerWidthInInlineDirection = containerLogicalWidth;
if (hasPerpendicularContainingBlock)
containerWidthInInlineDirection = perpendicularContainingBlockLogicalHeight();
if (isInline() && !isInlineBlockOrInlineTable()) {
RenderView* renderView = view();
setMarginStart(minimumValueForLength(styleToUse->marginStart(), containerLogicalWidth, renderView));
setMarginEnd(minimumValueForLength(styleToUse->marginEnd(), containerLogicalWidth, renderView));
if (treatAsReplaced)
setLogicalWidth(max<LayoutUnit>(floatValueForLength(logicalWidthLength, 0) + borderAndPaddingLogicalWidth(), minPreferredLogicalWidth()));
return;
}
if (treatAsReplaced)
setLogicalWidth(logicalWidthLength.value() + borderAndPaddingLogicalWidth());
else {
setLogicalWidth(computeLogicalWidthInRegionUsing(LogicalWidth, containerWidthInInlineDirection, cb, region, offsetFromLogicalTopOfFirstPage));
if (!styleToUse->logicalMaxWidth().isUndefined()) {
LayoutUnit maxLogicalWidth = computeLogicalWidthInRegionUsing(MaxLogicalWidth, containerWidthInInlineDirection, cb, region, offsetFromLogicalTopOfFirstPage);
if (logicalWidth() > maxLogicalWidth) {
setLogicalWidth(maxLogicalWidth);
logicalWidthLength = styleToUse->logicalMaxWidth();
}
}
LayoutUnit minLogicalWidth = computeLogicalWidthInRegionUsing(MinLogicalWidth, containerWidthInInlineDirection, cb, region, offsetFromLogicalTopOfFirstPage);
if (logicalWidth() < minLogicalWidth) {
setLogicalWidth(minLogicalWidth);
logicalWidthLength = styleToUse->logicalMinWidth();
}
}
if (stretchesToMinIntrinsicLogicalWidth()) {
setLogicalWidth(max(logicalWidth(), minPreferredLogicalWidth()));
logicalWidthLength = Length(logicalWidth(), Fixed);
}
if (hasPerpendicularContainingBlock || isFloating() || isInline()) {
RenderView* renderView = view();
setMarginStart(minimumValueForLength(styleToUse->marginStart(), containerLogicalWidth, renderView));
setMarginEnd(minimumValueForLength(styleToUse->marginEnd(), containerLogicalWidth, renderView));
} else {
LayoutUnit containerLogicalWidthForAutoMargins = containerLogicalWidth;
if (avoidsFloats() && cb->containsFloats())
containerLogicalWidthForAutoMargins = containingBlockAvailableLineWidthInRegion(region, offsetFromLogicalTopOfFirstPage);
computeInlineDirectionMargins(cb, containerLogicalWidthForAutoMargins, logicalWidth());
}
if (!hasPerpendicularContainingBlock && containerLogicalWidth && containerLogicalWidth != (logicalWidth() + marginStart() + marginEnd())
&& !isFloating() && !isInline() && !cb->isFlexibleBoxIncludingDeprecated())
cb->setMarginEndForChild(this, containerLogicalWidth - logicalWidth() - cb->marginStartForChild(this));
}
LayoutUnit RenderBox::computeLogicalWidthInRegionUsing(LogicalWidthType widthType, LayoutUnit availableLogicalWidth,
const RenderBlock* cb, RenderRegion* region, LayoutUnit offsetFromLogicalTopOfFirstPage)
{
LayoutUnit logicalWidthResult = logicalWidth();
RenderStyle* styleToUse = style();
Length logicalWidth;
if (widthType == LogicalWidth)
logicalWidth = styleToUse->logicalWidth();
else if (widthType == MinLogicalWidth)
logicalWidth = styleToUse->logicalMinWidth();
else
logicalWidth = styleToUse->logicalMaxWidth();
ASSERT(!logicalWidth.isUndefined());
if (logicalWidth.isIntrinsicOrAuto()) {
RenderView* renderView = view();
LayoutUnit marginStart = minimumValueForLength(styleToUse->marginStart(), availableLogicalWidth, renderView);
LayoutUnit marginEnd = minimumValueForLength(styleToUse->marginEnd(), availableLogicalWidth, renderView);
logicalWidthResult = availableLogicalWidth - marginStart - marginEnd;
if (shrinkToAvoidFloats() && cb->containsFloats())
logicalWidthResult = shrinkLogicalWidthToAvoidFloats(marginStart, marginEnd, cb, region, offsetFromLogicalTopOfFirstPage);
if (sizesToIntrinsicLogicalWidth(widthType)) {
logicalWidthResult = max(logicalWidthResult, minPreferredLogicalWidth());
logicalWidthResult = min(logicalWidthResult, maxPreferredLogicalWidth());
}
} else logicalWidthResult = computeBorderBoxLogicalWidth(valueForLength(logicalWidth, availableLogicalWidth, view()));
return logicalWidthResult;
}
bool RenderBox::sizesToIntrinsicLogicalWidth(LogicalWidthType widthType) const
{
if (isFloating() || (isInlineBlockOrInlineTable() && !isHTMLMarquee()))
return true;
Length logicalWidth = (widthType == MaxLogicalWidth) ? style()->logicalMaxWidth() : style()->logicalWidth();
if (logicalWidth.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()) {
if (!parent()->style()->isColumnFlexDirection() || parent()->style()->flexWrap() != FlexWrapNone)
return true;
EFlexAlign itemAlign = style()->flexItemAlign();
if (itemAlign != AlignStretch && (itemAlign != AlignAuto || parent()->style()->flexAlign() != AlignStretch))
return true;
}
if (parent()->isDeprecatedFlexibleBox()
&& (parent()->style()->boxOrient() == HORIZONTAL || parent()->style()->boxAlign() != BSTRETCH))
return true;
if (logicalWidth.type() == Auto && !(parent()->isDeprecatedFlexibleBox() && parent()->style()->boxOrient() == VERTICAL && parent()->style()->boxAlign() == BSTRETCH) && node() && (node()->hasTagName(inputTag) || node()->hasTagName(selectTag) || node()->hasTagName(buttonTag) || node()->hasTagName(textareaTag) || node()->hasTagName(legendTag)))
return true;
if (isHorizontalWritingMode() != containingBlock()->isHorizontalWritingMode())
return true;
return false;
}
void RenderBox::computeInlineDirectionMargins(RenderBlock* containingBlock, LayoutUnit containerWidth, LayoutUnit childWidth)
{
const RenderStyle* containingBlockStyle = containingBlock->style();
Length marginStartLength = style()->marginStartUsing(containingBlockStyle);
Length marginEndLength = style()->marginEndUsing(containingBlockStyle);
RenderView* renderView = view();
if (isFloating() || isInline()) {
containingBlock->setMarginStartForChild(this, minimumValueForLength(marginStartLength, containerWidth, renderView));
containingBlock->setMarginEndForChild(this, minimumValueForLength(marginEndLength, containerWidth, renderView));
return;
}
if ((marginStartLength.isAuto() && marginEndLength.isAuto() && childWidth < containerWidth)
|| (!marginStartLength.isAuto() && !marginEndLength.isAuto() && containingBlock->style()->textAlign() == WEBKIT_CENTER)) {
containingBlock->setMarginStartForChild(this, max<LayoutUnit>(0, (containerWidth - childWidth) / 2));
containingBlock->setMarginEndForChild(this, containerWidth - childWidth - containingBlock->marginStartForChild(this));
return;
}
if (marginEndLength.isAuto() && childWidth < containerWidth) {
containingBlock->setMarginStartForChild(this, valueForLength(marginStartLength, containerWidth, renderView));
containingBlock->setMarginEndForChild(this, containerWidth - childWidth - containingBlock->marginStartForChild(this));
return;
}
bool pushToEndFromTextAlign = !marginEndLength.isAuto() && ((!containingBlockStyle->isLeftToRightDirection() && containingBlockStyle->textAlign() == WEBKIT_LEFT)
|| (containingBlockStyle->isLeftToRightDirection() && containingBlockStyle->textAlign() == WEBKIT_RIGHT));
if ((marginStartLength.isAuto() && childWidth < containerWidth) || pushToEndFromTextAlign) {
containingBlock->setMarginEndForChild(this, valueForLength(marginEndLength, containerWidth, renderView));
containingBlock->setMarginStartForChild(this, containerWidth - childWidth - containingBlock->marginEndForChild(this));
return;
}
containingBlock->setMarginStartForChild(this, minimumValueForLength(marginStartLength, containerWidth, renderView));
containingBlock->setMarginEndForChild(this, minimumValueForLength(marginEndLength, containerWidth, renderView));
}
RenderBoxRegionInfo* RenderBox::renderBoxRegionInfo(RenderRegion* region, LayoutUnit offsetFromLogicalTopOfFirstPage, RenderBoxRegionInfoFlags cacheFlag) const
{
if (!region)
return 0;
RenderBoxRegionInfo* boxInfo = region->renderBoxRegionInfo(this);
if (boxInfo && cacheFlag == CacheRenderBoxRegionInfo)
return boxInfo;
if (!inRenderFlowThread() || isFloating() || isReplaced() || isInline() || hasColumns()
|| isTableCell() || !isBlockFlow() || isRenderFlowThread())
return 0;
RenderFlowThread* flowThread = enclosingRenderFlowThread();
if (flowThread->style()->writingMode() != style()->writingMode())
return 0;
LayoutUnit oldLogicalWidth = logicalWidth();
LayoutUnit oldLogicalLeft = logicalLeft();
LayoutUnit oldMarginStart = marginStart();
LayoutUnit oldMarginEnd = marginEnd();
RenderBox* mutableBox = const_cast<RenderBox*>(this);
mutableBox->computeLogicalWidthInRegion(region, offsetFromLogicalTopOfFirstPage);
RenderBlock* cb = containingBlock();
RenderRegion* clampedContainingBlockRegion = cb->clampToStartAndEndRegions(region);
RenderBoxRegionInfo* containingBlockInfo = cb->renderBoxRegionInfo(clampedContainingBlockRegion,
offsetFromLogicalTopOfFirstPage - logicalTop());
LayoutUnit containingBlockLogicalWidth = cb->logicalWidth();
LayoutUnit containingBlockLogicalWidthInRegion = containingBlockInfo ? containingBlockInfo->logicalWidth() : containingBlockLogicalWidth;
LayoutUnit marginStartInRegion = marginStart();
LayoutUnit startMarginDelta = marginStartInRegion - oldMarginStart;
LayoutUnit logicalWidthInRegion = logicalWidth();
LayoutUnit logicalLeftInRegion = logicalLeft();
LayoutUnit widthDelta = logicalWidthInRegion - oldLogicalWidth;
LayoutUnit logicalLeftDelta = isOutOfFlowPositioned() ? logicalLeftInRegion - oldLogicalLeft : startMarginDelta;
LayoutUnit logicalRightInRegion = containingBlockLogicalWidthInRegion - (logicalLeftInRegion + logicalWidthInRegion);
LayoutUnit oldLogicalRight = containingBlockLogicalWidth - (oldLogicalLeft + oldLogicalWidth);
LayoutUnit logicalRightDelta = isOutOfFlowPositioned() ? logicalRightInRegion - oldLogicalRight : startMarginDelta;
mutableBox->setLogicalWidth(oldLogicalWidth);
mutableBox->setLogicalLeft(oldLogicalLeft);
mutableBox->setMarginStart(oldMarginStart);
mutableBox->setMarginEnd(oldMarginEnd);
LayoutUnit logicalLeftOffset = 0;
if (!isOutOfFlowPositioned() && avoidsFloats() && cb->containsFloats()) {
LayoutUnit startPositionDelta = cb->computeStartPositionDeltaForChildAvoidingFloats(this, marginStartInRegion, region, offsetFromLogicalTopOfFirstPage);
if (cb->style()->isLeftToRightDirection())
logicalLeftDelta += startPositionDelta;
else
logicalRightDelta += startPositionDelta;
}
if (cb->style()->isLeftToRightDirection())
logicalLeftOffset += logicalLeftDelta;
else
logicalLeftOffset -= (widthDelta + logicalRightDelta);
LayoutUnit logicalRightOffset = logicalWidth() - (logicalLeftOffset + logicalWidthInRegion);
bool isShifted = (containingBlockInfo && containingBlockInfo->isShifted())
|| (style()->isLeftToRightDirection() && logicalLeftOffset)
|| (!style()->isLeftToRightDirection() && logicalRightOffset);
if (cacheFlag == CacheRenderBoxRegionInfo)
return region->setRenderBoxRegionInfo(this, logicalLeftOffset, logicalWidthInRegion, isShifted);
return new RenderBoxRegionInfo(logicalLeftOffset, logicalWidthInRegion, isShifted);
}
void RenderBox::computeLogicalHeight()
{
if (isTableCell() || (isInline() && !isReplaced()))
return;
Length h;
if (isOutOfFlowPositioned())
computePositionedLogicalHeight();
else {
RenderBlock* cb = containingBlock();
bool hasPerpendicularContainingBlock = cb->isHorizontalWritingMode() != isHorizontalWritingMode();
if (!hasPerpendicularContainingBlock)
computeBlockDirectionMargins(cb);
if (isTable()) {
if (hasPerpendicularContainingBlock)
computeInlineDirectionMargins(cb, containingBlockLogicalWidthForContent(), logicalHeight());
return;
}
bool inHorizontalBox = parent()->isDeprecatedFlexibleBox() && parent()->style()->boxOrient() == HORIZONTAL;
bool stretching = parent()->style()->boxAlign() == BSTRETCH;
bool treatAsReplaced = shouldComputeSizeAsReplaced() && (!inHorizontalBox || !stretching);
bool checkMinMaxHeight = false;
RenderStyle* styleToUse = style();
if (hasOverrideHeight() && parent()->isFlexibleBoxIncludingDeprecated())
h = Length(overrideHeight() - borderAndPaddingLogicalHeight(), Fixed);
else if (treatAsReplaced)
h = Length(computeReplacedLogicalHeight(), Fixed);
else {
h = styleToUse->logicalHeight();
checkMinMaxHeight = true;
}
if (h.isAuto() && parent()->isDeprecatedFlexibleBox() && parent()->style()->boxOrient() == HORIZONTAL
&& parent()->isStretchingChildren()) {
h = Length(parentBox()->contentLogicalHeight() - marginBefore() - marginAfter() - borderAndPaddingLogicalHeight(), Fixed);
checkMinMaxHeight = false;
}
LayoutUnit heightResult;
if (checkMinMaxHeight) {
heightResult = computeLogicalHeightUsing(styleToUse->logicalHeight());
if (heightResult == -1)
heightResult = logicalHeight();
LayoutUnit minH = computeLogicalHeightUsing(styleToUse->logicalMinHeight()); LayoutUnit maxH = styleToUse->logicalMaxHeight().isUndefined() ? heightResult : computeLogicalHeightUsing(styleToUse->logicalMaxHeight());
if (maxH == -1)
maxH = heightResult;
heightResult = min(maxH, heightResult);
heightResult = max(minH, heightResult);
} else {
heightResult = h.value() + borderAndPaddingLogicalHeight();
}
setLogicalHeight(heightResult);
if (hasPerpendicularContainingBlock)
computeInlineDirectionMargins(cb, containingBlockLogicalWidthForContent(), heightResult);
}
bool paginatedContentNeedsBaseHeight = document()->printing() && h.isPercent()
&& (isRoot() || (isBody() && document()->documentElement()->renderer()->style()->logicalHeight().isPercent()));
if (stretchesToViewport() || paginatedContentNeedsBaseHeight) {
LayoutUnit margins = collapsedMarginBefore() + collapsedMarginAfter();
LayoutUnit visHeight;
if (document()->printing())
visHeight = static_cast<LayoutUnit>(view()->pageLogicalHeight());
else {
if (isHorizontalWritingMode())
visHeight = view()->viewHeight();
else
visHeight = view()->viewWidth();
}
if (isRoot())
setLogicalHeight(max(logicalHeight(), visHeight - margins));
else {
LayoutUnit marginsBordersPadding = margins + parentBox()->marginBefore() + parentBox()->marginAfter() + parentBox()->borderAndPaddingLogicalHeight();
setLogicalHeight(max(logicalHeight(), visHeight - marginsBordersPadding));
}
}
}
LayoutUnit RenderBox::computeLogicalHeightUsing(const Length& height)
{
LayoutUnit logicalHeight = computeContentLogicalHeightUsing(height);
if (logicalHeight != -1)
logicalHeight = computeBorderBoxLogicalHeight(logicalHeight);
return logicalHeight;
}
LayoutUnit RenderBox::computeContentLogicalHeightUsing(const Length& height)
{
LayoutUnit logicalHeight = -1;
if (!height.isAuto()) {
if (height.isFixed())
logicalHeight = height.value();
else if (height.isPercent())
logicalHeight = computePercentageLogicalHeight(height);
else if (height.isViewportPercentage())
logicalHeight = valueForLength(height, 0, view());
}
return logicalHeight;
}
LayoutUnit RenderBox::computePercentageLogicalHeight(const Length& height)
{
LayoutUnit result = -1;
bool skippedAutoHeightContainingBlock = false;
RenderBlock* cb = containingBlock();
while (!cb->isRenderView() && !cb->isBody() && !cb->isTableCell() && !cb->isOutOfFlowPositioned() && cb->style()->logicalHeight().isAuto()) {
if (!document()->inQuirksMode() && !cb->isAnonymousBlock())
break;
skippedAutoHeightContainingBlock = true;
cb = cb->containingBlock();
cb->addPercentHeightDescendant(this);
}
RenderStyle* cbstyle = cb->style();
bool isOutOfFlowPositionedWithSpecifiedHeight = cb->isOutOfFlowPositioned() && (!cbstyle->logicalHeight().isAuto() || (!cbstyle->top().isAuto() && !cbstyle->bottom().isAuto()));
bool includeBorderPadding = isTable();
if (cb->isTableCell()) {
if (!skippedAutoHeightContainingBlock) {
if (!cb->hasOverrideHeight()) {
RenderTableCell* cell = toRenderTableCell(cb);
if (scrollsOverflowY() && (!cell->style()->logicalHeight().isAuto() || !cell->table()->style()->logicalHeight().isAuto()))
return 0;
return -1;
}
result = cb->overrideHeight();
includeBorderPadding = true;
}
}
else if (cbstyle->logicalHeight().isFixed())
result = cb->computeContentBoxLogicalHeight(cbstyle->logicalHeight().value());
else if (cbstyle->logicalHeight().isPercent() && !isOutOfFlowPositionedWithSpecifiedHeight) {
result = cb->computePercentageLogicalHeight(cbstyle->logicalHeight());
if (result != -1)
result = cb->computeContentBoxLogicalHeight(result);
} else if (cb->isRenderView() || (cb->isBody() && document()->inQuirksMode()) || isOutOfFlowPositionedWithSpecifiedHeight) {
LayoutUnit oldHeight = cb->logicalHeight();
cb->computeLogicalHeight();
result = cb->contentLogicalHeight();
cb->setLogicalHeight(oldHeight);
} else if (cb->isRoot() && isOutOfFlowPositioned())
result = cb->computeContentBoxLogicalHeight(cb->availableLogicalHeight());
if (result != -1) {
result = valueForLength(height, result);
if (includeBorderPadding) {
result -= borderAndPaddingLogicalHeight();
result = max<LayoutUnit>(0, result);
}
}
return result;
}
LayoutUnit RenderBox::computeReplacedLogicalWidth(bool includeMaxWidth) const
{
return computeReplacedLogicalWidthRespectingMinMaxWidth(computeReplacedLogicalWidthUsing(style()->logicalWidth()), includeMaxWidth);
}
LayoutUnit RenderBox::computeReplacedLogicalWidthRespectingMinMaxWidth(LayoutUnit logicalWidth, bool includeMaxWidth) const
{
LayoutUnit minLogicalWidth = computeReplacedLogicalWidthUsing(style()->logicalMinWidth());
LayoutUnit maxLogicalWidth = !includeMaxWidth || style()->logicalMaxWidth().isUndefined() ? logicalWidth : computeReplacedLogicalWidthUsing(style()->logicalMaxWidth());
return max(minLogicalWidth, min(logicalWidth, maxLogicalWidth));
}
LayoutUnit RenderBox::computeReplacedLogicalWidthUsing(Length logicalWidth) const
{
switch (logicalWidth.type()) {
case Fixed:
return computeContentBoxLogicalWidth(logicalWidth.value());
case ViewportPercentageWidth:
case ViewportPercentageHeight:
case ViewportPercentageMin:
return computeContentBoxLogicalWidth(valueForLength(logicalWidth, 0, view()));
case Percent:
case Calculated: {
const LayoutUnit cw = isOutOfFlowPositioned() ? containingBlockLogicalWidthForPositioned(toRenderBoxModelObject(container())) : containingBlockLogicalWidthForContent();
if (cw > 0)
return computeContentBoxLogicalWidth(minimumValueForLength(logicalWidth, cw));
}
default:
return intrinsicLogicalWidth();
}
}
LayoutUnit RenderBox::computeReplacedLogicalHeight() const
{
return computeReplacedLogicalHeightRespectingMinMaxHeight(computeReplacedLogicalHeightUsing(style()->logicalHeight()));
}
LayoutUnit RenderBox::computeReplacedLogicalHeightRespectingMinMaxHeight(LayoutUnit logicalHeight) const
{
LayoutUnit minLogicalHeight = computeReplacedLogicalHeightUsing(style()->logicalMinHeight());
LayoutUnit maxLogicalHeight = style()->logicalMaxHeight().isUndefined() ? logicalHeight : computeReplacedLogicalHeightUsing(style()->logicalMaxHeight());
return max(minLogicalHeight, min(logicalHeight, maxLogicalHeight));
}
LayoutUnit RenderBox::computeReplacedLogicalHeightUsing(Length logicalHeight) const
{
switch (logicalHeight.type()) {
case Fixed:
return computeContentBoxLogicalHeight(logicalHeight.value());
case Percent:
case Calculated:
{
RenderObject* cb = isOutOfFlowPositioned() ? container() : containingBlock();
while (cb->isAnonymous()) {
cb = cb->containingBlock();
toRenderBlock(cb)->addPercentHeightDescendant(const_cast<RenderBox*>(this));
}
if (cb->isOutOfFlowPositioned() && cb->style()->height().isAuto() && !(cb->style()->top().isAuto() || cb->style()->bottom().isAuto())) {
ASSERT(cb->isRenderBlock());
RenderBlock* block = toRenderBlock(cb);
LayoutUnit oldHeight = block->height();
block->computeLogicalHeight();
LayoutUnit newHeight = block->computeContentBoxLogicalHeight(block->contentHeight());
block->setHeight(oldHeight);
return computeContentBoxLogicalHeight(valueForLength(logicalHeight, newHeight));
}
LayoutUnit availableHeight;
if (isOutOfFlowPositioned())
availableHeight = containingBlockLogicalHeightForPositioned(toRenderBoxModelObject(cb));
else {
availableHeight = toRenderBox(cb)->availableLogicalHeight();
while (cb && !cb->isRenderView() && (cb->style()->logicalHeight().isAuto() || cb->style()->logicalHeight().isPercent())) {
if (cb->isTableCell()) {
availableHeight = max(availableHeight, intrinsicLogicalHeight());
return valueForLength(logicalHeight, availableHeight - borderAndPaddingLogicalHeight());
}
cb = cb->containingBlock();
toRenderBlock(cb)->addPercentHeightDescendant(const_cast<RenderBox*>(this));
}
}
return computeContentBoxLogicalHeight(valueForLength(logicalHeight, availableHeight));
}
case ViewportPercentageWidth:
case ViewportPercentageHeight:
case ViewportPercentageMin:
return computeContentBoxLogicalHeight(valueForLength(logicalHeight, 0, view()));
default:
return intrinsicLogicalHeight();
}
}
LayoutUnit RenderBox::availableLogicalHeight() const
{
return availableLogicalHeightUsing(style()->logicalHeight());
}
static inline int customContainingBlockWidth(const RenderView* view, const RenderBox* containingBlockBox)
{
return view->frameView()->customFixedPositionLayoutRect().width() - containingBlockBox->borderLeft() - containingBlockBox->borderRight() - containingBlockBox->verticalScrollbarWidth();
}
static inline int customContainingBlockHeight(const RenderView* view, const RenderBox* containingBlockBox)
{
return view->frameView()->customFixedPositionLayoutRect().height() - containingBlockBox->borderTop() - containingBlockBox->borderBottom() - containingBlockBox->horizontalScrollbarHeight();
}
static int customContainingBlockLogicalWidth(const RenderStyle* style, const RenderView* view, const RenderBox* containingBlockBox)
{
return style->isHorizontalWritingMode() ? customContainingBlockWidth(view, containingBlockBox) : customContainingBlockHeight(view, containingBlockBox);
}
static int customContainingBlockLogicalHeight(const RenderStyle* style, const RenderView* view, const RenderBox* containingBlockBox)
{
return style->isHorizontalWritingMode() ? customContainingBlockHeight(view, containingBlockBox) : customContainingBlockWidth(view, containingBlockBox);
}
static inline int customContainingBlockAvailableLogicalHeight(const RenderStyle* style, const RenderView* view)
{
return style->isHorizontalWritingMode() ? view->frameView()->customFixedPositionLayoutRect().height() : view->frameView()->customFixedPositionLayoutRect().width();
}
LayoutUnit RenderBox::availableLogicalHeightUsing(const Length& h) const
{
if (h.isFixed())
return computeContentBoxLogicalHeight(h.value());
if (isRenderView()) {
FrameView *frameView = toRenderView(this)->frameView();
if (document()->isPluginDocument() && frameView->useFixedLayout())
return frameView->fixedLayoutSize().height();
return isHorizontalWritingMode() ? frameView->visibleHeight() : frameView->visibleWidth();
}
if (isTableCell() && (h.isAuto() || h.isPercent()))
return overrideHeight() - borderAndPaddingLogicalWidth();
if (h.isPercent()) {
RenderBlock* containingBlock = this->containingBlock();
if (containingBlock->isRenderView()) {
RenderView* view = toRenderView(containingBlock);
if (view->hasCustomFixedPosition(this))
return computeContentBoxLogicalHeight(valueForLength(h, customContainingBlockAvailableLogicalHeight(containingBlock->style(), view)));
}
return computeContentBoxLogicalHeight(valueForLength(h, containingBlock->availableLogicalHeight()));
}
if (isRenderBlock() && isOutOfFlowPositioned() && style()->height().isAuto() && !(style()->top().isAuto() || style()->bottom().isAuto())) {
RenderBlock* block = const_cast<RenderBlock*>(toRenderBlock(this));
LayoutUnit oldHeight = block->logicalHeight();
block->computeLogicalHeight();
LayoutUnit newHeight = block->computeContentBoxLogicalHeight(block->contentLogicalHeight());
block->setLogicalHeight(oldHeight);
return computeContentBoxLogicalHeight(newHeight);
}
return containingBlock()->availableLogicalHeight();
}
void RenderBox::computeBlockDirectionMargins(const RenderBlock* containingBlock)
{
if (isTableCell()) {
setMarginBefore(0);
setMarginAfter(0);
return;
}
LayoutUnit cw = containingBlockLogicalWidthForContent();
RenderView* renderView = view();
RenderStyle* containingBlockStyle = containingBlock->style();
containingBlock->setMarginBeforeForChild(this, minimumValueForLength(style()->marginBeforeUsing(containingBlockStyle), cw, renderView));
containingBlock->setMarginAfterForChild(this, minimumValueForLength(style()->marginAfterUsing(containingBlockStyle), cw, renderView));
}
LayoutUnit RenderBox::containingBlockLogicalWidthForPositioned(const RenderBoxModelObject* containingBlock, RenderRegion* region,
LayoutUnit offsetFromLogicalTopOfFirstPage, bool checkForPerpendicularWritingMode) const
{
Frame* frame = view() ? view()->frame(): 0;
FrameView* frameView = view() ? view()->frameView() : 0;
if (fixedElementLaysOutRelativeToFrame(frame, frameView))
return (view()->isHorizontalWritingMode() ? frameView->visibleWidth() : frameView->visibleHeight()) / frame->frameScaleFactor();
if (checkForPerpendicularWritingMode && containingBlock->isHorizontalWritingMode() != isHorizontalWritingMode())
return containingBlockLogicalHeightForPositioned(containingBlock, false);
if (containingBlock->isBox()) {
RenderView* view = this->view();
if (view->hasCustomFixedPosition(this)) {
const RenderBox* containingBlockBox = toRenderBox(containingBlock);
return customContainingBlockLogicalWidth(containingBlockBox->style(), view, containingBlockBox);
}
const RenderBlock* cb = toRenderBlock(containingBlock);
LayoutUnit result = cb->clientLogicalWidth();
if (inRenderFlowThread()) {
RenderBoxRegionInfo* boxInfo = 0;
if (!region) {
if (containingBlock->isRenderFlowThread() && !checkForPerpendicularWritingMode)
return toRenderFlowThread(containingBlock)->contentLogicalWidthOfFirstRegion();
if (isWritingModeRoot()) {
LayoutUnit cbPageOffset = offsetFromLogicalTopOfFirstPage - logicalTop();
RenderRegion* cbRegion = cb->regionAtBlockOffset(cbPageOffset);
cbRegion = cb->clampToStartAndEndRegions(cbRegion);
boxInfo = cb->renderBoxRegionInfo(cbRegion, cbPageOffset);
}
} else if (region && enclosingRenderFlowThread()->isHorizontalWritingMode() == containingBlock->isHorizontalWritingMode()) {
RenderRegion* containingBlockRegion = cb->clampToStartAndEndRegions(region);
boxInfo = cb->renderBoxRegionInfo(containingBlockRegion, offsetFromLogicalTopOfFirstPage - logicalTop());
}
if (boxInfo)
return max<LayoutUnit>(0, result - (cb->logicalWidth() - boxInfo->logicalWidth()));
}
return result;
}
ASSERT(containingBlock->isRenderInline() && containingBlock->isInFlowPositioned());
const RenderInline* flow = toRenderInline(containingBlock);
InlineFlowBox* first = flow->firstLineBox();
InlineFlowBox* last = flow->lastLineBox();
if (!first || !last)
return 0;
LayoutUnit fromLeft;
LayoutUnit fromRight;
if (containingBlock->style()->isLeftToRightDirection()) {
fromLeft = first->logicalLeft() + first->borderLogicalLeft();
fromRight = last->logicalLeft() + last->logicalWidth() - last->borderLogicalRight();
} else {
fromRight = first->logicalLeft() + first->logicalWidth() - first->borderLogicalRight();
fromLeft = last->logicalLeft() + last->borderLogicalLeft();
}
return max<LayoutUnit>(0, fromRight - fromLeft);
}
LayoutUnit RenderBox::containingBlockLogicalHeightForPositioned(const RenderBoxModelObject* containingBlock, bool checkForPerpendicularWritingMode) const
{
Frame* frame = view() ? view()->frame(): 0;
FrameView* frameView = view() ? view()->frameView() : 0;
if (fixedElementLaysOutRelativeToFrame(frame, frameView))
return (view()->isHorizontalWritingMode() ? frameView->visibleHeight() : frameView->visibleWidth()) / frame->frameScaleFactor();
if (checkForPerpendicularWritingMode && containingBlock->isHorizontalWritingMode() != isHorizontalWritingMode())
return containingBlockLogicalWidthForPositioned(containingBlock, 0, 0, false);
if (containingBlock->isBox()) {
RenderView* view = this->view();
if (view->hasCustomFixedPosition(this)) {
const RenderBox* containingBlockBox = toRenderBox(containingBlock);
return customContainingBlockLogicalHeight(style(), view, containingBlockBox);
}
const RenderBlock* cb = toRenderBlock(containingBlock);
LayoutUnit result = cb->clientLogicalHeight();
if (inRenderFlowThread() && containingBlock->isRenderFlowThread() && enclosingRenderFlowThread()->isHorizontalWritingMode() == containingBlock->isHorizontalWritingMode())
return toRenderFlowThread(containingBlock)->contentLogicalHeightOfFirstRegion();
return result;
}
ASSERT(containingBlock->isRenderInline() && containingBlock->isInFlowPositioned());
const RenderInline* flow = toRenderInline(containingBlock);
InlineFlowBox* first = flow->firstLineBox();
InlineFlowBox* last = flow->lastLineBox();
if (!first || !last)
return 0;
LayoutUnit heightResult;
LayoutRect boundingBox = flow->linesBoundingBox();
if (containingBlock->isHorizontalWritingMode())
heightResult = boundingBox.height();
else
heightResult = boundingBox.width();
heightResult -= (containingBlock->borderBefore() + containingBlock->borderAfter());
return heightResult;
}
static void computeInlineStaticDistance(Length& logicalLeft, Length& logicalRight, const RenderBox* child, const RenderBoxModelObject* containerBlock, LayoutUnit containerLogicalWidth, RenderRegion* region)
{
if (!logicalLeft.isAuto() || !logicalRight.isAuto())
return;
if (child->parent()->style()->direction() == LTR) {
LayoutUnit staticPosition = child->layer()->staticInlinePosition() - containerBlock->borderLogicalLeft();
for (RenderObject* curr = child->parent(); curr && curr != containerBlock; curr = curr->container()) {
if (curr->isBox()) {
staticPosition += toRenderBox(curr)->logicalLeft();
if (region && curr->isRenderBlock()) {
const RenderBlock* cb = toRenderBlock(curr);
region = cb->clampToStartAndEndRegions(region);
RenderBoxRegionInfo* boxInfo = cb->renderBoxRegionInfo(region, region->offsetFromLogicalTopOfFirstPage());
if (boxInfo)
staticPosition += boxInfo->logicalLeft();
}
}
}
logicalLeft.setValue(Fixed, staticPosition);
} else {
RenderBox* enclosingBox = child->parent()->enclosingBox();
LayoutUnit staticPosition = child->layer()->staticInlinePosition() + containerLogicalWidth + containerBlock->borderLogicalLeft();
for (RenderObject* curr = enclosingBox; curr; curr = curr->container()) {
if (curr->isBox()) {
if (curr != containerBlock)
staticPosition -= toRenderBox(curr)->logicalLeft();
if (curr == enclosingBox)
staticPosition -= enclosingBox->logicalWidth();
if (region && curr->isRenderBlock()) {
const RenderBlock* cb = toRenderBlock(curr);
region = cb->clampToStartAndEndRegions(region);
RenderBoxRegionInfo* boxInfo = cb->renderBoxRegionInfo(region, region->offsetFromLogicalTopOfFirstPage());
if (boxInfo) {
if (curr != containerBlock)
staticPosition -= cb->logicalWidth() - (boxInfo->logicalLeft() + boxInfo->logicalWidth());
if (curr == enclosingBox)
staticPosition += enclosingBox->logicalWidth() - boxInfo->logicalWidth();
}
}
}
if (curr == containerBlock)
break;
}
logicalRight.setValue(Fixed, staticPosition);
}
}
void RenderBox::computePositionedLogicalWidth(RenderRegion* region, LayoutUnit offsetFromLogicalTopOfFirstPage)
{
if (isReplaced()) {
computePositionedLogicalWidthReplaced(); return;
}
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const LayoutUnit containerLogicalWidth = containingBlockLogicalWidthForPositioned(containerBlock, region, offsetFromLogicalTopOfFirstPage);
TextDirection containerDirection = containerBlock->style()->direction();
bool isHorizontal = isHorizontalWritingMode();
const LayoutUnit bordersPlusPadding = borderAndPaddingLogicalWidth();
const Length marginLogicalLeft = isHorizontal ? style()->marginLeft() : style()->marginTop();
const Length marginLogicalRight = isHorizontal ? style()->marginRight() : style()->marginBottom();
LayoutUnit& marginLogicalLeftAlias = isHorizontal ? m_marginLeft : m_marginTop;
LayoutUnit& marginLogicalRightAlias = isHorizontal ? m_marginRight : m_marginBottom;
Length logicalLeftLength = style()->logicalLeft();
Length logicalRightLength = style()->logicalRight();
computeInlineStaticDistance(logicalLeftLength, logicalRightLength, this, containerBlock, containerLogicalWidth, region);
LayoutUnit logicalWidthResult;
LayoutUnit logicalLeftResult;
computePositionedLogicalWidthUsing(style()->logicalWidth(), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeftLength, logicalRightLength, marginLogicalLeft, marginLogicalRight,
logicalWidthResult, marginLogicalLeftAlias, marginLogicalRightAlias, logicalLeftResult);
setLogicalWidth(logicalWidthResult);
setLogicalLeft(logicalLeftResult);
if (!style()->logicalMaxWidth().isUndefined()) {
LayoutUnit maxLogicalWidth;
LayoutUnit maxMarginLogicalLeft;
LayoutUnit maxMarginLogicalRight;
LayoutUnit maxLogicalLeftPos;
computePositionedLogicalWidthUsing(style()->logicalMaxWidth(), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeftLength, logicalRightLength, marginLogicalLeft, marginLogicalRight,
maxLogicalWidth, maxMarginLogicalLeft, maxMarginLogicalRight, maxLogicalLeftPos);
if (logicalWidth() > maxLogicalWidth) {
setLogicalWidth(maxLogicalWidth);
marginLogicalLeftAlias = maxMarginLogicalLeft;
marginLogicalRightAlias = maxMarginLogicalRight;
setLogicalLeft(maxLogicalLeftPos);
}
}
if (!style()->logicalMinWidth().isZero()) {
LayoutUnit minLogicalWidth;
LayoutUnit minMarginLogicalLeft;
LayoutUnit minMarginLogicalRight;
LayoutUnit minLogicalLeftPos;
computePositionedLogicalWidthUsing(style()->logicalMinWidth(), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeftLength, logicalRightLength, marginLogicalLeft, marginLogicalRight,
minLogicalWidth, minMarginLogicalLeft, minMarginLogicalRight, minLogicalLeftPos);
if (logicalWidth() < minLogicalWidth) {
setLogicalWidth(minLogicalWidth);
marginLogicalLeftAlias = minMarginLogicalLeft;
marginLogicalRightAlias = minMarginLogicalRight;
setLogicalLeft(minLogicalLeftPos);
}
}
if (stretchesToMinIntrinsicLogicalWidth() && logicalWidth() < minPreferredLogicalWidth() - bordersPlusPadding) {
computePositionedLogicalWidthUsing(Length(minPreferredLogicalWidth() - bordersPlusPadding, Fixed), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeftLength, logicalRightLength, marginLogicalLeft, marginLogicalRight,
logicalWidthResult, marginLogicalLeftAlias, marginLogicalRightAlias, logicalLeftResult);
setLogicalWidth(logicalWidthResult);
setLogicalLeft(logicalLeftResult);
}
setLogicalWidth(logicalWidth() + bordersPlusPadding);
if (inRenderFlowThread() && !region && isWritingModeRoot() && isHorizontalWritingMode() == containerBlock->isHorizontalWritingMode()) {
LayoutUnit logicalLeftPos = logicalLeft();
const RenderBlock* cb = toRenderBlock(containerBlock);
LayoutUnit cbPageOffset = offsetFromLogicalTopOfFirstPage - logicalTop();
RenderRegion* cbRegion = cb->regionAtBlockOffset(cbPageOffset);
cbRegion = cb->clampToStartAndEndRegions(cbRegion);
RenderBoxRegionInfo* boxInfo = cb->renderBoxRegionInfo(cbRegion, cbPageOffset);
if (boxInfo) {
logicalLeftPos += boxInfo->logicalLeft();
setLogicalLeft(logicalLeftPos);
}
}
}
static void computeLogicalLeftPositionedOffset(LayoutUnit& logicalLeftPos, const RenderBox* child, LayoutUnit logicalWidthValue, const RenderBoxModelObject* containerBlock, LayoutUnit containerLogicalWidth)
{
if (containerBlock->isHorizontalWritingMode() != child->isHorizontalWritingMode() && containerBlock->style()->isFlippedBlocksWritingMode()) {
logicalLeftPos = containerLogicalWidth - logicalWidthValue - logicalLeftPos;
logicalLeftPos += (child->isHorizontalWritingMode() ? containerBlock->borderRight() : containerBlock->borderBottom());
} else
logicalLeftPos += (child->isHorizontalWritingMode() ? containerBlock->borderLeft() : containerBlock->borderTop());
}
void RenderBox::computePositionedLogicalWidthUsing(Length logicalWidth, const RenderBoxModelObject* containerBlock, TextDirection containerDirection,
LayoutUnit containerLogicalWidth, LayoutUnit bordersPlusPadding,
Length logicalLeft, Length logicalRight, Length marginLogicalLeft, Length marginLogicalRight,
LayoutUnit& logicalWidthValue, LayoutUnit& marginLogicalLeftValue, LayoutUnit& marginLogicalRightValue, LayoutUnit& logicalLeftPos)
{
ASSERT(!(logicalLeft.isAuto() && logicalRight.isAuto()));
LayoutUnit logicalLeftValue = 0;
bool logicalWidthIsAuto = logicalWidth.isIntrinsicOrAuto();
bool logicalLeftIsAuto = logicalLeft.isAuto();
bool logicalRightIsAuto = logicalRight.isAuto();
RenderView* renderView = view();
if (!logicalLeftIsAuto && !logicalWidthIsAuto && !logicalRightIsAuto) {
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth, renderView);
logicalWidthValue = computeContentBoxLogicalWidth(valueForLength(logicalWidth, containerLogicalWidth, renderView));
const LayoutUnit availableSpace = containerLogicalWidth - (logicalLeftValue + logicalWidthValue + valueForLength(logicalRight, containerLogicalWidth, renderView) + bordersPlusPadding);
if (marginLogicalLeft.isAuto() && marginLogicalRight.isAuto()) {
if (availableSpace >= 0) {
marginLogicalLeftValue = availableSpace / 2; marginLogicalRightValue = availableSpace - marginLogicalLeftValue; } else {
if (containerDirection == LTR) {
marginLogicalLeftValue = 0;
marginLogicalRightValue = availableSpace; } else {
marginLogicalLeftValue = availableSpace; marginLogicalRightValue = 0;
}
}
} else if (marginLogicalLeft.isAuto()) {
marginLogicalRightValue = valueForLength(marginLogicalRight, containerLogicalWidth, renderView);
marginLogicalLeftValue = availableSpace - marginLogicalRightValue;
} else if (marginLogicalRight.isAuto()) {
marginLogicalLeftValue = valueForLength(marginLogicalLeft, containerLogicalWidth, renderView);
marginLogicalRightValue = availableSpace - marginLogicalLeftValue;
} else {
marginLogicalLeftValue = valueForLength(marginLogicalLeft, containerLogicalWidth, renderView);
marginLogicalRightValue = valueForLength(marginLogicalRight, containerLogicalWidth, renderView);
if (containerDirection == RTL)
logicalLeftValue = (availableSpace + logicalLeftValue) - marginLogicalLeftValue - marginLogicalRightValue;
}
} else {
marginLogicalLeftValue = minimumValueForLength(marginLogicalLeft, containerLogicalWidth, renderView);
marginLogicalRightValue = minimumValueForLength(marginLogicalRight, containerLogicalWidth, renderView);
const LayoutUnit availableSpace = containerLogicalWidth - (marginLogicalLeftValue + marginLogicalRightValue + bordersPlusPadding);
if (logicalLeftIsAuto && logicalWidthIsAuto && !logicalRightIsAuto) {
LayoutUnit logicalRightValue = valueForLength(logicalRight, containerLogicalWidth, renderView);
LayoutUnit preferredWidth = maxPreferredLogicalWidth() - bordersPlusPadding;
LayoutUnit preferredMinWidth = minPreferredLogicalWidth() - bordersPlusPadding;
LayoutUnit availableWidth = availableSpace - logicalRightValue;
logicalWidthValue = min(max(preferredMinWidth, availableWidth), preferredWidth);
logicalLeftValue = availableSpace - (logicalWidthValue + logicalRightValue);
} else if (!logicalLeftIsAuto && logicalWidthIsAuto && logicalRightIsAuto) {
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth, renderView);
LayoutUnit preferredWidth = maxPreferredLogicalWidth() - bordersPlusPadding;
LayoutUnit preferredMinWidth = minPreferredLogicalWidth() - bordersPlusPadding;
LayoutUnit availableWidth = availableSpace - logicalLeftValue;
logicalWidthValue = min(max(preferredMinWidth, availableWidth), preferredWidth);
} else if (logicalLeftIsAuto && !logicalWidthIsAuto && !logicalRightIsAuto) {
logicalWidthValue = computeContentBoxLogicalWidth(valueForLength(logicalWidth, containerLogicalWidth, renderView));
logicalLeftValue = availableSpace - (logicalWidthValue + valueForLength(logicalRight, containerLogicalWidth, renderView));
} else if (!logicalLeftIsAuto && logicalWidthIsAuto && !logicalRightIsAuto) {
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth, renderView);
logicalWidthValue = availableSpace - (logicalLeftValue + valueForLength(logicalRight, containerLogicalWidth, renderView));
} else if (!logicalLeftIsAuto && !logicalWidthIsAuto && logicalRightIsAuto) {
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth, renderView);
logicalWidthValue = computeContentBoxLogicalWidth(valueForLength(logicalWidth, containerLogicalWidth, renderView));
}
}
if (containerBlock->isRenderInline() && !containerBlock->style()->isLeftToRightDirection()) {
const RenderInline* flow = toRenderInline(containerBlock);
InlineFlowBox* firstLine = flow->firstLineBox();
InlineFlowBox* lastLine = flow->lastLineBox();
if (firstLine && lastLine && firstLine != lastLine) {
logicalLeftPos = logicalLeftValue + marginLogicalLeftValue + lastLine->borderLogicalLeft() + (lastLine->logicalLeft() - firstLine->logicalLeft());
return;
}
}
logicalLeftPos = logicalLeftValue + marginLogicalLeftValue;
computeLogicalLeftPositionedOffset(logicalLeftPos, this, logicalWidthValue, containerBlock, containerLogicalWidth);
}
static void computeBlockStaticDistance(Length& logicalTop, Length& logicalBottom, const RenderBox* child, const RenderBoxModelObject* containerBlock)
{
if (!logicalTop.isAuto() || !logicalBottom.isAuto())
return;
LayoutUnit staticLogicalTop = child->layer()->staticBlockPosition() - containerBlock->borderBefore();
for (RenderObject* curr = child->parent(); curr && curr != containerBlock; curr = curr->container()) {
if (curr->isBox() && !curr->isTableRow())
staticLogicalTop += toRenderBox(curr)->logicalTop();
}
logicalTop.setValue(Fixed, staticLogicalTop);
}
void RenderBox::computePositionedLogicalHeight()
{
if (isReplaced()) {
computePositionedLogicalHeightReplaced();
return;
}
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const LayoutUnit containerLogicalHeight = containingBlockLogicalHeightForPositioned(containerBlock);
bool isHorizontal = isHorizontalWritingMode();
RenderStyle* styleToUse = style();
bool isFlipped = styleToUse->isFlippedBlocksWritingMode();
const LayoutUnit bordersPlusPadding = borderAndPaddingLogicalHeight();
const Length marginBefore = styleToUse->marginBefore();
const Length marginAfter = styleToUse->marginAfter();
LayoutUnit& marginBeforeAlias = isHorizontal ? (isFlipped ? m_marginBottom : m_marginTop) : (isFlipped ? m_marginRight: m_marginLeft);
LayoutUnit& marginAfterAlias = isHorizontal ? (isFlipped ? m_marginTop : m_marginBottom) : (isFlipped ? m_marginLeft: m_marginRight);
Length logicalTopLength = styleToUse->logicalTop();
Length logicalBottomLength = styleToUse->logicalBottom();
computeBlockStaticDistance(logicalTopLength, logicalBottomLength, this, containerBlock);
LayoutUnit logicalHeightResult; LayoutUnit logicalTopPos;
computePositionedLogicalHeightUsing(styleToUse->logicalHeight(), containerBlock, containerLogicalHeight, bordersPlusPadding,
logicalTopLength, logicalBottomLength, marginBefore, marginAfter,
logicalHeightResult, marginBeforeAlias, marginAfterAlias, logicalTopPos);
setLogicalTop(logicalTopPos);
if (!styleToUse->logicalMaxHeight().isUndefined()) {
LayoutUnit maxLogicalHeight;
LayoutUnit maxMarginBefore;
LayoutUnit maxMarginAfter;
LayoutUnit maxLogicalTopPos;
computePositionedLogicalHeightUsing(styleToUse->logicalMaxHeight(), containerBlock, containerLogicalHeight, bordersPlusPadding,
logicalTopLength, logicalBottomLength, marginBefore, marginAfter,
maxLogicalHeight, maxMarginBefore, maxMarginAfter, maxLogicalTopPos);
if (logicalHeightResult > maxLogicalHeight) {
logicalHeightResult = maxLogicalHeight;
marginBeforeAlias = maxMarginBefore;
marginAfterAlias = maxMarginAfter;
setLogicalTop(maxLogicalTopPos);
}
}
if (!styleToUse->logicalMinHeight().isZero()) {
LayoutUnit minLogicalHeight;
LayoutUnit minMarginBefore;
LayoutUnit minMarginAfter;
LayoutUnit minLogicalTopPos;
computePositionedLogicalHeightUsing(styleToUse->logicalMinHeight(), containerBlock, containerLogicalHeight, bordersPlusPadding,
logicalTopLength, logicalBottomLength, marginBefore, marginAfter,
minLogicalHeight, minMarginBefore, minMarginAfter, minLogicalTopPos);
if (logicalHeightResult < minLogicalHeight) {
logicalHeightResult = minLogicalHeight;
marginBeforeAlias = minMarginBefore;
marginAfterAlias = minMarginAfter;
setLogicalTop(minLogicalTopPos);
}
}
setLogicalHeight(logicalHeightResult + bordersPlusPadding);
if (inRenderFlowThread() && isHorizontalWritingMode() != containerBlock->isHorizontalWritingMode()) {
LayoutUnit logicalTopPos = logicalTop();
const RenderBlock* cb = toRenderBlock(containerBlock);
LayoutUnit cbPageOffset = cb->offsetFromLogicalTopOfFirstPage() - logicalLeft();
RenderRegion* cbRegion = cb->regionAtBlockOffset(cbPageOffset);
cbRegion = cb->clampToStartAndEndRegions(cbRegion);
RenderBoxRegionInfo* boxInfo = cb->renderBoxRegionInfo(cbRegion, cbPageOffset);
if (boxInfo) {
logicalTopPos += boxInfo->logicalLeft();
setLogicalTop(logicalTopPos);
}
}
}
static void computeLogicalTopPositionedOffset(LayoutUnit& logicalTopPos, const RenderBox* child, LayoutUnit logicalHeightValue, const RenderBoxModelObject* containerBlock, LayoutUnit containerLogicalHeight)
{
if ((child->style()->isFlippedBlocksWritingMode() && child->isHorizontalWritingMode() != containerBlock->isHorizontalWritingMode())
|| (child->style()->isFlippedBlocksWritingMode() != containerBlock->style()->isFlippedBlocksWritingMode() && child->isHorizontalWritingMode() == containerBlock->isHorizontalWritingMode()))
logicalTopPos = containerLogicalHeight - logicalHeightValue - logicalTopPos;
if (containerBlock->style()->isFlippedBlocksWritingMode() && child->isHorizontalWritingMode() == containerBlock->isHorizontalWritingMode()) {
if (child->isHorizontalWritingMode())
logicalTopPos += containerBlock->borderBottom();
else
logicalTopPos += containerBlock->borderRight();
} else {
if (child->isHorizontalWritingMode())
logicalTopPos += containerBlock->borderTop();
else
logicalTopPos += containerBlock->borderLeft();
}
}
void RenderBox::computePositionedLogicalHeightUsing(Length logicalHeightLength, const RenderBoxModelObject* containerBlock,
LayoutUnit containerLogicalHeight, LayoutUnit bordersPlusPadding,
Length logicalTop, Length logicalBottom, Length marginBefore, Length marginAfter,
LayoutUnit& logicalHeightValue, LayoutUnit& marginBeforeValue, LayoutUnit& marginAfterValue, LayoutUnit& logicalTopPos)
{
ASSERT(!(logicalTop.isAuto() && logicalBottom.isAuto()));
LayoutUnit contentLogicalHeight = logicalHeight() - bordersPlusPadding;
LayoutUnit logicalTopValue = 0;
bool logicalHeightIsAuto = logicalHeightLength.isAuto();
bool logicalTopIsAuto = logicalTop.isAuto();
bool logicalBottomIsAuto = logicalBottom.isAuto();
RenderView* renderView = view();
if (isTable()) {
logicalHeightLength.setValue(Fixed, contentLogicalHeight);
logicalHeightIsAuto = false;
}
if (!logicalTopIsAuto && !logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalHeightValue = computeContentBoxLogicalHeight(valueForLength(logicalHeightLength, containerLogicalHeight, renderView));
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight, renderView);
const LayoutUnit availableSpace = containerLogicalHeight - (logicalTopValue + logicalHeightValue + valueForLength(logicalBottom, containerLogicalHeight, renderView) + bordersPlusPadding);
if (marginBefore.isAuto() && marginAfter.isAuto()) {
marginBeforeValue = availableSpace / 2; marginAfterValue = availableSpace - marginBeforeValue; } else if (marginBefore.isAuto()) {
marginAfterValue = valueForLength(marginAfter, containerLogicalHeight, renderView);
marginBeforeValue = availableSpace - marginAfterValue;
} else if (marginAfter.isAuto()) {
marginBeforeValue = valueForLength(marginBefore, containerLogicalHeight, renderView);
marginAfterValue = availableSpace - marginBeforeValue;
} else {
marginBeforeValue = valueForLength(marginBefore, containerLogicalHeight, renderView);
marginAfterValue = valueForLength(marginAfter, containerLogicalHeight, renderView);
}
} else {
marginBeforeValue = minimumValueForLength(marginBefore, containerLogicalHeight, renderView);
marginAfterValue = minimumValueForLength(marginAfter, containerLogicalHeight, renderView);
const LayoutUnit availableSpace = containerLogicalHeight - (marginBeforeValue + marginAfterValue + bordersPlusPadding);
if (logicalTopIsAuto && logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalHeightValue = contentLogicalHeight;
logicalTopValue = availableSpace - (logicalHeightValue + valueForLength(logicalBottom, containerLogicalHeight, renderView));
} else if (!logicalTopIsAuto && logicalHeightIsAuto && logicalBottomIsAuto) {
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight, renderView);
logicalHeightValue = contentLogicalHeight;
} else if (logicalTopIsAuto && !logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalHeightValue = computeContentBoxLogicalHeight(valueForLength(logicalHeightLength, containerLogicalHeight, renderView));
logicalTopValue = availableSpace - (logicalHeightValue + valueForLength(logicalBottom, containerLogicalHeight, renderView));
} else if (!logicalTopIsAuto && logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight, renderView);
logicalHeightValue = max<LayoutUnit>(0, availableSpace - (logicalTopValue + valueForLength(logicalBottom, containerLogicalHeight, renderView)));
} else if (!logicalTopIsAuto && !logicalHeightIsAuto && logicalBottomIsAuto) {
logicalHeightValue = computeContentBoxLogicalHeight(valueForLength(logicalHeightLength, containerLogicalHeight, renderView));
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight, renderView);
}
}
logicalTopPos = logicalTopValue + marginBeforeValue;
computeLogicalTopPositionedOffset(logicalTopPos, this, logicalHeightValue, containerBlock, containerLogicalHeight);
}
void RenderBox::computePositionedLogicalWidthReplaced()
{
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const LayoutUnit containerLogicalWidth = containingBlockLogicalWidthForPositioned(containerBlock);
TextDirection containerDirection = containerBlock->style()->direction();
bool isHorizontal = isHorizontalWritingMode();
Length logicalLeft = style()->logicalLeft();
Length logicalRight = style()->logicalRight();
Length marginLogicalLeft = isHorizontal ? style()->marginLeft() : style()->marginTop();
Length marginLogicalRight = isHorizontal ? style()->marginRight() : style()->marginBottom();
LayoutUnit& marginLogicalLeftAlias = isHorizontal ? m_marginLeft : m_marginTop;
LayoutUnit& marginLogicalRightAlias = isHorizontal ? m_marginRight : m_marginBottom;
setLogicalWidth(computeReplacedLogicalWidth() + borderAndPaddingLogicalWidth());
const LayoutUnit availableSpace = containerLogicalWidth - logicalWidth();
computeInlineStaticDistance(logicalLeft, logicalRight, this, containerBlock, containerLogicalWidth, 0);
if (logicalLeft.isAuto() || logicalRight.isAuto()) {
if (marginLogicalLeft.isAuto())
marginLogicalLeft.setValue(Fixed, 0);
if (marginLogicalRight.isAuto())
marginLogicalRight.setValue(Fixed, 0);
}
LayoutUnit logicalLeftValue = 0;
LayoutUnit logicalRightValue = 0;
RenderView* renderView = view();
if (marginLogicalLeft.isAuto() && marginLogicalRight.isAuto()) {
ASSERT(!(logicalLeft.isAuto() && logicalRight.isAuto()));
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth, renderView);
logicalRightValue = valueForLength(logicalRight, containerLogicalWidth, renderView);
LayoutUnit difference = availableSpace - (logicalLeftValue + logicalRightValue);
if (difference > 0) {
marginLogicalLeftAlias = difference / 2; marginLogicalRightAlias = difference - marginLogicalLeftAlias; } else {
if (containerDirection == LTR) {
marginLogicalLeftAlias = 0;
marginLogicalRightAlias = difference; } else {
marginLogicalLeftAlias = difference; marginLogicalRightAlias = 0;
}
}
} else if (logicalLeft.isAuto()) {
marginLogicalLeftAlias = valueForLength(marginLogicalLeft, containerLogicalWidth, renderView);
marginLogicalRightAlias = valueForLength(marginLogicalRight, containerLogicalWidth, renderView);
logicalRightValue = valueForLength(logicalRight, containerLogicalWidth, renderView);
logicalLeftValue = availableSpace - (logicalRightValue + marginLogicalLeftAlias + marginLogicalRightAlias);
} else if (logicalRight.isAuto()) {
marginLogicalLeftAlias = valueForLength(marginLogicalLeft, containerLogicalWidth, renderView);
marginLogicalRightAlias = valueForLength(marginLogicalRight, containerLogicalWidth, renderView);
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth, renderView);
logicalRightValue = availableSpace - (logicalLeftValue + marginLogicalLeftAlias + marginLogicalRightAlias);
} else if (marginLogicalLeft.isAuto()) {
marginLogicalRightAlias = valueForLength(marginLogicalRight, containerLogicalWidth, renderView);
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth, renderView);
logicalRightValue = valueForLength(logicalRight, containerLogicalWidth, renderView);
marginLogicalLeftAlias = availableSpace - (logicalLeftValue + logicalRightValue + marginLogicalRightAlias);
} else if (marginLogicalRight.isAuto()) {
marginLogicalLeftAlias = valueForLength(marginLogicalLeft, containerLogicalWidth, renderView);
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth, renderView);
logicalRightValue = valueForLength(logicalRight, containerLogicalWidth, renderView);
marginLogicalRightAlias = availableSpace - (logicalLeftValue + logicalRightValue + marginLogicalLeftAlias);
} else {
marginLogicalLeftAlias = valueForLength(marginLogicalLeft, containerLogicalWidth, renderView);
marginLogicalRightAlias = valueForLength(marginLogicalRight, containerLogicalWidth, renderView);
logicalRightValue = valueForLength(logicalRight, containerLogicalWidth, renderView);
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth, renderView);
if (containerDirection == RTL) {
int totalLogicalWidth = logicalWidth() + logicalLeftValue + logicalRightValue + marginLogicalLeftAlias + marginLogicalRightAlias;
logicalLeftValue = containerLogicalWidth - (totalLogicalWidth - logicalLeftValue);
}
}
if (containerBlock->isRenderInline() && !containerBlock->style()->isLeftToRightDirection()) {
const RenderInline* flow = toRenderInline(containerBlock);
InlineFlowBox* firstLine = flow->firstLineBox();
InlineFlowBox* lastLine = flow->lastLineBox();
if (firstLine && lastLine && firstLine != lastLine) {
setLogicalLeft(logicalLeftValue + marginLogicalLeftAlias + lastLine->borderLogicalLeft() + (lastLine->logicalLeft() - firstLine->logicalLeft()));
return;
}
}
LayoutUnit logicalLeftPos = logicalLeftValue + marginLogicalLeftAlias;
computeLogicalLeftPositionedOffset(logicalLeftPos, this, logicalWidth(), containerBlock, containerLogicalWidth);
setLogicalLeft(logicalLeftPos);
}
void RenderBox::computePositionedLogicalHeightReplaced()
{
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const LayoutUnit containerLogicalHeight = containingBlockLogicalHeightForPositioned(containerBlock);
bool isHorizontal = isHorizontalWritingMode();
bool isFlipped = style()->isFlippedBlocksWritingMode();
Length marginBefore = style()->marginBefore();
Length marginAfter = style()->marginAfter();
LayoutUnit& marginBeforeAlias = isHorizontal ? (isFlipped ? m_marginBottom : m_marginTop) : (isFlipped ? m_marginRight: m_marginLeft);
LayoutUnit& marginAfterAlias = isHorizontal ? (isFlipped ? m_marginTop : m_marginBottom) : (isFlipped ? m_marginLeft: m_marginRight);
Length logicalTop = style()->logicalTop();
Length logicalBottom = style()->logicalBottom();
RenderView* renderView = view();
setLogicalHeight(computeReplacedLogicalHeight() + borderAndPaddingLogicalHeight());
const LayoutUnit availableSpace = containerLogicalHeight - logicalHeight();
computeBlockStaticDistance(logicalTop, logicalBottom, this, containerBlock);
if (logicalTop.isAuto() || logicalBottom.isAuto()) {
if (marginBefore.isAuto())
marginBefore.setValue(Fixed, 0);
if (marginAfter.isAuto())
marginAfter.setValue(Fixed, 0);
}
LayoutUnit logicalTopValue = 0;
LayoutUnit logicalBottomValue = 0;
if (marginBefore.isAuto() && marginAfter.isAuto()) {
ASSERT(!(logicalTop.isAuto() || logicalBottom.isAuto()));
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight, renderView);
logicalBottomValue = valueForLength(logicalBottom, containerLogicalHeight, renderView);
LayoutUnit difference = availableSpace - (logicalTopValue + logicalBottomValue);
marginBeforeAlias = difference / 2; marginAfterAlias = difference - marginBeforeAlias;
} else if (logicalTop.isAuto()) {
marginBeforeAlias = valueForLength(marginBefore, containerLogicalHeight, renderView);
marginAfterAlias = valueForLength(marginAfter, containerLogicalHeight, renderView);
logicalBottomValue = valueForLength(logicalBottom, containerLogicalHeight, renderView);
logicalTopValue = availableSpace - (logicalBottomValue + marginBeforeAlias + marginAfterAlias);
} else if (logicalBottom.isAuto()) {
marginBeforeAlias = valueForLength(marginBefore, containerLogicalHeight, renderView);
marginAfterAlias = valueForLength(marginAfter, containerLogicalHeight, renderView);
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight, renderView);
} else if (marginBefore.isAuto()) {
marginAfterAlias = valueForLength(marginAfter, containerLogicalHeight, renderView);
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight, renderView);
logicalBottomValue = valueForLength(logicalBottom, containerLogicalHeight, renderView);
marginBeforeAlias = availableSpace - (logicalTopValue + logicalBottomValue + marginAfterAlias);
} else if (marginAfter.isAuto()) {
marginBeforeAlias = valueForLength(marginBefore, containerLogicalHeight, renderView);
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight, renderView);
logicalBottomValue = valueForLength(logicalBottom, containerLogicalHeight, renderView);
marginAfterAlias = availableSpace - (logicalTopValue + logicalBottomValue + marginBeforeAlias);
} else {
marginBeforeAlias = valueForLength(marginBefore, containerLogicalHeight, renderView);
marginAfterAlias = valueForLength(marginAfter, containerLogicalHeight, renderView);
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight, renderView);
}
LayoutUnit logicalTopPos = logicalTopValue + marginBeforeAlias;
computeLogicalTopPositionedOffset(logicalTopPos, this, logicalHeight(), containerBlock, containerLogicalHeight);
setLogicalTop(logicalTopPos);
}
LayoutRect RenderBox::localCaretRect(InlineBox* box, int caretOffset, LayoutUnit* extraWidthToEndOfLine)
{
LayoutRect rect(x(), y(), caretWidth, height());
bool ltr = box ? box->isLeftToRightDirection() : style()->isLeftToRightDirection();
if ((!caretOffset) ^ ltr)
rect.move(LayoutSize(width() - caretWidth, 0));
if (box) {
RootInlineBox* rootBox = box->root();
LayoutUnit top = rootBox->lineTop();
rect.setY(top);
rect.setHeight(rootBox->lineBottom() - top);
}
LayoutUnit fontHeight = style()->fontMetrics().height();
if (fontHeight > rect.height() || (!isReplaced() && !isTable()))
rect.setHeight(fontHeight);
if (extraWidthToEndOfLine)
*extraWidthToEndOfLine = x() + width() - rect.maxX();
rect.moveBy(-location());
return rect;
}
VisiblePosition RenderBox::positionForPoint(const LayoutPoint& point)
{
if (!firstChild())
return createVisiblePosition(node() ? firstPositionInOrBeforeNode(node()) : Position());
if (isTable() && node()) {
LayoutUnit right = contentWidth() + borderAndPaddingWidth();
LayoutUnit bottom = contentHeight() + borderAndPaddingHeight();
if (point.x() < 0 || point.x() > right || point.y() < 0 || point.y() > bottom) {
if (point.x() <= right / 2)
return createVisiblePosition(firstPositionInOrBeforeNode(node()));
return createVisiblePosition(lastPositionInOrAfterNode(node()));
}
}
LayoutUnit minDist = MAX_LAYOUT_UNIT;
RenderBox* closestRenderer = 0;
LayoutPoint adjustedPoint = point;
if (isTableRow())
adjustedPoint.moveBy(location());
for (RenderObject* renderObject = firstChild(); renderObject; renderObject = renderObject->nextSibling()) {
if ((!renderObject->firstChild() && !renderObject->isInline() && !renderObject->isBlockFlow() )
|| renderObject->style()->visibility() != VISIBLE)
continue;
if (!renderObject->isBox())
continue;
RenderBox* renderer = toRenderBox(renderObject);
LayoutUnit top = renderer->borderTop() + renderer->paddingTop() + (isTableRow() ? ZERO_LAYOUT_UNIT : renderer->y());
LayoutUnit bottom = top + renderer->contentHeight();
LayoutUnit left = renderer->borderLeft() + renderer->paddingLeft() + (isTableRow() ? ZERO_LAYOUT_UNIT : renderer->x());
LayoutUnit right = left + renderer->contentWidth();
if (point.x() <= right && point.x() >= left && point.y() <= top && point.y() >= bottom) {
if (renderer->isTableRow())
return renderer->positionForPoint(point + adjustedPoint - renderer->locationOffset());
return renderer->positionForPoint(point - renderer->locationOffset());
}
LayoutPoint cmp;
if (point.x() > right) {
if (point.y() < top)
cmp = LayoutPoint(right, top);
else if (point.y() > bottom)
cmp = LayoutPoint(right, bottom);
else
cmp = LayoutPoint(right, point.y());
} else if (point.x() < left) {
if (point.y() < top)
cmp = LayoutPoint(left, top);
else if (point.y() > bottom)
cmp = LayoutPoint(left, bottom);
else
cmp = LayoutPoint(left, point.y());
} else {
if (point.y() < top)
cmp = LayoutPoint(point.x(), top);
else
cmp = LayoutPoint(point.x(), bottom);
}
LayoutSize difference = cmp - point;
LayoutUnit dist = difference.width() * difference.width() + difference.height() * difference.height();
if (dist < minDist) {
closestRenderer = renderer;
minDist = dist;
}
}
if (closestRenderer)
return closestRenderer->positionForPoint(adjustedPoint - closestRenderer->locationOffset());
return createVisiblePosition(firstPositionInOrBeforeNode(node()));
}
bool RenderBox::shrinkToAvoidFloats() const
{
if ((isInline() && !isHTMLMarquee()) || !avoidsFloats() || isFloating())
return false;
return style()->width().isAuto();
}
bool RenderBox::avoidsFloats() const
{
return isReplaced() || hasOverflowClip() || isHR() || isLegend() || isWritingModeRoot() || isDeprecatedFlexItem();
}
void RenderBox::addVisualEffectOverflow()
{
if (!style()->boxShadow() && !style()->hasBorderImageOutsets())
return;
bool isFlipped = style()->isFlippedBlocksWritingMode();
bool isHorizontal = isHorizontalWritingMode();
LayoutRect borderBox = borderBoxRect();
LayoutUnit overflowMinX = borderBox.x();
LayoutUnit overflowMaxX = borderBox.maxX();
LayoutUnit overflowMinY = borderBox.y();
LayoutUnit overflowMaxY = borderBox.maxY();
if (style()->boxShadow()) {
LayoutUnit shadowLeft;
LayoutUnit shadowRight;
LayoutUnit shadowTop;
LayoutUnit shadowBottom;
style()->getBoxShadowExtent(shadowTop, shadowRight, shadowBottom, shadowLeft);
overflowMinX = borderBox.x() + ((!isFlipped || isHorizontal) ? shadowLeft : -shadowRight);
overflowMaxX = borderBox.maxX() + ((!isFlipped || isHorizontal) ? shadowRight : -shadowLeft);
overflowMinY = borderBox.y() + ((!isFlipped || !isHorizontal) ? shadowTop : -shadowBottom);
overflowMaxY = borderBox.maxY() + ((!isFlipped || !isHorizontal) ? shadowBottom : -shadowTop);
}
if (style()->hasBorderImageOutsets()) {
LayoutUnit borderOutsetLeft;
LayoutUnit borderOutsetRight;
LayoutUnit borderOutsetTop;
LayoutUnit borderOutsetBottom;
style()->getBorderImageOutsets(borderOutsetTop, borderOutsetRight, borderOutsetBottom, borderOutsetLeft);
overflowMinX = min(overflowMinX, borderBox.x() - ((!isFlipped || isHorizontal) ? borderOutsetLeft : borderOutsetRight));
overflowMaxX = max(overflowMaxX, borderBox.maxX() + ((!isFlipped || isHorizontal) ? borderOutsetRight : borderOutsetLeft));
overflowMinY = min(overflowMinY, borderBox.y() - ((!isFlipped || !isHorizontal) ? borderOutsetTop : borderOutsetBottom));
overflowMaxY = max(overflowMaxY, borderBox.maxY() + ((!isFlipped || !isHorizontal) ? borderOutsetBottom : borderOutsetTop));
}
addVisualOverflow(LayoutRect(overflowMinX, overflowMinY, overflowMaxX - overflowMinX, overflowMaxY - overflowMinY));
}
void RenderBox::addOverflowFromChild(RenderBox* child, const LayoutSize& delta)
{
LayoutRect childLayoutOverflowRect = child->layoutOverflowRectForPropagation(style());
childLayoutOverflowRect.move(delta);
addLayoutOverflow(childLayoutOverflowRect);
if (child->hasSelfPaintingLayer() || hasOverflowClip())
return;
LayoutRect childVisualOverflowRect = child->visualOverflowRectForPropagation(style());
childVisualOverflowRect.move(delta);
addVisualOverflow(childVisualOverflowRect);
}
void RenderBox::addLayoutOverflow(const LayoutRect& rect)
{
LayoutRect clientBox = clientBoxRect();
if (clientBox.contains(rect) || rect.isEmpty())
return;
LayoutRect overflowRect(rect);
if (hasOverflowClip() || isRenderView()) {
bool hasTopOverflow = !style()->isLeftToRightDirection() && !isHorizontalWritingMode();
bool hasLeftOverflow = !style()->isLeftToRightDirection() && isHorizontalWritingMode();
if (isFlexibleBox() && style()->isReverseFlexDirection()) {
RenderFlexibleBox* flexibleBox = static_cast<RenderFlexibleBox*>(this);
if (flexibleBox->isHorizontalFlow())
hasLeftOverflow = true;
else
hasTopOverflow = true;
}
if (hasColumns() && style()->columnProgression() == ReverseColumnProgression) {
if (isHorizontalWritingMode() ^ !style()->hasInlineColumnAxis())
hasLeftOverflow = !hasLeftOverflow;
else
hasTopOverflow = !hasTopOverflow;
}
if (!hasTopOverflow)
overflowRect.shiftYEdgeTo(max(overflowRect.y(), clientBox.y()));
else
overflowRect.shiftMaxYEdgeTo(min(overflowRect.maxY(), clientBox.maxY()));
if (!hasLeftOverflow)
overflowRect.shiftXEdgeTo(max(overflowRect.x(), clientBox.x()));
else
overflowRect.shiftMaxXEdgeTo(min(overflowRect.maxX(), clientBox.maxX()));
if (clientBox.contains(overflowRect) || overflowRect.isEmpty())
return;
}
if (!m_overflow)
m_overflow = adoptPtr(new RenderOverflow(clientBox, borderBoxRect()));
m_overflow->addLayoutOverflow(overflowRect);
}
void RenderBox::addVisualOverflow(const LayoutRect& rect)
{
LayoutRect borderBox = borderBoxRect();
if (borderBox.contains(rect) || rect.isEmpty())
return;
if (!m_overflow)
m_overflow = adoptPtr(new RenderOverflow(clientBoxRect(), borderBox));
m_overflow->addVisualOverflow(rect);
}
void RenderBox::clearLayoutOverflow()
{
if (!m_overflow)
return;
if (visualOverflowRect() == borderBoxRect()) {
m_overflow.clear();
return;
}
m_overflow->resetLayoutOverflow(borderBoxRect());
}
static bool percentageLogicalHeightIsResolvable(const RenderBox* box)
{
const RenderBlock* cb = box->containingBlock();
while (!cb->isRenderView() && !cb->isBody() && !cb->isTableCell() && !cb->isOutOfFlowPositioned() && cb->style()->logicalHeight().isAuto()) {
if (!box->document()->inQuirksMode() && !cb->isAnonymousBlock())
break;
cb = cb->containingBlock();
}
bool isOutOfFlowPositionedWithSpecifiedHeight = cb->isOutOfFlowPositioned() && (!cb->style()->logicalHeight().isAuto() || (!cb->style()->top().isAuto() && !cb->style()->bottom().isAuto()));
if (cb->isTableCell())
return true;
if (cb->style()->logicalHeight().isFixed())
return true;
if (cb->style()->logicalHeight().isPercent() && !isOutOfFlowPositionedWithSpecifiedHeight)
return percentageLogicalHeightIsResolvable(cb);
if (cb->isRenderView() || (cb->isBody() && box->document()->inQuirksMode()) || isOutOfFlowPositionedWithSpecifiedHeight)
return true;
if (cb->isRoot() && box->isOutOfFlowPositioned()) {
return true;
}
return false;
}
bool RenderBox::hasUnsplittableScrollingOverflow() const
{
bool isHorizontal = isHorizontalWritingMode();
if ((isHorizontal && !scrollsOverflowY()) || (!isHorizontal && !scrollsOverflowX()))
return false;
return !style()->logicalHeight().isIntrinsicOrAuto()
|| (!style()->logicalMaxHeight().isIntrinsicOrAuto() && !style()->logicalMaxHeight().isUndefined() && (!style()->logicalMaxHeight().isPercent() || percentageLogicalHeightIsResolvable(this)))
|| (!style()->logicalMinHeight().isIntrinsicOrAuto() && style()->logicalMinHeight().isPositive() && (!style()->logicalMinHeight().isPercent() || percentageLogicalHeightIsResolvable(this)));
}
bool RenderBox::isUnsplittableForPagination() const
{
return isReplaced() || hasUnsplittableScrollingOverflow() || (parent() && isWritingModeRoot());
}
LayoutUnit RenderBox::lineHeight(bool , LineDirectionMode direction, LinePositionMode ) const
{
if (isReplaced())
return direction == HorizontalLine ? m_marginTop + height() + m_marginBottom : m_marginRight + width() + m_marginLeft;
return 0;
}
LayoutUnit RenderBox::baselinePosition(FontBaseline baselineType, bool , LineDirectionMode direction, LinePositionMode ) const
{
if (isReplaced()) {
LayoutUnit result = direction == HorizontalLine ? m_marginTop + height() + m_marginBottom : m_marginRight + width() + m_marginLeft;
if (baselineType == AlphabeticBaseline)
return result;
return result - result / 2;
}
return 0;
}
RenderLayer* RenderBox::enclosingFloatPaintingLayer() const
{
const RenderObject* curr = this;
while (curr) {
RenderLayer* layer = curr->hasLayer() && curr->isBox() ? toRenderBoxModelObject(curr)->layer() : 0;
if (layer && layer->isSelfPaintingLayer())
return layer;
curr = curr->parent();
}
return 0;
}
LayoutRect RenderBox::logicalVisualOverflowRectForPropagation(RenderStyle* parentStyle) const
{
LayoutRect rect = visualOverflowRectForPropagation(parentStyle);
if (!parentStyle->isHorizontalWritingMode())
return rect.transposedRect();
return rect;
}
LayoutRect RenderBox::visualOverflowRectForPropagation(RenderStyle* parentStyle) const
{
LayoutRect rect = visualOverflowRect();
if (parentStyle->writingMode() == style()->writingMode())
return rect;
if (style()->writingMode() == RightToLeftWritingMode || parentStyle->writingMode() == RightToLeftWritingMode)
rect.setX(width() - rect.maxX());
else if (style()->writingMode() == BottomToTopWritingMode || parentStyle->writingMode() == BottomToTopWritingMode)
rect.setY(height() - rect.maxY());
return rect;
}
LayoutRect RenderBox::logicalLayoutOverflowRectForPropagation(RenderStyle* parentStyle) const
{
LayoutRect rect = layoutOverflowRectForPropagation(parentStyle);
if (!parentStyle->isHorizontalWritingMode())
return rect.transposedRect();
return rect;
}
LayoutRect RenderBox::layoutOverflowRectForPropagation(RenderStyle* parentStyle) const
{
LayoutRect rect = borderBoxRect();
if (!hasOverflowClip())
rect.unite(layoutOverflowRect());
bool hasTransform = hasLayer() && layer()->transform();
if (isInFlowPositioned() || (hasTransform && (!document()->settings() || document()->settings()->shouldTransformsAffectOverflow()))) {
flipForWritingMode(rect);
if (hasTransform)
rect = layer()->currentTransform().mapRect(rect);
if (isInFlowPositioned())
rect.move(offsetForInFlowPosition());
flipForWritingMode(rect);
}
if (parentStyle->writingMode() == style()->writingMode())
return rect;
if (style()->writingMode() == RightToLeftWritingMode || parentStyle->writingMode() == RightToLeftWritingMode)
rect.setX(width() - rect.maxX());
else if (style()->writingMode() == BottomToTopWritingMode || parentStyle->writingMode() == BottomToTopWritingMode)
rect.setY(height() - rect.maxY());
return rect;
}
LayoutUnit RenderBox::offsetLeft() const
{
return adjustedPositionRelativeToOffsetParent(topLeftLocation()).x();
}
LayoutUnit RenderBox::offsetTop() const
{
return adjustedPositionRelativeToOffsetParent(topLeftLocation()).y();
}
LayoutPoint RenderBox::flipForWritingModeForChild(const RenderBox* child, const LayoutPoint& point) const
{
if (!style()->isFlippedBlocksWritingMode())
return point;
if (isHorizontalWritingMode())
return LayoutPoint(point.x(), point.y() + height() - child->height() - (2 * child->y()));
return LayoutPoint(point.x() + width() - child->width() - (2 * child->x()), point.y());
}
void RenderBox::flipForWritingMode(LayoutRect& rect) const
{
if (!style()->isFlippedBlocksWritingMode())
return;
if (isHorizontalWritingMode())
rect.setY(height() - rect.maxY());
else
rect.setX(width() - rect.maxX());
}
LayoutUnit RenderBox::flipForWritingMode(LayoutUnit position) const
{
if (!style()->isFlippedBlocksWritingMode())
return position;
return logicalHeight() - position;
}
LayoutPoint RenderBox::flipForWritingMode(const LayoutPoint& position) const
{
if (!style()->isFlippedBlocksWritingMode())
return position;
return isHorizontalWritingMode() ? LayoutPoint(position.x(), height() - position.y()) : LayoutPoint(width() - position.x(), position.y());
}
LayoutPoint RenderBox::flipForWritingModeIncludingColumns(const LayoutPoint& point) const
{
if (!hasColumns() || !style()->isFlippedBlocksWritingMode())
return flipForWritingMode(point);
return toRenderBlock(this)->flipForWritingModeIncludingColumns(point);
}
LayoutSize RenderBox::flipForWritingMode(const LayoutSize& offset) const
{
if (!style()->isFlippedBlocksWritingMode())
return offset;
return isHorizontalWritingMode() ? LayoutSize(offset.width(), height() - offset.height()) : LayoutSize(width() - offset.width(), offset.height());
}
FloatPoint RenderBox::flipForWritingMode(const FloatPoint& position) const
{
if (!style()->isFlippedBlocksWritingMode())
return position;
return isHorizontalWritingMode() ? FloatPoint(position.x(), height() - position.y()) : FloatPoint(width() - position.x(), position.y());
}
void RenderBox::flipForWritingMode(FloatRect& rect) const
{
if (!style()->isFlippedBlocksWritingMode())
return;
if (isHorizontalWritingMode())
rect.setY(height() - rect.maxY());
else
rect.setX(width() - rect.maxX());
}
LayoutPoint RenderBox::topLeftLocation() const
{
RenderBlock* containerBlock = containingBlock();
if (!containerBlock || containerBlock == this)
return location();
return containerBlock->flipForWritingModeForChild(this, location());
}
LayoutSize RenderBox::topLeftLocationOffset() const
{
RenderBlock* containerBlock = containingBlock();
if (!containerBlock || containerBlock == this)
return locationOffset();
LayoutRect rect(frameRect());
containerBlock->flipForWritingMode(rect); return LayoutSize(rect.x(), rect.y());
}
bool RenderBox::hasRelativeDimensions() const
{
return style()->height().isPercent() || style()->width().isPercent()
|| style()->maxHeight().isPercent() || style()->maxWidth().isPercent()
|| style()->minHeight().isPercent() || style()->minWidth().isPercent();
}
bool RenderBox::hasRelativeLogicalHeight() const
{
return style()->logicalHeight().isPercent()
|| style()->logicalMinHeight().isPercent()
|| style()->logicalMaxHeight().isPercent();
}
static void markBoxForRelayoutAfterSplit(RenderBox* box)
{
if (box->isTable()) {
toRenderTable(box)->forceSectionsRecalc();
} else if (box->isTableSection())
toRenderTableSection(box)->setNeedsCellRecalc();
box->setNeedsLayoutAndPrefWidthsRecalc();
}
RenderObject* RenderBox::splitAnonymousBoxesAroundChild(RenderObject* beforeChild)
{
bool didSplitParentAnonymousBoxes = false;
while (beforeChild->parent() != this) {
RenderBox* boxToSplit = toRenderBox(beforeChild->parent());
if (boxToSplit->firstChild() != beforeChild && boxToSplit->isAnonymous()) {
didSplitParentAnonymousBoxes = true;
RenderBox* postBox = boxToSplit->createAnonymousBoxWithSameTypeAs(this);
postBox->setChildrenInline(boxToSplit->childrenInline());
RenderBox* parentBox = toRenderBox(boxToSplit->parent());
markBoxForRelayoutAfterSplit(parentBox);
parentBox->virtualChildren()->insertChildNode(parentBox, postBox, boxToSplit->nextSibling());
boxToSplit->moveChildrenTo(postBox, beforeChild, 0, true);
markBoxForRelayoutAfterSplit(boxToSplit);
markBoxForRelayoutAfterSplit(postBox);
beforeChild = postBox;
} else
beforeChild = boxToSplit;
}
if (didSplitParentAnonymousBoxes)
markBoxForRelayoutAfterSplit(this);
ASSERT(beforeChild->parent() == this);
return beforeChild;
}
}