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