Use "= default" to denote default constructor or destructor
[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().scheduleRelayout();
486         return;
487     }
488
489     if (renderer.isRooted())
490         renderer.view().frameView().scheduleRelayoutOfSubtree(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* inlineWrapperForDisplayContents = is<RenderText>(renderer) ? downcast<RenderText>(renderer).inlineWrapperForDisplayContents() : nullptr;
1461
1462     auto* destroyRoot = inlineWrapperForDisplayContents ? inlineWrapperForDisplayContents : &renderer;
1463     auto* destroyRootParent = destroyRoot->parent();
1464     while (destroyRootParent && destroyRootParent->isAnonymous()) {
1465         if (!destroyRootParent->isTableCell() && !destroyRootParent->isTableRow()
1466             && !destroyRootParent->isTableCaption() && !destroyRootParent->isTableSection() && !destroyRootParent->isTable())
1467             break;
1468         // single child?
1469         if (!(destroyRootParent->firstChild() == destroyRoot && destroyRootParent->lastChild() == destroyRoot))
1470             break;
1471         destroyRoot = destroyRootParent;
1472         destroyRootParent = destroyRootParent->parent();
1473     }
1474     return *destroyRoot;
1475 }
1476
1477 void RenderObject::removeFromParentAndDestroyCleaningUpAnonymousWrappers()
1478 {
1479     // If the tree is destroyed, there is no need for a clean-up phase.
1480     if (renderTreeBeingDestroyed()) {
1481         removeFromParentAndDestroy();
1482         return;
1483     }
1484
1485     auto& destroyRoot = findDestroyRootIncludingAnonymous(*this);
1486
1487     if (is<RenderTableRow>(destroyRoot))
1488         downcast<RenderTableRow>(destroyRoot).collapseAndDestroyAnonymousSiblingRows();
1489
1490     destroyRoot.removeFromParentAndDestroy();
1491     // WARNING: |this| is deleted here.
1492 }
1493
1494 void RenderObject::destroy()
1495 {
1496     RELEASE_ASSERT(!m_parent);
1497     RELEASE_ASSERT(!m_next);
1498     RELEASE_ASSERT(!m_previous);
1499     RELEASE_ASSERT(!m_bitfields.beingDestroyed());
1500
1501     m_bitfields.setBeingDestroyed(true);
1502
1503 #if PLATFORM(IOS)
1504     if (hasLayer())
1505         downcast<RenderBoxModelObject>(*this).layer()->willBeDestroyed();
1506 #endif
1507
1508     willBeDestroyed();
1509     if (is<RenderWidget>(*this)) {
1510         downcast<RenderWidget>(*this).deref();
1511         return;
1512     }
1513     delete this;
1514 }
1515
1516 Position RenderObject::positionForPoint(const LayoutPoint& point)
1517 {
1518     // FIXME: This should just create a Position object instead (webkit.org/b/168566). 
1519     return positionForPoint(point, nullptr).deepEquivalent();
1520 }
1521
1522 VisiblePosition RenderObject::positionForPoint(const LayoutPoint&, const RenderFragmentContainer*)
1523 {
1524     return createVisiblePosition(caretMinOffset(), DOWNSTREAM);
1525 }
1526
1527 void RenderObject::updateDragState(bool dragOn)
1528 {
1529     bool valueChanged = (dragOn != isDragging());
1530     setIsDragging(dragOn);
1531
1532     if (!is<RenderElement>(*this))
1533         return;
1534     auto& renderElement = downcast<RenderElement>(*this);
1535
1536     if (valueChanged && renderElement.element() && (style().affectedByDrag() || renderElement.element()->childrenAffectedByDrag()))
1537         renderElement.element()->invalidateStyleForSubtree();
1538
1539     for (auto& child : childrenOfType<RenderObject>(renderElement))
1540         child.updateDragState(dragOn);
1541 }
1542
1543 bool RenderObject::isComposited() const
1544 {
1545     return hasLayer() && downcast<RenderLayerModelObject>(*this).layer()->isComposited();
1546 }
1547
1548 bool RenderObject::hitTest(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestFilter hitTestFilter)
1549 {
1550     bool inside = false;
1551     if (hitTestFilter != HitTestSelf) {
1552         // First test the foreground layer (lines and inlines).
1553         inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestForeground);
1554
1555         // Test floats next.
1556         if (!inside)
1557             inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestFloat);
1558
1559         // Finally test to see if the mouse is in the background (within a child block's background).
1560         if (!inside)
1561             inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestChildBlockBackgrounds);
1562     }
1563
1564     // See if the mouse is inside us but not any of our descendants
1565     if (hitTestFilter != HitTestDescendants && !inside)
1566         inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestBlockBackground);
1567
1568     return inside;
1569 }
1570
1571 void RenderObject::updateHitTestResult(HitTestResult& result, const LayoutPoint& point)
1572 {
1573     if (result.innerNode())
1574         return;
1575
1576     Node* node = this->node();
1577
1578     // If we hit the anonymous renderers inside generated content we should
1579     // actually hit the generated content so walk up to the PseudoElement.
1580     if (!node && parent() && parent()->isBeforeOrAfterContent()) {
1581         for (auto* renderer = parent(); renderer && !node; renderer = renderer->parent())
1582             node = renderer->element();
1583     }
1584
1585     if (node) {
1586         result.setInnerNode(node);
1587         if (!result.innerNonSharedNode())
1588             result.setInnerNonSharedNode(node);
1589         result.setLocalPoint(point);
1590     }
1591 }
1592
1593 bool RenderObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& /*locationInContainer*/, const LayoutPoint& /*accumulatedOffset*/, HitTestAction)
1594 {
1595     return false;
1596 }
1597
1598 int RenderObject::innerLineHeight() const
1599 {
1600     return style().computedLineHeight();
1601 }
1602
1603 #if ENABLE(DASHBOARD_SUPPORT)
1604 void RenderObject::addAnnotatedRegions(Vector<AnnotatedRegionValue>& regions)
1605 {
1606     // Convert the style regions to absolute coordinates.
1607     if (style().visibility() != VISIBLE || !is<RenderBox>(*this))
1608         return;
1609     
1610     auto& box = downcast<RenderBox>(*this);
1611     FloatPoint absPos = localToAbsolute();
1612
1613     const Vector<StyleDashboardRegion>& styleRegions = style().dashboardRegions();
1614     for (const auto& styleRegion : styleRegions) {
1615         LayoutUnit w = box.width();
1616         LayoutUnit h = box.height();
1617
1618         AnnotatedRegionValue region;
1619         region.label = styleRegion.label;
1620         region.bounds = LayoutRect(styleRegion.offset.left().value(),
1621                                    styleRegion.offset.top().value(),
1622                                    w - styleRegion.offset.left().value() - styleRegion.offset.right().value(),
1623                                    h - styleRegion.offset.top().value() - styleRegion.offset.bottom().value());
1624         region.type = styleRegion.type;
1625
1626         region.clip = computeAbsoluteRepaintRect(region.bounds);
1627         if (region.clip.height() < 0) {
1628             region.clip.setHeight(0);
1629             region.clip.setWidth(0);
1630         }
1631
1632         region.bounds.setX(absPos.x() + styleRegion.offset.left().value());
1633         region.bounds.setY(absPos.y() + styleRegion.offset.top().value());
1634
1635         regions.append(region);
1636     }
1637 }
1638
1639 void RenderObject::collectAnnotatedRegions(Vector<AnnotatedRegionValue>& regions)
1640 {
1641     // RenderTexts don't have their own style, they just use their parent's style,
1642     // so we don't want to include them.
1643     if (is<RenderText>(*this))
1644         return;
1645
1646     addAnnotatedRegions(regions);
1647     for (RenderObject* current = downcast<RenderElement>(*this).firstChild(); current; current = current->nextSibling())
1648         current->collectAnnotatedRegions(regions);
1649 }
1650 #endif
1651
1652 int RenderObject::caretMinOffset() const
1653 {
1654     return 0;
1655 }
1656
1657 int RenderObject::caretMaxOffset() const
1658 {
1659     if (isReplaced())
1660         return node() ? std::max(1U, node()->countChildNodes()) : 1;
1661     if (isHR())
1662         return 1;
1663     return 0;
1664 }
1665
1666 int RenderObject::previousOffset(int current) const
1667 {
1668     return current - 1;
1669 }
1670
1671 int RenderObject::previousOffsetForBackwardDeletion(int current) const
1672 {
1673     return current - 1;
1674 }
1675
1676 int RenderObject::nextOffset(int current) const
1677 {
1678     return current + 1;
1679 }
1680
1681 void RenderObject::adjustRectForOutlineAndShadow(LayoutRect& rect) const
1682 {
1683     LayoutUnit outlineSize = outlineStyleForRepaint().outlineSize();
1684     if (const ShadowData* boxShadow = style().boxShadow()) {
1685         boxShadow->adjustRectForShadow(rect, outlineSize);
1686         return;
1687     }
1688     rect.inflate(outlineSize);
1689 }
1690
1691 void RenderObject::imageChanged(CachedImage* image, const IntRect* rect)
1692 {
1693     imageChanged(static_cast<WrappedImagePtr>(image), rect);
1694 }
1695
1696 RenderBoxModelObject* RenderObject::offsetParent() const
1697 {
1698     // If any of the following holds true return null and stop this algorithm:
1699     // A is the root element.
1700     // A is the HTML body element.
1701     // The computed value of the position property for element A is fixed.
1702     if (isDocumentElementRenderer() || isBody() || (isOutOfFlowPositioned() && style().position() == FixedPosition))
1703         return nullptr;
1704
1705     // If A is an area HTML element which has a map HTML element somewhere in the ancestor
1706     // chain return the nearest ancestor map HTML element and stop this algorithm.
1707     // FIXME: Implement!
1708     
1709     // Return the nearest ancestor element of A for which at least one of the following is
1710     // true and stop this algorithm if such an ancestor is found:
1711     //     * The computed value of the position property is not static.
1712     //     * It is the HTML body element.
1713     //     * The computed value of the position property of A is static and the ancestor
1714     //       is one of the following HTML elements: td, th, or table.
1715     //     * Our own extension: if there is a difference in the effective zoom
1716
1717     bool skipTables = isPositioned();
1718     float currZoom = style().effectiveZoom();
1719     auto current = parent();
1720     while (current && (!current->element() || (!current->isPositioned() && !current->isBody()))) {
1721         Element* element = current->element();
1722         if (!skipTables && element && (is<HTMLTableElement>(*element) || is<HTMLTableCellElement>(*element)))
1723             break;
1724  
1725         float newZoom = current->style().effectiveZoom();
1726         if (currZoom != newZoom)
1727             break;
1728         currZoom = newZoom;
1729         current = current->parent();
1730     }
1731
1732     return is<RenderBoxModelObject>(current) ? downcast<RenderBoxModelObject>(current) : nullptr;
1733 }
1734
1735 VisiblePosition RenderObject::createVisiblePosition(int offset, EAffinity affinity) const
1736 {
1737     // If this is a non-anonymous renderer in an editable area, then it's simple.
1738     if (Node* node = nonPseudoNode()) {
1739         if (!node->hasEditableStyle()) {
1740             // If it can be found, we prefer a visually equivalent position that is editable. 
1741             Position position = createLegacyEditingPosition(node, offset);
1742             Position candidate = position.downstream(CanCrossEditingBoundary);
1743             if (candidate.deprecatedNode()->hasEditableStyle())
1744                 return VisiblePosition(candidate, affinity);
1745             candidate = position.upstream(CanCrossEditingBoundary);
1746             if (candidate.deprecatedNode()->hasEditableStyle())
1747                 return VisiblePosition(candidate, affinity);
1748         }
1749         // FIXME: Eliminate legacy editing positions
1750         return VisiblePosition(createLegacyEditingPosition(node, offset), affinity);
1751     }
1752
1753     // We don't want to cross the boundary between editable and non-editable
1754     // regions of the document, but that is either impossible or at least
1755     // extremely unlikely in any normal case because we stop as soon as we
1756     // find a single non-anonymous renderer.
1757
1758     // Find a nearby non-anonymous renderer.
1759     const RenderObject* child = this;
1760     while (const auto parent = child->parent()) {
1761         // Find non-anonymous content after.
1762         const RenderObject* renderer = child;
1763         while ((renderer = renderer->nextInPreOrder(parent))) {
1764             if (Node* node = renderer->nonPseudoNode())
1765                 return VisiblePosition(firstPositionInOrBeforeNode(node), DOWNSTREAM);
1766         }
1767
1768         // Find non-anonymous content before.
1769         renderer = child;
1770         while ((renderer = renderer->previousInPreOrder())) {
1771             if (renderer == parent)
1772                 break;
1773             if (Node* node = renderer->nonPseudoNode())
1774                 return VisiblePosition(lastPositionInOrAfterNode(node), DOWNSTREAM);
1775         }
1776
1777         // Use the parent itself unless it too is anonymous.
1778         if (Element* element = parent->nonPseudoElement())
1779             return VisiblePosition(firstPositionInOrBeforeNode(element), DOWNSTREAM);
1780
1781         // Repeat at the next level up.
1782         child = parent;
1783     }
1784
1785     // Everything was anonymous. Give up.
1786     return VisiblePosition();
1787 }
1788
1789 VisiblePosition RenderObject::createVisiblePosition(const Position& position) const
1790 {
1791     if (position.isNotNull())
1792         return VisiblePosition(position);
1793
1794     ASSERT(!node());
1795     return createVisiblePosition(0, DOWNSTREAM);
1796 }
1797
1798 CursorDirective RenderObject::getCursor(const LayoutPoint&, Cursor&) const
1799 {
1800     return SetCursorBasedOnStyle;
1801 }
1802
1803 bool RenderObject::canUpdateSelectionOnRootLineBoxes()
1804 {
1805     if (needsLayout())
1806         return false;
1807
1808     RenderBlock* containingBlock = this->containingBlock();
1809     return containingBlock ? !containingBlock->needsLayout() : true;
1810 }
1811
1812 // We only create "generated" child renderers like one for first-letter if:
1813 // - the firstLetterBlock can have children in the DOM and
1814 // - the block doesn't have any special assumption on its text children.
1815 // This correctly prevents form controls from having such renderers.
1816 bool RenderObject::canHaveGeneratedChildren() const
1817 {
1818     return canHaveChildren();
1819 }
1820
1821 Node* RenderObject::generatingPseudoHostElement() const
1822 {
1823     return downcast<PseudoElement>(*node()).hostElement();
1824 }
1825
1826 void RenderObject::setNeedsBoundariesUpdate()
1827 {
1828     if (auto renderer = parent())
1829         renderer->setNeedsBoundariesUpdate();
1830 }
1831
1832 FloatRect RenderObject::objectBoundingBox() const
1833 {
1834     ASSERT_NOT_REACHED();
1835     return FloatRect();
1836 }
1837
1838 FloatRect RenderObject::strokeBoundingBox() const
1839 {
1840     ASSERT_NOT_REACHED();
1841     return FloatRect();
1842 }
1843
1844 // Returns the smallest rectangle enclosing all of the painted content
1845 // respecting clipping, masking, filters, opacity, stroke-width and markers
1846 FloatRect RenderObject::repaintRectInLocalCoordinates() const
1847 {
1848     ASSERT_NOT_REACHED();
1849     return FloatRect();
1850 }
1851
1852 AffineTransform RenderObject::localTransform() const
1853 {
1854     static const AffineTransform identity;
1855     return identity;
1856 }
1857
1858 const AffineTransform& RenderObject::localToParentTransform() const
1859 {
1860     static const AffineTransform identity;
1861     return identity;
1862 }
1863
1864 bool RenderObject::nodeAtFloatPoint(const HitTestRequest&, HitTestResult&, const FloatPoint&, HitTestAction)
1865 {
1866     ASSERT_NOT_REACHED();
1867     return false;
1868 }
1869
1870 RenderFragmentedFlow* RenderObject::locateEnclosingFragmentedFlow() const
1871 {
1872     RenderBlock* containingBlock = this->containingBlock();
1873     return containingBlock ? containingBlock->enclosingFragmentedFlow() : nullptr;
1874 }
1875
1876 void RenderObject::calculateBorderStyleColor(const EBorderStyle& style, const BoxSide& side, Color& color)
1877 {
1878     ASSERT(style == INSET || style == OUTSET);
1879     // This values were derived empirically.
1880     const RGBA32 baseDarkColor = 0xFF202020;
1881     const RGBA32 baseLightColor = 0xFFEBEBEB;
1882     enum Operation { Darken, Lighten };
1883
1884     Operation operation = (side == BSTop || side == BSLeft) == (style == INSET) ? Darken : Lighten;
1885
1886     // Here we will darken the border decoration color when needed. This will yield a similar behavior as in FF.
1887     if (operation == Darken) {
1888         if (differenceSquared(color, Color::black) > differenceSquared(baseDarkColor, Color::black))
1889             color = color.dark();
1890     } else {
1891         if (differenceSquared(color, Color::white) > differenceSquared(baseLightColor, Color::white))
1892             color = color.light();
1893     }
1894 }
1895
1896 void RenderObject::setIsDragging(bool isDragging)
1897 {
1898     if (isDragging || hasRareData())
1899         ensureRareData().setIsDragging(isDragging);
1900 }
1901
1902 void RenderObject::setHasReflection(bool hasReflection)
1903 {
1904     if (hasReflection || hasRareData())
1905         ensureRareData().setHasReflection(hasReflection);
1906 }
1907
1908 void RenderObject::setIsRenderFragmentedFlow(bool isFragmentedFlow)
1909 {
1910     if (isFragmentedFlow || hasRareData())
1911         ensureRareData().setIsRenderFragmentedFlow(isFragmentedFlow);
1912 }
1913
1914 void RenderObject::setHasOutlineAutoAncestor(bool hasOutlineAutoAncestor)
1915 {
1916     if (hasOutlineAutoAncestor || hasRareData())
1917         ensureRareData().setHasOutlineAutoAncestor(hasOutlineAutoAncestor);
1918 }
1919
1920 RenderObject::RareDataMap& RenderObject::rareDataMap()
1921 {
1922     static NeverDestroyed<RareDataMap> map;
1923     return map;
1924 }
1925
1926 const RenderObject::RenderObjectRareData& RenderObject::rareData() const
1927 {
1928     ASSERT(hasRareData());
1929     return *rareDataMap().get(this);
1930 }
1931
1932 RenderObject::RenderObjectRareData& RenderObject::ensureRareData()
1933 {
1934     setHasRareData(true);
1935     return *rareDataMap().ensure(this, [] { return std::make_unique<RenderObjectRareData>(); }).iterator->value;
1936 }
1937
1938 void RenderObject::removeRareData()
1939 {
1940     rareDataMap().remove(this);
1941     setHasRareData(false);
1942 }
1943
1944 #if ENABLE(TREE_DEBUGGING)
1945
1946 void printRenderTreeForLiveDocuments()
1947 {
1948     for (const auto* document : Document::allDocuments()) {
1949         if (!document->renderView())
1950             continue;
1951         if (document->frame() && document->frame()->isMainFrame())
1952             fprintf(stderr, "----------------------main frame--------------------------\n");
1953         fprintf(stderr, "%s", document->url().string().utf8().data());
1954         showRenderTree(document->renderView());
1955     }
1956 }
1957
1958 void printLayerTreeForLiveDocuments()
1959 {
1960     for (const auto* document : Document::allDocuments()) {
1961         if (!document->renderView())
1962             continue;
1963         if (document->frame() && document->frame()->isMainFrame())
1964             fprintf(stderr, "----------------------main frame--------------------------\n");
1965         fprintf(stderr, "%s", document->url().string().utf8().data());
1966         showLayerTree(document->renderView());
1967     }
1968 }
1969
1970 #endif // ENABLE(TREE_DEBUGGING)
1971
1972 } // namespace WebCore
1973
1974 #if ENABLE(TREE_DEBUGGING)
1975
1976 void showNodeTree(const WebCore::RenderObject* object)
1977 {
1978     if (!object)
1979         return;
1980     object->showNodeTreeForThis();
1981 }
1982
1983 void showLineTree(const WebCore::RenderObject* object)
1984 {
1985     if (!object)
1986         return;
1987     object->showLineTreeForThis();
1988 }
1989
1990 void showRenderTree(const WebCore::RenderObject* object)
1991 {
1992     if (!object)
1993         return;
1994     object->showRenderTreeForThis();
1995 }
1996
1997 #endif