#include "config.h"
#include "RenderBox.h"
#include "CSSFontSelector.h"
#include "ControlStates.h"
#include "Document.h"
#include "Editing.h"
#include "EventHandler.h"
#include "FloatQuad.h"
#include "FloatRoundedRect.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HTMLBodyElement.h"
#include "HTMLButtonElement.h"
#include "HTMLFrameOwnerElement.h"
#include "HTMLHtmlElement.h"
#include "HTMLImageElement.h"
#include "HTMLInputElement.h"
#include "HTMLLegendElement.h"
#include "HTMLNames.h"
#include "HTMLSelectElement.h"
#include "HTMLTextAreaElement.h"
#include "HitTestResult.h"
#include "InlineElementBox.h"
#include "MainFrame.h"
#include "Page.h"
#include "PaintInfo.h"
#include "RenderBoxRegionInfo.h"
#include "RenderChildIterator.h"
#include "RenderDeprecatedFlexibleBox.h"
#include "RenderFlexibleBox.h"
#include "RenderGeometryMap.h"
#include "RenderInline.h"
#include "RenderIterator.h"
#include "RenderLayer.h"
#include "RenderLayerCompositor.h"
#include "RenderMultiColumnFlowThread.h"
#include "RenderNamedFlowFragment.h"
#include "RenderNamedFlowThread.h"
#include "RenderTableCell.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "RuntimeApplicationChecks.h"
#include "ScrollAnimator.h"
#include "ScrollbarTheme.h"
#include "StyleScrollSnapPoints.h"
#include "TransformState.h"
#include <algorithm>
#include <math.h>
#include <wtf/StackStats.h>
#include "RenderGrid.h"
#if PLATFORM(IOS)
#include "Settings.h"
#endif
namespace WebCore {
struct SameSizeAsRenderBox : public RenderBoxModelObject {
virtual ~SameSizeAsRenderBox() { }
LayoutRect frameRect;
LayoutBoxExtent marginBox;
LayoutUnit preferredLogicalWidths[2];
void* pointers[2];
};
COMPILE_ASSERT(sizeof(RenderBox) == sizeof(SameSizeAsRenderBox), RenderBox_should_stay_small);
using namespace HTMLNames;
typedef WTF::HashMap<const RenderBox*, LayoutUnit> OverrideSizeMap;
static OverrideSizeMap* gOverrideHeightMap = nullptr;
static OverrideSizeMap* gOverrideWidthMap = nullptr;
typedef WTF::HashMap<const RenderBox*, std::optional<LayoutUnit>> OverrideOptionalSizeMap;
static OverrideOptionalSizeMap* gOverrideContainingBlockLogicalHeightMap = nullptr;
static OverrideOptionalSizeMap* gOverrideContainingBlockLogicalWidthMap = nullptr;
static const int autoscrollBeltSize = 20;
static const unsigned backgroundObscurationTestMaxDepth = 4;
using ControlStatesRendererMap = HashMap<const RenderObject*, std::unique_ptr<ControlStates>>;
static ControlStatesRendererMap& controlStatesRendererMap()
{
static NeverDestroyed<ControlStatesRendererMap> map;
return map;
}
static ControlStates* controlStatesForRenderer(const RenderBox& renderer)
{
return controlStatesRendererMap().ensure(&renderer, [] {
return std::make_unique<ControlStates>();
}).iterator->value.get();
}
static void removeControlStatesForRenderer(const RenderBox& renderer)
{
controlStatesRendererMap().remove(&renderer);
}
bool RenderBox::s_hadOverflowClip = false;
RenderBox::RenderBox(Element& element, RenderStyle&& style, BaseTypeFlags baseTypeFlags)
: RenderBoxModelObject(element, WTFMove(style), baseTypeFlags)
{
setIsBox();
}
RenderBox::RenderBox(Document& document, RenderStyle&& style, BaseTypeFlags baseTypeFlags)
: RenderBoxModelObject(document, WTFMove(style), baseTypeFlags)
{
setIsBox();
}
RenderBox::~RenderBox()
{
}
void RenderBox::willBeDestroyed()
{
if (frame().eventHandler().autoscrollRenderer() == this)
frame().eventHandler().stopAutoscrollTimer(true);
clearOverrideSize();
clearContainingBlockOverrideSize();
RenderBlock::removePercentHeightDescendantIfNeeded(*this);
ShapeOutsideInfo::removeInfo(*this);
view().unscheduleLazyRepaint(*this);
removeControlStatesForRenderer(*this);
RenderBoxModelObject::willBeDestroyed();
}
RenderRegion* RenderBox::clampToStartAndEndRegions(RenderRegion* region) const
{
RenderFlowThread* flowThread = flowThreadContainingBlock();
ASSERT(isRenderView() || (region && flowThread));
if (isRenderView())
return region;
RenderRegion* startRegion = nullptr;
RenderRegion* endRegion = nullptr;
if (!flowThread->getRegionRangeForBox(this, startRegion, endRegion))
return region;
if (region->logicalTopForFlowThreadContent() < startRegion->logicalTopForFlowThreadContent())
return startRegion;
if (region->logicalTopForFlowThreadContent() > endRegion->logicalTopForFlowThreadContent())
return endRegion;
return region;
}
bool RenderBox::hasRegionRangeInFlowThread() const
{
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (!flowThread || !flowThread->hasValidRegionInfo())
return false;
return flowThread->hasCachedRegionRangeForBox(*this);
}
LayoutRect RenderBox::clientBoxRectInRegion(RenderRegion* region) const
{
if (!region)
return clientBoxRect();
LayoutRect clientBox = borderBoxRectInRegion(region);
clientBox.setLocation(clientBox.location() + LayoutSize(borderLeft(), borderTop()));
clientBox.setSize(clientBox.size() - LayoutSize(borderLeft() + borderRight() + verticalScrollbarWidth(), borderTop() + borderBottom() + horizontalScrollbarHeight()));
return clientBox;
}
LayoutRect RenderBox::borderBoxRectInRegion(RenderRegion* region, RenderBoxRegionInfoFlags cacheFlag) const
{
if (!region)
return borderBoxRect();
auto* flowThread = flowThreadContainingBlock();
if (!is<RenderNamedFlowThread>(flowThread))
return borderBoxRect();
RenderRegion* startRegion = nullptr;
RenderRegion* endRegion = nullptr;
if (!flowThread->getRegionRangeForBox(this, startRegion, endRegion)) {
return borderBoxRect();
}
ASSERT(flowThread->regionInRange(region, startRegion, endRegion));
RenderBoxRegionInfo* boxInfo = renderBoxRegionInfo(region, cacheFlag);
if (!boxInfo)
return borderBoxRect();
LayoutUnit logicalWidth = boxInfo->logicalWidth();
LayoutUnit logicalLeft = boxInfo->logicalLeft();
const RenderBlock* currentBox = containingBlock();
RenderBoxRegionInfo* currentBoxInfo = isRenderFlowThread() ? nullptr : currentBox->renderBoxRegionInfo(region);
while (currentBoxInfo && currentBoxInfo->isShifted()) {
if (currentBox->style().direction() == LTR)
logicalLeft += currentBoxInfo->logicalLeft();
else
logicalLeft -= (currentBox->logicalWidth() - currentBoxInfo->logicalWidth()) - currentBoxInfo->logicalLeft();
if (currentBox->isRenderFlowThread())
break;
currentBox = currentBox->containingBlock();
if (!currentBox)
break;
region = currentBox->clampToStartAndEndRegions(region);
currentBoxInfo = currentBox->renderBoxRegionInfo(region);
}
if (cacheFlag == DoNotCacheRenderBoxRegionInfo)
delete boxInfo;
if (isHorizontalWritingMode())
return LayoutRect(logicalLeft, 0, logicalWidth, height());
return LayoutRect(0, logicalLeft, width(), logicalWidth);
}
static RenderBlockFlow* outermostBlockContainingFloatingObject(RenderBox& box)
{
ASSERT(box.isFloating());
RenderBlockFlow* parentBlock = nullptr;
for (auto& ancestor : ancestorsOfType<RenderBlockFlow>(box)) {
if (ancestor.isRenderView())
break;
if (!parentBlock || ancestor.containsFloat(box))
parentBlock = &ancestor;
}
return parentBlock;
}
void RenderBox::removeFloatingOrPositionedChildFromBlockLists()
{
ASSERT(isFloatingOrOutOfFlowPositioned());
if (renderTreeBeingDestroyed())
return;
if (isFloating()) {
if (RenderBlockFlow* parentBlock = outermostBlockContainingFloatingObject(*this)) {
parentBlock->markSiblingsWithFloatsForLayout(this);
parentBlock->markAllDescendantsWithFloatsForLayout(this, false);
}
}
if (isOutOfFlowPositioned())
RenderBlock::removePositionedObject(*this);
}
void RenderBox::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
{
s_hadOverflowClip = hasOverflowClip();
const RenderStyle* oldStyle = hasInitializedStyle() ? &style() : nullptr;
if (oldStyle) {
if (diff >= StyleDifferenceRepaint && (isDocumentElementRenderer() || isBody())) {
view().repaintRootContents();
if (oldStyle->hasEntirelyFixedBackground() != newStyle.hasEntirelyFixedBackground())
view().compositor().rootFixedBackgroundsChanged();
}
if (diff == StyleDifferenceLayout && parent() && oldStyle->position() != newStyle.position()) {
markContainingBlocksForLayout();
if (oldStyle->position() == StaticPosition)
repaint();
else if (newStyle.hasOutOfFlowPosition())
parent()->setChildNeedsLayout();
if (isFloating() && !isOutOfFlowPositioned() && newStyle.hasOutOfFlowPosition())
removeFloatingOrPositionedChildFromBlockLists();
}
} else if (isBody())
view().repaintRootContents();
#if ENABLE(CSS_SCROLL_SNAP)
bool boxContributesSnapPositions = newStyle.scrollSnapArea().hasSnapPosition();
if (boxContributesSnapPositions || (oldStyle && oldStyle->scrollSnapArea().hasSnapPosition())) {
if (boxContributesSnapPositions)
view().registerBoxWithScrollSnapPositions(*this);
else
view().unregisterBoxWithScrollSnapPositions(*this);
}
#endif
RenderBoxModelObject::styleWillChange(diff, newStyle);
}
void RenderBox::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
bool oldHorizontalWritingMode = isHorizontalWritingMode();
RenderBoxModelObject::styleDidChange(diff, oldStyle);
const RenderStyle& newStyle = style();
if (needsLayout() && oldStyle) {
RenderBlock::removePercentHeightDescendantIfNeeded(*this);
if (isOutOfFlowPositioned() && newStyle.hasStaticBlockPosition(isHorizontalWritingMode()) && oldStyle->marginBefore() != newStyle.marginBefore()
&& parent() && !parent()->normalChildNeedsLayout())
parent()->setChildNeedsLayout();
}
if (RenderBlock::hasPercentHeightContainerMap() && firstChild()
&& oldHorizontalWritingMode != isHorizontalWritingMode())
RenderBlock::clearPercentHeightDescendantsFrom(*this);
if (hasOverflowClip() && layer() && oldStyle && oldStyle->effectiveZoom() != newStyle.effectiveZoom()) {
ScrollPosition scrollPosition = layer()->scrollPosition();
float zoomScaleFactor = newStyle.effectiveZoom() / oldStyle->effectiveZoom();
scrollPosition.scale(zoomScaleFactor);
layer()->setPostLayoutScrollPosition(scrollPosition);
}
if (diff >= StyleDifferenceRepaint && diff <= StyleDifferenceRepaintLayer) {
auto parentToInvalidate = parent();
for (unsigned i = 0; i < backgroundObscurationTestMaxDepth && parentToInvalidate; ++i) {
parentToInvalidate->invalidateBackgroundObscurationStatus();
parentToInvalidate = parentToInvalidate->parent();
}
}
bool isBodyRenderer = isBody();
bool isDocElementRenderer = isDocumentElementRenderer();
if (isDocElementRenderer || isBodyRenderer) {
auto* documentElementRenderer = document().documentElement()->renderer();
auto& viewStyle = view().mutableStyle();
bool rootStyleChanged = false;
bool viewDirectionOrWritingModeChanged = false;
auto* rootRenderer = isBodyRenderer ? documentElementRenderer : nullptr;
if (viewStyle.direction() != newStyle.direction() && (isDocElementRenderer || !documentElementRenderer->style().hasExplicitlySetDirection())) {
viewStyle.setDirection(newStyle.direction());
viewDirectionOrWritingModeChanged = true;
if (isBodyRenderer) {
rootRenderer->mutableStyle().setDirection(newStyle.direction());
rootStyleChanged = true;
}
setNeedsLayoutAndPrefWidthsRecalc();
view().frameView().topContentDirectionDidChange();
}
if (viewStyle.writingMode() != newStyle.writingMode() && (isDocElementRenderer || !documentElementRenderer->style().hasExplicitlySetWritingMode())) {
viewStyle.setWritingMode(newStyle.writingMode());
viewDirectionOrWritingModeChanged = true;
view().setHorizontalWritingMode(newStyle.isHorizontalWritingMode());
view().markAllDescendantsWithFloatsForLayout();
if (isBodyRenderer) {
rootStyleChanged = true;
rootRenderer->mutableStyle().setWritingMode(newStyle.writingMode());
rootRenderer->setHorizontalWritingMode(newStyle.isHorizontalWritingMode());
}
setNeedsLayoutAndPrefWidthsRecalc();
}
view().frameView().recalculateScrollbarOverlayStyle();
const Pagination& pagination = view().frameView().pagination();
if (viewDirectionOrWritingModeChanged && pagination.mode != Pagination::Unpaginated) {
viewStyle.setColumnStylesFromPaginationMode(pagination.mode);
if (view().multiColumnFlowThread())
view().updateColumnProgressionFromStyle(viewStyle);
}
if (viewDirectionOrWritingModeChanged && view().multiColumnFlowThread())
view().updateStylesForColumnChildren();
if (rootStyleChanged && is<RenderBlockFlow>(rootRenderer) && downcast<RenderBlockFlow>(*rootRenderer).multiColumnFlowThread())
downcast<RenderBlockFlow>(*rootRenderer).updateStylesForColumnChildren();
if (isBodyRenderer && pagination.mode != Pagination::Unpaginated && page().paginationLineGridEnabled()) {
if (newStyle.fontDescription() != view().style().fontDescription()) {
view().mutableStyle().setFontDescription(newStyle.fontDescription());
view().mutableStyle().fontCascade().update(&document().fontSelector());
}
}
if (diff != StyleDifferenceEqual)
view().compositor().rootOrBodyStyleChanged(*this, oldStyle);
}
if ((oldStyle && oldStyle->shapeOutside()) || style().shapeOutside())
updateShapeOutsideInfoAfterStyleChange(style(), oldStyle);
updateGridPositionAfterStyleChange(style(), oldStyle);
}
void RenderBox::willBeRemovedFromTree()
{
#if ENABLE(CSS_SCROLL_SNAP)
if (hasInitializedStyle() && style().scrollSnapArea().hasSnapPosition())
view().unregisterBoxWithScrollSnapPositions(*this);
#endif
RenderBoxModelObject::willBeRemovedFromTree();
}
void RenderBox::updateGridPositionAfterStyleChange(const RenderStyle& style, const RenderStyle* oldStyle)
{
if (!oldStyle || !is<RenderGrid>(parent()))
return;
if (oldStyle->gridItemColumnStart() == style.gridItemColumnStart()
&& oldStyle->gridItemColumnEnd() == style.gridItemColumnEnd()
&& oldStyle->gridItemRowStart() == style.gridItemRowStart()
&& oldStyle->gridItemRowEnd() == style.gridItemRowEnd()
&& oldStyle->order() == style.order()
&& oldStyle->hasOutOfFlowPosition() == style.hasOutOfFlowPosition())
return;
if (oldStyle->hasOutOfFlowPosition() && style.hasOutOfFlowPosition())
return;
downcast<RenderGrid>(*parent()).dirtyGrid();
}
void RenderBox::updateShapeOutsideInfoAfterStyleChange(const RenderStyle& style, const RenderStyle* oldStyle)
{
const ShapeValue* shapeOutside = style.shapeOutside();
const ShapeValue* oldShapeOutside = oldStyle ? oldStyle->shapeOutside() : nullptr;
Length shapeMargin = style.shapeMargin();
Length oldShapeMargin = oldStyle ? oldStyle->shapeMargin() : RenderStyle::initialShapeMargin();
float shapeImageThreshold = style.shapeImageThreshold();
float oldShapeImageThreshold = oldStyle ? oldStyle->shapeImageThreshold() : RenderStyle::initialShapeImageThreshold();
if (shapeOutside == oldShapeOutside && shapeMargin == oldShapeMargin && shapeImageThreshold == oldShapeImageThreshold)
return;
if (!shapeOutside)
ShapeOutsideInfo::removeInfo(*this);
else
ShapeOutsideInfo::ensureInfo(*this).markShapeAsDirty();
if (shapeOutside || shapeOutside != oldShapeOutside)
markShapeOutsideDependentsForLayout();
}
void RenderBox::updateFromStyle()
{
RenderBoxModelObject::updateFromStyle();
const RenderStyle& styleToUse = style();
bool isDocElementRenderer = isDocumentElementRenderer();
bool isViewObject = isRenderView();
if (isDocElementRenderer || isViewObject)
setHasVisibleBoxDecorations(true);
setFloating(!isOutOfFlowPositioned() && styleToUse.isFloating());
if (styleToUse.overflowX() != OVISIBLE && !isDocElementRenderer && isRenderBlock()) {
bool boxHasOverflowClip = true;
if (isBody()) {
if (is<HTMLHtmlElement>(*document().documentElement())
&& document().body() == element()
&& document().documentElement()->renderer()->style().overflowX() == OVISIBLE) {
boxHasOverflowClip = false;
}
}
if (boxHasOverflowClip) {
if (!s_hadOverflowClip && hasRenderOverflow()) {
repaintRectangle(visualOverflowRect());
repaintRectangle(layoutOverflowRect());
}
setHasOverflowClip();
}
}
setHasTransformRelatedProperty(styleToUse.hasTransformRelatedProperty());
setHasReflection(styleToUse.boxReflect());
}
void RenderBox::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
ASSERT(needsLayout());
RenderObject* child = firstChild();
if (!child) {
clearNeedsLayout();
return;
}
LayoutStateMaintainer statePusher(view(), *this, locationOffset(), style().isFlippedBlocksWritingMode());
while (child) {
if (child->needsLayout())
downcast<RenderElement>(*child).layout();
ASSERT(!child->needsLayout());
child = child->nextSibling();
}
statePusher.pop();
invalidateBackgroundObscurationStatus();
clearNeedsLayout();
}
LayoutUnit RenderBox::clientWidth() const
{
return width() - borderLeft() - borderRight() - verticalScrollbarWidth();
}
LayoutUnit RenderBox::clientHeight() const
{
return height() - borderTop() - borderBottom() - horizontalScrollbarHeight();
}
int RenderBox::scrollWidth() const
{
if (hasOverflowClip() && layer())
return layer()->scrollWidth();
if (style().isLeftToRightDirection()) {
return roundToInt(std::max(clientWidth(), layoutOverflowRect().maxX() - borderLeft()));
}
return roundToInt(clientWidth() - std::min<LayoutUnit>(0, layoutOverflowRect().x() - borderLeft()));
}
int RenderBox::scrollHeight() const
{
if (hasOverflowClip() && layer())
return layer()->scrollHeight();
return roundToInt(std::max(clientHeight(), layoutOverflowRect().maxY() - borderTop()));
}
int RenderBox::scrollLeft() const
{
return hasOverflowClip() && layer() ? layer()->scrollPosition().x() : 0;
}
int RenderBox::scrollTop() const
{
return hasOverflowClip() && layer() ? layer()->scrollPosition().y() : 0;
}
static void setupWheelEventTestTrigger(RenderLayer& layer)
{
Page& page = layer.renderer().page();
if (!page.expectsWheelEventTriggers())
return;
layer.scrollAnimator().setWheelEventTestTrigger(page.testTrigger());
}
void RenderBox::setScrollLeft(int newLeft)
{
if (!hasOverflowClip() || !layer())
return;
setupWheelEventTestTrigger(*layer());
layer()->scrollToXPosition(newLeft, RenderLayer::ScrollOffsetClamped);
}
void RenderBox::setScrollTop(int newTop)
{
if (!hasOverflowClip() || !layer())
return;
setupWheelEventTestTrigger(*layer());
layer()->scrollToYPosition(newTop, RenderLayer::ScrollOffsetClamped);
}
void RenderBox::absoluteRects(Vector<IntRect>& rects, const LayoutPoint& accumulatedOffset) const
{
rects.append(snappedIntRect(accumulatedOffset, size()));
}
void RenderBox::absoluteQuads(Vector<FloatQuad>& quads, bool* wasFixed) const
{
FloatRect localRect(0, 0, width(), height());
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (flowThread && flowThread->absoluteQuadsForBox(quads, wasFixed, this, localRect.y(), localRect.maxY()))
return;
quads.append(localToAbsoluteQuad(localRect, UseTransforms, wasFixed));
}
void RenderBox::updateLayerTransform()
{
if (hasLayer())
layer()->updateTransform();
}
LayoutUnit RenderBox::constrainLogicalWidthInRegionByMinMax(LayoutUnit logicalWidth, LayoutUnit availableWidth, RenderBlock& cb, RenderRegion* region) const
{
const RenderStyle& styleToUse = style();
if (!styleToUse.logicalMaxWidth().isUndefined())
logicalWidth = std::min(logicalWidth, computeLogicalWidthInRegionUsing(MaxSize, styleToUse.logicalMaxWidth(), availableWidth, cb, region));
return std::max(logicalWidth, computeLogicalWidthInRegionUsing(MinSize, styleToUse.logicalMinWidth(), availableWidth, cb, region));
}
LayoutUnit RenderBox::constrainLogicalHeightByMinMax(LayoutUnit logicalHeight, std::optional<LayoutUnit> intrinsicContentHeight) const
{
const RenderStyle& styleToUse = style();
if (!styleToUse.logicalMaxHeight().isUndefined()) {
if (std::optional<LayoutUnit> maxH = computeLogicalHeightUsing(MaxSize, styleToUse.logicalMaxHeight(), intrinsicContentHeight))
logicalHeight = std::min(logicalHeight, maxH.value());
}
if (std::optional<LayoutUnit> computedLogicalHeight = computeLogicalHeightUsing(MinSize, styleToUse.logicalMinHeight(), intrinsicContentHeight))
return std::max(logicalHeight, computedLogicalHeight.value());
return logicalHeight;
}
LayoutUnit RenderBox::constrainContentBoxLogicalHeightByMinMax(LayoutUnit logicalHeight, std::optional<LayoutUnit> intrinsicContentHeight) const
{
const RenderStyle& styleToUse = style();
if (!styleToUse.logicalMaxHeight().isUndefined()) {
if (std::optional<LayoutUnit> maxH = computeContentLogicalHeight(MaxSize, styleToUse.logicalMaxHeight(), intrinsicContentHeight))
logicalHeight = std::min(logicalHeight, maxH.value());
}
if (std::optional<LayoutUnit> computedContentLogicalHeight = computeContentLogicalHeight(MinSize, styleToUse.logicalMinHeight(), intrinsicContentHeight))
return std::max(logicalHeight, computedContentLogicalHeight.value());
return logicalHeight;
}
RoundedRect::Radii RenderBox::borderRadii() const
{
auto& style = this->style();
LayoutRect bounds = frameRect();
unsigned borderLeft = style.borderLeftWidth();
unsigned borderTop = style.borderTopWidth();
bounds.moveBy(LayoutPoint(borderLeft, borderTop));
bounds.contract(borderLeft + style.borderRightWidth(), borderTop + style.borderBottomWidth());
return style.getRoundedBorderFor(bounds).radii();
}
LayoutRect RenderBox::contentBoxRect() const
{
LayoutUnit x = borderLeft() + paddingLeft();
if (shouldPlaceBlockDirectionScrollbarOnLeft())
x += verticalScrollbarWidth();
LayoutUnit y = borderTop() + paddingTop();
return LayoutRect(x, y, contentWidth(), contentHeight());
}
IntRect RenderBox::absoluteContentBox() const
{
IntRect rect = snappedIntRect(contentBoxRect());
FloatPoint absPos = localToAbsolute();
rect.move(absPos.x(), absPos.y());
return rect;
}
FloatQuad RenderBox::absoluteContentQuad() const
{
LayoutRect rect = contentBoxRect();
return localToAbsoluteQuad(FloatRect(rect));
}
LayoutRect RenderBox::outlineBoundsForRepaint(const RenderLayerModelObject* repaintContainer, const RenderGeometryMap* geometryMap) const
{
LayoutRect box = borderBoundingBox();
adjustRectForOutlineAndShadow(box);
if (repaintContainer != this) {
FloatQuad containerRelativeQuad;
if (geometryMap)
containerRelativeQuad = geometryMap->mapToContainer(box, repaintContainer);
else
containerRelativeQuad = localToContainerQuad(FloatRect(box), repaintContainer);
box = LayoutRect(containerRelativeQuad.boundingBox());
}
box.move(view().layoutDelta());
return LayoutRect(snapRectToDevicePixels(box, document().deviceScaleFactor()));
}
void RenderBox::addFocusRingRects(Vector<LayoutRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject*)
{
if (!size().isEmpty())
rects.append(LayoutRect(additionalOffset, size()));
}
int RenderBox::reflectionOffset() const
{
if (!style().boxReflect())
return 0;
if (style().boxReflect()->direction() == ReflectionLeft || style().boxReflect()->direction() == ReflectionRight)
return valueForLength(style().boxReflect()->offset(), borderBoxRect().width());
return valueForLength(style().boxReflect()->offset(), borderBoxRect().height());
}
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(const FrameView& frameView) const
{
return style().position() == FixedPosition && container()->isRenderView() && frameView.fixedElementsLayoutRelativeToFrame();
}
bool RenderBox::includeVerticalScrollbarSize() const
{
return hasOverflowClip() && layer() && !layer()->hasOverlayScrollbars()
&& (style().overflowY() == OSCROLL || style().overflowY() == OAUTO);
}
bool RenderBox::includeHorizontalScrollbarSize() const
{
return hasOverflowClip() && layer() && !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;
}
int RenderBox::intrinsicScrollbarLogicalWidth() const
{
if (!hasOverflowClip())
return 0;
if (isHorizontalWritingMode() && (style().overflowY() == OSCROLL && !hasVerticalScrollbarWithAutoBehavior())) {
ASSERT(layer() && layer()->hasVerticalScrollbar());
return verticalScrollbarWidth();
}
if (!isHorizontalWritingMode() && (style().overflowX() == OSCROLL && !hasHorizontalScrollbarWithAutoBehavior())) {
ASSERT(layer() && layer()->hasHorizontalScrollbar());
return horizontalScrollbarHeight();
}
return 0;
}
bool RenderBox::scrollLayer(ScrollDirection direction, ScrollGranularity granularity, float multiplier, Element** stopElement)
{
RenderLayer* boxLayer = layer();
if (boxLayer && boxLayer->scroll(direction, granularity, multiplier)) {
if (stopElement)
*stopElement = element();
return true;
}
return false;
}
bool RenderBox::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier, Element** stopElement, RenderBox* startBox, const IntPoint& wheelEventAbsolutePoint)
{
if (scrollLayer(direction, granularity, multiplier, stopElement))
return true;
if (stopElement && *stopElement && *stopElement == element())
return true;
RenderBlock* nextScrollBlock = containingBlock();
if (is<RenderNamedFlowThread>(nextScrollBlock)) {
ASSERT(startBox);
nextScrollBlock = downcast<RenderNamedFlowThread>(*nextScrollBlock).fragmentFromAbsolutePointAndBox(wheelEventAbsolutePoint, *startBox);
}
if (nextScrollBlock && !nextScrollBlock->isRenderView())
return nextScrollBlock->scroll(direction, granularity, multiplier, stopElement, startBox, wheelEventAbsolutePoint);
return false;
}
bool RenderBox::logicalScroll(ScrollLogicalDirection direction, ScrollGranularity granularity, float multiplier, Element** stopElement)
{
bool scrolled = false;
RenderLayer* l = layer();
if (l) {
#if PLATFORM(COCOA)
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 (stopElement)
*stopElement = element();
return true;
}
}
if (stopElement && *stopElement && *stopElement == element())
return true;
RenderBlock* b = containingBlock();
if (b && !b->isRenderView())
return b->logicalScroll(direction, granularity, multiplier, stopElement);
return false;
}
bool RenderBox::canBeScrolledAndHasScrollableArea() const
{
return canBeProgramaticallyScrolled() && (hasHorizontalOverflow() || hasVerticalOverflow());
}
bool RenderBox::isScrollableOrRubberbandableBox() const
{
return canBeScrolledAndHasScrollableArea();
}
bool RenderBox::canBeProgramaticallyScrolled() const
{
if (isRenderView())
return true;
if (!hasOverflowClip())
return false;
if (hasScrollableOverflowX() || hasScrollableOverflowY())
return true;
return element() && element()->hasEditableStyle();
}
bool RenderBox::usesCompositedScrolling() const
{
return hasOverflowClip() && hasLayer() && layer()->usesCompositedScrolling();
}
void RenderBox::autoscroll(const IntPoint& position)
{
if (layer())
layer()->autoscroll(position);
}
bool RenderBox::canAutoscroll() const
{
if (isRenderView())
return view().frameView().isScrollable();
if (canBeScrolledAndHasScrollableArea())
return true;
return false;
}
IntSize RenderBox::calculateAutoscrollDirection(const IntPoint& windowPoint) const
{
IntRect box(absoluteBoundingBoxRect());
box.moveBy(view().frameView().scrollPosition());
IntRect windowBox = view().frameView().contentsToWindow(box);
IntPoint windowAutoscrollPoint = windowPoint;
if (windowAutoscrollPoint.x() < windowBox.x() + autoscrollBeltSize)
windowAutoscrollPoint.move(-autoscrollBeltSize, 0);
else if (windowAutoscrollPoint.x() > windowBox.maxX() - autoscrollBeltSize)
windowAutoscrollPoint.move(autoscrollBeltSize, 0);
if (windowAutoscrollPoint.y() < windowBox.y() + autoscrollBeltSize)
windowAutoscrollPoint.move(0, -autoscrollBeltSize);
else if (windowAutoscrollPoint.y() > windowBox.maxY() - autoscrollBeltSize)
windowAutoscrollPoint.move(0, autoscrollBeltSize);
return windowAutoscrollPoint - windowPoint;
}
RenderBox* RenderBox::findAutoscrollable(RenderObject* renderer)
{
while (renderer && !(is<RenderBox>(*renderer) && downcast<RenderBox>(*renderer).canAutoscroll())) {
if (is<RenderView>(*renderer) && renderer->document().ownerElement())
renderer = renderer->document().ownerElement()->renderer();
else
renderer = renderer->parent();
}
return is<RenderBox>(renderer) ? downcast<RenderBox>(renderer) : nullptr;
}
void RenderBox::panScroll(const IntPoint& source)
{
if (layer())
layer()->panScrollFromPoint(source);
}
bool RenderBox::hasVerticalScrollbarWithAutoBehavior() const
{
bool overflowScrollActsLikeAuto = style().overflowY() == OSCROLL && !style().hasPseudoStyle(SCROLLBAR) && ScrollbarTheme::theme().usesOverlayScrollbars();
return hasOverflowClip() && (style().overflowY() == OAUTO || style().overflowY() == OOVERLAY || overflowScrollActsLikeAuto);
}
bool RenderBox::hasHorizontalScrollbarWithAutoBehavior() const
{
bool overflowScrollActsLikeAuto = style().overflowX() == OSCROLL && !style().hasPseudoStyle(SCROLLBAR) && ScrollbarTheme::theme().usesOverlayScrollbars();
return hasOverflowClip() && (style().overflowX() == OAUTO || style().overflowX() == OOVERLAY || overflowScrollActsLikeAuto);
}
bool RenderBox::needsPreferredWidthsRecalculation() const
{
return style().paddingStart().isPercentOrCalculated() || style().paddingEnd().isPercentOrCalculated();
}
ScrollPosition RenderBox::scrollPosition() const
{
if (!hasOverflowClip())
return { 0, 0 };
ASSERT(hasLayer());
return layer()->scrollPosition();
}
LayoutSize RenderBox::cachedSizeForOverflowClip() const
{
ASSERT(hasOverflowClip());
ASSERT(hasLayer());
return layer()->size();
}
void RenderBox::applyCachedClipAndScrollPositionForRepaint(LayoutRect& paintRect) const
{
flipForWritingMode(paintRect);
paintRect.moveBy(-scrollPosition());
if (usesCompositedScrolling()) {
flipForWritingMode(paintRect);
return;
}
LayoutRect clipRect(LayoutPoint(), cachedSizeForOverflowClip());
paintRect = intersection(paintRect, clipRect);
flipForWritingMode(paintRect);
}
void RenderBox::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
{
minLogicalWidth = minPreferredLogicalWidth() - borderAndPaddingLogicalWidth();
maxLogicalWidth = maxPreferredLogicalWidth() - borderAndPaddingLogicalWidth();
}
LayoutUnit RenderBox::minPreferredLogicalWidth() const
{
if (preferredLogicalWidthsDirty()) {
#ifndef NDEBUG
SetLayoutNeededForbiddenScope layoutForbiddenScope(const_cast<RenderBox*>(this));
#endif
const_cast<RenderBox*>(this)->computePreferredLogicalWidths();
}
return m_minPreferredLogicalWidth;
}
LayoutUnit RenderBox::maxPreferredLogicalWidth() const
{
if (preferredLogicalWidthsDirty()) {
#ifndef NDEBUG
SetLayoutNeededForbiddenScope layoutForbiddenScope(const_cast<RenderBox*>(this));
#endif
const_cast<RenderBox*>(this)->computePreferredLogicalWidths();
}
return m_maxPreferredLogicalWidth;
}
bool RenderBox::hasOverrideLogicalContentHeight() const
{
return gOverrideHeightMap && gOverrideHeightMap->contains(this);
}
bool RenderBox::hasOverrideLogicalContentWidth() const
{
return gOverrideWidthMap && gOverrideWidthMap->contains(this);
}
void RenderBox::setOverrideLogicalContentHeight(LayoutUnit height)
{
if (!gOverrideHeightMap)
gOverrideHeightMap = new OverrideSizeMap();
gOverrideHeightMap->set(this, height);
}
void RenderBox::setOverrideLogicalContentWidth(LayoutUnit width)
{
if (!gOverrideWidthMap)
gOverrideWidthMap = new OverrideSizeMap();
gOverrideWidthMap->set(this, width);
}
void RenderBox::clearOverrideLogicalContentHeight()
{
if (gOverrideHeightMap)
gOverrideHeightMap->remove(this);
}
void RenderBox::clearOverrideLogicalContentWidth()
{
if (gOverrideWidthMap)
gOverrideWidthMap->remove(this);
}
void RenderBox::clearOverrideSize()
{
clearOverrideLogicalContentHeight();
clearOverrideLogicalContentWidth();
}
LayoutUnit RenderBox::overrideLogicalContentWidth() const
{
ASSERT(hasOverrideLogicalContentWidth());
return gOverrideWidthMap->get(this);
}
LayoutUnit RenderBox::overrideLogicalContentHeight() const
{
ASSERT(hasOverrideLogicalContentHeight());
return gOverrideHeightMap->get(this);
}
std::optional<LayoutUnit> RenderBox::overrideContainingBlockContentLogicalWidth() const
{
ASSERT(hasOverrideContainingBlockLogicalWidth());
return gOverrideContainingBlockLogicalWidthMap->get(this);
}
std::optional<LayoutUnit> RenderBox::overrideContainingBlockContentLogicalHeight() const
{
ASSERT(hasOverrideContainingBlockLogicalHeight());
return gOverrideContainingBlockLogicalHeightMap->get(this);
}
bool RenderBox::hasOverrideContainingBlockLogicalWidth() const
{
return gOverrideContainingBlockLogicalWidthMap && gOverrideContainingBlockLogicalWidthMap->contains(this);
}
bool RenderBox::hasOverrideContainingBlockLogicalHeight() const
{
return gOverrideContainingBlockLogicalHeightMap && gOverrideContainingBlockLogicalHeightMap->contains(this);
}
void RenderBox::setOverrideContainingBlockContentLogicalWidth(std::optional<LayoutUnit> logicalWidth)
{
if (!gOverrideContainingBlockLogicalWidthMap)
gOverrideContainingBlockLogicalWidthMap = new OverrideOptionalSizeMap;
gOverrideContainingBlockLogicalWidthMap->set(this, logicalWidth);
}
void RenderBox::setOverrideContainingBlockContentLogicalHeight(std::optional<LayoutUnit> logicalHeight)
{
if (!gOverrideContainingBlockLogicalHeightMap)
gOverrideContainingBlockLogicalHeightMap = new OverrideOptionalSizeMap;
gOverrideContainingBlockLogicalHeightMap->set(this, logicalHeight);
}
void RenderBox::clearContainingBlockOverrideSize()
{
if (gOverrideContainingBlockLogicalWidthMap)
gOverrideContainingBlockLogicalWidthMap->remove(this);
clearOverrideContainingBlockContentLogicalHeight();
}
void RenderBox::clearOverrideContainingBlockContentLogicalHeight()
{
if (gOverrideContainingBlockLogicalHeightMap)
gOverrideContainingBlockLogicalHeightMap->remove(this);
}
LayoutUnit RenderBox::adjustBorderBoxLogicalWidthForBoxSizing(LayoutUnit width) const
{
LayoutUnit bordersPlusPadding = borderAndPaddingLogicalWidth();
if (style().boxSizing() == CONTENT_BOX)
return width + bordersPlusPadding;
return std::max(width, bordersPlusPadding);
}
LayoutUnit RenderBox::adjustBorderBoxLogicalHeightForBoxSizing(LayoutUnit height) const
{
LayoutUnit bordersPlusPadding = borderAndPaddingLogicalHeight();
if (style().boxSizing() == CONTENT_BOX)
return height + bordersPlusPadding;
return std::max(height, bordersPlusPadding);
}
LayoutUnit RenderBox::adjustContentBoxLogicalWidthForBoxSizing(LayoutUnit width) const
{
if (style().boxSizing() == BORDER_BOX)
width -= borderAndPaddingLogicalWidth();
return std::max<LayoutUnit>(0, width);
}
LayoutUnit RenderBox::adjustContentBoxLogicalHeightForBoxSizing(std::optional<LayoutUnit> height) const
{
if (!height)
return 0;
LayoutUnit result = height.value();
if (style().boxSizing() == BORDER_BOX)
result -= borderAndPaddingLogicalHeight();
return std::max(LayoutUnit(), result);
}
bool RenderBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction action)
{
LayoutPoint adjustedLocation = accumulatedOffset + location();
for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
if (!child->hasLayer() && child->nodeAtPoint(request, result, locationInContainer, adjustedLocation, action)) {
updateHitTestResult(result, locationInContainer.point() - toLayoutSize(adjustedLocation));
return true;
}
}
RenderFlowThread* flowThread = flowThreadContainingBlock();
RenderRegion* regionToUse = flowThread ? downcast<RenderNamedFlowFragment>(flowThread->currentRegion()) : nullptr;
if (regionToUse && !flowThread->objectShouldFragmentInFlowRegion(this, regionToUse))
return false;
LayoutRect boundsRect = borderBoxRectInRegion(regionToUse);
boundsRect.moveBy(adjustedLocation);
if (visibleToHitTesting() && action == HitTestForeground && locationInContainer.intersects(boundsRect)) {
updateHitTestResult(result, locationInContainer.point() - toLayoutSize(adjustedLocation));
if (!result.addNodeToRectBasedTestResult(element(), request, locationInContainer, boundsRect))
return true;
}
return false;
}
void RenderBox::paintRootBoxFillLayers(const PaintInfo& paintInfo)
{
ASSERT(isDocumentElementRenderer());
if (paintInfo.skipRootBackground())
return;
auto* rootBackgroundRenderer = view().rendererForRootBackground();
if (!rootBackgroundRenderer)
return;
auto& style = rootBackgroundRenderer->style();
auto color = style.visitedDependentColor(CSSPropertyBackgroundColor);
paintFillLayers(paintInfo, color, style.backgroundLayers(), view().backgroundRect(), 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 (contextScaling.width() > 1)
contextScaling.setWidth(1);
if (contextScaling.height() > 1)
contextScaling.setHeight(1);
if (borderObscuresBackgroundEdge(contextScaling))
return BackgroundBleedShrinkBackground;
if (!style.hasAppearance() && borderObscuresBackground() && backgroundHasOpaqueTopLayer())
return BackgroundBleedBackgroundOverBorder;
return BackgroundBleedUseTransparencyLayer;
}
void RenderBox::paintBoxDecorations(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (!paintInfo.shouldPaintWithinRoot(*this))
return;
LayoutRect paintRect = borderBoxRectInRegion(currentRenderNamedFlowFragment());
paintRect.moveBy(paintOffset);
adjustBorderBoxRectForPainting(paintRect);
#if PLATFORM(IOS)
if (style().appearance() == CheckboxPart || style().appearance() == RadioPart) {
int width = std::min(paintRect.width(), paintRect.height());
int height = width;
paintRect = IntRect(paintRect.x(), paintRect.y() + (this->height() - height) / 2, width, height); }
#endif
BackgroundBleedAvoidance bleedAvoidance = determineBackgroundBleedAvoidance(paintInfo.context());
if (!boxShadowShouldBeAppliedToBackground(paintRect.location(), bleedAvoidance))
paintBoxShadow(paintInfo, paintRect, style(), Normal);
GraphicsContextStateSaver stateSaver(paintInfo.context(), false);
if (bleedAvoidance == BackgroundBleedUseTransparencyLayer) {
stateSaver.save();
paintInfo.context().clipRoundedRect(style().getRoundedBorderFor(paintRect).pixelSnappedRoundedRectForPainting(document().deviceScaleFactor()));
paintInfo.context().beginTransparencyLayer(1);
}
bool borderOrBackgroundPaintingIsNeeded = true;
if (style().hasAppearance()) {
ControlStates* controlStates = controlStatesForRenderer(*this);
borderOrBackgroundPaintingIsNeeded = theme().paint(*this, *controlStates, paintInfo, paintRect);
if (controlStates->needsRepaint())
view().scheduleLazyRepaint(*this);
}
if (borderOrBackgroundPaintingIsNeeded) {
if (bleedAvoidance == BackgroundBleedBackgroundOverBorder)
paintBorder(paintInfo, paintRect, style(), bleedAvoidance);
paintBackground(paintInfo, paintRect, bleedAvoidance);
if (style().hasAppearance())
theme().paintDecorations(*this, paintInfo, paintRect);
}
paintBoxShadow(paintInfo, paintRect, style(), Inset);
if (bleedAvoidance != BackgroundBleedBackgroundOverBorder && (!style().hasAppearance() || (borderOrBackgroundPaintingIsNeeded && theme().paintBorderOnly(*this, paintInfo, paintRect))) && style().hasVisibleBorderDecoration())
paintBorder(paintInfo, paintRect, style(), bleedAvoidance);
if (bleedAvoidance == BackgroundBleedUseTransparencyLayer)
paintInfo.context().endTransparencyLayer();
}
bool RenderBox::paintsOwnBackground() const
{
if (isBody()) {
auto documentElementRenderer = document().documentElement()->renderer();
return !documentElementRenderer
|| documentElementRenderer->hasBackground()
|| (documentElementRenderer != parent());
}
return true;
}
void RenderBox::paintBackground(const PaintInfo& paintInfo, const LayoutRect& paintRect, BackgroundBleedAvoidance bleedAvoidance)
{
if (isDocumentElementRenderer()) {
paintRootBoxFillLayers(paintInfo);
return;
}
if (!paintsOwnBackground())
return;
if (backgroundIsKnownToBeObscured(paintRect.location()) && !boxShadowShouldBeAppliedToBackground(paintRect.location(), bleedAvoidance))
return;
paintFillLayers(paintInfo, style().visitedDependentColor(CSSPropertyBackgroundColor), style().backgroundLayers(), paintRect, bleedAvoidance);
}
bool RenderBox::getBackgroundPaintedExtent(const LayoutPoint& paintOffset, LayoutRect& paintedExtent) const
{
ASSERT(hasBackground());
LayoutRect backgroundRect = snappedIntRect(borderBoxRect());
Color backgroundColor = style().visitedDependentColor(CSSPropertyBackgroundColor);
if (backgroundColor.isVisible()) {
paintedExtent = backgroundRect;
return true;
}
auto& layers = style().backgroundLayers();
if (!layers.image() || layers.next()) {
paintedExtent = backgroundRect;
return true;
}
auto geometry = calculateBackgroundImageGeometry(nullptr, layers, paintOffset, backgroundRect);
paintedExtent = geometry.destRect();
return !geometry.hasNonLocalGeometry();
}
bool RenderBox::backgroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect) const
{
if (!paintsOwnBackground())
return false;
Color backgroundColor = style().visitedDependentColor(CSSPropertyBackgroundColor);
if (!backgroundColor.isOpaque())
return false;
if (style().hasAppearance())
return false;
if (hasClip() || hasClipPath())
return false;
if (style().hasBorderRadius())
return false;
if (style().backgroundLayers().next())
return false;
LayoutRect backgroundRect;
switch (style().backgroundClip()) {
case BorderFillBox:
backgroundRect = borderBoxRect();
break;
case PaddingFillBox:
backgroundRect = paddingBoxRect();
break;
case ContentFillBox:
backgroundRect = contentBoxRect();
break;
default:
break;
}
return backgroundRect.contains(localRect);
}
static bool isCandidateForOpaquenessTest(const RenderBox& childBox)
{
const RenderStyle& childStyle = childBox.style();
if (childStyle.position() != StaticPosition && childBox.containingBlock() != childBox.parent())
return false;
if (childStyle.visibility() != VISIBLE)
return false;
if (childStyle.shapeOutside())
return false;
if (!childBox.width() || !childBox.height())
return false;
if (RenderLayer* childLayer = childBox.layer()) {
if (childLayer->isComposited())
return false;
if (!childStyle.hasAutoZIndex())
return false;
if (childLayer->hasTransform() || childLayer->isTransparent() || childLayer->hasFilter())
return false;
if (!childBox.scrollPosition().isZero())
return false;
}
return true;
}
bool RenderBox::foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned maxDepthToTest) const
{
if (!maxDepthToTest)
return false;
for (auto& childBox : childrenOfType<RenderBox>(*this)) {
if (!isCandidateForOpaquenessTest(childBox))
continue;
LayoutPoint childLocation = childBox.location();
if (childBox.isRelPositioned())
childLocation.move(childBox.relativePositionOffset());
LayoutRect childLocalRect = localRect;
childLocalRect.moveBy(-childLocation);
if (childLocalRect.y() < 0 || childLocalRect.x() < 0) {
if (childBox.style().position() == StaticPosition)
return false;
continue;
}
if (childLocalRect.maxY() > childBox.height() || childLocalRect.maxX() > childBox.width())
continue;
if (childBox.backgroundIsKnownToBeOpaqueInRect(childLocalRect))
return true;
if (childBox.foregroundIsKnownToBeOpaqueInRect(childLocalRect, maxDepthToTest - 1))
return true;
}
return false;
}
bool RenderBox::computeBackgroundIsKnownToBeObscured(const LayoutPoint& paintOffset)
{
if (!hasBackground())
return false;
if (isTable() || isDocumentElementRenderer())
return false;
LayoutRect backgroundRect;
if (!getBackgroundPaintedExtent(paintOffset, backgroundRect))
return false;
return foregroundIsKnownToBeOpaqueInRect(backgroundRect, backgroundObscurationTestMaxDepth);
}
bool RenderBox::backgroundHasOpaqueTopLayer() const
{
auto& fillLayer = style().backgroundLayers();
if (fillLayer.clip() != BorderFillBox)
return false;
if (hasOverflowClip() && fillLayer.attachment() == LocalBackgroundAttachment)
return false;
if (fillLayer.hasOpaqueImage(*this) && fillLayer.hasRepeatXY() && fillLayer.image()->canRender(this, style().effectiveZoom()))
return true;
if (!fillLayer.next() && !fillLayer.hasImage()) {
Color bgColor = style().visitedDependentColor(CSSPropertyBackgroundColor);
if (bgColor.isOpaque())
return true;
}
return false;
}
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());
adjustBorderBoxRectForPainting(paintRect);
paintMaskImages(paintInfo, paintRect);
}
void RenderBox::paintClippingMask(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (!paintInfo.shouldPaintWithinRoot(*this) || style().visibility() != VISIBLE || paintInfo.phase != PaintPhaseClippingMask || paintInfo.context().paintingDisabled())
return;
LayoutRect paintRect = LayoutRect(paintOffset, size());
paintInfo.context().fillRect(snappedIntRect(paintRect), Color::black);
}
void RenderBox::paintMaskImages(const PaintInfo& paintInfo, const LayoutRect& paintRect)
{
bool pushTransparencyLayer = false;
bool compositedMask = hasLayer() && layer()->hasCompositedMask();
bool flattenCompositingLayers = paintInfo.paintBehavior & PaintBehaviorFlattenCompositingLayers;
CompositeOperator compositeOp = CompositeSourceOver;
bool allMaskImagesLoaded = true;
if (!compositedMask || flattenCompositingLayers) {
pushTransparencyLayer = true;
if (auto* maskBoxImage = style().maskBoxImage().image())
allMaskImagesLoaded &= maskBoxImage->isLoaded();
allMaskImagesLoaded &= style().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 LayoutPoint& paintOffset)
{
const NinePieceImage& maskBoxImage = style().maskBoxImage();
if (maskBoxImage.image()) {
LayoutRect borderImageRect = borderBoxRect();
borderImageRect.expand(style().maskBoxImageOutsets());
return borderImageRect;
}
LayoutRect result;
LayoutRect borderBox = borderBoxRect();
for (auto* maskLayer = &style().maskLayers(); maskLayer; maskLayer = maskLayer->next()) {
if (maskLayer->image()) {
result.unite(calculateBackgroundImageGeometry(nullptr, *maskLayer, paintOffset, borderBox).destRect());
}
}
return result;
}
void RenderBox::paintFillLayers(const PaintInfo& paintInfo, const Color& color, const FillLayer& fillLayer, const LayoutRect& rect,
BackgroundBleedAvoidance bleedAvoidance, CompositeOperator op, RenderElement* backgroundObject)
{
Vector<const FillLayer*, 8> layers;
bool shouldDrawBackgroundInSeparateBuffer = false;
for (auto* layer = &fillLayer; layer; layer = layer->next()) {
layers.append(layer);
if (layer->blendMode() != BlendModeNormal)
shouldDrawBackgroundInSeparateBuffer = true;
if (layer->clipOccludesNextLayers(layer == &fillLayer) && layer->hasOpaqueImage(*this) && layer->image()->canRender(this, style().effectiveZoom()) && layer->hasRepeatXY() && layer->blendMode() == BlendModeNormal)
break;
}
auto& context = paintInfo.context();
auto baseBgColorUsage = BaseBackgroundColorUse;
if (shouldDrawBackgroundInSeparateBuffer) {
paintFillLayer(paintInfo, color, *layers.last(), rect, bleedAvoidance, op, backgroundObject, BaseBackgroundColorOnly);
baseBgColorUsage = BaseBackgroundColorSkip;
context.beginTransparencyLayer(1);
}
auto topLayer = layers.rend();
for (auto it = layers.rbegin(); it != topLayer; ++it)
paintFillLayer(paintInfo, color, **it, rect, bleedAvoidance, op, backgroundObject, baseBgColorUsage);
if (shouldDrawBackgroundInSeparateBuffer)
context.endTransparencyLayer();
}
void RenderBox::paintFillLayer(const PaintInfo& paintInfo, const Color& c, const FillLayer& fillLayer, const LayoutRect& rect,
BackgroundBleedAvoidance bleedAvoidance, CompositeOperator op, RenderElement* backgroundObject, BaseBackgroundColorUsage baseBgColorUsage)
{
paintFillLayerExtended(paintInfo, c, fillLayer, rect, bleedAvoidance, nullptr, LayoutSize(), op, backgroundObject, baseBgColorUsage);
}
static bool layersUseImage(WrappedImagePtr image, const FillLayer& layers)
{
for (auto* layer = &layers; layer; layer = layer->next()) {
if (layer->image() && image == layer->image()->data())
return true;
}
return false;
}
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;
}
ShapeValue* shapeOutsideValue = style().shapeOutside();
if (!view().frameView().isInRenderTreeLayout() && isFloating() && shapeOutsideValue && shapeOutsideValue->image() && shapeOutsideValue->image()->data() == image) {
ShapeOutsideInfo::ensureInfo(*this).markShapeAsDirty();
markShapeOutsideDependentsForLayout();
}
bool didFullRepaint = repaintLayerRectsForImage(image, style().backgroundLayers(), true);
if (!didFullRepaint)
repaintLayerRectsForImage(image, style().maskLayers(), false);
if (!isComposited())
return;
if (layer()->hasCompositedMask() && layersUseImage(image, style().maskLayers()))
layer()->contentChanged(MaskImageChanged);
if (layersUseImage(image, style().backgroundLayers()))
layer()->contentChanged(BackgroundImageChanged);
}
bool RenderBox::repaintLayerRectsForImage(WrappedImagePtr image, const FillLayer& layers, bool drawingBackground)
{
LayoutRect rendererRect;
RenderBox* layerRenderer = nullptr;
for (auto* layer = &layers; layer; layer = layer->next()) {
if (layer->image() && image == layer->image()->data() && layer->image()->canRender(this, style().effectiveZoom())) {
bool drawingRootBackground = drawingBackground && (isDocumentElementRenderer() || (isBody() && !document().documentElement()->renderer()->hasBackground()));
if (!layerRenderer) {
if (drawingRootBackground) {
layerRenderer = &view();
LayoutUnit rw = downcast<RenderView>(*layerRenderer).frameView().contentsWidth();
LayoutUnit rh = downcast<RenderView>(*layerRenderer).frameView().contentsHeight();
rendererRect = LayoutRect(-layerRenderer->marginLeft(),
-layerRenderer->marginTop(),
std::max(layerRenderer->width() + layerRenderer->horizontalMarginExtent() + layerRenderer->borderLeft() + layerRenderer->borderRight(), rw),
std::max(layerRenderer->height() + layerRenderer->verticalMarginExtent() + layerRenderer->borderTop() + layerRenderer->borderBottom(), rh));
} else {
layerRenderer = this;
rendererRect = borderBoxRect();
}
}
BackgroundImageGeometry geometry = layerRenderer->calculateBackgroundImageGeometry(nullptr, *layer, LayoutPoint(), rendererRect);
if (geometry.hasNonLocalGeometry()) {
layerRenderer->repaint();
return true;
}
LayoutRect rectToRepaint = geometry.destRect();
bool shouldClipToLayer = true;
if (drawingRootBackground && view().frameView().hasExtendedBackgroundRectForPainting()) {
shouldClipToLayer = false;
IntRect extendedBackgroundRect = view().frameView().extendedBackgroundRectForPainting();
if (rectToRepaint.width() == rendererRect.width()) {
rectToRepaint.move(extendedBackgroundRect.x(), 0);
rectToRepaint.setWidth(extendedBackgroundRect.width());
}
if (rectToRepaint.height() == rendererRect.height()) {
rectToRepaint.move(0, extendedBackgroundRect.y());
rectToRepaint.setHeight(extendedBackgroundRect.height());
}
}
layerRenderer->repaintRectangle(rectToRepaint, shouldClipToLayer);
if (geometry.destRect() == rendererRect)
return true;
}
}
return false;
}
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;
}
float deviceScaleFactor = document().deviceScaleFactor();
FloatRect clipRect = snapRectToDevicePixels((isControlClip ? controlClipRect(accumulatedOffset) : overflowClipRect(accumulatedOffset, currentRenderNamedFlowFragment(), IgnoreOverlayScrollbarSize, paintInfo.phase)), deviceScaleFactor);
paintInfo.context().save();
if (style().hasBorderRadius())
paintInfo.context().clipRoundedRect(style().getRoundedInnerBorderFor(LayoutRect(accumulatedOffset, size())).pixelSnappedRoundedRectForPainting(deviceScaleFactor));
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, PaintPhase)
{
LayoutRect clipRect = borderBoxRectInRegion(region);
clipRect.setLocation(location + clipRect.location() + LayoutSize(borderLeft(), borderTop()));
clipRect.setSize(clipRect.size() - LayoutSize(borderLeft() + borderRight(), borderTop() + borderBottom()));
if (layer()) {
if (shouldPlaceBlockDirectionScrollbarOnLeft())
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());
if (!style().clipLeft().isAuto()) {
LayoutUnit c = valueForLength(style().clipLeft(), borderBoxRect.width());
clipRect.move(c, 0);
clipRect.contract(c, 0);
}
if (!style().clipRight().isAuto())
clipRect.contract(width() - valueForLength(style().clipRight(), width()), 0);
if (!style().clipTop().isAuto()) {
LayoutUnit c = valueForLength(style().clipTop(), borderBoxRect.height());
clipRect.move(0, c);
clipRect.contract(0, c);
}
if (!style().clipBottom().isAuto())
clipRect.contract(0, height() - valueForLength(style().clipBottom(), height()));
return clipRect;
}
LayoutUnit RenderBox::shrinkLogicalWidthToAvoidFloats(LayoutUnit childMarginStart, LayoutUnit childMarginEnd, const RenderBlock& cb, RenderRegion* region) const
{
RenderRegion* containingBlockRegion = nullptr;
LayoutUnit logicalTopPosition = logicalTop();
if (region) {
LayoutUnit offsetFromLogicalTopOfRegion = region ? region->logicalTopForFlowThreadContent() - offsetFromLogicalTopOfFirstPage() : LayoutUnit();
logicalTopPosition = std::max(logicalTopPosition, logicalTopPosition + offsetFromLogicalTopOfRegion);
containingBlockRegion = cb.clampToStartAndEndRegions(region);
}
LayoutUnit logicalHeight = cb.logicalHeightForChild(*this);
LayoutUnit result = cb.availableLogicalWidthForLineInRegion(logicalTopPosition, DoNotIndentText, containingBlockRegion, logicalHeight) - childMarginStart - childMarginEnd;
if (childMarginStart > 0) {
LayoutUnit startContentSide = cb.startOffsetForContent(containingBlockRegion);
LayoutUnit startContentSideWithMargin = startContentSide + childMarginStart;
LayoutUnit startOffset = cb.startOffsetForLineInRegion(logicalTopPosition, DoNotIndentText, containingBlockRegion, logicalHeight);
if (startOffset > startContentSideWithMargin)
result += childMarginStart;
else
result += startOffset - startContentSide;
}
if (childMarginEnd > 0) {
LayoutUnit endContentSide = cb.endOffsetForContent(containingBlockRegion);
LayoutUnit endContentSideWithMargin = endContentSide + childMarginEnd;
LayoutUnit endOffset = cb.endOffsetForLineInRegion(logicalTopPosition, DoNotIndentText, containingBlockRegion, logicalHeight);
if (endOffset > endContentSideWithMargin)
result += childMarginEnd;
else
result += endOffset - endContentSide;
}
return result;
}
LayoutUnit RenderBox::containingBlockLogicalWidthForContent() const
{
if (hasOverrideContainingBlockLogicalWidth()) {
if (auto overrideLogicalWidth = overrideContainingBlockContentLogicalWidth())
return overrideLogicalWidth.value();
}
if (RenderBlock* cb = containingBlock())
return cb->availableLogicalWidth();
return LayoutUnit();
}
LayoutUnit RenderBox::containingBlockLogicalHeightForContent(AvailableLogicalHeightType heightType) const
{
if (hasOverrideContainingBlockLogicalHeight()) {
if (auto overrideLogicalHeight = overrideContainingBlockContentLogicalHeight())
return overrideLogicalHeight.value();
}
if (RenderBlock* cb = containingBlock())
return cb->availableLogicalHeight(heightType);
return LayoutUnit();
}
LayoutUnit RenderBox::containingBlockLogicalWidthForContentInRegion(RenderRegion* region) const
{
if (!region)
return containingBlockLogicalWidthForContent();
RenderBlock* cb = containingBlock();
RenderRegion* containingBlockRegion = cb->clampToStartAndEndRegions(region);
LayoutUnit result = cb->availableLogicalWidth();
RenderBoxRegionInfo* boxInfo = cb->renderBoxRegionInfo(containingBlockRegion);
if (!boxInfo)
return result;
return std::max<LayoutUnit>(0, result - (cb->logicalWidth() - boxInfo->logicalWidth()));
}
LayoutUnit RenderBox::containingBlockAvailableLineWidthInRegion(RenderRegion* region) const
{
RenderBlock* cb = containingBlock();
RenderRegion* containingBlockRegion = nullptr;
LayoutUnit logicalTopPosition = logicalTop();
if (region) {
LayoutUnit offsetFromLogicalTopOfRegion = region ? region->logicalTopForFlowThreadContent() - offsetFromLogicalTopOfFirstPage() : LayoutUnit();
logicalTopPosition = std::max(logicalTopPosition, logicalTopPosition + offsetFromLogicalTopOfRegion);
containingBlockRegion = cb->clampToStartAndEndRegions(region);
}
return cb->availableLogicalWidthForLineInRegion(logicalTopPosition, DoNotIndentText, containingBlockRegion, availableLogicalHeight(IncludeMarginBorderPadding));
}
LayoutUnit RenderBox::perpendicularContainingBlockLogicalHeight() const
{
if (hasOverrideContainingBlockLogicalHeight()) {
if (auto overrideLogicalHeight = overrideContainingBlockContentLogicalHeight())
return overrideLogicalHeight.value();
}
RenderBlock* cb = containingBlock();
if (cb->hasOverrideLogicalContentHeight())
return cb->overrideLogicalContentHeight();
const RenderStyle& containingBlockStyle = cb->style();
Length logicalHeightLength = containingBlockStyle.logicalHeight();
if (!logicalHeightLength.isFixed()) {
LayoutUnit fillFallbackExtent = containingBlockStyle.isHorizontalWritingMode() ? view().frameView().visibleHeight() : view().frameView().visibleWidth();
LayoutUnit fillAvailableExtent = containingBlock()->availableLogicalHeight(ExcludeMarginBorderPadding);
view().addPercentHeightDescendant(const_cast<RenderBox&>(*this));
return std::min(fillAvailableExtent, fillFallbackExtent);
}
return cb->adjustContentBoxLogicalHeightForBoxSizing(LayoutUnit(logicalHeightLength.value()));
}
void RenderBox::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags mode, bool* wasFixed) const
{
if (repaintContainer == this)
return;
if (view().layoutStateEnabled() && !repaintContainer) {
LayoutState* layoutState = view().layoutState();
LayoutSize offset = layoutState->m_paintOffset + locationOffset();
if (style().hasInFlowPosition() && layer())
offset += layer()->offsetForInFlowPosition();
transformState.move(offset);
return;
}
bool containerSkipped;
RenderElement* container = this->container(repaintContainer, containerSkipped);
if (!container)
return;
bool isFixedPos = style().position() == FixedPosition;
if (hasTransform() && !isFixedPos)
mode &= ~IsFixed;
else if (isFixedPos)
mode |= IsFixed;
if (wasFixed)
*wasFixed = mode & IsFixed;
LayoutSize containerOffset = offsetFromContainer(*container, LayoutPoint(transformState.mappedPoint()));
bool preserve3D = mode & UseTransforms && (container->style().preserves3D() || style().preserves3D());
if (mode & UseTransforms && shouldUseTransformFromContainer(container)) {
TransformationMatrix t;
getTransformFromContainer(container, 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(*container);
transformState.move(-containerOffset.width(), -containerOffset.height(), preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
return;
}
mode &= ~ApplyContainerFlip;
if (!container->isOutOfFlowRenderFlowThread() || !fixedPositionedWithNamedFlowContainingBlock())
container->mapLocalToContainer(repaintContainer, transformState, mode, wasFixed);
else
container->mapLocalToContainer(downcast<RenderLayerModelObject>(container), transformState, mode, wasFixed);
}
const RenderObject* RenderBox::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
{
ASSERT(ancestorToStopAt != this);
bool ancestorSkipped;
RenderElement* container = this->container(ancestorToStopAt, ancestorSkipped);
if (!container)
return nullptr;
bool isFixedPos = style().position() == FixedPosition;
LayoutSize adjustmentForSkippedAncestor;
if (ancestorSkipped) {
adjustmentForSkippedAncestor = -ancestorToStopAt->offsetFromAncestorContainer(*container);
}
bool offsetDependsOnPoint = false;
LayoutSize containerOffset = offsetFromContainer(*container, LayoutPoint(), &offsetDependsOnPoint);
bool preserve3D = container->style().preserves3D() || style().preserves3D();
if (shouldUseTransformFromContainer(container) && (geometryMap.mapCoordinatesFlags() & UseTransforms)) {
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(MapCoordinatesFlags mode, TransformState& transformState) const
{
bool isFixedPos = style().position() == FixedPosition;
if (hasTransform() && !isFixedPos) {
mode &= ~IsFixed;
} else if (isFixedPos)
mode |= IsFixed;
RenderBoxModelObject::mapAbsoluteToLocalPoint(mode, transformState);
}
LayoutSize RenderBox::offsetFromContainer(RenderElement& renderer, const LayoutPoint&, bool* offsetDependsOnPoint) const
{
ASSERT(&renderer == container() || is<RenderRegion>(renderer));
LayoutSize offset;
if (isInFlowPositioned())
offset += offsetForInFlowPosition();
if (!isInline() || isReplaced())
offset += topLeftLocationOffset();
if (is<RenderBox>(renderer))
offset -= toLayoutSize(downcast<RenderBox>(renderer).scrollPosition());
if (style().position() == AbsolutePosition && renderer.isInFlowPositioned() && is<RenderInline>(renderer))
offset += downcast<RenderInline>(renderer).offsetForInFlowPositionedInline(this);
if (offsetDependsOnPoint)
*offsetDependsOnPoint |= is<RenderFlowThread>(renderer);
return offset;
}
std::unique_ptr<InlineElementBox> RenderBox::createInlineBox()
{
return std::make_unique<InlineElementBox>(*this);
}
void RenderBox::dirtyLineBoxes(bool fullLayout)
{
if (!m_inlineBoxWrapper)
return;
if (fullLayout) {
delete m_inlineBoxWrapper;
m_inlineBoxWrapper = nullptr;
} else
m_inlineBoxWrapper->dirtyLineBoxes();
}
void RenderBox::positionLineBox(InlineElementBox& box)
{
if (isOutOfFlowPositioned()) {
bool wasInline = style().isOriginalDisplayInlineType();
if (wasInline) {
RootInlineBox& rootBox = box.root();
rootBox.blockFlow().setStaticInlinePositionForChild(*this, rootBox.lineTopWithLeading(), LayoutUnit::fromFloatRound(box.logicalLeft()));
if (style().hasStaticInlinePosition(box.isHorizontal()))
setChildNeedsLayout(MarkOnlyThis); } else {
layer()->setStaticBlockPosition(box.logicalTop());
if (style().hasStaticBlockPosition(box.isHorizontal()))
setChildNeedsLayout(MarkOnlyThis); }
return;
}
if (isReplaced()) {
setLocation(LayoutPoint(box.topLeft()));
setInlineBoxWrapper(&box);
}
}
void RenderBox::deleteLineBoxWrapper()
{
if (!m_inlineBoxWrapper)
return;
if (!renderTreeBeingDestroyed())
m_inlineBoxWrapper->removeFromParent();
delete m_inlineBoxWrapper;
m_inlineBoxWrapper = nullptr;
}
LayoutRect RenderBox::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
{
if (style().visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
return LayoutRect();
LayoutRect r = visualOverflowRect();
r.move(view().layoutDelta());
return computeRectForRepaint(r, repaintContainer);
}
bool RenderBox::shouldApplyClipAndScrollPositionForRepaint(const RenderLayerModelObject* repaintContainer) const
{
#if PLATFORM(IOS)
if (!repaintContainer || repaintContainer != this)
return true;
return !hasLayer() || !layer()->usesCompositedScrolling();
#else
UNUSED_PARAM(repaintContainer);
return true;
#endif
}
LayoutRect RenderBox::computeRectForRepaint(const LayoutRect& rect, const RenderLayerModelObject* repaintContainer, RepaintContext context) const
{
LayoutRect adjustedRect = rect;
const RenderStyle& styleToUse = style();
if (view().layoutStateEnabled() && !repaintContainer && styleToUse.position() != FixedPosition) {
LayoutState* layoutState = view().layoutState();
if (layer() && layer()->transform())
adjustedRect = LayoutRect(encloseRectToDevicePixels(layer()->transform()->mapRect(adjustedRect), document().deviceScaleFactor()));
if (styleToUse.hasInFlowPosition() && layer())
adjustedRect.move(layer()->offsetForInFlowPosition());
adjustedRect.moveBy(location());
adjustedRect.move(layoutState->m_paintOffset);
if (layoutState->m_clipped)
adjustedRect.intersect(layoutState->m_clipRect);
return adjustedRect;
}
if (hasReflection())
adjustedRect.unite(reflectedRect(adjustedRect));
if (repaintContainer == this) {
if (repaintContainer->style().isFlippedBlocksWritingMode())
flipForWritingMode(adjustedRect);
return adjustedRect;
}
bool repaintContainerIsSkipped;
auto* container = this->container(repaintContainer, repaintContainerIsSkipped);
if (!container)
return adjustedRect;
EPosition position = styleToUse.position();
if (container->isOutOfFlowRenderFlowThread() && position != AbsolutePosition && containingBlock() != flowThreadContainingBlock()) {
RenderRegion* firstRegion = nullptr;
RenderRegion* lastRegion = nullptr;
if (downcast<RenderFlowThread>(*container).getRegionRangeForBox(this, firstRegion, lastRegion))
adjustedRect.moveBy(firstRegion->flowThreadPortionRect().location());
}
if (isWritingModeRoot()) {
if (!isOutOfFlowPositioned() || !context.m_dirtyRectIsFlipped) {
flipForWritingMode(adjustedRect);
context.m_dirtyRectIsFlipped = true;
}
}
LayoutSize locationOffset = this->locationOffset();
if (isRenderReplaced() && isWidget()) {
LayoutSize flooredLocationOffset = toIntSize(flooredIntPoint(locationOffset));
adjustedRect.expand(locationOffset - flooredLocationOffset);
locationOffset = flooredLocationOffset;
}
if (is<RenderMultiColumnFlowThread>(this)) {
LayoutPoint physicalPoint(flipForWritingMode(adjustedRect.location()));
if (auto* region = downcast<RenderMultiColumnFlowThread>(*this).physicalTranslationFromFlowToRegion((physicalPoint))) {
adjustedRect.setLocation(region->flipForWritingMode(physicalPoint));
return region->computeRectForRepaint(adjustedRect, repaintContainer, context);
}
}
LayoutPoint topLeft = adjustedRect.location();
topLeft.move(locationOffset);
if (hasLayer() && layer()->transform()) {
context.m_hasPositionFixedDescendant = position == FixedPosition;
adjustedRect = LayoutRect(encloseRectToDevicePixels(layer()->transform()->mapRect(adjustedRect), document().deviceScaleFactor()));
topLeft = adjustedRect.location();
topLeft.move(locationOffset);
} else if (position == FixedPosition)
context.m_hasPositionFixedDescendant = true;
if (position == AbsolutePosition && container->isInFlowPositioned() && is<RenderInline>(*container))
topLeft += downcast<RenderInline>(*container).offsetForInFlowPositionedInline(this);
else if (styleToUse.hasInFlowPosition() && layer()) {
topLeft += layer()->offsetForInFlowPosition();
}
adjustedRect.setLocation(topLeft);
if (container->hasOverflowClip()) {
RenderBox& containerBox = downcast<RenderBox>(*container);
if (containerBox.shouldApplyClipAndScrollPositionForRepaint(repaintContainer)) {
containerBox.applyCachedClipAndScrollPositionForRepaint(adjustedRect);
if (adjustedRect.isEmpty())
return adjustedRect;
}
}
if (repaintContainerIsSkipped) {
LayoutSize containerOffset = repaintContainer->offsetFromAncestorContainer(*container);
adjustedRect.move(-containerOffset);
return adjustedRect;
}
return container->computeRectForRepaint(adjustedRect, repaintContainer, context);
}
void RenderBox::repaintDuringLayoutIfMoved(const LayoutRect& oldRect)
{
if (oldRect.location() != m_frameRect.location()) {
LayoutRect newRect = m_frameRect;
m_frameRect = oldRect;
repaint();
repaintOverhangingFloats(true);
m_frameRect = newRect;
repaint();
repaintOverhangingFloats(true);
}
}
void RenderBox::repaintOverhangingFloats(bool)
{
}
void RenderBox::updateLogicalWidth()
{
LogicalExtentComputedValues computedValues;
computeLogicalWidthInRegion(computedValues);
setLogicalWidth(computedValues.m_extent);
setLogicalLeft(computedValues.m_position);
setMarginStart(computedValues.m_margins.m_start);
setMarginEnd(computedValues.m_margins.m_end);
}
void RenderBox::computeLogicalWidthInRegion(LogicalExtentComputedValues& computedValues, RenderRegion* region) const
{
computedValues.m_extent = logicalWidth();
computedValues.m_position = logicalLeft();
computedValues.m_margins.m_start = marginStart();
computedValues.m_margins.m_end = marginEnd();
if (isOutOfFlowPositioned()) {
computePositionedLogicalWidth(computedValues, region);
return;
}
if (element() && !view().frameView().layoutPending() && view().frameView().layoutRoot() == this)
return;
if (hasOverrideLogicalContentWidth() && (isRubyRun() || style().borderFit() == BorderFitLines || (parent()->isFlexibleBoxIncludingDeprecated()))) {
computedValues.m_extent = overrideLogicalContentWidth() + borderAndPaddingLogicalWidth();
return;
}
bool inVerticalBox = parent()->isDeprecatedFlexibleBox() && (parent()->style().boxOrient() == VERTICAL);
bool stretching = (parent()->style().boxAlign() == BSTRETCH);
bool treatAsReplaced = shouldComputeSizeAsReplaced() && (!inVerticalBox || !stretching);
treatAsReplaced = treatAsReplaced && (!isGridItem() || !hasStretchedLogicalWidth());
const RenderStyle& styleToUse = style();
Length logicalWidthLength = treatAsReplaced ? Length(computeReplacedLogicalWidth(), Fixed) : styleToUse.logicalWidth();
RenderBlock& cb = *containingBlock();
LayoutUnit containerLogicalWidth = std::max<LayoutUnit>(0, containingBlockLogicalWidthForContentInRegion(region));
bool hasPerpendicularContainingBlock = cb.isHorizontalWritingMode() != isHorizontalWritingMode();
if (isInline() && !isInlineBlockOrInlineTable()) {
computedValues.m_margins.m_start = minimumValueForLength(styleToUse.marginStart(), containerLogicalWidth);
computedValues.m_margins.m_end = minimumValueForLength(styleToUse.marginEnd(), containerLogicalWidth);
if (treatAsReplaced)
computedValues.m_extent = std::max<LayoutUnit>(floatValueForLength(logicalWidthLength, 0) + borderAndPaddingLogicalWidth(), minPreferredLogicalWidth());
return;
}
LayoutUnit containerWidthInInlineDirection = containerLogicalWidth;
if (hasPerpendicularContainingBlock)
containerWidthInInlineDirection = perpendicularContainingBlockLogicalHeight();
if (treatAsReplaced) {
computedValues.m_extent = logicalWidthLength.value() + borderAndPaddingLogicalWidth();
} else {
LayoutUnit preferredWidth = computeLogicalWidthInRegionUsing(MainOrPreferredSize, styleToUse.logicalWidth(), containerWidthInInlineDirection, cb, region);
computedValues.m_extent = constrainLogicalWidthInRegionByMinMax(preferredWidth, containerWidthInInlineDirection, cb, region);
}
if (hasPerpendicularContainingBlock || isFloating() || isInline()) {
computedValues.m_margins.m_start = minimumValueForLength(styleToUse.marginStart(), containerLogicalWidth);
computedValues.m_margins.m_end = minimumValueForLength(styleToUse.marginEnd(), containerLogicalWidth);
} else {
LayoutUnit containerLogicalWidthForAutoMargins = containerLogicalWidth;
if (avoidsFloats() && cb.containsFloats())
containerLogicalWidthForAutoMargins = containingBlockAvailableLineWidthInRegion(region);
bool hasInvertedDirection = cb.style().isLeftToRightDirection() != style().isLeftToRightDirection();
computeInlineDirectionMargins(cb, containerLogicalWidthForAutoMargins, computedValues.m_extent,
hasInvertedDirection ? computedValues.m_margins.m_end : computedValues.m_margins.m_start,
hasInvertedDirection ? computedValues.m_margins.m_start : computedValues.m_margins.m_end);
}
if (!hasPerpendicularContainingBlock && containerLogicalWidth && containerLogicalWidth != (computedValues.m_extent + computedValues.m_margins.m_start + computedValues.m_margins.m_end)
&& !isFloating() && !isInline() && !cb.isFlexibleBoxIncludingDeprecated()
#if ENABLE(MATHML)
&& !cb.isRenderMathMLBlock()
#endif
&& !cb.isRenderGrid()
) {
LayoutUnit newMargin = containerLogicalWidth - computedValues.m_extent - cb.marginStartForChild(*this);
bool hasInvertedDirection = cb.style().isLeftToRightDirection() != style().isLeftToRightDirection();
if (hasInvertedDirection)
computedValues.m_margins.m_start = newMargin;
else
computedValues.m_margins.m_end = newMargin;
}
}
LayoutUnit RenderBox::fillAvailableMeasure(LayoutUnit availableLogicalWidth) const
{
LayoutUnit marginStart = 0;
LayoutUnit marginEnd = 0;
return fillAvailableMeasure(availableLogicalWidth, marginStart, marginEnd);
}
LayoutUnit RenderBox::fillAvailableMeasure(LayoutUnit availableLogicalWidth, LayoutUnit& marginStart, LayoutUnit& marginEnd) const
{
marginStart = minimumValueForLength(style().marginStart(), availableLogicalWidth);
marginEnd = minimumValueForLength(style().marginEnd(), availableLogicalWidth);
return availableLogicalWidth - marginStart - marginEnd;
}
LayoutUnit RenderBox::computeIntrinsicLogicalWidthUsing(Length logicalWidthLength, LayoutUnit availableLogicalWidth, LayoutUnit borderAndPadding) const
{
if (logicalWidthLength.type() == FillAvailable)
return std::max(borderAndPadding, fillAvailableMeasure(availableLogicalWidth));
LayoutUnit minLogicalWidth = 0;
LayoutUnit maxLogicalWidth = 0;
computeIntrinsicLogicalWidths(minLogicalWidth, maxLogicalWidth);
if (logicalWidthLength.type() == MinContent)
return minLogicalWidth + borderAndPadding;
if (logicalWidthLength.type() == MaxContent)
return maxLogicalWidth + borderAndPadding;
if (logicalWidthLength.type() == FitContent) {
minLogicalWidth += borderAndPadding;
maxLogicalWidth += borderAndPadding;
return std::max(minLogicalWidth, std::min(maxLogicalWidth, fillAvailableMeasure(availableLogicalWidth)));
}
ASSERT_NOT_REACHED();
return 0;
}
LayoutUnit RenderBox::computeLogicalWidthInRegionUsing(SizeType widthType, Length logicalWidth, LayoutUnit availableLogicalWidth,
const RenderBlock& cb, RenderRegion* region) const
{
ASSERT(widthType == MinSize || widthType == MainOrPreferredSize || !logicalWidth.isAuto());
if (widthType == MinSize && logicalWidth.isAuto())
return adjustBorderBoxLogicalWidthForBoxSizing(0);
if (!logicalWidth.isIntrinsicOrAuto()) {
return adjustBorderBoxLogicalWidthForBoxSizing(valueForLength(logicalWidth, availableLogicalWidth));
}
if (logicalWidth.isIntrinsic())
return computeIntrinsicLogicalWidthUsing(logicalWidth, availableLogicalWidth, borderAndPaddingLogicalWidth());
LayoutUnit marginStart = 0;
LayoutUnit marginEnd = 0;
LayoutUnit logicalWidthResult = fillAvailableMeasure(availableLogicalWidth, marginStart, marginEnd);
if (shrinkToAvoidFloats() && cb.containsFloats())
logicalWidthResult = std::min(logicalWidthResult, shrinkLogicalWidthToAvoidFloats(marginStart, marginEnd, cb, region));
if (widthType == MainOrPreferredSize && sizesLogicalWidthToFitContent(widthType))
return std::max(minPreferredLogicalWidth(), std::min(maxPreferredLogicalWidth(), logicalWidthResult));
return logicalWidthResult;
}
bool RenderBox::columnFlexItemHasStretchAlignment() const
{
const auto& parentStyle = parent()->style();
ASSERT(parentStyle.isColumnFlexDirection());
if (style().marginStart().isAuto() || style().marginEnd().isAuto())
return false;
return style().resolvedAlignSelf(&parentStyle, containingBlock()->selfAlignmentNormalBehavior()).position() == ItemPositionStretch;
}
bool RenderBox::isStretchingColumnFlexItem() const
{
if (parent()->isDeprecatedFlexibleBox() && parent()->style().boxOrient() == VERTICAL && parent()->style().boxAlign() == BSTRETCH)
return true;
if (parent()->isFlexibleBox() && parent()->style().flexWrap() == FlexNoWrap && parent()->style().isColumnFlexDirection() && columnFlexItemHasStretchAlignment())
return true;
return false;
}
bool RenderBox::hasStretchedLogicalWidth() const
{
auto& style = this->style();
if (!style.logicalWidth().isAuto() || style.marginStart().isAuto() || style.marginEnd().isAuto())
return false;
RenderBlock* containingBlock = this->containingBlock();
if (!containingBlock) {
return false;
}
if (containingBlock->isHorizontalWritingMode() != isHorizontalWritingMode())
return style.resolvedAlignSelf(&containingBlock->style(), containingBlock->selfAlignmentNormalBehavior(this)).position() == ItemPositionStretch;
return style.resolvedJustifySelf(&containingBlock->style(), containingBlock->selfAlignmentNormalBehavior(this)).position() == ItemPositionStretch;
}
bool RenderBox::sizesLogicalWidthToFitContent(SizeType widthType) const
{
if (isAnonymousInlineBlock())
return false;
if (isFloating() || (isInlineBlockOrInlineTable() && !isHTMLMarquee()))
return true;
if (isGridItem())
return !hasStretchedLogicalWidth();
Length logicalWidth = (widthType == MaxSize) ? style().logicalMaxWidth() : style().logicalWidth();
if (logicalWidth.type() == Intrinsic)
return true;
if (parent()->isHTMLMarquee()) {
EMarqueeDirection dir = parent()->style().marqueeDirection();
if (dir == MAUTO || dir == MFORWARD || dir == MBACKWARD || dir == MLEFT || dir == MRIGHT)
return true;
}
#if ENABLE(MATHML)
if (parent()->isRenderMathMLBlock())
return true;
#endif
if (parent()->isFlexibleBox()) {
if (!parent()->style().isColumnFlexDirection() || parent()->style().flexWrap() != FlexNoWrap)
return true;
if (!columnFlexItemHasStretchAlignment())
return true;
}
if (parent()->isDeprecatedFlexibleBox() && (parent()->style().boxOrient() == HORIZONTAL || parent()->style().boxAlign() != BSTRETCH))
return true;
if (logicalWidth.type() == Auto && !isStretchingColumnFlexItem() && element() && (is<HTMLInputElement>(*element()) || is<HTMLSelectElement>(*element()) || is<HTMLButtonElement>(*element()) || is<HTMLTextAreaElement>(*element()) || is<HTMLLegendElement>(*element())))
return true;
if (isHorizontalWritingMode() != containingBlock()->isHorizontalWritingMode())
return true;
return false;
}
void RenderBox::computeInlineDirectionMargins(const RenderBlock& containingBlock, LayoutUnit containerWidth, LayoutUnit childWidth, LayoutUnit& marginStart, LayoutUnit& marginEnd) const
{
const RenderStyle& containingBlockStyle = containingBlock.style();
Length marginStartLength = style().marginStartUsing(&containingBlockStyle);
Length marginEndLength = style().marginEndUsing(&containingBlockStyle);
if (isFloating() || isInline()) {
marginStart = minimumValueForLength(marginStartLength, containerWidth);
marginEnd = minimumValueForLength(marginEndLength, containerWidth);
return;
}
if (containingBlock.isFlexibleBox()) {
if (marginStartLength.isAuto())
marginStartLength = Length(0, Fixed);
if (marginEndLength.isAuto())
marginEndLength = Length(0, Fixed);
}
if ((marginStartLength.isAuto() && marginEndLength.isAuto() && childWidth < containerWidth)
|| (!marginStartLength.isAuto() && !marginEndLength.isAuto() && containingBlock.style().textAlign() == WEBKIT_CENTER)) {
LayoutUnit marginStartWidth = minimumValueForLength(marginStartLength, containerWidth);
LayoutUnit marginEndWidth = minimumValueForLength(marginEndLength, containerWidth);
LayoutUnit centeredMarginBoxStart = std::max<LayoutUnit>(0, (containerWidth - childWidth - marginStartWidth - marginEndWidth) / 2);
marginStart = centeredMarginBoxStart + marginStartWidth;
marginEnd = containerWidth - childWidth - marginStart + marginEndWidth;
return;
}
if (marginEndLength.isAuto() && childWidth < containerWidth) {
marginStart = valueForLength(marginStartLength, containerWidth);
marginEnd = containerWidth - childWidth - marginStart;
return;
}
bool pushToEndFromTextAlign = !marginEndLength.isAuto() && ((!containingBlockStyle.isLeftToRightDirection() && containingBlockStyle.textAlign() == WEBKIT_LEFT)
|| (containingBlockStyle.isLeftToRightDirection() && containingBlockStyle.textAlign() == WEBKIT_RIGHT));
if ((marginStartLength.isAuto() || pushToEndFromTextAlign) && childWidth < containerWidth) {
marginEnd = valueForLength(marginEndLength, containerWidth);
marginStart = containerWidth - childWidth - marginEnd;
return;
}
marginStart = minimumValueForLength(marginStartLength, containerWidth);
marginEnd = minimumValueForLength(marginEndLength, containerWidth);
}
RenderBoxRegionInfo* RenderBox::renderBoxRegionInfo(RenderRegion* region, RenderBoxRegionInfoFlags cacheFlag) const
{
if (!region)
return nullptr;
RenderBoxRegionInfo* boxInfo = region->renderBoxRegionInfo(this);
if (boxInfo && cacheFlag == CacheRenderBoxRegionInfo)
return boxInfo;
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (isRenderFlowThread() || !is<RenderNamedFlowThread>(flowThread) || !canHaveBoxInfoInRegion() || flowThread->style().writingMode() != style().writingMode())
return nullptr;
LogicalExtentComputedValues computedValues;
computeLogicalWidthInRegion(computedValues, region);
RenderBlock& cb = *containingBlock();
RenderRegion* clampedContainingBlockRegion = cb.clampToStartAndEndRegions(region);
RenderBoxRegionInfo* containingBlockInfo = cb.renderBoxRegionInfo(clampedContainingBlockRegion);
LayoutUnit containingBlockLogicalWidth = cb.logicalWidth();
LayoutUnit containingBlockLogicalWidthInRegion = containingBlockInfo ? containingBlockInfo->logicalWidth() : containingBlockLogicalWidth;
LayoutUnit marginStartInRegion = computedValues.m_margins.m_start;
LayoutUnit startMarginDelta = marginStartInRegion - marginStart();
LayoutUnit logicalWidthInRegion = computedValues.m_extent;
LayoutUnit logicalLeftInRegion = computedValues.m_position;
LayoutUnit widthDelta = logicalWidthInRegion - logicalWidth();
LayoutUnit logicalLeftDelta = isOutOfFlowPositioned() ? logicalLeftInRegion - logicalLeft() : startMarginDelta;
LayoutUnit logicalRightInRegion = containingBlockLogicalWidthInRegion - (logicalLeftInRegion + logicalWidthInRegion);
LayoutUnit oldLogicalRight = containingBlockLogicalWidth - (logicalLeft() + logicalWidth());
LayoutUnit logicalRightDelta = isOutOfFlowPositioned() ? logicalRightInRegion - oldLogicalRight : startMarginDelta;
LayoutUnit logicalLeftOffset = 0;
if (!isOutOfFlowPositioned() && avoidsFloats() && cb.containsFloats()) {
LayoutUnit startPositionDelta = cb.computeStartPositionDeltaForChildAvoidingFloats(*this, marginStartInRegion, region);
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);
}
static bool shouldFlipBeforeAfterMargins(const RenderStyle& containingBlockStyle, const RenderStyle* childStyle)
{
ASSERT(containingBlockStyle.isHorizontalWritingMode() != childStyle->isHorizontalWritingMode());
WritingMode childWritingMode = childStyle->writingMode();
bool shouldFlip = false;
switch (containingBlockStyle.writingMode()) {
case TopToBottomWritingMode:
shouldFlip = (childWritingMode == RightToLeftWritingMode);
break;
case BottomToTopWritingMode:
shouldFlip = (childWritingMode == RightToLeftWritingMode);
break;
case RightToLeftWritingMode:
shouldFlip = (childWritingMode == BottomToTopWritingMode);
break;
case LeftToRightWritingMode:
shouldFlip = (childWritingMode == BottomToTopWritingMode);
break;
}
if (!containingBlockStyle.isLeftToRightDirection())
shouldFlip = !shouldFlip;
return shouldFlip;
}
void RenderBox::cacheIntrinsicContentLogicalHeightForFlexItem(LayoutUnit height) const
{
if (isFloatingOrOutOfFlowPositioned() || !parent() || !parent()->isFlexibleBox())
return;
downcast<RenderFlexibleBox>(parent())->setCachedChildIntrinsicContentLogicalHeight(*this, height);
}
void RenderBox::updateLogicalHeight()
{
cacheIntrinsicContentLogicalHeightForFlexItem(contentLogicalHeight());
auto computedValues = computeLogicalHeight(logicalHeight(), logicalTop());
setLogicalHeight(computedValues.m_extent);
setLogicalTop(computedValues.m_position);
setMarginBefore(computedValues.m_margins.m_before);
setMarginAfter(computedValues.m_margins.m_after);
}
RenderBox::LogicalExtentComputedValues RenderBox::computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop) const
{
LogicalExtentComputedValues computedValues;
computedValues.m_extent = logicalHeight;
computedValues.m_position = logicalTop;
if (isTableCell() || (isInline() && !isReplaced()))
return computedValues;
Length h;
if (isOutOfFlowPositioned())
computePositionedLogicalHeight(computedValues);
else {
RenderBlock& cb = *containingBlock();
bool hasPerpendicularContainingBlock = cb.isHorizontalWritingMode() != isHorizontalWritingMode();
if (!hasPerpendicularContainingBlock) {
bool shouldFlipBeforeAfter = cb.style().writingMode() != style().writingMode();
computeBlockDirectionMargins(cb,
shouldFlipBeforeAfter ? computedValues.m_margins.m_after : computedValues.m_margins.m_before,
shouldFlipBeforeAfter ? computedValues.m_margins.m_before : computedValues.m_margins.m_after);
}
if (isTable()) {
if (hasPerpendicularContainingBlock) {
bool shouldFlipBeforeAfter = shouldFlipBeforeAfterMargins(cb.style(), &style());
computeInlineDirectionMargins(cb, containingBlockLogicalWidthForContent(), computedValues.m_extent,
shouldFlipBeforeAfter ? computedValues.m_margins.m_after : computedValues.m_margins.m_before,
shouldFlipBeforeAfter ? computedValues.m_margins.m_before : computedValues.m_margins.m_after);
}
return computedValues;
}
bool inHorizontalBox = parent()->isDeprecatedFlexibleBox() && parent()->style().boxOrient() == HORIZONTAL;
bool stretching = parent()->style().boxAlign() == BSTRETCH;
bool treatAsReplaced = shouldComputeSizeAsReplaced() && (!inHorizontalBox || !stretching);
bool checkMinMaxHeight = false;
if (hasOverrideLogicalContentHeight() && (parent()->isFlexibleBoxIncludingDeprecated() || parent()->isRenderGrid())) {
h = Length(overrideLogicalContentHeight(), Fixed);
} else if (treatAsReplaced)
h = Length(computeReplacedLogicalHeight(), Fixed);
else {
h = style().logicalHeight();
checkMinMaxHeight = true;
}
if (h.isAuto() && is<RenderDeprecatedFlexibleBox>(*parent()) && parent()->style().boxOrient() == HORIZONTAL
&& downcast<RenderDeprecatedFlexibleBox>(*parent()).isStretchingChildren()) {
h = Length(parentBox()->contentLogicalHeight() - marginBefore() - marginAfter() - borderAndPaddingLogicalHeight(), Fixed);
checkMinMaxHeight = false;
}
LayoutUnit heightResult;
if (checkMinMaxHeight) {
LayoutUnit intrinsicHeight = computedValues.m_extent - borderAndPaddingLogicalHeight();
heightResult = computeLogicalHeightUsing(MainOrPreferredSize, style().logicalHeight(), intrinsicHeight).value_or(computedValues.m_extent);
heightResult = constrainLogicalHeightByMinMax(heightResult, intrinsicHeight);
} else {
ASSERT(h.isFixed());
heightResult = h.value() + borderAndPaddingLogicalHeight();
}
computedValues.m_extent = heightResult;
if (hasPerpendicularContainingBlock) {
bool shouldFlipBeforeAfter = shouldFlipBeforeAfterMargins(cb.style(), &style());
computeInlineDirectionMargins(cb, containingBlockLogicalWidthForContent(), heightResult,
shouldFlipBeforeAfter ? computedValues.m_margins.m_after : computedValues.m_margins.m_before,
shouldFlipBeforeAfter ? computedValues.m_margins.m_before : computedValues.m_margins.m_after);
}
}
bool paginatedContentNeedsBaseHeight = document().printing() && h.isPercentOrCalculated()
&& (isDocumentElementRenderer() || (isBody() && document().documentElement()->renderer()->style().logicalHeight().isPercentOrCalculated())) && !isInline();
if (stretchesToViewport() || paginatedContentNeedsBaseHeight) {
LayoutUnit margins = collapsedMarginBefore() + collapsedMarginAfter();
LayoutUnit visibleHeight = view().pageOrViewLogicalHeight();
if (isDocumentElementRenderer())
computedValues.m_extent = std::max(computedValues.m_extent, visibleHeight - margins);
else {
LayoutUnit marginsBordersPadding = margins + parentBox()->marginBefore() + parentBox()->marginAfter() + parentBox()->borderAndPaddingLogicalHeight();
computedValues.m_extent = std::max(computedValues.m_extent, visibleHeight - marginsBordersPadding);
}
}
return computedValues;
}
LayoutUnit RenderBox::computeLogicalHeightWithoutLayout() const
{
LogicalExtentComputedValues computedValues = computeLogicalHeight(borderAndPaddingLogicalHeight(), LayoutUnit());
return computedValues.m_extent;
}
std::optional<LayoutUnit> RenderBox::computeLogicalHeightUsing(SizeType heightType, const Length& height, std::optional<LayoutUnit> intrinsicContentHeight) const
{
if (std::optional<LayoutUnit> logicalHeight = computeContentAndScrollbarLogicalHeightUsing(heightType, height, intrinsicContentHeight))
return adjustBorderBoxLogicalHeightForBoxSizing(logicalHeight.value());
return std::nullopt;
}
std::optional<LayoutUnit> RenderBox::computeContentLogicalHeight(SizeType heightType, const Length& height, std::optional<LayoutUnit> intrinsicContentHeight) const
{
if (std::optional<LayoutUnit> heightIncludingScrollbar = computeContentAndScrollbarLogicalHeightUsing(heightType, height, intrinsicContentHeight))
return std::max<LayoutUnit>(0, adjustContentBoxLogicalHeightForBoxSizing(heightIncludingScrollbar) - scrollbarLogicalHeight());
return std::nullopt;
}
std::optional<LayoutUnit> RenderBox::computeIntrinsicLogicalContentHeightUsing(Length logicalHeightLength, std::optional<LayoutUnit> intrinsicContentHeight, LayoutUnit borderAndPadding) const
{
if (logicalHeightLength.isMinContent() || logicalHeightLength.isMaxContent() || logicalHeightLength.isFitContent()) {
if (!intrinsicContentHeight)
return intrinsicContentHeight;
if (style().boxSizing() == BORDER_BOX)
return intrinsicContentHeight.value() + borderAndPaddingLogicalHeight();
return intrinsicContentHeight;
}
if (logicalHeightLength.isFillAvailable())
return containingBlock()->availableLogicalHeight(ExcludeMarginBorderPadding) - borderAndPadding;
ASSERT_NOT_REACHED();
return LayoutUnit(0);
}
std::optional<LayoutUnit> RenderBox::computeContentAndScrollbarLogicalHeightUsing(SizeType heightType, const Length& height, std::optional<LayoutUnit> intrinsicContentHeight) const
{
if (height.isAuto())
return heightType == MinSize ? std::optional<LayoutUnit>(0) : std::nullopt;
if (height.isIntrinsic())
return computeIntrinsicLogicalContentHeightUsing(height, intrinsicContentHeight, borderAndPaddingLogicalHeight());
if (height.isFixed())
return LayoutUnit(height.value());
if (height.isPercentOrCalculated())
return computePercentageLogicalHeight(height);
return std::nullopt;
}
bool RenderBox::skipContainingBlockForPercentHeightCalculation(const RenderBox& containingBlock, bool isPerpendicularWritingMode) const
{
if (containingBlock.isInFlowRenderFlowThread() && !isPerpendicularWritingMode)
return true;
if (is<RenderView>(containingBlock))
return false;
if (isPerpendicularWritingMode)
return false;
if (containingBlock.isAnonymous())
return containingBlock.style().display() == BLOCK || containingBlock.style().display() == INLINE_BLOCK;
return document().inQuirksMode() && !containingBlock.isTableCell() && !containingBlock.isOutOfFlowPositioned() && !containingBlock.isRenderGrid() && containingBlock.style().logicalHeight().isAuto();
}
bool RenderBox::shouldTreatChildAsReplacedInTableCells() const
{
if (isReplaced())
return true;
return element() && (element()->isFormControlElement() || is<HTMLImageElement>(element()));
}
static bool tableCellShouldHaveZeroInitialSize(const RenderBlock& block, const RenderBox& child, bool scrollsOverflowY)
{
const RenderTableCell& cell = downcast<RenderTableCell>(block);
return scrollsOverflowY && !child.shouldTreatChildAsReplacedInTableCells() && (!cell.style().logicalHeight().isAuto() || !cell.table()->style().logicalHeight().isAuto());
}
std::optional<LayoutUnit> RenderBox::computePercentageLogicalHeight(const Length& height) const
{
std::optional<LayoutUnit> availableHeight;
bool skippedAutoHeightContainingBlock = false;
RenderBlock* cb = containingBlock();
const RenderBox* containingBlockChild = this;
LayoutUnit rootMarginBorderPaddingHeight = 0;
bool isHorizontal = isHorizontalWritingMode();
while (cb && !is<RenderView>(*cb) && skipContainingBlockForPercentHeightCalculation(*cb, isHorizontal != cb->isHorizontalWritingMode())) {
if (cb->isBody() || cb->isDocumentElementRenderer())
rootMarginBorderPaddingHeight += cb->marginBefore() + cb->marginAfter() + cb->borderAndPaddingLogicalHeight();
skippedAutoHeightContainingBlock = true;
containingBlockChild = cb;
cb = cb->containingBlock();
}
cb->addPercentHeightDescendant(const_cast<RenderBox&>(*this));
if (isHorizontal != cb->isHorizontalWritingMode())
availableHeight = containingBlockChild->containingBlockLogicalWidthForContent();
else if (hasOverrideContainingBlockLogicalHeight())
availableHeight = overrideContainingBlockContentLogicalHeight();
else if (is<RenderTableCell>(*cb)) {
if (!skippedAutoHeightContainingBlock) {
if (!cb->hasOverrideLogicalContentHeight())
return tableCellShouldHaveZeroInitialSize(*cb, *this, scrollsOverflowY()) ? std::optional<LayoutUnit>(0) : std::nullopt;
availableHeight = cb->overrideLogicalContentHeight();
}
} else
availableHeight = cb->availableLogicalHeightForPercentageComputation();
if (!availableHeight)
return availableHeight;
LayoutUnit result = valueForLength(height, availableHeight.value() - rootMarginBorderPaddingHeight + (isTable() && isOutOfFlowPositioned() ? cb->paddingBefore() + cb->paddingAfter() : LayoutUnit()));
bool subtractBorderAndPadding = isTable() || (is<RenderTableCell>(*cb) && !skippedAutoHeightContainingBlock && cb->hasOverrideLogicalContentHeight());
if (subtractBorderAndPadding) {
result -= borderAndPaddingLogicalHeight();
return std::max(LayoutUnit(), result);
}
return result;
}
LayoutUnit RenderBox::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
{
return computeReplacedLogicalWidthRespectingMinMaxWidth(computeReplacedLogicalWidthUsing(MainOrPreferredSize, style().logicalWidth()), shouldComputePreferred);
}
LayoutUnit RenderBox::computeReplacedLogicalWidthRespectingMinMaxWidth(LayoutUnit logicalWidth, ShouldComputePreferred shouldComputePreferred) const
{
auto& logicalMinWidth = style().logicalMinWidth();
auto& logicalMaxWidth = style().logicalMaxWidth();
bool useLogicalWidthForMinWidth = (shouldComputePreferred == ComputePreferred && logicalMinWidth.isPercentOrCalculated()) || logicalMinWidth.isUndefined();
bool useLogicalWidthForMaxWidth = (shouldComputePreferred == ComputePreferred && logicalMaxWidth.isPercentOrCalculated()) || logicalMaxWidth.isUndefined();
auto minLogicalWidth = useLogicalWidthForMinWidth ? logicalWidth : computeReplacedLogicalWidthUsing(MinSize, logicalMinWidth);
auto maxLogicalWidth = useLogicalWidthForMaxWidth ? logicalWidth : computeReplacedLogicalWidthUsing(MaxSize, logicalMaxWidth);
return std::max(minLogicalWidth, std::min(logicalWidth, maxLogicalWidth));
}
LayoutUnit RenderBox::computeReplacedLogicalWidthUsing(SizeType widthType, Length logicalWidth) const
{
ASSERT(widthType == MinSize || widthType == MainOrPreferredSize || !logicalWidth.isAuto());
if (widthType == MinSize && logicalWidth.isAuto())
return adjustContentBoxLogicalWidthForBoxSizing(0);
switch (logicalWidth.type()) {
case Fixed:
return adjustContentBoxLogicalWidthForBoxSizing(logicalWidth.value());
case MinContent:
case MaxContent: {
LayoutUnit availableLogicalWidth = 0;
return computeIntrinsicLogicalWidthUsing(logicalWidth, availableLogicalWidth, borderAndPaddingLogicalWidth()) - borderAndPaddingLogicalWidth();
}
case FitContent:
case FillAvailable:
case Percent:
case Calculated: {
const LayoutUnit cw = isOutOfFlowPositioned() ? containingBlockLogicalWidthForPositioned(downcast<RenderBoxModelObject>(*container())) : containingBlockLogicalWidthForContent();
Length containerLogicalWidth = containingBlock()->style().logicalWidth();
if (logicalWidth.isIntrinsic())
return computeIntrinsicLogicalWidthUsing(logicalWidth, cw, borderAndPaddingLogicalWidth()) - borderAndPaddingLogicalWidth();
if (cw > 0 || (!cw && (containerLogicalWidth.isFixed() || containerLogicalWidth.isPercentOrCalculated())))
return adjustContentBoxLogicalWidthForBoxSizing(minimumValueForLength(logicalWidth, cw));
}
FALLTHROUGH;
case Intrinsic:
case MinIntrinsic:
case Auto:
case Relative:
case Undefined:
return intrinsicLogicalWidth();
}
ASSERT_NOT_REACHED();
return 0;
}
LayoutUnit RenderBox::computeReplacedLogicalHeight(std::optional<LayoutUnit>) const
{
return computeReplacedLogicalHeightRespectingMinMaxHeight(computeReplacedLogicalHeightUsing(MainOrPreferredSize, style().logicalHeight()));
}
static bool allowMinMaxPercentagesInAutoHeightBlocksQuirk()
{
#if PLATFORM(MAC)
return MacApplication::isIBooks();
#elif PLATFORM(IOS)
return IOSApplication::isIBooks();
#endif
return false;
}
bool RenderBox::replacedMinMaxLogicalHeightComputesAsNone(SizeType sizeType) const
{
ASSERT(sizeType == MinSize || sizeType == MaxSize);
auto logicalHeight = sizeType == MinSize ? style().logicalMinHeight() : style().logicalMaxHeight();
auto initialLogicalHeight = sizeType == MinSize ? RenderStyle::initialMinSize() : RenderStyle::initialMaxSize();
if (logicalHeight == initialLogicalHeight)
return true;
if (auto* cb = containingBlockForAutoHeightDetection(logicalHeight))
return allowMinMaxPercentagesInAutoHeightBlocksQuirk() ? false : cb->hasAutoHeightOrContainingBlockWithAutoHeight();
return false;
}
LayoutUnit RenderBox::computeReplacedLogicalHeightRespectingMinMaxHeight(LayoutUnit logicalHeight) const
{
LayoutUnit minLogicalHeight;
if (!replacedMinMaxLogicalHeightComputesAsNone(MinSize))
minLogicalHeight = computeReplacedLogicalHeightUsing(MinSize, style().logicalMinHeight());
LayoutUnit maxLogicalHeight = logicalHeight;
if (!replacedMinMaxLogicalHeightComputesAsNone(MaxSize))
maxLogicalHeight = computeReplacedLogicalHeightUsing(MaxSize, style().logicalMaxHeight());
return std::max(minLogicalHeight, std::min(logicalHeight, maxLogicalHeight));
}
LayoutUnit RenderBox::computeReplacedLogicalHeightUsing(SizeType heightType, Length logicalHeight) const
{
ASSERT(heightType == MinSize || heightType == MainOrPreferredSize || !logicalHeight.isAuto());
if (heightType == MinSize && logicalHeight.isAuto())
return adjustContentBoxLogicalHeightForBoxSizing(std::optional<LayoutUnit>(0));
switch (logicalHeight.type()) {
case Fixed:
return adjustContentBoxLogicalHeightForBoxSizing(LayoutUnit(logicalHeight.value()));
case Percent:
case Calculated:
{
auto* container = isOutOfFlowPositioned() ? this->container() : containingBlock();
while (container && container->isAnonymous()) {
if (is<RenderView>(*container) || is<RenderNamedFlowThread>(*container))
break;
container = container->containingBlock();
}
std::optional<LayoutUnit> stretchedHeight;
if (is<RenderBlock>(container)) {
auto* block = downcast<RenderBlock>(container);
block->addPercentHeightDescendant(*const_cast<RenderBox*>(this));
if (block->isFlexItem())
stretchedHeight = downcast<RenderFlexibleBox>(block->parent())->childLogicalHeightForPercentageResolution(*block);
else if (block->isGridItem() && block->hasOverrideLogicalContentHeight())
stretchedHeight = block->overrideLogicalContentHeight();
}
if (container->isOutOfFlowPositioned()
&& container->style().height().isAuto()
&& !(container->style().top().isAuto() || container->style().bottom().isAuto())) {
ASSERT_WITH_SECURITY_IMPLICATION(container->isRenderBlock());
auto& block = downcast<RenderBlock>(*container);
auto computedValues = block.computeLogicalHeight(block.logicalHeight(), 0);
LayoutUnit newContentHeight = computedValues.m_extent - block.borderAndPaddingLogicalHeight() - block.scrollbarLogicalHeight();
LayoutUnit newHeight = block.adjustContentBoxLogicalHeightForBoxSizing(newContentHeight);
return adjustContentBoxLogicalHeightForBoxSizing(valueForLength(logicalHeight, newHeight));
}
LayoutUnit availableHeight;
if (isOutOfFlowPositioned())
availableHeight = containingBlockLogicalHeightForPositioned(downcast<RenderBoxModelObject>(*container));
else if (stretchedHeight)
availableHeight = stretchedHeight.value();
else {
availableHeight = containingBlockLogicalHeightForContent(IncludeMarginBorderPadding);
while (container && !is<RenderView>(*container)
&& (container->style().logicalHeight().isAuto() || container->style().logicalHeight().isPercentOrCalculated())) {
if (container->isTableCell()) {
availableHeight = std::max(availableHeight, intrinsicLogicalHeight());
return valueForLength(logicalHeight, availableHeight - borderAndPaddingLogicalHeight());
}
downcast<RenderBlock>(*container).addPercentHeightDescendant(const_cast<RenderBox&>(*this));
container = container->containingBlock();
}
}
return adjustContentBoxLogicalHeightForBoxSizing(valueForLength(logicalHeight, availableHeight));
}
case MinContent:
case MaxContent:
case FitContent:
case FillAvailable:
return adjustContentBoxLogicalHeightForBoxSizing(computeIntrinsicLogicalContentHeightUsing(logicalHeight, intrinsicLogicalHeight(), borderAndPaddingLogicalHeight()));
default:
return intrinsicLogicalHeight();
}
}
LayoutUnit RenderBox::availableLogicalHeight(AvailableLogicalHeightType heightType) const
{
return constrainLogicalHeightByMinMax(availableLogicalHeightUsing(style().logicalHeight(), heightType), std::nullopt);
}
LayoutUnit RenderBox::availableLogicalHeightUsing(const Length& h, AvailableLogicalHeightType heightType) const
{
if (isTableCell() && (h.isAuto() || h.isPercentOrCalculated())) {
if (hasOverrideLogicalContentHeight())
return overrideLogicalContentHeight();
return logicalHeight() - borderAndPaddingLogicalHeight();
}
if (isFlexItem()) {
auto& flexBox = downcast<RenderFlexibleBox>(*parent());
auto stretchedHeight = flexBox.childLogicalHeightForPercentageResolution(*this);
if (stretchedHeight)
return stretchedHeight.value();
}
if (h.isPercentOrCalculated() && isOutOfFlowPositioned() && !isRenderFlowThread()) {
LayoutUnit availableHeight = containingBlockLogicalHeightForPositioned(*containingBlock());
return adjustContentBoxLogicalHeightForBoxSizing(valueForLength(h, availableHeight));
}
if (std::optional<LayoutUnit> heightIncludingScrollbar = computeContentAndScrollbarLogicalHeightUsing(MainOrPreferredSize, h, std::nullopt))
return std::max<LayoutUnit>(0, adjustContentBoxLogicalHeightForBoxSizing(heightIncludingScrollbar) - scrollbarLogicalHeight());
if (is<RenderBlock>(*this) && isOutOfFlowPositioned() && style().height().isAuto() && !(style().top().isAuto() || style().bottom().isAuto())) {
RenderBlock& block = const_cast<RenderBlock&>(downcast<RenderBlock>(*this));
auto computedValues = block.computeLogicalHeight(block.logicalHeight(), 0);
LayoutUnit newContentHeight = computedValues.m_extent - block.borderAndPaddingLogicalHeight() - block.scrollbarLogicalHeight();
return adjustContentBoxLogicalHeightForBoxSizing(newContentHeight);
}
LayoutUnit availableHeight = containingBlockLogicalHeightForContent(heightType);
if (heightType == ExcludeMarginBorderPadding) {
availableHeight -= marginBefore() + marginAfter() + borderAndPaddingLogicalHeight();
}
return availableHeight;
}
void RenderBox::computeBlockDirectionMargins(const RenderBlock& containingBlock, LayoutUnit& marginBefore, LayoutUnit& marginAfter) const
{
if (isTableCell()) {
marginBefore = 0;
marginAfter = 0;
return;
}
LayoutUnit cw = containingBlockLogicalWidthForContent();
const RenderStyle& containingBlockStyle = containingBlock.style();
marginBefore = minimumValueForLength(style().marginBeforeUsing(&containingBlockStyle), cw);
marginAfter = minimumValueForLength(style().marginAfterUsing(&containingBlockStyle), cw);
}
void RenderBox::computeAndSetBlockDirectionMargins(const RenderBlock& containingBlock)
{
LayoutUnit marginBefore;
LayoutUnit marginAfter;
computeBlockDirectionMargins(containingBlock, marginBefore, marginAfter);
containingBlock.setMarginBeforeForChild(*this, marginBefore);
containingBlock.setMarginAfterForChild(*this, marginAfter);
}
LayoutUnit RenderBox::containingBlockLogicalWidthForPositioned(const RenderBoxModelObject& containingBlock, RenderRegion* region, bool checkForPerpendicularWritingMode) const
{
if (checkForPerpendicularWritingMode && containingBlock.isHorizontalWritingMode() != isHorizontalWritingMode())
return containingBlockLogicalHeightForPositioned(containingBlock, false);
if (hasOverrideContainingBlockLogicalWidth()) {
if (auto overrideLogicalWidth = overrideContainingBlockContentLogicalWidth())
return overrideLogicalWidth.value();
}
if (is<RenderBox>(containingBlock)) {
bool isFixedPosition = style().position() == FixedPosition;
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (!flowThread) {
if (isFixedPosition && is<RenderView>(containingBlock))
return downcast<RenderView>(containingBlock).clientLogicalWidthForFixedPosition();
return downcast<RenderBox>(containingBlock).clientLogicalWidth();
}
if (isFixedPosition && is<RenderNamedFlowThread>(containingBlock))
return containingBlock.view().clientLogicalWidth();
if (!is<RenderBlock>(containingBlock))
return downcast<RenderBox>(containingBlock).clientLogicalWidth();
const RenderBlock& cb = downcast<RenderBlock>(containingBlock);
RenderBoxRegionInfo* boxInfo = nullptr;
if (!region) {
if (is<RenderFlowThread>(containingBlock) && !checkForPerpendicularWritingMode)
return downcast<RenderFlowThread>(containingBlock).contentLogicalWidthOfFirstRegion();
if (isWritingModeRoot()) {
LayoutUnit cbPageOffset = cb.offsetFromLogicalTopOfFirstPage();
RenderRegion* cbRegion = cb.regionAtBlockOffset(cbPageOffset);
if (cbRegion)
boxInfo = cb.renderBoxRegionInfo(cbRegion);
}
} else if (flowThread->isHorizontalWritingMode() == containingBlock.isHorizontalWritingMode()) {
RenderRegion* containingBlockRegion = cb.clampToStartAndEndRegions(region);
boxInfo = cb.renderBoxRegionInfo(containingBlockRegion);
}
return (boxInfo) ? std::max<LayoutUnit>(0, cb.clientLogicalWidth() - (cb.logicalWidth() - boxInfo->logicalWidth())) : cb.clientLogicalWidth();
}
ASSERT(containingBlock.isInFlowPositioned());
const auto& flow = downcast<RenderInline>(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 std::max<LayoutUnit>(0, fromRight - fromLeft);
}
LayoutUnit RenderBox::containingBlockLogicalHeightForPositioned(const RenderBoxModelObject& containingBlock, bool checkForPerpendicularWritingMode) const
{
if (checkForPerpendicularWritingMode && containingBlock.isHorizontalWritingMode() != isHorizontalWritingMode())
return containingBlockLogicalWidthForPositioned(containingBlock, nullptr, false);
if (hasOverrideContainingBlockLogicalHeight()) {
if (auto overrideLogicalHeight = overrideContainingBlockContentLogicalHeight())
return overrideLogicalHeight.value();
}
if (containingBlock.isBox()) {
bool isFixedPosition = style().position() == FixedPosition;
if (isFixedPosition && is<RenderView>(containingBlock))
return downcast<RenderView>(containingBlock).clientLogicalHeightForFixedPosition();
const RenderBlock& cb = is<RenderBlock>(containingBlock) ? downcast<RenderBlock>(containingBlock) : *containingBlock.containingBlock();
LayoutUnit result = cb.clientLogicalHeight();
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (flowThread && is<RenderFlowThread>(containingBlock) && flowThread->isHorizontalWritingMode() == containingBlock.isHorizontalWritingMode()) {
if (is<RenderNamedFlowThread>(containingBlock) && isFixedPosition)
return containingBlock.view().clientLogicalHeight();
return downcast<RenderFlowThread>(containingBlock).contentLogicalHeightOfFirstRegion();
}
return result;
}
ASSERT(containingBlock.isInFlowPositioned());
const auto& flow = downcast<RenderInline>(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;
RenderObject* parent = child->parent();
TextDirection parentDirection = parent->style().direction();
if (parent->isRenderGrid() && parent == child->containingBlock()) {
if (parentDirection == LTR)
logicalLeft.setValue(Fixed, 0);
else
logicalRight.setValue(Fixed, 0);
return;
}
if (parentDirection == LTR) {
LayoutUnit staticPosition = child->layer()->staticInlinePosition() - containerBlock.borderLogicalLeft();
for (auto* current = parent; current && current != &containerBlock; current = current->container()) {
if (!is<RenderBox>(*current))
continue;
const auto& renderBox = downcast<RenderBox>(*current);
staticPosition += renderBox.logicalLeft();
if (renderBox.isInFlowPositioned())
staticPosition += renderBox.isHorizontalWritingMode() ? renderBox.offsetForInFlowPosition().width() : renderBox.offsetForInFlowPosition().height();
if (region && is<RenderBlock>(*current)) {
const RenderBlock& currentBlock = downcast<RenderBlock>(*current);
region = currentBlock.clampToStartAndEndRegions(region);
RenderBoxRegionInfo* boxInfo = currentBlock.renderBoxRegionInfo(region);
if (boxInfo)
staticPosition += boxInfo->logicalLeft();
}
}
logicalLeft.setValue(Fixed, staticPosition);
} else {
LayoutUnit staticPosition = child->layer()->staticInlinePosition() + containerLogicalWidth + containerBlock.borderLogicalLeft();
auto& enclosingBox = parent->enclosingBox();
if (&enclosingBox != &containerBlock && containerBlock.isDescendantOf(&enclosingBox)) {
logicalRight.setValue(Fixed, staticPosition);
return;
}
staticPosition -= enclosingBox.logicalWidth();
for (const RenderElement* current = &enclosingBox; current; current = current->container()) {
if (!is<RenderBox>(*current))
continue;
if (current != &containerBlock) {
auto& renderBox = downcast<RenderBox>(*current);
staticPosition -= renderBox.logicalLeft();
if (renderBox.isInFlowPositioned())
staticPosition -= renderBox.isHorizontalWritingMode() ? renderBox.offsetForInFlowPosition().width() : renderBox.offsetForInFlowPosition().height();
}
if (region && is<RenderBlock>(*current)) {
auto& currentBlock = downcast<RenderBlock>(*current);
region = currentBlock.clampToStartAndEndRegions(region);
RenderBoxRegionInfo* boxInfo = currentBlock.renderBoxRegionInfo(region);
if (boxInfo) {
if (current != &containerBlock)
staticPosition -= currentBlock.logicalWidth() - (boxInfo->logicalLeft() + boxInfo->logicalWidth());
if (current == &enclosingBox)
staticPosition += enclosingBox.logicalWidth() - boxInfo->logicalWidth();
}
}
if (current == &containerBlock)
break;
}
logicalRight.setValue(Fixed, staticPosition);
}
}
void RenderBox::computePositionedLogicalWidth(LogicalExtentComputedValues& computedValues, RenderRegion* region) const
{
if (isReplaced()) {
computePositionedLogicalWidthReplaced(computedValues);
return;
}
const RenderBoxModelObject& containerBlock = downcast<RenderBoxModelObject>(*container());
const LayoutUnit containerLogicalWidth = containingBlockLogicalWidthForPositioned(containerBlock, region);
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();
Length logicalLeftLength = style().logicalLeft();
Length logicalRightLength = style().logicalRight();
computeInlineStaticDistance(logicalLeftLength, logicalRightLength, this, containerBlock, containerLogicalWidth, region);
computePositionedLogicalWidthUsing(MainOrPreferredSize, style().logicalWidth(), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeftLength, logicalRightLength, marginLogicalLeft, marginLogicalRight,
computedValues);
if (!style().logicalMaxWidth().isUndefined()) {
LogicalExtentComputedValues maxValues;
computePositionedLogicalWidthUsing(MaxSize, style().logicalMaxWidth(), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeftLength, logicalRightLength, marginLogicalLeft, marginLogicalRight,
maxValues);
if (computedValues.m_extent > maxValues.m_extent) {
computedValues.m_extent = maxValues.m_extent;
computedValues.m_position = maxValues.m_position;
computedValues.m_margins.m_start = maxValues.m_margins.m_start;
computedValues.m_margins.m_end = maxValues.m_margins.m_end;
}
}
if (!style().logicalMinWidth().isZero() || style().logicalMinWidth().isIntrinsic()) {
LogicalExtentComputedValues minValues;
computePositionedLogicalWidthUsing(MinSize, style().logicalMinWidth(), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeftLength, logicalRightLength, marginLogicalLeft, marginLogicalRight,
minValues);
if (computedValues.m_extent < minValues.m_extent) {
computedValues.m_extent = minValues.m_extent;
computedValues.m_position = minValues.m_position;
computedValues.m_margins.m_start = minValues.m_margins.m_start;
computedValues.m_margins.m_end = minValues.m_margins.m_end;
}
}
computedValues.m_extent += bordersPlusPadding;
if (is<RenderBox>(containerBlock)) {
auto& containingBox = downcast<RenderBox>(containerBlock);
if (containingBox.shouldPlaceBlockDirectionScrollbarOnLeft())
computedValues.m_position += containingBox.verticalScrollbarWidth();
}
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (flowThread && !region && isWritingModeRoot() && isHorizontalWritingMode() == containerBlock.isHorizontalWritingMode() && is<RenderBlock>(containerBlock)) {
ASSERT(containerBlock.canHaveBoxInfoInRegion());
LayoutUnit logicalLeftPos = computedValues.m_position;
const RenderBlock& renderBlock = downcast<RenderBlock>(containerBlock);
LayoutUnit cbPageOffset = renderBlock.offsetFromLogicalTopOfFirstPage();
RenderRegion* cbRegion = renderBlock.regionAtBlockOffset(cbPageOffset);
if (cbRegion) {
RenderBoxRegionInfo* boxInfo = renderBlock.renderBoxRegionInfo(cbRegion);
if (boxInfo) {
logicalLeftPos += boxInfo->logicalLeft();
computedValues.m_position = 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(SizeType widthType, Length logicalWidth, const RenderBoxModelObject& containerBlock, TextDirection containerDirection,
LayoutUnit containerLogicalWidth, LayoutUnit bordersPlusPadding,
Length logicalLeft, Length logicalRight, Length marginLogicalLeft, Length marginLogicalRight,
LogicalExtentComputedValues& computedValues) const
{
ASSERT(widthType == MinSize || widthType == MainOrPreferredSize || !logicalWidth.isAuto());
if (widthType == MinSize && logicalWidth.isAuto())
logicalWidth = Length(0, Fixed);
else if (logicalWidth.isIntrinsic())
logicalWidth = Length(computeIntrinsicLogicalWidthUsing(logicalWidth, containerLogicalWidth, bordersPlusPadding) - bordersPlusPadding, Fixed);
ASSERT(!(logicalLeft.isAuto() && logicalRight.isAuto()));
LayoutUnit logicalLeftValue = 0;
const LayoutUnit containerRelativeLogicalWidth = containingBlockLogicalWidthForPositioned(containerBlock, nullptr, false);
bool logicalWidthIsAuto = logicalWidth.isIntrinsicOrAuto();
bool logicalLeftIsAuto = logicalLeft.isAuto();
bool logicalRightIsAuto = logicalRight.isAuto();
LayoutUnit& marginLogicalLeftValue = style().isLeftToRightDirection() ? computedValues.m_margins.m_start : computedValues.m_margins.m_end;
LayoutUnit& marginLogicalRightValue = style().isLeftToRightDirection() ? computedValues.m_margins.m_end : computedValues.m_margins.m_start;
if (!logicalLeftIsAuto && !logicalWidthIsAuto && !logicalRightIsAuto) {
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth);
computedValues.m_extent = adjustContentBoxLogicalWidthForBoxSizing(valueForLength(logicalWidth, containerLogicalWidth));
const LayoutUnit availableSpace = containerLogicalWidth - (logicalLeftValue + computedValues.m_extent + valueForLength(logicalRight, containerLogicalWidth) + 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, containerRelativeLogicalWidth);
marginLogicalLeftValue = availableSpace - marginLogicalRightValue;
} else if (marginLogicalRight.isAuto()) {
marginLogicalLeftValue = valueForLength(marginLogicalLeft, containerRelativeLogicalWidth);
marginLogicalRightValue = availableSpace - marginLogicalLeftValue;
} else {
marginLogicalLeftValue = valueForLength(marginLogicalLeft, containerRelativeLogicalWidth);
marginLogicalRightValue = valueForLength(marginLogicalRight, containerRelativeLogicalWidth);
if (containerDirection == RTL)
logicalLeftValue = (availableSpace + logicalLeftValue) - marginLogicalLeftValue - marginLogicalRightValue;
}
} else {
marginLogicalLeftValue = minimumValueForLength(marginLogicalLeft, containerRelativeLogicalWidth);
marginLogicalRightValue = minimumValueForLength(marginLogicalRight, containerRelativeLogicalWidth);
const LayoutUnit availableSpace = containerLogicalWidth - (marginLogicalLeftValue + marginLogicalRightValue + bordersPlusPadding);
if (logicalLeftIsAuto && logicalWidthIsAuto && !logicalRightIsAuto) {
LayoutUnit logicalRightValue = valueForLength(logicalRight, containerLogicalWidth);
LayoutUnit preferredWidth = maxPreferredLogicalWidth() - bordersPlusPadding;
LayoutUnit preferredMinWidth = minPreferredLogicalWidth() - bordersPlusPadding;
LayoutUnit availableWidth = availableSpace - logicalRightValue;
computedValues.m_extent = std::min(std::max(preferredMinWidth, availableWidth), preferredWidth);
logicalLeftValue = availableSpace - (computedValues.m_extent + logicalRightValue);
} else if (!logicalLeftIsAuto && logicalWidthIsAuto && logicalRightIsAuto) {
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth);
LayoutUnit preferredWidth = maxPreferredLogicalWidth() - bordersPlusPadding;
LayoutUnit preferredMinWidth = minPreferredLogicalWidth() - bordersPlusPadding;
LayoutUnit availableWidth = availableSpace - logicalLeftValue;
computedValues.m_extent = std::min(std::max(preferredMinWidth, availableWidth), preferredWidth);
} else if (logicalLeftIsAuto && !logicalWidthIsAuto && !logicalRightIsAuto) {
computedValues.m_extent = adjustContentBoxLogicalWidthForBoxSizing(valueForLength(logicalWidth, containerLogicalWidth));
logicalLeftValue = availableSpace - (computedValues.m_extent + valueForLength(logicalRight, containerLogicalWidth));
} else if (!logicalLeftIsAuto && logicalWidthIsAuto && !logicalRightIsAuto) {
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth);
computedValues.m_extent = availableSpace - (logicalLeftValue + valueForLength(logicalRight, containerLogicalWidth));
} else if (!logicalLeftIsAuto && !logicalWidthIsAuto && logicalRightIsAuto) {
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth);
computedValues.m_extent = adjustContentBoxLogicalWidthForBoxSizing(valueForLength(logicalWidth, containerLogicalWidth));
}
}
if (is<RenderInline>(containerBlock) && !containerBlock.style().isLeftToRightDirection()) {
const auto& flow = downcast<RenderInline>(containerBlock);
InlineFlowBox* firstLine = flow.firstLineBox();
InlineFlowBox* lastLine = flow.lastLineBox();
if (firstLine && lastLine && firstLine != lastLine) {
computedValues.m_position = logicalLeftValue + marginLogicalLeftValue + lastLine->borderLogicalLeft() + (lastLine->logicalLeft() - firstLine->logicalLeft());
return;
}
}
computedValues.m_position = logicalLeftValue + marginLogicalLeftValue;
computeLogicalLeftPositionedOffset(computedValues.m_position, this, computedValues.m_extent, 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 (RenderElement* container = child->parent(); container && container != &containerBlock; container = container->container()) {
if (!is<RenderBox>(*container))
continue;
const auto& renderBox = downcast<RenderBox>(*container);
if (!is<RenderTableRow>(renderBox))
staticLogicalTop += renderBox.logicalTop();
if (renderBox.isInFlowPositioned())
staticLogicalTop += renderBox.isHorizontalWritingMode() ? renderBox.offsetForInFlowPosition().height() : renderBox.offsetForInFlowPosition().width();
}
logicalTop.setValue(Fixed, staticLogicalTop);
}
void RenderBox::computePositionedLogicalHeight(LogicalExtentComputedValues& computedValues) const
{
if (isReplaced()) {
computePositionedLogicalHeightReplaced(computedValues);
return;
}
const RenderBoxModelObject& containerBlock = downcast<RenderBoxModelObject>(*container());
const LayoutUnit containerLogicalHeight = containingBlockLogicalHeightForPositioned(containerBlock);
const RenderStyle& styleToUse = style();
const LayoutUnit bordersPlusPadding = borderAndPaddingLogicalHeight();
const Length marginBefore = styleToUse.marginBefore();
const Length marginAfter = styleToUse.marginAfter();
Length logicalTopLength = styleToUse.logicalTop();
Length logicalBottomLength = styleToUse.logicalBottom();
computeBlockStaticDistance(logicalTopLength, logicalBottomLength, this, containerBlock);
LayoutUnit logicalHeight = computedValues.m_extent;
computePositionedLogicalHeightUsing(MainOrPreferredSize, styleToUse.logicalHeight(), containerBlock, containerLogicalHeight, bordersPlusPadding, logicalHeight,
logicalTopLength, logicalBottomLength, marginBefore, marginAfter,
computedValues);
if (!styleToUse.logicalMaxHeight().isUndefined()) {
LogicalExtentComputedValues maxValues;
computePositionedLogicalHeightUsing(MaxSize, styleToUse.logicalMaxHeight(), containerBlock, containerLogicalHeight, bordersPlusPadding, logicalHeight,
logicalTopLength, logicalBottomLength, marginBefore, marginAfter,
maxValues);
if (computedValues.m_extent > maxValues.m_extent) {
computedValues.m_extent = maxValues.m_extent;
computedValues.m_position = maxValues.m_position;
computedValues.m_margins.m_before = maxValues.m_margins.m_before;
computedValues.m_margins.m_after = maxValues.m_margins.m_after;
}
}
if (!styleToUse.logicalMinHeight().isZero() || styleToUse.logicalMinHeight().isIntrinsic()) {
LogicalExtentComputedValues minValues;
computePositionedLogicalHeightUsing(MinSize, styleToUse.logicalMinHeight(), containerBlock, containerLogicalHeight, bordersPlusPadding, logicalHeight,
logicalTopLength, logicalBottomLength, marginBefore, marginAfter,
minValues);
if (computedValues.m_extent < minValues.m_extent) {
computedValues.m_extent = minValues.m_extent;
computedValues.m_position = minValues.m_position;
computedValues.m_margins.m_before = minValues.m_margins.m_before;
computedValues.m_margins.m_after = minValues.m_margins.m_after;
}
}
computedValues.m_extent += bordersPlusPadding;
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (flowThread && isHorizontalWritingMode() != containerBlock.isHorizontalWritingMode() && is<RenderBlock>(containerBlock)) {
ASSERT(containerBlock.canHaveBoxInfoInRegion());
LayoutUnit logicalTopPos = computedValues.m_position;
const RenderBlock& renderBox = downcast<RenderBlock>(containerBlock);
LayoutUnit cbPageOffset = renderBox.offsetFromLogicalTopOfFirstPage() - logicalLeft();
RenderRegion* cbRegion = renderBox.regionAtBlockOffset(cbPageOffset);
if (cbRegion) {
RenderBoxRegionInfo* boxInfo = renderBox.renderBoxRegionInfo(cbRegion);
if (boxInfo) {
logicalTopPos += boxInfo->logicalLeft();
computedValues.m_position = 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(SizeType heightType, Length logicalHeightLength, const RenderBoxModelObject& containerBlock,
LayoutUnit containerLogicalHeight, LayoutUnit bordersPlusPadding, LayoutUnit logicalHeight,
Length logicalTop, Length logicalBottom, Length marginBefore, Length marginAfter,
LogicalExtentComputedValues& computedValues) const
{
ASSERT(heightType == MinSize || heightType == MainOrPreferredSize || !logicalHeightLength.isAuto());
if (heightType == MinSize && logicalHeightLength.isAuto())
logicalHeightLength = Length(0, Fixed);
ASSERT(!(logicalTop.isAuto() && logicalBottom.isAuto()));
LayoutUnit logicalHeightValue;
LayoutUnit contentLogicalHeight = logicalHeight - bordersPlusPadding;
const LayoutUnit containerRelativeLogicalWidth = containingBlockLogicalWidthForPositioned(containerBlock, nullptr, false);
LayoutUnit logicalTopValue = 0;
bool logicalHeightIsAuto = logicalHeightLength.isAuto();
bool logicalTopIsAuto = logicalTop.isAuto();
bool logicalBottomIsAuto = logicalBottom.isAuto();
LayoutUnit resolvedLogicalHeight;
if (isTable()) {
resolvedLogicalHeight = contentLogicalHeight;
logicalHeightIsAuto = false;
} else {
if (logicalHeightLength.isIntrinsic())
resolvedLogicalHeight = computeIntrinsicLogicalContentHeightUsing(logicalHeightLength, contentLogicalHeight, bordersPlusPadding).value();
else
resolvedLogicalHeight = adjustContentBoxLogicalHeightForBoxSizing(valueForLength(logicalHeightLength, containerLogicalHeight));
}
if (!logicalTopIsAuto && !logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalHeightValue = resolvedLogicalHeight;
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight);
const LayoutUnit availableSpace = containerLogicalHeight - (logicalTopValue + logicalHeightValue + valueForLength(logicalBottom, containerLogicalHeight) + bordersPlusPadding);
if (marginBefore.isAuto() && marginAfter.isAuto()) {
computedValues.m_margins.m_before = availableSpace / 2; computedValues.m_margins.m_after = availableSpace - computedValues.m_margins.m_before; } else if (marginBefore.isAuto()) {
computedValues.m_margins.m_after = valueForLength(marginAfter, containerRelativeLogicalWidth);
computedValues.m_margins.m_before = availableSpace - computedValues.m_margins.m_after;
} else if (marginAfter.isAuto()) {
computedValues.m_margins.m_before = valueForLength(marginBefore, containerRelativeLogicalWidth);
computedValues.m_margins.m_after = availableSpace - computedValues.m_margins.m_before;
} else {
computedValues.m_margins.m_before = valueForLength(marginBefore, containerRelativeLogicalWidth);
computedValues.m_margins.m_after = valueForLength(marginAfter, containerRelativeLogicalWidth);
}
} else {
computedValues.m_margins.m_before = minimumValueForLength(marginBefore, containerRelativeLogicalWidth);
computedValues.m_margins.m_after = minimumValueForLength(marginAfter, containerRelativeLogicalWidth);
const LayoutUnit availableSpace = containerLogicalHeight - (computedValues.m_margins.m_before + computedValues.m_margins.m_after + bordersPlusPadding);
if (logicalTopIsAuto && logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalHeightValue = contentLogicalHeight;
logicalTopValue = availableSpace - (logicalHeightValue + valueForLength(logicalBottom, containerLogicalHeight));
} else if (!logicalTopIsAuto && logicalHeightIsAuto && logicalBottomIsAuto) {
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight);
logicalHeightValue = contentLogicalHeight;
} else if (logicalTopIsAuto && !logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalHeightValue = resolvedLogicalHeight;
logicalTopValue = availableSpace - (logicalHeightValue + valueForLength(logicalBottom, containerLogicalHeight));
} else if (!logicalTopIsAuto && logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight);
logicalHeightValue = std::max<LayoutUnit>(0, availableSpace - (logicalTopValue + valueForLength(logicalBottom, containerLogicalHeight)));
} else if (!logicalTopIsAuto && !logicalHeightIsAuto && logicalBottomIsAuto) {
logicalHeightValue = resolvedLogicalHeight;
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight);
}
}
computedValues.m_extent = logicalHeightValue;
computedValues.m_position = logicalTopValue + computedValues.m_margins.m_before;
computeLogicalTopPositionedOffset(computedValues.m_position, this, logicalHeightValue, containerBlock, containerLogicalHeight);
}
void RenderBox::computePositionedLogicalWidthReplaced(LogicalExtentComputedValues& computedValues) const
{
const RenderBoxModelObject& containerBlock = downcast<RenderBoxModelObject>(*container());
const LayoutUnit containerLogicalWidth = containingBlockLogicalWidthForPositioned(containerBlock);
const LayoutUnit containerRelativeLogicalWidth = containingBlockLogicalWidthForPositioned(containerBlock, nullptr, false);
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 = style().isLeftToRightDirection() ? computedValues.m_margins.m_start : computedValues.m_margins.m_end;
LayoutUnit& marginLogicalRightAlias = style().isLeftToRightDirection() ? computedValues.m_margins.m_end : computedValues.m_margins.m_start;
computedValues.m_extent = computeReplacedLogicalWidth() + borderAndPaddingLogicalWidth();
const LayoutUnit availableSpace = containerLogicalWidth - computedValues.m_extent;
computeInlineStaticDistance(logicalLeft, logicalRight, this, containerBlock, containerLogicalWidth, nullptr);
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;
if (marginLogicalLeft.isAuto() && marginLogicalRight.isAuto()) {
ASSERT(!(logicalLeft.isAuto() && logicalRight.isAuto()));
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth);
logicalRightValue = valueForLength(logicalRight, containerLogicalWidth);
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, containerRelativeLogicalWidth);
marginLogicalRightAlias = valueForLength(marginLogicalRight, containerRelativeLogicalWidth);
logicalRightValue = valueForLength(logicalRight, containerLogicalWidth);
logicalLeftValue = availableSpace - (logicalRightValue + marginLogicalLeftAlias + marginLogicalRightAlias);
} else if (logicalRight.isAuto()) {
marginLogicalLeftAlias = valueForLength(marginLogicalLeft, containerRelativeLogicalWidth);
marginLogicalRightAlias = valueForLength(marginLogicalRight, containerRelativeLogicalWidth);
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth);
logicalRightValue = availableSpace - (logicalLeftValue + marginLogicalLeftAlias + marginLogicalRightAlias);
} else if (marginLogicalLeft.isAuto()) {
marginLogicalRightAlias = valueForLength(marginLogicalRight, containerRelativeLogicalWidth);
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth);
logicalRightValue = valueForLength(logicalRight, containerLogicalWidth);
marginLogicalLeftAlias = availableSpace - (logicalLeftValue + logicalRightValue + marginLogicalRightAlias);
} else if (marginLogicalRight.isAuto()) {
marginLogicalLeftAlias = valueForLength(marginLogicalLeft, containerRelativeLogicalWidth);
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth);
logicalRightValue = valueForLength(logicalRight, containerLogicalWidth);
marginLogicalRightAlias = availableSpace - (logicalLeftValue + logicalRightValue + marginLogicalLeftAlias);
} else {
marginLogicalLeftAlias = valueForLength(marginLogicalLeft, containerRelativeLogicalWidth);
marginLogicalRightAlias = valueForLength(marginLogicalRight, containerRelativeLogicalWidth);
logicalRightValue = valueForLength(logicalRight, containerLogicalWidth);
logicalLeftValue = valueForLength(logicalLeft, containerLogicalWidth);
if (containerDirection == RTL) {
int totalLogicalWidth = computedValues.m_extent + logicalLeftValue + logicalRightValue + marginLogicalLeftAlias + marginLogicalRightAlias;
logicalLeftValue = containerLogicalWidth - (totalLogicalWidth - logicalLeftValue);
}
}
if (is<RenderInline>(containerBlock) && !containerBlock.style().isLeftToRightDirection()) {
const auto& flow = downcast<RenderInline>(containerBlock);
InlineFlowBox* firstLine = flow.firstLineBox();
InlineFlowBox* lastLine = flow.lastLineBox();
if (firstLine && lastLine && firstLine != lastLine) {
computedValues.m_position = logicalLeftValue + marginLogicalLeftAlias + lastLine->borderLogicalLeft() + (lastLine->logicalLeft() - firstLine->logicalLeft());
return;
}
}
LayoutUnit logicalLeftPos = logicalLeftValue + marginLogicalLeftAlias;
computeLogicalLeftPositionedOffset(logicalLeftPos, this, computedValues.m_extent, containerBlock, containerLogicalWidth);
computedValues.m_position = logicalLeftPos;
}
void RenderBox::computePositionedLogicalHeightReplaced(LogicalExtentComputedValues& computedValues) const
{
const RenderBoxModelObject& containerBlock = downcast<RenderBoxModelObject>(*container());
const LayoutUnit containerLogicalHeight = containingBlockLogicalHeightForPositioned(containerBlock);
const LayoutUnit containerRelativeLogicalWidth = containingBlockLogicalWidthForPositioned(containerBlock, nullptr, false);
Length marginBefore = style().marginBefore();
Length marginAfter = style().marginAfter();
LayoutUnit& marginBeforeAlias = computedValues.m_margins.m_before;
LayoutUnit& marginAfterAlias = computedValues.m_margins.m_after;
Length logicalTop = style().logicalTop();
Length logicalBottom = style().logicalBottom();
computedValues.m_extent = computeReplacedLogicalHeight() + borderAndPaddingLogicalHeight();
const LayoutUnit availableSpace = containerLogicalHeight - computedValues.m_extent;
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);
logicalBottomValue = valueForLength(logicalBottom, containerLogicalHeight);
LayoutUnit difference = availableSpace - (logicalTopValue + logicalBottomValue);
marginBeforeAlias = difference / 2; marginAfterAlias = difference - marginBeforeAlias;
} else if (logicalTop.isAuto()) {
marginBeforeAlias = valueForLength(marginBefore, containerRelativeLogicalWidth);
marginAfterAlias = valueForLength(marginAfter, containerRelativeLogicalWidth);
logicalBottomValue = valueForLength(logicalBottom, containerLogicalHeight);
logicalTopValue = availableSpace - (logicalBottomValue + marginBeforeAlias + marginAfterAlias);
} else if (logicalBottom.isAuto()) {
marginBeforeAlias = valueForLength(marginBefore, containerRelativeLogicalWidth);
marginAfterAlias = valueForLength(marginAfter, containerRelativeLogicalWidth);
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight);
} else if (marginBefore.isAuto()) {
marginAfterAlias = valueForLength(marginAfter, containerRelativeLogicalWidth);
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight);
logicalBottomValue = valueForLength(logicalBottom, containerLogicalHeight);
marginBeforeAlias = availableSpace - (logicalTopValue + logicalBottomValue + marginAfterAlias);
} else if (marginAfter.isAuto()) {
marginBeforeAlias = valueForLength(marginBefore, containerRelativeLogicalWidth);
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight);
logicalBottomValue = valueForLength(logicalBottom, containerLogicalHeight);
marginAfterAlias = availableSpace - (logicalTopValue + logicalBottomValue + marginBeforeAlias);
} else {
marginBeforeAlias = valueForLength(marginBefore, containerRelativeLogicalWidth);
marginAfterAlias = valueForLength(marginAfter, containerRelativeLogicalWidth);
logicalTopValue = valueForLength(logicalTop, containerLogicalHeight);
}
LayoutUnit logicalTopPos = logicalTopValue + marginBeforeAlias;
computeLogicalTopPositionedOffset(logicalTopPos, this, computedValues.m_extent, containerBlock, containerLogicalHeight);
computedValues.m_position = logicalTopPos;
}
LayoutRect RenderBox::localCaretRect(InlineBox* box, unsigned caretOffset, LayoutUnit* extraWidthToEndOfLine)
{
LayoutRect rect(location(), LayoutSize(caretWidth, height()));
bool ltr = box ? box->isLeftToRightDirection() : style().isLeftToRightDirection();
if ((!caretOffset) ^ ltr)
rect.move(LayoutSize(width() - caretWidth, 0));
if (box) {
const 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());
if (element() && !(editingIgnoresContent(*element()) || isRenderedTable(element()))) {
rect.setX(rect.x() + borderLeft() + paddingLeft());
rect.setY(rect.y() + paddingTop() + borderTop());
}
if (!isHorizontalWritingMode())
return rect.transposedRect();
return rect;
}
VisiblePosition RenderBox::positionForPoint(const LayoutPoint& point, const RenderRegion* region)
{
if (!firstChild())
return createVisiblePosition(nonPseudoElement() ? firstPositionInOrBeforeNode(nonPseudoElement()) : Position());
if (isTable() && nonPseudoElement()) {
LayoutUnit right = contentWidth() + horizontalBorderAndPaddingExtent();
LayoutUnit bottom = contentHeight() + verticalBorderAndPaddingExtent();
if (point.x() < 0 || point.x() > right || point.y() < 0 || point.y() > bottom) {
if (point.x() <= right / 2)
return createVisiblePosition(firstPositionInOrBeforeNode(nonPseudoElement()));
return createVisiblePosition(lastPositionInOrAfterNode(nonPseudoElement()));
}
}
LayoutUnit minDist = LayoutUnit::max();
RenderBox* closestRenderer = nullptr;
LayoutPoint adjustedPoint = point;
if (isTableRow())
adjustedPoint.moveBy(location());
for (auto& renderer : childrenOfType<RenderBox>(*this)) {
if (is<RenderFlowThread>(*this)) {
ASSERT(region);
if (!downcast<RenderFlowThread>(*this).objectShouldFragmentInFlowRegion(&renderer, region))
continue;
}
if ((!renderer.firstChild() && !renderer.isInline() && !is<RenderBlockFlow>(renderer))
|| renderer.style().visibility() != VISIBLE)
continue;
LayoutUnit top = renderer.borderTop() + renderer.paddingTop() + (is<RenderTableRow>(*this) ? LayoutUnit() : renderer.y());
LayoutUnit bottom = top + renderer.contentHeight();
LayoutUnit left = renderer.borderLeft() + renderer.paddingLeft() + (is<RenderTableRow>(*this) ? LayoutUnit() : renderer.x());
LayoutUnit right = left + renderer.contentWidth();
if (point.x() <= right && point.x() >= left && point.y() <= top && point.y() >= bottom) {
if (is<RenderTableRow>(renderer))
return renderer.positionForPoint(point + adjustedPoint - renderer.locationOffset(), region);
return renderer.positionForPoint(point - renderer.locationOffset(), region);
}
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(), region);
return createVisiblePosition(firstPositionInOrBeforeNode(nonPseudoElement()));
}
bool RenderBox::shrinkToAvoidFloats() const
{
if ((isInline() && !isHTMLMarquee()) || !avoidsFloats() || isFloating())
return false;
return style().width().isAuto();
}
bool RenderBox::createsNewFormattingContext() const
{
return (isInlineBlockOrInlineTable() && !isAnonymousInlineBlock()) || isFloatingOrOutOfFlowPositioned() || hasOverflowClip() || isFlexItemIncludingDeprecated()
|| isTableCell() || isTableCaption() || isFieldset() || isWritingModeRoot() || isDocumentElementRenderer() || isRenderFlowThread() || isRenderRegion()
|| isGridItem() || style().specifiesColumns() || style().columnSpan();
}
bool RenderBox::avoidsFloats() const
{
return (isReplaced() && !isAnonymousInlineBlock()) || isHR() || isLegend() || isFieldset() || createsNewFormattingContext();
}
void RenderBox::addVisualEffectOverflow()
{
if (!style().boxShadow() && !style().hasBorderImageOutsets() && !outlineStyleForRepaint().hasOutlineInVisualOverflow())
return;
addVisualOverflow(applyVisualEffectOverflow(borderBoxRect()));
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (flowThread)
flowThread->addRegionsVisualEffectOverflow(this);
}
LayoutRect RenderBox::applyVisualEffectOverflow(const LayoutRect& borderBox) const
{
bool isFlipped = style().isFlippedBlocksWritingMode();
bool isHorizontal = isHorizontalWritingMode();
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()) {
LayoutBoxExtent borderOutsets = style().borderImageOutsets();
overflowMinX = std::min(overflowMinX, borderBox.x() - ((!isFlipped || isHorizontal) ? borderOutsets.left() : borderOutsets.right()));
overflowMaxX = std::max(overflowMaxX, borderBox.maxX() + ((!isFlipped || isHorizontal) ? borderOutsets.right() : borderOutsets.left()));
overflowMinY = std::min(overflowMinY, borderBox.y() - ((!isFlipped || !isHorizontal) ? borderOutsets.top() : borderOutsets.bottom()));
overflowMaxY = std::max(overflowMaxY, borderBox.maxY() + ((!isFlipped || !isHorizontal) ? borderOutsets.bottom() : borderOutsets.top()));
}
if (outlineStyleForRepaint().hasOutlineInVisualOverflow()) {
LayoutUnit outlineSize = outlineStyleForRepaint().outlineSize();
overflowMinX = std::min(overflowMinX, borderBox.x() - outlineSize);
overflowMaxX = std::max(overflowMaxX, borderBox.maxX() + outlineSize);
overflowMinY = std::min(overflowMinY, borderBox.y() - outlineSize);
overflowMaxY = std::max(overflowMaxY, borderBox.maxY() + outlineSize);
}
return LayoutRect(overflowMinX, overflowMinY, overflowMaxX - overflowMinX, overflowMaxY - overflowMinY);
}
void RenderBox::addOverflowFromChild(const RenderBox* child, const LayoutSize& delta)
{
if (child->isRenderFlowThread())
return;
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (flowThread)
flowThread->addRegionsOverflowFromChild(this, child, 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 = flippedClientBoxRect();
if (clientBox.contains(rect) || rect.isEmpty())
return;
LayoutRect overflowRect(rect);
if (hasOverflowClip() || isRenderView()) {
bool hasTopOverflow = isTopLayoutOverflowAllowed();
bool hasLeftOverflow = isLeftLayoutOverflowAllowed();
if (!hasTopOverflow)
overflowRect.shiftYEdgeTo(std::max(overflowRect.y(), clientBox.y()));
else
overflowRect.shiftMaxYEdgeTo(std::min(overflowRect.maxY(), clientBox.maxY()));
if (!hasLeftOverflow)
overflowRect.shiftXEdgeTo(std::max(overflowRect.x(), clientBox.x()));
else
overflowRect.shiftMaxXEdgeTo(std::min(overflowRect.maxX(), clientBox.maxX()));
if (clientBox.contains(overflowRect) || overflowRect.isEmpty())
return;
}
if (!m_overflow)
m_overflow = adoptRef(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 = adoptRef(new RenderOverflow(flippedClientBoxRect(), borderBox));
m_overflow->addVisualOverflow(rect);
}
void RenderBox::clearOverflow()
{
m_overflow = nullptr;
RenderFlowThread* flowThread = flowThreadContainingBlock();
if (flowThread)
flowThread->clearRegionsOverflow(this);
}
bool RenderBox::percentageLogicalHeightIsResolvable() const
{
Length fakeLength(100, Percent);
return computePercentageLogicalHeight(fakeLength) != std::nullopt;
}
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().isPercentOrCalculated() || percentageLogicalHeightIsResolvable()))
|| (!style().logicalMinHeight().isIntrinsicOrAuto() && style().logicalMinHeight().isPositive() && (!style().logicalMinHeight().isPercentOrCalculated() || percentageLogicalHeightIsResolvable()));
}
bool RenderBox::isUnsplittableForPagination() const
{
return isReplaced()
|| hasUnsplittableScrollingOverflow()
|| (parent() && isWritingModeRoot())
|| (isFloating() && style().styleType() == FIRST_LETTER && style().initialLetterDrop() > 0)
|| isRenderNamedFlowFragmentContainer()
|| fixedPositionedWithNamedFlowContainingBlock();
}
LayoutUnit RenderBox::lineHeight(bool , LineDirectionMode direction, LinePositionMode ) const
{
if (isReplaced())
return direction == HorizontalLine ? m_marginBox.top() + height() + m_marginBox.bottom() : m_marginBox.right() + width() + m_marginBox.left();
return 0;
}
int RenderBox::baselinePosition(FontBaseline baselineType, bool , LineDirectionMode direction, LinePositionMode ) const
{
if (isReplaced()) {
int result = direction == HorizontalLine ? m_marginBox.top() + height() + m_marginBox.bottom() : m_marginBox.right() + width() + m_marginBox.left();
if (baselineType == AlphabeticBaseline)
return result;
return result - result / 2;
}
return 0;
}
RenderLayer* RenderBox::enclosingFloatPaintingLayer() const
{
for (auto& box : lineageOfType<RenderBox>(*this)) {
if (box.layer() && box.layer()->isSelfPaintingLayer())
return box.layer();
}
return nullptr;
}
LayoutRect RenderBox::logicalVisualOverflowRectForPropagation(const RenderStyle* parentStyle) const
{
LayoutRect rect = visualOverflowRectForPropagation(parentStyle);
if (!parentStyle->isHorizontalWritingMode())
return rect.transposedRect();
return rect;
}
LayoutRect RenderBox::visualOverflowRectForPropagation(const 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(const RenderStyle* parentStyle) const
{
LayoutRect rect = layoutOverflowRectForPropagation(parentStyle);
if (!parentStyle->isHorizontalWritingMode())
return rect.transposedRect();
return rect;
}
LayoutRect RenderBox::layoutOverflowRectForPropagation(const RenderStyle* parentStyle) const
{
LayoutRect rect = borderBoxRect();
if (!hasOverflowClip())
rect.unite(layoutOverflowRect());
bool hasTransform = this->hasTransform();
if (isInFlowPositioned() || hasTransform) {
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;
}
LayoutRect RenderBox::flippedClientBoxRect() const
{
LayoutUnit left = borderLeft();
LayoutUnit top = borderTop();
LayoutUnit right = borderRight();
LayoutUnit bottom = borderBottom();
LayoutRect rect(left, top, width() - left - right, height() - top - bottom);
flipForWritingMode(rect);
if (shouldPlaceBlockDirectionScrollbarOnLeft())
rect.move(verticalScrollbarWidth(), 0);
rect.contract(verticalScrollbarWidth(), horizontalScrollbarHeight());
return rect;
}
LayoutRect RenderBox::overflowRectForPaintRejection(RenderNamedFlowFragment* namedFlowFragment) const
{
LayoutRect overflowRect = visualOverflowRect();
if (namedFlowFragment && namedFlowFragment->isValid()) {
RenderFlowThread* flowThread = namedFlowFragment->flowThread();
RenderRegion* startRegion = nullptr;
RenderRegion* endRegion = nullptr;
if (flowThread->getRegionRangeForBox(this, startRegion, endRegion))
overflowRect.unite(namedFlowFragment->visualOverflowRectForBox(*this));
}
if (!m_overflow || !usesCompositedScrolling())
return overflowRect;
overflowRect.unite(layoutOverflowRect());
overflowRect.moveBy(-scrollPosition());
return overflowRect;
}
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());
}
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
{
if (!view().frameView().hasFlippedBlockRenderers())
return location();
RenderBlock* containerBlock = containingBlock();
if (!containerBlock || containerBlock == this)
return location();
return containerBlock->flipForWritingModeForChild(this, location());
}
LayoutSize RenderBox::topLeftLocationOffset() const
{
if (!view().frameView().hasFlippedBlockRenderers())
return locationOffset();
RenderBlock* containerBlock = containingBlock();
if (!containerBlock || containerBlock == this)
return locationOffset();
LayoutRect rect(frameRect());
containerBlock->flipForWritingMode(rect); return LayoutSize(rect.x(), rect.y());
}
void RenderBox::applyTopLeftLocationOffsetWithFlipping(LayoutPoint& point) const
{
RenderBlock* containerBlock = containingBlock();
if (!containerBlock || containerBlock == this) {
point.move(m_frameRect.x(), m_frameRect.y());
return;
}
LayoutRect rect(frameRect());
containerBlock->flipForWritingMode(rect); point.move(rect.x(), rect.y());
}
bool RenderBox::hasRelativeDimensions() const
{
return style().height().isPercentOrCalculated() || style().width().isPercentOrCalculated()
|| style().maxHeight().isPercentOrCalculated() || style().maxWidth().isPercentOrCalculated()
|| style().minHeight().isPercentOrCalculated() || style().minWidth().isPercentOrCalculated();
}
bool RenderBox::hasRelativeLogicalHeight() const
{
return style().logicalHeight().isPercentOrCalculated()
|| style().logicalMinHeight().isPercentOrCalculated()
|| style().logicalMaxHeight().isPercentOrCalculated();
}
bool RenderBox::hasRelativeLogicalWidth() const
{
return style().logicalWidth().isPercentOrCalculated()
|| style().logicalMinWidth().isPercentOrCalculated()
|| style().logicalMaxWidth().isPercentOrCalculated();
}
static void markBoxForRelayoutAfterSplit(RenderBox& box)
{
if (is<RenderTable>(box)) {
downcast<RenderTable>(box).forceSectionsRecalc();
} else if (is<RenderTableSection>(box))
downcast<RenderTableSection>(box).setNeedsCellRecalc();
box.setNeedsLayoutAndPrefWidthsRecalc();
}
RenderObject* RenderBox::splitAnonymousBoxesAroundChild(RenderObject* beforeChild)
{
bool didSplitParentAnonymousBoxes = false;
while (beforeChild->parent() != this) {
auto& boxToSplit = downcast<RenderBox>(*beforeChild->parent());
if (boxToSplit.firstChild() != beforeChild && boxToSplit.isAnonymous()) {
didSplitParentAnonymousBoxes = true;
auto* postBox = boxToSplit.createAnonymousBoxWithSameTypeAs(*this).release();
postBox->setChildrenInline(boxToSplit.childrenInline());
RenderBox* parentBox = downcast<RenderBox>(boxToSplit.parent());
markBoxForRelayoutAfterSplit(*parentBox);
parentBox->insertChildInternal(postBox, boxToSplit.nextSibling(), NotifyChildren);
boxToSplit.moveChildrenTo(postBox, beforeChild, nullptr, true);
markBoxForRelayoutAfterSplit(boxToSplit);
markBoxForRelayoutAfterSplit(*postBox);
beforeChild = postBox;
} else
beforeChild = &boxToSplit;
}
if (didSplitParentAnonymousBoxes)
markBoxForRelayoutAfterSplit(*this);
ASSERT(beforeChild->parent() == this);
return beforeChild;
}
LayoutUnit RenderBox::offsetFromLogicalTopOfFirstPage() const
{
LayoutState* layoutState = view().layoutState();
if ((layoutState && !layoutState->isPaginated()) || (!layoutState && !flowThreadContainingBlock()))
return 0;
RenderBlock* containerBlock = containingBlock();
return containerBlock->offsetFromLogicalTopOfFirstPage() + logicalTop();
}
const RenderBox* RenderBox::findEnclosingScrollableContainer() const
{
for (auto& candidate : lineageOfType<RenderBox>(*this)) {
if (candidate.hasOverflowClip())
return &candidate;
}
if (document().body() && frame().mainFrame().view() && frame().mainFrame().view()->isScrollable())
return document().body()->renderBox();
return nullptr;
}
}