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