#include "config.h"
#include "RenderRegion.h"
#include "FlowThreadController.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "IntRect.h"
#include "LayoutRepainter.h"
#include "PaintInfo.h"
#include "Range.h"
#include "RenderBoxRegionInfo.h"
#include "RenderNamedFlowThread.h"
#include "RenderView.h"
#include "StyleResolver.h"
#include <wtf/StackStats.h>
using namespace std;
namespace WebCore {
RenderRegion::RenderRegion(Element* element, RenderFlowThread* flowThread)
: RenderBlock(element)
, m_flowThread(flowThread)
, m_parentNamedFlowThread(0)
, m_isValid(false)
, m_hasCustomRegionStyle(false)
, m_hasAutoLogicalHeight(false)
, m_regionState(RegionUndefined)
{
}
LayoutUnit RenderRegion::pageLogicalWidth() const
{
ASSERT(m_flowThread);
return m_flowThread->isHorizontalWritingMode() ? contentWidth() : contentHeight();
}
LayoutUnit RenderRegion::pageLogicalHeight() const
{
ASSERT(m_flowThread);
if (hasOverrideHeight() && !m_flowThread->inConstrainedLayoutPhase()) {
ASSERT(hasAutoLogicalHeight());
return overrideLogicalContentHeight();
}
return m_flowThread->isHorizontalWritingMode() ? contentHeight() : contentWidth();
}
LayoutUnit RenderRegion::maxPageLogicalHeight() const
{
ASSERT(m_flowThread);
ASSERT(hasAutoLogicalHeight() && !m_flowThread->inConstrainedLayoutPhase());
return style()->logicalMaxHeight().isUndefined() ? LayoutUnit::max() / 2 : computeReplacedLogicalHeightUsing(style()->logicalMaxHeight());
}
LayoutUnit RenderRegion::logicalHeightOfAllFlowThreadContent() const
{
ASSERT(m_flowThread);
if (hasOverrideHeight() && !m_flowThread->inConstrainedLayoutPhase()) {
ASSERT(hasAutoLogicalHeight());
return overrideLogicalContentHeight();
}
return m_flowThread->isHorizontalWritingMode() ? contentHeight() : contentWidth();
}
LayoutRect RenderRegion::flowThreadPortionOverflowRect() const
{
return overflowRectForFlowThreadPortion(flowThreadPortionRect(), isFirstRegion(), isLastRegion());
}
LayoutRect RenderRegion::overflowRectForFlowThreadPortion(const LayoutRect& flowThreadPortionRect, bool isFirstPortion, bool isLastPortion) const
{
ASSERT(isValid());
bool clipX = style()->overflowX() != OVISIBLE;
bool clipY = style()->overflowY() != OVISIBLE;
bool isLastRegionWithRegionFragmentBreak = (isLastPortion && (style()->regionFragment() == BreakRegionFragment));
if ((clipX && clipY) || isLastRegionWithRegionFragmentBreak)
return flowThreadPortionRect;
LayoutRect flowThreadOverflow = m_flowThread->visualOverflowRect();
LayoutUnit outlineSize = maximalOutlineSize(PaintPhaseOutline);
LayoutRect clipRect;
if (m_flowThread->isHorizontalWritingMode()) {
LayoutUnit minY = isFirstPortion ? (flowThreadOverflow.y() - outlineSize) : flowThreadPortionRect.y();
LayoutUnit maxY = isLastPortion ? max(flowThreadPortionRect.maxY(), flowThreadOverflow.maxY()) + outlineSize : flowThreadPortionRect.maxY();
LayoutUnit minX = clipX ? flowThreadPortionRect.x() : min(flowThreadPortionRect.x(), flowThreadOverflow.x() - outlineSize);
LayoutUnit maxX = clipX ? flowThreadPortionRect.maxX() : max(flowThreadPortionRect.maxX(), (flowThreadOverflow.maxX() + outlineSize));
clipRect = LayoutRect(minX, minY, maxX - minX, maxY - minY);
} else {
LayoutUnit minX = isFirstPortion ? (flowThreadOverflow.x() - outlineSize) : flowThreadPortionRect.x();
LayoutUnit maxX = isLastPortion ? max(flowThreadPortionRect.maxX(), flowThreadOverflow.maxX()) + outlineSize : flowThreadPortionRect.maxX();
LayoutUnit minY = clipY ? flowThreadPortionRect.y() : min(flowThreadPortionRect.y(), (flowThreadOverflow.y() - outlineSize));
LayoutUnit maxY = clipY ? flowThreadPortionRect.maxY() : max(flowThreadPortionRect.y(), (flowThreadOverflow.maxY() + outlineSize));
clipRect = LayoutRect(minX, minY, maxX - minX, maxY - minY);
}
return clipRect;
}
LayoutUnit RenderRegion::pageLogicalTopForOffset(LayoutUnit ) const
{
return flowThread()->isHorizontalWritingMode() ? flowThreadPortionRect().y() : flowThreadPortionRect().x();
}
bool RenderRegion::isFirstRegion() const
{
ASSERT(isValid());
return m_flowThread->firstRegion() == this;
}
bool RenderRegion::isLastRegion() const
{
ASSERT(isValid());
return m_flowThread->lastRegion() == this;
}
static bool shouldPaintRegionContentsInPhase(PaintPhase phase)
{
return phase == PaintPhaseBlockBackground
|| phase == PaintPhaseChildBlockBackground
|| phase == PaintPhaseSelection
|| phase == PaintPhaseTextClip;
}
void RenderRegion::paintObject(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (style()->visibility() != VISIBLE)
return;
RenderBlock::paintObject(paintInfo, paintOffset);
if (!isValid())
return;
if (!shouldPaintRegionContentsInPhase(paintInfo.phase))
return;
setRegionObjectsRegionStyle();
m_flowThread->paintFlowThreadPortionInRegion(paintInfo, this, flowThreadPortionRect(), flowThreadPortionOverflowRect(), LayoutPoint(paintOffset.x() + borderLeft() + paddingLeft(), paintOffset.y() + borderTop() + paddingTop()));
restoreRegionObjectsOriginalStyle();
}
bool RenderRegion::hitTestContents(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
{
if (!isValid() || action != HitTestForeground)
return false;
LayoutRect boundsRect = borderBoxRectInRegion(locationInContainer.region());
boundsRect.moveBy(accumulatedOffset);
if (visibleToHitTesting() && locationInContainer.intersects(boundsRect)) {
if (m_flowThread->hitTestFlowThreadPortionInRegion(this, flowThreadPortionRect(), flowThreadPortionOverflowRect(), request, result,
locationInContainer, LayoutPoint(accumulatedOffset.x() + borderLeft() + paddingLeft(), accumulatedOffset.y() + borderTop() + paddingTop())))
return true;
}
return false;
}
void RenderRegion::checkRegionStyle()
{
ASSERT(m_flowThread);
bool customRegionStyle = false;
if (node()) {
Element* regionElement = toElement(node());
customRegionStyle = view()->document()->ensureStyleResolver()->checkRegionStyle(regionElement);
}
setHasCustomRegionStyle(customRegionStyle);
m_flowThread->checkRegionsWithStyling();
}
void RenderRegion::incrementAutoLogicalHeightCount()
{
ASSERT(isValid());
ASSERT(m_hasAutoLogicalHeight);
m_flowThread->incrementAutoLogicalHeightRegions();
}
void RenderRegion::decrementAutoLogicalHeightCount()
{
ASSERT(isValid());
m_flowThread->decrementAutoLogicalHeightRegions();
}
void RenderRegion::updateRegionHasAutoLogicalHeightFlag()
{
ASSERT(m_flowThread);
if (!isValid())
return;
bool didHaveAutoLogicalHeight = m_hasAutoLogicalHeight;
m_hasAutoLogicalHeight = shouldHaveAutoLogicalHeight();
if (m_hasAutoLogicalHeight != didHaveAutoLogicalHeight) {
if (m_hasAutoLogicalHeight)
incrementAutoLogicalHeightCount();
else {
clearOverrideLogicalContentHeight();
decrementAutoLogicalHeightCount();
}
}
}
bool RenderRegion::shouldHaveAutoLogicalHeight() const
{
bool hasSpecifiedEndpointsForHeight = style()->logicalTop().isSpecified() && style()->logicalBottom().isSpecified();
bool hasAnchoredEndpointsForHeight = isOutOfFlowPositioned() && hasSpecifiedEndpointsForHeight;
return style()->logicalHeight().isAuto() && !hasAnchoredEndpointsForHeight;
}
void RenderRegion::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBlock::styleDidChange(diff, oldStyle);
if (!m_flowThread) {
setHasCustomRegionStyle(false);
return;
}
checkRegionStyle();
updateRegionHasAutoLogicalHeightFlag();
}
void RenderRegion::layoutBlock(bool relayoutChildren, LayoutUnit)
{
StackStats::LayoutCheckPoint layoutCheckPoint;
RenderBlock::layoutBlock(relayoutChildren);
if (isValid()) {
LayoutRect oldRegionRect(flowThreadPortionRect());
if (!isHorizontalWritingMode())
oldRegionRect = oldRegionRect.transposedRect();
if (hasAutoLogicalHeight() && !m_flowThread->inConstrainedLayoutPhase()) {
m_flowThread->invalidateRegions();
clearOverrideLogicalContentHeight();
return;
}
if (!isRenderRegionSet() && (oldRegionRect.width() != pageLogicalWidth() || oldRegionRect.height() != pageLogicalHeight()))
m_flowThread->invalidateRegions();
}
}
void RenderRegion::repaintFlowThreadContent(const LayoutRect& repaintRect, bool immediate) const
{
repaintFlowThreadContentRectangle(repaintRect, immediate, flowThreadPortionRect(), flowThreadPortionOverflowRect(), contentBoxRect().location());
}
void RenderRegion::repaintFlowThreadContentRectangle(const LayoutRect& repaintRect, bool immediate, const LayoutRect& flowThreadPortionRect, const LayoutRect& flowThreadPortionOverflowRect, const LayoutPoint& regionLocation) const
{
ASSERT(isValid());
LayoutRect flippedFlowThreadPortionRect(flowThreadPortionRect);
LayoutRect flippedFlowThreadPortionOverflowRect(flowThreadPortionOverflowRect);
flowThread()->flipForWritingMode(flippedFlowThreadPortionRect); flowThread()->flipForWritingMode(flippedFlowThreadPortionOverflowRect);
LayoutRect clippedRect(repaintRect);
clippedRect.intersect(flippedFlowThreadPortionOverflowRect);
if (clippedRect.isEmpty())
return;
clippedRect.setLocation(regionLocation + (clippedRect.location() - flippedFlowThreadPortionRect.location()));
flipForWritingMode(clippedRect);
repaintRectangle(clippedRect, immediate);
}
void RenderRegion::installFlowThread()
{
ASSERT(view());
m_flowThread = view()->flowThreadController()->ensureRenderFlowThreadWithName(style()->regionThread());
RenderObject* parentObject = parent();
m_parentNamedFlowThread = 0;
for ( ; parentObject; parentObject = parentObject->parent()) {
if (parentObject->isRenderNamedFlowThread()) {
m_parentNamedFlowThread = toRenderNamedFlowThread(parentObject);
if (m_flowThread == m_parentNamedFlowThread)
m_flowThread = 0;
break;
}
}
}
void RenderRegion::attachRegion()
{
if (documentBeingDestroyed())
return;
setIsValid(false);
installFlowThread();
if (!m_flowThread)
return;
m_flowThread->addRegionToThread(this);
checkRegionStyle();
if (!isValid())
return;
m_hasAutoLogicalHeight = shouldHaveAutoLogicalHeight();
if (hasAutoLogicalHeight())
incrementAutoLogicalHeightCount();
}
void RenderRegion::detachRegion()
{
if (m_flowThread) {
m_flowThread->removeRegionFromThread(this);
if (hasAutoLogicalHeight())
decrementAutoLogicalHeightCount();
}
m_flowThread = 0;
}
RenderBoxRegionInfo* RenderRegion::renderBoxRegionInfo(const RenderBox* box) const
{
ASSERT(isValid());
return m_renderBoxRegionInfo.get(box);
}
RenderBoxRegionInfo* RenderRegion::setRenderBoxRegionInfo(const RenderBox* box, LayoutUnit logicalLeftInset, LayoutUnit logicalRightInset,
bool containingBlockChainIsInset)
{
ASSERT(isValid());
OwnPtr<RenderBoxRegionInfo>& boxInfo = m_renderBoxRegionInfo.add(box, nullptr).iterator->value;
if (boxInfo)
*boxInfo = RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset);
else
boxInfo = adoptPtr(new RenderBoxRegionInfo(logicalLeftInset, logicalRightInset, containingBlockChainIsInset));
return boxInfo.get();
}
PassOwnPtr<RenderBoxRegionInfo> RenderRegion::takeRenderBoxRegionInfo(const RenderBox* box)
{
return m_renderBoxRegionInfo.take(box);
}
void RenderRegion::removeRenderBoxRegionInfo(const RenderBox* box)
{
m_renderBoxRegionInfo.remove(box);
}
void RenderRegion::deleteAllRenderBoxRegionInfo()
{
m_renderBoxRegionInfo.clear();
}
LayoutUnit RenderRegion::logicalTopOfFlowThreadContentRect(const LayoutRect& rect) const
{
ASSERT(isValid());
return flowThread()->isHorizontalWritingMode() ? rect.y() : rect.x();
}
LayoutUnit RenderRegion::logicalBottomOfFlowThreadContentRect(const LayoutRect& rect) const
{
ASSERT(isValid());
return flowThread()->isHorizontalWritingMode() ? rect.maxY() : rect.maxX();
}
void RenderRegion::setRegionObjectsRegionStyle()
{
if (!hasCustomRegionStyle())
return;
RenderNamedFlowThread* namedFlow = view()->flowThreadController()->ensureRenderFlowThreadWithName(style()->regionThread());
const NamedFlowContentNodes& contentNodes = namedFlow->contentNodes();
for (NamedFlowContentNodes::const_iterator iter = contentNodes.begin(), end = contentNodes.end(); iter != end; ++iter) {
const Node* node = *iter;
if (!node->renderer())
continue;
RenderObject* object = node->renderer();
if (!flowThread()->objectInFlowRegion(object, this))
continue;
RenderObjectRegionStyleMap::iterator it = m_renderObjectRegionStyle.find(object);
RefPtr<RenderStyle> objectStyleInRegion;
bool objectRegionStyleCached = false;
if (it != m_renderObjectRegionStyle.end()) {
objectStyleInRegion = it->value.style;
ASSERT(it->value.cached);
objectRegionStyleCached = true;
} else
objectStyleInRegion = computeStyleInRegion(object);
setObjectStyleInRegion(object, objectStyleInRegion, objectRegionStyleCached);
computeChildrenStyleInRegion(object);
}
}
void RenderRegion::restoreRegionObjectsOriginalStyle()
{
if (!hasCustomRegionStyle())
return;
RenderObjectRegionStyleMap temp;
for (RenderObjectRegionStyleMap::iterator iter = m_renderObjectRegionStyle.begin(), end = m_renderObjectRegionStyle.end(); iter != end; ++iter) {
RenderObject* object = const_cast<RenderObject*>(iter->key);
RefPtr<RenderStyle> objectRegionStyle = object->style();
RefPtr<RenderStyle> objectOriginalStyle = iter->value.style;
object->setStyleInternal(objectOriginalStyle);
bool shouldCacheRegionStyle = iter->value.cached;
if (!shouldCacheRegionStyle) {
unsigned changedContextSensitiveProperties = ContextSensitivePropertyNone;
StyleDifference styleDiff = objectOriginalStyle->diff(objectRegionStyle.get(), changedContextSensitiveProperties);
if (styleDiff < StyleDifferenceLayoutPositionedMovementOnly)
shouldCacheRegionStyle = true;
}
if (shouldCacheRegionStyle) {
ObjectRegionStyleInfo styleInfo;
styleInfo.style = objectRegionStyle;
styleInfo.cached = true;
temp.set(object, styleInfo);
}
}
m_renderObjectRegionStyle.swap(temp);
}
void RenderRegion::insertedIntoTree()
{
RenderBlock::insertedIntoTree();
attachRegion();
}
void RenderRegion::willBeRemovedFromTree()
{
RenderBlock::willBeRemovedFromTree();
detachRegion();
}
PassRefPtr<RenderStyle> RenderRegion::computeStyleInRegion(const RenderObject* object)
{
ASSERT(object);
ASSERT(object->view());
ASSERT(object->view()->document());
ASSERT(!object->isAnonymous());
ASSERT(object->node() && object->node()->isElementNode());
Element* element = toElement(object->node());
RefPtr<RenderStyle> renderObjectRegionStyle = object->view()->document()->ensureStyleResolver()->styleForElement(element, 0, DisallowStyleSharing, MatchAllRules, this);
return renderObjectRegionStyle.release();
}
void RenderRegion::computeChildrenStyleInRegion(const RenderObject* object)
{
for (RenderObject* child = object->firstChild(); child; child = child->nextSibling()) {
RenderObjectRegionStyleMap::iterator it = m_renderObjectRegionStyle.find(child);
RefPtr<RenderStyle> childStyleInRegion;
bool objectRegionStyleCached = false;
if (it != m_renderObjectRegionStyle.end()) {
childStyleInRegion = it->value.style;
objectRegionStyleCached = true;
} else {
if (child->isAnonymous() || child->isInFlowRenderFlowThread())
childStyleInRegion = RenderStyle::createAnonymousStyleWithDisplay(object->style(), child->style()->display());
else if (child->isText())
childStyleInRegion = RenderStyle::clone(object->style());
else
childStyleInRegion = computeStyleInRegion(child);
}
setObjectStyleInRegion(child, childStyleInRegion, objectRegionStyleCached);
computeChildrenStyleInRegion(child);
}
}
void RenderRegion::setObjectStyleInRegion(RenderObject* object, PassRefPtr<RenderStyle> styleInRegion, bool objectRegionStyleCached)
{
ASSERT(object->flowThreadContainingBlock());
RefPtr<RenderStyle> objectOriginalStyle = object->style();
object->setStyleInternal(styleInRegion);
if (object->isBoxModelObject() && !object->hasBoxDecorations()) {
bool hasBoxDecorations = object->isTableCell()
|| object->style()->hasBackground()
|| object->style()->hasBorder()
|| object->style()->hasAppearance()
|| object->style()->boxShadow();
object->setHasBoxDecorations(hasBoxDecorations);
}
ObjectRegionStyleInfo styleInfo;
styleInfo.style = objectOriginalStyle;
styleInfo.cached = objectRegionStyleCached;
m_renderObjectRegionStyle.set(object, styleInfo);
}
void RenderRegion::clearObjectStyleInRegion(const RenderObject* object)
{
ASSERT(object);
m_renderObjectRegionStyle.remove(object);
for (RenderObject* child = object->firstChild(); child; child = child->nextSibling())
clearObjectStyleInRegion(child);
}
void RenderRegion::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
{
if (!isValid()) {
RenderBlock::computeIntrinsicLogicalWidths(minLogicalWidth, maxLogicalWidth);
return;
}
minLogicalWidth = m_flowThread->minPreferredLogicalWidth();
maxLogicalWidth = m_flowThread->maxPreferredLogicalWidth();
}
void RenderRegion::computePreferredLogicalWidths()
{
ASSERT(preferredLogicalWidthsDirty());
if (!isValid()) {
RenderBlock::computePreferredLogicalWidths();
return;
}
m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = 0;
RenderStyle* styleToUse = style();
if (styleToUse->logicalWidth().isFixed() && styleToUse->logicalWidth().value() > 0)
m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalWidth().value());
else
computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
if (styleToUse->logicalMinWidth().isFixed() && styleToUse->logicalMinWidth().value() > 0) {
m_maxPreferredLogicalWidth = std::max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
}
if (styleToUse->logicalMaxWidth().isFixed()) {
m_maxPreferredLogicalWidth = std::min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
m_minPreferredLogicalWidth = std::min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
}
LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
m_minPreferredLogicalWidth += borderAndPadding;
m_maxPreferredLogicalWidth += borderAndPadding;
setPreferredLogicalWidthsDirty(false);
}
void RenderRegion::getRanges(Vector<RefPtr<Range> >& rangeObjects) const
{
RenderNamedFlowThread* namedFlow = view()->flowThreadController()->ensureRenderFlowThreadWithName(style()->regionThread());
namedFlow->getRanges(rangeObjects, this);
}
void RenderRegion::updateLogicalHeight()
{
RenderBlock::updateLogicalHeight();
if (!hasAutoLogicalHeight())
return;
if (!m_flowThread->inConstrainedLayoutPhase())
return;
if (!hasOverrideHeight())
return;
LayoutUnit newLogicalHeight = overrideLogicalContentHeight() + borderAndPaddingLogicalHeight();
ASSERT(newLogicalHeight < LayoutUnit::max() / 2);
if (newLogicalHeight > logicalHeight()) {
setLogicalHeight(newLogicalHeight);
RenderBlock::updateLogicalHeight();
}
}
}