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