Move RenderObject::shouldRespectImageOrientation to RenderElement.
[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, (R)elative/A(B)solute/Fi(X)ed/Stick(Y) positioned, (O)verflow clipping, (A)nonymous, (G)enerated, (F)loating, has(L)ayer, (C)omposited, (D)irty layout, Dirty (S)tyle\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('Y', stderr);
1030         else if (isOutOfFlowPositioned()) {
1031             if (style().position() == AbsolutePosition)
1032                 fputc('B', stderr);
1033             else
1034                 fputc('X', stderr);
1035         }
1036     } else
1037         fputc('-', stderr);
1038
1039     if (hasOverflowClip())
1040         fputc('O', stderr);
1041     else
1042         fputc('-', stderr);
1043
1044     if (isAnonymous())
1045         fputc('A', stderr);
1046     else
1047         fputc('-', stderr);
1048
1049     if (isPseudoElement() || isAnonymous())
1050         fputc('G', stderr);
1051     else
1052         fputc('-', stderr);
1053
1054     if (isFloating())
1055         fputc('F', 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 (needsLayout())
1072         fputc('D', stderr);
1073     else
1074         fputc('-', stderr);
1075
1076     if (node() && node()->needsStyleRecalc())
1077         fputc('S', 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     removeFromRenderFlowThread();
1430
1431     // Update cached boundaries in SVG renderers, if a child is removed.
1432     parent()->setNeedsBoundariesUpdate();
1433 }
1434
1435 void RenderObject::removeFromRenderFlowThread()
1436 {
1437     if (flowThreadState() == NotInsideFlowThread)
1438         return;
1439
1440     // Sometimes we remove the element from the flow, but it's not destroyed at that time.
1441     // It's only until later when we actually destroy it and remove all the children from it.
1442     // Currently, that happens for firstLetter elements and list markers.
1443     // Pass in the flow thread so that we don't have to look it up for all the children.
1444     removeFromRenderFlowThreadIncludingDescendants(true);
1445 }
1446
1447 void RenderObject::removeFromRenderFlowThreadIncludingDescendants(bool shouldUpdateState)
1448 {
1449     // Once we reach another flow thread we don't need to update the flow thread state
1450     // but we have to continue cleanup the flow thread info.
1451     if (isRenderFlowThread())
1452         shouldUpdateState = false;
1453
1454     if (is<RenderElement>(*this)) {
1455         for (auto& child : childrenOfType<RenderObject>(downcast<RenderElement>(*this)))
1456             child.removeFromRenderFlowThreadIncludingDescendants(shouldUpdateState);
1457     }
1458
1459     // We have to ask for our containing flow thread as it may be above the removed sub-tree.
1460     RenderFlowThread* flowThreadContainingBlock = this->flowThreadContainingBlock();
1461     while (flowThreadContainingBlock) {
1462         flowThreadContainingBlock->removeFlowChildInfo(this);
1463         if (flowThreadContainingBlock->flowThreadState() == NotInsideFlowThread)
1464             break;
1465         RenderObject* parent = flowThreadContainingBlock->parent();
1466         if (!parent)
1467             break;
1468         flowThreadContainingBlock = parent->flowThreadContainingBlock();
1469     }
1470     if (is<RenderBlock>(*this))
1471         downcast<RenderBlock>(*this).setCachedFlowThreadContainingBlockNeedsUpdate();
1472
1473     if (shouldUpdateState)
1474         setFlowThreadState(NotInsideFlowThread);
1475 }
1476
1477 void RenderObject::invalidateFlowThreadContainingBlockIncludingDescendants(RenderFlowThread* flowThread)
1478 {
1479     if (flowThreadState() == NotInsideFlowThread)
1480         return;
1481
1482     if (is<RenderBlock>(*this)) {
1483         RenderBlock& block = downcast<RenderBlock>(*this);
1484
1485         if (block.cachedFlowThreadContainingBlockNeedsUpdate())
1486             return;
1487
1488         flowThread = block.cachedFlowThreadContainingBlock();
1489         block.setCachedFlowThreadContainingBlockNeedsUpdate();
1490     }
1491
1492     if (flowThread)
1493         flowThread->removeFlowChildInfo(this);
1494
1495     if (!is<RenderElement>(*this))
1496         return;
1497
1498     for (auto& child : childrenOfType<RenderObject>(downcast<RenderElement>(*this)))
1499         child.invalidateFlowThreadContainingBlockIncludingDescendants(flowThread);
1500 }
1501
1502 void RenderObject::destroyAndCleanupAnonymousWrappers()
1503 {
1504     // If the tree is destroyed, there is no need for a clean-up phase.
1505     if (documentBeingDestroyed()) {
1506         destroy();
1507         return;
1508     }
1509
1510     auto* destroyRoot = this;
1511     auto* destroyRootParent = destroyRoot->parent();
1512     while (destroyRootParent && destroyRootParent->isAnonymous()) {
1513         if (!destroyRootParent->isTableCell() && !destroyRootParent->isTableRow()
1514             && !destroyRootParent->isTableCaption() && !destroyRootParent->isTableSection() && !destroyRootParent->isTable())
1515             break;
1516         // single child?
1517         if (!(destroyRootParent->firstChild() == destroyRoot && destroyRootParent->lastChild() == destroyRoot))
1518             break;
1519         destroyRoot = destroyRootParent;
1520         destroyRootParent = destroyRootParent->parent();
1521     }
1522
1523     if (is<RenderTableRow>(*destroyRoot)) {
1524         downcast<RenderTableRow>(*destroyRoot).destroyAndCollapseAnonymousSiblingRows();
1525         return;
1526     }
1527
1528     destroyRoot->destroy();
1529     // WARNING: |this| is deleted here.
1530 }
1531
1532 void RenderObject::destroy()
1533 {
1534     m_bitfields.setBeingDestroyed(true);
1535
1536 #if PLATFORM(IOS)
1537     if (hasLayer())
1538         downcast<RenderBoxModelObject>(*this).layer()->willBeDestroyed();
1539 #endif
1540
1541     willBeDestroyed();
1542     if (is<RenderWidget>(*this)) {
1543         downcast<RenderWidget>(*this).deref();
1544         return;
1545     }
1546     delete this;
1547 }
1548
1549 VisiblePosition RenderObject::positionForPoint(const LayoutPoint&, const RenderRegion*)
1550 {
1551     return createVisiblePosition(caretMinOffset(), DOWNSTREAM);
1552 }
1553
1554 void RenderObject::updateDragState(bool dragOn)
1555 {
1556     bool valueChanged = (dragOn != isDragging());
1557     setIsDragging(dragOn);
1558     if (valueChanged && node() && (style().affectedByDrag() || (is<Element>(*node()) && downcast<Element>(*node()).childrenAffectedByDrag())))
1559         node()->setNeedsStyleRecalc();
1560
1561     if (!is<RenderElement>(*this))
1562         return;
1563
1564     for (auto& child : childrenOfType<RenderObject>(downcast<RenderElement>(*this)))
1565         child.updateDragState(dragOn);
1566 }
1567
1568 bool RenderObject::isComposited() const
1569 {
1570     return hasLayer() && downcast<RenderLayerModelObject>(*this).layer()->isComposited();
1571 }
1572
1573 bool RenderObject::isAnonymousInlineBlock() const
1574 {
1575     return isAnonymous() && style().display() == INLINE_BLOCK && style().styleType() == NOPSEUDO && isRenderBlockFlow() && !isRubyRun() && !isRubyBase() && !isRuby(parent());
1576 }
1577
1578 bool RenderObject::hitTest(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestFilter hitTestFilter)
1579 {
1580     bool inside = false;
1581     if (hitTestFilter != HitTestSelf) {
1582         // First test the foreground layer (lines and inlines).
1583         inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestForeground);
1584
1585         // Test floats next.
1586         if (!inside)
1587             inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestFloat);
1588
1589         // Finally test to see if the mouse is in the background (within a child block's background).
1590         if (!inside)
1591             inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestChildBlockBackgrounds);
1592     }
1593
1594     // See if the mouse is inside us but not any of our descendants
1595     if (hitTestFilter != HitTestDescendants && !inside)
1596         inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestBlockBackground);
1597
1598     return inside;
1599 }
1600
1601 void RenderObject::updateHitTestResult(HitTestResult& result, const LayoutPoint& point)
1602 {
1603     if (result.innerNode())
1604         return;
1605
1606     Node* node = this->node();
1607
1608     // If we hit the anonymous renderers inside generated content we should
1609     // actually hit the generated content so walk up to the PseudoElement.
1610     if (!node && parent() && parent()->isBeforeOrAfterContent()) {
1611         for (auto* renderer = parent(); renderer && !node; renderer = renderer->parent())
1612             node = renderer->element();
1613     }
1614
1615     if (node) {
1616         result.setInnerNode(node);
1617         if (!result.innerNonSharedNode())
1618             result.setInnerNonSharedNode(node);
1619         result.setLocalPoint(point);
1620     }
1621 }
1622
1623 bool RenderObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& /*locationInContainer*/, const LayoutPoint& /*accumulatedOffset*/, HitTestAction)
1624 {
1625     return false;
1626 }
1627
1628 int RenderObject::innerLineHeight() const
1629 {
1630     return style().computedLineHeight();
1631 }
1632
1633 #if ENABLE(DASHBOARD_SUPPORT)
1634 void RenderObject::addAnnotatedRegions(Vector<AnnotatedRegionValue>& regions)
1635 {
1636     // Convert the style regions to absolute coordinates.
1637     if (style().visibility() != VISIBLE || !is<RenderBox>(*this))
1638         return;
1639     
1640     auto& box = downcast<RenderBox>(*this);
1641     FloatPoint absPos = localToAbsolute();
1642
1643     const Vector<StyleDashboardRegion>& styleRegions = style().dashboardRegions();
1644     for (const auto& styleRegion : styleRegions) {
1645         LayoutUnit w = box.width();
1646         LayoutUnit h = box.height();
1647
1648         AnnotatedRegionValue region;
1649         region.label = styleRegion.label;
1650         region.bounds = LayoutRect(styleRegion.offset.left().value(),
1651                                    styleRegion.offset.top().value(),
1652                                    w - styleRegion.offset.left().value() - styleRegion.offset.right().value(),
1653                                    h - styleRegion.offset.top().value() - styleRegion.offset.bottom().value());
1654         region.type = styleRegion.type;
1655
1656         region.clip = computeAbsoluteRepaintRect(region.bounds);
1657         if (region.clip.height() < 0) {
1658             region.clip.setHeight(0);
1659             region.clip.setWidth(0);
1660         }
1661
1662         region.bounds.setX(absPos.x() + styleRegion.offset.left().value());
1663         region.bounds.setY(absPos.y() + styleRegion.offset.top().value());
1664
1665         regions.append(region);
1666     }
1667 }
1668
1669 void RenderObject::collectAnnotatedRegions(Vector<AnnotatedRegionValue>& regions)
1670 {
1671     // RenderTexts don't have their own style, they just use their parent's style,
1672     // so we don't want to include them.
1673     if (is<RenderText>(*this))
1674         return;
1675
1676     addAnnotatedRegions(regions);
1677     for (RenderObject* current = downcast<RenderElement>(*this).firstChild(); current; current = current->nextSibling())
1678         current->collectAnnotatedRegions(regions);
1679 }
1680 #endif
1681
1682 int RenderObject::caretMinOffset() const
1683 {
1684     return 0;
1685 }
1686
1687 int RenderObject::caretMaxOffset() const
1688 {
1689     if (isReplaced())
1690         return node() ? std::max(1U, node()->countChildNodes()) : 1;
1691     if (isHR())
1692         return 1;
1693     return 0;
1694 }
1695
1696 int RenderObject::previousOffset(int current) const
1697 {
1698     return current - 1;
1699 }
1700
1701 int RenderObject::previousOffsetForBackwardDeletion(int current) const
1702 {
1703     return current - 1;
1704 }
1705
1706 int RenderObject::nextOffset(int current) const
1707 {
1708     return current + 1;
1709 }
1710
1711 void RenderObject::adjustRectForOutlineAndShadow(LayoutRect& rect) const
1712 {
1713     LayoutUnit outlineSize = outlineStyleForRepaint().outlineSize();
1714     if (const ShadowData* boxShadow = style().boxShadow()) {
1715         boxShadow->adjustRectForShadow(rect, outlineSize);
1716         return;
1717     }
1718     rect.inflate(outlineSize);
1719 }
1720
1721 void RenderObject::imageChanged(CachedImage* image, const IntRect* rect)
1722 {
1723     imageChanged(static_cast<WrappedImagePtr>(image), rect);
1724 }
1725
1726 RenderBoxModelObject* RenderObject::offsetParent() const
1727 {
1728     // If any of the following holds true return null and stop this algorithm:
1729     // A is the root element.
1730     // A is the HTML body element.
1731     // The computed value of the position property for element A is fixed.
1732     if (isDocumentElementRenderer() || isBody() || (isOutOfFlowPositioned() && style().position() == FixedPosition))
1733         return nullptr;
1734
1735     // If A is an area HTML element which has a map HTML element somewhere in the ancestor
1736     // chain return the nearest ancestor map HTML element and stop this algorithm.
1737     // FIXME: Implement!
1738     
1739     // Return the nearest ancestor element of A for which at least one of the following is
1740     // true and stop this algorithm if such an ancestor is found:
1741     //     * The computed value of the position property is not static.
1742     //     * It is the HTML body element.
1743     //     * The computed value of the position property of A is static and the ancestor
1744     //       is one of the following HTML elements: td, th, or table.
1745     //     * Our own extension: if there is a difference in the effective zoom
1746
1747     bool skipTables = isPositioned();
1748     float currZoom = style().effectiveZoom();
1749     auto current = parent();
1750     while (current && (!current->element() || (!current->isPositioned() && !current->isBody())) && !is<RenderNamedFlowThread>(*current)) {
1751         Element* element = current->element();
1752         if (!skipTables && element && (is<HTMLTableElement>(*element) || is<HTMLTableCellElement>(*element)))
1753             break;
1754  
1755         float newZoom = current->style().effectiveZoom();
1756         if (currZoom != newZoom)
1757             break;
1758         currZoom = newZoom;
1759         current = current->parent();
1760     }
1761     
1762     // CSS regions specification says that region flows should return the body element as their offsetParent.
1763     if (is<RenderNamedFlowThread>(current)) {
1764         auto* body = document().bodyOrFrameset();
1765         current = body ? body->renderer() : nullptr;
1766     }
1767     
1768     return is<RenderBoxModelObject>(current) ? downcast<RenderBoxModelObject>(current) : nullptr;
1769 }
1770
1771 VisiblePosition RenderObject::createVisiblePosition(int offset, EAffinity affinity) const
1772 {
1773     // If this is a non-anonymous renderer in an editable area, then it's simple.
1774     if (Node* node = nonPseudoNode()) {
1775         if (!node->hasEditableStyle()) {
1776             // If it can be found, we prefer a visually equivalent position that is editable. 
1777             Position position = createLegacyEditingPosition(node, offset);
1778             Position candidate = position.downstream(CanCrossEditingBoundary);
1779             if (candidate.deprecatedNode()->hasEditableStyle())
1780                 return VisiblePosition(candidate, affinity);
1781             candidate = position.upstream(CanCrossEditingBoundary);
1782             if (candidate.deprecatedNode()->hasEditableStyle())
1783                 return VisiblePosition(candidate, affinity);
1784         }
1785         // FIXME: Eliminate legacy editing positions
1786         return VisiblePosition(createLegacyEditingPosition(node, offset), affinity);
1787     }
1788
1789     // We don't want to cross the boundary between editable and non-editable
1790     // regions of the document, but that is either impossible or at least
1791     // extremely unlikely in any normal case because we stop as soon as we
1792     // find a single non-anonymous renderer.
1793
1794     // Find a nearby non-anonymous renderer.
1795     const RenderObject* child = this;
1796     while (const auto parent = child->parent()) {
1797         // Find non-anonymous content after.
1798         const RenderObject* renderer = child;
1799         while ((renderer = renderer->nextInPreOrder(parent))) {
1800             if (Node* node = renderer->nonPseudoNode())
1801                 return VisiblePosition(firstPositionInOrBeforeNode(node), DOWNSTREAM);
1802         }
1803
1804         // Find non-anonymous content before.
1805         renderer = child;
1806         while ((renderer = renderer->previousInPreOrder())) {
1807             if (renderer == parent)
1808                 break;
1809             if (Node* node = renderer->nonPseudoNode())
1810                 return VisiblePosition(lastPositionInOrAfterNode(node), DOWNSTREAM);
1811         }
1812
1813         // Use the parent itself unless it too is anonymous.
1814         if (Element* element = parent->nonPseudoElement())
1815             return VisiblePosition(firstPositionInOrBeforeNode(element), DOWNSTREAM);
1816
1817         // Repeat at the next level up.
1818         child = parent;
1819     }
1820
1821     // Everything was anonymous. Give up.
1822     return VisiblePosition();
1823 }
1824
1825 VisiblePosition RenderObject::createVisiblePosition(const Position& position) const
1826 {
1827     if (position.isNotNull())
1828         return VisiblePosition(position);
1829
1830     ASSERT(!node());
1831     return createVisiblePosition(0, DOWNSTREAM);
1832 }
1833
1834 CursorDirective RenderObject::getCursor(const LayoutPoint&, Cursor&) const
1835 {
1836     return SetCursorBasedOnStyle;
1837 }
1838
1839 bool RenderObject::canUpdateSelectionOnRootLineBoxes()
1840 {
1841     if (needsLayout())
1842         return false;
1843
1844     RenderBlock* containingBlock = this->containingBlock();
1845     return containingBlock ? !containingBlock->needsLayout() : true;
1846 }
1847
1848 // We only create "generated" child renderers like one for first-letter if:
1849 // - the firstLetterBlock can have children in the DOM and
1850 // - the block doesn't have any special assumption on its text children.
1851 // This correctly prevents form controls from having such renderers.
1852 bool RenderObject::canHaveGeneratedChildren() const
1853 {
1854     return canHaveChildren();
1855 }
1856
1857 Node* RenderObject::generatingPseudoHostElement() const
1858 {
1859     return downcast<PseudoElement>(*node()).hostElement();
1860 }
1861
1862 void RenderObject::setNeedsBoundariesUpdate()
1863 {
1864     if (auto renderer = parent())
1865         renderer->setNeedsBoundariesUpdate();
1866 }
1867
1868 FloatRect RenderObject::objectBoundingBox() const
1869 {
1870     ASSERT_NOT_REACHED();
1871     return FloatRect();
1872 }
1873
1874 FloatRect RenderObject::strokeBoundingBox() const
1875 {
1876     ASSERT_NOT_REACHED();
1877     return FloatRect();
1878 }
1879
1880 // Returns the smallest rectangle enclosing all of the painted content
1881 // respecting clipping, masking, filters, opacity, stroke-width and markers
1882 FloatRect RenderObject::repaintRectInLocalCoordinates() const
1883 {
1884     ASSERT_NOT_REACHED();
1885     return FloatRect();
1886 }
1887
1888 AffineTransform RenderObject::localTransform() const
1889 {
1890     static const AffineTransform identity;
1891     return identity;
1892 }
1893
1894 const AffineTransform& RenderObject::localToParentTransform() const
1895 {
1896     static const AffineTransform identity;
1897     return identity;
1898 }
1899
1900 bool RenderObject::nodeAtFloatPoint(const HitTestRequest&, HitTestResult&, const FloatPoint&, HitTestAction)
1901 {
1902     ASSERT_NOT_REACHED();
1903     return false;
1904 }
1905
1906 RenderNamedFlowFragment* RenderObject::currentRenderNamedFlowFragment() const
1907 {
1908     RenderFlowThread* flowThread = flowThreadContainingBlock();
1909     if (!is<RenderNamedFlowThread>(flowThread))
1910         return nullptr;
1911
1912     // FIXME: Once regions are fully integrated with the compositing system we should uncomment this assert.
1913     // This assert needs to be disabled because it's possible to ask for the ancestor clipping rectangle of
1914     // a layer without knowing the containing region in advance.
1915     // ASSERT(flowThread->currentRegion() && flowThread->currentRegion()->isRenderNamedFlowFragment());
1916
1917     return downcast<RenderNamedFlowFragment>(flowThread->currentRegion());
1918 }
1919
1920 RenderFlowThread* RenderObject::locateFlowThreadContainingBlock() const
1921 {
1922     RenderBlock* containingBlock = this->containingBlock();
1923     return containingBlock ? containingBlock->flowThreadContainingBlock() : nullptr;
1924 }
1925
1926 void RenderObject::calculateBorderStyleColor(const EBorderStyle& style, const BoxSide& side, Color& color)
1927 {
1928     ASSERT(style == INSET || style == OUTSET);
1929     // This values were derived empirically.
1930     const RGBA32 baseDarkColor = 0xFF202020;
1931     const RGBA32 baseLightColor = 0xFFEBEBEB;
1932     enum Operation { Darken, Lighten };
1933
1934     Operation operation = (side == BSTop || side == BSLeft) == (style == INSET) ? Darken : Lighten;
1935
1936     // Here we will darken the border decoration color when needed. This will yield a similar behavior as in FF.
1937     if (operation == Darken) {
1938         if (differenceSquared(color, Color::black) > differenceSquared(baseDarkColor, Color::black))
1939             color = color.dark();
1940     } else {
1941         if (differenceSquared(color, Color::white) > differenceSquared(baseLightColor, Color::white))
1942             color = color.light();
1943     }
1944 }
1945
1946 void RenderObject::setIsDragging(bool isDragging)
1947 {
1948     if (isDragging || hasRareData())
1949         ensureRareData().setIsDragging(isDragging);
1950 }
1951
1952 void RenderObject::setHasReflection(bool hasReflection)
1953 {
1954     if (hasReflection || hasRareData())
1955         ensureRareData().setHasReflection(hasReflection);
1956 }
1957
1958 void RenderObject::setIsRenderFlowThread(bool isFlowThread)
1959 {
1960     if (isFlowThread || hasRareData())
1961         ensureRareData().setIsRenderFlowThread(isFlowThread);
1962 }
1963
1964 void RenderObject::setHasOutlineAutoAncestor(bool hasOutlineAutoAncestor)
1965 {
1966     if (hasOutlineAutoAncestor || hasRareData())
1967         ensureRareData().setHasOutlineAutoAncestor(hasOutlineAutoAncestor);
1968 }
1969
1970 void RenderObject::setIsRegisteredForVisibleInViewportCallback(bool registered)
1971 {
1972     if (registered || hasRareData())
1973         ensureRareData().setIsRegisteredForVisibleInViewportCallback(registered);
1974 }
1975
1976 void RenderObject::setVisibleInViewportState(VisibleInViewportState visible)
1977 {
1978     if (visible != VisibilityUnknown || hasRareData())
1979         ensureRareData().setVisibleInViewportState(visible);
1980 }
1981
1982 RenderObject::RareDataHash& RenderObject::rareDataMap()
1983 {
1984     static NeverDestroyed<RareDataHash> map;
1985     return map;
1986 }
1987
1988 RenderObject::RenderObjectRareData RenderObject::rareData() const
1989 {
1990     if (!hasRareData())
1991         return RenderObjectRareData();
1992
1993     return rareDataMap().get(this);
1994 }
1995
1996 RenderObject::RenderObjectRareData& RenderObject::ensureRareData()
1997 {
1998     setHasRareData(true);
1999     return rareDataMap().add(this, RenderObjectRareData()).iterator->value;
2000 }
2001
2002 void RenderObject::removeRareData()
2003 {
2004     rareDataMap().remove(this);
2005     setHasRareData(false);
2006 }
2007
2008 #if ENABLE(TREE_DEBUGGING)
2009
2010 void printRenderTreeForLiveDocuments()
2011 {
2012     for (const auto* document : Document::allDocuments()) {
2013         if (!document->renderView() || document->pageCacheState() != Document::NotInPageCache)
2014             continue;
2015         if (document->frame() && document->frame()->isMainFrame())
2016             fprintf(stderr, "----------------------main frame--------------------------\n");
2017         fprintf(stderr, "%s", document->url().string().utf8().data());
2018         showRenderTree(document->renderView());
2019     }
2020 }
2021
2022 void printLayerTreeForLiveDocuments()
2023 {
2024     for (const auto* document : Document::allDocuments()) {
2025         if (!document->renderView() || document->pageCacheState() != Document::NotInPageCache)
2026             continue;
2027         if (document->frame() && document->frame()->isMainFrame())
2028             fprintf(stderr, "----------------------main frame--------------------------\n");
2029         fprintf(stderr, "%s", document->url().string().utf8().data());
2030         showLayerTree(document->renderView());
2031     }
2032 }
2033
2034 #endif // ENABLE(TREE_DEBUGGING)
2035
2036 } // namespace WebCore
2037
2038 #if ENABLE(TREE_DEBUGGING)
2039
2040 void showNodeTree(const WebCore::RenderObject* object)
2041 {
2042     if (!object)
2043         return;
2044     object->showNodeTreeForThis();
2045 }
2046
2047 void showLineTree(const WebCore::RenderObject* object)
2048 {
2049     if (!object)
2050         return;
2051     object->showLineTreeForThis();
2052 }
2053
2054 void showRenderTree(const WebCore::RenderObject* object)
2055 {
2056     if (!object)
2057         return;
2058     object->showRenderTreeForThis();
2059 }
2060
2061 #endif