71e2140f28a86fbc8076b3ffe6fecfadefa4a716
[WebKit-https.git] / Source / WebCore / rendering / RenderBlock.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2007 David Smith (catfish.man@gmail.com)
5  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include "config.h"
25 #include "RenderBlock.h"
26
27 #include "AXObjectCache.h"
28 #include "ColumnInfo.h"
29 #include "Document.h"
30 #include "Editor.h"
31 #include "Element.h"
32 #include "FloatQuad.h"
33 #include "Frame.h"
34 #include "FrameSelection.h"
35 #include "FrameView.h"
36 #include "GraphicsContext.h"
37 #include "HTMLInputElement.h"
38 #include "HTMLNames.h"
39 #include "HitTestLocation.h"
40 #include "HitTestResult.h"
41 #include "InlineIterator.h"
42 #include "InlineTextBox.h"
43 #include "LayoutRepainter.h"
44 #include "LogicalSelectionOffsetCaches.h"
45 #include "OverflowEvent.h"
46 #include "Page.h"
47 #include "PaintInfo.h"
48 #include "RenderBoxRegionInfo.h"
49 #include "RenderCombineText.h"
50 #include "RenderDeprecatedFlexibleBox.h"
51 #include "RenderFlexibleBox.h"
52 #include "RenderInline.h"
53 #include "RenderLayer.h"
54 #include "RenderMarquee.h"
55 #include "RenderNamedFlowThread.h"
56 #include "RenderRegion.h"
57 #include "RenderTableCell.h"
58 #include "RenderTextFragment.h"
59 #include "RenderTheme.h"
60 #include "RenderView.h"
61 #include "SVGTextRunRenderingContext.h"
62 #include "Settings.h"
63 #include "ShadowRoot.h"
64 #include "TransformState.h"
65 #include <wtf/StackStats.h>
66 #include <wtf/TemporaryChange.h>
67
68 #if ENABLE(CSS_SHAPES)
69 #include "ShapeInsideInfo.h"
70 #include "ShapeOutsideInfo.h"
71 #endif
72
73 #if ENABLE(IOS_TEXT_AUTOSIZING)
74 #include "HTMLElement.h"
75 #endif
76
77 using namespace std;
78 using namespace WTF;
79 using namespace Unicode;
80
81 namespace WebCore {
82
83 using namespace HTMLNames;
84
85 struct SameSizeAsRenderBlock : public RenderBox {
86     void* pointers[2];
87     RenderObjectChildList children;
88     RenderLineBoxList lineBoxes;
89     uint32_t bitfields;
90 };
91
92 COMPILE_ASSERT(sizeof(RenderBlock) == sizeof(SameSizeAsRenderBlock), RenderBlock_should_stay_small);
93
94 typedef WTF::HashMap<const RenderBox*, OwnPtr<ColumnInfo> > ColumnInfoMap;
95 static ColumnInfoMap* gColumnInfoMap = 0;
96
97 static TrackedDescendantsMap* gPositionedDescendantsMap = 0;
98 static TrackedDescendantsMap* gPercentHeightDescendantsMap = 0;
99
100 static TrackedContainerMap* gPositionedContainerMap = 0;
101 static TrackedContainerMap* gPercentHeightContainerMap = 0;
102     
103 typedef WTF::HashMap<RenderBlock*, OwnPtr<ListHashSet<RenderInline*> > > ContinuationOutlineTableMap;
104
105 typedef WTF::HashSet<RenderBlock*> DelayedUpdateScrollInfoSet;
106 static int gDelayUpdateScrollInfo = 0;
107 static DelayedUpdateScrollInfoSet* gDelayedUpdateScrollInfoSet = 0;
108
109 static bool gColumnFlowSplitEnabled = true;
110
111 bool RenderBlock::s_canPropagateFloatIntoSibling = false;
112
113 // This class helps dispatching the 'overflow' event on layout change. overflow can be set on RenderBoxes, yet the existing code
114 // only works on RenderBlocks. If this change, this class should be shared with other RenderBoxes.
115 class OverflowEventDispatcher {
116     WTF_MAKE_NONCOPYABLE(OverflowEventDispatcher);
117 public:
118     OverflowEventDispatcher(const RenderBlock* block)
119         : m_block(block)
120         , m_hadHorizontalLayoutOverflow(false)
121         , m_hadVerticalLayoutOverflow(false)
122     {
123         m_shouldDispatchEvent = !m_block->isAnonymous() && m_block->hasOverflowClip() && m_block->document().hasListenerType(Document::OVERFLOWCHANGED_LISTENER);
124         if (m_shouldDispatchEvent) {
125             m_hadHorizontalLayoutOverflow = m_block->hasHorizontalLayoutOverflow();
126             m_hadVerticalLayoutOverflow = m_block->hasVerticalLayoutOverflow();
127         }
128     }
129
130     ~OverflowEventDispatcher()
131     {
132         if (!m_shouldDispatchEvent)
133             return;
134
135         bool hasHorizontalLayoutOverflow = m_block->hasHorizontalLayoutOverflow();
136         bool hasVerticalLayoutOverflow = m_block->hasVerticalLayoutOverflow();
137
138         bool horizontalLayoutOverflowChanged = hasHorizontalLayoutOverflow != m_hadHorizontalLayoutOverflow;
139         bool verticalLayoutOverflowChanged = hasVerticalLayoutOverflow != m_hadVerticalLayoutOverflow;
140         if (horizontalLayoutOverflowChanged || verticalLayoutOverflowChanged)
141             m_block->view().frameView().scheduleEvent(OverflowEvent::create(horizontalLayoutOverflowChanged, hasHorizontalLayoutOverflow, verticalLayoutOverflowChanged, hasVerticalLayoutOverflow), m_block->element());
142     }
143
144 private:
145     const RenderBlock* m_block;
146     bool m_shouldDispatchEvent;
147     bool m_hadHorizontalLayoutOverflow;
148     bool m_hadVerticalLayoutOverflow;
149 };
150
151 // -------------------------------------------------------------------------------------------------------
152
153 RenderBlock::RenderBlock(Element* element)
154     : RenderBox(element)
155     , m_lineHeight(-1)
156     , m_hasMarginBeforeQuirk(false)
157     , m_hasMarginAfterQuirk(false)
158     , m_beingDestroyed(false)
159     , m_hasMarkupTruncation(false)
160     , m_hasBorderOrPaddingLogicalWidthChanged(false)
161 #if ENABLE(IOS_TEXT_AUTOSIZING)
162     , m_widthForTextAutosizing(-1)
163     , m_lineCountForTextAutosizing(NOT_SET)
164 #endif
165 {
166     setChildrenInline(true);
167 }
168
169 static void removeBlockFromDescendantAndContainerMaps(RenderBlock* block, TrackedDescendantsMap*& descendantMap, TrackedContainerMap*& containerMap)
170 {
171     if (OwnPtr<TrackedRendererListHashSet> descendantSet = descendantMap->take(block)) {
172         TrackedRendererListHashSet::iterator end = descendantSet->end();
173         for (TrackedRendererListHashSet::iterator descendant = descendantSet->begin(); descendant != end; ++descendant) {
174             TrackedContainerMap::iterator it = containerMap->find(*descendant);
175             ASSERT(it != containerMap->end());
176             if (it == containerMap->end())
177                 continue;
178             HashSet<RenderBlock*>* containerSet = it->value.get();
179             ASSERT(containerSet->contains(block));
180             containerSet->remove(block);
181             if (containerSet->isEmpty())
182                 containerMap->remove(it);
183         }
184     }
185 }
186
187 RenderBlock::~RenderBlock()
188 {
189     if (hasColumns())
190         gColumnInfoMap->take(this);
191     if (gPercentHeightDescendantsMap)
192         removeBlockFromDescendantAndContainerMaps(this, gPercentHeightDescendantsMap, gPercentHeightContainerMap);
193     if (gPositionedDescendantsMap)
194         removeBlockFromDescendantAndContainerMaps(this, gPositionedDescendantsMap, gPositionedContainerMap);
195 }
196
197 RenderBlock* RenderBlock::createAnonymous(Document* document)
198 {
199     RenderBlock* renderer = new (document->renderArena()) RenderBlockFlow(0);
200     renderer->setDocumentForAnonymous(document);
201     return renderer;
202 }
203
204 void RenderBlock::willBeDestroyed()
205 {
206     // Mark as being destroyed to avoid trouble with merges in removeChild().
207     m_beingDestroyed = true;
208
209     if (!documentBeingDestroyed()) {
210         if (firstChild() && firstChild()->isRunIn())
211             moveRunInToOriginalPosition(firstChild());
212     }
213
214     // Make sure to destroy anonymous children first while they are still connected to the rest of the tree, so that they will
215     // properly dirty line boxes that they are removed from. Effects that do :before/:after only on hover could crash otherwise.
216     children()->destroyLeftoverChildren();
217
218     // Destroy our continuation before anything other than anonymous children.
219     // The reason we don't destroy it before anonymous children is that they may
220     // have continuations of their own that are anonymous children of our continuation.
221     RenderBoxModelObject* continuation = this->continuation();
222     if (continuation) {
223         continuation->destroy();
224         setContinuation(0);
225     }
226     
227     if (!documentBeingDestroyed()) {
228         if (firstLineBox()) {
229             // We can't wait for RenderBox::destroy to clear the selection,
230             // because by then we will have nuked the line boxes.
231             // FIXME: The FrameSelection should be responsible for this when it
232             // is notified of DOM mutations.
233             if (isSelectionBorder())
234                 view().clearSelection();
235
236             // If we are an anonymous block, then our line boxes might have children
237             // that will outlast this block. In the non-anonymous block case those
238             // children will be destroyed by the time we return from this function.
239             if (isAnonymousBlock()) {
240                 for (InlineFlowBox* box = firstLineBox(); box; box = box->nextLineBox()) {
241                     while (InlineBox* childBox = box->firstChild())
242                         childBox->remove();
243                 }
244             }
245         } else if (parent())
246             parent()->dirtyLinesFromChangedChild(this);
247     }
248
249     m_lineBoxes.deleteLineBoxes(renderArena());
250
251     if (lineGridBox())
252         lineGridBox()->destroy(renderArena());
253
254     if (UNLIKELY(gDelayedUpdateScrollInfoSet != 0))
255         gDelayedUpdateScrollInfoSet->remove(this);
256
257     RenderBox::willBeDestroyed();
258 }
259
260 void RenderBlock::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
261 {
262     RenderStyle* oldStyle = style();
263     s_canPropagateFloatIntoSibling = oldStyle ? !isFloatingOrOutOfFlowPositioned() && !avoidsFloats() : false;
264
265     setReplaced(newStyle->isDisplayInlineType());
266     
267     if (oldStyle && parent() && diff == StyleDifferenceLayout && oldStyle->position() != newStyle->position()) {
268         if (newStyle->position() == StaticPosition)
269             // Clear our positioned objects list. Our absolutely positioned descendants will be
270             // inserted into our containing block's positioned objects list during layout.
271             removePositionedObjects(0, NewContainingBlock);
272         else if (oldStyle->position() == StaticPosition) {
273             // Remove our absolutely positioned descendants from their current containing block.
274             // They will be inserted into our positioned objects list during layout.
275             RenderObject* cb = parent();
276             while (cb && (cb->style()->position() == StaticPosition || (cb->isInline() && !cb->isReplaced())) && !cb->isRenderView()) {
277                 if (cb->style()->position() == RelativePosition && cb->isInline() && !cb->isReplaced()) {
278                     cb = cb->containingBlock();
279                     break;
280                 }
281                 cb = cb->parent();
282             }
283             
284             if (cb->isRenderBlock())
285                 toRenderBlock(cb)->removePositionedObjects(this, NewContainingBlock);
286         }
287
288         if (containsFloats() && !isFloating() && !isOutOfFlowPositioned() && newStyle->hasOutOfFlowPosition())
289             markAllDescendantsWithFloatsForLayout();
290     }
291
292     RenderBox::styleWillChange(diff, newStyle);
293 }
294
295 static bool borderOrPaddingLogicalWidthChanged(const RenderStyle* oldStyle, const RenderStyle* newStyle)
296 {
297     if (newStyle->isHorizontalWritingMode())
298         return oldStyle->borderLeftWidth() != newStyle->borderLeftWidth()
299             || oldStyle->borderRightWidth() != newStyle->borderRightWidth()
300             || oldStyle->paddingLeft() != newStyle->paddingLeft()
301             || oldStyle->paddingRight() != newStyle->paddingRight();
302
303     return oldStyle->borderTopWidth() != newStyle->borderTopWidth()
304         || oldStyle->borderBottomWidth() != newStyle->borderBottomWidth()
305         || oldStyle->paddingTop() != newStyle->paddingTop()
306         || oldStyle->paddingBottom() != newStyle->paddingBottom();
307 }
308
309 void RenderBlock::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
310 {
311     RenderBox::styleDidChange(diff, oldStyle);
312     
313     RenderStyle* newStyle = style();
314     
315 #if ENABLE(CSS_SHAPES)
316     updateShapeInsideInfoAfterStyleChange(newStyle->resolvedShapeInside(), oldStyle ? oldStyle->resolvedShapeInside() : 0);
317 #endif
318
319     if (!isAnonymousBlock()) {
320         // Ensure that all of our continuation blocks pick up the new style.
321         for (RenderBlock* currCont = blockElementContinuation(); currCont; currCont = currCont->blockElementContinuation()) {
322             RenderBoxModelObject* nextCont = currCont->continuation();
323             currCont->setContinuation(0);
324             currCont->setStyle(newStyle);
325             currCont->setContinuation(nextCont);
326         }
327     }
328
329     propagateStyleToAnonymousChildren(true);    
330     m_lineHeight = -1;
331
332     // After our style changed, if we lose our ability to propagate floats into next sibling
333     // blocks, then we need to find the top most parent containing that overhanging float and
334     // then mark its descendants with floats for layout and clear all floats from its next
335     // sibling blocks that exist in our floating objects list. See bug 56299 and 62875.
336     bool canPropagateFloatIntoSibling = !isFloatingOrOutOfFlowPositioned() && !avoidsFloats();
337     if (diff == StyleDifferenceLayout && s_canPropagateFloatIntoSibling && !canPropagateFloatIntoSibling && hasOverhangingFloats()) {
338         RenderBlock* parentBlock = this;
339         const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
340         FloatingObjectSetIterator end = floatingObjectSet.end();
341
342         for (RenderObject* curr = parent(); curr && !curr->isRenderView(); curr = curr->parent()) {
343             if (curr->isRenderBlock()) {
344                 RenderBlock* currBlock = toRenderBlock(curr);
345
346                 if (currBlock->hasOverhangingFloats()) {
347                     for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
348                         RenderBox* renderer = (*it)->renderer();
349                         if (currBlock->hasOverhangingFloat(renderer)) {
350                             parentBlock = currBlock;
351                             break;
352                         }
353                     }
354                 }
355             }
356         }
357               
358         parentBlock->markAllDescendantsWithFloatsForLayout();
359         parentBlock->markSiblingsWithFloatsForLayout();
360     }
361     
362     // It's possible for our border/padding to change, but for the overall logical width of the block to
363     // end up being the same. We keep track of this change so in layoutBlock, we can know to set relayoutChildren=true.
364     m_hasBorderOrPaddingLogicalWidthChanged = oldStyle && diff == StyleDifferenceLayout && needsLayout() && borderOrPaddingLogicalWidthChanged(oldStyle, newStyle);
365 }
366
367 RenderBlock* RenderBlock::continuationBefore(RenderObject* beforeChild)
368 {
369     if (beforeChild && beforeChild->parent() == this)
370         return this;
371
372     RenderBlock* curr = toRenderBlock(continuation());
373     RenderBlock* nextToLast = this;
374     RenderBlock* last = this;
375     while (curr) {
376         if (beforeChild && beforeChild->parent() == curr) {
377             if (curr->firstChild() == beforeChild)
378                 return last;
379             return curr;
380         }
381
382         nextToLast = last;
383         last = curr;
384         curr = toRenderBlock(curr->continuation());
385     }
386
387     if (!beforeChild && !last->firstChild())
388         return nextToLast;
389     return last;
390 }
391
392 void RenderBlock::addChildToContinuation(RenderObject* newChild, RenderObject* beforeChild)
393 {
394     RenderBlock* flow = continuationBefore(beforeChild);
395     ASSERT(!beforeChild || beforeChild->parent()->isAnonymousColumnSpanBlock() || beforeChild->parent()->isRenderBlock());
396     RenderBoxModelObject* beforeChildParent = 0;
397     if (beforeChild)
398         beforeChildParent = toRenderBoxModelObject(beforeChild->parent());
399     else {
400         RenderBoxModelObject* cont = flow->continuation();
401         if (cont)
402             beforeChildParent = cont;
403         else
404             beforeChildParent = flow;
405     }
406
407     if (newChild->isFloatingOrOutOfFlowPositioned()) {
408         beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
409         return;
410     }
411
412     // A continuation always consists of two potential candidates: a block or an anonymous
413     // column span box holding column span children.
414     bool childIsNormal = newChild->isInline() || !newChild->style()->columnSpan();
415     bool bcpIsNormal = beforeChildParent->isInline() || !beforeChildParent->style()->columnSpan();
416     bool flowIsNormal = flow->isInline() || !flow->style()->columnSpan();
417
418     if (flow == beforeChildParent) {
419         flow->addChildIgnoringContinuation(newChild, beforeChild);
420         return;
421     }
422     
423     // The goal here is to match up if we can, so that we can coalesce and create the
424     // minimal # of continuations needed for the inline.
425     if (childIsNormal == bcpIsNormal) {
426         beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
427         return;
428     }
429     if (flowIsNormal == childIsNormal) {
430         flow->addChildIgnoringContinuation(newChild, 0); // Just treat like an append.
431         return;
432     }
433     beforeChildParent->addChildIgnoringContinuation(newChild, beforeChild);
434 }
435
436
437 void RenderBlock::addChildToAnonymousColumnBlocks(RenderObject* newChild, RenderObject* beforeChild)
438 {
439     ASSERT(!continuation()); // We don't yet support column spans that aren't immediate children of the multi-column block.
440         
441     // The goal is to locate a suitable box in which to place our child.
442     RenderBlock* beforeChildParent = 0;
443     if (beforeChild) {
444         RenderObject* curr = beforeChild;
445         while (curr && curr->parent() != this)
446             curr = curr->parent();
447         beforeChildParent = toRenderBlock(curr);
448         ASSERT(beforeChildParent);
449         ASSERT(beforeChildParent->isAnonymousColumnsBlock() || beforeChildParent->isAnonymousColumnSpanBlock());
450     } else
451         beforeChildParent = toRenderBlock(lastChild());
452
453     // If the new child is floating or positioned it can just go in that block.
454     if (newChild->isFloatingOrOutOfFlowPositioned()) {
455         beforeChildParent->addChildIgnoringAnonymousColumnBlocks(newChild, beforeChild);
456         return;
457     }
458
459     // See if the child can be placed in the box.
460     bool newChildHasColumnSpan = newChild->style()->columnSpan() && !newChild->isInline();
461     bool beforeChildParentHoldsColumnSpans = beforeChildParent->isAnonymousColumnSpanBlock();
462
463     if (newChildHasColumnSpan == beforeChildParentHoldsColumnSpans) {
464         beforeChildParent->addChildIgnoringAnonymousColumnBlocks(newChild, beforeChild);
465         return;
466     }
467
468     if (!beforeChild) {
469         // Create a new block of the correct type.
470         RenderBlock* newBox = newChildHasColumnSpan ? createAnonymousColumnSpanBlock() : createAnonymousColumnsBlock();
471         children()->appendChildNode(this, newBox);
472         newBox->addChildIgnoringAnonymousColumnBlocks(newChild, 0);
473         return;
474     }
475
476     RenderObject* immediateChild = beforeChild;
477     bool isPreviousBlockViable = true;
478     while (immediateChild->parent() != this) {
479         if (isPreviousBlockViable)
480             isPreviousBlockViable = !immediateChild->previousSibling();
481         immediateChild = immediateChild->parent();
482     }
483     if (isPreviousBlockViable && immediateChild->previousSibling()) {
484         toRenderBlock(immediateChild->previousSibling())->addChildIgnoringAnonymousColumnBlocks(newChild, 0); // Treat like an append.
485         return;
486     }
487         
488     // Split our anonymous blocks.
489     RenderObject* newBeforeChild = splitAnonymousBoxesAroundChild(beforeChild);
490
491     
492     // Create a new anonymous box of the appropriate type.
493     RenderBlock* newBox = newChildHasColumnSpan ? createAnonymousColumnSpanBlock() : createAnonymousColumnsBlock();
494     children()->insertChildNode(this, newBox, newBeforeChild);
495     newBox->addChildIgnoringAnonymousColumnBlocks(newChild, 0);
496     return;
497 }
498
499 RenderBlock* RenderBlock::containingColumnsBlock(bool allowAnonymousColumnBlock)
500 {
501     RenderBlock* firstChildIgnoringAnonymousWrappers = 0;
502     for (RenderObject* curr = this; curr; curr = curr->parent()) {
503         if (!curr->isRenderBlock() || curr->isFloatingOrOutOfFlowPositioned() || curr->isTableCell() || curr->isRoot() || curr->isRenderView() || curr->hasOverflowClip()
504             || curr->isInlineBlockOrInlineTable())
505             return 0;
506
507         // FIXME: Tables, RenderButtons, and RenderListItems all do special management
508         // of their children that breaks when the flow is split through them. Disabling
509         // multi-column for them to avoid this problem.
510         if (curr->isTable() || curr->isRenderButton() || curr->isListItem())
511             return 0;
512         
513         RenderBlock* currBlock = toRenderBlock(curr);
514         if (!currBlock->createsAnonymousWrapper())
515             firstChildIgnoringAnonymousWrappers = currBlock;
516
517         if (currBlock->style()->specifiesColumns() && (allowAnonymousColumnBlock || !currBlock->isAnonymousColumnsBlock()))
518             return firstChildIgnoringAnonymousWrappers;
519             
520         if (currBlock->isAnonymousColumnSpanBlock())
521             return 0;
522     }
523     return 0;
524 }
525
526 RenderBlock* RenderBlock::clone() const
527 {
528     RenderBlock* cloneBlock;
529     if (isAnonymousBlock()) {
530         cloneBlock = createAnonymousBlock();
531         cloneBlock->setChildrenInline(childrenInline());
532     }
533     else {
534         RenderObject* cloneRenderer = element()->createRenderer(renderArena(), style());
535         cloneBlock = toRenderBlock(cloneRenderer);
536         cloneBlock->setStyle(style());
537
538         // This takes care of setting the right value of childrenInline in case
539         // generated content is added to cloneBlock and 'this' does not have
540         // generated content added yet.
541         cloneBlock->setChildrenInline(cloneBlock->firstChild() ? cloneBlock->firstChild()->isInline() : childrenInline());
542     }
543     cloneBlock->setFlowThreadState(flowThreadState());
544     return cloneBlock;
545 }
546
547 void RenderBlock::splitBlocks(RenderBlock* fromBlock, RenderBlock* toBlock,
548                               RenderBlock* middleBlock,
549                               RenderObject* beforeChild, RenderBoxModelObject* oldCont)
550 {
551     // Create a clone of this inline.
552     RenderBlock* cloneBlock = clone();
553     if (!isAnonymousBlock())
554         cloneBlock->setContinuation(oldCont);
555
556     if (!beforeChild && isAfterContent(lastChild()))
557         beforeChild = lastChild();
558
559     // If we are moving inline children from |this| to cloneBlock, then we need
560     // to clear our line box tree.
561     if (beforeChild && childrenInline())
562         deleteLineBoxTree();
563
564     // Now take all of the children from beforeChild to the end and remove
565     // them from |this| and place them in the clone.
566     moveChildrenTo(cloneBlock, beforeChild, 0, true);
567     
568     // Hook |clone| up as the continuation of the middle block.
569     if (!cloneBlock->isAnonymousBlock())
570         middleBlock->setContinuation(cloneBlock);
571
572     // We have been reparented and are now under the fromBlock.  We need
573     // to walk up our block parent chain until we hit the containing anonymous columns block.
574     // Once we hit the anonymous columns block we're done.
575     RenderBoxModelObject* curr = toRenderBoxModelObject(parent());
576     RenderBoxModelObject* currChild = this;
577     RenderObject* currChildNextSibling = currChild->nextSibling();
578
579     while (curr && curr != fromBlock) {
580         ASSERT_WITH_SECURITY_IMPLICATION(curr->isRenderBlock());
581         
582         RenderBlock* blockCurr = toRenderBlock(curr);
583         
584         // Create a new clone.
585         RenderBlock* cloneChild = cloneBlock;
586         cloneBlock = blockCurr->clone();
587
588         // Insert our child clone as the first child.
589         cloneBlock->addChildIgnoringContinuation(cloneChild, 0);
590
591         // Hook the clone up as a continuation of |curr|.  Note we do encounter
592         // anonymous blocks possibly as we walk up the block chain.  When we split an
593         // anonymous block, there's no need to do any continuation hookup, since we haven't
594         // actually split a real element.
595         if (!blockCurr->isAnonymousBlock()) {
596             oldCont = blockCurr->continuation();
597             blockCurr->setContinuation(cloneBlock);
598             cloneBlock->setContinuation(oldCont);
599         }
600
601         // Now we need to take all of the children starting from the first child
602         // *after* currChild and append them all to the clone.
603         blockCurr->moveChildrenTo(cloneBlock, currChildNextSibling, 0, true);
604
605         // Keep walking up the chain.
606         currChild = curr;
607         currChildNextSibling = currChild->nextSibling();
608         curr = toRenderBoxModelObject(curr->parent());
609     }
610
611     // Now we are at the columns block level. We need to put the clone into the toBlock.
612     toBlock->children()->appendChildNode(toBlock, cloneBlock);
613
614     // Now take all the children after currChild and remove them from the fromBlock
615     // and put them in the toBlock.
616     fromBlock->moveChildrenTo(toBlock, currChildNextSibling, 0, true);
617 }
618
619 void RenderBlock::splitFlow(RenderObject* beforeChild, RenderBlock* newBlockBox,
620                             RenderObject* newChild, RenderBoxModelObject* oldCont)
621 {
622     RenderBlock* pre = 0;
623     RenderBlock* block = containingColumnsBlock();
624     
625     // Delete our line boxes before we do the inline split into continuations.
626     block->deleteLineBoxTree();
627     
628     bool madeNewBeforeBlock = false;
629     if (block->isAnonymousColumnsBlock()) {
630         // We can reuse this block and make it the preBlock of the next continuation.
631         pre = block;
632         pre->removePositionedObjects(0);
633         pre->removeFloatingObjects();
634         block = toRenderBlock(block->parent());
635     } else {
636         // No anonymous block available for use.  Make one.
637         pre = block->createAnonymousColumnsBlock();
638         pre->setChildrenInline(false);
639         madeNewBeforeBlock = true;
640     }
641
642     RenderBlock* post = block->createAnonymousColumnsBlock();
643     post->setChildrenInline(false);
644
645     RenderObject* boxFirst = madeNewBeforeBlock ? block->firstChild() : pre->nextSibling();
646     if (madeNewBeforeBlock)
647         block->children()->insertChildNode(block, pre, boxFirst);
648     block->children()->insertChildNode(block, newBlockBox, boxFirst);
649     block->children()->insertChildNode(block, post, boxFirst);
650     block->setChildrenInline(false);
651     
652     if (madeNewBeforeBlock)
653         block->moveChildrenTo(pre, boxFirst, 0, true);
654
655     splitBlocks(pre, post, newBlockBox, beforeChild, oldCont);
656
657     // We already know the newBlockBox isn't going to contain inline kids, so avoid wasting
658     // time in makeChildrenNonInline by just setting this explicitly up front.
659     newBlockBox->setChildrenInline(false);
660
661     // We delayed adding the newChild until now so that the |newBlockBox| would be fully
662     // connected, thus allowing newChild access to a renderArena should it need
663     // to wrap itself in additional boxes (e.g., table construction).
664     newBlockBox->addChild(newChild);
665
666     // Always just do a full layout in order to ensure that line boxes (especially wrappers for images)
667     // get deleted properly.  Because objects moves from the pre block into the post block, we want to
668     // make new line boxes instead of leaving the old line boxes around.
669     pre->setNeedsLayoutAndPrefWidthsRecalc();
670     block->setNeedsLayoutAndPrefWidthsRecalc();
671     post->setNeedsLayoutAndPrefWidthsRecalc();
672 }
673
674 void RenderBlock::makeChildrenAnonymousColumnBlocks(RenderObject* beforeChild, RenderBlock* newBlockBox, RenderObject* newChild)
675 {
676     RenderBlock* pre = 0;
677     RenderBlock* post = 0;
678     RenderBlock* block = this; // Eventually block will not just be |this|, but will also be a block nested inside |this|.  Assign to a variable
679                                // so that we don't have to patch all of the rest of the code later on.
680     
681     // Delete the block's line boxes before we do the split.
682     block->deleteLineBoxTree();
683
684     if (beforeChild && beforeChild->parent() != this)
685         beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
686
687     if (beforeChild != firstChild()) {
688         pre = block->createAnonymousColumnsBlock();
689         pre->setChildrenInline(block->childrenInline());
690     }
691
692     if (beforeChild) {
693         post = block->createAnonymousColumnsBlock();
694         post->setChildrenInline(block->childrenInline());
695     }
696
697     RenderObject* boxFirst = block->firstChild();
698     if (pre)
699         block->children()->insertChildNode(block, pre, boxFirst);
700     block->children()->insertChildNode(block, newBlockBox, boxFirst);
701     if (post)
702         block->children()->insertChildNode(block, post, boxFirst);
703     block->setChildrenInline(false);
704     
705     // The pre/post blocks always have layers, so we know to always do a full insert/remove (so we pass true as the last argument).
706     block->moveChildrenTo(pre, boxFirst, beforeChild, true);
707     block->moveChildrenTo(post, beforeChild, 0, true);
708
709     // We already know the newBlockBox isn't going to contain inline kids, so avoid wasting
710     // time in makeChildrenNonInline by just setting this explicitly up front.
711     newBlockBox->setChildrenInline(false);
712
713     // We delayed adding the newChild until now so that the |newBlockBox| would be fully
714     // connected, thus allowing newChild access to a renderArena should it need
715     // to wrap itself in additional boxes (e.g., table construction).
716     newBlockBox->addChild(newChild);
717
718     // Always just do a full layout in order to ensure that line boxes (especially wrappers for images)
719     // get deleted properly.  Because objects moved from the pre block into the post block, we want to
720     // make new line boxes instead of leaving the old line boxes around.
721     if (pre)
722         pre->setNeedsLayoutAndPrefWidthsRecalc();
723     block->setNeedsLayoutAndPrefWidthsRecalc();
724     if (post)
725         post->setNeedsLayoutAndPrefWidthsRecalc();
726 }
727
728 RenderBlock* RenderBlock::columnsBlockForSpanningElement(RenderObject* newChild)
729 {
730     // FIXME: This function is the gateway for the addition of column-span support.  It will
731     // be added to in three stages:
732     // (1) Immediate children of a multi-column block can span.
733     // (2) Nested block-level children with only block-level ancestors between them and the multi-column block can span.
734     // (3) Nested children with block or inline ancestors between them and the multi-column block can span (this is when we
735     // cross the streams and have to cope with both types of continuations mixed together).
736     // This function currently supports (1) and (2).
737     RenderBlock* columnsBlockAncestor = 0;
738     if (!newChild->isText() && newChild->style()->columnSpan() && !newChild->isBeforeOrAfterContent()
739         && !newChild->isFloatingOrOutOfFlowPositioned() && !newChild->isInline() && !isAnonymousColumnSpanBlock()) {
740         columnsBlockAncestor = containingColumnsBlock(false);
741         if (columnsBlockAncestor) {
742             // Make sure that none of the parent ancestors have a continuation.
743             // If yes, we do not want split the block into continuations.
744             RenderObject* curr = this;
745             while (curr && curr != columnsBlockAncestor) {
746                 if (curr->isRenderBlock() && toRenderBlock(curr)->continuation()) {
747                     columnsBlockAncestor = 0;
748                     break;
749                 }
750                 curr = curr->parent();
751             }
752         }
753     }
754     return columnsBlockAncestor;
755 }
756
757 void RenderBlock::addChildIgnoringAnonymousColumnBlocks(RenderObject* newChild, RenderObject* beforeChild)
758 {
759     if (beforeChild && beforeChild->parent() != this) {
760         RenderObject* beforeChildContainer = beforeChild->parent();
761         while (beforeChildContainer->parent() != this)
762             beforeChildContainer = beforeChildContainer->parent();
763         ASSERT(beforeChildContainer);
764
765         if (beforeChildContainer->isAnonymous()) {
766             // If the requested beforeChild is not one of our children, then this is because
767             // there is an anonymous container within this object that contains the beforeChild.
768             RenderObject* beforeChildAnonymousContainer = beforeChildContainer;
769             if (beforeChildAnonymousContainer->isAnonymousBlock()
770 #if ENABLE(FULLSCREEN_API)
771                 // Full screen renderers and full screen placeholders act as anonymous blocks, not tables:
772                 || beforeChildAnonymousContainer->isRenderFullScreen()
773                 || beforeChildAnonymousContainer->isRenderFullScreenPlaceholder()
774 #endif
775                 ) {
776                 // Insert the child into the anonymous block box instead of here.
777                 if (newChild->isInline() || beforeChild->parent()->firstChild() != beforeChild)
778                     beforeChild->parent()->addChild(newChild, beforeChild);
779                 else
780                     addChild(newChild, beforeChild->parent());
781                 return;
782             }
783
784             ASSERT(beforeChildAnonymousContainer->isTable());
785             if (newChild->isTablePart()) {
786                 // Insert into the anonymous table.
787                 beforeChildAnonymousContainer->addChild(newChild, beforeChild);
788                 return;
789             }
790
791             beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
792
793             ASSERT(beforeChild->parent() == this);
794             if (beforeChild->parent() != this) {
795                 // We should never reach here. If we do, we need to use the
796                 // safe fallback to use the topmost beforeChild container.
797                 beforeChild = beforeChildContainer;
798             }
799         } else {
800             // We will reach here when beforeChild is a run-in element.
801             // If run-in element precedes a block-level element, it becomes the
802             // the first inline child of that block level element. The insertion
803             // point will be before that block-level element.
804             ASSERT(beforeChild->isRunIn());
805             beforeChild = beforeChildContainer;
806         }
807     }
808
809     // Nothing goes before the intruded run-in.
810     if (beforeChild && beforeChild->isRunIn() && runInIsPlacedIntoSiblingBlock(beforeChild))
811         beforeChild = beforeChild->nextSibling();
812
813     // Check for a spanning element in columns.
814     if (gColumnFlowSplitEnabled) {
815         RenderBlock* columnsBlockAncestor = columnsBlockForSpanningElement(newChild);
816         if (columnsBlockAncestor) {
817             TemporaryChange<bool> columnFlowSplitEnabled(gColumnFlowSplitEnabled, false);
818             // We are placing a column-span element inside a block.
819             RenderBlock* newBox = createAnonymousColumnSpanBlock();
820         
821             if (columnsBlockAncestor != this && !isRenderFlowThread()) {
822                 // We are nested inside a multi-column element and are being split by the span. We have to break up
823                 // our block into continuations.
824                 RenderBoxModelObject* oldContinuation = continuation();
825
826                 // When we split an anonymous block, there's no need to do any continuation hookup,
827                 // since we haven't actually split a real element.
828                 if (!isAnonymousBlock())
829                     setContinuation(newBox);
830
831                 splitFlow(beforeChild, newBox, newChild, oldContinuation);
832                 return;
833             }
834
835             // We have to perform a split of this block's children. This involves creating an anonymous block box to hold
836             // the column-spanning |newChild|. We take all of the children from before |newChild| and put them into
837             // one anonymous columns block, and all of the children after |newChild| go into another anonymous block.
838             makeChildrenAnonymousColumnBlocks(beforeChild, newBox, newChild);
839             return;
840         }
841     }
842
843     bool madeBoxesNonInline = false;
844
845     // A block has to either have all of its children inline, or all of its children as blocks.
846     // So, if our children are currently inline and a block child has to be inserted, we move all our
847     // inline children into anonymous block boxes.
848     if (childrenInline() && !newChild->isInline() && !newChild->isFloatingOrOutOfFlowPositioned()) {
849         // This is a block with inline content. Wrap the inline content in anonymous blocks.
850         makeChildrenNonInline(beforeChild);
851         madeBoxesNonInline = true;
852
853         if (beforeChild && beforeChild->parent() != this) {
854             beforeChild = beforeChild->parent();
855             ASSERT(beforeChild->isAnonymousBlock());
856             ASSERT(beforeChild->parent() == this);
857         }
858     } else if (!childrenInline() && (newChild->isFloatingOrOutOfFlowPositioned() || newChild->isInline())) {
859         // If we're inserting an inline child but all of our children are blocks, then we have to make sure
860         // it is put into an anomyous block box. We try to use an existing anonymous box if possible, otherwise
861         // a new one is created and inserted into our list of children in the appropriate position.
862         RenderObject* afterChild = beforeChild ? beforeChild->previousSibling() : lastChild();
863
864         if (afterChild && afterChild->isAnonymousBlock()) {
865             afterChild->addChild(newChild);
866             return;
867         }
868
869         if (newChild->isInline()) {
870             // No suitable existing anonymous box - create a new one.
871             RenderBlock* newBox = createAnonymousBlock();
872             RenderBox::addChild(newBox, beforeChild);
873             newBox->addChild(newChild);
874             return;
875         }
876     }
877
878     RenderBox::addChild(newChild, beforeChild);
879  
880     // Handle placement of run-ins.
881     placeRunInIfNeeded(newChild);
882
883     if (madeBoxesNonInline && parent() && isAnonymousBlock() && parent()->isRenderBlock())
884         toRenderBlock(parent())->removeLeftoverAnonymousBlock(this);
885     // this object may be dead here
886 }
887
888 void RenderBlock::addChild(RenderObject* newChild, RenderObject* beforeChild)
889 {
890     if (continuation() && !isAnonymousBlock())
891         addChildToContinuation(newChild, beforeChild);
892     else
893         addChildIgnoringContinuation(newChild, beforeChild);
894 }
895
896 void RenderBlock::addChildIgnoringContinuation(RenderObject* newChild, RenderObject* beforeChild)
897 {
898     if (!isAnonymousBlock() && firstChild() && (firstChild()->isAnonymousColumnsBlock() || firstChild()->isAnonymousColumnSpanBlock()))
899         addChildToAnonymousColumnBlocks(newChild, beforeChild);
900     else
901         addChildIgnoringAnonymousColumnBlocks(newChild, beforeChild);
902 }
903
904 static void getInlineRun(RenderObject* start, RenderObject* boundary,
905                          RenderObject*& inlineRunStart,
906                          RenderObject*& inlineRunEnd)
907 {
908     // Beginning at |start| we find the largest contiguous run of inlines that
909     // we can.  We denote the run with start and end points, |inlineRunStart|
910     // and |inlineRunEnd|.  Note that these two values may be the same if
911     // we encounter only one inline.
912     //
913     // We skip any non-inlines we encounter as long as we haven't found any
914     // inlines yet.
915     //
916     // |boundary| indicates a non-inclusive boundary point.  Regardless of whether |boundary|
917     // is inline or not, we will not include it in a run with inlines before it.  It's as though we encountered
918     // a non-inline.
919     
920     // Start by skipping as many non-inlines as we can.
921     RenderObject * curr = start;
922     bool sawInline;
923     do {
924         while (curr && !(curr->isInline() || curr->isFloatingOrOutOfFlowPositioned()))
925             curr = curr->nextSibling();
926         
927         inlineRunStart = inlineRunEnd = curr;
928         
929         if (!curr)
930             return; // No more inline children to be found.
931         
932         sawInline = curr->isInline();
933         
934         curr = curr->nextSibling();
935         while (curr && (curr->isInline() || curr->isFloatingOrOutOfFlowPositioned()) && (curr != boundary)) {
936             inlineRunEnd = curr;
937             if (curr->isInline())
938                 sawInline = true;
939             curr = curr->nextSibling();
940         }
941     } while (!sawInline);
942 }
943
944 void RenderBlock::deleteLineBoxTree()
945 {
946     if (containsFloats())
947         m_floatingObjects->clearLineBoxTreePointers();
948     m_lineBoxes.deleteLineBoxTree(renderArena());
949
950     if (AXObjectCache* cache = document().existingAXObjectCache())
951         cache->recomputeIsIgnored(this);
952 }
953
954 RootInlineBox* RenderBlock::createRootInlineBox()
955 {
956     return new (renderArena()) RootInlineBox(*this);
957 }
958
959 RootInlineBox* RenderBlock::createAndAppendRootInlineBox()
960 {
961     RootInlineBox* rootBox = createRootInlineBox();
962     m_lineBoxes.appendLineBox(rootBox);
963
964     if (UNLIKELY(AXObjectCache::accessibilityEnabled()) && m_lineBoxes.firstLineBox() == rootBox) {
965         if (AXObjectCache* cache = document().existingAXObjectCache())
966             cache->recomputeIsIgnored(this);
967     }
968
969     return rootBox;
970 }
971
972 void RenderBlock::makeChildrenNonInline(RenderObject *insertionPoint)
973 {    
974     // makeChildrenNonInline takes a block whose children are *all* inline and it
975     // makes sure that inline children are coalesced under anonymous
976     // blocks.  If |insertionPoint| is defined, then it represents the insertion point for
977     // the new block child that is causing us to have to wrap all the inlines.  This
978     // means that we cannot coalesce inlines before |insertionPoint| with inlines following
979     // |insertionPoint|, because the new child is going to be inserted in between the inlines,
980     // splitting them.
981     ASSERT(isInlineBlockOrInlineTable() || !isInline());
982     ASSERT(!insertionPoint || insertionPoint->parent() == this);
983
984     setChildrenInline(false);
985
986     RenderObject *child = firstChild();
987     if (!child)
988         return;
989
990     deleteLineBoxTree();
991
992     // Since we are going to have block children, we have to move
993     // back the run-in to its original place.
994     if (child->isRunIn()) {
995         moveRunInToOriginalPosition(child);
996         child = firstChild();
997     }
998
999     while (child) {
1000         RenderObject *inlineRunStart, *inlineRunEnd;
1001         getInlineRun(child, insertionPoint, inlineRunStart, inlineRunEnd);
1002
1003         if (!inlineRunStart)
1004             break;
1005
1006         child = inlineRunEnd->nextSibling();
1007
1008         RenderBlock* block = createAnonymousBlock();
1009         children()->insertChildNode(this, block, inlineRunStart);
1010         moveChildrenTo(block, inlineRunStart, child);
1011     }
1012
1013 #ifndef NDEBUG
1014     for (RenderObject *c = firstChild(); c; c = c->nextSibling())
1015         ASSERT(!c->isInline());
1016 #endif
1017
1018     repaint();
1019 }
1020
1021 void RenderBlock::removeLeftoverAnonymousBlock(RenderBlock* child)
1022 {
1023     ASSERT(child->isAnonymousBlock());
1024     ASSERT(!child->childrenInline());
1025     
1026     if (child->continuation() || (child->firstChild() && (child->isAnonymousColumnSpanBlock() || child->isAnonymousColumnsBlock())))
1027         return;
1028     
1029     RenderObject* firstAnChild = child->m_children.firstChild();
1030     RenderObject* lastAnChild = child->m_children.lastChild();
1031     if (firstAnChild) {
1032         RenderObject* o = firstAnChild;
1033         while (o) {
1034             o->setParent(this);
1035             o = o->nextSibling();
1036         }
1037         firstAnChild->setPreviousSibling(child->previousSibling());
1038         lastAnChild->setNextSibling(child->nextSibling());
1039         if (child->previousSibling())
1040             child->previousSibling()->setNextSibling(firstAnChild);
1041         if (child->nextSibling())
1042             child->nextSibling()->setPreviousSibling(lastAnChild);
1043             
1044         if (child == m_children.firstChild())
1045             m_children.setFirstChild(firstAnChild);
1046         if (child == m_children.lastChild())
1047             m_children.setLastChild(lastAnChild);
1048     } else {
1049         if (child == m_children.firstChild())
1050             m_children.setFirstChild(child->nextSibling());
1051         if (child == m_children.lastChild())
1052             m_children.setLastChild(child->previousSibling());
1053
1054         if (child->previousSibling())
1055             child->previousSibling()->setNextSibling(child->nextSibling());
1056         if (child->nextSibling())
1057             child->nextSibling()->setPreviousSibling(child->previousSibling());
1058     }
1059
1060     child->children()->setFirstChild(0);
1061     child->m_next = 0;
1062
1063     // Remove all the information in the flow thread associated with the leftover anonymous block.
1064     child->removeFromRenderFlowThread();
1065
1066     child->setParent(0);
1067     child->setPreviousSibling(0);
1068     child->setNextSibling(0);
1069
1070     child->destroy();
1071 }
1072
1073 static bool canMergeContiguousAnonymousBlocks(RenderObject* oldChild, RenderObject* prev, RenderObject* next)
1074 {
1075     if (oldChild->documentBeingDestroyed() || oldChild->isInline() || oldChild->virtualContinuation())
1076         return false;
1077
1078     if ((prev && (!prev->isAnonymousBlock() || toRenderBlock(prev)->continuation() || toRenderBlock(prev)->beingDestroyed()))
1079         || (next && (!next->isAnonymousBlock() || toRenderBlock(next)->continuation() || toRenderBlock(next)->beingDestroyed())))
1080         return false;
1081
1082     // FIXME: This check isn't required when inline run-ins can't be split into continuations.
1083     if (prev && prev->firstChild() && prev->firstChild()->isInline() && prev->firstChild()->isRunIn())
1084         return false;
1085
1086     if ((prev && (prev->isRubyRun() || prev->isRubyBase()))
1087         || (next && (next->isRubyRun() || next->isRubyBase())))
1088         return false;
1089
1090     if (!prev || !next)
1091         return true;
1092
1093     // Make sure the types of the anonymous blocks match up.
1094     return prev->isAnonymousColumnsBlock() == next->isAnonymousColumnsBlock()
1095            && prev->isAnonymousColumnSpanBlock() == next->isAnonymousColumnSpanBlock();
1096 }
1097
1098 void RenderBlock::collapseAnonymousBoxChild(RenderBlock* parent, RenderObject* child)
1099 {
1100     parent->setNeedsLayoutAndPrefWidthsRecalc();
1101     parent->setChildrenInline(child->childrenInline());
1102     RenderObject* nextSibling = child->nextSibling();
1103
1104     RenderFlowThread* childFlowThread = child->flowThreadContainingBlock();
1105     CurrentRenderFlowThreadMaintainer flowThreadMaintainer(childFlowThread);
1106     
1107     RenderBlock* anonBlock = toRenderBlock(parent->children()->removeChildNode(parent, child, child->hasLayer()));
1108     anonBlock->moveAllChildrenTo(parent, nextSibling, child->hasLayer());
1109     // Delete the now-empty block's lines and nuke it.
1110     anonBlock->deleteLineBoxTree();
1111     if (childFlowThread && childFlowThread->isRenderNamedFlowThread())
1112         toRenderNamedFlowThread(childFlowThread)->removeFlowChildInfo(anonBlock);
1113     anonBlock->destroy();
1114 }
1115
1116 void RenderBlock::moveAllChildrenIncludingFloatsTo(RenderBlock* toBlock, bool fullRemoveInsert)
1117 {
1118     moveAllChildrenTo(toBlock, fullRemoveInsert);
1119
1120     // When a portion of the render tree is being detached, anonymous blocks
1121     // will be combined as their children are deleted. In this process, the
1122     // anonymous block later in the tree is merged into the one preceeding it.
1123     // It can happen that the later block (this) contains floats that the
1124     // previous block (toBlock) did not contain, and thus are not in the
1125     // floating objects list for toBlock. This can result in toBlock containing
1126     // floats that are not in it's floating objects list, but are in the
1127     // floating objects lists of siblings and parents. This can cause problems
1128     // when the float itself is deleted, since the deletion code assumes that
1129     // if a float is not in it's containing block's floating objects list, it
1130     // isn't in any floating objects list. In order to preserve this condition
1131     // (removing it has serious performance implications), we need to copy the
1132     // floating objects from the old block (this) to the new block (toBlock).
1133     // The float's metrics will likely all be wrong, but since toBlock is
1134     // already marked for layout, this will get fixed before anything gets
1135     // displayed.
1136     // See bug https://bugs.webkit.org/show_bug.cgi?id=115566
1137     if (m_floatingObjects) {
1138         if (!toBlock->m_floatingObjects)
1139             toBlock->createFloatingObjects();
1140
1141         const FloatingObjectSet& fromFloatingObjectSet = m_floatingObjects->set();
1142         FloatingObjectSetIterator end = fromFloatingObjectSet.end();
1143
1144         for (FloatingObjectSetIterator it = fromFloatingObjectSet.begin(); it != end; ++it) {
1145             FloatingObject* floatingObject = *it;
1146
1147             // Don't insert the object again if it's already in the list
1148             if (toBlock->containsFloat(floatingObject->renderer()))
1149                 continue;
1150
1151             toBlock->m_floatingObjects->add(floatingObject->clone());
1152         }
1153     }
1154 }
1155
1156 void RenderBlock::removeChild(RenderObject* oldChild)
1157 {
1158     // No need to waste time in merging or removing empty anonymous blocks.
1159     // We can just bail out if our document is getting destroyed.
1160     if (documentBeingDestroyed()) {
1161         RenderBox::removeChild(oldChild);
1162         return;
1163     }
1164
1165     // This protects against column split flows when anonymous blocks are getting merged.
1166     TemporaryChange<bool> columnFlowSplitEnabled(gColumnFlowSplitEnabled, false);
1167
1168     // If this child is a block, and if our previous and next siblings are
1169     // both anonymous blocks with inline content, then we can go ahead and
1170     // fold the inline content back together.
1171     RenderObject* prev = oldChild->previousSibling();
1172     RenderObject* next = oldChild->nextSibling();
1173     bool canMergeAnonymousBlocks = canMergeContiguousAnonymousBlocks(oldChild, prev, next);
1174     if (canMergeAnonymousBlocks && prev && next) {
1175         prev->setNeedsLayoutAndPrefWidthsRecalc();
1176         RenderBlock* nextBlock = toRenderBlock(next);
1177         RenderBlock* prevBlock = toRenderBlock(prev);
1178        
1179         if (prev->childrenInline() != next->childrenInline()) {
1180             RenderBlock* inlineChildrenBlock = prev->childrenInline() ? prevBlock : nextBlock;
1181             RenderBlock* blockChildrenBlock = prev->childrenInline() ? nextBlock : prevBlock;
1182             
1183             // Place the inline children block inside of the block children block instead of deleting it.
1184             // In order to reuse it, we have to reset it to just be a generic anonymous block.  Make sure
1185             // to clear out inherited column properties by just making a new style, and to also clear the
1186             // column span flag if it is set.
1187             ASSERT(!inlineChildrenBlock->continuation());
1188             RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(style(), BLOCK);
1189             // Cache this value as it might get changed in setStyle() call.
1190             bool inlineChildrenBlockHasLayer = inlineChildrenBlock->hasLayer();
1191             inlineChildrenBlock->setStyle(newStyle);
1192             children()->removeChildNode(this, inlineChildrenBlock, inlineChildrenBlockHasLayer);
1193             
1194             // Now just put the inlineChildrenBlock inside the blockChildrenBlock.
1195             blockChildrenBlock->children()->insertChildNode(blockChildrenBlock, inlineChildrenBlock, prev == inlineChildrenBlock ? blockChildrenBlock->firstChild() : 0,
1196                                                             inlineChildrenBlockHasLayer || blockChildrenBlock->hasLayer());
1197             next->setNeedsLayoutAndPrefWidthsRecalc();
1198             
1199             // inlineChildrenBlock got reparented to blockChildrenBlock, so it is no longer a child
1200             // of "this". we null out prev or next so that is not used later in the function.
1201             if (inlineChildrenBlock == prevBlock)
1202                 prev = 0;
1203             else
1204                 next = 0;
1205         } else {
1206             // Take all the children out of the |next| block and put them in
1207             // the |prev| block.
1208             nextBlock->moveAllChildrenIncludingFloatsTo(prevBlock, nextBlock->hasLayer() || prevBlock->hasLayer());
1209             
1210             // Delete the now-empty block's lines and nuke it.
1211             nextBlock->deleteLineBoxTree();
1212             nextBlock->destroy();
1213             next = 0;
1214         }
1215     }
1216
1217     RenderBox::removeChild(oldChild);
1218
1219     RenderObject* child = prev ? prev : next;
1220     if (canMergeAnonymousBlocks && child && !child->previousSibling() && !child->nextSibling() && canCollapseAnonymousBlockChild()) {
1221         // The removal has knocked us down to containing only a single anonymous
1222         // box.  We can go ahead and pull the content right back up into our
1223         // box.
1224         collapseAnonymousBoxChild(this, child);
1225     } else if (((prev && prev->isAnonymousBlock()) || (next && next->isAnonymousBlock())) && canCollapseAnonymousBlockChild()) {
1226         // It's possible that the removal has knocked us down to a single anonymous
1227         // block with pseudo-style element siblings (e.g. first-letter). If these
1228         // are floating, then we need to pull the content up also.
1229         RenderBlock* anonBlock = toRenderBlock((prev && prev->isAnonymousBlock()) ? prev : next);
1230         if ((anonBlock->previousSibling() || anonBlock->nextSibling())
1231             && (!anonBlock->previousSibling() || (anonBlock->previousSibling()->style()->styleType() != NOPSEUDO && anonBlock->previousSibling()->isFloating() && !anonBlock->previousSibling()->previousSibling()))
1232             && (!anonBlock->nextSibling() || (anonBlock->nextSibling()->style()->styleType() != NOPSEUDO && anonBlock->nextSibling()->isFloating() && !anonBlock->nextSibling()->nextSibling()))) {
1233             collapseAnonymousBoxChild(this, anonBlock);
1234         }
1235     }
1236
1237     if (!firstChild()) {
1238         // If this was our last child be sure to clear out our line boxes.
1239         if (childrenInline())
1240             deleteLineBoxTree();
1241
1242         // If we are an empty anonymous block in the continuation chain,
1243         // we need to remove ourself and fix the continuation chain.
1244         if (!beingDestroyed() && isAnonymousBlockContinuation() && !oldChild->isListMarker()) {
1245             RenderObject* containingBlockIgnoringAnonymous = containingBlock();
1246             while (containingBlockIgnoringAnonymous && containingBlockIgnoringAnonymous->isAnonymousBlock())
1247                 containingBlockIgnoringAnonymous = containingBlockIgnoringAnonymous->containingBlock();
1248             for (RenderObject* curr = this; curr; curr = curr->previousInPreOrder(containingBlockIgnoringAnonymous)) {
1249                 if (curr->virtualContinuation() != this)
1250                     continue;
1251
1252                 // Found our previous continuation. We just need to point it to
1253                 // |this|'s next continuation.
1254                 RenderBoxModelObject* nextContinuation = continuation();
1255                 if (curr->isRenderInline())
1256                     toRenderInline(curr)->setContinuation(nextContinuation);
1257                 else if (curr->isRenderBlock())
1258                     toRenderBlock(curr)->setContinuation(nextContinuation);
1259                 else
1260                     ASSERT_NOT_REACHED();
1261
1262                 break;
1263             }
1264             setContinuation(0);
1265             destroy();
1266         }
1267     }
1268 }
1269
1270 bool RenderBlock::isSelfCollapsingBlock() const
1271 {
1272     // We are not self-collapsing if we
1273     // (a) have a non-zero height according to layout (an optimization to avoid wasting time)
1274     // (b) are a table,
1275     // (c) have border/padding,
1276     // (d) have a min-height
1277     // (e) have specified that one of our margins can't collapse using a CSS extension
1278     if (logicalHeight() > 0
1279         || isTable() || borderAndPaddingLogicalHeight()
1280         || style()->logicalMinHeight().isPositive()
1281         || style()->marginBeforeCollapse() == MSEPARATE || style()->marginAfterCollapse() == MSEPARATE)
1282         return false;
1283
1284     Length logicalHeightLength = style()->logicalHeight();
1285     bool hasAutoHeight = logicalHeightLength.isAuto();
1286     if (logicalHeightLength.isPercent() && !document().inQuirksMode()) {
1287         hasAutoHeight = true;
1288         for (RenderBlock* cb = containingBlock(); !cb->isRenderView(); cb = cb->containingBlock()) {
1289             if (cb->style()->logicalHeight().isFixed() || cb->isTableCell())
1290                 hasAutoHeight = false;
1291         }
1292     }
1293
1294     // If the height is 0 or auto, then whether or not we are a self-collapsing block depends
1295     // on whether we have content that is all self-collapsing or not.
1296     if (hasAutoHeight || ((logicalHeightLength.isFixed() || logicalHeightLength.isPercent()) && logicalHeightLength.isZero())) {
1297         // If the block has inline children, see if we generated any line boxes.  If we have any
1298         // line boxes, then we can't be self-collapsing, since we have content.
1299         if (childrenInline())
1300             return !firstLineBox();
1301         
1302         // Whether or not we collapse is dependent on whether all our normal flow children
1303         // are also self-collapsing.
1304         for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
1305             if (child->isFloatingOrOutOfFlowPositioned())
1306                 continue;
1307             if (!child->isSelfCollapsingBlock())
1308                 return false;
1309         }
1310         return true;
1311     }
1312     return false;
1313 }
1314
1315 void RenderBlock::startDelayUpdateScrollInfo()
1316 {
1317     if (gDelayUpdateScrollInfo == 0) {
1318         ASSERT(!gDelayedUpdateScrollInfoSet);
1319         gDelayedUpdateScrollInfoSet = new DelayedUpdateScrollInfoSet;
1320     }
1321     ASSERT(gDelayedUpdateScrollInfoSet);
1322     ++gDelayUpdateScrollInfo;
1323 }
1324
1325 void RenderBlock::finishDelayUpdateScrollInfo()
1326 {
1327     --gDelayUpdateScrollInfo;
1328     ASSERT(gDelayUpdateScrollInfo >= 0);
1329     if (gDelayUpdateScrollInfo == 0) {
1330         ASSERT(gDelayedUpdateScrollInfoSet);
1331
1332         OwnPtr<DelayedUpdateScrollInfoSet> infoSet(adoptPtr(gDelayedUpdateScrollInfoSet));
1333         gDelayedUpdateScrollInfoSet = 0;
1334
1335         for (DelayedUpdateScrollInfoSet::iterator it = infoSet->begin(); it != infoSet->end(); ++it) {
1336             RenderBlock* block = *it;
1337             if (block->hasOverflowClip()) {
1338                 block->layer()->updateScrollInfoAfterLayout();
1339                 block->clearLayoutOverflow();
1340             }
1341         }
1342     }
1343 }
1344
1345 void RenderBlock::updateScrollInfoAfterLayout()
1346 {
1347     if (hasOverflowClip()) {
1348         if (style()->isFlippedBlocksWritingMode()) {
1349             // FIXME: https://bugs.webkit.org/show_bug.cgi?id=97937
1350             // Workaround for now. We cannot delay the scroll info for overflow
1351             // for items with opposite writing directions, as the contents needs
1352             // to overflow in that direction
1353             layer()->updateScrollInfoAfterLayout();
1354             return;
1355         }
1356
1357         if (gDelayUpdateScrollInfo)
1358             gDelayedUpdateScrollInfoSet->add(this);
1359         else
1360             layer()->updateScrollInfoAfterLayout();
1361     }
1362 }
1363
1364 void RenderBlock::layout()
1365 {
1366     StackStats::LayoutCheckPoint layoutCheckPoint;
1367     OverflowEventDispatcher dispatcher(this);
1368
1369     // Update our first letter info now.
1370     updateFirstLetter();
1371
1372     // Table cells call layoutBlock directly, so don't add any logic here.  Put code into
1373     // layoutBlock().
1374     layoutBlock(false);
1375     
1376     // It's safe to check for control clip here, since controls can never be table cells.
1377     // If we have a lightweight clip, there can never be any overflow from children.
1378     if (hasControlClip() && m_overflow && !gDelayUpdateScrollInfo)
1379         clearLayoutOverflow();
1380
1381     invalidateBackgroundObscurationStatus();
1382 }
1383
1384 #if ENABLE(CSS_SHAPES)
1385 void RenderBlock::relayoutShapeDescendantIfMoved(RenderBlock* child, LayoutSize offset)
1386 {
1387     LayoutUnit left = isHorizontalWritingMode() ? offset.width() : offset.height();
1388     if (!left || !child || child->shapeInsideInfo() || !layoutShapeInsideInfo())
1389         return;
1390     // Propagate layout markers only up to the child, as we are still in the middle
1391     // of a layout pass
1392     child->setNormalChildNeedsLayout(true);
1393     child->markShapeInsideDescendantsForLayout();
1394     child->layoutIfNeeded();
1395 }
1396
1397 LayoutSize RenderBlock::logicalOffsetFromShapeAncestorContainer(const RenderBlock* container) const
1398 {
1399     const RenderBlock* currentBlock = this;
1400     LayoutRect blockRect(currentBlock->borderBoxRect());
1401     while (currentBlock && !currentBlock->isRenderFlowThread() && currentBlock != container) {
1402         RenderBlock* containerBlock = currentBlock->containingBlock();
1403         ASSERT(containerBlock);
1404         if (!containerBlock)
1405             return LayoutSize();
1406
1407         if (containerBlock->style()->writingMode() != currentBlock->style()->writingMode()) {
1408             // We have to put the block rect in container coordinates
1409             // and we have to take into account both the container and current block flipping modes
1410             // Bug 118073: Flipping inline and block directions at the same time will not work,
1411             // as one of the flipped dimensions will not yet have been set to its final size
1412             if (containerBlock->style()->isFlippedBlocksWritingMode()) {
1413                 if (containerBlock->isHorizontalWritingMode())
1414                     blockRect.setY(currentBlock->height() - blockRect.maxY());
1415                 else
1416                     blockRect.setX(currentBlock->width() - blockRect.maxX());
1417             }
1418             currentBlock->flipForWritingMode(blockRect);
1419         }
1420
1421         blockRect.moveBy(currentBlock->location());
1422         currentBlock = containerBlock;
1423     }
1424
1425     LayoutSize result = isHorizontalWritingMode() ? LayoutSize(blockRect.x(), blockRect.y()) : LayoutSize(blockRect.y(), blockRect.x());
1426     return result;
1427 }
1428
1429 void RenderBlock::imageChanged(WrappedImagePtr image, const IntRect*)
1430 {
1431     RenderBox::imageChanged(image);
1432
1433     if (!parent())
1434         return;
1435
1436     ShapeValue* shapeValue = style()->shapeInside();
1437     if (shapeValue && shapeValue->image() && shapeValue->image()->data() == image) {
1438         ShapeInsideInfo* shapeInsideInfo = ensureShapeInsideInfo();
1439         shapeInsideInfo->dirtyShapeSize();
1440         markShapeInsideDescendantsForLayout();
1441     }
1442 }
1443
1444 void RenderBlock::updateShapeInsideInfoAfterStyleChange(const ShapeValue* shapeInside, const ShapeValue* oldShapeInside)
1445 {
1446     // FIXME: A future optimization would do a deep comparison for equality.
1447     if (shapeInside == oldShapeInside)
1448         return;
1449
1450     if (shapeInside) {
1451         ShapeInsideInfo* shapeInsideInfo = ensureShapeInsideInfo();
1452         shapeInsideInfo->dirtyShapeSize();
1453     } else {
1454         setShapeInsideInfo(nullptr);
1455         markShapeInsideDescendantsForLayout();
1456     }
1457 }
1458
1459 void RenderBlock::markShapeInsideDescendantsForLayout()
1460 {
1461     if (!everHadLayout())
1462         return;
1463     if (childrenInline()) {
1464         setNeedsLayout(true);
1465         return;
1466     }
1467     for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
1468         if (!child->isRenderBlock())
1469             continue;
1470         RenderBlock* childBlock = toRenderBlock(child);
1471         childBlock->markShapeInsideDescendantsForLayout();
1472     }
1473 }
1474 #endif
1475
1476 #if ENABLE(CSS_SHAPES)
1477 static inline bool shapeInfoRequiresRelayout(const RenderBlock* block)
1478 {
1479     ShapeInsideInfo* info = block->shapeInsideInfo();
1480     if (info)
1481         info->setNeedsLayout(info->shapeSizeDirty());
1482     else
1483         info = block->layoutShapeInsideInfo();
1484     return info && info->needsLayout();
1485 }
1486 #endif
1487
1488 bool RenderBlock::updateShapesBeforeBlockLayout()
1489 {
1490 #if ENABLE(CSS_SHAPES)
1491     if (!flowThreadContainingBlock() && !shapeInsideInfo())
1492         return shapeInfoRequiresRelayout(this);
1493
1494     LayoutUnit oldHeight = logicalHeight();
1495     LayoutUnit oldTop = logicalTop();
1496
1497     // Compute the maximum logical height content may cause this block to expand to
1498     // FIXME: These should eventually use the const computeLogicalHeight rather than updateLogicalHeight
1499     setLogicalHeight(RenderFlowThread::maxLogicalHeight());
1500     updateLogicalHeight();
1501
1502     computeShapeSize();
1503
1504     setLogicalHeight(oldHeight);
1505     setLogicalTop(oldTop);
1506
1507     return shapeInfoRequiresRelayout(this);
1508 #else
1509     return false;
1510 #endif
1511 }
1512
1513 #if ENABLE(CSS_SHAPES)
1514 void RenderBlock::computeShapeSize()
1515 {
1516     ShapeInsideInfo* shapeInsideInfo = this->shapeInsideInfo();
1517     if (shapeInsideInfo) {
1518         bool percentageLogicalHeightResolvable = percentageLogicalHeightIsResolvableFromBlock(this, false);
1519         shapeInsideInfo->setShapeSize(logicalWidth(), percentageLogicalHeightResolvable ? logicalHeight() : LayoutUnit());
1520     }
1521 }
1522 #endif
1523
1524 void RenderBlock::updateShapesAfterBlockLayout(bool heightChanged)
1525 {
1526 #if ENABLE(CSS_SHAPES)
1527     // A previous sibling has changed dimension, so we need to relayout the shape with the content
1528     ShapeInsideInfo* shapeInsideInfo = layoutShapeInsideInfo();
1529     if (heightChanged && shapeInsideInfo)
1530         shapeInsideInfo->dirtyShapeSize();
1531 #else
1532     UNUSED_PARAM(heightChanged);
1533 #endif
1534 }
1535
1536 bool RenderBlock::updateLogicalWidthAndColumnWidth()
1537 {
1538     LayoutUnit oldWidth = logicalWidth();
1539     LayoutUnit oldColumnWidth = desiredColumnWidth();
1540
1541     updateLogicalWidth();
1542     calcColumnWidth();
1543
1544     bool hasBorderOrPaddingLogicalWidthChanged = m_hasBorderOrPaddingLogicalWidthChanged;
1545     m_hasBorderOrPaddingLogicalWidthChanged = false;
1546
1547     return oldWidth != logicalWidth() || oldColumnWidth != desiredColumnWidth() || hasBorderOrPaddingLogicalWidthChanged;
1548 }
1549
1550 void RenderBlock::checkForPaginationLogicalHeightChange(LayoutUnit& pageLogicalHeight, bool& pageLogicalHeightChanged, bool& hasSpecifiedPageLogicalHeight)
1551 {
1552     ColumnInfo* colInfo = columnInfo();
1553     if (hasColumns()) {
1554         if (!pageLogicalHeight) {
1555             // We need to go ahead and set our explicit page height if one exists, so that we can
1556             // avoid doing two layout passes.
1557             updateLogicalHeight();
1558             LayoutUnit columnHeight = isRenderView() ? view().pageOrViewLogicalHeight() : contentLogicalHeight();
1559             if (columnHeight > 0) {
1560                 pageLogicalHeight = columnHeight;
1561                 hasSpecifiedPageLogicalHeight = true;
1562             }
1563             setLogicalHeight(0);
1564         }
1565
1566         if (colInfo->columnHeight() != pageLogicalHeight && everHadLayout())
1567             pageLogicalHeightChanged = true;
1568
1569         colInfo->setColumnHeight(pageLogicalHeight);
1570         
1571         if (!hasSpecifiedPageLogicalHeight && !pageLogicalHeight)
1572             colInfo->clearForcedBreaks();
1573
1574         colInfo->setPaginationUnit(paginationUnit());
1575     } else if (isRenderFlowThread()) {
1576         pageLogicalHeight = 1; // This is just a hack to always make sure we have a page logical height.
1577         pageLogicalHeightChanged = toRenderFlowThread(this)->pageLogicalSizeChanged();
1578     }
1579 }
1580
1581 bool RenderBlock::relayoutToAvoidWidows(LayoutStateMaintainer& statePusher)
1582 {
1583     if (!shouldBreakAtLineToAvoidWidow())
1584         return false;
1585
1586     statePusher.pop();
1587     setEverHadLayout(true);
1588     layoutBlock(false);
1589     return true;
1590 }
1591
1592 void RenderBlock::layoutBlock(bool, LayoutUnit)
1593 {
1594     ASSERT_NOT_REACHED();
1595     setNeedsLayout(false);
1596 }
1597
1598 void RenderBlock::addOverflowFromChildren()
1599 {
1600     if (!hasColumns()) {
1601         if (childrenInline())
1602             addOverflowFromInlineChildren();
1603         else
1604             addOverflowFromBlockChildren();
1605     } else {
1606         ColumnInfo* colInfo = columnInfo();
1607         if (columnCount(colInfo)) {
1608             LayoutRect lastRect = columnRectAt(colInfo, columnCount(colInfo) - 1);
1609             addLayoutOverflow(lastRect);
1610             if (!hasOverflowClip())
1611                 addVisualOverflow(lastRect);
1612         }
1613     }
1614 }
1615
1616 void RenderBlock::computeOverflow(LayoutUnit oldClientAfterEdge, bool recomputeFloats)
1617 {
1618     clearOverflow();
1619
1620     // Add overflow from children.
1621     addOverflowFromChildren();
1622
1623     if (!hasColumns() && (recomputeFloats || isRoot() || expandsToEncloseOverhangingFloats() || hasSelfPaintingLayer()))
1624         addOverflowFromFloats();
1625
1626     // Add in the overflow from positioned objects.
1627     addOverflowFromPositionedObjects();
1628
1629     if (hasOverflowClip()) {
1630         // When we have overflow clip, propagate the original spillout since it will include collapsed bottom margins
1631         // and bottom padding.  Set the axis we don't care about to be 1, since we want this overflow to always
1632         // be considered reachable.
1633         LayoutRect clientRect(clientBoxRect());
1634         LayoutRect rectToApply;
1635         if (isHorizontalWritingMode())
1636             rectToApply = LayoutRect(clientRect.x(), clientRect.y(), 1, max<LayoutUnit>(0, oldClientAfterEdge - clientRect.y()));
1637         else
1638             rectToApply = LayoutRect(clientRect.x(), clientRect.y(), max<LayoutUnit>(0, oldClientAfterEdge - clientRect.x()), 1);
1639         addLayoutOverflow(rectToApply);
1640         if (hasRenderOverflow())
1641             m_overflow->setLayoutClientAfterEdge(oldClientAfterEdge);
1642     }
1643         
1644     // Add visual overflow from box-shadow and border-image-outset.
1645     addVisualEffectOverflow();
1646
1647     // Add visual overflow from theme.
1648     addVisualOverflowFromTheme();
1649
1650     if (isRenderNamedFlowThread())
1651         toRenderNamedFlowThread(this)->computeOversetStateForRegions(oldClientAfterEdge);
1652 }
1653
1654 void RenderBlock::clearLayoutOverflow()
1655 {
1656     if (!m_overflow)
1657         return;
1658     
1659     if (visualOverflowRect() == borderBoxRect()) {
1660         // FIXME: Implement complete solution for regions overflow.
1661         clearOverflow();
1662         return;
1663     }
1664     
1665     m_overflow->setLayoutOverflow(borderBoxRect());
1666 }
1667
1668 void RenderBlock::addOverflowFromBlockChildren()
1669 {
1670     for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
1671         if (!child->isFloatingOrOutOfFlowPositioned())
1672             addOverflowFromChild(child);
1673     }
1674 }
1675
1676 void RenderBlock::addOverflowFromFloats()
1677 {
1678     if (!m_floatingObjects)
1679         return;
1680
1681     const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1682     FloatingObjectSetIterator end = floatingObjectSet.end();
1683     for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
1684         FloatingObject* r = *it;
1685         if (r->isDescendant())
1686             addOverflowFromChild(r->renderer(), IntSize(xPositionForFloatIncludingMargin(r), yPositionForFloatIncludingMargin(r)));
1687     }
1688 }
1689
1690 void RenderBlock::addOverflowFromPositionedObjects()
1691 {
1692     TrackedRendererListHashSet* positionedDescendants = positionedObjects();
1693     if (!positionedDescendants)
1694         return;
1695
1696     RenderBox* positionedObject;
1697     TrackedRendererListHashSet::iterator end = positionedDescendants->end();
1698     for (TrackedRendererListHashSet::iterator it = positionedDescendants->begin(); it != end; ++it) {
1699         positionedObject = *it;
1700         
1701         // Fixed positioned elements don't contribute to layout overflow, since they don't scroll with the content.
1702         if (positionedObject->style()->position() != FixedPosition) {
1703             LayoutUnit x = positionedObject->x();
1704             if (style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
1705                 x -= verticalScrollbarWidth();
1706             addOverflowFromChild(positionedObject, LayoutSize(x, positionedObject->y()));
1707         }
1708     }
1709 }
1710
1711 void RenderBlock::addVisualOverflowFromTheme()
1712 {
1713     if (!style()->hasAppearance())
1714         return;
1715
1716     IntRect inflatedRect = pixelSnappedBorderBoxRect();
1717     theme()->adjustRepaintRect(this, inflatedRect);
1718     addVisualOverflow(inflatedRect);
1719
1720     if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1721         flowThread->addRegionsVisualOverflowFromTheme(this);
1722 }
1723
1724 bool RenderBlock::expandsToEncloseOverhangingFloats() const
1725 {
1726     return isInlineBlockOrInlineTable() || isFloatingOrOutOfFlowPositioned() || hasOverflowClip() || (parent() && parent()->isFlexibleBoxIncludingDeprecated())
1727            || hasColumns() || isTableCell() || isTableCaption() || isFieldset() || isWritingModeRoot() || isRoot();
1728 }
1729
1730 static void destroyRunIn(RenderBoxModelObject* runIn)
1731 {
1732     ASSERT(runIn->isRunIn());
1733     ASSERT(!runIn->firstChild());
1734
1735     // Delete our line box tree. This is needed as our children got moved
1736     // and our line box tree is no longer valid.
1737     if (runIn->isRenderBlock())
1738         toRenderBlock(runIn)->deleteLineBoxTree();
1739     else if (runIn->isRenderInline())
1740         toRenderInline(runIn)->deleteLineBoxTree();
1741     else
1742         ASSERT_NOT_REACHED();
1743
1744     runIn->destroy();
1745 }
1746
1747 void RenderBlock::placeRunInIfNeeded(RenderObject* newChild)
1748 {
1749     if (newChild->isRunIn())
1750         moveRunInUnderSiblingBlockIfNeeded(newChild);
1751     else if (RenderObject* prevSibling = newChild->previousSibling()) {
1752         if (prevSibling->isRunIn())
1753             moveRunInUnderSiblingBlockIfNeeded(prevSibling);
1754     }
1755 }
1756
1757 RenderBoxModelObject* RenderBlock::createReplacementRunIn(RenderBoxModelObject* runIn)
1758 {
1759     ASSERT(runIn->isRunIn());
1760     ASSERT(runIn->element());
1761
1762     RenderBoxModelObject* newRunIn = 0;
1763     if (!runIn->isRenderBlockFlow())
1764         newRunIn = new (renderArena()) RenderBlockFlow(runIn->element());
1765     else
1766         newRunIn = new (renderArena()) RenderInline(runIn->element());
1767
1768     runIn->element()->setRenderer(newRunIn);
1769     newRunIn->setStyle(runIn->style());
1770
1771     runIn->moveAllChildrenTo(newRunIn, true);
1772
1773     return newRunIn;
1774 }
1775
1776 void RenderBlock::moveRunInUnderSiblingBlockIfNeeded(RenderObject* runIn)
1777 {
1778     ASSERT(runIn->isRunIn());
1779
1780     // See if we have inline children. If the children aren't inline,
1781     // then just treat the run-in as a normal block.
1782     if (!runIn->childrenInline())
1783         return;
1784
1785     // FIXME: We don't handle non-block elements with run-in for now.
1786     if (!runIn->isRenderBlockFlow())
1787         return;
1788
1789     // FIXME: We don't support run-ins with or as part of a continuation
1790     // as it makes the back-and-forth placing complex.
1791     if (runIn->isElementContinuation() || runIn->virtualContinuation())
1792         return;
1793
1794     // Check if this node is allowed to run-in. E.g. <select> expects its renderer to
1795     // be a RenderListBox or RenderMenuList, and hence cannot be a RenderInline run-in.
1796     if (!runIn->canBeReplacedWithInlineRunIn())
1797         return;
1798
1799     RenderObject* curr = runIn->nextSibling();
1800     if (!curr || !curr->isRenderBlock() || !curr->childrenInline())
1801         return;
1802
1803     if (toRenderBlock(curr)->beingDestroyed())
1804         return;
1805
1806     // Per CSS3, "A run-in cannot run in to a block that already starts with a
1807     // run-in or that itself is a run-in".
1808     if (curr->isRunIn() || (curr->firstChild() && curr->firstChild()->isRunIn()))
1809         return;
1810
1811     if (curr->isAnonymous() || curr->isFloatingOrOutOfFlowPositioned())
1812         return;
1813
1814     RenderBoxModelObject* oldRunIn = toRenderBoxModelObject(runIn);
1815     RenderBoxModelObject* newRunIn = createReplacementRunIn(oldRunIn);
1816     destroyRunIn(oldRunIn);
1817
1818     // Now insert the new child under |curr| block. Use addChild instead of insertChildNode
1819     // since it handles correct placement of the children, especially where we cannot insert
1820     // anything before the first child. e.g. details tag. See https://bugs.webkit.org/show_bug.cgi?id=58228.
1821     curr->addChild(newRunIn, curr->firstChild());
1822
1823     // Make sure that |this| get a layout since its run-in child moved.
1824     curr->setNeedsLayoutAndPrefWidthsRecalc();
1825 }
1826
1827 bool RenderBlock::runInIsPlacedIntoSiblingBlock(RenderObject* runIn)
1828 {
1829     ASSERT(runIn->isRunIn());
1830
1831     // If we don't have a parent, we can't be moved into our sibling block.
1832     if (!parent())
1833         return false;
1834
1835     // An intruded run-in needs to be an inline.
1836     if (!runIn->isRenderInline())
1837         return false;
1838
1839     return true;
1840 }
1841
1842 void RenderBlock::moveRunInToOriginalPosition(RenderObject* runIn)
1843 {
1844     ASSERT(runIn->isRunIn());
1845
1846     if (!runInIsPlacedIntoSiblingBlock(runIn))
1847         return;
1848
1849     // FIXME: Run-in that are now placed in sibling block can break up into continuation
1850     // chains when new children are added to it. We cannot easily send them back to their
1851     // original place since that requires writing integration logic with RenderInline::addChild
1852     // and all other places that might cause continuations to be created (without blowing away
1853     // |this|). Disabling this feature for now to prevent crashes.
1854     if (runIn->isElementContinuation() || runIn->virtualContinuation())
1855         return;
1856
1857     RenderBoxModelObject* oldRunIn = toRenderBoxModelObject(runIn);
1858     RenderBoxModelObject* newRunIn = createReplacementRunIn(oldRunIn);
1859     destroyRunIn(oldRunIn);
1860
1861     // Add the run-in block as our previous sibling.
1862     parent()->addChild(newRunIn, this);
1863
1864     // Make sure that the parent holding the new run-in gets layout.
1865     parent()->setNeedsLayoutAndPrefWidthsRecalc();
1866 }
1867
1868 LayoutUnit RenderBlock::computeStartPositionDeltaForChildAvoidingFloats(const RenderBox* child, LayoutUnit childMarginStart, RenderRegion* region)
1869 {
1870     LayoutUnit startPosition = startOffsetForContent(region);
1871
1872     // Add in our start margin.
1873     LayoutUnit oldPosition = startPosition + childMarginStart;
1874     LayoutUnit newPosition = oldPosition;
1875
1876     LayoutUnit blockOffset = logicalTopForChild(child);
1877     if (region)
1878         blockOffset = max(blockOffset, blockOffset + (region->logicalTopForFlowThreadContent() - offsetFromLogicalTopOfFirstPage()));
1879
1880     LayoutUnit startOff = startOffsetForLine(blockOffset, false, region, logicalHeightForChild(child));
1881
1882     if (style()->textAlign() != WEBKIT_CENTER && !child->style()->marginStartUsing(style()).isAuto()) {
1883         if (childMarginStart < 0)
1884             startOff += childMarginStart;
1885         newPosition = max(newPosition, startOff); // Let the float sit in the child's margin if it can fit.
1886     } else if (startOff != startPosition)
1887         newPosition = startOff + childMarginStart;
1888
1889     return newPosition - oldPosition;
1890 }
1891
1892 void RenderBlock::determineLogicalLeftPositionForChild(RenderBox* child, ApplyLayoutDeltaMode applyDelta)
1893 {
1894     LayoutUnit startPosition = borderStart() + paddingStart();
1895     if (style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
1896         startPosition -= verticalScrollbarWidth();
1897     LayoutUnit totalAvailableLogicalWidth = borderAndPaddingLogicalWidth() + availableLogicalWidth();
1898
1899     // Add in our start margin.
1900     LayoutUnit childMarginStart = marginStartForChild(child);
1901     LayoutUnit newPosition = startPosition + childMarginStart;
1902         
1903     // Some objects (e.g., tables, horizontal rules, overflow:auto blocks) avoid floats.  They need
1904     // to shift over as necessary to dodge any floats that might get in the way.
1905     if (child->avoidsFloats() && containsFloats() && !flowThreadContainingBlock())
1906         newPosition += computeStartPositionDeltaForChildAvoidingFloats(child, marginStartForChild(child));
1907
1908     setLogicalLeftForChild(child, style()->isLeftToRightDirection() ? newPosition : totalAvailableLogicalWidth - newPosition - logicalWidthForChild(child), applyDelta);
1909 }
1910
1911 void RenderBlock::setLogicalLeftForChild(RenderBox* child, LayoutUnit logicalLeft, ApplyLayoutDeltaMode applyDelta)
1912 {
1913     if (isHorizontalWritingMode()) {
1914         if (applyDelta == ApplyLayoutDelta)
1915             view().addLayoutDelta(LayoutSize(child->x() - logicalLeft, 0));
1916         child->setX(logicalLeft);
1917     } else {
1918         if (applyDelta == ApplyLayoutDelta)
1919             view().addLayoutDelta(LayoutSize(0, child->y() - logicalLeft));
1920         child->setY(logicalLeft);
1921     }
1922 }
1923
1924 void RenderBlock::setLogicalTopForChild(RenderBox* child, LayoutUnit logicalTop, ApplyLayoutDeltaMode applyDelta)
1925 {
1926     if (isHorizontalWritingMode()) {
1927         if (applyDelta == ApplyLayoutDelta)
1928             view().addLayoutDelta(LayoutSize(0, child->y() - logicalTop));
1929         child->setY(logicalTop);
1930     } else {
1931         if (applyDelta == ApplyLayoutDelta)
1932             view().addLayoutDelta(LayoutSize(child->x() - logicalTop, 0));
1933         child->setX(logicalTop);
1934     }
1935 }
1936
1937 void RenderBlock::updateBlockChildDirtyBitsBeforeLayout(bool relayoutChildren, RenderBox* child)
1938 {
1939     // FIXME: Technically percentage height objects only need a relayout if their percentage isn't going to be turned into
1940     // an auto value. Add a method to determine this, so that we can avoid the relayout.
1941     if (relayoutChildren || (child->hasRelativeLogicalHeight() && !isRenderView()) || child->hasViewportPercentageLogicalHeight())
1942         child->setChildNeedsLayout(true, MarkOnlyThis);
1943
1944     // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
1945     if (relayoutChildren && child->needsPreferredWidthsRecalculation())
1946         child->setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
1947 }
1948
1949 void RenderBlock::dirtyForLayoutFromPercentageHeightDescendants()
1950 {
1951     if (!gPercentHeightDescendantsMap)
1952         return;
1953
1954     TrackedRendererListHashSet* descendants = gPercentHeightDescendantsMap->get(this);
1955     if (!descendants)
1956         return;
1957     
1958     TrackedRendererListHashSet::iterator end = descendants->end();
1959     for (TrackedRendererListHashSet::iterator it = descendants->begin(); it != end; ++it) {
1960         RenderBox* box = *it;
1961         while (box != this) {
1962             if (box->normalChildNeedsLayout())
1963                 break;
1964             box->setChildNeedsLayout(true, MarkOnlyThis);
1965             
1966             // If the width of an image is affected by the height of a child (e.g., an image with an aspect ratio),
1967             // then we have to dirty preferred widths, since even enclosing blocks can become dirty as a result.
1968             // (A horizontal flexbox that contains an inline image wrapped in an anonymous block for example.)
1969             if (box->hasAspectRatio()) 
1970                 box->setPreferredLogicalWidthsDirty(true);
1971             
1972             box = box->containingBlock();
1973             ASSERT(box);
1974             if (!box)
1975                 break;
1976         }
1977     }
1978 }
1979
1980 void RenderBlock::simplifiedNormalFlowLayout()
1981 {
1982     if (childrenInline()) {
1983         ListHashSet<RootInlineBox*> lineBoxes;
1984         for (InlineWalker walker(this); !walker.atEnd(); walker.advance()) {
1985             RenderObject* o = walker.current();
1986             if (!o->isOutOfFlowPositioned() && (o->isReplaced() || o->isFloating())) {
1987                 o->layoutIfNeeded();
1988                 if (toRenderBox(o)->inlineBoxWrapper())
1989                     lineBoxes.add(&toRenderBox(o)->inlineBoxWrapper()->root());
1990             } else if (o->isText() || (o->isRenderInline() && !walker.atEndOfInline()))
1991                 o->setNeedsLayout(false);
1992         }
1993
1994         // FIXME: Glyph overflow will get lost in this case, but not really a big deal.
1995         // FIXME: Find a way to invalidate the knownToHaveNoOverflow flag on the InlineBoxes.
1996         GlyphOverflowAndFallbackFontsMap textBoxDataMap;                  
1997         for (ListHashSet<RootInlineBox*>::const_iterator it = lineBoxes.begin(); it != lineBoxes.end(); ++it) {
1998             RootInlineBox* box = *it;
1999             box->computeOverflow(box->lineTop(), box->lineBottom(), textBoxDataMap);
2000         }
2001     } else {
2002         for (RenderBox* box = firstChildBox(); box; box = box->nextSiblingBox()) {
2003             if (!box->isOutOfFlowPositioned())
2004                 box->layoutIfNeeded();
2005         }
2006     }
2007 }
2008
2009 bool RenderBlock::simplifiedLayout()
2010 {
2011     if ((!posChildNeedsLayout() && !needsSimplifiedNormalFlowLayout()) || normalChildNeedsLayout() || selfNeedsLayout())
2012         return false;
2013
2014     LayoutStateMaintainer statePusher(&view(), this, locationOffset(), hasColumns() || hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
2015     
2016     if (needsPositionedMovementLayout() && !tryLayoutDoingPositionedMovementOnly())
2017         return false;
2018
2019     // Lay out positioned descendants or objects that just need to recompute overflow.
2020     if (needsSimplifiedNormalFlowLayout())
2021         simplifiedNormalFlowLayout();
2022
2023     // Make sure a forced break is applied after the content if we are a flow thread in a simplified layout.
2024     // This ensures the size information is correctly computed for the last auto-height region receiving content.
2025     if (isRenderFlowThread())
2026         toRenderFlowThread(this)->applyBreakAfterContent(clientLogicalBottom());
2027
2028     // Lay out our positioned objects if our positioned child bit is set.
2029     // Also, if an absolute position element inside a relative positioned container moves, and the absolute element has a fixed position
2030     // child, neither the fixed element nor its container learn of the movement since posChildNeedsLayout() is only marked as far as the 
2031     // relative positioned container. So if we can have fixed pos objects in our positioned objects list check if any of them
2032     // are statically positioned and thus need to move with their absolute ancestors.
2033     bool canContainFixedPosObjects = canContainFixedPositionObjects();
2034     if (posChildNeedsLayout() || canContainFixedPosObjects)
2035         layoutPositionedObjects(false, !posChildNeedsLayout() && canContainFixedPosObjects);
2036
2037     // Recompute our overflow information.
2038     // FIXME: We could do better here by computing a temporary overflow object from layoutPositionedObjects and only
2039     // updating our overflow if we either used to have overflow or if the new temporary object has overflow.
2040     // For now just always recompute overflow.  This is no worse performance-wise than the old code that called rightmostPosition and
2041     // lowestPosition on every relayout so it's not a regression.
2042     // computeOverflow expects the bottom edge before we clamp our height. Since this information isn't available during
2043     // simplifiedLayout, we cache the value in m_overflow.
2044     LayoutUnit oldClientAfterEdge = hasRenderOverflow() ? m_overflow->layoutClientAfterEdge() : clientLogicalBottom();
2045     computeOverflow(oldClientAfterEdge, true);
2046
2047     statePusher.pop();
2048     
2049     updateLayerTransform();
2050
2051     updateScrollInfoAfterLayout();
2052
2053     setNeedsLayout(false);
2054     return true;
2055 }
2056
2057 void RenderBlock::markFixedPositionObjectForLayoutIfNeeded(RenderObject* child)
2058 {
2059     if (child->style()->position() != FixedPosition)
2060         return;
2061
2062     bool hasStaticBlockPosition = child->style()->hasStaticBlockPosition(isHorizontalWritingMode());
2063     bool hasStaticInlinePosition = child->style()->hasStaticInlinePosition(isHorizontalWritingMode());
2064     if (!hasStaticBlockPosition && !hasStaticInlinePosition)
2065         return;
2066
2067     RenderObject* o = child->parent();
2068     while (o && !o->isRenderView() && o->style()->position() != AbsolutePosition)
2069         o = o->parent();
2070     if (o->style()->position() != AbsolutePosition)
2071         return;
2072
2073     RenderBox* box = toRenderBox(child);
2074     if (hasStaticInlinePosition) {
2075         LogicalExtentComputedValues computedValues;
2076         box->computeLogicalWidthInRegion(computedValues);
2077         LayoutUnit newLeft = computedValues.m_position;
2078         if (newLeft != box->logicalLeft())
2079             child->setChildNeedsLayout(true, MarkOnlyThis);
2080     } else if (hasStaticBlockPosition) {
2081         LayoutUnit oldTop = box->logicalTop();
2082         box->updateLogicalHeight();
2083         if (box->logicalTop() != oldTop)
2084             child->setChildNeedsLayout(true, MarkOnlyThis);
2085     }
2086 }
2087
2088 void RenderBlock::layoutPositionedObjects(bool relayoutChildren, bool fixedPositionObjectsOnly)
2089 {
2090     TrackedRendererListHashSet* positionedDescendants = positionedObjects();
2091     if (!positionedDescendants)
2092         return;
2093         
2094     if (hasColumns())
2095         view().layoutState()->clearPaginationInformation(); // Positioned objects are not part of the column flow, so they don't paginate with the columns.
2096
2097     RenderBox* r;
2098     TrackedRendererListHashSet::iterator end = positionedDescendants->end();
2099     for (TrackedRendererListHashSet::iterator it = positionedDescendants->begin(); it != end; ++it) {
2100         r = *it;
2101         
2102         estimateRegionRangeForBoxChild(r);
2103
2104         // A fixed position element with an absolute positioned ancestor has no way of knowing if the latter has changed position. So
2105         // if this is a fixed position element, mark it for layout if it has an abspos ancestor and needs to move with that ancestor, i.e. 
2106         // it has static position.
2107         markFixedPositionObjectForLayoutIfNeeded(r);
2108         if (fixedPositionObjectsOnly) {
2109             r->layoutIfNeeded();
2110             continue;
2111         }
2112
2113         // When a non-positioned block element moves, it may have positioned children that are implicitly positioned relative to the
2114         // non-positioned block.  Rather than trying to detect all of these movement cases, we just always lay out positioned
2115         // objects that are positioned implicitly like this.  Such objects are rare, and so in typical DHTML menu usage (where everything is
2116         // positioned explicitly) this should not incur a performance penalty.
2117         if (relayoutChildren || (r->style()->hasStaticBlockPosition(isHorizontalWritingMode()) && r->parent() != this))
2118             r->setChildNeedsLayout(true, MarkOnlyThis);
2119             
2120         // If relayoutChildren is set and the child has percentage padding or an embedded content box, we also need to invalidate the childs pref widths.
2121         if (relayoutChildren && r->needsPreferredWidthsRecalculation())
2122             r->setPreferredLogicalWidthsDirty(true, MarkOnlyThis);
2123         
2124         if (!r->needsLayout())
2125             r->markForPaginationRelayoutIfNeeded();
2126         
2127         // We don't have to do a full layout.  We just have to update our position. Try that first. If we have shrink-to-fit width
2128         // and we hit the available width constraint, the layoutIfNeeded() will catch it and do a full layout.
2129         if (r->needsPositionedMovementLayoutOnly() && r->tryLayoutDoingPositionedMovementOnly())
2130             r->setNeedsLayout(false);
2131             
2132         // If we are paginated or in a line grid, go ahead and compute a vertical position for our object now.
2133         // If it's wrong we'll lay out again.
2134         LayoutUnit oldLogicalTop = 0;
2135         bool needsBlockDirectionLocationSetBeforeLayout = r->needsLayout() && view().layoutState()->needsBlockDirectionLocationSetBeforeLayout();
2136         if (needsBlockDirectionLocationSetBeforeLayout) {
2137             if (isHorizontalWritingMode() == r->isHorizontalWritingMode())
2138                 r->updateLogicalHeight();
2139             else
2140                 r->updateLogicalWidth();
2141             oldLogicalTop = logicalTopForChild(r);
2142         }
2143
2144         r->layoutIfNeeded();
2145
2146         // Lay out again if our estimate was wrong.
2147         if (needsBlockDirectionLocationSetBeforeLayout && logicalTopForChild(r) != oldLogicalTop) {
2148             r->setChildNeedsLayout(true, MarkOnlyThis);
2149             r->layoutIfNeeded();
2150         }
2151
2152         if (updateRegionRangeForBoxChild(r)) {
2153             r->setNeedsLayout(true, MarkOnlyThis);
2154             r->layoutIfNeeded();
2155         }
2156     }
2157     
2158     if (hasColumns())
2159         view().layoutState()->m_columnInfo = columnInfo(); // FIXME: Kind of gross. We just put this back into the layout state so that pop() will work.
2160 }
2161
2162 void RenderBlock::markPositionedObjectsForLayout()
2163 {
2164     TrackedRendererListHashSet* positionedDescendants = positionedObjects();
2165     if (positionedDescendants) {
2166         RenderBox* r;
2167         TrackedRendererListHashSet::iterator end = positionedDescendants->end();
2168         for (TrackedRendererListHashSet::iterator it = positionedDescendants->begin(); it != end; ++it) {
2169             r = *it;
2170             r->setChildNeedsLayout(true);
2171         }
2172     }
2173 }
2174
2175 void RenderBlock::markForPaginationRelayoutIfNeeded()
2176 {
2177     ASSERT(!needsLayout());
2178     if (needsLayout())
2179         return;
2180
2181     if (view().layoutState()->pageLogicalHeightChanged() || (view().layoutState()->pageLogicalHeight() && view().layoutState()->pageLogicalOffset(this, logicalTop()) != pageLogicalOffset()))
2182         setChildNeedsLayout(true, MarkOnlyThis);
2183 }
2184
2185 void RenderBlock::repaintOverhangingFloats(bool paintAllDescendants)
2186 {
2187     // Repaint any overhanging floats (if we know we're the one to paint them).
2188     // Otherwise, bail out.
2189     if (!hasOverhangingFloats())
2190         return;
2191
2192     // FIXME: Avoid disabling LayoutState. At the very least, don't disable it for floats originating
2193     // in this block. Better yet would be to push extra state for the containers of other floats.
2194     LayoutStateDisabler layoutStateDisabler(&view());
2195     const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2196     FloatingObjectSetIterator end = floatingObjectSet.end();
2197     for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
2198         FloatingObject* r = *it;
2199         // Only repaint the object if it is overhanging, is not in its own layer, and
2200         // is our responsibility to paint (m_shouldPaint is set). When paintAllDescendants is true, the latter
2201         // condition is replaced with being a descendant of us.
2202         if (r->logicalBottom(isHorizontalWritingMode()) > logicalHeight()
2203             && !r->renderer()->hasSelfPaintingLayer()
2204             && (r->shouldPaint() || (paintAllDescendants && r->renderer()->isDescendantOf(this)))) {
2205             r->renderer()->repaint();
2206             r->renderer()->repaintOverhangingFloats(false);
2207         }
2208     }
2209 }
2210  
2211 void RenderBlock::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
2212 {
2213     LayoutPoint adjustedPaintOffset = paintOffset + location();
2214     
2215     PaintPhase phase = paintInfo.phase;
2216
2217     // Check if we need to do anything at all.
2218     // FIXME: Could eliminate the isRoot() check if we fix background painting so that the RenderView
2219     // paints the root's background.
2220     if (!isRoot()) {
2221         LayoutRect overflowBox = overflowRectForPaintRejection();
2222         flipForWritingMode(overflowBox);
2223         overflowBox.inflate(maximalOutlineSize(paintInfo.phase));
2224         overflowBox.moveBy(adjustedPaintOffset);
2225         if (!overflowBox.intersects(paintInfo.rect))
2226             return;
2227     }
2228
2229     bool pushedClip = pushContentsClip(paintInfo, adjustedPaintOffset);
2230     paintObject(paintInfo, adjustedPaintOffset);
2231     if (pushedClip)
2232         popContentsClip(paintInfo, phase, adjustedPaintOffset);
2233
2234     // Our scrollbar widgets paint exactly when we tell them to, so that they work properly with
2235     // z-index.  We paint after we painted the background/border, so that the scrollbars will
2236     // sit above the background/border.
2237     if (hasOverflowClip() && style()->visibility() == VISIBLE && (phase == PaintPhaseBlockBackground || phase == PaintPhaseChildBlockBackground) && paintInfo.shouldPaintWithinRoot(this) && !paintInfo.paintRootBackgroundOnly())
2238         layer()->paintOverflowControls(paintInfo.context, roundedIntPoint(adjustedPaintOffset), paintInfo.rect);
2239 }
2240
2241 void RenderBlock::paintColumnRules(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
2242 {
2243     if (paintInfo.context->paintingDisabled())
2244         return;
2245
2246     const Color& ruleColor = style()->visitedDependentColor(CSSPropertyWebkitColumnRuleColor);
2247     bool ruleTransparent = style()->columnRuleIsTransparent();
2248     EBorderStyle ruleStyle = style()->columnRuleStyle();
2249     LayoutUnit ruleThickness = style()->columnRuleWidth();
2250     LayoutUnit colGap = columnGap();
2251     bool renderRule = ruleStyle > BHIDDEN && !ruleTransparent;
2252     if (!renderRule)
2253         return;
2254
2255     ColumnInfo* colInfo = columnInfo();
2256     unsigned colCount = columnCount(colInfo);
2257
2258     bool antialias = shouldAntialiasLines(paintInfo.context);
2259
2260     if (colInfo->progressionAxis() == ColumnInfo::InlineAxis) {
2261         bool leftToRight = style()->isLeftToRightDirection() ^ colInfo->progressionIsReversed();
2262         LayoutUnit currLogicalLeftOffset = leftToRight ? LayoutUnit() : contentLogicalWidth();
2263         LayoutUnit ruleAdd = logicalLeftOffsetForContent();
2264         LayoutUnit ruleLogicalLeft = leftToRight ? LayoutUnit() : contentLogicalWidth();
2265         LayoutUnit inlineDirectionSize = colInfo->desiredColumnWidth();
2266         BoxSide boxSide = isHorizontalWritingMode()
2267             ? leftToRight ? BSLeft : BSRight
2268             : leftToRight ? BSTop : BSBottom;
2269
2270         for (unsigned i = 0; i < colCount; i++) {
2271             // Move to the next position.
2272             if (leftToRight) {
2273                 ruleLogicalLeft += inlineDirectionSize + colGap / 2;
2274                 currLogicalLeftOffset += inlineDirectionSize + colGap;
2275             } else {
2276                 ruleLogicalLeft -= (inlineDirectionSize + colGap / 2);
2277                 currLogicalLeftOffset -= (inlineDirectionSize + colGap);
2278             }
2279            
2280             // Now paint the column rule.
2281             if (i < colCount - 1) {
2282                 LayoutUnit ruleLeft = isHorizontalWritingMode() ? paintOffset.x() + ruleLogicalLeft - ruleThickness / 2 + ruleAdd : paintOffset.x() + borderLeft() + paddingLeft();
2283                 LayoutUnit ruleRight = isHorizontalWritingMode() ? ruleLeft + ruleThickness : ruleLeft + contentWidth();
2284                 LayoutUnit ruleTop = isHorizontalWritingMode() ? paintOffset.y() + borderTop() + paddingTop() : paintOffset.y() + ruleLogicalLeft - ruleThickness / 2 + ruleAdd;
2285                 LayoutUnit ruleBottom = isHorizontalWritingMode() ? ruleTop + contentHeight() : ruleTop + ruleThickness;
2286                 IntRect pixelSnappedRuleRect = pixelSnappedIntRectFromEdges(ruleLeft, ruleTop, ruleRight, ruleBottom);
2287                 drawLineForBoxSide(paintInfo.context, pixelSnappedRuleRect.x(), pixelSnappedRuleRect.y(), pixelSnappedRuleRect.maxX(), pixelSnappedRuleRect.maxY(), boxSide, ruleColor, ruleStyle, 0, 0, antialias);
2288             }
2289             
2290             ruleLogicalLeft = currLogicalLeftOffset;
2291         }
2292     } else {
2293         bool topToBottom = !style()->isFlippedBlocksWritingMode() ^ colInfo->progressionIsReversed();
2294         LayoutUnit ruleLeft = isHorizontalWritingMode()
2295             ? borderLeft() + paddingLeft()
2296             : colGap / 2 - colGap - ruleThickness / 2 + (!colInfo->progressionIsReversed() ? borderAndPaddingBefore() : borderAndPaddingAfter());
2297         LayoutUnit ruleWidth = isHorizontalWritingMode() ? contentWidth() : ruleThickness;
2298         LayoutUnit ruleTop = isHorizontalWritingMode()
2299             ? colGap / 2 - colGap - ruleThickness / 2 + (!colInfo->progressionIsReversed() ? borderAndPaddingBefore() : borderAndPaddingAfter())
2300             : borderStart() + paddingStart();
2301         LayoutUnit ruleHeight = isHorizontalWritingMode() ? ruleThickness : contentHeight();
2302         LayoutRect ruleRect(ruleLeft, ruleTop, ruleWidth, ruleHeight);
2303
2304         if (!topToBottom) {
2305             if (isHorizontalWritingMode())
2306                 ruleRect.setY(height() - ruleRect.maxY());
2307             else
2308                 ruleRect.setX(width() - ruleRect.maxX());
2309         }
2310
2311         ruleRect.moveBy(paintOffset);
2312
2313         BoxSide boxSide = isHorizontalWritingMode()
2314             ? topToBottom ? BSTop : BSBottom
2315             : topToBottom ? BSLeft : BSRight;
2316
2317         LayoutSize step(0, topToBottom ? colInfo->columnHeight() + colGap : -(colInfo->columnHeight() + colGap));
2318         if (!isHorizontalWritingMode())
2319             step = step.transposedSize();
2320
2321         for (unsigned i = 1; i < colCount; i++) {
2322             ruleRect.move(step);
2323             IntRect pixelSnappedRuleRect = pixelSnappedIntRect(ruleRect);
2324             drawLineForBoxSide(paintInfo.context, pixelSnappedRuleRect.x(), pixelSnappedRuleRect.y(), pixelSnappedRuleRect.maxX(), pixelSnappedRuleRect.maxY(), boxSide, ruleColor, ruleStyle, 0, 0, antialias);
2325         }
2326     }
2327 }
2328
2329 LayoutUnit RenderBlock::initialBlockOffsetForPainting() const
2330 {
2331     ColumnInfo* colInfo = columnInfo();
2332     LayoutUnit result = 0;
2333     if (colInfo->progressionAxis() == ColumnInfo::BlockAxis && colInfo->progressionIsReversed()) {
2334         LayoutRect colRect = columnRectAt(colInfo, 0);
2335         result = isHorizontalWritingMode() ? colRect.y() : colRect.x();
2336         result -= borderAndPaddingBefore();
2337         if (style()->isFlippedBlocksWritingMode())
2338             result = -result;
2339     }
2340     return result;
2341 }
2342     
2343 LayoutUnit RenderBlock::blockDeltaForPaintingNextColumn() const
2344 {
2345     ColumnInfo* colInfo = columnInfo();
2346     LayoutUnit blockDelta = -colInfo->columnHeight();
2347     LayoutUnit colGap = columnGap();
2348     if (colInfo->progressionAxis() == ColumnInfo::BlockAxis) {
2349         if (!colInfo->progressionIsReversed())
2350             blockDelta = colGap;
2351         else
2352             blockDelta -= (colInfo->columnHeight() + colGap);
2353     }
2354     if (style()->isFlippedBlocksWritingMode())
2355         blockDelta = -blockDelta;
2356     return blockDelta;
2357 }
2358
2359 void RenderBlock::paintColumnContents(PaintInfo& paintInfo, const LayoutPoint& paintOffset, bool paintingFloats)
2360 {
2361     // We need to do multiple passes, breaking up our child painting into strips.
2362     GraphicsContext* context = paintInfo.context;
2363     ColumnInfo* colInfo = columnInfo();
2364     unsigned colCount = columnCount(colInfo);
2365     if (!colCount)
2366         return;
2367     LayoutUnit colGap = columnGap();
2368     LayoutUnit currLogicalTopOffset = initialBlockOffsetForPainting();
2369     LayoutUnit blockDelta = blockDeltaForPaintingNextColumn();
2370     for (unsigned i = 0; i < colCount; i++) {
2371         // For each rect, we clip to the rect, and then we adjust our coords.
2372         LayoutRect colRect = columnRectAt(colInfo, i);
2373         flipForWritingMode(colRect);
2374         
2375         LayoutUnit logicalLeftOffset = (isHorizontalWritingMode() ? colRect.x() : colRect.y()) - logicalLeftOffsetForContent();
2376         LayoutSize offset = isHorizontalWritingMode() ? LayoutSize(logicalLeftOffset, currLogicalTopOffset) : LayoutSize(currLogicalTopOffset, logicalLeftOffset);
2377         colRect.moveBy(paintOffset);
2378         PaintInfo info(paintInfo);
2379         info.rect.intersect(pixelSnappedIntRect(colRect));
2380         
2381         if (!info.rect.isEmpty()) {
2382             GraphicsContextStateSaver stateSaver(*context);
2383             LayoutRect clipRect(colRect);
2384             
2385             if (i < colCount - 1) {
2386                 if (isHorizontalWritingMode())
2387                     clipRect.expand(colGap / 2, 0);
2388                 else
2389                     clipRect.expand(0, colGap / 2);
2390             }
2391             // Each strip pushes a clip, since column boxes are specified as being
2392             // like overflow:hidden.
2393             // FIXME: Content and column rules that extend outside column boxes at the edges of the multi-column element
2394             // are clipped according to the 'overflow' property.
2395             context->clip(pixelSnappedIntRect(clipRect));
2396
2397             // Adjust our x and y when painting.
2398             LayoutPoint adjustedPaintOffset = paintOffset + offset;
2399             if (paintingFloats)
2400                 paintFloats(info, adjustedPaintOffset, paintInfo.phase == PaintPhaseSelection || paintInfo.phase == PaintPhaseTextClip);
2401             else
2402                 paintContents(info, adjustedPaintOffset);
2403         }
2404         currLogicalTopOffset += blockDelta;
2405     }
2406 }
2407
2408 void RenderBlock::paintContents(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
2409 {
2410     // Avoid painting descendants of the root element when stylesheets haven't loaded.  This eliminates FOUC.
2411     // It's ok not to draw, because later on, when all the stylesheets do load, styleResolverChanged() on the Document
2412     // will do a full repaint.
2413     if (document().didLayoutWithPendingStylesheets() && !isRenderView())
2414         return;
2415
2416     if (childrenInline())
2417         m_lineBoxes.paint(this, paintInfo, paintOffset);
2418     else {
2419         PaintPhase newPhase = (paintInfo.phase == PaintPhaseChildOutlines) ? PaintPhaseOutline : paintInfo.phase;
2420         newPhase = (newPhase == PaintPhaseChildBlockBackgrounds) ? PaintPhaseChildBlockBackground : newPhase;
2421
2422         // We don't paint our own background, but we do let the kids paint their backgrounds.
2423         PaintInfo paintInfoForChild(paintInfo);
2424         paintInfoForChild.phase = newPhase;
2425         paintInfoForChild.updateSubtreePaintRootForChildren(this);
2426
2427         // FIXME: Paint-time pagination is obsolete and is now only used by embedded WebViews inside AppKit
2428         // NSViews. Do not add any more code for this.
2429         bool usePrintRect = !view().printRect().isEmpty();
2430         paintChildren(paintInfo, paintOffset, paintInfoForChild, usePrintRect);
2431     }
2432 }
2433
2434 void RenderBlock::paintChildren(PaintInfo& paintInfo, const LayoutPoint& paintOffset, PaintInfo& paintInfoForChild, bool usePrintRect)
2435 {
2436     for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox()) {
2437         if (!paintChild(child, paintInfo, paintOffset, paintInfoForChild, usePrintRect))
2438             return;
2439     }
2440 }
2441
2442 bool RenderBlock::paintChild(RenderBox* child, PaintInfo& paintInfo, const LayoutPoint& paintOffset, PaintInfo& paintInfoForChild, bool usePrintRect)
2443 {
2444     // Check for page-break-before: always, and if it's set, break and bail.
2445     bool checkBeforeAlways = !childrenInline() && (usePrintRect && child->style()->pageBreakBefore() == PBALWAYS);
2446     LayoutUnit absoluteChildY = paintOffset.y() + child->y();
2447     if (checkBeforeAlways
2448         && absoluteChildY > paintInfo.rect.y()
2449         && absoluteChildY < paintInfo.rect.maxY()) {
2450         view().setBestTruncatedAt(absoluteChildY, this, true);
2451         return false;
2452     }
2453
2454     if (!child->isFloating() && child->isReplaced() && usePrintRect && child->height() <= view().printRect().height()) {
2455         // Paginate block-level replaced elements.
2456         if (absoluteChildY + child->height() > view().printRect().maxY()) {
2457             if (absoluteChildY < view().truncatedAt())
2458                 view().setBestTruncatedAt(absoluteChildY, child);
2459             // If we were able to truncate, don't paint.
2460             if (absoluteChildY >= view().truncatedAt())
2461                 return false;
2462         }
2463     }
2464
2465     LayoutPoint childPoint = flipForWritingModeForChild(child, paintOffset);
2466     if (!child->hasSelfPaintingLayer() && !child->isFloating())
2467         child->paint(paintInfoForChild, childPoint);
2468
2469     // Check for page-break-after: always, and if it's set, break and bail.
2470     bool checkAfterAlways = !childrenInline() && (usePrintRect && child->style()->pageBreakAfter() == PBALWAYS);
2471     if (checkAfterAlways
2472         && (absoluteChildY + child->height()) > paintInfo.rect.y()
2473         && (absoluteChildY + child->height()) < paintInfo.rect.maxY()) {
2474         view().setBestTruncatedAt(absoluteChildY + child->height() + max<LayoutUnit>(0, child->collapsedMarginAfter()), this, true);
2475         return false;
2476     }
2477     return true;
2478 }
2479
2480
2481 void RenderBlock::paintCaret(PaintInfo& paintInfo, const LayoutPoint& paintOffset, CaretType type)
2482 {
2483     // Paint the caret if the FrameSelection says so or if caret browsing is enabled
2484     bool caretBrowsing = frame().settings().caretBrowsingEnabled();
2485     RenderObject* caretPainter;
2486     bool isContentEditable;
2487     if (type == CursorCaret) {
2488         caretPainter = frame().selection().caretRenderer();
2489         isContentEditable = frame().selection().rendererIsEditable();
2490     } else {
2491         caretPainter = frame().page()->dragCaretController().caretRenderer();
2492         isContentEditable = frame().page()->dragCaretController().isContentEditable();
2493     }
2494
2495     if (caretPainter == this && (isContentEditable || caretBrowsing)) {
2496         if (type == CursorCaret)
2497             frame().selection().paintCaret(paintInfo.context, paintOffset, paintInfo.rect);
2498         else
2499             frame().page()->dragCaretController().paintDragCaret(&frame(), paintInfo.context, paintOffset, paintInfo.rect);
2500     }
2501 }
2502
2503 void RenderBlock::paintObject(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
2504 {
2505     PaintPhase paintPhase = paintInfo.phase;
2506
2507     // 1. paint background, borders etc
2508     if ((paintPhase == PaintPhaseBlockBackground || paintPhase == PaintPhaseChildBlockBackground) && style()->visibility() == VISIBLE) {
2509         if (hasBoxDecorations())
2510             paintBoxDecorations(paintInfo, paintOffset);
2511         if (hasColumns() && !paintInfo.paintRootBackgroundOnly())
2512             paintColumnRules(paintInfo, paintOffset);
2513     }
2514
2515     if (paintPhase == PaintPhaseMask && style()->visibility() == VISIBLE) {
2516         paintMask(paintInfo, paintOffset);
2517         return;
2518     }
2519
2520     // We're done.  We don't bother painting any children.
2521     if (paintPhase == PaintPhaseBlockBackground || paintInfo.paintRootBackgroundOnly())
2522         return;
2523
2524     // Adjust our painting position if we're inside a scrolled layer (e.g., an overflow:auto div).
2525     LayoutPoint scrolledOffset = paintOffset;
2526     if (hasOverflowClip())
2527         scrolledOffset.move(-scrolledContentOffset());
2528
2529     // 2. paint contents
2530     if (paintPhase != PaintPhaseSelfOutline) {
2531         if (hasColumns())
2532             paintColumnContents(paintInfo, scrolledOffset);
2533         else
2534             paintContents(paintInfo, scrolledOffset);
2535     }
2536
2537     // 3. paint selection
2538     // FIXME: Make this work with multi column layouts.  For now don't fill gaps.
2539     bool isPrinting = document().printing();
2540     if (!isPrinting && !hasColumns())
2541         paintSelection(paintInfo, scrolledOffset); // Fill in gaps in selection on lines and between blocks.
2542
2543     // 4. paint floats.
2544     if (paintPhase == PaintPhaseFloat || paintPhase == PaintPhaseSelection || paintPhase == PaintPhaseTextClip) {
2545         if (hasColumns())
2546             paintColumnContents(paintInfo, scrolledOffset, true);
2547         else
2548             paintFloats(paintInfo, scrolledOffset, paintPhase == PaintPhaseSelection || paintPhase == PaintPhaseTextClip);
2549     }
2550
2551     // 5. paint outline.
2552     if ((paintPhase == PaintPhaseOutline || paintPhase == PaintPhaseSelfOutline) && hasOutline() && style()->visibility() == VISIBLE)
2553         paintOutline(paintInfo, LayoutRect(paintOffset, size()));
2554
2555     // 6. paint continuation outlines.
2556     if ((paintPhase == PaintPhaseOutline || paintPhase == PaintPhaseChildOutlines)) {
2557         RenderInline* inlineCont = inlineElementContinuation();
2558         if (inlineCont && inlineCont->hasOutline() && inlineCont->style()->visibility() == VISIBLE) {
2559             RenderInline* inlineRenderer = toRenderInline(inlineCont->element()->renderer());
2560             RenderBlock* cb = containingBlock();
2561
2562             bool inlineEnclosedInSelfPaintingLayer = false;
2563             for (RenderBoxModelObject* box = inlineRenderer; box != cb; box = box->parent()->enclosingBoxModelObject()) {
2564                 if (box->hasSelfPaintingLayer()) {
2565                     inlineEnclosedInSelfPaintingLayer = true;
2566                     break;
2567                 }
2568             }
2569
2570             // Do not add continuations for outline painting by our containing block if we are a relative positioned
2571             // anonymous block (i.e. have our own layer), paint them straightaway instead. This is because a block depends on renderers in its continuation table being
2572             // in the same layer. 
2573             if (!inlineEnclosedInSelfPaintingLayer && !hasLayer())
2574                 cb->addContinuationWithOutline(inlineRenderer);
2575             else if (!inlineRenderer->firstLineBox() || (!inlineEnclosedInSelfPaintingLayer && hasLayer()))
2576                 inlineRenderer->paintOutline(paintInfo, paintOffset - locationOffset() + inlineRenderer->containingBlock()->location());
2577         }
2578         paintContinuationOutlines(paintInfo, paintOffset);
2579     }
2580
2581     // 7. paint caret.
2582     // If the caret's node's render object's containing block is this block, and the paint action is PaintPhaseForeground,
2583     // then paint the caret.
2584     if (paintPhase == PaintPhaseForeground) {        
2585         paintCaret(paintInfo, paintOffset, CursorCaret);
2586         paintCaret(paintInfo, paintOffset, DragCaret);
2587     }
2588 }
2589
2590 LayoutPoint RenderBlock::flipFloatForWritingModeForChild(const FloatingObject* child, const LayoutPoint& point) const
2591 {
2592     if (!style()->isFlippedBlocksWritingMode())
2593         return point;
2594     
2595     // This is similar to RenderBox::flipForWritingModeForChild. We have to subtract out our left/top offsets twice, since
2596     // it's going to get added back in. We hide this complication here so that the calling code looks normal for the unflipped
2597     // case.
2598     if (isHorizontalWritingMode())
2599         return LayoutPoint(point.x(), point.y() + height() - child->renderer()->height() - 2 * yPositionForFloatIncludingMargin(child));
2600     return LayoutPoint(point.x() + width() - child->renderer()->width() - 2 * xPositionForFloatIncludingMargin(child), point.y());
2601 }
2602
2603 void RenderBlock::paintFloats(PaintInfo& paintInfo, const LayoutPoint& paintOffset, bool preservePhase)
2604 {
2605     if (!m_floatingObjects)
2606         return;
2607
2608     const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2609     FloatingObjectSetIterator end = floatingObjectSet.end();
2610     for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
2611         FloatingObject* r = *it;
2612         // Only paint the object if our m_shouldPaint flag is set.
2613         if (r->shouldPaint() && !r->renderer()->hasSelfPaintingLayer()) {
2614             PaintInfo currentPaintInfo(paintInfo);
2615             currentPaintInfo.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
2616             // FIXME: LayoutPoint version of xPositionForFloatIncludingMargin would make this much cleaner.
2617             LayoutPoint childPoint = flipFloatForWritingModeForChild(r, LayoutPoint(paintOffset.x() + xPositionForFloatIncludingMargin(r) - r->renderer()->x(), paintOffset.y() + yPositionForFloatIncludingMargin(r) - r->renderer()->y()));
2618             r->renderer()->paint(currentPaintInfo, childPoint);
2619             if (!preservePhase) {
2620                 currentPaintInfo.phase = PaintPhaseChildBlockBackgrounds;
2621                 r->renderer()->paint(currentPaintInfo, childPoint);
2622                 currentPaintInfo.phase = PaintPhaseFloat;
2623                 r->renderer()->paint(currentPaintInfo, childPoint);
2624                 currentPaintInfo.phase = PaintPhaseForeground;
2625                 r->renderer()->paint(currentPaintInfo, childPoint);
2626                 currentPaintInfo.phase = PaintPhaseOutline;
2627                 r->renderer()->paint(currentPaintInfo, childPoint);
2628             }
2629         }
2630     }
2631 }
2632
2633 RenderInline* RenderBlock::inlineElementContinuation() const
2634
2635     RenderBoxModelObject* continuation = this->continuation();
2636     return continuation && continuation->isInline() ? toRenderInline(continuation) : 0;
2637 }
2638
2639 RenderBlock* RenderBlock::blockElementContinuation() const
2640 {
2641     RenderBoxModelObject* currentContinuation = continuation();
2642     if (!currentContinuation || currentContinuation->isInline())
2643         return 0;
2644     RenderBlock* nextContinuation = toRenderBlock(currentContinuation);
2645     if (nextContinuation->isAnonymousBlock())
2646         return nextContinuation->blockElementContinuation();
2647     return nextContinuation;
2648 }
2649     
2650 static ContinuationOutlineTableMap* continuationOutlineTable()
2651 {
2652     DEFINE_STATIC_LOCAL(ContinuationOutlineTableMap, table, ());
2653     return &table;
2654 }
2655
2656 void RenderBlock::addContinuationWithOutline(RenderInline* flow)
2657 {
2658     // We can't make this work if the inline is in a layer.  We'll just rely on the broken
2659     // way of painting.
2660     ASSERT(!flow->layer() && !flow->isInlineElementContinuation());
2661     
2662     ContinuationOutlineTableMap* table = continuationOutlineTable();
2663     ListHashSet<RenderInline*>* continuations = table->get(this);
2664     if (!continuations) {
2665         continuations = new ListHashSet<RenderInline*>;
2666         table->set(this, adoptPtr(continuations));
2667     }
2668     
2669     continuations->add(flow);
2670 }
2671
2672 bool RenderBlock::paintsContinuationOutline(RenderInline* flow)
2673 {
2674     ContinuationOutlineTableMap* table = continuationOutlineTable();
2675     if (table->isEmpty())
2676         return false;
2677         
2678     ListHashSet<RenderInline*>* continuations = table->get(this);
2679     if (!continuations)
2680         return false;
2681
2682     return continuations->contains(flow);
2683 }
2684
2685 void RenderBlock::paintContinuationOutlines(PaintInfo& info, const LayoutPoint& paintOffset)
2686 {
2687     ContinuationOutlineTableMap* table = continuationOutlineTable();
2688     if (table->isEmpty())
2689         return;
2690         
2691     OwnPtr<ListHashSet<RenderInline*> > continuations = table->take(this);
2692     if (!continuations)
2693         return;
2694
2695     LayoutPoint accumulatedPaintOffset = paintOffset;
2696     // Paint each continuation outline.
2697     ListHashSet<RenderInline*>::iterator end = continuations->end();
2698     for (ListHashSet<RenderInline*>::iterator it = continuations->begin(); it != end; ++it) {
2699         // Need to add in the coordinates of the intervening blocks.
2700         RenderInline* flow = *it;
2701         RenderBlock* block = flow->containingBlock();
2702         for ( ; block && block != this; block = block->containingBlock())
2703             accumulatedPaintOffset.moveBy(block->location());
2704         ASSERT(block);   
2705         flow->paintOutline(info, accumulatedPaintOffset);
2706     }
2707 }
2708
2709 bool RenderBlock::shouldPaintSelectionGaps() const
2710 {
2711     return selectionState() != SelectionNone && style()->visibility() == VISIBLE && isSelectionRoot();
2712 }
2713
2714 bool RenderBlock::isSelectionRoot() const
2715 {
2716     if (isPseudoElement())
2717         return false;
2718     ASSERT(element() || isAnonymous());
2719         
2720     // FIXME: Eventually tables should have to learn how to fill gaps between cells, at least in simple non-spanning cases.
2721     if (isTable())
2722         return false;
2723         
2724     if (isBody() || isRoot() || hasOverflowClip()
2725         || isPositioned() || isFloating()
2726         || isTableCell() || isInlineBlockOrInlineTable()
2727         || hasTransform() || hasReflection() || hasMask() || isWritingModeRoot()
2728         || isRenderFlowThread())
2729         return true;
2730     
2731     if (view().selectionStart()) {
2732         Node* startElement = view().selectionStart()->node();
2733         if (startElement && startElement->rootEditableElement() == element())
2734             return true;
2735     }
2736     
2737     return false;
2738 }
2739
2740 GapRects RenderBlock::selectionGapRectsForRepaint(const RenderLayerModelObject* repaintContainer)
2741 {
2742     ASSERT(!needsLayout());
2743
2744     if (!shouldPaintSelectionGaps())
2745         return GapRects();
2746
2747     TransformState transformState(TransformState::ApplyTransformDirection, FloatPoint());
2748     mapLocalToContainer(repaintContainer, transformState, ApplyContainerFlip | UseTransforms);
2749     LayoutPoint offsetFromRepaintContainer = roundedLayoutPoint(transformState.mappedPoint());
2750
2751     if (hasOverflowClip())
2752         offsetFromRepaintContainer -= scrolledContentOffset();
2753
2754     LogicalSelectionOffsetCaches cache(this);
2755     LayoutUnit lastTop = 0;
2756     LayoutUnit lastLeft = logicalLeftSelectionOffset(this, lastTop, cache);
2757     LayoutUnit lastRight = logicalRightSelectionOffset(this, lastTop, cache);
2758     
2759     return selectionGaps(this, offsetFromRepaintContainer, IntSize(), lastTop, lastLeft, lastRight, cache);
2760 }
2761
2762 void RenderBlock::paintSelection(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
2763 {
2764     if (shouldPaintSelectionGaps() && paintInfo.phase == PaintPhaseForeground) {
2765         LogicalSelectionOffsetCaches cache(this);
2766         LayoutUnit lastTop = 0;
2767         LayoutUnit lastLeft = logicalLeftSelectionOffset(this, lastTop, cache);
2768         LayoutUnit lastRight = logicalRightSelectionOffset(this, lastTop, cache);
2769         GraphicsContextStateSaver stateSaver(*paintInfo.context);
2770
2771         LayoutRect gapRectsBounds = selectionGaps(this, paintOffset, LayoutSize(), lastTop, lastLeft, lastRight, cache, &paintInfo);
2772         if (!gapRectsBounds.isEmpty()) {
2773             if (RenderLayer* layer = enclosingLayer()) {
2774                 gapRectsBounds.moveBy(-paintOffset);
2775                 if (!hasLayer()) {
2776                     LayoutRect localBounds(gapRectsBounds);
2777                     flipForWritingMode(localBounds);
2778                     gapRectsBounds = localToContainerQuad(FloatRect(localBounds), &layer->renderer()).enclosingBoundingBox();
2779                     if (layer->renderer().hasOverflowClip())
2780                         gapRectsBounds.move(layer->renderBox()->scrolledContentOffset());
2781                 }
2782                 layer->addBlockSelectionGapsBounds(gapRectsBounds);
2783             }
2784         }
2785     }
2786 }
2787
2788 static void clipOutPositionedObjects(const PaintInfo* paintInfo, const LayoutPoint& offset, TrackedRendererListHashSet* positionedObjects)
2789 {
2790     if (!positionedObjects)
2791         return;
2792     
2793     TrackedRendererListHashSet::const_iterator end = positionedObjects->end();
2794     for (TrackedRendererListHashSet::const_iterator it = positionedObjects->begin(); it != end; ++it) {
2795         RenderBox* r = *it;
2796         paintInfo->context->clipOut(IntRect(offset.x() + r->x(), offset.y() + r->y(), r->width(), r->height()));
2797     }
2798 }
2799
2800 static LayoutUnit blockDirectionOffset(RenderBlock* rootBlock, const LayoutSize& offsetFromRootBlock)
2801 {
2802     return rootBlock->isHorizontalWritingMode() ? offsetFromRootBlock.height() : offsetFromRootBlock.width();
2803 }
2804
2805 static LayoutUnit inlineDirectionOffset(RenderBlock* rootBlock, const LayoutSize& offsetFromRootBlock)
2806 {
2807     return rootBlock->isHorizontalWritingMode() ? offsetFromRootBlock.width() : offsetFromRootBlock.height();
2808 }
2809
2810 LayoutRect RenderBlock::logicalRectToPhysicalRect(const LayoutPoint& rootBlockPhysicalPosition, const LayoutRect& logicalRect)
2811 {
2812     LayoutRect result;
2813     if (isHorizontalWritingMode())
2814         result = logicalRect;
2815     else
2816         result = LayoutRect(logicalRect.y(), logicalRect.x(), logicalRect.height(), logicalRect.width());
2817     flipForWritingMode(result);
2818     result.moveBy(rootBlockPhysicalPosition);
2819     return result;
2820 }
2821
2822 GapRects RenderBlock::selectionGaps(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
2823     LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
2824 {
2825     // IMPORTANT: Callers of this method that intend for painting to happen need to do a save/restore.
2826     // Clip out floating and positioned objects when painting selection gaps.
2827     if (paintInfo) {
2828         // Note that we don't clip out overflow for positioned objects.  We just stick to the border box.
2829         LayoutRect flippedBlockRect(offsetFromRootBlock.width(), offsetFromRootBlock.height(), width(), height());
2830         rootBlock->flipForWritingMode(flippedBlockRect);
2831         flippedBlockRect.moveBy(rootBlockPhysicalPosition);
2832         clipOutPositionedObjects(paintInfo, flippedBlockRect.location(), positionedObjects());
2833         if (isBody() || isRoot()) // The <body> must make sure to examine its containingBlock's positioned objects.
2834             for (RenderBlock* cb = containingBlock(); cb && !cb->isRenderView(); cb = cb->containingBlock())
2835                 clipOutPositionedObjects(paintInfo, LayoutPoint(cb->x(), cb->y()), cb->positionedObjects()); // FIXME: Not right for flipped writing modes.
2836         if (m_floatingObjects) {
2837             const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2838             FloatingObjectSetIterator end = floatingObjectSet.end();
2839             for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
2840                 FloatingObject* r = *it;
2841                 LayoutRect floatBox(offsetFromRootBlock.width() + xPositionForFloatIncludingMargin(r),
2842                                     offsetFromRootBlock.height() + yPositionForFloatIncludingMargin(r),
2843                                     r->renderer()->width(), r->renderer()->height());
2844                 rootBlock->flipForWritingMode(floatBox);
2845                 floatBox.move(rootBlockPhysicalPosition.x(), rootBlockPhysicalPosition.y());
2846                 paintInfo->context->clipOut(pixelSnappedIntRect(floatBox));
2847             }
2848         }
2849     }
2850
2851     // FIXME: overflow: auto/scroll regions need more math here, since painting in the border box is different from painting in the padding box (one is scrolled, the other is
2852     // fixed).
2853     GapRects result;
2854     if (!isRenderBlockFlow()) // FIXME: Make multi-column selection gap filling work someday.
2855         return result;
2856
2857     if (hasColumns() || hasTransform() || style()->columnSpan()) {
2858         // FIXME: We should learn how to gap fill multiple columns and transforms eventually.
2859         lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalHeight();
2860         lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, logicalHeight(), cache);
2861         lastLogicalRight = logicalRightSelectionOffset(rootBlock, logicalHeight(), cache);
2862         return result;
2863     }
2864
2865     if (childrenInline())
2866         result = inlineSelectionGaps(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop, lastLogicalLeft, lastLogicalRight, cache, paintInfo);
2867     else
2868         result = blockSelectionGaps(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop, lastLogicalLeft, lastLogicalRight, cache, paintInfo);
2869
2870     // Go ahead and fill the vertical gap all the way to the bottom of our block if the selection extends past our block.
2871     if (rootBlock == this && (selectionState() != SelectionBoth && selectionState() != SelectionEnd)) {
2872         result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
2873             lastLogicalTop, lastLogicalLeft, lastLogicalRight, logicalHeight(), cache, paintInfo));
2874     }
2875
2876     return result;
2877 }
2878
2879 GapRects RenderBlock::inlineSelectionGaps(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
2880     LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
2881 {
2882     GapRects result;
2883
2884     bool containsStart = selectionState() == SelectionStart || selectionState() == SelectionBoth;
2885
2886     if (!firstLineBox()) {
2887         if (containsStart) {
2888             // Go ahead and update our lastLogicalTop to be the bottom of the block.  <hr>s or empty blocks with height can trip this
2889             // case.
2890             lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalHeight();
2891             lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, logicalHeight(), cache);
2892             lastLogicalRight = logicalRightSelectionOffset(rootBlock, logicalHeight(), cache);
2893         }
2894         return result;
2895     }
2896
2897     RootInlineBox* lastSelectedLine = 0;
2898     RootInlineBox* curr;
2899     for (curr = firstRootBox(); curr && !curr->hasSelectedChildren(); curr = curr->nextRootBox()) { }
2900
2901     // Now paint the gaps for the lines.
2902     for (; curr && curr->hasSelectedChildren(); curr = curr->nextRootBox()) {
2903         LayoutUnit selTop =  curr->selectionTopAdjustedForPrecedingBlock();
2904         LayoutUnit selHeight = curr->selectionHeightAdjustedForPrecedingBlock();
2905
2906         if (!containsStart && !lastSelectedLine &&
2907             selectionState() != SelectionStart && selectionState() != SelectionBoth)
2908             result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop, lastLogicalLeft, lastLogicalRight, selTop, cache, paintInfo));
2909         
2910         LayoutRect logicalRect(curr->logicalLeft(), selTop, curr->logicalWidth(), selTop + selHeight);
2911         logicalRect.move(isHorizontalWritingMode() ? offsetFromRootBlock : offsetFromRootBlock.transposedSize());
2912         LayoutRect physicalRect = rootBlock->logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
2913         if (!paintInfo || (isHorizontalWritingMode() && physicalRect.y() < paintInfo->rect.maxY() && physicalRect.maxY() > paintInfo->rect.y())
2914             || (!isHorizontalWritingMode() && physicalRect.x() < paintInfo->rect.maxX() && physicalRect.maxX() > paintInfo->rect.x()))
2915             result.unite(curr->lineSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, selTop, selHeight, cache, paintInfo));
2916
2917         lastSelectedLine = curr;
2918     }
2919
2920     if (containsStart && !lastSelectedLine)
2921         // VisibleSelection must start just after our last line.
2922         lastSelectedLine = lastRootBox();
2923
2924     if (lastSelectedLine && selectionState() != SelectionEnd && selectionState() != SelectionBoth) {
2925         // Go ahead and update our lastY to be the bottom of the last selected line.
2926         lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + lastSelectedLine->selectionBottom();
2927         lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
2928         lastLogicalRight = logicalRightSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
2929     }
2930     return result;
2931 }
2932
2933 GapRects RenderBlock::blockSelectionGaps(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
2934     LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
2935 {
2936     GapRects result;
2937
2938     // Go ahead and jump right to the first block child that contains some selected objects.
2939     RenderBox* curr;
2940     for (curr = firstChildBox(); curr && curr->selectionState() == SelectionNone; curr = curr->nextSiblingBox()) { }
2941     
2942     if (!curr)
2943         return result;
2944
2945     LogicalSelectionOffsetCaches childCache(this, cache);
2946
2947     for (bool sawSelectionEnd = false; curr && !sawSelectionEnd; curr = curr->nextSiblingBox()) {
2948         SelectionState childState = curr->selectionState();
2949         if (childState == SelectionBoth || childState == SelectionEnd)
2950             sawSelectionEnd = true;
2951
2952         if (curr->isFloatingOrOutOfFlowPositioned())
2953             continue; // We must be a normal flow object in order to even be considered.
2954
2955         if (curr->isInFlowPositioned() && curr->hasLayer()) {
2956             // If the relposition offset is anything other than 0, then treat this just like an absolute positioned element.
2957             // Just disregard it completely.
2958             LayoutSize relOffset = curr->layer()->offsetForInFlowPosition();
2959             if (relOffset.width() || relOffset.height())
2960                 continue;
2961         }
2962
2963         bool paintsOwnSelection = curr->shouldPaintSelectionGaps() || curr->isTable(); // FIXME: Eventually we won't special-case table like this.
2964         bool fillBlockGaps = paintsOwnSelection || (curr->canBeSelectionLeaf() && childState != SelectionNone);
2965         if (fillBlockGaps) {
2966             // We need to fill the vertical gap above this object.
2967             if (childState == SelectionEnd || childState == SelectionInside) {
2968                 // Fill the gap above the object.
2969                 result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
2970                     lastLogicalTop, lastLogicalLeft, lastLogicalRight, curr->logicalTop(), cache, paintInfo));
2971             }
2972
2973             // Only fill side gaps for objects that paint their own selection if we know for sure the selection is going to extend all the way *past*
2974             // our object.  We know this if the selection did not end inside our object.
2975             if (paintsOwnSelection && (childState == SelectionStart || sawSelectionEnd))
2976                 childState = SelectionNone;
2977
2978             // Fill side gaps on this object based off its state.
2979             bool leftGap, rightGap;
2980             getSelectionGapInfo(childState, leftGap, rightGap);
2981
2982             if (leftGap)
2983                 result.uniteLeft(logicalLeftSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, this, curr->logicalLeft(), curr->logicalTop(), curr->logicalHeight(), cache, paintInfo));
2984             if (rightGap)
2985                 result.uniteRight(logicalRightSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, this, curr->logicalRight(), curr->logicalTop(), curr->logicalHeight(), cache, paintInfo));
2986
2987             // Update lastLogicalTop to be just underneath the object.  lastLogicalLeft and lastLogicalRight extend as far as
2988             // they can without bumping into floating or positioned objects.  Ideally they will go right up
2989             // to the border of the root selection block.
2990             lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + curr->logicalBottom();
2991             lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, curr->logicalBottom(), cache);
2992             lastLogicalRight = logicalRightSelectionOffset(rootBlock, curr->logicalBottom(), cache);
2993         } else if (childState != SelectionNone) {
2994             // We must be a block that has some selected object inside it.  Go ahead and recur.
2995             result.unite(toRenderBlock(curr)->selectionGaps(rootBlock, rootBlockPhysicalPosition, LayoutSize(offsetFromRootBlock.width() + curr->x(), offsetFromRootBlock.height() + curr->y()), 
2996                 lastLogicalTop, lastLogicalLeft, lastLogicalRight, childCache, paintInfo));
2997         }
2998     }
2999     return result;
3000 }
3001
3002 LayoutRect RenderBlock::blockSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
3003     LayoutUnit lastLogicalTop, LayoutUnit lastLogicalLeft, LayoutUnit lastLogicalRight, LayoutUnit logicalBottom, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
3004 {
3005     LayoutUnit logicalTop = lastLogicalTop;
3006     LayoutUnit logicalHeight = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalBottom - logicalTop;
3007     if (logicalHeight <= 0)
3008         return LayoutRect();
3009
3010     // Get the selection offsets for the bottom of the gap
3011     LayoutUnit logicalLeft = max(lastLogicalLeft, logicalLeftSelectionOffset(rootBlock, logicalBottom, cache));
3012     LayoutUnit logicalRight = min(lastLogicalRight, logicalRightSelectionOffset(rootBlock, logicalBottom, cache));
3013     LayoutUnit logicalWidth = logicalRight - logicalLeft;
3014     if (logicalWidth <= 0)
3015         return LayoutRect();
3016
3017     LayoutRect gapRect = rootBlock->logicalRectToPhysicalRect(rootBlockPhysicalPosition, LayoutRect(logicalLeft, logicalTop, logicalWidth, logicalHeight));
3018     if (paintInfo)
3019         paintInfo->context->fillRect(pixelSnappedIntRect(gapRect), selectionBackgroundColor(), style()->colorSpace());
3020     return gapRect;
3021 }
3022
3023 LayoutRect RenderBlock::logicalLeftSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
3024     RenderObject* selObj, LayoutUnit logicalLeft, LayoutUnit logicalTop, LayoutUnit logicalHeight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
3025 {
3026     LayoutUnit rootBlockLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalTop;
3027     LayoutUnit rootBlockLogicalLeft = max(logicalLeftSelectionOffset(rootBlock, logicalTop, cache), logicalLeftSelectionOffset(rootBlock, logicalTop + logicalHeight, cache));
3028     LayoutUnit rootBlockLogicalRight = min(inlineDirectionOffset(rootBlock, offsetFromRootBlock) + floorToInt(logicalLeft),
3029         min(logicalRightSelectionOffset(rootBlock, logicalTop, cache), logicalRightSelectionOffset(rootBlock, logicalTop + logicalHeight, cache)));
3030     LayoutUnit rootBlockLogicalWidth = rootBlockLogicalRight - rootBlockLogicalLeft;
3031     if (rootBlockLogicalWidth <= 0)
3032         return LayoutRect();
3033
3034     LayoutRect gapRect = rootBlock->logicalRectToPhysicalRect(rootBlockPhysicalPosition, LayoutRect(rootBlockLogicalLeft, rootBlockLogicalTop, rootBlockLogicalWidth, logicalHeight));
3035     if (paintInfo)
3036         paintInfo->context->fillRect(pixelSnappedIntRect(gapRect), selObj->selectionBackgroundColor(), selObj->style()->colorSpace());
3037     return gapRect;
3038 }
3039
3040 LayoutRect RenderBlock::logicalRightSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
3041     RenderObject* selObj, LayoutUnit logicalRight, LayoutUnit logicalTop, LayoutUnit logicalHeight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
3042 {
3043     LayoutUnit rootBlockLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalTop;
3044     LayoutUnit rootBlockLogicalLeft = max(inlineDirectionOffset(rootBlock, offsetFromRootBlock) + floorToInt(logicalRight),
3045         max(logicalLeftSelectionOffset(rootBlock, logicalTop, cache), logicalLeftSelectionOffset(rootBlock, logicalTop + logicalHeight, cache)));
3046     LayoutUnit rootBlockLogicalRight = min(logicalRightSelectionOffset(rootBlock, logicalTop, cache), logicalRightSelectionOffset(rootBlock, logicalTop + logicalHeight, cache));
3047     LayoutUnit rootBlockLogicalWidth = rootBlockLogicalRight - rootBlockLogicalLeft;
3048     if (rootBlockLogicalWidth <= 0)
3049         return LayoutRect();
3050
3051     LayoutRect gapRect = rootBlock->logicalRectToPhysicalRect(rootBlockPhysicalPosition, LayoutRect(rootBlockLogicalLeft, rootBlockLogicalTop, rootBlockLogicalWidth, logicalHeight));
3052     if (paintInfo)
3053         paintInfo->context->fillRect(pixelSnappedIntRect(gapRect), selObj->selectionBackgroundColor(), selObj->style()->colorSpace());
3054     return gapRect;
3055 }
3056
3057 void RenderBlock::getSelectionGapInfo(SelectionState state, bool& leftGap, bool& rightGap)
3058 {
3059     bool ltr = style()->isLeftToRightDirection();
3060     leftGap = (state == RenderObject::SelectionInside) ||
3061               (state == RenderObject::SelectionEnd && ltr) ||
3062               (state == RenderObject::SelectionStart && !ltr);
3063     rightGap = (state == RenderObject::SelectionInside) ||
3064                (state == RenderObject::SelectionStart && ltr) ||
3065                (state == RenderObject::SelectionEnd && !ltr);
3066 }
3067
3068 LayoutUnit RenderBlock::logicalLeftSelectionOffset(RenderBlock* rootBlock, LayoutUnit position, const LogicalSelectionOffsetCaches& cache)
3069 {
3070     LayoutUnit logicalLeft = logicalLeftOffsetForLine(position, false);
3071     if (logicalLeft == logicalLeftOffsetForContent()) {
3072         if (rootBlock != this) // The border can potentially be further extended by our containingBlock().
3073             return cache.containingBlockInfo(this).logicalLeftSelectionOffset(rootBlock, position + logicalTop());
3074         return logicalLeft;
3075     } else {
3076         RenderBlock* cb = this;
3077         const LogicalSelectionOffsetCaches* currentCache = &cache;
3078         while (cb != rootBlock) {
3079             logicalLeft += cb->logicalLeft();
3080
3081             ASSERT(currentCache);
3082             const LogicalSelectionOffsetCaches::ContainingBlockInfo& info = currentCache->containingBlockInfo(cb);
3083             cb = info.block();
3084             currentCache = info.cache();
3085         }
3086     }
3087     return logicalLeft;
3088 }
3089
3090 LayoutUnit RenderBlock::logicalRightSelectionOffset(RenderBlock* rootBlock, LayoutUnit position, const LogicalSelectionOffsetCaches& cache)
3091 {
3092     LayoutUnit logicalRight = logicalRightOffsetForLine(position, false);
3093     if (logicalRight == logicalRightOffsetForContent()) {
3094         if (rootBlock != this) // The border can potentially be further extended by our containingBlock().
3095             return cache.containingBlockInfo(this).logicalRightSelectionOffset(rootBlock, position + logicalTop());
3096         return logicalRight;
3097     } else {
3098         RenderBlock* cb = this;
3099         const LogicalSelectionOffsetCaches* currentCache = &cache;
3100         while (cb != rootBlock) {
3101             logicalRight += cb->logicalLeft();
3102
3103             ASSERT(currentCache);
3104             const LogicalSelectionOffsetCaches::ContainingBlockInfo& info = currentCache->containingBlockInfo(cb);
3105             cb = info.block();
3106             currentCache = info.cache();
3107         }
3108     }
3109     return logicalRight;
3110 }
3111
3112 RenderBlock* RenderBlock::blockBeforeWithinSelectionRoot(LayoutSize& offset) const
3113 {
3114     if (isSelectionRoot())
3115         return 0;
3116
3117     const RenderObject* object = this;
3118     RenderObject* sibling;
3119     do {
3120         sibling = object->previousSibling();
3121         while (sibling && (!sibling->isRenderBlock() || toRenderBlock(sibling)->isSelectionRoot()))
3122             sibling = sibling->previousSibling();
3123
3124         offset -= LayoutSize(toRenderBlock(object)->logicalLeft(), toRenderBlock(object)->logicalTop());
3125         object = object->parent();
3126     } while (!sibling && object && object->isRenderBlock() && !toRenderBlock(object)->isSelectionRoot());
3127
3128     if (!sibling)
3129         return 0;
3130
3131     RenderBlock* beforeBlock = toRenderBlock(sibling);
3132
3133     offset += LayoutSize(beforeBlock->logicalLeft(), beforeBlock->logicalTop());
3134
3135     RenderObject* child = beforeBlock->lastChild();
3136     while (child && child->isRenderBlock()) {
3137         beforeBlock = toRenderBlock(child);
3138         offset += LayoutSize(beforeBlock->logicalLeft(), beforeBlock->logicalTop());
3139         child = beforeBlock->lastChild();
3140     }
3141     return beforeBlock;
3142 }
3143
3144 void RenderBlock::insertIntoTrackedRendererMaps(RenderBox* descendant, TrackedDescendantsMap*& descendantsMap, TrackedContainerMap*& containerMap)
3145 {
3146     if (!descendantsMap) {
3147         descendantsMap = new TrackedDescendantsMap;
3148         containerMap = new TrackedContainerMap;
3149     }
3150     
3151     TrackedRendererListHashSet* descendantSet = descendantsMap->get(this);
3152     if (!descendantSet) {
3153         descendantSet = new TrackedRendererListHashSet;
3154         descendantsMap->set(this, adoptPtr(descendantSet));
3155     }
3156     bool added = descendantSet->add(descendant).isNewEntry;
3157     if (!added) {
3158         ASSERT(containerMap->get(descendant));
3159         ASSERT(containerMap->get(descendant)->contains(this));
3160         return;
3161     }
3162     
3163     HashSet<RenderBlock*>* containerSet = containerMap->get(descendant);
3164     if (!containerSet) {
3165         containerSet = new HashSet<RenderBlock*>;
3166         containerMap->set(descendant, adoptPtr(containerSet));
3167     }
3168     ASSERT(!containerSet->contains(this));
3169     containerSet->add(this);
3170 }
3171
3172 void RenderBlock::removeFromTrackedRendererMaps(RenderBox* descendant, TrackedDescendantsMap*& descendantsMap, TrackedContainerMap*& containerMap)
3173 {
3174     if (!descendantsMap)
3175         return;
3176     
3177     OwnPtr<HashSet<RenderBlock*> > containerSet = containerMap->take(descendant);
3178     if (!containerSet)
3179         return;
3180     
3181     HashSet<RenderBlock*>::iterator end = containerSet->end();
3182     for (HashSet<RenderBlock*>::iterator it = containerSet->begin(); it != end; ++it) {
3183         RenderBlock* container = *it;
3184
3185         // FIXME: Disabling this assert temporarily until we fix the layout
3186         // bugs associated with positioned objects not properly cleared from
3187         // their ancestor chain before being moved. See webkit bug 93766.
3188         // ASSERT(descendant->isDescendantOf(container));
3189
3190         TrackedDescendantsMap::iterator descendantsMapIterator = descendantsMap->find(container);
3191         ASSERT(descendantsMapIterator != descendantsMap->end());
3192         if (descendantsMapIterator == descendantsMap->end())
3193             continue;
3194         TrackedRendererListHashSet* descendantSet = descendantsMapIterator->value.get();
3195         ASSERT(descendantSet->contains(descendant));
3196         descendantSet->remove(descendant);
3197         if (descendantSet->isEmpty())
3198             descendantsMap->remove(descendantsMapIterator);
3199     }
3200 }
3201
3202 TrackedRendererListHashSet* RenderBlock::positionedObjects() const
3203 {
3204     if (gPositionedDescendantsMap)
3205         return gPositionedDescendantsMap->get(this);
3206     return 0;
3207 }
3208
3209 void RenderBlock::insertPositionedObject(RenderBox* o)
3210 {
3211     ASSERT(!isAnonymousBlock());
3212
3213     if (o->isRenderFlowThread())
3214         return;
3215     
3216     insertIntoTrackedRendererMaps(o, gPositionedDescendantsMap, gPositionedContainerMap);
3217 }
3218
3219 void RenderBlock::removePositionedObject(RenderBox* o)
3220 {
3221     removeFromTrackedRendererMaps(o, gPositionedDescendantsMap, gPositionedContainerMap);
3222 }
3223
3224 void RenderBlock::removePositionedObjects(RenderBlock* o, ContainingBlockState containingBlockState)
3225 {
3226     TrackedRendererListHashSet* positionedDescendants = positionedObjects();
3227     if (!positionedDescendants)
3228         return;
3229     
3230     RenderBox* r;
3231     
3232     TrackedRendererListHashSet::iterator end = positionedDescendants->end();
3233     
3234     Vector<RenderBox*, 16> deadObjects;
3235
3236     for (TrackedRendererListHashSet::iterator it = positionedDescendants->begin(); it != end; ++it) {
3237         r = *it;
3238         if (!o || r->isDescendantOf(o)) {
3239             if (containingBlockState == NewContainingBlock)
3240                 r->setChildNeedsLayout(true, MarkOnlyThis);
3241             
3242             // It is parent blocks job to add positioned child to positioned objects list of its containing block
3243             // Parent layout needs to be invalidated to ensure this happens.
3244             RenderObject* p = r->parent();
3245             while (p && !p->isRenderBlock())
3246                 p = p->parent();
3247             if (p)
3248                 p->setChildNeedsLayout(true);
3249             
3250             deadObjects.append(r);
3251         }
3252     }
3253     
3254     for (unsigned i = 0; i < deadObjects.size(); i++)
3255         removePositionedObject(deadObjects.at(i));
3256 }
3257
3258 void RenderBlock::removeFloatingObjects()
3259 {
3260     if (!m_floatingObjects)
3261         return;
3262
3263     deleteAllValues(m_floatingObjects->set());
3264     m_floatingObjects->clear();
3265 }
3266
3267 FloatingObject* RenderBlock::insertFloatingObject(RenderBox* o)
3268 {
3269     ASSERT(o->isFloating());
3270
3271     // Create the list of special objects if we don't aleady have one
3272     if (!m_floatingObjects)
3273         createFloatingObjects();
3274     else {
3275         // Don't insert the object again if it's already in the list
3276         const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
3277         FloatingObjectSetIterator it = floatingObjectSet.find<RenderBox*, FloatingObjectHashTranslator>(o);
3278         if (it != floatingObjectSet.end())
3279             return *it;
3280     }
3281
3282     // Create the special object entry & append it to the list
3283
3284     FloatingObject* newObj = new FloatingObject(o->style()->floating());
3285     
3286     // Our location is irrelevant if we're unsplittable or no pagination is in effect.
3287     // Just go ahead and lay out the float.
3288     bool isChildRenderBlock = o->isRenderBlock();
3289     if (isChildRenderBlock && !o->needsLayout() && view().layoutState()->pageLogicalHeightChanged())
3290         o->setChildNeedsLayout(true, MarkOnlyThis);
3291             
3292     bool needsBlockDirectionLocationSetBeforeLayout = isChildRenderBlock && view().layoutState()->needsBlockDirectionLocationSetBeforeLayout();
3293     if (!needsBlockDirectionLocationSetBeforeLayout || isWritingModeRoot()) // We are unsplittable if we're a block flow root.
3294         o->layoutIfNeeded();
3295     else {
3296         o->updateLogicalWidth();
3297         o->computeAndSetBlockDirectionMargins(this);
3298     }
3299
3300     newObj->setLogicalWidth(logicalWidthForChild(o) + marginStartForChild(o) + marginEndForChild(o), isHorizontalWritingMode());
3301
3302 #if ENABLE(CSS_SHAPES)
3303     if (ShapeOutsideInfo* shapeOutside = o->shapeOutsideInfo())
3304         shapeOutside->setShapeSize(logicalWidthForChild(o), logicalHeightForChild(o));
3305 #endif
3306
3307     newObj->setShouldPaint(!o->hasSelfPaintingLayer()); // If a layer exists, the float will paint itself. Otherwise someone else will.
3308     newObj->setIsDescendant(true);
3309     newObj->setRenderer(o);
3310
3311     m_floatingObjects->add(newObj);
3312     
3313     return newObj;
3314 }
3315
3316 void RenderBlock::removeFloatingObject(RenderBox* o)
3317 {
3318     if (m_floatingObjects) {
3319         const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
3320         FloatingObjectSetIterator it = floatingObjectSet.find<RenderBox*, FloatingObjectHashTranslator>(o);
3321         if (it != floatingObjectSet.end()) {
3322             FloatingObject* r = *it;
3323             if (childrenInline()) {
3324                 LayoutUnit logicalTop = r->logicalTop(isHorizontalWritingMode());
3325                 LayoutUnit logicalBottom = r->logicalBottom(isHorizontalWritingMode());
3326
3327                 // Fix for https://bugs.webkit.org/show_bug.cgi?id=54995.
3328                 if (logicalBottom < 0 || logicalBottom < logicalTop || logicalTop == LayoutUnit::max())
3329                     logicalBottom = LayoutUnit::max();
3330                 else {
3331                     // Special-case zero- and less-than-zero-height floats: those don't touch
3332                     // the line that they're on, but it still needs to be dirtied. This is
3333                     // accomplished by pretending they have a height of 1.
3334                     logicalBottom = max(logicalBottom, logicalTop + 1);
3335                 }
3336                 if (r->originatingLine()) {
3337                     if (!selfNeedsLayout()) {
3338                         ASSERT(&r->originatingLine()->renderer() == this);
3339                         r->originatingLine()->markDirty();
3340                     }
3341 #if !ASSERT_DISABLED
3342                     r->setOriginatingLine(0);
3343 #endif
3344                 }
3345                 markLinesDirtyInBlockRange(0, logicalBottom);
3346             }
3347             m_floatingObjects->remove(r);
3348             ASSERT(!r->originatingLine());
3349             delete r;
3350         }
3351     }
3352 }
3353
3354 void RenderBlock::removeFloatingObjectsBelow(FloatingObject* lastFloat, int logicalOffset)
3355 {
3356     if (!containsFloats())
3357         return;
3358     
3359     const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
3360     FloatingObject* curr = floatingObjectSet.last();
3361     while (curr != lastFloat && (!curr->isPlaced() || curr->logicalTop(isHorizontalWritingMode()) >= logicalOffset)) {
3362         m_floatingObjects->remove(curr);
3363         ASSERT(!curr->originatingLine());
3364         delete curr;
3365         if (floatingObjectSet.isEmpty())
3366             break;
3367         curr = floatingObjectSet.last();
3368     }
3369 }
3370
3371 LayoutPoint RenderBlock::computeLogicalLocationForFloat(const FloatingObject* floatingObject, LayoutUnit logicalTopOffset) const
3372 {
3373     RenderBox* childBox = floatingObject->renderer();
3374     LayoutUnit logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
3375     LayoutUnit logicalRightOffset; // Constant part of right offset.
3376 #if ENABLE(CSS_SHAPES)
3377     // FIXME Bug 102948: This only works for shape outside directly set on this block.
3378     ShapeInsideInfo* shapeInsideInfo = this->shapeInsideInfo();
3379     // FIXME Bug 102846: Take into account the height of the content. The offset should be
3380     // equal to the maximum segment length.
3381     if (shapeInsideInfo && shapeInsideInfo->hasSegments() && shapeInsideInfo->segments().size() == 1) {
3382         // FIXME Bug 102949: Add support for shapes with multipe segments.
3383
3384         // The segment offsets are relative to the content box.
3385         logicalRightOffset = logicalLeftOffset + shapeInsideInfo->segments()[0].logicalRight;
3386         logicalLeftOffset += shapeInsideInfo->segments()[0].logicalLeft;
3387     } else
3388 #endif
3389         logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset);
3390
3391     LayoutUnit floatLogicalWidth = min(floatingObject->logicalWidth(isHorizontalWritingMode()), logicalRightOffset - logicalLeftOffset); // The width we look for.
3392
3393     LayoutUnit floatLogicalLeft;
3394
3395     bool insideFlowThread = flowThreadContainingBlock();
3396
3397     if (childBox->style()->floating() == LeftFloat) {
3398         LayoutUnit heightRemainingLeft = 1;
3399         LayoutUnit heightRemainingRight = 1;
3400         floatLogicalLeft = logicalLeftOffsetForLineIgnoringShapeOutside(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
3401         while (logicalRightOffsetForLineIgnoringShapeOutside(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight) - floatLogicalLeft < floatLogicalWidth) {
3402             logicalTopOffset += min(heightRemainingLeft, heightRemainingRight);
3403             floatLogicalLeft = logicalLeftOffsetForLineIgnoringShapeOutside(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
3404             if (insideFlowThread) {
3405                 // Have to re-evaluate all of our offsets, since they may have changed.
3406                 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
3407                 logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
3408                 floatLogicalWidth = min(floatingObject->logicalWidth(isHorizontalWritingMode()), logicalRightOffset - logicalLeftOffset);
3409             }
3410         }
3411         floatLogicalLeft = max(logicalLeftOffset - borderAndPaddingLogicalLeft(), floatLogicalLeft);
3412     } else {
3413         LayoutUnit heightRemainingLeft = 1;
3414         LayoutUnit heightRemainingRight = 1;
3415         floatLogicalLeft = logicalRightOffsetForLineIgnoringShapeOutside(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
3416         while (floatLogicalLeft - logicalLeftOffsetForLineIgnoringShapeOutside(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft) < floatLogicalWidth) {
3417             logicalTopOffset += min(heightRemainingLeft, heightRemainingRight);
3418             floatLogicalLeft = logicalRightOffsetForLineIgnoringShapeOutside(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
3419             if (insideFlowThread) {
3420                 // Have to re-evaluate all of our offsets, since they may have changed.
3421                 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
3422                 logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
3423                 floatLogicalWidth = min(floatingObject->logicalWidth(isHorizontalWritingMode()), logicalRightOffset - logicalLeftOffset);
3424             }
3425         }
3426         floatLogicalLeft -= floatingObject->logicalWidth(isHorizontalWritingMode()); // Use the original width of the float here, since the local variable
3427                                                                   // |floatLogicalWidth| was capped to the available line width.
3428                                                                   // See fast/block/float/clamped-right-float.html.
3429     }
3430     
3431     return LayoutPoint(floatLogicalLeft, logicalTopOffset);
3432 }
3433
3434 bool RenderBlock::positionNewFloats()
3435 {
3436     if (!m_floatingObjects)
3437         return false;
3438
3439     const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
3440     if (floatingObjectSet.isEmpty())
3441         return false;
3442
3443     // If all floats have already been positioned, then we have no work to do.
3444     if (floatingObjectSet.last()->isPlaced())
3445         return false;
3446
3447     // Move backwards through our floating object list until we find a float that has
3448     // already been positioned.  Then we'll be able to move forward, positioning all of
3449     // the new floats that need it.
3450     FloatingObjectSetIterator it = floatingObjectSet.end();
3451     --it; // Go to last item.
3452     FloatingObjectSetIterator begin = floatingObjectSet.begin();
3453     FloatingObject* lastPlacedFloatingObject = 0;
3454     while (it != begin) {
3455         --it;
3456         if ((*it)->isPlaced()) {
3457             lastPlacedFloatingObject = *it;
3458             ++it;
3459             break;
3460         }
3461     }
3462
3463     LayoutUnit logicalTop = logicalHeight();
3464     
3465     // The float cannot start above the top position of the last positioned float.
3466     if (lastPlacedFloatingObject)
3467         logicalTop = max(lastPlacedFloatingObject->logicalTop(isHorizontalWritingMode()), logicalTop);
3468
3469     FloatingObjectSetIterator end = floatingObjectSet.end();
3470     // Now walk through the set of unpositioned floats and place them.
3471     for (; it != end; ++it) {
3472         FloatingObject* floatingObject = *it;
3473         // The containing block is responsible for positioning floats, so if we have floats in our
3474         // list that come from somewhere else, do not attempt to position them.
3475         if (floatingObject->renderer()->containingBlock() != this)
3476             continue;
3477
3478         RenderBox* childBox = floatingObject->renderer();
3479
3480         LayoutUnit childLogicalLeftMargin = style()->isLeftToRightDirection() ? marginStartForChild(childBox) : marginEndForChild(childBox);
3481
3482         LayoutRect oldRect = childBox->frameRect();
3483
3484         if (childBox->style()->clear() & CLEFT)
3485             logicalTop = max(lowestFloatLogicalBottom(FloatingObject::FloatLeft), logicalTop);
3486         if (childBox->style()->clear() & CRIGHT)
3487             logicalTop = max(lowestFloatLogicalBottom(FloatingObject::FloatRight), logicalTop);
3488
3489         LayoutPoint floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, logicalTop);
3490
3491         floatingObject->setLogicalLeft(floatLogicalLocation.x(), isHorizontalWritingMode());
3492
3493         setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
3494         setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
3495
3496         estimateRegionRangeForBoxChild(childBox);
3497
3498         LayoutState* layoutState = view().layoutState();
3499         bool isPaginated = layoutState->isPaginated();
3500         if (isPaginated && !childBox->needsLayout())
3501             childBox->markForPaginationRelayoutIfNeeded();
3502         
3503         childBox->layoutIfNeeded();
3504
3505         if (isPaginated) {
3506             // If we are unsplittable and don't fit, then we need to move down.
3507             // We include our margins as part of the unsplittable area.
3508             LayoutUnit newLogicalTop = adjustForUnsplittableChild(childBox, floatLogicalLocation.y(), true);
3509             
3510             // See if we have a pagination strut that is making us move down further.
3511             // Note that an unsplittable child can't also have a pagination strut, so this is
3512             // exclusive with the case above.
3513             RenderBlock* childBlock = childBox->isRenderBlock() ? toRenderBlock(childBox) : 0;
3514             if (childBlock && childBlock->paginationStrut()) {
3515                 newLogicalTop += childBlock->paginationStrut();
3516                 childBlock->setPaginationStrut(0);
3517             }
3518             
3519             if (newLogicalTop != floatLogicalLocation.y()) {
3520                 floatingObject->setPaginationStrut(newLogicalTop - floatLogicalLocation.y());
3521
3522                 floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, newLogicalTop);
3523                 floatingObject->setLogicalLeft(floatLogicalLocation.x(), isHorizontalWritingMode());
3524
3525                 setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
3526                 setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
3527         
3528                 if (childBlock)
3529                     childBlock->setChildNeedsLayout(true, MarkOnlyThis);
3530                 childBox->layoutIfNeeded();
3531             }
3532
3533             if (updateRegionRangeForBoxChild(childBox)) {
3534                 childBox->setNeedsLayout(true, MarkOnlyThis);
3535                 childBox->layoutIfNeeded();
3536             }
3537         }
3538
3539         floatingObject->setLogicalTop(floatLogicalLocation.y(), isHorizontalWritingMode());
3540
3541         floatingObject->setLogicalHeight(logicalHeightForChild(childBox) + marginBeforeForChild(childBox) + marginAfterForChild(childBox), isHorizontalWritingMode());
3542
3543         m_floatingObjects->addPlacedObject(floatingObject);
3544
3545         // If the child moved, we have to repaint it.
3546         if (childBox->checkForRepaintDuringLayout())
3547             childBox->repaintDuringLayoutIfMoved(oldRect);
3548     }
3549     return true;
3550 }
3551
3552 void RenderBlock::newLine(EClear clear)
3553 {
3554     positionNewFloats();
3555     // set y position
3556     LayoutUnit newY = 0;
3557     switch (clear)
3558     {
3559         case CLEFT:
3560             newY = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
3561             break;
3562         case CRIGHT:
3563             newY = lowestFloatLogicalBottom(FloatingObject::FloatRight);
3564             break;
3565         case CBOTH:
3566             newY = lowestFloatLogicalBottom();
3567         default:
3568             break;
3569     }
3570     if (height() < newY)
3571         setLogicalHeight(newY);
3572 }
3573
3574 void RenderBlock::addPercentHeightDescendant(RenderBox* descendant)
3575 {
3576     insertIntoTrackedRendererMaps(descendant, gPercentHeightDescendantsMap, gPercentHeightContainerMap);
3577 }
3578
3579 void RenderBlock::removePercentHeightDescendant(RenderBox* descendant)
3580 {
3581     removeFromTrackedRendererMaps(descendant, gPercentHeightDescendantsMap, gPercentHeightContainerMap);
3582 }
3583
3584 TrackedRendererListHashSet* RenderBlock::percentHeightDescendants() const
3585 {
3586     return gPercentHeightDescendantsMap ? gPercentHeightDescendantsMap->get(this) : 0;
3587 }
3588
3589 bool RenderBlock::hasPercentHeightContainerMap()
3590 {
3591     return gPercentHeightContainerMap;
3592 }
3593
3594 bool RenderBlock::hasPercentHeightDescendant(RenderBox* descendant)
3595 {
3596     // We don't null check gPercentHeightContainerMap since the caller
3597     // already ensures this and we need to call this function on every
3598     // descendant in clearPercentHeightDescendantsFrom().
3599     ASSERT(gPercentHeightContainerMap);
3600     return gPercentHeightContainerMap->contains(descendant);
3601 }
3602
3603 void RenderBlock::removePercentHeightDescendantIfNeeded(RenderBox* descendant)
3604 {
3605     // We query the map directly, rather than looking at style's
3606     // logicalHeight()/logicalMinHeight()/logicalMaxHeight() since those
3607     // can change with writing mode/directional changes.
3608     if (!hasPercentHeightContainerMap())
3609         return;
3610
3611     if (!hasPercentHeightDescendant(descendant))
3612         return;
3613
3614     removePercentHeightDescendant(descendant);
3615 }
3616
3617 void RenderBlock::clearPercentHeightDescendantsFrom(RenderBox* parent)
3618 {
3619     ASSERT(gPercentHeightContainerMap);
3620     for (RenderObject* curr = parent->firstChild(); curr; curr = curr->nextInPreOrder(parent)) {
3621         if (!curr->isBox())
3622             continue;
3623  
3624         RenderBox* box = toRenderBox(curr);
3625         if (!hasPercentHeightDescendant(box))
3626             continue;
3627
3628         removePercentHeightDescendant(box);
3629     }
3630 }
3631
3632 LayoutUnit RenderBlock::textIndentOffset() const
3633 {
3634     LayoutUnit cw = 0;
3635     RenderView* renderView = 0;
3636     if (style()->textIndent().isPercent())
3637         cw = containingBlock()->availableLogicalWidth();
3638     else if (style()->textIndent().isViewportPercentage())
3639         renderView = &view();
3640     return minimumValueForLength(style()->textIndent(), cw, renderView);
3641 }
3642
3643 LayoutUnit RenderBlock::logicalLeftOffsetForContent(RenderRegion* region) const
3644 {
3645     LayoutUnit logicalLeftOffset = style()->isHorizontalWritingMode() ? borderLeft() + paddingLeft() : borderTop() + paddingTop();
3646     if (!region)
3647         return logicalLeftOffset;
3648     LayoutRect boxRect = borderBoxRectInRegion(region);
3649     return logicalLeftOffset + (isHorizontalWritingMode() ? boxRect.x() : boxRect.y());
3650 }
3651
3652 LayoutUnit RenderBlock::logicalRightOffsetForContent(RenderRegion* region) const
3653 {
3654     LayoutUnit logicalRightOffset = style()->isHorizontalWritingMode() ? borderLeft() + paddingLeft() : borderTop() + paddingTop();
3655     logicalRightOffset += availableLogicalWidth();
3656     if (!region)
3657         return logicalRightOffset;
3658     LayoutRect boxRect = borderBoxRectInRegion(region);
3659     return logicalRightOffset - (logicalWidth() - (isHorizontalWritingMode() ? boxRect.maxX() : boxRect.maxY()));
3660 }
3661
3662 LayoutUnit RenderBlock::logicalLeftFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit* heightRemaining, LayoutUnit logicalHeight, ShapeOutsideFloatOffsetMode offsetMode) const
3663 {
3664     if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
3665         return m_floatingObjects->logicalLeftOffset(fixedOffset, logicalTop, logicalHeight, offsetMode, heightRemaining);
3666
3667     return fixedOffset;
3668 }
3669
3670 LayoutUnit RenderBlock::adjustLogicalLeftOffsetForLine(LayoutUnit offsetFromFloats, bool applyTextIndent) const
3671 {
3672     LayoutUnit left = offsetFromFloats;
3673
3674     if (applyTextIndent && style()->isLeftToRightDirection())
3675         left += textIndentOffset();
3676
3677     if (style()->lineAlign() == LineAlignNone)
3678         return left;
3679     
3680     // Push in our left offset so that it is aligned with the character grid.
3681     LayoutState* layoutState = view().layoutState();
3682     if (!layoutState)
3683         return left;
3684
3685     RenderBlock* lineGrid = layoutState->lineGrid();
3686     if (!lineGrid || lineGrid->style()->writingMode() != style()->writingMode())
3687         return left;
3688
3689     // FIXME: Should letter-spacing apply? This is complicated since it doesn't apply at the edge?
3690     float maxCharWidth = lineGrid->style()->font().primaryFont()->maxCharWidth();
3691     if (!maxCharWidth)
3692         return left;
3693
3694     LayoutUnit lineGridOffset = lineGrid->isHorizontalWritingMode() ? layoutState->lineGridOffset().width(): layoutState->lineGridOffset().height();
3695     LayoutUnit layoutOffset = lineGrid->isHorizontalWritingMode() ? layoutState->layoutOffset().width() : layoutState->layoutOffset().height();
3696     
3697     // Push in to the nearest character width (truncated so that we pixel snap left).
3698     // FIXME: Should be patched when subpixel layout lands, since this calculation doesn't have to pixel snap
3699     // any more (https://bugs.webkit.org/show_bug.cgi?id=79946).
3700     // FIXME: This is wrong for RTL (https://bugs.webkit.org/show_bug.cgi?id=79945).
3701     // FIXME: This doesn't work with columns or regions (https://bugs.webkit.org/show_bug.cgi?id=79942).
3702     // FIXME: This doesn't work when the inline position of the object isn't set ahead of time.
3703     // FIXME: Dynamic changes to the font or to the inline position need to result in a deep relayout.
3704     // (https://bugs.webkit.org/show_bug.cgi?id=79944)
3705     float remainder = fmodf(maxCharWidth - fmodf(left + layoutOffset - lineGridOffset, maxCharWidth), maxCharWidth);
3706     left += remainder;
3707     return left;
3708 }
3709
3710 LayoutUnit RenderBlock::logicalRightFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit* heightRemaining, LayoutUnit logicalHeight, ShapeOutsideFloatOffsetMode offsetMode) const
3711 {
3712     if (m_floatingObjects && m_floatingObjects->hasRightObjects())
3713         return m_floatingObjects->logicalRightOffset(fixedOffset, logicalTop, logicalHeight, offsetMode, heightRemaining);
3714
3715     return fixedOffset;
3716 }
3717
3718 LayoutUnit RenderBlock::adjustLogicalRightOffsetForLine(LayoutUnit offsetFromFloats, bool applyTextIndent) const
3719 {
3720     LayoutUnit right = offsetFromFloats;
3721     
3722     if (applyTextIndent && !style()->isLeftToRightDirection())
3723         right -= textIndentOffset();
3724     
3725     if (style()->lineAlign() == LineAlignNone)
3726         return right;
3727     
3728     // Push in our right offset so that it is aligned with the character grid.
3729     LayoutState* layoutState = view().layoutState();
3730     if (!layoutState)
3731         return right;
3732
3733     RenderBlock* lineGrid = layoutState->lineGrid();
3734     if (!lineGrid || lineGrid->style()->writingMode() != style()->writingMode())
3735         return right;
3736
3737     // FIXME: Should letter-spacing apply? This is complicated since it doesn't apply at the edge?
3738     float maxCharWidth = lineGrid->style()->font().primaryFont()->maxCharWidth();
3739     if (!maxCharWidth)
3740         return right;
3741
3742     LayoutUnit lineGridOffset = lineGrid->isHorizontalWritingMode() ? layoutState->lineGridOffset().width(): layoutState->lineGridOffset().height();
3743     LayoutUnit layoutOffset = lineGrid->isHorizontalWritingMode() ? layoutState->layoutOffset().width() : layoutState->layoutOffset().height();
3744     
3745     // Push in to the nearest character width (truncated so that we pixel snap right).
3746     // FIXME: Should be patched when subpixel layout lands, since this calculation doesn't have to pixel snap
3747     // any more (https://bugs.webkit.org/show_bug.cgi?id=79946).
3748     // FIXME: This is wrong for RTL (https://bugs.webkit.org/show_bug.cgi?id=79945).
3749     // FIXME: This doesn't work with columns or regions (https://bugs.webkit.org/show_bug.cgi?id=79942).
3750     // FIXME: This doesn't work when the inline position of the object isn't set ahead of time.
3751     // FIXME: Dynamic changes to the font or to the inline position need to result in a deep relayout.
3752     // (https://bugs.webkit.org/show_bug.cgi?id=79944)
3753     float remainder = fmodf(fmodf(right + layoutOffset - lineGridOffset, maxCharWidth), maxCharWidth);
3754     right -= ceilf(remainder);
3755     return right;
3756 }
3757
3758 LayoutUnit RenderBlock::nextFloatLogicalBottomBelow(LayoutUnit logicalHeight) const
3759 {
3760     if (!m_floatingObjects)
3761         return logicalHeight;
3762
3763     LayoutUnit bottom = LayoutUnit::max();
3764     const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
3765     FloatingObjectSetIterator end = floatingObjectSet.end();
3766     for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
3767         FloatingObject* r = *it;
3768         LayoutUnit floatBottom = r->logicalBottom(isHorizontalWritingMode());
3769         if (floatBottom > logicalHeight)
3770             bottom = min(floatBottom, bottom);
3771     }
3772
3773     return bottom == LayoutUnit::max() ? LayoutUnit() : bottom;
3774 }
3775
3776 LayoutUnit RenderBlock::lowestFloatLogicalBottom(FloatingObject::Type floatType) const
3777 {
3778     if (!m_floatingObjects)
3779         return 0;
3780     LayoutUnit lowestFloatBottom = 0;
3781     const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
3782     FloatingObjectSetIterator end = floatingObjectSet.end();
3783     for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
3784         FloatingObject* r = *it;
3785         if (r->isPlaced() && r->type() & floatType)
3786             lowestFloatBottom