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