71aa776b135e9fec2a192726d23ad4da460c26a3
[WebKit-https.git] / Source / WebCore / rendering / RenderObject.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2000 Dirk Mueller (mueller@kde.org)
5  *           (C) 2004 Allan Sandfeld Jensen (kde@carewolf.com)
6  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011, 2013 Apple Inc. All rights reserved.
7  * Copyright (C) 2009 Google Inc. All rights reserved.
8  * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public License
21  * along with this library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  *
25  */
26
27 #include "config.h"
28 #include "RenderObject.h"
29
30 #include "AXObjectCache.h"
31 #include "AnimationController.h"
32 #include "EventHandler.h"
33 #include "FloatQuad.h"
34 #include "FlowThreadController.h"
35 #include "FrameSelection.h"
36 #include "FrameView.h"
37 #include "GeometryUtilities.h"
38 #include "GraphicsContext.h"
39 #include "HTMLElement.h"
40 #include "HTMLImageElement.h"
41 #include "HTMLNames.h"
42 #include "HTMLTableCellElement.h"
43 #include "HTMLTableElement.h"
44 #include "HitTestResult.h"
45 #include "LogicalSelectionOffsetCaches.h"
46 #include "MainFrame.h"
47 #include "Page.h"
48 #include "PseudoElement.h"
49 #include "RenderChildIterator.h"
50 #include "RenderCounter.h"
51 #include "RenderFlowThread.h"
52 #include "RenderGeometryMap.h"
53 #include "RenderInline.h"
54 #include "RenderIterator.h"
55 #include "RenderLayer.h"
56 #include "RenderLayerBacking.h"
57 #include "RenderMultiColumnFlowThread.h"
58 #include "RenderNamedFlowFragment.h"
59 #include "RenderNamedFlowThread.h" 
60 #include "RenderRuby.h"
61 #include "RenderSVGResourceContainer.h"
62 #include "RenderScrollbarPart.h"
63 #include "RenderTableRow.h"
64 #include "RenderTheme.h"
65 #include "RenderView.h"
66 #include "RenderWidget.h"
67 #include "SVGRenderSupport.h"
68 #include "Settings.h"
69 #include "StyleResolver.h"
70 #include "TransformState.h"
71 #include "htmlediting.h"
72 #include <algorithm>
73 #include <stdio.h>
74 #include <wtf/RefCountedLeakCounter.h>
75
76 #if PLATFORM(IOS)
77 #include "SelectionRect.h"
78 #endif
79
80 namespace WebCore {
81
82 using namespace HTMLNames;
83
84 #ifndef NDEBUG
85
86 RenderObject::SetLayoutNeededForbiddenScope::SetLayoutNeededForbiddenScope(RenderObject* renderObject, bool isForbidden)
87     : m_renderObject(renderObject)
88     , m_preexistingForbidden(m_renderObject->isSetNeedsLayoutForbidden())
89 {
90     m_renderObject->setNeedsLayoutIsForbidden(isForbidden);
91 }
92
93 RenderObject::SetLayoutNeededForbiddenScope::~SetLayoutNeededForbiddenScope()
94 {
95     m_renderObject->setNeedsLayoutIsForbidden(m_preexistingForbidden);
96 }
97 #endif
98
99 struct SameSizeAsRenderObject {
100     virtual ~SameSizeAsRenderObject() { } // Allocate vtable pointer.
101     void* pointers[4];
102 #ifndef NDEBUG
103     unsigned m_debugBitfields : 2;
104 #endif
105     unsigned m_bitfields;
106 };
107
108 COMPILE_ASSERT(sizeof(RenderObject) == sizeof(SameSizeAsRenderObject), RenderObject_should_stay_small);
109
110 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, renderObjectCounter, ("RenderObject"));
111
112 RenderObject::RenderObject(Node& node)
113     : CachedImageClient()
114     , m_node(node)
115     , m_parent(nullptr)
116     , m_previous(nullptr)
117     , m_next(nullptr)
118 #ifndef NDEBUG
119     , m_hasAXObject(false)
120     , m_setNeedsLayoutForbidden(false)
121 #endif
122     , m_bitfields(node)
123 {
124     if (RenderView* renderView = node.document().renderView())
125         renderView->didCreateRenderer();
126 #ifndef NDEBUG
127     renderObjectCounter.increment();
128 #endif
129 }
130
131 RenderObject::~RenderObject()
132 {
133     view().didDestroyRenderer();
134 #ifndef NDEBUG
135     ASSERT(!m_hasAXObject);
136     renderObjectCounter.decrement();
137 #endif
138     ASSERT(!hasRareData());
139 }
140
141 RenderTheme& RenderObject::theme() const
142 {
143     ASSERT(document().page());
144     return document().page()->theme();
145 }
146
147 bool RenderObject::isDescendantOf(const RenderObject* ancestor) const
148 {
149     for (const RenderObject* renderer = this; renderer; renderer = renderer->m_parent) {
150         if (renderer == ancestor)
151             return true;
152     }
153     return false;
154 }
155
156 bool RenderObject::isLegend() const
157 {
158     return node() && node()->hasTagName(legendTag);
159 }
160
161 bool RenderObject::isHTMLMarquee() const
162 {
163     return node() && node()->renderer() == this && node()->hasTagName(marqueeTag);
164 }
165
166 void RenderObject::setFlowThreadStateIncludingDescendants(FlowThreadState state)
167 {
168     setFlowThreadState(state);
169
170     if (!is<RenderElement>(*this))
171         return;
172
173     for (auto& child : childrenOfType<RenderObject>(downcast<RenderElement>(*this))) {
174         // If the child is a fragmentation context it already updated the descendants flag accordingly.
175         if (child.isRenderFlowThread())
176             continue;
177         ASSERT(state != child.flowThreadState());
178         child.setFlowThreadStateIncludingDescendants(state);
179     }
180 }
181
182 void RenderObject::setParent(RenderElement* parent)
183 {
184     m_parent = parent;
185
186     // Only update if our flow thread state is different from our new parent and if we're not a RenderFlowThread.
187     // A RenderFlowThread is always considered to be inside itself, so it never has to change its state
188     // in response to parent changes.
189     FlowThreadState newState = parent ? parent->flowThreadState() : NotInsideFlowThread;
190     if (newState != flowThreadState() && !isRenderFlowThread())
191         setFlowThreadStateIncludingDescendants(newState);
192 }
193
194 void RenderObject::removeFromParent()
195 {
196     if (parent())
197         parent()->removeChild(*this);
198 }
199
200 RenderObject* RenderObject::nextInPreOrder() const
201 {
202     if (RenderObject* o = firstChildSlow())
203         return o;
204
205     return nextInPreOrderAfterChildren();
206 }
207
208 RenderObject* RenderObject::nextInPreOrderAfterChildren() const
209 {
210     RenderObject* o;
211     if (!(o = nextSibling())) {
212         o = parent();
213         while (o && !o->nextSibling())
214             o = o->parent();
215         if (o)
216             o = o->nextSibling();
217     }
218
219     return o;
220 }
221
222 RenderObject* RenderObject::nextInPreOrder(const RenderObject* stayWithin) const
223 {
224     if (RenderObject* o = firstChildSlow())
225         return o;
226
227     return nextInPreOrderAfterChildren(stayWithin);
228 }
229
230 RenderObject* RenderObject::nextInPreOrderAfterChildren(const RenderObject* stayWithin) const
231 {
232     if (this == stayWithin)
233         return nullptr;
234
235     const RenderObject* current = this;
236     RenderObject* next;
237     while (!(next = current->nextSibling())) {
238         current = current->parent();
239         if (!current || current == stayWithin)
240             return nullptr;
241     }
242     return next;
243 }
244
245 RenderObject* RenderObject::previousInPreOrder() const
246 {
247     if (RenderObject* o = previousSibling()) {
248         while (RenderObject* last = o->lastChildSlow())
249             o = last;
250         return o;
251     }
252
253     return parent();
254 }
255
256 RenderObject* RenderObject::previousInPreOrder(const RenderObject* stayWithin) const
257 {
258     if (this == stayWithin)
259         return nullptr;
260
261     return previousInPreOrder();
262 }
263
264 RenderObject* RenderObject::childAt(unsigned index) const
265 {
266     RenderObject* child = firstChildSlow();
267     for (unsigned i = 0; child && i < index; i++)
268         child = child->nextSibling();
269     return child;
270 }
271
272 RenderObject* RenderObject::firstLeafChild() const
273 {
274     RenderObject* r = firstChildSlow();
275     while (r) {
276         RenderObject* n = nullptr;
277         n = r->firstChildSlow();
278         if (!n)
279             break;
280         r = n;
281     }
282     return r;
283 }
284
285 RenderObject* RenderObject::lastLeafChild() const
286 {
287     RenderObject* r = lastChildSlow();
288     while (r) {
289         RenderObject* n = nullptr;
290         n = r->lastChildSlow();
291         if (!n)
292             break;
293         r = n;
294     }
295     return r;
296 }
297
298 #if ENABLE(IOS_TEXT_AUTOSIZING)
299
300 // Non-recursive version of the DFS search.
301 RenderObject* RenderObject::traverseNext(const RenderObject* stayWithin, HeightTypeTraverseNextInclusionFunction inclusionFunction, int& currentDepth, int& newFixedDepth) const
302 {
303     BlockContentHeightType overflowType;
304
305     // Check for suitable children.
306     for (RenderObject* child = firstChildSlow(); child; child = child->nextSibling()) {
307         overflowType = inclusionFunction(*child);
308         if (overflowType != FixedHeight) {
309             currentDepth++;
310             if (overflowType == OverflowHeight)
311                 newFixedDepth = currentDepth;
312             ASSERT(!stayWithin || child->isDescendantOf(stayWithin));
313             return child;
314         }
315     }
316
317     if (this == stayWithin)
318         return nullptr;
319
320     // Now we traverse other nodes if they exist, otherwise
321     // we go to the parent node and try doing the same.
322     const RenderObject* n = this;
323     while (n) {
324         while (n && !n->nextSibling() && (!stayWithin || n->parent() != stayWithin)) {
325             n = n->parent();
326             currentDepth--;
327         }
328         if (!n)
329             return nullptr;
330         for (RenderObject* sibling = n->nextSibling(); sibling; sibling = sibling->nextSibling()) {
331             overflowType = inclusionFunction(*sibling);
332             if (overflowType != FixedHeight) {
333                 if (overflowType == OverflowHeight)
334                     newFixedDepth = currentDepth;
335                 ASSERT(!stayWithin || !n->nextSibling() || n->nextSibling()->isDescendantOf(stayWithin));
336                 return sibling;
337             }
338         }
339         if (!stayWithin || n->parent() != stayWithin) {
340             n = n->parent();
341             currentDepth--;
342         } else
343             return nullptr;
344     }
345     return nullptr;
346 }
347
348 #endif // ENABLE(IOS_TEXT_AUTOSIZING)
349
350 RenderLayer* RenderObject::enclosingLayer() const
351 {
352     for (auto& renderer : lineageOfType<RenderLayerModelObject>(*this)) {
353         if (renderer.hasLayer())
354             return renderer.layer();
355     }
356     return nullptr;
357 }
358
359 bool RenderObject::scrollRectToVisible(SelectionRevealMode revealMode, const LayoutRect& rect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
360 {
361     if (revealMode == SelectionRevealMode::DoNotReveal)
362         return false;
363
364     RenderLayer* enclosingLayer = this->enclosingLayer();
365     if (!enclosingLayer)
366         return false;
367
368     enclosingLayer->scrollRectToVisible(revealMode, rect, alignX, alignY);
369     return true;
370 }
371
372 RenderBox& RenderObject::enclosingBox() const
373 {
374     return *lineageOfType<RenderBox>(const_cast<RenderObject&>(*this)).first();
375 }
376
377 RenderBoxModelObject& RenderObject::enclosingBoxModelObject() const
378 {
379     return *lineageOfType<RenderBoxModelObject>(const_cast<RenderObject&>(*this)).first();
380 }
381
382 bool RenderObject::fixedPositionedWithNamedFlowContainingBlock() const
383 {
384     return ((flowThreadState() == RenderObject::InsideOutOfFlowThread)
385         && (style().position() == FixedPosition)
386         && (containingBlock()->isOutOfFlowRenderFlowThread()));
387 }
388
389 static bool hasFixedPosInNamedFlowContainingBlock(const RenderObject* renderer)
390 {
391     ASSERT(renderer->flowThreadState() != RenderObject::NotInsideFlowThread);
392
393     RenderObject* curr = const_cast<RenderObject*>(renderer);
394     while (curr && !is<RenderView>(*curr)) {
395         if (curr->fixedPositionedWithNamedFlowContainingBlock())
396             return true;
397         curr = curr->containingBlock();
398     }
399
400     return false;
401 }
402
403 RenderBlock* RenderObject::firstLineBlock() const
404 {
405     return nullptr;
406 }
407
408 static inline bool objectIsRelayoutBoundary(const RenderElement* object)
409 {
410     // FIXME: In future it may be possible to broaden these conditions in order to improve performance.
411     if (object->isRenderView())
412         return true;
413
414     if (object->isTextControl())
415         return true;
416
417     if (object->isSVGRoot())
418         return true;
419
420     if (!object->hasOverflowClip())
421         return false;
422
423     if (object->style().width().isIntrinsicOrAuto() || object->style().height().isIntrinsicOrAuto() || object->style().height().isPercentOrCalculated())
424         return false;
425
426     // Table parts can't be relayout roots since the table is responsible for layouting all the parts.
427     if (object->isTablePart())
428         return false;
429
430     return true;
431 }
432
433 void RenderObject::clearNeedsLayout()
434 {
435     m_bitfields.setNeedsLayout(false);
436     setEverHadLayout(true);
437     setPosChildNeedsLayoutBit(false);
438     setNeedsSimplifiedNormalFlowLayoutBit(false);
439     setNormalChildNeedsLayoutBit(false);
440     setNeedsPositionedMovementLayoutBit(false);
441     if (is<RenderElement>(*this))
442         downcast<RenderElement>(*this).setAncestorLineBoxDirty(false);
443 #ifndef NDEBUG
444     checkBlockPositionedObjectsNeedLayout();
445 #endif
446 }
447
448 static void scheduleRelayoutForSubtree(RenderElement& renderer)
449 {
450     if (is<RenderView>(renderer)) {
451         downcast<RenderView>(renderer).frameView().scheduleRelayout();
452         return;
453     }
454
455     if (renderer.isRooted())
456         renderer.view().frameView().scheduleRelayoutOfSubtree(renderer);
457 }
458
459 void RenderObject::markContainingBlocksForLayout(ScheduleRelayout scheduleRelayout, RenderElement* newRoot)
460 {
461     ASSERT(scheduleRelayout == ScheduleRelayout::No || !newRoot);
462     ASSERT(!isSetNeedsLayoutForbidden());
463
464     auto ancestor = container();
465
466     bool simplifiedNormalFlowLayout = needsSimplifiedNormalFlowLayout() && !selfNeedsLayout() && !normalChildNeedsLayout();
467     bool hasOutOfFlowPosition = !isText() && style().hasOutOfFlowPosition();
468
469     while (ancestor) {
470 #ifndef NDEBUG
471         // FIXME: Remove this once we remove the special cases for counters, quotes and mathml
472         // calling setNeedsLayout during preferred width computation.
473         SetLayoutNeededForbiddenScope layoutForbiddenScope(ancestor, isSetNeedsLayoutForbidden());
474 #endif
475         // Don't mark the outermost object of an unrooted subtree. That object will be
476         // marked when the subtree is added to the document.
477         auto container = ancestor->container();
478         if (!container && !ancestor->isRenderView())
479             return;
480         if (hasOutOfFlowPosition) {
481             bool willSkipRelativelyPositionedInlines = !ancestor->isRenderBlock() || ancestor->isAnonymousBlock();
482             // Skip relatively positioned inlines and anonymous blocks to get to the enclosing RenderBlock.
483             while (ancestor && (!ancestor->isRenderBlock() || ancestor->isAnonymousBlock()))
484                 ancestor = ancestor->container();
485             if (!ancestor || ancestor->posChildNeedsLayout())
486                 return;
487             if (willSkipRelativelyPositionedInlines)
488                 container = ancestor->container();
489             ancestor->setPosChildNeedsLayoutBit(true);
490             simplifiedNormalFlowLayout = true;
491         } else if (simplifiedNormalFlowLayout) {
492             if (ancestor->needsSimplifiedNormalFlowLayout())
493                 return;
494             ancestor->setNeedsSimplifiedNormalFlowLayoutBit(true);
495         } else {
496             if (ancestor->normalChildNeedsLayout())
497                 return;
498             ancestor->setNormalChildNeedsLayoutBit(true);
499         }
500         ASSERT(!ancestor->isSetNeedsLayoutForbidden());
501
502         if (ancestor == newRoot)
503             return;
504
505         if (scheduleRelayout == ScheduleRelayout::Yes && objectIsRelayoutBoundary(ancestor))
506             break;
507
508         hasOutOfFlowPosition = ancestor->style().hasOutOfFlowPosition();
509         ancestor = container;
510     }
511
512     if (scheduleRelayout == ScheduleRelayout::Yes && ancestor)
513         scheduleRelayoutForSubtree(*ancestor);
514 }
515
516 #ifndef NDEBUG
517 void RenderObject::checkBlockPositionedObjectsNeedLayout()
518 {
519     ASSERT(!needsLayout());
520
521     if (is<RenderBlock>(*this))
522         downcast<RenderBlock>(*this).checkPositionedObjectsNeedLayout();
523 }
524 #endif
525
526 void RenderObject::setPreferredLogicalWidthsDirty(bool shouldBeDirty, MarkingBehavior markParents)
527 {
528     bool alreadyDirty = preferredLogicalWidthsDirty();
529     m_bitfields.setPreferredLogicalWidthsDirty(shouldBeDirty);
530     if (shouldBeDirty && !alreadyDirty && markParents == MarkContainingBlockChain && (isText() || !style().hasOutOfFlowPosition()))
531         invalidateContainerPreferredLogicalWidths();
532 }
533
534 void RenderObject::invalidateContainerPreferredLogicalWidths()
535 {
536     // In order to avoid pathological behavior when inlines are deeply nested, we do include them
537     // in the chain that we mark dirty (even though they're kind of irrelevant).
538     auto o = isTableCell() ? containingBlock() : container();
539     while (o && !o->preferredLogicalWidthsDirty()) {
540         // Don't invalidate the outermost object of an unrooted subtree. That object will be 
541         // invalidated when the subtree is added to the document.
542         auto container = o->isTableCell() ? o->containingBlock() : o->container();
543         if (!container && !o->isRenderView())
544             break;
545
546         o->m_bitfields.setPreferredLogicalWidthsDirty(true);
547         if (o->style().hasOutOfFlowPosition())
548             // A positioned object has no effect on the min/max width of its containing block ever.
549             // We can optimize this case and not go up any further.
550             break;
551         o = container;
552     }
553 }
554
555 void RenderObject::setLayerNeedsFullRepaint()
556 {
557     ASSERT(hasLayer());
558     downcast<RenderLayerModelObject>(*this).layer()->setRepaintStatus(NeedsFullRepaint);
559 }
560
561 void RenderObject::setLayerNeedsFullRepaintForPositionedMovementLayout()
562 {
563     ASSERT(hasLayer());
564     downcast<RenderLayerModelObject>(*this).layer()->setRepaintStatus(NeedsFullRepaintForPositionedMovementLayout);
565 }
566
567 RenderBlock* RenderObject::containingBlock() const
568 {
569     auto containingBlockForRenderer = [](const RenderElement& renderer)
570     {
571         auto& style = renderer.style();
572         if (style.position() == AbsolutePosition)
573             return renderer.containingBlockForAbsolutePosition();
574         if (style.position() == FixedPosition)
575             return renderer.containingBlockForFixedPosition();
576         return renderer.containingBlockForObjectInFlow();
577     };
578
579     if (is<RenderText>(*this))
580         return containingBlockForObjectInFlow();
581
582     if (!parent() && is<RenderScrollbarPart>(*this)) {
583         if (auto* scrollbarPart = downcast<RenderScrollbarPart>(*this).rendererOwningScrollbar())
584             return containingBlockForRenderer(*scrollbarPart);
585         return nullptr;
586     }
587     return containingBlockForRenderer(downcast<RenderElement>(*this));
588 }
589
590 RenderBlock* RenderObject::containingBlockForObjectInFlow() const
591 {
592     auto* renderer = parent();
593     while (renderer && ((renderer->isInline() && !renderer->isReplaced()) || !renderer->isRenderBlock()))
594         renderer = renderer->parent();
595     return downcast<RenderBlock>(renderer);
596 }
597
598 void RenderObject::addPDFURLRect(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
599 {
600     Vector<LayoutRect> focusRingRects;
601     addFocusRingRects(focusRingRects, paintOffset, paintInfo.paintContainer);
602     LayoutRect urlRect = unionRect(focusRingRects);
603
604     if (urlRect.isEmpty())
605         return;
606     Node* node = this->node();
607     if (!is<Element>(node) || !node->isLink())
608         return;
609     const AtomicString& href = downcast<Element>(*node).getAttribute(hrefAttr);
610     if (href.isNull())
611         return;
612     paintInfo.context().setURLForRect(node->document().completeURL(href), snappedIntRect(urlRect));
613 }
614
615 #if PLATFORM(IOS)
616 // This function is similar in spirit to RenderText::absoluteRectsForRange, but returns rectangles
617 // which are annotated with additional state which helps iOS draw selections in its unique way.
618 // No annotations are added in this class.
619 // FIXME: Move to RenderText with absoluteRectsForRange()?
620 void RenderObject::collectSelectionRects(Vector<SelectionRect>& rects, unsigned start, unsigned end)
621 {
622     Vector<FloatQuad> quads;
623
624     if (!firstChildSlow()) {
625         // FIXME: WebKit's position for an empty span after a BR is incorrect, so we can't trust 
626         // quads for them. We don't need selection rects for those anyway though, since they 
627         // are just empty containers. See <https://bugs.webkit.org/show_bug.cgi?id=49358>.
628         RenderObject* previous = previousSibling();
629         Node* node = this->node();
630         if (!previous || !previous->isBR() || !node || !node->isContainerNode() || !isInline()) {
631             // For inline elements we don't use absoluteQuads, since it takes into account continuations and leads to wrong results.
632             absoluteQuadsForSelection(quads);
633         }
634     } else {
635         unsigned offset = start;
636         for (RenderObject* child = childAt(start); child && offset < end; child = child->nextSibling(), ++offset)
637             child->absoluteQuads(quads);
638     }
639
640     unsigned numberOfQuads = quads.size();
641     for (unsigned i = 0; i < numberOfQuads; ++i)
642         rects.append(SelectionRect(quads[i].enclosingBoundingBox(), isHorizontalWritingMode(), view().pageNumberForBlockProgressionOffset(quads[i].enclosingBoundingBox().x())));
643 }
644 #endif
645
646 IntRect RenderObject::absoluteBoundingBoxRect(bool useTransforms, bool* wasFixed) const
647 {
648     if (useTransforms) {
649         Vector<FloatQuad> quads;
650         absoluteQuads(quads, wasFixed);
651
652         size_t n = quads.size();
653         if (!n)
654             return IntRect();
655     
656         IntRect result = quads[0].enclosingBoundingBox();
657         for (size_t i = 1; i < n; ++i)
658             result.unite(quads[i].enclosingBoundingBox());
659         return result;
660     }
661
662     FloatPoint absPos = localToAbsolute(FloatPoint(), 0 /* ignore transforms */, wasFixed);
663     Vector<IntRect> rects;
664     absoluteRects(rects, flooredLayoutPoint(absPos));
665
666     size_t n = rects.size();
667     if (!n)
668         return IntRect();
669
670     LayoutRect result = rects[0];
671     for (size_t i = 1; i < n; ++i)
672         result.unite(rects[i]);
673     return snappedIntRect(result);
674 }
675
676 void RenderObject::absoluteFocusRingQuads(Vector<FloatQuad>& quads)
677 {
678     Vector<LayoutRect> rects;
679     // FIXME: addFocusRingRects() needs to be passed this transform-unaware
680     // localToAbsolute() offset here because RenderInline::addFocusRingRects()
681     // implicitly assumes that. This doesn't work correctly with transformed
682     // descendants.
683     FloatPoint absolutePoint = localToAbsolute();
684     addFocusRingRects(rects, flooredLayoutPoint(absolutePoint));
685     float deviceScaleFactor = document().deviceScaleFactor();
686     for (auto rect : rects) {
687         rect.moveBy(LayoutPoint(-absolutePoint));
688         quads.append(localToAbsoluteQuad(FloatQuad(snapRectToDevicePixels(rect, deviceScaleFactor))));
689     }
690 }
691
692 FloatRect RenderObject::absoluteBoundingBoxRectForRange(const Range* range)
693 {
694     if (!range)
695         return FloatRect();
696
697     range->ownerDocument().updateLayout();
698
699     Vector<FloatQuad> quads;
700     range->absoluteTextQuads(quads);
701
702     if (quads.isEmpty())
703         return FloatRect();
704
705     FloatRect result = quads[0].boundingBox();
706     for (size_t i = 1; i < quads.size(); ++i)
707         result.uniteEvenIfEmpty(quads[i].boundingBox());
708
709     return result;
710 }
711
712 void RenderObject::addAbsoluteRectForLayer(LayoutRect& result)
713 {
714     if (hasLayer())
715         result.unite(absoluteBoundingBoxRectIgnoringTransforms());
716
717     if (!is<RenderElement>(*this))
718         return;
719
720     for (auto& child : childrenOfType<RenderObject>(downcast<RenderElement>(*this)))
721         child.addAbsoluteRectForLayer(result);
722 }
723
724 // FIXME: change this to use the subtreePaint terminology
725 LayoutRect RenderObject::paintingRootRect(LayoutRect& topLevelRect)
726 {
727     LayoutRect result = absoluteBoundingBoxRectIgnoringTransforms();
728     topLevelRect = result;
729     if (is<RenderElement>(*this)) {
730         for (auto& child : childrenOfType<RenderObject>(downcast<RenderElement>(*this)))
731             child.addAbsoluteRectForLayer(result);
732     }
733     return result;
734 }
735
736 RenderLayerModelObject* RenderObject::containerForRepaint() const
737 {
738     RenderLayerModelObject* repaintContainer = nullptr;
739
740     if (view().usesCompositing()) {
741         if (RenderLayer* parentLayer = enclosingLayer()) {
742             RenderLayer* compLayer = parentLayer->enclosingCompositingLayerForRepaint();
743             if (compLayer)
744                 repaintContainer = &compLayer->renderer();
745         }
746     }
747     if (view().hasSoftwareFilters()) {
748         if (RenderLayer* parentLayer = enclosingLayer()) {
749             RenderLayer* enclosingFilterLayer = parentLayer->enclosingFilterLayer();
750             if (enclosingFilterLayer)
751                 return &enclosingFilterLayer->renderer();
752         }
753     }
754
755     // If we have a flow thread, then we need to do individual repaints within the RenderRegions instead.
756     // Return the flow thread as a repaint container in order to create a chokepoint that allows us to change
757     // repainting to do individual region repaints.
758     RenderFlowThread* parentRenderFlowThread = flowThreadContainingBlock();
759     if (parentRenderFlowThread) {
760         // If the element has a fixed positioned element with named flow as CB along the CB chain
761         // then the repaint container is not the flow thread.
762         if (hasFixedPosInNamedFlowContainingBlock(this))
763             return repaintContainer;
764         // If we have already found a repaint container then we will repaint into that container only if it is part of the same
765         // flow thread. Otherwise we will need to catch the repaint call and send it to the flow thread.
766         RenderFlowThread* repaintContainerFlowThread = repaintContainer ? repaintContainer->flowThreadContainingBlock() : nullptr;
767         if (!repaintContainerFlowThread || repaintContainerFlowThread != parentRenderFlowThread)
768             repaintContainer = parentRenderFlowThread;
769     }
770     return repaintContainer;
771 }
772
773 void RenderObject::propagateRepaintToParentWithOutlineAutoIfNeeded(const RenderLayerModelObject& repaintContainer, const LayoutRect& repaintRect) const
774 {
775     if (!hasOutlineAutoAncestor())
776         return;
777
778     // FIXME: We should really propagate only when the the child renderer sticks out.
779     bool repaintRectNeedsConverting = false;
780     // Issue repaint on the renderer with outline: auto.
781     for (const auto* renderer = this; renderer; renderer = renderer->parent()) {
782         bool rendererHasOutlineAutoAncestor = renderer->hasOutlineAutoAncestor();
783         ASSERT(rendererHasOutlineAutoAncestor
784             || renderer->outlineStyleForRepaint().outlineStyleIsAuto()
785             || (is<RenderElement>(*renderer) && downcast<RenderElement>(*renderer).hasContinuation()));
786         if (renderer == &repaintContainer && rendererHasOutlineAutoAncestor)
787             repaintRectNeedsConverting = true;
788         if (rendererHasOutlineAutoAncestor)
789             continue;
790         // Issue repaint on the correct repaint container.
791         LayoutRect adjustedRepaintRect = repaintRect;
792         adjustedRepaintRect.inflate(renderer->outlineStyleForRepaint().outlineSize());
793         if (!repaintRectNeedsConverting)
794             repaintContainer.repaintRectangle(adjustedRepaintRect);
795         else if (is<RenderLayerModelObject>(renderer)) {
796             const auto& rendererWithOutline = downcast<RenderLayerModelObject>(*renderer);
797             adjustedRepaintRect = LayoutRect(repaintContainer.localToContainerQuad(FloatRect(adjustedRepaintRect), &rendererWithOutline).boundingBox());
798             rendererWithOutline.repaintRectangle(adjustedRepaintRect);
799         }
800         return;
801     }
802     ASSERT_NOT_REACHED();
803 }
804
805 void RenderObject::repaintUsingContainer(const RenderLayerModelObject* repaintContainer, const LayoutRect& r, bool shouldClipToLayer) const
806 {
807     if (r.isEmpty())
808         return;
809
810     if (!repaintContainer)
811         repaintContainer = &view();
812
813     if (is<RenderFlowThread>(*repaintContainer)) {
814         downcast<RenderFlowThread>(*repaintContainer).repaintRectangleInRegions(r);
815         return;
816     }
817
818     propagateRepaintToParentWithOutlineAutoIfNeeded(*repaintContainer, r);
819
820     if (repaintContainer->hasFilter() && repaintContainer->layer() && repaintContainer->layer()->requiresFullLayerImageForFilters()) {
821         repaintContainer->layer()->setFilterBackendNeedsRepaintingInRect(r);
822         return;
823     }
824
825     if (repaintContainer->isRenderView()) {
826         RenderView& view = this->view();
827         ASSERT(repaintContainer == &view);
828         bool viewHasCompositedLayer = view.isComposited();
829         if (!viewHasCompositedLayer || view.layer()->backing()->paintsIntoWindow()) {
830             LayoutRect rect = r;
831             if (viewHasCompositedLayer && view.layer()->transform())
832                 rect = LayoutRect(view.layer()->transform()->mapRect(snapRectToDevicePixels(rect, document().deviceScaleFactor())));
833             view.repaintViewRectangle(rect);
834             return;
835         }
836     }
837
838     if (view().usesCompositing()) {
839         ASSERT(repaintContainer->isComposited());
840         repaintContainer->layer()->setBackingNeedsRepaintInRect(r, shouldClipToLayer ? GraphicsLayer::ClipToLayer : GraphicsLayer::DoNotClipToLayer);
841     }
842 }
843
844 void RenderObject::repaint() const
845 {
846     // Don't repaint if we're unrooted (note that view() still returns the view when unrooted)
847     if (!isRooted())
848         return;
849
850     const RenderView& view = this->view();
851     if (view.printing())
852         return;
853
854     RenderLayerModelObject* repaintContainer = containerForRepaint();
855     repaintUsingContainer(repaintContainer, clippedOverflowRectForRepaint(repaintContainer));
856 }
857
858 void RenderObject::repaintRectangle(const LayoutRect& r, bool shouldClipToLayer) const
859 {
860     // Don't repaint if we're unrooted (note that view() still returns the view when unrooted)
861     if (!isRooted())
862         return;
863
864     const RenderView& view = this->view();
865     if (view.printing())
866         return;
867
868     LayoutRect dirtyRect(r);
869     // FIXME: layoutDelta needs to be applied in parts before/after transforms and
870     // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
871     dirtyRect.move(view.layoutDelta());
872
873     RenderLayerModelObject* repaintContainer = containerForRepaint();
874     repaintUsingContainer(repaintContainer, computeRectForRepaint(dirtyRect, repaintContainer), shouldClipToLayer);
875 }
876
877 void RenderObject::repaintSlowRepaintObject() const
878 {
879     // Don't repaint if we're unrooted (note that view() still returns the view when unrooted)
880     if (!isRooted())
881         return;
882
883     const RenderView& view = this->view();
884     if (view.printing())
885         return;
886
887     const RenderLayerModelObject* repaintContainer = containerForRepaint();
888     if (!repaintContainer)
889         repaintContainer = &view;
890
891     bool shouldClipToLayer = true;
892     IntRect repaintRect;
893     // If this is the root background, we need to check if there is an extended background rect. If
894     // there is, then we should not allow painting to clip to the layer size.
895     if (isDocumentElementRenderer() || isBody()) {
896         shouldClipToLayer = !view.frameView().hasExtendedBackgroundRectForPainting();
897         repaintRect = snappedIntRect(view.backgroundRect());
898     } else
899         repaintRect = snappedIntRect(clippedOverflowRectForRepaint(repaintContainer));
900
901     repaintUsingContainer(repaintContainer, repaintRect, shouldClipToLayer);
902 }
903
904 IntRect RenderObject::pixelSnappedAbsoluteClippedOverflowRect() const
905 {
906     return snappedIntRect(absoluteClippedOverflowRect());
907 }
908     
909 LayoutRect RenderObject::rectWithOutlineForRepaint(const RenderLayerModelObject* repaintContainer, LayoutUnit outlineWidth) const
910 {
911     LayoutRect r(clippedOverflowRectForRepaint(repaintContainer));
912     r.inflate(outlineWidth);
913     return r;
914 }
915
916 LayoutRect RenderObject::clippedOverflowRectForRepaint(const RenderLayerModelObject*) const
917 {
918     ASSERT_NOT_REACHED();
919     return LayoutRect();
920 }
921
922 LayoutRect RenderObject::computeRectForRepaint(const LayoutRect& rect, const RenderLayerModelObject* repaintContainer, RepaintContext context) const
923 {
924     if (repaintContainer == this)
925         return rect;
926
927     auto* parent = this->parent();
928     if (!parent)
929         return rect;
930
931     LayoutRect adjustedRect = rect;
932     if (parent->hasOverflowClip()) {
933         downcast<RenderBox>(*parent).applyCachedClipAndScrollPositionForRepaint(adjustedRect);
934         if (adjustedRect.isEmpty())
935             return adjustedRect;
936     }
937     return parent->computeRectForRepaint(adjustedRect, repaintContainer, context);
938 }
939
940 FloatRect RenderObject::computeFloatRectForRepaint(const FloatRect&, const RenderLayerModelObject*, bool) const
941 {
942     ASSERT_NOT_REACHED();
943     return FloatRect();
944 }
945
946 #if ENABLE(TREE_DEBUGGING)
947
948 static void showRenderTreeLegend()
949 {
950     fprintf(stderr, "\n(B)lock/(I)nline/I(N)line-block, (A)bsolute/Fi(X)ed/(R)elative/Stic(K)y, (F)loating, (O)verflow clip, Anon(Y)mous, (G)enerated, has(L)ayer, (C)omposited, (+)Dirty style, (+)Dirty layout\n");
951 }
952
953 void RenderObject::showNodeTreeForThis() const
954 {
955     if (!node())
956         return;
957     node()->showTreeForThis();
958 }
959
960 void RenderObject::showRenderTreeForThis() const
961 {
962     const WebCore::RenderObject* root = this;
963     while (root->parent())
964         root = root->parent();
965     showRenderTreeLegend();
966     root->showRenderSubTreeAndMark(this, 1);
967 }
968
969 void RenderObject::showLineTreeForThis() const
970 {
971     if (!is<RenderBlockFlow>(*this))
972         return;
973     showRenderTreeLegend();
974     showRenderObject(false, 1);
975     downcast<RenderBlockFlow>(*this).showLineTreeAndMark(nullptr, 2);
976 }
977
978 static const RenderFlowThread* flowThreadContainingBlockFromRenderer(const RenderObject* renderer)
979 {
980     if (!renderer)
981         return nullptr;
982
983     if (renderer->flowThreadState() == RenderObject::NotInsideFlowThread)
984         return nullptr;
985
986     if (is<RenderFlowThread>(*renderer))
987         return downcast<RenderFlowThread>(renderer);
988
989     if (is<RenderBlock>(*renderer))
990         return downcast<RenderBlock>(*renderer).cachedFlowThreadContainingBlock();
991
992     return nullptr;
993 }
994
995 void RenderObject::showRegionsInformation() const
996 {
997     const RenderFlowThread* ftcb = flowThreadContainingBlockFromRenderer(this);
998
999     if (!ftcb) {
1000         // Only the boxes have region range information.
1001         // Try to get the flow thread containing block information
1002         // from the containing block of this box.
1003         if (is<RenderBox>(*this))
1004             ftcb = flowThreadContainingBlockFromRenderer(containingBlock());
1005     }
1006
1007     if (!ftcb)
1008         return;
1009
1010     RenderRegion* startRegion = nullptr;
1011     RenderRegion* endRegion = nullptr;
1012     ftcb->getRegionRangeForBox(downcast<RenderBox>(this), startRegion, endRegion);
1013     fprintf(stderr, " [Rs:%p Re:%p]", startRegion, endRegion);
1014 }
1015
1016 void RenderObject::showRenderObject(bool mark, int depth) const
1017 {
1018     if (isInlineBlockOrInlineTable())
1019         fputc('N', stderr);
1020     else if (isInline())
1021         fputc('I', stderr);
1022     else
1023         fputc('B', stderr);
1024     
1025     if (isPositioned()) {
1026         if (isRelPositioned())
1027             fputc('R', stderr);
1028         else if (isStickyPositioned())
1029             fputc('K', stderr);
1030         else if (isOutOfFlowPositioned()) {
1031             if (style().position() == AbsolutePosition)
1032                 fputc('A', stderr);
1033             else
1034                 fputc('X', stderr);
1035         }
1036     } else
1037         fputc('-', stderr);
1038
1039     if (isFloating())
1040         fputc('F', stderr);
1041     else
1042         fputc('-', stderr);
1043
1044     if (hasOverflowClip())
1045         fputc('O', stderr);
1046     else
1047         fputc('-', stderr);
1048
1049     if (isAnonymous())
1050         fputc('Y', stderr);
1051     else
1052         fputc('-', stderr);
1053
1054     if (isPseudoElement() || isAnonymous())
1055         fputc('G', stderr);
1056     else
1057         fputc('-', stderr);
1058
1059     if (hasLayer())
1060         fputc('L', stderr);
1061     else
1062         fputc('-', stderr);
1063
1064     if (isComposited())
1065         fputc('C', stderr);
1066     else
1067         fputc('-', stderr);
1068
1069     fputc(' ', stderr);
1070
1071     if (node() && node()->needsStyleRecalc())
1072         fputc('+', stderr);
1073     else
1074         fputc('-', stderr);
1075
1076     if (needsLayout())
1077         fputc('+', stderr);
1078     else
1079         fputc('-', stderr);
1080
1081     int printedCharacters = 0;
1082     if (mark) {
1083         fprintf(stderr, "*");
1084         ++printedCharacters;
1085     }
1086
1087     while (++printedCharacters <= depth * 2)
1088         fputc(' ', stderr);
1089
1090     if (node())
1091         fprintf(stderr, "%s ", node()->nodeName().utf8().data());
1092
1093     String name = renderName();
1094     // FIXME: Renderer's name should not include property value listing.
1095     int pos = name.find('(');
1096     if (pos > 0)
1097         fprintf(stderr, "%s", name.left(pos - 1).utf8().data());
1098     else
1099         fprintf(stderr, "%s", name.utf8().data());
1100
1101     if (is<RenderBox>(*this)) {
1102         auto& renderBox = downcast<RenderBox>(*this);
1103         FloatRect boxRect = renderBox.frameRect();
1104         if (renderBox.isInFlowPositioned())
1105             boxRect.move(renderBox.offsetForInFlowPosition());
1106         fprintf(stderr, "  (%.2f, %.2f) (%.2f, %.2f)", boxRect.x(), boxRect.y(), boxRect.width(), boxRect.height());
1107     } else if (is<RenderInline>(*this) && isInFlowPositioned()) {
1108         FloatSize inlineOffset = downcast<RenderInline>(*this).offsetForInFlowPosition();
1109         fprintf(stderr, "  (%.2f, %.2f)", inlineOffset.width(), inlineOffset.height());
1110     }
1111
1112     fprintf(stderr, " renderer->(%p)", this);
1113     if (node()) {
1114         fprintf(stderr, " node->(%p)", node());
1115         if (node()->isTextNode()) {
1116             String value = node()->nodeValue();
1117             fprintf(stderr, " length->(%u)", value.length());
1118
1119             value.replaceWithLiteral('\\', "\\\\");
1120             value.replaceWithLiteral('\n', "\\n");
1121             
1122             const int maxPrintedLength = 80;
1123             if (value.length() > maxPrintedLength) {
1124                 String substring = value.substring(0, maxPrintedLength);
1125                 fprintf(stderr, " \"%s\"...", substring.utf8().data());
1126             } else
1127                 fprintf(stderr, " \"%s\"", value.utf8().data());
1128         }
1129     }
1130
1131     showRegionsInformation();
1132     fprintf(stderr, "\n");
1133 }
1134
1135 void RenderObject::showRenderSubTreeAndMark(const RenderObject* markedObject, int depth) const
1136 {
1137     showRenderObject(markedObject == this, depth);
1138     if (is<RenderBlockFlow>(*this))
1139         downcast<RenderBlockFlow>(*this).showLineTreeAndMark(nullptr, depth + 1);
1140
1141     for (const RenderObject* child = firstChildSlow(); child; child = child->nextSibling())
1142         child->showRenderSubTreeAndMark(markedObject, depth + 1);
1143 }
1144
1145 #endif // NDEBUG
1146
1147 SelectionSubtreeRoot& RenderObject::selectionRoot() const
1148 {
1149     RenderFlowThread* flowThread = flowThreadContainingBlock();
1150     if (!flowThread)
1151         return view();
1152
1153     if (is<RenderNamedFlowThread>(*flowThread))
1154         return downcast<RenderNamedFlowThread>(*flowThread);
1155     if (is<RenderMultiColumnFlowThread>(*flowThread)) {
1156         if (!flowThread->containingBlock())
1157             return view();
1158         return flowThread->containingBlock()->selectionRoot();
1159     }
1160     ASSERT_NOT_REACHED();
1161     return view();
1162 }
1163
1164 void RenderObject::selectionStartEnd(unsigned& spos, unsigned& epos) const
1165 {
1166     selectionRoot().selectionData().selectionStartEndPositions(spos, epos);
1167 }
1168
1169 FloatPoint RenderObject::localToAbsolute(const FloatPoint& localPoint, MapCoordinatesFlags mode, bool* wasFixed) const
1170 {
1171     TransformState transformState(TransformState::ApplyTransformDirection, localPoint);
1172     mapLocalToContainer(nullptr, transformState, mode | ApplyContainerFlip, wasFixed);
1173     transformState.flatten();
1174     
1175     return transformState.lastPlanarPoint();
1176 }
1177
1178 FloatPoint RenderObject::absoluteToLocal(const FloatPoint& containerPoint, MapCoordinatesFlags mode) const
1179 {
1180     TransformState transformState(TransformState::UnapplyInverseTransformDirection, containerPoint);
1181     mapAbsoluteToLocalPoint(mode, transformState);
1182     transformState.flatten();
1183     
1184     return transformState.lastPlanarPoint();
1185 }
1186
1187 FloatQuad RenderObject::absoluteToLocalQuad(const FloatQuad& quad, MapCoordinatesFlags mode) const
1188 {
1189     TransformState transformState(TransformState::UnapplyInverseTransformDirection, quad.boundingBox().center(), quad);
1190     mapAbsoluteToLocalPoint(mode, transformState);
1191     transformState.flatten();
1192     return transformState.lastPlanarQuad();
1193 }
1194
1195 void RenderObject::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags mode, bool* wasFixed) const
1196 {
1197     if (repaintContainer == this)
1198         return;
1199
1200     auto* parent = this->parent();
1201     if (!parent)
1202         return;
1203
1204     // FIXME: this should call offsetFromContainer to share code, but I'm not sure it's ever called.
1205     LayoutPoint centerPoint(transformState.mappedPoint());
1206     if (mode & ApplyContainerFlip && is<RenderBox>(*parent)) {
1207         if (parent->style().isFlippedBlocksWritingMode())
1208             transformState.move(downcast<RenderBox>(parent)->flipForWritingMode(LayoutPoint(transformState.mappedPoint())) - centerPoint);
1209         mode &= ~ApplyContainerFlip;
1210     }
1211
1212     if (is<RenderBox>(*parent))
1213         transformState.move(-toLayoutSize(downcast<RenderBox>(*parent).scrollPosition()));
1214
1215     parent->mapLocalToContainer(repaintContainer, transformState, mode, wasFixed);
1216 }
1217
1218 const RenderObject* RenderObject::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
1219 {
1220     ASSERT_UNUSED(ancestorToStopAt, ancestorToStopAt != this);
1221
1222     auto* container = parent();
1223     if (!container)
1224         return nullptr;
1225
1226     // FIXME: this should call offsetFromContainer to share code, but I'm not sure it's ever called.
1227     LayoutSize offset;
1228     if (is<RenderBox>(*container))
1229         offset = -toLayoutSize(downcast<RenderBox>(*container).scrollPosition());
1230
1231     geometryMap.push(this, offset, false);
1232     
1233     return container;
1234 }
1235
1236 void RenderObject::mapAbsoluteToLocalPoint(MapCoordinatesFlags mode, TransformState& transformState) const
1237 {
1238     if (auto* parent = this->parent()) {
1239         parent->mapAbsoluteToLocalPoint(mode, transformState);
1240         if (is<RenderBox>(*parent))
1241             transformState.move(toLayoutSize(downcast<RenderBox>(*parent).scrollPosition()));
1242     }
1243 }
1244
1245 bool RenderObject::shouldUseTransformFromContainer(const RenderObject* containerObject) const
1246 {
1247 #if ENABLE(3D_TRANSFORMS)
1248     return hasTransform() || (containerObject && containerObject->style().hasPerspective());
1249 #else
1250     UNUSED_PARAM(containerObject);
1251     return hasTransform();
1252 #endif
1253 }
1254
1255 void RenderObject::getTransformFromContainer(const RenderObject* containerObject, const LayoutSize& offsetInContainer, TransformationMatrix& transform) const
1256 {
1257     transform.makeIdentity();
1258     transform.translate(offsetInContainer.width(), offsetInContainer.height());
1259     RenderLayer* layer;
1260     if (hasLayer() && (layer = downcast<RenderLayerModelObject>(*this).layer()) && layer->transform())
1261         transform.multiply(layer->currentTransform());
1262     
1263 #if ENABLE(3D_TRANSFORMS)
1264     if (containerObject && containerObject->hasLayer() && containerObject->style().hasPerspective()) {
1265         // Perpsective on the container affects us, so we have to factor it in here.
1266         ASSERT(containerObject->hasLayer());
1267         FloatPoint perspectiveOrigin = downcast<RenderLayerModelObject>(*containerObject).layer()->perspectiveOrigin();
1268
1269         TransformationMatrix perspectiveMatrix;
1270         perspectiveMatrix.applyPerspective(containerObject->style().perspective());
1271         
1272         transform.translateRight3d(-perspectiveOrigin.x(), -perspectiveOrigin.y(), 0);
1273         transform = perspectiveMatrix * transform;
1274         transform.translateRight3d(perspectiveOrigin.x(), perspectiveOrigin.y(), 0);
1275     }
1276 #else
1277     UNUSED_PARAM(containerObject);
1278 #endif
1279 }
1280
1281 FloatQuad RenderObject::localToContainerQuad(const FloatQuad& localQuad, const RenderLayerModelObject* repaintContainer, MapCoordinatesFlags mode, bool* wasFixed) const
1282 {
1283     // Track the point at the center of the quad's bounding box. As mapLocalToContainer() calls offsetFromContainer(),
1284     // it will use that point as the reference point to decide which column's transform to apply in multiple-column blocks.
1285     TransformState transformState(TransformState::ApplyTransformDirection, localQuad.boundingBox().center(), localQuad);
1286     mapLocalToContainer(repaintContainer, transformState, mode | ApplyContainerFlip, wasFixed);
1287     transformState.flatten();
1288     
1289     return transformState.lastPlanarQuad();
1290 }
1291
1292 FloatPoint RenderObject::localToContainerPoint(const FloatPoint& localPoint, const RenderLayerModelObject* repaintContainer, MapCoordinatesFlags mode, bool* wasFixed) const
1293 {
1294     TransformState transformState(TransformState::ApplyTransformDirection, localPoint);
1295     mapLocalToContainer(repaintContainer, transformState, mode | ApplyContainerFlip, wasFixed);
1296     transformState.flatten();
1297
1298     return transformState.lastPlanarPoint();
1299 }
1300
1301 LayoutSize RenderObject::offsetFromContainer(RenderElement& container, const LayoutPoint&, bool* offsetDependsOnPoint) const
1302 {
1303     ASSERT(&container == this->container());
1304
1305     LayoutSize offset;
1306     if (is<RenderBox>(container))
1307         offset -= toLayoutSize(downcast<RenderBox>(container).scrollPosition());
1308
1309     if (offsetDependsOnPoint)
1310         *offsetDependsOnPoint = is<RenderFlowThread>(container);
1311
1312     return offset;
1313 }
1314
1315 LayoutSize RenderObject::offsetFromAncestorContainer(RenderElement& container) const
1316 {
1317     LayoutSize offset;
1318     LayoutPoint referencePoint;
1319     const RenderObject* currContainer = this;
1320     do {
1321         RenderElement* nextContainer = currContainer->container();
1322         ASSERT(nextContainer);  // This means we reached the top without finding container.
1323         if (!nextContainer)
1324             break;
1325         ASSERT(!currContainer->hasTransform());
1326         LayoutSize currentOffset = currContainer->offsetFromContainer(*nextContainer, referencePoint);
1327         offset += currentOffset;
1328         referencePoint.move(currentOffset);
1329         currContainer = nextContainer;
1330     } while (currContainer != &container);
1331
1332     return offset;
1333 }
1334
1335 LayoutRect RenderObject::localCaretRect(InlineBox*, unsigned, LayoutUnit* extraWidthToEndOfLine)
1336 {
1337     if (extraWidthToEndOfLine)
1338         *extraWidthToEndOfLine = 0;
1339
1340     return LayoutRect();
1341 }
1342
1343 bool RenderObject::isRooted() const
1344 {
1345     return isDescendantOf(&view());
1346 }
1347
1348 static inline RenderElement* containerForElement(const RenderObject& renderer, const RenderLayerModelObject* repaintContainer, bool* repaintContainerSkipped)
1349 {
1350     // This method is extremely similar to containingBlock(), but with a few notable
1351     // exceptions.
1352     // (1) For normal flow elements, it just returns the parent.
1353     // (2) For absolute positioned elements, it will return a relative positioned inline, while
1354     // containingBlock() skips to the non-anonymous containing block.
1355     // This does mean that computePositionedLogicalWidth and computePositionedLogicalHeight have to use container().
1356     EPosition pos = renderer.style().position();
1357     auto* parent = renderer.parent();
1358     if (is<RenderText>(renderer) || (pos != FixedPosition && pos != AbsolutePosition))
1359         return parent;
1360     for (; parent && (pos == AbsolutePosition ? !parent->canContainAbsolutelyPositionedObjects() : !parent->canContainFixedPositionObjects()); parent = parent->parent()) {
1361         if (repaintContainerSkipped && repaintContainer == parent)
1362             *repaintContainerSkipped = true;
1363     }
1364     return parent;
1365 }
1366
1367 RenderElement* RenderObject::container() const
1368 {
1369     return containerForElement(*this, nullptr, nullptr);
1370 }
1371
1372 RenderElement* RenderObject::container(const RenderLayerModelObject* repaintContainer, bool& repaintContainerSkipped) const
1373 {
1374     repaintContainerSkipped = false;
1375     return containerForElement(*this, repaintContainer, &repaintContainerSkipped);
1376 }
1377
1378 bool RenderObject::isSelectionBorder() const
1379 {
1380     SelectionState st = selectionState();
1381     return st == SelectionStart
1382         || st == SelectionEnd
1383         || st == SelectionBoth
1384         || view().selectionUnsplitStart() == this
1385         || view().selectionUnsplitEnd() == this;
1386 }
1387
1388 void RenderObject::willBeDestroyed()
1389 {
1390     // For accessibility management, notify the parent of the imminent change to its child set.
1391     // We do it now, before remove(), while the parent pointer is still available.
1392     if (AXObjectCache* cache = document().existingAXObjectCache())
1393         cache->childrenChanged(this->parent());
1394
1395     removeFromParent();
1396
1397     ASSERT(documentBeingDestroyed() || !is<RenderElement>(*this) || !view().frameView().hasSlowRepaintObject(downcast<RenderElement>(*this)));
1398
1399     // The remove() call above may invoke axObjectCache()->childrenChanged() on the parent, which may require the AX render
1400     // object for this renderer. So we remove the AX render object now, after the renderer is removed.
1401     if (AXObjectCache* cache = document().existingAXObjectCache())
1402         cache->remove(this);
1403
1404     // FIXME: Would like to do this in RenderBoxModelObject, but the timing is so complicated that this can't easily
1405     // be moved into RenderBoxModelObject::destroy.
1406     if (hasLayer()) {
1407         setHasLayer(false);
1408         downcast<RenderLayerModelObject>(*this).destroyLayer();
1409     }
1410
1411     removeRareData();
1412 }
1413
1414 void RenderObject::insertedIntoTree()
1415 {
1416     // FIXME: We should ASSERT(isRooted()) here but generated content makes some out-of-order insertion.
1417
1418     if (!isFloating() && parent()->childrenInline())
1419         parent()->dirtyLinesFromChangedChild(*this);
1420
1421     if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1422         flowThread->flowThreadDescendantInserted(this);
1423 }
1424
1425 void RenderObject::willBeRemovedFromTree()
1426 {
1427     // FIXME: We should ASSERT(isRooted()) but we have some out-of-order removals which would need to be fixed first.
1428
1429     setFlowThreadState(NotInsideFlowThread);
1430
1431     // Update cached boundaries in SVG renderers, if a child is removed.
1432     parent()->setNeedsBoundariesUpdate();
1433 }
1434
1435 void RenderObject::destroyAndCleanupAnonymousWrappers()
1436 {
1437     // If the tree is destroyed, there is no need for a clean-up phase.
1438     if (documentBeingDestroyed()) {
1439         destroy();
1440         return;
1441     }
1442
1443     auto* destroyRoot = this;
1444     auto* destroyRootParent = destroyRoot->parent();
1445     while (destroyRootParent && destroyRootParent->isAnonymous()) {
1446         if (!destroyRootParent->isTableCell() && !destroyRootParent->isTableRow()
1447             && !destroyRootParent->isTableCaption() && !destroyRootParent->isTableSection() && !destroyRootParent->isTable())
1448             break;
1449         // single child?
1450         if (!(destroyRootParent->firstChild() == destroyRoot && destroyRootParent->lastChild() == destroyRoot))
1451             break;
1452         destroyRoot = destroyRootParent;
1453         destroyRootParent = destroyRootParent->parent();
1454     }
1455
1456     if (is<RenderTableRow>(*destroyRoot)) {
1457         downcast<RenderTableRow>(*destroyRoot).destroyAndCollapseAnonymousSiblingRows();
1458         return;
1459     }
1460
1461     destroyRoot->destroy();
1462     // WARNING: |this| is deleted here.
1463 }
1464
1465 void RenderObject::destroy()
1466 {
1467     m_bitfields.setBeingDestroyed(true);
1468
1469 #if PLATFORM(IOS)
1470     if (hasLayer())
1471         downcast<RenderBoxModelObject>(*this).layer()->willBeDestroyed();
1472 #endif
1473
1474     willBeDestroyed();
1475     if (is<RenderWidget>(*this)) {
1476         downcast<RenderWidget>(*this).deref();
1477         return;
1478     }
1479     delete this;
1480 }
1481
1482 VisiblePosition RenderObject::positionForPoint(const LayoutPoint&, const RenderRegion*)
1483 {
1484     return createVisiblePosition(caretMinOffset(), DOWNSTREAM);
1485 }
1486
1487 void RenderObject::updateDragState(bool dragOn)
1488 {
1489     bool valueChanged = (dragOn != isDragging());
1490     setIsDragging(dragOn);
1491     if (valueChanged && node() && (style().affectedByDrag() || (is<Element>(*node()) && downcast<Element>(*node()).childrenAffectedByDrag())))
1492         node()->setNeedsStyleRecalc();
1493
1494     if (!is<RenderElement>(*this))
1495         return;
1496
1497     for (auto& child : childrenOfType<RenderObject>(downcast<RenderElement>(*this)))
1498         child.updateDragState(dragOn);
1499 }
1500
1501 bool RenderObject::isComposited() const
1502 {
1503     return hasLayer() && downcast<RenderLayerModelObject>(*this).layer()->isComposited();
1504 }
1505
1506 bool RenderObject::isAnonymousInlineBlock() const
1507 {
1508     return isAnonymous() && style().display() == INLINE_BLOCK && style().styleType() == NOPSEUDO && isRenderBlockFlow() && !isRubyRun() && !isRubyBase() && !isRuby(parent());
1509 }
1510
1511 bool RenderObject::hitTest(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestFilter hitTestFilter)
1512 {
1513     bool inside = false;
1514     if (hitTestFilter != HitTestSelf) {
1515         // First test the foreground layer (lines and inlines).
1516         inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestForeground);
1517
1518         // Test floats next.
1519         if (!inside)
1520             inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestFloat);
1521
1522         // Finally test to see if the mouse is in the background (within a child block's background).
1523         if (!inside)
1524             inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestChildBlockBackgrounds);
1525     }
1526
1527     // See if the mouse is inside us but not any of our descendants
1528     if (hitTestFilter != HitTestDescendants && !inside)
1529         inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestBlockBackground);
1530
1531     return inside;
1532 }
1533
1534 void RenderObject::updateHitTestResult(HitTestResult& result, const LayoutPoint& point)
1535 {
1536     if (result.innerNode())
1537         return;
1538
1539     Node* node = this->node();
1540
1541     // If we hit the anonymous renderers inside generated content we should
1542     // actually hit the generated content so walk up to the PseudoElement.
1543     if (!node && parent() && parent()->isBeforeOrAfterContent()) {
1544         for (auto* renderer = parent(); renderer && !node; renderer = renderer->parent())
1545             node = renderer->element();
1546     }
1547
1548     if (node) {
1549         result.setInnerNode(node);
1550         if (!result.innerNonSharedNode())
1551             result.setInnerNonSharedNode(node);
1552         result.setLocalPoint(point);
1553     }
1554 }
1555
1556 bool RenderObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& /*locationInContainer*/, const LayoutPoint& /*accumulatedOffset*/, HitTestAction)
1557 {
1558     return false;
1559 }
1560
1561 int RenderObject::innerLineHeight() const
1562 {
1563     return style().computedLineHeight();
1564 }
1565
1566 #if ENABLE(DASHBOARD_SUPPORT)
1567 void RenderObject::addAnnotatedRegions(Vector<AnnotatedRegionValue>& regions)
1568 {
1569     // Convert the style regions to absolute coordinates.
1570     if (style().visibility() != VISIBLE || !is<RenderBox>(*this))
1571         return;
1572     
1573     auto& box = downcast<RenderBox>(*this);
1574     FloatPoint absPos = localToAbsolute();
1575
1576     const Vector<StyleDashboardRegion>& styleRegions = style().dashboardRegions();
1577     for (const auto& styleRegion : styleRegions) {
1578         LayoutUnit w = box.width();
1579         LayoutUnit h = box.height();
1580
1581         AnnotatedRegionValue region;
1582         region.label = styleRegion.label;
1583         region.bounds = LayoutRect(styleRegion.offset.left().value(),
1584                                    styleRegion.offset.top().value(),
1585                                    w - styleRegion.offset.left().value() - styleRegion.offset.right().value(),
1586                                    h - styleRegion.offset.top().value() - styleRegion.offset.bottom().value());
1587         region.type = styleRegion.type;
1588
1589         region.clip = computeAbsoluteRepaintRect(region.bounds);
1590         if (region.clip.height() < 0) {
1591             region.clip.setHeight(0);
1592             region.clip.setWidth(0);
1593         }
1594
1595         region.bounds.setX(absPos.x() + styleRegion.offset.left().value());
1596         region.bounds.setY(absPos.y() + styleRegion.offset.top().value());
1597
1598         regions.append(region);
1599     }
1600 }
1601
1602 void RenderObject::collectAnnotatedRegions(Vector<AnnotatedRegionValue>& regions)
1603 {
1604     // RenderTexts don't have their own style, they just use their parent's style,
1605     // so we don't want to include them.
1606     if (is<RenderText>(*this))
1607         return;
1608
1609     addAnnotatedRegions(regions);
1610     for (RenderObject* current = downcast<RenderElement>(*this).firstChild(); current; current = current->nextSibling())
1611         current->collectAnnotatedRegions(regions);
1612 }
1613 #endif
1614
1615 int RenderObject::caretMinOffset() const
1616 {
1617     return 0;
1618 }
1619
1620 int RenderObject::caretMaxOffset() const
1621 {
1622     if (isReplaced())
1623         return node() ? std::max(1U, node()->countChildNodes()) : 1;
1624     if (isHR())
1625         return 1;
1626     return 0;
1627 }
1628
1629 int RenderObject::previousOffset(int current) const
1630 {
1631     return current - 1;
1632 }
1633
1634 int RenderObject::previousOffsetForBackwardDeletion(int current) const
1635 {
1636     return current - 1;
1637 }
1638
1639 int RenderObject::nextOffset(int current) const
1640 {
1641     return current + 1;
1642 }
1643
1644 void RenderObject::adjustRectForOutlineAndShadow(LayoutRect& rect) const
1645 {
1646     LayoutUnit outlineSize = outlineStyleForRepaint().outlineSize();
1647     if (const ShadowData* boxShadow = style().boxShadow()) {
1648         boxShadow->adjustRectForShadow(rect, outlineSize);
1649         return;
1650     }
1651     rect.inflate(outlineSize);
1652 }
1653
1654 void RenderObject::imageChanged(CachedImage* image, const IntRect* rect)
1655 {
1656     imageChanged(static_cast<WrappedImagePtr>(image), rect);
1657 }
1658
1659 RenderBoxModelObject* RenderObject::offsetParent() const
1660 {
1661     // If any of the following holds true return null and stop this algorithm:
1662     // A is the root element.
1663     // A is the HTML body element.
1664     // The computed value of the position property for element A is fixed.
1665     if (isDocumentElementRenderer() || isBody() || (isOutOfFlowPositioned() && style().position() == FixedPosition))
1666         return nullptr;
1667
1668     // If A is an area HTML element which has a map HTML element somewhere in the ancestor
1669     // chain return the nearest ancestor map HTML element and stop this algorithm.
1670     // FIXME: Implement!
1671     
1672     // Return the nearest ancestor element of A for which at least one of the following is
1673     // true and stop this algorithm if such an ancestor is found:
1674     //     * The computed value of the position property is not static.
1675     //     * It is the HTML body element.
1676     //     * The computed value of the position property of A is static and the ancestor
1677     //       is one of the following HTML elements: td, th, or table.
1678     //     * Our own extension: if there is a difference in the effective zoom
1679
1680     bool skipTables = isPositioned();
1681     float currZoom = style().effectiveZoom();
1682     auto current = parent();
1683     while (current && (!current->element() || (!current->isPositioned() && !current->isBody())) && !is<RenderNamedFlowThread>(*current)) {
1684         Element* element = current->element();
1685         if (!skipTables && element && (is<HTMLTableElement>(*element) || is<HTMLTableCellElement>(*element)))
1686             break;
1687  
1688         float newZoom = current->style().effectiveZoom();
1689         if (currZoom != newZoom)
1690             break;
1691         currZoom = newZoom;
1692         current = current->parent();
1693     }
1694     
1695     // CSS regions specification says that region flows should return the body element as their offsetParent.
1696     if (is<RenderNamedFlowThread>(current)) {
1697         auto* body = document().bodyOrFrameset();
1698         current = body ? body->renderer() : nullptr;
1699     }
1700     
1701     return is<RenderBoxModelObject>(current) ? downcast<RenderBoxModelObject>(current) : nullptr;
1702 }
1703
1704 VisiblePosition RenderObject::createVisiblePosition(int offset, EAffinity affinity) const
1705 {
1706     // If this is a non-anonymous renderer in an editable area, then it's simple.
1707     if (Node* node = nonPseudoNode()) {
1708         if (!node->hasEditableStyle()) {
1709             // If it can be found, we prefer a visually equivalent position that is editable. 
1710             Position position = createLegacyEditingPosition(node, offset);
1711             Position candidate = position.downstream(CanCrossEditingBoundary);
1712             if (candidate.deprecatedNode()->hasEditableStyle())
1713                 return VisiblePosition(candidate, affinity);
1714             candidate = position.upstream(CanCrossEditingBoundary);
1715             if (candidate.deprecatedNode()->hasEditableStyle())
1716                 return VisiblePosition(candidate, affinity);
1717         }
1718         // FIXME: Eliminate legacy editing positions
1719         return VisiblePosition(createLegacyEditingPosition(node, offset), affinity);
1720     }
1721
1722     // We don't want to cross the boundary between editable and non-editable
1723     // regions of the document, but that is either impossible or at least
1724     // extremely unlikely in any normal case because we stop as soon as we
1725     // find a single non-anonymous renderer.
1726
1727     // Find a nearby non-anonymous renderer.
1728     const RenderObject* child = this;
1729     while (const auto parent = child->parent()) {
1730         // Find non-anonymous content after.
1731         const RenderObject* renderer = child;
1732         while ((renderer = renderer->nextInPreOrder(parent))) {
1733             if (Node* node = renderer->nonPseudoNode())
1734                 return VisiblePosition(firstPositionInOrBeforeNode(node), DOWNSTREAM);
1735         }
1736
1737         // Find non-anonymous content before.
1738         renderer = child;
1739         while ((renderer = renderer->previousInPreOrder())) {
1740             if (renderer == parent)
1741                 break;
1742             if (Node* node = renderer->nonPseudoNode())
1743                 return VisiblePosition(lastPositionInOrAfterNode(node), DOWNSTREAM);
1744         }
1745
1746         // Use the parent itself unless it too is anonymous.
1747         if (Element* element = parent->nonPseudoElement())
1748             return VisiblePosition(firstPositionInOrBeforeNode(element), DOWNSTREAM);
1749
1750         // Repeat at the next level up.
1751         child = parent;
1752     }
1753
1754     // Everything was anonymous. Give up.
1755     return VisiblePosition();
1756 }
1757
1758 VisiblePosition RenderObject::createVisiblePosition(const Position& position) const
1759 {
1760     if (position.isNotNull())
1761         return VisiblePosition(position);
1762
1763     ASSERT(!node());
1764     return createVisiblePosition(0, DOWNSTREAM);
1765 }
1766
1767 CursorDirective RenderObject::getCursor(const LayoutPoint&, Cursor&) const
1768 {
1769     return SetCursorBasedOnStyle;
1770 }
1771
1772 bool RenderObject::canUpdateSelectionOnRootLineBoxes()
1773 {
1774     if (needsLayout())
1775         return false;
1776
1777     RenderBlock* containingBlock = this->containingBlock();
1778     return containingBlock ? !containingBlock->needsLayout() : true;
1779 }
1780
1781 // We only create "generated" child renderers like one for first-letter if:
1782 // - the firstLetterBlock can have children in the DOM and
1783 // - the block doesn't have any special assumption on its text children.
1784 // This correctly prevents form controls from having such renderers.
1785 bool RenderObject::canHaveGeneratedChildren() const
1786 {
1787     return canHaveChildren();
1788 }
1789
1790 Node* RenderObject::generatingPseudoHostElement() const
1791 {
1792     return downcast<PseudoElement>(*node()).hostElement();
1793 }
1794
1795 void RenderObject::setNeedsBoundariesUpdate()
1796 {
1797     if (auto renderer = parent())
1798         renderer->setNeedsBoundariesUpdate();
1799 }
1800
1801 FloatRect RenderObject::objectBoundingBox() const
1802 {
1803     ASSERT_NOT_REACHED();
1804     return FloatRect();
1805 }
1806
1807 FloatRect RenderObject::strokeBoundingBox() const
1808 {
1809     ASSERT_NOT_REACHED();
1810     return FloatRect();
1811 }
1812
1813 // Returns the smallest rectangle enclosing all of the painted content
1814 // respecting clipping, masking, filters, opacity, stroke-width and markers
1815 FloatRect RenderObject::repaintRectInLocalCoordinates() const
1816 {
1817     ASSERT_NOT_REACHED();
1818     return FloatRect();
1819 }
1820
1821 AffineTransform RenderObject::localTransform() const
1822 {
1823     static const AffineTransform identity;
1824     return identity;
1825 }
1826
1827 const AffineTransform& RenderObject::localToParentTransform() const
1828 {
1829     static const AffineTransform identity;
1830     return identity;
1831 }
1832
1833 bool RenderObject::nodeAtFloatPoint(const HitTestRequest&, HitTestResult&, const FloatPoint&, HitTestAction)
1834 {
1835     ASSERT_NOT_REACHED();
1836     return false;
1837 }
1838
1839 RenderNamedFlowFragment* RenderObject::currentRenderNamedFlowFragment() const
1840 {
1841     RenderFlowThread* flowThread = flowThreadContainingBlock();
1842     if (!is<RenderNamedFlowThread>(flowThread))
1843         return nullptr;
1844
1845     // FIXME: Once regions are fully integrated with the compositing system we should uncomment this assert.
1846     // This assert needs to be disabled because it's possible to ask for the ancestor clipping rectangle of
1847     // a layer without knowing the containing region in advance.
1848     // ASSERT(flowThread->currentRegion() && flowThread->currentRegion()->isRenderNamedFlowFragment());
1849
1850     return downcast<RenderNamedFlowFragment>(flowThread->currentRegion());
1851 }
1852
1853 RenderFlowThread* RenderObject::locateFlowThreadContainingBlock() const
1854 {
1855     RenderBlock* containingBlock = this->containingBlock();
1856     return containingBlock ? containingBlock->flowThreadContainingBlock() : nullptr;
1857 }
1858
1859 void RenderObject::calculateBorderStyleColor(const EBorderStyle& style, const BoxSide& side, Color& color)
1860 {
1861     ASSERT(style == INSET || style == OUTSET);
1862     // This values were derived empirically.
1863     const RGBA32 baseDarkColor = 0xFF202020;
1864     const RGBA32 baseLightColor = 0xFFEBEBEB;
1865     enum Operation { Darken, Lighten };
1866
1867     Operation operation = (side == BSTop || side == BSLeft) == (style == INSET) ? Darken : Lighten;
1868
1869     // Here we will darken the border decoration color when needed. This will yield a similar behavior as in FF.
1870     if (operation == Darken) {
1871         if (differenceSquared(color, Color::black) > differenceSquared(baseDarkColor, Color::black))
1872             color = color.dark();
1873     } else {
1874         if (differenceSquared(color, Color::white) > differenceSquared(baseLightColor, Color::white))
1875             color = color.light();
1876     }
1877 }
1878
1879 void RenderObject::setIsDragging(bool isDragging)
1880 {
1881     if (isDragging || hasRareData())
1882         ensureRareData().setIsDragging(isDragging);
1883 }
1884
1885 void RenderObject::setHasReflection(bool hasReflection)
1886 {
1887     if (hasReflection || hasRareData())
1888         ensureRareData().setHasReflection(hasReflection);
1889 }
1890
1891 void RenderObject::setIsRenderFlowThread(bool isFlowThread)
1892 {
1893     if (isFlowThread || hasRareData())
1894         ensureRareData().setIsRenderFlowThread(isFlowThread);
1895 }
1896
1897 void RenderObject::setHasOutlineAutoAncestor(bool hasOutlineAutoAncestor)
1898 {
1899     if (hasOutlineAutoAncestor || hasRareData())
1900         ensureRareData().setHasOutlineAutoAncestor(hasOutlineAutoAncestor);
1901 }
1902
1903 void RenderObject::setIsRegisteredForVisibleInViewportCallback(bool registered)
1904 {
1905     if (registered || hasRareData())
1906         ensureRareData().setIsRegisteredForVisibleInViewportCallback(registered);
1907 }
1908
1909 void RenderObject::setVisibleInViewportState(VisibleInViewportState visible)
1910 {
1911     if (visible != VisibilityUnknown || hasRareData())
1912         ensureRareData().setVisibleInViewportState(visible);
1913 }
1914
1915 RenderObject::RareDataHash& RenderObject::rareDataMap()
1916 {
1917     static NeverDestroyed<RareDataHash> map;
1918     return map;
1919 }
1920
1921 RenderObject::RenderObjectRareData RenderObject::rareData() const
1922 {
1923     if (!hasRareData())
1924         return RenderObjectRareData();
1925
1926     return rareDataMap().get(this);
1927 }
1928
1929 RenderObject::RenderObjectRareData& RenderObject::ensureRareData()
1930 {
1931     setHasRareData(true);
1932     return rareDataMap().add(this, RenderObjectRareData()).iterator->value;
1933 }
1934
1935 void RenderObject::removeRareData()
1936 {
1937     rareDataMap().remove(this);
1938     setHasRareData(false);
1939 }
1940
1941 #if ENABLE(TREE_DEBUGGING)
1942
1943 void printRenderTreeForLiveDocuments()
1944 {
1945     for (const auto* document : Document::allDocuments()) {
1946         if (!document->renderView() || document->pageCacheState() != Document::NotInPageCache)
1947             continue;
1948         if (document->frame() && document->frame()->isMainFrame())
1949             fprintf(stderr, "----------------------main frame--------------------------\n");
1950         fprintf(stderr, "%s", document->url().string().utf8().data());
1951         showRenderTree(document->renderView());
1952     }
1953 }
1954
1955 void printLayerTreeForLiveDocuments()
1956 {
1957     for (const auto* document : Document::allDocuments()) {
1958         if (!document->renderView() || document->pageCacheState() != Document::NotInPageCache)
1959             continue;
1960         if (document->frame() && document->frame()->isMainFrame())
1961             fprintf(stderr, "----------------------main frame--------------------------\n");
1962         fprintf(stderr, "%s", document->url().string().utf8().data());
1963         showLayerTree(document->renderView());
1964     }
1965 }
1966
1967 #endif // ENABLE(TREE_DEBUGGING)
1968
1969 } // namespace WebCore
1970
1971 #if ENABLE(TREE_DEBUGGING)
1972
1973 void showNodeTree(const WebCore::RenderObject* object)
1974 {
1975     if (!object)
1976         return;
1977     object->showNodeTreeForThis();
1978 }
1979
1980 void showLineTree(const WebCore::RenderObject* object)
1981 {
1982     if (!object)
1983         return;
1984     object->showLineTreeForThis();
1985 }
1986
1987 void showRenderTree(const WebCore::RenderObject* object)
1988 {
1989     if (!object)
1990         return;
1991     object->showRenderTreeForThis();
1992 }
1993
1994 #endif