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