2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003, 2006, 2007, 2009, 2013 Apple Inc. All rights reserved.
5 * Copyright (C) 2010, 2012 Google Inc. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
25 #include "LengthFunctions.h"
26 #include "RenderObject.h"
32 class RenderTreeBuilder;
34 class RenderElement : public RenderObject {
35 WTF_MAKE_ISO_ALLOCATED(RenderElement);
37 virtual ~RenderElement();
39 enum RendererCreationType { CreateAllRenderers, OnlyCreateBlockAndFlexboxRenderers };
40 static RenderPtr<RenderElement> createFor(Element&, RenderStyle&&, RendererCreationType = CreateAllRenderers);
42 bool hasInitializedStyle() const { return m_hasInitializedStyle; }
44 const RenderStyle& style() const { return m_style; }
45 const RenderStyle& firstLineStyle() const;
47 // FIXME: Style shouldn't be mutated.
48 RenderStyle& mutableStyle() { return m_style; }
50 void initializeStyle();
52 // Calling with minimalStyleDifference > StyleDifferenceEqual indicates that
53 // out-of-band state (e.g. animations) requires that styleDidChange processing
54 // continue even if the style isn't different from the current style.
55 void setStyle(RenderStyle&&, StyleDifference minimalStyleDifference = StyleDifferenceEqual);
57 // The pseudo element style can be cached or uncached. Use the cached method if the pseudo element doesn't respect
58 // any pseudo classes (and therefore has no concept of changing state).
59 const RenderStyle* getCachedPseudoStyle(PseudoId, const RenderStyle* parentStyle = nullptr) const;
60 std::unique_ptr<RenderStyle> getUncachedPseudoStyle(const PseudoStyleRequest&, const RenderStyle* parentStyle = nullptr, const RenderStyle* ownStyle = nullptr) const;
62 // This is null for anonymous renderers.
63 Element* element() const { return downcast<Element>(RenderObject::node()); }
64 Element* nonPseudoElement() const { return downcast<Element>(RenderObject::nonPseudoNode()); }
65 Element* generatingElement() const;
67 RenderObject* firstChild() const { return m_firstChild; }
68 RenderObject* lastChild() const { return m_lastChild; }
70 bool canContainFixedPositionObjects() const;
71 bool canContainAbsolutelyPositionedObjects() const;
73 Color selectionColor(int colorProperty) const;
74 std::unique_ptr<RenderStyle> selectionPseudoStyle() const;
76 // Obtains the selection colors that should be used when painting a selection.
77 Color selectionBackgroundColor() const;
78 Color selectionForegroundColor() const;
79 Color selectionEmphasisMarkColor() const;
81 // FIXME: Make these standalone and move to relevant files.
82 bool isRenderLayerModelObject() const;
83 bool isBoxModelObject() const;
84 bool isRenderBlock() const;
85 bool isRenderBlockFlow() const;
86 bool isRenderReplaced() const;
87 bool isRenderInline() const;
89 virtual bool isChildAllowed(const RenderObject&, const RenderStyle&) const { return true; }
90 void didAttachChild(RenderObject& child, RenderObject* beforeChild);
92 // The following functions are used when the render tree hierarchy changes to make sure layers get
93 // properly added and removed. Since containership can be implemented by any subclass, and since a hierarchy
94 // can contain a mixture of boxes and other object types, these functions need to be in the base class.
95 void addLayers(RenderLayer* parentLayer);
96 void removeLayers(RenderLayer* parentLayer);
97 void moveLayers(RenderLayer* oldParent, RenderLayer* newParent);
98 RenderLayer* findNextLayer(RenderLayer* parentLayer, RenderObject* startPoint, bool checkParent = true);
100 virtual RenderElement* hoverAncestor() const;
102 virtual void dirtyLinesFromChangedChild(RenderObject&) { }
104 bool ancestorLineBoxDirty() const { return m_ancestorLineBoxDirty; }
105 void setAncestorLineBoxDirty(bool f = true);
107 void setChildNeedsLayout(MarkingBehavior = MarkContainingBlockChain);
108 void clearChildNeedsLayout();
109 void setNeedsPositionedMovementLayout(const RenderStyle* oldStyle);
110 void setNeedsSimplifiedNormalFlowLayout();
112 virtual void paint(PaintInfo&, const LayoutPoint&) = 0;
114 // inline-block elements paint all phases atomically. This function ensures that. Certain other elements
115 // (grid items, flex items) require this behavior as well, and this function exists as a helper for them.
116 // It is expected that the caller will call this function independent of the value of paintInfo.phase.
117 void paintAsInlineBlock(PaintInfo&, const LayoutPoint&);
119 // Recursive function that computes the size and position of this object and all its descendants.
120 virtual void layout();
122 /* This function performs a layout only if one is needed. */
123 void layoutIfNeeded() { if (needsLayout()) layout(); }
125 // Updates only the local style ptr of the object. Does not update the state of the object,
126 // and so only should be called when the style is known not to have changed (or from setStyle).
127 void setStyleInternal(RenderStyle&& style) { m_style = WTFMove(style); }
129 // Repaint only if our old bounds and new bounds are different. The caller may pass in newBounds and newOutlineBox if they are known.
130 bool repaintAfterLayoutIfNeeded(const RenderLayerModelObject* repaintContainer, const LayoutRect& oldBounds, const LayoutRect& oldOutlineBox, const LayoutRect* newBoundsPtr = nullptr, const LayoutRect* newOutlineBoxPtr = nullptr);
132 bool borderImageIsLoadedAndCanBeRendered() const;
133 bool mayCauseRepaintInsideViewport(const IntRect* visibleRect = nullptr) const;
134 bool isVisibleInDocumentRect(const IntRect& documentRect) const;
136 // Returns true if this renderer requires a new stacking context.
137 static bool createsGroupForStyle(const RenderStyle&);
138 bool createsGroup() const { return createsGroupForStyle(style()); }
140 bool isTransparent() const { return style().opacity() < 1.0f; }
141 float opacity() const { return style().opacity(); }
143 bool visibleToHitTesting() const { return style().visibility() == VISIBLE && style().pointerEvents() != PE_NONE; }
145 bool hasBackground() const { return style().hasBackground(); }
146 bool hasMask() const { return style().hasMask(); }
147 bool hasClip() const { return isOutOfFlowPositioned() && style().hasClip(); }
148 bool hasClipOrOverflowClip() const { return hasClip() || hasOverflowClip(); }
149 bool hasClipPath() const { return style().clipPath(); }
150 bool hasHiddenBackface() const { return style().backfaceVisibility() == BackfaceVisibilityHidden; }
151 bool hasOutlineAnnotation() const;
152 bool hasOutline() const { return style().hasOutline() || hasOutlineAnnotation(); }
153 bool hasSelfPaintingLayer() const;
155 bool checkForRepaintDuringLayout() const;
157 // anchorRect() is conceptually similar to absoluteBoundingBoxRect(), but is intended for scrolling to an anchor.
158 // For inline renderers, this gets the logical top left of the first leaf child and the logical bottom right of the
159 // last leaf child, converts them to absolute coordinates, and makes a box out of them.
160 LayoutRect absoluteAnchorRect(bool* insideFixed = nullptr) const;
162 bool hasFilter() const { return style().hasFilter(); }
163 bool hasBackdropFilter() const
165 #if ENABLE(FILTERS_LEVEL_2)
166 return style().hasBackdropFilter();
173 #if ENABLE(CSS_COMPOSITING)
174 bool hasBlendMode() const { return style().hasBlendMode(); }
176 bool hasBlendMode() const { return false; }
179 bool hasShapeOutside() const { return style().shapeOutside(); }
181 void registerForVisibleInViewportCallback();
182 void unregisterForVisibleInViewportCallback();
184 VisibleInViewportState visibleInViewportState() const { return static_cast<VisibleInViewportState>(m_visibleInViewportState); }
185 void setVisibleInViewportState(VisibleInViewportState);
186 virtual void visibleInViewportStateChanged();
188 bool repaintForPausedImageAnimationsIfNeeded(const IntRect& visibleRect, CachedImage&);
189 bool hasPausedImageAnimations() const { return m_hasPausedImageAnimations; }
190 void setHasPausedImageAnimations(bool b) { m_hasPausedImageAnimations = b; }
192 void setRenderBoxNeedsLazyRepaint(bool b) { m_renderBoxNeedsLazyRepaint = b; }
193 bool renderBoxNeedsLazyRepaint() const { return m_renderBoxNeedsLazyRepaint; }
195 bool hasCounterNodeMap() const { return m_hasCounterNodeMap; }
196 void setHasCounterNodeMap(bool f) { m_hasCounterNodeMap = f; }
198 const RenderElement* enclosingRendererWithTextDecoration(TextDecoration, bool firstLine) const;
199 void drawLineForBoxSide(GraphicsContext&, const FloatRect&, BoxSide, Color, EBorderStyle, float adjacentWidth1, float adjacentWidth2, bool antialias = false) const;
201 #if ENABLE(TEXT_AUTOSIZING)
202 void adjustComputedFontSizesOnBlocks(float size, float visibleWidth);
203 WEBCORE_EXPORT void resetTextAutosizing();
205 RenderBlock* containingBlockForFixedPosition() const;
206 RenderBlock* containingBlockForAbsolutePosition() const;
208 RespectImageOrientationEnum shouldRespectImageOrientation() const;
210 void removeFromRenderFragmentedFlow();
211 virtual void resetEnclosingFragmentedFlowAndChildInfoIncludingDescendants(RenderFragmentedFlow*);
213 // Called before anonymousChild.setStyle(). Override to set custom styles for
215 virtual void updateAnonymousChildStyle(RenderStyle&) const { };
217 bool hasContinuationChainNode() const { return m_hasContinuationChainNode; }
218 bool isContinuation() const { return m_isContinuation; }
219 void setIsContinuation() { m_isContinuation = true; }
220 bool isFirstLetter() const { return m_isFirstLetter; }
221 void setIsFirstLetter() { m_isFirstLetter = true; }
223 RenderObject* attachRendererInternal(RenderPtr<RenderObject> child, RenderObject* beforeChild);
224 RenderPtr<RenderObject> detachRendererInternal(RenderObject&);
228 RenderLayerModelObjectFlag = 1 << 0,
229 RenderBoxModelObjectFlag = 1 << 1,
230 RenderInlineFlag = 1 << 2,
231 RenderReplacedFlag = 1 << 3,
232 RenderBlockFlag = 1 << 4,
233 RenderBlockFlowFlag = 1 << 5,
236 typedef unsigned BaseTypeFlags;
238 RenderElement(Element&, RenderStyle&&, BaseTypeFlags);
239 RenderElement(Document&, RenderStyle&&, BaseTypeFlags);
241 bool layerCreationAllowedForSubtree() const;
243 enum StylePropagationType { PropagateToAllChildren, PropagateToBlockChildrenOnly };
244 void propagateStyleToAnonymousChildren(StylePropagationType);
246 LayoutUnit valueForLength(const Length&, LayoutUnit maximumValue) const;
247 LayoutUnit minimumValueForLength(const Length&, LayoutUnit maximumValue) const;
249 void setFirstChild(RenderObject* child) { m_firstChild = child; }
250 void setLastChild(RenderObject* child) { m_lastChild = child; }
252 virtual void styleWillChange(StyleDifference, const RenderStyle& newStyle);
253 virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
255 void insertedIntoTree() override;
256 void willBeRemovedFromTree() override;
257 void willBeDestroyed() override;
259 void setRenderInlineAlwaysCreatesLineBoxes(bool b) { m_renderInlineAlwaysCreatesLineBoxes = b; }
260 bool renderInlineAlwaysCreatesLineBoxes() const { return m_renderInlineAlwaysCreatesLineBoxes; }
262 void setHasContinuationChainNode(bool b) { m_hasContinuationChainNode = b; }
264 void setRenderBlockHasMarginBeforeQuirk(bool b) { m_renderBlockHasMarginBeforeQuirk = b; }
265 void setRenderBlockHasMarginAfterQuirk(bool b) { m_renderBlockHasMarginAfterQuirk = b; }
266 void setRenderBlockShouldForceRelayoutChildren(bool b) { m_renderBlockShouldForceRelayoutChildren = b; }
267 bool renderBlockHasMarginBeforeQuirk() const { return m_renderBlockHasMarginBeforeQuirk; }
268 bool renderBlockHasMarginAfterQuirk() const { return m_renderBlockHasMarginAfterQuirk; }
269 bool renderBlockShouldForceRelayoutChildren() const { return m_renderBlockShouldForceRelayoutChildren; }
271 void setRenderBlockFlowLineLayoutPath(unsigned u) { m_renderBlockFlowLineLayoutPath = u; }
272 void setRenderBlockFlowHasMarkupTruncation(bool b) { m_renderBlockFlowHasMarkupTruncation = b; }
273 unsigned renderBlockFlowLineLayoutPath() const { return m_renderBlockFlowLineLayoutPath; }
274 bool renderBlockFlowHasMarkupTruncation() const { return m_renderBlockFlowHasMarkupTruncation; }
276 void paintFocusRing(PaintInfo&, const RenderStyle&, const Vector<LayoutRect>& focusRingRects);
277 void paintOutline(PaintInfo&, const LayoutRect&);
278 void updateOutlineAutoAncestor(bool hasOutlineAuto);
280 void removeFromRenderFragmentedFlowIncludingDescendants(bool shouldUpdateState);
281 void adjustFragmentedFlowStateOnContainingBlockChangeIfNeeded();
283 bool isVisibleInViewport() const;
286 RenderElement(ContainerNode&, RenderStyle&&, BaseTypeFlags);
287 void node() const = delete;
288 void nonPseudoNode() const = delete;
289 void generatingNode() const = delete;
290 void isText() const = delete;
291 void isRenderElement() const = delete;
293 RenderObject* firstChildSlow() const final { return firstChild(); }
294 RenderObject* lastChildSlow() const final { return lastChild(); }
296 // Called when an object that was floating or positioned becomes a normal flow object
297 // again. We have to make sure the render tree updates as needed to accommodate the new
298 // normal flow object.
299 void handleDynamicFloatPositionChange();
301 bool shouldRepaintForStyleDifference(StyleDifference) const;
302 bool hasImmediateNonWhitespaceTextChildOrBorderOrOutline() const;
304 void updateFillImages(const FillLayer*, const FillLayer&);
305 void updateImage(StyleImage*, StyleImage*);
306 void updateShapeImage(const ShapeValue*, const ShapeValue*);
308 StyleDifference adjustStyleDifference(StyleDifference, unsigned contextSensitiveProperties) const;
309 std::unique_ptr<RenderStyle> computeFirstLineStyle() const;
310 void invalidateCachedFirstLineStyle();
312 bool canDestroyDecodedData() final { return !isVisibleInViewport(); }
313 VisibleInViewportState imageFrameAvailable(CachedImage&, ImageAnimatingState, const IntRect* changeRect) final;
314 void didRemoveCachedImageClient(CachedImage&) final;
316 bool getLeadingCorner(FloatPoint& output, bool& insideFixed) const;
317 bool getTrailingCorner(FloatPoint& output, bool& insideFixed) const;
319 void clearSubtreeLayoutRootIfNeeded() const;
321 bool shouldWillChangeCreateStackingContext() const;
322 void issueRepaintForOutlineAuto(float outlineSize);
324 unsigned m_baseTypeFlags : 6;
325 unsigned m_ancestorLineBoxDirty : 1;
326 unsigned m_hasInitializedStyle : 1;
328 unsigned m_renderInlineAlwaysCreatesLineBoxes : 1;
329 unsigned m_renderBoxNeedsLazyRepaint : 1;
330 unsigned m_hasPausedImageAnimations : 1;
331 unsigned m_hasCounterNodeMap : 1;
332 unsigned m_hasContinuationChainNode : 1;
333 unsigned m_isContinuation : 1;
334 unsigned m_isFirstLetter : 1;
335 mutable unsigned m_hasValidCachedFirstLineStyle : 1;
337 unsigned m_renderBlockHasMarginBeforeQuirk : 1;
338 unsigned m_renderBlockHasMarginAfterQuirk : 1;
339 unsigned m_renderBlockShouldForceRelayoutChildren : 1;
340 unsigned m_renderBlockFlowHasMarkupTruncation : 1;
341 unsigned m_renderBlockFlowLineLayoutPath : 2;
343 unsigned m_isRegisteredForVisibleInViewportCallback : 1;
344 unsigned m_visibleInViewportState : 2;
346 RenderObject* m_firstChild;
347 RenderObject* m_lastChild;
352 inline void RenderElement::setAncestorLineBoxDirty(bool f)
354 m_ancestorLineBoxDirty = f;
355 if (m_ancestorLineBoxDirty)
359 inline void RenderElement::setChildNeedsLayout(MarkingBehavior markParents)
361 ASSERT(!isSetNeedsLayoutForbidden());
362 if (normalChildNeedsLayout())
364 setNormalChildNeedsLayoutBit(true);
365 if (markParents == MarkContainingBlockChain)
366 markContainingBlocksForLayout();
369 inline LayoutUnit RenderElement::valueForLength(const Length& length, LayoutUnit maximumValue) const
371 return WebCore::valueForLength(length, maximumValue);
374 inline LayoutUnit RenderElement::minimumValueForLength(const Length& length, LayoutUnit maximumValue) const
376 return WebCore::minimumValueForLength(length, maximumValue);
379 inline bool RenderElement::isRenderLayerModelObject() const
381 return m_baseTypeFlags & RenderLayerModelObjectFlag;
384 inline bool RenderElement::isBoxModelObject() const
386 return m_baseTypeFlags & RenderBoxModelObjectFlag;
389 inline bool RenderElement::isRenderBlock() const
391 return m_baseTypeFlags & RenderBlockFlag;
394 inline bool RenderElement::isRenderBlockFlow() const
396 return m_baseTypeFlags & RenderBlockFlowFlag;
399 inline bool RenderElement::isRenderReplaced() const
401 return m_baseTypeFlags & RenderReplacedFlag;
404 inline bool RenderElement::isRenderInline() const
406 return m_baseTypeFlags & RenderInlineFlag;
409 inline Element* RenderElement::generatingElement() const
411 return downcast<Element>(RenderObject::generatingNode());
414 inline bool RenderElement::canContainFixedPositionObjects() const
416 return isRenderView()
417 || (hasTransform() && isRenderBlock())
418 || isSVGForeignObject()
419 || isOutOfFlowRenderFragmentedFlow();
422 inline bool RenderElement::canContainAbsolutelyPositionedObjects() const
424 return style().position() != StaticPosition
425 || (isRenderBlock() && hasTransformRelatedProperty())
426 || isSVGForeignObject()
430 inline bool RenderElement::createsGroupForStyle(const RenderStyle& style)
432 return style.opacity() < 1.0f || style.hasMask() || style.clipPath() || style.hasFilter() || style.hasBackdropFilter() || style.hasBlendMode();
435 inline bool RenderObject::isRenderLayerModelObject() const
437 return is<RenderElement>(*this) && downcast<RenderElement>(*this).isRenderLayerModelObject();
440 inline bool RenderObject::isBoxModelObject() const
442 return is<RenderElement>(*this) && downcast<RenderElement>(*this).isBoxModelObject();
445 inline bool RenderObject::isRenderBlock() const
447 return is<RenderElement>(*this) && downcast<RenderElement>(*this).isRenderBlock();
450 inline bool RenderObject::isRenderBlockFlow() const
452 return is<RenderElement>(*this) && downcast<RenderElement>(*this).isRenderBlockFlow();
455 inline bool RenderObject::isRenderReplaced() const
457 return is<RenderElement>(*this) && downcast<RenderElement>(*this).isRenderReplaced();
460 inline bool RenderObject::isRenderInline() const
462 return is<RenderElement>(*this) && downcast<RenderElement>(*this).isRenderInline();
465 inline const RenderStyle& RenderObject::style() const
468 return m_parent->style();
469 return downcast<RenderElement>(*this).style();
472 inline const RenderStyle& RenderObject::firstLineStyle() const
475 return m_parent->firstLineStyle();
476 return downcast<RenderElement>(*this).firstLineStyle();
479 inline RenderElement* ContainerNode::renderer() const
481 return downcast<RenderElement>(Node::renderer());
484 inline int adjustForAbsoluteZoom(int value, const RenderElement& renderer)
486 return adjustForAbsoluteZoom(value, renderer.style());
489 inline LayoutUnit adjustLayoutUnitForAbsoluteZoom(LayoutUnit value, const RenderElement& renderer)
491 return adjustLayoutUnitForAbsoluteZoom(value, renderer.style());
494 } // namespace WebCore
496 SPECIALIZE_TYPE_TRAITS_RENDER_OBJECT(RenderElement, isRenderElement())