18abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)/*
28abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
38abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *
48abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
58abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * modification, are permitted provided that the following conditions are
68abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * met:
78abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *
88abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
98abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
108abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
118abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
128abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * in the documentation and/or other materials provided with the
138abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * distribution.
148abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
158abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
168abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * this software without specific prior written permission.
178abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) *
188abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
198abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
208abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
218abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
228abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
238abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
248abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
258abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
268abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
288abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) */
308abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
318abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#include "config.h"
328abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#include "core/rendering/RenderBlockFlow.h"
338abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
3409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "core/accessibility/AXObjectCache.h"
351e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "core/frame/FrameView.h"
367242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "core/frame/LocalFrame.h"
377242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "core/frame/Settings.h"
387242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "core/html/HTMLDialogElement.h"
397242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "core/paint/BlockFlowPainter.h"
40f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#include "core/rendering/HitTestLocation.h"
4109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "core/rendering/RenderFlowThread.h"
428abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#include "core/rendering/RenderLayer.h"
43d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "core/rendering/RenderMultiColumnFlowThread.h"
44197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "core/rendering/RenderPagedFlowThread.h"
4519cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)#include "core/rendering/RenderText.h"
468abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)#include "core/rendering/RenderView.h"
47c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#include "core/rendering/TextAutosizer.h"
4809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "core/rendering/line/LineWidth.h"
4919cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)#include "core/rendering/svg/SVGTextRunRenderingContext.h"
5009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "platform/text/BidiTextRun.h"
518abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
52c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
538abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
54f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)bool RenderBlockFlow::s_canPropagateFloatIntoSibling = false;
55f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
569bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)struct SameSizeAsMarginInfo {
579bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    uint16_t bitfields;
589bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit margins[2];
599bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)};
609bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
619bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)COMPILE_ASSERT(sizeof(RenderBlockFlow::MarginValues) == sizeof(LayoutUnit[4]), MarginValues_should_stay_small);
629bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
639bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)class MarginInfo {
649bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Collapsing flags for whether we can collapse our margins with our children's margins.
659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_canCollapseWithChildren : 1;
669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_canCollapseMarginBeforeWithChildren : 1;
679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_canCollapseMarginAfterWithChildren : 1;
6809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool m_canCollapseMarginAfterWithLastChild: 1;
699bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
709bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Whether or not we are a quirky container, i.e., do we collapse away top and bottom
719bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // margins in our container. Table cells and the body are the common examples. We
729bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // also have a custom style property for Safari RSS to deal with TypePad blog articles.
739bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_quirkContainer : 1;
749bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
759bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // This flag tracks whether we are still looking at child margins that can all collapse together at the beginning of a block.
769bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // They may or may not collapse with the top margin of the block (|m_canCollapseTopWithChildren| tells us that), but they will
779bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // always be collapsing with one another. This variable can remain set to true through multiple iterations
789bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // as long as we keep encountering self-collapsing blocks.
799bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_atBeforeSideOfBlock : 1;
809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
819bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // This flag is set when we know we're examining bottom margins and we know we're at the bottom of the block.
829bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_atAfterSideOfBlock : 1;
839bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
849bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // These variables are used to detect quirky margins that we need to collapse away (in table cells
859bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // and in the body element).
869bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_hasMarginBeforeQuirk : 1;
879bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_hasMarginAfterQuirk : 1;
889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_determinedMarginBeforeQuirk : 1;
899bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
909bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool m_discardMargin : 1;
919bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
929bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // These flags track the previous maximal positive and negative margins.
939bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit m_positiveMargin;
949bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit m_negativeMargin;
959bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
969bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)public:
979bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    MarginInfo(RenderBlockFlow*, LayoutUnit beforeBorderPadding, LayoutUnit afterBorderPadding);
989bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
999bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setAtBeforeSideOfBlock(bool b) { m_atBeforeSideOfBlock = b; }
1009bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setAtAfterSideOfBlock(bool b) { m_atAfterSideOfBlock = b; }
1019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void clearMargin()
1029bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    {
1039bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        m_positiveMargin = 0;
1049bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        m_negativeMargin = 0;
1059bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
1069bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setHasMarginBeforeQuirk(bool b) { m_hasMarginBeforeQuirk = b; }
1079bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setHasMarginAfterQuirk(bool b) { m_hasMarginAfterQuirk = b; }
1089bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setDeterminedMarginBeforeQuirk(bool b) { m_determinedMarginBeforeQuirk = b; }
1099bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setPositiveMargin(LayoutUnit p) { ASSERT(!m_discardMargin); m_positiveMargin = p; }
1109bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setNegativeMargin(LayoutUnit n) { ASSERT(!m_discardMargin); m_negativeMargin = n; }
1119bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setPositiveMarginIfLarger(LayoutUnit p)
1129bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    {
1139bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        ASSERT(!m_discardMargin);
1149bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (p > m_positiveMargin)
1159bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            m_positiveMargin = p;
1169bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
1179bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setNegativeMarginIfLarger(LayoutUnit n)
1189bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    {
1199bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        ASSERT(!m_discardMargin);
1209bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (n > m_negativeMargin)
1219bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            m_negativeMargin = n;
1229bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
1239bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
1249bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setMargin(LayoutUnit p, LayoutUnit n) { ASSERT(!m_discardMargin); m_positiveMargin = p; m_negativeMargin = n; }
1259bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setCanCollapseMarginAfterWithChildren(bool collapse) { m_canCollapseMarginAfterWithChildren = collapse; }
12609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    void setCanCollapseMarginAfterWithLastChild(bool collapse) { m_canCollapseMarginAfterWithLastChild = collapse; }
1279bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    void setDiscardMargin(bool value) { m_discardMargin = value; }
1289bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
1299bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool atBeforeSideOfBlock() const { return m_atBeforeSideOfBlock; }
1309bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool canCollapseWithMarginBefore() const { return m_atBeforeSideOfBlock && m_canCollapseMarginBeforeWithChildren; }
1319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool canCollapseWithMarginAfter() const { return m_atAfterSideOfBlock && m_canCollapseMarginAfterWithChildren; }
1329bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool canCollapseMarginBeforeWithChildren() const { return m_canCollapseMarginBeforeWithChildren; }
1339bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool canCollapseMarginAfterWithChildren() const { return m_canCollapseMarginAfterWithChildren; }
13409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool canCollapseMarginAfterWithLastChild() const { return m_canCollapseMarginAfterWithLastChild; }
1359bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool quirkContainer() const { return m_quirkContainer; }
1369bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool determinedMarginBeforeQuirk() const { return m_determinedMarginBeforeQuirk; }
1379bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool hasMarginBeforeQuirk() const { return m_hasMarginBeforeQuirk; }
1389bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool hasMarginAfterQuirk() const { return m_hasMarginAfterQuirk; }
1399bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit positiveMargin() const { return m_positiveMargin; }
1409bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit negativeMargin() const { return m_negativeMargin; }
1419bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool discardMargin() const { return m_discardMargin; }
1429bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit margin() const { return m_positiveMargin - m_negativeMargin; }
1439bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)};
1449bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)static bool inNormalFlow(RenderBox* child)
1459bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
1469bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    RenderBlock* curr = child->containingBlock();
1479bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    RenderView* renderView = child->view();
1489bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    while (curr && curr != renderView) {
1499bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (curr->hasColumns() || curr->isRenderFlowThread())
1509bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            return true;
1519bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (curr->isFloatingOrOutOfFlowPositioned())
1529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            return false;
1539bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        curr = curr->containingBlock();
1549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
1559bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return true;
1569bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
1579bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
158e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)void RenderBlockFlow::RenderBlockFlowRareData::trace(Visitor* visitor)
159e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles){
160e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    visitor->trace(m_multiColumnFlowThread);
161e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)}
162e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
1638abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)RenderBlockFlow::RenderBlockFlow(ContainerNode* node)
1648abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    : RenderBlock(node)
1658abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles){
1669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    COMPILE_ASSERT(sizeof(MarginInfo) == sizeof(SameSizeAsMarginInfo), MarginInfo_should_stay_small);
167197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    setChildrenInline(true);
1688abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)}
1698abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
1708abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)RenderBlockFlow::~RenderBlockFlow()
1718abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles){
1728abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)}
1738abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
174e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)void RenderBlockFlow::trace(Visitor* visitor)
175e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles){
176e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    visitor->trace(m_rareData);
177e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    RenderBlock::trace(visitor);
178e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)}
179e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
180a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)RenderBlockFlow* RenderBlockFlow::createAnonymous(Document* document)
181a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
182a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    RenderBlockFlow* renderer = new RenderBlockFlow(0);
183a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    renderer->setDocumentForAnonymous(document);
184a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    return renderer;
185a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
186a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
187d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)RenderObject* RenderBlockFlow::layoutSpecialExcludedChild(bool relayoutChildren, SubtreeLayoutScope& layoutScope)
188d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
189d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    RenderMultiColumnFlowThread* flowThread = multiColumnFlowThread();
190d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (!flowThread)
191d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return 0;
192d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    setLogicalTopForChild(flowThread, borderBefore() + paddingBefore());
193d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    flowThread->layoutColumns(relayoutChildren, layoutScope);
194d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    determineLogicalLeftPositionForChild(flowThread);
195d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return flowThread;
196d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
197d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
198d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)bool RenderBlockFlow::updateLogicalWidthAndColumnWidth()
199d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
200d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    bool relayoutChildren = RenderBlock::updateLogicalWidthAndColumnWidth();
201d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (RenderMultiColumnFlowThread* flowThread = multiColumnFlowThread()) {
202d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        if (flowThread->needsNewWidth())
203d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            return true;
204d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
205d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return relayoutChildren;
206d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
207d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
20809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void RenderBlockFlow::checkForPaginationLogicalHeightChange(LayoutUnit& pageLogicalHeight, bool& pageLogicalHeightChanged, bool& hasSpecifiedPageLogicalHeight)
20951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
210d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (RenderMultiColumnFlowThread* flowThread = multiColumnFlowThread()) {
21110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        LogicalExtentComputedValues computedValues;
21210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        computeLogicalHeight(LayoutUnit(), logicalTop(), computedValues);
21310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        LayoutUnit columnHeight = computedValues.m_extent - borderAndPaddingLogicalHeight() - scrollbarLogicalHeight();
21410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        pageLogicalHeightChanged = columnHeight != flowThread->columnHeightAvailable();
21510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        flowThread->setColumnHeightAvailable(std::max<LayoutUnit>(columnHeight, 0));
216d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    } else if (hasColumns()) {
217d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        ColumnInfo* colInfo = columnInfo();
218d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
21909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        if (!pageLogicalHeight) {
22009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            LayoutUnit oldLogicalHeight = logicalHeight();
22109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            setLogicalHeight(0);
22209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            // We need to go ahead and set our explicit page height if one exists, so that we can
22309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            // avoid doing two layout passes.
22409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            updateLogicalHeight();
22509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            LayoutUnit columnHeight = contentLogicalHeight();
22609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            if (columnHeight > 0) {
22709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                pageLogicalHeight = columnHeight;
22809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                hasSpecifiedPageLogicalHeight = true;
22909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            }
23009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            setLogicalHeight(oldLogicalHeight);
23109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        }
23209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        if (colInfo->columnHeight() != pageLogicalHeight && everHadLayout()) {
23309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            colInfo->setColumnHeight(pageLogicalHeight);
23409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            pageLogicalHeightChanged = true;
23509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        }
23651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
23709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        if (!hasSpecifiedPageLogicalHeight && !pageLogicalHeight)
23809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            colInfo->clearForcedBreaks();
23909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    } else if (isRenderFlowThread()) {
24007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        RenderFlowThread* flowThread = toRenderFlowThread(this);
24107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
24207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        // FIXME: This is a hack to always make sure we have a page logical height, if said height
24307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        // is known. The page logical height thing in LayoutState is meaningless for flow
24407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        // thread-based pagination (page height isn't necessarily uniform throughout the flow
24507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        // thread), but as long as it is used universally as a means to determine whether page
24607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        // height is known or not, we need this. Page height is unknown when column balancing is
24707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        // enabled and flow thread height is still unknown (i.e. during the first layout pass). When
24807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        // it's unknown, we need to prevent the pagination code from assuming page breaks everywhere
24907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        // and thereby eating every top margin. It should be trivial to clean up and get rid of this
25007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        // hack once the old multicol implementation is gone.
25107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        pageLogicalHeight = flowThread->isPageLogicalHeightKnown() ? LayoutUnit(1) : LayoutUnit(0);
25207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
25307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        pageLogicalHeightChanged = flowThread->pageLogicalSizeChanged();
25409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    }
25551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
25651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
25709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)bool RenderBlockFlow::shouldRelayoutForPagination(LayoutUnit& pageLogicalHeight, LayoutUnit layoutOverflowLogicalBottom) const
258a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
259a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // FIXME: We don't balance properly at all in the presence of forced page breaks. We need to understand what
260a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // the distance between forced page breaks is so that we can avoid making the minimum column height too tall.
261a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    ColumnInfo* colInfo = columnInfo();
26209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit columnHeight = pageLogicalHeight;
26309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    const int minColumnCount = colInfo->forcedBreaks() + 1;
26409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    const int desiredColumnCount = colInfo->desiredColumnCount();
26509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (minColumnCount >= desiredColumnCount) {
26609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        // The forced page breaks are in control of the balancing. Just set the column height to the
26709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        // maximum page break distance.
26809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        if (!pageLogicalHeight) {
269197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            LayoutUnit distanceBetweenBreaks = std::max<LayoutUnit>(colInfo->maximumDistanceBetweenForcedBreaks(),
270d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)                view()->layoutState()->pageLogicalOffset(*this, borderBefore() + paddingBefore() + layoutOverflowLogicalBottom) - colInfo->forcedBreakOffset());
271197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            columnHeight = std::max(colInfo->minimumColumnHeight(), distanceBetweenBreaks);
272a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        }
27309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    } else if (layoutOverflowLogicalBottom > boundedMultiply(pageLogicalHeight, desiredColumnCount)) {
27409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        // Now that we know the intrinsic height of the columns, we have to rebalance them.
275197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        columnHeight = std::max<LayoutUnit>(colInfo->minimumColumnHeight(), ceilf(layoutOverflowLogicalBottom.toFloat() / desiredColumnCount));
27609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    }
277a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
27809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (columnHeight && columnHeight != pageLogicalHeight) {
27909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        pageLogicalHeight = columnHeight;
28009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return true;
281a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    }
282a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
28309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return false;
28409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
28509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
28609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void RenderBlockFlow::setColumnCountAndHeight(unsigned count, LayoutUnit pageLogicalHeight)
28709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
28809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ColumnInfo* colInfo = columnInfo();
289a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    if (pageLogicalHeight)
29009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        colInfo->setColumnCountAndHeight(count, pageLogicalHeight);
291a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
292a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    if (columnCount(colInfo)) {
293a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        setLogicalHeight(borderBefore() + paddingBefore() + colInfo->columnHeight() + borderAfter() + paddingAfter() + scrollbarLogicalHeight());
294a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        m_overflow.clear();
295a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    }
29609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
297a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
298c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)void RenderBlockFlow::setBreakAtLineToAvoidWidow(int lineToBreak)
299c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles){
300c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    ASSERT(lineToBreak >= 0);
301c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    ensureRareData();
302c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    ASSERT(!m_rareData->m_didBreakAtLineToAvoidWidow);
303c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    m_rareData->m_lineBreakToAvoidWidow = lineToBreak;
304c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)}
305c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
306c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)void RenderBlockFlow::setDidBreakAtLineToAvoidWidow()
307c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles){
308c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    ASSERT(!shouldBreakAtLineToAvoidWidow());
309c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
310c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    // This function should be called only after a break was applied to avoid widows
311c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    // so assert |m_rareData| exists.
312c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    ASSERT(m_rareData);
313c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
314c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    m_rareData->m_didBreakAtLineToAvoidWidow = true;
315c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)}
316c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
317c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)void RenderBlockFlow::clearDidBreakAtLineToAvoidWidow()
318c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles){
319c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (!m_rareData)
320c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        return;
321c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
322c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    m_rareData->m_didBreakAtLineToAvoidWidow = false;
323c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)}
324c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
325c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)void RenderBlockFlow::clearShouldBreakAtLineToAvoidWidow() const
326c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles){
327c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    ASSERT(shouldBreakAtLineToAvoidWidow());
328c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (!m_rareData)
329c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        return;
330c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
331c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    m_rareData->m_lineBreakToAvoidWidow = -1;
332c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)}
333c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
33409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)bool RenderBlockFlow::isSelfCollapsingBlock() const
33509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
33609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_hasOnlySelfCollapsingChildren = RenderBlock::isSelfCollapsingBlock();
33709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return m_hasOnlySelfCollapsingChildren;
338a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
339a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
34009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void RenderBlockFlow::layoutBlock(bool relayoutChildren)
3418abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles){
3428abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    ASSERT(needsLayout());
34309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ASSERT(isInlineBlockOrInlineTable() || !isInline());
3448abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
34509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // If we are self-collapsing with self-collapsing descendants this will get set to save us burrowing through our
34609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // descendants every time in |isSelfCollapsingBlock|. We reset it here so that |isSelfCollapsingBlock| attempts to burrow
34709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // at least once and so that it always gives a reliable result reflecting the latest layout.
34809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_hasOnlySelfCollapsingChildren = false;
3498abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
3508abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    if (!relayoutChildren && simplifiedLayout())
3518abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        return;
3528abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
35310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    SubtreeLayoutScope layoutScope(*this);
35409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
35509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // Multiple passes might be required for column and pagination based layout
35609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // In the case of the old column code the number of passes will only be two
35709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // however, in the newer column code the number of passes could equal the
35809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // number of columns.
35909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool done = false;
36009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit pageLogicalHeight = 0;
36109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    while (!done)
36209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        done = layoutBlockFlow(relayoutChildren, pageLogicalHeight, layoutScope);
36307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
36407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    fitBorderToLinesIfNeeded();
36507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
36607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    RenderView* renderView = view();
36707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    if (renderView->layoutState()->pageLogicalHeight())
36807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch        setPageLogicalOffset(renderView->layoutState()->pageLogicalOffset(*this, logicalTop()));
36907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
37076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    updateLayerTransformAfterLayout();
37107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
37207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
37307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    // we overflow or not.
37407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    updateScrollInfoAfterLayout();
37507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
3769e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    if (m_paintInvalidationLogicalTop != m_paintInvalidationLogicalBottom) {
377e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        bool hasVisibleContent = style()->visibility() == VISIBLE;
378e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        if (!hasVisibleContent) {
379e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            RenderLayer* layer = enclosingLayer();
380e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            layer->updateDescendantDependentFlags();
381e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            hasVisibleContent = layer->hasVisibleContent();
382e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        }
383e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        if (hasVisibleContent)
384e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)            setShouldInvalidateOverflowForPaint(true);
385e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    }
386197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
3877242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (isHTMLDialogElement(node()) && isOutOfFlowPositioned())
3887242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        positionDialog();
3897242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
39007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    clearNeedsLayout();
39109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
39209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
39309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)inline bool RenderBlockFlow::layoutBlockFlow(bool relayoutChildren, LayoutUnit &pageLogicalHeight, SubtreeLayoutScope& layoutScope)
39409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
395d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    LayoutUnit oldLeft = logicalLeft();
396c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    bool logicalWidthChanged = updateLogicalWidthAndColumnWidth();
397c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    relayoutChildren |= logicalWidthChanged;
3988abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
3991e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    rebuildFloatsFromIntruding();
4008abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
4018abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    bool pageLogicalHeightChanged = false;
4028abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    bool hasSpecifiedPageLogicalHeight = false;
4038abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    checkForPaginationLogicalHeightChange(pageLogicalHeight, pageLogicalHeightChanged, hasSpecifiedPageLogicalHeight);
404a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    if (pageLogicalHeightChanged)
405a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        relayoutChildren = true;
4068abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
407c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    LayoutState state(*this, locationOffset(), pageLogicalHeight, pageLogicalHeightChanged, columnInfo(), logicalWidthChanged);
4088abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
4098abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // We use four values, maxTopPos, maxTopNeg, maxBottomPos, and maxBottomNeg, to track
4108abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // our current maximal positive and negative margins. These values are used when we
4118abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // are collapsed with adjacent blocks, so for example, if you have block A and B
4128abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // collapsing together, then you'd take the maximal positive margin from both A and B
4138abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // and subtract it from the maximal negative margin from both A and B to get the
4148abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // true collapsed margin. This algorithm is recursive, so when we finish layout()
4158abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // our block knows its current maximal positive/negative values.
4168abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    //
4178abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // Start out by setting our margin values to our current margins. Table cells have
4188abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // no margins, so we don't fill in the values for table cells.
41909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (!isTableCell()) {
4208abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        initMaxMarginValues();
42109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        setHasMarginBeforeQuirk(style()->hasMarginBeforeQuirk());
42209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        setHasMarginAfterQuirk(style()->hasMarginAfterQuirk());
4238abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        setPaginationStrut(0);
4248abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
4258abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
42609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit beforeEdge = borderBefore() + paddingBefore();
42709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit afterEdge = borderAfter() + paddingAfter() + scrollbarLogicalHeight();
42809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit previousHeight = logicalHeight();
42909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    setLogicalHeight(beforeEdge);
4308abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
4319e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    m_paintInvalidationLogicalTop = 0;
4329e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    m_paintInvalidationLogicalBottom = 0;
4338abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    if (!firstChild() && !isAnonymousBlock())
4348abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        setChildrenInline(true);
43509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
436c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    TextAutosizer::LayoutScope textAutosizerLayoutScope(this);
43709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
4388abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    if (childrenInline())
4399e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        layoutInlineChildren(relayoutChildren, m_paintInvalidationLogicalTop, m_paintInvalidationLogicalBottom, afterEdge);
4408abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    else
44110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        layoutBlockChildren(relayoutChildren, layoutScope, beforeEdge, afterEdge);
4428abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
4438abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // Expand our intrinsic height to encompass floats.
44409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (lowestFloatLogicalBottom() > (logicalHeight() - afterEdge) && createsBlockFormattingContext())
44509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        setLogicalHeight(lowestFloatLogicalBottom() + afterEdge);
4468abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
447d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (RenderMultiColumnFlowThread* flowThread = multiColumnFlowThread()) {
448d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        if (flowThread->recalculateColumnHeights()) {
44909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            setChildNeedsLayout(MarkOnlyThis);
45009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            return false;
45109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        }
45209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    } else if (hasColumns()) {
45309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        OwnPtr<RenderOverflow> savedOverflow = m_overflow.release();
45409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        if (childrenInline())
45509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            addOverflowFromInlineChildren();
45609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        else
45709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            addOverflowFromBlockChildren();
45809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        LayoutUnit layoutOverflowLogicalBottom = (isHorizontalWritingMode() ? layoutOverflowRect().maxY() : layoutOverflowRect().maxX()) - borderBefore() - paddingBefore();
45909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        m_overflow = savedOverflow.release();
46009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
46109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        if (!hasSpecifiedPageLogicalHeight && shouldRelayoutForPagination(pageLogicalHeight, layoutOverflowLogicalBottom)) {
46209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            setEverHadLayout(true);
46309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            return false;
46409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        }
46509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
46643e7502580f146aa5b3db8267ba6dbb5c733a489Torne (Richard Coles)        setColumnCountAndHeight(ceilf(layoutOverflowLogicalBottom.toFloat() / pageLogicalHeight.toFloat()), pageLogicalHeight.toFloat());
46709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    }
46809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
46909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (shouldBreakAtLineToAvoidWidow()) {
47009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        setEverHadLayout(true);
47109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return false;
472bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    }
4738abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
4748abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // Calculate our new height.
4758abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    LayoutUnit oldHeight = logicalHeight();
4768abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    LayoutUnit oldClientAfterEdge = clientLogicalBottom();
4778abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
4788abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    updateLogicalHeight();
4798abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    LayoutUnit newHeight = logicalHeight();
48010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    if (oldHeight > newHeight && !childrenInline()) {
48110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        // One of our children's floats may have become an overhanging float for us.
48210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
48310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            if (child->isRenderBlockFlow() && !child->isFloatingOrOutOfFlowPositioned()) {
48410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch                RenderBlockFlow* block = toRenderBlockFlow(child);
48510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch                if (block->lowestFloatLogicalBottom() + block->logicalTop() <= newHeight)
48610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch                    break;
48710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch                addOverhangingFloats(block, false);
4888abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            }
4898abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        }
4908abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
4918abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
4928abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    bool heightChanged = (previousHeight != newHeight);
4938abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    if (heightChanged)
4948abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        relayoutChildren = true;
4958abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
4966f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    layoutPositionedObjects(relayoutChildren || isDocumentElement(), oldLeft != logicalLeft() ? ForcedLayoutAfterContainingBlockMoved : DefaultLayout);
4978abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
4988abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // Add overflow from children (unless we're multi-column, since in that case all our child overflow is clipped anyway).
4998abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    computeOverflow(oldClientAfterEdge);
5008abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
501197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    m_descendantsWithFloatsMarkedForLayout = false;
50209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return true;
5038abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)}
5048abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
505197021e6b966cfb06891637935ef33fff06433d1Ben Murdochvoid RenderBlockFlow::determineLogicalLeftPositionForChild(RenderBox* child)
506d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
507d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    LayoutUnit startPosition = borderStart() + paddingStart();
5089e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutUnit initialStartPosition = startPosition;
509d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (style()->shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
510d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        startPosition -= verticalScrollbarWidth();
511d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    LayoutUnit totalAvailableLogicalWidth = borderAndPaddingLogicalWidth() + availableLogicalWidth();
512d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
513d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    LayoutUnit childMarginStart = marginStartForChild(child);
514d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    LayoutUnit newPosition = startPosition + childMarginStart;
515d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
5169e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutUnit positionToAvoidFloats;
517d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (child->avoidsFloats() && containsFloats() && !flowThreadContainingBlock())
5189e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        positionToAvoidFloats = startOffsetForLine(logicalTopForChild(child), false, logicalHeightForChild(child));
5199e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)
5209e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    // If the child has an offset from the content edge to avoid floats then use that, otherwise let any negative
5219e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    // margin pull it back over the content edge or any positive margin push it out.
5229e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    // If the child is being centred then the margin calculated to do that has factored in any offset required to
5239e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    // avoid floats, so use it if necessary.
5249e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    if (style()->textAlign() == WEBKIT_CENTER || child->style()->marginStartUsing(style()).isAuto())
5259e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        newPosition = std::max(newPosition, positionToAvoidFloats + childMarginStart);
5269e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    else if (positionToAvoidFloats > initialStartPosition)
5279e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        newPosition = std::max(newPosition, positionToAvoidFloats);
528d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
529197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    setLogicalLeftForChild(child, style()->isLeftToRightDirection() ? newPosition : totalAvailableLogicalWidth - newPosition - logicalWidthForChild(child));
530d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
531d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
532197021e6b966cfb06891637935ef33fff06433d1Ben Murdochvoid RenderBlockFlow::setLogicalLeftForChild(RenderBox* child, LayoutUnit logicalLeft)
533d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
534d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (isHorizontalWritingMode()) {
535d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        child->setX(logicalLeft);
536d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    } else {
537d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        child->setY(logicalLeft);
538d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
539d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
540d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
541197021e6b966cfb06891637935ef33fff06433d1Ben Murdochvoid RenderBlockFlow::setLogicalTopForChild(RenderBox* child, LayoutUnit logicalTop)
542d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
543d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    if (isHorizontalWritingMode()) {
544d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        child->setY(logicalTop);
545d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    } else {
546d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        child->setX(logicalTop);
547d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    }
548d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
549d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
55010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochvoid RenderBlockFlow::layoutBlockChild(RenderBox* child, MarginInfo& marginInfo, LayoutUnit& previousFloatLogicalBottom)
5519bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
5529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit oldPosMarginBefore = maxPositiveMarginBefore();
5539bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit oldNegMarginBefore = maxNegativeMarginBefore();
5549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
5559bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // The child is a normal flow object. Compute the margins we will use for collapsing now.
5569bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    child->computeAndSetBlockDirectionMargins(this);
5579bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
5589bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Try to guess our correct logical top position. In most cases this guess will
5599bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // be correct. Only if we're wrong (when we compute the real logical top position)
5609bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // will we have to potentially relayout.
5619bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit estimateWithoutPagination;
5629bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit logicalTopEstimate = estimateLogicalTopPosition(child, marginInfo, estimateWithoutPagination);
5639bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
5649e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    // Cache our old rect so that we can dirty the proper paint invalidation rects if the child moves.
5659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutRect oldRect = child->frameRect();
5669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit oldLogicalTop = logicalTopForChild(child);
5679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
5689bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Go ahead and position the child as though it didn't collapse with the top.
569197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    setLogicalTopForChild(child, logicalTopEstimate);
5709bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
571c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    RenderBlockFlow* childRenderBlockFlow = child->isRenderBlockFlow() ? toRenderBlockFlow(child) : 0;
5729bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool markDescendantsWithFloats = false;
573c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (logicalTopEstimate != oldLogicalTop && childRenderBlockFlow && !childRenderBlockFlow->avoidsFloats() && childRenderBlockFlow->containsFloats()) {
5749bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        markDescendantsWithFloats = true;
5759bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    } else if (UNLIKELY(logicalTopEstimate.mightBeSaturated())) {
5769bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // logicalTopEstimate, returned by estimateLogicalTopPosition, might be saturated for
5779bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // very large elements. If it does the comparison with oldLogicalTop might yield a
5789bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // false negative as adding and removing margins, borders etc from a saturated number
5799bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // might yield incorrect results. If this is the case always mark for layout.
5809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        markDescendantsWithFloats = true;
5819bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    } else if (!child->avoidsFloats() || child->shrinkToAvoidFloats()) {
5829bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // If an element might be affected by the presence of floats, then always mark it for
5839bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // layout.
584197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        LayoutUnit fb = std::max(previousFloatLogicalBottom, lowestFloatLogicalBottom());
5859bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (fb > logicalTopEstimate)
5869bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            markDescendantsWithFloats = true;
5879bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
5889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
589f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (childRenderBlockFlow) {
5909bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (markDescendantsWithFloats)
591f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            childRenderBlockFlow->markAllDescendantsWithFloatsForLayout();
5929bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (!child->isWritingModeRoot())
593197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            previousFloatLogicalBottom = std::max(previousFloatLogicalBottom, oldLogicalTop + childRenderBlockFlow->lowestFloatLogicalBottom());
5949bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
5959bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
59610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    SubtreeLayoutScope layoutScope(*child);
5979bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!child->needsLayout())
5989bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        child->markForPaginationRelayoutIfNeeded(layoutScope);
5999bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6009bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool childHadLayout = child->everHadLayout();
6019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool childNeededLayout = child->needsLayout();
6029bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (childNeededLayout)
6039bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        child->layout();
6049bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6059bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Cache if we are at the top of the block right now.
6069bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool atBeforeSideOfBlock = marginInfo.atBeforeSideOfBlock();
60709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool childIsSelfCollapsing = child->isSelfCollapsingBlock();
6089bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6099bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Now determine the correct ypos based off examination of collapsing margin
6109bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // values.
61109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit logicalTopBeforeClear = collapseMargins(child, marginInfo, childIsSelfCollapsing);
6129bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6139bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Now check for clear.
61409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit logicalTopAfterClear = clearFloatsIfNeeded(child, marginInfo, oldPosMarginBefore, oldNegMarginBefore, logicalTopBeforeClear, childIsSelfCollapsing);
6159bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6169bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool paginated = view()->layoutState()->isPaginated();
6179bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (paginated) {
6189bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        logicalTopAfterClear = adjustBlockChildForPagination(logicalTopAfterClear, estimateWithoutPagination, child,
6199bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            atBeforeSideOfBlock && logicalTopBeforeClear == logicalTopAfterClear);
6209bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
6219bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
622197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    setLogicalTopForChild(child, logicalTopAfterClear);
6239bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6249bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Now we have a final top position. See if it really does end up being different from our estimate.
6259bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // clearFloatsIfNeeded can also mark the child as needing a layout even though we didn't move. This happens
6269bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // when collapseMargins dynamically adds overhanging floats because of a child with negative margins.
627c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (logicalTopAfterClear != logicalTopEstimate || child->needsLayout() || (paginated && childRenderBlockFlow && childRenderBlockFlow->shouldBreakAtLineToAvoidWidow())) {
62810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        SubtreeLayoutScope layoutScope(*child);
6299bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (child->shrinkToAvoidFloats()) {
6309bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // The child's width depends on the line width.
6319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // When the child shifts to clear an item, its width can
6329bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // change (because it has more available line width).
6339bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // So go ahead and mark the item as dirty.
6349bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            layoutScope.setChildNeedsLayout(child);
6359bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
6369bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
637c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        if (childRenderBlockFlow && !childRenderBlockFlow->avoidsFloats() && childRenderBlockFlow->containsFloats())
638c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)            childRenderBlockFlow->markAllDescendantsWithFloatsForLayout();
639c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
640c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        if (!child->needsLayout())
641c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)            child->markForPaginationRelayoutIfNeeded(layoutScope);
6429bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6439bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Our guess was wrong. Make the child lay itself out again.
6449bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        child->layoutIfNeeded();
6459bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
6469bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
64709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // If we previously encountered a self-collapsing sibling of this child that had clearance then
64809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // we set this bit to ensure we would not collapse the child's margins, and those of any subsequent
64909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // self-collapsing siblings, with our parent. If this child is not self-collapsing then it can
65009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    // collapse its margins with the parent so reset the bit.
65109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (!marginInfo.canCollapseMarginAfterWithLastChild() && !childIsSelfCollapsing)
65209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        marginInfo.setCanCollapseMarginAfterWithLastChild(true);
65309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
6549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // We are no longer at the top of the block if we encounter a non-empty child.
6559bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // This has to be done after checking for clear, so that margins can be reset if a clear occurred.
65609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (marginInfo.atBeforeSideOfBlock() && !childIsSelfCollapsing)
6579bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        marginInfo.setAtBeforeSideOfBlock(false);
6589bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6599bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Now place the child in the correct left position
660197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    determineLogicalLeftPositionForChild(child);
6619bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
66206f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)    LayoutSize childOffset = child->location() - oldRect.location();
66306f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
6649bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Update our height now that the child has been placed in the correct position.
6659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    setLogicalHeight(logicalHeight() + logicalHeightForChild(child));
6669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (mustSeparateMarginAfterForChild(child)) {
6679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        setLogicalHeight(logicalHeight() + marginAfterForChild(child));
6689bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        marginInfo.clearMargin();
6699bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
6709bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // If the child has overhanging floats that intrude into following siblings (or possibly out
6719bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // of this block), then the parent gets notified of the floats now.
67210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    if (childRenderBlockFlow)
67310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        addOverhangingFloats(childRenderBlockFlow, !childNeededLayout);
6749bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
675197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // If the child moved, we have to invalidate it's paint  as well as any floating/positioned
676197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // descendants. An exception is if we need a layout. In this case, we know we're going to
677197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // invalidate our paint (and the child) anyway.
678197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    bool didNotDoFullLayoutAndMoved = childHadLayout && !selfNeedsLayout() && (childOffset.width() || childOffset.height());
679197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    bool didNotLayoutAndNeedsPaintInvalidation = !childHadLayout && child->checkForPaintInvalidation();
6809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
681197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (didNotDoFullLayoutAndMoved || didNotLayoutAndNeedsPaintInvalidation)
682197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        child->invalidatePaintForOverhangingFloats(true);
6839bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6849bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (paginated) {
6859bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Check for an after page/column break.
6869bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        LayoutUnit newHeight = applyAfterBreak(child, logicalHeight(), marginInfo);
6879bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (newHeight != height())
6889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            setLogicalHeight(newHeight);
6899bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
6909bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
6919bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6929bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)LayoutUnit RenderBlockFlow::adjustBlockChildForPagination(LayoutUnit logicalTopAfterClear, LayoutUnit estimateWithoutPagination, RenderBox* child, bool atBeforeSideOfBlock)
6939bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
694197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    RenderBlockFlow* childBlockFlow = child->isRenderBlockFlow() ? toRenderBlockFlow(child) : 0;
6959bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
6969bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (estimateWithoutPagination != logicalTopAfterClear) {
6979bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Our guess prior to pagination movement was wrong. Before we attempt to paginate, let's try again at the new
6989bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // position.
6999bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        setLogicalHeight(logicalTopAfterClear);
700197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        setLogicalTopForChild(child, logicalTopAfterClear);
7019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
7029bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (child->shrinkToAvoidFloats()) {
7039bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // The child's width depends on the line width.
7049bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // When the child shifts to clear an item, its width can
7059bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // change (because it has more available line width).
7069bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // So go ahead and mark the item as dirty.
7079bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            child->setChildNeedsLayout(MarkOnlyThis);
7089bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
7099bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
71010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        SubtreeLayoutScope layoutScope(*child);
7119bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
712197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if (childBlockFlow) {
713c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)            if (!childBlockFlow->avoidsFloats() && childBlockFlow->containsFloats())
714197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                childBlockFlow->markAllDescendantsWithFloatsForLayout();
7159bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            if (!child->needsLayout())
7169bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                child->markForPaginationRelayoutIfNeeded(layoutScope);
7179bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
7189bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
7199bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Our guess was wrong. Make the child lay itself out again.
7209bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        child->layoutIfNeeded();
7219bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
7229bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
7239bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit oldTop = logicalTopAfterClear;
7249bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
7259bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // If the object has a page or column break value of "before", then we should shift to the top of the next page.
7269bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit result = applyBeforeBreak(child, logicalTopAfterClear);
7279bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
7289bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
7299bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit logicalTopBeforeUnsplittableAdjustment = result;
7309bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit logicalTopAfterUnsplittableAdjustment = adjustForUnsplittableChild(child, result);
7319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
7329bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit paginationStrut = 0;
7339bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit unsplittableAdjustmentDelta = logicalTopAfterUnsplittableAdjustment - logicalTopBeforeUnsplittableAdjustment;
734d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    LayoutUnit childLogicalHeight = child->logicalHeight();
735d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    if (unsplittableAdjustmentDelta) {
736d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        setPageBreak(result, childLogicalHeight - unsplittableAdjustmentDelta);
7379bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        paginationStrut = unsplittableAdjustmentDelta;
738197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    } else if (childBlockFlow && childBlockFlow->paginationStrut()) {
739197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        paginationStrut = childBlockFlow->paginationStrut();
740d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    }
7419bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
7429bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (paginationStrut) {
7439bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // We are willing to propagate out to our parent block as long as we were at the top of the block prior
7449bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // to collapsing our margins, and as long as we didn't clear or move as a result of other pagination.
7459bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (atBeforeSideOfBlock && oldTop == result && !isOutOfFlowPositioned() && !isTableCell()) {
7469bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // FIXME: Should really check if we're exceeding the page height before propagating the strut, but we don't
7479bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // have all the information to do so (the strut only has the remaining amount to push). Gecko gets this wrong too
7489bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // and pushes to the next page anyway, so not too concerned about it.
7499bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            setPaginationStrut(result + paginationStrut);
750197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            if (childBlockFlow)
751197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                childBlockFlow->setPaginationStrut(0);
7529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        } else {
7539bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            result += paginationStrut;
7549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
7559bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
7569bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
757d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    if (!unsplittableAdjustmentDelta) {
758d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        if (LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(result)) {
759d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(result, ExcludePageBoundary);
760d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            LayoutUnit spaceShortage = childLogicalHeight - remainingLogicalHeight;
761d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            if (spaceShortage > 0) {
762d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                // If the child crosses a column boundary, report a break, in case nothing inside it
763d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                // has already done so. The column balancer needs to know how much it has to stretch
764d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                // the columns to make more content fit. If no breaks are reported (but do occur),
765d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                // the balancer will have no clue. Only measure the space after the last column
766d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                // boundary, in case it crosses more than one.
767d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                LayoutUnit spaceShortageInLastColumn = intMod(spaceShortage, pageLogicalHeight);
768d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                setPageBreak(result, spaceShortageInLastColumn ? spaceShortageInLastColumn : spaceShortage);
769d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            } else if (remainingLogicalHeight == pageLogicalHeight && offsetFromLogicalTopOfFirstPage() + child->logicalTop()) {
770d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                // We're at the very top of a page or column, and it's not the first one. This child
771d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                // may turn out to be the smallest piece of content that causes a page break, so we
772d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                // need to report it.
773d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                setPageBreak(result, childLogicalHeight);
774d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            }
775d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        }
776d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    }
777d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
7789bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Similar to how we apply clearance. Go ahead and boost height() to be the place where we're going to position the child.
7799bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    setLogicalHeight(logicalHeight() + (result - oldTop));
7809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
7819bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Return the final adjusted logical top.
7829bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return result;
7839bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
7849bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
785197021e6b966cfb06891637935ef33fff06433d1Ben Murdochstatic inline LayoutUnit calculateMinimumPageHeight(RenderStyle* renderStyle, RootInlineBox* lastLine, LayoutUnit lineTop, LayoutUnit lineBottom)
786197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch{
787197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // We may require a certain minimum number of lines per page in order to satisfy
788197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // orphans and widows, and that may affect the minimum page height.
789197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    unsigned lineCount = std::max<unsigned>(renderStyle->hasAutoOrphans() ? 1 : renderStyle->orphans(), renderStyle->hasAutoWidows() ? 1 : renderStyle->widows());
790197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (lineCount > 1) {
791197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        RootInlineBox* line = lastLine;
792197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        for (unsigned i = 1; i < lineCount && line->prevRootBox(); i++)
793197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            line = line->prevRootBox();
794197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
795197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        // FIXME: Paginating using line overflow isn't all fine. See FIXME in
796197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        // adjustLinePositionForPagination() for more details.
797197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        LayoutRect overflow = line->logicalVisualOverflowRect(line->lineTop(), line->lineBottom());
798197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        lineTop = std::min(line->lineTopWithLeading(), overflow.y());
799197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
800197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    return lineBottom - lineTop;
801197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch}
802197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
803197021e6b966cfb06891637935ef33fff06433d1Ben Murdochvoid RenderBlockFlow::adjustLinePositionForPagination(RootInlineBox* lineBox, LayoutUnit& delta, RenderFlowThread* flowThread)
804197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch{
805197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // FIXME: For now we paginate using line overflow. This ensures that lines don't overlap at all when we
806197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // put a strut between them for pagination purposes. However, this really isn't the desired rendering, since
807197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // the line on the top of the next page will appear too far down relative to the same kind of line at the top
808197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // of the first column.
809197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    //
810197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // The rendering we would like to see is one where the lineTopWithLeading is at the top of the column, and any line overflow
811197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // simply spills out above the top of the column. This effect would match what happens at the top of the first column.
812197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // We can't achieve this rendering, however, until we stop columns from clipping to the column bounds (thus allowing
813197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // for overflow to occur), and then cache visible overflow for each column rect.
814197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    //
815197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // Furthermore, the paint we have to do when a column has overflow has to be special. We need to exclude
816197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // content that paints in a previous column (and content that paints in the following column).
817197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    //
818197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // For now we'll at least honor the lineTopWithLeading when paginating if it is above the logical top overflow. This will
819197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // at least make positive leading work in typical cases.
820197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    //
821197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // FIXME: Another problem with simply moving lines is that the available line width may change (because of floats).
822197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // Technically if the location we move the line to has a different line width than our old position, then we need to dirty the
823197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // line and all following lines.
824197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    LayoutRect logicalVisualOverflow = lineBox->logicalVisualOverflowRect(lineBox->lineTop(), lineBox->lineBottom());
825197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    LayoutUnit logicalOffset = std::min(lineBox->lineTopWithLeading(), logicalVisualOverflow.y());
826197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    LayoutUnit logicalBottom = std::max(lineBox->lineBottomWithLeading(), logicalVisualOverflow.maxY());
827197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    LayoutUnit lineHeight = logicalBottom - logicalOffset;
828197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    updateMinimumPageHeight(logicalOffset, calculateMinimumPageHeight(style(), lineBox, logicalOffset, logicalBottom));
829197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    logicalOffset += delta;
830197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    lineBox->setPaginationStrut(0);
831197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    lineBox->setIsFirstAfterPageBreak(false);
832197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
833197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
834197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // If lineHeight is greater than pageLogicalHeight, but logicalVisualOverflow.height() still fits, we are
835197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // still going to add a strut, so that the visible overflow fits on a single page.
836197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (!pageLogicalHeight || (hasUniformPageLogicalHeight && logicalVisualOverflow.height() > pageLogicalHeight)) {
837197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        // FIXME: In case the line aligns with the top of the page (or it's slightly shifted downwards) it will not be marked as the first line in the page.
838197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        // From here, the fix is not straightforward because it's not easy to always determine when the current line is the first in the page.
839197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return;
840197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
841197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
842197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
843197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    int lineIndex = lineCount(lineBox);
844197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (remainingLogicalHeight < lineHeight || (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex)) {
845197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex) {
846197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            clearShouldBreakAtLineToAvoidWidow();
847197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            setDidBreakAtLineToAvoidWidow();
848197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        }
849197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if (lineHeight > pageLogicalHeight) {
850197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            // Split the top margin in order to avoid splitting the visible part of the line.
851197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            remainingLogicalHeight -= std::min(lineHeight - pageLogicalHeight, std::max<LayoutUnit>(0, logicalVisualOverflow.y() - lineBox->lineTopWithLeading()));
852197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        }
853197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        LayoutUnit totalLogicalHeight = lineHeight + std::max<LayoutUnit>(0, logicalOffset);
854197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        LayoutUnit pageLogicalHeightAtNewOffset = hasUniformPageLogicalHeight ? pageLogicalHeight : pageLogicalHeightForOffset(logicalOffset + remainingLogicalHeight);
855197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        setPageBreak(logicalOffset, lineHeight - remainingLogicalHeight);
856197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if (((lineBox == firstRootBox() && totalLogicalHeight < pageLogicalHeightAtNewOffset) || (!style()->hasAutoOrphans() && style()->orphans() >= lineIndex))
857197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            && !isOutOfFlowPositioned() && !isTableCell()) {
858197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            setPaginationStrut(remainingLogicalHeight + std::max<LayoutUnit>(0, logicalOffset));
859197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        } else {
860197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            delta += remainingLogicalHeight;
861197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            lineBox->setPaginationStrut(remainingLogicalHeight);
862197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            lineBox->setIsFirstAfterPageBreak(true);
863197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        }
864197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    } else if (remainingLogicalHeight == pageLogicalHeight) {
865197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        // We're at the very top of a page or column.
866197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if (lineBox != firstRootBox())
867197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            lineBox->setIsFirstAfterPageBreak(true);
868197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if (lineBox != firstRootBox() || offsetFromLogicalTopOfFirstPage())
869197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            setPageBreak(logicalOffset, lineHeight);
870197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
871197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch}
872197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
8737242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano TucciLayoutUnit RenderBlockFlow::adjustForUnsplittableChild(RenderBox* child, LayoutUnit logicalOffset, bool includeMargins)
8747242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci{
8757242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    bool checkColumnBreaks = view()->layoutState()->isPaginatingColumns() || flowThreadContainingBlock();
8767242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    bool checkPageBreaks = !checkColumnBreaks && view()->layoutState()->pageLogicalHeight();
8777242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    bool isUnsplittable = child->isUnsplittableForPagination() || (checkColumnBreaks && child->style()->columnBreakInside() == PBAVOID)
8787242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        || (checkPageBreaks && child->style()->pageBreakInside() == PBAVOID);
8797242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (!isUnsplittable)
8807242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return logicalOffset;
8817242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    LayoutUnit childLogicalHeight = logicalHeightForChild(child) + (includeMargins ? marginBeforeForChild(child) + marginAfterForChild(child) : LayoutUnit());
8827242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
8837242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    updateMinimumPageHeight(logicalOffset, childLogicalHeight);
8847242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (!pageLogicalHeight || childLogicalHeight > pageLogicalHeight)
8857242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return logicalOffset;
8867242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
8877242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (remainingLogicalHeight < childLogicalHeight)
8887242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return logicalOffset + remainingLogicalHeight;
8897242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    return logicalOffset;
8907242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci}
8917242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
8921e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)void RenderBlockFlow::rebuildFloatsFromIntruding()
8938abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles){
8948abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    if (m_floatingObjects)
8958abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        m_floatingObjects->setHorizontalWritingMode(isHorizontalWritingMode());
8968abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
8978abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    HashSet<RenderBox*> oldIntrudingFloatSet;
8988abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    if (!childrenInline() && m_floatingObjects) {
8998abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
9008abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        FloatingObjectSetIterator end = floatingObjectSet.end();
9018abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
902d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            FloatingObject* floatingObject = it->get();
9038abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            if (!floatingObject->isDescendant())
9048abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                oldIntrudingFloatSet.add(floatingObject->renderer());
9058abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        }
9068abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
9078abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
9088abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // Inline blocks are covered by the isReplaced() check in the avoidFloats method.
909197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (avoidsFloats() || isDocumentElement() || isRenderView() || isFloatingOrOutOfFlowPositioned() || isTableCell()) {
9108abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        if (m_floatingObjects) {
9118abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            m_floatingObjects->clear();
9128abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        }
9138abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        if (!oldIntrudingFloatSet.isEmpty())
9148abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            markAllDescendantsWithFloatsForLayout();
9158abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        return;
9168abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
9178abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
9188abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    RendererToFloatInfoMap floatMap;
9198abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
9208abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    if (m_floatingObjects) {
9219bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (childrenInline())
9229bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            m_floatingObjects->moveAllToFloatInfoMap(floatMap);
9239bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        else
9249bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            m_floatingObjects->clear();
9258abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
9268abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
927f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // We should not process floats if the parent node is not a RenderBlockFlow. Otherwise, we will add
9288abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // floats in an invalid context. This will cause a crash arising from a bad cast on the parent.
9298abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // See <rdar://problem/8049753>, where float property is applied on a text node in a SVG.
930f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!parent() || !parent()->isRenderBlockFlow())
9318abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        return;
9328abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
933f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    // Attempt to locate a previous sibling with overhanging floats. We skip any elements that
934f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    // may have shifted to avoid floats, and any objects whose floats cannot interact with objects
935f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    // outside it (i.e. objects that create a new block formatting context).
936f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    RenderBlockFlow* parentBlockFlow = toRenderBlockFlow(parent());
9378abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    bool parentHasFloats = false;
9388abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    RenderObject* prev = previousSibling();
939197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    while (prev && (!prev->isBox() || !prev->isRenderBlock() || toRenderBlock(prev)->avoidsFloats() || toRenderBlock(prev)->createsBlockFormattingContext())) {
9408abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        if (prev->isFloating())
9418abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            parentHasFloats = true;
9428abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        prev = prev->previousSibling();
9438abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
9448abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
945a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // First add in floats from the parent. Self-collapsing blocks let their parent track any floats that intrude into
946a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // them (as opposed to floats they contain themselves) so check for those here too.
9478abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    LayoutUnit logicalTopOffset = logicalTop();
94810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    bool parentHasIntrudingFloats = !parentHasFloats && (!prev || toRenderBlockFlow(prev)->isSelfCollapsingBlock()) && parentBlockFlow->lowestFloatLogicalBottom() > logicalTopOffset;
94910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    if (parentHasFloats || parentHasIntrudingFloats)
950f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        addIntrudingFloats(parentBlockFlow, parentBlockFlow->logicalLeftOffsetForContent(), logicalTopOffset);
9518abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
95210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    // Add overhanging floats from the previous RenderBlockFlow, but only if it has a float that intrudes into our space.
9538abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    if (prev) {
95410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        RenderBlockFlow* blockFlow = toRenderBlockFlow(prev);
95510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        logicalTopOffset -= blockFlow->logicalTop();
95610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        if (blockFlow->lowestFloatLogicalBottom() > logicalTopOffset)
95710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch            addIntrudingFloats(blockFlow, 0, logicalTopOffset);
9588abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
9598abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
9608abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    if (childrenInline()) {
9618abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        LayoutUnit changeLogicalTop = LayoutUnit::max();
9628abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        LayoutUnit changeLogicalBottom = LayoutUnit::min();
9638abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        if (m_floatingObjects) {
9648abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
9658abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            FloatingObjectSetIterator end = floatingObjectSet.end();
9668abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
967d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                FloatingObject* floatingObject = it->get();
968f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                FloatingObject* oldFloatingObject = floatMap.get(floatingObject->renderer());
969f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
9708abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                if (oldFloatingObject) {
971f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    LayoutUnit oldLogicalBottom = logicalBottomForFloat(oldFloatingObject);
972f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    if (logicalWidthForFloat(floatingObject) != logicalWidthForFloat(oldFloatingObject) || logicalLeftForFloat(floatingObject) != logicalLeftForFloat(oldFloatingObject)) {
9738abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                        changeLogicalTop = 0;
974197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                        changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalBottom, oldLogicalBottom));
9758abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                    } else {
9768abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                        if (logicalBottom != oldLogicalBottom) {
977197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                            changeLogicalTop = std::min(changeLogicalTop, std::min(logicalBottom, oldLogicalBottom));
978197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                            changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalBottom, oldLogicalBottom));
9798abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                        }
980f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                        LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
981f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                        LayoutUnit oldLogicalTop = logicalTopForFloat(oldFloatingObject);
9828abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                        if (logicalTop != oldLogicalTop) {
983197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                            changeLogicalTop = std::min(changeLogicalTop, std::min(logicalTop, oldLogicalTop));
984197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                            changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalTop, oldLogicalTop));
9858abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                        }
9868abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                    }
9878abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
9888abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                    if (oldFloatingObject->originatingLine() && !selfNeedsLayout()) {
9898abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                        ASSERT(oldFloatingObject->originatingLine()->renderer() == this);
9908abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                        oldFloatingObject->originatingLine()->markDirty();
9918abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                    }
992d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)
993d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)                    floatMap.remove(floatingObject->renderer());
9948abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                } else {
9958abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                    changeLogicalTop = 0;
996197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                    changeLogicalBottom = std::max(changeLogicalBottom, logicalBottom);
9978abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                }
9988abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            }
9998abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        }
10008abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10018abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        RendererToFloatInfoMap::iterator end = floatMap.end();
10028abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        for (RendererToFloatInfoMap::iterator it = floatMap.begin(); it != end; ++it) {
1003d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            OwnPtr<FloatingObject>& floatingObject = it->value;
10048abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            if (!floatingObject->isDescendant()) {
10058abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                changeLogicalTop = 0;
1006197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                changeLogicalBottom = std::max(changeLogicalBottom, logicalBottomForFloat(floatingObject.get()));
10078abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            }
10088abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        }
10098abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10108abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        markLinesDirtyInBlockRange(changeLogicalTop, changeLogicalBottom);
10118abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    } else if (!oldIntrudingFloatSet.isEmpty()) {
10128abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        // If there are previously intruding floats that no longer intrude, then children with floats
10138abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        // should also get layout because they might need their floating object lists cleared.
10148abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        if (m_floatingObjects->set().size() < oldIntrudingFloatSet.size()) {
10158abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            markAllDescendantsWithFloatsForLayout();
10168abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        } else {
10178abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
10188abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            FloatingObjectSetIterator end = floatingObjectSet.end();
10198abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end && !oldIntrudingFloatSet.isEmpty(); ++it)
10208abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                oldIntrudingFloatSet.remove((*it)->renderer());
10218abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            if (!oldIntrudingFloatSet.isEmpty())
10228abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)                markAllDescendantsWithFloatsForLayout();
10238abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        }
10248abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
10258abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)}
10268abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
102710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochvoid RenderBlockFlow::layoutBlockChildren(bool relayoutChildren, SubtreeLayoutScope& layoutScope, LayoutUnit beforeEdge, LayoutUnit afterEdge)
10288abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles){
10298abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    dirtyForLayoutFromPercentageHeightDescendants(layoutScope);
10308abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10318abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // The margin struct caches all our current margin collapsing state. The compact struct caches state when we encounter compacts,
10328abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    MarginInfo marginInfo(this, beforeEdge, afterEdge);
10338abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10348abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // Fieldsets need to find their legend and position it inside the border of the object.
10358abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // The legend then gets skipped during normal layout. The same is true for ruby text.
10368abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // It doesn't get included in the normal layout process but is instead skipped.
10378abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    RenderObject* childToExclude = layoutSpecialExcludedChild(relayoutChildren, layoutScope);
10388abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10398abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    LayoutUnit previousFloatLogicalBottom = 0;
10408abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10418abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    RenderBox* next = firstChildBox();
104209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    RenderBox* lastNormalFlowChild = 0;
10438abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10448abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    while (next) {
10458abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        RenderBox* child = next;
10468abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        next = child->nextSiblingBox();
10478abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
1048a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        // FIXME: this should only be set from clearNeedsLayout crbug.com/361250
1049a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        child->setLayoutDidGetCalled(true);
1050a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
10518abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        if (childToExclude == child)
10528abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            continue; // Skip this child, since it will be positioned by the specialized subclass (fieldsets and ruby runs).
10538abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10548abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        updateBlockChildDirtyBitsBeforeLayout(relayoutChildren, child);
10558abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10568abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        if (child->isOutOfFlowPositioned()) {
10578abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            child->containingBlock()->insertPositionedObject(child);
10588abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            adjustPositionedBlock(child, marginInfo);
10598abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            continue;
10608abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        }
10618abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        if (child->isFloating()) {
10628abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            insertFloatingObject(child);
10638abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            adjustFloatingBlock(marginInfo);
10648abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)            continue;
10658abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        }
10668abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10678abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)        // Lay out the child.
106810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        layoutBlockChild(child, marginInfo, previousFloatLogicalBottom);
106909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        lastNormalFlowChild = child;
10708abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    }
10718abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10728abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // Now do the handling of the bottom of the block, adding in our bottom border/padding and
10738abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)    // determining the correct collapsed bottom margin information.
107409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    handleAfterSideOfBlock(lastNormalFlowChild, beforeEdge, afterEdge, marginInfo);
10758abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)}
10768abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
10779bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)// Our MarginInfo state used when laying out block children.
10789bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)MarginInfo::MarginInfo(RenderBlockFlow* blockFlow, LayoutUnit beforeBorderPadding, LayoutUnit afterBorderPadding)
107909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    : m_canCollapseMarginAfterWithLastChild(true)
108009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    , m_atBeforeSideOfBlock(true)
10819bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    , m_atAfterSideOfBlock(false)
10829bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    , m_hasMarginBeforeQuirk(false)
10839bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    , m_hasMarginAfterQuirk(false)
10849bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    , m_determinedMarginBeforeQuirk(false)
10859bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    , m_discardMargin(false)
10869bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
10879bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    RenderStyle* blockStyle = blockFlow->style();
10889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    ASSERT(blockFlow->isRenderView() || blockFlow->parent());
108909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_canCollapseWithChildren = !blockFlow->createsBlockFormattingContext() && !blockFlow->isRenderFlowThread() && !blockFlow->isRenderView();
10909bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
10919bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_canCollapseMarginBeforeWithChildren = m_canCollapseWithChildren && !beforeBorderPadding && blockStyle->marginBeforeCollapse() != MSEPARATE;
10929bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
10939bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // If any height other than auto is specified in CSS, then we don't collapse our bottom
10949bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // margins with our children's margins. To do otherwise would be to risk odd visual
10959bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // effects when the children overflow out of the parent block and yet still collapse
10969bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // with it. We also don't collapse if we have any bottom border/padding.
10979bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_canCollapseMarginAfterWithChildren = m_canCollapseWithChildren && !afterBorderPadding
10989bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        && (blockStyle->logicalHeight().isAuto() && !blockStyle->logicalHeight().value()) && blockStyle->marginAfterCollapse() != MSEPARATE;
10999bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11009bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_quirkContainer = blockFlow->isTableCell() || blockFlow->isBody();
11019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11029bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_discardMargin = m_canCollapseMarginBeforeWithChildren && blockFlow->mustDiscardMarginBefore();
11039bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11049bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_positiveMargin = (m_canCollapseMarginBeforeWithChildren && !blockFlow->mustDiscardMarginBefore()) ? blockFlow->maxPositiveMarginBefore() : LayoutUnit();
11059bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_negativeMargin = (m_canCollapseMarginBeforeWithChildren && !blockFlow->mustDiscardMarginBefore()) ? blockFlow->maxNegativeMarginBefore() : LayoutUnit();
11069bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
11079bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11089bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)RenderBlockFlow::MarginValues RenderBlockFlow::marginValuesForChild(RenderBox* child) const
11099bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
11109bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit childBeforePositive = 0;
11119bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit childBeforeNegative = 0;
11129bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit childAfterPositive = 0;
11139bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit childAfterNegative = 0;
11149bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11159bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit beforeMargin = 0;
11169bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit afterMargin = 0;
11179bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11189bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    RenderBlockFlow* childRenderBlockFlow = child->isRenderBlockFlow() ? toRenderBlockFlow(child) : 0;
11199bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11209bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // If the child has the same directionality as we do, then we can just return its
11219bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // margins in the same direction.
11229bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!child->isWritingModeRoot()) {
11239bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (childRenderBlockFlow) {
11249bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childBeforePositive = childRenderBlockFlow->maxPositiveMarginBefore();
11259bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childBeforeNegative = childRenderBlockFlow->maxNegativeMarginBefore();
11269bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childAfterPositive = childRenderBlockFlow->maxPositiveMarginAfter();
11279bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childAfterNegative = childRenderBlockFlow->maxNegativeMarginAfter();
11289bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        } else {
11299bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            beforeMargin = child->marginBefore();
11309bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            afterMargin = child->marginAfter();
11319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
11329bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    } else if (child->isHorizontalWritingMode() == isHorizontalWritingMode()) {
11339bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // The child has a different directionality. If the child is parallel, then it's just
11349bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // flipped relative to us. We can use the margins for the opposite edges.
11359bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (childRenderBlockFlow) {
11369bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childBeforePositive = childRenderBlockFlow->maxPositiveMarginAfter();
11379bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childBeforeNegative = childRenderBlockFlow->maxNegativeMarginAfter();
11389bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childAfterPositive = childRenderBlockFlow->maxPositiveMarginBefore();
11399bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childAfterNegative = childRenderBlockFlow->maxNegativeMarginBefore();
11409bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        } else {
11419bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            beforeMargin = child->marginAfter();
11429bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            afterMargin = child->marginBefore();
11439bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
11449bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    } else {
11459bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // The child is perpendicular to us, which means its margins don't collapse but are on the
11469bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // "logical left/right" sides of the child box. We can just return the raw margin in this case.
11479bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        beforeMargin = marginBeforeForChild(child);
11489bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        afterMargin = marginAfterForChild(child);
11499bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
11509bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11519bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Resolve uncollapsing margins into their positive/negative buckets.
11529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (beforeMargin) {
11539bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (beforeMargin > 0)
11549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childBeforePositive = beforeMargin;
11559bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        else
11569bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childBeforeNegative = -beforeMargin;
11579bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
11589bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (afterMargin) {
11599bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (afterMargin > 0)
11609bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childAfterPositive = afterMargin;
11619bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        else
11629bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            childAfterNegative = -afterMargin;
11639bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
11649bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return RenderBlockFlow::MarginValues(childBeforePositive, childBeforeNegative, childAfterPositive, childAfterNegative);
11669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
11679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
116809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)LayoutUnit RenderBlockFlow::collapseMargins(RenderBox* child, MarginInfo& marginInfo, bool childIsSelfCollapsing)
11699bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
11709bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool childDiscardMarginBefore = mustDiscardMarginBeforeForChild(child);
11719bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool childDiscardMarginAfter = mustDiscardMarginAfterForChild(child);
11729bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11739bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // The child discards the before margin when the the after margin has discard in the case of a self collapsing block.
11749bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    childDiscardMarginBefore = childDiscardMarginBefore || (childDiscardMarginAfter && childIsSelfCollapsing);
11759bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11769bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Get the four margin values for the child and cache them.
11779bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    const RenderBlockFlow::MarginValues childMargins = marginValuesForChild(child);
11789bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11799bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Get our max pos and neg top margins.
11809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit posTop = childMargins.positiveMarginBefore();
11819bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit negTop = childMargins.negativeMarginBefore();
11829bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11839bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // For self-collapsing blocks, collapse our bottom margins into our
11849bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // top to get new posTop and negTop values.
11859bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (childIsSelfCollapsing) {
1186197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        posTop = std::max(posTop, childMargins.positiveMarginAfter());
1187197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        negTop = std::max(negTop, childMargins.negativeMarginAfter());
11889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
11899bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11909bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // See if the top margin is quirky. We only care if this child has
11919bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // margins that will collapse with us.
11929bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool topQuirk = hasMarginBeforeQuirk(child);
11939bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
11949bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (marginInfo.canCollapseWithMarginBefore()) {
11959bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
11969bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // This child is collapsing with the top of the
11979bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // block. If it has larger margin values, then we need to update
11989bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // our own maximal values.
11999bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            if (!document().inQuirksMode() || !marginInfo.quirkContainer() || !topQuirk)
1200197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                setMaxMarginBeforeValues(std::max(posTop, maxPositiveMarginBefore()), std::max(negTop, maxNegativeMarginBefore()));
12019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
12029bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // The minute any of the margins involved isn't a quirk, don't
12039bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // collapse it away, even if the margin is smaller (www.webreference.com
12049bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // has an example of this, a <dt> with 0.8em author-specified inside
12059bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // a <dl> inside a <td>.
12069bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            if (!marginInfo.determinedMarginBeforeQuirk() && !topQuirk && (posTop - negTop)) {
12079bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                setHasMarginBeforeQuirk(false);
12089bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                marginInfo.setDeterminedMarginBeforeQuirk(true);
12099bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            }
12109bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
12119bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            if (!marginInfo.determinedMarginBeforeQuirk() && topQuirk && !marginBefore()) {
12129bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                // We have no top margin and our top child has a quirky margin.
12139bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                // We will pick up this quirky margin and pass it through.
12149bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                // This deals with the <td><div><p> case.
12159bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                // Don't do this for a block that split two inlines though. You do
12169bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                // still apply margins in this case.
12179bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                setHasMarginBeforeQuirk(true);
12189bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            }
12199bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        } else {
12209bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // The before margin of the container will also discard all the margins it is collapsing with.
12219bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            setMustDiscardMarginBefore();
12229bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
12239bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
12249bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
12259bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Once we find a child with discardMarginBefore all the margins collapsing with us must also discard.
12269bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (childDiscardMarginBefore) {
12279bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        marginInfo.setDiscardMargin(true);
12289bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        marginInfo.clearMargin();
12299bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
12309bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
12319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (marginInfo.quirkContainer() && marginInfo.atBeforeSideOfBlock() && (posTop - negTop))
12329bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        marginInfo.setHasMarginBeforeQuirk(topQuirk);
12339bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
12349bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit beforeCollapseLogicalTop = logicalHeight();
12359bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit logicalTop = beforeCollapseLogicalTop;
1236a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
1237a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    LayoutUnit clearanceForSelfCollapsingBlock;
1238a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    RenderObject* prev = child->previousSibling();
123909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    RenderBlockFlow* previousBlockFlow =  prev && prev->isRenderBlockFlow() && !prev->isFloatingOrOutOfFlowPositioned() ? toRenderBlockFlow(prev) : 0;
1240a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // If the child's previous sibling is a self-collapsing block that cleared a float then its top border edge has been set at the bottom border edge
1241a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // of the float. Since we want to collapse the child's top margin with the self-collapsing block's top and bottom margins we need to adjust our parent's height to match the
1242a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // margin top of the self-collapsing block. If the resulting collapsed margin leaves the child still intruding into the float then we will want to clear it.
124309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (!marginInfo.canCollapseWithMarginBefore() && previousBlockFlow && previousBlockFlow->isSelfCollapsingBlock()) {
124409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        clearanceForSelfCollapsingBlock = previousBlockFlow->marginOffsetForSelfCollapsingBlock();
1245a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        setLogicalHeight(logicalHeight() - clearanceForSelfCollapsingBlock);
1246a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    }
1247a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
12489bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (childIsSelfCollapsing) {
12499bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // For a self collapsing block both the before and after margins get discarded. The block doesn't contribute anything to the height of the block.
12509bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Also, the child's top position equals the logical height of the container.
12519bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
12529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // This child has no height. We need to compute our
12539bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // position before we collapse the child's margins together,
12549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // so that we can get an accurate position for the zero-height block.
1255197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            LayoutUnit collapsedBeforePos = std::max(marginInfo.positiveMargin(), childMargins.positiveMarginBefore());
1256197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            LayoutUnit collapsedBeforeNeg = std::max(marginInfo.negativeMargin(), childMargins.negativeMarginBefore());
12579bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            marginInfo.setMargin(collapsedBeforePos, collapsedBeforeNeg);
12589bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
12599bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // Now collapse the child's margins together, which means examining our
12609bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // bottom margin values as well.
12619bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            marginInfo.setPositiveMarginIfLarger(childMargins.positiveMarginAfter());
12629bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            marginInfo.setNegativeMarginIfLarger(childMargins.negativeMarginAfter());
12639bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
12649bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            if (!marginInfo.canCollapseWithMarginBefore()) {
12659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                // We need to make sure that the position of the self-collapsing block
12669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                // is correct, since it could have overflowing content
12679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                // that needs to be positioned correctly (e.g., a block that
12689bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                // had a specified height of 0 but that actually had subcontent).
12699bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)                logicalTop = logicalHeight() + collapsedBeforePos - collapsedBeforeNeg;
12709bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            }
12719bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
12729bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    } else {
12739bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (mustSeparateMarginBeforeForChild(child)) {
12749bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            ASSERT(!marginInfo.discardMargin() || (marginInfo.discardMargin() && !marginInfo.margin()));
12759bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // If we are at the before side of the block and we collapse, ignore the computed margin
12769bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // and just add the child margin to the container height. This will correctly position
12779bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // the child inside the container.
12789bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            LayoutUnit separateMargin = !marginInfo.canCollapseWithMarginBefore() ? marginInfo.margin() : LayoutUnit(0);
12799bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            setLogicalHeight(logicalHeight() + separateMargin + marginBeforeForChild(child));
12809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            logicalTop = logicalHeight();
12819bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        } else if (!marginInfo.discardMargin() && (!marginInfo.atBeforeSideOfBlock()
12829bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            || (!marginInfo.canCollapseMarginBeforeWithChildren()
12839bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            && (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginBeforeQuirk())))) {
12849bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // We're collapsing with a previous sibling's margins and not
12859bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // with the top of the block.
1286197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            setLogicalHeight(logicalHeight() + std::max(marginInfo.positiveMargin(), posTop) - std::max(marginInfo.negativeMargin(), negTop));
12879bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            logicalTop = logicalHeight();
12889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
12899bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
12909bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        marginInfo.setDiscardMargin(childDiscardMarginAfter);
12919bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
12929bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (!marginInfo.discardMargin()) {
12939bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            marginInfo.setPositiveMargin(childMargins.positiveMarginAfter());
12949bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            marginInfo.setNegativeMargin(childMargins.negativeMarginAfter());
12959bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        } else {
12969bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            marginInfo.clearMargin();
12979bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
12989bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
12999bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (marginInfo.margin())
13009bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            marginInfo.setHasMarginAfterQuirk(hasMarginAfterQuirk(child));
13019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
13029bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
13039bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // If margins would pull us past the top of the next page, then we need to pull back and pretend like the margins
13049bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // collapsed into the page edge.
13059bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutState* layoutState = view()->layoutState();
130609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTop > beforeCollapseLogicalTop) {
13079bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        LayoutUnit oldLogicalTop = logicalTop;
1308197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        logicalTop = std::min(logicalTop, nextPageLogicalTop(beforeCollapseLogicalTop));
13099bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        setLogicalHeight(logicalHeight() + (logicalTop - oldLogicalTop));
13109bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
13119bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
131209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (previousBlockFlow) {
1313a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // If |child| is a self-collapsing block it may have collapsed into a previous sibling and although it hasn't reduced the height of the parent yet
1314a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // any floats from the parent will now overhang.
1315a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        LayoutUnit oldLogicalHeight = logicalHeight();
1316a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        setLogicalHeight(logicalTop);
131710f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        if (!previousBlockFlow->avoidsFloats() && (previousBlockFlow->logicalTop() + previousBlockFlow->lowestFloatLogicalBottom()) > logicalTop)
131809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            addOverhangingFloats(previousBlockFlow, false);
1319a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        setLogicalHeight(oldLogicalHeight);
1320a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
1321a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // If |child|'s previous sibling is a self-collapsing block that cleared a float and margin collapsing resulted in |child| moving up
1322a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // into the margin area of the self-collapsing block then the float it clears is now intruding into |child|. Layout again so that we can look for
1323a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // floats in the parent that overhang |child|'s new logical top.
1324a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        bool logicalTopIntrudesIntoFloat = clearanceForSelfCollapsingBlock > 0 && logicalTop < beforeCollapseLogicalTop;
1325a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        if (logicalTopIntrudesIntoFloat && containsFloats() && !child->avoidsFloats() && lowestFloatLogicalBottom() > logicalTop)
13265d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)            child->setNeedsLayoutAndFullPaintInvalidation();
13279bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
13289bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
13299bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return logicalTop;
13309bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
13319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
13329bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)void RenderBlockFlow::adjustPositionedBlock(RenderBox* child, const MarginInfo& marginInfo)
13339bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
13349bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool isHorizontal = isHorizontalWritingMode();
13359bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    bool hasStaticBlockPosition = child->style()->hasStaticBlockPosition(isHorizontal);
13369bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
13379bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit logicalTop = logicalHeight();
13389bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    updateStaticInlinePositionForChild(child, logicalTop);
13399bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
13409bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!marginInfo.canCollapseWithMarginBefore()) {
13419bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Positioned blocks don't collapse margins, so add the margin provided by
13429bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // the container now. The child's own margin is added later when calculating its logical top.
13439bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        LayoutUnit collapsedBeforePos = marginInfo.positiveMargin();
13449bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        LayoutUnit collapsedBeforeNeg = marginInfo.negativeMargin();
13459bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        logicalTop += collapsedBeforePos - collapsedBeforeNeg;
13469bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
13479bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
13489bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    RenderLayer* childLayer = child->layer();
13499bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (childLayer->staticBlockPosition() != logicalTop) {
13509bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        childLayer->setStaticBlockPosition(logicalTop);
13519bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (hasStaticBlockPosition)
13529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            child->setChildNeedsLayout(MarkOnlyThis);
13539bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
13549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
13559bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
135609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)LayoutUnit RenderBlockFlow::clearFloatsIfNeeded(RenderBox* child, MarginInfo& marginInfo, LayoutUnit oldTopPosMargin, LayoutUnit oldTopNegMargin, LayoutUnit yPos, bool childIsSelfCollapsing)
13579bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
13589bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit heightIncrease = getClearDelta(child, yPos);
13599bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!heightIncrease)
13609bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return yPos;
13619bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
136209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (childIsSelfCollapsing) {
13639bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        bool childDiscardMargin = mustDiscardMarginBeforeForChild(child) || mustDiscardMarginAfterForChild(child);
13649bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
13659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // For self-collapsing blocks that clear, they can still collapse their
13669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // margins with following siblings. Reset the current margins to represent
13679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // the self-collapsing block's margins only.
13689bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // If DISCARD is specified for -webkit-margin-collapse, reset the margin values.
1369a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        RenderBlockFlow::MarginValues childMargins = marginValuesForChild(child);
13709bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (!childDiscardMargin) {
1371197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            marginInfo.setPositiveMargin(std::max(childMargins.positiveMarginBefore(), childMargins.positiveMarginAfter()));
1372197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            marginInfo.setNegativeMargin(std::max(childMargins.negativeMarginBefore(), childMargins.negativeMarginAfter()));
13739bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        } else {
13749bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            marginInfo.clearMargin();
13759bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
13769bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        marginInfo.setDiscardMargin(childDiscardMargin);
13779bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
13789bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // CSS2.1 states:
13799bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // "If the top and bottom margins of an element with clearance are adjoining, its margins collapse with
13809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // the adjoining margins of following siblings but that resulting margin does not collapse with the bottom margin of the parent block."
138109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        // So the parent's bottom margin cannot collapse through this block or any subsequent self-collapsing blocks. Set a bit to ensure
138209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        // this happens; it will get reset if we encounter an in-flow sibling that is not self-collapsing.
138309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        marginInfo.setCanCollapseMarginAfterWithLastChild(false);
13849bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
1385a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // For now set the border-top of |child| flush with the bottom border-edge of the float so it can layout any floating or positioned children of
1386a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // its own at the correct vertical position. If subsequent siblings attempt to collapse with |child|'s margins in |collapseMargins| we will
1387a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // adjust the height of the parent to |child|'s margin top (which if it is positive sits up 'inside' the float it's clearing) so that all three
1388a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // margins can collapse at the correct vertical position.
1389a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // Per CSS2.1 we need to ensure that any negative margin-top clears |child| beyond the bottom border-edge of the float so that the top border edge of the child
1390a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // (i.e. its clearance)  is at a position that satisfies the equation: "the amount of clearance is set so that clearance + margin-top = [height of float],
1391a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // i.e., clearance = [height of float] - margin-top".
1392a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        setLogicalHeight(child->logicalTop() + childMargins.negativeMarginBefore());
13939bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    } else {
13949bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Increase our height by the amount we had to clear.
13959bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        setLogicalHeight(logicalHeight() + heightIncrease);
13969bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
13979bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
13989bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (marginInfo.canCollapseWithMarginBefore()) {
13999bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // We can no longer collapse with the top of the block since a clear
14009bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // occurred. The empty blocks collapse into the cleared block.
14019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        setMaxMarginBeforeValues(oldTopPosMargin, oldTopNegMargin);
14029bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        marginInfo.setAtBeforeSideOfBlock(false);
14039bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14049bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // In case the child discarded the before margin of the block we need to reset the mustDiscardMarginBefore flag to the initial value.
14059bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        setMustDiscardMarginBefore(style()->marginBeforeCollapse() == MDISCARD);
14069bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
14079bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
1408a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    return yPos + heightIncrease;
14099bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
14109bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14119bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)void RenderBlockFlow::setCollapsedBottomMargin(const MarginInfo& marginInfo)
14129bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
14139bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()) {
14149bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Update the after side margin of the container to discard if the after margin of the last child also discards and we collapse with it.
14159bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Don't update the max margin values because we won't need them anyway.
14169bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (marginInfo.discardMargin()) {
14179bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            setMustDiscardMarginAfter();
14189bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            return;
14199bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
14209bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14219bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Update our max pos/neg bottom margins, since we collapsed our bottom margins
14229bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // with our children.
1423197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        setMaxMarginAfterValues(std::max(maxPositiveMarginAfter(), marginInfo.positiveMargin()), std::max(maxNegativeMarginAfter(), marginInfo.negativeMargin()));
14249bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14259bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (!marginInfo.hasMarginAfterQuirk())
14269bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            setHasMarginAfterQuirk(false);
14279bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14289bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (marginInfo.hasMarginAfterQuirk() && !marginAfter()) {
14299bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // We have no bottom margin and our last child has a quirky margin.
14309bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // We will pick up this quirky margin and pass it through.
14319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // This deals with the <td><div><p> case.
14329bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            setHasMarginAfterQuirk(true);
14339bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
14349bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
14359bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
14369bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14379bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)void RenderBlockFlow::marginBeforeEstimateForChild(RenderBox* child, LayoutUnit& positiveMarginBefore, LayoutUnit& negativeMarginBefore, bool& discardMarginBefore) const
14389bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
14399bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Give up if in quirks mode and we're a body/table cell and the top margin of the child box is quirky.
14409bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Give up if the child specified -webkit-margin-collapse: separate that prevents collapsing.
14419bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // FIXME: Use writing mode independent accessor for marginBeforeCollapse.
144207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    if ((document().inQuirksMode() && hasMarginBeforeQuirk(child) && (isTableCell() || isBody())) || child->style()->marginBeforeCollapse() == MSEPARATE)
14439bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
14449bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14459bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // The margins are discarded by a child that specified -webkit-margin-collapse: discard.
14469bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // FIXME: Use writing mode independent accessor for marginBeforeCollapse.
14479bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (child->style()->marginBeforeCollapse() == MDISCARD) {
14489bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        positiveMarginBefore = 0;
14499bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        negativeMarginBefore = 0;
14509bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        discardMarginBefore = true;
14519bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
14529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
14539bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit beforeChildMargin = marginBeforeForChild(child);
1455197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    positiveMarginBefore = std::max(positiveMarginBefore, beforeChildMargin);
1456197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    negativeMarginBefore = std::max(negativeMarginBefore, -beforeChildMargin);
14579bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14589bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!child->isRenderBlockFlow())
14599bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
14609bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14619bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    RenderBlockFlow* childBlockFlow = toRenderBlockFlow(child);
14629bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (childBlockFlow->childrenInline() || childBlockFlow->isWritingModeRoot())
14639bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
14649bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    MarginInfo childMarginInfo(childBlockFlow, childBlockFlow->borderBefore() + childBlockFlow->paddingBefore(), childBlockFlow->borderAfter() + childBlockFlow->paddingAfter());
14669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!childMarginInfo.canCollapseMarginBeforeWithChildren())
14679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
14689bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14699bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    RenderBox* grandchildBox = childBlockFlow->firstChildBox();
14709bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    for ( ; grandchildBox; grandchildBox = grandchildBox->nextSiblingBox()) {
14719bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (!grandchildBox->isFloatingOrOutOfFlowPositioned())
14729bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            break;
14739bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
14749bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14759bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Give up if there is clearance on the box, since it probably won't collapse into us.
14769bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!grandchildBox || grandchildBox->style()->clear() != CNONE)
14779bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
14789bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14799bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Make sure to update the block margins now for the grandchild box so that we're looking at current values.
14809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (grandchildBox->needsLayout()) {
14819bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        grandchildBox->computeAndSetBlockDirectionMargins(this);
14829bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (grandchildBox->isRenderBlock()) {
14839bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            RenderBlock* grandchildBlock = toRenderBlock(grandchildBox);
14849bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            grandchildBlock->setHasMarginBeforeQuirk(grandchildBox->style()->hasMarginBeforeQuirk());
14859bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            grandchildBlock->setHasMarginAfterQuirk(grandchildBox->style()->hasMarginAfterQuirk());
14869bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
14879bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
14889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14899bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Collapse the margin of the grandchild box with our own to produce an estimate.
14909bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    childBlockFlow->marginBeforeEstimateForChild(grandchildBox, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
14919bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
14929bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
14939bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)LayoutUnit RenderBlockFlow::estimateLogicalTopPosition(RenderBox* child, const MarginInfo& marginInfo, LayoutUnit& estimateWithoutPagination)
14949bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
14959bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // FIXME: We need to eliminate the estimation of vertical position, because when it's wrong we sometimes trigger a pathological
14969bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // relayout if there are intruding floats.
14979bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit logicalTopEstimate = logicalHeight();
14989bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!marginInfo.canCollapseWithMarginBefore()) {
14999bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        LayoutUnit positiveMarginBefore = 0;
15009bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        LayoutUnit negativeMarginBefore = 0;
15019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        bool discardMarginBefore = false;
15029bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (child->selfNeedsLayout()) {
15039bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // Try to do a basic estimation of how the collapse is going to go.
15049bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            marginBeforeEstimateForChild(child, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
15059bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        } else {
15069bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // Use the cached collapsed margin values from a previous layout. Most of the time they
15079bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            // will be right.
15089bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            RenderBlockFlow::MarginValues marginValues = marginValuesForChild(child);
1509197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            positiveMarginBefore = std::max(positiveMarginBefore, marginValues.positiveMarginBefore());
1510197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            negativeMarginBefore = std::max(negativeMarginBefore, marginValues.negativeMarginBefore());
15119bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            discardMarginBefore = mustDiscardMarginBeforeForChild(child);
15129bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
15139bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
15149bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // Collapse the result with our current margins.
15159bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (!discardMarginBefore)
1516197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            logicalTopEstimate += std::max(marginInfo.positiveMargin(), positiveMarginBefore) - std::max(marginInfo.negativeMargin(), negativeMarginBefore);
15179bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
15189bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
15199bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Adjust logicalTopEstimate down to the next page if the margins are so large that we don't fit on the current
15209bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // page.
15219bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutState* layoutState = view()->layoutState();
152209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTopEstimate > logicalHeight())
1523197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        logicalTopEstimate = std::min(logicalTopEstimate, nextPageLogicalTop(logicalHeight()));
15249bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
15259bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    logicalTopEstimate += getClearDelta(child, logicalTopEstimate);
15269bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
15279bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    estimateWithoutPagination = logicalTopEstimate;
15289bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
15299bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (layoutState->isPaginated()) {
15309bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // If the object has a page or column break value of "before", then we should shift to the top of the next page.
15319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        logicalTopEstimate = applyBeforeBreak(child, logicalTopEstimate);
15329bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
15339bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
15349bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        logicalTopEstimate = adjustForUnsplittableChild(child, logicalTopEstimate);
15359bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
1536197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if (!child->selfNeedsLayout() && child->isRenderBlockFlow())
1537197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            logicalTopEstimate += toRenderBlockFlow(child)->paginationStrut();
15389bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
15399bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
15409bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return logicalTopEstimate;
15419bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
15429bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
1543a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)LayoutUnit RenderBlockFlow::marginOffsetForSelfCollapsingBlock()
1544a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
1545a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    ASSERT(isSelfCollapsingBlock());
1546a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    RenderBlockFlow* parentBlock = toRenderBlockFlow(parent());
1547a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    if (parentBlock && style()->clear() && parentBlock->getClearDelta(this, logicalHeight()))
1548a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        return marginValuesForChild(this).positiveMarginBefore();
1549a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    return LayoutUnit();
1550a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
1551a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
15529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)void RenderBlockFlow::adjustFloatingBlock(const MarginInfo& marginInfo)
15539bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
15549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // The float should be positioned taking into account the bottom margin
15559bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // of the previous flow. We add that margin into the height, get the
15569bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // float positioned properly, and then subtract the margin out of the
15579bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // height again. In the case of self-collapsing blocks, we always just
15589bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // use the top margins, since the self-collapsing block collapsed its
15599bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // own bottom margin into its top margin.
15609bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    //
15619bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Note also that the previous flow may collapse its margin into the top of
15629bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // our block. If this is the case, then we do not add the margin in to our
15639bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // height when computing the position of the float. This condition can be tested
15649bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // for by simply calling canCollapseWithMarginBefore. See
15659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // http://www.hixie.ch/tests/adhoc/css/box/block/margin-collapse/046.html for
15669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // an example of this scenario.
15679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
15689bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    setLogicalHeight(logicalHeight() + marginOffset);
15699bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    positionNewFloats();
15709bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    setLogicalHeight(logicalHeight() - marginOffset);
15719bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
15729bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
157309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void RenderBlockFlow::handleAfterSideOfBlock(RenderBox* lastChild, LayoutUnit beforeSide, LayoutUnit afterSide, MarginInfo& marginInfo)
15749bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
15759bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    marginInfo.setAtAfterSideOfBlock(true);
15769bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
1577a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // If our last child was a self-collapsing block with clearance then our logical height is flush with the
1578a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // bottom edge of the float that the child clears. The correct vertical position for the margin-collapsing we want
1579a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // to perform now is at the child's margin-top - so adjust our height to that position.
158009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (lastChild && lastChild->isRenderBlockFlow() && lastChild->isSelfCollapsingBlock())
158109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        setLogicalHeight(logicalHeight() - toRenderBlockFlow(lastChild)->marginOffsetForSelfCollapsingBlock());
158209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
158309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (marginInfo.canCollapseMarginAfterWithChildren() && !marginInfo.canCollapseMarginAfterWithLastChild())
158409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        marginInfo.setCanCollapseMarginAfterWithChildren(false);
1585a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
15869bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // If we can't collapse with children then go ahead and add in the bottom margin.
15879bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!marginInfo.discardMargin() && (!marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()
15889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        && (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginAfterQuirk())))
15899bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        setLogicalHeight(logicalHeight() + marginInfo.margin());
15909bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
15919bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Now add in our bottom border/padding.
15929bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    setLogicalHeight(logicalHeight() + afterSide);
15939bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
15949bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Negative margins can cause our height to shrink below our minimal height (border/padding).
15959bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // If this happens, ensure that the computed height is increased to the minimal height.
1596197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    setLogicalHeight(std::max(logicalHeight(), beforeSide + afterSide));
15979bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
15989bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Update our bottom collapsed margin info.
15999bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    setCollapsedBottomMargin(marginInfo);
16009bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
16019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16029bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)void RenderBlockFlow::setMustDiscardMarginBefore(bool value)
16039bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
16049bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (style()->marginBeforeCollapse() == MDISCARD) {
16059bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        ASSERT(value);
16069bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
16079bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
16089bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16099bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!m_rareData && !value)
16109bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
16119bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16129bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!m_rareData)
1613e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        m_rareData = adoptPtrWillBeNoop(new RenderBlockFlowRareData(this));
16149bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16159bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_rareData->m_discardMarginBefore = value;
16169bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
16179bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16189bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)void RenderBlockFlow::setMustDiscardMarginAfter(bool value)
16199bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
16209bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (style()->marginAfterCollapse() == MDISCARD) {
16219bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        ASSERT(value);
16229bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
16239bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
16249bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16259bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!m_rareData && !value)
16269bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
16279bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16289bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!m_rareData)
1629e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        m_rareData = adoptPtrWillBeNoop(new RenderBlockFlowRareData(this));
16309bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16319bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_rareData->m_discardMarginAfter = value;
16329bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
16339bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16349bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)bool RenderBlockFlow::mustDiscardMarginBefore() const
16359bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
16369bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return style()->marginBeforeCollapse() == MDISCARD || (m_rareData && m_rareData->m_discardMarginBefore);
16379bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
16389bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16399bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)bool RenderBlockFlow::mustDiscardMarginAfter() const
16409bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
16419bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return style()->marginAfterCollapse() == MDISCARD || (m_rareData && m_rareData->m_discardMarginAfter);
16429bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
16439bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16449bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)bool RenderBlockFlow::mustDiscardMarginBeforeForChild(const RenderBox* child) const
16459bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
16469bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    ASSERT(!child->selfNeedsLayout());
16479bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!child->isWritingModeRoot())
16489bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return child->isRenderBlockFlow() ? toRenderBlockFlow(child)->mustDiscardMarginBefore() : (child->style()->marginBeforeCollapse() == MDISCARD);
16499bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (child->isHorizontalWritingMode() == isHorizontalWritingMode())
16509bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return child->isRenderBlockFlow() ? toRenderBlockFlow(child)->mustDiscardMarginAfter() : (child->style()->marginAfterCollapse() == MDISCARD);
16519bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // FIXME: We return false here because the implementation is not geometrically complete. We have values only for before/after, not start/end.
16539bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // In case the boxes are perpendicular we assume the property is not specified.
16549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return false;
16559bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
16569bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16579bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)bool RenderBlockFlow::mustDiscardMarginAfterForChild(const RenderBox* child) const
16589bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
16599bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    ASSERT(!child->selfNeedsLayout());
16609bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!child->isWritingModeRoot())
16619bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return child->isRenderBlockFlow() ? toRenderBlockFlow(child)->mustDiscardMarginAfter() : (child->style()->marginAfterCollapse() == MDISCARD);
16629bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (child->isHorizontalWritingMode() == isHorizontalWritingMode())
16639bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return child->isRenderBlockFlow() ? toRenderBlockFlow(child)->mustDiscardMarginBefore() : (child->style()->marginBeforeCollapse() == MDISCARD);
16649bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // FIXME: See |mustDiscardMarginBeforeForChild| above.
16669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return false;
16679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
16689bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16699bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)void RenderBlockFlow::setMaxMarginBeforeValues(LayoutUnit pos, LayoutUnit neg)
16709bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
16719bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!m_rareData) {
16729bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (pos == RenderBlockFlowRareData::positiveMarginBeforeDefault(this) && neg == RenderBlockFlowRareData::negativeMarginBeforeDefault(this))
16739bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            return;
1674e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        m_rareData = adoptPtrWillBeNoop(new RenderBlockFlowRareData(this));
16759bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
16769bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_rareData->m_margins.setPositiveMarginBefore(pos);
16779bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_rareData->m_margins.setNegativeMarginBefore(neg);
16789bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
16799bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)void RenderBlockFlow::setMaxMarginAfterValues(LayoutUnit pos, LayoutUnit neg)
16819bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
16829bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!m_rareData) {
16839bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        if (pos == RenderBlockFlowRareData::positiveMarginAfterDefault(this) && neg == RenderBlockFlowRareData::negativeMarginAfterDefault(this))
16849bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)            return;
1685e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        m_rareData = adoptPtrWillBeNoop(new RenderBlockFlowRareData(this));
16869bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
16879bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_rareData->m_margins.setPositiveMarginAfter(pos);
16889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_rareData->m_margins.setNegativeMarginAfter(neg);
16899bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
16909bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
16919bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)bool RenderBlockFlow::mustSeparateMarginBeforeForChild(const RenderBox* child) const
16929bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
16939bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    ASSERT(!child->selfNeedsLayout());
16949bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    const RenderStyle* childStyle = child->style();
16959bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!child->isWritingModeRoot())
16969bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return childStyle->marginBeforeCollapse() == MSEPARATE;
16979bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (child->isHorizontalWritingMode() == isHorizontalWritingMode())
16989bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return childStyle->marginAfterCollapse() == MSEPARATE;
16999bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
17009bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // FIXME: See |mustDiscardMarginBeforeForChild| above.
17019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return false;
17029bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
17039bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
17049bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)bool RenderBlockFlow::mustSeparateMarginAfterForChild(const RenderBox* child) const
17059bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
17069bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    ASSERT(!child->selfNeedsLayout());
17079bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    const RenderStyle* childStyle = child->style();
17089bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!child->isWritingModeRoot())
17099bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return childStyle->marginAfterCollapse() == MSEPARATE;
17109bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (child->isHorizontalWritingMode() == isHorizontalWritingMode())
17119bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return childStyle->marginBeforeCollapse() == MSEPARATE;
17129bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
17139bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // FIXME: See |mustDiscardMarginBeforeForChild| above.
17149bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return false;
17159bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
17169bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
17179bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)LayoutUnit RenderBlockFlow::applyBeforeBreak(RenderBox* child, LayoutUnit logicalOffset)
17189bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
17199bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // FIXME: Add page break checking here when we support printing.
17209bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    RenderFlowThread* flowThread = flowThreadContainingBlock();
172109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool isInsideMulticolFlowThread = flowThread;
172209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool checkColumnBreaks = isInsideMulticolFlowThread || view()->layoutState()->isPaginatingColumns();
172307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    bool checkPageBreaks = !checkColumnBreaks && view()->layoutState()->pageLogicalHeight(); // FIXME: Once columns can print we have to check this.
172409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool checkBeforeAlways = (checkColumnBreaks && child->style()->columnBreakBefore() == PBALWAYS)
172509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        || (checkPageBreaks && child->style()->pageBreakBefore() == PBALWAYS);
172609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (checkBeforeAlways && inNormalFlow(child)) {
172709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        if (checkColumnBreaks) {
172809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            if (isInsideMulticolFlowThread) {
172909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                LayoutUnit offsetBreakAdjustment = 0;
173009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                if (flowThread->addForcedRegionBreak(offsetFromLogicalTopOfFirstPage() + logicalOffset, child, true, &offsetBreakAdjustment))
173109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                    return logicalOffset + offsetBreakAdjustment;
173209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            } else {
1733d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)                view()->layoutState()->addForcedColumnBreak(*child, logicalOffset);
173409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            }
17359bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
17369bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
17379bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
17389bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return logicalOffset;
17399bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
17409bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
17419bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)LayoutUnit RenderBlockFlow::applyAfterBreak(RenderBox* child, LayoutUnit logicalOffset, MarginInfo& marginInfo)
17429bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
17439bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // FIXME: Add page break checking here when we support printing.
17449bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    RenderFlowThread* flowThread = flowThreadContainingBlock();
174509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool isInsideMulticolFlowThread = flowThread;
174609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool checkColumnBreaks = isInsideMulticolFlowThread || view()->layoutState()->isPaginatingColumns();
174707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    bool checkPageBreaks = !checkColumnBreaks && view()->layoutState()->pageLogicalHeight(); // FIXME: Once columns can print we have to check this.
174809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool checkAfterAlways = (checkColumnBreaks && child->style()->columnBreakAfter() == PBALWAYS)
174909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        || (checkPageBreaks && child->style()->pageBreakAfter() == PBALWAYS);
175009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (checkAfterAlways && inNormalFlow(child)) {
17519bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
17529bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
17539bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        // So our margin doesn't participate in the next collapsing steps.
17549bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        marginInfo.clearMargin();
17559bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
175609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        if (checkColumnBreaks) {
175709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            if (isInsideMulticolFlowThread) {
175809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                LayoutUnit offsetBreakAdjustment = 0;
175909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                if (flowThread->addForcedRegionBreak(offsetFromLogicalTopOfFirstPage() + logicalOffset + marginOffset, child, false, &offsetBreakAdjustment))
176009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                    return logicalOffset + marginOffset + offsetBreakAdjustment;
176109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            } else {
1762d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)                view()->layoutState()->addForcedColumnBreak(*child, logicalOffset);
176309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            }
17649bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        }
17659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
17669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
17679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    return logicalOffset;
17689bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
17699bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
1770f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::addOverflowFromFloats()
1771f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
1772f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!m_floatingObjects)
1773f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return;
1774f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1775f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1776f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObjectSetIterator end = floatingObjectSet.end();
1777f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
1778d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        FloatingObject* floatingObject = it->get();
1779f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (floatingObject->isDescendant())
1780f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            addOverflowFromChild(floatingObject->renderer(), IntSize(xPositionForFloatIncludingMargin(floatingObject), yPositionForFloatIncludingMargin(floatingObject)));
1781f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
1782f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
1783f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1784f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::computeOverflow(LayoutUnit oldClientAfterEdge, bool recomputeFloats)
1785f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
1786f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    RenderBlock::computeOverflow(oldClientAfterEdge, recomputeFloats);
178709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (!hasColumns() && (recomputeFloats || createsBlockFormattingContext() || hasSelfPaintingLayer()))
1788f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        addOverflowFromFloats();
1789f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
1790f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
179109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)RootInlineBox* RenderBlockFlow::createAndAppendRootInlineBox()
179209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
179309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    RootInlineBox* rootBox = createRootInlineBox();
179409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_lineBoxes.appendLineBox(rootBox);
179509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
179609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return rootBox;
179709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
179809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
1799f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::deleteLineBoxTree()
1800f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
1801f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (containsFloats())
1802f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        m_floatingObjects->clearLineBoxTreePointers();
1803c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
1804c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    m_lineBoxes.deleteLineBoxTree();
1805f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
1806f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1807f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::markAllDescendantsWithFloatsForLayout(RenderBox* floatToRemove, bool inLayout)
1808f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
1809f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!everHadLayout() && !containsFloats())
1810f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return;
1811f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1812197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (m_descendantsWithFloatsMarkedForLayout && !floatToRemove)
1813197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return;
1814197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    m_descendantsWithFloatsMarkedForLayout |= !floatToRemove;
1815197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
1816f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    MarkingBehavior markParents = inLayout ? MarkOnlyThis : MarkContainingBlockChain;
1817f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    setChildNeedsLayout(markParents);
1818f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1819f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (floatToRemove)
1820f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        removeFloatingObject(floatToRemove);
1821f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1822f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Iterate over our children and mark them as needed.
1823f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!childrenInline()) {
1824f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        for (RenderObject* child = firstChild(); child; child = child->nextSibling()) {
1825f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if ((!floatToRemove && child->isFloatingOrOutOfFlowPositioned()) || !child->isRenderBlock())
1826f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                continue;
1827f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (!child->isRenderBlockFlow()) {
1828f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                RenderBlock* childBlock = toRenderBlock(child);
1829f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                if (childBlock->shrinkToAvoidFloats() && childBlock->everHadLayout())
1830f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    childBlock->setChildNeedsLayout(markParents);
1831f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                continue;
1832f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
1833f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            RenderBlockFlow* childBlockFlow = toRenderBlockFlow(child);
1834f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if ((floatToRemove ? childBlockFlow->containsFloat(floatToRemove) : childBlockFlow->containsFloats()) || childBlockFlow->shrinkToAvoidFloats())
1835f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                childBlockFlow->markAllDescendantsWithFloatsForLayout(floatToRemove, inLayout);
1836f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
1837f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
1838f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
1839f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1840f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::markSiblingsWithFloatsForLayout(RenderBox* floatToRemove)
1841f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
1842f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!m_floatingObjects)
1843f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return;
1844f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1845f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1846f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObjectSetIterator end = floatingObjectSet.end();
1847f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1848f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    for (RenderObject* next = nextSibling(); next; next = next->nextSibling()) {
1849c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        if (!next->isRenderBlockFlow() || next->isFloatingOrOutOfFlowPositioned() || toRenderBlockFlow(next)->avoidsFloats())
1850f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            continue;
1851f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1852f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        RenderBlockFlow* nextBlock = toRenderBlockFlow(next);
1853f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
1854f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            RenderBox* floatingBox = (*it)->renderer();
1855f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (floatToRemove && floatingBox != floatToRemove)
1856f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                continue;
1857f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (nextBlock->containsFloat(floatingBox))
1858f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                nextBlock->markAllDescendantsWithFloatsForLayout(floatingBox);
1859f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
1860f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
1861f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
1862f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1863f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)LayoutUnit RenderBlockFlow::getClearDelta(RenderBox* child, LayoutUnit logicalTop)
1864f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
1865f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // There is no need to compute clearance if we have no floats.
1866f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!containsFloats())
1867f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return 0;
1868f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1869f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // At least one float is present. We need to perform the clearance computation.
1870f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    bool clearSet = child->style()->clear() != CNONE;
1871f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    LayoutUnit logicalBottom = 0;
1872f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    switch (child->style()->clear()) {
1873f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    case CNONE:
1874f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        break;
1875f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    case CLEFT:
1876f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
1877f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        break;
1878f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    case CRIGHT:
1879f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatRight);
1880f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        break;
1881f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    case CBOTH:
1882f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        logicalBottom = lowestFloatLogicalBottom();
1883f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        break;
1884f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
1885f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1886f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // We also clear floats if we are too big to sit on the same line as a float (and wish to avoid floats by default).
1887197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    LayoutUnit result = clearSet ? std::max<LayoutUnit>(0, logicalBottom - logicalTop) : LayoutUnit();
1888f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!result && child->avoidsFloats()) {
1889f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        LayoutUnit newLogicalTop = logicalTop;
1890f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        while (true) {
1891f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit availableLogicalWidthAtNewLogicalTopOffset = availableLogicalWidthForLine(newLogicalTop, false, logicalHeightForChild(child));
189209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            if (availableLogicalWidthAtNewLogicalTopOffset == availableLogicalWidthForContent())
1893f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                return newLogicalTop - logicalTop;
1894f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
189509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            LayoutRect borderBox = child->borderBoxRect();
1896f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit childLogicalWidthAtOldLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
1897f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1898f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // FIXME: None of this is right for perpendicular writing-mode children.
1899f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit childOldLogicalWidth = child->logicalWidth();
1900f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit childOldMarginLeft = child->marginLeft();
1901f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit childOldMarginRight = child->marginRight();
1902f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit childOldLogicalTop = child->logicalTop();
1903f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1904f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            child->setLogicalTop(newLogicalTop);
1905f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            child->updateLogicalWidth();
190609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)            borderBox = child->borderBoxRect();
1907f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit childLogicalWidthAtNewLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
1908f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1909f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            child->setLogicalTop(childOldLogicalTop);
1910f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            child->setLogicalWidth(childOldLogicalWidth);
1911f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            child->setMarginLeft(childOldMarginLeft);
1912f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            child->setMarginRight(childOldMarginRight);
1913f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1914f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (childLogicalWidthAtNewLogicalTopOffset <= availableLogicalWidthAtNewLogicalTopOffset) {
1915f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // Even though we may not be moving, if the logical width did shrink because of the presence of new floats, then
1916f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // we need to force a relayout as though we shifted. This happens because of the dynamic addition of overhanging floats
1917f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // from previous siblings when negative margins exist on a child (see the addOverhangingFloats call at the end of collapseMargins).
1918f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                if (childLogicalWidthAtOldLogicalTopOffset != childLogicalWidthAtNewLogicalTopOffset)
1919f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    child->setChildNeedsLayout(MarkOnlyThis);
1920f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                return newLogicalTop - logicalTop;
1921f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
1922f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1923f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            newLogicalTop = nextFloatLogicalBottomBelow(newLogicalTop);
1924f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            ASSERT(newLogicalTop >= logicalTop);
1925f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (newLogicalTop < logicalTop)
1926f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                break;
1927f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
1928f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        ASSERT_NOT_REACHED();
1929f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
1930f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return result;
1931f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
1932f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1933f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::createFloatingObjects()
1934f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
1935f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    m_floatingObjects = adoptPtr(new FloatingObjects(this, isHorizontalWritingMode()));
1936f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
1937f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1938aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdochvoid RenderBlockFlow::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
1939f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
1940f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    RenderStyle* oldStyle = style();
1941197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    s_canPropagateFloatIntoSibling = oldStyle ? !isFloatingOrOutOfFlowPositioned() && !avoidsFloats() : false;
194210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    if (oldStyle && parent() && diff.needsFullLayout() && oldStyle->position() != newStyle.position()
1943aafa69cb17c9d6606c07663ade5f81388a2c5986Ben Murdoch        && containsFloats() && !isFloating() && !isOutOfFlowPositioned() && newStyle.hasOutOfFlowPosition())
1944f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            markAllDescendantsWithFloatsForLayout();
1945f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1946f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    RenderBlock::styleWillChange(diff, newStyle);
1947f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
1948f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1949f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
1950f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
1951f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    RenderBlock::styleDidChange(diff, oldStyle);
1952f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1953f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // After our style changed, if we lose our ability to propagate floats into next sibling
1954f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // blocks, then we need to find the top most parent containing that overhanging float and
1955f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // then mark its descendants with floats for layout and clear all floats from its next
1956f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // sibling blocks that exist in our floating objects list. See bug 56299 and 62875.
1957197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    bool canPropagateFloatIntoSibling = !isFloatingOrOutOfFlowPositioned() && !avoidsFloats();
195810f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    if (diff.needsFullLayout() && s_canPropagateFloatIntoSibling && !canPropagateFloatIntoSibling && hasOverhangingFloats()) {
1959f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        RenderBlockFlow* parentBlockFlow = this;
1960f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1961f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        FloatingObjectSetIterator end = floatingObjectSet.end();
1962f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1963f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        for (RenderObject* curr = parent(); curr && !curr->isRenderView(); curr = curr->parent()) {
1964f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (curr->isRenderBlockFlow()) {
1965f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                RenderBlockFlow* currBlock = toRenderBlockFlow(curr);
1966f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1967f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                if (currBlock->hasOverhangingFloats()) {
1968f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
1969f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                        RenderBox* renderer = (*it)->renderer();
1970f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                        if (currBlock->hasOverhangingFloat(renderer)) {
1971f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                            parentBlockFlow = currBlock;
1972f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                            break;
1973f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                        }
1974f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    }
1975f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                }
1976f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
1977f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
1978f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
1979f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        parentBlockFlow->markAllDescendantsWithFloatsForLayout();
1980f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        parentBlockFlow->markSiblingsWithFloatsForLayout();
1981f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
1982d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
198310f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    if (diff.needsFullLayout() || !oldStyle)
1984197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        createOrDestroyMultiColumnFlowThreadIfNeeded(oldStyle);
198551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
198651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
198751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)void RenderBlockFlow::updateStaticInlinePositionForChild(RenderBox* child, LayoutUnit logicalTop)
198851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
198951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (child->style()->isOriginalDisplayInlineType())
1990197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        setStaticInlinePositionForChild(child, startAlignedOffsetForLine(logicalTop, false));
199151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    else
1992197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        setStaticInlinePositionForChild(child, startOffsetForContent());
199351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
199451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
1995197021e6b966cfb06891637935ef33fff06433d1Ben Murdochvoid RenderBlockFlow::setStaticInlinePositionForChild(RenderBox* child, LayoutUnit inlinePosition)
199651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
199751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    child->layer()->setStaticInlinePosition(inlinePosition);
1998f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
1999f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2000d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)void RenderBlockFlow::addChild(RenderObject* newChild, RenderObject* beforeChild)
2001d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
2002d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    if (RenderMultiColumnFlowThread* flowThread = multiColumnFlowThread()) {
2003d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        flowThread->addChild(newChild, beforeChild);
2004d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        return;
2005d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    }
2006d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    RenderBlock::addChild(newChild, beforeChild);
2007d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
2008d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
2009f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::moveAllChildrenIncludingFloatsTo(RenderBlock* toBlock, bool fullRemoveInsert)
2010f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2011f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    RenderBlockFlow* toBlockFlow = toRenderBlockFlow(toBlock);
2012f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    moveAllChildrenTo(toBlockFlow, fullRemoveInsert);
2013f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2014f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // When a portion of the render tree is being detached, anonymous blocks
2015f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // will be combined as their children are deleted. In this process, the
2016f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // anonymous block later in the tree is merged into the one preceeding it.
2017f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // It can happen that the later block (this) contains floats that the
2018f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // previous block (toBlockFlow) did not contain, and thus are not in the
2019f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // floating objects list for toBlockFlow. This can result in toBlockFlow containing
2020f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // floats that are not in it's floating objects list, but are in the
2021f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // floating objects lists of siblings and parents. This can cause problems
2022f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // when the float itself is deleted, since the deletion code assumes that
2023f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // if a float is not in it's containing block's floating objects list, it
2024f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // isn't in any floating objects list. In order to preserve this condition
2025f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // (removing it has serious performance implications), we need to copy the
2026f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // floating objects from the old block (this) to the new block (toBlockFlow).
2027f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // The float's metrics will likely all be wrong, but since toBlockFlow is
2028f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // already marked for layout, this will get fixed before anything gets
2029f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // displayed.
2030f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // See bug https://code.google.com/p/chromium/issues/detail?id=230907
2031f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (m_floatingObjects) {
2032f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (!toBlockFlow->m_floatingObjects)
2033f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            toBlockFlow->createFloatingObjects();
2034f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2035f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        const FloatingObjectSet& fromFloatingObjectSet = m_floatingObjects->set();
2036f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        FloatingObjectSetIterator end = fromFloatingObjectSet.end();
2037f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2038f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        for (FloatingObjectSetIterator it = fromFloatingObjectSet.begin(); it != end; ++it) {
2039d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            FloatingObject* floatingObject = it->get();
2040f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2041f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // Don't insert the object again if it's already in the list
2042f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (toBlockFlow->containsFloat(floatingObject->renderer()))
2043f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                continue;
2044f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2045f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            toBlockFlow->m_floatingObjects->add(floatingObject->unsafeClone());
2046f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2047f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2048f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2049f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2050f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2051197021e6b966cfb06891637935ef33fff06433d1Ben Murdochvoid RenderBlockFlow::invalidatePaintForOverhangingFloats(bool paintAllDescendants)
2052f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
20539e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    // Invalidate paint of any overhanging floats (if we know we're the one to paint them).
2054f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Otherwise, bail out.
2055f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!hasOverhangingFloats())
2056f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return;
2057f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2058f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2059f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObjectSetIterator end = floatingObjectSet.end();
2060f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
2061d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        FloatingObject* floatingObject = it->get();
20629e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        // Only issue paint invaldiations for the object if it is overhanging, is not in its own layer, and
2063f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        // is our responsibility to paint (m_shouldPaint is set). When paintAllDescendants is true, the latter
2064f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        // condition is replaced with being a descendant of us.
2065f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (logicalBottomForFloat(floatingObject) > logicalHeight()
2066f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            && !floatingObject->renderer()->hasSelfPaintingLayer()
2067f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            && (floatingObject->shouldPaint() || (paintAllDescendants && floatingObject->renderer()->isDescendantOf(this)))) {
2068a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
2069a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            RenderBox* floatingRenderer = floatingObject->renderer();
2070197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            floatingRenderer->setShouldDoFullPaintInvalidation(true);
2071197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            floatingRenderer->invalidatePaintForOverhangingFloats(false);
2072f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2073f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2074f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2075f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
20765d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)void RenderBlockFlow::invalidatePaintForOverflow()
2077a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
2078a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // FIXME: We could tighten up the left and right invalidation points if we let layoutInlineChildren fill them in based off the particular lines
2079a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // it had to lay out. We wouldn't need the hasOverflowClip() hack in that case either.
20809e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutUnit paintInvalidationLogicalLeft = logicalLeftVisualOverflow();
20819e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutUnit paintInvalidationLogicalRight = logicalRightVisualOverflow();
2082a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    if (hasOverflowClip()) {
2083a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // If we have clipped overflow, we should use layout overflow as well, since visual overflow from lines didn't propagate to our block's overflow.
2084a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // Note the old code did this as well but even for overflow:visible. The addition of hasOverflowClip() at least tightens up the hack a bit.
20859e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        // layoutInlineChildren should be patched to compute the entire paint invalidation rect.
20869e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        paintInvalidationLogicalLeft = std::min(paintInvalidationLogicalLeft, logicalLeftLayoutOverflow());
20879e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        paintInvalidationLogicalRight = std::max(paintInvalidationLogicalRight, logicalRightLayoutOverflow());
2088a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    }
2089a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
20909e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutRect paintInvalidationRect;
2091a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    if (isHorizontalWritingMode())
20929e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        paintInvalidationRect = LayoutRect(paintInvalidationLogicalLeft, m_paintInvalidationLogicalTop, paintInvalidationLogicalRight - paintInvalidationLogicalLeft, m_paintInvalidationLogicalBottom - m_paintInvalidationLogicalTop);
2093a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    else
20949e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        paintInvalidationRect = LayoutRect(m_paintInvalidationLogicalTop, paintInvalidationLogicalLeft, m_paintInvalidationLogicalBottom - m_paintInvalidationLogicalTop, paintInvalidationLogicalRight - paintInvalidationLogicalLeft);
2095a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
20969e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    // The paint invalidation rect may be split across columns, in which case adjustRectForColumns() will return the union.
20979e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    adjustRectForColumns(paintInvalidationRect);
2098a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
2099a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    if (hasOverflowClip()) {
21009e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        // Adjust the paint invalidation rect for scroll offset
21019e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        paintInvalidationRect.move(-scrolledContentOffset());
2102a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
2103a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // Don't allow this rect to spill out of our overflow box.
21049e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        paintInvalidationRect.intersect(LayoutRect(LayoutPoint(), size()));
2105a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    }
2106a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
2107a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    // Make sure the rect is still non-empty after intersecting for overflow above
21089e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    if (!paintInvalidationRect.isEmpty()) {
2109d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        // Hits in media/event-attributes.html
2110d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        DisableCompositingQueryAsserts disabler;
2111d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
21129e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        invalidatePaintRectangle(paintInvalidationRect); // We need to do a partial paint invalidation of our content.
2113a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        if (hasReflection())
21149e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)            invalidatePaintRectangle(reflectedRect(paintInvalidationRect));
2115a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    }
2116a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
21179e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    m_paintInvalidationLogicalTop = 0;
21189e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    m_paintInvalidationLogicalBottom = 0;
2119a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
2120a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
2121f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::paintFloats(PaintInfo& paintInfo, const LayoutPoint& paintOffset, bool preservePhase)
2122f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
21237242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    BlockFlowPainter(*this).paintFloats(paintInfo, paintOffset, preservePhase);
2124f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2125f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
21267242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccivoid RenderBlockFlow::clipOutFloatingObjects(const RenderBlock* rootBlock, const PaintInfo* paintInfo, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock) const
2127f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2128f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (m_floatingObjects) {
2129f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2130f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        FloatingObjectSetIterator end = floatingObjectSet.end();
2131f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
2132d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            FloatingObject* floatingObject = it->get();
2133f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutRect floatBox(offsetFromRootBlock.width() + xPositionForFloatIncludingMargin(floatingObject),
2134f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                offsetFromRootBlock.height() + yPositionForFloatIncludingMargin(floatingObject),
2135f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                floatingObject->renderer()->width(), floatingObject->renderer()->height());
2136f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            rootBlock->flipForWritingMode(floatBox);
2137f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            floatBox.move(rootBlockPhysicalPosition.x(), rootBlockPhysicalPosition.y());
2138f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            paintInfo->context->clipOut(pixelSnappedIntRect(floatBox));
2139f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2140f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2141f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2142f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2143f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::clearFloats(EClear clear)
2144f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2145f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    positionNewFloats();
2146f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // set y position
2147f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    LayoutUnit newY = 0;
2148f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    switch (clear) {
2149f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    case CLEFT:
2150f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        newY = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
2151f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        break;
2152f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    case CRIGHT:
2153f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        newY = lowestFloatLogicalBottom(FloatingObject::FloatRight);
2154f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        break;
2155f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    case CBOTH:
2156f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        newY = lowestFloatLogicalBottom();
2157f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    default:
2158f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        break;
2159f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2160f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (height() < newY)
2161f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        setLogicalHeight(newY);
2162f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2163f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2164f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)bool RenderBlockFlow::containsFloat(RenderBox* renderer) const
2165f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
216651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return m_floatingObjects && m_floatingObjects->set().contains<FloatingObjectHashTranslator>(renderer);
2167f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2168f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2169f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::removeFloatingObjects()
2170f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2171f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!m_floatingObjects)
2172f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return;
2173f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
217407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    markSiblingsWithFloatsForLayout();
217507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
2176f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    m_floatingObjects->clear();
2177f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2178f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2179f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)LayoutPoint RenderBlockFlow::flipFloatForWritingModeForChild(const FloatingObject* child, const LayoutPoint& point) const
2180f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2181f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!style()->isFlippedBlocksWritingMode())
2182f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return point;
2183f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2184f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // This is similar to RenderBox::flipForWritingModeForChild. We have to subtract out our left/top offsets twice, since
2185f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // it's going to get added back in. We hide this complication here so that the calling code looks normal for the unflipped
2186f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // case.
2187f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (isHorizontalWritingMode())
2188f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return LayoutPoint(point.x(), point.y() + height() - child->renderer()->height() - 2 * yPositionForFloatIncludingMargin(child));
2189f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return LayoutPoint(point.x() + width() - child->renderer()->width() - 2 * xPositionForFloatIncludingMargin(child), point.y());
2190f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2191f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
219251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)LayoutUnit RenderBlockFlow::logicalLeftOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
219351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
219451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    LayoutUnit offset = fixedOffset;
219551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
219651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        offset = m_floatingObjects->logicalLeftOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
219751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return adjustLogicalLeftOffsetForLine(offset, applyTextIndent);
219851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
219951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
220051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)LayoutUnit RenderBlockFlow::logicalRightOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
220151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
220251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    LayoutUnit offset = fixedOffset;
220351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (m_floatingObjects && m_floatingObjects->hasRightObjects())
220451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        offset = m_floatingObjects->logicalRightOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
220551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return adjustLogicalRightOffsetForLine(offset, applyTextIndent);
220651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
220751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
220809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)LayoutUnit RenderBlockFlow::adjustLogicalLeftOffsetForLine(LayoutUnit offsetFromFloats, bool applyTextIndent) const
220909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
221009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit left = offsetFromFloats;
221109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
221209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (applyTextIndent && style()->isLeftToRightDirection())
221309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        left += textIndentOffset();
221409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
221509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return left;
221609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
221709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
221809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)LayoutUnit RenderBlockFlow::adjustLogicalRightOffsetForLine(LayoutUnit offsetFromFloats, bool applyTextIndent) const
221909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
222009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit right = offsetFromFloats;
222109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
222209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (applyTextIndent && !style()->isLeftToRightDirection())
222309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        right -= textIndentOffset();
222409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
222509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return right;
222609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
222709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
2228f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)LayoutPoint RenderBlockFlow::computeLogicalLocationForFloat(const FloatingObject* floatingObject, LayoutUnit logicalTopOffset) const
2229f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2230f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    RenderBox* childBox = floatingObject->renderer();
223109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit logicalLeftOffset = logicalLeftOffsetForContent(); // Constant part of left offset.
2232f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    LayoutUnit logicalRightOffset; // Constant part of right offset.
223307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    logicalRightOffset = logicalRightOffsetForContent();
2234f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2235197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    LayoutUnit floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset); // The width we look for.
2236f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2237f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    LayoutUnit floatLogicalLeft;
2238f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2239f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    bool insideFlowThread = flowThreadContainingBlock();
2240f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2241f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (childBox->style()->floating() == LeftFloat) {
2242f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        LayoutUnit heightRemainingLeft = 1;
2243f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        LayoutUnit heightRemainingRight = 1;
224451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
224551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        while (logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight) - floatLogicalLeft < floatLogicalWidth) {
2246197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
224751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
2248f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (insideFlowThread) {
2249f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // Have to re-evaluate all of our offsets, since they may have changed.
225009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                logicalRightOffset = logicalRightOffsetForContent(); // Constant part of right offset.
225109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                logicalLeftOffset = logicalLeftOffsetForContent(); // Constant part of left offset.
2252197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
2253f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
2254f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2255197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        floatLogicalLeft = std::max(logicalLeftOffset - borderAndPaddingLogicalLeft(), floatLogicalLeft);
2256f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    } else {
2257f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        LayoutUnit heightRemainingLeft = 1;
2258f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        LayoutUnit heightRemainingRight = 1;
225951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
226051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        while (floatLogicalLeft - logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft) < floatLogicalWidth) {
2261197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
226251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
2263f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (insideFlowThread) {
2264f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // Have to re-evaluate all of our offsets, since they may have changed.
226509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                logicalRightOffset = logicalRightOffsetForContent(); // Constant part of right offset.
226609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)                logicalLeftOffset = logicalLeftOffsetForContent(); // Constant part of left offset.
2267197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
2268f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
2269f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2270f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        // Use the original width of the float here, since the local variable
2271f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        // |floatLogicalWidth| was capped to the available line width. See
2272f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        // fast/block/float/clamped-right-float.html.
2273f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        floatLogicalLeft -= logicalWidthForFloat(floatingObject);
2274f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2275f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2276f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return LayoutPoint(floatLogicalLeft, logicalTopOffset);
2277f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2278f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2279f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)FloatingObject* RenderBlockFlow::insertFloatingObject(RenderBox* floatBox)
2280f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2281f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    ASSERT(floatBox->isFloating());
2282f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2283f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Create the list of special objects if we don't aleady have one
2284f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!m_floatingObjects) {
2285f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        createFloatingObjects();
2286f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    } else {
2287f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        // Don't insert the object again if it's already in the list
2288f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
228951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        FloatingObjectSetIterator it = floatingObjectSet.find<FloatingObjectHashTranslator>(floatBox);
2290f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (it != floatingObjectSet.end())
2291d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            return it->get();
2292f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2293f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2294f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Create the special object entry & append it to the list
2295f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2296f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    OwnPtr<FloatingObject> newObj = FloatingObject::create(floatBox);
2297f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2298f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Our location is irrelevant if we're unsplittable or no pagination is in effect.
2299f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Just go ahead and lay out the float.
2300f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    bool isChildRenderBlock = floatBox->isRenderBlock();
2301f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (isChildRenderBlock && !floatBox->needsLayout() && view()->layoutState()->pageLogicalHeightChanged())
2302f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        floatBox->setChildNeedsLayout(MarkOnlyThis);
2303f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2304f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    bool needsBlockDirectionLocationSetBeforeLayout = isChildRenderBlock && view()->layoutState()->needsBlockDirectionLocationSetBeforeLayout();
2305f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!needsBlockDirectionLocationSetBeforeLayout || isWritingModeRoot()) { // We are unsplittable if we're a block flow root.
2306f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        floatBox->layoutIfNeeded();
2307f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    } else {
2308f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        floatBox->updateLogicalWidth();
2309f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        floatBox->computeAndSetBlockDirectionMargins(this);
2310f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2311f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2312f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    setLogicalWidthForFloat(newObj.get(), logicalWidthForChild(floatBox) + marginStartForChild(floatBox) + marginEndForChild(floatBox));
2313f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2314f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return m_floatingObjects->add(newObj.release());
2315f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2316f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2317f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::removeFloatingObject(RenderBox* floatBox)
2318f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2319f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (m_floatingObjects) {
2320f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
232151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        FloatingObjectSetIterator it = floatingObjectSet.find<FloatingObjectHashTranslator>(floatBox);
2322f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (it != floatingObjectSet.end()) {
2323d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            FloatingObject* floatingObject = it->get();
2324f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (childrenInline()) {
2325f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
2326f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
2327f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2328f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // Fix for https://bugs.webkit.org/show_bug.cgi?id=54995.
2329f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                if (logicalBottom < 0 || logicalBottom < logicalTop || logicalTop == LayoutUnit::max()) {
2330f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    logicalBottom = LayoutUnit::max();
2331f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                } else {
2332f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    // Special-case zero- and less-than-zero-height floats: those don't touch
2333f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    // the line that they're on, but it still needs to be dirtied. This is
2334f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    // accomplished by pretending they have a height of 1.
2335197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                    logicalBottom = std::max(logicalBottom, logicalTop + 1);
2336f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                }
2337f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                if (floatingObject->originatingLine()) {
2338f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    if (!selfNeedsLayout()) {
2339f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                        ASSERT(floatingObject->originatingLine()->renderer() == this);
2340f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                        floatingObject->originatingLine()->markDirty();
2341f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    }
2342197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#if ENABLE(ASSERT)
2343f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    floatingObject->setOriginatingLine(0);
2344f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)#endif
2345f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                }
2346f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                markLinesDirtyInBlockRange(0, logicalBottom);
2347f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
2348f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            m_floatingObjects->remove(floatingObject);
2349f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2350f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2351f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2352f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2353f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::removeFloatingObjectsBelow(FloatingObject* lastFloat, int logicalOffset)
2354f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2355f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!containsFloats())
2356f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return;
2357f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2358f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2359d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    FloatingObject* curr = floatingObjectSet.last().get();
2360f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    while (curr != lastFloat && (!curr->isPlaced() || logicalTopForFloat(curr) >= logicalOffset)) {
2361f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        m_floatingObjects->remove(curr);
2362f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (floatingObjectSet.isEmpty())
2363f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            break;
2364d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        curr = floatingObjectSet.last().get();
2365f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2366f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2367f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2368f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)bool RenderBlockFlow::positionNewFloats()
2369f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2370f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!m_floatingObjects)
2371f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return false;
2372f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2373f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2374f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (floatingObjectSet.isEmpty())
2375f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return false;
2376f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2377f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // If all floats have already been positioned, then we have no work to do.
2378f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (floatingObjectSet.last()->isPlaced())
2379f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return false;
2380f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2381f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Move backwards through our floating object list until we find a float that has
2382f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // already been positioned. Then we'll be able to move forward, positioning all of
2383f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // the new floats that need it.
2384f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObjectSetIterator it = floatingObjectSet.end();
2385f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    --it; // Go to last item.
2386f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObjectSetIterator begin = floatingObjectSet.begin();
2387f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObject* lastPlacedFloatingObject = 0;
2388f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    while (it != begin) {
2389f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        --it;
2390f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if ((*it)->isPlaced()) {
2391d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            lastPlacedFloatingObject = it->get();
2392f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            ++it;
2393f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            break;
2394f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2395f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2396f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2397f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    LayoutUnit logicalTop = logicalHeight();
2398f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2399f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // The float cannot start above the top position of the last positioned float.
2400f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (lastPlacedFloatingObject)
2401197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        logicalTop = std::max(logicalTopForFloat(lastPlacedFloatingObject), logicalTop);
2402f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2403f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObjectSetIterator end = floatingObjectSet.end();
2404f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Now walk through the set of unpositioned floats and place them.
2405f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    for (; it != end; ++it) {
2406d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        FloatingObject* floatingObject = it->get();
2407f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        // The containing block is responsible for positioning floats, so if we have floats in our
2408f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        // list that come from somewhere else, do not attempt to position them.
2409f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (floatingObject->renderer()->containingBlock() != this)
2410f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            continue;
2411f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2412f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        RenderBox* childBox = floatingObject->renderer();
2413f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2414323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)        // FIXME Investigate if this can be removed. crbug.com/370006
2415f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        childBox->setMayNeedPaintInvalidation(true);
2416323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
241709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        LayoutUnit childLogicalLeftMargin = style()->isLeftToRightDirection() ? marginStartForChild(childBox) : marginEndForChild(childBox);
2418f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (childBox->style()->clear() & CLEFT)
2419197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatLeft), logicalTop);
2420f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (childBox->style()->clear() & CRIGHT)
2421197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatRight), logicalTop);
2422f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2423f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        LayoutPoint floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, logicalTop);
2424f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2425f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
2426f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2427f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
2428f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
2429f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
243010f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        SubtreeLayoutScope layoutScope(*childBox);
2431f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        LayoutState* layoutState = view()->layoutState();
2432f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        bool isPaginated = layoutState->isPaginated();
2433f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (isPaginated && !childBox->needsLayout())
2434f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            childBox->markForPaginationRelayoutIfNeeded(layoutScope);
2435f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2436f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        childBox->layoutIfNeeded();
2437f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2438f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (isPaginated) {
2439f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // If we are unsplittable and don't fit, then we need to move down.
2440f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // We include our margins as part of the unsplittable area.
2441f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit newLogicalTop = adjustForUnsplittableChild(childBox, floatLogicalLocation.y(), true);
2442f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2443f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // See if we have a pagination strut that is making us move down further.
2444f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // Note that an unsplittable child can't also have a pagination strut, so this is
2445f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // exclusive with the case above.
2446197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            RenderBlockFlow* childBlockFlow = childBox->isRenderBlockFlow() ? toRenderBlockFlow(childBox) : 0;
2447197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            if (childBlockFlow && childBlockFlow->paginationStrut()) {
2448197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                newLogicalTop += childBlockFlow->paginationStrut();
2449197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                childBlockFlow->setPaginationStrut(0);
2450f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
2451f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2452f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (newLogicalTop != floatLogicalLocation.y()) {
2453f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                floatingObject->setPaginationStrut(newLogicalTop - floatLogicalLocation.y());
2454f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2455f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, newLogicalTop);
2456f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
2457f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2458f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
2459f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
2460f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2461197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                if (childBox->isRenderBlock())
2462197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                    childBox->setChildNeedsLayout(MarkOnlyThis);
2463f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                childBox->layoutIfNeeded();
2464f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
2465f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2466f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2467f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        setLogicalTopForFloat(floatingObject, floatLogicalLocation.y());
2468f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2469f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        setLogicalHeightForFloat(floatingObject, logicalHeightForChild(childBox) + marginBeforeForChild(childBox) + marginAfterForChild(childBox));
2470f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2471f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        m_floatingObjects->addPlacedObject(floatingObject);
2472f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
247351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        if (ShapeOutsideInfo* shapeOutside = childBox->shapeOutsideInfo())
2474d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            shapeOutside->setReferenceBoxLogicalSize(logicalSizeForChild(childBox));
2475f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2476f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return true;
2477f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2478f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2479f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)bool RenderBlockFlow::hasOverhangingFloat(RenderBox* renderer)
2480f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2481f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!m_floatingObjects || hasColumns() || !parent())
2482f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return false;
2483f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2484f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
248551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    FloatingObjectSetIterator it = floatingObjectSet.find<FloatingObjectHashTranslator>(renderer);
2486f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (it == floatingObjectSet.end())
2487f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return false;
2488f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2489d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    return logicalBottomForFloat(it->get()) > logicalHeight();
2490f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2491f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2492f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::addIntrudingFloats(RenderBlockFlow* prev, LayoutUnit logicalLeftOffset, LayoutUnit logicalTopOffset)
2493f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2494f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    ASSERT(!avoidsFloats());
2495f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2496f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    // If we create our own block formatting context then our contents don't interact with floats outside it, even those from our parent.
2497f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    if (createsBlockFormattingContext())
2498f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu        return;
2499f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu
2500f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // If the parent or previous sibling doesn't have any floats to add, don't bother.
2501f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!prev->m_floatingObjects)
2502f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return;
2503f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2504f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    logicalLeftOffset += marginLogicalLeft();
2505f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2506f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    const FloatingObjectSet& prevSet = prev->m_floatingObjects->set();
2507f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObjectSetIterator prevEnd = prevSet.end();
2508f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    for (FloatingObjectSetIterator prevIt = prevSet.begin(); prevIt != prevEnd; ++prevIt) {
2509d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        FloatingObject* floatingObject = prevIt->get();
2510f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (logicalBottomForFloat(floatingObject) > logicalTopOffset) {
2511f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (!m_floatingObjects || !m_floatingObjects->set().contains(floatingObject)) {
2512f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // We create the floating object list lazily.
2513f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                if (!m_floatingObjects)
2514f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    createFloatingObjects();
2515f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2516f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // Applying the child's margin makes no sense in the case where the child was passed in.
2517f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // since this margin was added already through the modification of the |logicalLeftOffset| variable
2518f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // above. |logicalLeftOffset| will equal the margin in this case, so it's already been taken
2519f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // into account. Only apply this code if prev is the parent, since otherwise the left margin
2520f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // will get applied twice.
2521f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                LayoutSize offset = isHorizontalWritingMode()
2522f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    ? LayoutSize(logicalLeftOffset - (prev != parent() ? prev->marginLeft() : LayoutUnit()), logicalTopOffset)
2523f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    : LayoutSize(logicalTopOffset, logicalLeftOffset - (prev != parent() ? prev->marginTop() : LayoutUnit()));
2524f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2525f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                m_floatingObjects->add(floatingObject->copyToNewContainer(offset));
2526f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
2527f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2528f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2529f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2530f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
253110f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochvoid RenderBlockFlow::addOverhangingFloats(RenderBlockFlow* child, bool makeChildPaintOtherFloats)
2532f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2533f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Prevent floats from being added to the canvas by the root element, e.g., <html>.
2534f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu    if (!child->containsFloats() || child->isRenderRegion() || child->createsBlockFormattingContext())
253510f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch        return;
2536f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2537f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    LayoutUnit childLogicalTop = child->logicalTop();
2538f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    LayoutUnit childLogicalLeft = child->logicalLeft();
2539f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2540f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // Floats that will remain the child's responsibility to paint should factor into its
2541f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    // overflow.
2542f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObjectSetIterator childEnd = child->m_floatingObjects->set().end();
2543f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    for (FloatingObjectSetIterator childIt = child->m_floatingObjects->set().begin(); childIt != childEnd; ++childIt) {
2544d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        FloatingObject* floatingObject = childIt->get();
2545197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        LayoutUnit logicalBottomForFloat = std::min(this->logicalBottomForFloat(floatingObject), LayoutUnit::max() - childLogicalTop);
2546f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        LayoutUnit logicalBottom = childLogicalTop + logicalBottomForFloat;
2547f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2548f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (logicalBottom > logicalHeight()) {
2549f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // If the object is not in the list, we add it now.
2550f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (!containsFloat(floatingObject->renderer())) {
2551f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                LayoutSize offset = isHorizontalWritingMode() ? LayoutSize(-childLogicalLeft, -childLogicalTop) : LayoutSize(-childLogicalTop, -childLogicalLeft);
2552f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                bool shouldPaint = false;
2553f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2554f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // The nearest enclosing layer always paints the float (so that zindex and stacking
2555f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // behaves properly). We always want to propagate the desire to paint the float as
2556f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // far out as we can, to the outermost block that overlaps the float, stopping only
2557f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // if we hit a self-painting layer boundary.
2558f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                if (floatingObject->renderer()->enclosingFloatPaintingLayer() == enclosingFloatPaintingLayer()) {
2559f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    floatingObject->setShouldPaint(false);
2560f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    shouldPaint = true;
2561f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                }
2562f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // We create the floating object list lazily.
2563f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                if (!m_floatingObjects)
2564f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                    createFloatingObjects();
2565f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2566f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                m_floatingObjects->add(floatingObject->copyToNewContainer(offset, shouldPaint, true));
2567f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
2568f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        } else {
2569f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (makeChildPaintOtherFloats && !floatingObject->shouldPaint() && !floatingObject->renderer()->hasSelfPaintingLayer()
2570f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                && floatingObject->renderer()->isDescendantOf(child) && floatingObject->renderer()->enclosingFloatPaintingLayer() == child->enclosingFloatPaintingLayer()) {
2571f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // The float is not overhanging from this block, so if it is a descendant of the child, the child should
2572f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // paint it (the other case is that it is intruding into the child), unless it has its own layer or enclosing
2573f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // layer.
2574f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // If makeChildPaintOtherFloats is false, it means that the child must already know about all the floats
2575f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                // it should paint.
2576f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                floatingObject->setShouldPaint(true);
2577f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
2578f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2579f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // Since the float doesn't overhang, it didn't get put into our list. We need to go ahead and add its overflow in to the
2580f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // child now.
2581f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (floatingObject->isDescendant())
2582f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                child->addOverflowFromChild(floatingObject->renderer(), LayoutSize(xPositionForFloatIncludingMargin(floatingObject), yPositionForFloatIncludingMargin(floatingObject)));
2583f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2584f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2585f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2586f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2587f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)LayoutUnit RenderBlockFlow::lowestFloatLogicalBottom(FloatingObject::Type floatType) const
2588f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2589f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!m_floatingObjects)
2590f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return 0;
2591f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2592f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return m_floatingObjects->lowestFloatLogicalBottom(floatType);
2593f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2594f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2595f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelow(LayoutUnit logicalHeight, ShapeOutsideFloatOffsetMode offsetMode) const
2596f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2597f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!m_floatingObjects)
2598f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return logicalHeight;
2599f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
26006f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    LayoutUnit logicalBottom;
2601f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2602f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObjectSetIterator end = floatingObjectSet.end();
2603f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
2604d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        FloatingObject* floatingObject = it->get();
2605f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        LayoutUnit floatLogicalBottom = logicalBottomForFloat(floatingObject);
2606f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        ShapeOutsideInfo* shapeOutside = floatingObject->renderer()->shapeOutsideInfo();
2607f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (shapeOutside && (offsetMode == ShapeOutsideFloatShapeOffset)) {
2608f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit shapeLogicalBottom = logicalTopForFloat(floatingObject) + marginBeforeForChild(floatingObject->renderer()) + shapeOutside->shapeLogicalBottom();
2609f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // Use the shapeLogicalBottom unless it extends outside of the margin box, in which case it is clipped.
2610f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (shapeLogicalBottom < floatLogicalBottom)
2611f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                floatLogicalBottom = shapeLogicalBottom;
2612f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2613f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (floatLogicalBottom > logicalHeight)
2614197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            logicalBottom = logicalBottom ? std::min(floatLogicalBottom, logicalBottom) : floatLogicalBottom;
2615f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2616f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
26176f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    return logicalBottom;
2618f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2619f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2620f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)bool RenderBlockFlow::hitTestFloats(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset)
2621f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2622f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (!m_floatingObjects)
2623f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        return false;
2624f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2625f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    LayoutPoint adjustedLocation = accumulatedOffset;
2626f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (isRenderView()) {
2627f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        adjustedLocation += toLayoutSize(toRenderView(this)->frameView()->scrollPosition());
2628f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2629f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2630f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2631f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    FloatingObjectSetIterator begin = floatingObjectSet.begin();
2632f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    for (FloatingObjectSetIterator it = floatingObjectSet.end(); it != begin;) {
2633f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        --it;
2634d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)        FloatingObject* floatingObject = it->get();
2635f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        if (floatingObject->shouldPaint() && !floatingObject->renderer()->hasSelfPaintingLayer()) {
2636f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit xOffset = xPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer()->x();
2637f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutUnit yOffset = yPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer()->y();
2638f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            LayoutPoint childPoint = flipFloatForWritingModeForChild(floatingObject, adjustedLocation + LayoutSize(xOffset, yOffset));
2639f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (floatingObject->renderer()->hitTest(request, result, locationInContainer, childPoint)) {
2640f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                updateHitTestResult(result, locationInContainer.point() - toLayoutSize(childPoint));
2641f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                return true;
2642f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
2643f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2644f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2645f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2646f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return false;
2647f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2648f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2649f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)void RenderBlockFlow::adjustForBorderFit(LayoutUnit x, LayoutUnit& left, LayoutUnit& right) const
2650f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2651c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (style()->visibility() != VISIBLE)
2652c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        return;
2653c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
2654c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    // We don't deal with relative positioning. Our assumption is that you shrink to fit the lines without accounting
2655c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    // for either overflow or translations via relative positioning.
2656c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (childrenInline()) {
2657c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        for (RootInlineBox* box = firstRootBox(); box; box = box->nextRootBox()) {
2658c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)            if (box->firstChild())
2659c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)                left = std::min(left, x + static_cast<LayoutUnit>(box->firstChild()->x()));
2660c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)            if (box->lastChild())
2661c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)                right = std::max(right, x + static_cast<LayoutUnit>(ceilf(box->lastChild()->logicalRight())));
2662c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        }
2663c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    } else {
2664c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        for (RenderBox* obj = firstChildBox(); obj; obj = obj->nextSiblingBox()) {
2665c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)            if (!obj->isFloatingOrOutOfFlowPositioned()) {
2666c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)                if (obj->isRenderBlockFlow() && !obj->hasOverflowClip()) {
2667c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)                    toRenderBlockFlow(obj)->adjustForBorderFit(x + obj->x(), left, right);
2668c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)                } else if (obj->style()->visibility() == VISIBLE) {
2669c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)                    // We are a replaced element or some kind of non-block-flow object.
2670c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)                    left = std::min(left, x + obj->x());
2671c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)                    right = std::max(right, x + obj->x() + obj->width());
2672c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)                }
2673c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)            }
2674c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        }
2675c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    }
2676c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
2677c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (m_floatingObjects) {
2678f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2679f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        FloatingObjectSetIterator end = floatingObjectSet.end();
2680f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
2681d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)            FloatingObject* floatingObject = it->get();
2682f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            // Only examine the object if our m_shouldPaint flag is set.
2683f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            if (floatingObject->shouldPaint()) {
2684f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                LayoutUnit floatLeft = xPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer()->x();
2685f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)                LayoutUnit floatRight = floatLeft + floatingObject->renderer()->width();
2686197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                left = std::min(left, floatLeft);
2687197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch                right = std::max(right, floatRight);
2688f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)            }
2689f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)        }
2690f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    }
2691f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2692f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2693c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)void RenderBlockFlow::fitBorderToLinesIfNeeded()
2694c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles){
2695c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (style()->borderFit() == BorderFitBorder || hasOverrideWidth())
2696c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        return;
2697c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
2698c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    // Walk any normal flow lines to snugly fit.
2699c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    LayoutUnit left = LayoutUnit::max();
2700c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    LayoutUnit right = LayoutUnit::min();
2701c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    LayoutUnit oldWidth = contentWidth();
2702c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    adjustForBorderFit(0, left, right);
2703c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
2704c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    // Clamp to our existing edges. We can never grow. We only shrink.
2705c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    LayoutUnit leftEdge = borderLeft() + paddingLeft();
2706c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    LayoutUnit rightEdge = leftEdge + oldWidth;
2707c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    left = std::min(rightEdge, std::max(leftEdge, left));
2708c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    right = std::max(left, std::min(rightEdge, right));
2709c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
2710c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    LayoutUnit newContentWidth = right - left;
2711c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    if (newContentWidth == oldWidth)
2712c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)        return;
2713c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
2714c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    setOverrideLogicalContentWidth(newContentWidth);
2715c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    layoutBlock(false);
2716c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    clearOverrideLogicalContentWidth();
2717c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)}
2718c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
271951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)LayoutUnit RenderBlockFlow::logicalLeftFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
2720f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2721f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
272251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return m_floatingObjects->logicalLeftOffset(fixedOffset, logicalTop, logicalHeight);
2723f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2724f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return fixedOffset;
2725f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2726f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
272751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)LayoutUnit RenderBlockFlow::logicalRightFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
2728f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles){
2729f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    if (m_floatingObjects && m_floatingObjects->hasRightObjects())
273051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return m_floatingObjects->logicalRightOffset(fixedOffset, logicalTop, logicalHeight);
2731f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
2732f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)    return fixedOffset;
2733f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)}
2734f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)
27357242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano TucciGapRects RenderBlockFlow::inlineSelectionGaps(const RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
27367242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const PaintInfo* paintInfo) const
273751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
273851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    GapRects result;
273951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
274051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    bool containsStart = selectionState() == SelectionStart || selectionState() == SelectionBoth;
274151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
274251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (!firstLineBox()) {
274351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        if (containsStart) {
274451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            // Go ahead and update our lastLogicalTop to be the bottom of the block.  <hr>s or empty blocks with height can trip this
274551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            // case.
274651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            lastLogicalTop = rootBlock->blockDirectionOffset(offsetFromRootBlock) + logicalHeight();
274751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, logicalHeight());
274851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            lastLogicalRight = logicalRightSelectionOffset(rootBlock, logicalHeight());
274951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        }
275051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return result;
275151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
275251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
275351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    RootInlineBox* lastSelectedLine = 0;
275451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    RootInlineBox* curr;
275551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    for (curr = firstRootBox(); curr && !curr->hasSelectedChildren(); curr = curr->nextRootBox()) { }
275651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
275751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    // Now paint the gaps for the lines.
275851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    for (; curr && curr->hasSelectedChildren(); curr = curr->nextRootBox()) {
275951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        LayoutUnit selTop =  curr->selectionTopAdjustedForPrecedingBlock();
276051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        LayoutUnit selHeight = curr->selectionHeightAdjustedForPrecedingBlock();
276151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
276251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        if (!containsStart && !lastSelectedLine && selectionState() != SelectionStart && selectionState() != SelectionBoth) {
276351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop,
276451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)                lastLogicalLeft, lastLogicalRight, selTop, paintInfo));
276551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        }
276651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
276751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        LayoutRect logicalRect(curr->logicalLeft(), selTop, curr->logicalWidth(), selTop + selHeight);
276851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        logicalRect.move(isHorizontalWritingMode() ? offsetFromRootBlock : offsetFromRootBlock.transposedSize());
276951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        LayoutRect physicalRect = rootBlock->logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
277051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        if (!paintInfo || (isHorizontalWritingMode() && physicalRect.y() < paintInfo->rect.maxY() && physicalRect.maxY() > paintInfo->rect.y())
277151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            || (!isHorizontalWritingMode() && physicalRect.x() < paintInfo->rect.maxX() && physicalRect.maxX() > paintInfo->rect.x()))
277251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)            result.unite(curr->lineSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, selTop, selHeight, paintInfo));
277351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
277451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        lastSelectedLine = curr;
277551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
277651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
277751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (containsStart && !lastSelectedLine) {
277851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        // VisibleSelection must start just after our last line.
277951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        lastSelectedLine = lastRootBox();
278051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
278151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
278251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (lastSelectedLine && selectionState() != SelectionEnd && selectionState() != SelectionBoth) {
278351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        // Go ahead and update our lastY to be the bottom of the last selected line.
278451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        lastLogicalTop = rootBlock->blockDirectionOffset(offsetFromRootBlock) + lastSelectedLine->selectionBottom();
278551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, lastSelectedLine->selectionBottom());
278651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        lastLogicalRight = logicalRightSelectionOffset(rootBlock, lastSelectedLine->selectionBottom());
278751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    }
278851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return result;
278951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
279051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
2791197021e6b966cfb06891637935ef33fff06433d1Ben Murdochvoid RenderBlockFlow::setPaginationStrut(LayoutUnit strut)
2792197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch{
2793197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (!m_rareData) {
2794197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if (!strut)
2795197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            return;
2796e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)        m_rareData = adoptPtrWillBeNoop(new RenderBlockFlowRareData(this));
2797197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
2798197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    m_rareData->m_paginationStrut = strut;
2799197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch}
2800197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
2801c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)bool RenderBlockFlow::avoidsFloats() const
2802c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles){
2803c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    // Floats can't intrude into our box if we have a non-auto column count or width.
2804c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    // Note: we need to use RenderBox::avoidsFloats here since RenderBlock::avoidsFloats is always true.
2805c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)    return RenderBox::avoidsFloats() || !style()->hasAutoColumnCount() || !style()->hasAutoColumnWidth();
2806c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)}
2807c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)
28087242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano TucciLayoutUnit RenderBlockFlow::logicalLeftSelectionOffset(const RenderBlock* rootBlock, LayoutUnit position) const
280909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
281009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit logicalLeft = logicalLeftOffsetForLine(position, false);
281109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (logicalLeft == logicalLeftOffsetForContent())
281209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return RenderBlock::logicalLeftSelectionOffset(rootBlock, position);
281309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
28147242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    const RenderBlock* cb = this;
281509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    while (cb != rootBlock) {
281609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        logicalLeft += cb->logicalLeft();
281709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        cb = cb->containingBlock();
281809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    }
281909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return logicalLeft;
282009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
282109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
28227242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano TucciLayoutUnit RenderBlockFlow::logicalRightSelectionOffset(const RenderBlock* rootBlock, LayoutUnit position) const
282309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
282409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    LayoutUnit logicalRight = logicalRightOffsetForLine(position, false);
282509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (logicalRight == logicalRightOffsetForContent())
282609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return RenderBlock::logicalRightSelectionOffset(rootBlock, position);
282709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
28287242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    const RenderBlock* cb = this;
282909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    while (cb != rootBlock) {
283009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        logicalRight += cb->logicalLeft();
283109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        cb = cb->containingBlock();
283209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    }
283309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return logicalRight;
283409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
283509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
283609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)RootInlineBox* RenderBlockFlow::createRootInlineBox()
283751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
2838d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return new RootInlineBox(*this);
2839d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
2840d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
2841197021e6b966cfb06891637935ef33fff06433d1Ben Murdochbool RenderBlockFlow::isPagedOverflow(const RenderStyle* style)
2842197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch{
2843197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    return style->isOverflowPaged() && node() != document().viewportDefiningElement();
2844197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch}
2845197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
2846197021e6b966cfb06891637935ef33fff06433d1Ben MurdochRenderBlockFlow::FlowThreadType RenderBlockFlow::flowThreadType(const RenderStyle* style)
2847197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch{
2848197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (isPagedOverflow(style))
2849197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return PagedFlowThread;
2850197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (style->specifiesColumns())
2851197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return MultiColumnFlowThread;
2852197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    return NoFlowThread;
2853197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch}
2854197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
2855197021e6b966cfb06891637935ef33fff06433d1Ben MurdochRenderMultiColumnFlowThread* RenderBlockFlow::createMultiColumnFlowThread(FlowThreadType type)
2856197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch{
2857197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    switch (type) {
2858197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    case MultiColumnFlowThread:
2859197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return RenderMultiColumnFlowThread::createAnonymous(document(), style());
2860197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    case PagedFlowThread:
2861197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        // Paged overflow is currently done using the multicol implementation.
2862197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return RenderPagedFlowThread::createAnonymous(document(), style());
2863197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    default:
2864197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        ASSERT_NOT_REACHED();
2865197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return nullptr;
2866197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    }
2867197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch}
2868197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
2869197021e6b966cfb06891637935ef33fff06433d1Ben Murdochvoid RenderBlockFlow::createOrDestroyMultiColumnFlowThreadIfNeeded(const RenderStyle* oldStyle)
2870d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
28716f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    if (!document().regionBasedColumnsEnabled())
2872d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        return;
2873d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
2874197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // Paged overflow trumps multicol in this implementation. Ideally, it should be possible to have
2875197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // both paged overflow and multicol on the same element, but then we need two flow
2876197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // threads. Anyway, this is nothing to worry about until we can actually nest multicol properly
2877197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    // inside other fragmentation contexts.
2878197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    FlowThreadType type = flowThreadType(style());
2879197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
2880197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (multiColumnFlowThread()) {
2881197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        ASSERT(oldStyle);
2882197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        if (type != flowThreadType(oldStyle)) {
2883197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            // If we're no longer to be multicol/paged, destroy the flow thread. Also destroy it
2884197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            // when switching between multicol and paged, since that affects the column set
2885197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch            // structure (multicol containers may have spanners, paged containers may not).
28866f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch            multiColumnFlowThread()->evacuateAndDestroy();
28876f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch            ASSERT(!multiColumnFlowThread());
28886f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch        }
28896f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch    }
2890197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
2891197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    if (type == NoFlowThread || multiColumnFlowThread())
2892197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch        return;
2893197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
2894197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    RenderMultiColumnFlowThread* flowThread = createMultiColumnFlowThread(type);
2895197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    addChild(flowThread);
2896197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    flowThread->populate();
2897197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    RenderBlockFlowRareData& rareData = ensureRareData();
2898197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    ASSERT(!rareData.m_multiColumnFlowThread);
2899197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    rareData.m_multiColumnFlowThread = flowThread;
290051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
290151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
290251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)RenderBlockFlow::RenderBlockFlowRareData& RenderBlockFlow::ensureRareData()
290351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles){
290451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    if (m_rareData)
290551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)        return *m_rareData;
290651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
2907e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    m_rareData = adoptPtrWillBeNoop(new RenderBlockFlowRareData(this));
290851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    return *m_rareData;
290951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)}
291051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)
29117242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccivoid RenderBlockFlow::positionDialog()
29127242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci{
29137242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    HTMLDialogElement* dialog = toHTMLDialogElement(node());
29147242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (dialog->centeringMode() == HTMLDialogElement::NotCentered)
29157242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return;
29167242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
29177242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    bool canCenterDialog = (style()->position() == AbsolutePosition || style()->position() == FixedPosition)
29187242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        && style()->hasAutoTopAndBottom();
29197242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
29207242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (dialog->centeringMode() == HTMLDialogElement::Centered) {
29217242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        if (canCenterDialog)
29227242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci            setY(dialog->centeredPosition());
29237242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return;
29247242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
29257242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
29267242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    ASSERT(dialog->centeringMode() == HTMLDialogElement::NeedsCentering);
29277242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (!canCenterDialog) {
29287242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        dialog->setNotCentered();
29297242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return;
29307242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
29317242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
29327242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    FrameView* frameView = document().view();
29337242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    LayoutUnit top = (style()->position() == FixedPosition) ? 0 : frameView->scrollOffset().height();
29347242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    int visibleHeight = frameView->visibleContentRect(IncludeScrollbars).height();
29357242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (height() < visibleHeight)
29367242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        top += (visibleHeight - height()) / 2;
29377242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    setY(top);
29387242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    dialog->setCentered(top);
29397242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci}
29407242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
2941c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
2942