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