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