1 /*
   2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
   3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
   4  * Copyright (C) 2003, 2006, 2007, 2015 Apple Inc. All rights reserved.
   5  *
   6  * This library is free software; you can redistribute it and/or
   7  * modify it under the terms of the GNU Library General Public
   8  * License as published by the Free Software Foundation; either
   9  * version 2 of the License, or (at your option) any later version.
  10  *
  11  * This library is distributed in the hope that it will be useful,
  12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14  * Library General Public License for more details.
  15  *
  16  * You should have received a copy of the GNU Library General Public License
  17  * along with this library; see the file COPYING.LIB.  If not, write to
  18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19  * Boston, MA 02110-1301, USA.
  20  *
  21  */
  22 
  23 #pragma once
  24 
  25 #include "FrameView.h"
  26 #include "RenderBoxModelObject.h"
  27 #include "RenderOverflow.h"
  28 #include "ScrollTypes.h"
  29 #include "ShapeOutsideInfo.h"
  30 
  31 namespace WebCore {
  32 
  33 class InlineElementBox;
  34 class RenderBlockFlow;
  35 class RenderBoxFragmentInfo;
  36 class RenderFragmentContainer;
  37 struct PaintInfo;
  38 
  39 enum SizeType { MainOrPreferredSize, MinSize, MaxSize };
  40 enum AvailableLogicalHeightType { ExcludeMarginBorderPadding, IncludeMarginBorderPadding };
  41 enum OverlayScrollbarSizeRelevancy { IgnoreOverlayScrollbarSize, IncludeOverlayScrollbarSize };
  42 
  43 enum ShouldComputePreferred { ComputeActual, ComputePreferred };
  44 
  45 class RenderBox : public RenderBoxModelObject {
  46     WTF_MAKE_ISO_ALLOCATED(RenderBox);
  47 public:
  48     virtual ~RenderBox();
  49 
  50     // hasAutoZIndex only returns true if the element is positioned or a flex-item since
  51     // position:static elements that are not flex-items get their z-index coerced to auto.
  52     bool requiresLayer() const override
  53     {
  54         return isDocumentElementRenderer() || isPositioned() || createsGroup() || hasClipPath() || hasOverflowClip()
  55             || hasTransformRelatedProperty() || hasHiddenBackface() || hasReflection() || style().specifiesColumns()
  56             || !style().hasAutoZIndex();
  57     }
  58 
  59     bool backgroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect) const final;
  60 
  61     // Returns false for the body renderer if its background is propagated to the root.
  62     bool paintsOwnBackground() const;
  63 
  64     // Use this with caution! No type checking is done!
  65     RenderBox* firstChildBox() const;
  66     RenderBox* lastChildBox() const;
  67 
  68     LayoutUnit x() const { return m_frameRect.x(); }
  69     LayoutUnit y() const { return m_frameRect.y(); }
  70     LayoutUnit width() const { return m_frameRect.width(); }
  71     LayoutUnit height() const { return m_frameRect.height(); }
  72 
  73     // These represent your location relative to your container as a physical offset.
  74     // In layout related methods you almost always want the logical location (e.g. x() and y()).
  75     LayoutUnit top() const { return topLeftLocation().y(); }
  76     LayoutUnit left() const { return topLeftLocation().x(); }
  77 
  78     void setX(LayoutUnit x) { m_frameRect.setX(x); }
  79     void setY(LayoutUnit y) { m_frameRect.setY(y); }
  80     void setWidth(LayoutUnit width) { m_frameRect.setWidth(width); }
  81     void setHeight(LayoutUnit height) { m_frameRect.setHeight(height); }
  82 
  83     LayoutUnit logicalLeft() const { return style().isHorizontalWritingMode() ? x() : y(); }
  84     LayoutUnit logicalRight() const { return logicalLeft() + logicalWidth(); }
  85     LayoutUnit logicalTop() const { return style().isHorizontalWritingMode() ? y() : x(); }
  86     LayoutUnit logicalBottom() const { return logicalTop() + logicalHeight(); }
  87     LayoutUnit logicalWidth() const { return style().isHorizontalWritingMode() ? width() : height(); }
  88     LayoutUnit logicalHeight() const { return style().isHorizontalWritingMode() ? height() : width(); }
  89 
  90     LayoutUnit constrainLogicalWidthInFragmentByMinMax(LayoutUnit, LayoutUnit, RenderBlock&, RenderFragmentContainer* = nullptr) const;
  91     LayoutUnit constrainLogicalHeightByMinMax(LayoutUnit logicalHeight, std::optional<LayoutUnit> intrinsicContentHeight) const;
  92     LayoutUnit constrainContentBoxLogicalHeightByMinMax(LayoutUnit logicalHeight, std::optional<LayoutUnit> intrinsicContentHeight) const;
  93 
  94     void setLogicalLeft(LayoutUnit left)
  95     {
  96         if (style().isHorizontalWritingMode())
  97             setX(left);
  98         else
  99             setY(left);
 100     }
 101     void setLogicalTop(LayoutUnit top)
 102     {
 103         if (style().isHorizontalWritingMode())
 104             setY(top);
 105         else
 106             setX(top);
 107     }
 108     void setLogicalLocation(const LayoutPoint& location)
 109     {
 110         if (style().isHorizontalWritingMode())
 111             setLocation(location);
 112         else
 113             setLocation(location.transposedPoint());
 114     }
 115     void setLogicalWidth(LayoutUnit size)
 116     {
 117         if (style().isHorizontalWritingMode())
 118             setWidth(size);
 119         else
 120             setHeight(size);
 121     }
 122     void setLogicalHeight(LayoutUnit size)
 123     {
 124         if (style().isHorizontalWritingMode())
 125             setHeight(size);
 126         else
 127             setWidth(size);
 128     }
 129     void setLogicalSize(const LayoutSize& size)
 130     {
 131         if (style().isHorizontalWritingMode())
 132             setSize(size);
 133         else
 134             setSize(size.transposedSize());
 135     }
 136 
 137     LayoutPoint location() const { return m_frameRect.location(); }
 138     LayoutSize locationOffset() const { return LayoutSize(x(), y()); }
 139     LayoutSize size() const { return m_frameRect.size(); }
 140 
 141     void setLocation(const LayoutPoint& location) { m_frameRect.setLocation(location); }
 142 
 143     void setSize(const LayoutSize& size) { m_frameRect.setSize(size); }
 144     void move(LayoutUnit dx, LayoutUnit dy) { m_frameRect.move(dx, dy); }
 145 
 146     LayoutRect frameRect() const { return m_frameRect; }
 147     void setFrameRect(const LayoutRect& rect) { m_frameRect = rect; }
 148 
 149     LayoutRect marginBoxRect() const
 150     {
 151         LayoutRect box = borderBoxRect();
 152         box.expand(m_marginBox);
 153         return box;
 154     }
 155     LayoutRect borderBoxRect() const { return LayoutRect(LayoutPoint(), size()); }
 156     LayoutRect paddingBoxRect() const { return LayoutRect(borderLeft(), borderTop(), contentWidth() + paddingLeft() + paddingRight(), contentHeight() + paddingTop() + paddingBottom()); }
 157     LayoutRect borderBoundingBox() const final { return borderBoxRect(); }
 158 
 159     WEBCORE_EXPORT RoundedRect::Radii borderRadii() const;
 160 
 161     // The content area of the box (excludes padding - and intrinsic padding for table cells, etc... - and border).
 162     LayoutRect contentBoxRect() const;
 163     LayoutPoint contentBoxLocation() const;
 164 
 165     // The content box in absolute coords. Ignores transforms.
 166     IntRect absoluteContentBox() const;
 167     // The content box converted to absolute coords (taking transforms into account).
 168     WEBCORE_EXPORT FloatQuad absoluteContentQuad() const;
 169 
 170     // This returns the content area of the box (excluding padding and border). The only difference with contentBoxRect is that computedCSSContentBoxRect
 171     // does include the intrinsic padding in the content box as this is what some callers expect (like getComputedStyle).
 172     LayoutRect computedCSSContentBoxRect() const { return LayoutRect(borderLeft() + computedCSSPaddingLeft(), borderTop() + computedCSSPaddingTop(), clientWidth() - computedCSSPaddingLeft() - computedCSSPaddingRight(), clientHeight() - computedCSSPaddingTop() - computedCSSPaddingBottom()); }
 173 
 174     // Bounds of the outline box in absolute coords. Respects transforms
 175     LayoutRect outlineBoundsForRepaint(const RenderLayerModelObject* /*repaintContainer*/, const RenderGeometryMap*) const final;
 176     void addFocusRingRects(Vector<LayoutRect>&, const LayoutPoint& additionalOffset, const RenderLayerModelObject* paintContainer = nullptr) override;
 177 
 178     FloatRect repaintRectInLocalCoordinates() const override { return borderBoxRect(); }
 179     FloatRect objectBoundingBox() const override { return borderBoxRect(); }
 180 
 181     // Use this with caution! No type checking is done!
 182     RenderBox* previousSiblingBox() const;
 183     RenderBox* nextSiblingBox() const;
 184     RenderBox* parentBox() const;
 185 
 186     // Visual and layout overflow are in the coordinate space of the box.  This means that they aren't purely physical directions.
 187     // For horizontal-tb and vertical-lr they will match physical directions, but for horizontal-bt and vertical-rl, the top/bottom and left/right
 188     // respectively are flipped when compared to their physical counterparts.  For example minX is on the left in vertical-lr,
 189     // but it is on the right in vertical-rl.
 190     WEBCORE_EXPORT LayoutRect flippedClientBoxRect() const;
 191     LayoutRect layoutOverflowRect() const { return m_overflow ? m_overflow->layoutOverflowRect() : flippedClientBoxRect(); }
 192     LayoutUnit logicalLeftLayoutOverflow() const { return style().isHorizontalWritingMode() ? layoutOverflowRect().x() : layoutOverflowRect().y(); }
 193     LayoutUnit logicalRightLayoutOverflow() const { return style().isHorizontalWritingMode() ? layoutOverflowRect().maxX() : layoutOverflowRect().maxY(); }
 194 
 195     virtual LayoutRect visualOverflowRect() const { return m_overflow ? m_overflow->visualOverflowRect() : borderBoxRect(); }
 196     LayoutUnit logicalLeftVisualOverflow() const { return style().isHorizontalWritingMode() ? visualOverflowRect().x() : visualOverflowRect().y(); }
 197     LayoutUnit logicalRightVisualOverflow() const { return style().isHorizontalWritingMode() ? visualOverflowRect().maxX() : visualOverflowRect().maxY(); }
 198 
 199     LayoutRect overflowRectForPaintRejection() const;
 200 
 201     void addLayoutOverflow(const LayoutRect&);
 202     void addVisualOverflow(const LayoutRect&);
 203     void clearOverflow();
 204 
 205     virtual bool isTopLayoutOverflowAllowed() const { return !style().isLeftToRightDirection() && !isHorizontalWritingMode(); }
 206     virtual bool isLeftLayoutOverflowAllowed() const { return !style().isLeftToRightDirection() && isHorizontalWritingMode(); }
 207 
 208     void addVisualEffectOverflow();
 209     LayoutRect applyVisualEffectOverflow(const LayoutRect&) const;
 210     void addOverflowFromChild(const RenderBox* child) { addOverflowFromChild(child, child->locationOffset()); }
 211     void addOverflowFromChild(const RenderBox* child, const LayoutSize& delta);
 212 
 213     void updateLayerTransform();
 214 
 215     LayoutSize contentSize() const { return { contentWidth(), contentHeight() }; }
 216     LayoutUnit contentWidth() const { return clientWidth() - paddingLeft() - paddingRight(); }
 217     LayoutUnit contentHeight() const { return clientHeight() - paddingTop() - paddingBottom(); }
 218     LayoutUnit contentLogicalWidth() const { return style().isHorizontalWritingMode() ? contentWidth() : contentHeight(); }
 219     LayoutUnit contentLogicalHeight() const { return style().isHorizontalWritingMode() ? contentHeight() : contentWidth(); }
 220 
 221     // IE extensions. Used to calculate offsetWidth/Height.  Overridden by inlines (RenderFlow)
 222     // to return the remaining width on a given line (and the height of a single line).
 223     LayoutUnit offsetWidth() const override { return width(); }
 224     LayoutUnit offsetHeight() const override { return height(); }
 225 
 226     // More IE extensions.  clientWidth and clientHeight represent the interior of an object
 227     // excluding border and scrollbar.  clientLeft/Top are just the borderLeftWidth and borderTopWidth.
 228     LayoutUnit clientLeft() const { return borderLeft(); }
 229     LayoutUnit clientTop() const { return borderTop(); }
 230     WEBCORE_EXPORT LayoutUnit clientWidth() const;
 231     WEBCORE_EXPORT LayoutUnit clientHeight() const;
 232     LayoutUnit clientLogicalWidth() const { return style().isHorizontalWritingMode() ? clientWidth() : clientHeight(); }
 233     LayoutUnit clientLogicalHeight() const { return style().isHorizontalWritingMode() ? clientHeight() : clientWidth(); }
 234     LayoutUnit clientLogicalBottom() const { return borderBefore() + clientLogicalHeight(); }
 235     LayoutRect clientBoxRect() const { return LayoutRect(clientLeft(), clientTop(), clientWidth(), clientHeight()); }
 236 
 237     // scrollWidth/scrollHeight will be the same as clientWidth/clientHeight unless the
 238     // object has overflow:hidden/scroll/auto specified and also has overflow.
 239     // scrollLeft/Top return the current scroll position.  These methods are virtual so that objects like
 240     // textareas can scroll shadow content (but pretend that they are the objects that are
 241     // scrolling).
 242     virtual int scrollLeft() const;
 243     virtual int scrollTop() const;
 244     virtual int scrollWidth() const;
 245     virtual int scrollHeight() const;
 246     virtual void setScrollLeft(int, ScrollClamping = ScrollClamping::Clamped);
 247     virtual void setScrollTop(int, ScrollClamping = ScrollClamping::Clamped);
 248 
 249     LayoutUnit marginTop() const override { return m_marginBox.top(); }
 250     LayoutUnit marginBottom() const override { return m_marginBox.bottom(); }
 251     LayoutUnit marginLeft() const override { return m_marginBox.left(); }
 252     LayoutUnit marginRight() const override { return m_marginBox.right(); }
 253     void setMarginTop(LayoutUnit margin) { m_marginBox.setTop(margin); }
 254     void setMarginBottom(LayoutUnit margin) { m_marginBox.setBottom(margin); }
 255     void setMarginLeft(LayoutUnit margin) { m_marginBox.setLeft(margin); }
 256     void setMarginRight(LayoutUnit margin) { m_marginBox.setRight(margin); }
 257 
 258     LayoutUnit marginLogicalLeft() const { return m_marginBox.start(style().writingMode()); }
 259     LayoutUnit marginLogicalRight() const { return m_marginBox.end(style().writingMode()); }
 260 
 261     LayoutUnit marginBefore(const RenderStyle* overrideStyle = nullptr) const final { return m_marginBox.before((overrideStyle ? overrideStyle : &style())->writingMode()); }
 262     LayoutUnit marginAfter(const RenderStyle* overrideStyle = nullptr) const final { return m_marginBox.after((overrideStyle ? overrideStyle : &style())->writingMode()); }
 263     LayoutUnit marginStart(const RenderStyle* overrideStyle = nullptr) const final
 264     {
 265         const RenderStyle* styleToUse = overrideStyle ? overrideStyle : &style();
 266         return m_marginBox.start(styleToUse->writingMode(), styleToUse->direction());
 267     }
 268     LayoutUnit marginEnd(const RenderStyle* overrideStyle = nullptr) const final
 269     {
 270         const RenderStyle* styleToUse = overrideStyle ? overrideStyle : &style();
 271         return m_marginBox.end(styleToUse->writingMode(), styleToUse->direction());
 272     }
 273     void setMarginBefore(LayoutUnit value, const RenderStyle* overrideStyle = nullptr) { m_marginBox.setBefore(value, (overrideStyle ? overrideStyle : &style())->writingMode()); }
 274     void setMarginAfter(LayoutUnit value, const RenderStyle* overrideStyle = nullptr) { m_marginBox.setAfter(value, (overrideStyle ? overrideStyle : &style())->writingMode()); }
 275     void setMarginStart(LayoutUnit value, const RenderStyle* overrideStyle = nullptr)
 276     {
 277         const RenderStyle* styleToUse = overrideStyle ? overrideStyle : &style();
 278         m_marginBox.setStart(value, styleToUse->writingMode(), styleToUse->direction());
 279     }
 280     void setMarginEnd(LayoutUnit value, const RenderStyle* overrideStyle = nullptr)
 281     {
 282         const RenderStyle* styleToUse = overrideStyle ? overrideStyle : &style();
 283         m_marginBox.setEnd(value, styleToUse->writingMode(), styleToUse->direction());
 284     }
 285 
 286     virtual bool isSelfCollapsingBlock() const { return false; }
 287     virtual LayoutUnit collapsedMarginBefore() const { return marginBefore(); }
 288     virtual LayoutUnit collapsedMarginAfter() const { return marginAfter(); }
 289 
 290     void absoluteRects(Vector<IntRect>&, const LayoutPoint& accumulatedOffset) const override;
 291     void absoluteQuads(Vector<FloatQuad>&, bool* wasFixed) const override;
 292 
 293     int reflectionOffset() const;
 294     // Given a rect in the object's coordinate space, returns the corresponding rect in the reflection.
 295     LayoutRect reflectedRect(const LayoutRect&) const;
 296 
 297     void layout() override;
 298     bool nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction) override;
 299 
 300     LayoutUnit minPreferredLogicalWidth() const override;
 301     LayoutUnit maxPreferredLogicalWidth() const override;
 302 
 303     // FIXME: We should rename these back to overrideLogicalHeight/Width and have them store
 304     // the border-box height/width like the regular height/width accessors on RenderBox.
 305     // Right now, these are different than contentHeight/contentWidth because they still
 306     // include the scrollbar height/width.
 307     LayoutUnit overrideLogicalContentWidth() const;
 308     LayoutUnit overrideLogicalContentHeight() const;
 309     bool hasOverrideLogicalContentHeight() const;
 310     bool hasOverrideLogicalContentWidth() const;
 311     void setOverrideLogicalContentHeight(LayoutUnit);
 312     void setOverrideLogicalContentWidth(LayoutUnit);
 313     void clearOverrideSize();
 314     void clearOverrideLogicalContentHeight();
 315     void clearOverrideLogicalContentWidth();
 316 
 317     std::optional<LayoutUnit> overrideContainingBlockContentLogicalWidth() const;
 318     std::optional<LayoutUnit> overrideContainingBlockContentLogicalHeight() const;
 319     bool hasOverrideContainingBlockLogicalWidth() const;
 320     bool hasOverrideContainingBlockLogicalHeight() const;
 321     void setOverrideContainingBlockContentLogicalWidth(std::optional<LayoutUnit>);
 322     void setOverrideContainingBlockContentLogicalHeight(std::optional<LayoutUnit>);
 323     void clearContainingBlockOverrideSize();
 324     void clearOverrideContainingBlockContentLogicalHeight();
 325 
 326     LayoutSize offsetFromContainer(RenderElement&, const LayoutPoint&, bool* offsetDependsOnPoint = nullptr) const override;
 327 
 328     LayoutUnit adjustBorderBoxLogicalWidthForBoxSizing(LayoutUnit width) const;
 329     LayoutUnit adjustContentBoxLogicalWidthForBoxSizing(LayoutUnit width) const;
 330 
 331     // Overridden by fieldsets to subtract out the intrinsic border.
 332     virtual LayoutUnit adjustBorderBoxLogicalHeightForBoxSizing(LayoutUnit height) const;
 333     virtual LayoutUnit adjustContentBoxLogicalHeightForBoxSizing(std::optional<LayoutUnit> height) const;
 334 
 335     struct ComputedMarginValues {
 336         LayoutUnit m_before;
 337         LayoutUnit m_after;
 338         LayoutUnit m_start;
 339         LayoutUnit m_end;
 340     };
 341     struct LogicalExtentComputedValues {
 342         LayoutUnit m_extent;
 343         LayoutUnit m_position;
 344         ComputedMarginValues m_margins;
 345     };
 346     // Resolve auto margins in the inline direction of the containing block so that objects can be pushed to the start, middle or end
 347     // of the containing block.
 348     void computeInlineDirectionMargins(const RenderBlock& containingBlock, LayoutUnit containerWidth, LayoutUnit childWidth, LayoutUnit& marginStart, LayoutUnit& marginEnd) const;
 349 
 350     // Used to resolve margins in the containing block's block-flow direction.
 351     void computeBlockDirectionMargins(const RenderBlock& containingBlock, LayoutUnit& marginBefore, LayoutUnit& marginAfter) const;
 352     void computeAndSetBlockDirectionMargins(const RenderBlock& containingBlock);
 353 
 354     enum RenderBoxFragmentInfoFlags { CacheRenderBoxFragmentInfo, DoNotCacheRenderBoxFragmentInfo };
 355     LayoutRect borderBoxRectInFragment(RenderFragmentContainer*, RenderBoxFragmentInfoFlags = CacheRenderBoxFragmentInfo) const;
 356     LayoutRect clientBoxRectInFragment(RenderFragmentContainer*) const;
 357     RenderFragmentContainer* clampToStartAndEndFragments(RenderFragmentContainer*) const;
 358     bool hasFragmentRangeInFragmentedFlow() const;
 359     virtual LayoutUnit offsetFromLogicalTopOfFirstPage() const;
 360 
 361     void positionLineBox(InlineElementBox&);
 362 
 363     virtual std::unique_ptr<InlineElementBox> createInlineBox();
 364     void dirtyLineBoxes(bool fullLayout);
 365 
 366     // For inline replaced elements, this function returns the inline box that owns us.  Enables
 367     // the replaced RenderObject to quickly determine what line it is contained on and to easily
 368     // iterate over structures on the line.
 369     InlineElementBox* inlineBoxWrapper() const { return m_inlineBoxWrapper; }
 370     void setInlineBoxWrapper(InlineElementBox*);
 371     void deleteLineBoxWrapper();
 372 
 373     LayoutRect clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const override;
 374     LayoutRect computeRectForRepaint(const LayoutRect&, const RenderLayerModelObject* repaintContainer, RepaintContext context = { false, false }) const override;
 375     void repaintDuringLayoutIfMoved(const LayoutRect&);
 376     virtual void repaintOverhangingFloats(bool paintAllDescendants);
 377 
 378     LayoutUnit containingBlockLogicalWidthForContent() const override;
 379     LayoutUnit containingBlockLogicalHeightForContent(AvailableLogicalHeightType) const;
 380 
 381     LayoutUnit containingBlockLogicalWidthForContentInFragment(RenderFragmentContainer*) const;
 382     LayoutUnit containingBlockAvailableLineWidthInFragment(RenderFragmentContainer*) const;
 383     LayoutUnit perpendicularContainingBlockLogicalHeight() const;
 384 
 385     virtual void updateLogicalWidth();
 386     virtual void updateLogicalHeight();
 387     virtual LogicalExtentComputedValues computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop) const;
 388 
 389     void cacheIntrinsicContentLogicalHeightForFlexItem(LayoutUnit) const;
 390 
 391     // This function will compute the logical border-box height, without laying
 392     // out the box. This means that the result is only "correct" when the height
 393     // is explicitly specified. This function exists so that intrinsic width
 394     // calculations have a way to deal with children that have orthogonal writing modes.
 395     // When there is no explicit height, this function assumes a content height of
 396     // zero (and returns just border + padding).
 397     LayoutUnit computeLogicalHeightWithoutLayout() const;
 398 
 399     RenderBoxFragmentInfo* renderBoxFragmentInfo(RenderFragmentContainer*, RenderBoxFragmentInfoFlags = CacheRenderBoxFragmentInfo) const;
 400     void computeLogicalWidthInFragment(LogicalExtentComputedValues&, RenderFragmentContainer* = nullptr) const;
 401 
 402     bool stretchesToViewport() const
 403     {
 404         return document().inQuirksMode() && style().logicalHeight().isAuto() && !isFloatingOrOutOfFlowPositioned() && (isDocumentElementRenderer() || isBody()) && !isInline();
 405     }
 406 
 407     virtual LayoutSize intrinsicSize() const { return LayoutSize(); }
 408     LayoutUnit intrinsicLogicalWidth() const { return style().isHorizontalWritingMode() ? intrinsicSize().width() : intrinsicSize().height(); }
 409     LayoutUnit intrinsicLogicalHeight() const { return style().isHorizontalWritingMode() ? intrinsicSize().height() : intrinsicSize().width(); }
 410 
 411     // Whether or not the element shrinks to its intrinsic width (rather than filling the width
 412     // of a containing block).  HTML4 buttons, <select>s, <input>s, legends, and floating/compact elements do this.
 413     bool sizesLogicalWidthToFitContent(SizeType) const;
 414 
 415     bool hasStretchedLogicalWidth() const;
 416     bool isStretchingColumnFlexItem() const;
 417     bool columnFlexItemHasStretchAlignment() const;
 418 
 419     LayoutUnit shrinkLogicalWidthToAvoidFloats(LayoutUnit childMarginStart, LayoutUnit childMarginEnd, const RenderBlock& cb, RenderFragmentContainer*) const;
 420 
 421     LayoutUnit computeLogicalWidthInFragmentUsing(SizeType, Length logicalWidth, LayoutUnit availableLogicalWidth, const RenderBlock& containingBlock, RenderFragmentContainer*) const;
 422     std::optional<LayoutUnit> computeLogicalHeightUsing(SizeType, const Length& height, std::optional<LayoutUnit> intrinsicContentHeight) const;
 423     std::optional<LayoutUnit> computeContentLogicalHeight(SizeType, const Length& height, std::optional<LayoutUnit> intrinsicContentHeight) const;
 424     std::optional<LayoutUnit> computeContentAndScrollbarLogicalHeightUsing(SizeType, const Length& height, std::optional<LayoutUnit> intrinsicContentHeight) const;
 425     LayoutUnit computeReplacedLogicalWidthUsing(SizeType, Length width) const;
 426     LayoutUnit computeReplacedLogicalWidthRespectingMinMaxWidth(LayoutUnit logicalWidth, ShouldComputePreferred  = ComputeActual) const;
 427     LayoutUnit computeReplacedLogicalHeightUsing(SizeType, Length height) const;
 428     LayoutUnit computeReplacedLogicalHeightRespectingMinMaxHeight(LayoutUnit logicalHeight) const;
 429 
 430     virtual LayoutUnit computeReplacedLogicalWidth(ShouldComputePreferred  = ComputeActual) const;
 431     virtual LayoutUnit computeReplacedLogicalHeight(std::optional<LayoutUnit> estimatedUsedWidth = std::nullopt) const;
 432 
 433     std::optional<LayoutUnit> computePercentageLogicalHeight(const Length& height) const;
 434 
 435     virtual LayoutUnit availableLogicalWidth() const { return contentLogicalWidth(); }
 436     virtual LayoutUnit availableLogicalHeight(AvailableLogicalHeightType) const;
 437     LayoutUnit availableLogicalHeightUsing(const Length&, AvailableLogicalHeightType) const;
 438 
 439     // There are a few cases where we need to refer specifically to the available physical width and available physical height.
 440     // Relative positioning is one of those cases, since left/top offsets are physical.
 441     LayoutUnit availableWidth() const { return style().isHorizontalWritingMode() ? availableLogicalWidth() : availableLogicalHeight(IncludeMarginBorderPadding); }
 442     LayoutUnit availableHeight() const { return style().isHorizontalWritingMode() ? availableLogicalHeight(IncludeMarginBorderPadding) : availableLogicalWidth(); }
 443 
 444     virtual int verticalScrollbarWidth() const;
 445     int horizontalScrollbarHeight() const;
 446     int intrinsicScrollbarLogicalWidth() const;
 447     int scrollbarLogicalWidth() const { return style().isHorizontalWritingMode() ? verticalScrollbarWidth() : horizontalScrollbarHeight(); }
 448     int scrollbarLogicalHeight() const { return style().isHorizontalWritingMode() ? horizontalScrollbarHeight() : verticalScrollbarWidth(); }
 449     virtual bool scroll(ScrollDirection, ScrollGranularity, float multiplier = 1, Element** stopElement = nullptr, RenderBox* startBox = nullptr, const IntPoint& wheelEventAbsolutePoint = IntPoint());
 450     virtual bool logicalScroll(ScrollLogicalDirection, ScrollGranularity, float multiplier = 1, Element** stopElement = nullptr);
 451     WEBCORE_EXPORT bool canBeScrolledAndHasScrollableArea() const;
 452     virtual bool canBeProgramaticallyScrolled() const;
 453     virtual void autoscroll(const IntPoint&);
 454     bool canAutoscroll() const;
 455     IntSize calculateAutoscrollDirection(const IntPoint& windowPoint) const;
 456     static RenderBox* findAutoscrollable(RenderObject*);
 457     virtual void stopAutoscroll() { }
 458     virtual void panScroll(const IntPoint&);
 459 
 460     bool hasVerticalScrollbarWithAutoBehavior() const;
 461     bool hasHorizontalScrollbarWithAutoBehavior() const;
 462 
 463     bool scrollsOverflow() const { return scrollsOverflowX() || scrollsOverflowY(); }
 464     bool scrollsOverflowX() const { return hasOverflowClip() && (style().overflowX() == OSCROLL || hasHorizontalScrollbarWithAutoBehavior()); }
 465     bool scrollsOverflowY() const { return hasOverflowClip() && (style().overflowY() == OSCROLL || hasVerticalScrollbarWithAutoBehavior()); }
 466 
 467     bool hasHorizontalOverflow() const { return scrollWidth() != roundToInt(clientWidth()); }
 468     bool hasVerticalOverflow() const { return scrollHeight() != roundToInt(clientHeight()); }
 469 
 470     bool hasScrollableOverflowX() const { return scrollsOverflowX() && hasHorizontalOverflow(); }
 471     bool hasScrollableOverflowY() const { return scrollsOverflowY() && hasVerticalOverflow(); }
 472 
 473     bool usesCompositedScrolling() const;
 474 
 475     bool percentageLogicalHeightIsResolvable() const;
 476     bool hasUnsplittableScrollingOverflow() const;
 477     bool isUnsplittableForPagination() const;
 478 
 479     bool shouldTreatChildAsReplacedInTableCells() const;
 480 
 481     LayoutRect localCaretRect(InlineBox*, unsigned caretOffset, LayoutUnit* extraWidthToEndOfLine = nullptr) override;
 482 
 483     virtual LayoutRect overflowClipRect(const LayoutPoint& location, RenderFragmentContainer* = nullptr, OverlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize, PaintPhase = PaintPhaseBlockBackground);
 484     virtual LayoutRect overflowClipRectForChildLayers(const LayoutPoint& location, RenderFragmentContainer* fragment, OverlayScrollbarSizeRelevancy relevancy) { return overflowClipRect(location, fragment, relevancy); }
 485     LayoutRect clipRect(const LayoutPoint& location, RenderFragmentContainer*);
 486     virtual bool hasControlClip() const { return false; }
 487     virtual LayoutRect controlClipRect(const LayoutPoint&) const { return LayoutRect(); }
 488     bool pushContentsClip(PaintInfo&, const LayoutPoint& accumulatedOffset);
 489     void popContentsClip(PaintInfo&, PaintPhase originalPhase, const LayoutPoint& accumulatedOffset);
 490 
 491     virtual void paintObject(PaintInfo&, const LayoutPoint&) { ASSERT_NOT_REACHED(); }
 492     virtual void paintBoxDecorations(PaintInfo&, const LayoutPoint&);
 493     virtual void paintMask(PaintInfo&, const LayoutPoint&);
 494     virtual void paintClippingMask(PaintInfo&, const LayoutPoint&);
 495     void imageChanged(WrappedImagePtr, const IntRect* = nullptr) override;
 496 
 497     // Called when a positioned object moves but doesn't necessarily change size.  A simplified layout is attempted
 498     // that just updates the object's position. If the size does change, the object remains dirty.
 499     bool tryLayoutDoingPositionedMovementOnly()
 500     {
 501         LayoutUnit oldWidth = width();
 502         updateLogicalWidth();
 503         // If we shrink to fit our width may have changed, so we still need full layout.
 504         if (oldWidth != width())
 505             return false;
 506         updateLogicalHeight();
 507         return true;
 508     }
 509 
 510     LayoutRect maskClipRect(const LayoutPoint& paintOffset);
 511 
 512     VisiblePosition positionForPoint(const LayoutPoint&, const RenderFragmentContainer*) override;
 513 
 514     void removeFloatingOrPositionedChildFromBlockLists();
 515 
 516     RenderLayer* enclosingFloatPaintingLayer() const;
 517 
 518     virtual std::optional<int> firstLineBaseline() const { return std::optional<int>(); }
 519     virtual std::optional<int> inlineBlockBaseline(LineDirectionMode) const { return std::optional<int>(); } // Returns empty if we should skip this box when computing the baseline of an inline-block.
 520 
 521     bool shrinkToAvoidFloats() const;
 522     virtual bool avoidsFloats() const;
 523 
 524     virtual void markForPaginationRelayoutIfNeeded() { }
 525 
 526     bool isWritingModeRoot() const { return !parent() || parent()->style().writingMode() != style().writingMode(); }
 527 
 528     bool isDeprecatedFlexItem() const { return !isInline() && !isFloatingOrOutOfFlowPositioned() && parent() && parent()->isDeprecatedFlexibleBox(); }
 529     bool isFlexItemIncludingDeprecated() const { return !isInline() && !isFloatingOrOutOfFlowPositioned() && parent() && parent()->isFlexibleBoxIncludingDeprecated(); }
 530 
 531     LayoutUnit lineHeight(bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const override;
 532     int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const override;
 533 
 534     LayoutUnit offsetLeft() const override;
 535     LayoutUnit offsetTop() const override;
 536 
 537     LayoutPoint flipForWritingModeForChild(const RenderBox* child, const LayoutPoint&) const;
 538     LayoutUnit flipForWritingMode(LayoutUnit position) const; // The offset is in the block direction (y for horizontal writing modes, x for vertical writing modes).
 539     LayoutPoint flipForWritingMode(const LayoutPoint&) const;
 540     LayoutSize flipForWritingMode(const LayoutSize&) const;
 541     void flipForWritingMode(LayoutRect&) const;
 542     FloatPoint flipForWritingMode(const FloatPoint&) const;
 543     void flipForWritingMode(FloatRect&) const;
 544     // These represent your location relative to your container as a physical offset.
 545     // In layout related methods you almost always want the logical location (e.g. x() and y()).
 546     LayoutPoint topLeftLocation() const;
 547     LayoutSize topLeftLocationOffset() const;
 548     void applyTopLeftLocationOffset(LayoutPoint& point) const
 549     {
 550         // This is inlined for speed, since it is used by updateLayerPosition() during scrolling.
 551         if (!document().view()->hasFlippedBlockRenderers())
 552             point.move(m_frameRect.x(), m_frameRect.y());
 553         else
 554             applyTopLeftLocationOffsetWithFlipping(point);
 555     }
 556 
 557     LayoutRect logicalVisualOverflowRectForPropagation(const RenderStyle*) const;
 558     LayoutRect visualOverflowRectForPropagation(const RenderStyle*) const;
 559     LayoutRect logicalLayoutOverflowRectForPropagation(const RenderStyle*) const;
 560     LayoutRect layoutOverflowRectForPropagation(const RenderStyle*) const;
 561 
 562     bool hasRenderOverflow() const { return m_overflow; }
 563     bool hasVisualOverflow() const { return m_overflow && !borderBoxRect().contains(m_overflow->visualOverflowRect()); }
 564 
 565     virtual bool needsPreferredWidthsRecalculation() const;
 566     virtual void computeIntrinsicRatioInformation(FloatSize& /* intrinsicSize */, double& /* intrinsicRatio */) const { }
 567 
 568     ScrollPosition scrollPosition() const;
 569     LayoutSize cachedSizeForOverflowClip() const;
 570 
 571     bool shouldApplyClipAndScrollPositionForRepaint(const RenderLayerModelObject* repaintContainer) const;
 572     void applyCachedClipAndScrollPositionForRepaint(LayoutRect& paintRect) const;
 573 
 574     virtual bool hasRelativeDimensions() const;
 575     virtual bool hasRelativeLogicalHeight() const;
 576     virtual bool hasRelativeLogicalWidth() const;
 577 
 578     bool hasHorizontalLayoutOverflow() const
 579     {
 580         if (!m_overflow)
 581             return false;
 582 
 583         LayoutRect layoutOverflowRect = m_overflow->layoutOverflowRect();
 584         flipForWritingMode(layoutOverflowRect);
 585         return layoutOverflowRect.x() < x() || layoutOverflowRect.maxX() > x() + logicalWidth();
 586     }
 587 
 588     bool hasVerticalLayoutOverflow() const
 589     {
 590         if (!m_overflow)
 591             return false;
 592 
 593         LayoutRect layoutOverflowRect = m_overflow->layoutOverflowRect();
 594         flipForWritingMode(layoutOverflowRect);
 595         return layoutOverflowRect.y() < y() || layoutOverflowRect.maxY() > y() + logicalHeight();
 596     }
 597 
 598     virtual RenderPtr<RenderBox> createAnonymousBoxWithSameTypeAs(const RenderBox&) const
 599     {
 600         ASSERT_NOT_REACHED();
 601         return nullptr;
 602     }
 603 
 604     ShapeOutsideInfo* shapeOutsideInfo() const
 605     {
 606         return ShapeOutsideInfo::isEnabledFor(*this) ? ShapeOutsideInfo::info(*this) : nullptr;
 607     }
 608 
 609     void markShapeOutsideDependentsForLayout()
 610     {
 611         if (isFloating())
 612             removeFloatingOrPositionedChildFromBlockLists();
 613     }
 614 
 615     // True if this box can have a range in an outside fragmentation context.
 616     bool canHaveOutsideFragmentRange() const { return !isInFlowRenderFragmentedFlow(); }
 617     virtual bool needsLayoutAfterFragmentRangeChange() const { return false; }
 618 
 619     const RenderBox* findEnclosingScrollableContainer() const;
 620 
 621     bool isGridItem() const { return parent() && parent()->isRenderGrid() && !isExcludedFromNormalLayout(); }
 622     bool isFlexItem() const { return parent() && parent()->isFlexibleBox() && !isExcludedFromNormalLayout(); }
 623 
 624     virtual void adjustBorderBoxRectForPainting(LayoutRect&) { };
 625 
 626 protected:
 627     RenderBox(Element&, RenderStyle&&, BaseTypeFlags);
 628     RenderBox(Document&, RenderStyle&&, BaseTypeFlags);
 629 
 630     void styleWillChange(StyleDifference, const RenderStyle& newStyle) override;
 631     void styleDidChange(StyleDifference, const RenderStyle* oldStyle) override;
 632     void updateFromStyle() override;
 633 
 634     void willBeDestroyed() override;
 635 
 636     bool createsNewFormattingContext() const;
 637 
 638     virtual ItemPosition selfAlignmentNormalBehavior(const RenderBox* = nullptr) const { return ItemPositionStretch; }
 639 
 640     // Returns false if it could not cheaply compute the extent (e.g. fixed background), in which case the returned rect may be incorrect.
 641     bool getBackgroundPaintedExtent(const LayoutPoint& paintOffset, LayoutRect&) const;
 642     virtual bool foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned maxDepthToTest) const;
 643     bool computeBackgroundIsKnownToBeObscured(const LayoutPoint& paintOffset) override;
 644 
 645     void paintBackground(const PaintInfo&, const LayoutRect&, BackgroundBleedAvoidance = BackgroundBleedNone);
 646 
 647     void paintFillLayer(const PaintInfo&, const Color&, const FillLayer&, const LayoutRect&, BackgroundBleedAvoidance, CompositeOperator, RenderElement* backgroundObject, BaseBackgroundColorUsage = BaseBackgroundColorUse);
 648     void paintFillLayers(const PaintInfo&, const Color&, const FillLayer&, const LayoutRect&, BackgroundBleedAvoidance = BackgroundBleedNone, CompositeOperator = CompositeSourceOver, RenderElement* backgroundObject = nullptr);
 649 
 650     void paintMaskImages(const PaintInfo&, const LayoutRect&);
 651 
 652     BackgroundBleedAvoidance determineBackgroundBleedAvoidance(GraphicsContext&) const;
 653     bool backgroundHasOpaqueTopLayer() const;
 654 
 655     void computePositionedLogicalWidth(LogicalExtentComputedValues&, RenderFragmentContainer* = nullptr) const;
 656 
 657     LayoutUnit computeIntrinsicLogicalWidthUsing(Length logicalWidthLength, LayoutUnit availableLogicalWidth, LayoutUnit borderAndPadding) const;
 658     virtual std::optional<LayoutUnit> computeIntrinsicLogicalContentHeightUsing(Length logicalHeightLength, std::optional<LayoutUnit> intrinsicContentHeight, LayoutUnit borderAndPadding) const;
 659 
 660     virtual bool shouldComputeSizeAsReplaced() const { return isReplaced() && !isInlineBlockOrInlineTable(); }
 661 
 662     void mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState&, MapCoordinatesFlags, bool* wasFixed) const override;
 663     const RenderObject* pushMappingToContainer(const RenderLayerModelObject*, RenderGeometryMap&) const override;
 664     void mapAbsoluteToLocalPoint(MapCoordinatesFlags, TransformState&) const override;
 665 
 666     void paintRootBoxFillLayers(const PaintInfo&);
 667 
 668     bool skipContainingBlockForPercentHeightCalculation(const RenderBox& containingBlock, bool isPerpendicularWritingMode) const;
 669 
 670 private:
 671     bool replacedMinMaxLogicalHeightComputesAsNone(SizeType) const;
 672 
 673     void updateShapeOutsideInfoAfterStyleChange(const RenderStyle&, const RenderStyle* oldStyle);
 674 
 675     void updateGridPositionAfterStyleChange(const RenderStyle&, const RenderStyle* oldStyle);
 676 
 677     bool scrollLayer(ScrollDirection, ScrollGranularity, float multiplier, Element** stopElement);
 678 
 679     bool fixedElementLaysOutRelativeToFrame(const FrameView&) const;
 680 
 681     bool includeVerticalScrollbarSize() const;
 682     bool includeHorizontalScrollbarSize() const;
 683 
 684     bool isScrollableOrRubberbandableBox() const override;
 685 
 686     // Returns true if we did a full repaint.
 687     bool repaintLayerRectsForImage(WrappedImagePtr, const FillLayer& layers, bool drawingBackground);
 688 
 689     LayoutUnit containingBlockLogicalWidthForPositioned(const RenderBoxModelObject& containingBlock, RenderFragmentContainer* = nullptr, bool checkForPerpendicularWritingMode = true) const;
 690     LayoutUnit containingBlockLogicalHeightForPositioned(const RenderBoxModelObject& containingBlock, bool checkForPerpendicularWritingMode = true) const;
 691 
 692     void computePositionedLogicalHeight(LogicalExtentComputedValues&) const;
 693     void computePositionedLogicalWidthUsing(SizeType, Length logicalWidth, const RenderBoxModelObject& containerBlock, TextDirection containerDirection,
 694                                             LayoutUnit containerLogicalWidth, LayoutUnit bordersPlusPadding,
 695                                             Length logicalLeft, Length logicalRight, Length marginLogicalLeft, Length marginLogicalRight,
 696                                             LogicalExtentComputedValues&) const;
 697     void computePositionedLogicalHeightUsing(SizeType, Length logicalHeightLength, const RenderBoxModelObject& containerBlock,
 698                                              LayoutUnit containerLogicalHeight, LayoutUnit bordersPlusPadding, LayoutUnit logicalHeight,
 699                                              Length logicalTop, Length logicalBottom, Length marginLogicalTop, Length marginLogicalBottom,
 700                                              LogicalExtentComputedValues&) const;
 701 
 702     void computePositionedLogicalHeightReplaced(LogicalExtentComputedValues&) const;
 703     void computePositionedLogicalWidthReplaced(LogicalExtentComputedValues&) const;
 704 
 705     LayoutUnit fillAvailableMeasure(LayoutUnit availableLogicalWidth) const;
 706     LayoutUnit fillAvailableMeasure(LayoutUnit availableLogicalWidth, LayoutUnit& marginStart, LayoutUnit& marginEnd) const;
 707 
 708     virtual void computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const;
 709 
 710     // This function calculates the minimum and maximum preferred widths for an object.
 711     // These values are used in shrink-to-fit layout systems.
 712     // These include tables, positioned objects, floats and flexible boxes.
 713     virtual void computePreferredLogicalWidths() { setPreferredLogicalWidthsDirty(false); }
 714 
 715     LayoutRect frameRectForStickyPositioning() const final { return frameRect(); }
 716 
 717     void applyTopLeftLocationOffsetWithFlipping(LayoutPoint&) const;
 718 
 719 private:
 720     // The width/height of the contents + borders + padding.  The x/y location is relative to our container (which is not always our parent).
 721     LayoutRect m_frameRect;
 722 
 723 protected:
 724     LayoutBoxExtent m_marginBox;
 725 
 726     // The preferred logical width of the element if it were to break its lines at every possible opportunity.
 727     LayoutUnit m_minPreferredLogicalWidth;
 728 
 729     // The preferred logical width of the element if it never breaks any lines at all.
 730     LayoutUnit m_maxPreferredLogicalWidth;
 731 
 732     // For inline replaced elements, the inline box that owns us.
 733     InlineElementBox* m_inlineBoxWrapper { nullptr };
 734 
 735     // Our overflow information.
 736     RefPtr<RenderOverflow> m_overflow;
 737 
 738 private:
 739     // Used to store state between styleWillChange and styleDidChange
 740     static bool s_hadOverflowClip;
 741 };
 742 
 743 inline RenderBox* RenderBox::previousSiblingBox() const
 744 {
 745     return downcast<RenderBox>(previousSibling());
 746 }
 747 
 748 inline RenderBox* RenderBox::nextSiblingBox() const
 749 {
 750     return downcast<RenderBox>(nextSibling());
 751 }
 752 
 753 inline RenderBox* RenderBox::parentBox() const
 754 {
 755     return downcast<RenderBox>(parent());
 756 }
 757 
 758 inline RenderBox* RenderBox::firstChildBox() const
 759 {
 760     return downcast<RenderBox>(firstChild());
 761 }
 762 
 763 inline RenderBox* RenderBox::lastChildBox() const
 764 {
 765     return downcast<RenderBox>(lastChild());
 766 }
 767 
 768 inline void RenderBox::setInlineBoxWrapper(InlineElementBox* boxWrapper)
 769 {
 770     if (boxWrapper) {
 771         ASSERT(!m_inlineBoxWrapper);
 772         // m_inlineBoxWrapper should already be 0. Deleting it is a safeguard against security issues.
 773         // Otherwise, there will two line box wrappers keeping the reference to this renderer, and
 774         // only one will be notified when the renderer is getting destroyed. The second line box wrapper
 775         // will keep a stale reference.
 776         if (UNLIKELY(m_inlineBoxWrapper != nullptr))
 777             deleteLineBoxWrapper();
 778     }
 779 
 780     m_inlineBoxWrapper = boxWrapper;
 781 }
 782 
 783 } // namespace WebCore
 784 
 785 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderBox, isBox())