WebCore:
[WebKit-https.git] / 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 Apple Inc. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  */
24
25 #include "config.h"
26 #include "RenderObject.h"
27
28 #include "AXObjectCache.h"
29 #include "AffineTransform.h"
30 #include "AnimationController.h"
31 #include "CSSStyleSelector.h"
32 #include "CachedImage.h"
33 #include "Chrome.h"
34 #include "Document.h"
35 #include "Element.h"
36 #include "EventHandler.h"
37 #include "EventNames.h"
38 #include "FloatRect.h"
39 #include "Frame.h"
40 #include "FrameView.h"
41 #include "GraphicsContext.h"
42 #include "HTMLNames.h"
43 #include "HTMLOListElement.h"
44 #include "HitTestRequest.h"
45 #include "HitTestResult.h"
46 #include "KURL.h"
47 #include "Page.h"
48 #include "PlatformScreen.h"
49 #include "Position.h"
50 #include "RenderArena.h"
51 #include "RenderCounter.h"
52 #include "RenderFlexibleBox.h"
53 #include "RenderImage.h"
54 #include "RenderInline.h"
55 #include "RenderListItem.h"
56 #include "RenderTableCell.h"
57 #include "RenderTableCol.h"
58 #include "RenderTableRow.h"
59 #include "RenderText.h"
60 #include "RenderTheme.h"
61 #include "RenderView.h"
62 #include "TextResourceDecoder.h"
63 #include "TextStream.h"
64 #include <algorithm>
65
66 using namespace std;
67
68 namespace WebCore {
69
70 using namespace EventNames;
71 using namespace HTMLNames;
72
73 #ifndef NDEBUG
74 static void* baseOfRenderObjectBeingDeleted;
75 #endif
76
77 void* RenderObject::operator new(size_t sz, RenderArena* renderArena) throw()
78 {
79     return renderArena->allocate(sz);
80 }
81
82 void RenderObject::operator delete(void* ptr, size_t sz)
83 {
84     ASSERT(baseOfRenderObjectBeingDeleted == ptr);
85
86     // Stash size where destroy can find it.
87     *(size_t *)ptr = sz;
88 }
89
90 RenderObject* RenderObject::createObject(Node* node, RenderStyle* style)
91 {
92     Document* doc = node->document();
93     RenderArena* arena = doc->renderArena();
94
95     // Minimal support for content properties replacing an entire element.
96     // Works only if we have exactly one piece of content and it's a URL.
97     // Otherwise acts as if we didn't support this feature.
98     const ContentData* contentData = style->contentData();
99     if (contentData && !contentData->m_next && contentData->m_type == CONTENT_OBJECT && doc != node) {
100         RenderImage* image = new (arena) RenderImage(node);
101         image->setStyle(style);
102         if (CachedResource* resource = contentData->m_content.m_object)
103             if (resource->type() == CachedResource::ImageResource)
104                 image->setCachedImage(static_cast<CachedImage*>(resource));
105         image->setIsAnonymousImage(true);
106         return image;
107     }
108
109     RenderObject* o = 0;
110
111     switch (style->display()) {
112         case NONE:
113             break;
114         case INLINE:
115             o = new (arena) RenderInline(node);
116             break;
117         case BLOCK:
118             o = new (arena) RenderBlock(node);
119             break;
120         case INLINE_BLOCK:
121             o = new (arena) RenderBlock(node);
122             break;
123         case LIST_ITEM:
124             o = new (arena) RenderListItem(node);
125             break;
126         case RUN_IN:
127         case COMPACT:
128             o = new (arena) RenderBlock(node);
129             break;
130         case TABLE:
131         case INLINE_TABLE:
132             o = new (arena) RenderTable(node);
133             break;
134         case TABLE_ROW_GROUP:
135         case TABLE_HEADER_GROUP:
136         case TABLE_FOOTER_GROUP:
137             o = new (arena) RenderTableSection(node);
138             break;
139         case TABLE_ROW:
140             o = new (arena) RenderTableRow(node);
141             break;
142         case TABLE_COLUMN_GROUP:
143         case TABLE_COLUMN:
144             o = new (arena) RenderTableCol(node);
145             break;
146         case TABLE_CELL:
147             o = new (arena) RenderTableCell(node);
148             break;
149         case TABLE_CAPTION:
150             o = new (arena) RenderBlock(node);
151             break;
152         case BOX:
153         case INLINE_BOX:
154             o = new (arena) RenderFlexibleBox(node);
155             break;
156     }
157
158     return o;
159 }
160
161 #ifndef NDEBUG
162 struct RenderObjectCounter {
163     static int count;
164     ~RenderObjectCounter() { if (count != 0) fprintf(stderr, "LEAK: %d RenderObject\n", count); }
165 };
166 int RenderObjectCounter::count;
167 static RenderObjectCounter renderObjectCounter;
168 #endif
169
170 RenderObject::RenderObject(Node* node)
171     : CachedResourceClient()
172     , m_style(0)
173     , m_node(node)
174     , m_parent(0)
175     , m_previous(0)
176     , m_next(0)
177     , m_verticalPosition(PositionUndefined)
178     , m_needsLayout(false)
179     , m_normalChildNeedsLayout(false)
180     , m_posChildNeedsLayout(false)
181     , m_prefWidthsDirty(false)
182     , m_floating(false)
183     , m_positioned(false)
184     , m_relPositioned(false)
185     , m_paintBackground(false)
186     , m_isAnonymous(node == node->document())
187     , m_isText(false)
188     , m_inline(true)
189     , m_replaced(false)
190     , m_isDragging(false)
191     , m_hasLayer(false)
192     , m_hasOverflowClip(false)
193     , m_hasTransform(false)
194     , m_hasOverrideSize(false)
195     , m_hasCounterNodeMap(false)
196 {
197 #ifndef NDEBUG
198     ++RenderObjectCounter::count;
199 #endif
200 }
201
202 RenderObject::~RenderObject()
203 {
204     ASSERT(!node() || documentBeingDestroyed() || !document()->frame()->view() || document()->frame()->view()->layoutRoot() != this);
205 #ifndef NDEBUG
206     --RenderObjectCounter::count;
207 #endif
208 }
209
210 bool RenderObject::isDescendantOf(const RenderObject* obj) const
211 {
212     for (const RenderObject* r = this; r; r = r->m_parent) {
213         if (r == obj)
214             return true;
215     }
216     return false;
217 }
218
219 bool RenderObject::isBody() const
220 {
221     return node()->hasTagName(bodyTag);
222 }
223
224 bool RenderObject::isHR() const
225 {
226     return element() && element()->hasTagName(hrTag);
227 }
228
229 bool RenderObject::isHTMLMarquee() const
230 {
231     return element() && element()->renderer() == this && element()->hasTagName(marqueeTag);
232 }
233
234 bool RenderObject::canHaveChildren() const
235 {
236     return false;
237 }
238
239 RenderFlow* RenderObject::continuation() const
240 {
241     return 0;
242 }
243
244 bool RenderObject::isInlineContinuation() const
245 {
246     return false;
247 }
248
249 void RenderObject::addChild(RenderObject*, RenderObject*)
250 {
251     ASSERT_NOT_REACHED();
252 }
253
254 RenderObject* RenderObject::removeChildNode(RenderObject*, bool)
255 {
256     ASSERT_NOT_REACHED();
257     return 0;
258 }
259
260 void RenderObject::removeChild(RenderObject*)
261 {
262     ASSERT_NOT_REACHED();
263 }
264
265 void RenderObject::moveChildNode(RenderObject*)
266 {
267     ASSERT_NOT_REACHED();
268 }
269
270 void RenderObject::appendChildNode(RenderObject*, bool)
271 {
272     ASSERT_NOT_REACHED();
273 }
274
275 void RenderObject::insertChildNode(RenderObject*, RenderObject*, bool)
276 {
277     ASSERT_NOT_REACHED();
278 }
279
280 RenderObject* RenderObject::nextInPreOrder() const
281 {
282     if (RenderObject* o = firstChild())
283         return o;
284
285     return nextInPreOrderAfterChildren();
286 }
287
288 RenderObject* RenderObject::nextInPreOrderAfterChildren() const
289 {
290     RenderObject* o;
291     if (!(o = nextSibling())) {
292         o = parent();
293         while (o && !o->nextSibling())
294             o = o->parent();
295         if (o)
296             o = o->nextSibling();
297     }
298
299     return o;
300 }
301
302 RenderObject* RenderObject::nextInPreOrder(RenderObject* stayWithin) const
303 {
304     if (RenderObject* o = firstChild())
305         return o;
306
307     return nextInPreOrderAfterChildren(stayWithin);
308 }
309
310 RenderObject* RenderObject::nextInPreOrderAfterChildren(RenderObject* stayWithin) const
311 {
312     if (this == stayWithin)
313         return 0;
314
315     RenderObject* o;
316     if (!(o = nextSibling())) {
317         o = parent();
318         while (o && !o->nextSibling()) {
319             o = o->parent();
320             if (o == stayWithin)
321                 return 0;
322         }
323         if (o)
324             o = o->nextSibling();
325     }
326
327     return o;
328 }
329
330 RenderObject* RenderObject::previousInPreOrder() const
331 {
332     if (RenderObject* o = previousSibling()) {
333         while (o->lastChild())
334             o = o->lastChild();
335         return o;
336     }
337
338     return parent();
339 }
340
341 RenderObject* RenderObject::childAt(unsigned index) const
342 {
343     RenderObject* child = firstChild();
344     for (unsigned i = 0; child && i < index; i++)
345         child = child->nextSibling();
346     return child;
347 }
348
349 bool RenderObject::isEditable() const
350 {
351     RenderText* textRenderer = 0;
352     if (isText())
353         textRenderer = static_cast<RenderText*>(const_cast<RenderObject*>(this));
354
355     return style()->visibility() == VISIBLE &&
356         element() && element()->isContentEditable() &&
357         ((isBlockFlow() && !firstChild()) ||
358         isReplaced() ||
359         isBR() ||
360         (textRenderer && textRenderer->firstTextBox()));
361 }
362
363 RenderObject* RenderObject::firstLeafChild() const
364 {
365     RenderObject* r = firstChild();
366     while (r) {
367         RenderObject* n = 0;
368         n = r->firstChild();
369         if (!n)
370             break;
371         r = n;
372     }
373     return r;
374 }
375
376 RenderObject* RenderObject::lastLeafChild() const
377 {
378     RenderObject* r = lastChild();
379     while (r) {
380         RenderObject* n = 0;
381         n = r->lastChild();
382         if (!n)
383             break;
384         r = n;
385     }
386     return r;
387 }
388
389 static void addLayers(RenderObject* obj, RenderLayer* parentLayer, RenderObject*& newObject,
390                       RenderLayer*& beforeChild)
391 {
392     if (obj->hasLayer()) {
393         if (!beforeChild && newObject) {
394             // We need to figure out the layer that follows newObject.  We only do
395             // this the first time we find a child layer, and then we update the
396             // pointer values for newObject and beforeChild used by everyone else.
397             beforeChild = newObject->parent()->findNextLayer(parentLayer, newObject);
398             newObject = 0;
399         }
400         parentLayer->addChild(obj->layer(), beforeChild);
401         return;
402     }
403
404     for (RenderObject* curr = obj->firstChild(); curr; curr = curr->nextSibling())
405         addLayers(curr, parentLayer, newObject, beforeChild);
406 }
407
408 void RenderObject::addLayers(RenderLayer* parentLayer, RenderObject* newObject)
409 {
410     if (!parentLayer)
411         return;
412
413     RenderObject* object = newObject;
414     RenderLayer* beforeChild = 0;
415     WebCore::addLayers(this, parentLayer, object, beforeChild);
416 }
417
418 void RenderObject::removeLayers(RenderLayer* parentLayer)
419 {
420     if (!parentLayer)
421         return;
422
423     if (hasLayer()) {
424         parentLayer->removeChild(layer());
425         return;
426     }
427
428     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
429         curr->removeLayers(parentLayer);
430 }
431
432 void RenderObject::moveLayers(RenderLayer* oldParent, RenderLayer* newParent)
433 {
434     if (!newParent)
435         return;
436
437     if (hasLayer()) {
438         if (oldParent)
439             oldParent->removeChild(layer());
440         newParent->addChild(layer());
441         return;
442     }
443
444     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
445         curr->moveLayers(oldParent, newParent);
446 }
447
448 RenderLayer* RenderObject::findNextLayer(RenderLayer* parentLayer, RenderObject* startPoint,
449                                          bool checkParent)
450 {
451     // Error check the parent layer passed in.  If it's null, we can't find anything.
452     if (!parentLayer)
453         return 0;
454
455     // Step 1: If our layer is a child of the desired parent, then return our layer.
456     RenderLayer* ourLayer = layer();
457     if (ourLayer && ourLayer->parent() == parentLayer)
458         return ourLayer;
459
460     // Step 2: If we don't have a layer, or our layer is the desired parent, then descend
461     // into our siblings trying to find the next layer whose parent is the desired parent.
462     if (!ourLayer || ourLayer == parentLayer) {
463         for (RenderObject* curr = startPoint ? startPoint->nextSibling() : firstChild();
464              curr; curr = curr->nextSibling()) {
465             RenderLayer* nextLayer = curr->findNextLayer(parentLayer, 0, false);
466             if (nextLayer)
467                 return nextLayer;
468         }
469     }
470
471     // Step 3: If our layer is the desired parent layer, then we're finished.  We didn't
472     // find anything.
473     if (parentLayer == ourLayer)
474         return 0;
475
476     // Step 4: If |checkParent| is set, climb up to our parent and check its siblings that
477     // follow us to see if we can locate a layer.
478     if (checkParent && parent())
479         return parent()->findNextLayer(parentLayer, this, true);
480
481     return 0;
482 }
483
484 RenderLayer* RenderObject::enclosingLayer() const
485 {
486     const RenderObject* curr = this;
487     while (curr) {
488         RenderLayer* layer = curr->layer();
489         if (layer)
490             return layer;
491         curr = curr->parent();
492     }
493     return 0;
494 }
495
496 bool RenderObject::requiresLayer()
497 {
498     return isRoot() || isPositioned() || isRelPositioned() || isTransparent() || hasOverflowClip() || hasTransform();
499 }
500
501 RenderBlock* RenderObject::firstLineBlock() const
502 {
503     return 0;
504 }
505
506 int RenderObject::offsetLeft() const
507 {
508     RenderObject* offsetPar = offsetParent();
509     if (!offsetPar)
510         return 0;
511     int x = xPos() - offsetPar->borderLeft();
512     if (!isPositioned()) {
513         if (isRelPositioned())
514             x += static_cast<const RenderBox*>(this)->relativePositionOffsetX();
515         RenderObject* curr = parent();
516         while (curr && curr != offsetPar) {
517             x += curr->xPos();
518             curr = curr->parent();
519         }
520         if (offsetPar->isBody() && !offsetPar->isRelPositioned() && !offsetPar->isPositioned())
521             x += offsetPar->xPos();
522     }
523     return x;
524 }
525
526 int RenderObject::offsetTop() const
527 {
528     RenderObject* offsetPar = offsetParent();
529     if (!offsetPar)
530         return 0;
531     int y = yPos() - offsetPar->borderTop();
532     if (!isPositioned()) {
533         if (isRelPositioned())
534             y += static_cast<const RenderBox*>(this)->relativePositionOffsetY();
535         RenderObject* curr = parent();
536         while (curr && curr != offsetPar) {
537             if (!curr->isTableRow())
538                 y += curr->yPos();
539             curr = curr->parent();
540         }
541         if (offsetPar->isBody() && !offsetPar->isRelPositioned() && !offsetPar->isPositioned())
542             y += offsetPar->yPos();
543     }
544     return y;
545 }
546
547 RenderObject* RenderObject::offsetParent() const
548 {
549     // FIXME: It feels like this function could almost be written using containing blocks.
550     if (isBody())
551         return 0;
552
553     bool skipTables = isPositioned() || isRelPositioned();
554     RenderObject* curr = parent();
555     while (curr && (!curr->element() ||
556                     (!curr->isPositioned() && !curr->isRelPositioned() && !curr->isBody()))) {
557         if (!skipTables && curr->element() && (curr->element()->hasTagName(tableTag) || 
558                                                curr->element()->hasTagName(tdTag) || curr->element()->hasTagName(thTag)))
559             break;
560         curr = curr->parent();
561     }
562     return curr;
563 }
564
565 int RenderObject::verticalScrollbarWidth() const
566 {
567     return includeVerticalScrollbarSize() ? layer()->verticalScrollbarWidth() : 0;
568 }
569
570 int RenderObject::horizontalScrollbarHeight() const
571 {
572     return includeHorizontalScrollbarSize() ? layer()->horizontalScrollbarHeight() : 0;
573 }
574
575 // More IE extensions.  clientWidth and clientHeight represent the interior of an object
576 // excluding border and scrollbar.
577 int RenderObject::clientWidth() const
578 {
579     return width() - borderLeft() - borderRight() - verticalScrollbarWidth();
580 }
581
582 int RenderObject::clientHeight() const
583 {
584     return height() - borderTop() - borderBottom() - horizontalScrollbarHeight();
585 }
586
587 // scrollWidth/scrollHeight will be the same as clientWidth/clientHeight unless the
588 // object has overflow:hidden/scroll/auto specified and also has overflow.
589 int RenderObject::scrollWidth() const
590 {
591     return hasOverflowClip() ? layer()->scrollWidth() : overflowWidth();
592 }
593
594 int RenderObject::scrollHeight() const
595 {
596     return hasOverflowClip() ? layer()->scrollHeight() : overflowHeight();
597 }
598
599 int RenderObject::scrollLeft() const
600 {
601     return hasOverflowClip() ? layer()->scrollXOffset() : 0;
602 }
603
604 int RenderObject::scrollTop() const
605 {
606     return hasOverflowClip() ? layer()->scrollYOffset() : 0;
607 }
608
609 void RenderObject::setScrollLeft(int newLeft)
610 {
611     if (hasOverflowClip())
612         layer()->scrollToXOffset(newLeft);
613 }
614
615 void RenderObject::setScrollTop(int newTop)
616 {
617     if (hasOverflowClip())
618         layer()->scrollToYOffset(newTop);
619 }
620
621 bool RenderObject::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier)
622 {
623     RenderLayer* l = layer();
624     if (l && l->scroll(direction, granularity, multiplier))
625         return true;
626     RenderBlock* b = containingBlock();
627     if (b && !b->isRenderView())
628         return b->scroll(direction, granularity, multiplier);
629     return false;
630 }
631
632 bool RenderObject::shouldAutoscroll() const
633 {
634     return ((isRoot()) || (hasOverflowClip() && (scrollsOverflow() || (node() && node()->isContentEditable()))));
635 }
636
637 void RenderObject::autoscroll()
638 {
639     if (RenderLayer* l = layer())
640         l->autoscroll();
641 }
642
643 bool RenderObject::hasStaticX() const
644 {
645     return (style()->left().isAuto() && style()->right().isAuto()) || style()->left().isStatic() || style()->right().isStatic();
646 }
647
648 bool RenderObject::hasStaticY() const
649 {
650     return (style()->top().isAuto() && style()->bottom().isAuto()) || style()->top().isStatic();
651 }
652
653 void RenderObject::markAllDescendantsWithFloatsForLayout(RenderObject*)
654 {
655 }
656
657 void RenderObject::setPrefWidthsDirty(bool b, bool markParents)
658 {
659     bool alreadyDirty = m_prefWidthsDirty;
660     m_prefWidthsDirty = b;
661     if (b && !alreadyDirty && markParents && (isText() || (style()->position() != FixedPosition && style()->position() != AbsolutePosition)))
662         invalidateContainerPrefWidths();
663 }
664
665 void RenderObject::invalidateContainerPrefWidths()
666 {
667     // In order to avoid pathological behavior when inlines are deeply nested, we do include them
668     // in the chain that we mark dirty (even though they're kind of irrelevant).
669     RenderObject* o = isTableCell() ? containingBlock() : container();
670     while (o && !o->m_prefWidthsDirty) {
671         o->m_prefWidthsDirty = true;
672         if (o->style()->position() == FixedPosition || o->style()->position() == AbsolutePosition)
673             // A positioned object has no effect on the min/max width of its containing block ever.
674             // We can optimize this case and not go up any further.
675             break;
676         o = o->isTableCell() ? o->containingBlock() : o->container();
677     }
678 }
679
680 void RenderObject::setNeedsLayout(bool b, bool markParents)
681 {
682     bool alreadyNeededLayout = m_needsLayout;
683     m_needsLayout = b;
684     if (b) {
685         if (!alreadyNeededLayout) {
686             if (markParents)
687                 markContainingBlocksForLayout();
688             if (hasLayer())
689                 layer()->setNeedsFullRepaint();
690         }
691     } else {
692         m_posChildNeedsLayout = false;
693         m_normalChildNeedsLayout = false;
694     }
695 }
696
697 void RenderObject::setChildNeedsLayout(bool b, bool markParents)
698 {
699     bool alreadyNeededLayout = m_normalChildNeedsLayout;
700     m_normalChildNeedsLayout = b;
701     if (b) {
702         if (!alreadyNeededLayout && markParents)
703             markContainingBlocksForLayout();
704     } else {
705         m_posChildNeedsLayout = false;
706         m_normalChildNeedsLayout = false;
707     }
708 }
709
710 static inline bool objectIsRelayoutBoundary(const RenderObject *obj) 
711 {
712     // FIXME: In future it may be possible to broaden this condition in order to improve performance 
713     return obj->isTextField() || obj->isTextArea()
714         || obj->hasOverflowClip() && !obj->style()->width().isIntrinsicOrAuto() && !obj->style()->height().isIntrinsicOrAuto() && !obj->style()->height().isPercent()
715 #if ENABLE(SVG)
716            || obj->isSVGRoot()
717 #endif
718            ;
719 }
720     
721 void RenderObject::markContainingBlocksForLayout(bool scheduleRelayout, RenderObject* newRoot)
722 {
723     ASSERT(!scheduleRelayout || !newRoot);
724
725     RenderObject* o = container();
726     RenderObject* last = this;
727
728     while (o) {
729         if (!last->isText() && (last->style()->position() == FixedPosition || last->style()->position() == AbsolutePosition)) {
730             if (last->hasStaticY()) {
731                 RenderObject* parent = last->parent();
732                 if (!parent->normalChildNeedsLayout()) {
733                     parent->setChildNeedsLayout(true, false);
734                     parent->markContainingBlocksForLayout(scheduleRelayout, newRoot);
735                 }
736             }
737             if (o->m_posChildNeedsLayout)
738                 return;
739             o->m_posChildNeedsLayout = true;
740         } else {
741             if (o->m_normalChildNeedsLayout)
742                 return;
743             o->m_normalChildNeedsLayout = true;
744         }
745
746         if (o == newRoot)
747             return;
748
749         last = o;
750         if (scheduleRelayout && objectIsRelayoutBoundary(last))
751             break;
752         o = o->container();
753     }
754
755     if (scheduleRelayout)
756         last->scheduleRelayout();
757 }
758
759 RenderBlock* RenderObject::containingBlock() const
760 {
761     if (isTableCell()) {
762         const RenderTableCell* cell = static_cast<const RenderTableCell*>(this);
763         if (parent() && cell->section())
764             return cell->table();
765         return 0;
766     }
767
768     if (isRenderView())
769         return const_cast<RenderBlock*>(static_cast<const RenderBlock*>(this));
770
771     RenderObject* o = parent();
772     if (!isText() && m_style->position() == FixedPosition) {
773         while (o && !o->isRenderView() && !o->hasTransform())
774             o = o->parent();
775     } else if (!isText() && m_style->position() == AbsolutePosition) {
776         while (o && (o->style()->position() == StaticPosition || (o->isInline() && !o->isReplaced())) && !o->isRenderView() && !o->hasTransform()) {
777             // For relpositioned inlines, we return the nearest 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() == RelativePosition && o->isInline() && !o->isReplaced())
783                 return o->containingBlock();
784             o = o->parent();
785         }
786     } else {
787         while (o && ((o->isInline() && !o->isReplaced()) || o->isTableRow() || o->isTableSection()
788                      || o->isTableCol() || o->isFrameSet()
789 #if ENABLE(SVG)
790                      || o->isSVGContainer() || o->isSVGRoot()
791 #endif
792                      ))
793             o = o->parent();
794     }
795
796     if (!o || !o->isRenderBlock())
797         return 0; // Probably doesn't happen any more, but leave just in case. -dwh
798
799     return static_cast<RenderBlock*>(o);
800 }
801
802 int RenderObject::containingBlockWidth() const
803 {
804     // FIXME ?
805     return containingBlock()->availableWidth();
806 }
807
808 int RenderObject::containingBlockHeight() const
809 {
810     // FIXME ?
811     return containingBlock()->contentHeight();
812 }
813
814 bool RenderObject::mustRepaintBackgroundOrBorder() const
815 {
816     // If we don't have a background/border, then nothing to do.
817     if (!hasBoxDecorations())
818         return false;
819
820     // Ok, let's check the background first.
821     const BackgroundLayer* bgLayer = style()->backgroundLayers();
822
823     // Nobody will use multiple background layers without wanting fancy positioning.
824     if (bgLayer->next())
825         return true;
826
827     // Make sure we have a valid background image.
828     CachedImage* bg = bgLayer->backgroundImage();
829     bool shouldPaintBackgroundImage = bg && bg->canRender();
830
831     // These are always percents or auto.
832     if (shouldPaintBackgroundImage &&
833             (!bgLayer->backgroundXPosition().isZero() || !bgLayer->backgroundYPosition().isZero() ||
834              bgLayer->backgroundSize().width.isPercent() || bgLayer->backgroundSize().height.isPercent()))
835         // The background image will shift unpredictably if the size changes.
836         return true;
837
838     // Background is ok.  Let's check border.
839     if (style()->hasBorder()) {
840         // Border images are not ok.
841         CachedImage* borderImage = style()->borderImage().image();
842         bool shouldPaintBorderImage = borderImage && borderImage->canRender();
843
844         // If the image hasn't loaded, we're still using the normal border style.
845         if (shouldPaintBorderImage && borderImage->isLoaded())
846             return true;
847     }
848
849     return false;
850 }
851
852 void RenderObject::drawBorderArc(GraphicsContext* graphicsContext, int x, int y, float thickness, IntSize radius,
853                                  int angleStart, int angleSpan, BorderSide s, Color c, const Color& textColor,
854                                  EBorderStyle style, bool firstCorner)
855 {
856     if ((style == DOUBLE && thickness / 2 < 3) || ((style == RIDGE || style == GROOVE) && thickness / 2 < 2))
857         style = SOLID;
858
859     if (!c.isValid()) {
860         if (style == INSET || style == OUTSET || style == RIDGE || style == GROOVE)
861             c.setRGB(238, 238, 238);
862         else
863             c = textColor;
864     }
865
866     switch (style) {
867         case BNONE:
868         case BHIDDEN:
869             return;
870         case DOTTED:
871         case DASHED:
872             graphicsContext->setStrokeColor(c);
873             graphicsContext->setStrokeStyle(style == DOTTED ? DottedStroke : DashedStroke);
874             graphicsContext->setStrokeThickness(thickness);
875             graphicsContext->strokeArc(IntRect(x, y, radius.width() * 2, radius.height() * 2), angleStart, angleSpan);
876             break;
877         case DOUBLE: {
878             float third = thickness / 3.0f;
879             float innerThird = (thickness + 1.0f) / 6.0f;
880             int shiftForInner = static_cast<int>(innerThird * 2.5f);
881
882             int outerY = y;
883             int outerHeight = radius.height() * 2;
884             int innerX = x + shiftForInner;
885             int innerY = y + shiftForInner;
886             int innerWidth = (radius.width() - shiftForInner) * 2;
887             int innerHeight = (radius.height() - shiftForInner) * 2;
888             if (innerThird > 1 && (s == BSTop || (firstCorner && (s == BSLeft || s == BSRight)))) {
889                 outerHeight += 2;
890                 innerHeight += 2;
891             }
892
893             graphicsContext->setStrokeStyle(SolidStroke);
894             graphicsContext->setStrokeColor(c);
895             graphicsContext->setStrokeThickness(third);
896             graphicsContext->strokeArc(IntRect(x, outerY, radius.width() * 2, outerHeight), angleStart, angleSpan);
897             graphicsContext->setStrokeThickness(innerThird > 2 ? innerThird - 1 : innerThird);
898             graphicsContext->strokeArc(IntRect(innerX, innerY, innerWidth, innerHeight), angleStart, angleSpan);
899             break;
900         }
901         case GROOVE:
902         case RIDGE: {
903             Color c2;
904             if ((style == RIDGE && (s == BSTop || s == BSLeft)) ||
905                     (style == GROOVE && (s == BSBottom || s == BSRight)))
906                 c2 = c.dark();
907             else {
908                 c2 = c;
909                 c = c.dark();
910             }
911
912             graphicsContext->setStrokeStyle(SolidStroke);
913             graphicsContext->setStrokeColor(c);
914             graphicsContext->setStrokeThickness(thickness);
915             graphicsContext->strokeArc(IntRect(x, y, radius.width() * 2, radius.height() * 2), angleStart, angleSpan);
916
917             float halfThickness = (thickness + 1.0f) / 4.0f;
918             int shiftForInner = static_cast<int>(halfThickness * 1.5f);
919             graphicsContext->setStrokeColor(c2);
920             graphicsContext->setStrokeThickness(halfThickness > 2 ? halfThickness - 1 : halfThickness);
921             graphicsContext->strokeArc(IntRect(x + shiftForInner, y + shiftForInner, (radius.width() - shiftForInner) * 2,
922                                        (radius.height() - shiftForInner) * 2), angleStart, angleSpan);
923             break;
924         }
925         case INSET:
926             if (s == BSTop || s == BSLeft)
927                 c = c.dark();
928         case OUTSET:
929             if (style == OUTSET && (s == BSBottom || s == BSRight))
930                 c = c.dark();
931         case SOLID:
932             graphicsContext->setStrokeStyle(SolidStroke);
933             graphicsContext->setStrokeColor(c);
934             graphicsContext->setStrokeThickness(thickness);
935             graphicsContext->strokeArc(IntRect(x, y, radius.width() * 2, radius.height() * 2), angleStart, angleSpan);
936             break;
937     }
938 }
939
940 void RenderObject::drawBorder(GraphicsContext* graphicsContext, int x1, int y1, int x2, int y2,
941                               BorderSide s, Color c, const Color& textcolor, EBorderStyle style,
942                               int adjbw1, int adjbw2)
943 {
944     int width = (s == BSTop || s == BSBottom ? y2 - y1 : x2 - x1);
945
946     if (style == DOUBLE && width < 3)
947         style = SOLID;
948
949     if (!c.isValid()) {
950         if (style == INSET || style == OUTSET || style == RIDGE || style == GROOVE)
951             c.setRGB(238, 238, 238);
952         else
953             c = textcolor;
954     }
955
956     switch (style) {
957         case BNONE:
958         case BHIDDEN:
959             return;
960         case DOTTED:
961         case DASHED:
962             graphicsContext->setStrokeColor(c);
963             graphicsContext->setStrokeThickness(width);
964             graphicsContext->setStrokeStyle(style == DASHED ? DashedStroke : DottedStroke);
965
966             if (width > 0)
967                 switch (s) {
968                     case BSBottom:
969                     case BSTop:
970                         graphicsContext->drawLine(IntPoint(x1, (y1 + y2) / 2), IntPoint(x2, (y1 + y2) / 2));
971                         break;
972                     case BSRight:
973                     case BSLeft:
974                         graphicsContext->drawLine(IntPoint((x1 + x2) / 2, y1), IntPoint((x1 + x2) / 2, y2));
975                         break;
976                 }
977             break;
978         case DOUBLE: {
979             int third = (width + 1) / 3;
980
981             if (adjbw1 == 0 && adjbw2 == 0) {
982                 graphicsContext->setStrokeStyle(NoStroke);
983                 graphicsContext->setFillColor(c);
984                 switch (s) {
985                     case BSTop:
986                     case BSBottom:
987                         graphicsContext->drawRect(IntRect(x1, y1, x2 - x1, third));
988                         graphicsContext->drawRect(IntRect(x1, y2 - third, x2 - x1, third));
989                         break;
990                     case BSLeft:
991                         graphicsContext->drawRect(IntRect(x1, y1 + 1, third, y2 - y1 - 1));
992                         graphicsContext->drawRect(IntRect(x2 - third, y1 + 1, third, y2 - y1 - 1));
993                         break;
994                     case BSRight:
995                         graphicsContext->drawRect(IntRect(x1, y1 + 1, third, y2 - y1 - 1));
996                         graphicsContext->drawRect(IntRect(x2 - third, y1 + 1, third, y2 - y1 - 1));
997                         break;
998                 }
999             } else {
1000                 int adjbw1bigthird = ((adjbw1 > 0) ? adjbw1 + 1 : adjbw1 - 1) / 3;
1001                 int adjbw2bigthird = ((adjbw2 > 0) ? adjbw2 + 1 : adjbw2 - 1) / 3;
1002
1003                 switch (s) {
1004                     case BSTop:
1005                         drawBorder(graphicsContext, x1 + max((-adjbw1 * 2 + 1) / 3, 0),
1006                                    y1, x2 - max((-adjbw2 * 2 + 1) / 3, 0), y1 + third,
1007                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
1008                         drawBorder(graphicsContext, x1 + max((adjbw1 * 2 + 1) / 3, 0),
1009                                    y2 - third, x2 - max((adjbw2 * 2 + 1) / 3, 0), y2,
1010                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
1011                         break;
1012                     case BSLeft:
1013                         drawBorder(graphicsContext, x1, y1 + max((-adjbw1 * 2 + 1) / 3, 0),
1014                                    x1 + third, y2 - max((-adjbw2 * 2 + 1) / 3, 0),
1015                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
1016                         drawBorder(graphicsContext, x2 - third, y1 + max((adjbw1 * 2 + 1) / 3, 0),
1017                                    x2, y2 - max((adjbw2 * 2 + 1) / 3, 0),
1018                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
1019                         break;
1020                     case BSBottom:
1021                         drawBorder(graphicsContext, x1 + max((adjbw1 * 2 + 1) / 3, 0),
1022                                    y1, x2 - max((adjbw2 * 2 + 1) / 3, 0), y1 + third,
1023                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
1024                         drawBorder(graphicsContext, x1 + max((-adjbw1 * 2 + 1) / 3, 0),
1025                                    y2 - third, x2 - max((-adjbw2 * 2 + 1) / 3, 0), y2,
1026                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
1027                         break;
1028                     case BSRight:
1029                         drawBorder(graphicsContext, x1, y1 + max((adjbw1 * 2 + 1) / 3, 0),
1030                                    x1 + third, y2 - max(( adjbw2 * 2 + 1) / 3, 0),
1031                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
1032                         drawBorder(graphicsContext, x2 - third, y1 + max((-adjbw1 * 2 + 1) / 3, 0),
1033                                    x2, y2 - max((-adjbw2 * 2 + 1) / 3, 0),
1034                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
1035                         break;
1036                     default:
1037                         break;
1038                 }
1039             }
1040             break;
1041         }
1042         case RIDGE:
1043         case GROOVE:
1044         {
1045             EBorderStyle s1;
1046             EBorderStyle s2;
1047             if (style == GROOVE) {
1048                 s1 = INSET;
1049                 s2 = OUTSET;
1050             } else {
1051                 s1 = OUTSET;
1052                 s2 = INSET;
1053             }
1054
1055             int adjbw1bighalf = ((adjbw1 > 0) ? adjbw1 + 1 : adjbw1 - 1) / 2;
1056             int adjbw2bighalf = ((adjbw2 > 0) ? adjbw2 + 1 : adjbw2 - 1) / 2;
1057
1058             switch (s) {
1059                 case BSTop:
1060                     drawBorder(graphicsContext, x1 + max(-adjbw1, 0) / 2, y1, x2 - max(-adjbw2, 0) / 2, (y1 + y2 + 1) / 2,
1061                                s, c, textcolor, s1, adjbw1bighalf, adjbw2bighalf);
1062                     drawBorder(graphicsContext, x1 + max(adjbw1 + 1, 0) / 2, (y1 + y2 + 1) / 2, x2 - max(adjbw2 + 1, 0) / 2, y2,
1063                                s, c, textcolor, s2, adjbw1 / 2, adjbw2 / 2);
1064                     break;
1065                 case BSLeft:
1066                     drawBorder(graphicsContext, x1, y1 + max(-adjbw1, 0) / 2, (x1 + x2 + 1) / 2, y2 - max(-adjbw2, 0) / 2,
1067                                s, c, textcolor, s1, adjbw1bighalf, adjbw2bighalf);
1068                     drawBorder(graphicsContext, (x1 + x2 + 1) / 2, y1 + max(adjbw1 + 1, 0) / 2, x2, y2 - max(adjbw2 + 1, 0) / 2,
1069                                s, c, textcolor, s2, adjbw1 / 2, adjbw2 / 2);
1070                     break;
1071                 case BSBottom:
1072                     drawBorder(graphicsContext, x1 + max(adjbw1, 0) / 2, y1, x2 - max(adjbw2, 0) / 2, (y1 + y2 + 1) / 2,
1073                                s, c, textcolor, s2, adjbw1bighalf, adjbw2bighalf);
1074                     drawBorder(graphicsContext, x1 + max(-adjbw1 + 1, 0) / 2, (y1 + y2 + 1) / 2, x2 - max(-adjbw2 + 1, 0) / 2, y2,
1075                                s, c, textcolor, s1, adjbw1/2, adjbw2/2);
1076                     break;
1077                 case BSRight:
1078                     drawBorder(graphicsContext, x1, y1 + max(adjbw1, 0) / 2, (x1 + x2 + 1) / 2, y2 - max(adjbw2, 0) / 2,
1079                                s, c, textcolor, s2, adjbw1bighalf, adjbw2bighalf);
1080                     drawBorder(graphicsContext, (x1 + x2 + 1) / 2, y1 + max(-adjbw1 + 1, 0) / 2, x2, y2 - max(-adjbw2 + 1, 0) / 2,
1081                                s, c, textcolor, s1, adjbw1/2, adjbw2/2);
1082                     break;
1083             }
1084             break;
1085         }
1086         case INSET:
1087             if (s == BSTop || s == BSLeft)
1088                 c = c.dark();
1089             // fall through
1090         case OUTSET:
1091             if (style == OUTSET && (s == BSBottom || s == BSRight))
1092                 c = c.dark();
1093             // fall through
1094         case SOLID: {
1095             graphicsContext->setStrokeStyle(NoStroke);
1096             graphicsContext->setFillColor(c);
1097             ASSERT(x2 >= x1);
1098             ASSERT(y2 >= y1);
1099             if (!adjbw1 && !adjbw2) {
1100                 graphicsContext->drawRect(IntRect(x1, y1, x2 - x1, y2 - y1));
1101                 return;
1102             }
1103             FloatPoint quad[4];
1104             switch (s) {
1105                 case BSTop:
1106                     quad[0] = FloatPoint(x1 + max(-adjbw1, 0), y1);
1107                     quad[1] = FloatPoint(x1 + max(adjbw1, 0), y2);
1108                     quad[2] = FloatPoint(x2 - max(adjbw2, 0), y2);
1109                     quad[3] = FloatPoint(x2 - max(-adjbw2, 0), y1);
1110                     break;
1111                 case BSBottom:
1112                     quad[0] = FloatPoint(x1 + max(adjbw1, 0), y1);
1113                     quad[1] = FloatPoint(x1 + max(-adjbw1, 0), y2);
1114                     quad[2] = FloatPoint(x2 - max(-adjbw2, 0), y2);
1115                     quad[3] = FloatPoint(x2 - max(adjbw2, 0), y1);
1116                     break;
1117                 case BSLeft:
1118                     quad[0] = FloatPoint(x1, y1 + max(-adjbw1, 0));
1119                     quad[1] = FloatPoint(x1, y2 - max(-adjbw2, 0));
1120                     quad[2] = FloatPoint(x2, y2 - max(adjbw2, 0));
1121                     quad[3] = FloatPoint(x2, y1 + max(adjbw1, 0));
1122                     break;
1123                 case BSRight:
1124                     quad[0] = FloatPoint(x1, y1 + max(adjbw1, 0));
1125                     quad[1] = FloatPoint(x1, y2 - max(adjbw2, 0));
1126                     quad[2] = FloatPoint(x2, y2 - max(-adjbw2, 0));
1127                     quad[3] = FloatPoint(x2, y1 + max(-adjbw1, 0));
1128                     break;
1129             }
1130             graphicsContext->drawConvexPolygon(4, quad);
1131             break;
1132         }
1133     }
1134 }
1135
1136 bool RenderObject::paintBorderImage(GraphicsContext* graphicsContext, int tx, int ty, int w, int h, const RenderStyle* style)
1137 {
1138     CachedImage* borderImage = style->borderImage().image();
1139     if (!borderImage->isLoaded())
1140         return true; // Never paint a border image incrementally, but don't paint the fallback borders either.
1141
1142     // If we have a border radius, the border image gets clipped to the rounded rect.
1143     bool clipped = false;
1144     if (style->hasBorderRadius()) {
1145         IntRect clipRect(tx, ty, w, h);
1146         graphicsContext->save();
1147         graphicsContext->addRoundedRectClip(clipRect, style->borderTopLeftRadius(), style->borderTopRightRadius(),
1148                                             style->borderBottomLeftRadius(), style->borderBottomRightRadius());
1149         clipped = true;
1150     }
1151
1152     borderImage->setImageContainerSize(IntSize(w, h));
1153     int imageWidth = borderImage->image()->width();
1154     int imageHeight = borderImage->image()->height();
1155
1156     int topSlice = min(imageHeight, style->borderImage().m_slices.top.calcValue(borderImage->image()->height()));
1157     int bottomSlice = min(imageHeight, style->borderImage().m_slices.bottom.calcValue(borderImage->image()->height()));
1158     int leftSlice = min(imageWidth, style->borderImage().m_slices.left.calcValue(borderImage->image()->width()));
1159     int rightSlice = min(imageWidth, style->borderImage().m_slices.right.calcValue(borderImage->image()->width()));
1160
1161     EBorderImageRule hRule = style->borderImage().horizontalRule();
1162     EBorderImageRule vRule = style->borderImage().verticalRule();
1163
1164     bool drawLeft = leftSlice > 0 && style->borderLeftWidth() > 0;
1165     bool drawTop = topSlice > 0 && style->borderTopWidth() > 0;
1166     bool drawRight = rightSlice > 0 && style->borderRightWidth() > 0;
1167     bool drawBottom = bottomSlice > 0 && style->borderBottomWidth() > 0;
1168     bool drawMiddle = (imageWidth - leftSlice - rightSlice) > 0 && (w - style->borderLeftWidth() - style->borderRightWidth()) > 0 &&
1169                       (imageHeight - topSlice - bottomSlice) > 0 && (h - style->borderTopWidth() - style->borderBottomWidth()) > 0;
1170
1171     if (drawLeft) {
1172         // Paint the top and bottom left corners.
1173
1174         // The top left corner rect is (tx, ty, leftWidth, topWidth)
1175         // The rect to use from within the image is obtained from our slice, and is (0, 0, leftSlice, topSlice)
1176         if (drawTop)
1177             graphicsContext->drawImage(borderImage->image(), IntRect(tx, ty, style->borderLeftWidth(), style->borderTopWidth()),
1178                                        IntRect(0, 0, leftSlice, topSlice));
1179
1180         // The bottom left corner rect is (tx, ty + h - bottomWidth, leftWidth, bottomWidth)
1181         // The rect to use from within the image is (0, imageHeight - bottomSlice, leftSlice, botomSlice)
1182         if (drawBottom)
1183             graphicsContext->drawImage(borderImage->image(), IntRect(tx, ty + h - style->borderBottomWidth(), style->borderLeftWidth(), style->borderBottomWidth()),
1184                                        IntRect(0, imageHeight - bottomSlice, leftSlice, bottomSlice));
1185
1186         // Paint the left edge.
1187         // Have to scale and tile into the border rect.
1188         graphicsContext->drawTiledImage(borderImage->image(), IntRect(tx, ty + style->borderTopWidth(), style->borderLeftWidth(),
1189                                         h - style->borderTopWidth() - style->borderBottomWidth()),
1190                                         IntRect(0, topSlice, leftSlice, imageHeight - topSlice - bottomSlice),
1191                                         Image::StretchTile, (Image::TileRule)vRule);
1192     }
1193
1194     if (drawRight) {
1195         // Paint the top and bottom right corners
1196         // The top right corner rect is (tx + w - rightWidth, ty, rightWidth, topWidth)
1197         // The rect to use from within the image is obtained from our slice, and is (imageWidth - rightSlice, 0, rightSlice, topSlice)
1198         if (drawTop)
1199             graphicsContext->drawImage(borderImage->image(), IntRect(tx + w - style->borderRightWidth(), ty, style->borderRightWidth(), style->borderTopWidth()),
1200                                        IntRect(imageWidth - rightSlice, 0, rightSlice, topSlice));
1201
1202         // The bottom right corner rect is (tx + w - rightWidth, ty + h - bottomWidth, rightWidth, bottomWidth)
1203         // The rect to use from within the image is (imageWidth - rightSlice, imageHeight - bottomSlice, rightSlice, botomSlice)
1204         if (drawBottom)
1205             graphicsContext->drawImage(borderImage->image(), IntRect(tx + w - style->borderRightWidth(), ty + h - style->borderBottomWidth(), style->borderRightWidth(), style->borderBottomWidth()),
1206                                        IntRect(imageWidth - rightSlice, imageHeight - bottomSlice, rightSlice, bottomSlice));
1207
1208         // Paint the right edge.
1209         graphicsContext->drawTiledImage(borderImage->image(), IntRect(tx + w - style->borderRightWidth(), ty + style->borderTopWidth(), style->borderRightWidth(),
1210                                         h - style->borderTopWidth() - style->borderBottomWidth()),
1211                                         IntRect(imageWidth - rightSlice, topSlice, rightSlice, imageHeight - topSlice - bottomSlice),
1212                                         Image::StretchTile, (Image::TileRule)vRule);
1213     }
1214
1215     // Paint the top edge.
1216     if (drawTop)
1217         graphicsContext->drawTiledImage(borderImage->image(), IntRect(tx + style->borderLeftWidth(), ty, w - style->borderLeftWidth() - style->borderRightWidth(), style->borderTopWidth()),
1218                                         IntRect(leftSlice, 0, imageWidth - rightSlice - leftSlice, topSlice),
1219                                         (Image::TileRule)hRule, Image::StretchTile);
1220
1221     // Paint the bottom edge.
1222     if (drawBottom)
1223         graphicsContext->drawTiledImage(borderImage->image(), IntRect(tx + style->borderLeftWidth(), ty + h - style->borderBottomWidth(),
1224                                         w - style->borderLeftWidth() - style->borderRightWidth(), style->borderBottomWidth()),
1225                                         IntRect(leftSlice, imageHeight - bottomSlice, imageWidth - rightSlice - leftSlice, bottomSlice),
1226                                         (Image::TileRule)hRule, Image::StretchTile);
1227
1228     // Paint the middle.
1229     if (drawMiddle)
1230         graphicsContext->drawTiledImage(borderImage->image(), IntRect(tx + style->borderLeftWidth(), ty + style->borderTopWidth(), w - style->borderLeftWidth() - style->borderRightWidth(),
1231                                         h - style->borderTopWidth() - style->borderBottomWidth()),
1232                                         IntRect(leftSlice, topSlice, imageWidth - rightSlice - leftSlice, imageHeight - topSlice - bottomSlice),
1233                                         (Image::TileRule)hRule, (Image::TileRule)vRule);
1234
1235     // Clear the clip for the border radius.
1236     if (clipped)
1237         graphicsContext->restore();
1238
1239     return true;
1240 }
1241
1242 void RenderObject::paintBorder(GraphicsContext* graphicsContext, int tx, int ty, int w, int h,
1243                                const RenderStyle* style, bool begin, bool end)
1244 {
1245     CachedImage* borderImage = style->borderImage().image();
1246     bool shouldPaintBackgroundImage = borderImage && borderImage->canRender();
1247     if (shouldPaintBackgroundImage)
1248         shouldPaintBackgroundImage = paintBorderImage(graphicsContext, tx, ty, w, h, style);
1249
1250     if (shouldPaintBackgroundImage)
1251         return;
1252
1253     const Color& tc = style->borderTopColor();
1254     const Color& bc = style->borderBottomColor();
1255     const Color& lc = style->borderLeftColor();
1256     const Color& rc = style->borderRightColor();
1257
1258     bool tt = style->borderTopIsTransparent();
1259     bool bt = style->borderBottomIsTransparent();
1260     bool rt = style->borderRightIsTransparent();
1261     bool lt = style->borderLeftIsTransparent();
1262
1263     EBorderStyle ts = style->borderTopStyle();
1264     EBorderStyle bs = style->borderBottomStyle();
1265     EBorderStyle ls = style->borderLeftStyle();
1266     EBorderStyle rs = style->borderRightStyle();
1267
1268     bool renderTop = ts > BHIDDEN && !tt;
1269     bool renderLeft = ls > BHIDDEN && begin && !lt;
1270     bool renderRight = rs > BHIDDEN && end && !rt;
1271     bool renderBottom = bs > BHIDDEN && !bt;
1272
1273     // Need sufficient width and height to contain border radius curves.  Sanity check our border radii
1274     // and our width/height values to make sure the curves can all fit. If not, then we won't paint
1275     // any border radii.
1276     bool renderRadii = false;
1277     IntSize topLeft = style->borderTopLeftRadius();
1278     IntSize topRight = style->borderTopRightRadius();
1279     IntSize bottomLeft = style->borderBottomLeftRadius();
1280     IntSize bottomRight = style->borderBottomRightRadius();
1281
1282     if (style->hasBorderRadius() &&
1283         static_cast<unsigned>(w) >= static_cast<unsigned>(topLeft.width()) + static_cast<unsigned>(topRight.width()) &&
1284         static_cast<unsigned>(w) >= static_cast<unsigned>(bottomLeft.width()) + static_cast<unsigned>(bottomRight.width()) &&
1285         static_cast<unsigned>(h) >= static_cast<unsigned>(topLeft.height()) + static_cast<unsigned>(bottomLeft.height()) &&
1286         static_cast<unsigned>(h) >= static_cast<unsigned>(topRight.height()) + static_cast<unsigned>(bottomRight.height()))
1287         renderRadii = true;
1288
1289     // Clip to the rounded rectangle.
1290     if (renderRadii) {
1291         graphicsContext->save();
1292         graphicsContext->addRoundedRectClip(IntRect(tx, ty, w, h), topLeft, topRight, bottomLeft, bottomRight);
1293     }
1294
1295     int firstAngleStart, secondAngleStart, firstAngleSpan, secondAngleSpan;
1296     float thickness;
1297     bool upperLeftBorderStylesMatch = renderLeft && (ts == ls) && (tc == lc);
1298     bool upperRightBorderStylesMatch = renderRight && (ts == rs) && (tc == rc) && (ts != OUTSET) && (ts != RIDGE) && (ts != INSET) && (ts != GROOVE);
1299     bool lowerLeftBorderStylesMatch = renderLeft && (bs == ls) && (bc == lc) && (bs != OUTSET) && (bs != RIDGE) && (bs != INSET) && (bs != GROOVE);
1300     bool lowerRightBorderStylesMatch = renderRight && (bs == rs) && (bc == rc);
1301
1302     if (renderTop) {
1303         bool ignore_left = (renderRadii && topLeft.width() > 0) ||
1304             (tc == lc && tt == lt && ts >= OUTSET &&
1305              (ls == DOTTED || ls == DASHED || ls == SOLID || ls == OUTSET));
1306
1307         bool ignore_right = (renderRadii && topRight.width() > 0) ||
1308             (tc == rc && tt == rt && ts >= OUTSET &&
1309              (rs == DOTTED || rs == DASHED || rs == SOLID || rs == INSET));
1310
1311         int x = tx;
1312         int x2 = tx + w;
1313         if (renderRadii) {
1314             x += topLeft.width();
1315             x2 -= topRight.width();
1316         }
1317
1318         drawBorder(graphicsContext, x, ty, x2, ty + style->borderTopWidth(), BSTop, tc, style->color(), ts,
1319                    ignore_left ? 0 : style->borderLeftWidth(), ignore_right ? 0 : style->borderRightWidth());
1320
1321         if (renderRadii) {
1322             int leftY = ty;
1323
1324             // We make the arc double thick and let the clip rect take care of clipping the extra off.
1325             // We're doing this because it doesn't seem possible to match the curve of the clip exactly
1326             // with the arc-drawing function.
1327             thickness = style->borderTopWidth() * 2;
1328
1329             if (topLeft.width()) {
1330                 int leftX = tx;
1331                 // The inner clip clips inside the arc. This is especially important for 1px borders.
1332                 bool applyLeftInnerClip = (style->borderLeftWidth() < topLeft.width())
1333                     && (style->borderTopWidth() < topLeft.height())
1334                     && (ts != DOUBLE || style->borderTopWidth() > 6);
1335                 if (applyLeftInnerClip) {
1336                     graphicsContext->save();
1337                     graphicsContext->addInnerRoundedRectClip(IntRect(leftX, leftY, topLeft.width() * 2, topLeft.height() * 2),
1338                                                              style->borderTopWidth());
1339                 }
1340
1341                 firstAngleStart = 90;
1342                 firstAngleSpan = upperLeftBorderStylesMatch ? 90 : 45;
1343
1344                 // Draw upper left arc
1345                 drawBorderArc(graphicsContext, leftX, leftY, thickness, topLeft, firstAngleStart, firstAngleSpan,
1346                               BSTop, tc, style->color(), ts, true);
1347                 if (applyLeftInnerClip)
1348                     graphicsContext->restore();
1349             }
1350
1351             if (topRight.width()) {
1352                 int rightX = tx + w - topRight.width() * 2;
1353                 bool applyRightInnerClip = (style->borderRightWidth() < topRight.width())
1354                     && (style->borderTopWidth() < topRight.height())
1355                     && (ts != DOUBLE || style->borderTopWidth() > 6);
1356                 if (applyRightInnerClip) {
1357                     graphicsContext->save();
1358                     graphicsContext->addInnerRoundedRectClip(IntRect(rightX, leftY, topRight.width() * 2, topRight.height() * 2),
1359                                                              style->borderTopWidth());
1360                 }
1361
1362                 if (upperRightBorderStylesMatch) {
1363                     secondAngleStart = 0;
1364                     secondAngleSpan = 90;
1365                 } else {
1366                     secondAngleStart = 45;
1367                     secondAngleSpan = 45;
1368                 }
1369
1370                 // Draw upper right arc
1371                 drawBorderArc(graphicsContext, rightX, leftY, thickness, topRight, secondAngleStart, secondAngleSpan,
1372                               BSTop, tc, style->color(), ts, false);
1373                 if (applyRightInnerClip)
1374                     graphicsContext->restore();
1375             }
1376         }
1377     }
1378
1379     if (renderBottom) {
1380         bool ignore_left = (renderRadii && bottomLeft.width() > 0) ||
1381             (bc == lc && bt == lt && bs >= OUTSET &&
1382              (ls == DOTTED || ls == DASHED || ls == SOLID || ls == OUTSET));
1383
1384         bool ignore_right = (renderRadii && bottomRight.width() > 0) ||
1385             (bc == rc && bt == rt && bs >= OUTSET &&
1386              (rs == DOTTED || rs == DASHED || rs == SOLID || rs == INSET));
1387
1388         int x = tx;
1389         int x2 = tx + w;
1390         if (renderRadii) {
1391             x += bottomLeft.width();
1392             x2 -= bottomRight.width();
1393         }
1394
1395         drawBorder(graphicsContext, x, ty + h - style->borderBottomWidth(), x2, ty + h, BSBottom, bc, style->color(), bs,
1396                    ignore_left ? 0 : style->borderLeftWidth(), ignore_right ? 0 : style->borderRightWidth());
1397
1398         if (renderRadii) {
1399             thickness = style->borderBottomWidth() * 2;
1400
1401             if (bottomLeft.width()) {
1402                 int leftX = tx;
1403                 int leftY = ty + h - bottomLeft.height() * 2;
1404                 bool applyLeftInnerClip = (style->borderLeftWidth() < bottomLeft.width())
1405                     && (style->borderBottomWidth() < bottomLeft.height())
1406                     && (bs != DOUBLE || style->borderBottomWidth() > 6);
1407                 if (applyLeftInnerClip) {
1408                     graphicsContext->save();
1409                     graphicsContext->addInnerRoundedRectClip(IntRect(leftX, leftY, bottomLeft.width() * 2, bottomLeft.height() * 2),
1410                                                              style->borderBottomWidth());
1411                 }
1412
1413                 if (lowerLeftBorderStylesMatch) {
1414                     firstAngleStart = 180;
1415                     firstAngleSpan = 90;
1416                 } else {
1417                     firstAngleStart = 225;
1418                     firstAngleSpan = 45;
1419                 }
1420
1421                 // Draw lower left arc
1422                 drawBorderArc(graphicsContext, leftX, leftY, thickness, bottomLeft, firstAngleStart, firstAngleSpan,
1423                               BSBottom, bc, style->color(), bs, true);
1424                 if (applyLeftInnerClip)
1425                     graphicsContext->restore();
1426             }
1427
1428             if (bottomRight.width()) {
1429                 int rightY = ty + h - bottomRight.height() * 2;
1430                 int rightX = tx + w - bottomRight.width() * 2;
1431                 bool applyRightInnerClip = (style->borderRightWidth() < bottomRight.width())
1432                     && (style->borderBottomWidth() < bottomRight.height())
1433                     && (bs != DOUBLE || style->borderBottomWidth() > 6);
1434                 if (applyRightInnerClip) {
1435                     graphicsContext->save();
1436                     graphicsContext->addInnerRoundedRectClip(IntRect(rightX, rightY, bottomRight.width() * 2, bottomRight.height() * 2),
1437                                                              style->borderBottomWidth());
1438                 }
1439
1440                 secondAngleStart = 270;
1441                 secondAngleSpan = lowerRightBorderStylesMatch ? 90 : 45;
1442
1443                 // Draw lower right arc
1444                 drawBorderArc(graphicsContext, rightX, rightY, thickness, bottomRight, secondAngleStart, secondAngleSpan,
1445                               BSBottom, bc, style->color(), bs, false);
1446                 if (applyRightInnerClip)
1447                     graphicsContext->restore();
1448             }
1449         }
1450     }
1451
1452     if (renderLeft) {
1453         bool ignore_top = (renderRadii && topLeft.height() > 0) ||
1454             (tc == lc && tt == lt && ls >= OUTSET &&
1455              (ts == DOTTED || ts == DASHED || ts == SOLID || ts == OUTSET));
1456
1457         bool ignore_bottom = (renderRadii && bottomLeft.height() > 0) ||
1458             (bc == lc && bt == lt && ls >= OUTSET &&
1459              (bs == DOTTED || bs == DASHED || bs == SOLID || bs == INSET));
1460
1461         int y = ty;
1462         int y2 = ty + h;
1463         if (renderRadii) {
1464             y += topLeft.height();
1465             y2 -= bottomLeft.height();
1466         }
1467
1468         drawBorder(graphicsContext, tx, y, tx + style->borderLeftWidth(), y2, BSLeft, lc, style->color(), ls,
1469                    ignore_top ? 0 : style->borderTopWidth(), ignore_bottom ? 0 : style->borderBottomWidth());
1470
1471         if (renderRadii && (!upperLeftBorderStylesMatch || !lowerLeftBorderStylesMatch)) {
1472             int topX = tx;
1473             thickness = style->borderLeftWidth() * 2;
1474
1475             if (!upperLeftBorderStylesMatch && topLeft.width()) {
1476                 int topY = ty;
1477                 bool applyTopInnerClip = (style->borderLeftWidth() < topLeft.width())
1478                     && (style->borderTopWidth() < topLeft.height())
1479                     && (ls != DOUBLE || style->borderLeftWidth() > 6);
1480                 if (applyTopInnerClip) {
1481                     graphicsContext->save();
1482                     graphicsContext->addInnerRoundedRectClip(IntRect(topX, topY, topLeft.width() * 2, topLeft.height() * 2),
1483                                                              style->borderLeftWidth());
1484                 }
1485
1486                 firstAngleStart = 135;
1487                 firstAngleSpan = 45;
1488
1489                 // Draw top left arc
1490                 drawBorderArc(graphicsContext, topX, topY, thickness, topLeft, firstAngleStart, firstAngleSpan,
1491                               BSLeft, lc, style->color(), ls, true);
1492                 if (applyTopInnerClip)
1493                     graphicsContext->restore();
1494             }
1495
1496             if (!lowerLeftBorderStylesMatch && bottomLeft.width()) {
1497                 int bottomY = ty + h - bottomLeft.height() * 2;
1498                 bool applyBottomInnerClip = (style->borderLeftWidth() < bottomLeft.width())
1499                     && (style->borderBottomWidth() < bottomLeft.height())
1500                     && (ls != DOUBLE || style->borderLeftWidth() > 6);
1501                 if (applyBottomInnerClip) {
1502                     graphicsContext->save();
1503                     graphicsContext->addInnerRoundedRectClip(IntRect(topX, bottomY, bottomLeft.width() * 2, bottomLeft.height() * 2),
1504                                                              style->borderLeftWidth());
1505                 }
1506
1507                 secondAngleStart = 180;
1508                 secondAngleSpan = 45;
1509
1510                 // Draw bottom left arc
1511                 drawBorderArc(graphicsContext, topX, bottomY, thickness, bottomLeft, secondAngleStart, secondAngleSpan,
1512                               BSLeft, lc, style->color(), ls, false);
1513                 if (applyBottomInnerClip)
1514                     graphicsContext->restore();
1515             }
1516         }
1517     }
1518
1519     if (renderRight) {
1520         bool ignore_top = (renderRadii && topRight.height() > 0) ||
1521             ((tc == rc) && (tt == rt) &&
1522             (rs >= DOTTED || rs == INSET) &&
1523             (ts == DOTTED || ts == DASHED || ts == SOLID || ts == OUTSET));
1524
1525         bool ignore_bottom = (renderRadii && bottomRight.height() > 0) ||
1526             ((bc == rc) && (bt == rt) &&
1527             (rs >= DOTTED || rs == INSET) &&
1528             (bs == DOTTED || bs == DASHED || bs == SOLID || bs == INSET));
1529
1530         int y = ty;
1531         int y2 = ty + h;
1532         if (renderRadii) {
1533             y += topRight.height();
1534             y2 -= bottomRight.height();
1535         }
1536
1537         drawBorder(graphicsContext, tx + w - style->borderRightWidth(), y, tx + w, y2, BSRight, rc, style->color(), rs,
1538                    ignore_top ? 0 : style->borderTopWidth(), ignore_bottom ? 0 : style->borderBottomWidth());
1539
1540         if (renderRadii && (!upperRightBorderStylesMatch || !lowerRightBorderStylesMatch)) {
1541             thickness = style->borderRightWidth() * 2;
1542
1543             if (!upperRightBorderStylesMatch && topRight.width()) {
1544                 int topX = tx + w - topRight.width() * 2;
1545                 int topY = ty;
1546                 bool applyTopInnerClip = (style->borderRightWidth() < topRight.width())
1547                     && (style->borderTopWidth() < topRight.height())
1548                     && (rs != DOUBLE || style->borderRightWidth() > 6);
1549                 if (applyTopInnerClip) {
1550                     graphicsContext->save();
1551                     graphicsContext->addInnerRoundedRectClip(IntRect(topX, topY, topRight.width() * 2, topRight.height() * 2),
1552                                                              style->borderRightWidth());
1553                 }
1554
1555                 firstAngleStart = 0;
1556                 firstAngleSpan = 45;
1557
1558                 // Draw top right arc
1559                 drawBorderArc(graphicsContext, topX, topY, thickness, topRight, firstAngleStart, firstAngleSpan,
1560                               BSRight, rc, style->color(), rs, true);
1561                 if (applyTopInnerClip)
1562                     graphicsContext->restore();
1563             }
1564
1565             if (!lowerRightBorderStylesMatch && bottomRight.width()) {
1566                 int bottomX = tx + w - bottomRight.width() * 2;
1567                 int bottomY = ty + h - bottomRight.height() * 2;
1568                 bool applyBottomInnerClip = (style->borderRightWidth() < bottomRight.width())
1569                     && (style->borderBottomWidth() < bottomRight.height())
1570                     && (rs != DOUBLE || style->borderRightWidth() > 6);
1571                 if (applyBottomInnerClip) {
1572                     graphicsContext->save();
1573                     graphicsContext->addInnerRoundedRectClip(IntRect(bottomX, bottomY, bottomRight.width() * 2, bottomRight.height() * 2),
1574                                                              style->borderRightWidth());
1575                 }
1576
1577                 secondAngleStart = 315;
1578                 secondAngleSpan = 45;
1579
1580                 // Draw bottom right arc
1581                 drawBorderArc(graphicsContext, bottomX, bottomY, thickness, bottomRight, secondAngleStart, secondAngleSpan,
1582                               BSRight, rc, style->color(), rs, false);
1583                 if (applyBottomInnerClip)
1584                     graphicsContext->restore();
1585             }
1586         }
1587     }
1588
1589     if (renderRadii)
1590         graphicsContext->restore();
1591 }
1592
1593 void RenderObject::paintBoxShadow(GraphicsContext* context, int tx, int ty, int w, int h, const RenderStyle* s, bool begin, bool end)
1594 {
1595     if (!s->boxShadow())
1596         return;
1597     
1598     // FIXME: Deal with border-image.  Would be great to use border-image as a mask.
1599     context->save();
1600     context->setShadow(IntSize(s->boxShadow()->x, s->boxShadow()->y),
1601                                s->boxShadow()->blur, s->boxShadow()->color);
1602     IntRect rect(tx, ty, w, h);
1603     if (s->hasBorderRadius()) {
1604         IntSize topLeft = begin ? s->borderTopLeftRadius() : IntSize();
1605         IntSize topRight = end ? s->borderTopRightRadius() : IntSize();
1606         IntSize bottomLeft = begin ? s->borderBottomLeftRadius() : IntSize();
1607         IntSize bottomRight = end ? s->borderBottomRightRadius() : IntSize();
1608         context->clipOutRoundedRect(rect, topLeft, topRight, bottomLeft, bottomRight);
1609         context->fillRoundedRect(rect, topLeft, topRight, bottomLeft, bottomRight, Color::black);
1610     } else {
1611         context->clipOut(rect);
1612         context->fillRect(IntRect(tx, ty, w, h), Color::black);
1613     }
1614     context->restore();
1615 }
1616
1617 void RenderObject::addLineBoxRects(Vector<IntRect>&, unsigned startOffset, unsigned endOffset, bool useSelectionHeight)
1618 {
1619 }
1620
1621 void RenderObject::absoluteRects(Vector<IntRect>& rects, int tx, int ty, bool topLevel)
1622 {
1623     // For blocks inside inlines, we go ahead and include margins so that we run right up to the
1624     // inline boxes above and below us (thus getting merged with them to form a single irregular
1625     // shape).
1626     if (topLevel && continuation()) {
1627         rects.append(IntRect(tx, ty - collapsedMarginTop(),
1628                              width(), height() + collapsedMarginTop() + collapsedMarginBottom()));
1629         continuation()->absoluteRects(rects,
1630                                       tx - xPos() + continuation()->containingBlock()->xPos(),
1631                                       ty - yPos() + continuation()->containingBlock()->yPos(), topLevel);
1632     } else
1633         rects.append(IntRect(tx, ty, width(), height() + borderTopExtra() + borderBottomExtra()));
1634 }
1635
1636 IntRect RenderObject::absoluteBoundingBoxRect()
1637 {
1638     int x, y;
1639     absolutePosition(x, y);
1640     Vector<IntRect> rects;
1641     absoluteRects(rects, x, y);
1642
1643     size_t n = rects.size();
1644     if (!n)
1645         return IntRect();
1646
1647     IntRect result = rects[0];
1648     for (size_t i = 1; i < n; ++i)
1649         result.unite(rects[i]);
1650     return result;
1651 }
1652
1653 void RenderObject::addAbsoluteRectForLayer(IntRect& result)
1654 {
1655     if (hasLayer())
1656         result.unite(absoluteBoundingBoxRect());
1657     for (RenderObject* current = firstChild(); current; current = current->nextSibling())
1658         current->addAbsoluteRectForLayer(result);
1659 }
1660
1661 IntRect RenderObject::paintingRootRect(IntRect& topLevelRect)
1662 {
1663     IntRect result = absoluteBoundingBoxRect();
1664     topLevelRect = result;
1665     for (RenderObject* current = firstChild(); current; current = current->nextSibling())
1666         current->addAbsoluteRectForLayer(result);
1667     return result;
1668 }
1669
1670 void RenderObject::addPDFURLRect(GraphicsContext* graphicsContext, IntRect rect)
1671 {
1672     Node* node = element();
1673     if (node) {
1674         if (graphicsContext) {
1675             if (rect.width() > 0 && rect.height() > 0) {
1676                 Element* element = static_cast<Element*>(node);
1677                 String href;
1678                 if (element->isLink())
1679                     href = element->getAttribute(hrefAttr);
1680
1681                 if (!href.isNull()) {
1682                     KURL link = element->document()->completeURL(href.deprecatedString());
1683                     graphicsContext->setURLForRect(link, rect);
1684                 }
1685             }
1686         }
1687     }
1688 }
1689
1690
1691 void RenderObject::addFocusRingRects(GraphicsContext* graphicsContext, int tx, int ty)
1692 {
1693     // For blocks inside inlines, we go ahead and include margins so that we run right up to the
1694     // inline boxes above and below us (thus getting merged with them to form a single irregular
1695     // shape).
1696     if (continuation()) {
1697         graphicsContext->addFocusRingRect(IntRect(tx, ty - collapsedMarginTop(), width(), height() + collapsedMarginTop() + collapsedMarginBottom()));
1698         continuation()->addFocusRingRects(graphicsContext,
1699                                           tx - xPos() + continuation()->containingBlock()->xPos(),
1700                                           ty - yPos() + continuation()->containingBlock()->yPos());
1701     } else
1702         graphicsContext->addFocusRingRect(IntRect(tx, ty, width(), height()));
1703 }
1704
1705 void RenderObject::paintOutline(GraphicsContext* graphicsContext, int tx, int ty, int w, int h, const RenderStyle* style)
1706 {
1707     if (!hasOutline())
1708         return;
1709
1710     int ow = style->outlineWidth();
1711
1712     EBorderStyle os = style->outlineStyle();
1713
1714     Color oc = style->outlineColor();
1715     if (!oc.isValid())
1716         oc = style->color();
1717
1718     int offset = style->outlineOffset();
1719
1720     if (style->outlineStyleIsAuto() || hasOutlineAnnotation()) {
1721         if (!theme()->supportsFocusRing(style)) {
1722             // Only paint the focus ring by hand if the theme isn't able to draw the focus ring.
1723             graphicsContext->initFocusRing(ow, offset);
1724             if (style->outlineStyleIsAuto())
1725                 addFocusRingRects(graphicsContext, tx, ty);
1726             else
1727                 addPDFURLRect(graphicsContext, graphicsContext->focusRingBoundingRect());
1728             graphicsContext->drawFocusRing(oc);
1729             graphicsContext->clearFocusRing();
1730         }
1731     }
1732
1733     if (style->outlineStyleIsAuto() || style->outlineStyle() <= BHIDDEN)
1734         return;
1735
1736     tx -= offset;
1737     ty -= offset;
1738     w += 2 * offset;
1739     h += 2 * offset;
1740
1741     if (h < 0 || w < 0)
1742         return;
1743
1744     drawBorder(graphicsContext, tx - ow, ty - ow, tx, ty + h + ow,
1745                BSLeft, Color(oc), style->color(), os, ow, ow);
1746
1747     drawBorder(graphicsContext, tx - ow, ty - ow, tx + w + ow, ty,
1748                BSTop, Color(oc), style->color(), os, ow, ow);
1749
1750     drawBorder(graphicsContext, tx + w, ty - ow, tx + w + ow, ty + h + ow,
1751                BSRight, Color(oc), style->color(), os, ow, ow);
1752
1753     drawBorder(graphicsContext, tx - ow, ty + h, tx + w + ow, ty + h + ow,
1754                BSBottom, Color(oc), style->color(), os, ow, ow);
1755 }
1756
1757 void RenderObject::paint(PaintInfo& /*paintInfo*/, int /*tx*/, int /*ty*/)
1758 {
1759 }
1760
1761 void RenderObject::repaint(bool immediate)
1762 {
1763     // Can't use view(), since we might be unrooted.
1764     RenderObject* o = this;
1765     while (o->parent())
1766         o = o->parent();
1767     if (!o->isRenderView())
1768         return;
1769     RenderView* view = static_cast<RenderView*>(o);
1770     if (view->printing())
1771         return; // Don't repaint if we're printing.
1772     view->repaintViewRectangle(absoluteClippedOverflowRect(), immediate);
1773 }
1774
1775 void RenderObject::repaintRectangle(const IntRect& r, bool immediate)
1776 {
1777     // Can't use view(), since we might be unrooted.
1778     RenderObject* o = this;
1779     while (o->parent())
1780         o = o->parent();
1781     if (!o->isRenderView())
1782         return;
1783     RenderView* view = static_cast<RenderView*>(o);
1784     if (view->printing())
1785         return; // Don't repaint if we're printing.
1786     IntRect absRect(r);
1787     computeAbsoluteRepaintRect(absRect);
1788     view->repaintViewRectangle(absRect, immediate);
1789 }
1790
1791 bool RenderObject::repaintAfterLayoutIfNeeded(const IntRect& oldBounds, const IntRect& oldOutlineBox)
1792 {
1793     RenderView* v = view();
1794     if (v->printing())
1795         return false; // Don't repaint if we're printing.
1796
1797     IntRect newBounds = absoluteClippedOverflowRect();
1798     IntRect newOutlineBox;
1799
1800     bool fullRepaint = selfNeedsLayout();
1801     // Presumably a background or a border exists if border-fit:lines was specified.
1802     if (!fullRepaint && style()->borderFit() == BorderFitLines)
1803         fullRepaint = true;
1804     if (!fullRepaint) {
1805         newOutlineBox = absoluteOutlineBox();
1806         if (newOutlineBox.location() != oldOutlineBox.location() || (mustRepaintBackgroundOrBorder() && (newBounds != oldBounds || newOutlineBox != oldOutlineBox)))
1807             fullRepaint = true;
1808     }
1809     if (fullRepaint) {
1810         v->repaintViewRectangle(oldBounds);
1811         if (newBounds != oldBounds)
1812             v->repaintViewRectangle(newBounds);
1813         return true;
1814     }
1815
1816     if (newBounds == oldBounds && newOutlineBox == oldOutlineBox)
1817         return false;
1818
1819     int deltaLeft = newBounds.x() - oldBounds.x();
1820     if (deltaLeft > 0)
1821         v->repaintViewRectangle(IntRect(oldBounds.x(), oldBounds.y(), deltaLeft, oldBounds.height()));
1822     else if (deltaLeft < 0)
1823         v->repaintViewRectangle(IntRect(newBounds.x(), newBounds.y(), -deltaLeft, newBounds.height()));
1824
1825     int deltaRight = newBounds.right() - oldBounds.right();
1826     if (deltaRight > 0)
1827         v->repaintViewRectangle(IntRect(oldBounds.right(), newBounds.y(), deltaRight, newBounds.height()));
1828     else if (deltaRight < 0)
1829         v->repaintViewRectangle(IntRect(newBounds.right(), oldBounds.y(), -deltaRight, oldBounds.height()));
1830
1831     int deltaTop = newBounds.y() - oldBounds.y();
1832     if (deltaTop > 0)
1833         v->repaintViewRectangle(IntRect(oldBounds.x(), oldBounds.y(), oldBounds.width(), deltaTop));
1834     else if (deltaTop < 0)
1835         v->repaintViewRectangle(IntRect(newBounds.x(), newBounds.y(), newBounds.width(), -deltaTop));
1836
1837     int deltaBottom = newBounds.bottom() - oldBounds.bottom();
1838     if (deltaBottom > 0)
1839         v->repaintViewRectangle(IntRect(newBounds.x(), oldBounds.bottom(), newBounds.width(), deltaBottom));
1840     else if (deltaBottom < 0)
1841         v->repaintViewRectangle(IntRect(oldBounds.x(), newBounds.bottom(), oldBounds.width(), -deltaBottom));
1842
1843     if (newOutlineBox == oldOutlineBox)
1844         return false;
1845
1846     // We didn't move, but we did change size.  Invalidate the delta, which will consist of possibly
1847     // two rectangles (but typically only one).
1848     RenderStyle* outlineStyle = !isInline() && continuation() ? continuation()->style() : style();
1849     int ow = outlineStyle->outlineSize();
1850     ShadowData* boxShadow = style()->boxShadow();
1851     int width = abs(newOutlineBox.width() - oldOutlineBox.width());
1852     if (width) {
1853         int shadowRight = boxShadow ? max(boxShadow->x + boxShadow->blur, 0) : 0;
1854         int borderWidth = max(-outlineStyle->outlineOffset(), max(borderRight(), max(style()->borderTopRightRadius().width(), style()->borderBottomRightRadius().width()))) + max(ow, shadowRight);
1855         IntRect rightRect(newOutlineBox.x() + min(newOutlineBox.width(), oldOutlineBox.width()) - borderWidth,
1856             newOutlineBox.y(),
1857             width + borderWidth,
1858             max(newOutlineBox.height(), oldOutlineBox.height()));
1859         int right = min(newBounds.right(), oldBounds.right());
1860         if (rightRect.x() < right) {
1861             rightRect.setWidth(min(rightRect.width(), right - rightRect.x()));
1862             v->repaintViewRectangle(rightRect);
1863         }
1864     }
1865     int height = abs(newOutlineBox.height() - oldOutlineBox.height());
1866     if (height) {
1867         int shadowBottom = boxShadow ? max(boxShadow->y + boxShadow->blur, 0) : 0;
1868         int borderHeight = max(-outlineStyle->outlineOffset(), max(borderBottom(), max(style()->borderBottomLeftRadius().height(), style()->borderBottomRightRadius().height()))) + max(ow, shadowBottom);
1869         IntRect bottomRect(newOutlineBox.x(),
1870             min(newOutlineBox.bottom(), oldOutlineBox.bottom()) - borderHeight,
1871             max(newOutlineBox.width(), oldOutlineBox.width()),
1872             height + borderHeight);
1873         int bottom = min(newBounds.bottom(), oldBounds.bottom());
1874         if (bottomRect.y() < bottom) {
1875             bottomRect.setHeight(min(bottomRect.height(), bottom - bottomRect.y()));
1876             v->repaintViewRectangle(bottomRect);
1877         }
1878     }
1879     return false;
1880 }
1881
1882 void RenderObject::repaintDuringLayoutIfMoved(const IntRect& rect)
1883 {
1884 }
1885
1886 void RenderObject::repaintOverhangingFloats(bool paintAllDescendants)
1887 {
1888 }
1889
1890 bool RenderObject::checkForRepaintDuringLayout() const
1891 {
1892     return !document()->view()->needsFullRepaint() && !hasLayer();
1893 }
1894
1895 IntRect RenderObject::getAbsoluteRepaintRectWithOutline(int ow)
1896 {
1897     IntRect r(absoluteClippedOverflowRect());
1898     r.inflate(ow);
1899
1900     if (continuation() && !isInline())
1901         r.inflateY(collapsedMarginTop());
1902
1903     if (isInlineFlow()) {
1904         for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling()) {
1905             if (!curr->isText())
1906                 r.unite(curr->getAbsoluteRepaintRectWithOutline(ow));
1907         }
1908     }
1909
1910     return r;
1911 }
1912
1913 IntRect RenderObject::absoluteClippedOverflowRect()
1914 {
1915     if (parent())
1916         return parent()->absoluteClippedOverflowRect();
1917     return IntRect();
1918 }
1919
1920 void RenderObject::computeAbsoluteRepaintRect(IntRect& rect, bool fixed)
1921 {
1922     if (RenderObject* o = parent()) {
1923         if (o->isBlockFlow()) {
1924             RenderBlock* cb = static_cast<RenderBlock*>(o);
1925             if (cb->hasColumns())
1926                 cb->adjustRectForColumns(rect);
1927         }
1928
1929         if (o->hasOverflowClip()) {
1930             // o->height() is inaccurate if we're in the middle of a layout of |o|, so use the
1931             // layer's size instead.  Even if the layer's size is wrong, the layer itself will repaint
1932             // anyway if its size does change.
1933             IntRect boxRect(0, 0, o->layer()->width(), o->layer()->height());
1934             int x = rect.x();
1935             int y = rect.y();
1936             o->layer()->subtractScrollOffset(x, y); // For overflow:auto/scroll/hidden.
1937             IntRect repaintRect(x, y, rect.width(), rect.height());
1938             rect = intersection(repaintRect, boxRect);
1939             if (rect.isEmpty())
1940                 return;
1941         }
1942
1943         o->computeAbsoluteRepaintRect(rect, fixed);
1944     }
1945 }
1946
1947 void RenderObject::dirtyLinesFromChangedChild(RenderObject* child)
1948 {
1949 }
1950
1951 #ifndef NDEBUG
1952
1953 DeprecatedString RenderObject::information() const
1954 {
1955     DeprecatedString str;
1956     TextStream ts(&str);
1957     ts << renderName()
1958        << "(" << (style() ? style()->refCount() : 0) << ")"
1959        << ": " << (void*)this << "  ";
1960     if (isInline())
1961         ts << "il ";
1962     if (childrenInline())
1963         ts << "ci ";
1964     if (isFloating())
1965         ts << "fl ";
1966     if (isAnonymous())
1967         ts << "an ";
1968     if (isRelPositioned())
1969         ts << "rp ";
1970     if (isPositioned())
1971         ts << "ps ";
1972     if (needsLayout())
1973         ts << "nl ";
1974     if (style() && style()->zIndex())
1975         ts << "zI: " << style()->zIndex();
1976     if (element()) {
1977         if (element()->active())
1978             ts << "act ";
1979         if (element()->isLink())
1980             ts << "anchor ";
1981         if (element()->focused())
1982             ts << "focus ";
1983         ts << " <" << element()->localName().deprecatedString() << ">";
1984         ts << " (" << xPos() << "," << yPos() << "," << width() << "," << height() << ")";
1985         if (isTableCell()) {
1986             const RenderTableCell* cell = static_cast<const RenderTableCell*>(this);
1987             ts << " [r=" << cell->row() << " c=" << cell->col() << " rs=" << cell->rowSpan() << " cs=" << cell->colSpan() << "]";
1988         }
1989     }
1990     return str;
1991 }
1992
1993 void RenderObject::dump(TextStream* stream, DeprecatedString ind) const
1994 {
1995     if (isAnonymous())
1996         *stream << " anonymous";
1997     if (isFloating())
1998         *stream << " floating";
1999     if (isPositioned())
2000         *stream << " positioned";
2001     if (isRelPositioned())
2002         *stream << " relPositioned";
2003     if (isText())
2004         *stream << " text";
2005     if (isInline())
2006         *stream << " inline";
2007     if (isReplaced())
2008         *stream << " replaced";
2009     if (hasBoxDecorations())
2010         *stream << " paintBackground";
2011     if (needsLayout())
2012         *stream << " needsLayout";
2013     if (prefWidthsDirty())
2014         *stream << " prefWidthsDirty";
2015     *stream << endl;
2016
2017     RenderObject* child = firstChild();
2018     while (child) {
2019         *stream << ind << child->renderName() << ": ";
2020         child->dump(stream, ind + "  ");
2021         child = child->nextSibling();
2022     }
2023 }
2024
2025 void RenderObject::showTreeForThis() const
2026 {
2027     if (element())
2028         element()->showTreeForThis();
2029 }
2030
2031 #endif // NDEBUG
2032
2033 Color RenderObject::selectionBackgroundColor() const
2034 {
2035     Color color;
2036     if (style()->userSelect() != SELECT_NONE) {
2037         RenderStyle* pseudoStyle = getPseudoStyle(RenderStyle::SELECTION);
2038         if (pseudoStyle && pseudoStyle->backgroundColor().isValid())
2039             color = pseudoStyle->backgroundColor().blendWithWhite();
2040         else
2041             color = document()->frame()->isActive() ?
2042                     theme()->activeSelectionBackgroundColor() :
2043                     theme()->inactiveSelectionBackgroundColor();
2044     }
2045
2046     return color;
2047 }
2048
2049 Color RenderObject::selectionForegroundColor() const
2050 {
2051     Color color;
2052     if (style()->userSelect() != SELECT_NONE) {
2053         RenderStyle* pseudoStyle = getPseudoStyle(RenderStyle::SELECTION);
2054         if (pseudoStyle) {
2055             color = pseudoStyle->textFillColor();
2056             if (!color.isValid())
2057                 color = pseudoStyle->color();
2058         } else
2059             color = document()->frame()->isActive() ?
2060                     theme()->platformActiveSelectionForegroundColor() :
2061                     theme()->platformInactiveSelectionForegroundColor();
2062     }
2063
2064     return color;
2065 }
2066
2067 Node* RenderObject::draggableNode(bool dhtmlOK, bool uaOK, int x, int y, bool& dhtmlWillDrag) const
2068 {
2069     if (!dhtmlOK && !uaOK)
2070         return 0;
2071
2072     for (const RenderObject* curr = this; curr; curr = curr->parent()) {
2073         Node* elt = curr->element();
2074         if (elt && elt->nodeType() == Node::TEXT_NODE) {
2075             // Since there's no way for the author to address the -webkit-user-drag style for a text node,
2076             // we use our own judgement.
2077             if (uaOK && view()->frameView()->frame()->eventHandler()->shouldDragAutoNode(curr->node(), IntPoint(x, y))) {
2078                 dhtmlWillDrag = false;
2079                 return curr->node();
2080             }
2081             if (elt->canStartSelection())
2082                 // In this case we have a click in the unselected portion of text.  If this text is
2083                 // selectable, we want to start the selection process instead of looking for a parent
2084                 // to try to drag.
2085                 return 0;
2086         } else {
2087             EUserDrag dragMode = curr->style()->userDrag();
2088             if (dhtmlOK && dragMode == DRAG_ELEMENT) {
2089                 dhtmlWillDrag = true;
2090                 return curr->node();
2091             }
2092             if (uaOK && dragMode == DRAG_AUTO
2093                     && view()->frameView()->frame()->eventHandler()->shouldDragAutoNode(curr->node(), IntPoint(x, y))) {
2094                 dhtmlWillDrag = false;
2095                 return curr->node();
2096             }
2097         }
2098     }
2099     return 0;
2100 }
2101
2102 void RenderObject::selectionStartEnd(int& spos, int& epos) const
2103 {
2104     view()->selectionStartEnd(spos, epos);
2105 }
2106
2107 RenderBlock* RenderObject::createAnonymousBlock()
2108 {
2109     RenderStyle* newStyle = new (renderArena()) RenderStyle();
2110     newStyle->inheritFrom(m_style);
2111     newStyle->setDisplay(BLOCK);
2112
2113     RenderBlock* newBox = new (renderArena()) RenderBlock(document() /* anonymous box */);
2114     newBox->setStyle(newStyle);
2115     return newBox;
2116 }
2117
2118 void RenderObject::handleDynamicFloatPositionChange()
2119 {
2120     // We have gone from not affecting the inline status of the parent flow to suddenly
2121     // having an impact.  See if there is a mismatch between the parent flow's
2122     // childrenInline() state and our state.
2123     setInline(style()->isDisplayInlineType());
2124     if (isInline() != parent()->childrenInline()) {
2125         if (!isInline()) {
2126             if (parent()->isRenderInline()) {
2127                 // We have to split the parent flow.
2128                 RenderInline* parentInline = static_cast<RenderInline*>(parent());
2129                 RenderBlock* newBox = parentInline->createAnonymousBlock();
2130
2131                 RenderFlow* oldContinuation = parent()->continuation();
2132                 parentInline->setContinuation(newBox);
2133
2134                 RenderObject* beforeChild = nextSibling();
2135                 parent()->removeChildNode(this);
2136                 parentInline->splitFlow(beforeChild, newBox, this, oldContinuation);
2137             } else if (parent()->isRenderBlock()) {
2138                 RenderBlock* o = static_cast<RenderBlock*>(parent());
2139                 o->makeChildrenNonInline();
2140                 if (o->isAnonymousBlock() && o->parent())
2141                     o->parent()->removeLeftoverAnonymousBlock(o);
2142                 // o may be dead here
2143             }
2144         } else {
2145             // An anonymous block must be made to wrap this inline.
2146             RenderBlock* box = createAnonymousBlock();
2147             parent()->insertChildNode(box, this);
2148             box->appendChildNode(parent()->removeChildNode(this));
2149         }
2150     }
2151 }
2152
2153 void RenderObject::setAnimatableStyle(RenderStyle* style)
2154 {
2155     if (!isText() && m_style && style) {
2156         if (!m_style->transitions())
2157             animationController()->cancelImplicitAnimations(this);
2158         else
2159             style = animationController()->updateImplicitAnimations(this, style);
2160     }
2161     setStyle(style);
2162 }
2163
2164 void RenderObject::setStyle(RenderStyle* style)
2165 {
2166     if (m_style == style)
2167         return;
2168
2169     bool affectsParentBlock = false;
2170     RenderStyle::Diff d = RenderStyle::Equal;
2171     if (m_style) {
2172         d = m_style->diff(style);
2173
2174         // If our z-index changes value or our visibility changes,
2175         // we need to dirty our stacking context's z-order list.
2176         if (style) {
2177             if (m_style->visibility() != style->visibility() ||
2178                     m_style->zIndex() != style->zIndex() ||
2179                     m_style->hasAutoZIndex() != style->hasAutoZIndex())
2180                 document()->setDashboardRegionsDirty(true);
2181
2182             if ((m_style->hasAutoZIndex() != style->hasAutoZIndex() ||
2183                     m_style->zIndex() != style->zIndex() ||
2184                     m_style->visibility() != style->visibility()) && hasLayer()) {
2185                 layer()->stackingContext()->dirtyZOrderLists();
2186                 if (m_style->hasAutoZIndex() != style->hasAutoZIndex() ||
2187                         m_style->visibility() != style->visibility())
2188                     layer()->dirtyZOrderLists();
2189             }
2190             // keep layer hierarchy visibility bits up to date if visibility changes
2191             if (m_style->visibility() != style->visibility()) {
2192                 if (RenderLayer* l = enclosingLayer()) {
2193                     if (style->visibility() == VISIBLE)
2194                         l->setHasVisibleContent(true);
2195                     else if (l->hasVisibleContent() && (this == l->renderer() || l->renderer()->style()->visibility() != VISIBLE)) {
2196                         l->dirtyVisibleContentStatus();
2197                         if (d > RenderStyle::RepaintLayer)
2198                             repaint();
2199                     }
2200                 }
2201             }
2202         }
2203
2204         // If we have no layer(), just treat a RepaintLayer hint as a normal Repaint.
2205         if (d == RenderStyle::RepaintLayer && !hasLayer())
2206             d = RenderStyle::Repaint;
2207
2208         // The background of the root element or the body element could propagate up to
2209         // the canvas.  Just dirty the entire canvas when our style changes substantially.
2210         if (d >= RenderStyle::Repaint && element() &&
2211                 (element()->hasTagName(htmlTag) || element()->hasTagName(bodyTag)))
2212             view()->repaint();
2213         else if (m_parent && !isText()) {
2214             // Do a repaint with the old style first, e.g., for example if we go from
2215             // having an outline to not having an outline.
2216             if (d == RenderStyle::RepaintLayer) {
2217                 layer()->repaintIncludingDescendants();
2218                 if (!(m_style->clip() == style->clip()))
2219                     layer()->clearClipRects();
2220             } else if (d == RenderStyle::Repaint || style->outlineSize() < m_style->outlineSize())
2221                 repaint();
2222         }
2223
2224         // When a layout hint happens, we go ahead and do a repaint of the layer, since the layer could
2225         // end up being destroyed.
2226         if (d == RenderStyle::Layout && hasLayer() &&
2227                 (m_style->position() != style->position() ||
2228                  m_style->zIndex() != style->zIndex() ||
2229                  m_style->hasAutoZIndex() != style->hasAutoZIndex() ||
2230                  !(m_style->clip() == style->clip()) ||
2231                  m_style->hasClip() != style->hasClip() ||
2232                  m_style->opacity() != style->opacity()))
2233             layer()->repaintIncludingDescendants();
2234
2235         // When a layout hint happens and an object's position style changes, we have to do a layout
2236         // to dirty the render tree using the old position value now.
2237         if (d == RenderStyle::Layout && m_parent && m_style->position() != style->position()) {
2238             markContainingBlocksForLayout();
2239             if (m_style->position() == StaticPosition)
2240                 repaint();
2241             if (isRenderBlock()) {
2242                 if (style->position() == StaticPosition)
2243                     // Clear our positioned objects list. Our absolutely positioned descendants will be
2244                     // inserted into our containing block's positioned objects list during layout.
2245                     removePositionedObjects(0);
2246                 else if (m_style->position() == StaticPosition) {
2247                     // Remove our absolutely positioned descendants from their current containing block.
2248                     // They will be inserted into our positioned objects list during layout.
2249                     RenderObject* cb = parent();
2250                     while (cb && (cb->style()->position() == StaticPosition || (cb->isInline() && !cb->isReplaced())) && !cb->isRenderView()) {
2251                         if (cb->style()->position() == RelativePosition && cb->isInline() && !cb->isReplaced()) {
2252                             cb = cb->containingBlock();
2253                             break;
2254                         }
2255                         cb = cb->parent();
2256                     }
2257                     cb->removePositionedObjects(static_cast<RenderBlock*>(this));
2258                 }
2259             }
2260         }
2261
2262         if (isFloating() && (m_style->floating() != style->floating()))
2263             // For changes in float styles, we need to conceivably remove ourselves
2264             // from the floating objects list.
2265             removeFromObjectLists();
2266         else if (isPositioned() && (style->position() != AbsolutePosition && style->position() != FixedPosition))
2267             // For changes in positioning styles, we need to conceivably remove ourselves
2268             // from the positioned objects list.
2269             removeFromObjectLists();
2270
2271         affectsParentBlock = m_style && isFloatingOrPositioned() &&
2272             (!style->isFloating() && style->position() != AbsolutePosition && style->position() != FixedPosition)
2273             && parent() && (parent()->isBlockFlow() || parent()->isInlineFlow());
2274
2275         // reset style flags
2276         if (d == RenderStyle::Layout) {
2277             m_floating = false;
2278             m_positioned = false;
2279             m_relPositioned = false;
2280         }
2281         m_paintBackground = false;
2282         m_hasOverflowClip = false;
2283         m_hasTransform = false;
2284     }
2285
2286     if (view()->frameView()) {
2287         // FIXME: A better solution would be to only invalidate the fixed regions when scrolling.  It's overkill to
2288         // prevent the entire view from blitting on a scroll.
2289         bool newStyleSlowScroll = style && (style->position() == FixedPosition || style->hasFixedBackgroundImage());
2290         bool oldStyleSlowScroll = m_style && (m_style->position() == FixedPosition || m_style->hasFixedBackgroundImage());
2291         if (oldStyleSlowScroll != newStyleSlowScroll) {
2292             if (oldStyleSlowScroll)
2293                 view()->frameView()->removeSlowRepaintObject();
2294             if (newStyleSlowScroll)
2295                 view()->frameView()->addSlowRepaintObject();
2296         }
2297     }
2298
2299     RenderStyle* oldStyle = m_style;
2300     m_style = style;
2301
2302     updateBackgroundImages(oldStyle);
2303
2304     if (m_style)
2305         m_style->ref();
2306
2307     if (oldStyle)
2308         oldStyle->deref(renderArena());
2309
2310     setHasBoxDecorations(m_style->hasBorder() || m_style->hasBackground() || m_style->hasAppearance() || m_style->boxShadow());
2311
2312     if (affectsParentBlock)
2313         handleDynamicFloatPositionChange();
2314
2315     // No need to ever schedule repaints from a style change of a text run, since
2316     // we already did this for the parent of the text run.
2317     // We do have to schedule layouts, though, since a style change can force us to
2318     // need to relayout.
2319     if (d == RenderStyle::Layout && m_parent)
2320         setNeedsLayoutAndPrefWidthsRecalc();
2321     else if (m_parent && !isText() && (d == RenderStyle::RepaintLayer || d == RenderStyle::Repaint))
2322         // Do a repaint with the new style now, e.g., for example if we go from
2323         // not having an outline to having an outline.
2324         repaint();
2325 }
2326
2327 void RenderObject::setStyleInternal(RenderStyle* style)
2328 {
2329     if (m_style == style)
2330         return;
2331     if (m_style)
2332         m_style->deref(renderArena());
2333     m_style = style;
2334     if (m_style)
2335         m_style->ref();
2336 }
2337
2338 void RenderObject::updateBackgroundImages(RenderStyle* oldStyle)
2339 {
2340     // FIXME: This will be slow when a large number of images is used.  Fix by using a dict.
2341     const BackgroundLayer* oldLayers = oldStyle ? oldStyle->backgroundLayers() : 0;
2342     const BackgroundLayer* newLayers = m_style ? m_style->backgroundLayers() : 0;
2343     for (const BackgroundLayer* currOld = oldLayers; currOld; currOld = currOld->next()) {
2344         if (currOld->backgroundImage() && (!newLayers || !newLayers->containsImage(currOld->backgroundImage())))
2345             currOld->backgroundImage()->deref(this);
2346     }
2347     for (const BackgroundLayer* currNew = newLayers; currNew; currNew = currNew->next()) {
2348         if (currNew->backgroundImage() && (!oldLayers || !oldLayers->containsImage(currNew->backgroundImage())))
2349             currNew->backgroundImage()->ref(this);
2350     }
2351
2352     CachedImage* oldBorderImage = oldStyle ? oldStyle->borderImage().image() : 0;
2353     CachedImage* newBorderImage = m_style ? m_style->borderImage().image() : 0;
2354     if (oldBorderImage != newBorderImage) {
2355         if (oldBorderImage)
2356             oldBorderImage->deref(this);
2357         if (newBorderImage)
2358             newBorderImage->ref(this);
2359     }
2360 }
2361
2362 IntRect RenderObject::viewRect() const
2363 {
2364     return view()->viewRect();
2365 }
2366
2367 bool RenderObject::absolutePosition(int& xPos, int& yPos, bool f) const
2368 {
2369     RenderObject* o = parent();
2370     if (o) {
2371         o->absolutePosition(xPos, yPos, f);
2372         yPos += o->borderTopExtra();
2373         if (o->hasOverflowClip())
2374             o->layer()->subtractScrollOffset(xPos, yPos);
2375         return true;
2376     } else {
2377         xPos = yPos = 0;
2378         return false;
2379     }
2380 }
2381
2382 IntRect RenderObject::caretRect(int offset, EAffinity affinity, int* extraWidthToEndOfLine)
2383 {
2384    if (extraWidthToEndOfLine)
2385        *extraWidthToEndOfLine = 0;
2386
2387     return IntRect();
2388 }
2389
2390 int RenderObject::paddingTop() const
2391 {
2392     int w = 0;
2393     Length padding = m_style->paddingTop();
2394     if (padding.isPercent())
2395         w = containingBlock()->availableWidth();
2396     w = padding.calcMinValue(w);
2397     if (isTableCell() && padding.isAuto())
2398         w = static_cast<const RenderTableCell*>(this)->table()->cellPadding();
2399     return w;
2400 }
2401
2402 int RenderObject::paddingBottom() const
2403 {
2404     int w = 0;
2405     Length padding = style()->paddingBottom();
2406     if (padding.isPercent())
2407         w = containingBlock()->availableWidth();
2408     w = padding.calcMinValue(w);
2409     if (isTableCell() && padding.isAuto())
2410         w = static_cast<const RenderTableCell*>(this)->table()->cellPadding();
2411     return w;
2412 }
2413
2414 int RenderObject::paddingLeft() const
2415 {
2416     int w = 0;
2417     Length padding = style()->paddingLeft();
2418     if (padding.isPercent())
2419         w = containingBlock()->availableWidth();
2420     w = padding.calcMinValue(w);
2421     if (isTableCell() && padding.isAuto())
2422         w = static_cast<const RenderTableCell*>(this)->table()->cellPadding();
2423     return w;
2424 }
2425
2426 int RenderObject::paddingRight() const
2427 {
2428     int w = 0;
2429     Length padding = style()->paddingRight();
2430     if (padding.isPercent())
2431         w = containingBlock()->availableWidth();
2432     w = padding.calcMinValue(w);
2433     if (isTableCell() && padding.isAuto())
2434         w = static_cast<const RenderTableCell*>(this)->table()->cellPadding();
2435     return w;
2436 }
2437
2438 RenderView* RenderObject::view() const
2439 {
2440     return static_cast<RenderView*>(document()->renderer());
2441 }
2442
2443 bool RenderObject::hasOutlineAnnotation() const
2444 {
2445     return element() && element()->isLink() && document()->printing();
2446 }
2447
2448 RenderObject* RenderObject::container() const
2449 {
2450     // This method is extremely similar to containingBlock(), but with a few notable
2451     // exceptions.
2452     // (1) It can be used on orphaned subtrees, i.e., it can be called safely even when
2453     // the object is not part of the primary document subtree yet.
2454     // (2) For normal flow elements, it just returns the parent.
2455     // (3) For absolute positioned elements, it will return a relative positioned inline.
2456     // containingBlock() simply skips relpositioned inlines and lets an enclosing block handle
2457     // the layout of the positioned object.  This does mean that calcAbsoluteHorizontal and
2458     // calcAbsoluteVertical have to use container().
2459     RenderObject* o = parent();
2460
2461     if (isText())
2462         return o;
2463
2464     EPosition pos = m_style->position();
2465     if (pos == FixedPosition) {
2466         // container() can be called on an object that is not in the
2467         // tree yet.  We don't call view() since it will assert if it
2468         // can't get back to the canvas.  Instead we just walk as high up
2469         // as we can.  If we're in the tree, we'll get the root.  If we
2470         // aren't we'll get the root of our little subtree (most likely
2471         // we'll just return 0).
2472         while (o && o->parent() && !o->hasTransform())
2473             o = o->parent();
2474     } else if (pos == AbsolutePosition) {
2475         // Same goes here.  We technically just want our containing block, but
2476         // we may not have one if we're part of an uninstalled subtree.  We'll
2477         // climb as high as we can though.
2478         while (o && o->style()->position() == StaticPosition && !o->isRenderView() && !o->hasTransform())
2479             o = o->parent();
2480     }
2481
2482     return o;
2483 }
2484
2485 // This code has been written to anticipate the addition of CSS3-::outside and ::inside generated
2486 // content (and perhaps XBL).  That's why it uses the render tree and not the DOM tree.
2487 RenderObject* RenderObject::hoverAncestor() const
2488 {
2489     return (!isInline() && continuation()) ? continuation() : parent();
2490 }
2491
2492 bool RenderObject::isSelectionBorder() const
2493 {
2494     SelectionState st = selectionState();
2495     return st == SelectionStart || st == SelectionEnd || st == SelectionBoth;
2496 }
2497
2498 void RenderObject::removeFromObjectLists()
2499 {
2500     if (documentBeingDestroyed())
2501         return;
2502
2503     if (isFloating()) {
2504         RenderBlock* outermostBlock = containingBlock();
2505         for (RenderBlock* p = outermostBlock; p && !p->isRenderView(); p = p->containingBlock()) {
2506             if (p->containsFloat(this))
2507                 outermostBlock = p;
2508         }
2509
2510         if (outermostBlock)
2511             outermostBlock->markAllDescendantsWithFloatsForLayout(this);
2512     }
2513
2514     if (isPositioned()) {
2515         RenderObject* p;
2516         for (p = parent(); p; p = p->parent()) {
2517             if (p->isRenderBlock())
2518                 static_cast<RenderBlock*>(p)->removePositionedObject(this);
2519         }
2520     }
2521 }
2522
2523 bool RenderObject::documentBeingDestroyed() const
2524 {
2525     return !document()->renderer();
2526 }
2527
2528 void RenderObject::destroy()
2529 {
2530     // If this renderer is being autoscrolled, stop the autoscroll timer
2531     if (document()->frame() && document()->frame()->eventHandler()->autoscrollRenderer() == this)
2532         document()->frame()->eventHandler()->stopAutoscrollTimer(true);
2533
2534     if (m_hasCounterNodeMap)
2535         RenderCounter::destroyCounterNodes(this);
2536
2537     if (AXObjectCache::accessibilityEnabled())
2538         document()->axObjectCache()->remove(this);
2539
2540     animationController()->cancelImplicitAnimations(this);
2541
2542     // By default no ref-counting. RenderWidget::destroy() doesn't call
2543     // this function because it needs to do ref-counting. If anything
2544     // in this function changes, be sure to fix RenderWidget::destroy() as well.
2545
2546     remove();
2547
2548     arenaDelete(document()->renderArena(), this);
2549 }
2550
2551 void RenderObject::arenaDelete(RenderArena* arena, void* base)
2552 {
2553     if (m_style) {
2554         for (const BackgroundLayer* bgLayer = m_style->backgroundLayers(); bgLayer; bgLayer = bgLayer->next()) {
2555             if (CachedImage* backgroundImage = bgLayer->backgroundImage())
2556                 backgroundImage->deref(this);
2557         }
2558
2559         if (CachedImage* borderImage = m_style->borderImage().image())
2560             borderImage->deref(this);
2561
2562         m_style->deref(arena);
2563     }
2564
2565 #ifndef NDEBUG
2566     void* savedBase = baseOfRenderObjectBeingDeleted;
2567     baseOfRenderObjectBeingDeleted = base;
2568 #endif
2569     delete this;
2570 #ifndef NDEBUG
2571     baseOfRenderObjectBeingDeleted = savedBase;
2572 #endif
2573
2574     // Recover the size left there for us by operator delete and free the memory.
2575     arena->free(*(size_t*)base, base);
2576 }
2577
2578 VisiblePosition RenderObject::positionForCoordinates(int x, int y)
2579 {
2580     return VisiblePosition(element(), caretMinOffset(), DOWNSTREAM);
2581 }
2582
2583 void RenderObject::updateDragState(bool dragOn)
2584 {
2585     bool valueChanged = (dragOn != m_isDragging);
2586     m_isDragging = dragOn;
2587     if (valueChanged && style()->affectedByDragRules())
2588         element()->setChanged();
2589     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
2590         curr->updateDragState(dragOn);
2591     if (continuation())
2592         continuation()->updateDragState(dragOn);
2593 }
2594
2595 bool RenderObject::hitTest(const HitTestRequest& request, HitTestResult& result, const IntPoint& point, int tx, int ty, HitTestFilter hitTestFilter)
2596 {
2597     bool inside = false;
2598     if (hitTestFilter != HitTestSelf) {
2599         // First test the foreground layer (lines and inlines).
2600         inside = nodeAtPoint(request, result, point.x(), point.y(), tx, ty, HitTestForeground);
2601
2602         // Test floats next.
2603         if (!inside)
2604             inside = nodeAtPoint(request, result, point.x(), point.y(), tx, ty, HitTestFloat);
2605
2606         // Finally test to see if the mouse is in the background (within a child block's background).
2607         if (!inside)
2608             inside = nodeAtPoint(request, result, point.x(), point.y(), tx, ty, HitTestChildBlockBackgrounds);
2609     }
2610
2611     // See if the mouse is inside us but not any of our descendants
2612     if (hitTestFilter != HitTestDescendants && !inside)
2613         inside = nodeAtPoint(request, result, point.x(), point.y(), tx, ty, HitTestBlockBackground);
2614
2615     return inside;
2616 }
2617
2618 void RenderObject::updateHitTestResult(HitTestResult& result, const IntPoint& point)
2619 {
2620     if (result.innerNode())
2621         return;
2622
2623     Node* node = element();
2624     IntPoint localPoint(point);
2625     if (isRenderView())
2626         node = document()->documentElement();
2627     else if (!isInline() && continuation())
2628         // We are in the margins of block elements that are part of a continuation.  In
2629         // this case we're actually still inside the enclosing inline element that was
2630         // split.  Go ahead and set our inner node accordingly.
2631         node = continuation()->element();
2632
2633     if (node) {
2634         if (node->renderer() && node->renderer()->continuation() && node->renderer() != this) {
2635             // We're in the continuation of a split inline.  Adjust our local point to be in the coordinate space
2636             // of the principal renderer's containing block.  This will end up being the innerNonSharedNode.
2637             RenderObject* firstBlock = node->renderer()->containingBlock();
2638             
2639             // Get our containing block.
2640             RenderObject* block = this;
2641             if (isInline())
2642                 block = containingBlock();
2643         
2644             localPoint.move(block->xPos() - firstBlock->xPos(), block->yPos() - firstBlock->yPos());
2645         }
2646
2647         result.setInnerNode(node);
2648         if (!result.innerNonSharedNode())
2649             result.setInnerNonSharedNode(node);
2650         result.setLocalPoint(localPoint);
2651     }
2652 }
2653
2654 bool RenderObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, int /*x*/, int /*y*/, int /*tx*/, int /*ty*/, HitTestAction)
2655 {
2656     return false;
2657 }
2658
2659 short RenderObject::verticalPositionHint(bool firstLine) const
2660 {
2661     if (firstLine) // We're only really a first-line style if the document actually uses first-line rules.
2662         firstLine = document()->usesFirstLineRules();
2663     short vpos = m_verticalPosition;
2664     if (m_verticalPosition == PositionUndefined || firstLine) {
2665         vpos = getVerticalPosition(firstLine);
2666         if (!firstLine)
2667             m_verticalPosition = vpos;
2668     }
2669
2670     return vpos;
2671 }
2672
2673 short RenderObject::getVerticalPosition(bool firstLine) const
2674 {
2675     if (!isInline())
2676         return 0;
2677
2678     // This method determines the vertical position for inline elements.
2679     int vpos = 0;
2680     EVerticalAlign va = style()->verticalAlign();
2681     if (va == TOP)
2682         vpos = PositionTop;
2683     else if (va == BOTTOM)
2684         vpos = PositionBottom;
2685     else if (va == LENGTH)
2686         vpos = -style()->verticalAlignLength().calcValue(lineHeight(firstLine));
2687     else {
2688         bool checkParent = parent()->isInline() && !parent()->isInlineBlockOrInlineTable() && parent()->style()->verticalAlign() != TOP && parent()->style()->verticalAlign() != BOTTOM;
2689         vpos = checkParent ? parent()->verticalPositionHint(firstLine) : 0;
2690         // don't allow elements nested inside text-top to have a different valignment.
2691         if (va == BASELINE)
2692             return vpos;
2693
2694         const Font& f = parent()->style(firstLine)->font();
2695         int fontsize = f.pixelSize();
2696
2697         if (va == SUB)
2698             vpos += fontsize / 5 + 1;
2699         else if (va == SUPER)
2700             vpos -= fontsize / 3 + 1;
2701         else if (va == TEXT_TOP)
2702             vpos += baselinePosition(firstLine) - f.ascent();
2703         else if (va == MIDDLE)
2704             vpos += -static_cast<int>(f.xHeight() / 2) - lineHeight(firstLine) / 2 + baselinePosition(firstLine);
2705         else if (va == TEXT_BOTTOM) {
2706             vpos += f.descent();
2707             if (!isReplaced())
2708                 vpos -= style(firstLine)->font().descent();
2709         } else if (va == BASELINE_MIDDLE)
2710             vpos += -lineHeight(firstLine) / 2 + baselinePosition(firstLine);
2711     }
2712
2713     return vpos;
2714 }
2715
2716 short RenderObject::lineHeight(bool firstLine, bool /*isRootLineBox*/) const
2717 {
2718     RenderStyle* s = style(firstLine);
2719
2720     Length lh = s->lineHeight();
2721
2722     // its "unset", choose nice default
2723     if (lh.isNegative())
2724         return s->font().lineSpacing();
2725
2726     if (lh.isPercent())
2727         return lh.calcMinValue(s->fontSize());
2728
2729     // its fixed
2730     return lh.value();
2731 }
2732
2733 short RenderObject::baselinePosition(bool firstLine, bool isRootLineBox) const
2734 {
2735     const Font& f = style(firstLine)->font();
2736     return f.ascent() + (lineHeight(firstLine, isRootLineBox) - f.height()) / 2;
2737 }
2738
2739 void RenderObject::scheduleRelayout()
2740 {
2741     if (isRenderView()) {
2742         FrameView* view = static_cast<RenderView*>(this)->frameView();
2743         if (view)
2744             view->scheduleRelayout();
2745     } else if (parent()) {
2746         FrameView* v = view() ? view()->frameView() : 0;
2747         if (v)
2748             v->scheduleRelayoutOfSubtree(this);
2749     }
2750 }
2751
2752 void RenderObject::removeLeftoverAnonymousBlock(RenderBlock*)
2753 {
2754 }
2755
2756 InlineBox* RenderObject::createInlineBox(bool, bool isRootLineBox, bool)
2757 {
2758     ASSERT(!isRootLineBox);
2759     return new (renderArena()) InlineBox(this);
2760 }
2761
2762 void RenderObject::dirtyLineBoxes(bool, bool)
2763 {
2764 }
2765
2766 InlineBox* RenderObject::inlineBoxWrapper() const
2767 {
2768     return 0;
2769 }
2770
2771 void RenderObject::setInlineBoxWrapper(InlineBox*)
2772 {
2773 }
2774
2775 void RenderObject::deleteLineBoxWrapper()
2776 {
2777 }
2778
2779 RenderStyle* RenderObject::firstLineStyle() const
2780 {
2781     if (!document()->usesFirstLineRules())
2782         return m_style;
2783
2784     RenderStyle* s = m_style;
2785     const RenderObject* obj = isText() ? parent() : this;
2786     if (obj->isBlockFlow()) {
2787         RenderBlock* firstLineBlock = obj->firstLineBlock();
2788         if (firstLineBlock)
2789             s = firstLineBlock->getPseudoStyle(RenderStyle::FIRST_LINE, style());
2790     } else if (!obj->isAnonymous() && obj->isInlineFlow()) {
2791         RenderStyle* parentStyle = obj->parent()->firstLineStyle();
2792         if (parentStyle != obj->parent()->style()) {
2793             // A first-line style is in effect. We need to cache a first-line style
2794             // for ourselves.
2795             style()->setHasPseudoStyle(RenderStyle::FIRST_LINE_INHERITED);
2796             s = obj->getPseudoStyle(RenderStyle::FIRST_LINE_INHERITED, parentStyle);
2797         }
2798     }
2799     return s;
2800 }
2801
2802 RenderStyle* RenderObject::getPseudoStyle(RenderStyle::PseudoId pseudo, RenderStyle* parentStyle) const
2803 {
2804     if (pseudo < RenderStyle::FIRST_INTERNAL_PSEUDOID && !style()->hasPseudoStyle(pseudo))
2805         return 0;
2806
2807     if (!parentStyle)
2808         parentStyle = style();
2809
2810     RenderStyle* result = style()->getPseudoStyle(pseudo);
2811     if (result)
2812         return result;
2813
2814     Node* node = element();
2815     if (node && isText())
2816         node = node->parentNode();
2817     if (!node)
2818         return 0;
2819
2820     if (pseudo == RenderStyle::FIRST_LINE_INHERITED) {
2821         result = document()->styleSelector()->styleForElement(static_cast<Element*>(node), parentStyle, false);
2822         result->setStyleType(RenderStyle::FIRST_LINE_INHERITED);
2823     } else
2824         result = document()->styleSelector()->pseudoStyleForElement(pseudo, static_cast<Element*>(node), parentStyle);
2825     if (result) {
2826         style()->addPseudoStyle(result);
2827         result->deref(document()->renderArena());
2828     }
2829     return result;
2830 }
2831
2832 static Color decorationColor(RenderStyle* style)
2833 {
2834     Color result;
2835     if (style->textStrokeWidth() > 0) {
2836         // Prefer stroke color if possible but not if it's fully transparent.
2837         result = style->textStrokeColor();
2838         if (!result.isValid())
2839             result = style->color();
2840         if (result.alpha())
2841             return result;
2842     }
2843     
2844     result = style->textFillColor();
2845     if (!result.isValid())
2846         result = style->color();
2847     return result;
2848 }
2849
2850 void RenderObject::getTextDecorationColors(int decorations, Color& underline, Color& overline,
2851                                            Color& linethrough, bool quirksMode)
2852 {
2853     RenderObject* curr = this;
2854     do {
2855         int currDecs = curr->style()->textDecoration();
2856         if (currDecs) {
2857             if (currDecs & UNDERLINE) {
2858                 decorations &= ~UNDERLINE;
2859                 underline = decorationColor(curr->style());
2860             }
2861             if (currDecs & OVERLINE) {
2862                 decorations &= ~OVERLINE;
2863                 overline = decorationColor(curr->style());
2864             }
2865             if (currDecs & LINE_THROUGH) {
2866                 decorations &= ~LINE_THROUGH;
2867                 linethrough = decorationColor(curr->style());
2868             }
2869         }
2870         curr = curr->parent();
2871         if (curr && curr->isRenderBlock() && curr->continuation())
2872             curr = curr->continuation();
2873     } while (curr && decorations && (!quirksMode || !curr->element() ||
2874                                      (!curr->element()->hasTagName(aTag) && !curr->element()->hasTagName(fontTag))));
2875
2876     // If we bailed out, use the element we bailed out at (typically a <font> or <a> element).
2877     if (decorations && curr) {
2878         if (decorations & UNDERLINE)
2879             underline = decorationColor(curr->style());
2880         if (decorations & OVERLINE)
2881             overline = decorationColor(curr->style());
2882         if (decorations & LINE_THROUGH)
2883             linethrough = decorationColor(curr->style());
2884     }
2885 }
2886
2887 void RenderObject::updateWidgetPosition()
2888 {
2889 }
2890
2891 void RenderObject::addDashboardRegions(Vector<DashboardRegionValue>& regions)
2892 {
2893     // Convert the style regions to absolute coordinates.
2894     if (style()->visibility() != VISIBLE)
2895         return;
2896
2897     const Vector<StyleDashboardRegion>& styleRegions = style()->dashboardRegions();
2898     unsigned i, count = styleRegions.size();
2899     for (i = 0; i < count; i++) {
2900         StyleDashboardRegion styleRegion = styleRegions[i];
2901
2902         int w = width();
2903         int h = height();
2904
2905         DashboardRegionValue region;
2906         region.label = styleRegion.label;
2907         region.bounds = IntRect(styleRegion.offset.left.value(),
2908                                 styleRegion.offset.top.value(),
2909                                 w - styleRegion.offset.left.value() - styleRegion.offset.right.value(),
2910                                 h - styleRegion.offset.top.value() - styleRegion.offset.bottom.value());
2911         region.type = styleRegion.type;
2912
2913         region.clip = region.bounds;
2914         computeAbsoluteRepaintRect(region.clip);
2915         if (region.clip.height() < 0) {
2916             region.clip.setHeight(0);
2917             region.clip.setWidth(0);
2918         }
2919
2920         int x, y;
2921         absolutePosition(x, y);
2922         region.bounds.setX(x + styleRegion.offset.left.value());
2923         region.bounds.setY(y + styleRegion.offset.top.value());
2924
2925         if (document()->frame()) {
2926             float pageScaleFactor = document()->frame()->page()->chrome()->scaleFactor();
2927             if (pageScaleFactor != 1.0f) {
2928                 region.bounds.scale(pageScaleFactor);
2929                 region.clip.scale(pageScaleFactor);
2930             }
2931         }
2932
2933         regions.append(region);
2934     }
2935 }
2936
2937 void RenderObject::collectDashboardRegions(Vector<DashboardRegionValue>& regions)
2938 {
2939     // RenderTexts don't have their own style, they just use their parent's style,
2940     // so we don't want to include them.
2941     if (isText())
2942         return;
2943
2944     addDashboardRegions(regions);
2945     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
2946         curr->collectDashboardRegions(regions);
2947 }
2948
2949 bool RenderObject::avoidsFloats() const
2950 {
2951     return isReplaced() || hasOverflowClip() || isHR();
2952 }
2953
2954 bool RenderObject::shrinkToAvoidFloats() const
2955 {
2956     // FIXME: Technically we should be able to shrink replaced elements on a line, but this is difficult to accomplish, since this
2957     // involves doing a relayout during findNextLineBreak and somehow overriding the containingBlockWidth method to return the
2958     // current remaining width on a line.
2959     if (isInline() && !isHTMLMarquee() || !avoidsFloats())
2960         return false;
2961
2962     // All auto-width objects that avoid floats should always use lineWidth.
2963     return style()->width().isAuto();
2964 }
2965
2966 UChar RenderObject::backslashAsCurrencySymbol() const
2967 {
2968     if (Node *node = element()) {
2969         if (TextResourceDecoder* decoder = node->document()->decoder())
2970             return decoder->encoding().backslashAsCurrencySymbol();
2971     }
2972     return '\\';
2973 }
2974
2975 bool RenderObject::willRenderImage(CachedImage*)
2976 {
2977     // Without visibility we won't render (and therefore don't care about animation).
2978     if (style()->visibility() != VISIBLE)
2979         return false;
2980
2981     // 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)
2982     // then we don't want to render either.
2983     return !document()->inPageCache() && document()->view()->inWindow();
2984 }
2985
2986 int RenderObject::maximalOutlineSize(PaintPhase p) const
2987 {
2988     if (p != PaintPhaseOutline && p != PaintPhaseSelfOutline && p != PaintPhaseChildOutlines)
2989         return 0;
2990     return static_cast<RenderView*>(document()->renderer())->maximalOutlineSize();
2991 }
2992
2993 int RenderObject::caretMinOffset() const
2994 {
2995     return 0;
2996 }
2997
2998 int RenderObject::caretMaxOffset() const
2999 {
3000     return isReplaced() ? 1 : 0;
3001 }
3002
3003 unsigned RenderObject::caretMaxRenderedOffset() const
3004 {
3005     return 0;
3006 }
3007
3008 int RenderObject::previousOffset(int current) const
3009 {
3010     return current - 1;
3011 }
3012
3013 int RenderObject::nextOffset(int current) const
3014 {
3015     return current + 1;
3016 }
3017
3018 InlineBox* RenderObject::inlineBox(int offset, EAffinity affinity)
3019 {
3020     return inlineBoxWrapper();
3021 }
3022
3023 int RenderObject::maxTopMargin(bool positive) const
3024 {
3025     return positive ? max(0, marginTop()) : -min(0, marginTop());
3026 }
3027
3028 int RenderObject::maxBottomMargin(bool positive) const
3029 {
3030     return positive ? max(0, marginBottom()) : -min(0, marginBottom());
3031 }
3032
3033 IntRect RenderObject::contentBox() const
3034 {
3035     return IntRect(borderLeft() + paddingLeft(), borderTop() + paddingTop(),
3036         contentWidth(), contentHeight());
3037 }
3038
3039 IntRect RenderObject::absoluteContentBox() const
3040 {
3041     IntRect rect = contentBox();
3042     int x, y;
3043     absolutePositionForContent(x, y);
3044     rect.move(x, y);
3045     return rect;
3046 }
3047
3048 void RenderObject::adjustRectForOutlineAndShadow(IntRect& rect) const
3049 {
3050     int outlineSize = !isInline() && continuation() ? continuation()->style()->outlineSize() : style()->outlineSize();
3051     if (ShadowData* boxShadow = style()->boxShadow()) {
3052         int shadowLeft = min(boxShadow->x - boxShadow->blur - outlineSize, 0);
3053         int shadowRight = max(boxShadow->x + boxShadow->blur + outlineSize, 0);
3054         int shadowTop = min(boxShadow->y - boxShadow->blur - outlineSize, 0);
3055         int shadowBottom = max(boxShadow->y + boxShadow->blur + outlineSize, 0);
3056         rect.move(shadowLeft, shadowTop);
3057         rect.setWidth(rect.width() - shadowLeft + shadowRight);
3058         rect.setHeight(rect.height() - shadowTop + shadowBottom);
3059     } else
3060         rect.inflate(outlineSize);
3061 }
3062
3063 IntRect RenderObject::absoluteOutlineBox() const
3064 {
3065     IntRect box = borderBox();
3066     int x, y;
3067     absolutePosition(x, y);
3068     box.move(x, y);
3069     box.move(view()->layoutDelta());
3070     adjustRectForOutlineAndShadow(box);
3071     return box;
3072 }
3073
3074 bool RenderObject::isScrollable() const
3075 {
3076     RenderLayer* l = enclosingLayer();
3077     return l && (l->verticalScrollbar() || l->horizontalScrollbar());
3078 }
3079
3080 AnimationController* RenderObject::animationController() const
3081 {
3082     return document()->frame()->animationController();
3083 }
3084
3085 #if ENABLE(SVG)
3086
3087 FloatRect RenderObject::relativeBBox(bool) const
3088 {
3089     return FloatRect();
3090 }
3091
3092 AffineTransform RenderObject::localTransform() const
3093 {
3094     return AffineTransform(1, 0, 0, 1, xPos(), yPos());
3095 }
3096
3097 AffineTransform RenderObject::absoluteTransform() const
3098 {
3099     if (parent())
3100         return localTransform() * parent()->absoluteTransform();
3101     return localTransform();
3102 }
3103
3104 #endif // ENABLE(SVG)
3105
3106 } // namespace WebCore
3107
3108 #ifndef NDEBUG
3109
3110 void showTree(const WebCore::RenderObject* ro)
3111 {
3112     if (ro)
3113         ro->showTreeForThis();
3114 }
3115
3116 #endif