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