blob: 07e1832052936caa8b367c7c33db30246766fd9c [file] [log] [blame]
hyatt@apple.com5388e672013-09-06 20:54:47 +00001/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2007 David Smith (catfish.man@gmail.com)
5 * Copyright (C) 2003-2013 Apple Inc. All rights reserved.
6 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24#include "config.h"
25#include "RenderBlockFlow.h"
26
weinig@apple.com611b9292013-10-20 22:57:54 +000027#include "Editor.h"
bjonesbe@adobe.com67478092013-09-09 22:18:17 +000028#include "FloatingObjects.h"
weinig@apple.com611b9292013-10-20 22:57:54 +000029#include "Frame.h"
zalan@apple.come36543a2014-07-29 01:45:54 +000030#include "FrameSelection.h"
darin@apple.com15708b12014-03-16 16:38:58 +000031#include "HTMLElement.h"
bjonesbe@adobe.com24199752013-10-08 23:20:42 +000032#include "HitTestLocation.h"
weinig@apple.com611b9292013-10-20 22:57:54 +000033#include "InlineTextBox.h"
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +000034#include "LayoutRepainter.h"
35#include "RenderFlowThread.h"
akling@apple.comf3028052013-11-04 08:46:06 +000036#include "RenderIterator.h"
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +000037#include "RenderLayer.h"
antti@apple.com0b3dffe2014-03-24 16:30:52 +000038#include "RenderListItem.h"
hyatt@apple.com73715ca2014-05-06 21:35:52 +000039#include "RenderMarquee.h"
hyatt@apple.comd4be3772014-01-24 19:55:33 +000040#include "RenderMultiColumnFlowThread.h"
41#include "RenderMultiColumnSet.h"
mihnea@adobe.combe79cf12013-10-17 09:02:19 +000042#include "RenderNamedFlowFragment.h"
hyatt@apple.com73715ca2014-05-06 21:35:52 +000043#include "RenderTableCell.h"
antti@apple.com940f5872013-10-24 20:31:11 +000044#include "RenderText.h"
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +000045#include "RenderView.h"
antti@apple.com940f5872013-10-24 20:31:11 +000046#include "SimpleLineLayoutFunctions.h"
hyatt@apple.com3cd5c772013-09-27 18:22:50 +000047#include "VerticalPositionCache.h"
weinig@apple.com611b9292013-10-20 22:57:54 +000048#include "VisiblePosition.h"
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +000049
hyatt@apple.com5388e672013-09-06 20:54:47 +000050namespace WebCore {
51
bjonesbe@adobe.com24199752013-10-08 23:20:42 +000052bool RenderBlock::s_canPropagateFloatIntoSibling = false;
53
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000054struct SameSizeAsMarginInfo {
55 uint32_t bitfields : 16;
56 LayoutUnit margins[2];
57};
58
59COMPILE_ASSERT(sizeof(RenderBlockFlow::MarginValues) == sizeof(LayoutUnit[4]), MarginValues_should_stay_small);
akling@apple.com42e10632013-10-14 17:55:52 +000060COMPILE_ASSERT(sizeof(RenderBlockFlow::MarginInfo) == sizeof(SameSizeAsMarginInfo), MarginInfo_should_stay_small);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000061
62// Our MarginInfo state used when laying out block children.
weinig@apple.com12840dc2013-10-22 23:59:08 +000063RenderBlockFlow::MarginInfo::MarginInfo(RenderBlockFlow& block, LayoutUnit beforeBorderPadding, LayoutUnit afterBorderPadding)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000064 : m_atBeforeSideOfBlock(true)
65 , m_atAfterSideOfBlock(false)
66 , m_hasMarginBeforeQuirk(false)
67 , m_hasMarginAfterQuirk(false)
68 , m_determinedMarginBeforeQuirk(false)
69 , m_discardMargin(false)
70{
akling@apple.com827be9c2013-10-29 02:58:43 +000071 const RenderStyle& blockStyle = block.style();
weinig@apple.com12840dc2013-10-22 23:59:08 +000072 ASSERT(block.isRenderView() || block.parent());
73 m_canCollapseWithChildren = !block.isRenderView() && !block.isRoot() && !block.isOutOfFlowPositioned()
74 && !block.isFloating() && !block.isTableCell() && !block.hasOverflowClip() && !block.isInlineBlockOrInlineTable()
75 && !block.isRenderFlowThread() && !block.isWritingModeRoot() && !block.parent()->isFlexibleBox()
akling@apple.com827be9c2013-10-29 02:58:43 +000076 && blockStyle.hasAutoColumnCount() && blockStyle.hasAutoColumnWidth() && !blockStyle.columnSpan();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000077
akling@apple.com827be9c2013-10-29 02:58:43 +000078 m_canCollapseMarginBeforeWithChildren = m_canCollapseWithChildren && !beforeBorderPadding && blockStyle.marginBeforeCollapse() != MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000079
80 // If any height other than auto is specified in CSS, then we don't collapse our bottom
81 // margins with our children's margins. To do otherwise would be to risk odd visual
82 // effects when the children overflow out of the parent block and yet still collapse
83 // with it. We also don't collapse if we have any bottom border/padding.
84 m_canCollapseMarginAfterWithChildren = m_canCollapseWithChildren && !afterBorderPadding
akling@apple.com827be9c2013-10-29 02:58:43 +000085 && (blockStyle.logicalHeight().isAuto() && !blockStyle.logicalHeight().value()) && blockStyle.marginAfterCollapse() != MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000086
weinig@apple.com12840dc2013-10-22 23:59:08 +000087 m_quirkContainer = block.isTableCell() || block.isBody();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000088
weinig@apple.com12840dc2013-10-22 23:59:08 +000089 m_discardMargin = m_canCollapseMarginBeforeWithChildren && block.mustDiscardMarginBefore();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000090
weinig@apple.com12840dc2013-10-22 23:59:08 +000091 m_positiveMargin = (m_canCollapseMarginBeforeWithChildren && !block.mustDiscardMarginBefore()) ? block.maxPositiveMarginBefore() : LayoutUnit();
92 m_negativeMargin = (m_canCollapseMarginBeforeWithChildren && !block.mustDiscardMarginBefore()) ? block.maxNegativeMarginBefore() : LayoutUnit();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000093}
94
akling@apple.com8f40c5b2013-10-27 22:54:07 +000095RenderBlockFlow::RenderBlockFlow(Element& element, PassRef<RenderStyle> style)
dbates@webkit.org0cefe4f2014-07-03 22:13:54 +000096 : RenderBlock(element, WTF::move(style), RenderBlockFlowFlag)
aestes@apple.com6751d842014-01-12 02:51:25 +000097#if ENABLE(IOS_TEXT_AUTOSIZING)
98 , m_widthForTextAutosizing(-1)
99 , m_lineCountForTextAutosizing(NOT_SET)
100#endif
hyatt@apple.com5388e672013-09-06 20:54:47 +0000101{
weinig@apple.com611b9292013-10-20 22:57:54 +0000102 setChildrenInline(true);
akling@apple.com42e10632013-10-14 17:55:52 +0000103}
104
akling@apple.com8f40c5b2013-10-27 22:54:07 +0000105RenderBlockFlow::RenderBlockFlow(Document& document, PassRef<RenderStyle> style)
dbates@webkit.org0cefe4f2014-07-03 22:13:54 +0000106 : RenderBlock(document, WTF::move(style), RenderBlockFlowFlag)
aestes@apple.com6751d842014-01-12 02:51:25 +0000107#if ENABLE(IOS_TEXT_AUTOSIZING)
108 , m_widthForTextAutosizing(-1)
109 , m_lineCountForTextAutosizing(NOT_SET)
110#endif
akling@apple.com42e10632013-10-14 17:55:52 +0000111{
weinig@apple.com611b9292013-10-20 22:57:54 +0000112 setChildrenInline(true);
hyatt@apple.com5388e672013-09-06 20:54:47 +0000113}
114
115RenderBlockFlow::~RenderBlockFlow()
116{
117}
118
hyatt@apple.com39746fd2014-01-24 22:52:41 +0000119void RenderBlockFlow::createMultiColumnFlowThread()
hyatt@apple.comd4be3772014-01-24 19:55:33 +0000120{
hyatt@apple.comd4be3772014-01-24 19:55:33 +0000121 RenderMultiColumnFlowThread* flowThread = new RenderMultiColumnFlowThread(document(), RenderStyle::createAnonymousStyleWithDisplay(&style(), BLOCK));
122 flowThread->initializeStyle();
hyatt@apple.comc1c39032014-04-15 23:25:58 +0000123 setChildrenInline(false); // Do this to avoid wrapping inline children that are just going to move into the flow thread.
abucur@adobe.com99757c62014-07-31 13:43:52 +0000124 deleteLines();
hyatt@apple.comd4be3772014-01-24 19:55:33 +0000125 RenderBlock::addChild(flowThread);
hyatt@apple.comc1c39032014-04-15 23:25:58 +0000126 flowThread->populate(); // Called after the flow thread is inserted so that we are reachable by the flow thread.
hyatt@apple.comd4be3772014-01-24 19:55:33 +0000127 setMultiColumnFlowThread(flowThread);
128}
129
hyatt@apple.com39746fd2014-01-24 22:52:41 +0000130void RenderBlockFlow::destroyMultiColumnFlowThread()
131{
hyatt@apple.comc1c39032014-04-15 23:25:58 +0000132 multiColumnFlowThread()->evacuateAndDestroy();
133 ASSERT(!multiColumnFlowThread());
hyatt@apple.com39746fd2014-01-24 22:52:41 +0000134}
135
mihnea@adobe.combe79cf12013-10-17 09:02:19 +0000136void RenderBlockFlow::insertedIntoTree()
137{
138 RenderBlock::insertedIntoTree();
139 createRenderNamedFlowFragmentIfNeeded();
140}
141
hyatt@apple.com3cd5c772013-09-27 18:22:50 +0000142void RenderBlockFlow::willBeDestroyed()
143{
weinig@apple.com611b9292013-10-20 22:57:54 +0000144 // Mark as being destroyed to avoid trouble with merges in removeChild().
145 m_beingDestroyed = true;
146
mihnea@adobe.combe79cf12013-10-17 09:02:19 +0000147 if (renderNamedFlowFragment())
148 setRenderNamedFlowFragment(0);
weinig@apple.com611b9292013-10-20 22:57:54 +0000149
weinig@apple.com611b9292013-10-20 22:57:54 +0000150 // Make sure to destroy anonymous children first while they are still connected to the rest of the tree, so that they will
151 // properly dirty line boxes that they are removed from. Effects that do :before/:after only on hover could crash otherwise.
152 destroyLeftoverChildren();
153
154 // Destroy our continuation before anything other than anonymous children.
155 // The reason we don't destroy it before anonymous children is that they may
156 // have continuations of their own that are anonymous children of our continuation.
157 RenderBoxModelObject* continuation = this->continuation();
158 if (continuation) {
159 continuation->destroy();
160 setContinuation(0);
161 }
162
163 if (!documentBeingDestroyed()) {
akling@apple.comee3c8df2013-11-06 08:09:44 +0000164 if (firstRootBox()) {
weinig@apple.com611b9292013-10-20 22:57:54 +0000165 // We can't wait for RenderBox::destroy to clear the selection,
166 // because by then we will have nuked the line boxes.
weinig@apple.com611b9292013-10-20 22:57:54 +0000167 if (isSelectionBorder())
zalan@apple.come36543a2014-07-29 01:45:54 +0000168 frame().selection().setNeedsSelectionUpdate();
weinig@apple.com611b9292013-10-20 22:57:54 +0000169
170 // If we are an anonymous block, then our line boxes might have children
171 // that will outlast this block. In the non-anonymous block case those
172 // children will be destroyed by the time we return from this function.
173 if (isAnonymousBlock()) {
akling@apple.comee3c8df2013-11-06 08:09:44 +0000174 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
weinig@apple.com611b9292013-10-20 22:57:54 +0000175 while (auto childBox = box->firstChild())
176 childBox->removeFromParent();
177 }
178 }
179 } else if (parent())
180 parent()->dirtyLinesFromChangedChild(this);
181 }
182
akling@apple.com31dd4f42013-10-30 22:27:59 +0000183 m_lineBoxes.deleteLineBoxes();
weinig@apple.com611b9292013-10-20 22:57:54 +0000184
dbates@webkit.org34f59002014-05-20 20:34:35 +0000185 removeFromUpdateScrollInfoAfterLayoutTransaction();
weinig@apple.com611b9292013-10-20 22:57:54 +0000186
187 // NOTE: This jumps down to RenderBox, bypassing RenderBlock since it would do duplicate work.
188 RenderBox::willBeDestroyed();
hyatt@apple.com3cd5c772013-09-27 18:22:50 +0000189}
190
bjonesbe@adobe.comf9f10402014-02-20 19:40:28 +0000191void RenderBlockFlow::rebuildFloatingObjectSetFromIntrudingFloats()
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000192{
193 if (m_floatingObjects)
194 m_floatingObjects->setHorizontalWritingMode(isHorizontalWritingMode());
195
196 HashSet<RenderBox*> oldIntrudingFloatSet;
197 if (!childrenInline() && m_floatingObjects) {
198 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +0000199 auto end = floatingObjectSet.end();
200 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
201 FloatingObject* floatingObject = it->get();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000202 if (!floatingObject->isDescendant())
darin@apple.com7cad7042013-09-24 05:53:55 +0000203 oldIntrudingFloatSet.add(&floatingObject->renderer());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000204 }
205 }
206
207 // Inline blocks are covered by the isReplaced() check in the avoidFloats method.
208 if (avoidsFloats() || isRoot() || isRenderView() || isFloatingOrOutOfFlowPositioned() || isTableCell()) {
mihnea@adobe.combe79cf12013-10-17 09:02:19 +0000209 if (m_floatingObjects)
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000210 m_floatingObjects->clear();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000211 if (!oldIntrudingFloatSet.isEmpty())
212 markAllDescendantsWithFloatsForLayout();
213 return;
214 }
215
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000216 RendererToFloatInfoMap floatMap;
217
218 if (m_floatingObjects) {
bjonesbe@adobe.com0434768a2013-09-16 22:01:38 +0000219 if (childrenInline())
220 m_floatingObjects->moveAllToFloatInfoMap(floatMap);
221 else
222 m_floatingObjects->clear();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000223 }
224
225 // We should not process floats if the parent node is not a RenderBlock. Otherwise, we will add
226 // floats in an invalid context. This will cause a crash arising from a bad cast on the parent.
227 // See <rdar://problem/8049753>, where float property is applied on a text node in a SVG.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000228 if (!parent() || !parent()->isRenderBlockFlow())
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000229 return;
230
231 // Attempt to locate a previous sibling with overhanging floats. We skip any elements that are
232 // out of flow (like floating/positioned elements), and we also skip over any objects that may have shifted
233 // to avoid floats.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000234 RenderBlockFlow* parentBlock = toRenderBlockFlow(parent());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000235 bool parentHasFloats = false;
236 RenderObject* prev = previousSibling();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000237 while (prev && (prev->isFloatingOrOutOfFlowPositioned() || !prev->isBox() || !prev->isRenderBlockFlow() || toRenderBlockFlow(prev)->avoidsFloats())) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000238 if (prev->isFloating())
239 parentHasFloats = true;
240 prev = prev->previousSibling();
241 }
242
robert@webkit.org97037ef2013-11-20 19:26:10 +0000243 // First add in floats from the parent. Self-collapsing blocks let their parent track any floats that intrude into
244 // them (as opposed to floats they contain themselves) so check for those here too.
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000245 LayoutUnit logicalTopOffset = logicalTop();
robert@webkit.org97037ef2013-11-20 19:26:10 +0000246 if (parentHasFloats || (parentBlock->lowestFloatLogicalBottom() > logicalTopOffset && prev && toRenderBlockFlow(prev)->isSelfCollapsingBlock()))
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000247 addIntrudingFloats(parentBlock, parentBlock->logicalLeftOffsetForContent(), logicalTopOffset);
248
249 LayoutUnit logicalLeftOffset = 0;
250 if (prev)
251 logicalTopOffset -= toRenderBox(prev)->logicalTop();
252 else {
253 prev = parentBlock;
254 logicalLeftOffset += parentBlock->logicalLeftOffsetForContent();
255 }
256
257 // Add overhanging floats from the previous RenderBlock, but only if it has a float that intrudes into our space.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000258 RenderBlockFlow* block = toRenderBlockFlow(prev);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000259 if (block->m_floatingObjects && block->lowestFloatLogicalBottom() > logicalTopOffset)
260 addIntrudingFloats(block, logicalLeftOffset, logicalTopOffset);
261
262 if (childrenInline()) {
263 LayoutUnit changeLogicalTop = LayoutUnit::max();
264 LayoutUnit changeLogicalBottom = LayoutUnit::min();
265 if (m_floatingObjects) {
266 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +0000267 auto end = floatingObjectSet.end();
268 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +0000269 FloatingObject* floatingObject = it->get();
270 std::unique_ptr<FloatingObject> oldFloatingObject = floatMap.take(&floatingObject->renderer());
271 LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000272 if (oldFloatingObject) {
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +0000273 LayoutUnit oldLogicalBottom = logicalBottomForFloat(oldFloatingObject.get());
274 if (logicalWidthForFloat(floatingObject) != logicalWidthForFloat(oldFloatingObject.get()) || logicalLeftForFloat(floatingObject) != logicalLeftForFloat(oldFloatingObject.get())) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000275 changeLogicalTop = 0;
andersca@apple.com86298632013-11-10 19:32:33 +0000276 changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalBottom, oldLogicalBottom));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000277 } else {
278 if (logicalBottom != oldLogicalBottom) {
andersca@apple.com86298632013-11-10 19:32:33 +0000279 changeLogicalTop = std::min(changeLogicalTop, std::min(logicalBottom, oldLogicalBottom));
280 changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalBottom, oldLogicalBottom));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000281 }
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +0000282 LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
283 LayoutUnit oldLogicalTop = logicalTopForFloat(oldFloatingObject.get());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000284 if (logicalTop != oldLogicalTop) {
andersca@apple.com86298632013-11-10 19:32:33 +0000285 changeLogicalTop = std::min(changeLogicalTop, std::min(logicalTop, oldLogicalTop));
286 changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalTop, oldLogicalTop));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000287 }
288 }
289
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000290 if (oldFloatingObject->originatingLine() && !selfNeedsLayout()) {
291 ASSERT(&oldFloatingObject->originatingLine()->renderer() == this);
292 oldFloatingObject->originatingLine()->markDirty();
293 }
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000294 } else {
295 changeLogicalTop = 0;
andersca@apple.com86298632013-11-10 19:32:33 +0000296 changeLogicalBottom = std::max(changeLogicalBottom, logicalBottom);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000297 }
298 }
299 }
300
darin@apple.com7cad7042013-09-24 05:53:55 +0000301 auto end = floatMap.end();
302 for (auto it = floatMap.begin(); it != end; ++it) {
303 FloatingObject* floatingObject = it->value.get();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000304 if (!floatingObject->isDescendant()) {
305 changeLogicalTop = 0;
andersca@apple.com86298632013-11-10 19:32:33 +0000306 changeLogicalBottom = std::max(changeLogicalBottom, logicalBottomForFloat(floatingObject));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000307 }
308 }
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000309
310 markLinesDirtyInBlockRange(changeLogicalTop, changeLogicalBottom);
311 } else if (!oldIntrudingFloatSet.isEmpty()) {
312 // If there are previously intruding floats that no longer intrude, then children with floats
313 // should also get layout because they might need their floating object lists cleared.
314 if (m_floatingObjects->set().size() < oldIntrudingFloatSet.size())
315 markAllDescendantsWithFloatsForLayout();
316 else {
317 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +0000318 auto end = floatingObjectSet.end();
319 for (auto it = floatingObjectSet.begin(); it != end && !oldIntrudingFloatSet.isEmpty(); ++it)
320 oldIntrudingFloatSet.remove(&(*it)->renderer());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000321 if (!oldIntrudingFloatSet.isEmpty())
322 markAllDescendantsWithFloatsForLayout();
323 }
324 }
325}
326
hyatt@apple.com73715ca2014-05-06 21:35:52 +0000327void RenderBlockFlow::adjustIntrinsicLogicalWidthsForColumns(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
328{
329 if (!style().hasAutoColumnCount() || !style().hasAutoColumnWidth()) {
330 // The min/max intrinsic widths calculated really tell how much space elements need when
331 // laid out inside the columns. In order to eventually end up with the desired column width,
332 // we need to convert them to values pertaining to the multicol container.
333 int columnCount = style().hasAutoColumnCount() ? 1 : style().columnCount();
334 LayoutUnit columnWidth;
335 LayoutUnit colGap = columnGap();
336 LayoutUnit gapExtra = (columnCount - 1) * colGap;
337 if (style().hasAutoColumnWidth())
338 minLogicalWidth = minLogicalWidth * columnCount + gapExtra;
339 else {
340 columnWidth = style().columnWidth();
341 minLogicalWidth = std::min(minLogicalWidth, columnWidth);
342 }
343 // FIXME: If column-count is auto here, we should resolve it to calculate the maximum
344 // intrinsic width, instead of pretending that it's 1. The only way to do that is by
345 // performing a layout pass, but this is not an appropriate time or place for layout. The
346 // good news is that if height is unconstrained and there are no explicit breaks, the
347 // resolved column-count really should be 1.
348 maxLogicalWidth = std::max(maxLogicalWidth, columnWidth) * columnCount + gapExtra;
349 }
350}
351
352void RenderBlockFlow::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
353{
354 if (childrenInline()) {
355 // FIXME: Remove this const_cast.
356 const_cast<RenderBlockFlow*>(this)->computeInlinePreferredLogicalWidths(minLogicalWidth, maxLogicalWidth);
357 } else
358 computeBlockPreferredLogicalWidths(minLogicalWidth, maxLogicalWidth);
359
360 maxLogicalWidth = std::max(minLogicalWidth, maxLogicalWidth);
361
362 adjustIntrinsicLogicalWidthsForColumns(minLogicalWidth, maxLogicalWidth);
363
364 if (!style().autoWrap() && childrenInline()) {
365 // A horizontal marquee with inline children has no minimum width.
366 if (layer() && layer()->marquee() && layer()->marquee()->isHorizontal())
367 minLogicalWidth = 0;
368 }
369
370 if (isTableCell()) {
371 Length tableCellWidth = toRenderTableCell(this)->styleOrColLogicalWidth();
372 if (tableCellWidth.isFixed() && tableCellWidth.value() > 0)
373 maxLogicalWidth = std::max(minLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(tableCellWidth.value()));
374 }
375
376 int scrollbarWidth = instrinsicScrollbarLogicalWidth();
377 maxLogicalWidth += scrollbarWidth;
378 minLogicalWidth += scrollbarWidth;
379}
380
381bool RenderBlockFlow::recomputeLogicalWidthAndColumnWidth()
382{
383 bool changed = recomputeLogicalWidth();
384
385 LayoutUnit oldColumnWidth = computedColumnWidth();
386 computeColumnCountAndWidth();
387
388 return changed || oldColumnWidth != computedColumnWidth();
389}
390
391LayoutUnit RenderBlockFlow::columnGap() const
392{
393 if (style().hasNormalColumnGap())
394 return style().fontDescription().computedPixelSize(); // "1em" is recommended as the normal gap setting. Matches <p> margins.
395 return style().columnGap();
396}
397
398void RenderBlockFlow::computeColumnCountAndWidth()
399{
400 // Calculate our column width and column count.
401 // FIXME: Can overflow on fast/block/float/float-not-removed-from-next-sibling4.html, see https://bugs.webkit.org/show_bug.cgi?id=68744
402 unsigned desiredColumnCount = 1;
403 LayoutUnit desiredColumnWidth = contentLogicalWidth();
404
405 // For now, we don't support multi-column layouts when printing, since we have to do a lot of work for proper pagination.
406 if (document().paginated() || (style().hasAutoColumnCount() && style().hasAutoColumnWidth()) || !style().hasInlineColumnAxis()) {
407 setComputedColumnCountAndWidth(desiredColumnCount, desiredColumnWidth);
408 return;
409 }
410
411 LayoutUnit availWidth = desiredColumnWidth;
412 LayoutUnit colGap = columnGap();
413 LayoutUnit colWidth = std::max<LayoutUnit>(LayoutUnit::fromPixel(1), LayoutUnit(style().columnWidth()));
414 int colCount = std::max<int>(1, style().columnCount());
415
416 if (style().hasAutoColumnWidth() && !style().hasAutoColumnCount()) {
417 desiredColumnCount = colCount;
418 desiredColumnWidth = std::max<LayoutUnit>(0, (availWidth - ((desiredColumnCount - 1) * colGap)) / desiredColumnCount);
419 } else if (!style().hasAutoColumnWidth() && style().hasAutoColumnCount()) {
420 desiredColumnCount = std::max<LayoutUnit>(1, (availWidth + colGap) / (colWidth + colGap));
421 desiredColumnWidth = ((availWidth + colGap) / desiredColumnCount) - colGap;
422 } else {
423 desiredColumnCount = std::max<LayoutUnit>(std::min<LayoutUnit>(colCount, (availWidth + colGap) / (colWidth + colGap)), 1);
424 desiredColumnWidth = ((availWidth + colGap) / desiredColumnCount) - colGap;
425 }
426 setComputedColumnCountAndWidth(desiredColumnCount, desiredColumnWidth);
427}
428
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000429void RenderBlockFlow::layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight)
430{
431 ASSERT(needsLayout());
432
433 if (!relayoutChildren && simplifiedLayout())
434 return;
435
436 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
437
hyatt@apple.com73715ca2014-05-06 21:35:52 +0000438 if (recomputeLogicalWidthAndColumnWidth())
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000439 relayoutChildren = true;
440
bjonesbe@adobe.comf9f10402014-02-20 19:40:28 +0000441 rebuildFloatingObjectSetFromIntrudingFloats();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000442
443 LayoutUnit previousHeight = logicalHeight();
444 // FIXME: should this start out as borderAndPaddingLogicalHeight() + scrollbarLogicalHeight(),
445 // for consistency with other render classes?
446 setLogicalHeight(0);
447
448 bool pageLogicalHeightChanged = false;
hyatt@apple.com73715ca2014-05-06 21:35:52 +0000449 checkForPaginationLogicalHeightChange(relayoutChildren, pageLogicalHeight, pageLogicalHeightChanged);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000450
akling@apple.com827be9c2013-10-29 02:58:43 +0000451 const RenderStyle& styleToUse = style();
hyatt@apple.com73715ca2014-05-06 21:35:52 +0000452 LayoutStateMaintainer statePusher(view(), *this, locationOffset(), hasTransform() || hasReflection() || styleToUse.isFlippedBlocksWritingMode(), pageLogicalHeight, pageLogicalHeightChanged);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000453
zoltan@webkit.org7d4f8cc2014-03-26 18:20:15 +0000454 preparePaginationBeforeBlockLayout(relayoutChildren);
abucur@adobe.com0e81bc72013-10-22 14:50:37 +0000455 if (!relayoutChildren)
456 relayoutChildren = namedFlowFragmentNeedsUpdate();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000457
458 // We use four values, maxTopPos, maxTopNeg, maxBottomPos, and maxBottomNeg, to track
459 // our current maximal positive and negative margins. These values are used when we
460 // are collapsed with adjacent blocks, so for example, if you have block A and B
461 // collapsing together, then you'd take the maximal positive margin from both A and B
462 // and subtract it from the maximal negative margin from both A and B to get the
463 // true collapsed margin. This algorithm is recursive, so when we finish layout()
464 // our block knows its current maximal positive/negative values.
465 //
466 // Start out by setting our margin values to our current margins. Table cells have
467 // no margins, so we don't fill in the values for table cells.
468 bool isCell = isTableCell();
469 if (!isCell) {
470 initMaxMarginValues();
471
akling@apple.com827be9c2013-10-29 02:58:43 +0000472 setHasMarginBeforeQuirk(styleToUse.hasMarginBeforeQuirk());
473 setHasMarginAfterQuirk(styleToUse.hasMarginAfterQuirk());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000474 setPaginationStrut(0);
475 }
476
477 LayoutUnit repaintLogicalTop = 0;
478 LayoutUnit repaintLogicalBottom = 0;
479 LayoutUnit maxFloatLogicalBottom = 0;
480 if (!firstChild() && !isAnonymousBlock())
481 setChildrenInline(true);
482 if (childrenInline())
483 layoutInlineChildren(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
484 else
485 layoutBlockChildren(relayoutChildren, maxFloatLogicalBottom);
486
487 // Expand our intrinsic height to encompass floats.
488 LayoutUnit toAdd = borderAndPaddingAfter() + scrollbarLogicalHeight();
489 if (lowestFloatLogicalBottom() > (logicalHeight() - toAdd) && expandsToEncloseOverhangingFloats())
490 setLogicalHeight(lowestFloatLogicalBottom() + toAdd);
491
hyatt@apple.com73715ca2014-05-06 21:35:52 +0000492 if (relayoutForPagination(statePusher) || relayoutToAvoidWidows(statePusher)) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000493 ASSERT(!shouldBreakAtLineToAvoidWidow());
494 return;
495 }
496
497 // Calculate our new height.
498 LayoutUnit oldHeight = logicalHeight();
499 LayoutUnit oldClientAfterEdge = clientLogicalBottom();
500
501 // Before updating the final size of the flow thread make sure a forced break is applied after the content.
502 // This ensures the size information is correctly computed for the last auto-height region receiving content.
503 if (isRenderFlowThread())
504 toRenderFlowThread(this)->applyBreakAfterContent(oldClientAfterEdge);
505
506 updateLogicalHeight();
507 LayoutUnit newHeight = logicalHeight();
508 if (oldHeight != newHeight) {
509 if (oldHeight > newHeight && maxFloatLogicalBottom > newHeight && !childrenInline()) {
510 // One of our children's floats may have become an overhanging float for us. We need to look for it.
akling@apple.com525dae62014-01-03 20:22:09 +0000511 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
512 if (blockFlow.isFloatingOrOutOfFlowPositioned())
513 continue;
514 if (blockFlow.lowestFloatLogicalBottom() + blockFlow.logicalTop() > newHeight)
515 addOverhangingFloats(blockFlow, false);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000516 }
517 }
518 }
519
520 bool heightChanged = (previousHeight != newHeight);
521 if (heightChanged)
522 relayoutChildren = true;
523
524 layoutPositionedObjects(relayoutChildren || isRoot());
525
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000526 // Add overflow from children (unless we're multi-column, since in that case all our child overflow is clipped anyway).
527 computeOverflow(oldClientAfterEdge);
528
529 statePusher.pop();
530
531 fitBorderToLinesIfNeeded();
532
533 if (view().layoutState()->m_pageLogicalHeight)
534 setPageLogicalOffset(view().layoutState()->pageLogicalOffset(this, logicalTop()));
535
536 updateLayerTransform();
537
538 // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
539 // we overflow or not.
540 updateScrollInfoAfterLayout();
541
542 // FIXME: This repaint logic should be moved into a separate helper function!
543 // Repaint with our new bounds if they are different from our old bounds.
544 bool didFullRepaint = repainter.repaintAfterLayout();
akling@apple.com827be9c2013-10-29 02:58:43 +0000545 if (!didFullRepaint && repaintLogicalTop != repaintLogicalBottom && (styleToUse.visibility() == VISIBLE || enclosingLayer()->hasVisibleContent())) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000546 // FIXME: We could tighten up the left and right invalidation points if we let layoutInlineChildren fill them in based off the particular lines
547 // it had to lay out. We wouldn't need the hasOverflowClip() hack in that case either.
548 LayoutUnit repaintLogicalLeft = logicalLeftVisualOverflow();
549 LayoutUnit repaintLogicalRight = logicalRightVisualOverflow();
550 if (hasOverflowClip()) {
551 // 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.
552 // 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.
553 // layoutInlineChildren should be patched to compute the entire repaint rect.
andersca@apple.com86298632013-11-10 19:32:33 +0000554 repaintLogicalLeft = std::min(repaintLogicalLeft, logicalLeftLayoutOverflow());
555 repaintLogicalRight = std::max(repaintLogicalRight, logicalRightLayoutOverflow());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000556 }
557
558 LayoutRect repaintRect;
559 if (isHorizontalWritingMode())
560 repaintRect = LayoutRect(repaintLogicalLeft, repaintLogicalTop, repaintLogicalRight - repaintLogicalLeft, repaintLogicalBottom - repaintLogicalTop);
561 else
562 repaintRect = LayoutRect(repaintLogicalTop, repaintLogicalLeft, repaintLogicalBottom - repaintLogicalTop, repaintLogicalRight - repaintLogicalLeft);
563
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000564 repaintRect.inflate(maximalOutlineSize(PaintPhaseOutline));
565
566 if (hasOverflowClip()) {
567 // Adjust repaint rect for scroll offset
568 repaintRect.move(-scrolledContentOffset());
569
570 // Don't allow this rect to spill out of our overflow box.
571 repaintRect.intersect(LayoutRect(LayoutPoint(), size()));
572 }
573
574 // Make sure the rect is still non-empty after intersecting for overflow above
575 if (!repaintRect.isEmpty()) {
576 repaintRectangle(repaintRect); // We need to do a partial repaint of our content.
577 if (hasReflection())
578 repaintRectangle(reflectedRect(repaintRect));
579 }
580 }
581
antti@apple.comca2a8ff2013-10-04 04:04:35 +0000582 clearNeedsLayout();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000583}
584
585void RenderBlockFlow::layoutBlockChildren(bool relayoutChildren, LayoutUnit& maxFloatLogicalBottom)
586{
587 dirtyForLayoutFromPercentageHeightDescendants();
588
589 LayoutUnit beforeEdge = borderAndPaddingBefore();
590 LayoutUnit afterEdge = borderAndPaddingAfter() + scrollbarLogicalHeight();
591
592 setLogicalHeight(beforeEdge);
593
594 // Lay out our hypothetical grid line as though it occurs at the top of the block.
595 if (view().layoutState()->lineGrid() == this)
596 layoutLineGridBox();
597
598 // The margin struct caches all our current margin collapsing state.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000599 MarginInfo marginInfo(*this, beforeEdge, afterEdge);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000600
601 // Fieldsets need to find their legend and position it inside the border of the object.
602 // The legend then gets skipped during normal layout. The same is true for ruby text.
603 // It doesn't get included in the normal layout process but is instead skipped.
604 RenderObject* childToExclude = layoutSpecialExcludedChild(relayoutChildren);
605
606 LayoutUnit previousFloatLogicalBottom = 0;
607 maxFloatLogicalBottom = 0;
608
609 RenderBox* next = firstChildBox();
610
611 while (next) {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000612 RenderBox& child = *next;
613 next = child.nextSiblingBox();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000614
weinig@apple.com12840dc2013-10-22 23:59:08 +0000615 if (childToExclude == &child)
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000616 continue; // Skip this child, since it will be positioned by the specialized subclass (fieldsets and ruby runs).
617
618 updateBlockChildDirtyBitsBeforeLayout(relayoutChildren, child);
619
weinig@apple.com12840dc2013-10-22 23:59:08 +0000620 if (child.isOutOfFlowPositioned()) {
621 child.containingBlock()->insertPositionedObject(child);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000622 adjustPositionedBlock(child, marginInfo);
623 continue;
624 }
weinig@apple.com12840dc2013-10-22 23:59:08 +0000625 if (child.isFloating()) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000626 insertFloatingObject(child);
627 adjustFloatingBlock(marginInfo);
628 continue;
629 }
630
631 // Lay out the child.
632 layoutBlockChild(child, marginInfo, previousFloatLogicalBottom, maxFloatLogicalBottom);
633 }
634
635 // Now do the handling of the bottom of the block, adding in our bottom border/padding and
636 // determining the correct collapsed bottom margin information.
637 handleAfterSideOfBlock(beforeEdge, afterEdge, marginInfo);
638}
639
antti@apple.com940f5872013-10-24 20:31:11 +0000640void RenderBlockFlow::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
641{
antti@apple.com42fb53d2013-10-25 02:33:11 +0000642 if (m_lineLayoutPath == UndeterminedPath)
643 m_lineLayoutPath = SimpleLineLayout::canUseFor(*this) ? SimpleLinesPath : LineBoxesPath;
644
645 if (m_lineLayoutPath == SimpleLinesPath) {
antti@apple.com940f5872013-10-24 20:31:11 +0000646 deleteLineBoxesBeforeSimpleLineLayout();
647 layoutSimpleLines(repaintLogicalTop, repaintLogicalBottom);
648 return;
649 }
650
antti@apple.comfea51992013-10-28 13:39:23 +0000651 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +0000652 layoutLineBoxes(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
653}
654
weinig@apple.com12840dc2013-10-22 23:59:08 +0000655void RenderBlockFlow::layoutBlockChild(RenderBox& child, MarginInfo& marginInfo, LayoutUnit& previousFloatLogicalBottom, LayoutUnit& maxFloatLogicalBottom)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000656{
657 LayoutUnit oldPosMarginBefore = maxPositiveMarginBefore();
658 LayoutUnit oldNegMarginBefore = maxNegativeMarginBefore();
659
660 // The child is a normal flow object. Compute the margins we will use for collapsing now.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000661 child.computeAndSetBlockDirectionMargins(this);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000662
663 // Try to guess our correct logical top position. In most cases this guess will
664 // be correct. Only if we're wrong (when we compute the real logical top position)
665 // will we have to potentially relayout.
666 LayoutUnit estimateWithoutPagination;
667 LayoutUnit logicalTopEstimate = estimateLogicalTopPosition(child, marginInfo, estimateWithoutPagination);
668
669 // Cache our old rect so that we can dirty the proper repaint rects if the child moves.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000670 LayoutRect oldRect = child.frameRect();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000671 LayoutUnit oldLogicalTop = logicalTopForChild(child);
672
673#if !ASSERT_DISABLED
674 LayoutSize oldLayoutDelta = view().layoutDelta();
675#endif
676 // Go ahead and position the child as though it didn't collapse with the top.
677 setLogicalTopForChild(child, logicalTopEstimate, ApplyLayoutDelta);
678 estimateRegionRangeForBoxChild(child);
679
weinig@apple.com12840dc2013-10-22 23:59:08 +0000680 RenderBlockFlow* childBlockFlow = child.isRenderBlockFlow() ? toRenderBlockFlow(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000681 bool markDescendantsWithFloats = false;
weinig@apple.com12840dc2013-10-22 23:59:08 +0000682 if (logicalTopEstimate != oldLogicalTop && !child.avoidsFloats() && childBlockFlow && childBlockFlow->containsFloats())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000683 markDescendantsWithFloats = true;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000684 else if (UNLIKELY(logicalTopEstimate.mightBeSaturated()))
685 // logicalTopEstimate, returned by estimateLogicalTopPosition, might be saturated for
686 // very large elements. If it does the comparison with oldLogicalTop might yield a
687 // false negative as adding and removing margins, borders etc from a saturated number
688 // might yield incorrect results. If this is the case always mark for layout.
689 markDescendantsWithFloats = true;
weinig@apple.com12840dc2013-10-22 23:59:08 +0000690 else if (!child.avoidsFloats() || child.shrinkToAvoidFloats()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000691 // If an element might be affected by the presence of floats, then always mark it for
692 // layout.
andersca@apple.com86298632013-11-10 19:32:33 +0000693 LayoutUnit fb = std::max(previousFloatLogicalBottom, lowestFloatLogicalBottom());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000694 if (fb > logicalTopEstimate)
695 markDescendantsWithFloats = true;
696 }
697
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000698 if (childBlockFlow) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000699 if (markDescendantsWithFloats)
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000700 childBlockFlow->markAllDescendantsWithFloatsForLayout();
weinig@apple.com12840dc2013-10-22 23:59:08 +0000701 if (!child.isWritingModeRoot())
andersca@apple.com86298632013-11-10 19:32:33 +0000702 previousFloatLogicalBottom = std::max(previousFloatLogicalBottom, oldLogicalTop + childBlockFlow->lowestFloatLogicalBottom());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000703 }
704
weinig@apple.com12840dc2013-10-22 23:59:08 +0000705 if (!child.needsLayout())
706 child.markForPaginationRelayoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000707
weinig@apple.com12840dc2013-10-22 23:59:08 +0000708 bool childHadLayout = child.everHadLayout();
709 bool childNeededLayout = child.needsLayout();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000710 if (childNeededLayout)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000711 child.layout();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000712
713 // Cache if we are at the top of the block right now.
714 bool atBeforeSideOfBlock = marginInfo.atBeforeSideOfBlock();
715
716 // Now determine the correct ypos based off examination of collapsing margin
717 // values.
718 LayoutUnit logicalTopBeforeClear = collapseMargins(child, marginInfo);
719
720 // Now check for clear.
721 LayoutUnit logicalTopAfterClear = clearFloatsIfNeeded(child, marginInfo, oldPosMarginBefore, oldNegMarginBefore, logicalTopBeforeClear);
722
723 bool paginated = view().layoutState()->isPaginated();
724 if (paginated)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000725 logicalTopAfterClear = adjustBlockChildForPagination(logicalTopAfterClear, estimateWithoutPagination, child, atBeforeSideOfBlock && logicalTopBeforeClear == logicalTopAfterClear);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000726
727 setLogicalTopForChild(child, logicalTopAfterClear, ApplyLayoutDelta);
728
729 // Now we have a final top position. See if it really does end up being different from our estimate.
730 // clearFloatsIfNeeded can also mark the child as needing a layout even though we didn't move. This happens
731 // when collapseMargins dynamically adds overhanging floats because of a child with negative margins.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000732 if (logicalTopAfterClear != logicalTopEstimate || child.needsLayout() || (paginated && childBlockFlow && childBlockFlow->shouldBreakAtLineToAvoidWidow())) {
733 if (child.shrinkToAvoidFloats()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000734 // The child's width depends on the line width.
735 // When the child shifts to clear an item, its width can
736 // change (because it has more available line width).
737 // So go ahead and mark the item as dirty.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000738 child.setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000739 }
740
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000741 if (childBlockFlow) {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000742 if (!child.avoidsFloats() && childBlockFlow->containsFloats())
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000743 childBlockFlow->markAllDescendantsWithFloatsForLayout();
weinig@apple.com12840dc2013-10-22 23:59:08 +0000744 if (!child.needsLayout())
745 child.markForPaginationRelayoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000746 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000747 }
748
abucur@adobe.comeaf5e222014-05-14 14:35:07 +0000749 if (updateRegionRangeForBoxChild(child))
weinig@apple.com12840dc2013-10-22 23:59:08 +0000750 child.setNeedsLayout(MarkOnlyThis);
abucur@adobe.comeaf5e222014-05-14 14:35:07 +0000751
752 // In case our guess was wrong, relayout the child.
753 child.layoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000754
755 // We are no longer at the top of the block if we encounter a non-empty child.
756 // This has to be done after checking for clear, so that margins can be reset if a clear occurred.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000757 if (marginInfo.atBeforeSideOfBlock() && !child.isSelfCollapsingBlock())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000758 marginInfo.setAtBeforeSideOfBlock(false);
759
760 // Now place the child in the correct left position
761 determineLogicalLeftPositionForChild(child, ApplyLayoutDelta);
762
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000763 // Update our height now that the child has been placed in the correct position.
stavila@adobe.comb0d86c42014-04-09 17:07:50 +0000764 setLogicalHeight(logicalHeight() + logicalHeightForChildForFragmentation(child));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000765 if (mustSeparateMarginAfterForChild(child)) {
766 setLogicalHeight(logicalHeight() + marginAfterForChild(child));
767 marginInfo.clearMargin();
768 }
769 // If the child has overhanging floats that intrude into following siblings (or possibly out
770 // of this block), then the parent gets notified of the floats now.
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000771 if (childBlockFlow && childBlockFlow->containsFloats())
andersca@apple.com86298632013-11-10 19:32:33 +0000772 maxFloatLogicalBottom = std::max(maxFloatLogicalBottom, addOverhangingFloats(*childBlockFlow, !childNeededLayout));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000773
zoltan@webkit.org7d4f8cc2014-03-26 18:20:15 +0000774 LayoutSize childOffset = child.location() - oldRect.location();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000775 if (childOffset.width() || childOffset.height()) {
776 view().addLayoutDelta(childOffset);
777
778 // If the child moved, we have to repaint it as well as any floating/positioned
779 // descendants. An exception is if we need a layout. In this case, we know we're going to
780 // repaint ourselves (and the child) anyway.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000781 if (childHadLayout && !selfNeedsLayout() && child.checkForRepaintDuringLayout())
782 child.repaintDuringLayoutIfMoved(oldRect);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000783 }
784
weinig@apple.com12840dc2013-10-22 23:59:08 +0000785 if (!childHadLayout && child.checkForRepaintDuringLayout()) {
786 child.repaint();
787 child.repaintOverhangingFloats(true);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000788 }
789
790 if (paginated) {
hyatt@apple.comc1c39032014-04-15 23:25:58 +0000791 if (RenderFlowThread* flowThread = flowThreadContainingBlock())
792 flowThread->flowThreadDescendantBoxLaidOut(&child);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000793 // Check for an after page/column break.
794 LayoutUnit newHeight = applyAfterBreak(child, logicalHeight(), marginInfo);
795 if (newHeight != height())
796 setLogicalHeight(newHeight);
797 }
798
799 ASSERT(view().layoutDeltaMatches(oldLayoutDelta));
800}
801
weinig@apple.com12840dc2013-10-22 23:59:08 +0000802void RenderBlockFlow::adjustPositionedBlock(RenderBox& child, const MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000803{
804 bool isHorizontal = isHorizontalWritingMode();
akling@apple.com827be9c2013-10-29 02:58:43 +0000805 bool hasStaticBlockPosition = child.style().hasStaticBlockPosition(isHorizontal);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000806
807 LayoutUnit logicalTop = logicalHeight();
808 updateStaticInlinePositionForChild(child, logicalTop);
809
810 if (!marginInfo.canCollapseWithMarginBefore()) {
811 // Positioned blocks don't collapse margins, so add the margin provided by
812 // the container now. The child's own margin is added later when calculating its logical top.
813 LayoutUnit collapsedBeforePos = marginInfo.positiveMargin();
814 LayoutUnit collapsedBeforeNeg = marginInfo.negativeMargin();
815 logicalTop += collapsedBeforePos - collapsedBeforeNeg;
816 }
817
weinig@apple.com12840dc2013-10-22 23:59:08 +0000818 RenderLayer* childLayer = child.layer();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000819 if (childLayer->staticBlockPosition() != logicalTop) {
820 childLayer->setStaticBlockPosition(logicalTop);
821 if (hasStaticBlockPosition)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000822 child.setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000823 }
824}
825
robert@webkit.org97037ef2013-11-20 19:26:10 +0000826LayoutUnit RenderBlockFlow::marginOffsetForSelfCollapsingBlock()
827{
828 ASSERT(isSelfCollapsingBlock());
829 RenderBlockFlow* parentBlock = toRenderBlockFlow(parent());
830 if (parentBlock && style().clear() && parentBlock->getClearDelta(*this, logicalHeight()))
831 return marginValuesForChild(*this).positiveMarginBefore();
832 return LayoutUnit();
833}
834
hyatt@apple.com31a5daa2014-01-28 01:26:37 +0000835void RenderBlockFlow::determineLogicalLeftPositionForChild(RenderBox& child, ApplyLayoutDeltaMode applyDelta)
836{
837 LayoutUnit startPosition = borderStart() + paddingStart();
838 if (style().shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
839 startPosition -= verticalScrollbarWidth();
840 LayoutUnit totalAvailableLogicalWidth = borderAndPaddingLogicalWidth() + availableLogicalWidth();
841
842 // Add in our start margin.
843 LayoutUnit childMarginStart = marginStartForChild(child);
844 LayoutUnit newPosition = startPosition + childMarginStart;
845
846 // Some objects (e.g., tables, horizontal rules, overflow:auto blocks) avoid floats. They need
847 // to shift over as necessary to dodge any floats that might get in the way.
848 if (child.avoidsFloats() && containsFloats() && !flowThreadContainingBlock())
849 newPosition += computeStartPositionDeltaForChildAvoidingFloats(child, marginStartForChild(child));
850
851 setLogicalLeftForChild(child, style().isLeftToRightDirection() ? newPosition : totalAvailableLogicalWidth - newPosition - logicalWidthForChild(child), applyDelta);
852}
robert@webkit.org97037ef2013-11-20 19:26:10 +0000853
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000854void RenderBlockFlow::adjustFloatingBlock(const MarginInfo& marginInfo)
855{
856 // The float should be positioned taking into account the bottom margin
857 // of the previous flow. We add that margin into the height, get the
858 // float positioned properly, and then subtract the margin out of the
859 // height again. In the case of self-collapsing blocks, we always just
860 // use the top margins, since the self-collapsing block collapsed its
861 // own bottom margin into its top margin.
862 //
863 // Note also that the previous flow may collapse its margin into the top of
864 // our block. If this is the case, then we do not add the margin in to our
865 // height when computing the position of the float. This condition can be tested
866 // for by simply calling canCollapseWithMarginBefore. See
867 // http://www.hixie.ch/tests/adhoc/css/box/block/margin-collapse/046.html for
868 // an example of this scenario.
869 LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
870 setLogicalHeight(logicalHeight() + marginOffset);
871 positionNewFloats();
872 setLogicalHeight(logicalHeight() - marginOffset);
873}
874
weinig@apple.com12840dc2013-10-22 23:59:08 +0000875void RenderBlockFlow::updateStaticInlinePositionForChild(RenderBox& child, LayoutUnit logicalTop)
876{
akling@apple.com827be9c2013-10-29 02:58:43 +0000877 if (child.style().isOriginalDisplayInlineType())
weinig@apple.com12840dc2013-10-22 23:59:08 +0000878 setStaticInlinePositionForChild(child, logicalTop, startAlignedOffsetForLine(logicalTop, false));
879 else
880 setStaticInlinePositionForChild(child, logicalTop, startOffsetForContent(logicalTop));
881}
882
883void RenderBlockFlow::setStaticInlinePositionForChild(RenderBox& child, LayoutUnit blockOffset, LayoutUnit inlinePosition)
884{
885 if (flowThreadContainingBlock()) {
886 // Shift the inline position to exclude the region offset.
887 inlinePosition += startOffsetForContent() - startOffsetForContent(blockOffset);
888 }
889 child.layer()->setStaticInlinePosition(inlinePosition);
890}
891
892RenderBlockFlow::MarginValues RenderBlockFlow::marginValuesForChild(RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000893{
894 LayoutUnit childBeforePositive = 0;
895 LayoutUnit childBeforeNegative = 0;
896 LayoutUnit childAfterPositive = 0;
897 LayoutUnit childAfterNegative = 0;
898
899 LayoutUnit beforeMargin = 0;
900 LayoutUnit afterMargin = 0;
901
weinig@apple.com12840dc2013-10-22 23:59:08 +0000902 RenderBlockFlow* childRenderBlock = child.isRenderBlockFlow() ? toRenderBlockFlow(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000903
904 // If the child has the same directionality as we do, then we can just return its
905 // margins in the same direction.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000906 if (!child.isWritingModeRoot()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000907 if (childRenderBlock) {
908 childBeforePositive = childRenderBlock->maxPositiveMarginBefore();
909 childBeforeNegative = childRenderBlock->maxNegativeMarginBefore();
910 childAfterPositive = childRenderBlock->maxPositiveMarginAfter();
911 childAfterNegative = childRenderBlock->maxNegativeMarginAfter();
912 } else {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000913 beforeMargin = child.marginBefore();
914 afterMargin = child.marginAfter();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000915 }
weinig@apple.com12840dc2013-10-22 23:59:08 +0000916 } else if (child.isHorizontalWritingMode() == isHorizontalWritingMode()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000917 // The child has a different directionality. If the child is parallel, then it's just
918 // flipped relative to us. We can use the margins for the opposite edges.
919 if (childRenderBlock) {
920 childBeforePositive = childRenderBlock->maxPositiveMarginAfter();
921 childBeforeNegative = childRenderBlock->maxNegativeMarginAfter();
922 childAfterPositive = childRenderBlock->maxPositiveMarginBefore();
923 childAfterNegative = childRenderBlock->maxNegativeMarginBefore();
924 } else {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000925 beforeMargin = child.marginAfter();
926 afterMargin = child.marginBefore();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000927 }
928 } else {
929 // The child is perpendicular to us, which means its margins don't collapse but are on the
930 // "logical left/right" sides of the child box. We can just return the raw margin in this case.
931 beforeMargin = marginBeforeForChild(child);
932 afterMargin = marginAfterForChild(child);
933 }
934
935 // Resolve uncollapsing margins into their positive/negative buckets.
936 if (beforeMargin) {
937 if (beforeMargin > 0)
938 childBeforePositive = beforeMargin;
939 else
940 childBeforeNegative = -beforeMargin;
941 }
942 if (afterMargin) {
943 if (afterMargin > 0)
944 childAfterPositive = afterMargin;
945 else
946 childAfterNegative = -afterMargin;
947 }
948
949 return MarginValues(childBeforePositive, childBeforeNegative, childAfterPositive, childAfterNegative);
950}
951
weinig@apple.com12840dc2013-10-22 23:59:08 +0000952LayoutUnit RenderBlockFlow::collapseMargins(RenderBox& child, MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000953{
954 bool childDiscardMarginBefore = mustDiscardMarginBeforeForChild(child);
955 bool childDiscardMarginAfter = mustDiscardMarginAfterForChild(child);
weinig@apple.com12840dc2013-10-22 23:59:08 +0000956 bool childIsSelfCollapsing = child.isSelfCollapsingBlock();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000957
958 // The child discards the before margin when the the after margin has discard in the case of a self collapsing block.
959 childDiscardMarginBefore = childDiscardMarginBefore || (childDiscardMarginAfter && childIsSelfCollapsing);
960
961 // Get the four margin values for the child and cache them.
962 const MarginValues childMargins = marginValuesForChild(child);
963
964 // Get our max pos and neg top margins.
965 LayoutUnit posTop = childMargins.positiveMarginBefore();
966 LayoutUnit negTop = childMargins.negativeMarginBefore();
967
968 // For self-collapsing blocks, collapse our bottom margins into our
969 // top to get new posTop and negTop values.
970 if (childIsSelfCollapsing) {
andersca@apple.com86298632013-11-10 19:32:33 +0000971 posTop = std::max(posTop, childMargins.positiveMarginAfter());
972 negTop = std::max(negTop, childMargins.negativeMarginAfter());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000973 }
974
975 // See if the top margin is quirky. We only care if this child has
976 // margins that will collapse with us.
977 bool topQuirk = hasMarginBeforeQuirk(child);
978
979 if (marginInfo.canCollapseWithMarginBefore()) {
980 if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
981 // This child is collapsing with the top of the
982 // block. If it has larger margin values, then we need to update
983 // our own maximal values.
984 if (!document().inQuirksMode() || !marginInfo.quirkContainer() || !topQuirk)
andersca@apple.com86298632013-11-10 19:32:33 +0000985 setMaxMarginBeforeValues(std::max(posTop, maxPositiveMarginBefore()), std::max(negTop, maxNegativeMarginBefore()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000986
987 // The minute any of the margins involved isn't a quirk, don't
988 // collapse it away, even if the margin is smaller (www.webreference.com
989 // has an example of this, a <dt> with 0.8em author-specified inside
990 // a <dl> inside a <td>.
991 if (!marginInfo.determinedMarginBeforeQuirk() && !topQuirk && (posTop - negTop)) {
992 setHasMarginBeforeQuirk(false);
993 marginInfo.setDeterminedMarginBeforeQuirk(true);
994 }
995
996 if (!marginInfo.determinedMarginBeforeQuirk() && topQuirk && !marginBefore())
997 // We have no top margin and our top child has a quirky margin.
998 // We will pick up this quirky margin and pass it through.
999 // This deals with the <td><div><p> case.
1000 // Don't do this for a block that split two inlines though. You do
1001 // still apply margins in this case.
1002 setHasMarginBeforeQuirk(true);
1003 } else
1004 // The before margin of the container will also discard all the margins it is collapsing with.
1005 setMustDiscardMarginBefore();
1006 }
1007
1008 // Once we find a child with discardMarginBefore all the margins collapsing with us must also discard.
1009 if (childDiscardMarginBefore) {
1010 marginInfo.setDiscardMargin(true);
1011 marginInfo.clearMargin();
1012 }
1013
1014 if (marginInfo.quirkContainer() && marginInfo.atBeforeSideOfBlock() && (posTop - negTop))
1015 marginInfo.setHasMarginBeforeQuirk(topQuirk);
1016
1017 LayoutUnit beforeCollapseLogicalTop = logicalHeight();
1018 LayoutUnit logicalTop = beforeCollapseLogicalTop;
robert@webkit.org97037ef2013-11-20 19:26:10 +00001019
1020 LayoutUnit clearanceForSelfCollapsingBlock;
1021 RenderObject* prev = child.previousSibling();
1022 // 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
1023 // 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
1024 // 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.
1025 if (!marginInfo.canCollapseWithMarginBefore() && prev && prev->isRenderBlockFlow() && toRenderBlockFlow(prev)->isSelfCollapsingBlock()) {
1026 clearanceForSelfCollapsingBlock = toRenderBlockFlow(prev)->marginOffsetForSelfCollapsingBlock();
1027 setLogicalHeight(logicalHeight() - clearanceForSelfCollapsingBlock);
1028 }
1029
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001030 if (childIsSelfCollapsing) {
1031 // 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.
1032 // Also, the child's top position equals the logical height of the container.
1033 if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
1034 // This child has no height. We need to compute our
1035 // position before we collapse the child's margins together,
1036 // so that we can get an accurate position for the zero-height block.
andersca@apple.com86298632013-11-10 19:32:33 +00001037 LayoutUnit collapsedBeforePos = std::max(marginInfo.positiveMargin(), childMargins.positiveMarginBefore());
1038 LayoutUnit collapsedBeforeNeg = std::max(marginInfo.negativeMargin(), childMargins.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001039 marginInfo.setMargin(collapsedBeforePos, collapsedBeforeNeg);
1040
1041 // Now collapse the child's margins together, which means examining our
1042 // bottom margin values as well.
1043 marginInfo.setPositiveMarginIfLarger(childMargins.positiveMarginAfter());
1044 marginInfo.setNegativeMarginIfLarger(childMargins.negativeMarginAfter());
1045
1046 if (!marginInfo.canCollapseWithMarginBefore())
1047 // We need to make sure that the position of the self-collapsing block
1048 // is correct, since it could have overflowing content
1049 // that needs to be positioned correctly (e.g., a block that
1050 // had a specified height of 0 but that actually had subcontent).
1051 logicalTop = logicalHeight() + collapsedBeforePos - collapsedBeforeNeg;
1052 }
1053 } else {
1054 if (mustSeparateMarginBeforeForChild(child)) {
1055 ASSERT(!marginInfo.discardMargin() || (marginInfo.discardMargin() && !marginInfo.margin()));
1056 // If we are at the before side of the block and we collapse, ignore the computed margin
1057 // and just add the child margin to the container height. This will correctly position
1058 // the child inside the container.
zalan@apple.coma4d00552014-01-25 00:21:59 +00001059 LayoutUnit separateMargin = !marginInfo.canCollapseWithMarginBefore() ? marginInfo.margin() : LayoutUnit::fromPixel(0);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001060 setLogicalHeight(logicalHeight() + separateMargin + marginBeforeForChild(child));
1061 logicalTop = logicalHeight();
1062 } else if (!marginInfo.discardMargin() && (!marginInfo.atBeforeSideOfBlock()
1063 || (!marginInfo.canCollapseMarginBeforeWithChildren()
1064 && (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginBeforeQuirk())))) {
1065 // We're collapsing with a previous sibling's margins and not
1066 // with the top of the block.
andersca@apple.com86298632013-11-10 19:32:33 +00001067 setLogicalHeight(logicalHeight() + std::max(marginInfo.positiveMargin(), posTop) - std::max(marginInfo.negativeMargin(), negTop));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001068 logicalTop = logicalHeight();
1069 }
1070
1071 marginInfo.setDiscardMargin(childDiscardMarginAfter);
1072
1073 if (!marginInfo.discardMargin()) {
1074 marginInfo.setPositiveMargin(childMargins.positiveMarginAfter());
1075 marginInfo.setNegativeMargin(childMargins.negativeMarginAfter());
1076 } else
1077 marginInfo.clearMargin();
1078
1079 if (marginInfo.margin())
1080 marginInfo.setHasMarginAfterQuirk(hasMarginAfterQuirk(child));
1081 }
1082
1083 // If margins would pull us past the top of the next page, then we need to pull back and pretend like the margins
1084 // collapsed into the page edge.
1085 LayoutState* layoutState = view().layoutState();
1086 if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTop > beforeCollapseLogicalTop
1087 && hasNextPage(beforeCollapseLogicalTop)) {
1088 LayoutUnit oldLogicalTop = logicalTop;
andersca@apple.com86298632013-11-10 19:32:33 +00001089 logicalTop = std::min(logicalTop, nextPageLogicalTop(beforeCollapseLogicalTop));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001090 setLogicalHeight(logicalHeight() + (logicalTop - oldLogicalTop));
1091 }
1092
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001093 if (prev && prev->isRenderBlockFlow() && !prev->isFloatingOrOutOfFlowPositioned()) {
robert@webkit.org97037ef2013-11-20 19:26:10 +00001094 // 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
1095 // any floats from the parent will now overhang.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001096 RenderBlockFlow& block = toRenderBlockFlow(*prev);
robert@webkit.org97037ef2013-11-20 19:26:10 +00001097 LayoutUnit oldLogicalHeight = logicalHeight();
1098 setLogicalHeight(logicalTop);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001099 if (block.containsFloats() && !block.avoidsFloats() && (block.logicalTop() + block.lowestFloatLogicalBottom()) > logicalTop)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001100 addOverhangingFloats(block, false);
robert@webkit.org97037ef2013-11-20 19:26:10 +00001101 setLogicalHeight(oldLogicalHeight);
1102
1103 // If |child|'s previous sibling is a self-collapsing block that cleared a float and margin collapsing resulted in |child| moving up
1104 // 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
1105 // floats in the parent that overhang |child|'s new logical top.
1106 bool logicalTopIntrudesIntoFloat = clearanceForSelfCollapsingBlock > 0 && logicalTop < beforeCollapseLogicalTop;
1107 if (logicalTopIntrudesIntoFloat && containsFloats() && !child.avoidsFloats() && lowestFloatLogicalBottom() > logicalTop)
1108 child.setNeedsLayout();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001109 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001110
1111 return logicalTop;
1112}
1113
weinig@apple.com12840dc2013-10-22 23:59:08 +00001114LayoutUnit RenderBlockFlow::clearFloatsIfNeeded(RenderBox& child, MarginInfo& marginInfo, LayoutUnit oldTopPosMargin, LayoutUnit oldTopNegMargin, LayoutUnit yPos)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001115{
1116 LayoutUnit heightIncrease = getClearDelta(child, yPos);
1117 if (!heightIncrease)
1118 return yPos;
1119
weinig@apple.com12840dc2013-10-22 23:59:08 +00001120 if (child.isSelfCollapsingBlock()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001121 bool childDiscardMargin = mustDiscardMarginBeforeForChild(child) || mustDiscardMarginAfterForChild(child);
1122
1123 // For self-collapsing blocks that clear, they can still collapse their
1124 // margins with following siblings. Reset the current margins to represent
1125 // the self-collapsing block's margins only.
1126 // If DISCARD is specified for -webkit-margin-collapse, reset the margin values.
robert@webkit.org97037ef2013-11-20 19:26:10 +00001127 MarginValues childMargins = marginValuesForChild(child);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001128 if (!childDiscardMargin) {
andersca@apple.com86298632013-11-10 19:32:33 +00001129 marginInfo.setPositiveMargin(std::max(childMargins.positiveMarginBefore(), childMargins.positiveMarginAfter()));
1130 marginInfo.setNegativeMargin(std::max(childMargins.negativeMarginBefore(), childMargins.negativeMarginAfter()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001131 } else
1132 marginInfo.clearMargin();
1133 marginInfo.setDiscardMargin(childDiscardMargin);
1134
1135 // CSS2.1 states:
1136 // "If the top and bottom margins of an element with clearance are adjoining, its margins collapse with
1137 // the adjoining margins of following siblings but that resulting margin does not collapse with the bottom margin of the parent block."
1138 // So the parent's bottom margin cannot collapse through this block or any subsequent self-collapsing blocks. Check subsequent siblings
1139 // for a block with height - if none is found then don't allow the margins to collapse with the parent.
1140 bool wouldCollapseMarginsWithParent = marginInfo.canCollapseMarginAfterWithChildren();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001141 for (RenderBox* curr = child.nextSiblingBox(); curr && wouldCollapseMarginsWithParent; curr = curr->nextSiblingBox()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001142 if (!curr->isFloatingOrOutOfFlowPositioned() && !curr->isSelfCollapsingBlock())
1143 wouldCollapseMarginsWithParent = false;
1144 }
1145 if (wouldCollapseMarginsWithParent)
1146 marginInfo.setCanCollapseMarginAfterWithChildren(false);
1147
robert@webkit.org97037ef2013-11-20 19:26:10 +00001148 // 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
1149 // its own at the correct vertical position. If subsequent siblings attempt to collapse with |child|'s margins in |collapseMargins| we will
1150 // 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
1151 // margins can collapse at the correct vertical position.
1152 // 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
1153 // (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],
1154 // i.e., clearance = [height of float] - margin-top".
1155 setLogicalHeight(child.logicalTop() + childMargins.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001156 } else
1157 // Increase our height by the amount we had to clear.
1158 setLogicalHeight(logicalHeight() + heightIncrease);
1159
1160 if (marginInfo.canCollapseWithMarginBefore()) {
1161 // We can no longer collapse with the top of the block since a clear
1162 // occurred. The empty blocks collapse into the cleared block.
1163 // FIXME: This isn't quite correct. Need clarification for what to do
1164 // if the height the cleared block is offset by is smaller than the
1165 // margins involved.
1166 setMaxMarginBeforeValues(oldTopPosMargin, oldTopNegMargin);
1167 marginInfo.setAtBeforeSideOfBlock(false);
1168
1169 // In case the child discarded the before margin of the block we need to reset the mustDiscardMarginBefore flag to the initial value.
akling@apple.com827be9c2013-10-29 02:58:43 +00001170 setMustDiscardMarginBefore(style().marginBeforeCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001171 }
1172
robert@webkit.org97037ef2013-11-20 19:26:10 +00001173 return yPos + heightIncrease;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001174}
1175
weinig@apple.com12840dc2013-10-22 23:59:08 +00001176void RenderBlockFlow::marginBeforeEstimateForChild(RenderBox& child, LayoutUnit& positiveMarginBefore, LayoutUnit& negativeMarginBefore, bool& discardMarginBefore) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001177{
1178 // Give up if in quirks mode and we're a body/table cell and the top margin of the child box is quirky.
1179 // Give up if the child specified -webkit-margin-collapse: separate that prevents collapsing.
1180 // FIXME: Use writing mode independent accessor for marginBeforeCollapse.
akling@apple.com827be9c2013-10-29 02:58:43 +00001181 if ((document().inQuirksMode() && hasMarginAfterQuirk(child) && (isTableCell() || isBody())) || child.style().marginBeforeCollapse() == MSEPARATE)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001182 return;
1183
1184 // The margins are discarded by a child that specified -webkit-margin-collapse: discard.
1185 // FIXME: Use writing mode independent accessor for marginBeforeCollapse.
akling@apple.com827be9c2013-10-29 02:58:43 +00001186 if (child.style().marginBeforeCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001187 positiveMarginBefore = 0;
1188 negativeMarginBefore = 0;
1189 discardMarginBefore = true;
1190 return;
1191 }
1192
1193 LayoutUnit beforeChildMargin = marginBeforeForChild(child);
andersca@apple.com86298632013-11-10 19:32:33 +00001194 positiveMarginBefore = std::max(positiveMarginBefore, beforeChildMargin);
1195 negativeMarginBefore = std::max(negativeMarginBefore, -beforeChildMargin);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001196
weinig@apple.com12840dc2013-10-22 23:59:08 +00001197 if (!child.isRenderBlockFlow())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001198 return;
1199
weinig@apple.com12840dc2013-10-22 23:59:08 +00001200 RenderBlockFlow& childBlock = toRenderBlockFlow(child);
1201 if (childBlock.childrenInline() || childBlock.isWritingModeRoot())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001202 return;
1203
weinig@apple.com12840dc2013-10-22 23:59:08 +00001204 MarginInfo childMarginInfo(childBlock, childBlock.borderAndPaddingBefore(), childBlock.borderAndPaddingAfter());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001205 if (!childMarginInfo.canCollapseMarginBeforeWithChildren())
1206 return;
1207
weinig@apple.com12840dc2013-10-22 23:59:08 +00001208 RenderBox* grandchildBox = childBlock.firstChildBox();
1209 for (; grandchildBox; grandchildBox = grandchildBox->nextSiblingBox()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001210 if (!grandchildBox->isFloatingOrOutOfFlowPositioned())
1211 break;
1212 }
1213
1214 // Give up if there is clearance on the box, since it probably won't collapse into us.
akling@apple.com827be9c2013-10-29 02:58:43 +00001215 if (!grandchildBox || grandchildBox->style().clear() != CNONE)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001216 return;
1217
1218 // Make sure to update the block margins now for the grandchild box so that we're looking at current values.
1219 if (grandchildBox->needsLayout()) {
1220 grandchildBox->computeAndSetBlockDirectionMargins(this);
1221 if (grandchildBox->isRenderBlock()) {
1222 RenderBlock* grandchildBlock = toRenderBlock(grandchildBox);
akling@apple.com827be9c2013-10-29 02:58:43 +00001223 grandchildBlock->setHasMarginBeforeQuirk(grandchildBox->style().hasMarginBeforeQuirk());
1224 grandchildBlock->setHasMarginAfterQuirk(grandchildBox->style().hasMarginAfterQuirk());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001225 }
1226 }
1227
1228 // Collapse the margin of the grandchild box with our own to produce an estimate.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001229 childBlock.marginBeforeEstimateForChild(*grandchildBox, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001230}
1231
weinig@apple.com12840dc2013-10-22 23:59:08 +00001232LayoutUnit RenderBlockFlow::estimateLogicalTopPosition(RenderBox& child, const MarginInfo& marginInfo, LayoutUnit& estimateWithoutPagination)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001233{
1234 // FIXME: We need to eliminate the estimation of vertical position, because when it's wrong we sometimes trigger a pathological
1235 // relayout if there are intruding floats.
1236 LayoutUnit logicalTopEstimate = logicalHeight();
1237 if (!marginInfo.canCollapseWithMarginBefore()) {
1238 LayoutUnit positiveMarginBefore = 0;
1239 LayoutUnit negativeMarginBefore = 0;
1240 bool discardMarginBefore = false;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001241 if (child.selfNeedsLayout()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001242 // Try to do a basic estimation of how the collapse is going to go.
1243 marginBeforeEstimateForChild(child, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
1244 } else {
1245 // Use the cached collapsed margin values from a previous layout. Most of the time they
1246 // will be right.
1247 MarginValues marginValues = marginValuesForChild(child);
andersca@apple.com86298632013-11-10 19:32:33 +00001248 positiveMarginBefore = std::max(positiveMarginBefore, marginValues.positiveMarginBefore());
1249 negativeMarginBefore = std::max(negativeMarginBefore, marginValues.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001250 discardMarginBefore = mustDiscardMarginBeforeForChild(child);
1251 }
1252
1253 // Collapse the result with our current margins.
1254 if (!discardMarginBefore)
andersca@apple.com86298632013-11-10 19:32:33 +00001255 logicalTopEstimate += std::max(marginInfo.positiveMargin(), positiveMarginBefore) - std::max(marginInfo.negativeMargin(), negativeMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001256 }
1257
1258 // Adjust logicalTopEstimate down to the next page if the margins are so large that we don't fit on the current
1259 // page.
1260 LayoutState* layoutState = view().layoutState();
1261 if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTopEstimate > logicalHeight()
1262 && hasNextPage(logicalHeight()))
andersca@apple.com86298632013-11-10 19:32:33 +00001263 logicalTopEstimate = std::min(logicalTopEstimate, nextPageLogicalTop(logicalHeight()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001264
1265 logicalTopEstimate += getClearDelta(child, logicalTopEstimate);
1266
1267 estimateWithoutPagination = logicalTopEstimate;
1268
1269 if (layoutState->isPaginated()) {
1270 // If the object has a page or column break value of "before", then we should shift to the top of the next page.
1271 logicalTopEstimate = applyBeforeBreak(child, logicalTopEstimate);
1272
1273 // For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
1274 logicalTopEstimate = adjustForUnsplittableChild(child, logicalTopEstimate);
1275
weinig@apple.com12840dc2013-10-22 23:59:08 +00001276 if (!child.selfNeedsLayout() && child.isRenderBlock())
1277 logicalTopEstimate += toRenderBlock(child).paginationStrut();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001278 }
1279
1280 return logicalTopEstimate;
1281}
1282
1283void RenderBlockFlow::setCollapsedBottomMargin(const MarginInfo& marginInfo)
1284{
1285 if (marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()) {
1286 // 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.
1287 // Don't update the max margin values because we won't need them anyway.
1288 if (marginInfo.discardMargin()) {
1289 setMustDiscardMarginAfter();
1290 return;
1291 }
1292
1293 // Update our max pos/neg bottom margins, since we collapsed our bottom margins
1294 // with our children.
andersca@apple.com86298632013-11-10 19:32:33 +00001295 setMaxMarginAfterValues(std::max(maxPositiveMarginAfter(), marginInfo.positiveMargin()), std::max(maxNegativeMarginAfter(), marginInfo.negativeMargin()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001296
1297 if (!marginInfo.hasMarginAfterQuirk())
1298 setHasMarginAfterQuirk(false);
1299
1300 if (marginInfo.hasMarginAfterQuirk() && !marginAfter())
1301 // We have no bottom margin and our last child has a quirky margin.
1302 // We will pick up this quirky margin and pass it through.
1303 // This deals with the <td><div><p> case.
1304 setHasMarginAfterQuirk(true);
1305 }
1306}
1307
1308void RenderBlockFlow::handleAfterSideOfBlock(LayoutUnit beforeSide, LayoutUnit afterSide, MarginInfo& marginInfo)
1309{
1310 marginInfo.setAtAfterSideOfBlock(true);
1311
robert@webkit.org97037ef2013-11-20 19:26:10 +00001312 // If our last child was a self-collapsing block with clearance then our logical height is flush with the
1313 // bottom edge of the float that the child clears. The correct vertical position for the margin-collapsing we want
1314 // to perform now is at the child's margin-top - so adjust our height to that position.
1315 RenderObject* lastBlock = lastChild();
1316 if (lastBlock && lastBlock->isRenderBlockFlow() && toRenderBlockFlow(lastBlock)->isSelfCollapsingBlock())
1317 setLogicalHeight(logicalHeight() - toRenderBlockFlow(lastBlock)->marginOffsetForSelfCollapsingBlock());
1318
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001319 // If we can't collapse with children then go ahead and add in the bottom margin.
1320 if (!marginInfo.discardMargin() && (!marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()
1321 && (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginAfterQuirk())))
1322 setLogicalHeight(logicalHeight() + marginInfo.margin());
1323
1324 // Now add in our bottom border/padding.
1325 setLogicalHeight(logicalHeight() + afterSide);
1326
1327 // Negative margins can cause our height to shrink below our minimal height (border/padding).
1328 // If this happens, ensure that the computed height is increased to the minimal height.
andersca@apple.com86298632013-11-10 19:32:33 +00001329 setLogicalHeight(std::max(logicalHeight(), beforeSide + afterSide));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001330
1331 // Update our bottom collapsed margin info.
1332 setCollapsedBottomMargin(marginInfo);
1333}
1334
1335void RenderBlockFlow::setMaxMarginBeforeValues(LayoutUnit pos, LayoutUnit neg)
1336{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001337 if (!hasRareBlockFlowData()) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001338 if (pos == RenderBlockFlowRareData::positiveMarginBeforeDefault(*this) && neg == RenderBlockFlowRareData::negativeMarginBeforeDefault(*this))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001339 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001340 materializeRareBlockFlowData();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001341 }
weinig@apple.com924a77a2013-11-11 00:22:38 +00001342
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001343 rareBlockFlowData()->m_margins.setPositiveMarginBefore(pos);
1344 rareBlockFlowData()->m_margins.setNegativeMarginBefore(neg);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001345}
1346
1347void RenderBlockFlow::setMaxMarginAfterValues(LayoutUnit pos, LayoutUnit neg)
1348{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001349 if (!hasRareBlockFlowData()) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001350 if (pos == RenderBlockFlowRareData::positiveMarginAfterDefault(*this) && neg == RenderBlockFlowRareData::negativeMarginAfterDefault(*this))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001351 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001352 materializeRareBlockFlowData();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001353 }
weinig@apple.com924a77a2013-11-11 00:22:38 +00001354
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001355 rareBlockFlowData()->m_margins.setPositiveMarginAfter(pos);
1356 rareBlockFlowData()->m_margins.setNegativeMarginAfter(neg);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001357}
1358
1359void RenderBlockFlow::setMustDiscardMarginBefore(bool value)
1360{
akling@apple.com827be9c2013-10-29 02:58:43 +00001361 if (style().marginBeforeCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001362 ASSERT(value);
1363 return;
1364 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001365
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001366 if (!hasRareBlockFlowData()) {
weinig@apple.com924a77a2013-11-11 00:22:38 +00001367 if (!value)
1368 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001369 materializeRareBlockFlowData();
weinig@apple.com924a77a2013-11-11 00:22:38 +00001370 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001371
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001372 rareBlockFlowData()->m_discardMarginBefore = value;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001373}
1374
1375void RenderBlockFlow::setMustDiscardMarginAfter(bool value)
1376{
akling@apple.com827be9c2013-10-29 02:58:43 +00001377 if (style().marginAfterCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001378 ASSERT(value);
1379 return;
1380 }
1381
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001382 if (!hasRareBlockFlowData()) {
weinig@apple.com924a77a2013-11-11 00:22:38 +00001383 if (!value)
1384 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001385 materializeRareBlockFlowData();
weinig@apple.com924a77a2013-11-11 00:22:38 +00001386 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001387
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001388 rareBlockFlowData()->m_discardMarginAfter = value;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001389}
1390
1391bool RenderBlockFlow::mustDiscardMarginBefore() const
1392{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001393 return style().marginBeforeCollapse() == MDISCARD || (hasRareBlockFlowData() && rareBlockFlowData()->m_discardMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001394}
1395
1396bool RenderBlockFlow::mustDiscardMarginAfter() const
1397{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001398 return style().marginAfterCollapse() == MDISCARD || (hasRareBlockFlowData() && rareBlockFlowData()->m_discardMarginAfter);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001399}
1400
weinig@apple.com12840dc2013-10-22 23:59:08 +00001401bool RenderBlockFlow::mustDiscardMarginBeforeForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001402{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001403 ASSERT(!child.selfNeedsLayout());
1404 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001405 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginBefore() : (child.style().marginBeforeCollapse() == MDISCARD);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001406 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001407 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginAfter() : (child.style().marginAfterCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001408
1409 // FIXME: We return false here because the implementation is not geometrically complete. We have values only for before/after, not start/end.
1410 // In case the boxes are perpendicular we assume the property is not specified.
1411 return false;
1412}
1413
weinig@apple.com12840dc2013-10-22 23:59:08 +00001414bool RenderBlockFlow::mustDiscardMarginAfterForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001415{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001416 ASSERT(!child.selfNeedsLayout());
1417 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001418 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginAfter() : (child.style().marginAfterCollapse() == MDISCARD);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001419 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001420 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginBefore() : (child.style().marginBeforeCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001421
1422 // FIXME: See |mustDiscardMarginBeforeForChild| above.
1423 return false;
1424}
1425
weinig@apple.com12840dc2013-10-22 23:59:08 +00001426bool RenderBlockFlow::mustSeparateMarginBeforeForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001427{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001428 ASSERT(!child.selfNeedsLayout());
akling@apple.com827be9c2013-10-29 02:58:43 +00001429 const RenderStyle& childStyle = child.style();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001430 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001431 return childStyle.marginBeforeCollapse() == MSEPARATE;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001432 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001433 return childStyle.marginAfterCollapse() == MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001434
1435 // FIXME: See |mustDiscardMarginBeforeForChild| above.
1436 return false;
1437}
1438
weinig@apple.com12840dc2013-10-22 23:59:08 +00001439bool RenderBlockFlow::mustSeparateMarginAfterForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001440{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001441 ASSERT(!child.selfNeedsLayout());
akling@apple.com827be9c2013-10-29 02:58:43 +00001442 const RenderStyle& childStyle = child.style();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001443 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001444 return childStyle.marginAfterCollapse() == MSEPARATE;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001445 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001446 return childStyle.marginBeforeCollapse() == MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001447
1448 // FIXME: See |mustDiscardMarginBeforeForChild| above.
1449 return false;
1450}
1451
weinig@apple.com12840dc2013-10-22 23:59:08 +00001452static bool inNormalFlow(RenderBox& child)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001453{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001454 RenderBlock* curr = child.containingBlock();
1455 while (curr && curr != &child.view()) {
hyatt@apple.com73715ca2014-05-06 21:35:52 +00001456 if (curr->isRenderFlowThread())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001457 return true;
1458 if (curr->isFloatingOrOutOfFlowPositioned())
1459 return false;
1460 curr = curr->containingBlock();
1461 }
1462 return true;
1463}
1464
weinig@apple.com12840dc2013-10-22 23:59:08 +00001465LayoutUnit RenderBlockFlow::applyBeforeBreak(RenderBox& child, LayoutUnit logicalOffset)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001466{
1467 // FIXME: Add page break checking here when we support printing.
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001468 RenderFlowThread* flowThread = flowThreadContainingBlock();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001469 bool isInsideMulticolFlowThread = flowThread && !flowThread->isRenderNamedFlowThread();
hyatt@apple.com73715ca2014-05-06 21:35:52 +00001470 bool checkColumnBreaks = flowThread && flowThread->shouldCheckColumnBreaks();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001471 bool checkPageBreaks = !checkColumnBreaks && view().layoutState()->m_pageLogicalHeight; // FIXME: Once columns can print we have to check this.
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001472 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001473 bool checkBeforeAlways = (checkColumnBreaks && child.style().columnBreakBefore() == PBALWAYS)
1474 || (checkPageBreaks && child.style().pageBreakBefore() == PBALWAYS)
akling@apple.com827be9c2013-10-29 02:58:43 +00001475 || (checkRegionBreaks && child.style().regionBreakBefore() == PBALWAYS);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001476 if (checkBeforeAlways && inNormalFlow(child) && hasNextPage(logicalOffset, IncludePageBoundary)) {
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001477 if (checkColumnBreaks) {
1478 if (isInsideMulticolFlowThread)
1479 checkRegionBreaks = true;
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001480 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001481 if (checkRegionBreaks) {
1482 LayoutUnit offsetBreakAdjustment = 0;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001483 if (flowThread->addForcedRegionBreak(this, offsetFromLogicalTopOfFirstPage() + logicalOffset, &child, true, &offsetBreakAdjustment))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001484 return logicalOffset + offsetBreakAdjustment;
1485 }
1486 return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
1487 }
1488 return logicalOffset;
1489}
1490
weinig@apple.com12840dc2013-10-22 23:59:08 +00001491LayoutUnit RenderBlockFlow::applyAfterBreak(RenderBox& child, LayoutUnit logicalOffset, MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001492{
1493 // FIXME: Add page break checking here when we support printing.
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001494 RenderFlowThread* flowThread = flowThreadContainingBlock();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001495 bool isInsideMulticolFlowThread = flowThread && !flowThread->isRenderNamedFlowThread();
hyatt@apple.com73715ca2014-05-06 21:35:52 +00001496 bool checkColumnBreaks = flowThread && flowThread->shouldCheckColumnBreaks();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001497 bool checkPageBreaks = !checkColumnBreaks && view().layoutState()->m_pageLogicalHeight; // FIXME: Once columns can print we have to check this.
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001498 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001499 bool checkAfterAlways = (checkColumnBreaks && child.style().columnBreakAfter() == PBALWAYS)
1500 || (checkPageBreaks && child.style().pageBreakAfter() == PBALWAYS)
akling@apple.com827be9c2013-10-29 02:58:43 +00001501 || (checkRegionBreaks && child.style().regionBreakAfter() == PBALWAYS);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001502 if (checkAfterAlways && inNormalFlow(child) && hasNextPage(logicalOffset, IncludePageBoundary)) {
1503 LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
1504
1505 // So our margin doesn't participate in the next collapsing steps.
1506 marginInfo.clearMargin();
1507
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001508 if (checkColumnBreaks) {
1509 if (isInsideMulticolFlowThread)
1510 checkRegionBreaks = true;
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001511 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001512 if (checkRegionBreaks) {
1513 LayoutUnit offsetBreakAdjustment = 0;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001514 if (flowThread->addForcedRegionBreak(this, offsetFromLogicalTopOfFirstPage() + logicalOffset + marginOffset, &child, false, &offsetBreakAdjustment))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001515 return logicalOffset + marginOffset + offsetBreakAdjustment;
1516 }
1517 return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
1518 }
1519 return logicalOffset;
1520}
1521
weinig@apple.com12840dc2013-10-22 23:59:08 +00001522LayoutUnit RenderBlockFlow::adjustBlockChildForPagination(LayoutUnit logicalTopAfterClear, LayoutUnit estimateWithoutPagination, RenderBox& child, bool atBeforeSideOfBlock)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001523{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001524 RenderBlock* childRenderBlock = child.isRenderBlock() ? toRenderBlock(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001525
1526 if (estimateWithoutPagination != logicalTopAfterClear) {
1527 // Our guess prior to pagination movement was wrong. Before we attempt to paginate, let's try again at the new
1528 // position.
1529 setLogicalHeight(logicalTopAfterClear);
1530 setLogicalTopForChild(child, logicalTopAfterClear, ApplyLayoutDelta);
1531
weinig@apple.com12840dc2013-10-22 23:59:08 +00001532 if (child.shrinkToAvoidFloats()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001533 // The child's width depends on the line width.
1534 // When the child shifts to clear an item, its width can
1535 // change (because it has more available line width).
1536 // So go ahead and mark the item as dirty.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001537 child.setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001538 }
1539
1540 if (childRenderBlock) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001541 if (!child.avoidsFloats() && childRenderBlock->containsFloats())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001542 toRenderBlockFlow(childRenderBlock)->markAllDescendantsWithFloatsForLayout();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001543 if (!child.needsLayout())
1544 child.markForPaginationRelayoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001545 }
1546
1547 // Our guess was wrong. Make the child lay itself out again.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001548 child.layoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001549 }
1550
1551 LayoutUnit oldTop = logicalTopAfterClear;
1552
1553 // If the object has a page or column break value of "before", then we should shift to the top of the next page.
1554 LayoutUnit result = applyBeforeBreak(child, logicalTopAfterClear);
1555
1556 if (pageLogicalHeightForOffset(result)) {
1557 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(result, ExcludePageBoundary);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001558 LayoutUnit spaceShortage = child.logicalHeight() - remainingLogicalHeight;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001559 if (spaceShortage > 0) {
1560 // If the child crosses a column boundary, report a break, in case nothing inside it has already
1561 // done so. The column balancer needs to know how much it has to stretch the columns to make more
1562 // content fit. If no breaks are reported (but do occur), the balancer will have no clue. FIXME:
1563 // This should be improved, though, because here we just pretend that the child is
1564 // unsplittable. A splittable child, on the other hand, has break opportunities at every position
1565 // where there's no child content, border or padding. In other words, we risk stretching more
1566 // than necessary.
1567 setPageBreak(result, spaceShortage);
1568 }
1569 }
1570
1571 // For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
1572 LayoutUnit logicalTopBeforeUnsplittableAdjustment = result;
1573 LayoutUnit logicalTopAfterUnsplittableAdjustment = adjustForUnsplittableChild(child, result);
1574
1575 LayoutUnit paginationStrut = 0;
1576 LayoutUnit unsplittableAdjustmentDelta = logicalTopAfterUnsplittableAdjustment - logicalTopBeforeUnsplittableAdjustment;
1577 if (unsplittableAdjustmentDelta)
1578 paginationStrut = unsplittableAdjustmentDelta;
1579 else if (childRenderBlock && childRenderBlock->paginationStrut())
1580 paginationStrut = childRenderBlock->paginationStrut();
1581
1582 if (paginationStrut) {
1583 // We are willing to propagate out to our parent block as long as we were at the top of the block prior
1584 // to collapsing our margins, and as long as we didn't clear or move as a result of other pagination.
1585 if (atBeforeSideOfBlock && oldTop == result && !isOutOfFlowPositioned() && !isTableCell()) {
1586 // FIXME: Should really check if we're exceeding the page height before propagating the strut, but we don't
1587 // have all the information to do so (the strut only has the remaining amount to push). Gecko gets this wrong too
1588 // and pushes to the next page anyway, so not too concerned about it.
1589 setPaginationStrut(result + paginationStrut);
1590 if (childRenderBlock)
1591 childRenderBlock->setPaginationStrut(0);
1592 } else
1593 result += paginationStrut;
1594 }
1595
1596 // Similar to how we apply clearance. Go ahead and boost height() to be the place where we're going to position the child.
1597 setLogicalHeight(logicalHeight() + (result - oldTop));
1598
1599 // Return the final adjusted logical top.
1600 return result;
1601}
1602
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001603static inline LayoutUnit calculateMinimumPageHeight(RenderStyle* renderStyle, RootInlineBox* lastLine, LayoutUnit lineTop, LayoutUnit lineBottom)
1604{
1605 // We may require a certain minimum number of lines per page in order to satisfy
1606 // orphans and widows, and that may affect the minimum page height.
andersca@apple.com86298632013-11-10 19:32:33 +00001607 unsigned lineCount = std::max<unsigned>(renderStyle->hasAutoOrphans() ? 1 : renderStyle->orphans(), renderStyle->hasAutoWidows() ? 1 : renderStyle->widows());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001608 if (lineCount > 1) {
1609 RootInlineBox* line = lastLine;
1610 for (unsigned i = 1; i < lineCount && line->prevRootBox(); i++)
1611 line = line->prevRootBox();
1612
1613 // FIXME: Paginating using line overflow isn't all fine. See FIXME in
1614 // adjustLinePositionForPagination() for more details.
1615 LayoutRect overflow = line->logicalVisualOverflowRect(line->lineTop(), line->lineBottom());
andersca@apple.com86298632013-11-10 19:32:33 +00001616 lineTop = std::min(line->lineTopWithLeading(), overflow.y());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001617 }
1618 return lineBottom - lineTop;
1619}
1620
stavila@adobe.come1efa7f2014-05-20 14:34:56 +00001621void RenderBlockFlow::adjustLinePositionForPagination(RootInlineBox* lineBox, LayoutUnit& delta, bool& overflowsRegion, RenderFlowThread* flowThread)
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001622{
1623 // FIXME: For now we paginate using line overflow. This ensures that lines don't overlap at all when we
1624 // put a strut between them for pagination purposes. However, this really isn't the desired rendering, since
1625 // the line on the top of the next page will appear too far down relative to the same kind of line at the top
1626 // of the first column.
1627 //
1628 // The rendering we would like to see is one where the lineTopWithLeading is at the top of the column, and any line overflow
1629 // simply spills out above the top of the column. This effect would match what happens at the top of the first column.
1630 // We can't achieve this rendering, however, until we stop columns from clipping to the column bounds (thus allowing
1631 // for overflow to occur), and then cache visible overflow for each column rect.
1632 //
1633 // Furthermore, the paint we have to do when a column has overflow has to be special. We need to exclude
1634 // content that paints in a previous column (and content that paints in the following column).
1635 //
1636 // For now we'll at least honor the lineTopWithLeading when paginating if it is above the logical top overflow. This will
1637 // at least make positive leading work in typical cases.
1638 //
1639 // FIXME: Another problem with simply moving lines is that the available line width may change (because of floats).
1640 // Technically if the location we move the line to has a different line width than our old position, then we need to dirty the
1641 // line and all following lines.
stavila@adobe.come1efa7f2014-05-20 14:34:56 +00001642 overflowsRegion = false;
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001643 LayoutRect logicalVisualOverflow = lineBox->logicalVisualOverflowRect(lineBox->lineTop(), lineBox->lineBottom());
andersca@apple.com86298632013-11-10 19:32:33 +00001644 LayoutUnit logicalOffset = std::min(lineBox->lineTopWithLeading(), logicalVisualOverflow.y());
1645 LayoutUnit logicalBottom = std::max(lineBox->lineBottomWithLeading(), logicalVisualOverflow.maxY());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001646 LayoutUnit lineHeight = logicalBottom - logicalOffset;
akling@apple.com827be9c2013-10-29 02:58:43 +00001647 updateMinimumPageHeight(logicalOffset, calculateMinimumPageHeight(&style(), lineBox, logicalOffset, logicalBottom));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001648 logicalOffset += delta;
1649 lineBox->setPaginationStrut(0);
1650 lineBox->setIsFirstAfterPageBreak(false);
1651 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1652 bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
1653 // If lineHeight is greater than pageLogicalHeight, but logicalVisualOverflow.height() still fits, we are
1654 // still going to add a strut, so that the visible overflow fits on a single page.
1655 if (!pageLogicalHeight || (hasUniformPageLogicalHeight && logicalVisualOverflow.height() > pageLogicalHeight)
1656 || !hasNextPage(logicalOffset))
abucur@adobe.comd40287b2013-10-08 17:33:05 +00001657 // 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.
1658 // 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.
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001659 return;
1660 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
stavila@adobe.come1efa7f2014-05-20 14:34:56 +00001661 overflowsRegion = (lineHeight > remainingLogicalHeight);
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001662
1663 int lineIndex = lineCount(lineBox);
1664 if (remainingLogicalHeight < lineHeight || (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex)) {
abucur@adobe.comfc497132013-10-04 08:49:21 +00001665 if (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex) {
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001666 clearShouldBreakAtLineToAvoidWidow();
abucur@adobe.comfc497132013-10-04 08:49:21 +00001667 setDidBreakAtLineToAvoidWidow();
1668 }
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001669 // If we have a non-uniform page height, then we have to shift further possibly.
1670 if (!hasUniformPageLogicalHeight && !pushToNextPageWithMinimumLogicalHeight(remainingLogicalHeight, logicalOffset, lineHeight))
1671 return;
1672 if (lineHeight > pageLogicalHeight) {
1673 // Split the top margin in order to avoid splitting the visible part of the line.
andersca@apple.com86298632013-11-10 19:32:33 +00001674 remainingLogicalHeight -= std::min(lineHeight - pageLogicalHeight, std::max<LayoutUnit>(0, logicalVisualOverflow.y() - lineBox->lineTopWithLeading()));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001675 }
stavila@adobe.come1efa7f2014-05-20 14:34:56 +00001676 LayoutUnit remainingLogicalHeightAtNewOffset = pageRemainingLogicalHeightForOffset(logicalOffset + remainingLogicalHeight, ExcludePageBoundary);
1677 overflowsRegion = (lineHeight > remainingLogicalHeightAtNewOffset);
andersca@apple.com86298632013-11-10 19:32:33 +00001678 LayoutUnit totalLogicalHeight = lineHeight + std::max<LayoutUnit>(0, logicalOffset);
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001679 LayoutUnit pageLogicalHeightAtNewOffset = hasUniformPageLogicalHeight ? pageLogicalHeight : pageLogicalHeightForOffset(logicalOffset + remainingLogicalHeight);
1680 setPageBreak(logicalOffset, lineHeight - remainingLogicalHeight);
akling@apple.com827be9c2013-10-29 02:58:43 +00001681 if (((lineBox == firstRootBox() && totalLogicalHeight < pageLogicalHeightAtNewOffset) || (!style().hasAutoOrphans() && style().orphans() >= lineIndex))
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001682 && !isOutOfFlowPositioned() && !isTableCell())
andersca@apple.com86298632013-11-10 19:32:33 +00001683 setPaginationStrut(remainingLogicalHeight + std::max<LayoutUnit>(0, logicalOffset));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001684 else {
1685 delta += remainingLogicalHeight;
1686 lineBox->setPaginationStrut(remainingLogicalHeight);
1687 lineBox->setIsFirstAfterPageBreak(true);
1688 }
commit-queue@webkit.org883b01c2014-01-20 08:58:36 +00001689 } else if (remainingLogicalHeight == pageLogicalHeight) {
1690 // We're at the very top of a page or column.
1691 if (lineBox != firstRootBox())
1692 lineBox->setIsFirstAfterPageBreak(true);
1693 if (lineBox != firstRootBox() || offsetFromLogicalTopOfFirstPage())
1694 setPageBreak(logicalOffset, lineHeight);
1695 }
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001696}
1697
1698void RenderBlockFlow::setBreakAtLineToAvoidWidow(int lineToBreak)
1699{
abucur@adobe.comfc497132013-10-04 08:49:21 +00001700 ASSERT(lineToBreak >= 0);
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001701 ASSERT(!ensureRareBlockFlowData().m_didBreakAtLineToAvoidWidow);
1702 ensureRareBlockFlowData().m_lineBreakToAvoidWidow = lineToBreak;
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001703}
1704
abucur@adobe.comfc497132013-10-04 08:49:21 +00001705void RenderBlockFlow::setDidBreakAtLineToAvoidWidow()
1706{
1707 ASSERT(!shouldBreakAtLineToAvoidWidow());
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001708 if (!hasRareBlockFlowData())
abucur@adobe.comfc497132013-10-04 08:49:21 +00001709 return;
1710
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001711 rareBlockFlowData()->m_didBreakAtLineToAvoidWidow = true;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001712}
1713
1714void RenderBlockFlow::clearDidBreakAtLineToAvoidWidow()
1715{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001716 if (!hasRareBlockFlowData())
abucur@adobe.comfc497132013-10-04 08:49:21 +00001717 return;
1718
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001719 rareBlockFlowData()->m_didBreakAtLineToAvoidWidow = false;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001720}
1721
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001722void RenderBlockFlow::clearShouldBreakAtLineToAvoidWidow() const
1723{
abucur@adobe.comfc497132013-10-04 08:49:21 +00001724 ASSERT(shouldBreakAtLineToAvoidWidow());
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001725 if (!hasRareBlockFlowData())
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001726 return;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001727
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001728 rareBlockFlowData()->m_lineBreakToAvoidWidow = -1;
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001729}
1730
1731bool RenderBlockFlow::relayoutToAvoidWidows(LayoutStateMaintainer& statePusher)
1732{
1733 if (!shouldBreakAtLineToAvoidWidow())
1734 return false;
1735
1736 statePusher.pop();
1737 setEverHadLayout(true);
1738 layoutBlock(false);
1739 return true;
1740}
1741
weinig@apple.com31324fd2013-10-28 19:22:51 +00001742bool RenderBlockFlow::hasNextPage(LayoutUnit logicalOffset, PageBoundaryRule pageBoundaryRule) const
1743{
1744 ASSERT(view().layoutState() && view().layoutState()->isPaginated());
1745
1746 RenderFlowThread* flowThread = flowThreadContainingBlock();
1747 if (!flowThread)
1748 return true; // Printing and multi-column both make new pages to accommodate content.
1749
1750 // See if we're in the last region.
1751 LayoutUnit pageOffset = offsetFromLogicalTopOfFirstPage() + logicalOffset;
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00001752 RenderRegion* region = flowThread->regionAtBlockOffset(this, pageOffset, true);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001753 if (!region)
1754 return false;
mihnea@adobe.comc191b0a2014-03-19 12:38:51 +00001755
weinig@apple.com31324fd2013-10-28 19:22:51 +00001756 if (region->isLastRegion())
akling@apple.com827be9c2013-10-29 02:58:43 +00001757 return region->isRenderRegionSet() || region->style().regionFragment() == BreakRegionFragment
weinig@apple.com31324fd2013-10-28 19:22:51 +00001758 || (pageBoundaryRule == IncludePageBoundary && pageOffset == region->logicalTopForFlowThreadContent());
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00001759
mihnea@adobe.comc191b0a2014-03-19 12:38:51 +00001760 RenderRegion* startRegion = nullptr;
1761 RenderRegion* endRegion = nullptr;
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00001762 flowThread->getRegionRangeForBox(this, startRegion, endRegion);
stavila@adobe.come1efa7f2014-05-20 14:34:56 +00001763 return (endRegion && region != endRegion);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001764}
1765
1766LayoutUnit RenderBlockFlow::adjustForUnsplittableChild(RenderBox& child, LayoutUnit logicalOffset, bool includeMargins)
1767{
abucur@adobe.com4cddad82014-03-19 06:57:17 +00001768 if (!childBoxIsUnsplittableForFragmentation(child))
weinig@apple.com31324fd2013-10-28 19:22:51 +00001769 return logicalOffset;
abucur@adobe.com4cddad82014-03-19 06:57:17 +00001770
1771 RenderFlowThread* flowThread = flowThreadContainingBlock();
weinig@apple.com31324fd2013-10-28 19:22:51 +00001772 LayoutUnit childLogicalHeight = logicalHeightForChild(child) + (includeMargins ? marginBeforeForChild(child) + marginAfterForChild(child) : LayoutUnit());
1773 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1774 bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
1775 updateMinimumPageHeight(logicalOffset, childLogicalHeight);
1776 if (!pageLogicalHeight || (hasUniformPageLogicalHeight && childLogicalHeight > pageLogicalHeight)
1777 || !hasNextPage(logicalOffset))
1778 return logicalOffset;
1779 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
1780 if (remainingLogicalHeight < childLogicalHeight) {
1781 if (!hasUniformPageLogicalHeight && !pushToNextPageWithMinimumLogicalHeight(remainingLogicalHeight, logicalOffset, childLogicalHeight))
1782 return logicalOffset;
1783 return logicalOffset + remainingLogicalHeight;
1784 }
1785 return logicalOffset;
1786}
1787
1788bool RenderBlockFlow::pushToNextPageWithMinimumLogicalHeight(LayoutUnit& adjustment, LayoutUnit logicalOffset, LayoutUnit minimumLogicalHeight) const
1789{
1790 bool checkRegion = false;
1791 for (LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset + adjustment); pageLogicalHeight;
1792 pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset + adjustment)) {
1793 if (minimumLogicalHeight <= pageLogicalHeight)
1794 return true;
1795 if (!hasNextPage(logicalOffset + adjustment))
1796 return false;
1797 adjustment += pageLogicalHeight;
1798 checkRegion = true;
1799 }
1800 return !checkRegion;
1801}
1802
1803void RenderBlockFlow::setPageBreak(LayoutUnit offset, LayoutUnit spaceShortage)
1804{
1805 if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1806 flowThread->setPageBreak(this, offsetFromLogicalTopOfFirstPage() + offset, spaceShortage);
1807}
1808
1809void RenderBlockFlow::updateMinimumPageHeight(LayoutUnit offset, LayoutUnit minHeight)
1810{
1811 if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1812 flowThread->updateMinimumPageHeight(this, offsetFromLogicalTopOfFirstPage() + offset, minHeight);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001813}
1814
1815LayoutUnit RenderBlockFlow::nextPageLogicalTop(LayoutUnit logicalOffset, PageBoundaryRule pageBoundaryRule) const
1816{
1817 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1818 if (!pageLogicalHeight)
1819 return logicalOffset;
1820
1821 // The logicalOffset is in our coordinate space. We can add in our pushed offset.
1822 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset);
1823 if (pageBoundaryRule == ExcludePageBoundary)
1824 return logicalOffset + (remainingLogicalHeight ? remainingLogicalHeight : pageLogicalHeight);
1825 return logicalOffset + remainingLogicalHeight;
1826}
1827
1828LayoutUnit RenderBlockFlow::pageLogicalTopForOffset(LayoutUnit offset) const
1829{
1830 LayoutUnit firstPageLogicalTop = isHorizontalWritingMode() ? view().layoutState()->m_pageOffset.height() : view().layoutState()->m_pageOffset.width();
1831 LayoutUnit blockLogicalTop = isHorizontalWritingMode() ? view().layoutState()->m_layoutOffset.height() : view().layoutState()->m_layoutOffset.width();
1832
1833 LayoutUnit cumulativeOffset = offset + blockLogicalTop;
1834 RenderFlowThread* flowThread = flowThreadContainingBlock();
1835 if (!flowThread) {
1836 LayoutUnit pageLogicalHeight = view().layoutState()->pageLogicalHeight();
1837 if (!pageLogicalHeight)
1838 return 0;
1839 return cumulativeOffset - roundToInt(cumulativeOffset - firstPageLogicalTop) % roundToInt(pageLogicalHeight);
1840 }
hyatt@apple.com150e7f22014-02-11 16:51:45 +00001841 return firstPageLogicalTop + flowThread->pageLogicalTopForOffset(cumulativeOffset - firstPageLogicalTop);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001842}
1843
1844LayoutUnit RenderBlockFlow::pageLogicalHeightForOffset(LayoutUnit offset) const
1845{
1846 RenderFlowThread* flowThread = flowThreadContainingBlock();
1847 if (!flowThread)
1848 return view().layoutState()->m_pageLogicalHeight;
1849 return flowThread->pageLogicalHeightForOffset(offset + offsetFromLogicalTopOfFirstPage());
1850}
1851
1852LayoutUnit RenderBlockFlow::pageRemainingLogicalHeightForOffset(LayoutUnit offset, PageBoundaryRule pageBoundaryRule) const
1853{
1854 offset += offsetFromLogicalTopOfFirstPage();
1855
1856 RenderFlowThread* flowThread = flowThreadContainingBlock();
1857 if (!flowThread) {
1858 LayoutUnit pageLogicalHeight = view().layoutState()->m_pageLogicalHeight;
1859 LayoutUnit remainingHeight = pageLogicalHeight - intMod(offset, pageLogicalHeight);
1860 if (pageBoundaryRule == IncludePageBoundary) {
1861 // If includeBoundaryPoint is true the line exactly on the top edge of a
1862 // column will act as being part of the previous column.
1863 remainingHeight = intMod(remainingHeight, pageLogicalHeight);
1864 }
1865 return remainingHeight;
1866 }
1867
1868 return flowThread->pageRemainingLogicalHeightForOffset(offset, pageBoundaryRule);
1869}
1870
stavila@adobe.comb0d86c42014-04-09 17:07:50 +00001871LayoutUnit RenderBlockFlow::logicalHeightForChildForFragmentation(const RenderBox& child) const
1872{
1873 // This method is required because regions do not fragment monolithic elements but instead
1874 // they let them overflow the region they flow in. This behaviour is different from the
1875 // multicol/printing implementations, which have not yet been updated to correctly handle
1876 // monolithic elements.
1877 // As a result, for the moment, this method will only be used for regions, the multicol and
1878 // printing implementations will stick to the existing behaviour until their fragmentation
1879 // implementation is updated to match the regions implementation.
1880 if (!flowThreadContainingBlock() || !flowThreadContainingBlock()->isRenderNamedFlowThread())
1881 return logicalHeightForChild(child);
1882
1883 // For unsplittable elements, this method will just return the height of the element that
1884 // fits into the current region, without the height of the part that overflows the region.
1885 // This is done for all regions, except the last one because in that case, the logical
1886 // height of the flow thread needs to also
1887 if (!childBoxIsUnsplittableForFragmentation(child) || !pageLogicalHeightForOffset(logicalTopForChild(child)))
1888 return logicalHeightForChild(child);
1889
1890 // If we're on the last page this block fragments to, the logical height of the flow thread must include
1891 // the entire unsplittable child because any following children will not be moved to the next page
1892 // so they will need to be laid out below the current unsplittable child.
1893 LayoutUnit childLogicalTop = logicalTopForChild(child);
1894 if (!hasNextPage(childLogicalTop))
1895 return logicalHeightForChild(child);
1896
1897 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(childLogicalTop, ExcludePageBoundary);
1898 return std::min(child.logicalHeight(), remainingLogicalHeight);
1899}
weinig@apple.com31324fd2013-10-28 19:22:51 +00001900
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001901void RenderBlockFlow::layoutLineGridBox()
1902{
akling@apple.com827be9c2013-10-29 02:58:43 +00001903 if (style().lineGrid() == RenderStyle::initialLineGrid()) {
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001904 setLineGridBox(0);
1905 return;
1906 }
1907
1908 setLineGridBox(0);
1909
akling@apple.com1aa97b02013-10-31 21:59:49 +00001910 auto lineGridBox = std::make_unique<RootInlineBox>(*this);
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001911 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
1912 lineGridBox->setConstructed();
1913 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1914 VerticalPositionCache verticalPositionCache;
1915 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
1916
dbates@webkit.org0cefe4f2014-07-03 22:13:54 +00001917 setLineGridBox(WTF::move(lineGridBox));
akling@apple.com1aa97b02013-10-31 21:59:49 +00001918
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001919 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
1920 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
1921 // to this grid.
1922}
1923
weinig@apple.com12840dc2013-10-22 23:59:08 +00001924bool RenderBlockFlow::containsFloat(RenderBox& renderer) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001925{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001926 return m_floatingObjects && m_floatingObjects->set().contains<RenderBox&, FloatingObjectHashTranslator>(renderer);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001927}
1928
1929void RenderBlockFlow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
1930{
1931 RenderBlock::styleDidChange(diff, oldStyle);
1932
1933 // After our style changed, if we lose our ability to propagate floats into next sibling
1934 // blocks, then we need to find the top most parent containing that overhanging float and
1935 // then mark its descendants with floats for layout and clear all floats from its next
1936 // sibling blocks that exist in our floating objects list. See bug 56299 and 62875.
1937 bool canPropagateFloatIntoSibling = !isFloatingOrOutOfFlowPositioned() && !avoidsFloats();
1938 if (diff == StyleDifferenceLayout && s_canPropagateFloatIntoSibling && !canPropagateFloatIntoSibling && hasOverhangingFloats()) {
1939 RenderBlockFlow* parentBlock = this;
1940 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001941
weinig@apple.comc77041e2013-12-14 18:05:45 +00001942 for (auto& ancestor : ancestorsOfType<RenderBlockFlow>(*this)) {
1943 if (ancestor.isRenderView())
akling@apple.comf3028052013-11-04 08:46:06 +00001944 break;
weinig@apple.comc77041e2013-12-14 18:05:45 +00001945 if (ancestor.hasOverhangingFloats()) {
akling@apple.comf3028052013-11-04 08:46:06 +00001946 for (auto it = floatingObjectSet.begin(), end = floatingObjectSet.end(); it != end; ++it) {
1947 RenderBox& renderer = (*it)->renderer();
weinig@apple.comc77041e2013-12-14 18:05:45 +00001948 if (ancestor.hasOverhangingFloat(renderer)) {
1949 parentBlock = &ancestor;
akling@apple.comf3028052013-11-04 08:46:06 +00001950 break;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001951 }
1952 }
1953 }
1954 }
1955
1956 parentBlock->markAllDescendantsWithFloatsForLayout();
1957 parentBlock->markSiblingsWithFloatsForLayout();
1958 }
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00001959
akling@apple.com8f40c5b2013-10-27 22:54:07 +00001960 if (auto fragment = renderNamedFlowFragment())
akling@apple.com827be9c2013-10-29 02:58:43 +00001961 fragment->setStyle(RenderNamedFlowFragment::createStyle(style()));
antti@apple.com42fb53d2013-10-25 02:33:11 +00001962
antti@apple.com9e891c82014-05-22 06:12:34 +00001963 if (diff >= StyleDifferenceRepaint) {
1964 // FIXME: This could use a cheaper style-only test instead of SimpleLineLayout::canUseFor.
1965 if (selfNeedsLayout() || !m_simpleLineLayout || !SimpleLineLayout::canUseFor(*this))
1966 invalidateLineLayoutPath();
1967 }
1968
hyatt@apple.comfdb12812014-06-23 18:56:52 +00001969 if (multiColumnFlowThread())
1970 updateStylesForColumnChildren();
1971}
1972
1973void RenderBlockFlow::updateStylesForColumnChildren()
1974{
1975 for (auto child = firstChildBox(); child && (child->isInFlowRenderFlowThread() || child->isRenderMultiColumnSet()); child = child->nextSiblingBox())
1976 child->setStyle(RenderStyle::createAnonymousStyleWithDisplay(&style(), BLOCK));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001977}
1978
akling@apple.combdae43242013-10-25 12:00:20 +00001979void RenderBlockFlow::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001980{
akling@apple.com827be9c2013-10-29 02:58:43 +00001981 const RenderStyle* oldStyle = hasInitializedStyle() ? &style() : nullptr;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001982 s_canPropagateFloatIntoSibling = oldStyle ? !isFloatingOrOutOfFlowPositioned() && !avoidsFloats() : false;
1983
stavila@adobe.comd40a2dc2014-06-23 14:59:48 +00001984 if (oldStyle) {
1985 EPosition oldPosition = oldStyle->position();
1986 EPosition newPosition = newStyle.position();
1987
1988 if (parent() && diff == StyleDifferenceLayout && oldPosition != newPosition) {
1989 if (containsFloats() && !isFloating() && !isOutOfFlowPositioned() && newStyle.hasOutOfFlowPosition())
1990 markAllDescendantsWithFloatsForLayout();
1991
1992 // If this block is inside a multicol and is moving from in-flow positioning to out-of-flow positioning,
1993 // remove its info (such as lines-to-region mapping) from the flowthread because it won't be able to do it later.
1994 // The flowthread will no longer be in its containing block chain and, as such, flowThreadContainingBlock will return null.
1995 if (RenderFlowThread* flowThread = flowThreadContainingBlock(SkipFlowThreadCache)) {
1996 if (flowThread->isRenderMultiColumnFlowThread() && !isOutOfFlowPositioned() && (newPosition == AbsolutePosition || newPosition == FixedPosition))
1997 flowThread->removeFlowChildInfo(this);
1998 }
1999 }
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002000 }
2001
2002 RenderBlock::styleWillChange(diff, newStyle);
2003}
2004
antti@apple.coma2c7f242013-10-22 22:37:25 +00002005void RenderBlockFlow::deleteLines()
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002006{
2007 if (containsFloats())
2008 m_floatingObjects->clearLineBoxTreePointers();
weinig@apple.com611b9292013-10-20 22:57:54 +00002009
antti@apple.comfea51992013-10-28 13:39:23 +00002010 if (m_simpleLineLayout) {
antti@apple.com940f5872013-10-24 20:31:11 +00002011 ASSERT(!m_lineBoxes.firstLineBox());
antti@apple.comfea51992013-10-28 13:39:23 +00002012 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +00002013 } else
akling@apple.com31dd4f42013-10-30 22:27:59 +00002014 m_lineBoxes.deleteLineBoxTree();
weinig@apple.com611b9292013-10-20 22:57:54 +00002015
antti@apple.coma2c7f242013-10-22 22:37:25 +00002016 RenderBlock::deleteLines();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002017}
2018
jhoneycutt@apple.com5ad82202014-02-18 22:55:39 +00002019void RenderBlockFlow::moveFloatsTo(RenderBlockFlow* toBlockFlow)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002020{
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002021 // When a portion of the render tree is being detached, anonymous blocks
2022 // will be combined as their children are deleted. In this process, the
2023 // anonymous block later in the tree is merged into the one preceeding it.
2024 // It can happen that the later block (this) contains floats that the
2025 // previous block (toBlockFlow) did not contain, and thus are not in the
2026 // floating objects list for toBlockFlow. This can result in toBlockFlow
2027 // containing floats that are not in it's floating objects list, but are in
2028 // the floating objects lists of siblings and parents. This can cause
2029 // problems when the float itself is deleted, since the deletion code
2030 // assumes that if a float is not in it's containing block's floating
2031 // objects list, it isn't in any floating objects list. In order to
2032 // preserve this condition (removing it has serious performance
2033 // implications), we need to copy the floating objects from the old block
2034 // (this) to the new block (toBlockFlow). The float's metrics will likely
2035 // all be wrong, but since toBlockFlow is already marked for layout, this
2036 // will get fixed before anything gets displayed.
2037 // See bug https://bugs.webkit.org/show_bug.cgi?id=115566
2038 if (m_floatingObjects) {
2039 if (!toBlockFlow->m_floatingObjects)
2040 toBlockFlow->createFloatingObjects();
2041
2042 const FloatingObjectSet& fromFloatingObjectSet = m_floatingObjects->set();
2043 auto end = fromFloatingObjectSet.end();
2044
2045 for (auto it = fromFloatingObjectSet.begin(); it != end; ++it) {
2046 FloatingObject* floatingObject = it->get();
2047
2048 // Don't insert the object again if it's already in the list
weinig@apple.com12840dc2013-10-22 23:59:08 +00002049 if (toBlockFlow->containsFloat(floatingObject->renderer()))
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002050 continue;
2051
2052 toBlockFlow->m_floatingObjects->add(floatingObject->unsafeClone());
2053 }
2054 }
2055}
2056
jhoneycutt@apple.com5ad82202014-02-18 22:55:39 +00002057void RenderBlockFlow::moveAllChildrenIncludingFloatsTo(RenderBlock* toBlock, bool fullRemoveInsert)
2058{
2059 RenderBlockFlow* toBlockFlow = toRenderBlockFlow(toBlock);
2060 moveAllChildrenTo(toBlockFlow, fullRemoveInsert);
2061 moveFloatsTo(toBlockFlow);
2062}
2063
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002064void RenderBlockFlow::addOverflowFromFloats()
2065{
2066 if (!m_floatingObjects)
2067 return;
2068
2069 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2070 auto end = floatingObjectSet.end();
2071 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2072 FloatingObject* r = it->get();
2073 if (r->isDescendant())
2074 addOverflowFromChild(&r->renderer(), IntSize(xPositionForFloatIncludingMargin(r), yPositionForFloatIncludingMargin(r)));
2075 }
2076}
2077
2078void RenderBlockFlow::computeOverflow(LayoutUnit oldClientAfterEdge, bool recomputeFloats)
2079{
2080 RenderBlock::computeOverflow(oldClientAfterEdge, recomputeFloats);
2081
hyatt@apple.com73715ca2014-05-06 21:35:52 +00002082 if (!multiColumnFlowThread() && (recomputeFloats || isRoot() || expandsToEncloseOverhangingFloats() || hasSelfPaintingLayer()))
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002083 addOverflowFromFloats();
2084}
2085
2086void RenderBlockFlow::repaintOverhangingFloats(bool paintAllDescendants)
2087{
2088 // Repaint any overhanging floats (if we know we're the one to paint them).
2089 // Otherwise, bail out.
2090 if (!hasOverhangingFloats())
2091 return;
2092
2093 // FIXME: Avoid disabling LayoutState. At the very least, don't disable it for floats originating
2094 // in this block. Better yet would be to push extra state for the containers of other floats.
2095 LayoutStateDisabler layoutStateDisabler(&view());
2096 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2097 auto end = floatingObjectSet.end();
2098 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2099 FloatingObject* floatingObject = it->get();
2100 // Only repaint the object if it is overhanging, is not in its own layer, and
2101 // is our responsibility to paint (m_shouldPaint is set). When paintAllDescendants is true, the latter
2102 // condition is replaced with being a descendant of us.
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002103 if (logicalBottomForFloat(floatingObject) > logicalHeight()
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002104 && !floatingObject->renderer().hasSelfPaintingLayer()
2105 && (floatingObject->shouldPaint() || (paintAllDescendants && floatingObject->renderer().isDescendantOf(this)))) {
2106 floatingObject->renderer().repaint();
2107 floatingObject->renderer().repaintOverhangingFloats(false);
2108 }
2109 }
2110}
2111
hyatt@apple.comc9021b72014-04-25 21:05:59 +00002112void RenderBlockFlow::paintColumnRules(PaintInfo& paintInfo, const LayoutPoint& point)
hyatt@apple.com58b5ecc2014-04-17 23:06:02 +00002113{
hyatt@apple.comc9021b72014-04-25 21:05:59 +00002114 RenderBlock::paintColumnRules(paintInfo, point);
hyatt@apple.com58b5ecc2014-04-17 23:06:02 +00002115
hyatt@apple.comc9021b72014-04-25 21:05:59 +00002116 if (!multiColumnFlowThread() || paintInfo.context->paintingDisabled())
hyatt@apple.com58b5ecc2014-04-17 23:06:02 +00002117 return;
hyatt@apple.comc9021b72014-04-25 21:05:59 +00002118
hyatt@apple.com58b5ecc2014-04-17 23:06:02 +00002119 // Iterate over our children and paint the column rules as needed.
2120 for (auto& columnSet : childrenOfType<RenderMultiColumnSet>(*this)) {
2121 LayoutPoint childPoint = columnSet.location() + flipForWritingModeForChild(&columnSet, point);
2122 columnSet.paintColumnRules(paintInfo, childPoint);
2123 }
2124}
2125
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002126void RenderBlockFlow::paintFloats(PaintInfo& paintInfo, const LayoutPoint& paintOffset, bool preservePhase)
2127{
2128 if (!m_floatingObjects)
2129 return;
2130
2131 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2132 auto end = floatingObjectSet.end();
2133 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2134 FloatingObject* r = it->get();
2135 // Only paint the object if our m_shouldPaint flag is set.
2136 if (r->shouldPaint() && !r->renderer().hasSelfPaintingLayer()) {
2137 PaintInfo currentPaintInfo(paintInfo);
2138 currentPaintInfo.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
2139 // FIXME: LayoutPoint version of xPositionForFloatIncludingMargin would make this much cleaner.
2140 LayoutPoint childPoint = flipFloatForWritingModeForChild(r, LayoutPoint(paintOffset.x() + xPositionForFloatIncludingMargin(r) - r->renderer().x(), paintOffset.y() + yPositionForFloatIncludingMargin(r) - r->renderer().y()));
2141 r->renderer().paint(currentPaintInfo, childPoint);
2142 if (!preservePhase) {
2143 currentPaintInfo.phase = PaintPhaseChildBlockBackgrounds;
2144 r->renderer().paint(currentPaintInfo, childPoint);
2145 currentPaintInfo.phase = PaintPhaseFloat;
2146 r->renderer().paint(currentPaintInfo, childPoint);
2147 currentPaintInfo.phase = PaintPhaseForeground;
2148 r->renderer().paint(currentPaintInfo, childPoint);
2149 currentPaintInfo.phase = PaintPhaseOutline;
2150 r->renderer().paint(currentPaintInfo, childPoint);
2151 }
2152 }
2153 }
2154}
2155
weinig@apple.com12840dc2013-10-22 23:59:08 +00002156void RenderBlockFlow::clipOutFloatingObjects(RenderBlock& rootBlock, const PaintInfo* paintInfo, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002157{
2158 if (m_floatingObjects) {
2159 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2160 auto end = floatingObjectSet.end();
2161 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2162 FloatingObject* floatingObject = it->get();
2163 LayoutRect floatBox(offsetFromRootBlock.width() + xPositionForFloatIncludingMargin(floatingObject),
2164 offsetFromRootBlock.height() + yPositionForFloatIncludingMargin(floatingObject),
2165 floatingObject->renderer().width(), floatingObject->renderer().height());
weinig@apple.com12840dc2013-10-22 23:59:08 +00002166 rootBlock.flipForWritingMode(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002167 floatBox.move(rootBlockPhysicalPosition.x(), rootBlockPhysicalPosition.y());
zalan@apple.com376339c2014-08-28 04:24:31 +00002168 paintInfo->context->clipOut(snappedIntRect(floatBox));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002169 }
2170 }
2171}
2172
2173void RenderBlockFlow::createFloatingObjects()
2174{
zandobersek@gmail.com31dae992014-03-31 10:12:49 +00002175 m_floatingObjects = std::make_unique<FloatingObjects>(*this);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002176}
2177
2178void RenderBlockFlow::removeFloatingObjects()
2179{
2180 if (!m_floatingObjects)
2181 return;
2182
bjonesbe@adobe.com0b2195a2014-04-11 22:46:02 +00002183 markSiblingsWithFloatsForLayout();
2184
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002185 m_floatingObjects->clear();
2186}
2187
weinig@apple.com12840dc2013-10-22 23:59:08 +00002188FloatingObject* RenderBlockFlow::insertFloatingObject(RenderBox& floatBox)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002189{
weinig@apple.com12840dc2013-10-22 23:59:08 +00002190 ASSERT(floatBox.isFloating());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002191
2192 // Create the list of special objects if we don't aleady have one
2193 if (!m_floatingObjects)
2194 createFloatingObjects();
2195 else {
2196 // Don't insert the floatingObject again if it's already in the list
2197 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002198 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002199 if (it != floatingObjectSet.end())
2200 return it->get();
2201 }
2202
2203 // Create the special floatingObject entry & append it to the list
2204
weinig@apple.com12840dc2013-10-22 23:59:08 +00002205 std::unique_ptr<FloatingObject> floatingObject = FloatingObject::create(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002206
2207 // Our location is irrelevant if we're unsplittable or no pagination is in effect.
2208 // Just go ahead and lay out the float.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002209 bool isChildRenderBlock = floatBox.isRenderBlock();
2210 if (isChildRenderBlock && !floatBox.needsLayout() && view().layoutState()->pageLogicalHeightChanged())
2211 floatBox.setChildNeedsLayout(MarkOnlyThis);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002212
2213 bool needsBlockDirectionLocationSetBeforeLayout = isChildRenderBlock && view().layoutState()->needsBlockDirectionLocationSetBeforeLayout();
2214 if (!needsBlockDirectionLocationSetBeforeLayout || isWritingModeRoot()) // We are unsplittable if we're a block flow root.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002215 floatBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002216 else {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002217 floatBox.updateLogicalWidth();
2218 floatBox.computeAndSetBlockDirectionMargins(this);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002219 }
2220
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002221 setLogicalWidthForFloat(floatingObject.get(), logicalWidthForChild(floatBox) + marginStartForChild(floatBox) + marginEndForChild(floatBox));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002222
dbates@webkit.org0cefe4f2014-07-03 22:13:54 +00002223 return m_floatingObjects->add(WTF::move(floatingObject));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002224}
2225
weinig@apple.com12840dc2013-10-22 23:59:08 +00002226void RenderBlockFlow::removeFloatingObject(RenderBox& floatBox)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002227{
2228 if (m_floatingObjects) {
2229 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002230 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002231 if (it != floatingObjectSet.end()) {
2232 FloatingObject* floatingObject = it->get();
2233 if (childrenInline()) {
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002234 LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
2235 LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002236
2237 // Fix for https://bugs.webkit.org/show_bug.cgi?id=54995.
2238 if (logicalBottom < 0 || logicalBottom < logicalTop || logicalTop == LayoutUnit::max())
2239 logicalBottom = LayoutUnit::max();
2240 else {
2241 // Special-case zero- and less-than-zero-height floats: those don't touch
2242 // the line that they're on, but it still needs to be dirtied. This is
2243 // accomplished by pretending they have a height of 1.
andersca@apple.com86298632013-11-10 19:32:33 +00002244 logicalBottom = std::max(logicalBottom, logicalTop + 1);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002245 }
2246 if (floatingObject->originatingLine()) {
2247 if (!selfNeedsLayout()) {
2248 ASSERT(&floatingObject->originatingLine()->renderer() == this);
2249 floatingObject->originatingLine()->markDirty();
2250 }
2251#if !ASSERT_DISABLED
2252 floatingObject->setOriginatingLine(0);
2253#endif
2254 }
2255 markLinesDirtyInBlockRange(0, logicalBottom);
2256 }
2257 m_floatingObjects->remove(floatingObject);
2258 }
2259 }
2260}
2261
2262void RenderBlockFlow::removeFloatingObjectsBelow(FloatingObject* lastFloat, int logicalOffset)
2263{
2264 if (!containsFloats())
2265 return;
2266
2267 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2268 FloatingObject* curr = floatingObjectSet.last().get();
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002269 while (curr != lastFloat && (!curr->isPlaced() || logicalTopForFloat(curr) >= logicalOffset)) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002270 m_floatingObjects->remove(curr);
2271 if (floatingObjectSet.isEmpty())
2272 break;
2273 curr = floatingObjectSet.last().get();
2274 }
2275}
2276
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002277LayoutUnit RenderBlockFlow::logicalLeftOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
2278{
2279 LayoutUnit offset = fixedOffset;
2280 if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
2281 offset = m_floatingObjects->logicalLeftOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
2282 return adjustLogicalLeftOffsetForLine(offset, applyTextIndent);
2283}
2284
2285LayoutUnit RenderBlockFlow::logicalRightOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
2286{
2287 LayoutUnit offset = fixedOffset;
2288 if (m_floatingObjects && m_floatingObjects->hasRightObjects())
2289 offset = m_floatingObjects->logicalRightOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
2290 return adjustLogicalRightOffsetForLine(offset, applyTextIndent);
2291}
2292
hyatt@apple.comc2e15522014-09-03 19:26:38 +00002293LayoutPoint RenderBlockFlow::computeLogicalLocationForFloat(const FloatingObject* floatingObject, LayoutUnit logicalTopOffset)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002294{
weinig@apple.com12840dc2013-10-22 23:59:08 +00002295 RenderBox& childBox = floatingObject->renderer();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002296 LayoutUnit logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
zoltan@webkit.org7d4f8cc2014-03-26 18:20:15 +00002297 LayoutUnit logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002298
andersca@apple.com86298632013-11-10 19:32:33 +00002299 LayoutUnit floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset); // The width we look for.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002300
2301 LayoutUnit floatLogicalLeft;
2302
2303 bool insideFlowThread = flowThreadContainingBlock();
hyatt@apple.com87515262014-09-04 21:20:12 +00002304 bool isInitialLetter = childBox.style().styleType() == FIRST_LETTER && childBox.style().initialLetterDrop() > 0;
2305
2306 if (isInitialLetter) {
2307 int letterClearance = lowestInitialLetterLogicalBottom() - logicalTopOffset;
2308 if (letterClearance > 0) {
2309 logicalTopOffset += letterClearance;
2310 setLogicalHeight(logicalHeight() + letterClearance);
2311 }
2312 }
2313
akling@apple.com827be9c2013-10-29 02:58:43 +00002314 if (childBox.style().floating() == LeftFloat) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002315 LayoutUnit heightRemainingLeft = 1;
2316 LayoutUnit heightRemainingRight = 1;
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002317 floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
2318 while (logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight) - floatLogicalLeft < floatLogicalWidth) {
andersca@apple.com86298632013-11-10 19:32:33 +00002319 logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002320 floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002321 if (insideFlowThread) {
2322 // Have to re-evaluate all of our offsets, since they may have changed.
2323 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
2324 logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
andersca@apple.com86298632013-11-10 19:32:33 +00002325 floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002326 }
2327 }
andersca@apple.com86298632013-11-10 19:32:33 +00002328 floatLogicalLeft = std::max(logicalLeftOffset - borderAndPaddingLogicalLeft(), floatLogicalLeft);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002329 } else {
2330 LayoutUnit heightRemainingLeft = 1;
2331 LayoutUnit heightRemainingRight = 1;
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002332 floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
2333 while (floatLogicalLeft - logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft) < floatLogicalWidth) {
andersca@apple.com86298632013-11-10 19:32:33 +00002334 logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002335 floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002336 if (insideFlowThread) {
2337 // Have to re-evaluate all of our offsets, since they may have changed.
2338 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
2339 logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
andersca@apple.com86298632013-11-10 19:32:33 +00002340 floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002341 }
2342 }
2343 // Use the original width of the float here, since the local variable
2344 // |floatLogicalWidth| was capped to the available line width. See
2345 // fast/block/float/clamped-right-float.html.
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002346 floatLogicalLeft -= logicalWidthForFloat(floatingObject);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002347 }
2348
hyatt@apple.com87515262014-09-04 21:20:12 +00002349 if (isInitialLetter) {
hyatt@apple.comc2e15522014-09-03 19:26:38 +00002350 const RenderStyle& style = firstLineStyle();
2351 const FontMetrics& fontMetrics = style.fontMetrics();
2352 if (fontMetrics.hasCapHeight()) {
2353 LayoutUnit heightOfLine = lineHeight(true, isHorizontalWritingMode() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
2354 LayoutUnit beforeMarginBorderPadding = childBox.borderAndPaddingBefore() + childBox.marginBefore();
2355
2356 // Make an adjustment to align with the cap height of a theoretical block line.
2357 LayoutUnit adjustment = fontMetrics.ascent() + (heightOfLine - fontMetrics.height()) / 2 - fontMetrics.capHeight() - beforeMarginBorderPadding;
2358 logicalTopOffset += adjustment;
2359
2360 // For sunken and raised caps, we have to make some adjustments. Test if we're sunken or raised (dropHeightDelta will be
2361 // positive for raised and negative for sunken).
2362 int dropHeightDelta = childBox.style().initialLetterHeight() - childBox.style().initialLetterDrop();
2363
2364 // If we're sunken, the float needs to shift down but lines still need to avoid it. In order to do that we increase the float's margin.
2365 if (dropHeightDelta < 0) {
2366 LayoutUnit marginTopIncrease = -dropHeightDelta * heightOfLine;
2367 childBox.setMarginBefore(childBox.marginTop() + marginTopIncrease);
2368 }
2369
2370 // If we're raised, then we actually have to grow the height of the block, since the lines have to be pushed down as though we're placing
2371 // empty lines beside the first letter.
2372 if (dropHeightDelta > 0)
2373 setLogicalHeight(logicalHeight() + dropHeightDelta * heightOfLine);
2374 }
2375 }
2376
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002377 return LayoutPoint(floatLogicalLeft, logicalTopOffset);
2378}
2379
2380bool RenderBlockFlow::positionNewFloats()
2381{
2382 if (!m_floatingObjects)
2383 return false;
2384
2385 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2386 if (floatingObjectSet.isEmpty())
2387 return false;
2388
2389 // If all floats have already been positioned, then we have no work to do.
2390 if (floatingObjectSet.last()->isPlaced())
2391 return false;
2392
2393 // Move backwards through our floating object list until we find a float that has
2394 // already been positioned. Then we'll be able to move forward, positioning all of
2395 // the new floats that need it.
2396 auto it = floatingObjectSet.end();
2397 --it; // Go to last item.
2398 auto begin = floatingObjectSet.begin();
2399 FloatingObject* lastPlacedFloatingObject = 0;
2400 while (it != begin) {
2401 --it;
2402 if ((*it)->isPlaced()) {
2403 lastPlacedFloatingObject = it->get();
2404 ++it;
2405 break;
2406 }
2407 }
2408
2409 LayoutUnit logicalTop = logicalHeight();
2410
2411 // The float cannot start above the top position of the last positioned float.
2412 if (lastPlacedFloatingObject)
andersca@apple.com86298632013-11-10 19:32:33 +00002413 logicalTop = std::max(logicalTopForFloat(lastPlacedFloatingObject), logicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002414
2415 auto end = floatingObjectSet.end();
2416 // Now walk through the set of unpositioned floats and place them.
2417 for (; it != end; ++it) {
2418 FloatingObject* floatingObject = it->get();
2419 // The containing block is responsible for positioning floats, so if we have floats in our
2420 // list that come from somewhere else, do not attempt to position them.
2421 if (floatingObject->renderer().containingBlock() != this)
2422 continue;
2423
weinig@apple.com12840dc2013-10-22 23:59:08 +00002424 RenderBox& childBox = floatingObject->renderer();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002425
akling@apple.com827be9c2013-10-29 02:58:43 +00002426 LayoutUnit childLogicalLeftMargin = style().isLeftToRightDirection() ? marginStartForChild(childBox) : marginEndForChild(childBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002427
weinig@apple.com12840dc2013-10-22 23:59:08 +00002428 LayoutRect oldRect = childBox.frameRect();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002429
akling@apple.com827be9c2013-10-29 02:58:43 +00002430 if (childBox.style().clear() & CLEFT)
andersca@apple.com86298632013-11-10 19:32:33 +00002431 logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatLeft), logicalTop);
akling@apple.com827be9c2013-10-29 02:58:43 +00002432 if (childBox.style().clear() & CRIGHT)
andersca@apple.com86298632013-11-10 19:32:33 +00002433 logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatRight), logicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002434
2435 LayoutPoint floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, logicalTop);
2436
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002437 setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002438
2439 setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
2440 setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
2441
2442 estimateRegionRangeForBoxChild(childBox);
2443
2444 LayoutState* layoutState = view().layoutState();
2445 bool isPaginated = layoutState->isPaginated();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002446 if (isPaginated && !childBox.needsLayout())
2447 childBox.markForPaginationRelayoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002448
weinig@apple.com12840dc2013-10-22 23:59:08 +00002449 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002450
2451 if (isPaginated) {
2452 // If we are unsplittable and don't fit, then we need to move down.
2453 // We include our margins as part of the unsplittable area.
2454 LayoutUnit newLogicalTop = adjustForUnsplittableChild(childBox, floatLogicalLocation.y(), true);
2455
2456 // See if we have a pagination strut that is making us move down further.
2457 // Note that an unsplittable child can't also have a pagination strut, so this is
2458 // exclusive with the case above.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002459 RenderBlock* childBlock = childBox.isRenderBlock() ? toRenderBlock(&childBox) : nullptr;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002460 if (childBlock && childBlock->paginationStrut()) {
2461 newLogicalTop += childBlock->paginationStrut();
2462 childBlock->setPaginationStrut(0);
2463 }
2464
2465 if (newLogicalTop != floatLogicalLocation.y()) {
2466 floatingObject->setPaginationStrut(newLogicalTop - floatLogicalLocation.y());
2467
2468 floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, newLogicalTop);
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002469 setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002470
2471 setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
2472 setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
2473
2474 if (childBlock)
2475 childBlock->setChildNeedsLayout(MarkOnlyThis);
weinig@apple.com12840dc2013-10-22 23:59:08 +00002476 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002477 }
2478
2479 if (updateRegionRangeForBoxChild(childBox)) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002480 childBox.setNeedsLayout(MarkOnlyThis);
2481 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002482 }
2483 }
2484
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002485 setLogicalTopForFloat(floatingObject, floatLogicalLocation.y());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002486
stavila@adobe.comb0d86c42014-04-09 17:07:50 +00002487 setLogicalHeightForFloat(floatingObject, logicalHeightForChildForFragmentation(childBox) + marginBeforeForChild(childBox) + marginAfterForChild(childBox));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002488
2489 m_floatingObjects->addPlacedObject(floatingObject);
2490
zoltan@webkit.org0faf5722013-11-05 02:34:16 +00002491#if ENABLE(CSS_SHAPES)
2492 if (ShapeOutsideInfo* shapeOutside = childBox.shapeOutsideInfo())
bjonesbe@adobe.com029f74e2014-02-13 03:02:53 +00002493 shapeOutside->setReferenceBoxLogicalSize(logicalSizeForChild(childBox));
zoltan@webkit.org0faf5722013-11-05 02:34:16 +00002494#endif
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002495 // If the child moved, we have to repaint it.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002496 if (childBox.checkForRepaintDuringLayout())
2497 childBox.repaintDuringLayoutIfMoved(oldRect);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002498 }
2499 return true;
2500}
2501
bjonesbe@adobe.comf9f10402014-02-20 19:40:28 +00002502void RenderBlockFlow::clearFloats(EClear clear)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002503{
2504 positionNewFloats();
2505 // set y position
2506 LayoutUnit newY = 0;
2507 switch (clear) {
2508 case CLEFT:
2509 newY = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
2510 break;
2511 case CRIGHT:
2512 newY = lowestFloatLogicalBottom(FloatingObject::FloatRight);
2513 break;
2514 case CBOTH:
2515 newY = lowestFloatLogicalBottom();
joepeck@webkit.orgaa676ee52014-01-28 04:04:52 +00002516 break;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002517 default:
2518 break;
2519 }
2520 if (height() < newY)
2521 setLogicalHeight(newY);
2522}
2523
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002524LayoutUnit RenderBlockFlow::logicalLeftFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002525{
2526 if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002527 return m_floatingObjects->logicalLeftOffset(fixedOffset, logicalTop, logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002528
2529 return fixedOffset;
2530}
2531
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002532LayoutUnit RenderBlockFlow::logicalRightFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002533{
2534 if (m_floatingObjects && m_floatingObjects->hasRightObjects())
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002535 return m_floatingObjects->logicalRightOffset(fixedOffset, logicalTop, logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002536
2537 return fixedOffset;
2538}
2539
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002540LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelow(LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002541{
2542 if (!m_floatingObjects)
2543 return logicalHeight;
2544
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002545 return m_floatingObjects->findNextFloatLogicalBottomBelow(logicalHeight);
2546}
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002547
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002548LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelowForBlock(LayoutUnit logicalHeight) const
2549{
2550 if (!m_floatingObjects)
2551 return logicalHeight;
2552
2553 return m_floatingObjects->findNextFloatLogicalBottomBelowForBlock(logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002554}
2555
2556LayoutUnit RenderBlockFlow::lowestFloatLogicalBottom(FloatingObject::Type floatType) const
2557{
2558 if (!m_floatingObjects)
2559 return 0;
2560 LayoutUnit lowestFloatBottom = 0;
2561 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2562 auto end = floatingObjectSet.end();
2563 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2564 FloatingObject* floatingObject = it->get();
2565 if (floatingObject->isPlaced() && floatingObject->type() & floatType)
andersca@apple.com86298632013-11-10 19:32:33 +00002566 lowestFloatBottom = std::max(lowestFloatBottom, logicalBottomForFloat(floatingObject));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002567 }
2568 return lowestFloatBottom;
2569}
2570
hyatt@apple.com87515262014-09-04 21:20:12 +00002571LayoutUnit RenderBlockFlow::lowestInitialLetterLogicalBottom() const
2572{
2573 if (!m_floatingObjects)
2574 return 0;
2575 LayoutUnit lowestFloatBottom = 0;
2576 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2577 auto end = floatingObjectSet.end();
2578 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2579 FloatingObject* floatingObject = it->get();
2580 if (floatingObject->isPlaced() && floatingObject->renderer().style().styleType() == FIRST_LETTER && floatingObject->renderer().style().initialLetterDrop() > 0)
2581 lowestFloatBottom = std::max(lowestFloatBottom, logicalBottomForFloat(floatingObject));
2582 }
2583 return lowestFloatBottom;
2584}
2585
weinig@apple.com12840dc2013-10-22 23:59:08 +00002586LayoutUnit RenderBlockFlow::addOverhangingFloats(RenderBlockFlow& child, bool makeChildPaintOtherFloats)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002587{
2588 // Prevent floats from being added to the canvas by the root element, e.g., <html>.
hyatt@apple.com73715ca2014-05-06 21:35:52 +00002589 if (child.hasOverflowClip() || !child.containsFloats() || child.isRoot() || child.isWritingModeRoot() || child.isRenderFlowThread() || child.isRenderRegion())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002590 return 0;
2591
weinig@apple.com12840dc2013-10-22 23:59:08 +00002592 LayoutUnit childLogicalTop = child.logicalTop();
2593 LayoutUnit childLogicalLeft = child.logicalLeft();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002594 LayoutUnit lowestFloatLogicalBottom = 0;
2595
2596 // Floats that will remain the child's responsibility to paint should factor into its
2597 // overflow.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002598 auto childEnd = child.m_floatingObjects->set().end();
2599 for (auto childIt = child.m_floatingObjects->set().begin(); childIt != childEnd; ++childIt) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002600 FloatingObject* floatingObject = childIt->get();
andersca@apple.com86298632013-11-10 19:32:33 +00002601 LayoutUnit floatLogicalBottom = std::min(logicalBottomForFloat(floatingObject), LayoutUnit::max() - childLogicalTop);
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002602 LayoutUnit logicalBottom = childLogicalTop + floatLogicalBottom;
andersca@apple.com86298632013-11-10 19:32:33 +00002603 lowestFloatLogicalBottom = std::max(lowestFloatLogicalBottom, logicalBottom);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002604
2605 if (logicalBottom > logicalHeight()) {
2606 // If the object is not in the list, we add it now.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002607 if (!containsFloat(floatingObject->renderer())) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002608 LayoutSize offset = isHorizontalWritingMode() ? LayoutSize(-childLogicalLeft, -childLogicalTop) : LayoutSize(-childLogicalTop, -childLogicalLeft);
2609 bool shouldPaint = false;
2610
2611 // The nearest enclosing layer always paints the float (so that zindex and stacking
2612 // behaves properly). We always want to propagate the desire to paint the float as
2613 // far out as we can, to the outermost block that overlaps the float, stopping only
2614 // if we hit a self-painting layer boundary.
2615 if (floatingObject->renderer().enclosingFloatPaintingLayer() == enclosingFloatPaintingLayer()) {
2616 floatingObject->setShouldPaint(false);
2617 shouldPaint = true;
2618 }
2619 // We create the floating object list lazily.
2620 if (!m_floatingObjects)
2621 createFloatingObjects();
2622
2623 m_floatingObjects->add(floatingObject->copyToNewContainer(offset, shouldPaint, true));
2624 }
2625 } else {
2626 if (makeChildPaintOtherFloats && !floatingObject->shouldPaint() && !floatingObject->renderer().hasSelfPaintingLayer()
weinig@apple.com12840dc2013-10-22 23:59:08 +00002627 && floatingObject->renderer().isDescendantOf(&child) && floatingObject->renderer().enclosingFloatPaintingLayer() == child.enclosingFloatPaintingLayer()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002628 // The float is not overhanging from this block, so if it is a descendant of the child, the child should
2629 // paint it (the other case is that it is intruding into the child), unless it has its own layer or enclosing
2630 // layer.
2631 // If makeChildPaintOtherFloats is false, it means that the child must already know about all the floats
2632 // it should paint.
2633 floatingObject->setShouldPaint(true);
2634 }
2635
2636 // 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
2637 // child now.
2638 if (floatingObject->isDescendant())
weinig@apple.com12840dc2013-10-22 23:59:08 +00002639 child.addOverflowFromChild(&floatingObject->renderer(), LayoutSize(xPositionForFloatIncludingMargin(floatingObject), yPositionForFloatIncludingMargin(floatingObject)));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002640 }
2641 }
2642 return lowestFloatLogicalBottom;
2643}
2644
weinig@apple.com12840dc2013-10-22 23:59:08 +00002645bool RenderBlockFlow::hasOverhangingFloat(RenderBox& renderer)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002646{
hyatt@apple.com73715ca2014-05-06 21:35:52 +00002647 if (!m_floatingObjects || !parent())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002648 return false;
2649
2650 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002651 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(renderer);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002652 if (it == floatingObjectSet.end())
2653 return false;
2654
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002655 return logicalBottomForFloat(it->get()) > logicalHeight();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002656}
2657
2658void RenderBlockFlow::addIntrudingFloats(RenderBlockFlow* prev, LayoutUnit logicalLeftOffset, LayoutUnit logicalTopOffset)
2659{
2660 ASSERT(!avoidsFloats());
2661
2662 // If the parent or previous sibling doesn't have any floats to add, don't bother.
2663 if (!prev->m_floatingObjects)
2664 return;
2665
2666 logicalLeftOffset += marginLogicalLeft();
2667
2668 const FloatingObjectSet& prevSet = prev->m_floatingObjects->set();
2669 auto prevEnd = prevSet.end();
2670 for (auto prevIt = prevSet.begin(); prevIt != prevEnd; ++prevIt) {
2671 FloatingObject* floatingObject = prevIt->get();
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002672 if (logicalBottomForFloat(floatingObject) > logicalTopOffset) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002673 if (!m_floatingObjects || !m_floatingObjects->set().contains<FloatingObject&, FloatingObjectHashTranslator>(*floatingObject)) {
2674 // We create the floating object list lazily.
2675 if (!m_floatingObjects)
2676 createFloatingObjects();
2677
2678 // Applying the child's margin makes no sense in the case where the child was passed in.
2679 // since this margin was added already through the modification of the |logicalLeftOffset| variable
2680 // above. |logicalLeftOffset| will equal the margin in this case, so it's already been taken
2681 // into account. Only apply this code if prev is the parent, since otherwise the left margin
2682 // will get applied twice.
2683 LayoutSize offset = isHorizontalWritingMode()
2684 ? LayoutSize(logicalLeftOffset - (prev != parent() ? prev->marginLeft() : LayoutUnit()), logicalTopOffset)
2685 : LayoutSize(logicalTopOffset, logicalLeftOffset - (prev != parent() ? prev->marginTop() : LayoutUnit()));
2686
2687 m_floatingObjects->add(floatingObject->copyToNewContainer(offset));
2688 }
2689 }
2690 }
2691}
2692
2693void RenderBlockFlow::markAllDescendantsWithFloatsForLayout(RenderBox* floatToRemove, bool inLayout)
2694{
2695 if (!everHadLayout() && !containsFloats())
2696 return;
2697
2698 MarkingBehavior markParents = inLayout ? MarkOnlyThis : MarkContainingBlockChain;
2699 setChildNeedsLayout(markParents);
2700
2701 if (floatToRemove)
weinig@apple.com12840dc2013-10-22 23:59:08 +00002702 removeFloatingObject(*floatToRemove);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002703
akling@apple.com525dae62014-01-03 20:22:09 +00002704 if (childrenInline())
2705 return;
2706
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002707 // Iterate over our children and mark them as needed.
akling@apple.com525dae62014-01-03 20:22:09 +00002708 for (auto& block : childrenOfType<RenderBlock>(*this)) {
2709 if (!floatToRemove && block.isFloatingOrOutOfFlowPositioned())
2710 continue;
2711 if (!block.isRenderBlockFlow()) {
2712 if (block.shrinkToAvoidFloats() && block.everHadLayout())
2713 block.setChildNeedsLayout(markParents);
2714 continue;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002715 }
akling@apple.com525dae62014-01-03 20:22:09 +00002716 auto& blockFlow = toRenderBlockFlow(block);
2717 if ((floatToRemove ? blockFlow.containsFloat(*floatToRemove) : blockFlow.containsFloats()) || blockFlow.shrinkToAvoidFloats())
2718 blockFlow.markAllDescendantsWithFloatsForLayout(floatToRemove, inLayout);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002719 }
2720}
2721
2722void RenderBlockFlow::markSiblingsWithFloatsForLayout(RenderBox* floatToRemove)
2723{
2724 if (!m_floatingObjects)
2725 return;
2726
2727 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2728 auto end = floatingObjectSet.end();
2729
2730 for (RenderObject* next = nextSibling(); next; next = next->nextSibling()) {
2731 if (!next->isRenderBlockFlow() || next->isFloatingOrOutOfFlowPositioned() || toRenderBlock(next)->avoidsFloats())
2732 continue;
2733
2734 RenderBlockFlow* nextBlock = toRenderBlockFlow(next);
2735 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002736 RenderBox& floatingBox = (*it)->renderer();
2737 if (floatToRemove && &floatingBox != floatToRemove)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002738 continue;
2739 if (nextBlock->containsFloat(floatingBox))
weinig@apple.com12840dc2013-10-22 23:59:08 +00002740 nextBlock->markAllDescendantsWithFloatsForLayout(&floatingBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002741 }
2742 }
2743}
2744
weinig@apple.com31324fd2013-10-28 19:22:51 +00002745LayoutPoint RenderBlockFlow::flipFloatForWritingModeForChild(const FloatingObject* child, const LayoutPoint& point) const
2746{
akling@apple.com827be9c2013-10-29 02:58:43 +00002747 if (!style().isFlippedBlocksWritingMode())
weinig@apple.com31324fd2013-10-28 19:22:51 +00002748 return point;
2749
2750 // This is similar to RenderBox::flipForWritingModeForChild. We have to subtract out our left/top offsets twice, since
2751 // it's going to get added back in. We hide this complication here so that the calling code looks normal for the unflipped
2752 // case.
2753 if (isHorizontalWritingMode())
2754 return LayoutPoint(point.x(), point.y() + height() - child->renderer().height() - 2 * yPositionForFloatIncludingMargin(child));
2755 return LayoutPoint(point.x() + width() - child->renderer().width() - 2 * xPositionForFloatIncludingMargin(child), point.y());
2756}
2757
weinig@apple.com12840dc2013-10-22 23:59:08 +00002758LayoutUnit RenderBlockFlow::getClearDelta(RenderBox& child, LayoutUnit logicalTop)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002759{
2760 // There is no need to compute clearance if we have no floats.
2761 if (!containsFloats())
2762 return 0;
2763
2764 // At least one float is present. We need to perform the clearance computation.
akling@apple.com827be9c2013-10-29 02:58:43 +00002765 bool clearSet = child.style().clear() != CNONE;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002766 LayoutUnit logicalBottom = 0;
akling@apple.com827be9c2013-10-29 02:58:43 +00002767 switch (child.style().clear()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002768 case CNONE:
2769 break;
2770 case CLEFT:
2771 logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
2772 break;
2773 case CRIGHT:
2774 logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatRight);
2775 break;
2776 case CBOTH:
2777 logicalBottom = lowestFloatLogicalBottom();
2778 break;
2779 }
2780
2781 // 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).
andersca@apple.com86298632013-11-10 19:32:33 +00002782 LayoutUnit result = clearSet ? std::max<LayoutUnit>(0, logicalBottom - logicalTop) : LayoutUnit();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002783 if (!result && child.avoidsFloats()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002784 LayoutUnit newLogicalTop = logicalTop;
2785 while (true) {
2786 LayoutUnit availableLogicalWidthAtNewLogicalTopOffset = availableLogicalWidthForLine(newLogicalTop, false, logicalHeightForChild(child));
2787 if (availableLogicalWidthAtNewLogicalTopOffset == availableLogicalWidthForContent(newLogicalTop))
2788 return newLogicalTop - logicalTop;
2789
2790 RenderRegion* region = regionAtBlockOffset(logicalTopForChild(child));
weinig@apple.com12840dc2013-10-22 23:59:08 +00002791 LayoutRect borderBox = child.borderBoxRectInRegion(region, DoNotCacheRenderBoxRegionInfo);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002792 LayoutUnit childLogicalWidthAtOldLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
2793
2794 // FIXME: None of this is right for perpendicular writing-mode children.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002795 LayoutUnit childOldLogicalWidth = child.logicalWidth();
2796 LayoutUnit childOldMarginLeft = child.marginLeft();
2797 LayoutUnit childOldMarginRight = child.marginRight();
2798 LayoutUnit childOldLogicalTop = child.logicalTop();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002799
weinig@apple.com12840dc2013-10-22 23:59:08 +00002800 child.setLogicalTop(newLogicalTop);
2801 child.updateLogicalWidth();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002802 region = regionAtBlockOffset(logicalTopForChild(child));
weinig@apple.com12840dc2013-10-22 23:59:08 +00002803 borderBox = child.borderBoxRectInRegion(region, DoNotCacheRenderBoxRegionInfo);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002804 LayoutUnit childLogicalWidthAtNewLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
2805
weinig@apple.com12840dc2013-10-22 23:59:08 +00002806 child.setLogicalTop(childOldLogicalTop);
2807 child.setLogicalWidth(childOldLogicalWidth);
2808 child.setMarginLeft(childOldMarginLeft);
2809 child.setMarginRight(childOldMarginRight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002810
2811 if (childLogicalWidthAtNewLogicalTopOffset <= availableLogicalWidthAtNewLogicalTopOffset) {
2812 // Even though we may not be moving, if the logical width did shrink because of the presence of new floats, then
2813 // we need to force a relayout as though we shifted. This happens because of the dynamic addition of overhanging floats
2814 // from previous siblings when negative margins exist on a child (see the addOverhangingFloats call at the end of collapseMargins).
2815 if (childLogicalWidthAtOldLogicalTopOffset != childLogicalWidthAtNewLogicalTopOffset)
weinig@apple.com12840dc2013-10-22 23:59:08 +00002816 child.setChildNeedsLayout(MarkOnlyThis);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002817 return newLogicalTop - logicalTop;
2818 }
2819
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002820 newLogicalTop = nextFloatLogicalBottomBelowForBlock(newLogicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002821 ASSERT(newLogicalTop >= logicalTop);
2822 if (newLogicalTop < logicalTop)
2823 break;
2824 }
2825 ASSERT_NOT_REACHED();
2826 }
2827 return result;
2828}
2829
2830bool RenderBlockFlow::hitTestFloats(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset)
2831{
2832 if (!m_floatingObjects)
2833 return false;
2834
2835 LayoutPoint adjustedLocation = accumulatedOffset;
2836 if (isRenderView())
2837 adjustedLocation += toLayoutSize(toRenderView(*this).frameView().scrollPosition());
2838
2839 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2840 auto begin = floatingObjectSet.begin();
2841 for (auto it = floatingObjectSet.end(); it != begin;) {
2842 --it;
2843 FloatingObject* floatingObject = it->get();
2844 if (floatingObject->shouldPaint() && !floatingObject->renderer().hasSelfPaintingLayer()) {
2845 LayoutUnit xOffset = xPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer().x();
2846 LayoutUnit yOffset = yPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer().y();
2847 LayoutPoint childPoint = flipFloatForWritingModeForChild(floatingObject, adjustedLocation + LayoutSize(xOffset, yOffset));
2848 if (floatingObject->renderer().hitTest(request, result, locationInContainer, childPoint)) {
2849 updateHitTestResult(result, locationInContainer.point() - toLayoutSize(childPoint));
2850 return true;
2851 }
2852 }
2853 }
2854
2855 return false;
2856}
2857
weinig@apple.com611b9292013-10-20 22:57:54 +00002858bool RenderBlockFlow::hitTestInlineChildren(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
2859{
2860 ASSERT(childrenInline());
antti@apple.com940f5872013-10-24 20:31:11 +00002861
darin@apple.come1be6ca2014-04-28 04:19:10 +00002862 if (auto simpleLineLayout = this->simpleLineLayout())
2863 return SimpleLineLayout::hitTestFlow(*this, *simpleLineLayout, request, result, locationInContainer, accumulatedOffset, hitTestAction);
antti@apple.com940f5872013-10-24 20:31:11 +00002864
weinig@apple.com611b9292013-10-20 22:57:54 +00002865 return m_lineBoxes.hitTest(this, request, result, locationInContainer, accumulatedOffset, hitTestAction);
2866}
2867
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002868void RenderBlockFlow::adjustForBorderFit(LayoutUnit x, LayoutUnit& left, LayoutUnit& right) const
2869{
akling@apple.com827be9c2013-10-29 02:58:43 +00002870 if (style().visibility() != VISIBLE)
weinig@apple.com611b9292013-10-20 22:57:54 +00002871 return;
2872
2873 // We don't deal with relative positioning. Our assumption is that you shrink to fit the lines without accounting
2874 // for either overflow or translations via relative positioning.
2875 if (childrenInline()) {
antti@apple.com940f5872013-10-24 20:31:11 +00002876 const_cast<RenderBlockFlow&>(*this).ensureLineBoxes();
2877
weinig@apple.com611b9292013-10-20 22:57:54 +00002878 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
2879 if (box->firstChild())
zalan@apple.com390064f2014-02-26 06:23:03 +00002880 left = std::min(left, x + LayoutUnit(box->firstChild()->x()));
weinig@apple.com611b9292013-10-20 22:57:54 +00002881 if (box->lastChild())
zalan@apple.com390064f2014-02-26 06:23:03 +00002882 right = std::max(right, x + LayoutUnit(ceilf(box->lastChild()->logicalRight())));
weinig@apple.com611b9292013-10-20 22:57:54 +00002883 }
2884 } else {
2885 for (RenderBox* obj = firstChildBox(); obj; obj = obj->nextSiblingBox()) {
2886 if (!obj->isFloatingOrOutOfFlowPositioned()) {
2887 if (obj->isRenderBlockFlow() && !obj->hasOverflowClip())
2888 toRenderBlockFlow(obj)->adjustForBorderFit(x + obj->x(), left, right);
akling@apple.com827be9c2013-10-29 02:58:43 +00002889 else if (obj->style().visibility() == VISIBLE) {
weinig@apple.com611b9292013-10-20 22:57:54 +00002890 // We are a replaced element or some kind of non-block-flow object.
andersca@apple.com86298632013-11-10 19:32:33 +00002891 left = std::min(left, x + obj->x());
2892 right = std::max(right, x + obj->x() + obj->width());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002893 }
2894 }
2895 }
2896 }
weinig@apple.com611b9292013-10-20 22:57:54 +00002897
2898 if (m_floatingObjects) {
2899 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2900 auto end = floatingObjectSet.end();
2901 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2902 FloatingObject* r = it->get();
2903 // Only examine the object if our m_shouldPaint flag is set.
2904 if (r->shouldPaint()) {
2905 LayoutUnit floatLeft = xPositionForFloatIncludingMargin(r) - r->renderer().x();
2906 LayoutUnit floatRight = floatLeft + r->renderer().width();
andersca@apple.com86298632013-11-10 19:32:33 +00002907 left = std::min(left, floatLeft);
2908 right = std::max(right, floatRight);
weinig@apple.com611b9292013-10-20 22:57:54 +00002909 }
2910 }
2911 }
2912}
2913
2914void RenderBlockFlow::fitBorderToLinesIfNeeded()
2915{
akling@apple.com827be9c2013-10-29 02:58:43 +00002916 if (style().borderFit() == BorderFitBorder || hasOverrideWidth())
weinig@apple.com611b9292013-10-20 22:57:54 +00002917 return;
2918
2919 // Walk any normal flow lines to snugly fit.
2920 LayoutUnit left = LayoutUnit::max();
2921 LayoutUnit right = LayoutUnit::min();
2922 LayoutUnit oldWidth = contentWidth();
2923 adjustForBorderFit(0, left, right);
2924
2925 // Clamp to our existing edges. We can never grow. We only shrink.
2926 LayoutUnit leftEdge = borderLeft() + paddingLeft();
2927 LayoutUnit rightEdge = leftEdge + oldWidth;
andersca@apple.com86298632013-11-10 19:32:33 +00002928 left = std::min(rightEdge, std::max(leftEdge, left));
2929 right = std::max(leftEdge, std::min(rightEdge, right));
weinig@apple.com611b9292013-10-20 22:57:54 +00002930
2931 LayoutUnit newContentWidth = right - left;
2932 if (newContentWidth == oldWidth)
2933 return;
2934
2935 setOverrideLogicalContentWidth(newContentWidth);
2936 layoutBlock(false);
2937 clearOverrideLogicalContentWidth();
2938}
2939
2940void RenderBlockFlow::markLinesDirtyInBlockRange(LayoutUnit logicalTop, LayoutUnit logicalBottom, RootInlineBox* highest)
2941{
2942 if (logicalTop >= logicalBottom)
2943 return;
2944
antti@apple.combe9d3e12014-05-11 09:42:47 +00002945 // Floats currently affect the choice whether to use simple line layout path.
2946 if (m_simpleLineLayout) {
2947 invalidateLineLayoutPath();
2948 return;
2949 }
2950
weinig@apple.com611b9292013-10-20 22:57:54 +00002951 RootInlineBox* lowestDirtyLine = lastRootBox();
2952 RootInlineBox* afterLowest = lowestDirtyLine;
2953 while (lowestDirtyLine && lowestDirtyLine->lineBottomWithLeading() >= logicalBottom && logicalBottom < LayoutUnit::max()) {
2954 afterLowest = lowestDirtyLine;
2955 lowestDirtyLine = lowestDirtyLine->prevRootBox();
2956 }
2957
2958 while (afterLowest && afterLowest != highest && (afterLowest->lineBottomWithLeading() >= logicalTop || afterLowest->lineBottomWithLeading() < 0)) {
2959 afterLowest->markDirty();
2960 afterLowest = afterLowest->prevRootBox();
2961 }
2962}
2963
antti@apple.com0e632aa2013-10-22 21:03:38 +00002964int RenderBlockFlow::firstLineBaseline() const
weinig@apple.com611b9292013-10-20 22:57:54 +00002965{
2966 if (isWritingModeRoot() && !isRubyRun())
2967 return -1;
2968
2969 if (!childrenInline())
antti@apple.com0e632aa2013-10-22 21:03:38 +00002970 return RenderBlock::firstLineBaseline();
weinig@apple.com611b9292013-10-20 22:57:54 +00002971
antti@apple.com940f5872013-10-24 20:31:11 +00002972 if (!hasLines())
2973 return -1;
weinig@apple.com611b9292013-10-20 22:57:54 +00002974
darin@apple.come1be6ca2014-04-28 04:19:10 +00002975 if (auto simpleLineLayout = this->simpleLineLayout())
2976 return SimpleLineLayout::computeFlowFirstLineBaseline(*this, *simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00002977
akling@apple.comee3c8df2013-11-06 08:09:44 +00002978 ASSERT(firstRootBox());
2979 return firstRootBox()->logicalTop() + firstLineStyle().fontMetrics().ascent(firstRootBox()->baselineType());
weinig@apple.com611b9292013-10-20 22:57:54 +00002980}
2981
2982int RenderBlockFlow::inlineBlockBaseline(LineDirectionMode lineDirection) const
2983{
2984 if (isWritingModeRoot() && !isRubyRun())
2985 return -1;
2986
2987 if (!childrenInline())
2988 return RenderBlock::inlineBlockBaseline(lineDirection);
2989
antti@apple.com0e632aa2013-10-22 21:03:38 +00002990 if (!hasLines()) {
2991 if (!hasLineIfEmpty())
2992 return -1;
akling@apple.com827be9c2013-10-29 02:58:43 +00002993 const FontMetrics& fontMetrics = firstLineStyle().fontMetrics();
weinig@apple.com611b9292013-10-20 22:57:54 +00002994 return fontMetrics.ascent()
2995 + (lineHeight(true, lineDirection, PositionOfInteriorLineBoxes) - fontMetrics.height()) / 2
2996 + (lineDirection == HorizontalLine ? borderTop() + paddingTop() : borderRight() + paddingRight());
2997 }
2998
darin@apple.come1be6ca2014-04-28 04:19:10 +00002999 if (auto simpleLineLayout = this->simpleLineLayout())
3000 return SimpleLineLayout::computeFlowLastLineBaseline(*this, *simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00003001
akling@apple.comee3c8df2013-11-06 08:09:44 +00003002 bool isFirstLine = lastRootBox() == firstRootBox();
akling@apple.com827be9c2013-10-29 02:58:43 +00003003 const RenderStyle& style = isFirstLine ? firstLineStyle() : this->style();
akling@apple.comee3c8df2013-11-06 08:09:44 +00003004 return lastRootBox()->logicalTop() + style.fontMetrics().ascent(lastRootBox()->baselineType());
weinig@apple.com611b9292013-10-20 22:57:54 +00003005}
3006
weinig@apple.com12840dc2013-10-22 23:59:08 +00003007GapRects RenderBlockFlow::inlineSelectionGaps(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
weinig@apple.com611b9292013-10-20 22:57:54 +00003008 LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
3009{
antti@apple.comfea51992013-10-28 13:39:23 +00003010 ASSERT(!m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00003011
weinig@apple.com611b9292013-10-20 22:57:54 +00003012 GapRects result;
3013
3014 bool containsStart = selectionState() == SelectionStart || selectionState() == SelectionBoth;
3015
antti@apple.com0e632aa2013-10-22 21:03:38 +00003016 if (!hasLines()) {
weinig@apple.com611b9292013-10-20 22:57:54 +00003017 if (containsStart) {
3018 // Go ahead and update our lastLogicalTop to be the bottom of the block. <hr>s or empty blocks with height can trip this
3019 // case.
3020 lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalHeight();
3021 lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, logicalHeight(), cache);
3022 lastLogicalRight = logicalRightSelectionOffset(rootBlock, logicalHeight(), cache);
3023 }
3024 return result;
3025 }
3026
3027 RootInlineBox* lastSelectedLine = 0;
3028 RootInlineBox* curr;
3029 for (curr = firstRootBox(); curr && !curr->hasSelectedChildren(); curr = curr->nextRootBox()) { }
3030
3031 // Now paint the gaps for the lines.
3032 for (; curr && curr->hasSelectedChildren(); curr = curr->nextRootBox()) {
3033 LayoutUnit selTop = curr->selectionTopAdjustedForPrecedingBlock();
3034 LayoutUnit selHeight = curr->selectionHeightAdjustedForPrecedingBlock();
3035
3036 if (!containsStart && !lastSelectedLine &&
3037 selectionState() != SelectionStart && selectionState() != SelectionBoth)
3038 result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop, lastLogicalLeft, lastLogicalRight, selTop, cache, paintInfo));
3039
3040 LayoutRect logicalRect(curr->logicalLeft(), selTop, curr->logicalWidth(), selTop + selHeight);
3041 logicalRect.move(isHorizontalWritingMode() ? offsetFromRootBlock : offsetFromRootBlock.transposedSize());
weinig@apple.com12840dc2013-10-22 23:59:08 +00003042 LayoutRect physicalRect = rootBlock.logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
weinig@apple.com611b9292013-10-20 22:57:54 +00003043 if (!paintInfo || (isHorizontalWritingMode() && physicalRect.y() < paintInfo->rect.maxY() && physicalRect.maxY() > paintInfo->rect.y())
3044 || (!isHorizontalWritingMode() && physicalRect.x() < paintInfo->rect.maxX() && physicalRect.maxX() > paintInfo->rect.x()))
3045 result.unite(curr->lineSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, selTop, selHeight, cache, paintInfo));
3046
3047 lastSelectedLine = curr;
3048 }
3049
3050 if (containsStart && !lastSelectedLine)
3051 // VisibleSelection must start just after our last line.
3052 lastSelectedLine = lastRootBox();
3053
3054 if (lastSelectedLine && selectionState() != SelectionEnd && selectionState() != SelectionBoth) {
3055 // Go ahead and update our lastY to be the bottom of the last selected line.
3056 lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + lastSelectedLine->selectionBottom();
3057 lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
3058 lastLogicalRight = logicalRightSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
3059 }
3060 return result;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00003061}
3062
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00003063void RenderBlockFlow::createRenderNamedFlowFragmentIfNeeded()
3064{
abucur@adobe.com0e81bc72013-10-22 14:50:37 +00003065 if (!document().cssRegionsEnabled() || renderNamedFlowFragment() || isRenderNamedFlowFragment())
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00003066 return;
3067
mihnea@adobe.com7c5101d2014-07-23 12:12:36 +00003068 // FIXME: Multicolumn regions not yet supported (http://dev.w3.org/csswg/css-regions/#multi-column-regions)
3069 if (style().isDisplayRegionType() && style().hasFlowFrom() && !style().specifiesColumns()) {
akling@apple.com827be9c2013-10-29 02:58:43 +00003070 RenderNamedFlowFragment* flowFragment = new RenderNamedFlowFragment(document(), RenderNamedFlowFragment::createStyle(style()));
akling@apple.com8f40c5b2013-10-27 22:54:07 +00003071 flowFragment->initializeStyle();
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00003072 setRenderNamedFlowFragment(flowFragment);
3073 addChild(renderNamedFlowFragment());
3074 }
3075}
3076
abucur@adobe.comeaf5e222014-05-14 14:35:07 +00003077bool RenderBlockFlow::needsLayoutAfterRegionRangeChange() const
3078{
3079 // A block without floats or that expands to enclose them won't need a relayout
3080 // after a region range change. There is no overflow content needing relayout
3081 // in the region chain because the region range can only shrink after the estimation.
3082 if (!containsFloats() || expandsToEncloseOverhangingFloats())
3083 return false;
3084
3085 return true;
3086}
3087
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00003088bool RenderBlockFlow::canHaveChildren() const
3089{
3090 return !renderNamedFlowFragment() ? RenderBlock::canHaveChildren() : renderNamedFlowFragment()->canHaveChildren();
3091}
3092
3093bool RenderBlockFlow::canHaveGeneratedChildren() const
3094{
3095 return !renderNamedFlowFragment() ? RenderBlock::canHaveGeneratedChildren() : renderNamedFlowFragment()->canHaveGeneratedChildren();
3096}
3097
3098bool RenderBlockFlow::namedFlowFragmentNeedsUpdate() const
3099{
3100 if (!isRenderNamedFlowFragmentContainer())
3101 return false;
3102
3103 return hasRelativeLogicalHeight() && !isRenderView();
3104}
3105
3106void RenderBlockFlow::updateLogicalHeight()
3107{
3108 RenderBlock::updateLogicalHeight();
3109
abucur@adobe.comfad53712014-05-06 17:30:40 +00003110 if (renderNamedFlowFragment()) {
andersca@apple.com86298632013-11-10 19:32:33 +00003111 renderNamedFlowFragment()->setLogicalHeight(std::max<LayoutUnit>(0, logicalHeight() - borderAndPaddingLogicalHeight()));
abucur@adobe.comfad53712014-05-06 17:30:40 +00003112 renderNamedFlowFragment()->invalidateRegionIfNeeded();
3113 }
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00003114}
3115
3116void RenderBlockFlow::setRenderNamedFlowFragment(RenderNamedFlowFragment* flowFragment)
3117{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00003118 RenderBlockFlowRareData& rareData = ensureRareBlockFlowData();
abucur@adobe.com0e81bc72013-10-22 14:50:37 +00003119 if (rareData.m_renderNamedFlowFragment)
3120 rareData.m_renderNamedFlowFragment->destroy();
3121 rareData.m_renderNamedFlowFragment = flowFragment;
3122}
3123
hyatt@apple.come9fe3d32014-01-24 17:14:22 +00003124void RenderBlockFlow::setMultiColumnFlowThread(RenderMultiColumnFlowThread* flowThread)
3125{
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003126 if (flowThread || hasRareBlockFlowData()) {
3127 RenderBlockFlowRareData& rareData = ensureRareBlockFlowData();
3128 rareData.m_multiColumnFlowThread = flowThread;
3129 }
hyatt@apple.come9fe3d32014-01-24 17:14:22 +00003130}
3131
akling@apple.com525dae62014-01-03 20:22:09 +00003132static bool shouldCheckLines(const RenderBlockFlow& blockFlow)
weinig@apple.com17140912013-10-19 19:55:40 +00003133{
akling@apple.com38f0a652014-02-06 21:24:17 +00003134 return !blockFlow.isFloatingOrOutOfFlowPositioned() && blockFlow.style().height().isAuto();
weinig@apple.com17140912013-10-19 19:55:40 +00003135}
3136
3137RootInlineBox* RenderBlockFlow::lineAtIndex(int i) const
3138{
3139 ASSERT(i >= 0);
3140
akling@apple.com827be9c2013-10-29 02:58:43 +00003141 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00003142 return nullptr;
3143
3144 if (childrenInline()) {
3145 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
3146 if (!i--)
3147 return box;
3148 }
akling@apple.com525dae62014-01-03 20:22:09 +00003149 return nullptr;
3150 }
3151
3152 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
3153 if (!shouldCheckLines(blockFlow))
3154 continue;
3155 if (RootInlineBox* box = blockFlow.lineAtIndex(i))
3156 return box;
weinig@apple.com17140912013-10-19 19:55:40 +00003157 }
3158
3159 return nullptr;
3160}
3161
3162int RenderBlockFlow::lineCount(const RootInlineBox* stopRootInlineBox, bool* found) const
3163{
akling@apple.com827be9c2013-10-29 02:58:43 +00003164 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00003165 return 0;
3166
3167 int count = 0;
3168
3169 if (childrenInline()) {
darin@apple.come1be6ca2014-04-28 04:19:10 +00003170 if (auto simpleLineLayout = this->simpleLineLayout()) {
antti@apple.com0b3dffe2014-03-24 16:30:52 +00003171 ASSERT(!stopRootInlineBox);
darin@apple.come1be6ca2014-04-28 04:19:10 +00003172 return simpleLineLayout->lineCount();
antti@apple.com0b3dffe2014-03-24 16:30:52 +00003173 }
weinig@apple.com17140912013-10-19 19:55:40 +00003174 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
3175 count++;
3176 if (box == stopRootInlineBox) {
3177 if (found)
3178 *found = true;
3179 break;
3180 }
3181 }
akling@apple.com525dae62014-01-03 20:22:09 +00003182 return count;
3183 }
3184
3185 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
3186 if (!shouldCheckLines(blockFlow))
3187 continue;
3188 bool recursiveFound = false;
3189 count += blockFlow.lineCount(stopRootInlineBox, &recursiveFound);
3190 if (recursiveFound) {
3191 if (found)
3192 *found = true;
3193 break;
weinig@apple.com17140912013-10-19 19:55:40 +00003194 }
3195 }
3196
3197 return count;
3198}
3199
3200static int getHeightForLineCount(const RenderBlockFlow& block, int lineCount, bool includeBottom, int& count)
3201{
akling@apple.com827be9c2013-10-29 02:58:43 +00003202 if (block.style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00003203 return -1;
3204
3205 if (block.childrenInline()) {
3206 for (auto box = block.firstRootBox(); box; box = box->nextRootBox()) {
3207 if (++count == lineCount)
3208 return box->lineBottom() + (includeBottom ? (block.borderBottom() + block.paddingBottom()) : LayoutUnit());
3209 }
3210 } else {
3211 RenderBox* normalFlowChildWithoutLines = 0;
3212 for (auto obj = block.firstChildBox(); obj; obj = obj->nextSiblingBox()) {
akling@apple.com525dae62014-01-03 20:22:09 +00003213 if (obj->isRenderBlockFlow() && shouldCheckLines(toRenderBlockFlow(*obj))) {
weinig@apple.com17140912013-10-19 19:55:40 +00003214 int result = getHeightForLineCount(toRenderBlockFlow(*obj), lineCount, false, count);
3215 if (result != -1)
3216 return result + obj->y() + (includeBottom ? (block.borderBottom() + block.paddingBottom()) : LayoutUnit());
akling@apple.com38f0a652014-02-06 21:24:17 +00003217 } else if (!obj->isFloatingOrOutOfFlowPositioned())
weinig@apple.com17140912013-10-19 19:55:40 +00003218 normalFlowChildWithoutLines = obj;
3219 }
3220 if (normalFlowChildWithoutLines && !lineCount)
3221 return normalFlowChildWithoutLines->y() + normalFlowChildWithoutLines->height();
3222 }
3223
3224 return -1;
3225}
3226
3227int RenderBlockFlow::heightForLineCount(int lineCount)
3228{
3229 int count = 0;
3230 return getHeightForLineCount(*this, lineCount, true, count);
3231}
3232
3233void RenderBlockFlow::clearTruncation()
3234{
akling@apple.com827be9c2013-10-29 02:58:43 +00003235 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00003236 return;
3237
3238 if (childrenInline() && hasMarkupTruncation()) {
antti@apple.com940f5872013-10-24 20:31:11 +00003239 ensureLineBoxes();
3240
weinig@apple.com17140912013-10-19 19:55:40 +00003241 setHasMarkupTruncation(false);
3242 for (auto box = firstRootBox(); box; box = box->nextRootBox())
3243 box->clearTruncation();
akling@apple.com525dae62014-01-03 20:22:09 +00003244 return;
3245 }
3246
3247 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
3248 if (shouldCheckLines(blockFlow))
3249 blockFlow.clearTruncation();
weinig@apple.com17140912013-10-19 19:55:40 +00003250 }
3251}
3252
weinig@apple.com3f23b382013-10-19 20:26:58 +00003253bool RenderBlockFlow::containsNonZeroBidiLevel() const
3254{
3255 for (auto root = firstRootBox(); root; root = root->nextRootBox()) {
3256 for (auto box = root->firstLeafChild(); box; box = box->nextLeafChild()) {
3257 if (box->bidiLevel())
3258 return true;
3259 }
3260 }
3261 return false;
3262}
3263
weinig@apple.com611b9292013-10-20 22:57:54 +00003264Position RenderBlockFlow::positionForBox(InlineBox *box, bool start) const
3265{
3266 if (!box)
3267 return Position();
3268
3269 if (!box->renderer().nonPseudoNode())
3270 return createLegacyEditingPosition(nonPseudoElement(), start ? caretMinOffset() : caretMaxOffset());
3271
3272 if (!box->isInlineTextBox())
3273 return createLegacyEditingPosition(box->renderer().nonPseudoNode(), start ? box->renderer().caretMinOffset() : box->renderer().caretMaxOffset());
3274
3275 InlineTextBox* textBox = toInlineTextBox(box);
3276 return createLegacyEditingPosition(box->renderer().nonPseudoNode(), start ? textBox->start() : textBox->start() + textBox->len());
3277}
3278
stavila@adobe.com4ce2fff2014-04-25 13:56:12 +00003279VisiblePosition RenderBlockFlow::positionForPointWithInlineChildren(const LayoutPoint& pointInLogicalContents, const RenderRegion* region)
weinig@apple.com611b9292013-10-20 22:57:54 +00003280{
3281 ASSERT(childrenInline());
3282
antti@apple.com940f5872013-10-24 20:31:11 +00003283 ensureLineBoxes();
3284
weinig@apple.com611b9292013-10-20 22:57:54 +00003285 if (!firstRootBox())
3286 return createVisiblePosition(0, DOWNSTREAM);
3287
akling@apple.com827be9c2013-10-29 02:58:43 +00003288 bool linesAreFlipped = style().isFlippedLinesWritingMode();
3289 bool blocksAreFlipped = style().isFlippedBlocksWritingMode();
weinig@apple.com611b9292013-10-20 22:57:54 +00003290
3291 // look for the closest line box in the root box which is at the passed-in y coordinate
3292 InlineBox* closestBox = 0;
3293 RootInlineBox* firstRootBoxWithChildren = 0;
3294 RootInlineBox* lastRootBoxWithChildren = 0;
3295 for (RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox()) {
stavila@adobe.com4ce2fff2014-04-25 13:56:12 +00003296 if (region && root->containingRegion() != region)
3297 continue;
3298
weinig@apple.com611b9292013-10-20 22:57:54 +00003299 if (!root->firstLeafChild())
3300 continue;
3301 if (!firstRootBoxWithChildren)
3302 firstRootBoxWithChildren = root;
3303
3304 if (!linesAreFlipped && root->isFirstAfterPageBreak() && (pointInLogicalContents.y() < root->lineTopWithLeading()
3305 || (blocksAreFlipped && pointInLogicalContents.y() == root->lineTopWithLeading())))
3306 break;
3307
3308 lastRootBoxWithChildren = root;
3309
3310 // check if this root line box is located at this y coordinate
3311 if (pointInLogicalContents.y() < root->selectionBottom() || (blocksAreFlipped && pointInLogicalContents.y() == root->selectionBottom())) {
3312 if (linesAreFlipped) {
3313 RootInlineBox* nextRootBoxWithChildren = root->nextRootBox();
3314 while (nextRootBoxWithChildren && !nextRootBoxWithChildren->firstLeafChild())
3315 nextRootBoxWithChildren = nextRootBoxWithChildren->nextRootBox();
3316
3317 if (nextRootBoxWithChildren && nextRootBoxWithChildren->isFirstAfterPageBreak() && (pointInLogicalContents.y() > nextRootBoxWithChildren->lineTopWithLeading()
3318 || (!blocksAreFlipped && pointInLogicalContents.y() == nextRootBoxWithChildren->lineTopWithLeading())))
3319 continue;
3320 }
3321 closestBox = root->closestLeafChildForLogicalLeftPosition(pointInLogicalContents.x());
3322 if (closestBox)
3323 break;
3324 }
3325 }
3326
3327 bool moveCaretToBoundary = frame().editor().behavior().shouldMoveCaretToHorizontalBoundaryWhenPastTopOrBottom();
3328
3329 if (!moveCaretToBoundary && !closestBox && lastRootBoxWithChildren) {
3330 // y coordinate is below last root line box, pretend we hit it
3331 closestBox = lastRootBoxWithChildren->closestLeafChildForLogicalLeftPosition(pointInLogicalContents.x());
3332 }
3333
3334 if (closestBox) {
3335 if (moveCaretToBoundary) {
andersca@apple.com86298632013-11-10 19:32:33 +00003336 LayoutUnit firstRootBoxWithChildrenTop = std::min<LayoutUnit>(firstRootBoxWithChildren->selectionTop(), firstRootBoxWithChildren->logicalTop());
weinig@apple.com611b9292013-10-20 22:57:54 +00003337 if (pointInLogicalContents.y() < firstRootBoxWithChildrenTop
3338 || (blocksAreFlipped && pointInLogicalContents.y() == firstRootBoxWithChildrenTop)) {
3339 InlineBox* box = firstRootBoxWithChildren->firstLeafChild();
3340 if (box->isLineBreak()) {
3341 if (InlineBox* newBox = box->nextLeafChildIgnoringLineBreak())
3342 box = newBox;
3343 }
3344 // y coordinate is above first root line box, so return the start of the first
3345 return VisiblePosition(positionForBox(box, true), DOWNSTREAM);
3346 }
3347 }
3348
3349 // pass the box a top position that is inside it
3350 LayoutPoint point(pointInLogicalContents.x(), closestBox->root().blockDirectionPointInLine());
3351 if (!isHorizontalWritingMode())
3352 point = point.transposedPoint();
3353 if (closestBox->renderer().isReplaced())
weinig@apple.com12840dc2013-10-22 23:59:08 +00003354 return positionForPointRespectingEditingBoundaries(*this, toRenderBox(closestBox->renderer()), point);
stavila@adobe.com4ce2fff2014-04-25 13:56:12 +00003355 return closestBox->renderer().positionForPoint(point, nullptr);
weinig@apple.com611b9292013-10-20 22:57:54 +00003356 }
3357
3358 if (lastRootBoxWithChildren) {
3359 // We hit this case for Mac behavior when the Y coordinate is below the last box.
3360 ASSERT(moveCaretToBoundary);
3361 InlineBox* logicallyLastBox;
3362 if (lastRootBoxWithChildren->getLogicalEndBoxWithNode(logicallyLastBox))
3363 return VisiblePosition(positionForBox(logicallyLastBox, false), DOWNSTREAM);
3364 }
3365
3366 // Can't reach this. We have a root line box, but it has no kids.
3367 // FIXME: This should ASSERT_NOT_REACHED(), but clicking on placeholder text
3368 // seems to hit this code path.
3369 return createVisiblePosition(0, DOWNSTREAM);
3370}
3371
stavila@adobe.com4ce2fff2014-04-25 13:56:12 +00003372VisiblePosition RenderBlockFlow::positionForPoint(const LayoutPoint& point, const RenderRegion* region)
commit-queue@webkit.org5ce6c902013-11-11 18:21:05 +00003373{
3374 if (auto fragment = renderNamedFlowFragment())
stavila@adobe.com4ce2fff2014-04-25 13:56:12 +00003375 return fragment->positionForPoint(point, region);
3376 return RenderBlock::positionForPoint(point, region);
commit-queue@webkit.org5ce6c902013-11-11 18:21:05 +00003377}
3378
3379
weinig@apple.com611b9292013-10-20 22:57:54 +00003380void RenderBlockFlow::addFocusRingRectsForInlineChildren(Vector<IntRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject*)
3381{
antti@apple.com940f5872013-10-24 20:31:11 +00003382 ASSERT(childrenInline());
3383
3384 ensureLineBoxes();
3385
weinig@apple.com611b9292013-10-20 22:57:54 +00003386 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
andersca@apple.com86298632013-11-10 19:32:33 +00003387 LayoutUnit top = std::max<LayoutUnit>(curr->lineTop(), curr->top());
3388 LayoutUnit bottom = std::min<LayoutUnit>(curr->lineBottom(), curr->top() + curr->height());
weinig@apple.com611b9292013-10-20 22:57:54 +00003389 LayoutRect rect(additionalOffset.x() + curr->x(), additionalOffset.y() + top, curr->width(), bottom - top);
3390 if (!rect.isEmpty())
zalan@apple.com376339c2014-08-28 04:24:31 +00003391 rects.append(snappedIntRect(rect));
weinig@apple.com611b9292013-10-20 22:57:54 +00003392 }
3393}
3394
3395void RenderBlockFlow::paintInlineChildren(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
3396{
3397 ASSERT(childrenInline());
antti@apple.com940f5872013-10-24 20:31:11 +00003398
darin@apple.come1be6ca2014-04-28 04:19:10 +00003399 if (auto simpleLineLayout = this->simpleLineLayout()) {
3400 SimpleLineLayout::paintFlow(*this, *simpleLineLayout, paintInfo, paintOffset);
antti@apple.com940f5872013-10-24 20:31:11 +00003401 return;
3402 }
weinig@apple.com611b9292013-10-20 22:57:54 +00003403 m_lineBoxes.paint(this, paintInfo, paintOffset);
3404}
3405
hyatt@apple.com73715ca2014-05-06 21:35:52 +00003406bool RenderBlockFlow::relayoutForPagination(LayoutStateMaintainer& statePusher)
weinig@apple.com611b9292013-10-20 22:57:54 +00003407{
hyatt@apple.com73715ca2014-05-06 21:35:52 +00003408 if (!multiColumnFlowThread() || !multiColumnFlowThread()->shouldRelayoutForPagination())
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003409 return false;
3410
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003411 multiColumnFlowThread()->setNeedsHeightsRecalculation(false);
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003412 multiColumnFlowThread()->setInBalancingPass(true); // Prevent re-entering this method (and recursion into layout).
3413
3414 bool needsRelayout;
3415 bool neededRelayout = false;
3416 bool firstPass = true;
3417 do {
3418 // Column heights may change here because of balancing. We may have to do multiple layout
3419 // passes, depending on how the contents is fitted to the changed column heights. In most
3420 // cases, laying out again twice or even just once will suffice. Sometimes we need more
3421 // passes than that, though, but the number of retries should not exceed the number of
3422 // columns, unless we have a bug.
3423 needsRelayout = false;
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003424 for (RenderMultiColumnSet* multicolSet = multiColumnFlowThread()->firstMultiColumnSet(); multicolSet; multicolSet = multicolSet->nextSiblingMultiColumnSet()) {
3425 if (multicolSet->recalculateColumnHeight(firstPass))
3426 needsRelayout = true;
3427 if (needsRelayout) {
3428 // Once a column set gets a new column height, that column set and all successive column
3429 // sets need to be laid out over again, since their logical top will be affected by
3430 // this, and therefore their column heights may change as well, at least if the multicol
3431 // height is constrained.
3432 multicolSet->setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003433 }
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003434 }
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003435 if (needsRelayout) {
3436 // Layout again. Column balancing resulted in a new height.
3437 neededRelayout = true;
3438 multiColumnFlowThread()->setChildNeedsLayout(MarkOnlyThis);
3439 setChildNeedsLayout(MarkOnlyThis);
3440 if (firstPass)
3441 statePusher.pop();
3442 layoutBlock(false);
3443 }
3444 firstPass = false;
3445 } while (needsRelayout);
3446
3447 multiColumnFlowThread()->setInBalancingPass(false);
3448
3449 return neededRelayout;
weinig@apple.com611b9292013-10-20 22:57:54 +00003450}
3451
antti@apple.com940f5872013-10-24 20:31:11 +00003452bool RenderBlockFlow::hasLines() const
3453{
3454 ASSERT(childrenInline());
3455
darin@apple.come1be6ca2014-04-28 04:19:10 +00003456 if (auto simpleLineLayout = this->simpleLineLayout())
3457 return simpleLineLayout->lineCount();
antti@apple.com940f5872013-10-24 20:31:11 +00003458
3459 return lineBoxes().firstLineBox();
3460}
3461
antti@apple.com9e891c82014-05-22 06:12:34 +00003462void RenderBlockFlow::invalidateLineLayoutPath()
3463{
3464 switch (m_lineLayoutPath) {
3465 case UndeterminedPath:
3466 case ForceLineBoxesPath:
3467 ASSERT(!m_simpleLineLayout);
3468 return;
3469 case LineBoxesPath:
3470 ASSERT(!m_simpleLineLayout);
3471 m_lineLayoutPath = UndeterminedPath;
3472 return;
3473 case SimpleLinesPath:
3474 // The simple line layout may have become invalid.
3475 m_simpleLineLayout = nullptr;
3476 setNeedsLayout();
3477 m_lineLayoutPath = UndeterminedPath;
3478 return;
3479 }
3480 ASSERT_NOT_REACHED();
3481}
3482
antti@apple.com940f5872013-10-24 20:31:11 +00003483void RenderBlockFlow::layoutSimpleLines(LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
3484{
3485 ASSERT(!m_lineBoxes.firstLineBox());
3486
antti@apple.comfea51992013-10-28 13:39:23 +00003487 m_simpleLineLayout = SimpleLineLayout::create(*this);
antti@apple.com940f5872013-10-24 20:31:11 +00003488
antti@apple.comfea51992013-10-28 13:39:23 +00003489 LayoutUnit lineLayoutHeight = SimpleLineLayout::computeFlowHeight(*this, *m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00003490 LayoutUnit lineLayoutTop = borderAndPaddingBefore();
3491
3492 repaintLogicalTop = lineLayoutTop;
3493 repaintLogicalBottom = lineLayoutTop + lineLayoutHeight;
3494
3495 setLogicalHeight(lineLayoutTop + lineLayoutHeight + borderAndPaddingAfter());
3496}
3497
3498void RenderBlockFlow::deleteLineBoxesBeforeSimpleLineLayout()
3499{
antti@apple.com42fb53d2013-10-25 02:33:11 +00003500 ASSERT(m_lineLayoutPath == SimpleLinesPath);
akling@apple.com31dd4f42013-10-30 22:27:59 +00003501 lineBoxes().deleteLineBoxes();
antti@apple.com940f5872013-10-24 20:31:11 +00003502 toRenderText(firstChild())->deleteLineBoxesBeforeSimpleLineLayout();
3503}
3504
3505void RenderBlockFlow::ensureLineBoxes()
3506{
antti@apple.com42fb53d2013-10-25 02:33:11 +00003507 m_lineLayoutPath = ForceLineBoxesPath;
antti@apple.comfea51992013-10-28 13:39:23 +00003508 if (!m_simpleLineLayout)
antti@apple.com940f5872013-10-24 20:31:11 +00003509 return;
antti@apple.comfea51992013-10-28 13:39:23 +00003510 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +00003511
3512#if !ASSERT_DISABLED
3513 LayoutUnit oldHeight = logicalHeight();
3514#endif
3515 bool didNeedLayout = needsLayout();
3516
3517 bool relayoutChildren = false;
3518 LayoutUnit repaintLogicalTop;
3519 LayoutUnit repaintLogicalBottom;
3520 layoutLineBoxes(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
3521
3522 updateLogicalHeight();
3523 ASSERT(didNeedLayout || logicalHeight() == oldHeight);
3524
3525 if (!didNeedLayout)
3526 clearNeedsLayout();
3527}
3528
weinig@apple.com611b9292013-10-20 22:57:54 +00003529#ifndef NDEBUG
zalan@apple.comfac337f2014-08-29 17:55:34 +00003530void RenderBlockFlow::showLineTreeAndMark(const InlineBox* markedBox, int depth) const
weinig@apple.com611b9292013-10-20 22:57:54 +00003531{
weinig@apple.com611b9292013-10-20 22:57:54 +00003532 for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox())
zalan@apple.comfac337f2014-08-29 17:55:34 +00003533 root->showLineTreeAndMark(markedBox, depth);
simon.fraser@apple.com3518b142014-09-03 21:18:05 +00003534
3535 if (auto simpleLineLayout = this->simpleLineLayout())
3536 SimpleLineLayout::showLineLayoutForFlow(*this, *simpleLineLayout, depth);
weinig@apple.com611b9292013-10-20 22:57:54 +00003537}
3538#endif
3539
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00003540RenderBlockFlow::RenderBlockFlowRareData& RenderBlockFlow::ensureRareBlockFlowData()
3541{
3542 if (hasRareBlockFlowData())
3543 return *m_rareBlockFlowData;
3544 materializeRareBlockFlowData();
3545 return *m_rareBlockFlowData;
3546}
3547
3548void RenderBlockFlow::materializeRareBlockFlowData()
3549{
3550 ASSERT(!hasRareBlockFlowData());
3551 m_rareBlockFlowData = std::make_unique<RenderBlockFlow::RenderBlockFlowRareData>(*this);
3552}
3553
aestes@apple.com6751d842014-01-12 02:51:25 +00003554#if ENABLE(IOS_TEXT_AUTOSIZING)
3555inline static bool isVisibleRenderText(RenderObject* renderer)
3556{
3557 if (!renderer->isText())
3558 return false;
3559 RenderText* renderText = toRenderText(renderer);
3560 return !renderText->linesBoundingBox().isEmpty() && !renderText->text()->containsOnlyWhitespace();
3561}
3562
3563inline static bool resizeTextPermitted(RenderObject* render)
3564{
3565 // We disallow resizing for text input fields and textarea to address <rdar://problem/5792987> and <rdar://problem/8021123>
3566 auto renderer = render->parent();
3567 while (renderer) {
3568 // Get the first non-shadow HTMLElement and see if it's an input.
3569 if (renderer->element() && renderer->element()->isHTMLElement() && !renderer->element()->isInShadowTree()) {
3570 const HTMLElement& element = toHTMLElement(*renderer->element());
3571 return !isHTMLInputElement(element) && !isHTMLTextAreaElement(element);
3572 }
3573 renderer = renderer->parent();
3574 }
3575 return true;
3576}
3577
antti@apple.com0b3dffe2014-03-24 16:30:52 +00003578int RenderBlockFlow::lineCountForTextAutosizing()
aestes@apple.com6751d842014-01-12 02:51:25 +00003579{
antti@apple.com0b3dffe2014-03-24 16:30:52 +00003580 if (style().visibility() != VISIBLE)
3581 return 0;
3582 if (childrenInline())
3583 return lineCount();
aestes@apple.com6751d842014-01-12 02:51:25 +00003584 // Only descend into list items.
3585 int count = 0;
antti@apple.com0b3dffe2014-03-24 16:30:52 +00003586 for (auto& listItem : childrenOfType<RenderListItem>(*this))
3587 count += listItem.lineCount();
aestes@apple.com6751d842014-01-12 02:51:25 +00003588 return count;
3589}
3590
3591static bool isNonBlocksOrNonFixedHeightListItems(const RenderObject* render)
3592{
3593 if (!render->isRenderBlock())
3594 return true;
3595 if (render->isListItem())
3596 return render->style().height().type() != Fixed;
3597 return false;
3598}
3599
3600// For now, we auto size single lines of text the same as multiple lines.
3601// We've been experimenting with low values for single lines of text.
3602static inline float oneLineTextMultiplier(float specifiedSize)
3603{
3604 return std::max((1.0f / log10f(specifiedSize) * 1.7f), 1.0f);
3605}
3606
3607static inline float textMultiplier(float specifiedSize)
3608{
3609 return std::max((1.0f / log10f(specifiedSize) * 1.95f), 1.0f);
3610}
3611
3612void RenderBlockFlow::adjustComputedFontSizes(float size, float visibleWidth)
3613{
3614 // Don't do any work if the block is smaller than the visible area.
3615 if (visibleWidth >= width())
3616 return;
3617
3618 unsigned lineCount;
3619 if (m_lineCountForTextAutosizing == NOT_SET) {
antti@apple.com0b3dffe2014-03-24 16:30:52 +00003620 int count = lineCountForTextAutosizing();
aestes@apple.com6751d842014-01-12 02:51:25 +00003621 if (!count)
3622 lineCount = NO_LINE;
3623 else if (count == 1)
3624 lineCount = ONE_LINE;
3625 else
3626 lineCount = MULTI_LINE;
3627 } else
3628 lineCount = m_lineCountForTextAutosizing;
3629
3630 ASSERT(lineCount != NOT_SET);
3631 if (lineCount == NO_LINE)
3632 return;
3633
3634 float actualWidth = m_widthForTextAutosizing != -1 ? static_cast<float>(m_widthForTextAutosizing) : static_cast<float>(width());
3635 float scale = visibleWidth / actualWidth;
3636 float minFontSize = roundf(size / scale);
3637
3638 for (RenderObject* descendent = traverseNext(this, isNonBlocksOrNonFixedHeightListItems); descendent; descendent = descendent->traverseNext(this, isNonBlocksOrNonFixedHeightListItems)) {
3639 if (isVisibleRenderText(descendent) && resizeTextPermitted(descendent)) {
3640 RenderText* text = toRenderText(descendent);
3641 RenderStyle& oldStyle = text->style();
3642 FontDescription fontDescription = oldStyle.fontDescription();
3643 float specifiedSize = fontDescription.specifiedSize();
3644 float scaledSize = roundf(specifiedSize * scale);
3645 if (scaledSize > 0 && scaledSize < minFontSize) {
3646 // Record the width of the block and the line count the first time we resize text and use it from then on for text resizing.
3647 // This makes text resizing consistent even if the block's width or line count changes (which can be caused by text resizing itself 5159915).
3648 if (m_lineCountForTextAutosizing == NOT_SET)
3649 m_lineCountForTextAutosizing = lineCount;
3650 if (m_widthForTextAutosizing == -1)
3651 m_widthForTextAutosizing = actualWidth;
3652
3653 float candidateNewSize = 0;
3654 float lineTextMultiplier = lineCount == ONE_LINE ? oneLineTextMultiplier(specifiedSize) : textMultiplier(specifiedSize);
3655 candidateNewSize = roundf(std::min(minFontSize, specifiedSize * lineTextMultiplier));
3656 if (candidateNewSize > specifiedSize && candidateNewSize != fontDescription.computedSize() && text->textNode() && oldStyle.textSizeAdjust().isAuto())
3657 document().addAutoSizingNode(text->textNode(), candidateNewSize);
3658 }
3659 }
3660 }
3661}
3662#endif // ENABLE(IOS_TEXT_AUTOSIZING)
3663
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003664RenderObject* RenderBlockFlow::layoutSpecialExcludedChild(bool relayoutChildren)
3665{
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003666 RenderMultiColumnFlowThread* flowThread = multiColumnFlowThread();
3667 if (!flowThread)
3668 return nullptr;
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003669
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003670 setLogicalTopForChild(*flowThread, borderAndPaddingBefore());
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003671
3672 if (relayoutChildren)
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003673 flowThread->setChildNeedsLayout(MarkOnlyThis);
3674
3675 if (flowThread->needsLayout()) {
3676 for (RenderMultiColumnSet* columnSet = flowThread->firstMultiColumnSet(); columnSet; columnSet = columnSet->nextSiblingMultiColumnSet())
3677 columnSet->prepareForLayout(!flowThread->inBalancingPass());
3678
3679 flowThread->invalidateRegions();
3680 flowThread->setNeedsHeightsRecalculation(true);
3681 flowThread->layout();
3682 } else {
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003683 // At the end of multicol layout, relayoutForPagination() is called unconditionally, but if
3684 // no children are to be laid out (e.g. fixed width with layout already being up-to-date),
3685 // we want to prevent it from doing any work, so that the column balancing machinery doesn't
3686 // kick in and trigger additional unnecessary layout passes. Actually, it's not just a good
3687 // idea in general to not waste time on balancing content that hasn't been re-laid out; we
3688 // are actually required to guarantee this. The calculation of implicit breaks needs to be
3689 // preceded by a proper layout pass, since it's layout that sets up content runs, and the
3690 // runs get deleted right after every pass.
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003691 flowThread->setNeedsHeightsRecalculation(false);
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003692 }
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003693 determineLogicalLeftPositionForChild(*flowThread);
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003694
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003695 return flowThread;
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003696}
3697
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003698void RenderBlockFlow::addChild(RenderObject* newChild, RenderObject* beforeChild)
3699{
3700 if (multiColumnFlowThread())
3701 return multiColumnFlowThread()->addChild(newChild, beforeChild);
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003702 if (beforeChild) {
3703 if (RenderFlowThread* containingFlowThread = flowThreadContainingBlock())
3704 beforeChild = containingFlowThread->resolveMovedChild(beforeChild);
3705 }
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003706 RenderBlock::addChild(newChild, beforeChild);
3707}
3708
stavila@adobe.com2dae4b62014-05-27 16:36:09 +00003709RenderObject* RenderBlockFlow::removeChild(RenderObject& oldChild)
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003710{
3711 if (!documentBeingDestroyed()) {
3712 RenderFlowThread* flowThread = multiColumnFlowThread();
3713 if (flowThread && flowThread != &oldChild)
3714 flowThread->flowThreadRelativeWillBeRemoved(&oldChild);
3715 }
stavila@adobe.com2dae4b62014-05-27 16:36:09 +00003716 return RenderBlock::removeChild(oldChild);
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003717}
3718
hyatt@apple.com73715ca2014-05-06 21:35:52 +00003719void RenderBlockFlow::checkForPaginationLogicalHeightChange(bool& relayoutChildren, LayoutUnit& pageLogicalHeight, bool& pageLogicalHeightChanged)
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003720{
hyatt@apple.com73715ca2014-05-06 21:35:52 +00003721 // If we don't use columns or flow threads, then bail.
3722 if (!isRenderFlowThread() && !multiColumnFlowThread())
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003723 return;
3724
3725 // We don't actually update any of the variables. We just subclassed to adjust our column height.
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003726 if (RenderMultiColumnFlowThread* flowThread = multiColumnFlowThread()) {
3727 LogicalExtentComputedValues computedValues;
3728 computeLogicalHeight(LayoutUnit(), logicalTop(), computedValues);
3729 LayoutUnit columnHeight = computedValues.m_extent - borderAndPaddingLogicalHeight() - scrollbarLogicalHeight();
hyatt@apple.comc9d96572014-04-21 20:20:27 +00003730 LayoutUnit oldHeightAvailable = flowThread->columnHeightAvailable();
hyatt@apple.comc1c39032014-04-15 23:25:58 +00003731 flowThread->setColumnHeightAvailable(std::max<LayoutUnit>(columnHeight, 0));
hyatt@apple.comc9d96572014-04-21 20:20:27 +00003732 if (oldHeightAvailable != flowThread->columnHeightAvailable())
3733 relayoutChildren = true;
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003734 } else if (isRenderFlowThread()) {
commit-queue@webkit.org3d0f60b2014-04-08 18:19:47 +00003735 RenderFlowThread* flowThread = toRenderFlowThread(this);
3736
3737 // FIXME: This is a hack to always make sure we have a page logical height, if said height
3738 // is known. The page logical height thing in LayoutState is meaningless for flow
3739 // thread-based pagination (page height isn't necessarily uniform throughout the flow
3740 // thread), but as long as it is used universally as a means to determine whether page
3741 // height is known or not, we need this. Page height is unknown when column balancing is
3742 // enabled and flow thread height is still unknown (i.e. during the first layout pass). When
3743 // it's unknown, we need to prevent the pagination code from assuming page breaks everywhere
3744 // and thereby eating every top margin. It should be trivial to clean up and get rid of this
3745 // hack once the old multicol implementation is gone.
3746 pageLogicalHeight = flowThread->isPageLogicalHeightKnown() ? LayoutUnit(1) : LayoutUnit(0);
3747
3748 pageLogicalHeightChanged = flowThread->pageLogicalSizeChanged();
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003749 }
3750}
3751
hyatt@apple.com73715ca2014-05-06 21:35:52 +00003752bool RenderBlockFlow::requiresColumns(int desiredColumnCount) const
3753{
3754 // If overflow-y is set to paged-x or paged-y on the body or html element, we'll handle the paginating
3755 // in the RenderView instead.
3756 bool isPaginated = (style().overflowY() == OPAGEDX || style().overflowY() == OPAGEDY) && !(isRoot() || isBody());
3757
3758 return firstChild() && (desiredColumnCount != 1 || !style().hasAutoColumnWidth() || !style().hasInlineColumnAxis() || isPaginated);
3759}
3760
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003761void RenderBlockFlow::setComputedColumnCountAndWidth(int count, LayoutUnit width)
3762{
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003763 bool destroyColumns = !requiresColumns(count);
3764 if (destroyColumns) {
3765 if (multiColumnFlowThread())
3766 destroyMultiColumnFlowThread();
3767 } else {
3768 if (!multiColumnFlowThread())
3769 createMultiColumnFlowThread();
3770 multiColumnFlowThread()->setColumnCountAndWidth(count, width);
hyatt@apple.com86919862014-01-27 16:27:45 +00003771 multiColumnFlowThread()->setProgressionIsInline(style().hasInlineColumnAxis());
3772 multiColumnFlowThread()->setProgressionIsReversed(style().columnProgression() == ReverseColumnProgression);
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003773 }
3774}
3775
hyatt@apple.com86919862014-01-27 16:27:45 +00003776void RenderBlockFlow::updateColumnProgressionFromStyle(RenderStyle* style)
3777{
hyatt@apple.com86919862014-01-27 16:27:45 +00003778 if (!multiColumnFlowThread())
3779 return;
3780
3781 bool needsLayout = false;
3782 bool oldProgressionIsInline = multiColumnFlowThread()->progressionIsInline();
3783 bool newProgressionIsInline = style->hasInlineColumnAxis();
3784 if (oldProgressionIsInline != newProgressionIsInline) {
3785 multiColumnFlowThread()->setProgressionIsInline(newProgressionIsInline);
3786 needsLayout = true;
3787 }
3788
3789 bool oldProgressionIsReversed = multiColumnFlowThread()->progressionIsReversed();
3790 bool newProgressionIsReversed = style->columnProgression() == ReverseColumnProgression;
3791 if (oldProgressionIsReversed != newProgressionIsReversed) {
3792 multiColumnFlowThread()->setProgressionIsReversed(newProgressionIsReversed);
3793 needsLayout = true;
3794 }
3795
3796 if (needsLayout)
3797 setNeedsLayoutAndPrefWidthsRecalc();
3798}
3799
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003800LayoutUnit RenderBlockFlow::computedColumnWidth() const
3801{
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003802 if (multiColumnFlowThread())
3803 return multiColumnFlowThread()->computedColumnWidth();
3804 return contentLogicalWidth();
3805}
3806
3807unsigned RenderBlockFlow::computedColumnCount() const
3808{
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003809 if (multiColumnFlowThread())
3810 return multiColumnFlowThread()->computedColumnCount();
3811
3812 return 1;
3813}
3814
hyatt@apple.com31a5daa2014-01-28 01:26:37 +00003815bool RenderBlockFlow::isTopLayoutOverflowAllowed() const
3816{
3817 bool hasTopOverflow = RenderBlock::isTopLayoutOverflowAllowed();
3818 if (!multiColumnFlowThread() || style().columnProgression() == NormalColumnProgression)
3819 return hasTopOverflow;
3820
3821 if (!(isHorizontalWritingMode() ^ !style().hasInlineColumnAxis()))
3822 hasTopOverflow = !hasTopOverflow;
3823
3824 return hasTopOverflow;
3825}
3826
3827bool RenderBlockFlow::isLeftLayoutOverflowAllowed() const
3828{
3829 bool hasLeftOverflow = RenderBlock::isLeftLayoutOverflowAllowed();
3830 if (!multiColumnFlowThread() || style().columnProgression() == NormalColumnProgression)
3831 return hasLeftOverflow;
3832
3833 if (isHorizontalWritingMode() ^ !style().hasInlineColumnAxis())
3834 hasLeftOverflow = !hasLeftOverflow;
3835
3836 return hasLeftOverflow;
3837}
3838
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00003839}
3840// namespace WebCore