Make it possible to compute a region for elements on the page that have wheel event...
[WebKit-https.git] / Source / WebCore / rendering / RenderObject.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2000 Dirk Mueller (mueller@kde.org)
5  *           (C) 2004 Allan Sandfeld Jensen (kde@carewolf.com)
6  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2011, 2013 Apple Inc. All rights reserved.
7  * Copyright (C) 2009 Google Inc. All rights reserved.
8  * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public License
21  * along with this library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  *
25  */
26
27 #include "config.h"
28 #include "RenderObject.h"
29
30 #include "AXObjectCache.h"
31 #include "AnimationController.h"
32 #include "EventHandler.h"
33 #include "FloatQuad.h"
34 #include "FlowThreadController.h"
35 #include "FocusController.h"
36 #include "FrameSelection.h"
37 #include "FrameView.h"
38 #include "GeometryUtilities.h"
39 #include "GraphicsContext.h"
40 #include "HTMLAnchorElement.h"
41 #include "HTMLElement.h"
42 #include "HTMLImageElement.h"
43 #include "HTMLNames.h"
44 #include "HTMLTableCellElement.h"
45 #include "HTMLTableElement.h"
46 #include "HitTestResult.h"
47 #include "LogicalSelectionOffsetCaches.h"
48 #include "Page.h"
49 #include "PseudoElement.h"
50 #include "RenderCounter.h"
51 #include "RenderFlowThread.h"
52 #include "RenderGeometryMap.h"
53 #include "RenderInline.h"
54 #include "RenderIterator.h"
55 #include "RenderLayer.h"
56 #include "RenderLayerBacking.h"
57 #include "RenderNamedFlowFragment.h"
58 #include "RenderNamedFlowThread.h" 
59 #include "RenderSVGResourceContainer.h"
60 #include "RenderScrollbarPart.h"
61 #include "RenderTheme.h"
62 #include "RenderView.h"
63 #include "SVGRenderSupport.h"
64 #include "Settings.h"
65 #include "StyleResolver.h"
66 #include "TransformState.h"
67 #include "htmlediting.h"
68 #include <algorithm>
69 #include <stdio.h>
70 #include <wtf/RefCountedLeakCounter.h>
71
72 #if PLATFORM(IOS)
73 #include "SelectionRect.h"
74 #endif
75
76 namespace WebCore {
77
78 using namespace HTMLNames;
79
80 #ifndef NDEBUG
81 RenderObject::SetLayoutNeededForbiddenScope::SetLayoutNeededForbiddenScope(RenderObject* renderObject, bool isForbidden)
82     : m_renderObject(renderObject)
83     , m_preexistingForbidden(m_renderObject->isSetNeedsLayoutForbidden())
84 {
85     m_renderObject->setNeedsLayoutIsForbidden(isForbidden);
86 }
87
88 RenderObject::SetLayoutNeededForbiddenScope::~SetLayoutNeededForbiddenScope()
89 {
90     m_renderObject->setNeedsLayoutIsForbidden(m_preexistingForbidden);
91 }
92 #endif
93
94 struct SameSizeAsRenderObject {
95     virtual ~SameSizeAsRenderObject() { } // Allocate vtable pointer.
96     void* pointers[4];
97 #ifndef NDEBUG
98     unsigned m_debugBitfields : 2;
99 #endif
100     unsigned m_bitfields;
101 };
102
103 COMPILE_ASSERT(sizeof(RenderObject) == sizeof(SameSizeAsRenderObject), RenderObject_should_stay_small);
104
105 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, renderObjectCounter, ("RenderObject"));
106
107 RenderObject::RenderObject(Node& node)
108     : CachedImageClient()
109     , m_node(node)
110     , m_parent(nullptr)
111     , m_previous(nullptr)
112     , m_next(nullptr)
113 #ifndef NDEBUG
114     , m_hasAXObject(false)
115     , m_setNeedsLayoutForbidden(false)
116 #endif
117     , m_bitfields(node)
118 {
119     if (!node.isDocumentNode())
120         view().didCreateRenderer();
121 #ifndef NDEBUG
122     renderObjectCounter.increment();
123 #endif
124 }
125
126 RenderObject::~RenderObject()
127 {
128 #ifndef NDEBUG
129     ASSERT(!m_hasAXObject);
130     renderObjectCounter.decrement();
131 #endif
132     view().didDestroyRenderer();
133 }
134
135 RenderTheme& RenderObject::theme() const
136 {
137     ASSERT(document().page());
138     return document().page()->theme();
139 }
140
141 bool RenderObject::isDescendantOf(const RenderObject* obj) const
142 {
143     for (const RenderObject* r = this; r; r = r->m_parent) {
144         if (r == obj)
145             return true;
146     }
147     return false;
148 }
149
150 bool RenderObject::isLegend() const
151 {
152     return node() && node()->hasTagName(legendTag);
153 }
154
155 bool RenderObject::isHTMLMarquee() const
156 {
157     return node() && node()->renderer() == this && node()->hasTagName(marqueeTag);
158 }
159
160 void RenderObject::setFlowThreadStateIncludingDescendants(FlowThreadState state)
161 {
162     setFlowThreadState(state);
163
164     for (RenderObject* child = firstChildSlow(); child; child = child->nextSibling()) {
165         // If the child is a fragmentation context it already updated the descendants flag accordingly.
166         if (child->isRenderFlowThread())
167             continue;
168         ASSERT(state != child->flowThreadState());
169         child->setFlowThreadStateIncludingDescendants(state);
170     }
171 }
172
173 void RenderObject::setParent(RenderElement* parent)
174 {
175     m_parent = parent;
176
177     // Only update if our flow thread state is different from our new parent and if we're not a RenderFlowThread.
178     // A RenderFlowThread is always considered to be inside itself, so it never has to change its state
179     // in response to parent changes.
180     FlowThreadState newState = parent ? parent->flowThreadState() : NotInsideFlowThread;
181     if (newState != flowThreadState() && !isRenderFlowThread())
182         setFlowThreadStateIncludingDescendants(newState);
183 }
184
185 void RenderObject::removeFromParent()
186 {
187     if (parent())
188         parent()->removeChild(*this);
189 }
190
191 RenderObject* RenderObject::nextInPreOrder() const
192 {
193     if (RenderObject* o = firstChildSlow())
194         return o;
195
196     return nextInPreOrderAfterChildren();
197 }
198
199 RenderObject* RenderObject::nextInPreOrderAfterChildren() const
200 {
201     RenderObject* o;
202     if (!(o = nextSibling())) {
203         o = parent();
204         while (o && !o->nextSibling())
205             o = o->parent();
206         if (o)
207             o = o->nextSibling();
208     }
209
210     return o;
211 }
212
213 RenderObject* RenderObject::nextInPreOrder(const RenderObject* stayWithin) const
214 {
215     if (RenderObject* o = firstChildSlow())
216         return o;
217
218     return nextInPreOrderAfterChildren(stayWithin);
219 }
220
221 RenderObject* RenderObject::nextInPreOrderAfterChildren(const RenderObject* stayWithin) const
222 {
223     if (this == stayWithin)
224         return nullptr;
225
226     const RenderObject* current = this;
227     RenderObject* next;
228     while (!(next = current->nextSibling())) {
229         current = current->parent();
230         if (!current || current == stayWithin)
231             return nullptr;
232     }
233     return next;
234 }
235
236 RenderObject* RenderObject::previousInPreOrder() const
237 {
238     if (RenderObject* o = previousSibling()) {
239         while (RenderObject* last = o->lastChildSlow())
240             o = last;
241         return o;
242     }
243
244     return parent();
245 }
246
247 RenderObject* RenderObject::previousInPreOrder(const RenderObject* stayWithin) const
248 {
249     if (this == stayWithin)
250         return nullptr;
251
252     return previousInPreOrder();
253 }
254
255 RenderObject* RenderObject::childAt(unsigned index) const
256 {
257     RenderObject* child = firstChildSlow();
258     for (unsigned i = 0; child && i < index; i++)
259         child = child->nextSibling();
260     return child;
261 }
262
263 RenderObject* RenderObject::firstLeafChild() const
264 {
265     RenderObject* r = firstChildSlow();
266     while (r) {
267         RenderObject* n = nullptr;
268         n = r->firstChildSlow();
269         if (!n)
270             break;
271         r = n;
272     }
273     return r;
274 }
275
276 RenderObject* RenderObject::lastLeafChild() const
277 {
278     RenderObject* r = lastChildSlow();
279     while (r) {
280         RenderObject* n = nullptr;
281         n = r->lastChildSlow();
282         if (!n)
283             break;
284         r = n;
285     }
286     return r;
287 }
288
289 #if ENABLE(IOS_TEXT_AUTOSIZING)
290 // Inspired by Node::traverseNextNode.
291 RenderObject* RenderObject::traverseNext(const RenderObject* stayWithin) const
292 {
293     RenderObject* child = firstChildSlow();
294     if (child) {
295         ASSERT(!stayWithin || child->isDescendantOf(stayWithin));
296         return child;
297     }
298     if (this == stayWithin)
299         return nullptr;
300     if (nextSibling()) {
301         ASSERT(!stayWithin || nextSibling()->isDescendantOf(stayWithin));
302         return nextSibling();
303     }
304     const RenderObject* n = this;
305     while (n && !n->nextSibling() && (!stayWithin || n->parent() != stayWithin))
306         n = n->parent();
307     if (n) {
308         ASSERT(!stayWithin || !n->nextSibling() || n->nextSibling()->isDescendantOf(stayWithin));
309         return n->nextSibling();
310     }
311     return nullptr;
312 }
313
314 // Non-recursive version of the DFS search.
315 RenderObject* RenderObject::traverseNext(const RenderObject* stayWithin, HeightTypeTraverseNextInclusionFunction inclusionFunction, int& currentDepth, int& newFixedDepth) const
316 {
317     BlockContentHeightType overflowType;
318
319     // Check for suitable children.
320     for (RenderObject* child = firstChildSlow(); child; child = child->nextSibling()) {
321         overflowType = inclusionFunction(child);
322         if (overflowType != FixedHeight) {
323             currentDepth++;
324             if (overflowType == OverflowHeight)
325                 newFixedDepth = currentDepth;
326             ASSERT(!stayWithin || child->isDescendantOf(stayWithin));
327             return child;
328         }
329     }
330
331     if (this == stayWithin)
332         return nullptr;
333
334     // Now we traverse other nodes if they exist, otherwise
335     // we go to the parent node and try doing the same.
336     const RenderObject* n = this;
337     while (n) {
338         while (n && !n->nextSibling() && (!stayWithin || n->parent() != stayWithin)) {
339             n = n->parent();
340             currentDepth--;
341         }
342         if (!n)
343             return nullptr;
344         for (RenderObject* sibling = n->nextSibling(); sibling; sibling = sibling->nextSibling()) {
345             overflowType = inclusionFunction(sibling);
346             if (overflowType != FixedHeight) {
347                 if (overflowType == OverflowHeight)
348                     newFixedDepth = currentDepth;
349                 ASSERT(!stayWithin || !n->nextSibling() || n->nextSibling()->isDescendantOf(stayWithin));
350                 return sibling;
351             }
352         }
353         if (!stayWithin || n->parent() != stayWithin) {
354             n = n->parent();
355             currentDepth--;
356         } else
357             return nullptr;
358     }
359     return nullptr;
360 }
361
362 RenderObject* RenderObject::traverseNext(const RenderObject* stayWithin, TraverseNextInclusionFunction inclusionFunction) const
363 {
364     for (RenderObject* child = firstChildSlow(); child; child = child->nextSibling()) {
365         if (inclusionFunction(child)) {
366             ASSERT(!stayWithin || child->isDescendantOf(stayWithin));
367             return child;
368         }
369     }
370
371     if (this == stayWithin)
372         return nullptr;
373
374     for (RenderObject* sibling = nextSibling(); sibling; sibling = sibling->nextSibling()) {
375         if (inclusionFunction(sibling)) {
376             ASSERT(!stayWithin || sibling->isDescendantOf(stayWithin));
377             return sibling;
378         }
379     }
380
381     const RenderObject* n = this;
382     while (n) {
383         while (n && !n->nextSibling() && (!stayWithin || n->parent() != stayWithin))
384             n = n->parent();
385         if (n) {
386             for (RenderObject* sibling = n->nextSibling(); sibling; sibling = sibling->nextSibling()) {
387                 if (inclusionFunction(sibling)) {
388                     ASSERT(!stayWithin || !n->nextSibling() || n->nextSibling()->isDescendantOf(stayWithin));
389                     return sibling;
390                 }
391             }
392             if ((!stayWithin || n->parent() != stayWithin))
393                 n = n->parent();
394             else
395                 return nullptr;
396         }
397     }
398     return nullptr;
399 }
400
401 static RenderObject::BlockContentHeightType includeNonFixedHeight(const RenderObject* renderer)
402 {
403     const RenderStyle& style = renderer->style();
404     if (style.height().type() == Fixed) {
405         if (is<RenderBlock>(*renderer)) {
406             // For fixed height styles, if the overflow size of the element spills out of the specified
407             // height, assume we can apply text auto-sizing.
408             if (style.overflowY() == OVISIBLE
409                 && style.height().value() < downcast<RenderBlock>(renderer)->layoutOverflowRect().maxY())
410                 return RenderObject::OverflowHeight;
411         }
412         return RenderObject::FixedHeight;
413     }
414     return RenderObject::FlexibleHeight;
415 }
416
417
418 void RenderObject::adjustComputedFontSizesOnBlocks(float size, float visibleWidth)
419 {
420     Document* document = view().frameView().frame().document();
421     if (!document)
422         return;
423
424     Vector<int> depthStack;
425     int currentDepth = 0;
426     int newFixedDepth = 0;
427
428     // We don't apply autosizing to nodes with fixed height normally.
429     // But we apply it to nodes which are located deep enough
430     // (nesting depth is greater than some const) inside of a parent block
431     // which has fixed height but its content overflows intentionally.
432     for (RenderObject* descendent = traverseNext(this, includeNonFixedHeight, currentDepth, newFixedDepth); descendent; descendent = descendent->traverseNext(this, includeNonFixedHeight, currentDepth, newFixedDepth)) {
433         while (depthStack.size() > 0 && currentDepth <= depthStack[depthStack.size() - 1])
434             depthStack.remove(depthStack.size() - 1);
435         if (newFixedDepth)
436             depthStack.append(newFixedDepth);
437
438         int stackSize = depthStack.size();
439         if (is<RenderBlockFlow>(*descendent) && !descendent->isListItem() && (!stackSize || currentDepth - depthStack[stackSize - 1] > TextAutoSizingFixedHeightDepth))
440             downcast<RenderBlockFlow>(*descendent).adjustComputedFontSizes(size, visibleWidth);
441         newFixedDepth = 0;
442     }
443
444     // Remove style from auto-sizing table that are no longer valid.
445     document->validateAutoSizingNodes();
446 }
447
448 void RenderObject::resetTextAutosizing()
449 {
450     Document* document = view().frameView().frame().document();
451     if (!document)
452         return;
453
454     document->resetAutoSizingNodes();
455
456     Vector<int> depthStack;
457     int currentDepth = 0;
458     int newFixedDepth = 0;
459
460     for (RenderObject* descendent = traverseNext(this, includeNonFixedHeight, currentDepth, newFixedDepth); descendent; descendent = descendent->traverseNext(this, includeNonFixedHeight, currentDepth, newFixedDepth)) {
461         while (depthStack.size() > 0 && currentDepth <= depthStack[depthStack.size() - 1])
462             depthStack.remove(depthStack.size() - 1);
463         if (newFixedDepth)
464             depthStack.append(newFixedDepth);
465
466         int stackSize = depthStack.size();
467         if (is<RenderBlockFlow>(*descendent) && !descendent->isListItem() && (!stackSize || currentDepth - depthStack[stackSize - 1] > TextAutoSizingFixedHeightDepth))
468             downcast<RenderBlockFlow>(*descendent).resetComputedFontSize();
469         newFixedDepth = 0;
470     }
471 }
472 #endif // ENABLE(IOS_TEXT_AUTOSIZING)
473
474 RenderLayer* RenderObject::enclosingLayer() const
475 {
476     for (auto& renderer : lineageOfType<RenderLayerModelObject>(*this)) {
477         if (renderer.hasLayer())
478             return renderer.layer();
479     }
480     return nullptr;
481 }
482
483 bool RenderObject::scrollRectToVisible(const LayoutRect& rect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
484 {
485     RenderLayer* enclosingLayer = this->enclosingLayer();
486     if (!enclosingLayer)
487         return false;
488
489     enclosingLayer->scrollRectToVisible(rect, alignX, alignY);
490     return true;
491 }
492
493 RenderBox& RenderObject::enclosingBox() const
494 {
495     return *lineageOfType<RenderBox>(const_cast<RenderObject&>(*this)).first();
496 }
497
498 RenderBoxModelObject& RenderObject::enclosingBoxModelObject() const
499 {
500     return *lineageOfType<RenderBoxModelObject>(const_cast<RenderObject&>(*this)).first();
501 }
502
503 bool RenderObject::fixedPositionedWithNamedFlowContainingBlock() const
504 {
505     return ((flowThreadState() == RenderObject::InsideOutOfFlowThread)
506         && (style().position() == FixedPosition)
507         && (containingBlock()->isOutOfFlowRenderFlowThread()));
508 }
509
510 static bool hasFixedPosInNamedFlowContainingBlock(const RenderObject* renderer)
511 {
512     ASSERT(renderer->flowThreadState() != RenderObject::NotInsideFlowThread);
513
514     RenderObject* curr = const_cast<RenderObject*>(renderer);
515     while (curr) {
516         if (curr->fixedPositionedWithNamedFlowContainingBlock())
517             return true;
518         curr = curr->containingBlock();
519     }
520
521     return false;
522 }
523
524 RenderBlock* RenderObject::firstLineBlock() const
525 {
526     return nullptr;
527 }
528
529 static inline bool objectIsRelayoutBoundary(const RenderElement* object)
530 {
531     // FIXME: In future it may be possible to broaden these conditions in order to improve performance.
532     if (object->isRenderView())
533         return true;
534
535     if (object->isTextControl())
536         return true;
537
538     if (object->isSVGRoot())
539         return true;
540
541     if (!object->hasOverflowClip())
542         return false;
543
544     if (object->style().width().isIntrinsicOrAuto() || object->style().height().isIntrinsicOrAuto() || object->style().height().isPercent())
545         return false;
546
547     // Table parts can't be relayout roots since the table is responsible for layouting all the parts.
548     if (object->isTablePart())
549         return false;
550
551     return true;
552 }
553
554 void RenderObject::clearNeedsLayout()
555 {
556     m_bitfields.setNeedsLayout(false);
557     setEverHadLayout(true);
558     setPosChildNeedsLayoutBit(false);
559     setNeedsSimplifiedNormalFlowLayoutBit(false);
560     setNormalChildNeedsLayoutBit(false);
561     setNeedsPositionedMovementLayoutBit(false);
562     if (is<RenderElement>(*this))
563         downcast<RenderElement>(*this).setAncestorLineBoxDirty(false);
564 #ifndef NDEBUG
565     checkBlockPositionedObjectsNeedLayout();
566 #endif
567 }
568
569 static void scheduleRelayoutForSubtree(RenderElement& renderer)
570 {
571     if (!is<RenderView>(renderer)) {
572         if (!renderer.isRooted())
573             return;
574         renderer.view().frameView().scheduleRelayoutOfSubtree(renderer);
575         return;
576     }
577     downcast<RenderView>(renderer).frameView().scheduleRelayout();
578 }
579
580 void RenderObject::markContainingBlocksForLayout(bool scheduleRelayout, RenderElement* newRoot)
581 {
582     ASSERT(!scheduleRelayout || !newRoot);
583     ASSERT(!isSetNeedsLayoutForbidden());
584
585     auto ancestor = container();
586
587     bool simplifiedNormalFlowLayout = needsSimplifiedNormalFlowLayout() && !selfNeedsLayout() && !normalChildNeedsLayout();
588     bool hasOutOfFlowPosition = !isText() && style().hasOutOfFlowPosition();
589
590     while (ancestor) {
591 #ifndef NDEBUG
592         // FIXME: Remove this once we remove the special cases for counters, quotes and mathml
593         // calling setNeedsLayout during preferred width computation.
594         SetLayoutNeededForbiddenScope layoutForbiddenScope(ancestor, isSetNeedsLayoutForbidden());
595 #endif
596         // Don't mark the outermost object of an unrooted subtree. That object will be
597         // marked when the subtree is added to the document.
598         auto container = ancestor->container();
599         if (!container && !ancestor->isRenderView())
600             return;
601         if (hasOutOfFlowPosition) {
602             bool willSkipRelativelyPositionedInlines = !ancestor->isRenderBlock() || ancestor->isAnonymousBlock();
603             // Skip relatively positioned inlines and anonymous blocks to get to the enclosing RenderBlock.
604             while (ancestor && (!ancestor->isRenderBlock() || ancestor->isAnonymousBlock()))
605                 ancestor = ancestor->container();
606             if (!ancestor || ancestor->posChildNeedsLayout())
607                 return;
608             if (willSkipRelativelyPositionedInlines)
609                 container = ancestor->container();
610             ancestor->setPosChildNeedsLayoutBit(true);
611             simplifiedNormalFlowLayout = true;
612         } else if (simplifiedNormalFlowLayout) {
613             if (ancestor->needsSimplifiedNormalFlowLayout())
614                 return;
615             ancestor->setNeedsSimplifiedNormalFlowLayoutBit(true);
616         } else {
617             if (ancestor->normalChildNeedsLayout())
618                 return;
619             ancestor->setNormalChildNeedsLayoutBit(true);
620         }
621         ASSERT(!ancestor->isSetNeedsLayoutForbidden());
622
623         if (ancestor == newRoot)
624             return;
625
626         if (scheduleRelayout && objectIsRelayoutBoundary(ancestor))
627             break;
628
629         hasOutOfFlowPosition = ancestor->style().hasOutOfFlowPosition();
630         ancestor = container;
631     }
632
633     if (scheduleRelayout && ancestor)
634         scheduleRelayoutForSubtree(*ancestor);
635 }
636
637 #ifndef NDEBUG
638 void RenderObject::checkBlockPositionedObjectsNeedLayout()
639 {
640     ASSERT(!needsLayout());
641
642     if (is<RenderBlock>(*this))
643         downcast<RenderBlock>(*this).checkPositionedObjectsNeedLayout();
644 }
645 #endif
646
647 void RenderObject::setPreferredLogicalWidthsDirty(bool shouldBeDirty, MarkingBehavior markParents)
648 {
649     bool alreadyDirty = preferredLogicalWidthsDirty();
650     m_bitfields.setPreferredLogicalWidthsDirty(shouldBeDirty);
651     if (shouldBeDirty && !alreadyDirty && markParents == MarkContainingBlockChain && (isText() || !style().hasOutOfFlowPosition()))
652         invalidateContainerPreferredLogicalWidths();
653 }
654
655 void RenderObject::invalidateContainerPreferredLogicalWidths()
656 {
657     // In order to avoid pathological behavior when inlines are deeply nested, we do include them
658     // in the chain that we mark dirty (even though they're kind of irrelevant).
659     auto o = isTableCell() ? containingBlock() : container();
660     while (o && !o->preferredLogicalWidthsDirty()) {
661         // Don't invalidate the outermost object of an unrooted subtree. That object will be 
662         // invalidated when the subtree is added to the document.
663         auto container = o->isTableCell() ? o->containingBlock() : o->container();
664         if (!container && !o->isRenderView())
665             break;
666
667         o->m_bitfields.setPreferredLogicalWidthsDirty(true);
668         if (o->style().hasOutOfFlowPosition())
669             // A positioned object has no effect on the min/max width of its containing block ever.
670             // We can optimize this case and not go up any further.
671             break;
672         o = container;
673     }
674 }
675
676 void RenderObject::setLayerNeedsFullRepaint()
677 {
678     ASSERT(hasLayer());
679     downcast<RenderLayerModelObject>(*this).layer()->setRepaintStatus(NeedsFullRepaint);
680 }
681
682 void RenderObject::setLayerNeedsFullRepaintForPositionedMovementLayout()
683 {
684     ASSERT(hasLayer());
685     downcast<RenderLayerModelObject>(*this).layer()->setRepaintStatus(NeedsFullRepaintForPositionedMovementLayout);
686 }
687
688 RenderBlock* RenderObject::containingBlock() const
689 {
690     auto parent = this->parent();
691     if (!parent && is<RenderScrollbarPart>(*this))
692         parent = downcast<RenderScrollbarPart>(*this).rendererOwningScrollbar();
693
694     const RenderStyle& style = this->style();
695     if (!is<RenderText>(*this) && style.position() == FixedPosition)
696         parent = parent->containingBlockForFixedPosition();
697     else if (!is<RenderText>(*this) && style.position() == AbsolutePosition)
698         parent = parent->containingBlockForAbsolutePosition();
699     else
700         parent = parent->containingBlockForObjectInFlow();
701
702     if (!is<RenderBlock>(parent))
703         return nullptr; // This can still happen in case of an orphaned tree
704
705     return downcast<RenderBlock>(parent);
706 }
707
708 void RenderObject::drawLineForBoxSide(GraphicsContext* graphicsContext, float x1, float y1, float x2, float y2,
709     BoxSide side, Color color, EBorderStyle borderStyle, float adjacentWidth1, float adjacentWidth2, bool antialias) const
710 {
711     float deviceScaleFactor = document().deviceScaleFactor();
712     float thickness;
713     float length;
714     if (side == BSTop || side == BSBottom) {
715         thickness = y2 - y1;
716         length = x2 - x1;
717     } else {
718         thickness = x2 - x1;
719         length = y2 - y1;
720     }
721     if (borderStyle == DOUBLE && (thickness * deviceScaleFactor) < 3)
722         borderStyle = SOLID;
723
724     // FIXME: We really would like this check to be an ASSERT as we don't want to draw empty borders. However
725     // nothing guarantees that the following recursive calls to drawLineForBoxSide will have non-null dimensions.
726     if (!thickness || !length)
727         return;
728
729     const RenderStyle& style = this->style();
730     switch (borderStyle) {
731         case BNONE:
732         case BHIDDEN:
733             return;
734         case DOTTED:
735         case DASHED: {
736             bool wasAntialiased = graphicsContext->shouldAntialias();
737             StrokeStyle oldStrokeStyle = graphicsContext->strokeStyle();
738             graphicsContext->setShouldAntialias(antialias);
739             graphicsContext->setStrokeColor(color, style.colorSpace());
740             graphicsContext->setStrokeThickness(thickness);
741             graphicsContext->setStrokeStyle(borderStyle == DASHED ? DashedStroke : DottedStroke);
742             graphicsContext->drawLine(roundPointToDevicePixels(LayoutPoint(x1, y1), deviceScaleFactor), roundPointToDevicePixels(LayoutPoint(x2, y2), deviceScaleFactor));
743             graphicsContext->setShouldAntialias(wasAntialiased);
744             graphicsContext->setStrokeStyle(oldStrokeStyle);
745             break;
746         }
747         case DOUBLE: {
748             float thirdOfThickness = ceilToDevicePixel(thickness / 3, deviceScaleFactor);
749             ASSERT(thirdOfThickness);
750
751             if (adjacentWidth1 == 0 && adjacentWidth2 == 0) {
752                 StrokeStyle oldStrokeStyle = graphicsContext->strokeStyle();
753                 graphicsContext->setStrokeStyle(NoStroke);
754                 graphicsContext->setFillColor(color, style.colorSpace());
755                 
756                 bool wasAntialiased = graphicsContext->shouldAntialias();
757                 graphicsContext->setShouldAntialias(antialias);
758
759                 switch (side) {
760                     case BSTop:
761                     case BSBottom:
762                         graphicsContext->drawRect(snapRectToDevicePixels(x1, y1, length, thirdOfThickness, deviceScaleFactor));
763                         graphicsContext->drawRect(snapRectToDevicePixels(x1, y2 - thirdOfThickness, length, thirdOfThickness, deviceScaleFactor));
764                         break;
765                     case BSLeft:
766                     case BSRight:
767                         graphicsContext->drawRect(snapRectToDevicePixels(x1, y1, thirdOfThickness, length, deviceScaleFactor));
768                         graphicsContext->drawRect(snapRectToDevicePixels(x2 - thirdOfThickness, y1, thirdOfThickness, length, deviceScaleFactor));
769                         break;
770                 }
771
772                 graphicsContext->setShouldAntialias(wasAntialiased);
773                 graphicsContext->setStrokeStyle(oldStrokeStyle);
774             } else {
775                 float adjacent1BigThird = ceilToDevicePixel(adjacentWidth1 / 3, deviceScaleFactor);
776                 float adjacent2BigThird = ceilToDevicePixel(adjacentWidth2 / 3, deviceScaleFactor);
777
778                 float offset1 = floorToDevicePixel(fabs(adjacentWidth1) * 2 / 3, deviceScaleFactor);
779                 float offset2 = floorToDevicePixel(fabs(adjacentWidth2) * 2 / 3, deviceScaleFactor);
780
781                 float mitreOffset1 = adjacentWidth1 < 0 ? offset1 : 0;
782                 float mitreOffset2 = adjacentWidth1 > 0 ? offset1 : 0;
783                 float mitreOffset3 = adjacentWidth2 < 0 ? offset2 : 0;
784                 float mitreOffset4 = adjacentWidth2 > 0 ? offset2 : 0;
785
786                 FloatRect paintBorderRect;
787                 switch (side) {
788                     case BSTop:
789                         paintBorderRect = snapRectToDevicePixels(LayoutRect(x1 + mitreOffset1, y1, (x2 - mitreOffset3) - (x1 + mitreOffset1), thirdOfThickness), deviceScaleFactor);
790                         drawLineForBoxSide(graphicsContext, paintBorderRect.x(), paintBorderRect.y(), paintBorderRect.maxX(), paintBorderRect.maxY(), side, color, SOLID,
791                             adjacent1BigThird, adjacent2BigThird, antialias);
792
793                         paintBorderRect = snapRectToDevicePixels(LayoutRect(x1 + mitreOffset2, y2 - thirdOfThickness, (x2 - mitreOffset4) - (x1 + mitreOffset2), thirdOfThickness), deviceScaleFactor);
794                         drawLineForBoxSide(graphicsContext, paintBorderRect.x(), paintBorderRect.y(), paintBorderRect.maxX(), paintBorderRect.maxY(), side, color, SOLID,
795                             adjacent1BigThird, adjacent2BigThird, antialias);
796                         break;
797                     case BSLeft:
798                         paintBorderRect = snapRectToDevicePixels(LayoutRect(x1, y1 + mitreOffset1, thirdOfThickness, (y2 - mitreOffset3) - (y1 + mitreOffset1)), deviceScaleFactor);
799                         drawLineForBoxSide(graphicsContext, paintBorderRect.x(), paintBorderRect.y(), paintBorderRect.maxX(), paintBorderRect.maxY(), side, color, SOLID,
800                             adjacent1BigThird, adjacent2BigThird, antialias);
801
802                         paintBorderRect = snapRectToDevicePixels(LayoutRect(x2 - thirdOfThickness, y1 + mitreOffset2, thirdOfThickness, (y2 - mitreOffset4) - (y1 + mitreOffset2)), deviceScaleFactor);
803                         drawLineForBoxSide(graphicsContext, paintBorderRect.x(), paintBorderRect.y(), paintBorderRect.maxX(), paintBorderRect.maxY(), side, color, SOLID,
804                             adjacent1BigThird, adjacent2BigThird, antialias);
805                         break;
806                     case BSBottom:
807                         paintBorderRect = snapRectToDevicePixels(LayoutRect(x1 + mitreOffset2, y1, (x2 - mitreOffset4) - (x1 + mitreOffset2), thirdOfThickness), deviceScaleFactor);
808                         drawLineForBoxSide(graphicsContext, paintBorderRect.x(), paintBorderRect.y(), paintBorderRect.maxX(), paintBorderRect.maxY(), side, color, SOLID,
809                             adjacent1BigThird, adjacent2BigThird, antialias);
810
811                         paintBorderRect = snapRectToDevicePixels(LayoutRect(x1 + mitreOffset1, y2 - thirdOfThickness, (x2 - mitreOffset3) - (x1 + mitreOffset1), thirdOfThickness), deviceScaleFactor);
812                         drawLineForBoxSide(graphicsContext, paintBorderRect.x(), paintBorderRect.y(), paintBorderRect.maxX(), paintBorderRect.maxY(), side, color, SOLID,
813                             adjacent1BigThird, adjacent2BigThird, antialias);
814                         break;
815                     case BSRight:
816                         paintBorderRect = snapRectToDevicePixels(LayoutRect(x1, y1 + mitreOffset2, thirdOfThickness, (y2 - mitreOffset4) - (y1 + mitreOffset2)), deviceScaleFactor);
817                         drawLineForBoxSide(graphicsContext, paintBorderRect.x(), paintBorderRect.y(), paintBorderRect.maxX(), paintBorderRect.maxY(), side, color, SOLID,
818                             adjacent1BigThird, adjacent2BigThird, antialias);
819
820                         paintBorderRect = snapRectToDevicePixels(LayoutRect(x2 - thirdOfThickness, y1 + mitreOffset1, thirdOfThickness, (y2 - mitreOffset3) - (y1 + mitreOffset1)), deviceScaleFactor);
821                         drawLineForBoxSide(graphicsContext, paintBorderRect.x(), paintBorderRect.y(), paintBorderRect.maxX(), paintBorderRect.maxY(), side, color, SOLID,
822                             adjacent1BigThird, adjacent2BigThird, antialias);
823                         break;
824                     default:
825                         break;
826                 }
827             }
828             break;
829         }
830         case RIDGE:
831         case GROOVE: {
832             EBorderStyle s1;
833             EBorderStyle s2;
834             if (borderStyle == GROOVE) {
835                 s1 = INSET;
836                 s2 = OUTSET;
837             } else {
838                 s1 = OUTSET;
839                 s2 = INSET;
840             }
841
842             float adjacent1BigHalf = ceilToDevicePixel(adjacentWidth1 / 2, deviceScaleFactor);
843             float adjacent2BigHalf = ceilToDevicePixel(adjacentWidth2 / 2, deviceScaleFactor);
844
845             float adjacent1SmallHalf = floorToDevicePixel(adjacentWidth1 / 2, deviceScaleFactor);
846             float adjacent2SmallHalf = floorToDevicePixel(adjacentWidth2 / 2, deviceScaleFactor);
847
848             float offset1 = 0;
849             float offset2 = 0;
850             float offset3 = 0;
851             float offset4 = 0;
852
853             if (((side == BSTop || side == BSLeft) && adjacentWidth1 < 0) || ((side == BSBottom || side == BSRight) && adjacentWidth1 > 0))
854                 offset1 = floorToDevicePixel(adjacentWidth1 / 2, deviceScaleFactor);
855
856             if (((side == BSTop || side == BSLeft) && adjacentWidth2 < 0) || ((side == BSBottom || side == BSRight) && adjacentWidth2 > 0))
857                 offset2 = ceilToDevicePixel(adjacentWidth2 / 2, deviceScaleFactor);
858
859             if (((side == BSTop || side == BSLeft) && adjacentWidth1 > 0) || ((side == BSBottom || side == BSRight) && adjacentWidth1 < 0))
860                 offset3 = floorToDevicePixel(fabs(adjacentWidth1) / 2, deviceScaleFactor);
861
862             if (((side == BSTop || side == BSLeft) && adjacentWidth2 > 0) || ((side == BSBottom || side == BSRight) && adjacentWidth2 < 0))
863                 offset4 = ceilToDevicePixel(adjacentWidth2 / 2, deviceScaleFactor);
864
865             float adjustedX = ceilToDevicePixel((x1 + x2) / 2, deviceScaleFactor);
866             float adjustedY = ceilToDevicePixel((y1 + y2) / 2, deviceScaleFactor);
867             /// Quads can't use the default snapping rect functions.
868             x1 = roundToDevicePixel(x1, deviceScaleFactor);
869             x2 = roundToDevicePixel(x2, deviceScaleFactor);
870             y1 = roundToDevicePixel(y1, deviceScaleFactor);
871             y2 = roundToDevicePixel(y2, deviceScaleFactor);
872
873             switch (side) {
874                 case BSTop:
875                     drawLineForBoxSide(graphicsContext, x1 + offset1, y1, x2 - offset2, adjustedY, side, color, s1, adjacent1BigHalf, adjacent2BigHalf, antialias);
876                     drawLineForBoxSide(graphicsContext, x1 + offset3, adjustedY, x2 - offset4, y2, side, color, s2, adjacent1SmallHalf, adjacent2SmallHalf, antialias);
877                     break;
878                 case BSLeft:
879                     drawLineForBoxSide(graphicsContext, x1, y1 + offset1, adjustedX, y2 - offset2, side, color, s1, adjacent1BigHalf, adjacent2BigHalf, antialias);
880                     drawLineForBoxSide(graphicsContext, adjustedX, y1 + offset3, x2, y2 - offset4, side, color, s2, adjacent1SmallHalf, adjacent2SmallHalf, antialias);
881                     break;
882                 case BSBottom:
883                     drawLineForBoxSide(graphicsContext, x1 + offset1, y1, x2 - offset2, adjustedY, side, color, s2, adjacent1BigHalf, adjacent2BigHalf, antialias);
884                     drawLineForBoxSide(graphicsContext, x1 + offset3, adjustedY, x2 - offset4, y2, side, color, s1, adjacent1SmallHalf, adjacent2SmallHalf, antialias);
885                     break;
886                 case BSRight:
887                     drawLineForBoxSide(graphicsContext, x1, y1 + offset1, adjustedX, y2 - offset2, side, color, s2, adjacent1BigHalf, adjacent2BigHalf, antialias);
888                     drawLineForBoxSide(graphicsContext, adjustedX, y1 + offset3, x2, y2 - offset4, side, color, s1, adjacent1SmallHalf, adjacent2SmallHalf, antialias);
889                     break;
890             }
891             break;
892         }
893         case INSET:
894         case OUTSET:
895             calculateBorderStyleColor(borderStyle, side, color);
896             FALLTHROUGH;
897         case SOLID: {
898             StrokeStyle oldStrokeStyle = graphicsContext->strokeStyle();
899             ASSERT(x2 >= x1);
900             ASSERT(y2 >= y1);
901             if (!adjacentWidth1 && !adjacentWidth2) {
902                 // Turn off antialiasing to match the behavior of drawConvexPolygon();
903                 // this matters for rects in transformed contexts.
904                 graphicsContext->setStrokeStyle(NoStroke);
905                 graphicsContext->setFillColor(color, style.colorSpace());
906                 bool wasAntialiased = graphicsContext->shouldAntialias();
907                 graphicsContext->setShouldAntialias(antialias);
908                 graphicsContext->drawRect(snapRectToDevicePixels(x1, y1, x2 - x1, y2 - y1, deviceScaleFactor));
909                 graphicsContext->setShouldAntialias(wasAntialiased);
910                 graphicsContext->setStrokeStyle(oldStrokeStyle);
911                 return;
912             }
913
914             // FIXME: These roundings should be replaced by ASSERT(device pixel positioned) when all the callers transitioned to device pixels.
915             x1 = roundToDevicePixel(x1, deviceScaleFactor);
916             y1 = roundToDevicePixel(y1, deviceScaleFactor);
917             x2 = roundToDevicePixel(x2, deviceScaleFactor);
918             y2 = roundToDevicePixel(y2, deviceScaleFactor);
919
920             FloatPoint quad[4];
921             switch (side) {
922                 case BSTop:
923                     quad[0] = FloatPoint(x1 + std::max<float>(-adjacentWidth1, 0), y1);
924                     quad[1] = FloatPoint(x1 + std::max<float>(adjacentWidth1, 0), y2);
925                     quad[2] = FloatPoint(x2 - std::max<float>(adjacentWidth2, 0), y2);
926                     quad[3] = FloatPoint(x2 - std::max<float>(-adjacentWidth2, 0), y1);
927                     break;
928                 case BSBottom:
929                     quad[0] = FloatPoint(x1 + std::max<float>(adjacentWidth1, 0), y1);
930                     quad[1] = FloatPoint(x1 + std::max<float>(-adjacentWidth1, 0), y2);
931                     quad[2] = FloatPoint(x2 - std::max<float>(-adjacentWidth2, 0), y2);
932                     quad[3] = FloatPoint(x2 - std::max<float>(adjacentWidth2, 0), y1);
933                     break;
934                 case BSLeft:
935                     quad[0] = FloatPoint(x1, y1 + std::max<float>(-adjacentWidth1, 0));
936                     quad[1] = FloatPoint(x1, y2 - std::max<float>(-adjacentWidth2, 0));
937                     quad[2] = FloatPoint(x2, y2 - std::max<float>(adjacentWidth2, 0));
938                     quad[3] = FloatPoint(x2, y1 + std::max<float>(adjacentWidth1, 0));
939                     break;
940                 case BSRight:
941                     quad[0] = FloatPoint(x1, y1 + std::max<float>(adjacentWidth1, 0));
942                     quad[1] = FloatPoint(x1, y2 - std::max<float>(adjacentWidth2, 0));
943                     quad[2] = FloatPoint(x2, y2 - std::max<float>(-adjacentWidth2, 0));
944                     quad[3] = FloatPoint(x2, y1 + std::max<float>(-adjacentWidth1, 0));
945                     break;
946             }
947
948             graphicsContext->setStrokeStyle(NoStroke);
949             graphicsContext->setFillColor(color, style.colorSpace());
950             graphicsContext->drawConvexPolygon(4, quad, antialias);
951             graphicsContext->setStrokeStyle(oldStrokeStyle);
952             break;
953         }
954     }
955 }
956
957 void RenderObject::paintFocusRing(PaintInfo& paintInfo, const LayoutPoint& paintOffset, RenderStyle* style)
958 {
959     ASSERT(style->outlineStyleIsAuto());
960
961     Vector<IntRect> focusRingRects;
962     addFocusRingRects(focusRingRects, paintOffset, paintInfo.paintContainer);
963 #if PLATFORM(MAC)
964     bool needsRepaint;
965     paintInfo.context->drawFocusRing(focusRingRects, style->outlineWidth(), style->outlineOffset(), document().page()->focusController().timeSinceFocusWasSet(), needsRepaint);
966     if (needsRepaint)
967         document().page()->focusController().setFocusedElementNeedsRepaint();
968 #else
969     paintInfo.context->drawFocusRing(focusRingRects, style->outlineWidth(), style->outlineOffset(), style->visitedDependentColor(CSSPropertyOutlineColor));
970 #endif
971 }
972
973 void RenderObject::addPDFURLRect(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
974 {
975     Vector<IntRect> focusRingRects;
976     addFocusRingRects(focusRingRects, paintOffset, paintInfo.paintContainer);
977     IntRect urlRect = unionRect(focusRingRects);
978
979     if (urlRect.isEmpty())
980         return;
981     Node* node = this->node();
982     if (!is<Element>(node) || !node->isLink())
983         return;
984     const AtomicString& href = downcast<Element>(*node).getAttribute(hrefAttr);
985     if (href.isNull())
986         return;
987     paintInfo.context->setURLForRect(node->document().completeURL(href), snappedIntRect(urlRect));
988 }
989
990 void RenderObject::paintOutline(PaintInfo& paintInfo, const LayoutRect& paintRect)
991 {
992     if (!hasOutline())
993         return;
994
995     RenderStyle& styleToUse = style();
996     LayoutUnit outlineWidth = styleToUse.outlineWidth();
997
998     int outlineOffset = styleToUse.outlineOffset();
999
1000     // Only paint the focus ring by hand if the theme isn't able to draw it.
1001     if (styleToUse.outlineStyleIsAuto() && !theme().supportsFocusRing(styleToUse))
1002         paintFocusRing(paintInfo, paintRect.location(), &styleToUse);
1003
1004     if (hasOutlineAnnotation() && !styleToUse.outlineStyleIsAuto() && !theme().supportsFocusRing(styleToUse))
1005         addPDFURLRect(paintInfo, paintRect.location());
1006
1007     if (styleToUse.outlineStyleIsAuto() || styleToUse.outlineStyle() == BNONE)
1008         return;
1009
1010     IntRect inner = snappedIntRect(paintRect);
1011     inner.inflate(outlineOffset);
1012
1013     IntRect outer = snappedIntRect(inner);
1014     outer.inflate(outlineWidth);
1015
1016     // FIXME: This prevents outlines from painting inside the object. See bug 12042
1017     if (outer.isEmpty())
1018         return;
1019
1020     EBorderStyle outlineStyle = styleToUse.outlineStyle();
1021     Color outlineColor = styleToUse.visitedDependentColor(CSSPropertyOutlineColor);
1022
1023     GraphicsContext* graphicsContext = paintInfo.context;
1024     bool useTransparencyLayer = outlineColor.hasAlpha();
1025     if (useTransparencyLayer) {
1026         if (outlineStyle == SOLID) {
1027             Path path;
1028             path.addRect(outer);
1029             path.addRect(inner);
1030             graphicsContext->setFillRule(RULE_EVENODD);
1031             graphicsContext->setFillColor(outlineColor, styleToUse.colorSpace());
1032             graphicsContext->fillPath(path);
1033             return;
1034         }
1035         graphicsContext->beginTransparencyLayer(static_cast<float>(outlineColor.alpha()) / 255);
1036         outlineColor = Color(outlineColor.red(), outlineColor.green(), outlineColor.blue());
1037     }
1038
1039     int leftOuter = outer.x();
1040     int leftInner = inner.x();
1041     int rightOuter = outer.maxX();
1042     int rightInner = inner.maxX();
1043     int topOuter = outer.y();
1044     int topInner = inner.y();
1045     int bottomOuter = outer.maxY();
1046     int bottomInner = inner.maxY();
1047     
1048     drawLineForBoxSide(graphicsContext, leftOuter, topOuter, leftInner, bottomOuter, BSLeft, outlineColor, outlineStyle, outlineWidth, outlineWidth);
1049     drawLineForBoxSide(graphicsContext, leftOuter, topOuter, rightOuter, topInner, BSTop, outlineColor, outlineStyle, outlineWidth, outlineWidth);
1050     drawLineForBoxSide(graphicsContext, rightInner, topOuter, rightOuter, bottomOuter, BSRight, outlineColor, outlineStyle, outlineWidth, outlineWidth);
1051     drawLineForBoxSide(graphicsContext, leftOuter, bottomInner, rightOuter, bottomOuter, BSBottom, outlineColor, outlineStyle, outlineWidth, outlineWidth);
1052
1053     if (useTransparencyLayer)
1054         graphicsContext->endTransparencyLayer();
1055 }
1056
1057 #if PLATFORM(IOS)
1058 // This function is similar in spirit to RenderText::absoluteRectsForRange, but returns rectangles
1059 // which are annotated with additional state which helps iOS draw selections in its unique way.
1060 // No annotations are added in this class.
1061 // FIXME: Move to RenderText with absoluteRectsForRange()?
1062 void RenderObject::collectSelectionRects(Vector<SelectionRect>& rects, unsigned start, unsigned end)
1063 {
1064     Vector<FloatQuad> quads;
1065
1066     if (!firstChildSlow()) {
1067         // FIXME: WebKit's position for an empty span after a BR is incorrect, so we can't trust 
1068         // quads for them. We don't need selection rects for those anyway though, since they 
1069         // are just empty containers. See <https://bugs.webkit.org/show_bug.cgi?id=49358>.
1070         RenderObject* previous = previousSibling();
1071         Node* node = this->node();
1072         if (!previous || !previous->isBR() || !node || !node->isContainerNode() || !isInline()) {
1073             // For inline elements we don't use absoluteQuads, since it takes into account continuations and leads to wrong results.
1074             absoluteQuadsForSelection(quads);
1075         }
1076     } else {
1077         unsigned offset = start;
1078         for (RenderObject* child = childAt(start); child && offset < end; child = child->nextSibling(), ++offset)
1079             child->absoluteQuads(quads);
1080     }
1081
1082     unsigned numberOfQuads = quads.size();
1083     for (unsigned i = 0; i < numberOfQuads; ++i)
1084         rects.append(SelectionRect(quads[i].enclosingBoundingBox(), isHorizontalWritingMode(), view().pageNumberForBlockProgressionOffset(quads[i].enclosingBoundingBox().x())));
1085 }
1086 #endif
1087
1088 IntRect RenderObject::absoluteBoundingBoxRect(bool useTransforms, bool* wasFixed) const
1089 {
1090     if (useTransforms) {
1091         Vector<FloatQuad> quads;
1092         absoluteQuads(quads, wasFixed);
1093
1094         size_t n = quads.size();
1095         if (!n)
1096             return IntRect();
1097     
1098         IntRect result = quads[0].enclosingBoundingBox();
1099         for (size_t i = 1; i < n; ++i)
1100             result.unite(quads[i].enclosingBoundingBox());
1101         return result;
1102     }
1103
1104     FloatPoint absPos = localToAbsolute(FloatPoint(), 0 /* ignore transforms */, wasFixed);
1105     Vector<IntRect> rects;
1106     absoluteRects(rects, flooredLayoutPoint(absPos));
1107
1108     size_t n = rects.size();
1109     if (!n)
1110         return IntRect();
1111
1112     LayoutRect result = rects[0];
1113     for (size_t i = 1; i < n; ++i)
1114         result.unite(rects[i]);
1115     return snappedIntRect(result);
1116 }
1117
1118 void RenderObject::absoluteFocusRingQuads(Vector<FloatQuad>& quads)
1119 {
1120     Vector<IntRect> rects;
1121     // FIXME: addFocusRingRects() needs to be passed this transform-unaware
1122     // localToAbsolute() offset here because RenderInline::addFocusRingRects()
1123     // implicitly assumes that. This doesn't work correctly with transformed
1124     // descendants.
1125     FloatPoint absolutePoint = localToAbsolute();
1126     addFocusRingRects(rects, flooredLayoutPoint(absolutePoint));
1127     size_t count = rects.size();
1128     for (size_t i = 0; i < count; ++i) {
1129         IntRect rect = rects[i];
1130         rect.move(-absolutePoint.x(), -absolutePoint.y());
1131         quads.append(localToAbsoluteQuad(FloatQuad(rect)));
1132     }
1133 }
1134
1135 FloatRect RenderObject::absoluteBoundingBoxRectForRange(const Range* range)
1136 {
1137     if (!range || !range->startContainer())
1138         return FloatRect();
1139
1140     range->ownerDocument().updateLayout();
1141
1142     Vector<FloatQuad> quads;
1143     range->textQuads(quads);
1144
1145     if (quads.isEmpty())
1146         return FloatRect();
1147
1148     FloatRect result = quads[0].boundingBox();
1149     for (size_t i = 1; i < quads.size(); ++i)
1150         result.uniteEvenIfEmpty(quads[i].boundingBox());
1151
1152     return result;
1153 }
1154
1155 void RenderObject::addAbsoluteRectForLayer(LayoutRect& result)
1156 {
1157     if (hasLayer())
1158         result.unite(absoluteBoundingBoxRectIgnoringTransforms());
1159     for (RenderObject* current = firstChildSlow(); current; current = current->nextSibling())
1160         current->addAbsoluteRectForLayer(result);
1161 }
1162
1163 // FIXME: change this to use the subtreePaint terminology
1164 LayoutRect RenderObject::paintingRootRect(LayoutRect& topLevelRect)
1165 {
1166     LayoutRect result = absoluteBoundingBoxRectIgnoringTransforms();
1167     topLevelRect = result;
1168     for (RenderObject* current = firstChildSlow(); current; current = current->nextSibling())
1169         current->addAbsoluteRectForLayer(result);
1170     return result;
1171 }
1172
1173 RenderLayerModelObject* RenderObject::containerForRepaint() const
1174 {
1175     RenderLayerModelObject* repaintContainer = nullptr;
1176
1177     if (view().usesCompositing()) {
1178         if (RenderLayer* parentLayer = enclosingLayer()) {
1179             RenderLayer* compLayer = parentLayer->enclosingCompositingLayerForRepaint();
1180             if (compLayer)
1181                 repaintContainer = &compLayer->renderer();
1182         }
1183     }
1184     if (view().hasSoftwareFilters()) {
1185         if (RenderLayer* parentLayer = enclosingLayer()) {
1186             RenderLayer* enclosingFilterLayer = parentLayer->enclosingFilterLayer();
1187             if (enclosingFilterLayer)
1188                 return &enclosingFilterLayer->renderer();
1189         }
1190     }
1191
1192     // If we have a flow thread, then we need to do individual repaints within the RenderRegions instead.
1193     // Return the flow thread as a repaint container in order to create a chokepoint that allows us to change
1194     // repainting to do individual region repaints.
1195     RenderFlowThread* parentRenderFlowThread = flowThreadContainingBlock();
1196     if (parentRenderFlowThread) {
1197         // If the element has a fixed positioned element with named flow as CB along the CB chain
1198         // then the repaint container is not the flow thread.
1199         if (hasFixedPosInNamedFlowContainingBlock(this))
1200             return repaintContainer;
1201         // If we have already found a repaint container then we will repaint into that container only if it is part of the same
1202         // flow thread. Otherwise we will need to catch the repaint call and send it to the flow thread.
1203         RenderFlowThread* repaintContainerFlowThread = repaintContainer ? repaintContainer->flowThreadContainingBlock() : nullptr;
1204         if (!repaintContainerFlowThread || repaintContainerFlowThread != parentRenderFlowThread)
1205             repaintContainer = parentRenderFlowThread;
1206     }
1207     return repaintContainer;
1208 }
1209
1210 void RenderObject::repaintUsingContainer(const RenderLayerModelObject* repaintContainer, const LayoutRect& r, bool shouldClipToLayer) const
1211 {
1212     if (r.isEmpty())
1213         return;
1214
1215     if (!repaintContainer) {
1216         view().repaintViewRectangle(r);
1217         return;
1218     }
1219
1220     if (is<RenderFlowThread>(*repaintContainer)) {
1221         downcast<RenderFlowThread>(*repaintContainer).repaintRectangleInRegions(r);
1222         return;
1223     }
1224
1225     if (repaintContainer->hasFilter() && repaintContainer->layer() && repaintContainer->layer()->requiresFullLayerImageForFilters()) {
1226         repaintContainer->layer()->setFilterBackendNeedsRepaintingInRect(r);
1227         return;
1228     }
1229
1230     RenderView& v = view();
1231     if (repaintContainer->isRenderView()) {
1232         ASSERT(repaintContainer == &v);
1233         bool viewHasCompositedLayer = v.hasLayer() && v.layer()->isComposited();
1234         if (!viewHasCompositedLayer || v.layer()->backing()->paintsIntoWindow()) {
1235             v.repaintViewRectangle(viewHasCompositedLayer && v.layer()->transform() ? LayoutRect(v.layer()->transform()->mapRect(snapRectToDevicePixels(r, document().deviceScaleFactor()))) : r);
1236             return;
1237         }
1238     }
1239     
1240     if (v.usesCompositing()) {
1241         ASSERT(repaintContainer->hasLayer() && repaintContainer->layer()->isComposited());
1242         repaintContainer->layer()->setBackingNeedsRepaintInRect(r, shouldClipToLayer ? GraphicsLayer::ClipToLayer : GraphicsLayer::DoNotClipToLayer);
1243     }
1244 }
1245
1246 void RenderObject::repaint() const
1247 {
1248     // Don't repaint if we're unrooted (note that view() still returns the view when unrooted)
1249     RenderView* view;
1250     if (!isRooted(&view))
1251         return;
1252
1253     if (view->printing())
1254         return; // Don't repaint if we're printing.
1255
1256     RenderLayerModelObject* repaintContainer = containerForRepaint();
1257     repaintUsingContainer(repaintContainer ? repaintContainer : view, clippedOverflowRectForRepaint(repaintContainer));
1258 }
1259
1260 void RenderObject::repaintRectangle(const LayoutRect& r, bool shouldClipToLayer) const
1261 {
1262     // Don't repaint if we're unrooted (note that view() still returns the view when unrooted)
1263     RenderView* view;
1264     if (!isRooted(&view))
1265         return;
1266
1267     if (view->printing())
1268         return; // Don't repaint if we're printing.
1269
1270     LayoutRect dirtyRect(r);
1271
1272     // FIXME: layoutDelta needs to be applied in parts before/after transforms and
1273     // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
1274     dirtyRect.move(view->layoutDelta());
1275
1276     RenderLayerModelObject* repaintContainer = containerForRepaint();
1277     computeRectForRepaint(repaintContainer, dirtyRect);
1278     repaintUsingContainer(repaintContainer ? repaintContainer : view, dirtyRect, shouldClipToLayer);
1279 }
1280
1281 void RenderObject::repaintSlowRepaintObject() const
1282 {
1283     // Don't repaint if we're unrooted (note that view() still returns the view when unrooted)
1284     RenderView* view;
1285     if (!isRooted(&view))
1286         return;
1287
1288     // Don't repaint if we're printing.
1289     if (view->printing())
1290         return;
1291
1292     RenderLayerModelObject* repaintContainer = containerForRepaint();
1293     if (!repaintContainer)
1294         repaintContainer = view;
1295
1296     bool shouldClipToLayer = true;
1297     IntRect repaintRect;
1298
1299     // If this is the root background, we need to check if there is an extended background rect. If
1300     // there is, then we should not allow painting to clip to the layer size.
1301     if (isRoot() || isBody()) {
1302         shouldClipToLayer = !view->frameView().hasExtendedBackgroundRectForPainting();
1303         repaintRect = snappedIntRect(view->backgroundRect(view));
1304     } else
1305         repaintRect = snappedIntRect(clippedOverflowRectForRepaint(repaintContainer));
1306
1307     repaintUsingContainer(repaintContainer, repaintRect, shouldClipToLayer);
1308 }
1309
1310 IntRect RenderObject::pixelSnappedAbsoluteClippedOverflowRect() const
1311 {
1312     return snappedIntRect(absoluteClippedOverflowRect());
1313 }
1314
1315 bool RenderObject::checkForRepaintDuringLayout() const
1316 {
1317     return !document().view()->needsFullRepaint() && !hasLayer() && everHadLayout();
1318 }
1319
1320 LayoutRect RenderObject::rectWithOutlineForRepaint(const RenderLayerModelObject* repaintContainer, LayoutUnit outlineWidth) const
1321 {
1322     LayoutRect r(clippedOverflowRectForRepaint(repaintContainer));
1323     r.inflate(outlineWidth);
1324     return r;
1325 }
1326
1327 LayoutRect RenderObject::clippedOverflowRectForRepaint(const RenderLayerModelObject*) const
1328 {
1329     ASSERT_NOT_REACHED();
1330     return LayoutRect();
1331 }
1332
1333 void RenderObject::computeRectForRepaint(const RenderLayerModelObject* repaintContainer, LayoutRect& rect, bool fixed) const
1334 {
1335     if (repaintContainer == this)
1336         return;
1337
1338     if (auto* parent = this->parent()) {
1339         if (parent->hasOverflowClip()) {
1340             downcast<RenderBox>(*parent).applyCachedClipAndScrollOffsetForRepaint(rect);
1341             if (rect.isEmpty())
1342                 return;
1343         }
1344
1345         parent->computeRectForRepaint(repaintContainer, rect, fixed);
1346     }
1347 }
1348
1349 void RenderObject::computeFloatRectForRepaint(const RenderLayerModelObject*, FloatRect&, bool) const
1350 {
1351     ASSERT_NOT_REACHED();
1352 }
1353
1354 #if ENABLE(TREE_DEBUGGING)
1355
1356 static void showRenderTreeLegend()
1357 {
1358     fprintf(stderr, "\n(R)elative/A(B)solute/Fi(X)ed/Stick(Y) positioned, (O)verflow clipping, (A)nonymous, (G)enerated, (F)loating, has(L)ayer, (C)omposited, (D)irty layout, Dirty (S)tyle\n");
1359 }
1360
1361 void RenderObject::showNodeTreeForThis() const
1362 {
1363     if (!node())
1364         return;
1365     node()->showTreeForThis();
1366 }
1367
1368 void RenderObject::showRenderTreeForThis() const
1369 {
1370     const WebCore::RenderObject* root = this;
1371     while (root->parent())
1372         root = root->parent();
1373     showRenderTreeLegend();
1374     root->showRenderSubTreeAndMark(this, 1);
1375 }
1376
1377 void RenderObject::showLineTreeForThis() const
1378 {
1379     if (!is<RenderBlockFlow>(*this))
1380         return;
1381     showRenderTreeLegend();
1382     showRenderObject(false, 1);
1383     downcast<RenderBlockFlow>(*this).showLineTreeAndMark(nullptr, 2);
1384 }
1385
1386 static const RenderFlowThread* flowThreadContainingBlockFromRenderer(const RenderObject* renderer)
1387 {
1388     if (!renderer)
1389         return nullptr;
1390
1391     if (renderer->flowThreadState() == RenderObject::NotInsideFlowThread)
1392         return nullptr;
1393
1394     if (is<RenderFlowThread>(*renderer))
1395         return downcast<RenderFlowThread>(renderer);
1396
1397     if (is<RenderBlock>(*renderer))
1398         return downcast<RenderBlock>(*renderer).cachedFlowThreadContainingBlock();
1399
1400     return nullptr;
1401 }
1402
1403 void RenderObject::showRegionsInformation() const
1404 {
1405     const RenderFlowThread* ftcb = flowThreadContainingBlockFromRenderer(this);
1406
1407     if (!ftcb) {
1408         // Only the boxes have region range information.
1409         // Try to get the flow thread containing block information
1410         // from the containing block of this box.
1411         if (is<RenderBox>(*this))
1412             ftcb = flowThreadContainingBlockFromRenderer(containingBlock());
1413     }
1414
1415     if (!ftcb)
1416         return;
1417
1418     RenderRegion* startRegion = nullptr;
1419     RenderRegion* endRegion = nullptr;
1420     ftcb->getRegionRangeForBox(downcast<RenderBox>(this), startRegion, endRegion);
1421     fprintf(stderr, " [Rs:%p Re:%p]", startRegion, endRegion);
1422 }
1423
1424 void RenderObject::showRenderObject(bool mark, int depth) const
1425 {
1426 #if COMPILER(CLANG)
1427 #pragma clang diagnostic push
1428 #pragma clang diagnostic ignored "-Wunknown-pragmas"
1429 #pragma clang diagnostic ignored "-Wundefined-bool-conversion"
1430 #endif
1431     // As this function is intended to be used when debugging, the |this| pointer may be 0.
1432     if (!this) {
1433         fprintf(stderr, "(null)\n");
1434         return;
1435     }
1436 #if COMPILER(CLANG)
1437 #pragma clang diagnostic pop
1438 #endif
1439
1440     if (isPositioned()) {
1441         if (isRelPositioned())
1442             fputc('R', stderr);
1443         else if (isStickyPositioned())
1444             fputc('Y', stderr);
1445         else if (isOutOfFlowPositioned()) {
1446             if (style().position() == AbsolutePosition)
1447                 fputc('B', stderr);
1448             else
1449                 fputc('X', stderr);
1450         }
1451     } else
1452         fputc('-', stderr);
1453
1454     if (hasOverflowClip())
1455         fputc('O', stderr);
1456     else
1457         fputc('-', stderr);
1458
1459     if (isAnonymousBlock())
1460         fputc('A', stderr);
1461     else
1462         fputc('-', stderr);
1463
1464     if (isPseudoElement() || isAnonymous())
1465         fputc('G', stderr);
1466     else
1467         fputc('-', stderr);
1468
1469     if (isFloating())
1470         fputc('F', stderr);
1471     else
1472         fputc('-', stderr);
1473
1474     if (hasLayer())
1475         fputc('L', stderr);
1476     else
1477         fputc('-', stderr);
1478
1479     if (isComposited())
1480         fputc('C', stderr);
1481     else
1482         fputc('-', stderr);
1483
1484     fputc(' ', stderr);
1485
1486     if (needsLayout())
1487         fputc('D', stderr);
1488     else
1489         fputc('-', stderr);
1490
1491     if (node() && node()->needsStyleRecalc())
1492         fputc('S', stderr);
1493     else
1494         fputc('-', stderr);
1495
1496     int printedCharacters = 0;
1497     if (mark) {
1498         fprintf(stderr, "*");
1499         ++printedCharacters;
1500     }
1501
1502     while (++printedCharacters <= depth * 2)
1503         fputc(' ', stderr);
1504
1505     if (node())
1506         fprintf(stderr, "%s ", node()->nodeName().utf8().data());
1507
1508     String name = renderName();
1509     // FIXME: Renderer's name should not include property value listing.
1510     int pos = name.find('(');
1511     if (pos > 0)
1512         fprintf(stderr, "%s", name.left(pos - 1).utf8().data());
1513     else
1514         fprintf(stderr, "%s", name.utf8().data());
1515
1516     if (is<RenderBox>(*this)) {
1517         const auto& box = downcast<RenderBox>(*this);
1518         fprintf(stderr, "  (%.2f, %.2f) (%.2f, %.2f)", box.x().toFloat(), box.y().toFloat(), box.width().toFloat(), box.height().toFloat());
1519     }
1520
1521     fprintf(stderr, " renderer->(%p)", this);
1522     if (node()) {
1523         fprintf(stderr, " node->(%p)", node());
1524         if (node()->isTextNode()) {
1525             String value = node()->nodeValue();
1526             fprintf(stderr, " length->(%u)", value.length());
1527
1528             value.replaceWithLiteral('\\', "\\\\");
1529             value.replaceWithLiteral('\n', "\\n");
1530             
1531             const int maxPrintedLength = 80;
1532             if (value.length() > maxPrintedLength) {
1533                 String substring = value.substring(0, maxPrintedLength);
1534                 fprintf(stderr, " \"%s\"...", substring.utf8().data());
1535             } else
1536                 fprintf(stderr, " \"%s\"", value.utf8().data());
1537         }
1538     }
1539
1540     showRegionsInformation();
1541     fprintf(stderr, "\n");
1542 }
1543
1544 void RenderObject::showRenderSubTreeAndMark(const RenderObject* markedObject, int depth) const
1545 {
1546 #if COMPILER(CLANG)
1547 #pragma clang diagnostic push
1548 #pragma clang diagnostic ignored "-Wunknown-pragmas"
1549 #pragma clang diagnostic ignored "-Wundefined-bool-conversion"
1550 #endif
1551     // As this function is intended to be used when debugging, the |this| pointer may be nullptr.
1552     if (!this)
1553         return;
1554 #if COMPILER(CLANG)
1555 #pragma clang diagnostic pop
1556 #endif
1557
1558     showRenderObject(markedObject == this, depth);
1559     if (is<RenderBlockFlow>(*this))
1560         downcast<RenderBlockFlow>(*this).showLineTreeAndMark(nullptr, depth + 1);
1561
1562     for (const RenderObject* child = firstChildSlow(); child; child = child->nextSibling())
1563         child->showRenderSubTreeAndMark(markedObject, depth + 1);
1564 }
1565
1566 #endif // NDEBUG
1567
1568 SelectionSubtreeRoot& RenderObject::selectionRoot() const
1569 {
1570     RenderFlowThread* flowThread = flowThreadContainingBlock();
1571     if (is<RenderNamedFlowThread>(flowThread))
1572         return downcast<RenderNamedFlowThread>(*flowThread);
1573
1574     return view();
1575 }
1576
1577 void RenderObject::selectionStartEnd(int& spos, int& epos) const
1578 {
1579     selectionRoot().selectionData().selectionStartEndPositions(spos, epos);
1580 }
1581
1582 FloatPoint RenderObject::localToAbsolute(const FloatPoint& localPoint, MapCoordinatesFlags mode, bool* wasFixed) const
1583 {
1584     TransformState transformState(TransformState::ApplyTransformDirection, localPoint);
1585     mapLocalToContainer(nullptr, transformState, mode | ApplyContainerFlip, wasFixed);
1586     transformState.flatten();
1587     
1588     return transformState.lastPlanarPoint();
1589 }
1590
1591 FloatPoint RenderObject::absoluteToLocal(const FloatPoint& containerPoint, MapCoordinatesFlags mode) const
1592 {
1593     TransformState transformState(TransformState::UnapplyInverseTransformDirection, containerPoint);
1594     mapAbsoluteToLocalPoint(mode, transformState);
1595     transformState.flatten();
1596     
1597     return transformState.lastPlanarPoint();
1598 }
1599
1600 FloatQuad RenderObject::absoluteToLocalQuad(const FloatQuad& quad, MapCoordinatesFlags mode) const
1601 {
1602     TransformState transformState(TransformState::UnapplyInverseTransformDirection, quad.boundingBox().center(), quad);
1603     mapAbsoluteToLocalPoint(mode, transformState);
1604     transformState.flatten();
1605     return transformState.lastPlanarQuad();
1606 }
1607
1608 void RenderObject::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags mode, bool* wasFixed) const
1609 {
1610     if (repaintContainer == this)
1611         return;
1612
1613     auto* parent = this->parent();
1614     if (!parent)
1615         return;
1616
1617     // FIXME: this should call offsetFromContainer to share code, but I'm not sure it's ever called.
1618     LayoutPoint centerPoint(transformState.mappedPoint());
1619     if (mode & ApplyContainerFlip && is<RenderBox>(*parent)) {
1620         if (parent->style().isFlippedBlocksWritingMode())
1621             transformState.move(downcast<RenderBox>(parent)->flipForWritingMode(LayoutPoint(transformState.mappedPoint())) - centerPoint);
1622         mode &= ~ApplyContainerFlip;
1623     }
1624
1625     if (is<RenderBox>(*parent))
1626         transformState.move(-downcast<RenderBox>(*parent).scrolledContentOffset());
1627
1628     parent->mapLocalToContainer(repaintContainer, transformState, mode, wasFixed);
1629 }
1630
1631 const RenderObject* RenderObject::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
1632 {
1633     ASSERT_UNUSED(ancestorToStopAt, ancestorToStopAt != this);
1634
1635     auto* container = parent();
1636     if (!container)
1637         return nullptr;
1638
1639     // FIXME: this should call offsetFromContainer to share code, but I'm not sure it's ever called.
1640     LayoutSize offset;
1641     if (is<RenderBox>(*container))
1642         offset = -downcast<RenderBox>(*container).scrolledContentOffset();
1643
1644     geometryMap.push(this, offset, false);
1645     
1646     return container;
1647 }
1648
1649 void RenderObject::mapAbsoluteToLocalPoint(MapCoordinatesFlags mode, TransformState& transformState) const
1650 {
1651     if (auto* parent = this->parent()) {
1652         parent->mapAbsoluteToLocalPoint(mode, transformState);
1653         if (is<RenderBox>(*parent))
1654             transformState.move(downcast<RenderBox>(*parent).scrolledContentOffset());
1655     }
1656 }
1657
1658 bool RenderObject::shouldUseTransformFromContainer(const RenderObject* containerObject) const
1659 {
1660 #if ENABLE(3D_RENDERING)
1661     return hasTransform() || (containerObject && containerObject->style().hasPerspective());
1662 #else
1663     UNUSED_PARAM(containerObject);
1664     return hasTransform();
1665 #endif
1666 }
1667
1668 void RenderObject::getTransformFromContainer(const RenderObject* containerObject, const LayoutSize& offsetInContainer, TransformationMatrix& transform) const
1669 {
1670     transform.makeIdentity();
1671     transform.translate(offsetInContainer.width(), offsetInContainer.height());
1672     RenderLayer* layer;
1673     if (hasLayer() && (layer = downcast<RenderLayerModelObject>(*this).layer()) && layer->transform())
1674         transform.multiply(layer->currentTransform());
1675     
1676 #if ENABLE(3D_RENDERING)
1677     if (containerObject && containerObject->hasLayer() && containerObject->style().hasPerspective()) {
1678         // Perpsective on the container affects us, so we have to factor it in here.
1679         ASSERT(containerObject->hasLayer());
1680         FloatPoint perspectiveOrigin = downcast<RenderLayerModelObject>(*containerObject).layer()->perspectiveOrigin();
1681
1682         TransformationMatrix perspectiveMatrix;
1683         perspectiveMatrix.applyPerspective(containerObject->style().perspective());
1684         
1685         transform.translateRight3d(-perspectiveOrigin.x(), -perspectiveOrigin.y(), 0);
1686         transform = perspectiveMatrix * transform;
1687         transform.translateRight3d(perspectiveOrigin.x(), perspectiveOrigin.y(), 0);
1688     }
1689 #else
1690     UNUSED_PARAM(containerObject);
1691 #endif
1692 }
1693
1694 FloatQuad RenderObject::localToContainerQuad(const FloatQuad& localQuad, const RenderLayerModelObject* repaintContainer, MapCoordinatesFlags mode, bool* wasFixed) const
1695 {
1696     // Track the point at the center of the quad's bounding box. As mapLocalToContainer() calls offsetFromContainer(),
1697     // it will use that point as the reference point to decide which column's transform to apply in multiple-column blocks.
1698     TransformState transformState(TransformState::ApplyTransformDirection, localQuad.boundingBox().center(), localQuad);
1699     mapLocalToContainer(repaintContainer, transformState, mode | ApplyContainerFlip, wasFixed);
1700     transformState.flatten();
1701     
1702     return transformState.lastPlanarQuad();
1703 }
1704
1705 FloatPoint RenderObject::localToContainerPoint(const FloatPoint& localPoint, const RenderLayerModelObject* repaintContainer, MapCoordinatesFlags mode, bool* wasFixed) const
1706 {
1707     TransformState transformState(TransformState::ApplyTransformDirection, localPoint);
1708     mapLocalToContainer(repaintContainer, transformState, mode | ApplyContainerFlip, wasFixed);
1709     transformState.flatten();
1710
1711     return transformState.lastPlanarPoint();
1712 }
1713
1714 LayoutSize RenderObject::offsetFromContainer(RenderElement& container, const LayoutPoint&, bool* offsetDependsOnPoint) const
1715 {
1716     ASSERT(&container == this->container());
1717
1718     LayoutSize offset;
1719     if (is<RenderBox>(container))
1720         offset -= downcast<RenderBox>(container).scrolledContentOffset();
1721
1722     if (offsetDependsOnPoint)
1723         *offsetDependsOnPoint = is<RenderFlowThread>(container);
1724
1725     return offset;
1726 }
1727
1728 LayoutSize RenderObject::offsetFromAncestorContainer(RenderElement& container) const
1729 {
1730     LayoutSize offset;
1731     LayoutPoint referencePoint;
1732     const RenderObject* currContainer = this;
1733     do {
1734         RenderElement* nextContainer = currContainer->container();
1735         ASSERT(nextContainer);  // This means we reached the top without finding container.
1736         if (!nextContainer)
1737             break;
1738         ASSERT(!currContainer->hasTransform());
1739         LayoutSize currentOffset = currContainer->offsetFromContainer(*nextContainer, referencePoint);
1740         offset += currentOffset;
1741         referencePoint.move(currentOffset);
1742         currContainer = nextContainer;
1743     } while (currContainer != &container);
1744
1745     return offset;
1746 }
1747
1748 LayoutRect RenderObject::localCaretRect(InlineBox*, int, LayoutUnit* extraWidthToEndOfLine)
1749 {
1750     if (extraWidthToEndOfLine)
1751         *extraWidthToEndOfLine = 0;
1752
1753     return LayoutRect();
1754 }
1755
1756 bool RenderObject::isRooted(RenderView** view) const
1757 {
1758     const RenderObject* renderer = this;
1759     while (renderer->parent())
1760         renderer = renderer->parent();
1761
1762     if (!is<RenderView>(*renderer))
1763         return false;
1764
1765     if (view)
1766         *view = const_cast<RenderView*>(downcast<RenderView>(renderer));
1767
1768     return true;
1769 }
1770
1771 RespectImageOrientationEnum RenderObject::shouldRespectImageOrientation() const
1772 {
1773 #if USE(CG) || USE(CAIRO)
1774     // This can only be enabled for ports which honor the orientation flag in their drawing code.
1775     if (document().isImageDocument())
1776         return RespectImageOrientation;
1777 #endif
1778     // Respect the image's orientation if it's being used as a full-page image or it's
1779     // an <img> and the setting to respect it everywhere is set.
1780     return (frame().settings().shouldRespectImageOrientation() && is<HTMLImageElement>(node())) ? RespectImageOrientation : DoNotRespectImageOrientation;
1781 }
1782
1783 bool RenderObject::hasOutlineAnnotation() const
1784 {
1785     return node() && node()->isLink() && document().printing();
1786 }
1787
1788 bool RenderObject::hasEntirelyFixedBackground() const
1789 {
1790     return style().hasEntirelyFixedBackground();
1791 }
1792
1793 RenderElement* RenderObject::container(const RenderLayerModelObject* repaintContainer, bool* repaintContainerSkipped) const
1794 {
1795     if (repaintContainerSkipped)
1796         *repaintContainerSkipped = false;
1797
1798     // This method is extremely similar to containingBlock(), but with a few notable
1799     // exceptions.
1800     // (1) It can be used on orphaned subtrees, i.e., it can be called safely even when
1801     // the object is not part of the primary document subtree yet.
1802     // (2) For normal flow elements, it just returns the parent.
1803     // (3) For absolute positioned elements, it will return a relative positioned inline.
1804     // containingBlock() simply skips relpositioned inlines and lets an enclosing block handle
1805     // the layout of the positioned object.  This does mean that computePositionedLogicalWidth and
1806     // computePositionedLogicalHeight have to use container().
1807     auto o = parent();
1808
1809     if (isText())
1810         return o;
1811
1812     EPosition pos = style().position();
1813     if (pos == FixedPosition) {
1814         // container() can be called on an object that is not in the
1815         // tree yet.  We don't call view() since it will assert if it
1816         // can't get back to the canvas.  Instead we just walk as high up
1817         // as we can.  If we're in the tree, we'll get the root.  If we
1818         // aren't we'll get the root of our little subtree (most likely
1819         // we'll just return nullptr).
1820         // FIXME: The definition of view() has changed to not crawl up the render tree.  It might
1821         // be safe now to use it.
1822         // FIXME: share code with containingBlockForFixedPosition().
1823         while (o && o->parent() && !(o->hasTransform() && o->isRenderBlock())) {
1824             // foreignObject is the containing block for its contents.
1825             if (o->isSVGForeignObject())
1826                 break;
1827
1828             // The render flow thread is the top most containing block
1829             // for the fixed positioned elements.
1830             if (o->isOutOfFlowRenderFlowThread())
1831                 break;
1832
1833             if (repaintContainerSkipped && o == repaintContainer)
1834                 *repaintContainerSkipped = true;
1835
1836             o = o->parent();
1837         }
1838     } else if (pos == AbsolutePosition) {
1839         // Same goes here.  We technically just want our containing block, but
1840         // we may not have one if we're part of an uninstalled subtree.  We'll
1841         // climb as high as we can though.
1842         // FIXME: share code with isContainingBlockCandidateForAbsolutelyPositionedObject().
1843         // FIXME: hasTransformRelatedProperty() includes preserves3D() check, but this may need to change: https://www.w3.org/Bugs/Public/show_bug.cgi?id=27566
1844         while (o && o->style().position() == StaticPosition && !o->isRenderView() && !(o->hasTransformRelatedProperty() && o->isRenderBlock())) {
1845             if (o->isSVGForeignObject()) // foreignObject is the containing block for contents inside it
1846                 break;
1847
1848             if (repaintContainerSkipped && o == repaintContainer)
1849                 *repaintContainerSkipped = true;
1850
1851             o = o->parent();
1852         }
1853     }
1854
1855     return o;
1856 }
1857
1858 bool RenderObject::isSelectionBorder() const
1859 {
1860     SelectionState st = selectionState();
1861     return st == SelectionStart
1862         || st == SelectionEnd
1863         || st == SelectionBoth
1864         || view().selectionUnsplitStart() == this
1865         || view().selectionUnsplitEnd() == this;
1866 }
1867
1868 inline void RenderObject::clearLayoutRootIfNeeded() const
1869 {
1870     if (documentBeingDestroyed())
1871         return;
1872
1873     if (view().frameView().layoutRoot() == this) {
1874         ASSERT_NOT_REACHED();
1875         // This indicates a failure to layout the child, which is why
1876         // the layout root is still set to |this|. Make sure to clear it
1877         // since we are getting destroyed.
1878         view().frameView().clearLayoutRoot();
1879     }
1880 }
1881
1882 void RenderObject::willBeDestroyed()
1883 {
1884     // For accessibility management, notify the parent of the imminent change to its child set.
1885     // We do it now, before remove(), while the parent pointer is still available.
1886     if (AXObjectCache* cache = document().existingAXObjectCache())
1887         cache->childrenChanged(this->parent());
1888
1889     removeFromParent();
1890
1891     ASSERT(documentBeingDestroyed() || !is<RenderElement>(*this) || !view().frameView().hasSlowRepaintObject(downcast<RenderElement>(this)));
1892
1893     // The remove() call above may invoke axObjectCache()->childrenChanged() on the parent, which may require the AX render
1894     // object for this renderer. So we remove the AX render object now, after the renderer is removed.
1895     if (AXObjectCache* cache = document().existingAXObjectCache())
1896         cache->remove(this);
1897
1898     // FIXME: Would like to do this in RenderBoxModelObject, but the timing is so complicated that this can't easily
1899     // be moved into RenderBoxModelObject::destroy.
1900     if (hasLayer()) {
1901         setHasLayer(false);
1902         downcast<RenderLayerModelObject>(*this).destroyLayer();
1903     }
1904
1905     clearLayoutRootIfNeeded();
1906 }
1907
1908 void RenderObject::insertedIntoTree()
1909 {
1910     // FIXME: We should ASSERT(isRooted()) here but generated content makes some out-of-order insertion.
1911
1912     if (!isFloating() && parent()->childrenInline())
1913         parent()->dirtyLinesFromChangedChild(*this);
1914
1915     if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1916         flowThread->flowThreadDescendantInserted(this);
1917 }
1918
1919 void RenderObject::willBeRemovedFromTree()
1920 {
1921     // FIXME: We should ASSERT(isRooted()) but we have some out-of-order removals which would need to be fixed first.
1922
1923     removeFromRenderFlowThread();
1924
1925     // Update cached boundaries in SVG renderers, if a child is removed.
1926     parent()->setNeedsBoundariesUpdate();
1927 }
1928
1929 void RenderObject::removeFromRenderFlowThread()
1930 {
1931     if (flowThreadState() == NotInsideFlowThread)
1932         return;
1933
1934     // Sometimes we remove the element from the flow, but it's not destroyed at that time.
1935     // It's only until later when we actually destroy it and remove all the children from it.
1936     // Currently, that happens for firstLetter elements and list markers.
1937     // Pass in the flow thread so that we don't have to look it up for all the children.
1938     removeFromRenderFlowThreadIncludingDescendants(true);
1939 }
1940
1941 void RenderObject::removeFromRenderFlowThreadIncludingDescendants(bool shouldUpdateState)
1942 {
1943     // Once we reach another flow thread we don't need to update the flow thread state
1944     // but we have to continue cleanup the flow thread info.
1945     if (isRenderFlowThread())
1946         shouldUpdateState = false;
1947
1948     for (RenderObject* child = firstChildSlow(); child; child = child->nextSibling())
1949         child->removeFromRenderFlowThreadIncludingDescendants(shouldUpdateState);
1950
1951     // We have to ask for our containing flow thread as it may be above the removed sub-tree.
1952     RenderFlowThread* flowThreadContainingBlock = this->flowThreadContainingBlock();
1953     while (flowThreadContainingBlock) {
1954         flowThreadContainingBlock->removeFlowChildInfo(this);
1955         if (flowThreadContainingBlock->flowThreadState() == NotInsideFlowThread)
1956             break;
1957         RenderObject* parent = flowThreadContainingBlock->parent();
1958         if (!parent)
1959             break;
1960         flowThreadContainingBlock = parent->flowThreadContainingBlock();
1961     }
1962     if (is<RenderBlock>(*this))
1963         downcast<RenderBlock>(*this).setCachedFlowThreadContainingBlockNeedsUpdate();
1964
1965     if (shouldUpdateState)
1966         setFlowThreadState(NotInsideFlowThread);
1967 }
1968
1969 void RenderObject::invalidateFlowThreadContainingBlockIncludingDescendants(RenderFlowThread* flowThread)
1970 {
1971     if (flowThreadState() == NotInsideFlowThread)
1972         return;
1973
1974     if (is<RenderBlock>(*this)) {
1975         RenderBlock& block = downcast<RenderBlock>(*this);
1976
1977         if (block.cachedFlowThreadContainingBlockNeedsUpdate())
1978             return;
1979
1980         flowThread = block.cachedFlowThreadContainingBlock();
1981         block.setCachedFlowThreadContainingBlockNeedsUpdate();
1982     }
1983
1984     if (flowThread)
1985         flowThread->removeFlowChildInfo(this);
1986
1987     for (RenderObject* child = firstChildSlow(); child; child = child->nextSibling())
1988         child->invalidateFlowThreadContainingBlockIncludingDescendants(flowThread);
1989 }
1990
1991 void RenderObject::destroyAndCleanupAnonymousWrappers()
1992 {
1993     // If the tree is destroyed, there is no need for a clean-up phase.
1994     if (documentBeingDestroyed()) {
1995         destroy();
1996         return;
1997     }
1998
1999     RenderObject* destroyRoot = this;
2000     for (auto destroyRootParent = destroyRoot->parent(); destroyRootParent && destroyRootParent->isAnonymous(); destroyRoot = destroyRootParent, destroyRootParent = destroyRootParent->parent()) {
2001         // Currently we only remove anonymous cells' and table sections' wrappers but we should remove all unneeded
2002         // wrappers. See http://webkit.org/b/52123 as an example where this is needed.
2003         if (!destroyRootParent->isTableCell() && !destroyRootParent->isTableSection())
2004             break;
2005
2006         if (destroyRootParent->firstChild() != this || destroyRootParent->lastChild() != this)
2007             break;
2008     }
2009
2010     destroyRoot->destroy();
2011
2012     // WARNING: |this| is deleted here.
2013 }
2014
2015 void RenderObject::destroy()
2016 {
2017     m_bitfields.setBeingDestroyed(true);
2018
2019 #if PLATFORM(IOS)
2020     if (hasLayer())
2021         downcast<RenderBoxModelObject>(*this).layer()->willBeDestroyed();
2022 #endif
2023
2024     willBeDestroyed();
2025     delete this;
2026 }
2027
2028 VisiblePosition RenderObject::positionForPoint(const LayoutPoint&, const RenderRegion*)
2029 {
2030     return createVisiblePosition(caretMinOffset(), DOWNSTREAM);
2031 }
2032
2033 void RenderObject::updateDragState(bool dragOn)
2034 {
2035     bool valueChanged = (dragOn != isDragging());
2036     setIsDragging(dragOn);
2037     if (valueChanged && node() && (style().affectedByDrag() || (is<Element>(*node()) && downcast<Element>(*node()).childrenAffectedByDrag())))
2038         node()->setNeedsStyleRecalc();
2039     for (RenderObject* curr = firstChildSlow(); curr; curr = curr->nextSibling())
2040         curr->updateDragState(dragOn);
2041 }
2042
2043 bool RenderObject::isComposited() const
2044 {
2045     return hasLayer() && downcast<RenderLayerModelObject>(*this).layer()->isComposited();
2046 }
2047
2048 bool RenderObject::hitTest(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestFilter hitTestFilter)
2049 {
2050     bool inside = false;
2051     if (hitTestFilter != HitTestSelf) {
2052         // First test the foreground layer (lines and inlines).
2053         inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestForeground);
2054
2055         // Test floats next.
2056         if (!inside)
2057             inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestFloat);
2058
2059         // Finally test to see if the mouse is in the background (within a child block's background).
2060         if (!inside)
2061             inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestChildBlockBackgrounds);
2062     }
2063
2064     // See if the mouse is inside us but not any of our descendants
2065     if (hitTestFilter != HitTestDescendants && !inside)
2066         inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestBlockBackground);
2067
2068     return inside;
2069 }
2070
2071 void RenderObject::updateHitTestResult(HitTestResult& result, const LayoutPoint& point)
2072 {
2073     if (result.innerNode())
2074         return;
2075
2076     Node* node = this->node();
2077
2078     // If we hit the anonymous renderers inside generated content we should
2079     // actually hit the generated content so walk up to the PseudoElement.
2080     if (!node && parent() && parent()->isBeforeOrAfterContent()) {
2081         for (auto renderer = parent(); renderer && !node; renderer = renderer->parent())
2082             node = renderer->element();
2083     }
2084
2085     if (node) {
2086         result.setInnerNode(node);
2087         if (!result.innerNonSharedNode())
2088             result.setInnerNonSharedNode(node);
2089         result.setLocalPoint(point);
2090     }
2091 }
2092
2093 bool RenderObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& /*locationInContainer*/, const LayoutPoint& /*accumulatedOffset*/, HitTestAction)
2094 {
2095     return false;
2096 }
2097
2098 int RenderObject::innerLineHeight() const
2099 {
2100     return style().computedLineHeight();
2101 }
2102
2103 static Color decorationColor(RenderStyle* style)
2104 {
2105     Color result;
2106     // Check for text decoration color first.
2107     result = style->visitedDependentColor(CSSPropertyWebkitTextDecorationColor);
2108     if (result.isValid())
2109         return result;
2110     if (style->textStrokeWidth() > 0) {
2111         // Prefer stroke color if possible but not if it's fully transparent.
2112         result = style->visitedDependentColor(CSSPropertyWebkitTextStrokeColor);
2113         if (result.alpha())
2114             return result;
2115     }
2116     
2117     result = style->visitedDependentColor(CSSPropertyWebkitTextFillColor);
2118     return result;
2119 }
2120
2121 void RenderObject::getTextDecorationColorsAndStyles(int decorations, Color& underlineColor, Color& overlineColor, Color& linethroughColor,
2122     TextDecorationStyle& underlineStyle, TextDecorationStyle& overlineStyle, TextDecorationStyle& linethroughStyle, bool firstlineStyle)
2123 {
2124     RenderObject* current = this;
2125     RenderStyle* styleToUse = nullptr;
2126     TextDecoration currDecs = TextDecorationNone;
2127     Color resultColor;
2128     do {
2129         styleToUse = firstlineStyle ? &current->firstLineStyle() : &current->style();
2130         currDecs = styleToUse->textDecoration();
2131         resultColor = decorationColor(styleToUse);
2132         // Parameter 'decorations' is cast as an int to enable the bitwise operations below.
2133         if (currDecs) {
2134             if (currDecs & TextDecorationUnderline) {
2135                 decorations &= ~TextDecorationUnderline;
2136                 underlineColor = resultColor;
2137                 underlineStyle = styleToUse->textDecorationStyle();
2138             }
2139             if (currDecs & TextDecorationOverline) {
2140                 decorations &= ~TextDecorationOverline;
2141                 overlineColor = resultColor;
2142                 overlineStyle = styleToUse->textDecorationStyle();
2143             }
2144             if (currDecs & TextDecorationLineThrough) {
2145                 decorations &= ~TextDecorationLineThrough;
2146                 linethroughColor = resultColor;
2147                 linethroughStyle = styleToUse->textDecorationStyle();
2148             }
2149         }
2150         if (current->isRubyText())
2151             return;
2152         current = current->parent();
2153         if (current && current->isAnonymousBlock() && downcast<RenderBlock>(*current).continuation())
2154             current = downcast<RenderBlock>(*current).continuation();
2155     } while (current && decorations && (!current->node() || (!is<HTMLAnchorElement>(*current->node()) && !current->node()->hasTagName(fontTag))));
2156
2157     // If we bailed out, use the element we bailed out at (typically a <font> or <a> element).
2158     if (decorations && current) {
2159         styleToUse = firstlineStyle ? &current->firstLineStyle() : &current->style();
2160         resultColor = decorationColor(styleToUse);
2161         if (decorations & TextDecorationUnderline) {
2162             underlineColor = resultColor;
2163             underlineStyle = styleToUse->textDecorationStyle();
2164         }
2165         if (decorations & TextDecorationOverline) {
2166             overlineColor = resultColor;
2167             overlineStyle = styleToUse->textDecorationStyle();
2168         }
2169         if (decorations & TextDecorationLineThrough) {
2170             linethroughColor = resultColor;
2171             linethroughStyle = styleToUse->textDecorationStyle();
2172         }
2173     }
2174 }
2175
2176 #if ENABLE(DASHBOARD_SUPPORT)
2177 void RenderObject::addAnnotatedRegions(Vector<AnnotatedRegionValue>& regions)
2178 {
2179     // Convert the style regions to absolute coordinates.
2180     if (style().visibility() != VISIBLE || !is<RenderBox>(*this))
2181         return;
2182     
2183     auto& box = downcast<RenderBox>(*this);
2184     FloatPoint absPos = localToAbsolute();
2185
2186     const Vector<StyleDashboardRegion>& styleRegions = style().dashboardRegions();
2187     for (const auto& styleRegion : styleRegions) {
2188         LayoutUnit w = box.width();
2189         LayoutUnit h = box.height();
2190
2191         AnnotatedRegionValue region;
2192         region.label = styleRegion.label;
2193         region.bounds = LayoutRect(styleRegion.offset.left().value(),
2194                                    styleRegion.offset.top().value(),
2195                                    w - styleRegion.offset.left().value() - styleRegion.offset.right().value(),
2196                                    h - styleRegion.offset.top().value() - styleRegion.offset.bottom().value());
2197         region.type = styleRegion.type;
2198
2199         region.clip = region.bounds;
2200         computeAbsoluteRepaintRect(region.clip);
2201         if (region.clip.height() < 0) {
2202             region.clip.setHeight(0);
2203             region.clip.setWidth(0);
2204         }
2205
2206         region.bounds.setX(absPos.x() + styleRegion.offset.left().value());
2207         region.bounds.setY(absPos.y() + styleRegion.offset.top().value());
2208
2209         regions.append(region);
2210     }
2211 }
2212
2213 void RenderObject::collectAnnotatedRegions(Vector<AnnotatedRegionValue>& regions)
2214 {
2215     // RenderTexts don't have their own style, they just use their parent's style,
2216     // so we don't want to include them.
2217     if (is<RenderText>(*this))
2218         return;
2219
2220     addAnnotatedRegions(regions);
2221     for (RenderObject* current = downcast<RenderElement>(*this).firstChild(); current; current = current->nextSibling())
2222         current->collectAnnotatedRegions(regions);
2223 }
2224 #endif
2225
2226 int RenderObject::maximalOutlineSize(PaintPhase p) const
2227 {
2228     if (p != PaintPhaseOutline && p != PaintPhaseSelfOutline && p != PaintPhaseChildOutlines)
2229         return 0;
2230     return view().maximalOutlineSize();
2231 }
2232
2233 int RenderObject::caretMinOffset() const
2234 {
2235     return 0;
2236 }
2237
2238 int RenderObject::caretMaxOffset() const
2239 {
2240     if (isReplaced())
2241         return node() ? std::max(1U, node()->countChildNodes()) : 1;
2242     if (isHR())
2243         return 1;
2244     return 0;
2245 }
2246
2247 int RenderObject::previousOffset(int current) const
2248 {
2249     return current - 1;
2250 }
2251
2252 int RenderObject::previousOffsetForBackwardDeletion(int current) const
2253 {
2254     return current - 1;
2255 }
2256
2257 int RenderObject::nextOffset(int current) const
2258 {
2259     return current + 1;
2260 }
2261
2262 void RenderObject::adjustRectForOutlineAndShadow(LayoutRect& rect) const
2263 {
2264     int outlineSize = outlineStyleForRepaint().outlineSize();
2265     if (const ShadowData* boxShadow = style().boxShadow()) {
2266         boxShadow->adjustRectForShadow(rect, outlineSize);
2267         return;
2268     }
2269
2270     rect.inflate(outlineSize);
2271 }
2272
2273 void RenderObject::imageChanged(CachedImage* image, const IntRect* rect)
2274 {
2275     imageChanged(static_cast<WrappedImagePtr>(image), rect);
2276 }
2277
2278 RenderBoxModelObject* RenderObject::offsetParent() const
2279 {
2280     // If any of the following holds true return null and stop this algorithm:
2281     // A is the root element.
2282     // A is the HTML body element.
2283     // The computed value of the position property for element A is fixed.
2284     if (isRoot() || isBody() || (isOutOfFlowPositioned() && style().position() == FixedPosition))
2285         return nullptr;
2286
2287     // If A is an area HTML element which has a map HTML element somewhere in the ancestor
2288     // chain return the nearest ancestor map HTML element and stop this algorithm.
2289     // FIXME: Implement!
2290     
2291     // Return the nearest ancestor element of A for which at least one of the following is
2292     // true and stop this algorithm if such an ancestor is found:
2293     //     * The computed value of the position property is not static.
2294     //     * It is the HTML body element.
2295     //     * The computed value of the position property of A is static and the ancestor
2296     //       is one of the following HTML elements: td, th, or table.
2297     //     * Our own extension: if there is a difference in the effective zoom
2298
2299     bool skipTables = isPositioned();
2300     float currZoom = style().effectiveZoom();
2301     auto current = parent();
2302     while (current && (!current->element() || (!current->isPositioned() && !current->isBody())) && !is<RenderNamedFlowThread>(*current)) {
2303         Element* element = current->element();
2304         if (!skipTables && element && (is<HTMLTableElement>(*element) || is<HTMLTableCellElement>(*element)))
2305             break;
2306  
2307         float newZoom = current->style().effectiveZoom();
2308         if (currZoom != newZoom)
2309             break;
2310         currZoom = newZoom;
2311         current = current->parent();
2312     }
2313     
2314     // CSS regions specification says that region flows should return the body element as their offsetParent.
2315     if (is<RenderNamedFlowThread>(current)) {
2316         auto* body = document().bodyOrFrameset();
2317         current = body ? body->renderer() : nullptr;
2318     }
2319     
2320     return is<RenderBoxModelObject>(current) ? downcast<RenderBoxModelObject>(current) : nullptr;
2321 }
2322
2323 VisiblePosition RenderObject::createVisiblePosition(int offset, EAffinity affinity) const
2324 {
2325     // If this is a non-anonymous renderer in an editable area, then it's simple.
2326     if (Node* node = nonPseudoNode()) {
2327         if (!node->hasEditableStyle()) {
2328             // If it can be found, we prefer a visually equivalent position that is editable. 
2329             Position position = createLegacyEditingPosition(node, offset);
2330             Position candidate = position.downstream(CanCrossEditingBoundary);
2331             if (candidate.deprecatedNode()->hasEditableStyle())
2332                 return VisiblePosition(candidate, affinity);
2333             candidate = position.upstream(CanCrossEditingBoundary);
2334             if (candidate.deprecatedNode()->hasEditableStyle())
2335                 return VisiblePosition(candidate, affinity);
2336         }
2337         // FIXME: Eliminate legacy editing positions
2338         return VisiblePosition(createLegacyEditingPosition(node, offset), affinity);
2339     }
2340
2341     // We don't want to cross the boundary between editable and non-editable
2342     // regions of the document, but that is either impossible or at least
2343     // extremely unlikely in any normal case because we stop as soon as we
2344     // find a single non-anonymous renderer.
2345
2346     // Find a nearby non-anonymous renderer.
2347     const RenderObject* child = this;
2348     while (const auto parent = child->parent()) {
2349         // Find non-anonymous content after.
2350         const RenderObject* renderer = child;
2351         while ((renderer = renderer->nextInPreOrder(parent))) {
2352             if (Node* node = renderer->nonPseudoNode())
2353                 return VisiblePosition(firstPositionInOrBeforeNode(node), DOWNSTREAM);
2354         }
2355
2356         // Find non-anonymous content before.
2357         renderer = child;
2358         while ((renderer = renderer->previousInPreOrder())) {
2359             if (renderer == parent)
2360                 break;
2361             if (Node* node = renderer->nonPseudoNode())
2362                 return VisiblePosition(lastPositionInOrAfterNode(node), DOWNSTREAM);
2363         }
2364
2365         // Use the parent itself unless it too is anonymous.
2366         if (Element* element = parent->nonPseudoElement())
2367             return VisiblePosition(firstPositionInOrBeforeNode(element), DOWNSTREAM);
2368
2369         // Repeat at the next level up.
2370         child = parent;
2371     }
2372
2373     // Everything was anonymous. Give up.
2374     return VisiblePosition();
2375 }
2376
2377 VisiblePosition RenderObject::createVisiblePosition(const Position& position) const
2378 {
2379     if (position.isNotNull())
2380         return VisiblePosition(position);
2381
2382     ASSERT(!node());
2383     return createVisiblePosition(0, DOWNSTREAM);
2384 }
2385
2386 CursorDirective RenderObject::getCursor(const LayoutPoint&, Cursor&) const
2387 {
2388     return SetCursorBasedOnStyle;
2389 }
2390
2391 bool RenderObject::canUpdateSelectionOnRootLineBoxes()
2392 {
2393     if (needsLayout())
2394         return false;
2395
2396     RenderBlock* containingBlock = this->containingBlock();
2397     return containingBlock ? !containingBlock->needsLayout() : true;
2398 }
2399
2400 // We only create "generated" child renderers like one for first-letter if:
2401 // - the firstLetterBlock can have children in the DOM and
2402 // - the block doesn't have any special assumption on its text children.
2403 // This correctly prevents form controls from having such renderers.
2404 bool RenderObject::canHaveGeneratedChildren() const
2405 {
2406     return canHaveChildren();
2407 }
2408
2409 Node* RenderObject::generatingPseudoHostElement() const
2410 {
2411     return downcast<PseudoElement>(*node()).hostElement();
2412 }
2413
2414 void RenderObject::setNeedsBoundariesUpdate()
2415 {
2416     if (auto renderer = parent())
2417         renderer->setNeedsBoundariesUpdate();
2418 }
2419
2420 FloatRect RenderObject::objectBoundingBox() const
2421 {
2422     ASSERT_NOT_REACHED();
2423     return FloatRect();
2424 }
2425
2426 FloatRect RenderObject::strokeBoundingBox() const
2427 {
2428     ASSERT_NOT_REACHED();
2429     return FloatRect();
2430 }
2431
2432 // Returns the smallest rectangle enclosing all of the painted content
2433 // respecting clipping, masking, filters, opacity, stroke-width and markers
2434 FloatRect RenderObject::repaintRectInLocalCoordinates() const
2435 {
2436     ASSERT_NOT_REACHED();
2437     return FloatRect();
2438 }
2439
2440 AffineTransform RenderObject::localTransform() const
2441 {
2442     static const AffineTransform identity;
2443     return identity;
2444 }
2445
2446 const AffineTransform& RenderObject::localToParentTransform() const
2447 {
2448     static const AffineTransform identity;
2449     return identity;
2450 }
2451
2452 bool RenderObject::nodeAtFloatPoint(const HitTestRequest&, HitTestResult&, const FloatPoint&, HitTestAction)
2453 {
2454     ASSERT_NOT_REACHED();
2455     return false;
2456 }
2457
2458 RenderNamedFlowFragment* RenderObject::currentRenderNamedFlowFragment() const
2459 {
2460     RenderFlowThread* flowThread = flowThreadContainingBlock();
2461     if (!is<RenderNamedFlowThread>(flowThread))
2462         return nullptr;
2463
2464     // FIXME: Once regions are fully integrated with the compositing system we should uncomment this assert.
2465     // This assert needs to be disabled because it's possible to ask for the ancestor clipping rectangle of
2466     // a layer without knowing the containing region in advance.
2467     // ASSERT(flowThread->currentRegion() && flowThread->currentRegion()->isRenderNamedFlowFragment());
2468
2469     return downcast<RenderNamedFlowFragment>(flowThread->currentRegion());
2470 }
2471
2472 RenderFlowThread* RenderObject::locateFlowThreadContainingBlock() const
2473 {
2474     RenderBlock* containingBlock = this->containingBlock();
2475     return containingBlock ? containingBlock->flowThreadContainingBlock() : nullptr;
2476 }
2477
2478 void RenderObject::calculateBorderStyleColor(const EBorderStyle& style, const BoxSide& side, Color& color)
2479 {
2480     ASSERT(style == INSET || style == OUTSET);
2481     // This values were derived empirically.
2482     const RGBA32 baseDarkColor = 0xFF202020;
2483     const RGBA32 baseLightColor = 0xFFEBEBEB;
2484     enum Operation { Darken, Lighten };
2485
2486     Operation operation = (side == BSTop || side == BSLeft) == (style == INSET) ? Darken : Lighten;
2487
2488     // Here we will darken the border decoration color when needed. This will yield a similar behavior as in FF.
2489     if (operation == Darken) {
2490         if (differenceSquared(color, Color::black) > differenceSquared(baseDarkColor, Color::black))
2491             color = color.dark();
2492     } else {
2493         if (differenceSquared(color, Color::white) > differenceSquared(baseLightColor, Color::white))
2494             color = color.light();
2495     }
2496 }
2497
2498 } // namespace WebCore
2499
2500 #if ENABLE(TREE_DEBUGGING)
2501
2502 void showNodeTree(const WebCore::RenderObject* object)
2503 {
2504     if (!object)
2505         return;
2506     object->showNodeTreeForThis();
2507 }
2508
2509 void showLineTree(const WebCore::RenderObject* object)
2510 {
2511     if (!object)
2512         return;
2513     object->showLineTreeForThis();
2514 }
2515
2516 void showRenderTree(const WebCore::RenderObject* object)
2517 {
2518     if (!object)
2519         return;
2520     object->showRenderTreeForThis();
2521 }
2522
2523 #endif