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