• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2000 Dirk Mueller (mueller@kde.org)
5  *           (C) 2004 Allan Sandfeld Jensen (kde@carewolf.com)
6  * Copyright (C) 2004, 2005, 2006, 2007, 2008 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 "CSSStyleSelector.h"
30 #include "FloatQuad.h"
31 #include "Frame.h"
32 #include "FrameView.h"
33 #include "GraphicsContext.h"
34 #include "HTMLNames.h"
35 #include "HitTestResult.h"
36 #include "Page.h"
37 #include "RenderArena.h"
38 #include "RenderCounter.h"
39 #include "RenderFlexibleBox.h"
40 #include "RenderImageGeneratedContent.h"
41 #include "RenderInline.h"
42 #include "RenderListItem.h"
43 #include "RenderTableCell.h"
44 #include "RenderTableCol.h"
45 #include "RenderTableRow.h"
46 #include "RenderTheme.h"
47 #include "RenderView.h"
48 #include "TransformState.h"
49 #include <algorithm>
50 #ifdef ANDROID_LAYOUT
51 #include "Settings.h"
52 #endif
53 #include <stdio.h>
54 #include <wtf/RefCountedLeakCounter.h>
55 #include <wtf/UnusedParam.h>
56 
57 #if USE(ACCELERATED_COMPOSITING)
58 #include "RenderLayerCompositor.h"
59 #endif
60 
61 #if ENABLE(WML)
62 #include "WMLNames.h"
63 #endif
64 
65 using namespace std;
66 
67 namespace WebCore {
68 
69 using namespace HTMLNames;
70 
71 #ifndef NDEBUG
72 static void* baseOfRenderObjectBeingDeleted;
73 #endif
74 
75 bool RenderObject::s_affectsParentBlock = false;
76 
operator new(size_t sz,RenderArena * renderArena)77 void* RenderObject::operator new(size_t sz, RenderArena* renderArena) throw()
78 {
79     return renderArena->allocate(sz);
80 }
81 
operator delete(void * ptr,size_t sz)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 
createObject(Node * node,RenderStyle * style)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->next() && contentData->isImage() && doc != node) {
100         RenderImageGeneratedContent* image = new (arena) RenderImageGeneratedContent(node);
101         image->setStyle(style);
102         if (StyleImage* styleImage = contentData->image())
103             image->setStyleImage(styleImage);
104         return image;
105     }
106 
107     RenderObject* o = 0;
108 
109     switch (style->display()) {
110         case NONE:
111             break;
112         case INLINE:
113             o = new (arena) RenderInline(node);
114             break;
115         case BLOCK:
116             o = new (arena) RenderBlock(node);
117             break;
118         case INLINE_BLOCK:
119             o = new (arena) RenderBlock(node);
120             break;
121         case LIST_ITEM:
122             o = new (arena) RenderListItem(node);
123             break;
124         case RUN_IN:
125         case COMPACT:
126             o = new (arena) RenderBlock(node);
127             break;
128         case TABLE:
129         case INLINE_TABLE:
130             o = new (arena) RenderTable(node);
131             break;
132         case TABLE_ROW_GROUP:
133         case TABLE_HEADER_GROUP:
134         case TABLE_FOOTER_GROUP:
135             o = new (arena) RenderTableSection(node);
136             break;
137         case TABLE_ROW:
138             o = new (arena) RenderTableRow(node);
139             break;
140         case TABLE_COLUMN_GROUP:
141         case TABLE_COLUMN:
142             o = new (arena) RenderTableCol(node);
143             break;
144         case TABLE_CELL:
145             o = new (arena) RenderTableCell(node);
146             break;
147         case TABLE_CAPTION:
148             o = new (arena) RenderBlock(node);
149             break;
150         case BOX:
151         case INLINE_BOX:
152             o = new (arena) RenderFlexibleBox(node);
153             break;
154     }
155 
156     return o;
157 }
158 
159 #ifndef NDEBUG
160 static WTF::RefCountedLeakCounter renderObjectCounter("RenderObject");
161 #endif
162 
RenderObject(Node * node)163 RenderObject::RenderObject(Node* node)
164     : CachedResourceClient()
165     , m_style(0)
166     , m_node(node)
167     , m_parent(0)
168     , m_previous(0)
169     , m_next(0)
170 #ifndef NDEBUG
171     , m_hasAXObject(false)
172     , m_setNeedsLayoutForbidden(false)
173 #endif
174     , m_needsLayout(false)
175     , m_needsPositionedMovementLayout(false)
176     , m_normalChildNeedsLayout(false)
177     , m_posChildNeedsLayout(false)
178     , m_prefWidthsDirty(false)
179     , m_floating(false)
180     , m_positioned(false)
181     , m_relPositioned(false)
182     , m_paintBackground(false)
183     , m_isAnonymous(node == node->document())
184     , m_isText(false)
185     , m_isBox(false)
186     , m_inline(true)
187     , m_replaced(false)
188     , m_isDragging(false)
189     , m_hasLayer(false)
190     , m_hasOverflowClip(false)
191     , m_hasTransform(false)
192     , m_hasReflection(false)
193     , m_hasOverrideSize(false)
194     , m_hasCounterNodeMap(false)
195     , m_everHadLayout(false)
196     , m_childrenInline(false)
197     , m_topMarginQuirk(false)
198     , m_bottomMarginQuirk(false)
199     , m_hasMarkupTruncation(false)
200     , m_selectionState(SelectionNone)
201     , m_hasColumns(false)
202     , m_cellWidthChanged(false)
203     , m_replacedHasOverflow(false)
204 {
205 #ifndef NDEBUG
206     renderObjectCounter.increment();
207 #endif
208     ASSERT(node);
209 }
210 
~RenderObject()211 RenderObject::~RenderObject()
212 {
213     ASSERT(!node() || documentBeingDestroyed() || !document()->frame()->view() || document()->frame()->view()->layoutRoot() != this);
214 #ifndef NDEBUG
215     ASSERT(!m_hasAXObject);
216     renderObjectCounter.decrement();
217 #endif
218 }
219 
theme() const220 RenderTheme* RenderObject::theme() const
221 {
222     ASSERT(document()->page());
223 
224     return document()->page()->theme();
225 }
226 
isDescendantOf(const RenderObject * obj) const227 bool RenderObject::isDescendantOf(const RenderObject* obj) const
228 {
229     for (const RenderObject* r = this; r; r = r->m_parent) {
230         if (r == obj)
231             return true;
232     }
233     return false;
234 }
235 
isBody() const236 bool RenderObject::isBody() const
237 {
238     return node() && node()->hasTagName(bodyTag);
239 }
240 
isHR() const241 bool RenderObject::isHR() const
242 {
243     return node() && node()->hasTagName(hrTag);
244 }
245 
isHTMLMarquee() const246 bool RenderObject::isHTMLMarquee() const
247 {
248     return node() && node()->renderer() == this && node()->hasTagName(marqueeTag);
249 }
250 
updateListMarkerNumbers(RenderObject * child)251 static void updateListMarkerNumbers(RenderObject* child)
252 {
253     for (RenderObject* sibling = child; sibling; sibling = sibling->nextSibling()) {
254         if (sibling->isListItem())
255             toRenderListItem(sibling)->updateValue();
256     }
257 }
258 
addChild(RenderObject * newChild,RenderObject * beforeChild)259 void RenderObject::addChild(RenderObject* newChild, RenderObject* beforeChild)
260 {
261     RenderObjectChildList* children = virtualChildren();
262     ASSERT(children);
263     if (!children)
264         return;
265 
266     bool needsTable = false;
267 
268     if (newChild->isListItem())
269         updateListMarkerNumbers(beforeChild ? beforeChild : children->lastChild());
270     else if (newChild->isTableCol() && newChild->style()->display() == TABLE_COLUMN_GROUP)
271         needsTable = !isTable();
272     else if (newChild->isRenderBlock() && newChild->style()->display() == TABLE_CAPTION)
273         needsTable = !isTable();
274     else if (newChild->isTableSection())
275         needsTable = !isTable();
276     else if (newChild->isTableRow())
277         needsTable = !isTableSection();
278     else if (newChild->isTableCell()) {
279         needsTable = !isTableRow();
280         // I'm not 100% sure this is the best way to fix this, but without this
281         // change we recurse infinitely when trying to render the CSS2 test page:
282         // http://www.bath.ac.uk/%7Epy8ieh/internet/eviltests/htmlbodyheadrendering2.html.
283         // See Radar 2925291.
284         if (needsTable && isTableCell() && !children->firstChild() && !newChild->isTableCell())
285             needsTable = false;
286     }
287 
288     if (needsTable) {
289         RenderTable* table;
290         RenderObject* afterChild = beforeChild ? beforeChild->previousSibling() : children->lastChild();
291         if (afterChild && afterChild->isAnonymous() && afterChild->isTable())
292             table = toRenderTable(afterChild);
293         else {
294             table = new (renderArena()) RenderTable(document() /* is anonymous */);
295             RefPtr<RenderStyle> newStyle = RenderStyle::create();
296             newStyle->inheritFrom(style());
297             newStyle->setDisplay(TABLE);
298             table->setStyle(newStyle.release());
299             addChild(table, beforeChild);
300         }
301         table->addChild(newChild);
302     } else {
303         // Just add it...
304         children->insertChildNode(this, newChild, beforeChild);
305     }
306 
307     if (newChild->isText() && newChild->style()->textTransform() == CAPITALIZE) {
308         RefPtr<StringImpl> textToTransform = toRenderText(newChild)->originalText();
309         if (textToTransform)
310             toRenderText(newChild)->setText(textToTransform.release(), true);
311     }
312 }
313 
removeChild(RenderObject * oldChild)314 void RenderObject::removeChild(RenderObject* oldChild)
315 {
316     RenderObjectChildList* children = virtualChildren();
317     ASSERT(children);
318     if (!children)
319         return;
320 
321     // We do this here instead of in removeChildNode, since the only extremely low-level uses of remove/appendChildNode
322     // cannot affect the positioned object list, and the floating object list is irrelevant (since the list gets cleared on
323     // layout anyway).
324     if (oldChild->isFloatingOrPositioned())
325         toRenderBox(oldChild)->removeFloatingOrPositionedChildFromBlockLists();
326 
327     children->removeChildNode(this, oldChild);
328 }
329 
nextInPreOrder() const330 RenderObject* RenderObject::nextInPreOrder() const
331 {
332     if (RenderObject* o = firstChild())
333         return o;
334 
335     return nextInPreOrderAfterChildren();
336 }
337 
nextInPreOrderAfterChildren() const338 RenderObject* RenderObject::nextInPreOrderAfterChildren() const
339 {
340     RenderObject* o;
341     if (!(o = nextSibling())) {
342         o = parent();
343         while (o && !o->nextSibling())
344             o = o->parent();
345         if (o)
346             o = o->nextSibling();
347     }
348 
349     return o;
350 }
351 
nextInPreOrder(RenderObject * stayWithin) const352 RenderObject* RenderObject::nextInPreOrder(RenderObject* stayWithin) const
353 {
354     if (RenderObject* o = firstChild())
355         return o;
356 
357     return nextInPreOrderAfterChildren(stayWithin);
358 }
359 
nextInPreOrderAfterChildren(RenderObject * stayWithin) const360 RenderObject* RenderObject::nextInPreOrderAfterChildren(RenderObject* stayWithin) const
361 {
362     if (this == stayWithin)
363         return 0;
364 
365     RenderObject* o;
366     if (!(o = nextSibling())) {
367         o = parent();
368         while (o && !o->nextSibling()) {
369             if (o == stayWithin)
370                 return 0;
371             o = o->parent();
372         }
373         if (o)
374             o = o->nextSibling();
375     }
376 
377     return o;
378 }
379 
previousInPreOrder() const380 RenderObject* RenderObject::previousInPreOrder() const
381 {
382     if (RenderObject* o = previousSibling()) {
383         while (o->lastChild())
384             o = o->lastChild();
385         return o;
386     }
387 
388     return parent();
389 }
390 
childAt(unsigned index) const391 RenderObject* RenderObject::childAt(unsigned index) const
392 {
393     RenderObject* child = firstChild();
394     for (unsigned i = 0; child && i < index; i++)
395         child = child->nextSibling();
396     return child;
397 }
398 
firstLeafChild() const399 RenderObject* RenderObject::firstLeafChild() const
400 {
401     RenderObject* r = firstChild();
402     while (r) {
403         RenderObject* n = 0;
404         n = r->firstChild();
405         if (!n)
406             break;
407         r = n;
408     }
409     return r;
410 }
411 
lastLeafChild() const412 RenderObject* RenderObject::lastLeafChild() const
413 {
414     RenderObject* r = lastChild();
415     while (r) {
416         RenderObject* n = 0;
417         n = r->lastChild();
418         if (!n)
419             break;
420         r = n;
421     }
422     return r;
423 }
424 
addLayers(RenderObject * obj,RenderLayer * parentLayer,RenderObject * & newObject,RenderLayer * & beforeChild)425 static void addLayers(RenderObject* obj, RenderLayer* parentLayer, RenderObject*& newObject,
426                       RenderLayer*& beforeChild)
427 {
428     if (obj->hasLayer()) {
429         if (!beforeChild && newObject) {
430             // We need to figure out the layer that follows newObject.  We only do
431             // this the first time we find a child layer, and then we update the
432             // pointer values for newObject and beforeChild used by everyone else.
433             beforeChild = newObject->parent()->findNextLayer(parentLayer, newObject);
434             newObject = 0;
435         }
436         parentLayer->addChild(toRenderBoxModelObject(obj)->layer(), beforeChild);
437         return;
438     }
439 
440     for (RenderObject* curr = obj->firstChild(); curr; curr = curr->nextSibling())
441         addLayers(curr, parentLayer, newObject, beforeChild);
442 }
443 
addLayers(RenderLayer * parentLayer,RenderObject * newObject)444 void RenderObject::addLayers(RenderLayer* parentLayer, RenderObject* newObject)
445 {
446     if (!parentLayer)
447         return;
448 
449     RenderObject* object = newObject;
450     RenderLayer* beforeChild = 0;
451     WebCore::addLayers(this, parentLayer, object, beforeChild);
452 }
453 
removeLayers(RenderLayer * parentLayer)454 void RenderObject::removeLayers(RenderLayer* parentLayer)
455 {
456     if (!parentLayer)
457         return;
458 
459     if (hasLayer()) {
460         parentLayer->removeChild(toRenderBoxModelObject(this)->layer());
461         return;
462     }
463 
464     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
465         curr->removeLayers(parentLayer);
466 }
467 
moveLayers(RenderLayer * oldParent,RenderLayer * newParent)468 void RenderObject::moveLayers(RenderLayer* oldParent, RenderLayer* newParent)
469 {
470     if (!newParent)
471         return;
472 
473     if (hasLayer()) {
474         RenderLayer* layer = toRenderBoxModelObject(this)->layer();
475         ASSERT(oldParent == layer->parent());
476         if (oldParent)
477             oldParent->removeChild(layer);
478         newParent->addChild(layer);
479         return;
480     }
481 
482     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
483         curr->moveLayers(oldParent, newParent);
484 }
485 
findNextLayer(RenderLayer * parentLayer,RenderObject * startPoint,bool checkParent)486 RenderLayer* RenderObject::findNextLayer(RenderLayer* parentLayer, RenderObject* startPoint,
487                                          bool checkParent)
488 {
489     // Error check the parent layer passed in.  If it's null, we can't find anything.
490     if (!parentLayer)
491         return 0;
492 
493     // Step 1: If our layer is a child of the desired parent, then return our layer.
494     RenderLayer* ourLayer = hasLayer() ? toRenderBoxModelObject(this)->layer() : 0;
495     if (ourLayer && ourLayer->parent() == parentLayer)
496         return ourLayer;
497 
498     // Step 2: If we don't have a layer, or our layer is the desired parent, then descend
499     // into our siblings trying to find the next layer whose parent is the desired parent.
500     if (!ourLayer || ourLayer == parentLayer) {
501         for (RenderObject* curr = startPoint ? startPoint->nextSibling() : firstChild();
502              curr; curr = curr->nextSibling()) {
503             RenderLayer* nextLayer = curr->findNextLayer(parentLayer, 0, false);
504             if (nextLayer)
505                 return nextLayer;
506         }
507     }
508 
509     // Step 3: If our layer is the desired parent layer, then we're finished.  We didn't
510     // find anything.
511     if (parentLayer == ourLayer)
512         return 0;
513 
514     // Step 4: If |checkParent| is set, climb up to our parent and check its siblings that
515     // follow us to see if we can locate a layer.
516     if (checkParent && parent())
517         return parent()->findNextLayer(parentLayer, this, true);
518 
519     return 0;
520 }
521 
enclosingLayer() const522 RenderLayer* RenderObject::enclosingLayer() const
523 {
524     const RenderObject* curr = this;
525     while (curr) {
526         RenderLayer* layer = curr->hasLayer() ? toRenderBoxModelObject(curr)->layer() : 0;
527         if (layer)
528             return layer;
529         curr = curr->parent();
530     }
531     return 0;
532 }
533 
enclosingSelfPaintingLayer() const534 RenderLayer* RenderObject::enclosingSelfPaintingLayer() const
535 {
536     const RenderObject* curr = this;
537     while (curr) {
538         RenderLayer* layer = curr->hasLayer() ? toRenderBoxModelObject(curr)->layer() : 0;
539         if (layer && layer->isSelfPaintingLayer())
540             return layer;
541         curr = curr->parent();
542     }
543     return 0;
544 }
545 
enclosingBox() const546 RenderBox* RenderObject::enclosingBox() const
547 {
548     RenderObject* curr = const_cast<RenderObject*>(this);
549     while (curr) {
550         if (curr->isBox())
551             return toRenderBox(curr);
552         curr = curr->parent();
553     }
554 
555     ASSERT_NOT_REACHED();
556     return 0;
557 }
558 
firstLineBlock() const559 RenderBlock* RenderObject::firstLineBlock() const
560 {
561     return 0;
562 }
563 
setPrefWidthsDirty(bool b,bool markParents)564 void RenderObject::setPrefWidthsDirty(bool b, bool markParents)
565 {
566     bool alreadyDirty = m_prefWidthsDirty;
567     m_prefWidthsDirty = b;
568     if (b && !alreadyDirty && markParents && (isText() || (style()->position() != FixedPosition && style()->position() != AbsolutePosition)))
569         invalidateContainerPrefWidths();
570 }
571 
invalidateContainerPrefWidths()572 void RenderObject::invalidateContainerPrefWidths()
573 {
574     // In order to avoid pathological behavior when inlines are deeply nested, we do include them
575     // in the chain that we mark dirty (even though they're kind of irrelevant).
576     RenderObject* o = isTableCell() ? containingBlock() : container();
577     while (o && !o->m_prefWidthsDirty) {
578         // Don't invalidate the outermost object of an unrooted subtree. That object will be
579         // invalidated when the subtree is added to the document.
580         RenderObject* container = o->isTableCell() ? o->containingBlock() : o->container();
581         if (!container && !o->isRenderView())
582             break;
583 
584         o->m_prefWidthsDirty = true;
585         if (o->style()->position() == FixedPosition || o->style()->position() == AbsolutePosition)
586             // A positioned object has no effect on the min/max width of its containing block ever.
587             // We can optimize this case and not go up any further.
588             break;
589         o = container;
590     }
591 }
592 
setLayerNeedsFullRepaint()593 void RenderObject::setLayerNeedsFullRepaint()
594 {
595     ASSERT(hasLayer());
596     toRenderBoxModelObject(this)->layer()->setNeedsFullRepaint(true);
597 }
598 
containingBlock() const599 RenderBlock* RenderObject::containingBlock() const
600 {
601     if (isTableCell()) {
602         const RenderTableCell* cell = toRenderTableCell(this);
603         if (parent() && cell->section())
604             return cell->table();
605         return 0;
606     }
607 
608     if (isRenderView())
609         return const_cast<RenderView*>(toRenderView(this));
610 
611     RenderObject* o = parent();
612     if (!isText() && m_style->position() == FixedPosition) {
613         while (o && !o->isRenderView() && !(o->hasTransform() && o->isRenderBlock()))
614             o = o->parent();
615     } else if (!isText() && m_style->position() == AbsolutePosition) {
616         while (o && (o->style()->position() == StaticPosition || (o->isInline() && !o->isReplaced())) && !o->isRenderView() && !(o->hasTransform() && o->isRenderBlock())) {
617             // For relpositioned inlines, we return the nearest enclosing block.  We don't try
618             // to return the inline itself.  This allows us to avoid having a positioned objects
619             // list in all RenderInlines and lets us return a strongly-typed RenderBlock* result
620             // from this method.  The container() method can actually be used to obtain the
621             // inline directly.
622             if (o->style()->position() == RelativePosition && o->isInline() && !o->isReplaced())
623                 return o->containingBlock();
624             o = o->parent();
625         }
626     } else {
627         while (o && ((o->isInline() && !o->isReplaced()) || o->isTableRow() || o->isTableSection()
628                      || o->isTableCol() || o->isFrameSet() || o->isMedia()
629 #if ENABLE(SVG)
630                      || o->isSVGContainer() || o->isSVGRoot()
631 #endif
632                      ))
633             o = o->parent();
634     }
635 
636     if (!o || !o->isRenderBlock())
637         return 0; // Probably doesn't happen any more, but leave just in case. -dwh
638 
639     return toRenderBlock(o);
640 }
641 
mustRepaintFillLayers(const RenderObject * renderer,const FillLayer * layer)642 static bool mustRepaintFillLayers(const RenderObject* renderer, const FillLayer* layer)
643 {
644     // Nobody will use multiple layers without wanting fancy positioning.
645     if (layer->next())
646         return true;
647 
648     // Make sure we have a valid image.
649     StyleImage* img = layer->image();
650     if (!img || !img->canRender(renderer->style()->effectiveZoom()))
651         return false;
652 
653     if (!layer->xPosition().isZero() || !layer->yPosition().isZero())
654         return true;
655 
656     if (layer->isSizeSet()) {
657         if (layer->size().width().isPercent() || layer->size().height().isPercent())
658             return true;
659     } else if (img->usesImageContainerSize())
660         return true;
661 
662     return false;
663 }
664 
mustRepaintBackgroundOrBorder() const665 bool RenderObject::mustRepaintBackgroundOrBorder() const
666 {
667     if (hasMask() && mustRepaintFillLayers(this, style()->maskLayers()))
668         return true;
669 
670     // If we don't have a background/border/mask, then nothing to do.
671     if (!hasBoxDecorations())
672         return false;
673 
674     if (mustRepaintFillLayers(this, style()->backgroundLayers()))
675         return true;
676 
677     // Our fill layers are ok.  Let's check border.
678     if (style()->hasBorder()) {
679         // Border images are not ok.
680         StyleImage* borderImage = style()->borderImage().image();
681         bool shouldPaintBorderImage = borderImage && borderImage->canRender(style()->effectiveZoom());
682 
683         // If the image hasn't loaded, we're still using the normal border style.
684         if (shouldPaintBorderImage && borderImage->isLoaded())
685             return true;
686     }
687 
688     return false;
689 }
690 
drawLineForBoxSide(GraphicsContext * graphicsContext,int x1,int y1,int x2,int y2,BoxSide s,Color c,const Color & textcolor,EBorderStyle style,int adjbw1,int adjbw2)691 void RenderObject::drawLineForBoxSide(GraphicsContext* graphicsContext, int x1, int y1, int x2, int y2,
692                                       BoxSide s, Color c, const Color& textcolor, EBorderStyle style,
693                                       int adjbw1, int adjbw2)
694 {
695     int width = (s == BSTop || s == BSBottom ? y2 - y1 : x2 - x1);
696 
697     if (style == DOUBLE && width < 3)
698         style = SOLID;
699 
700     if (!c.isValid()) {
701         if (style == INSET || style == OUTSET || style == RIDGE || style == GROOVE)
702             c.setRGB(238, 238, 238);
703         else
704             c = textcolor;
705     }
706 
707     switch (style) {
708         case BNONE:
709         case BHIDDEN:
710             return;
711         case DOTTED:
712         case DASHED:
713             graphicsContext->setStrokeColor(c);
714             graphicsContext->setStrokeThickness(width);
715             graphicsContext->setStrokeStyle(style == DASHED ? DashedStroke : DottedStroke);
716 
717             if (width > 0)
718                 switch (s) {
719                     case BSBottom:
720                     case BSTop:
721                         graphicsContext->drawLine(IntPoint(x1, (y1 + y2) / 2), IntPoint(x2, (y1 + y2) / 2));
722                         break;
723                     case BSRight:
724                     case BSLeft:
725                         graphicsContext->drawLine(IntPoint((x1 + x2) / 2, y1), IntPoint((x1 + x2) / 2, y2));
726                         break;
727                 }
728             break;
729         case DOUBLE: {
730             int third = (width + 1) / 3;
731 
732             if (adjbw1 == 0 && adjbw2 == 0) {
733                 graphicsContext->setStrokeStyle(NoStroke);
734                 graphicsContext->setFillColor(c);
735                 switch (s) {
736                     case BSTop:
737                     case BSBottom:
738                         graphicsContext->drawRect(IntRect(x1, y1, x2 - x1, third));
739                         graphicsContext->drawRect(IntRect(x1, y2 - third, x2 - x1, third));
740                         break;
741                     case BSLeft:
742                         graphicsContext->drawRect(IntRect(x1, y1 + 1, third, y2 - y1 - 1));
743                         graphicsContext->drawRect(IntRect(x2 - third, y1 + 1, third, y2 - y1 - 1));
744                         break;
745                     case BSRight:
746                         graphicsContext->drawRect(IntRect(x1, y1 + 1, third, y2 - y1 - 1));
747                         graphicsContext->drawRect(IntRect(x2 - third, y1 + 1, third, y2 - y1 - 1));
748                         break;
749                 }
750             } else {
751                 int adjbw1bigthird = ((adjbw1 > 0) ? adjbw1 + 1 : adjbw1 - 1) / 3;
752                 int adjbw2bigthird = ((adjbw2 > 0) ? adjbw2 + 1 : adjbw2 - 1) / 3;
753 
754                 switch (s) {
755                     case BSTop:
756                         drawLineForBoxSide(graphicsContext, x1 + max((-adjbw1 * 2 + 1) / 3, 0),
757                                    y1, x2 - max((-adjbw2 * 2 + 1) / 3, 0), y1 + third,
758                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
759                         drawLineForBoxSide(graphicsContext, x1 + max((adjbw1 * 2 + 1) / 3, 0),
760                                    y2 - third, x2 - max((adjbw2 * 2 + 1) / 3, 0), y2,
761                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
762                         break;
763                     case BSLeft:
764                         drawLineForBoxSide(graphicsContext, x1, y1 + max((-adjbw1 * 2 + 1) / 3, 0),
765                                    x1 + third, y2 - max((-adjbw2 * 2 + 1) / 3, 0),
766                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
767                         drawLineForBoxSide(graphicsContext, x2 - third, y1 + max((adjbw1 * 2 + 1) / 3, 0),
768                                    x2, y2 - max((adjbw2 * 2 + 1) / 3, 0),
769                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
770                         break;
771                     case BSBottom:
772                         drawLineForBoxSide(graphicsContext, x1 + max((adjbw1 * 2 + 1) / 3, 0),
773                                    y1, x2 - max((adjbw2 * 2 + 1) / 3, 0), y1 + third,
774                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
775                         drawLineForBoxSide(graphicsContext, x1 + max((-adjbw1 * 2 + 1) / 3, 0),
776                                    y2 - third, x2 - max((-adjbw2 * 2 + 1) / 3, 0), y2,
777                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
778                         break;
779                     case BSRight:
780                         drawLineForBoxSide(graphicsContext, x1, y1 + max((adjbw1 * 2 + 1) / 3, 0),
781                                    x1 + third, y2 - max((adjbw2 * 2 + 1) / 3, 0),
782                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
783                         drawLineForBoxSide(graphicsContext, x2 - third, y1 + max((-adjbw1 * 2 + 1) / 3, 0),
784                                    x2, y2 - max((-adjbw2 * 2 + 1) / 3, 0),
785                                    s, c, textcolor, SOLID, adjbw1bigthird, adjbw2bigthird);
786                         break;
787                     default:
788                         break;
789                 }
790             }
791             break;
792         }
793         case RIDGE:
794         case GROOVE:
795         {
796             EBorderStyle s1;
797             EBorderStyle s2;
798             if (style == GROOVE) {
799                 s1 = INSET;
800                 s2 = OUTSET;
801             } else {
802                 s1 = OUTSET;
803                 s2 = INSET;
804             }
805 
806             int adjbw1bighalf = ((adjbw1 > 0) ? adjbw1 + 1 : adjbw1 - 1) / 2;
807             int adjbw2bighalf = ((adjbw2 > 0) ? adjbw2 + 1 : adjbw2 - 1) / 2;
808 
809             switch (s) {
810                 case BSTop:
811                     drawLineForBoxSide(graphicsContext, x1 + max(-adjbw1, 0) / 2, y1, x2 - max(-adjbw2, 0) / 2, (y1 + y2 + 1) / 2,
812                                s, c, textcolor, s1, adjbw1bighalf, adjbw2bighalf);
813                     drawLineForBoxSide(graphicsContext, x1 + max(adjbw1 + 1, 0) / 2, (y1 + y2 + 1) / 2, x2 - max(adjbw2 + 1, 0) / 2, y2,
814                                s, c, textcolor, s2, adjbw1 / 2, adjbw2 / 2);
815                     break;
816                 case BSLeft:
817                     drawLineForBoxSide(graphicsContext, x1, y1 + max(-adjbw1, 0) / 2, (x1 + x2 + 1) / 2, y2 - max(-adjbw2, 0) / 2,
818                                s, c, textcolor, s1, adjbw1bighalf, adjbw2bighalf);
819                     drawLineForBoxSide(graphicsContext, (x1 + x2 + 1) / 2, y1 + max(adjbw1 + 1, 0) / 2, x2, y2 - max(adjbw2 + 1, 0) / 2,
820                                s, c, textcolor, s2, adjbw1 / 2, adjbw2 / 2);
821                     break;
822                 case BSBottom:
823                     drawLineForBoxSide(graphicsContext, x1 + max(adjbw1, 0) / 2, y1, x2 - max(adjbw2, 0) / 2, (y1 + y2 + 1) / 2,
824                                s, c, textcolor, s2, adjbw1bighalf, adjbw2bighalf);
825                     drawLineForBoxSide(graphicsContext, x1 + max(-adjbw1 + 1, 0) / 2, (y1 + y2 + 1) / 2, x2 - max(-adjbw2 + 1, 0) / 2, y2,
826                                s, c, textcolor, s1, adjbw1/2, adjbw2/2);
827                     break;
828                 case BSRight:
829                     drawLineForBoxSide(graphicsContext, x1, y1 + max(adjbw1, 0) / 2, (x1 + x2 + 1) / 2, y2 - max(adjbw2, 0) / 2,
830                                s, c, textcolor, s2, adjbw1bighalf, adjbw2bighalf);
831                     drawLineForBoxSide(graphicsContext, (x1 + x2 + 1) / 2, y1 + max(-adjbw1 + 1, 0) / 2, x2, y2 - max(-adjbw2 + 1, 0) / 2,
832                                s, c, textcolor, s1, adjbw1/2, adjbw2/2);
833                     break;
834             }
835             break;
836         }
837         case INSET:
838             if (s == BSTop || s == BSLeft)
839                 c = c.dark();
840             // fall through
841         case OUTSET:
842             if (style == OUTSET && (s == BSBottom || s == BSRight))
843                 c = c.dark();
844             // fall through
845         case SOLID: {
846             graphicsContext->setStrokeStyle(NoStroke);
847             graphicsContext->setFillColor(c);
848             ASSERT(x2 >= x1);
849             ASSERT(y2 >= y1);
850             if (!adjbw1 && !adjbw2) {
851                 graphicsContext->drawRect(IntRect(x1, y1, x2 - x1, y2 - y1));
852                 return;
853             }
854             FloatPoint quad[4];
855             switch (s) {
856                 case BSTop:
857                     quad[0] = FloatPoint(x1 + max(-adjbw1, 0), y1);
858                     quad[1] = FloatPoint(x1 + max(adjbw1, 0), y2);
859                     quad[2] = FloatPoint(x2 - max(adjbw2, 0), y2);
860                     quad[3] = FloatPoint(x2 - max(-adjbw2, 0), y1);
861                     break;
862                 case BSBottom:
863                     quad[0] = FloatPoint(x1 + max(adjbw1, 0), y1);
864                     quad[1] = FloatPoint(x1 + max(-adjbw1, 0), y2);
865                     quad[2] = FloatPoint(x2 - max(-adjbw2, 0), y2);
866                     quad[3] = FloatPoint(x2 - max(adjbw2, 0), y1);
867                     break;
868                 case BSLeft:
869                     quad[0] = FloatPoint(x1, y1 + max(-adjbw1, 0));
870                     quad[1] = FloatPoint(x1, y2 - max(-adjbw2, 0));
871                     quad[2] = FloatPoint(x2, y2 - max(adjbw2, 0));
872                     quad[3] = FloatPoint(x2, y1 + max(adjbw1, 0));
873                     break;
874                 case BSRight:
875                     quad[0] = FloatPoint(x1, y1 + max(adjbw1, 0));
876                     quad[1] = FloatPoint(x1, y2 - max(adjbw2, 0));
877                     quad[2] = FloatPoint(x2, y2 - max(-adjbw2, 0));
878                     quad[3] = FloatPoint(x2, y1 + max(-adjbw1, 0));
879                     break;
880             }
881             graphicsContext->drawConvexPolygon(4, quad);
882             break;
883         }
884     }
885 }
886 
drawArcForBoxSide(GraphicsContext * graphicsContext,int x,int y,float thickness,IntSize radius,int angleStart,int angleSpan,BoxSide s,Color c,const Color & textColor,EBorderStyle style,bool firstCorner)887 void RenderObject::drawArcForBoxSide(GraphicsContext* graphicsContext, int x, int y, float thickness, IntSize radius,
888                                      int angleStart, int angleSpan, BoxSide s, Color c, const Color& textColor,
889                                      EBorderStyle style, bool firstCorner)
890 {
891     if ((style == DOUBLE && thickness / 2 < 3) || ((style == RIDGE || style == GROOVE) && thickness / 2 < 2))
892         style = SOLID;
893 
894     if (!c.isValid()) {
895         if (style == INSET || style == OUTSET || style == RIDGE || style == GROOVE)
896             c.setRGB(238, 238, 238);
897         else
898             c = textColor;
899     }
900 
901     switch (style) {
902         case BNONE:
903         case BHIDDEN:
904             return;
905         case DOTTED:
906         case DASHED:
907             graphicsContext->setStrokeColor(c);
908             graphicsContext->setStrokeStyle(style == DOTTED ? DottedStroke : DashedStroke);
909             graphicsContext->setStrokeThickness(thickness);
910             graphicsContext->strokeArc(IntRect(x, y, radius.width() * 2, radius.height() * 2), angleStart, angleSpan);
911             break;
912         case DOUBLE: {
913             float third = thickness / 3.0f;
914             float innerThird = (thickness + 1.0f) / 6.0f;
915             int shiftForInner = static_cast<int>(innerThird * 2.5f);
916 
917             int outerY = y;
918             int outerHeight = radius.height() * 2;
919             int innerX = x + shiftForInner;
920             int innerY = y + shiftForInner;
921             int innerWidth = (radius.width() - shiftForInner) * 2;
922             int innerHeight = (radius.height() - shiftForInner) * 2;
923             if (innerThird > 1 && (s == BSTop || (firstCorner && (s == BSLeft || s == BSRight)))) {
924                 outerHeight += 2;
925                 innerHeight += 2;
926             }
927 
928             graphicsContext->setStrokeStyle(SolidStroke);
929             graphicsContext->setStrokeColor(c);
930             graphicsContext->setStrokeThickness(third);
931             graphicsContext->strokeArc(IntRect(x, outerY, radius.width() * 2, outerHeight), angleStart, angleSpan);
932             graphicsContext->setStrokeThickness(innerThird > 2 ? innerThird - 1 : innerThird);
933             graphicsContext->strokeArc(IntRect(innerX, innerY, innerWidth, innerHeight), angleStart, angleSpan);
934             break;
935         }
936         case GROOVE:
937         case RIDGE: {
938             Color c2;
939             if ((style == RIDGE && (s == BSTop || s == BSLeft)) ||
940                     (style == GROOVE && (s == BSBottom || s == BSRight)))
941                 c2 = c.dark();
942             else {
943                 c2 = c;
944                 c = c.dark();
945             }
946 
947             graphicsContext->setStrokeStyle(SolidStroke);
948             graphicsContext->setStrokeColor(c);
949             graphicsContext->setStrokeThickness(thickness);
950             graphicsContext->strokeArc(IntRect(x, y, radius.width() * 2, radius.height() * 2), angleStart, angleSpan);
951 
952             float halfThickness = (thickness + 1.0f) / 4.0f;
953             int shiftForInner = static_cast<int>(halfThickness * 1.5f);
954             graphicsContext->setStrokeColor(c2);
955             graphicsContext->setStrokeThickness(halfThickness > 2 ? halfThickness - 1 : halfThickness);
956             graphicsContext->strokeArc(IntRect(x + shiftForInner, y + shiftForInner, (radius.width() - shiftForInner) * 2,
957                                        (radius.height() - shiftForInner) * 2), angleStart, angleSpan);
958             break;
959         }
960         case INSET:
961             if (s == BSTop || s == BSLeft)
962                 c = c.dark();
963         case OUTSET:
964             if (style == OUTSET && (s == BSBottom || s == BSRight))
965                 c = c.dark();
966         case SOLID:
967             graphicsContext->setStrokeStyle(SolidStroke);
968             graphicsContext->setStrokeColor(c);
969             graphicsContext->setStrokeThickness(thickness);
970             graphicsContext->strokeArc(IntRect(x, y, radius.width() * 2, radius.height() * 2), angleStart, angleSpan);
971             break;
972     }
973 }
974 
addPDFURLRect(GraphicsContext * context,const IntRect & rect)975 void RenderObject::addPDFURLRect(GraphicsContext* context, const IntRect& rect)
976 {
977     if (rect.isEmpty())
978         return;
979     Node* n = node();
980     if (!n || !n->isLink() || !n->isElementNode())
981         return;
982     const AtomicString& href = static_cast<Element*>(n)->getAttribute(hrefAttr);
983     if (href.isNull())
984         return;
985     context->setURLForRect(n->document()->completeURL(href), rect);
986 }
987 
paintOutline(GraphicsContext * graphicsContext,int tx,int ty,int w,int h,const RenderStyle * style)988 void RenderObject::paintOutline(GraphicsContext* graphicsContext, int tx, int ty, int w, int h, const RenderStyle* style)
989 {
990     if (!hasOutline())
991         return;
992 
993     int ow = style->outlineWidth();
994     EBorderStyle os = style->outlineStyle();
995 
996     Color oc = style->outlineColor();
997     if (!oc.isValid())
998         oc = style->color();
999 
1000     int offset = style->outlineOffset();
1001 
1002     if (style->outlineStyleIsAuto() || hasOutlineAnnotation()) {
1003         if (!theme()->supportsFocusRing(style)) {
1004             // Only paint the focus ring by hand if the theme isn't able to draw the focus ring.
1005             graphicsContext->initFocusRing(ow, offset);
1006             addFocusRingRects(graphicsContext, tx, ty);
1007             if (style->outlineStyleIsAuto())
1008                 graphicsContext->drawFocusRing(oc);
1009             else
1010                 addPDFURLRect(graphicsContext, graphicsContext->focusRingBoundingRect());
1011             graphicsContext->clearFocusRing();
1012         }
1013     }
1014 
1015     if (style->outlineStyleIsAuto() || style->outlineStyle() == BNONE)
1016         return;
1017 
1018     tx -= offset;
1019     ty -= offset;
1020     w += 2 * offset;
1021     h += 2 * offset;
1022 
1023     if (h < 0 || w < 0)
1024         return;
1025 
1026     drawLineForBoxSide(graphicsContext, tx - ow, ty - ow, tx, ty + h + ow,
1027                BSLeft, Color(oc), style->color(), os, ow, ow);
1028 
1029     drawLineForBoxSide(graphicsContext, tx - ow, ty - ow, tx + w + ow, ty,
1030                BSTop, Color(oc), style->color(), os, ow, ow);
1031 
1032     drawLineForBoxSide(graphicsContext, tx + w, ty - ow, tx + w + ow, ty + h + ow,
1033                BSRight, Color(oc), style->color(), os, ow, ow);
1034 
1035     drawLineForBoxSide(graphicsContext, tx - ow, ty + h, tx + w + ow, ty + h + ow,
1036                BSBottom, Color(oc), style->color(), os, ow, ow);
1037 }
1038 
absoluteBoundingBoxRect(bool useTransforms)1039 IntRect RenderObject::absoluteBoundingBoxRect(bool useTransforms)
1040 {
1041     if (useTransforms) {
1042         Vector<FloatQuad> quads;
1043         absoluteQuads(quads);
1044 
1045         size_t n = quads.size();
1046         if (!n)
1047             return IntRect();
1048 
1049         IntRect result = quads[0].enclosingBoundingBox();
1050         for (size_t i = 1; i < n; ++i)
1051             result.unite(quads[i].enclosingBoundingBox());
1052         return result;
1053     }
1054 
1055     FloatPoint absPos = localToAbsolute();
1056     Vector<IntRect> rects;
1057     absoluteRects(rects, absPos.x(), absPos.y());
1058 
1059     size_t n = rects.size();
1060     if (!n)
1061         return IntRect();
1062 
1063     IntRect result = rects[0];
1064     for (size_t i = 1; i < n; ++i)
1065         result.unite(rects[i]);
1066     return result;
1067 }
1068 
addAbsoluteRectForLayer(IntRect & result)1069 void RenderObject::addAbsoluteRectForLayer(IntRect& result)
1070 {
1071     if (hasLayer())
1072         result.unite(absoluteBoundingBoxRect());
1073     for (RenderObject* current = firstChild(); current; current = current->nextSibling())
1074         current->addAbsoluteRectForLayer(result);
1075 }
1076 
paintingRootRect(IntRect & topLevelRect)1077 IntRect RenderObject::paintingRootRect(IntRect& topLevelRect)
1078 {
1079     IntRect result = absoluteBoundingBoxRect();
1080     topLevelRect = result;
1081     for (RenderObject* current = firstChild(); current; current = current->nextSibling())
1082         current->addAbsoluteRectForLayer(result);
1083     return result;
1084 }
1085 
paint(PaintInfo &,int,int)1086 void RenderObject::paint(PaintInfo& /*paintInfo*/, int /*tx*/, int /*ty*/)
1087 {
1088 }
1089 
containerForRepaint() const1090 RenderBoxModelObject* RenderObject::containerForRepaint() const
1091 {
1092 #if USE(ACCELERATED_COMPOSITING)
1093     if (RenderView* v = view()) {
1094         if (v->usesCompositing()) {
1095             RenderLayer* compLayer = enclosingLayer()->enclosingCompositingLayer();
1096             return compLayer ? compLayer->renderer() : 0;
1097         }
1098     }
1099 #endif
1100     // Do root-relative repaint.
1101     return 0;
1102 }
1103 
repaintUsingContainer(RenderBoxModelObject * repaintContainer,const IntRect & r,bool immediate)1104 void RenderObject::repaintUsingContainer(RenderBoxModelObject* repaintContainer, const IntRect& r, bool immediate)
1105 {
1106     if (!repaintContainer || repaintContainer->isRenderView()) {
1107         RenderView* v = repaintContainer ? toRenderView(repaintContainer) : view();
1108         v->repaintViewRectangle(r, immediate);
1109     } else {
1110 #if USE(ACCELERATED_COMPOSITING)
1111         RenderView* v = view();
1112         if (v->usesCompositing()) {
1113             ASSERT(repaintContainer->hasLayer() && repaintContainer->layer()->isComposited());
1114             repaintContainer->layer()->setBackingNeedsRepaintInRect(r);
1115         }
1116 #else
1117         ASSERT_NOT_REACHED();
1118 #endif
1119     }
1120 }
1121 
repaint(bool immediate)1122 void RenderObject::repaint(bool immediate)
1123 {
1124     // Don't repaint if we're unrooted (note that view() still returns the view when unrooted)
1125     RenderView* view;
1126     if (!isRooted(&view))
1127         return;
1128 
1129     if (view->printing())
1130         return; // Don't repaint if we're printing.
1131 
1132     RenderBoxModelObject* repaintContainer = containerForRepaint();
1133     repaintUsingContainer(repaintContainer ? repaintContainer : view, clippedOverflowRectForRepaint(repaintContainer), immediate);
1134 }
1135 
repaintRectangle(const IntRect & r,bool immediate)1136 void RenderObject::repaintRectangle(const IntRect& r, bool immediate)
1137 {
1138     // Don't repaint if we're unrooted (note that view() still returns the view when unrooted)
1139     RenderView* view;
1140     if (!isRooted(&view))
1141         return;
1142 
1143     if (view->printing())
1144         return; // Don't repaint if we're printing.
1145 
1146     IntRect dirtyRect(r);
1147 
1148     // FIXME: layoutDelta needs to be applied in parts before/after transforms and
1149     // repaint containers. https://bugs.webkit.org/show_bug.cgi?id=23308
1150     dirtyRect.move(view->layoutDelta());
1151 
1152     RenderBoxModelObject* repaintContainer = containerForRepaint();
1153     computeRectForRepaint(repaintContainer, dirtyRect);
1154     repaintUsingContainer(repaintContainer ? repaintContainer : view, dirtyRect, immediate);
1155 }
1156 
repaintAfterLayoutIfNeeded(RenderBoxModelObject * repaintContainer,const IntRect & oldBounds,const IntRect & oldOutlineBox)1157 bool RenderObject::repaintAfterLayoutIfNeeded(RenderBoxModelObject* repaintContainer, const IntRect& oldBounds, const IntRect& oldOutlineBox)
1158 {
1159     RenderView* v = view();
1160     if (v->printing())
1161         return false; // Don't repaint if we're printing.
1162 
1163     IntRect newBounds = clippedOverflowRectForRepaint(repaintContainer);
1164     IntRect newOutlineBox;
1165 
1166     bool fullRepaint = selfNeedsLayout();
1167     // Presumably a background or a border exists if border-fit:lines was specified.
1168     if (!fullRepaint && style()->borderFit() == BorderFitLines)
1169         fullRepaint = true;
1170     if (!fullRepaint) {
1171         newOutlineBox = outlineBoundsForRepaint(repaintContainer);
1172         if (newOutlineBox.location() != oldOutlineBox.location() || (mustRepaintBackgroundOrBorder() && (newBounds != oldBounds || newOutlineBox != oldOutlineBox)))
1173             fullRepaint = true;
1174     }
1175 
1176     if (!repaintContainer)
1177         repaintContainer = v;
1178 
1179     if (fullRepaint) {
1180         repaintUsingContainer(repaintContainer, oldBounds);
1181         if (newBounds != oldBounds)
1182             repaintUsingContainer(repaintContainer, newBounds);
1183         return true;
1184     }
1185 
1186     if (newBounds == oldBounds && newOutlineBox == oldOutlineBox)
1187         return false;
1188 
1189     int deltaLeft = newBounds.x() - oldBounds.x();
1190     if (deltaLeft > 0)
1191         repaintUsingContainer(repaintContainer, IntRect(oldBounds.x(), oldBounds.y(), deltaLeft, oldBounds.height()));
1192     else if (deltaLeft < 0)
1193         repaintUsingContainer(repaintContainer, IntRect(newBounds.x(), newBounds.y(), -deltaLeft, newBounds.height()));
1194 
1195     int deltaRight = newBounds.right() - oldBounds.right();
1196     if (deltaRight > 0)
1197         repaintUsingContainer(repaintContainer, IntRect(oldBounds.right(), newBounds.y(), deltaRight, newBounds.height()));
1198     else if (deltaRight < 0)
1199         repaintUsingContainer(repaintContainer, IntRect(newBounds.right(), oldBounds.y(), -deltaRight, oldBounds.height()));
1200 
1201     int deltaTop = newBounds.y() - oldBounds.y();
1202     if (deltaTop > 0)
1203         repaintUsingContainer(repaintContainer, IntRect(oldBounds.x(), oldBounds.y(), oldBounds.width(), deltaTop));
1204     else if (deltaTop < 0)
1205         repaintUsingContainer(repaintContainer, IntRect(newBounds.x(), newBounds.y(), newBounds.width(), -deltaTop));
1206 
1207     int deltaBottom = newBounds.bottom() - oldBounds.bottom();
1208     if (deltaBottom > 0)
1209         repaintUsingContainer(repaintContainer, IntRect(newBounds.x(), oldBounds.bottom(), newBounds.width(), deltaBottom));
1210     else if (deltaBottom < 0)
1211         repaintUsingContainer(repaintContainer, IntRect(oldBounds.x(), newBounds.bottom(), oldBounds.width(), -deltaBottom));
1212 
1213     if (newOutlineBox == oldOutlineBox)
1214         return false;
1215 
1216     // We didn't move, but we did change size.  Invalidate the delta, which will consist of possibly
1217     // two rectangles (but typically only one).
1218     RenderStyle* outlineStyle = outlineStyleForRepaint();
1219     int ow = outlineStyle->outlineSize();
1220     int width = abs(newOutlineBox.width() - oldOutlineBox.width());
1221     if (width) {
1222         int shadowLeft;
1223         int shadowRight;
1224         style()->getBoxShadowHorizontalExtent(shadowLeft, shadowRight);
1225 
1226         int borderRight = isBox() ? toRenderBox(this)->borderRight() : 0;
1227         int borderWidth = max(-outlineStyle->outlineOffset(), max(borderRight, max(style()->borderTopRightRadius().width(), style()->borderBottomRightRadius().width()))) + max(ow, shadowRight);
1228         IntRect rightRect(newOutlineBox.x() + min(newOutlineBox.width(), oldOutlineBox.width()) - borderWidth,
1229             newOutlineBox.y(),
1230             width + borderWidth,
1231             max(newOutlineBox.height(), oldOutlineBox.height()));
1232         int right = min(newBounds.right(), oldBounds.right());
1233         if (rightRect.x() < right) {
1234             rightRect.setWidth(min(rightRect.width(), right - rightRect.x()));
1235             repaintUsingContainer(repaintContainer, rightRect);
1236         }
1237     }
1238     int height = abs(newOutlineBox.height() - oldOutlineBox.height());
1239     if (height) {
1240         int shadowTop;
1241         int shadowBottom;
1242         style()->getBoxShadowVerticalExtent(shadowTop, shadowBottom);
1243 
1244         int borderBottom = isBox() ? toRenderBox(this)->borderBottom() : 0;
1245         int borderHeight = max(-outlineStyle->outlineOffset(), max(borderBottom, max(style()->borderBottomLeftRadius().height(), style()->borderBottomRightRadius().height()))) + max(ow, shadowBottom);
1246         IntRect bottomRect(newOutlineBox.x(),
1247             min(newOutlineBox.bottom(), oldOutlineBox.bottom()) - borderHeight,
1248             max(newOutlineBox.width(), oldOutlineBox.width()),
1249             height + borderHeight);
1250         int bottom = min(newBounds.bottom(), oldBounds.bottom());
1251         if (bottomRect.y() < bottom) {
1252             bottomRect.setHeight(min(bottomRect.height(), bottom - bottomRect.y()));
1253             repaintUsingContainer(repaintContainer, bottomRect);
1254         }
1255     }
1256     return false;
1257 }
1258 
repaintDuringLayoutIfMoved(const IntRect &)1259 void RenderObject::repaintDuringLayoutIfMoved(const IntRect&)
1260 {
1261 }
1262 
repaintOverhangingFloats(bool)1263 void RenderObject::repaintOverhangingFloats(bool)
1264 {
1265 }
1266 
checkForRepaintDuringLayout() const1267 bool RenderObject::checkForRepaintDuringLayout() const
1268 {
1269     // FIXME: <https://bugs.webkit.org/show_bug.cgi?id=20885> It is probably safe to also require
1270     // m_everHadLayout. Currently, only RenderBlock::layoutBlock() adds this condition. See also
1271     // <https://bugs.webkit.org/show_bug.cgi?id=15129>.
1272     return !document()->view()->needsFullRepaint() && !hasLayer();
1273 }
1274 
rectWithOutlineForRepaint(RenderBoxModelObject * repaintContainer,int outlineWidth)1275 IntRect RenderObject::rectWithOutlineForRepaint(RenderBoxModelObject* repaintContainer, int outlineWidth)
1276 {
1277     IntRect r(clippedOverflowRectForRepaint(repaintContainer));
1278     r.inflate(outlineWidth);
1279     return r;
1280 }
1281 
clippedOverflowRectForRepaint(RenderBoxModelObject *)1282 IntRect RenderObject::clippedOverflowRectForRepaint(RenderBoxModelObject*)
1283 {
1284     ASSERT_NOT_REACHED();
1285     return IntRect();
1286 }
1287 
computeRectForRepaint(RenderBoxModelObject * repaintContainer,IntRect & rect,bool fixed)1288 void RenderObject::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& rect, bool fixed)
1289 {
1290     if (repaintContainer == this)
1291         return;
1292 
1293     if (RenderObject* o = parent()) {
1294         if (o->isBlockFlow()) {
1295             RenderBlock* cb = toRenderBlock(o);
1296             if (cb->hasColumns())
1297                 cb->adjustRectForColumns(rect);
1298         }
1299 
1300         if (o->hasOverflowClip()) {
1301             // o->height() is inaccurate if we're in the middle of a layout of |o|, so use the
1302             // layer's size instead.  Even if the layer's size is wrong, the layer itself will repaint
1303             // anyway if its size does change.
1304             RenderBox* boxParent = toRenderBox(o);
1305 
1306             IntRect boxRect(0, 0, boxParent->layer()->width(), boxParent->layer()->height());
1307             int x = rect.x();
1308             int y = rect.y();
1309             boxParent->layer()->subtractScrolledContentOffset(x, y); // For overflow:auto/scroll/hidden.
1310             IntRect repaintRect(x, y, rect.width(), rect.height());
1311             rect = intersection(repaintRect, boxRect);
1312             if (rect.isEmpty())
1313                 return;
1314         }
1315 
1316         o->computeRectForRepaint(repaintContainer, rect, fixed);
1317     }
1318 }
1319 
dirtyLinesFromChangedChild(RenderObject *)1320 void RenderObject::dirtyLinesFromChangedChild(RenderObject*)
1321 {
1322 }
1323 
1324 #ifndef NDEBUG
1325 
showTreeForThis() const1326 void RenderObject::showTreeForThis() const
1327 {
1328     if (node())
1329         node()->showTreeForThis();
1330 }
1331 
1332 #endif // NDEBUG
1333 
selectionBackgroundColor() const1334 Color RenderObject::selectionBackgroundColor() const
1335 {
1336     Color color;
1337     if (style()->userSelect() != SELECT_NONE) {
1338          RefPtr<RenderStyle> pseudoStyle = getUncachedPseudoStyle(SELECTION);
1339         if (pseudoStyle && pseudoStyle->backgroundColor().isValid())
1340             color = pseudoStyle->backgroundColor().blendWithWhite();
1341         else
1342             color = document()->frame()->selection()->isFocusedAndActive() ?
1343                     theme()->activeSelectionBackgroundColor() :
1344                     theme()->inactiveSelectionBackgroundColor();
1345     }
1346 
1347     return color;
1348 }
1349 
selectionForegroundColor() const1350 Color RenderObject::selectionForegroundColor() const
1351 {
1352     Color color;
1353     if (style()->userSelect() == SELECT_NONE)
1354         return color;
1355 
1356     if (RefPtr<RenderStyle> pseudoStyle = getUncachedPseudoStyle(SELECTION)) {
1357         color = pseudoStyle->textFillColor();
1358         if (!color.isValid())
1359             color = pseudoStyle->color();
1360     } else
1361         color = document()->frame()->selection()->isFocusedAndActive() ?
1362                 theme()->activeSelectionForegroundColor() :
1363                 theme()->inactiveSelectionForegroundColor();
1364 
1365     return color;
1366 }
1367 
draggableNode(bool dhtmlOK,bool uaOK,int x,int y,bool & dhtmlWillDrag) const1368 Node* RenderObject::draggableNode(bool dhtmlOK, bool uaOK, int x, int y, bool& dhtmlWillDrag) const
1369 {
1370     if (!dhtmlOK && !uaOK)
1371         return 0;
1372 
1373     for (const RenderObject* curr = this; curr; curr = curr->parent()) {
1374         Node* elt = curr->node();
1375         if (elt && elt->nodeType() == Node::TEXT_NODE) {
1376             // Since there's no way for the author to address the -webkit-user-drag style for a text node,
1377             // we use our own judgement.
1378             if (uaOK && view()->frameView()->frame()->eventHandler()->shouldDragAutoNode(curr->node(), IntPoint(x, y))) {
1379                 dhtmlWillDrag = false;
1380                 return curr->node();
1381             }
1382             if (elt->canStartSelection())
1383                 // In this case we have a click in the unselected portion of text.  If this text is
1384                 // selectable, we want to start the selection process instead of looking for a parent
1385                 // to try to drag.
1386                 return 0;
1387         } else {
1388             EUserDrag dragMode = curr->style()->userDrag();
1389             if (dhtmlOK && dragMode == DRAG_ELEMENT) {
1390                 dhtmlWillDrag = true;
1391                 return curr->node();
1392             }
1393             if (uaOK && dragMode == DRAG_AUTO
1394                     && view()->frameView()->frame()->eventHandler()->shouldDragAutoNode(curr->node(), IntPoint(x, y))) {
1395                 dhtmlWillDrag = false;
1396                 return curr->node();
1397             }
1398         }
1399     }
1400     return 0;
1401 }
1402 
selectionStartEnd(int & spos,int & epos) const1403 void RenderObject::selectionStartEnd(int& spos, int& epos) const
1404 {
1405     view()->selectionStartEnd(spos, epos);
1406 }
1407 
handleDynamicFloatPositionChange()1408 void RenderObject::handleDynamicFloatPositionChange()
1409 {
1410     // We have gone from not affecting the inline status of the parent flow to suddenly
1411     // having an impact.  See if there is a mismatch between the parent flow's
1412     // childrenInline() state and our state.
1413     setInline(style()->isDisplayInlineType());
1414     if (isInline() != parent()->childrenInline()) {
1415         if (!isInline())
1416             toRenderBoxModelObject(parent())->childBecameNonInline(this);
1417         else {
1418             // An anonymous block must be made to wrap this inline.
1419             RenderBlock* block = toRenderBlock(parent())->createAnonymousBlock();
1420             RenderObjectChildList* childlist = parent()->virtualChildren();
1421             childlist->insertChildNode(parent(), block, this);
1422             block->children()->appendChildNode(block, childlist->removeChildNode(parent(), this));
1423         }
1424     }
1425 }
1426 
setAnimatableStyle(PassRefPtr<RenderStyle> style)1427 void RenderObject::setAnimatableStyle(PassRefPtr<RenderStyle> style)
1428 {
1429     if (!isText() && style)
1430         setStyle(animation()->updateAnimations(this, style.get()));
1431     else
1432         setStyle(style);
1433 }
1434 
adjustStyleDifference(StyleDifference diff,unsigned contextSensitiveProperties) const1435 StyleDifference RenderObject::adjustStyleDifference(StyleDifference diff, unsigned contextSensitiveProperties) const
1436 {
1437 #if USE(ACCELERATED_COMPOSITING)
1438     // If transform changed, and we are not composited, need to do a layout.
1439     if (contextSensitiveProperties & ContextSensitivePropertyTransform) {
1440         // Text nodes share style with their parents but transforms don't apply to them,
1441         // hence the !isText() check.
1442         // FIXME: when transforms are taken into account for overflow, we will need to do a layout.
1443         if (!isText() && (!hasLayer() || !toRenderBoxModelObject(this)->layer()->isComposited()))
1444             diff = StyleDifferenceLayout;
1445         else if (diff < StyleDifferenceRecompositeLayer)
1446             diff = StyleDifferenceRecompositeLayer;
1447     }
1448 
1449     // If opacity changed, and we are not composited, need to repaint (also
1450     // ignoring text nodes)
1451     if (contextSensitiveProperties & ContextSensitivePropertyOpacity) {
1452         if (!isText() && (!hasLayer() || !toRenderBoxModelObject(this)->layer()->isComposited()))
1453             diff = StyleDifferenceRepaintLayer;
1454         else if (diff < StyleDifferenceRecompositeLayer)
1455             diff = StyleDifferenceRecompositeLayer;
1456     }
1457 #else
1458     UNUSED_PARAM(contextSensitiveProperties);
1459 #endif
1460 
1461     // If we have no layer(), just treat a RepaintLayer hint as a normal Repaint.
1462     if (diff == StyleDifferenceRepaintLayer && !hasLayer())
1463         diff = StyleDifferenceRepaint;
1464 
1465     return diff;
1466 }
1467 
setStyle(PassRefPtr<RenderStyle> style)1468 void RenderObject::setStyle(PassRefPtr<RenderStyle> style)
1469 {
1470     if (m_style == style)
1471         return;
1472 
1473     StyleDifference diff = StyleDifferenceEqual;
1474     unsigned contextSensitiveProperties = ContextSensitivePropertyNone;
1475     if (m_style)
1476         diff = m_style->diff(style.get(), contextSensitiveProperties);
1477 
1478     diff = adjustStyleDifference(diff, contextSensitiveProperties);
1479 
1480     styleWillChange(diff, style.get());
1481 
1482     RefPtr<RenderStyle> oldStyle = m_style.release();
1483     m_style = style;
1484 
1485     updateFillImages(oldStyle ? oldStyle->backgroundLayers() : 0, m_style ? m_style->backgroundLayers() : 0);
1486     updateFillImages(oldStyle ? oldStyle->maskLayers() : 0, m_style ? m_style->maskLayers() : 0);
1487 
1488     updateImage(oldStyle ? oldStyle->borderImage().image() : 0, m_style ? m_style->borderImage().image() : 0);
1489     updateImage(oldStyle ? oldStyle->maskBoxImage().image() : 0, m_style ? m_style->maskBoxImage().image() : 0);
1490 
1491     // We need to ensure that view->maximalOutlineSize() is valid for any repaints that happen
1492     // during styleDidChange (it's used by clippedOverflowRectForRepaint()).
1493     if (m_style->outlineWidth() > 0 && m_style->outlineSize() > maximalOutlineSize(PaintPhaseOutline))
1494         toRenderView(document()->renderer())->setMaximalOutlineSize(m_style->outlineSize());
1495 
1496     styleDidChange(diff, oldStyle.get());
1497 
1498     if (!m_parent || isText())
1499         return;
1500 
1501     // Now that the layer (if any) has been updated, we need to adjust the diff again,
1502     // check whether we should layout now, and decide if we need to repaint.
1503     StyleDifference updatedDiff = adjustStyleDifference(diff, contextSensitiveProperties);
1504 
1505     if (diff <= StyleDifferenceLayoutPositionedMovementOnly) {
1506         if (updatedDiff == StyleDifferenceLayout)
1507             setNeedsLayoutAndPrefWidthsRecalc();
1508         else if (updatedDiff == StyleDifferenceLayoutPositionedMovementOnly)
1509             setNeedsPositionedMovementLayout();
1510     }
1511 
1512     if (updatedDiff == StyleDifferenceRepaintLayer || updatedDiff == StyleDifferenceRepaint) {
1513         // Do a repaint with the new style now, e.g., for example if we go from
1514         // not having an outline to having an outline.
1515         repaint();
1516     }
1517 }
1518 
setStyleInternal(PassRefPtr<RenderStyle> style)1519 void RenderObject::setStyleInternal(PassRefPtr<RenderStyle> style)
1520 {
1521     m_style = style;
1522 }
1523 
styleWillChange(StyleDifference diff,const RenderStyle * newStyle)1524 void RenderObject::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
1525 {
1526     if (m_style) {
1527         // If our z-index changes value or our visibility changes,
1528         // we need to dirty our stacking context's z-order list.
1529         if (newStyle) {
1530             bool visibilityChanged = m_style->visibility() != newStyle->visibility()
1531                 || m_style->zIndex() != newStyle->zIndex()
1532                 || m_style->hasAutoZIndex() != newStyle->hasAutoZIndex();
1533 #if ENABLE(DASHBOARD_SUPPORT)
1534             if (visibilityChanged)
1535                 document()->setDashboardRegionsDirty(true);
1536 #endif
1537             if (visibilityChanged && AXObjectCache::accessibilityEnabled())
1538                 document()->axObjectCache()->childrenChanged(this);
1539 
1540             // Keep layer hierarchy visibility bits up to date if visibility changes.
1541             if (m_style->visibility() != newStyle->visibility()) {
1542                 if (RenderLayer* l = enclosingLayer()) {
1543                     if (newStyle->visibility() == VISIBLE)
1544                         l->setHasVisibleContent(true);
1545                     else if (l->hasVisibleContent() && (this == l->renderer() || l->renderer()->style()->visibility() != VISIBLE)) {
1546                         l->dirtyVisibleContentStatus();
1547                         if (diff > StyleDifferenceRepaintLayer)
1548                             repaint();
1549                     }
1550                 }
1551             }
1552         }
1553 
1554         if (m_parent && (diff == StyleDifferenceRepaint || newStyle->outlineSize() < m_style->outlineSize()))
1555             repaint();
1556         if (isFloating() && (m_style->floating() != newStyle->floating()))
1557             // For changes in float styles, we need to conceivably remove ourselves
1558             // from the floating objects list.
1559             toRenderBox(this)->removeFloatingOrPositionedChildFromBlockLists();
1560         else if (isPositioned() && (newStyle->position() != AbsolutePosition && newStyle->position() != FixedPosition))
1561             // For changes in positioning styles, we need to conceivably remove ourselves
1562             // from the positioned objects list.
1563             toRenderBox(this)->removeFloatingOrPositionedChildFromBlockLists();
1564 
1565         s_affectsParentBlock = isFloatingOrPositioned() &&
1566             (!newStyle->isFloating() && newStyle->position() != AbsolutePosition && newStyle->position() != FixedPosition)
1567             && parent() && (parent()->isBlockFlow() || parent()->isRenderInline());
1568 
1569         // reset style flags
1570         if (diff == StyleDifferenceLayout || diff == StyleDifferenceLayoutPositionedMovementOnly) {
1571             m_floating = false;
1572             m_positioned = false;
1573             m_relPositioned = false;
1574         }
1575         m_paintBackground = false;
1576         m_hasOverflowClip = false;
1577         m_hasTransform = false;
1578         m_hasReflection = false;
1579     } else
1580         s_affectsParentBlock = false;
1581 
1582     if (view()->frameView()) {
1583         // FIXME: A better solution would be to only invalidate the fixed regions when scrolling.  It's overkill to
1584         // prevent the entire view from blitting on a scroll.
1585         bool newStyleSlowScroll = newStyle && (newStyle->position() == FixedPosition || newStyle->hasFixedBackgroundImage());
1586         bool oldStyleSlowScroll = m_style && (m_style->position() == FixedPosition || m_style->hasFixedBackgroundImage());
1587         if (oldStyleSlowScroll != newStyleSlowScroll) {
1588             if (oldStyleSlowScroll)
1589                 view()->frameView()->removeSlowRepaintObject();
1590             if (newStyleSlowScroll)
1591                 view()->frameView()->addSlowRepaintObject();
1592         }
1593     }
1594 }
1595 
styleDidChange(StyleDifference diff,const RenderStyle *)1596 void RenderObject::styleDidChange(StyleDifference diff, const RenderStyle*)
1597 {
1598     if (s_affectsParentBlock)
1599         handleDynamicFloatPositionChange();
1600 
1601     if (!m_parent)
1602         return;
1603 
1604     if (diff == StyleDifferenceLayout)
1605         setNeedsLayoutAndPrefWidthsRecalc();
1606     else if (diff == StyleDifferenceLayoutPositionedMovementOnly)
1607         setNeedsPositionedMovementLayout();
1608 
1609     // Don't check for repaint here; we need to wait until the layer has been
1610     // updated by subclasses before we know if we have to repaint (in setStyle()).
1611 }
1612 
updateFillImages(const FillLayer * oldLayers,const FillLayer * newLayers)1613 void RenderObject::updateFillImages(const FillLayer* oldLayers, const FillLayer* newLayers)
1614 {
1615     // FIXME: This will be slow when a large number of images is used.  Fix by using a dict.
1616     for (const FillLayer* currOld = oldLayers; currOld; currOld = currOld->next()) {
1617         if (currOld->image() && (!newLayers || !newLayers->containsImage(currOld->image())))
1618             currOld->image()->removeClient(this);
1619     }
1620     for (const FillLayer* currNew = newLayers; currNew; currNew = currNew->next()) {
1621         if (currNew->image() && (!oldLayers || !oldLayers->containsImage(currNew->image())))
1622             currNew->image()->addClient(this);
1623     }
1624 }
1625 
updateImage(StyleImage * oldImage,StyleImage * newImage)1626 void RenderObject::updateImage(StyleImage* oldImage, StyleImage* newImage)
1627 {
1628     if (oldImage != newImage) {
1629         if (oldImage)
1630             oldImage->removeClient(this);
1631         if (newImage)
1632             newImage->addClient(this);
1633     }
1634 }
1635 
viewRect() const1636 IntRect RenderObject::viewRect() const
1637 {
1638     return view()->viewRect();
1639 }
1640 
localToAbsolute(FloatPoint localPoint,bool fixed,bool useTransforms) const1641 FloatPoint RenderObject::localToAbsolute(FloatPoint localPoint, bool fixed, bool useTransforms) const
1642 {
1643     TransformState transformState(TransformState::ApplyTransformDirection, localPoint);
1644     mapLocalToContainer(0, fixed, useTransforms, transformState);
1645     transformState.flatten();
1646 
1647     return transformState.lastPlanarPoint();
1648 }
1649 
absoluteToLocal(FloatPoint containerPoint,bool fixed,bool useTransforms) const1650 FloatPoint RenderObject::absoluteToLocal(FloatPoint containerPoint, bool fixed, bool useTransforms) const
1651 {
1652     TransformState transformState(TransformState::UnapplyInverseTransformDirection, containerPoint);
1653     mapAbsoluteToLocalPoint(fixed, useTransforms, transformState);
1654     transformState.flatten();
1655 
1656     return transformState.lastPlanarPoint();
1657 }
1658 
mapLocalToContainer(RenderBoxModelObject * repaintContainer,bool fixed,bool useTransforms,TransformState & transformState) const1659 void RenderObject::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState) const
1660 {
1661     if (repaintContainer == this)
1662         return;
1663 
1664     RenderObject* o = parent();
1665     if (!o)
1666         return;
1667 
1668     if (o->hasOverflowClip())
1669         transformState.move(-toRenderBox(o)->layer()->scrolledContentOffset());
1670 
1671     o->mapLocalToContainer(repaintContainer, fixed, useTransforms, transformState);
1672 }
1673 
mapAbsoluteToLocalPoint(bool fixed,bool useTransforms,TransformState & transformState) const1674 void RenderObject::mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState& transformState) const
1675 {
1676     RenderObject* o = parent();
1677     if (o) {
1678         o->mapAbsoluteToLocalPoint(fixed, useTransforms, transformState);
1679         if (o->hasOverflowClip())
1680             transformState.move(toRenderBox(o)->layer()->scrolledContentOffset());
1681     }
1682 }
1683 
shouldUseTransformFromContainer(const RenderObject * containerObject) const1684 bool RenderObject::shouldUseTransformFromContainer(const RenderObject* containerObject) const
1685 {
1686 #if ENABLE(3D_RENDERING)
1687     // hasTransform() indicates whether the object has transform, transform-style or perspective. We just care about transform,
1688     // so check the layer's transform directly.
1689     return (hasLayer() && toRenderBoxModelObject(this)->layer()->transform()) || (containerObject && containerObject->style()->hasPerspective());
1690 #else
1691     UNUSED_PARAM(containerObject);
1692     return hasTransform();
1693 #endif
1694 }
1695 
getTransformFromContainer(const RenderObject * containerObject,const IntSize & offsetInContainer,TransformationMatrix & transform) const1696 void RenderObject::getTransformFromContainer(const RenderObject* containerObject, const IntSize& offsetInContainer, TransformationMatrix& transform) const
1697 {
1698     transform.makeIdentity();
1699     transform.translate(offsetInContainer.width(), offsetInContainer.height());
1700     RenderLayer* layer;
1701     if (hasLayer() && (layer = toRenderBoxModelObject(this)->layer()) && layer->transform())
1702         transform.multLeft(layer->currentTransform());
1703 
1704 #if ENABLE(3D_RENDERING)
1705     if (containerObject && containerObject->hasLayer() && containerObject->style()->hasPerspective()) {
1706         // Perpsective on the container affects us, so we have to factor it in here.
1707         ASSERT(containerObject->hasLayer());
1708         FloatPoint perspectiveOrigin = toRenderBox(containerObject)->layer()->perspectiveOrigin();
1709 
1710         TransformationMatrix perspectiveMatrix;
1711         perspectiveMatrix.applyPerspective(containerObject->style()->perspective());
1712 
1713         transform.translateRight3d(-perspectiveOrigin.x(), -perspectiveOrigin.y(), 0);
1714         transform.multiply(perspectiveMatrix);
1715         transform.translateRight3d(perspectiveOrigin.x(), perspectiveOrigin.y(), 0);
1716     }
1717 #else
1718     UNUSED_PARAM(containerObject);
1719 #endif
1720 }
1721 
localToContainerQuad(const FloatQuad & localQuad,RenderBoxModelObject * repaintContainer,bool fixed) const1722 FloatQuad RenderObject::localToContainerQuad(const FloatQuad& localQuad, RenderBoxModelObject* repaintContainer, bool fixed) const
1723 {
1724     TransformState transformState(TransformState::ApplyTransformDirection, FloatPoint(), &localQuad);
1725     mapLocalToContainer(repaintContainer, fixed, true, transformState);
1726     transformState.flatten();
1727 
1728     return transformState.lastPlanarQuad();
1729 }
1730 
offsetFromContainer(RenderObject * o) const1731 IntSize RenderObject::offsetFromContainer(RenderObject* o) const
1732 {
1733     ASSERT(o == container());
1734 
1735     IntSize offset;
1736     if (o->hasOverflowClip())
1737         offset -= toRenderBox(o)->layer()->scrolledContentOffset();
1738 
1739     return offset;
1740 }
1741 
localCaretRect(InlineBox *,int,int * extraWidthToEndOfLine)1742 IntRect RenderObject::localCaretRect(InlineBox*, int, int* extraWidthToEndOfLine)
1743 {
1744     if (extraWidthToEndOfLine)
1745         *extraWidthToEndOfLine = 0;
1746 
1747     return IntRect();
1748 }
1749 
view() const1750 RenderView* RenderObject::view() const
1751 {
1752     return toRenderView(document()->renderer());
1753 }
1754 
isRooted(RenderView ** view)1755 bool RenderObject::isRooted(RenderView** view)
1756 {
1757     RenderObject* o = this;
1758     while (o->parent())
1759         o = o->parent();
1760 
1761     if (!o->isRenderView())
1762         return false;
1763 
1764     if (view)
1765         *view = toRenderView(o);
1766 
1767     return true;
1768 }
1769 
hasOutlineAnnotation() const1770 bool RenderObject::hasOutlineAnnotation() const
1771 {
1772     return node() && node()->isLink() && document()->printing();
1773 }
1774 
container() const1775 RenderObject* RenderObject::container() const
1776 {
1777     // This method is extremely similar to containingBlock(), but with a few notable
1778     // exceptions.
1779     // (1) It can be used on orphaned subtrees, i.e., it can be called safely even when
1780     // the object is not part of the primary document subtree yet.
1781     // (2) For normal flow elements, it just returns the parent.
1782     // (3) For absolute positioned elements, it will return a relative positioned inline.
1783     // containingBlock() simply skips relpositioned inlines and lets an enclosing block handle
1784     // the layout of the positioned object.  This does mean that calcAbsoluteHorizontal and
1785     // calcAbsoluteVertical have to use container().
1786     RenderObject* o = parent();
1787 
1788     if (isText())
1789         return o;
1790 
1791     EPosition pos = m_style->position();
1792     if (pos == FixedPosition) {
1793         // container() can be called on an object that is not in the
1794         // tree yet.  We don't call view() since it will assert if it
1795         // can't get back to the canvas.  Instead we just walk as high up
1796         // as we can.  If we're in the tree, we'll get the root.  If we
1797         // aren't we'll get the root of our little subtree (most likely
1798         // we'll just return 0).
1799         // FIXME: The definition of view() has changed to not crawl up the render tree.  It might
1800         // be safe now to use it.
1801         while (o && o->parent() && !(o->hasTransform() && o->isRenderBlock()))
1802             o = o->parent();
1803     } else if (pos == AbsolutePosition) {
1804         // Same goes here.  We technically just want our containing block, but
1805         // we may not have one if we're part of an uninstalled subtree.  We'll
1806         // climb as high as we can though.
1807         while (o && o->style()->position() == StaticPosition && !o->isRenderView() && !(o->hasTransform() && o->isRenderBlock()))
1808             o = o->parent();
1809     }
1810 
1811     return o;
1812 }
1813 
isSelectionBorder() const1814 bool RenderObject::isSelectionBorder() const
1815 {
1816     SelectionState st = selectionState();
1817     return st == SelectionStart || st == SelectionEnd || st == SelectionBoth;
1818 }
1819 
destroy()1820 void RenderObject::destroy()
1821 {
1822     // Destroy any leftover anonymous children.
1823     RenderObjectChildList* children = virtualChildren();
1824     if (children)
1825         children->destroyLeftoverChildren();
1826 
1827     // If this renderer is being autoscrolled, stop the autoscroll timer
1828     if (document()->frame()->eventHandler()->autoscrollRenderer() == this)
1829         document()->frame()->eventHandler()->stopAutoscrollTimer(true);
1830 
1831     if (m_hasCounterNodeMap)
1832         RenderCounter::destroyCounterNodes(this);
1833 
1834     if (AXObjectCache::accessibilityEnabled()) {
1835         document()->axObjectCache()->childrenChanged(this->parent());
1836         document()->axObjectCache()->remove(this);
1837     }
1838     animation()->cancelAnimations(this);
1839 
1840     // By default no ref-counting. RenderWidget::destroy() doesn't call
1841     // this function because it needs to do ref-counting. If anything
1842     // in this function changes, be sure to fix RenderWidget::destroy() as well.
1843 
1844     remove();
1845 
1846     // FIXME: Would like to do this in RenderBoxModelObject, but the timing is so complicated that this can't easily
1847     // be moved into RenderBoxModelObject::destroy.
1848     if (hasLayer()) {
1849         setHasLayer(false);
1850         toRenderBoxModelObject(this)->destroyLayer();
1851     }
1852     arenaDelete(renderArena(), this);
1853 }
1854 
arenaDelete(RenderArena * arena,void * base)1855 void RenderObject::arenaDelete(RenderArena* arena, void* base)
1856 {
1857     if (m_style) {
1858         for (const FillLayer* bgLayer = m_style->backgroundLayers(); bgLayer; bgLayer = bgLayer->next()) {
1859             if (StyleImage* backgroundImage = bgLayer->image())
1860                 backgroundImage->removeClient(this);
1861         }
1862 
1863         for (const FillLayer* maskLayer = m_style->maskLayers(); maskLayer; maskLayer = maskLayer->next()) {
1864             if (StyleImage* maskImage = maskLayer->image())
1865                 maskImage->removeClient(this);
1866         }
1867 
1868         if (StyleImage* borderImage = m_style->borderImage().image())
1869             borderImage->removeClient(this);
1870 
1871         if (StyleImage* maskBoxImage = m_style->maskBoxImage().image())
1872             maskBoxImage->removeClient(this);
1873     }
1874 
1875 #ifndef NDEBUG
1876     void* savedBase = baseOfRenderObjectBeingDeleted;
1877     baseOfRenderObjectBeingDeleted = base;
1878 #endif
1879     delete this;
1880 #ifndef NDEBUG
1881     baseOfRenderObjectBeingDeleted = savedBase;
1882 #endif
1883 
1884     // Recover the size left there for us by operator delete and free the memory.
1885     arena->free(*(size_t*)base, base);
1886 }
1887 
positionForCoordinates(int x,int y)1888 VisiblePosition RenderObject::positionForCoordinates(int x, int y)
1889 {
1890     return positionForPoint(IntPoint(x, y));
1891 }
1892 
positionForPoint(const IntPoint &)1893 VisiblePosition RenderObject::positionForPoint(const IntPoint&)
1894 {
1895     return createVisiblePosition(caretMinOffset(), DOWNSTREAM);
1896 }
1897 
updateDragState(bool dragOn)1898 void RenderObject::updateDragState(bool dragOn)
1899 {
1900     bool valueChanged = (dragOn != m_isDragging);
1901     m_isDragging = dragOn;
1902     if (valueChanged && style()->affectedByDragRules())
1903         node()->setNeedsStyleRecalc();
1904     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
1905         curr->updateDragState(dragOn);
1906 }
1907 
hitTest(const HitTestRequest & request,HitTestResult & result,const IntPoint & point,int tx,int ty,HitTestFilter hitTestFilter)1908 bool RenderObject::hitTest(const HitTestRequest& request, HitTestResult& result, const IntPoint& point, int tx, int ty, HitTestFilter hitTestFilter)
1909 {
1910     bool inside = false;
1911     if (hitTestFilter != HitTestSelf) {
1912         // First test the foreground layer (lines and inlines).
1913         inside = nodeAtPoint(request, result, point.x(), point.y(), tx, ty, HitTestForeground);
1914 
1915         // Test floats next.
1916         if (!inside)
1917             inside = nodeAtPoint(request, result, point.x(), point.y(), tx, ty, HitTestFloat);
1918 
1919         // Finally test to see if the mouse is in the background (within a child block's background).
1920         if (!inside)
1921             inside = nodeAtPoint(request, result, point.x(), point.y(), tx, ty, HitTestChildBlockBackgrounds);
1922     }
1923 
1924     // See if the mouse is inside us but not any of our descendants
1925     if (hitTestFilter != HitTestDescendants && !inside)
1926         inside = nodeAtPoint(request, result, point.x(), point.y(), tx, ty, HitTestBlockBackground);
1927 
1928     return inside;
1929 }
1930 
updateHitTestResult(HitTestResult & result,const IntPoint & point)1931 void RenderObject::updateHitTestResult(HitTestResult& result, const IntPoint& point)
1932 {
1933     if (result.innerNode())
1934         return;
1935 
1936     Node* n = node();
1937     if (n) {
1938         result.setInnerNode(n);
1939         if (!result.innerNonSharedNode())
1940             result.setInnerNonSharedNode(n);
1941         result.setLocalPoint(point);
1942     }
1943 }
1944 
nodeAtPoint(const HitTestRequest &,HitTestResult &,int,int,int,int,HitTestAction)1945 bool RenderObject::nodeAtPoint(const HitTestRequest&, HitTestResult&, int /*x*/, int /*y*/, int /*tx*/, int /*ty*/, HitTestAction)
1946 {
1947     return false;
1948 }
1949 
lineHeight(bool firstLine,bool) const1950 int RenderObject::lineHeight(bool firstLine, bool /*isRootLineBox*/) const
1951 {
1952     return style(firstLine)->computedLineHeight();
1953 }
1954 
baselinePosition(bool firstLine,bool isRootLineBox) const1955 int RenderObject::baselinePosition(bool firstLine, bool isRootLineBox) const
1956 {
1957     const Font& f = style(firstLine)->font();
1958     return f.ascent() + (lineHeight(firstLine, isRootLineBox) - f.height()) / 2;
1959 }
1960 
scheduleRelayout()1961 void RenderObject::scheduleRelayout()
1962 {
1963     if (isRenderView()) {
1964         FrameView* view = toRenderView(this)->frameView();
1965         if (view)
1966             view->scheduleRelayout();
1967     } else if (parent()) {
1968         FrameView* v = view() ? view()->frameView() : 0;
1969         if (v)
1970             v->scheduleRelayoutOfSubtree(this);
1971     }
1972 }
1973 
layout()1974 void RenderObject::layout()
1975 {
1976     ASSERT(needsLayout());
1977     RenderObject* child = firstChild();
1978     while (child) {
1979         child->layoutIfNeeded();
1980         ASSERT(!child->needsLayout());
1981         child = child->nextSibling();
1982     }
1983     setNeedsLayout(false);
1984 }
1985 
uncachedFirstLineStyle(RenderStyle * style) const1986 PassRefPtr<RenderStyle> RenderObject::uncachedFirstLineStyle(RenderStyle* style) const
1987 {
1988     if (!document()->usesFirstLineRules())
1989         return 0;
1990 
1991     ASSERT(!isText());
1992 
1993     RefPtr<RenderStyle> result;
1994 
1995     if (isBlockFlow()) {
1996         if (RenderBlock* firstLineBlock = this->firstLineBlock())
1997             result = firstLineBlock->getUncachedPseudoStyle(FIRST_LINE, style, firstLineBlock == this ? style : 0);
1998     } else if (!isAnonymous() && isRenderInline()) {
1999         RenderStyle* parentStyle = parent()->firstLineStyle();
2000         if (parentStyle != parent()->style())
2001             result = getUncachedPseudoStyle(FIRST_LINE_INHERITED, parentStyle, style);
2002     }
2003 
2004     return result.release();
2005 }
2006 
firstLineStyleSlowCase() const2007 RenderStyle* RenderObject::firstLineStyleSlowCase() const
2008 {
2009     ASSERT(document()->usesFirstLineRules());
2010 
2011     RenderStyle* style = m_style.get();
2012     const RenderObject* renderer = isText() ? parent() : this;
2013     if (renderer->isBlockFlow()) {
2014         if (RenderBlock* firstLineBlock = renderer->firstLineBlock())
2015             style = firstLineBlock->getCachedPseudoStyle(FIRST_LINE, style);
2016     } else if (!renderer->isAnonymous() && renderer->isRenderInline()) {
2017         RenderStyle* parentStyle = renderer->parent()->firstLineStyle();
2018         if (parentStyle != renderer->parent()->style()) {
2019             // A first-line style is in effect. Cache a first-line style for ourselves.
2020             style->setHasPseudoStyle(FIRST_LINE_INHERITED);
2021             style = renderer->getCachedPseudoStyle(FIRST_LINE_INHERITED, parentStyle);
2022         }
2023     }
2024 
2025     return style;
2026 }
2027 
getCachedPseudoStyle(PseudoId pseudo,RenderStyle * parentStyle) const2028 RenderStyle* RenderObject::getCachedPseudoStyle(PseudoId pseudo, RenderStyle* parentStyle) const
2029 {
2030     if (pseudo < FIRST_INTERNAL_PSEUDOID && !style()->hasPseudoStyle(pseudo))
2031         return 0;
2032 
2033     RenderStyle* cachedStyle = style()->getCachedPseudoStyle(pseudo);
2034     if (cachedStyle)
2035         return cachedStyle;
2036 
2037     RefPtr<RenderStyle> result = getUncachedPseudoStyle(pseudo, parentStyle);
2038     if (result)
2039         return style()->addCachedPseudoStyle(result.release());
2040     return 0;
2041 }
2042 
getUncachedPseudoStyle(PseudoId pseudo,RenderStyle * parentStyle,RenderStyle * ownStyle) const2043 PassRefPtr<RenderStyle> RenderObject::getUncachedPseudoStyle(PseudoId pseudo, RenderStyle* parentStyle, RenderStyle* ownStyle) const
2044 {
2045     if (pseudo < FIRST_INTERNAL_PSEUDOID && !ownStyle && !style()->hasPseudoStyle(pseudo))
2046         return 0;
2047 
2048     if (!parentStyle) {
2049         ASSERT(!ownStyle);
2050         parentStyle = style();
2051     }
2052 
2053     Node* n = node();
2054     while (n && !n->isElementNode())
2055         n = n->parentNode();
2056     if (!n)
2057         return 0;
2058 
2059     RefPtr<RenderStyle> result;
2060     if (pseudo == FIRST_LINE_INHERITED) {
2061         result = document()->styleSelector()->styleForElement(static_cast<Element*>(n), parentStyle, false);
2062         result->setStyleType(FIRST_LINE_INHERITED);
2063     } else
2064         result = document()->styleSelector()->pseudoStyleForElement(pseudo, static_cast<Element*>(n), parentStyle);
2065     return result.release();
2066 }
2067 
decorationColor(RenderStyle * style)2068 static Color decorationColor(RenderStyle* style)
2069 {
2070     Color result;
2071     if (style->textStrokeWidth() > 0) {
2072         // Prefer stroke color if possible but not if it's fully transparent.
2073         result = style->textStrokeColor();
2074         if (!result.isValid())
2075             result = style->color();
2076         if (result.alpha())
2077             return result;
2078     }
2079 
2080     result = style->textFillColor();
2081     if (!result.isValid())
2082         result = style->color();
2083     return result;
2084 }
2085 
getTextDecorationColors(int decorations,Color & underline,Color & overline,Color & linethrough,bool quirksMode)2086 void RenderObject::getTextDecorationColors(int decorations, Color& underline, Color& overline,
2087                                            Color& linethrough, bool quirksMode)
2088 {
2089     RenderObject* curr = this;
2090     do {
2091         int currDecs = curr->style()->textDecoration();
2092         if (currDecs) {
2093             if (currDecs & UNDERLINE) {
2094                 decorations &= ~UNDERLINE;
2095                 underline = decorationColor(curr->style());
2096             }
2097             if (currDecs & OVERLINE) {
2098                 decorations &= ~OVERLINE;
2099                 overline = decorationColor(curr->style());
2100             }
2101             if (currDecs & LINE_THROUGH) {
2102                 decorations &= ~LINE_THROUGH;
2103                 linethrough = decorationColor(curr->style());
2104             }
2105         }
2106         curr = curr->parent();
2107         if (curr && curr->isRenderBlock() && toRenderBlock(curr)->inlineContinuation())
2108             curr = toRenderBlock(curr)->inlineContinuation();
2109     } while (curr && decorations && (!quirksMode || !curr->node() ||
2110                                      (!curr->node()->hasTagName(aTag) && !curr->node()->hasTagName(fontTag))));
2111 
2112     // If we bailed out, use the element we bailed out at (typically a <font> or <a> element).
2113     if (decorations && curr) {
2114         if (decorations & UNDERLINE)
2115             underline = decorationColor(curr->style());
2116         if (decorations & OVERLINE)
2117             overline = decorationColor(curr->style());
2118         if (decorations & LINE_THROUGH)
2119             linethrough = decorationColor(curr->style());
2120     }
2121 }
2122 
2123 #if ENABLE(DASHBOARD_SUPPORT)
addDashboardRegions(Vector<DashboardRegionValue> & regions)2124 void RenderObject::addDashboardRegions(Vector<DashboardRegionValue>& regions)
2125 {
2126     // Convert the style regions to absolute coordinates.
2127     if (style()->visibility() != VISIBLE || !isBox())
2128         return;
2129 
2130     RenderBox* box = toRenderBox(this);
2131 
2132     const Vector<StyleDashboardRegion>& styleRegions = style()->dashboardRegions();
2133     unsigned i, count = styleRegions.size();
2134     for (i = 0; i < count; i++) {
2135         StyleDashboardRegion styleRegion = styleRegions[i];
2136 
2137         int w = box->width();
2138         int h = box->height();
2139 
2140         DashboardRegionValue region;
2141         region.label = styleRegion.label;
2142         region.bounds = IntRect(styleRegion.offset.left().value(),
2143                                 styleRegion.offset.top().value(),
2144                                 w - styleRegion.offset.left().value() - styleRegion.offset.right().value(),
2145                                 h - styleRegion.offset.top().value() - styleRegion.offset.bottom().value());
2146         region.type = styleRegion.type;
2147 
2148         region.clip = region.bounds;
2149         computeAbsoluteRepaintRect(region.clip);
2150         if (region.clip.height() < 0) {
2151             region.clip.setHeight(0);
2152             region.clip.setWidth(0);
2153         }
2154 
2155         FloatPoint absPos = localToAbsolute();
2156         region.bounds.setX(absPos.x() + styleRegion.offset.left().value());
2157         region.bounds.setY(absPos.y() + styleRegion.offset.top().value());
2158 
2159         if (document()->frame()) {
2160             float pageScaleFactor = document()->frame()->page()->chrome()->scaleFactor();
2161             if (pageScaleFactor != 1.0f) {
2162                 region.bounds.scale(pageScaleFactor);
2163                 region.clip.scale(pageScaleFactor);
2164             }
2165         }
2166 
2167         regions.append(region);
2168     }
2169 }
2170 
collectDashboardRegions(Vector<DashboardRegionValue> & regions)2171 void RenderObject::collectDashboardRegions(Vector<DashboardRegionValue>& regions)
2172 {
2173     // RenderTexts don't have their own style, they just use their parent's style,
2174     // so we don't want to include them.
2175     if (isText())
2176         return;
2177 
2178     addDashboardRegions(regions);
2179     for (RenderObject* curr = firstChild(); curr; curr = curr->nextSibling())
2180         curr->collectDashboardRegions(regions);
2181 }
2182 #endif
2183 
willRenderImage(CachedImage *)2184 bool RenderObject::willRenderImage(CachedImage*)
2185 {
2186     // Without visibility we won't render (and therefore don't care about animation).
2187     if (style()->visibility() != VISIBLE)
2188         return false;
2189 
2190     // 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)
2191     // then we don't want to render either.
2192     return !document()->inPageCache() && !document()->view()->isOffscreen();
2193 }
2194 
maximalOutlineSize(PaintPhase p) const2195 int RenderObject::maximalOutlineSize(PaintPhase p) const
2196 {
2197     if (p != PaintPhaseOutline && p != PaintPhaseSelfOutline && p != PaintPhaseChildOutlines)
2198         return 0;
2199     return toRenderView(document()->renderer())->maximalOutlineSize();
2200 }
2201 
caretMinOffset() const2202 int RenderObject::caretMinOffset() const
2203 {
2204     return 0;
2205 }
2206 
caretMaxOffset() const2207 int RenderObject::caretMaxOffset() const
2208 {
2209     if (isReplaced())
2210         return node() ? max(1U, node()->childNodeCount()) : 1;
2211     if (isHR())
2212         return 1;
2213     return 0;
2214 }
2215 
caretMaxRenderedOffset() const2216 unsigned RenderObject::caretMaxRenderedOffset() const
2217 {
2218     return 0;
2219 }
2220 
previousOffset(int current) const2221 int RenderObject::previousOffset(int current) const
2222 {
2223     return current - 1;
2224 }
2225 
previousOffsetForBackwardDeletion(int current) const2226 int RenderObject::previousOffsetForBackwardDeletion(int current) const
2227 {
2228     return current - 1;
2229 }
2230 
nextOffset(int current) const2231 int RenderObject::nextOffset(int current) const
2232 {
2233     return current + 1;
2234 }
2235 
adjustRectForOutlineAndShadow(IntRect & rect) const2236 void RenderObject::adjustRectForOutlineAndShadow(IntRect& rect) const
2237 {
2238     int outlineSize = outlineStyleForRepaint()->outlineSize();
2239     if (ShadowData* boxShadow = style()->boxShadow()) {
2240         int shadowLeft = 0;
2241         int shadowRight = 0;
2242         int shadowTop = 0;
2243         int shadowBottom = 0;
2244 
2245         do {
2246             if (boxShadow->style == Normal) {
2247                 shadowLeft = min(boxShadow->x - boxShadow->blur - boxShadow->spread - outlineSize, shadowLeft);
2248                 shadowRight = max(boxShadow->x + boxShadow->blur + boxShadow->spread + outlineSize, shadowRight);
2249                 shadowTop = min(boxShadow->y - boxShadow->blur - boxShadow->spread - outlineSize, shadowTop);
2250                 shadowBottom = max(boxShadow->y + boxShadow->blur + boxShadow->spread + outlineSize, shadowBottom);
2251             }
2252 
2253             boxShadow = boxShadow->next;
2254         } while (boxShadow);
2255 
2256         rect.move(shadowLeft, shadowTop);
2257         rect.setWidth(rect.width() - shadowLeft + shadowRight);
2258         rect.setHeight(rect.height() - shadowTop + shadowBottom);
2259     } else
2260         rect.inflate(outlineSize);
2261 }
2262 
animation() const2263 AnimationController* RenderObject::animation() const
2264 {
2265     return document()->frame()->animation();
2266 }
2267 
imageChanged(CachedImage * image,const IntRect * rect)2268 void RenderObject::imageChanged(CachedImage* image, const IntRect* rect)
2269 {
2270     imageChanged(static_cast<WrappedImagePtr>(image), rect);
2271 }
2272 
offsetParent() const2273 RenderBoxModelObject* RenderObject::offsetParent() const
2274 {
2275     // If any of the following holds true return null and stop this algorithm:
2276     // A is the root element.
2277     // A is the HTML body element.
2278     // The computed value of the position property for element A is fixed.
2279     if (isRoot() || isBody() || (isPositioned() && style()->position() == FixedPosition))
2280         return 0;
2281 
2282     // If A is an area HTML element which has a map HTML element somewhere in the ancestor
2283     // chain return the nearest ancestor map HTML element and stop this algorithm.
2284     // FIXME: Implement!
2285 
2286     // Return the nearest ancestor element of A for which at least one of the following is
2287     // true and stop this algorithm if such an ancestor is found:
2288     //     * The computed value of the position property is not static.
2289     //     * It is the HTML body element.
2290     //     * The computed value of the position property of A is static and the ancestor
2291     //       is one of the following HTML elements: td, th, or table.
2292     //     * Our own extension: if there is a difference in the effective zoom
2293     bool skipTables = isPositioned() || isRelPositioned();
2294     float currZoom = style()->effectiveZoom();
2295     RenderObject* curr = parent();
2296     while (curr && (!curr->node() ||
2297                     (!curr->isPositioned() && !curr->isRelPositioned() && !curr->isBody()))) {
2298         Node* element = curr->node();
2299         if (!skipTables && element) {
2300             bool isTableElement = element->hasTagName(tableTag) ||
2301                                   element->hasTagName(tdTag) ||
2302                                   element->hasTagName(thTag);
2303 
2304 #if ENABLE(WML)
2305             if (!isTableElement && element->isWMLElement())
2306                 isTableElement = element->hasTagName(WMLNames::tableTag) ||
2307                                  element->hasTagName(WMLNames::tdTag);
2308 #endif
2309 
2310             if (isTableElement)
2311                 break;
2312         }
2313 
2314         float newZoom = curr->style()->effectiveZoom();
2315         if (currZoom != newZoom)
2316             break;
2317         currZoom = newZoom;
2318         curr = curr->parent();
2319     }
2320     return curr && curr->isBoxModelObject() ? toRenderBoxModelObject(curr) : 0;
2321 }
2322 
createVisiblePosition(int offset,EAffinity affinity)2323 VisiblePosition RenderObject::createVisiblePosition(int offset, EAffinity affinity)
2324 {
2325     // If this is a non-anonymous renderer, then it's simple.
2326     if (Node* node = this->node())
2327         return VisiblePosition(node, offset, affinity);
2328 
2329     // We don't want to cross the boundary between editable and non-editable
2330     // regions of the document, but that is either impossible or at least
2331     // extremely unlikely in any normal case because we stop as soon as we
2332     // find a single non-anonymous renderer.
2333 
2334     // Find a nearby non-anonymous renderer.
2335     RenderObject* child = this;
2336     while (RenderObject* parent = child->parent()) {
2337         // Find non-anonymous content after.
2338         RenderObject* renderer = child;
2339         while ((renderer = renderer->nextInPreOrder(parent))) {
2340             if (Node* node = renderer->node())
2341                 return VisiblePosition(node, 0, DOWNSTREAM);
2342         }
2343 
2344         // Find non-anonymous content before.
2345         renderer = child;
2346         while ((renderer = renderer->previousInPreOrder())) {
2347             if (renderer == parent)
2348                 break;
2349             if (Node* node = renderer->node())
2350                 return VisiblePosition(lastDeepEditingPositionForNode(node), DOWNSTREAM);
2351         }
2352 
2353         // Use the parent itself unless it too is anonymous.
2354         if (Node* node = parent->node())
2355             return VisiblePosition(node, 0, DOWNSTREAM);
2356 
2357         // Repeat at the next level up.
2358         child = parent;
2359     }
2360 
2361     // Everything was anonymous. Give up.
2362     return VisiblePosition();
2363 }
2364 
createVisiblePosition(const Position & position)2365 VisiblePosition RenderObject::createVisiblePosition(const Position& position)
2366 {
2367     if (position.isNotNull())
2368         return VisiblePosition(position);
2369 
2370     ASSERT(!node());
2371     return createVisiblePosition(0, DOWNSTREAM);
2372 }
2373 
2374 #if ENABLE(SVG)
2375 
objectBoundingBox() const2376 FloatRect RenderObject::objectBoundingBox() const
2377 {
2378     ASSERT_NOT_REACHED();
2379     return FloatRect();
2380 }
2381 
2382 // Returns the smallest rectangle enclosing all of the painted content
2383 // respecting clipping, masking, filters, opacity, stroke-width and markers
repaintRectInLocalCoordinates() const2384 FloatRect RenderObject::repaintRectInLocalCoordinates() const
2385 {
2386     ASSERT_NOT_REACHED();
2387     return FloatRect();
2388 }
2389 
localTransform() const2390 TransformationMatrix RenderObject::localTransform() const
2391 {
2392     return TransformationMatrix();
2393 }
2394 
localToParentTransform() const2395 TransformationMatrix RenderObject::localToParentTransform() const
2396 {
2397     // FIXME: This double virtual call indirection is temporary until I can land the
2398     // rest of the of the localToParentTransform() support for SVG.
2399     return localTransform();
2400 }
2401 
absoluteTransform() const2402 TransformationMatrix RenderObject::absoluteTransform() const
2403 {
2404     // FIXME: This should use localToParentTransform(), but much of the SVG code
2405     // depends on RenderBox::absoluteTransform() being the sum of the localTransform()s of all parent renderers.
2406     if (parent())
2407         return localTransform() * parent()->absoluteTransform();
2408     return localTransform();
2409 }
2410 
nodeAtFloatPoint(const HitTestRequest &,HitTestResult &,const FloatPoint &,HitTestAction)2411 bool RenderObject::nodeAtFloatPoint(const HitTestRequest&, HitTestResult&, const FloatPoint&, HitTestAction)
2412 {
2413     ASSERT_NOT_REACHED();
2414     return false;
2415 }
2416 
2417 #endif // ENABLE(SVG)
2418 
2419 } // namespace WebCore
2420 
2421 #ifndef NDEBUG
2422 
showTree(const WebCore::RenderObject * ro)2423 void showTree(const WebCore::RenderObject* ro)
2424 {
2425     if (ro)
2426         ro->showTreeForThis();
2427 }
2428 
2429 #endif
2430