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