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