Rename feature define ENABLE_WIDGET_REGION to ENABLE_DRAGGBALE_REGION
[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 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 "Chrome.h"
32 #include "ContentData.h"
33 #include "CursorList.h"
34 #include "DashArray.h"
35 #include "EditingBoundary.h"
36 #include "FloatQuad.h"
37 #include "FlowThreadController.h"
38 #include "Frame.h"
39 #include "FrameView.h"
40 #include "GraphicsContext.h"
41 #include "HTMLElement.h"
42 #include "HTMLNames.h"
43 #include "HitTestResult.h"
44 #include "Page.h"
45 #include "RenderArena.h"
46 #include "RenderCounter.h"
47 #include "RenderDeprecatedFlexibleBox.h"
48 #include "RenderFlexibleBox.h"
49 #include "RenderGeometryMap.h"
50 #include "RenderGrid.h"
51 #include "RenderImage.h"
52 #include "RenderImageResourceStyleImage.h"
53 #include "RenderInline.h"
54 #include "RenderLayer.h"
55 #include "RenderListItem.h"
56 #include "RenderMultiColumnBlock.h"
57 #include "RenderNamedFlowThread.h"
58 #include "RenderRegion.h"
59 #include "RenderRuby.h"
60 #include "RenderRubyText.h"
61 #include "RenderScrollbarPart.h"
62 #include "RenderTableCaption.h"
63 #include "RenderTableCell.h"
64 #include "RenderTableCol.h"
65 #include "RenderTableRow.h"
66 #include "RenderTheme.h"
67 #include "RenderView.h"
68 #include "Settings.h"
69 #include "StyleResolver.h"
70 #include "TransformState.h"
71 #include "htmlediting.h"
72 #include <algorithm>
73 #include <stdio.h>
74 #include <wtf/RefCountedLeakCounter.h>
75 #include <wtf/UnusedParam.h>
76
77 #if USE(ACCELERATED_COMPOSITING)
78 #include "RenderLayerCompositor.h"
79 #endif
80
81 #if ENABLE(SVG)
82 #include "RenderSVGResourceContainer.h"
83 #include "SVGRenderSupport.h"
84 #endif
85
86 using namespace std;
87
88 namespace WebCore {
89
90 using namespace HTMLNames;
91
92 #ifndef NDEBUG
93 static void* baseOfRenderObjectBeingDeleted;
94 #endif
95
96 struct SameSizeAsRenderObject {
97     virtual ~SameSizeAsRenderObject() { } // Allocate vtable pointer.
98     void* pointers[5];
99 #ifndef NDEBUG
100     unsigned m_debugBitfields : 2;
101 #endif
102     unsigned m_bitfields;
103 };
104
105 COMPILE_ASSERT(sizeof(RenderObject) == sizeof(SameSizeAsRenderObject), RenderObject_should_stay_small);
106
107 bool RenderObject::s_affectsParentBlock = false;
108
109 RenderObjectAncestorLineboxDirtySet* RenderObject::s_ancestorLineboxDirtySet = 0;
110
111 void* RenderObject::operator new(size_t sz, RenderArena* renderArena)
112 {
113     return renderArena->allocate(sz);
114 }
115
116 void RenderObject::operator delete(void* ptr, size_t sz)
117 {
118     ASSERT(baseOfRenderObjectBeingDeleted == ptr);
119
120     // Stash size where destroy can find it.
121     *(size_t *)ptr = sz;
122 }
123
124 RenderObject* RenderObject::createObject(Node* node, RenderStyle* style)
125 {
126     Document* doc = node->document();
127     RenderArena* arena = doc->renderArena();
128
129     // Minimal support for content properties replacing an entire element.
130     // Works only if we have exactly one piece of content and it's a URL.
131     // Otherwise acts as if we didn't support this feature.
132     const ContentData* contentData = style->contentData();
133     if (contentData && !contentData->next() && contentData->isImage() && doc != node) {
134         RenderImage* image = new (arena) RenderImage(node);
135         // RenderImageResourceStyleImage requires a style being present on the image but we don't want to
136         // trigger a style change now as the node is not fully attached. Moving this code to style change
137         // doesn't make sense as it should be run once at renderer creation.
138         image->m_style = style;
139         if (const StyleImage* styleImage = static_cast<const ImageContentData*>(contentData)->image()) {
140             image->setImageResource(RenderImageResourceStyleImage::create(const_cast<StyleImage*>(styleImage)));
141             image->setIsGeneratedContent();
142         } else
143             image->setImageResource(RenderImageResource::create());
144         image->m_style = 0;
145         return image;
146     }
147
148     if (node->hasTagName(rubyTag)) {
149         if (style->display() == INLINE)
150             return new (arena) RenderRubyAsInline(node);
151         else if (style->display() == BLOCK)
152             return new (arena) RenderRubyAsBlock(node);
153     }
154     // treat <rt> as ruby text ONLY if it still has its default treatment of block
155     if (node->hasTagName(rtTag) && style->display() == BLOCK)
156         return new (arena) RenderRubyText(node);
157     if (doc->cssRegionsEnabled() && style->isDisplayRegionType() && !style->regionThread().isEmpty() && doc->renderView())
158         return new (arena) RenderRegion(node, 0);
159     switch (style->display()) {
160     case NONE:
161         return 0;
162     case INLINE:
163         return new (arena) RenderInline(node);
164     case BLOCK:
165     case INLINE_BLOCK:
166     case RUN_IN:
167     case COMPACT:
168         if ((!style->hasAutoColumnCount() || !style->hasAutoColumnWidth()) && doc->regionBasedColumnsEnabled())
169             return new (arena) RenderMultiColumnBlock(node);
170         return new (arena) RenderBlock(node);
171     case LIST_ITEM:
172         return new (arena) RenderListItem(node);
173     case TABLE:
174     case INLINE_TABLE:
175         return new (arena) RenderTable(node);
176     case TABLE_ROW_GROUP:
177     case TABLE_HEADER_GROUP:
178     case TABLE_FOOTER_GROUP:
179         return new (arena) RenderTableSection(node);
180     case TABLE_ROW:
181         return new (arena) RenderTableRow(node);
182     case TABLE_COLUMN_GROUP:
183     case TABLE_COLUMN:
184         return new (arena) RenderTableCol(node);
185     case TABLE_CELL:
186         return new (arena) RenderTableCell(node);
187     case TABLE_CAPTION:
188         return new (arena) RenderTableCaption(node);
189     case BOX:
190     case INLINE_BOX:
191         return new (arena) RenderDeprecatedFlexibleBox(node);
192     case FLEX:
193     case INLINE_FLEX:
194         return new (arena) RenderFlexibleBox(node);
195     case GRID:
196     case INLINE_GRID:
197         return new (arena) RenderGrid(node);
198     }
199
200     return 0;
201 }
202
203 DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, renderObjectCounter, ("RenderObject"));
204
205 RenderObject::RenderObject(Node* node)
206     : CachedImageClient()
207     , m_style(0)
208     , m_node(node)
209     , m_parent(0)
210     , m_previous(0)
211     , m_next(0)
212 #ifndef NDEBUG
213     , m_hasAXObject(false)
214     , m_setNeedsLayoutForbidden(false)
215 #endif
216     , m_bitfields(node)
217 {
218 #ifndef NDEBUG
219     renderObjectCounter.increment();
220 #endif
221     ASSERT(node);
222 }
223
224 RenderObject::~RenderObject()
225 {
226 #ifndef NDEBUG
227     ASSERT(!m_hasAXObject);
228     renderObjectCounter.decrement();
229 #endif
230 }
231
232 RenderTheme* RenderObject::theme() const
233 {
234     ASSERT(document()->page());
235
236     return document()->page()->theme();
237 }
238
239 bool RenderObject::isDescendantOf(const RenderObject* obj) const
240 {
241     for (const RenderObject* r = this; r; r = r->m_parent) {
242         if (r == obj)
243             return true;
244     }
245     return false;
246 }
247
248 bool RenderObject::isBody() const
249 {
250     return node() && node()->hasTagName(bodyTag);
251 }
252
253 bool RenderObject::isHR() const
254 {
255     return node() && node()->hasTagName(hrTag);
256 }
257
258 bool RenderObject::isLegend() const
259 {
260     return node() && node()->hasTagName(legendTag);
261 }
262
263 bool RenderObject::isHTMLMarquee() const
264 {
265     return node() && node()->renderer() == this && node()->hasTagName(marqueeTag);
266 }
267
268 void RenderObject::addChild(RenderObject* newChild, RenderObject* beforeChild)
269 {
270     RenderObjectChildList* children = virtualChildren();
271     ASSERT(children);
272     if (!children)
273         return;
274
275     bool needsTable = false;
276
277     if (newChild->isRenderTableCol()) {
278         RenderTableCol* newTableColumn = toRenderTableCol(newChild);
279         bool isColumnInColumnGroup = newTableColumn->isTableColumn() && isRenderTableCol();
280         needsTable = !isTable() && !isColumnInColumnGroup;
281     } else if (newChild->isTableCaption())
282         needsTable = !isTable();
283     else if (newChild->isTableSection())
284         needsTable = !isTable();
285     else if (newChild->isTableRow())
286         needsTable = !isTableSection();
287     else if (newChild->isTableCell())
288         needsTable = !isTableRow();
289
290     if (needsTable) {
291         RenderTable* table;
292         RenderObject* afterChild = beforeChild ? beforeChild->previousSibling() : children->lastChild();
293         if (afterChild && afterChild->isAnonymous() && afterChild->isTable() && !afterChild->isBeforeContent())
294             table = toRenderTable(afterChild);
295         else {
296             table = RenderTable::createAnonymousWithParentRenderer(this);
297             addChild(table, beforeChild);
298         }
299         table->addChild(newChild);
300     } else
301         children->insertChildNode(this, newChild, beforeChild);
302
303     if (newChild->isText() && newChild->style()->textTransform() == CAPITALIZE)
304         toRenderText(newChild)->transformText();
305
306     // SVG creates renderers for <g display="none">, as SVG requires children of hidden
307     // <g>s to have renderers - at least that's how our implementation works. Consider:
308     // <g display="none"><foreignObject><body style="position: relative">FOO...
309     // - requiresLayer() would return true for the <body>, creating a new RenderLayer
310     // - when the document is painted, both layers are painted. The <body> layer doesn't
311     //   know that it's inside a "hidden SVG subtree", and thus paints, even if it shouldn't.
312     // To avoid the problem alltogether, detect early if we're inside a hidden SVG subtree
313     // and stop creating layers at all for these cases - they're not used anyways.
314     if (newChild->hasLayer() && !layerCreationAllowedForSubtree())
315         toRenderLayerModelObject(newChild)->layer()->removeOnlyThisLayer();
316 }
317
318 void RenderObject::removeChild(RenderObject* oldChild)
319 {
320     RenderObjectChildList* children = virtualChildren();
321     ASSERT(children);
322     if (!children)
323         return;
324
325     children->removeChildNode(this, oldChild);
326 }
327
328 RenderObject* RenderObject::nextInPreOrder() const
329 {
330     if (RenderObject* o = firstChild())
331         return o;
332
333     return nextInPreOrderAfterChildren();
334 }
335
336 RenderObject* RenderObject::nextInPreOrderAfterChildren() const
337 {
338     RenderObject* o;
339     if (!(o = nextSibling())) {
340         o = parent();
341         while (o && !o->nextSibling())
342             o = o->parent();
343         if (o)
344             o = o->nextSibling();
345     }
346
347     return o;
348 }
349
350 RenderObject* RenderObject::nextInPreOrder(const RenderObject* stayWithin) const
351 {
352     if (RenderObject* o = firstChild())
353         return o;
354
355     return nextInPreOrderAfterChildren(stayWithin);
356 }
357
358 RenderObject* RenderObject::nextInPreOrderAfterChildren(const RenderObject* stayWithin) const
359 {
360     if (this == stayWithin)
361         return 0;
362
363     const RenderObject* current = this;
364     RenderObject* next;
365     while (!(next = current->nextSibling())) {
366         current = current->parent();
367         if (!current || current == stayWithin)
368             return 0;
369     }
370     return next;
371 }
372
373 RenderObject* RenderObject::previousInPreOrder() const
374 {
375     if (RenderObject* o = previousSibling()) {
376         while (o->lastChild())
377             o = o->lastChild();
378         return o;
379     }
380
381     return parent();
382 }
383
384 RenderObject* RenderObject::previousInPreOrder(const RenderObject* stayWithin) const
385 {
386     if (this == stayWithin)
387         return 0;
388
389     return previousInPreOrder();
390 }
391
392 RenderObject* RenderObject::childAt(unsigned index) const
393 {
394     RenderObject* child = firstChild();
395     for (unsigned i = 0; child && i < index; i++)
396         child = child->nextSibling();
397     return child;
398 }
399
400 RenderObject* RenderObject::firstLeafChild() const
401 {
402     RenderObject* r = firstChild();
403     while (r) {
404         RenderObject* n = 0;
405         n = r->firstChild();
406         if (!n)
407             break;
408         r = n;
409     }
410     return r;
411 }
412
413 RenderObject* RenderObject::lastLeafChild() const
414 {
415     RenderObject* r = lastChild();
416     while (r) {
417         RenderObject* n = 0;
418         n = r->lastChild();
419         if (!n)
420             break;
421         r = n;
422     }
423     return r;
424 }
425
426 static void addLayers(RenderObject* obj, RenderLayer* parentLayer, RenderObject*& newObject,
427                       RenderLayer*& beforeChild)
428 {
429     if (obj->hasLayer()) {
430         if (!beforeChild && newObject) {
431             // We need to figure out the layer that follows newObject.  We only do
432             // this the first time we find a child layer, and then we update the
433             // pointer values for newObject and beforeChild used by everyone else.
434             beforeChild = newObject->parent()->findNextLayer(parentLayer, newObject);
435             newObject = 0;
436         }
437         parentLayer->addChild(toRenderLayerModelObject(obj)->layer(), beforeChild);
438         return;
439     }
440
441     for (RenderObject* curr = obj->firstChild(); curr; curr = curr->nextSibling())
442         addLayers(curr, parentLayer, newObject, beforeChild);
443 }
444
445 void RenderObject::addLayers(RenderLayer* parentLayer)
446 {
447     if (!parentLayer)
448         return;
449
450     RenderObject* object = this;
451     RenderLayer* beforeChild = 0;
452     WebCore::addLayers(this, parentLayer, object, beforeChild);
453 }
454
455 void RenderObject::removeLayers(RenderLayer* parentLayer)
456 {
457     if (!parentLayer)
458         return;
459
460     if (hasLayer()) {
461         parentLayer->removeChild(toRenderLayerModelObject(this)->layer());
462         return;
463     }
464
465     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
466         curr->removeLayers(parentLayer);
467 }
468
469 void RenderObject::moveLayers(RenderLayer* oldParent, RenderLayer* newParent)
470 {
471     if (!newParent)
472         return;
473
474     if (hasLayer()) {
475         RenderLayer* layer = toRenderLayerModelObject(this)->layer();
476         ASSERT(oldParent == layer->parent());
477         if (oldParent)
478             oldParent->removeChild(layer);
479         newParent->addChild(layer);
480         return;
481     }
482
483     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
484         curr->moveLayers(oldParent, newParent);
485 }
486
487 RenderLayer* RenderObject::findNextLayer(RenderLayer* parentLayer, RenderObject* startPoint,
488                                          bool checkParent)
489 {
490     // Error check the parent layer passed in.  If it's null, we can't find anything.
491     if (!parentLayer)
492         return 0;
493
494     // Step 1: If our layer is a child of the desired parent, then return our layer.
495     RenderLayer* ourLayer = hasLayer() ? toRenderLayerModelObject(this)->layer() : 0;
496     if (ourLayer && ourLayer->parent() == parentLayer)
497         return ourLayer;
498
499     // Step 2: If we don't have a layer, or our layer is the desired parent, then descend
500     // into our siblings trying to find the next layer whose parent is the desired parent.
501     if (!ourLayer || ourLayer == parentLayer) {
502         for (RenderObject* curr = startPoint ? startPoint->nextSibling() : firstChild();
503              curr; curr = curr->nextSibling()) {
504             RenderLayer* nextLayer = curr->findNextLayer(parentLayer, 0, false);
505             if (nextLayer)
506                 return nextLayer;
507         }
508     }
509
510     // Step 3: If our layer is the desired parent layer, then we're finished.  We didn't
511     // find anything.
512     if (parentLayer == ourLayer)
513         return 0;
514
515     // Step 4: If |checkParent| is set, climb up to our parent and check its siblings that
516     // follow us to see if we can locate a layer.
517     if (checkParent && parent())
518         return parent()->findNextLayer(parentLayer, this, true);
519
520     return 0;
521 }
522
523 RenderLayer* RenderObject::enclosingLayer() const
524 {
525     const RenderObject* curr = this;
526     while (curr) {
527         RenderLayer* layer = curr->hasLayer() ? toRenderLayerModelObject(curr)->layer() : 0;
528         if (layer)
529             return layer;
530         curr = curr->parent();
531     }
532     return 0;
533 }
534
535 bool RenderObject::scrollRectToVisible(const LayoutRect& rect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
536 {
537     RenderLayer* enclosingLayer = this->enclosingLayer();
538     if (!enclosingLayer)
539         return false;
540
541     enclosingLayer->scrollRectToVisible(rect, alignX, alignY);
542     return true;
543 }
544
545 RenderBox* RenderObject::enclosingBox() const
546 {
547     RenderObject* curr = const_cast<RenderObject*>(this);
548     while (curr) {
549         if (curr->isBox())
550             return toRenderBox(curr);
551         curr = curr->parent();
552     }
553     
554     ASSERT_NOT_REACHED();
555     return 0;
556 }
557
558 RenderBoxModelObject* RenderObject::enclosingBoxModelObject() const
559 {
560     RenderObject* curr = const_cast<RenderObject*>(this);
561     while (curr) {
562         if (curr->isBoxModelObject())
563             return toRenderBoxModelObject(curr);
564         curr = curr->parent();
565     }
566
567     ASSERT_NOT_REACHED();
568     return 0;
569 }
570
571 RenderFlowThread* RenderObject::enclosingRenderFlowThread() const
572 {   
573     if (!inRenderFlowThread())
574         return 0;
575     
576     // See if we have the thread cached because we're in the middle of layout.
577     RenderFlowThread* flowThread = view()->flowThreadController()->currentRenderFlowThread();
578     if (flowThread)
579         return flowThread;
580     
581     // Not in the middle of layout so have to find the thread the slow way.
582     RenderObject* curr = const_cast<RenderObject*>(this);
583     while (curr) {
584         if (curr->isRenderFlowThread())
585             return toRenderFlowThread(curr);
586         curr = curr->parent();
587     }
588     return 0;
589 }
590
591 RenderNamedFlowThread* RenderObject::enclosingRenderNamedFlowThread() const
592 {
593     RenderObject* object = const_cast<RenderObject*>(this);
594     while (object && object->isAnonymousBlock() && !object->isRenderNamedFlowThread())
595         object = object->parent();
596
597     return object && object->isRenderNamedFlowThread() ? toRenderNamedFlowThread(object) : 0;
598 }
599
600 RenderBlock* RenderObject::firstLineBlock() const
601 {
602     return 0;
603 }
604
605 static inline bool objectIsRelayoutBoundary(const RenderObject* object)
606 {
607     // FIXME: In future it may be possible to broaden these conditions in order to improve performance.
608     if (object->isTextControl())
609         return true;
610
611 #if ENABLE(SVG)
612     if (object->isSVGRoot())
613         return true;
614 #endif
615
616     if (!object->hasOverflowClip())
617         return false;
618
619     if (object->style()->width().isIntrinsicOrAuto() || object->style()->height().isIntrinsicOrAuto() || object->style()->height().isPercent())
620         return false;
621
622     // Table parts can't be relayout roots since the table is responsible for layouting all the parts.
623     if (object->isTablePart())
624         return false;
625
626     return true;
627 }
628
629 void RenderObject::markContainingBlocksForLayout(bool scheduleRelayout, RenderObject* newRoot)
630 {
631     ASSERT(!scheduleRelayout || !newRoot);
632
633     RenderObject* object = container();
634     RenderObject* last = this;
635
636     bool simplifiedNormalFlowLayout = needsSimplifiedNormalFlowLayout() && !selfNeedsLayout() && !normalChildNeedsLayout();
637
638     while (object) {
639         // Don't mark the outermost object of an unrooted subtree. That object will be
640         // marked when the subtree is added to the document.
641         RenderObject* container = object->container();
642         if (!container && !object->isRenderView())
643             return;
644         if (!last->isText() && last->style()->hasOutOfFlowPosition()) {
645             bool willSkipRelativelyPositionedInlines = !object->isRenderBlock() || object->isAnonymousBlock();
646             // Skip relatively positioned inlines and anonymous blocks to get to the enclosing RenderBlock.
647             while (object && (!object->isRenderBlock() || object->isAnonymousBlock()))
648                 object = object->container();
649             if (!object || object->posChildNeedsLayout())
650                 return;
651             if (willSkipRelativelyPositionedInlines)
652                 container = object->container();
653             object->setPosChildNeedsLayout(true);
654             simplifiedNormalFlowLayout = true;
655             ASSERT(!object->isSetNeedsLayoutForbidden());
656         } else if (simplifiedNormalFlowLayout) {
657             if (object->needsSimplifiedNormalFlowLayout())
658                 return;
659             object->setNeedsSimplifiedNormalFlowLayout(true);
660             ASSERT(!object->isSetNeedsLayoutForbidden());
661         } else {
662             if (object->normalChildNeedsLayout())
663                 return;
664             object->setNormalChildNeedsLayout(true);
665             ASSERT(!object->isSetNeedsLayoutForbidden());
666         }
667
668         if (object == newRoot)
669             return;
670
671         last = object;
672         if (scheduleRelayout && objectIsRelayoutBoundary(last))
673             break;
674         object = container;
675     }
676
677     if (scheduleRelayout)
678         last->scheduleRelayout();
679 }
680
681 #ifndef NDEBUG
682 void RenderObject::checkBlockPositionedObjectsNeedLayout()
683 {
684     ASSERT(!needsLayout());
685
686     if (isRenderBlock())
687         toRenderBlock(this)->checkPositionedObjectsNeedLayout();
688 }
689 #endif
690
691 void RenderObject::setPreferredLogicalWidthsDirty(bool shouldBeDirty, MarkingBehavior markParents)
692 {
693     bool alreadyDirty = preferredLogicalWidthsDirty();
694     m_bitfields.setPreferredLogicalWidthsDirty(shouldBeDirty);
695     if (shouldBeDirty && !alreadyDirty && markParents == MarkContainingBlockChain && (isText() || !style()->hasOutOfFlowPosition()))
696         invalidateContainerPreferredLogicalWidths();
697 }
698
699 void RenderObject::invalidateContainerPreferredLogicalWidths()
700 {
701     // In order to avoid pathological behavior when inlines are deeply nested, we do include them
702     // in the chain that we mark dirty (even though they're kind of irrelevant).
703     RenderObject* o = isTableCell() ? containingBlock() : container();
704     while (o && !o->preferredLogicalWidthsDirty()) {
705         // Don't invalidate the outermost object of an unrooted subtree. That object will be 
706         // invalidated when the subtree is added to the document.
707         RenderObject* container = o->isTableCell() ? o->containingBlock() : o->container();
708         if (!container && !o->isRenderView())
709             break;
710
711         o->m_bitfields.setPreferredLogicalWidthsDirty(true);
712         if (o->style()->hasOutOfFlowPosition())
713             // A positioned object has no effect on the min/max width of its containing block ever.
714             // We can optimize this case and not go up any further.
715             break;
716         o = container;
717     }
718 }
719
720 void RenderObject::setLayerNeedsFullRepaint()
721 {
722     ASSERT(hasLayer());
723     toRenderLayerModelObject(this)->layer()->setRepaintStatus(NeedsFullRepaint);
724 }
725
726 void RenderObject::setLayerNeedsFullRepaintForPositionedMovementLayout()
727 {
728     ASSERT(hasLayer());
729     toRenderLayerModelObject(this)->layer()->setRepaintStatus(NeedsFullRepaintForPositionedMovementLayout);
730 }
731
732 RenderBlock* RenderObject::containingBlock() const
733 {
734     RenderObject* o = parent();
735     if (!o && isRenderScrollbarPart())
736         o = toRenderScrollbarPart(this)->rendererOwningScrollbar();
737     if (!isText() && m_style->position() == FixedPosition) {
738         while (o) {
739             if (o->isRenderView())
740                 break;
741             if (o->hasTransform() && o->isRenderBlock())
742                 break;
743             // The render flow thread is the top most containing block
744             // for the fixed positioned elements.
745             if (o->isRenderFlowThread())
746                 break;
747 #if ENABLE(SVG)
748             // foreignObject is the containing block for its contents.
749             if (o->isSVGForeignObject())
750                 break;
751 #endif
752             o = o->parent();
753         }
754         ASSERT(!o->isAnonymousBlock());
755     } else if (!isText() && m_style->position() == AbsolutePosition) {
756         while (o) {
757             // For relpositioned inlines, we return the nearest non-anonymous enclosing block. We don't try
758             // to return the inline itself.  This allows us to avoid having a positioned objects
759             // list in all RenderInlines and lets us return a strongly-typed RenderBlock* result
760             // from this method.  The container() method can actually be used to obtain the
761             // inline directly.
762             if (!o->style()->position() == StaticPosition && !(o->isInline() && !o->isReplaced()))
763                 break;
764             if (o->isRenderView())
765                 break;
766             if (o->hasTransform() && o->isRenderBlock())
767                 break;
768
769             if (o->style()->hasInFlowPosition() && o->isInline() && !o->isReplaced()) {
770                 o = o->containingBlock();
771                 break;
772             }
773 #if ENABLE(SVG)
774             if (o->isSVGForeignObject()) //foreignObject is the containing block for contents inside it
775                 break;
776 #endif
777
778             o = o->parent();
779         }
780
781         while (o && o->isAnonymousBlock())
782             o = o->containingBlock();
783     } else {
784         while (o && ((o->isInline() && !o->isReplaced()) || !o->isRenderBlock()))
785             o = o->parent();
786     }
787
788     if (!o || !o->isRenderBlock())
789         return 0; // This can still happen in case of an orphaned tree
790
791     return toRenderBlock(o);
792 }
793
794 static bool mustRepaintFillLayers(const RenderObject* renderer, const FillLayer* layer)
795 {
796     // Nobody will use multiple layers without wanting fancy positioning.
797     if (layer->next())
798         return true;
799
800     // Make sure we have a valid image.
801     StyleImage* img = layer->image();
802     if (!img || !img->canRender(renderer, renderer->style()->effectiveZoom()))
803         return false;
804
805     if (!layer->xPosition().isZero() || !layer->yPosition().isZero())
806         return true;
807
808     if (layer->size().type == SizeLength) {
809         if (layer->size().size.width().isPercent() || layer->size().size.height().isPercent())
810             return true;
811     } else if (layer->size().type == Contain || layer->size().type == Cover || img->usesImageContainerSize())
812         return true;
813
814     return false;
815 }
816
817 bool RenderObject::borderImageIsLoadedAndCanBeRendered() const
818 {
819     ASSERT(style()->hasBorder());
820
821     StyleImage* borderImage = style()->borderImage().image();
822     return borderImage && borderImage->canRender(this, style()->effectiveZoom()) && borderImage->isLoaded();
823 }
824
825 bool RenderObject::mustRepaintBackgroundOrBorder() const
826 {
827     if (hasMask() && mustRepaintFillLayers(this, style()->maskLayers()))
828         return true;
829
830     // If we don't have a background/border/mask, then nothing to do.
831     if (!hasBoxDecorations())
832         return false;
833
834     if (mustRepaintFillLayers(this, style()->backgroundLayers()))
835         return true;
836      
837     // Our fill layers are ok.  Let's check border.
838     if (style()->hasBorder() && borderImageIsLoadedAndCanBeRendered())
839         return true;
840
841     return false;
842 }
843
844 void RenderObject::drawLineForBoxSide(GraphicsContext* graphicsContext, int x1, int y1, int x2, int y2,
845                                       BoxSide side, Color color, EBorderStyle style,
846                                       int adjacentWidth1, int adjacentWidth2, bool antialias)
847 {
848     int thickness;
849     int length;
850     if (side == BSTop || side == BSBottom) {
851         thickness = y2 - y1;
852         length = x2 - x1;
853     } else {
854         thickness = x2 - x1;
855         length = y2 - y1;
856     }
857
858     // FIXME: We really would like this check to be an ASSERT as we don't want to draw empty borders. However
859     // nothing guarantees that the following recursive calls to drawLineForBoxSide will have non-null dimensions.
860     if (!thickness || !length)
861         return;
862
863     if (style == DOUBLE && thickness < 3)
864         style = SOLID;
865
866     switch (style) {
867         case BNONE:
868         case BHIDDEN:
869             return;
870         case DOTTED:
871         case DASHED: {
872             graphicsContext->setStrokeColor(color, m_style->colorSpace());
873             graphicsContext->setStrokeThickness(thickness);
874             StrokeStyle oldStrokeStyle = graphicsContext->strokeStyle();
875             graphicsContext->setStrokeStyle(style == DASHED ? DashedStroke : DottedStroke);
876
877             if (thickness > 0) {
878                 bool wasAntialiased = graphicsContext->shouldAntialias();
879                 graphicsContext->setShouldAntialias(antialias);
880
881                 switch (side) {
882                     case BSBottom:
883                     case BSTop:
884                         graphicsContext->drawLine(IntPoint(x1, (y1 + y2) / 2), IntPoint(x2, (y1 + y2) / 2));
885                         break;
886                     case BSRight:
887                     case BSLeft:
888                         graphicsContext->drawLine(IntPoint((x1 + x2) / 2, y1), IntPoint((x1 + x2) / 2, y2));
889                         break;
890                 }
891                 graphicsContext->setShouldAntialias(wasAntialiased);
892                 graphicsContext->setStrokeStyle(oldStrokeStyle);
893             }
894             break;
895         }
896         case DOUBLE: {
897             int thirdOfThickness = (thickness + 1) / 3;
898             ASSERT(thirdOfThickness);
899
900             if (adjacentWidth1 == 0 && adjacentWidth2 == 0) {
901                 StrokeStyle oldStrokeStyle = graphicsContext->strokeStyle();
902                 graphicsContext->setStrokeStyle(NoStroke);
903                 graphicsContext->setFillColor(color, m_style->colorSpace());
904                 
905                 bool wasAntialiased = graphicsContext->shouldAntialias();
906                 graphicsContext->setShouldAntialias(antialias);
907
908                 switch (side) {
909                     case BSTop:
910                     case BSBottom:
911                         graphicsContext->drawRect(IntRect(x1, y1, length, thirdOfThickness));
912                         graphicsContext->drawRect(IntRect(x1, y2 - thirdOfThickness, length, thirdOfThickness));
913                         break;
914                     case BSLeft:
915                     case BSRight:
916                         // FIXME: Why do we offset the border by 1 in this case but not the other one?
917                         if (length > 1) {
918                             graphicsContext->drawRect(IntRect(x1, y1 + 1, thirdOfThickness, length - 1));
919                             graphicsContext->drawRect(IntRect(x2 - thirdOfThickness, y1 + 1, thirdOfThickness, length - 1));
920                         }
921                         break;
922                 }
923
924                 graphicsContext->setShouldAntialias(wasAntialiased);
925                 graphicsContext->setStrokeStyle(oldStrokeStyle);
926             } else {
927                 int adjacent1BigThird = ((adjacentWidth1 > 0) ? adjacentWidth1 + 1 : adjacentWidth1 - 1) / 3;
928                 int adjacent2BigThird = ((adjacentWidth2 > 0) ? adjacentWidth2 + 1 : adjacentWidth2 - 1) / 3;
929
930                 switch (side) {
931                     case BSTop:
932                         drawLineForBoxSide(graphicsContext, x1 + max((-adjacentWidth1 * 2 + 1) / 3, 0),
933                                    y1, x2 - max((-adjacentWidth2 * 2 + 1) / 3, 0), y1 + thirdOfThickness,
934                                    side, color, SOLID, adjacent1BigThird, adjacent2BigThird, antialias);
935                         drawLineForBoxSide(graphicsContext, x1 + max((adjacentWidth1 * 2 + 1) / 3, 0),
936                                    y2 - thirdOfThickness, x2 - max((adjacentWidth2 * 2 + 1) / 3, 0), y2,
937                                    side, color, SOLID, adjacent1BigThird, adjacent2BigThird, antialias);
938                         break;
939                     case BSLeft:
940                         drawLineForBoxSide(graphicsContext, x1, y1 + max((-adjacentWidth1 * 2 + 1) / 3, 0),
941                                    x1 + thirdOfThickness, y2 - max((-adjacentWidth2 * 2 + 1) / 3, 0),
942                                    side, color, SOLID, adjacent1BigThird, adjacent2BigThird, antialias);
943                         drawLineForBoxSide(graphicsContext, x2 - thirdOfThickness, y1 + max((adjacentWidth1 * 2 + 1) / 3, 0),
944                                    x2, y2 - max((adjacentWidth2 * 2 + 1) / 3, 0),
945                                    side, color, SOLID, adjacent1BigThird, adjacent2BigThird, antialias);
946                         break;
947                     case BSBottom:
948                         drawLineForBoxSide(graphicsContext, x1 + max((adjacentWidth1 * 2 + 1) / 3, 0),
949                                    y1, x2 - max((adjacentWidth2 * 2 + 1) / 3, 0), y1 + thirdOfThickness,
950                                    side, color, SOLID, adjacent1BigThird, adjacent2BigThird, antialias);
951                         drawLineForBoxSide(graphicsContext, x1 + max((-adjacentWidth1 * 2 + 1) / 3, 0),
952                                    y2 - thirdOfThickness, x2 - max((-adjacentWidth2 * 2 + 1) / 3, 0), y2,
953                                    side, color, SOLID, adjacent1BigThird, adjacent2BigThird, antialias);
954                         break;
955                     case BSRight:
956                         drawLineForBoxSide(graphicsContext, x1, y1 + max((adjacentWidth1 * 2 + 1) / 3, 0),
957                                    x1 + thirdOfThickness, y2 - max((adjacentWidth2 * 2 + 1) / 3, 0),
958                                    side, color, SOLID, adjacent1BigThird, adjacent2BigThird, antialias);
959                         drawLineForBoxSide(graphicsContext, x2 - thirdOfThickness, y1 + max((-adjacentWidth1 * 2 + 1) / 3, 0),
960                                    x2, y2 - max((-adjacentWidth2 * 2 + 1) / 3, 0),
961                                    side, color, SOLID, adjacent1BigThird, adjacent2BigThird, antialias);
962                         break;
963                     default:
964                         break;
965                 }
966             }
967             break;
968         }
969         case RIDGE:
970         case GROOVE: {
971             EBorderStyle s1;
972             EBorderStyle s2;
973             if (style == GROOVE) {
974                 s1 = INSET;
975                 s2 = OUTSET;
976             } else {
977                 s1 = OUTSET;
978                 s2 = INSET;
979             }
980
981             int adjacent1BigHalf = ((adjacentWidth1 > 0) ? adjacentWidth1 + 1 : adjacentWidth1 - 1) / 2;
982             int adjacent2BigHalf = ((adjacentWidth2 > 0) ? adjacentWidth2 + 1 : adjacentWidth2 - 1) / 2;
983
984             switch (side) {
985                 case BSTop:
986                     drawLineForBoxSide(graphicsContext, x1 + max(-adjacentWidth1, 0) / 2, y1, x2 - max(-adjacentWidth2, 0) / 2, (y1 + y2 + 1) / 2,
987                                side, color, s1, adjacent1BigHalf, adjacent2BigHalf, antialias);
988                     drawLineForBoxSide(graphicsContext, x1 + max(adjacentWidth1 + 1, 0) / 2, (y1 + y2 + 1) / 2, x2 - max(adjacentWidth2 + 1, 0) / 2, y2,
989                                side, color, s2, adjacentWidth1 / 2, adjacentWidth2 / 2, antialias);
990                     break;
991                 case BSLeft:
992                     drawLineForBoxSide(graphicsContext, x1, y1 + max(-adjacentWidth1, 0) / 2, (x1 + x2 + 1) / 2, y2 - max(-adjacentWidth2, 0) / 2,
993                                side, color, s1, adjacent1BigHalf, adjacent2BigHalf, antialias);
994                     drawLineForBoxSide(graphicsContext, (x1 + x2 + 1) / 2, y1 + max(adjacentWidth1 + 1, 0) / 2, x2, y2 - max(adjacentWidth2 + 1, 0) / 2,
995                                side, color, s2, adjacentWidth1 / 2, adjacentWidth2 / 2, antialias);
996                     break;
997                 case BSBottom:
998                     drawLineForBoxSide(graphicsContext, x1 + max(adjacentWidth1, 0) / 2, y1, x2 - max(adjacentWidth2, 0) / 2, (y1 + y2 + 1) / 2,
999                                side, color, s2, adjacent1BigHalf, adjacent2BigHalf, antialias);
1000                     drawLineForBoxSide(graphicsContext, x1 + max(-adjacentWidth1 + 1, 0) / 2, (y1 + y2 + 1) / 2, x2 - max(-adjacentWidth2 + 1, 0) / 2, y2,
1001                                side, color, s1, adjacentWidth1 / 2, adjacentWidth2 / 2, antialias);
1002                     break;
1003                 case BSRight:
1004                     drawLineForBoxSide(graphicsContext, x1, y1 + max(adjacentWidth1, 0) / 2, (x1 + x2 + 1) / 2, y2 - max(adjacentWidth2, 0) / 2,
1005                                side, color, s2, adjacent1BigHalf, adjacent2BigHalf, antialias);
1006                     drawLineForBoxSide(graphicsContext, (x1 + x2 + 1) / 2, y1 + max(-adjacentWidth1 + 1, 0) / 2, x2, y2 - max(-adjacentWidth2 + 1, 0) / 2,
1007                                side, color, s1, adjacentWidth1 / 2, adjacentWidth2 / 2, antialias);
1008                     break;
1009             }
1010             break;
1011         }
1012         case INSET:
1013             // FIXME: Maybe we should lighten the colors on one side like Firefox.
1014             // https://bugs.webkit.org/show_bug.cgi?id=58608
1015             if (side == BSTop || side == BSLeft)
1016                 color = color.dark();
1017             // fall through
1018         case OUTSET:
1019             if (style == OUTSET && (side == BSBottom || side == BSRight))
1020                 color = color.dark();
1021             // fall through
1022         case SOLID: {
1023             StrokeStyle oldStrokeStyle = graphicsContext->strokeStyle();
1024             graphicsContext->setStrokeStyle(NoStroke);
1025             graphicsContext->setFillColor(color, m_style->colorSpace());
1026             ASSERT(x2 >= x1);
1027             ASSERT(y2 >= y1);
1028             if (!adjacentWidth1 && !adjacentWidth2) {
1029                 // Turn off antialiasing to match the behavior of drawConvexPolygon();
1030                 // this matters for rects in transformed contexts.
1031                 bool wasAntialiased = graphicsContext->shouldAntialias();
1032                 graphicsContext->setShouldAntialias(antialias);
1033                 graphicsContext->drawRect(IntRect(x1, y1, x2 - x1, y2 - y1));
1034                 graphicsContext->setShouldAntialias(wasAntialiased);
1035                 graphicsContext->setStrokeStyle(oldStrokeStyle);
1036                 return;
1037             }
1038             FloatPoint quad[4];
1039             switch (side) {
1040                 case BSTop:
1041                     quad[0] = FloatPoint(x1 + max(-adjacentWidth1, 0), y1);
1042                     quad[1] = FloatPoint(x1 + max(adjacentWidth1, 0), y2);
1043                     quad[2] = FloatPoint(x2 - max(adjacentWidth2, 0), y2);
1044                     quad[3] = FloatPoint(x2 - max(-adjacentWidth2, 0), y1);
1045                     break;
1046                 case BSBottom:
1047                     quad[0] = FloatPoint(x1 + max(adjacentWidth1, 0), y1);
1048                     quad[1] = FloatPoint(x1 + max(-adjacentWidth1, 0), y2);
1049                     quad[2] = FloatPoint(x2 - max(-adjacentWidth2, 0), y2);
1050                     quad[3] = FloatPoint(x2 - max(adjacentWidth2, 0), y1);
1051                     break;
1052                 case BSLeft:
1053                     quad[0] = FloatPoint(x1, y1 + max(-adjacentWidth1, 0));
1054                     quad[1] = FloatPoint(x1, y2 - max(-adjacentWidth2, 0));
1055                     quad[2] = FloatPoint(x2, y2 - max(adjacentWidth2, 0));
1056                     quad[3] = FloatPoint(x2, y1 + max(adjacentWidth1, 0));
1057                     break;
1058                 case BSRight:
1059                     quad[0] = FloatPoint(x1, y1 + max(adjacentWidth1, 0));
1060                     quad[1] = FloatPoint(x1, y2 - max(adjacentWidth2, 0));
1061                     quad[2] = FloatPoint(x2, y2 - max(-adjacentWidth2, 0));
1062                     quad[3] = FloatPoint(x2, y1 + max(-adjacentWidth1, 0));
1063                     break;
1064             }
1065
1066             graphicsContext->drawConvexPolygon(4, quad, antialias);
1067             graphicsContext->setStrokeStyle(oldStrokeStyle);
1068             break;
1069         }
1070     }
1071 }
1072
1073 void RenderObject::paintFocusRing(GraphicsContext* context, const LayoutPoint& paintOffset, RenderStyle* style)
1074 {
1075     Vector<IntRect> focusRingRects;
1076     addFocusRingRects(focusRingRects, paintOffset);
1077     if (style->outlineStyleIsAuto())
1078         context->drawFocusRing(focusRingRects, style->outlineWidth(), style->outlineOffset(), style->visitedDependentColor(CSSPropertyOutlineColor));
1079     else
1080         addPDFURLRect(context, unionRect(focusRingRects));
1081 }
1082
1083 void RenderObject::addPDFURLRect(GraphicsContext* context, const LayoutRect& rect)
1084 {
1085     if (rect.isEmpty())
1086         return;
1087     Node* n = node();
1088     if (!n || !n->isLink() || !n->isElementNode())
1089         return;
1090     const AtomicString& href = static_cast<Element*>(n)->getAttribute(hrefAttr);
1091     if (href.isNull())
1092         return;
1093     context->setURLForRect(n->document()->completeURL(href), pixelSnappedIntRect(rect));
1094 }
1095
1096 void RenderObject::paintOutline(GraphicsContext* graphicsContext, const LayoutRect& paintRect)
1097 {
1098     if (!hasOutline())
1099         return;
1100
1101     RenderStyle* styleToUse = style();
1102     LayoutUnit outlineWidth = styleToUse->outlineWidth();
1103     EBorderStyle outlineStyle = styleToUse->outlineStyle();
1104
1105     Color outlineColor = styleToUse->visitedDependentColor(CSSPropertyOutlineColor);
1106
1107     int outlineOffset = styleToUse->outlineOffset();
1108
1109     if (styleToUse->outlineStyleIsAuto() || hasOutlineAnnotation()) {
1110         if (!theme()->supportsFocusRing(styleToUse)) {
1111             // Only paint the focus ring by hand if the theme isn't able to draw the focus ring.
1112             paintFocusRing(graphicsContext, paintRect.location(), styleToUse);
1113         }
1114     }
1115
1116     if (styleToUse->outlineStyleIsAuto() || styleToUse->outlineStyle() == BNONE)
1117         return;
1118
1119     IntRect inner = pixelSnappedIntRect(paintRect);
1120     inner.inflate(outlineOffset);
1121
1122     IntRect outer = pixelSnappedIntRect(inner);
1123     outer.inflate(outlineWidth);
1124
1125     // FIXME: This prevents outlines from painting inside the object. See bug 12042
1126     if (outer.isEmpty())
1127         return;
1128
1129     bool useTransparencyLayer = outlineColor.hasAlpha();
1130     if (useTransparencyLayer) {
1131         if (outlineStyle == SOLID) {
1132             Path path;
1133             path.addRect(outer);
1134             path.addRect(inner);
1135             graphicsContext->setFillRule(RULE_EVENODD);
1136             graphicsContext->setFillColor(outlineColor, styleToUse->colorSpace());
1137             graphicsContext->fillPath(path);
1138             return;
1139         }
1140         graphicsContext->beginTransparencyLayer(static_cast<float>(outlineColor.alpha()) / 255);
1141         outlineColor = Color(outlineColor.red(), outlineColor.green(), outlineColor.blue());
1142     }
1143
1144     int leftOuter = outer.x();
1145     int leftInner = inner.x();
1146     int rightOuter = outer.maxX();
1147     int rightInner = inner.maxX();
1148     int topOuter = outer.y();
1149     int topInner = inner.y();
1150     int bottomOuter = outer.maxY();
1151     int bottomInner = inner.maxY();
1152     
1153     drawLineForBoxSide(graphicsContext, leftOuter, topOuter, leftInner, bottomOuter, BSLeft, outlineColor, outlineStyle, outlineWidth, outlineWidth);
1154     drawLineForBoxSide(graphicsContext, leftOuter, topOuter, rightOuter, topInner, BSTop, outlineColor, outlineStyle, outlineWidth, outlineWidth);
1155     drawLineForBoxSide(graphicsContext, rightInner, topOuter, rightOuter, bottomOuter, BSRight, outlineColor, outlineStyle, outlineWidth, outlineWidth);
1156     drawLineForBoxSide(graphicsContext, leftOuter, bottomInner, rightOuter, bottomOuter, BSBottom, outlineColor, outlineStyle, outlineWidth, outlineWidth);
1157
1158     if (useTransparencyLayer)
1159         graphicsContext->endTransparencyLayer();
1160 }
1161
1162 IntRect RenderObject::absoluteBoundingBoxRect(bool useTransforms) const
1163 {
1164     if (useTransforms) {
1165         Vector<FloatQuad> quads;
1166         absoluteQuads(quads);
1167
1168         size_t n = quads.size();
1169         if (!n)
1170             return IntRect();
1171     
1172         IntRect result = quads[0].enclosingBoundingBox();
1173         for (size_t i = 1; i < n; ++i)
1174             result.unite(quads[i].enclosingBoundingBox());
1175         return result;
1176     }
1177
1178     FloatPoint absPos = localToAbsolute();
1179     Vector<IntRect> rects;
1180     absoluteRects(rects, flooredLayoutPoint(absPos));
1181
1182     size_t n = rects.size();
1183     if (!n)
1184         return IntRect();
1185
1186     LayoutRect result = rects[0];
1187     for (size_t i = 1; i < n; ++i)
1188         result.unite(rects[i]);
1189     return pixelSnappedIntRect(result);
1190 }
1191
1192 void RenderObject::absoluteFocusRingQuads(Vector<FloatQuad>& quads)
1193 {
1194     Vector<IntRect> rects;
1195     // FIXME: addFocusRingRects() needs to be passed this transform-unaware
1196     // localToAbsolute() offset here because RenderInline::addFocusRingRects()
1197     // implicitly assumes that. This doesn't work correctly with transformed
1198     // descendants.
1199     FloatPoint absolutePoint = localToAbsolute();
1200     addFocusRingRects(rects, flooredLayoutPoint(absolutePoint));
1201     size_t count = rects.size(); 
1202     for (size_t i = 0; i < count; ++i) {
1203         IntRect rect = rects[i];
1204         rect.move(-absolutePoint.x(), -absolutePoint.y());
1205         quads.append(localToAbsoluteQuad(FloatQuad(rect), SnapOffsetForTransforms));
1206     }
1207 }
1208
1209 FloatRect RenderObject::absoluteBoundingBoxRectForRange(const Range* range)
1210 {
1211     if (!range || !range->startContainer())
1212         return FloatRect();
1213
1214     if (range->ownerDocument())
1215         range->ownerDocument()->updateLayout();
1216
1217     Vector<FloatQuad> quads;
1218     range->textQuads(quads);
1219
1220     FloatRect result;
1221     for (size_t i = 0; i < quads.size(); ++i)
1222         result.unite(quads[i].boundingBox());
1223
1224     return result;
1225 }
1226
1227 void RenderObject::addAbsoluteRectForLayer(LayoutRect& result)
1228 {
1229     if (hasLayer())
1230         result.unite(absoluteBoundingBoxRectIgnoringTransforms());
1231     for (RenderObject* current = firstChild(); current; current = current->nextSibling())
1232         current->addAbsoluteRectForLayer(result);
1233 }
1234
1235 LayoutRect RenderObject::paintingRootRect(LayoutRect& topLevelRect)
1236 {
1237     LayoutRect result = absoluteBoundingBoxRectIgnoringTransforms();
1238     topLevelRect = result;
1239     for (RenderObject* current = firstChild(); current; current = current->nextSibling())
1240         current->addAbsoluteRectForLayer(result);
1241     return result;
1242 }
1243
1244 void RenderObject::paint(PaintInfo&, const LayoutPoint&)
1245 {
1246 }
1247
1248 RenderLayerModelObject* RenderObject::containerForRepaint() const
1249 {
1250     RenderView* v = view();
1251     if (!v)
1252         return 0;
1253     
1254     RenderLayerModelObject* repaintContainer = 0;
1255
1256 #if USE(ACCELERATED_COMPOSITING)
1257     if (v->usesCompositing()) {
1258         if (RenderLayer* parentLayer = enclosingLayer()) {
1259             RenderLayer* compLayer = parentLayer->enclosingCompositingLayerForRepaint();
1260             if (compLayer)
1261                 repaintContainer = compLayer->renderer();
1262         }
1263     }
1264 #endif
1265     
1266 #if ENABLE(CSS_FILTERS)
1267     if (RenderLayer* parentLayer = enclosingLayer()) {
1268         RenderLayer* enclosingFilterLayer = parentLayer->enclosingFilterLayer();
1269         if (enclosingFilterLayer)
1270             return enclosingFilterLayer->renderer();
1271     }
1272 #endif
1273
1274     // If we have a flow thread, then we need to do individual repaints within the RenderRegions instead.
1275     // Return the flow thread as a repaint container in order to create a chokepoint that allows us to change
1276     // repainting to do individual region repaints.
1277     // FIXME: Composited layers inside a flow thread will bypass this mechanism and will malfunction. It's not
1278     // clear how to address this problem for composited descendants of a RenderFlowThread.
1279     if (!repaintContainer && inRenderFlowThread())
1280         repaintContainer = enclosingRenderFlowThread();
1281     return repaintContainer;
1282 }
1283
1284 void RenderObject::repaintUsingContainer(RenderLayerModelObject* repaintContainer, const IntRect& r, bool immediate) const
1285 {
1286     if (!repaintContainer) {
1287         view()->repaintViewRectangle(r, immediate);
1288         return;
1289     }
1290
1291     if (repaintContainer->isRenderFlowThread()) {
1292         toRenderFlowThread(repaintContainer)->repaintRectangleInRegions(r, immediate);
1293         return;
1294     }
1295
1296 #if ENABLE(CSS_FILTERS)
1297     if (repaintContainer->hasFilter() && repaintContainer->layer() && repaintContainer->layer()->requiresFullLayerImageForFilters()) {
1298         repaintContainer->layer()->setFilterBackendNeedsRepaintingInRect(r, immediate);
1299         return;
1300     }
1301 #endif
1302
1303 #if USE(ACCELERATED_COMPOSITING)
1304     RenderView* v = view();
1305     if (repaintContainer->isRenderView()) {
1306         ASSERT(repaintContainer == v);
1307         bool viewHasCompositedLayer = v->hasLayer() && v->layer()->isComposited();
1308         if (!viewHasCompositedLayer || v->layer()->backing()->paintsIntoWindow()) {
1309             LayoutRect repaintRectangle = r;
1310             if (viewHasCompositedLayer &&  v->layer()->transform())
1311                 repaintRectangle = enclosingIntRect(v->layer()->transform()->mapRect(r));
1312             v->repaintViewRectangle(repaintRectangle, immediate);
1313             return;
1314         }
1315     }
1316     
1317     if (v->usesCompositing()) {
1318         ASSERT(repaintContainer->hasLayer() && repaintContainer->layer()->isComposited());
1319         repaintContainer->layer()->setBackingNeedsRepaintInRect(r);
1320     }
1321 #else
1322     if (repaintContainer->isRenderView())
1323         toRenderView(repaintContainer)->repaintViewRectangle(r, immediate);
1324 #endif
1325 }
1326
1327 void RenderObject::repaint(bool immediate) const
1328 {
1329     // Don't repaint if we're unrooted (note that view() still returns the view when unrooted)
1330     RenderView* view;
1331     if (!isRooted(&view))
1332         return;
1333
1334     if (view->printing())
1335         return; // Don't repaint if we're printing.
1336
1337     RenderLayerModelObject* repaintContainer = containerForRepaint();
1338     repaintUsingContainer(repaintContainer ? repaintContainer : view, pixelSnappedIntRect(clippedOverflowRectForRepaint(repaintContainer)), immediate);
1339 }
1340
1341 void RenderObject::repaintRectangle(const LayoutRect& r, bool immediate) const
1342 {
1343     // Don't repaint if we're unrooted (note that view() still returns the view when unrooted)
1344     RenderView* view;
1345     if (!isRooted(&view))
1346         return;
1347
1348     if (view->printing())
1349         return; // Don't repaint if we're printing.
1350
1351     LayoutRect dirtyRect(r);
1352
1353     // FIXME: layoutDelta needs to be applied in parts before/after transforms and
1354     // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
1355     dirtyRect.move(view->layoutDelta());
1356
1357     RenderLayerModelObject* repaintContainer = containerForRepaint();
1358     computeRectForRepaint(repaintContainer, dirtyRect);
1359     repaintUsingContainer(repaintContainer ? repaintContainer : view, pixelSnappedIntRect(dirtyRect), immediate);
1360 }
1361
1362 IntRect RenderObject::pixelSnappedAbsoluteClippedOverflowRect() const
1363 {
1364     return pixelSnappedIntRect(absoluteClippedOverflowRect());
1365 }
1366
1367 bool RenderObject::repaintAfterLayoutIfNeeded(RenderLayerModelObject* repaintContainer, const LayoutRect& oldBounds, const LayoutRect& oldOutlineBox, const LayoutRect* newBoundsPtr, const LayoutRect* newOutlineBoxRectPtr)
1368 {
1369     RenderView* v = view();
1370     if (v->printing())
1371         return false; // Don't repaint if we're printing.
1372
1373     // This ASSERT fails due to animations.  See https://bugs.webkit.org/show_bug.cgi?id=37048
1374     // ASSERT(!newBoundsPtr || *newBoundsPtr == clippedOverflowRectForRepaint(repaintContainer));
1375     LayoutRect newBounds = newBoundsPtr ? *newBoundsPtr : clippedOverflowRectForRepaint(repaintContainer);
1376     LayoutRect newOutlineBox;
1377
1378     bool fullRepaint = selfNeedsLayout();
1379     // Presumably a background or a border exists if border-fit:lines was specified.
1380     if (!fullRepaint && style()->borderFit() == BorderFitLines)
1381         fullRepaint = true;
1382     if (!fullRepaint) {
1383         // This ASSERT fails due to animations.  See https://bugs.webkit.org/show_bug.cgi?id=37048
1384         // ASSERT(!newOutlineBoxRectPtr || *newOutlineBoxRectPtr == outlineBoundsForRepaint(repaintContainer));
1385         newOutlineBox = newOutlineBoxRectPtr ? *newOutlineBoxRectPtr : outlineBoundsForRepaint(repaintContainer);
1386         if (newOutlineBox.location() != oldOutlineBox.location() || (mustRepaintBackgroundOrBorder() && (newBounds != oldBounds || newOutlineBox != oldOutlineBox)))
1387             fullRepaint = true;
1388     }
1389
1390     if (!repaintContainer)
1391         repaintContainer = v;
1392
1393     if (fullRepaint) {
1394         repaintUsingContainer(repaintContainer, pixelSnappedIntRect(oldBounds));
1395         if (newBounds != oldBounds)
1396             repaintUsingContainer(repaintContainer, pixelSnappedIntRect(newBounds));
1397         return true;
1398     }
1399
1400     if (newBounds == oldBounds && newOutlineBox == oldOutlineBox)
1401         return false;
1402
1403     LayoutUnit deltaLeft = newBounds.x() - oldBounds.x();
1404     if (deltaLeft > 0)
1405         repaintUsingContainer(repaintContainer, pixelSnappedIntRect(oldBounds.x(), oldBounds.y(), deltaLeft, oldBounds.height()));
1406     else if (deltaLeft < 0)
1407         repaintUsingContainer(repaintContainer, pixelSnappedIntRect(newBounds.x(), newBounds.y(), -deltaLeft, newBounds.height()));
1408
1409     LayoutUnit deltaRight = newBounds.maxX() - oldBounds.maxX();
1410     if (deltaRight > 0)
1411         repaintUsingContainer(repaintContainer, pixelSnappedIntRect(oldBounds.maxX(), newBounds.y(), deltaRight, newBounds.height()));
1412     else if (deltaRight < 0)
1413         repaintUsingContainer(repaintContainer, pixelSnappedIntRect(newBounds.maxX(), oldBounds.y(), -deltaRight, oldBounds.height()));
1414
1415     LayoutUnit deltaTop = newBounds.y() - oldBounds.y();
1416     if (deltaTop > 0)
1417         repaintUsingContainer(repaintContainer, pixelSnappedIntRect(oldBounds.x(), oldBounds.y(), oldBounds.width(), deltaTop));
1418     else if (deltaTop < 0)
1419         repaintUsingContainer(repaintContainer, pixelSnappedIntRect(newBounds.x(), newBounds.y(), newBounds.width(), -deltaTop));
1420
1421     LayoutUnit deltaBottom = newBounds.maxY() - oldBounds.maxY();
1422     if (deltaBottom > 0)
1423         repaintUsingContainer(repaintContainer, pixelSnappedIntRect(newBounds.x(), oldBounds.maxY(), newBounds.width(), deltaBottom));
1424     else if (deltaBottom < 0)
1425         repaintUsingContainer(repaintContainer, pixelSnappedIntRect(oldBounds.x(), newBounds.maxY(), oldBounds.width(), -deltaBottom));
1426
1427     if (newOutlineBox == oldOutlineBox)
1428         return false;
1429
1430     // We didn't move, but we did change size. Invalidate the delta, which will consist of possibly
1431     // two rectangles (but typically only one).
1432     RenderStyle* outlineStyle = outlineStyleForRepaint();
1433     LayoutUnit outlineWidth = outlineStyle->outlineSize();
1434     LayoutBoxExtent insetShadowExtent = style()->getBoxShadowInsetExtent();
1435     LayoutUnit width = absoluteValue(newOutlineBox.width() - oldOutlineBox.width());
1436     if (width) {
1437         LayoutUnit shadowLeft;
1438         LayoutUnit shadowRight;
1439         style()->getBoxShadowHorizontalExtent(shadowLeft, shadowRight);
1440         int borderRight = isBox() ? toRenderBox(this)->borderRight() : 0;
1441         LayoutUnit boxWidth = isBox() ? toRenderBox(this)->width() : ZERO_LAYOUT_UNIT;
1442         LayoutUnit minInsetRightShadowExtent = min<LayoutUnit>(-insetShadowExtent.right(), min<LayoutUnit>(newBounds.width(), oldBounds.width()));
1443         LayoutUnit borderWidth = max<LayoutUnit>(borderRight, max<LayoutUnit>(valueForLength(style()->borderTopRightRadius().width(), boxWidth, v), valueForLength(style()->borderBottomRightRadius().width(), boxWidth, v)));
1444         LayoutUnit decorationsWidth = max<LayoutUnit>(-outlineStyle->outlineOffset(), borderWidth + minInsetRightShadowExtent) + max<LayoutUnit>(outlineWidth, shadowRight);
1445         LayoutRect rightRect(newOutlineBox.x() + min(newOutlineBox.width(), oldOutlineBox.width()) - decorationsWidth,
1446             newOutlineBox.y(),
1447             width + decorationsWidth,
1448             max(newOutlineBox.height(), oldOutlineBox.height()));
1449         LayoutUnit right = min<LayoutUnit>(newBounds.maxX(), oldBounds.maxX());
1450         if (rightRect.x() < right) {
1451             rightRect.setWidth(min(rightRect.width(), right - rightRect.x()));
1452             repaintUsingContainer(repaintContainer, pixelSnappedIntRect(rightRect));
1453         }
1454     }
1455     LayoutUnit height = absoluteValue(newOutlineBox.height() - oldOutlineBox.height());
1456     if (height) {
1457         LayoutUnit shadowTop;
1458         LayoutUnit shadowBottom;
1459         style()->getBoxShadowVerticalExtent(shadowTop, shadowBottom);
1460         int borderBottom = isBox() ? toRenderBox(this)->borderBottom() : 0;
1461         LayoutUnit boxHeight = isBox() ? toRenderBox(this)->height() : ZERO_LAYOUT_UNIT;
1462         LayoutUnit minInsetBottomShadowExtent = min<LayoutUnit>(-insetShadowExtent.bottom(), min<LayoutUnit>(newBounds.height(), oldBounds.height()));
1463         LayoutUnit borderHeight = max<LayoutUnit>(borderBottom, max<LayoutUnit>(valueForLength(style()->borderBottomLeftRadius().height(), boxHeight, v), valueForLength(style()->borderBottomRightRadius().height(), boxHeight, v)));
1464         LayoutUnit decorationsHeight = max<LayoutUnit>(-outlineStyle->outlineOffset(), borderHeight + minInsetBottomShadowExtent) + max<LayoutUnit>(outlineWidth, shadowBottom);
1465         LayoutRect bottomRect(newOutlineBox.x(),
1466             min(newOutlineBox.maxY(), oldOutlineBox.maxY()) - decorationsHeight,
1467             max(newOutlineBox.width(), oldOutlineBox.width()),
1468             height + decorationsHeight);
1469         LayoutUnit bottom = min(newBounds.maxY(), oldBounds.maxY());
1470         if (bottomRect.y() < bottom) {
1471             bottomRect.setHeight(min(bottomRect.height(), bottom - bottomRect.y()));
1472             repaintUsingContainer(repaintContainer, pixelSnappedIntRect(bottomRect));
1473         }
1474     }
1475     return false;
1476 }
1477
1478 void RenderObject::repaintDuringLayoutIfMoved(const LayoutRect&)
1479 {
1480 }
1481
1482 void RenderObject::repaintOverhangingFloats(bool)
1483 {
1484 }
1485
1486 bool RenderObject::checkForRepaintDuringLayout() const
1487 {
1488     return !document()->view()->needsFullRepaint() && !hasLayer() && everHadLayout();
1489 }
1490
1491 LayoutRect RenderObject::rectWithOutlineForRepaint(RenderLayerModelObject* repaintContainer, LayoutUnit outlineWidth) const
1492 {
1493     LayoutRect r(clippedOverflowRectForRepaint(repaintContainer));
1494     r.inflate(outlineWidth);
1495     return r;
1496 }
1497
1498 LayoutRect RenderObject::clippedOverflowRectForRepaint(RenderLayerModelObject*) const
1499 {
1500     ASSERT_NOT_REACHED();
1501     return LayoutRect();
1502 }
1503
1504 void RenderObject::computeRectForRepaint(RenderLayerModelObject* repaintContainer, LayoutRect& rect, bool fixed) const
1505 {
1506     if (repaintContainer == this)
1507         return;
1508
1509     if (RenderObject* o = parent()) {
1510         if (o->isBlockFlow()) {
1511             RenderBlock* cb = toRenderBlock(o);
1512             if (cb->hasColumns())
1513                 cb->adjustRectForColumns(rect);
1514         }
1515
1516         if (o->hasOverflowClip()) {
1517             RenderBox* boxParent = toRenderBox(o);
1518             boxParent->applyCachedClipAndScrollOffsetForRepaint(rect);
1519             if (rect.isEmpty())
1520                 return;
1521         }
1522
1523         o->computeRectForRepaint(repaintContainer, rect, fixed);
1524     }
1525 }
1526
1527 void RenderObject::computeFloatRectForRepaint(RenderLayerModelObject*, FloatRect&, bool) const
1528 {
1529     ASSERT_NOT_REACHED();
1530 }
1531
1532 void RenderObject::dirtyLinesFromChangedChild(RenderObject*)
1533 {
1534 }
1535
1536 #ifndef NDEBUG
1537
1538 void RenderObject::showTreeForThis() const
1539 {
1540     if (node())
1541         node()->showTreeForThis();
1542 }
1543
1544 void RenderObject::showRenderTreeForThis() const
1545 {
1546     showRenderTree(this, 0);
1547 }
1548
1549 void RenderObject::showLineTreeForThis() const
1550 {
1551     if (containingBlock())
1552         containingBlock()->showLineTreeAndMark(0, 0, 0, 0, this);
1553 }
1554
1555 void RenderObject::showRenderObject() const
1556 {
1557     showRenderObject(0);
1558 }
1559
1560 void RenderObject::showRenderObject(int printedCharacters) const
1561 {
1562     // As this function is intended to be used when debugging, the
1563     // this pointer may be 0.
1564     if (!this) {
1565         fputs("(null)\n", stderr);
1566         return;
1567     }
1568
1569     printedCharacters += fprintf(stderr, "%s %p", renderName(), this);
1570
1571     if (node()) {
1572         if (printedCharacters)
1573             for (; printedCharacters < showTreeCharacterOffset; printedCharacters++)
1574                 fputc(' ', stderr);
1575         fputc('\t', stderr);
1576         node()->showNode();
1577     } else
1578         fputc('\n', stderr);
1579 }
1580
1581 void RenderObject::showRenderTreeAndMark(const RenderObject* markedObject1, const char* markedLabel1, const RenderObject* markedObject2, const char* markedLabel2, int depth) const
1582 {
1583     int printedCharacters = 0;
1584     if (markedObject1 == this && markedLabel1)
1585         printedCharacters += fprintf(stderr, "%s", markedLabel1);
1586     if (markedObject2 == this && markedLabel2)
1587         printedCharacters += fprintf(stderr, "%s", markedLabel2);
1588     for (; printedCharacters < depth * 2; printedCharacters++)
1589         fputc(' ', stderr);
1590
1591     showRenderObject(printedCharacters);
1592     if (!this)
1593         return;
1594
1595     for (const RenderObject* child = firstChild(); child; child = child->nextSibling())
1596         child->showRenderTreeAndMark(markedObject1, markedLabel1, markedObject2, markedLabel2, depth + 1);
1597 }
1598
1599 #endif // NDEBUG
1600
1601 Color RenderObject::selectionBackgroundColor() const
1602 {
1603     Color color;
1604     if (style()->userSelect() != SELECT_NONE) {
1605         RefPtr<RenderStyle> pseudoStyle = getUncachedPseudoStyle(SELECTION);
1606         if (pseudoStyle && pseudoStyle->visitedDependentColor(CSSPropertyBackgroundColor).isValid())
1607             color = pseudoStyle->visitedDependentColor(CSSPropertyBackgroundColor).blendWithWhite();
1608         else
1609             color = frame()->selection()->isFocusedAndActive() ?
1610                     theme()->activeSelectionBackgroundColor() :
1611                     theme()->inactiveSelectionBackgroundColor();
1612     }
1613
1614     return color;
1615 }
1616
1617 Color RenderObject::selectionColor(int colorProperty) const
1618 {
1619     Color color;
1620     // If the element is unselectable, or we are only painting the selection,
1621     // don't override the foreground color with the selection foreground color.
1622     if (style()->userSelect() == SELECT_NONE
1623         || (frame()->view()->paintBehavior() & PaintBehaviorSelectionOnly))
1624         return color;
1625
1626     if (RefPtr<RenderStyle> pseudoStyle = getUncachedPseudoStyle(SELECTION)) {
1627         color = pseudoStyle->visitedDependentColor(colorProperty);
1628         if (!color.isValid())
1629             color = pseudoStyle->visitedDependentColor(CSSPropertyColor);
1630     } else
1631         color = frame()->selection()->isFocusedAndActive() ?
1632                 theme()->activeSelectionForegroundColor() :
1633                 theme()->inactiveSelectionForegroundColor();
1634
1635     return color;
1636 }
1637
1638 Color RenderObject::selectionForegroundColor() const
1639 {
1640     return selectionColor(CSSPropertyWebkitTextFillColor);
1641 }
1642
1643 Color RenderObject::selectionEmphasisMarkColor() const
1644 {
1645     return selectionColor(CSSPropertyWebkitTextEmphasisColor);
1646 }
1647
1648 void RenderObject::selectionStartEnd(int& spos, int& epos) const
1649 {
1650     view()->selectionStartEnd(spos, epos);
1651 }
1652
1653 void RenderObject::handleDynamicFloatPositionChange()
1654 {
1655     // We have gone from not affecting the inline status of the parent flow to suddenly
1656     // having an impact.  See if there is a mismatch between the parent flow's
1657     // childrenInline() state and our state.
1658     setInline(style()->isDisplayInlineType());
1659     if (isInline() != parent()->childrenInline()) {
1660         if (!isInline())
1661             toRenderBoxModelObject(parent())->childBecameNonInline(this);
1662         else {
1663             // An anonymous block must be made to wrap this inline.
1664             RenderBlock* block = toRenderBlock(parent())->createAnonymousBlock();
1665             RenderObjectChildList* childlist = parent()->virtualChildren();
1666             childlist->insertChildNode(parent(), block, this);
1667             block->children()->appendChildNode(block, childlist->removeChildNode(parent(), this));
1668         }
1669     }
1670 }
1671
1672 void RenderObject::setAnimatableStyle(PassRefPtr<RenderStyle> style)
1673 {
1674     if (!isText() && style)
1675         setStyle(animation()->updateAnimations(this, style.get()));
1676     else
1677         setStyle(style);
1678 }
1679
1680 StyleDifference RenderObject::adjustStyleDifference(StyleDifference diff, unsigned contextSensitiveProperties) const
1681 {
1682 #if USE(ACCELERATED_COMPOSITING)
1683     // If transform changed, and we are not composited, need to do a layout.
1684     if (contextSensitiveProperties & ContextSensitivePropertyTransform) {
1685         // Text nodes share style with their parents but transforms don't apply to them,
1686         // hence the !isText() check.
1687         // FIXME: when transforms are taken into account for overflow, we will need to do a layout.
1688         if (!isText() && (!hasLayer() || !toRenderLayerModelObject(this)->layer()->isComposited())) {
1689             // We need to set at least SimplifiedLayout, but if PositionedMovementOnly is already set
1690             // then we actually need SimplifiedLayoutAndPositionedMovement.
1691             if (!hasLayer())
1692                 diff = StyleDifferenceLayout; // FIXME: Do this for now since SimplifiedLayout cannot handle updating floating objects lists.
1693             else if (diff < StyleDifferenceLayoutPositionedMovementOnly)
1694                 diff = StyleDifferenceSimplifiedLayout;
1695             else if (diff < StyleDifferenceSimplifiedLayout)
1696                 diff = StyleDifferenceSimplifiedLayoutAndPositionedMovement;
1697         } else if (diff < StyleDifferenceRecompositeLayer)
1698             diff = StyleDifferenceRecompositeLayer;
1699     }
1700
1701     // If opacity changed, and we are not composited, need to repaint (also
1702     // ignoring text nodes)
1703     if (contextSensitiveProperties & ContextSensitivePropertyOpacity) {
1704         if (!isText() && (!hasLayer() || !toRenderLayerModelObject(this)->layer()->isComposited()))
1705             diff = StyleDifferenceRepaintLayer;
1706         else if (diff < StyleDifferenceRecompositeLayer)
1707             diff = StyleDifferenceRecompositeLayer;
1708     }
1709     
1710 #if ENABLE(CSS_FILTERS)
1711     if ((contextSensitiveProperties & ContextSensitivePropertyFilter) && hasLayer()) {
1712         RenderLayer* layer = toRenderLayerModelObject(this)->layer();
1713         if (!layer->isComposited() || layer->paintsWithFilters())
1714             diff = StyleDifferenceRepaintLayer;
1715         else if (diff < StyleDifferenceRecompositeLayer)
1716             diff = StyleDifferenceRecompositeLayer;
1717     }
1718 #endif
1719     
1720     // The answer to requiresLayer() for plugins, iframes, and canvas can change without the actual
1721     // style changing, since it depends on whether we decide to composite these elements. When the
1722     // layer status of one of these elements changes, we need to force a layout.
1723     if (diff == StyleDifferenceEqual && style() && isLayerModelObject()) {
1724         if (hasLayer() != toRenderLayerModelObject(this)->requiresLayer())
1725             diff = StyleDifferenceLayout;
1726     }
1727 #else
1728     UNUSED_PARAM(contextSensitiveProperties);
1729 #endif
1730
1731     // If we have no layer(), just treat a RepaintLayer hint as a normal Repaint.
1732     if (diff == StyleDifferenceRepaintLayer && !hasLayer())
1733         diff = StyleDifferenceRepaint;
1734
1735     return diff;
1736 }
1737
1738 void RenderObject::setStyle(PassRefPtr<RenderStyle> style)
1739 {
1740     if (m_style == style) {
1741 #if USE(ACCELERATED_COMPOSITING)
1742         // We need to run through adjustStyleDifference() for iframes, plugins, and canvas so
1743         // style sharing is disabled for them. That should ensure that we never hit this code path.
1744         ASSERT(!isRenderIFrame() && !isEmbeddedObject() && !isCanvas());
1745 #endif
1746         return;
1747     }
1748
1749     StyleDifference diff = StyleDifferenceEqual;
1750     unsigned contextSensitiveProperties = ContextSensitivePropertyNone;
1751     if (m_style)
1752         diff = m_style->diff(style.get(), contextSensitiveProperties);
1753
1754     diff = adjustStyleDifference(diff, contextSensitiveProperties);
1755
1756     styleWillChange(diff, style.get());
1757     
1758     RefPtr<RenderStyle> oldStyle = m_style.release();
1759     m_style = style;
1760
1761     updateFillImages(oldStyle ? oldStyle->backgroundLayers() : 0, m_style ? m_style->backgroundLayers() : 0);
1762     updateFillImages(oldStyle ? oldStyle->maskLayers() : 0, m_style ? m_style->maskLayers() : 0);
1763
1764     updateImage(oldStyle ? oldStyle->borderImage().image() : 0, m_style ? m_style->borderImage().image() : 0);
1765     updateImage(oldStyle ? oldStyle->maskBoxImage().image() : 0, m_style ? m_style->maskBoxImage().image() : 0);
1766
1767     // We need to ensure that view->maximalOutlineSize() is valid for any repaints that happen
1768     // during styleDidChange (it's used by clippedOverflowRectForRepaint()).
1769     if (m_style->outlineWidth() > 0 && m_style->outlineSize() > maximalOutlineSize(PaintPhaseOutline))
1770         toRenderView(document()->renderer())->setMaximalOutlineSize(m_style->outlineSize());
1771
1772     bool doesNotNeedLayout = !m_parent || isText();
1773
1774     styleDidChange(diff, oldStyle.get());
1775
1776     // FIXME: |this| might be destroyed here. This can currently happen for a RenderTextFragment when
1777     // its first-letter block gets an update in RenderTextFragment::styleDidChange. For RenderTextFragment(s),
1778     // we will safely bail out with the doesNotNeedLayout flag. We might want to broaden this condition
1779     // in the future as we move renderer changes out of layout and into style changes.
1780     if (doesNotNeedLayout)
1781         return;
1782
1783     // Now that the layer (if any) has been updated, we need to adjust the diff again,
1784     // check whether we should layout now, and decide if we need to repaint.
1785     StyleDifference updatedDiff = adjustStyleDifference(diff, contextSensitiveProperties);
1786     
1787     if (diff <= StyleDifferenceLayoutPositionedMovementOnly) {
1788         if (updatedDiff == StyleDifferenceLayout)
1789             setNeedsLayoutAndPrefWidthsRecalc();
1790         else if (updatedDiff == StyleDifferenceLayoutPositionedMovementOnly)
1791             setNeedsPositionedMovementLayout();
1792         else if (updatedDiff == StyleDifferenceSimplifiedLayoutAndPositionedMovement) {
1793             setNeedsPositionedMovementLayout();
1794             setNeedsSimplifiedNormalFlowLayout();
1795         } else if (updatedDiff == StyleDifferenceSimplifiedLayout)
1796             setNeedsSimplifiedNormalFlowLayout();
1797     }
1798     
1799     if (updatedDiff == StyleDifferenceRepaintLayer || updatedDiff == StyleDifferenceRepaint) {
1800         // Do a repaint with the new style now, e.g., for example if we go from
1801         // not having an outline to having an outline.
1802         repaint();
1803     }
1804 }
1805
1806 void RenderObject::setStyleInternal(PassRefPtr<RenderStyle> style)
1807 {
1808     m_style = style;
1809 }
1810
1811 void RenderObject::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
1812 {
1813     if (m_style) {
1814         // If our z-index changes value or our visibility changes,
1815         // we need to dirty our stacking context's z-order list.
1816         if (newStyle) {
1817             bool visibilityChanged = m_style->visibility() != newStyle->visibility() 
1818                 || m_style->zIndex() != newStyle->zIndex() 
1819                 || m_style->hasAutoZIndex() != newStyle->hasAutoZIndex();
1820 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(DRAGGABLE_REGION)
1821             if (visibilityChanged)
1822                 document()->setAnnotatedRegionsDirty(true);
1823 #endif
1824             if (visibilityChanged && AXObjectCache::accessibilityEnabled())
1825                 document()->axObjectCache()->childrenChanged(this);
1826
1827             // Keep layer hierarchy visibility bits up to date if visibility changes.
1828             if (m_style->visibility() != newStyle->visibility()) {
1829                 if (RenderLayer* l = enclosingLayer()) {
1830                     if (newStyle->visibility() == VISIBLE)
1831                         l->setHasVisibleContent();
1832                     else if (l->hasVisibleContent() && (this == l->renderer() || l->renderer()->style()->visibility() != VISIBLE)) {
1833                         l->dirtyVisibleContentStatus();
1834                         if (diff > StyleDifferenceRepaintLayer)
1835                             repaint();
1836                     }
1837                 }
1838             }
1839         }
1840
1841         if (m_parent && (diff == StyleDifferenceRepaint || newStyle->outlineSize() < m_style->outlineSize()))
1842             repaint();
1843         if (isFloating() && (m_style->floating() != newStyle->floating()))
1844             // For changes in float styles, we need to conceivably remove ourselves
1845             // from the floating objects list.
1846             toRenderBox(this)->removeFloatingOrPositionedChildFromBlockLists();
1847         else if (isOutOfFlowPositioned() && (m_style->position() != newStyle->position()))
1848             // For changes in positioning styles, we need to conceivably remove ourselves
1849             // from the positioned objects list.
1850             toRenderBox(this)->removeFloatingOrPositionedChildFromBlockLists();
1851
1852         s_affectsParentBlock = isFloatingOrOutOfFlowPositioned()
1853             && (!newStyle->isFloating() && !newStyle->hasOutOfFlowPosition())
1854             && parent() && (parent()->isBlockFlow() || parent()->isRenderInline());
1855
1856         // reset style flags
1857         if (diff == StyleDifferenceLayout || diff == StyleDifferenceLayoutPositionedMovementOnly) {
1858             setFloating(false);
1859             setPositioned(false);
1860             setRelPositioned(false);
1861             setStickyPositioned(false);
1862         }
1863         setHorizontalWritingMode(true);
1864         setPaintBackground(false);
1865         setHasOverflowClip(false);
1866         setHasTransform(false);
1867         setHasReflection(false);
1868     } else
1869         s_affectsParentBlock = false;
1870
1871     if (view()->frameView()) {
1872         bool shouldBlitOnFixedBackgroundImage = false;
1873 #if ENABLE(FAST_MOBILE_SCROLLING)
1874         // On low-powered/mobile devices, preventing blitting on a scroll can cause noticeable delays
1875         // when scrolling a page with a fixed background image. As an optimization, assuming there are
1876         // no fixed positoned elements on the page, we can acclerate scrolling (via blitting) if we
1877         // ignore the CSS property "background-attachment: fixed".
1878 #if PLATFORM(QT)
1879         if (view()->frameView()->delegatesScrolling())
1880 #endif
1881             shouldBlitOnFixedBackgroundImage = true;
1882 #endif
1883
1884         bool newStyleSlowScroll = newStyle && !shouldBlitOnFixedBackgroundImage && newStyle->hasFixedBackgroundImage();
1885         bool oldStyleSlowScroll = m_style && !shouldBlitOnFixedBackgroundImage && m_style->hasFixedBackgroundImage();
1886         if (oldStyleSlowScroll != newStyleSlowScroll) {
1887             if (oldStyleSlowScroll)
1888                 view()->frameView()->removeSlowRepaintObject();
1889             if (newStyleSlowScroll)
1890                 view()->frameView()->addSlowRepaintObject();
1891         }
1892     }
1893 }
1894
1895 static bool areNonIdenticalCursorListsEqual(const RenderStyle* a, const RenderStyle* b)
1896 {
1897     ASSERT(a->cursors() != b->cursors());
1898     return a->cursors() && b->cursors() && *a->cursors() == *b->cursors();
1899 }
1900
1901 static inline bool areCursorsEqual(const RenderStyle* a, const RenderStyle* b)
1902 {
1903     return a->cursor() == b->cursor() && (a->cursors() == b->cursors() || areNonIdenticalCursorListsEqual(a, b));
1904 }
1905
1906 void RenderObject::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
1907 {
1908     if (s_affectsParentBlock)
1909         handleDynamicFloatPositionChange();
1910
1911     if (!m_parent)
1912         return;
1913     
1914     if (diff == StyleDifferenceLayout || diff == StyleDifferenceSimplifiedLayout) {
1915         RenderCounter::rendererStyleChanged(this, oldStyle, m_style.get());
1916
1917         // If the object already needs layout, then setNeedsLayout won't do
1918         // any work. But if the containing block has changed, then we may need
1919         // to mark the new containing blocks for layout. The change that can
1920         // directly affect the containing block of this object is a change to
1921         // the position style.
1922         if (needsLayout() && oldStyle->position() != m_style->position())
1923             markContainingBlocksForLayout();
1924
1925         if (diff == StyleDifferenceLayout)
1926             setNeedsLayoutAndPrefWidthsRecalc();
1927         else
1928             setNeedsSimplifiedNormalFlowLayout();
1929     } else if (diff == StyleDifferenceSimplifiedLayoutAndPositionedMovement) {
1930         setNeedsPositionedMovementLayout();
1931         setNeedsSimplifiedNormalFlowLayout();
1932     } else if (diff == StyleDifferenceLayoutPositionedMovementOnly)
1933         setNeedsPositionedMovementLayout();
1934
1935     // Don't check for repaint here; we need to wait until the layer has been
1936     // updated by subclasses before we know if we have to repaint (in setStyle()).
1937
1938     if (oldStyle && !areCursorsEqual(oldStyle, style())) {
1939         if (Frame* frame = this->frame())
1940             frame->eventHandler()->dispatchFakeMouseMoveEventSoon();
1941     }
1942 }
1943
1944 void RenderObject::propagateStyleToAnonymousChildren(bool blockChildrenOnly)
1945 {
1946     // FIXME: We could save this call when the change only affected non-inherited properties.
1947     for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
1948         if (!child->isAnonymous() || child->style()->styleType() != NOPSEUDO)
1949             continue;
1950
1951         if (blockChildrenOnly && !child->isRenderBlock())
1952             continue;
1953
1954 #if ENABLE(FULLSCREEN_API)
1955         if (child->isRenderFullScreen() || child->isRenderFullScreenPlaceholder())
1956             continue;
1957 #endif
1958
1959         RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(style(), child->style()->display());
1960         if (style()->specifiesColumns()) {
1961             if (child->style()->specifiesColumns())
1962                 newStyle->inheritColumnPropertiesFrom(style());
1963             if (child->style()->columnSpan())
1964                 newStyle->setColumnSpan(ColumnSpanAll);
1965         }
1966
1967         // Preserve the position style of anonymous block continuations as they can have relative or sticky position when
1968         // they contain block descendants of relative or sticky positioned inlines.
1969         if (child->isInFlowPositioned() && toRenderBlock(child)->isAnonymousBlockContinuation())
1970             newStyle->setPosition(child->style()->position());
1971
1972         child->setStyle(newStyle.release());
1973     }
1974 }
1975
1976 void RenderObject::updateFillImages(const FillLayer* oldLayers, const FillLayer* newLayers)
1977 {
1978     // Optimize the common case
1979     if (oldLayers && !oldLayers->next() && newLayers && !newLayers->next() && (oldLayers->image() == newLayers->image()))
1980         return;
1981     
1982     // Go through the new layers and addClients first, to avoid removing all clients of an image.
1983     for (const FillLayer* currNew = newLayers; currNew; currNew = currNew->next()) {
1984         if (currNew->image())
1985             currNew->image()->addClient(this);
1986     }
1987
1988     for (const FillLayer* currOld = oldLayers; currOld; currOld = currOld->next()) {
1989         if (currOld->image())
1990             currOld->image()->removeClient(this);
1991     }
1992 }
1993
1994 void RenderObject::updateImage(StyleImage* oldImage, StyleImage* newImage)
1995 {
1996     if (oldImage != newImage) {
1997         if (oldImage)
1998             oldImage->removeClient(this);
1999         if (newImage)
2000             newImage->addClient(this);
2001     }
2002 }
2003
2004 LayoutRect RenderObject::viewRect() const
2005 {
2006     return view()->viewRect();
2007 }
2008
2009 FloatPoint RenderObject::localToAbsolute(const FloatPoint& localPoint, MapCoordinatesFlags mode) const
2010 {
2011     TransformState transformState(TransformState::ApplyTransformDirection, localPoint);
2012     mapLocalToContainer(0, transformState, mode | ApplyContainerFlip);
2013     transformState.flatten();
2014     
2015     return transformState.lastPlanarPoint();
2016 }
2017
2018 FloatPoint RenderObject::absoluteToLocal(const FloatPoint& containerPoint, MapCoordinatesFlags mode) const
2019 {
2020     TransformState transformState(TransformState::UnapplyInverseTransformDirection, containerPoint);
2021     mapAbsoluteToLocalPoint(mode, transformState);
2022     transformState.flatten();
2023     
2024     return transformState.lastPlanarPoint();
2025 }
2026
2027 void RenderObject::mapLocalToContainer(RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags mode, bool* wasFixed) const
2028 {
2029     if (repaintContainer == this)
2030         return;
2031
2032     RenderObject* o = parent();
2033     if (!o)
2034         return;
2035
2036     // FIXME: this should call offsetFromContainer to share code, but I'm not sure it's ever called.
2037     LayoutPoint centerPoint = roundedLayoutPoint(transformState.mappedPoint());
2038     if (mode & ApplyContainerFlip && o->isBox()) {
2039         if (o->style()->isFlippedBlocksWritingMode())
2040             transformState.move(toRenderBox(o)->flipForWritingModeIncludingColumns(roundedLayoutPoint(transformState.mappedPoint())) - centerPoint);
2041         mode &= ~ApplyContainerFlip;
2042     }
2043
2044     LayoutSize columnOffset;
2045     o->adjustForColumns(columnOffset, roundedLayoutPoint(transformState.mappedPoint()));
2046     if (!columnOffset.isZero())
2047         transformState.move(columnOffset);
2048
2049     if (o->hasOverflowClip())
2050         transformState.move(-toRenderBox(o)->scrolledContentOffset());
2051
2052     o->mapLocalToContainer(repaintContainer, transformState, mode, wasFixed);
2053 }
2054
2055 const RenderObject* RenderObject::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
2056 {
2057     ASSERT_UNUSED(ancestorToStopAt, ancestorToStopAt != this);
2058
2059     RenderObject* container = parent();
2060     if (!container)
2061         return 0;
2062
2063     // FIXME: this should call offsetFromContainer to share code, but I'm not sure it's ever called.
2064     LayoutSize offset;
2065     if (container->hasOverflowClip())
2066         offset = -toRenderBox(container)->scrolledContentOffset();
2067
2068     geometryMap.push(this, offset, hasColumns());
2069     
2070     return container;
2071 }
2072
2073 void RenderObject::mapAbsoluteToLocalPoint(MapCoordinatesFlags mode, TransformState& transformState) const
2074 {
2075     RenderObject* o = parent();
2076     if (o) {
2077         o->mapAbsoluteToLocalPoint(mode, transformState);
2078         if (o->hasOverflowClip())
2079             transformState.move(toRenderBox(o)->scrolledContentOffset());
2080     }
2081 }
2082
2083 bool RenderObject::shouldUseTransformFromContainer(const RenderObject* containerObject) const
2084 {
2085 #if ENABLE(3D_RENDERING)
2086     // hasTransform() indicates whether the object has transform, transform-style or perspective. We just care about transform,
2087     // so check the layer's transform directly.
2088     return (hasLayer() && toRenderLayerModelObject(this)->layer()->transform()) || (containerObject && containerObject->style()->hasPerspective());
2089 #else
2090     UNUSED_PARAM(containerObject);
2091     return hasTransform();
2092 #endif
2093 }
2094
2095 void RenderObject::getTransformFromContainer(const RenderObject* containerObject, const LayoutSize& offsetInContainer, TransformationMatrix& transform) const
2096 {
2097     transform.makeIdentity();
2098     transform.translate(offsetInContainer.width(), offsetInContainer.height());
2099     RenderLayer* layer;
2100     if (hasLayer() && (layer = toRenderLayerModelObject(this)->layer()) && layer->transform())
2101         transform.multiply(layer->currentTransform());
2102     
2103 #if ENABLE(3D_RENDERING)
2104     if (containerObject && containerObject->hasLayer() && containerObject->style()->hasPerspective()) {
2105         // Perpsective on the container affects us, so we have to factor it in here.
2106         ASSERT(containerObject->hasLayer());
2107         FloatPoint perspectiveOrigin = toRenderLayerModelObject(containerObject)->layer()->perspectiveOrigin();
2108
2109         TransformationMatrix perspectiveMatrix;
2110         perspectiveMatrix.applyPerspective(containerObject->style()->perspective());
2111         
2112         transform.translateRight3d(-perspectiveOrigin.x(), -perspectiveOrigin.y(), 0);
2113         transform = perspectiveMatrix * transform;
2114         transform.translateRight3d(perspectiveOrigin.x(), perspectiveOrigin.y(), 0);
2115     }
2116 #else
2117     UNUSED_PARAM(containerObject);
2118 #endif
2119 }
2120
2121 FloatQuad RenderObject::localToContainerQuad(const FloatQuad& localQuad, RenderLayerModelObject* repaintContainer, MapCoordinatesFlags mode, bool* wasFixed) const
2122 {
2123     // Track the point at the center of the quad's bounding box. As mapLocalToContainer() calls offsetFromContainer(),
2124     // it will use that point as the reference point to decide which column's transform to apply in multiple-column blocks.
2125     TransformState transformState(TransformState::ApplyTransformDirection, localQuad.boundingBox().center(), localQuad);
2126     mapLocalToContainer(repaintContainer, transformState, mode | ApplyContainerFlip | UseTransforms, wasFixed);
2127     transformState.flatten();
2128     
2129     return transformState.lastPlanarQuad();
2130 }
2131
2132 FloatPoint RenderObject::localToContainerPoint(const FloatPoint& localPoint, RenderLayerModelObject* repaintContainer, MapCoordinatesFlags mode, bool* wasFixed) const
2133 {
2134     TransformState transformState(TransformState::ApplyTransformDirection, localPoint);
2135     mapLocalToContainer(repaintContainer, transformState, mode | ApplyContainerFlip | UseTransforms, wasFixed);
2136     transformState.flatten();
2137
2138     return transformState.lastPlanarPoint();
2139 }
2140
2141 LayoutSize RenderObject::offsetFromContainer(RenderObject* o, const LayoutPoint& point, bool* offsetDependsOnPoint) const
2142 {
2143     ASSERT(o == container());
2144
2145     LayoutSize offset;
2146
2147     o->adjustForColumns(offset, point);
2148
2149     if (o->hasOverflowClip())
2150         offset -= toRenderBox(o)->scrolledContentOffset();
2151
2152     if (offsetDependsOnPoint)
2153         *offsetDependsOnPoint = hasColumns();
2154
2155     return offset;
2156 }
2157
2158 LayoutSize RenderObject::offsetFromAncestorContainer(RenderObject* container) const
2159 {
2160     LayoutSize offset;
2161     LayoutPoint referencePoint;
2162     const RenderObject* currContainer = this;
2163     do {
2164         RenderObject* nextContainer = currContainer->container();
2165         ASSERT(nextContainer);  // This means we reached the top without finding container.
2166         if (!nextContainer)
2167             break;
2168         ASSERT(!currContainer->hasTransform());
2169         LayoutSize currentOffset = currContainer->offsetFromContainer(nextContainer, referencePoint);
2170         offset += currentOffset;
2171         referencePoint.move(currentOffset);
2172         currContainer = nextContainer;
2173     } while (currContainer != container);
2174
2175     return offset;
2176 }
2177
2178 LayoutRect RenderObject::localCaretRect(InlineBox*, int, LayoutUnit* extraWidthToEndOfLine)
2179 {
2180     if (extraWidthToEndOfLine)
2181         *extraWidthToEndOfLine = 0;
2182
2183     return LayoutRect();
2184 }
2185
2186 bool RenderObject::isRooted(RenderView** view) const
2187 {
2188     const RenderObject* o = this;
2189     while (o->parent())
2190         o = o->parent();
2191
2192     if (!o->isRenderView())
2193         return false;
2194
2195     if (view)
2196         *view = const_cast<RenderView*>(toRenderView(o));
2197
2198     return true;
2199 }
2200
2201 RenderObject* RenderObject::rendererForRootBackground()
2202 {
2203     ASSERT(isRoot());
2204     if (!hasBackground() && node() && node()->hasTagName(HTMLNames::htmlTag)) {
2205         // Locate the <body> element using the DOM. This is easier than trying
2206         // to crawl around a render tree with potential :before/:after content and
2207         // anonymous blocks created by inline <body> tags etc. We can locate the <body>
2208         // render object very easily via the DOM.
2209         HTMLElement* body = document()->body();
2210         RenderObject* bodyObject = (body && body->hasLocalName(bodyTag)) ? body->renderer() : 0;
2211         if (bodyObject)
2212             return bodyObject;
2213     }
2214     
2215     return this;
2216 }
2217
2218 RespectImageOrientationEnum RenderObject::shouldRespectImageOrientation() const
2219 {
2220     // Respect the image's orientation if it's being used as a full-page image or it's
2221     // an <img> and the setting to respect it everywhere is set.
2222     return document()->isImageDocument() || (document()->settings() && document()->settings()->shouldRespectImageOrientation() && node() && (node()->hasTagName(HTMLNames::imgTag) || node()->hasTagName(HTMLNames::webkitInnerImageTag))) ? RespectImageOrientation : DoNotRespectImageOrientation;
2223 }
2224
2225 bool RenderObject::hasOutlineAnnotation() const
2226 {
2227     return node() && node()->isLink() && document()->printing();
2228 }
2229
2230 RenderObject* RenderObject::container(const RenderLayerModelObject* repaintContainer, bool* repaintContainerSkipped) const
2231 {
2232     if (repaintContainerSkipped)
2233         *repaintContainerSkipped = false;
2234
2235     // This method is extremely similar to containingBlock(), but with a few notable
2236     // exceptions.
2237     // (1) It can be used on orphaned subtrees, i.e., it can be called safely even when
2238     // the object is not part of the primary document subtree yet.
2239     // (2) For normal flow elements, it just returns the parent.
2240     // (3) For absolute positioned elements, it will return a relative positioned inline.
2241     // containingBlock() simply skips relpositioned inlines and lets an enclosing block handle
2242     // the layout of the positioned object.  This does mean that computePositionedLogicalWidth and
2243     // computePositionedLogicalHeight have to use container().
2244     RenderObject* o = parent();
2245
2246     if (isText())
2247         return o;
2248
2249     EPosition pos = m_style->position();
2250     if (pos == FixedPosition) {
2251         // container() can be called on an object that is not in the
2252         // tree yet.  We don't call view() since it will assert if it
2253         // can't get back to the canvas.  Instead we just walk as high up
2254         // as we can.  If we're in the tree, we'll get the root.  If we
2255         // aren't we'll get the root of our little subtree (most likely
2256         // we'll just return 0).
2257         // FIXME: The definition of view() has changed to not crawl up the render tree.  It might
2258         // be safe now to use it.
2259         while (o && o->parent() && !(o->hasTransform() && o->isRenderBlock())) {
2260 #if ENABLE(SVG)
2261             // foreignObject is the containing block for its contents.
2262             if (o->isSVGForeignObject())
2263                 break;
2264 #endif
2265             // The render flow thread is the top most containing block
2266             // for the fixed positioned elements.
2267             if (o->isRenderFlowThread())
2268                 break;
2269
2270             if (repaintContainerSkipped && o == repaintContainer)
2271                 *repaintContainerSkipped = true;
2272
2273             o = o->parent();
2274         }
2275     } else if (pos == AbsolutePosition) {
2276         // Same goes here.  We technically just want our containing block, but
2277         // we may not have one if we're part of an uninstalled subtree.  We'll
2278         // climb as high as we can though.
2279         while (o && o->style()->position() == StaticPosition && !o->isRenderView() && !(o->hasTransform() && o->isRenderBlock())) {
2280 #if ENABLE(SVG)
2281             if (o->isSVGForeignObject()) // foreignObject is the containing block for contents inside it
2282                 break;
2283 #endif
2284             if (repaintContainerSkipped && o == repaintContainer)
2285                 *repaintContainerSkipped = true;
2286
2287             o = o->parent();
2288         }
2289     }
2290
2291     return o;
2292 }
2293
2294 bool RenderObject::isSelectionBorder() const
2295 {
2296     SelectionState st = selectionState();
2297     return st == SelectionStart || st == SelectionEnd || st == SelectionBoth;
2298 }
2299
2300 inline void RenderObject::clearLayoutRootIfNeeded() const
2301 {
2302     if (!documentBeingDestroyed() && frame()) {
2303         if (FrameView* view = frame()->view()) {
2304             if (view->layoutRoot() == this) {
2305                 ASSERT_NOT_REACHED();
2306                 // This indicates a failure to layout the child, which is why
2307                 // the layout root is still set to |this|. Make sure to clear it
2308                 // since we are getting destroyed.
2309                 view->clearLayoutRoot();
2310             }
2311         }
2312     }
2313 }
2314
2315 void RenderObject::willBeDestroyed()
2316 {
2317     // Destroy any leftover anonymous children.
2318     RenderObjectChildList* children = virtualChildren();
2319     if (children)
2320         children->destroyLeftoverChildren();
2321
2322     // If this renderer is being autoscrolled, stop the autoscroll timer
2323     
2324     // FIXME: RenderObject::destroy should not get called with a renderer whose document
2325     // has a null frame, so we assert this. However, we don't want release builds to crash which is why we
2326     // check that the frame is not null.
2327     ASSERT(frame());
2328     if (frame() && frame()->eventHandler()->autoscrollRenderer() == this)
2329         frame()->eventHandler()->stopAutoscrollTimer(true);
2330
2331     if (AXObjectCache::accessibilityEnabled()) {
2332         document()->axObjectCache()->childrenChanged(this->parent());
2333         document()->axObjectCache()->remove(this);
2334     }
2335     animation()->cancelAnimations(this);
2336
2337     remove();
2338
2339 #ifndef NDEBUG
2340     if (!documentBeingDestroyed() && view() && view()->hasRenderNamedFlowThreads()) {
2341         // After remove, the object and the associated information should not be in any flow thread.
2342         const RenderNamedFlowThreadList* flowThreadList = view()->flowThreadController()->renderNamedFlowThreadList();
2343         for (RenderNamedFlowThreadList::const_iterator iter = flowThreadList->begin(); iter != flowThreadList->end(); ++iter) {
2344             const RenderNamedFlowThread* renderFlowThread = *iter;
2345             ASSERT(!renderFlowThread->hasChild(this));
2346             ASSERT(!renderFlowThread->hasChildInfo(this));
2347         }
2348     }
2349 #endif
2350
2351     // If this renderer had a parent, remove should have destroyed any counters
2352     // attached to this renderer and marked the affected other counters for
2353     // reevaluation. This apparently redundant check is here for the case when
2354     // this renderer had no parent at the time remove() was called.
2355
2356     if (hasCounterNodeMap())
2357         RenderCounter::destroyCounterNodes(this);
2358
2359     // FIXME: Would like to do this in RenderBoxModelObject, but the timing is so complicated that this can't easily
2360     // be moved into RenderBoxModelObject::destroy.
2361     if (hasLayer()) {
2362         setHasLayer(false);
2363         toRenderLayerModelObject(this)->destroyLayer();
2364     }
2365
2366     setAncestorLineBoxDirty(false);
2367
2368     clearLayoutRootIfNeeded();
2369 }
2370
2371 void RenderObject::insertedIntoTree()
2372 {
2373     // FIXME: We should ASSERT(isRooted()) here but generated content makes some out-of-order insertion.
2374
2375     // Keep our layer hierarchy updated. Optimize for the common case where we don't have any children
2376     // and don't have a layer attached to ourselves.
2377     RenderLayer* layer = 0;
2378     if (firstChild() || hasLayer()) {
2379         layer = parent()->enclosingLayer();
2380         addLayers(layer);
2381     }
2382
2383     // If |this| is visible but this object was not, tell the layer it has some visible content
2384     // that needs to be drawn and layer visibility optimization can't be used
2385     if (parent()->style()->visibility() != VISIBLE && style()->visibility() == VISIBLE && !hasLayer()) {
2386         if (!layer)
2387             layer = parent()->enclosingLayer();
2388         if (layer)
2389             layer->setHasVisibleContent();
2390     }
2391
2392     if (!isFloating() && parent()->childrenInline())
2393         parent()->dirtyLinesFromChangedChild(this);
2394
2395     if (RenderNamedFlowThread* containerFlowThread = parent()->enclosingRenderNamedFlowThread())
2396         containerFlowThread->addFlowChild(this);
2397 }
2398
2399 void RenderObject::willBeRemovedFromTree()
2400 {
2401     // FIXME: We should ASSERT(isRooted()) but we have some out-of-order removals which would need to be fixed first.
2402
2403     // If we remove a visible child from an invisible parent, we don't know the layer visibility any more.
2404     RenderLayer* layer = 0;
2405     if (parent()->style()->visibility() != VISIBLE && style()->visibility() == VISIBLE && !hasLayer()) {
2406         if ((layer = parent()->enclosingLayer()))
2407             layer->dirtyVisibleContentStatus();
2408     }
2409
2410     // Keep our layer hierarchy updated.
2411     if (firstChild() || hasLayer()) {
2412         if (!layer)
2413             layer = parent()->enclosingLayer();
2414         removeLayers(layer);
2415     }
2416
2417     if (isOutOfFlowPositioned() && parent()->childrenInline())
2418         parent()->dirtyLinesFromChangedChild(this);
2419
2420     if (inRenderFlowThread()) {
2421         ASSERT(enclosingRenderFlowThread());
2422         enclosingRenderFlowThread()->removeFlowChildInfo(this);
2423     }
2424
2425     if (RenderNamedFlowThread* containerFlowThread = parent()->enclosingRenderNamedFlowThread())
2426         containerFlowThread->removeFlowChild(this);
2427
2428 #if ENABLE(SVG)
2429     // Update cached boundaries in SVG renderers, if a child is removed.
2430     parent()->setNeedsBoundariesUpdate();
2431 #endif
2432 }
2433
2434 void RenderObject::destroyAndCleanupAnonymousWrappers()
2435 {
2436     RenderObject* parent = this->parent();
2437
2438     // If the tree is destroyed or our parent is not anonymous, there is no need for a clean-up phase.
2439     if (documentBeingDestroyed() || !parent || !parent->isAnonymous()) {
2440         destroy();
2441         return;
2442     }
2443
2444     bool parentIsLeftOverAnonymousWrapper = false;
2445
2446     // Currently we only remove anonymous cells' wrapper but we should remove all unneeded
2447     // wrappers. See http://webkit.org/b/52123 as an example where this is needed.
2448     if (parent->isTableCell())
2449         parentIsLeftOverAnonymousWrapper = parent->firstChild() == this && parent->lastChild() == this;
2450
2451     destroy();
2452
2453     // WARNING: |this| is deleted here.
2454
2455     if (parentIsLeftOverAnonymousWrapper) {
2456         ASSERT(!parent->firstChild());
2457         parent->destroyAndCleanupAnonymousWrappers();
2458     }
2459 }
2460
2461 void RenderObject::destroy()
2462 {
2463     willBeDestroyed();
2464     arenaDelete(renderArena(), this);
2465 }
2466
2467 void RenderObject::arenaDelete(RenderArena* arena, void* base)
2468 {
2469     if (m_style) {
2470         for (const FillLayer* bgLayer = m_style->backgroundLayers(); bgLayer; bgLayer = bgLayer->next()) {
2471             if (StyleImage* backgroundImage = bgLayer->image())
2472                 backgroundImage->removeClient(this);
2473         }
2474
2475         for (const FillLayer* maskLayer = m_style->maskLayers(); maskLayer; maskLayer = maskLayer->next()) {
2476             if (StyleImage* maskImage = maskLayer->image())
2477                 maskImage->removeClient(this);
2478         }
2479
2480         if (StyleImage* borderImage = m_style->borderImage().image())
2481             borderImage->removeClient(this);
2482
2483         if (StyleImage* maskBoxImage = m_style->maskBoxImage().image())
2484             maskBoxImage->removeClient(this);
2485     }
2486
2487 #ifndef NDEBUG
2488     void* savedBase = baseOfRenderObjectBeingDeleted;
2489     baseOfRenderObjectBeingDeleted = base;
2490 #endif
2491     delete this;
2492 #ifndef NDEBUG
2493     baseOfRenderObjectBeingDeleted = savedBase;
2494 #endif
2495
2496     // Recover the size left there for us by operator delete and free the memory.
2497     arena->free(*(size_t*)base, base);
2498 }
2499
2500 VisiblePosition RenderObject::positionForPoint(const LayoutPoint&)
2501 {
2502     return createVisiblePosition(caretMinOffset(), DOWNSTREAM);
2503 }
2504
2505 void RenderObject::updateDragState(bool dragOn)
2506 {
2507     bool valueChanged = (dragOn != isDragging());
2508     setIsDragging(dragOn);
2509     if (valueChanged && style()->affectedByDragRules() && node())
2510         node()->setNeedsStyleRecalc();
2511     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
2512         curr->updateDragState(dragOn);
2513 }
2514
2515 bool RenderObject::isComposited() const
2516 {
2517     return hasLayer() && toRenderLayerModelObject(this)->layer()->isComposited();
2518 }
2519
2520 bool RenderObject::hitTest(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestFilter hitTestFilter)
2521 {
2522     bool inside = false;
2523     if (hitTestFilter != HitTestSelf) {
2524         // First test the foreground layer (lines and inlines).
2525         inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestForeground);
2526
2527         // Test floats next.
2528         if (!inside)
2529             inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestFloat);
2530
2531         // Finally test to see if the mouse is in the background (within a child block's background).
2532         if (!inside)
2533             inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestChildBlockBackgrounds);
2534     }
2535
2536     // See if the mouse is inside us but not any of our descendants
2537     if (hitTestFilter != HitTestDescendants && !inside)
2538         inside = nodeAtPoint(request, result, locationInContainer, accumulatedOffset, HitTestBlockBackground);
2539
2540     return inside;
2541 }
2542
2543 void RenderObject::updateHitTestResult(HitTestResult& result, const LayoutPoint& point)
2544 {
2545     if (result.innerNode())
2546         return;
2547
2548     Node* n = node();
2549     if (n) {
2550         result.setInnerNode(n);
2551         if (!result.innerNonSharedNode())
2552             result.setInnerNonSharedNode(n);
2553         result.setLocalPoint(point);
2554     }
2555 }
2556
2557 bool RenderObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, const HitTestLocation& /*locationInContainer*/, const LayoutPoint& /*accumulatedOffset*/, HitTestAction)
2558 {
2559     return false;
2560 }
2561
2562 void RenderObject::scheduleRelayout()
2563 {
2564     if (isRenderView()) {
2565         FrameView* view = toRenderView(this)->frameView();
2566         if (view)
2567             view->scheduleRelayout();
2568     } else {
2569         if (isRooted()) {
2570             if (RenderView* renderView = view()) {
2571                 if (FrameView* frameView = renderView->frameView())
2572                     frameView->scheduleRelayoutOfSubtree(this);
2573             }
2574         }
2575     }
2576 }
2577
2578 void RenderObject::layout()
2579 {
2580     ASSERT(needsLayout());
2581     RenderObject* child = firstChild();
2582     while (child) {
2583         child->layoutIfNeeded();
2584         ASSERT(!child->needsLayout());
2585         child = child->nextSibling();
2586     }
2587     setNeedsLayout(false);
2588 }
2589
2590 enum StyleCacheState {
2591     Cached,
2592     Uncached
2593 };
2594
2595 static PassRefPtr<RenderStyle> firstLineStyleForCachedUncachedType(StyleCacheState type, const RenderObject* renderer, RenderStyle* style)
2596 {
2597     const RenderObject* rendererForFirstLineStyle = renderer;
2598     if (renderer->isBeforeOrAfterContent())
2599         rendererForFirstLineStyle = renderer->parent();
2600
2601     if (rendererForFirstLineStyle->isBlockFlow()) {
2602         if (RenderBlock* firstLineBlock = rendererForFirstLineStyle->firstLineBlock()) {
2603             if (type == Cached)
2604                 return firstLineBlock->getCachedPseudoStyle(FIRST_LINE, style);
2605             return firstLineBlock->getUncachedPseudoStyle(FIRST_LINE, style, firstLineBlock == renderer ? style : 0);
2606         }
2607     } else if (!rendererForFirstLineStyle->isAnonymous() && rendererForFirstLineStyle->isRenderInline()) {
2608         RenderStyle* parentStyle = rendererForFirstLineStyle->parent()->firstLineStyle();
2609         if (parentStyle != rendererForFirstLineStyle->parent()->style()) {
2610             if (type == Cached) {
2611                 // A first-line style is in effect. Cache a first-line style for ourselves.
2612                 rendererForFirstLineStyle->style()->setHasPseudoStyle(FIRST_LINE_INHERITED);
2613                 return rendererForFirstLineStyle->getCachedPseudoStyle(FIRST_LINE_INHERITED, parentStyle);
2614             }
2615             return rendererForFirstLineStyle->getUncachedPseudoStyle(FIRST_LINE_INHERITED, parentStyle, style);
2616         }
2617     }
2618     return 0;
2619 }
2620
2621 PassRefPtr<RenderStyle> RenderObject::uncachedFirstLineStyle(RenderStyle* style) const
2622 {
2623     if (!document()->styleSheetCollection()->usesFirstLineRules())
2624         return 0;
2625
2626     ASSERT(!isText());
2627
2628     return firstLineStyleForCachedUncachedType(Uncached, this, style);
2629 }
2630
2631 RenderStyle* RenderObject::cachedFirstLineStyle() const
2632 {
2633     ASSERT(document()->styleSheetCollection()->usesFirstLineRules());
2634
2635     if (RefPtr<RenderStyle> style = firstLineStyleForCachedUncachedType(Cached, isText() ? parent() : this, m_style.get()))
2636         return style.get();
2637
2638     return m_style.get();
2639 }
2640
2641 RenderStyle* RenderObject::getCachedPseudoStyle(PseudoId pseudo, RenderStyle* parentStyle) const
2642 {
2643     if (pseudo < FIRST_INTERNAL_PSEUDOID && !style()->hasPseudoStyle(pseudo))
2644         return 0;
2645
2646     RenderStyle* cachedStyle = style()->getCachedPseudoStyle(pseudo);
2647     if (cachedStyle)
2648         return cachedStyle;
2649     
2650     RefPtr<RenderStyle> result = getUncachedPseudoStyle(pseudo, parentStyle);
2651     if (result)
2652         return style()->addCachedPseudoStyle(result.release());
2653     return 0;
2654 }
2655
2656 PassRefPtr<RenderStyle> RenderObject::getUncachedPseudoStyle(PseudoId pseudo, RenderStyle* parentStyle, RenderStyle* ownStyle) const
2657 {
2658     if (pseudo < FIRST_INTERNAL_PSEUDOID && !ownStyle && !style()->hasPseudoStyle(pseudo))
2659         return 0;
2660     
2661     if (!parentStyle) {
2662         ASSERT(!ownStyle);
2663         parentStyle = style();
2664     }
2665
2666     // FIXME: This "find nearest element parent" should be a helper function.
2667     Node* n = node();
2668     while (n && !n->isElementNode())
2669         n = n->parentNode();
2670     if (!n)
2671         return 0;
2672     Element* element = toElement(n);
2673
2674     if (pseudo == FIRST_LINE_INHERITED) {
2675         RefPtr<RenderStyle> result = document()->styleResolver()->styleForElement(element, parentStyle, DisallowStyleSharing);
2676         result->setStyleType(FIRST_LINE_INHERITED);
2677         return result.release();
2678     }
2679     return document()->styleResolver()->pseudoStyleForElement(pseudo, element, parentStyle);
2680 }
2681
2682 static Color decorationColor(RenderStyle* style)
2683 {
2684     Color result;
2685     if (style->textStrokeWidth() > 0) {
2686         // Prefer stroke color if possible but not if it's fully transparent.
2687         result = style->visitedDependentColor(CSSPropertyWebkitTextStrokeColor);
2688         if (result.alpha())
2689             return result;
2690     }
2691     
2692     result = style->visitedDependentColor(CSSPropertyWebkitTextFillColor);
2693     return result;
2694 }
2695
2696 void RenderObject::getTextDecorationColors(int decorations, Color& underline, Color& overline,
2697                                            Color& linethrough, bool quirksMode, bool firstlineStyle)
2698 {
2699     RenderObject* curr = this;
2700     RenderStyle* styleToUse = 0;
2701     do {
2702         styleToUse = curr->style(firstlineStyle);
2703         int currDecs = styleToUse->textDecoration();
2704         if (currDecs) {
2705             if (currDecs & UNDERLINE) {
2706                 decorations &= ~UNDERLINE;
2707                 underline = decorationColor(styleToUse);
2708             }
2709             if (currDecs & OVERLINE) {
2710                 decorations &= ~OVERLINE;
2711                 overline = decorationColor(styleToUse);
2712             }
2713             if (currDecs & LINE_THROUGH) {
2714                 decorations &= ~LINE_THROUGH;
2715                 linethrough = decorationColor(styleToUse);
2716             }
2717         }
2718         if (curr->isRubyText())
2719             return;
2720         curr = curr->parent();
2721         if (curr && curr->isAnonymousBlock() && toRenderBlock(curr)->continuation())
2722             curr = toRenderBlock(curr)->continuation();
2723     } while (curr && decorations && (!quirksMode || !curr->node() ||
2724                                      (!curr->node()->hasTagName(aTag) && !curr->node()->hasTagName(fontTag))));
2725
2726     // If we bailed out, use the element we bailed out at (typically a <font> or <a> element).
2727     if (decorations && curr) {
2728         styleToUse = curr->style(firstlineStyle);
2729         if (decorations & UNDERLINE)
2730             underline = decorationColor(styleToUse);
2731         if (decorations & OVERLINE)
2732             overline = decorationColor(styleToUse);
2733         if (decorations & LINE_THROUGH)
2734             linethrough = decorationColor(styleToUse);
2735     }
2736 }
2737
2738 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(DRAGGABLE_REGION)
2739 void RenderObject::addAnnotatedRegions(Vector<AnnotatedRegionValue>& regions)
2740 {
2741     // Convert the style regions to absolute coordinates.
2742     if (style()->visibility() != VISIBLE || !isBox())
2743         return;
2744     
2745     RenderBox* box = toRenderBox(this);
2746     FloatPoint absPos = localToAbsolute();
2747
2748 #if ENABLE(DASHBOARD_SUPPORT)
2749     const Vector<StyleDashboardRegion>& styleRegions = style()->dashboardRegions();
2750     unsigned i, count = styleRegions.size();
2751     for (i = 0; i < count; i++) {
2752         StyleDashboardRegion styleRegion = styleRegions[i];
2753
2754         LayoutUnit w = box->width();
2755         LayoutUnit h = box->height();
2756
2757         AnnotatedRegionValue region;
2758         region.label = styleRegion.label;
2759         region.bounds = LayoutRect(styleRegion.offset.left().value(),
2760                                    styleRegion.offset.top().value(),
2761                                    w - styleRegion.offset.left().value() - styleRegion.offset.right().value(),
2762                                    h - styleRegion.offset.top().value() - styleRegion.offset.bottom().value());
2763         region.type = styleRegion.type;
2764
2765         region.clip = region.bounds;
2766         computeAbsoluteRepaintRect(region.clip);
2767         if (region.clip.height() < 0) {
2768             region.clip.setHeight(0);
2769             region.clip.setWidth(0);
2770         }
2771
2772         region.bounds.setX(absPos.x() + styleRegion.offset.left().value());
2773         region.bounds.setY(absPos.y() + styleRegion.offset.top().value());
2774
2775         regions.append(region);
2776     }
2777 #else // ENABLE(DRAGGABLE_REGION)
2778     if (style()->getDraggableRegionMode() == DraggableRegionNone)
2779         return;
2780     AnnotatedRegionValue region;
2781     region.draggable = style()->getDraggableRegionMode() == DraggableRegionDrag;
2782     region.bounds = LayoutRect(absPos.x(), absPos.y(), box->width(), box->height());
2783     regions.append(region);
2784 #endif
2785 }
2786
2787 void RenderObject::collectAnnotatedRegions(Vector<AnnotatedRegionValue>& regions)
2788 {
2789     // RenderTexts don't have their own style, they just use their parent's style,
2790     // so we don't want to include them.
2791     if (isText())
2792         return;
2793
2794     addAnnotatedRegions(regions);
2795     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
2796         curr->collectAnnotatedRegions(regions);
2797 }
2798 #endif
2799
2800 bool RenderObject::willRenderImage(CachedImage*)
2801 {
2802     // Without visibility we won't render (and therefore don't care about animation).
2803     if (style()->visibility() != VISIBLE)
2804         return false;
2805
2806     // We will not render a new image when Active DOM is suspended
2807     if (document()->activeDOMObjectsAreSuspended())
2808         return false;
2809
2810     // If we're not in a window (i.e., we're dormant from being put in the b/f cache or in a background tab)
2811     // then we don't want to render either.
2812     return !document()->inPageCache() && !document()->view()->isOffscreen();
2813 }
2814
2815 int RenderObject::maximalOutlineSize(PaintPhase p) const
2816 {
2817     if (p != PaintPhaseOutline && p != PaintPhaseSelfOutline && p != PaintPhaseChildOutlines)
2818         return 0;
2819     return toRenderView(document()->renderer())->maximalOutlineSize();
2820 }
2821
2822 int RenderObject::caretMinOffset() const
2823 {
2824     return 0;
2825 }
2826
2827 int RenderObject::caretMaxOffset() const
2828 {
2829     if (isReplaced())
2830         return node() ? max(1U, node()->childNodeCount()) : 1;
2831     if (isHR())
2832         return 1;
2833     return 0;
2834 }
2835
2836 int RenderObject::previousOffset(int current) const
2837 {
2838     return current - 1;
2839 }
2840
2841 int RenderObject::previousOffsetForBackwardDeletion(int current) const
2842 {
2843     return current - 1;
2844 }
2845
2846 int RenderObject::nextOffset(int current) const
2847 {
2848     return current + 1;
2849 }
2850
2851 void RenderObject::adjustRectForOutlineAndShadow(LayoutRect& rect) const
2852 {
2853     int outlineSize = outlineStyleForRepaint()->outlineSize();
2854     if (const ShadowData* boxShadow = style()->boxShadow()) {
2855         boxShadow->adjustRectForShadow(rect, outlineSize);
2856         return;
2857     }
2858
2859     rect.inflate(outlineSize);
2860 }
2861
2862 AnimationController* RenderObject::animation() const
2863 {
2864     return frame()->animation();
2865 }
2866
2867 void RenderObject::imageChanged(CachedImage* image, const IntRect* rect)
2868 {
2869     imageChanged(static_cast<WrappedImagePtr>(image), rect);
2870 }
2871
2872 RenderBoxModelObject* RenderObject::offsetParent() const
2873 {
2874     // If any of the following holds true return null and stop this algorithm:
2875     // A is the root element.
2876     // A is the HTML body element.
2877     // The computed value of the position property for element A is fixed.
2878     if (isRoot() || isBody() || (isOutOfFlowPositioned() && style()->position() == FixedPosition))
2879         return 0;
2880
2881     // If A is an area HTML element which has a map HTML element somewhere in the ancestor
2882     // chain return the nearest ancestor map HTML element and stop this algorithm.
2883     // FIXME: Implement!
2884     
2885     // Return the nearest ancestor element of A for which at least one of the following is
2886     // true and stop this algorithm if such an ancestor is found:
2887     //     * The computed value of the position property is not static.
2888     //     * It is the HTML body element.
2889     //     * The computed value of the position property of A is static and the ancestor
2890     //       is one of the following HTML elements: td, th, or table.
2891     //     * Our own extension: if there is a difference in the effective zoom
2892
2893     bool skipTables = isPositioned();
2894     float currZoom = style()->effectiveZoom();
2895     RenderObject* curr = parent();
2896     while (curr && (!curr->node() || (!curr->isPositioned() && !curr->isBody()))) {
2897         Node* element = curr->node();
2898         if (!skipTables && element && (element->hasTagName(tableTag) || element->hasTagName(tdTag) || element->hasTagName(thTag)))
2899             break;
2900
2901         float newZoom = curr->style()->effectiveZoom();
2902         if (currZoom != newZoom)
2903             break;
2904         currZoom = newZoom;
2905         curr = curr->parent();
2906     }
2907     return curr && curr->isBoxModelObject() ? toRenderBoxModelObject(curr) : 0;
2908 }
2909
2910 VisiblePosition RenderObject::createVisiblePosition(int offset, EAffinity affinity)
2911 {
2912     // If this is a non-anonymous renderer in an editable area, then it's simple.
2913     if (Node* node = this->node()) {
2914         if (!node->rendererIsEditable()) {
2915             // If it can be found, we prefer a visually equivalent position that is editable. 
2916             Position position = createLegacyEditingPosition(node, offset);
2917             Position candidate = position.downstream(CanCrossEditingBoundary);
2918             if (candidate.deprecatedNode()->rendererIsEditable())
2919                 return VisiblePosition(candidate, affinity);
2920             candidate = position.upstream(CanCrossEditingBoundary);
2921             if (candidate.deprecatedNode()->rendererIsEditable())
2922                 return VisiblePosition(candidate, affinity);
2923         }
2924         // FIXME: Eliminate legacy editing positions
2925         return VisiblePosition(createLegacyEditingPosition(node, offset), affinity);
2926     }
2927
2928     // We don't want to cross the boundary between editable and non-editable
2929     // regions of the document, but that is either impossible or at least
2930     // extremely unlikely in any normal case because we stop as soon as we
2931     // find a single non-anonymous renderer.
2932
2933     // Find a nearby non-anonymous renderer.
2934     RenderObject* child = this;
2935     while (RenderObject* parent = child->parent()) {
2936         // Find non-anonymous content after.
2937         RenderObject* renderer = child;
2938         while ((renderer = renderer->nextInPreOrder(parent))) {
2939             if (Node* node = renderer->node())
2940                 return VisiblePosition(firstPositionInOrBeforeNode(node), DOWNSTREAM);
2941         }
2942
2943         // Find non-anonymous content before.
2944         renderer = child;
2945         while ((renderer = renderer->previousInPreOrder())) {
2946             if (renderer == parent)
2947                 break;
2948             if (Node* node = renderer->node())
2949                 return VisiblePosition(lastPositionInOrAfterNode(node), DOWNSTREAM);
2950         }
2951
2952         // Use the parent itself unless it too is anonymous.
2953         if (Node* node = parent->node())
2954             return VisiblePosition(firstPositionInOrBeforeNode(node), DOWNSTREAM);
2955
2956         // Repeat at the next level up.
2957         child = parent;
2958     }
2959
2960     // Everything was anonymous. Give up.
2961     return VisiblePosition();
2962 }
2963
2964 VisiblePosition RenderObject::createVisiblePosition(const Position& position)
2965 {
2966     if (position.isNotNull())
2967         return VisiblePosition(position);
2968
2969     ASSERT(!node());
2970     return createVisiblePosition(0, DOWNSTREAM);
2971 }
2972
2973 CursorDirective RenderObject::getCursor(const LayoutPoint&, Cursor&) const
2974 {
2975     return SetCursorBasedOnStyle;
2976 }
2977
2978 bool RenderObject::canUpdateSelectionOnRootLineBoxes()
2979 {
2980     if (needsLayout())
2981         return false;
2982
2983     RenderBlock* containingBlock = this->containingBlock();
2984     return containingBlock ? !containingBlock->needsLayout() : true;
2985 }
2986
2987 // We only create "generated" child renderers like one for first-letter if:
2988 // - the firstLetterBlock can have children in the DOM and
2989 // - the block doesn't have any special assumption on its text children.
2990 // This correctly prevents form controls from having such renderers.
2991 bool RenderObject::canHaveGeneratedChildren() const
2992 {
2993     return canHaveChildren();
2994 }
2995
2996 bool RenderObject::canBeReplacedWithInlineRunIn() const
2997 {
2998     return true;
2999 }
3000
3001 #if ENABLE(SVG)
3002
3003 RenderSVGResourceContainer* RenderObject::toRenderSVGResourceContainer()
3004 {
3005     ASSERT_NOT_REACHED();
3006     return 0;
3007 }
3008
3009 void RenderObject::setNeedsBoundariesUpdate()
3010 {
3011     if (RenderObject* renderer = parent())
3012         renderer->setNeedsBoundariesUpdate();
3013 }
3014
3015 FloatRect RenderObject::objectBoundingBox() const
3016 {
3017     ASSERT_NOT_REACHED();
3018     return FloatRect();
3019 }
3020
3021 FloatRect RenderObject::strokeBoundingBox() const
3022 {
3023     ASSERT_NOT_REACHED();
3024     return FloatRect();
3025 }
3026
3027 // Returns the smallest rectangle enclosing all of the painted content
3028 // respecting clipping, masking, filters, opacity, stroke-width and markers
3029 FloatRect RenderObject::repaintRectInLocalCoordinates() const
3030 {
3031     ASSERT_NOT_REACHED();
3032     return FloatRect();
3033 }
3034
3035 AffineTransform RenderObject::localTransform() const
3036 {
3037     static const AffineTransform identity;
3038     return identity;
3039 }
3040
3041 const AffineTransform& RenderObject::localToParentTransform() const
3042 {
3043     static const AffineTransform identity;
3044     return identity;
3045 }
3046
3047 bool RenderObject::nodeAtFloatPoint(const HitTestRequest&, HitTestResult&, const FloatPoint&, HitTestAction)
3048 {
3049     ASSERT_NOT_REACHED();
3050     return false;
3051 }
3052
3053 #endif // ENABLE(SVG)
3054
3055 } // namespace WebCore
3056
3057 #ifndef NDEBUG
3058
3059 void showTree(const WebCore::RenderObject* object)
3060 {
3061     if (object)
3062         object->showTreeForThis();
3063 }
3064
3065 void showLineTree(const WebCore::RenderObject* object)
3066 {
3067     if (object)
3068         object->showLineTreeForThis();
3069 }
3070
3071 void showRenderTree(const WebCore::RenderObject* object1)
3072 {
3073     showRenderTree(object1, 0);
3074 }
3075
3076 void showRenderTree(const WebCore::RenderObject* object1, const WebCore::RenderObject* object2)
3077 {
3078     if (object1) {
3079         const WebCore::RenderObject* root = object1;
3080         while (root->parent())
3081             root = root->parent();
3082         root->showRenderTreeAndMark(object1, "*", object2, "-", 0);
3083     }
3084 }
3085
3086 #endif