blob: 9e9c9622d403f3492d896c4b99b346c538af127e [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"
bjonesbe@adobe.com24199752013-10-08 23:20:42 +000030#include "HitTestLocation.h"
weinig@apple.com611b9292013-10-20 22:57:54 +000031#include "InlineTextBox.h"
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +000032#include "LayoutRepainter.h"
33#include "RenderFlowThread.h"
akling@apple.comf3028052013-11-04 08:46:06 +000034#include "RenderIterator.h"
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +000035#include "RenderLayer.h"
hyatt@apple.comd4be3772014-01-24 19:55:33 +000036#include "RenderMultiColumnFlowThread.h"
37#include "RenderMultiColumnSet.h"
mihnea@adobe.combe79cf12013-10-17 09:02:19 +000038#include "RenderNamedFlowFragment.h"
antti@apple.com940f5872013-10-24 20:31:11 +000039#include "RenderText.h"
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +000040#include "RenderView.h"
antti@apple.com940f5872013-10-24 20:31:11 +000041#include "SimpleLineLayoutFunctions.h"
hyatt@apple.com3cd5c772013-09-27 18:22:50 +000042#include "VerticalPositionCache.h"
weinig@apple.com611b9292013-10-20 22:57:54 +000043#include "VisiblePosition.h"
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +000044
zoltan@webkit.org7b4eb622013-12-06 19:31:50 +000045#if ENABLE(CSS_SHAPES)
46#include "ShapeInsideInfo.h"
47#endif
48
aestes@apple.com6751d842014-01-12 02:51:25 +000049#if ENABLE(IOS_TEXT_AUTOSIZING)
50#include "HTMLElement.h"
51#endif
52
hyatt@apple.com5388e672013-09-06 20:54:47 +000053namespace WebCore {
54
bjonesbe@adobe.com24199752013-10-08 23:20:42 +000055bool RenderBlock::s_canPropagateFloatIntoSibling = false;
56
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000057struct SameSizeAsMarginInfo {
58 uint32_t bitfields : 16;
59 LayoutUnit margins[2];
60};
61
62COMPILE_ASSERT(sizeof(RenderBlockFlow::MarginValues) == sizeof(LayoutUnit[4]), MarginValues_should_stay_small);
akling@apple.com42e10632013-10-14 17:55:52 +000063COMPILE_ASSERT(sizeof(RenderBlockFlow::MarginInfo) == sizeof(SameSizeAsMarginInfo), MarginInfo_should_stay_small);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000064
65// Our MarginInfo state used when laying out block children.
weinig@apple.com12840dc2013-10-22 23:59:08 +000066RenderBlockFlow::MarginInfo::MarginInfo(RenderBlockFlow& block, LayoutUnit beforeBorderPadding, LayoutUnit afterBorderPadding)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000067 : m_atBeforeSideOfBlock(true)
68 , m_atAfterSideOfBlock(false)
69 , m_hasMarginBeforeQuirk(false)
70 , m_hasMarginAfterQuirk(false)
71 , m_determinedMarginBeforeQuirk(false)
72 , m_discardMargin(false)
73{
akling@apple.com827be9c2013-10-29 02:58:43 +000074 const RenderStyle& blockStyle = block.style();
weinig@apple.com12840dc2013-10-22 23:59:08 +000075 ASSERT(block.isRenderView() || block.parent());
76 m_canCollapseWithChildren = !block.isRenderView() && !block.isRoot() && !block.isOutOfFlowPositioned()
77 && !block.isFloating() && !block.isTableCell() && !block.hasOverflowClip() && !block.isInlineBlockOrInlineTable()
78 && !block.isRenderFlowThread() && !block.isWritingModeRoot() && !block.parent()->isFlexibleBox()
akling@apple.com827be9c2013-10-29 02:58:43 +000079 && blockStyle.hasAutoColumnCount() && blockStyle.hasAutoColumnWidth() && !blockStyle.columnSpan();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000080
akling@apple.com827be9c2013-10-29 02:58:43 +000081 m_canCollapseMarginBeforeWithChildren = m_canCollapseWithChildren && !beforeBorderPadding && blockStyle.marginBeforeCollapse() != MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000082
83 // If any height other than auto is specified in CSS, then we don't collapse our bottom
84 // margins with our children's margins. To do otherwise would be to risk odd visual
85 // effects when the children overflow out of the parent block and yet still collapse
86 // with it. We also don't collapse if we have any bottom border/padding.
87 m_canCollapseMarginAfterWithChildren = m_canCollapseWithChildren && !afterBorderPadding
akling@apple.com827be9c2013-10-29 02:58:43 +000088 && (blockStyle.logicalHeight().isAuto() && !blockStyle.logicalHeight().value()) && blockStyle.marginAfterCollapse() != MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000089
weinig@apple.com12840dc2013-10-22 23:59:08 +000090 m_quirkContainer = block.isTableCell() || block.isBody();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000091
weinig@apple.com12840dc2013-10-22 23:59:08 +000092 m_discardMargin = m_canCollapseMarginBeforeWithChildren && block.mustDiscardMarginBefore();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000093
weinig@apple.com12840dc2013-10-22 23:59:08 +000094 m_positiveMargin = (m_canCollapseMarginBeforeWithChildren && !block.mustDiscardMarginBefore()) ? block.maxPositiveMarginBefore() : LayoutUnit();
95 m_negativeMargin = (m_canCollapseMarginBeforeWithChildren && !block.mustDiscardMarginBefore()) ? block.maxNegativeMarginBefore() : LayoutUnit();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +000096}
97
akling@apple.com8f40c5b2013-10-27 22:54:07 +000098RenderBlockFlow::RenderBlockFlow(Element& element, PassRef<RenderStyle> style)
99 : RenderBlock(element, std::move(style), RenderBlockFlowFlag)
aestes@apple.com6751d842014-01-12 02:51:25 +0000100#if ENABLE(IOS_TEXT_AUTOSIZING)
101 , m_widthForTextAutosizing(-1)
102 , m_lineCountForTextAutosizing(NOT_SET)
103#endif
hyatt@apple.com5388e672013-09-06 20:54:47 +0000104{
weinig@apple.com611b9292013-10-20 22:57:54 +0000105 setChildrenInline(true);
akling@apple.com42e10632013-10-14 17:55:52 +0000106}
107
akling@apple.com8f40c5b2013-10-27 22:54:07 +0000108RenderBlockFlow::RenderBlockFlow(Document& document, PassRef<RenderStyle> style)
109 : RenderBlock(document, std::move(style), RenderBlockFlowFlag)
aestes@apple.com6751d842014-01-12 02:51:25 +0000110#if ENABLE(IOS_TEXT_AUTOSIZING)
111 , m_widthForTextAutosizing(-1)
112 , m_lineCountForTextAutosizing(NOT_SET)
113#endif
akling@apple.com42e10632013-10-14 17:55:52 +0000114{
weinig@apple.com611b9292013-10-20 22:57:54 +0000115 setChildrenInline(true);
hyatt@apple.com5388e672013-09-06 20:54:47 +0000116}
117
118RenderBlockFlow::~RenderBlockFlow()
119{
120}
121
hyatt@apple.com39746fd2014-01-24 22:52:41 +0000122void RenderBlockFlow::createMultiColumnFlowThread()
hyatt@apple.comd4be3772014-01-24 19:55:33 +0000123{
hyatt@apple.comd4be3772014-01-24 19:55:33 +0000124 RenderMultiColumnFlowThread* flowThread = new RenderMultiColumnFlowThread(document(), RenderStyle::createAnonymousStyleWithDisplay(&style(), BLOCK));
125 flowThread->initializeStyle();
hyatt@apple.com39746fd2014-01-24 22:52:41 +0000126 moveAllChildrenTo(flowThread, true);
hyatt@apple.comd4be3772014-01-24 19:55:33 +0000127 RenderBlock::addChild(flowThread);
128 setMultiColumnFlowThread(flowThread);
129}
130
hyatt@apple.com39746fd2014-01-24 22:52:41 +0000131void RenderBlockFlow::destroyMultiColumnFlowThread()
132{
133 // Get the flow thread out of our list.
134 multiColumnFlowThread()->removeFromParent();
135
136 // Destroy all the multicolumn sets.
137 destroyLeftoverChildren();
138
139 // Move all the children of the flow thread into our block.
140 multiColumnFlowThread()->moveAllChildrenTo(this, true);
141
142 // Now destroy the flow thread.
143 multiColumnFlowThread()->destroy();
144
145 // Clear the multi-column flow thread pointer.
146 setMultiColumnFlowThread(nullptr);
147}
148
mihnea@adobe.combe79cf12013-10-17 09:02:19 +0000149void RenderBlockFlow::insertedIntoTree()
150{
151 RenderBlock::insertedIntoTree();
152 createRenderNamedFlowFragmentIfNeeded();
153}
154
hyatt@apple.com3cd5c772013-09-27 18:22:50 +0000155void RenderBlockFlow::willBeDestroyed()
156{
weinig@apple.com611b9292013-10-20 22:57:54 +0000157 // Mark as being destroyed to avoid trouble with merges in removeChild().
158 m_beingDestroyed = true;
159
mihnea@adobe.combe79cf12013-10-17 09:02:19 +0000160 if (renderNamedFlowFragment())
161 setRenderNamedFlowFragment(0);
weinig@apple.com611b9292013-10-20 22:57:54 +0000162
163 if (!documentBeingDestroyed()) {
164 if (firstChild() && firstChild()->isRunIn())
weinig@apple.com12840dc2013-10-22 23:59:08 +0000165 moveRunInToOriginalPosition(*firstChild());
weinig@apple.com611b9292013-10-20 22:57:54 +0000166 }
167
168 // Make sure to destroy anonymous children first while they are still connected to the rest of the tree, so that they will
169 // properly dirty line boxes that they are removed from. Effects that do :before/:after only on hover could crash otherwise.
170 destroyLeftoverChildren();
171
172 // Destroy our continuation before anything other than anonymous children.
173 // The reason we don't destroy it before anonymous children is that they may
174 // have continuations of their own that are anonymous children of our continuation.
175 RenderBoxModelObject* continuation = this->continuation();
176 if (continuation) {
177 continuation->destroy();
178 setContinuation(0);
179 }
180
181 if (!documentBeingDestroyed()) {
akling@apple.comee3c8df2013-11-06 08:09:44 +0000182 if (firstRootBox()) {
weinig@apple.com611b9292013-10-20 22:57:54 +0000183 // We can't wait for RenderBox::destroy to clear the selection,
184 // because by then we will have nuked the line boxes.
185 // FIXME: The FrameSelection should be responsible for this when it
186 // is notified of DOM mutations.
187 if (isSelectionBorder())
188 view().clearSelection();
189
190 // If we are an anonymous block, then our line boxes might have children
191 // that will outlast this block. In the non-anonymous block case those
192 // children will be destroyed by the time we return from this function.
193 if (isAnonymousBlock()) {
akling@apple.comee3c8df2013-11-06 08:09:44 +0000194 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
weinig@apple.com611b9292013-10-20 22:57:54 +0000195 while (auto childBox = box->firstChild())
196 childBox->removeFromParent();
197 }
198 }
199 } else if (parent())
200 parent()->dirtyLinesFromChangedChild(this);
201 }
202
akling@apple.com31dd4f42013-10-30 22:27:59 +0000203 m_lineBoxes.deleteLineBoxes();
weinig@apple.com611b9292013-10-20 22:57:54 +0000204
205 removeFromDelayedUpdateScrollInfoSet();
206
207 // NOTE: This jumps down to RenderBox, bypassing RenderBlock since it would do duplicate work.
208 RenderBox::willBeDestroyed();
hyatt@apple.com3cd5c772013-09-27 18:22:50 +0000209}
210
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000211void RenderBlockFlow::clearFloats()
212{
213 if (m_floatingObjects)
214 m_floatingObjects->setHorizontalWritingMode(isHorizontalWritingMode());
215
216 HashSet<RenderBox*> oldIntrudingFloatSet;
217 if (!childrenInline() && m_floatingObjects) {
218 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +0000219 auto end = floatingObjectSet.end();
220 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
221 FloatingObject* floatingObject = it->get();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000222 if (!floatingObject->isDescendant())
darin@apple.com7cad7042013-09-24 05:53:55 +0000223 oldIntrudingFloatSet.add(&floatingObject->renderer());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000224 }
225 }
226
227 // Inline blocks are covered by the isReplaced() check in the avoidFloats method.
228 if (avoidsFloats() || isRoot() || isRenderView() || isFloatingOrOutOfFlowPositioned() || isTableCell()) {
mihnea@adobe.combe79cf12013-10-17 09:02:19 +0000229 if (m_floatingObjects)
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000230 m_floatingObjects->clear();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000231 if (!oldIntrudingFloatSet.isEmpty())
232 markAllDescendantsWithFloatsForLayout();
233 return;
234 }
235
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000236 RendererToFloatInfoMap floatMap;
237
238 if (m_floatingObjects) {
bjonesbe@adobe.com0434768a2013-09-16 22:01:38 +0000239 if (childrenInline())
240 m_floatingObjects->moveAllToFloatInfoMap(floatMap);
241 else
242 m_floatingObjects->clear();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000243 }
244
245 // We should not process floats if the parent node is not a RenderBlock. Otherwise, we will add
246 // floats in an invalid context. This will cause a crash arising from a bad cast on the parent.
247 // See <rdar://problem/8049753>, where float property is applied on a text node in a SVG.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000248 if (!parent() || !parent()->isRenderBlockFlow())
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000249 return;
250
251 // Attempt to locate a previous sibling with overhanging floats. We skip any elements that are
252 // out of flow (like floating/positioned elements), and we also skip over any objects that may have shifted
253 // to avoid floats.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000254 RenderBlockFlow* parentBlock = toRenderBlockFlow(parent());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000255 bool parentHasFloats = false;
256 RenderObject* prev = previousSibling();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000257 while (prev && (prev->isFloatingOrOutOfFlowPositioned() || !prev->isBox() || !prev->isRenderBlockFlow() || toRenderBlockFlow(prev)->avoidsFloats())) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000258 if (prev->isFloating())
259 parentHasFloats = true;
260 prev = prev->previousSibling();
261 }
262
robert@webkit.org97037ef2013-11-20 19:26:10 +0000263 // First add in floats from the parent. Self-collapsing blocks let their parent track any floats that intrude into
264 // them (as opposed to floats they contain themselves) so check for those here too.
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000265 LayoutUnit logicalTopOffset = logicalTop();
robert@webkit.org97037ef2013-11-20 19:26:10 +0000266 if (parentHasFloats || (parentBlock->lowestFloatLogicalBottom() > logicalTopOffset && prev && toRenderBlockFlow(prev)->isSelfCollapsingBlock()))
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000267 addIntrudingFloats(parentBlock, parentBlock->logicalLeftOffsetForContent(), logicalTopOffset);
268
269 LayoutUnit logicalLeftOffset = 0;
270 if (prev)
271 logicalTopOffset -= toRenderBox(prev)->logicalTop();
272 else {
273 prev = parentBlock;
274 logicalLeftOffset += parentBlock->logicalLeftOffsetForContent();
275 }
276
277 // 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 +0000278 RenderBlockFlow* block = toRenderBlockFlow(prev);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000279 if (block->m_floatingObjects && block->lowestFloatLogicalBottom() > logicalTopOffset)
280 addIntrudingFloats(block, logicalLeftOffset, logicalTopOffset);
281
282 if (childrenInline()) {
283 LayoutUnit changeLogicalTop = LayoutUnit::max();
284 LayoutUnit changeLogicalBottom = LayoutUnit::min();
285 if (m_floatingObjects) {
286 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +0000287 auto end = floatingObjectSet.end();
288 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +0000289 FloatingObject* floatingObject = it->get();
290 std::unique_ptr<FloatingObject> oldFloatingObject = floatMap.take(&floatingObject->renderer());
291 LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000292 if (oldFloatingObject) {
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +0000293 LayoutUnit oldLogicalBottom = logicalBottomForFloat(oldFloatingObject.get());
294 if (logicalWidthForFloat(floatingObject) != logicalWidthForFloat(oldFloatingObject.get()) || logicalLeftForFloat(floatingObject) != logicalLeftForFloat(oldFloatingObject.get())) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000295 changeLogicalTop = 0;
andersca@apple.com86298632013-11-10 19:32:33 +0000296 changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalBottom, oldLogicalBottom));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000297 } else {
298 if (logicalBottom != oldLogicalBottom) {
andersca@apple.com86298632013-11-10 19:32:33 +0000299 changeLogicalTop = std::min(changeLogicalTop, std::min(logicalBottom, oldLogicalBottom));
300 changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalBottom, oldLogicalBottom));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000301 }
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +0000302 LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
303 LayoutUnit oldLogicalTop = logicalTopForFloat(oldFloatingObject.get());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000304 if (logicalTop != oldLogicalTop) {
andersca@apple.com86298632013-11-10 19:32:33 +0000305 changeLogicalTop = std::min(changeLogicalTop, std::min(logicalTop, oldLogicalTop));
306 changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalTop, oldLogicalTop));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000307 }
308 }
309
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000310 if (oldFloatingObject->originatingLine() && !selfNeedsLayout()) {
311 ASSERT(&oldFloatingObject->originatingLine()->renderer() == this);
312 oldFloatingObject->originatingLine()->markDirty();
313 }
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000314 } else {
315 changeLogicalTop = 0;
andersca@apple.com86298632013-11-10 19:32:33 +0000316 changeLogicalBottom = std::max(changeLogicalBottom, logicalBottom);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000317 }
318 }
319 }
320
darin@apple.com7cad7042013-09-24 05:53:55 +0000321 auto end = floatMap.end();
322 for (auto it = floatMap.begin(); it != end; ++it) {
323 FloatingObject* floatingObject = it->value.get();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000324 if (!floatingObject->isDescendant()) {
325 changeLogicalTop = 0;
andersca@apple.com86298632013-11-10 19:32:33 +0000326 changeLogicalBottom = std::max(changeLogicalBottom, logicalBottomForFloat(floatingObject));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000327 }
328 }
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000329
330 markLinesDirtyInBlockRange(changeLogicalTop, changeLogicalBottom);
331 } else if (!oldIntrudingFloatSet.isEmpty()) {
332 // If there are previously intruding floats that no longer intrude, then children with floats
333 // should also get layout because they might need their floating object lists cleared.
334 if (m_floatingObjects->set().size() < oldIntrudingFloatSet.size())
335 markAllDescendantsWithFloatsForLayout();
336 else {
337 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +0000338 auto end = floatingObjectSet.end();
339 for (auto it = floatingObjectSet.begin(); it != end && !oldIntrudingFloatSet.isEmpty(); ++it)
340 oldIntrudingFloatSet.remove(&(*it)->renderer());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000341 if (!oldIntrudingFloatSet.isEmpty())
342 markAllDescendantsWithFloatsForLayout();
343 }
344 }
345}
346
347void RenderBlockFlow::layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight)
348{
349 ASSERT(needsLayout());
350
351 if (!relayoutChildren && simplifiedLayout())
352 return;
353
354 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
355
356 if (updateLogicalWidthAndColumnWidth())
357 relayoutChildren = true;
358
359 clearFloats();
360
361 LayoutUnit previousHeight = logicalHeight();
362 // FIXME: should this start out as borderAndPaddingLogicalHeight() + scrollbarLogicalHeight(),
363 // for consistency with other render classes?
364 setLogicalHeight(0);
365
366 bool pageLogicalHeightChanged = false;
367 bool hasSpecifiedPageLogicalHeight = false;
368 checkForPaginationLogicalHeightChange(pageLogicalHeight, pageLogicalHeightChanged, hasSpecifiedPageLogicalHeight);
369
akling@apple.com827be9c2013-10-29 02:58:43 +0000370 const RenderStyle& styleToUse = style();
weinig@apple.comf8d77f32013-11-18 01:12:17 +0000371 LayoutStateMaintainer statePusher(view(), *this, locationOffset(), hasColumns() || hasTransform() || hasReflection() || styleToUse.isFlippedBlocksWritingMode(), pageLogicalHeight, pageLogicalHeightChanged, columnInfo());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000372
abucur@adobe.com0e81bc72013-10-22 14:50:37 +0000373 prepareShapesAndPaginationBeforeBlockLayout(relayoutChildren);
374 if (!relayoutChildren)
375 relayoutChildren = namedFlowFragmentNeedsUpdate();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000376
377 // We use four values, maxTopPos, maxTopNeg, maxBottomPos, and maxBottomNeg, to track
378 // our current maximal positive and negative margins. These values are used when we
379 // are collapsed with adjacent blocks, so for example, if you have block A and B
380 // collapsing together, then you'd take the maximal positive margin from both A and B
381 // and subtract it from the maximal negative margin from both A and B to get the
382 // true collapsed margin. This algorithm is recursive, so when we finish layout()
383 // our block knows its current maximal positive/negative values.
384 //
385 // Start out by setting our margin values to our current margins. Table cells have
386 // no margins, so we don't fill in the values for table cells.
387 bool isCell = isTableCell();
388 if (!isCell) {
389 initMaxMarginValues();
390
akling@apple.com827be9c2013-10-29 02:58:43 +0000391 setHasMarginBeforeQuirk(styleToUse.hasMarginBeforeQuirk());
392 setHasMarginAfterQuirk(styleToUse.hasMarginAfterQuirk());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000393 setPaginationStrut(0);
394 }
395
396 LayoutUnit repaintLogicalTop = 0;
397 LayoutUnit repaintLogicalBottom = 0;
398 LayoutUnit maxFloatLogicalBottom = 0;
399 if (!firstChild() && !isAnonymousBlock())
400 setChildrenInline(true);
401 if (childrenInline())
402 layoutInlineChildren(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
403 else
404 layoutBlockChildren(relayoutChildren, maxFloatLogicalBottom);
405
406 // Expand our intrinsic height to encompass floats.
407 LayoutUnit toAdd = borderAndPaddingAfter() + scrollbarLogicalHeight();
408 if (lowestFloatLogicalBottom() > (logicalHeight() - toAdd) && expandsToEncloseOverhangingFloats())
409 setLogicalHeight(lowestFloatLogicalBottom() + toAdd);
410
411 if (relayoutForPagination(hasSpecifiedPageLogicalHeight, pageLogicalHeight, statePusher) || relayoutToAvoidWidows(statePusher)) {
412 ASSERT(!shouldBreakAtLineToAvoidWidow());
413 return;
414 }
415
416 // Calculate our new height.
417 LayoutUnit oldHeight = logicalHeight();
418 LayoutUnit oldClientAfterEdge = clientLogicalBottom();
419
420 // Before updating the final size of the flow thread make sure a forced break is applied after the content.
421 // This ensures the size information is correctly computed for the last auto-height region receiving content.
422 if (isRenderFlowThread())
423 toRenderFlowThread(this)->applyBreakAfterContent(oldClientAfterEdge);
424
425 updateLogicalHeight();
426 LayoutUnit newHeight = logicalHeight();
427 if (oldHeight != newHeight) {
428 if (oldHeight > newHeight && maxFloatLogicalBottom > newHeight && !childrenInline()) {
429 // 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 +0000430 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
431 if (blockFlow.isFloatingOrOutOfFlowPositioned())
432 continue;
433 if (blockFlow.lowestFloatLogicalBottom() + blockFlow.logicalTop() > newHeight)
434 addOverhangingFloats(blockFlow, false);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000435 }
436 }
437 }
438
439 bool heightChanged = (previousHeight != newHeight);
440 if (heightChanged)
441 relayoutChildren = true;
442
443 layoutPositionedObjects(relayoutChildren || isRoot());
444
445 updateShapesAfterBlockLayout(heightChanged);
446
447 // Add overflow from children (unless we're multi-column, since in that case all our child overflow is clipped anyway).
448 computeOverflow(oldClientAfterEdge);
449
450 statePusher.pop();
451
452 fitBorderToLinesIfNeeded();
453
454 if (view().layoutState()->m_pageLogicalHeight)
455 setPageLogicalOffset(view().layoutState()->pageLogicalOffset(this, logicalTop()));
456
457 updateLayerTransform();
458
459 // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
460 // we overflow or not.
461 updateScrollInfoAfterLayout();
462
463 // FIXME: This repaint logic should be moved into a separate helper function!
464 // Repaint with our new bounds if they are different from our old bounds.
465 bool didFullRepaint = repainter.repaintAfterLayout();
akling@apple.com827be9c2013-10-29 02:58:43 +0000466 if (!didFullRepaint && repaintLogicalTop != repaintLogicalBottom && (styleToUse.visibility() == VISIBLE || enclosingLayer()->hasVisibleContent())) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000467 // FIXME: We could tighten up the left and right invalidation points if we let layoutInlineChildren fill them in based off the particular lines
468 // it had to lay out. We wouldn't need the hasOverflowClip() hack in that case either.
469 LayoutUnit repaintLogicalLeft = logicalLeftVisualOverflow();
470 LayoutUnit repaintLogicalRight = logicalRightVisualOverflow();
471 if (hasOverflowClip()) {
472 // 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.
473 // 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.
474 // layoutInlineChildren should be patched to compute the entire repaint rect.
andersca@apple.com86298632013-11-10 19:32:33 +0000475 repaintLogicalLeft = std::min(repaintLogicalLeft, logicalLeftLayoutOverflow());
476 repaintLogicalRight = std::max(repaintLogicalRight, logicalRightLayoutOverflow());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000477 }
478
479 LayoutRect repaintRect;
480 if (isHorizontalWritingMode())
481 repaintRect = LayoutRect(repaintLogicalLeft, repaintLogicalTop, repaintLogicalRight - repaintLogicalLeft, repaintLogicalBottom - repaintLogicalTop);
482 else
483 repaintRect = LayoutRect(repaintLogicalTop, repaintLogicalLeft, repaintLogicalBottom - repaintLogicalTop, repaintLogicalRight - repaintLogicalLeft);
484
485 // The repaint rect may be split across columns, in which case adjustRectForColumns() will return the union.
486 adjustRectForColumns(repaintRect);
487
488 repaintRect.inflate(maximalOutlineSize(PaintPhaseOutline));
489
490 if (hasOverflowClip()) {
491 // Adjust repaint rect for scroll offset
492 repaintRect.move(-scrolledContentOffset());
493
494 // Don't allow this rect to spill out of our overflow box.
495 repaintRect.intersect(LayoutRect(LayoutPoint(), size()));
496 }
497
498 // Make sure the rect is still non-empty after intersecting for overflow above
499 if (!repaintRect.isEmpty()) {
500 repaintRectangle(repaintRect); // We need to do a partial repaint of our content.
501 if (hasReflection())
502 repaintRectangle(reflectedRect(repaintRect));
503 }
504 }
505
antti@apple.comca2a8ff2013-10-04 04:04:35 +0000506 clearNeedsLayout();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000507}
508
509void RenderBlockFlow::layoutBlockChildren(bool relayoutChildren, LayoutUnit& maxFloatLogicalBottom)
510{
511 dirtyForLayoutFromPercentageHeightDescendants();
512
513 LayoutUnit beforeEdge = borderAndPaddingBefore();
514 LayoutUnit afterEdge = borderAndPaddingAfter() + scrollbarLogicalHeight();
515
516 setLogicalHeight(beforeEdge);
517
518 // Lay out our hypothetical grid line as though it occurs at the top of the block.
519 if (view().layoutState()->lineGrid() == this)
520 layoutLineGridBox();
521
522 // The margin struct caches all our current margin collapsing state.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000523 MarginInfo marginInfo(*this, beforeEdge, afterEdge);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000524
525 // Fieldsets need to find their legend and position it inside the border of the object.
526 // The legend then gets skipped during normal layout. The same is true for ruby text.
527 // It doesn't get included in the normal layout process but is instead skipped.
528 RenderObject* childToExclude = layoutSpecialExcludedChild(relayoutChildren);
529
530 LayoutUnit previousFloatLogicalBottom = 0;
531 maxFloatLogicalBottom = 0;
532
533 RenderBox* next = firstChildBox();
534
535 while (next) {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000536 RenderBox& child = *next;
537 next = child.nextSiblingBox();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000538
weinig@apple.com12840dc2013-10-22 23:59:08 +0000539 if (childToExclude == &child)
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000540 continue; // Skip this child, since it will be positioned by the specialized subclass (fieldsets and ruby runs).
541
542 updateBlockChildDirtyBitsBeforeLayout(relayoutChildren, child);
543
weinig@apple.com12840dc2013-10-22 23:59:08 +0000544 if (child.isOutOfFlowPositioned()) {
545 child.containingBlock()->insertPositionedObject(child);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000546 adjustPositionedBlock(child, marginInfo);
547 continue;
548 }
weinig@apple.com12840dc2013-10-22 23:59:08 +0000549 if (child.isFloating()) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000550 insertFloatingObject(child);
551 adjustFloatingBlock(marginInfo);
552 continue;
553 }
554
555 // Lay out the child.
556 layoutBlockChild(child, marginInfo, previousFloatLogicalBottom, maxFloatLogicalBottom);
557 }
558
559 // Now do the handling of the bottom of the block, adding in our bottom border/padding and
560 // determining the correct collapsed bottom margin information.
561 handleAfterSideOfBlock(beforeEdge, afterEdge, marginInfo);
562}
563
antti@apple.com940f5872013-10-24 20:31:11 +0000564void RenderBlockFlow::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
565{
antti@apple.com42fb53d2013-10-25 02:33:11 +0000566 if (m_lineLayoutPath == UndeterminedPath)
567 m_lineLayoutPath = SimpleLineLayout::canUseFor(*this) ? SimpleLinesPath : LineBoxesPath;
568
569 if (m_lineLayoutPath == SimpleLinesPath) {
antti@apple.com940f5872013-10-24 20:31:11 +0000570 deleteLineBoxesBeforeSimpleLineLayout();
571 layoutSimpleLines(repaintLogicalTop, repaintLogicalBottom);
572 return;
573 }
574
antti@apple.comfea51992013-10-28 13:39:23 +0000575 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +0000576 layoutLineBoxes(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
577}
578
weinig@apple.com12840dc2013-10-22 23:59:08 +0000579void RenderBlockFlow::layoutBlockChild(RenderBox& child, MarginInfo& marginInfo, LayoutUnit& previousFloatLogicalBottom, LayoutUnit& maxFloatLogicalBottom)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000580{
581 LayoutUnit oldPosMarginBefore = maxPositiveMarginBefore();
582 LayoutUnit oldNegMarginBefore = maxNegativeMarginBefore();
583
584 // The child is a normal flow object. Compute the margins we will use for collapsing now.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000585 child.computeAndSetBlockDirectionMargins(this);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000586
587 // Try to guess our correct logical top position. In most cases this guess will
588 // be correct. Only if we're wrong (when we compute the real logical top position)
589 // will we have to potentially relayout.
590 LayoutUnit estimateWithoutPagination;
591 LayoutUnit logicalTopEstimate = estimateLogicalTopPosition(child, marginInfo, estimateWithoutPagination);
592
593 // 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 +0000594 LayoutRect oldRect = child.frameRect();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000595 LayoutUnit oldLogicalTop = logicalTopForChild(child);
596
597#if !ASSERT_DISABLED
598 LayoutSize oldLayoutDelta = view().layoutDelta();
599#endif
600 // Go ahead and position the child as though it didn't collapse with the top.
601 setLogicalTopForChild(child, logicalTopEstimate, ApplyLayoutDelta);
602 estimateRegionRangeForBoxChild(child);
603
weinig@apple.com12840dc2013-10-22 23:59:08 +0000604 RenderBlockFlow* childBlockFlow = child.isRenderBlockFlow() ? toRenderBlockFlow(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000605 bool markDescendantsWithFloats = false;
weinig@apple.com12840dc2013-10-22 23:59:08 +0000606 if (logicalTopEstimate != oldLogicalTop && !child.avoidsFloats() && childBlockFlow && childBlockFlow->containsFloats())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000607 markDescendantsWithFloats = true;
608#if ENABLE(SUBPIXEL_LAYOUT)
609 else if (UNLIKELY(logicalTopEstimate.mightBeSaturated()))
610 // logicalTopEstimate, returned by estimateLogicalTopPosition, might be saturated for
611 // very large elements. If it does the comparison with oldLogicalTop might yield a
612 // false negative as adding and removing margins, borders etc from a saturated number
613 // might yield incorrect results. If this is the case always mark for layout.
614 markDescendantsWithFloats = true;
615#endif
weinig@apple.com12840dc2013-10-22 23:59:08 +0000616 else if (!child.avoidsFloats() || child.shrinkToAvoidFloats()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000617 // If an element might be affected by the presence of floats, then always mark it for
618 // layout.
andersca@apple.com86298632013-11-10 19:32:33 +0000619 LayoutUnit fb = std::max(previousFloatLogicalBottom, lowestFloatLogicalBottom());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000620 if (fb > logicalTopEstimate)
621 markDescendantsWithFloats = true;
622 }
623
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000624 if (childBlockFlow) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000625 if (markDescendantsWithFloats)
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000626 childBlockFlow->markAllDescendantsWithFloatsForLayout();
weinig@apple.com12840dc2013-10-22 23:59:08 +0000627 if (!child.isWritingModeRoot())
andersca@apple.com86298632013-11-10 19:32:33 +0000628 previousFloatLogicalBottom = std::max(previousFloatLogicalBottom, oldLogicalTop + childBlockFlow->lowestFloatLogicalBottom());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000629 }
630
weinig@apple.com12840dc2013-10-22 23:59:08 +0000631 if (!child.needsLayout())
632 child.markForPaginationRelayoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000633
weinig@apple.com12840dc2013-10-22 23:59:08 +0000634 bool childHadLayout = child.everHadLayout();
635 bool childNeededLayout = child.needsLayout();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000636 if (childNeededLayout)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000637 child.layout();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000638
639 // Cache if we are at the top of the block right now.
640 bool atBeforeSideOfBlock = marginInfo.atBeforeSideOfBlock();
641
642 // Now determine the correct ypos based off examination of collapsing margin
643 // values.
644 LayoutUnit logicalTopBeforeClear = collapseMargins(child, marginInfo);
645
646 // Now check for clear.
647 LayoutUnit logicalTopAfterClear = clearFloatsIfNeeded(child, marginInfo, oldPosMarginBefore, oldNegMarginBefore, logicalTopBeforeClear);
648
649 bool paginated = view().layoutState()->isPaginated();
650 if (paginated)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000651 logicalTopAfterClear = adjustBlockChildForPagination(logicalTopAfterClear, estimateWithoutPagination, child, atBeforeSideOfBlock && logicalTopBeforeClear == logicalTopAfterClear);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000652
653 setLogicalTopForChild(child, logicalTopAfterClear, ApplyLayoutDelta);
654
655 // Now we have a final top position. See if it really does end up being different from our estimate.
656 // clearFloatsIfNeeded can also mark the child as needing a layout even though we didn't move. This happens
657 // when collapseMargins dynamically adds overhanging floats because of a child with negative margins.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000658 if (logicalTopAfterClear != logicalTopEstimate || child.needsLayout() || (paginated && childBlockFlow && childBlockFlow->shouldBreakAtLineToAvoidWidow())) {
659 if (child.shrinkToAvoidFloats()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000660 // The child's width depends on the line width.
661 // When the child shifts to clear an item, its width can
662 // change (because it has more available line width).
663 // So go ahead and mark the item as dirty.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000664 child.setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000665 }
666
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000667 if (childBlockFlow) {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000668 if (!child.avoidsFloats() && childBlockFlow->containsFloats())
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000669 childBlockFlow->markAllDescendantsWithFloatsForLayout();
weinig@apple.com12840dc2013-10-22 23:59:08 +0000670 if (!child.needsLayout())
671 child.markForPaginationRelayoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000672 }
673
674 // Our guess was wrong. Make the child lay itself out again.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000675 child.layoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000676 }
677
678 if (updateRegionRangeForBoxChild(child)) {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000679 child.setNeedsLayout(MarkOnlyThis);
680 child.layoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000681 }
682
683 // We are no longer at the top of the block if we encounter a non-empty child.
684 // 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 +0000685 if (marginInfo.atBeforeSideOfBlock() && !child.isSelfCollapsingBlock())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000686 marginInfo.setAtBeforeSideOfBlock(false);
687
688 // Now place the child in the correct left position
689 determineLogicalLeftPositionForChild(child, ApplyLayoutDelta);
690
weinig@apple.com12840dc2013-10-22 23:59:08 +0000691 LayoutSize childOffset = child.location() - oldRect.location();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000692#if ENABLE(CSS_SHAPES)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000693 relayoutShapeDescendantIfMoved(child.isRenderBlock() ? toRenderBlock(&child) : nullptr, childOffset);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000694#endif
695
696 // Update our height now that the child has been placed in the correct position.
697 setLogicalHeight(logicalHeight() + logicalHeightForChild(child));
698 if (mustSeparateMarginAfterForChild(child)) {
699 setLogicalHeight(logicalHeight() + marginAfterForChild(child));
700 marginInfo.clearMargin();
701 }
702 // If the child has overhanging floats that intrude into following siblings (or possibly out
703 // of this block), then the parent gets notified of the floats now.
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000704 if (childBlockFlow && childBlockFlow->containsFloats())
andersca@apple.com86298632013-11-10 19:32:33 +0000705 maxFloatLogicalBottom = std::max(maxFloatLogicalBottom, addOverhangingFloats(*childBlockFlow, !childNeededLayout));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000706
707 if (childOffset.width() || childOffset.height()) {
708 view().addLayoutDelta(childOffset);
709
710 // If the child moved, we have to repaint it as well as any floating/positioned
711 // descendants. An exception is if we need a layout. In this case, we know we're going to
712 // repaint ourselves (and the child) anyway.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000713 if (childHadLayout && !selfNeedsLayout() && child.checkForRepaintDuringLayout())
714 child.repaintDuringLayoutIfMoved(oldRect);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000715 }
716
weinig@apple.com12840dc2013-10-22 23:59:08 +0000717 if (!childHadLayout && child.checkForRepaintDuringLayout()) {
718 child.repaint();
719 child.repaintOverhangingFloats(true);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000720 }
721
722 if (paginated) {
723 // Check for an after page/column break.
724 LayoutUnit newHeight = applyAfterBreak(child, logicalHeight(), marginInfo);
725 if (newHeight != height())
726 setLogicalHeight(newHeight);
727 }
728
729 ASSERT(view().layoutDeltaMatches(oldLayoutDelta));
730}
731
weinig@apple.com12840dc2013-10-22 23:59:08 +0000732void RenderBlockFlow::adjustPositionedBlock(RenderBox& child, const MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000733{
734 bool isHorizontal = isHorizontalWritingMode();
akling@apple.com827be9c2013-10-29 02:58:43 +0000735 bool hasStaticBlockPosition = child.style().hasStaticBlockPosition(isHorizontal);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000736
737 LayoutUnit logicalTop = logicalHeight();
738 updateStaticInlinePositionForChild(child, logicalTop);
739
740 if (!marginInfo.canCollapseWithMarginBefore()) {
741 // Positioned blocks don't collapse margins, so add the margin provided by
742 // the container now. The child's own margin is added later when calculating its logical top.
743 LayoutUnit collapsedBeforePos = marginInfo.positiveMargin();
744 LayoutUnit collapsedBeforeNeg = marginInfo.negativeMargin();
745 logicalTop += collapsedBeforePos - collapsedBeforeNeg;
746 }
747
weinig@apple.com12840dc2013-10-22 23:59:08 +0000748 RenderLayer* childLayer = child.layer();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000749 if (childLayer->staticBlockPosition() != logicalTop) {
750 childLayer->setStaticBlockPosition(logicalTop);
751 if (hasStaticBlockPosition)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000752 child.setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000753 }
754}
755
robert@webkit.org97037ef2013-11-20 19:26:10 +0000756LayoutUnit RenderBlockFlow::marginOffsetForSelfCollapsingBlock()
757{
758 ASSERT(isSelfCollapsingBlock());
759 RenderBlockFlow* parentBlock = toRenderBlockFlow(parent());
760 if (parentBlock && style().clear() && parentBlock->getClearDelta(*this, logicalHeight()))
761 return marginValuesForChild(*this).positiveMarginBefore();
762 return LayoutUnit();
763}
764
hyatt@apple.com31a5daa2014-01-28 01:26:37 +0000765void RenderBlockFlow::determineLogicalLeftPositionForChild(RenderBox& child, ApplyLayoutDeltaMode applyDelta)
766{
767 LayoutUnit startPosition = borderStart() + paddingStart();
768 if (style().shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
769 startPosition -= verticalScrollbarWidth();
770 LayoutUnit totalAvailableLogicalWidth = borderAndPaddingLogicalWidth() + availableLogicalWidth();
771
772 // Add in our start margin.
773 LayoutUnit childMarginStart = marginStartForChild(child);
774 LayoutUnit newPosition = startPosition + childMarginStart;
775
776 // Some objects (e.g., tables, horizontal rules, overflow:auto blocks) avoid floats. They need
777 // to shift over as necessary to dodge any floats that might get in the way.
778 if (child.avoidsFloats() && containsFloats() && !flowThreadContainingBlock())
779 newPosition += computeStartPositionDeltaForChildAvoidingFloats(child, marginStartForChild(child));
780
781 setLogicalLeftForChild(child, style().isLeftToRightDirection() ? newPosition : totalAvailableLogicalWidth - newPosition - logicalWidthForChild(child), applyDelta);
782}
robert@webkit.org97037ef2013-11-20 19:26:10 +0000783
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000784void RenderBlockFlow::adjustFloatingBlock(const MarginInfo& marginInfo)
785{
786 // The float should be positioned taking into account the bottom margin
787 // of the previous flow. We add that margin into the height, get the
788 // float positioned properly, and then subtract the margin out of the
789 // height again. In the case of self-collapsing blocks, we always just
790 // use the top margins, since the self-collapsing block collapsed its
791 // own bottom margin into its top margin.
792 //
793 // Note also that the previous flow may collapse its margin into the top of
794 // our block. If this is the case, then we do not add the margin in to our
795 // height when computing the position of the float. This condition can be tested
796 // for by simply calling canCollapseWithMarginBefore. See
797 // http://www.hixie.ch/tests/adhoc/css/box/block/margin-collapse/046.html for
798 // an example of this scenario.
799 LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
800 setLogicalHeight(logicalHeight() + marginOffset);
801 positionNewFloats();
802 setLogicalHeight(logicalHeight() - marginOffset);
803}
804
weinig@apple.com12840dc2013-10-22 23:59:08 +0000805void RenderBlockFlow::updateStaticInlinePositionForChild(RenderBox& child, LayoutUnit logicalTop)
806{
akling@apple.com827be9c2013-10-29 02:58:43 +0000807 if (child.style().isOriginalDisplayInlineType())
weinig@apple.com12840dc2013-10-22 23:59:08 +0000808 setStaticInlinePositionForChild(child, logicalTop, startAlignedOffsetForLine(logicalTop, false));
809 else
810 setStaticInlinePositionForChild(child, logicalTop, startOffsetForContent(logicalTop));
811}
812
813void RenderBlockFlow::setStaticInlinePositionForChild(RenderBox& child, LayoutUnit blockOffset, LayoutUnit inlinePosition)
814{
815 if (flowThreadContainingBlock()) {
816 // Shift the inline position to exclude the region offset.
817 inlinePosition += startOffsetForContent() - startOffsetForContent(blockOffset);
818 }
819 child.layer()->setStaticInlinePosition(inlinePosition);
820}
821
822RenderBlockFlow::MarginValues RenderBlockFlow::marginValuesForChild(RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000823{
824 LayoutUnit childBeforePositive = 0;
825 LayoutUnit childBeforeNegative = 0;
826 LayoutUnit childAfterPositive = 0;
827 LayoutUnit childAfterNegative = 0;
828
829 LayoutUnit beforeMargin = 0;
830 LayoutUnit afterMargin = 0;
831
weinig@apple.com12840dc2013-10-22 23:59:08 +0000832 RenderBlockFlow* childRenderBlock = child.isRenderBlockFlow() ? toRenderBlockFlow(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000833
834 // If the child has the same directionality as we do, then we can just return its
835 // margins in the same direction.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000836 if (!child.isWritingModeRoot()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000837 if (childRenderBlock) {
838 childBeforePositive = childRenderBlock->maxPositiveMarginBefore();
839 childBeforeNegative = childRenderBlock->maxNegativeMarginBefore();
840 childAfterPositive = childRenderBlock->maxPositiveMarginAfter();
841 childAfterNegative = childRenderBlock->maxNegativeMarginAfter();
842 } else {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000843 beforeMargin = child.marginBefore();
844 afterMargin = child.marginAfter();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000845 }
weinig@apple.com12840dc2013-10-22 23:59:08 +0000846 } else if (child.isHorizontalWritingMode() == isHorizontalWritingMode()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000847 // The child has a different directionality. If the child is parallel, then it's just
848 // flipped relative to us. We can use the margins for the opposite edges.
849 if (childRenderBlock) {
850 childBeforePositive = childRenderBlock->maxPositiveMarginAfter();
851 childBeforeNegative = childRenderBlock->maxNegativeMarginAfter();
852 childAfterPositive = childRenderBlock->maxPositiveMarginBefore();
853 childAfterNegative = childRenderBlock->maxNegativeMarginBefore();
854 } else {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000855 beforeMargin = child.marginAfter();
856 afterMargin = child.marginBefore();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000857 }
858 } else {
859 // The child is perpendicular to us, which means its margins don't collapse but are on the
860 // "logical left/right" sides of the child box. We can just return the raw margin in this case.
861 beforeMargin = marginBeforeForChild(child);
862 afterMargin = marginAfterForChild(child);
863 }
864
865 // Resolve uncollapsing margins into their positive/negative buckets.
866 if (beforeMargin) {
867 if (beforeMargin > 0)
868 childBeforePositive = beforeMargin;
869 else
870 childBeforeNegative = -beforeMargin;
871 }
872 if (afterMargin) {
873 if (afterMargin > 0)
874 childAfterPositive = afterMargin;
875 else
876 childAfterNegative = -afterMargin;
877 }
878
879 return MarginValues(childBeforePositive, childBeforeNegative, childAfterPositive, childAfterNegative);
880}
881
weinig@apple.com12840dc2013-10-22 23:59:08 +0000882LayoutUnit RenderBlockFlow::collapseMargins(RenderBox& child, MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000883{
884 bool childDiscardMarginBefore = mustDiscardMarginBeforeForChild(child);
885 bool childDiscardMarginAfter = mustDiscardMarginAfterForChild(child);
weinig@apple.com12840dc2013-10-22 23:59:08 +0000886 bool childIsSelfCollapsing = child.isSelfCollapsingBlock();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000887
888 // The child discards the before margin when the the after margin has discard in the case of a self collapsing block.
889 childDiscardMarginBefore = childDiscardMarginBefore || (childDiscardMarginAfter && childIsSelfCollapsing);
890
891 // Get the four margin values for the child and cache them.
892 const MarginValues childMargins = marginValuesForChild(child);
893
894 // Get our max pos and neg top margins.
895 LayoutUnit posTop = childMargins.positiveMarginBefore();
896 LayoutUnit negTop = childMargins.negativeMarginBefore();
897
898 // For self-collapsing blocks, collapse our bottom margins into our
899 // top to get new posTop and negTop values.
900 if (childIsSelfCollapsing) {
andersca@apple.com86298632013-11-10 19:32:33 +0000901 posTop = std::max(posTop, childMargins.positiveMarginAfter());
902 negTop = std::max(negTop, childMargins.negativeMarginAfter());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000903 }
904
905 // See if the top margin is quirky. We only care if this child has
906 // margins that will collapse with us.
907 bool topQuirk = hasMarginBeforeQuirk(child);
908
909 if (marginInfo.canCollapseWithMarginBefore()) {
910 if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
911 // This child is collapsing with the top of the
912 // block. If it has larger margin values, then we need to update
913 // our own maximal values.
914 if (!document().inQuirksMode() || !marginInfo.quirkContainer() || !topQuirk)
andersca@apple.com86298632013-11-10 19:32:33 +0000915 setMaxMarginBeforeValues(std::max(posTop, maxPositiveMarginBefore()), std::max(negTop, maxNegativeMarginBefore()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000916
917 // The minute any of the margins involved isn't a quirk, don't
918 // collapse it away, even if the margin is smaller (www.webreference.com
919 // has an example of this, a <dt> with 0.8em author-specified inside
920 // a <dl> inside a <td>.
921 if (!marginInfo.determinedMarginBeforeQuirk() && !topQuirk && (posTop - negTop)) {
922 setHasMarginBeforeQuirk(false);
923 marginInfo.setDeterminedMarginBeforeQuirk(true);
924 }
925
926 if (!marginInfo.determinedMarginBeforeQuirk() && topQuirk && !marginBefore())
927 // We have no top margin and our top child has a quirky margin.
928 // We will pick up this quirky margin and pass it through.
929 // This deals with the <td><div><p> case.
930 // Don't do this for a block that split two inlines though. You do
931 // still apply margins in this case.
932 setHasMarginBeforeQuirk(true);
933 } else
934 // The before margin of the container will also discard all the margins it is collapsing with.
935 setMustDiscardMarginBefore();
936 }
937
938 // Once we find a child with discardMarginBefore all the margins collapsing with us must also discard.
939 if (childDiscardMarginBefore) {
940 marginInfo.setDiscardMargin(true);
941 marginInfo.clearMargin();
942 }
943
944 if (marginInfo.quirkContainer() && marginInfo.atBeforeSideOfBlock() && (posTop - negTop))
945 marginInfo.setHasMarginBeforeQuirk(topQuirk);
946
947 LayoutUnit beforeCollapseLogicalTop = logicalHeight();
948 LayoutUnit logicalTop = beforeCollapseLogicalTop;
robert@webkit.org97037ef2013-11-20 19:26:10 +0000949
950 LayoutUnit clearanceForSelfCollapsingBlock;
951 RenderObject* prev = child.previousSibling();
952 // 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
953 // 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
954 // 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.
955 if (!marginInfo.canCollapseWithMarginBefore() && prev && prev->isRenderBlockFlow() && toRenderBlockFlow(prev)->isSelfCollapsingBlock()) {
956 clearanceForSelfCollapsingBlock = toRenderBlockFlow(prev)->marginOffsetForSelfCollapsingBlock();
957 setLogicalHeight(logicalHeight() - clearanceForSelfCollapsingBlock);
958 }
959
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000960 if (childIsSelfCollapsing) {
961 // 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.
962 // Also, the child's top position equals the logical height of the container.
963 if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
964 // This child has no height. We need to compute our
965 // position before we collapse the child's margins together,
966 // so that we can get an accurate position for the zero-height block.
andersca@apple.com86298632013-11-10 19:32:33 +0000967 LayoutUnit collapsedBeforePos = std::max(marginInfo.positiveMargin(), childMargins.positiveMarginBefore());
968 LayoutUnit collapsedBeforeNeg = std::max(marginInfo.negativeMargin(), childMargins.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000969 marginInfo.setMargin(collapsedBeforePos, collapsedBeforeNeg);
970
971 // Now collapse the child's margins together, which means examining our
972 // bottom margin values as well.
973 marginInfo.setPositiveMarginIfLarger(childMargins.positiveMarginAfter());
974 marginInfo.setNegativeMarginIfLarger(childMargins.negativeMarginAfter());
975
976 if (!marginInfo.canCollapseWithMarginBefore())
977 // We need to make sure that the position of the self-collapsing block
978 // is correct, since it could have overflowing content
979 // that needs to be positioned correctly (e.g., a block that
980 // had a specified height of 0 but that actually had subcontent).
981 logicalTop = logicalHeight() + collapsedBeforePos - collapsedBeforeNeg;
982 }
983 } else {
984 if (mustSeparateMarginBeforeForChild(child)) {
985 ASSERT(!marginInfo.discardMargin() || (marginInfo.discardMargin() && !marginInfo.margin()));
986 // If we are at the before side of the block and we collapse, ignore the computed margin
987 // and just add the child margin to the container height. This will correctly position
988 // the child inside the container.
zalan@apple.coma4d00552014-01-25 00:21:59 +0000989 LayoutUnit separateMargin = !marginInfo.canCollapseWithMarginBefore() ? marginInfo.margin() : LayoutUnit::fromPixel(0);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000990 setLogicalHeight(logicalHeight() + separateMargin + marginBeforeForChild(child));
991 logicalTop = logicalHeight();
992 } else if (!marginInfo.discardMargin() && (!marginInfo.atBeforeSideOfBlock()
993 || (!marginInfo.canCollapseMarginBeforeWithChildren()
994 && (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginBeforeQuirk())))) {
995 // We're collapsing with a previous sibling's margins and not
996 // with the top of the block.
andersca@apple.com86298632013-11-10 19:32:33 +0000997 setLogicalHeight(logicalHeight() + std::max(marginInfo.positiveMargin(), posTop) - std::max(marginInfo.negativeMargin(), negTop));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000998 logicalTop = logicalHeight();
999 }
1000
1001 marginInfo.setDiscardMargin(childDiscardMarginAfter);
1002
1003 if (!marginInfo.discardMargin()) {
1004 marginInfo.setPositiveMargin(childMargins.positiveMarginAfter());
1005 marginInfo.setNegativeMargin(childMargins.negativeMarginAfter());
1006 } else
1007 marginInfo.clearMargin();
1008
1009 if (marginInfo.margin())
1010 marginInfo.setHasMarginAfterQuirk(hasMarginAfterQuirk(child));
1011 }
1012
1013 // If margins would pull us past the top of the next page, then we need to pull back and pretend like the margins
1014 // collapsed into the page edge.
1015 LayoutState* layoutState = view().layoutState();
1016 if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTop > beforeCollapseLogicalTop
1017 && hasNextPage(beforeCollapseLogicalTop)) {
1018 LayoutUnit oldLogicalTop = logicalTop;
andersca@apple.com86298632013-11-10 19:32:33 +00001019 logicalTop = std::min(logicalTop, nextPageLogicalTop(beforeCollapseLogicalTop));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001020 setLogicalHeight(logicalHeight() + (logicalTop - oldLogicalTop));
1021 }
1022
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001023 if (prev && prev->isRenderBlockFlow() && !prev->isFloatingOrOutOfFlowPositioned()) {
robert@webkit.org97037ef2013-11-20 19:26:10 +00001024 // 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
1025 // any floats from the parent will now overhang.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001026 RenderBlockFlow& block = toRenderBlockFlow(*prev);
robert@webkit.org97037ef2013-11-20 19:26:10 +00001027 LayoutUnit oldLogicalHeight = logicalHeight();
1028 setLogicalHeight(logicalTop);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001029 if (block.containsFloats() && !block.avoidsFloats() && (block.logicalTop() + block.lowestFloatLogicalBottom()) > logicalTop)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001030 addOverhangingFloats(block, false);
robert@webkit.org97037ef2013-11-20 19:26:10 +00001031 setLogicalHeight(oldLogicalHeight);
1032
1033 // If |child|'s previous sibling is a self-collapsing block that cleared a float and margin collapsing resulted in |child| moving up
1034 // 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
1035 // floats in the parent that overhang |child|'s new logical top.
1036 bool logicalTopIntrudesIntoFloat = clearanceForSelfCollapsingBlock > 0 && logicalTop < beforeCollapseLogicalTop;
1037 if (logicalTopIntrudesIntoFloat && containsFloats() && !child.avoidsFloats() && lowestFloatLogicalBottom() > logicalTop)
1038 child.setNeedsLayout();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001039 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001040
1041 return logicalTop;
1042}
1043
weinig@apple.com12840dc2013-10-22 23:59:08 +00001044LayoutUnit RenderBlockFlow::clearFloatsIfNeeded(RenderBox& child, MarginInfo& marginInfo, LayoutUnit oldTopPosMargin, LayoutUnit oldTopNegMargin, LayoutUnit yPos)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001045{
1046 LayoutUnit heightIncrease = getClearDelta(child, yPos);
1047 if (!heightIncrease)
1048 return yPos;
1049
weinig@apple.com12840dc2013-10-22 23:59:08 +00001050 if (child.isSelfCollapsingBlock()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001051 bool childDiscardMargin = mustDiscardMarginBeforeForChild(child) || mustDiscardMarginAfterForChild(child);
1052
1053 // For self-collapsing blocks that clear, they can still collapse their
1054 // margins with following siblings. Reset the current margins to represent
1055 // the self-collapsing block's margins only.
1056 // If DISCARD is specified for -webkit-margin-collapse, reset the margin values.
robert@webkit.org97037ef2013-11-20 19:26:10 +00001057 MarginValues childMargins = marginValuesForChild(child);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001058 if (!childDiscardMargin) {
andersca@apple.com86298632013-11-10 19:32:33 +00001059 marginInfo.setPositiveMargin(std::max(childMargins.positiveMarginBefore(), childMargins.positiveMarginAfter()));
1060 marginInfo.setNegativeMargin(std::max(childMargins.negativeMarginBefore(), childMargins.negativeMarginAfter()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001061 } else
1062 marginInfo.clearMargin();
1063 marginInfo.setDiscardMargin(childDiscardMargin);
1064
1065 // CSS2.1 states:
1066 // "If the top and bottom margins of an element with clearance are adjoining, its margins collapse with
1067 // the adjoining margins of following siblings but that resulting margin does not collapse with the bottom margin of the parent block."
1068 // So the parent's bottom margin cannot collapse through this block or any subsequent self-collapsing blocks. Check subsequent siblings
1069 // for a block with height - if none is found then don't allow the margins to collapse with the parent.
1070 bool wouldCollapseMarginsWithParent = marginInfo.canCollapseMarginAfterWithChildren();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001071 for (RenderBox* curr = child.nextSiblingBox(); curr && wouldCollapseMarginsWithParent; curr = curr->nextSiblingBox()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001072 if (!curr->isFloatingOrOutOfFlowPositioned() && !curr->isSelfCollapsingBlock())
1073 wouldCollapseMarginsWithParent = false;
1074 }
1075 if (wouldCollapseMarginsWithParent)
1076 marginInfo.setCanCollapseMarginAfterWithChildren(false);
1077
robert@webkit.org97037ef2013-11-20 19:26:10 +00001078 // 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
1079 // its own at the correct vertical position. If subsequent siblings attempt to collapse with |child|'s margins in |collapseMargins| we will
1080 // 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
1081 // margins can collapse at the correct vertical position.
1082 // 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
1083 // (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],
1084 // i.e., clearance = [height of float] - margin-top".
1085 setLogicalHeight(child.logicalTop() + childMargins.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001086 } else
1087 // Increase our height by the amount we had to clear.
1088 setLogicalHeight(logicalHeight() + heightIncrease);
1089
1090 if (marginInfo.canCollapseWithMarginBefore()) {
1091 // We can no longer collapse with the top of the block since a clear
1092 // occurred. The empty blocks collapse into the cleared block.
1093 // FIXME: This isn't quite correct. Need clarification for what to do
1094 // if the height the cleared block is offset by is smaller than the
1095 // margins involved.
1096 setMaxMarginBeforeValues(oldTopPosMargin, oldTopNegMargin);
1097 marginInfo.setAtBeforeSideOfBlock(false);
1098
1099 // 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 +00001100 setMustDiscardMarginBefore(style().marginBeforeCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001101 }
1102
robert@webkit.org97037ef2013-11-20 19:26:10 +00001103 return yPos + heightIncrease;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001104}
1105
weinig@apple.com12840dc2013-10-22 23:59:08 +00001106void RenderBlockFlow::marginBeforeEstimateForChild(RenderBox& child, LayoutUnit& positiveMarginBefore, LayoutUnit& negativeMarginBefore, bool& discardMarginBefore) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001107{
1108 // Give up if in quirks mode and we're a body/table cell and the top margin of the child box is quirky.
1109 // Give up if the child specified -webkit-margin-collapse: separate that prevents collapsing.
1110 // FIXME: Use writing mode independent accessor for marginBeforeCollapse.
akling@apple.com827be9c2013-10-29 02:58:43 +00001111 if ((document().inQuirksMode() && hasMarginAfterQuirk(child) && (isTableCell() || isBody())) || child.style().marginBeforeCollapse() == MSEPARATE)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001112 return;
1113
1114 // The margins are discarded by a child that specified -webkit-margin-collapse: discard.
1115 // FIXME: Use writing mode independent accessor for marginBeforeCollapse.
akling@apple.com827be9c2013-10-29 02:58:43 +00001116 if (child.style().marginBeforeCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001117 positiveMarginBefore = 0;
1118 negativeMarginBefore = 0;
1119 discardMarginBefore = true;
1120 return;
1121 }
1122
1123 LayoutUnit beforeChildMargin = marginBeforeForChild(child);
andersca@apple.com86298632013-11-10 19:32:33 +00001124 positiveMarginBefore = std::max(positiveMarginBefore, beforeChildMargin);
1125 negativeMarginBefore = std::max(negativeMarginBefore, -beforeChildMargin);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001126
weinig@apple.com12840dc2013-10-22 23:59:08 +00001127 if (!child.isRenderBlockFlow())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001128 return;
1129
weinig@apple.com12840dc2013-10-22 23:59:08 +00001130 RenderBlockFlow& childBlock = toRenderBlockFlow(child);
1131 if (childBlock.childrenInline() || childBlock.isWritingModeRoot())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001132 return;
1133
weinig@apple.com12840dc2013-10-22 23:59:08 +00001134 MarginInfo childMarginInfo(childBlock, childBlock.borderAndPaddingBefore(), childBlock.borderAndPaddingAfter());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001135 if (!childMarginInfo.canCollapseMarginBeforeWithChildren())
1136 return;
1137
weinig@apple.com12840dc2013-10-22 23:59:08 +00001138 RenderBox* grandchildBox = childBlock.firstChildBox();
1139 for (; grandchildBox; grandchildBox = grandchildBox->nextSiblingBox()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001140 if (!grandchildBox->isFloatingOrOutOfFlowPositioned())
1141 break;
1142 }
1143
1144 // 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 +00001145 if (!grandchildBox || grandchildBox->style().clear() != CNONE)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001146 return;
1147
1148 // Make sure to update the block margins now for the grandchild box so that we're looking at current values.
1149 if (grandchildBox->needsLayout()) {
1150 grandchildBox->computeAndSetBlockDirectionMargins(this);
1151 if (grandchildBox->isRenderBlock()) {
1152 RenderBlock* grandchildBlock = toRenderBlock(grandchildBox);
akling@apple.com827be9c2013-10-29 02:58:43 +00001153 grandchildBlock->setHasMarginBeforeQuirk(grandchildBox->style().hasMarginBeforeQuirk());
1154 grandchildBlock->setHasMarginAfterQuirk(grandchildBox->style().hasMarginAfterQuirk());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001155 }
1156 }
1157
1158 // Collapse the margin of the grandchild box with our own to produce an estimate.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001159 childBlock.marginBeforeEstimateForChild(*grandchildBox, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001160}
1161
weinig@apple.com12840dc2013-10-22 23:59:08 +00001162LayoutUnit RenderBlockFlow::estimateLogicalTopPosition(RenderBox& child, const MarginInfo& marginInfo, LayoutUnit& estimateWithoutPagination)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001163{
1164 // FIXME: We need to eliminate the estimation of vertical position, because when it's wrong we sometimes trigger a pathological
1165 // relayout if there are intruding floats.
1166 LayoutUnit logicalTopEstimate = logicalHeight();
1167 if (!marginInfo.canCollapseWithMarginBefore()) {
1168 LayoutUnit positiveMarginBefore = 0;
1169 LayoutUnit negativeMarginBefore = 0;
1170 bool discardMarginBefore = false;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001171 if (child.selfNeedsLayout()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001172 // Try to do a basic estimation of how the collapse is going to go.
1173 marginBeforeEstimateForChild(child, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
1174 } else {
1175 // Use the cached collapsed margin values from a previous layout. Most of the time they
1176 // will be right.
1177 MarginValues marginValues = marginValuesForChild(child);
andersca@apple.com86298632013-11-10 19:32:33 +00001178 positiveMarginBefore = std::max(positiveMarginBefore, marginValues.positiveMarginBefore());
1179 negativeMarginBefore = std::max(negativeMarginBefore, marginValues.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001180 discardMarginBefore = mustDiscardMarginBeforeForChild(child);
1181 }
1182
1183 // Collapse the result with our current margins.
1184 if (!discardMarginBefore)
andersca@apple.com86298632013-11-10 19:32:33 +00001185 logicalTopEstimate += std::max(marginInfo.positiveMargin(), positiveMarginBefore) - std::max(marginInfo.negativeMargin(), negativeMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001186 }
1187
1188 // Adjust logicalTopEstimate down to the next page if the margins are so large that we don't fit on the current
1189 // page.
1190 LayoutState* layoutState = view().layoutState();
1191 if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTopEstimate > logicalHeight()
1192 && hasNextPage(logicalHeight()))
andersca@apple.com86298632013-11-10 19:32:33 +00001193 logicalTopEstimate = std::min(logicalTopEstimate, nextPageLogicalTop(logicalHeight()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001194
1195 logicalTopEstimate += getClearDelta(child, logicalTopEstimate);
1196
1197 estimateWithoutPagination = logicalTopEstimate;
1198
1199 if (layoutState->isPaginated()) {
1200 // If the object has a page or column break value of "before", then we should shift to the top of the next page.
1201 logicalTopEstimate = applyBeforeBreak(child, logicalTopEstimate);
1202
1203 // For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
1204 logicalTopEstimate = adjustForUnsplittableChild(child, logicalTopEstimate);
1205
weinig@apple.com12840dc2013-10-22 23:59:08 +00001206 if (!child.selfNeedsLayout() && child.isRenderBlock())
1207 logicalTopEstimate += toRenderBlock(child).paginationStrut();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001208 }
1209
1210 return logicalTopEstimate;
1211}
1212
1213void RenderBlockFlow::setCollapsedBottomMargin(const MarginInfo& marginInfo)
1214{
1215 if (marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()) {
1216 // 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.
1217 // Don't update the max margin values because we won't need them anyway.
1218 if (marginInfo.discardMargin()) {
1219 setMustDiscardMarginAfter();
1220 return;
1221 }
1222
1223 // Update our max pos/neg bottom margins, since we collapsed our bottom margins
1224 // with our children.
andersca@apple.com86298632013-11-10 19:32:33 +00001225 setMaxMarginAfterValues(std::max(maxPositiveMarginAfter(), marginInfo.positiveMargin()), std::max(maxNegativeMarginAfter(), marginInfo.negativeMargin()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001226
1227 if (!marginInfo.hasMarginAfterQuirk())
1228 setHasMarginAfterQuirk(false);
1229
1230 if (marginInfo.hasMarginAfterQuirk() && !marginAfter())
1231 // We have no bottom margin and our last child has a quirky margin.
1232 // We will pick up this quirky margin and pass it through.
1233 // This deals with the <td><div><p> case.
1234 setHasMarginAfterQuirk(true);
1235 }
1236}
1237
1238void RenderBlockFlow::handleAfterSideOfBlock(LayoutUnit beforeSide, LayoutUnit afterSide, MarginInfo& marginInfo)
1239{
1240 marginInfo.setAtAfterSideOfBlock(true);
1241
robert@webkit.org97037ef2013-11-20 19:26:10 +00001242 // If our last child was a self-collapsing block with clearance then our logical height is flush with the
1243 // bottom edge of the float that the child clears. The correct vertical position for the margin-collapsing we want
1244 // to perform now is at the child's margin-top - so adjust our height to that position.
1245 RenderObject* lastBlock = lastChild();
1246 if (lastBlock && lastBlock->isRenderBlockFlow() && toRenderBlockFlow(lastBlock)->isSelfCollapsingBlock())
1247 setLogicalHeight(logicalHeight() - toRenderBlockFlow(lastBlock)->marginOffsetForSelfCollapsingBlock());
1248
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001249 // If we can't collapse with children then go ahead and add in the bottom margin.
1250 if (!marginInfo.discardMargin() && (!marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()
1251 && (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginAfterQuirk())))
1252 setLogicalHeight(logicalHeight() + marginInfo.margin());
1253
1254 // Now add in our bottom border/padding.
1255 setLogicalHeight(logicalHeight() + afterSide);
1256
1257 // Negative margins can cause our height to shrink below our minimal height (border/padding).
1258 // If this happens, ensure that the computed height is increased to the minimal height.
andersca@apple.com86298632013-11-10 19:32:33 +00001259 setLogicalHeight(std::max(logicalHeight(), beforeSide + afterSide));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001260
1261 // Update our bottom collapsed margin info.
1262 setCollapsedBottomMargin(marginInfo);
1263}
1264
1265void RenderBlockFlow::setMaxMarginBeforeValues(LayoutUnit pos, LayoutUnit neg)
1266{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001267 if (!hasRareBlockFlowData()) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001268 if (pos == RenderBlockFlowRareData::positiveMarginBeforeDefault(*this) && neg == RenderBlockFlowRareData::negativeMarginBeforeDefault(*this))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001269 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001270 materializeRareBlockFlowData();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001271 }
weinig@apple.com924a77a2013-11-11 00:22:38 +00001272
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001273 rareBlockFlowData()->m_margins.setPositiveMarginBefore(pos);
1274 rareBlockFlowData()->m_margins.setNegativeMarginBefore(neg);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001275}
1276
1277void RenderBlockFlow::setMaxMarginAfterValues(LayoutUnit pos, LayoutUnit neg)
1278{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001279 if (!hasRareBlockFlowData()) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001280 if (pos == RenderBlockFlowRareData::positiveMarginAfterDefault(*this) && neg == RenderBlockFlowRareData::negativeMarginAfterDefault(*this))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001281 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001282 materializeRareBlockFlowData();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001283 }
weinig@apple.com924a77a2013-11-11 00:22:38 +00001284
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001285 rareBlockFlowData()->m_margins.setPositiveMarginAfter(pos);
1286 rareBlockFlowData()->m_margins.setNegativeMarginAfter(neg);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001287}
1288
1289void RenderBlockFlow::setMustDiscardMarginBefore(bool value)
1290{
akling@apple.com827be9c2013-10-29 02:58:43 +00001291 if (style().marginBeforeCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001292 ASSERT(value);
1293 return;
1294 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001295
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001296 if (!hasRareBlockFlowData()) {
weinig@apple.com924a77a2013-11-11 00:22:38 +00001297 if (!value)
1298 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001299 materializeRareBlockFlowData();
weinig@apple.com924a77a2013-11-11 00:22:38 +00001300 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001301
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001302 rareBlockFlowData()->m_discardMarginBefore = value;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001303}
1304
1305void RenderBlockFlow::setMustDiscardMarginAfter(bool value)
1306{
akling@apple.com827be9c2013-10-29 02:58:43 +00001307 if (style().marginAfterCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001308 ASSERT(value);
1309 return;
1310 }
1311
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001312 if (!hasRareBlockFlowData()) {
weinig@apple.com924a77a2013-11-11 00:22:38 +00001313 if (!value)
1314 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001315 materializeRareBlockFlowData();
weinig@apple.com924a77a2013-11-11 00:22:38 +00001316 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001317
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001318 rareBlockFlowData()->m_discardMarginAfter = value;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001319}
1320
1321bool RenderBlockFlow::mustDiscardMarginBefore() const
1322{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001323 return style().marginBeforeCollapse() == MDISCARD || (hasRareBlockFlowData() && rareBlockFlowData()->m_discardMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001324}
1325
1326bool RenderBlockFlow::mustDiscardMarginAfter() const
1327{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001328 return style().marginAfterCollapse() == MDISCARD || (hasRareBlockFlowData() && rareBlockFlowData()->m_discardMarginAfter);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001329}
1330
weinig@apple.com12840dc2013-10-22 23:59:08 +00001331bool RenderBlockFlow::mustDiscardMarginBeforeForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001332{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001333 ASSERT(!child.selfNeedsLayout());
1334 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001335 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginBefore() : (child.style().marginBeforeCollapse() == MDISCARD);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001336 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001337 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginAfter() : (child.style().marginAfterCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001338
1339 // FIXME: We return false here because the implementation is not geometrically complete. We have values only for before/after, not start/end.
1340 // In case the boxes are perpendicular we assume the property is not specified.
1341 return false;
1342}
1343
weinig@apple.com12840dc2013-10-22 23:59:08 +00001344bool RenderBlockFlow::mustDiscardMarginAfterForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001345{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001346 ASSERT(!child.selfNeedsLayout());
1347 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001348 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginAfter() : (child.style().marginAfterCollapse() == MDISCARD);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001349 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001350 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginBefore() : (child.style().marginBeforeCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001351
1352 // FIXME: See |mustDiscardMarginBeforeForChild| above.
1353 return false;
1354}
1355
weinig@apple.com12840dc2013-10-22 23:59:08 +00001356bool RenderBlockFlow::mustSeparateMarginBeforeForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001357{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001358 ASSERT(!child.selfNeedsLayout());
akling@apple.com827be9c2013-10-29 02:58:43 +00001359 const RenderStyle& childStyle = child.style();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001360 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001361 return childStyle.marginBeforeCollapse() == MSEPARATE;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001362 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001363 return childStyle.marginAfterCollapse() == MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001364
1365 // FIXME: See |mustDiscardMarginBeforeForChild| above.
1366 return false;
1367}
1368
weinig@apple.com12840dc2013-10-22 23:59:08 +00001369bool RenderBlockFlow::mustSeparateMarginAfterForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001370{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001371 ASSERT(!child.selfNeedsLayout());
akling@apple.com827be9c2013-10-29 02:58:43 +00001372 const RenderStyle& childStyle = child.style();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001373 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001374 return childStyle.marginAfterCollapse() == MSEPARATE;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001375 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001376 return childStyle.marginBeforeCollapse() == MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001377
1378 // FIXME: See |mustDiscardMarginBeforeForChild| above.
1379 return false;
1380}
1381
weinig@apple.com12840dc2013-10-22 23:59:08 +00001382static bool inNormalFlow(RenderBox& child)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001383{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001384 RenderBlock* curr = child.containingBlock();
1385 while (curr && curr != &child.view()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001386 if (curr->hasColumns() || curr->isRenderFlowThread())
1387 return true;
1388 if (curr->isFloatingOrOutOfFlowPositioned())
1389 return false;
1390 curr = curr->containingBlock();
1391 }
1392 return true;
1393}
1394
weinig@apple.com12840dc2013-10-22 23:59:08 +00001395LayoutUnit RenderBlockFlow::applyBeforeBreak(RenderBox& child, LayoutUnit logicalOffset)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001396{
1397 // FIXME: Add page break checking here when we support printing.
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001398 RenderFlowThread* flowThread = flowThreadContainingBlock();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001399 bool isInsideMulticolFlowThread = flowThread && !flowThread->isRenderNamedFlowThread();
1400 bool checkColumnBreaks = isInsideMulticolFlowThread || view().layoutState()->isPaginatingColumns();
1401 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 +00001402 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001403 bool checkBeforeAlways = (checkColumnBreaks && child.style().columnBreakBefore() == PBALWAYS)
1404 || (checkPageBreaks && child.style().pageBreakBefore() == PBALWAYS)
akling@apple.com827be9c2013-10-29 02:58:43 +00001405 || (checkRegionBreaks && child.style().regionBreakBefore() == PBALWAYS);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001406 if (checkBeforeAlways && inNormalFlow(child) && hasNextPage(logicalOffset, IncludePageBoundary)) {
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001407 if (checkColumnBreaks) {
1408 if (isInsideMulticolFlowThread)
1409 checkRegionBreaks = true;
1410 else
1411 view().layoutState()->addForcedColumnBreak(&child, logicalOffset);
1412 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001413 if (checkRegionBreaks) {
1414 LayoutUnit offsetBreakAdjustment = 0;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001415 if (flowThread->addForcedRegionBreak(this, offsetFromLogicalTopOfFirstPage() + logicalOffset, &child, true, &offsetBreakAdjustment))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001416 return logicalOffset + offsetBreakAdjustment;
1417 }
1418 return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
1419 }
1420 return logicalOffset;
1421}
1422
weinig@apple.com12840dc2013-10-22 23:59:08 +00001423LayoutUnit RenderBlockFlow::applyAfterBreak(RenderBox& child, LayoutUnit logicalOffset, MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001424{
1425 // FIXME: Add page break checking here when we support printing.
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001426 RenderFlowThread* flowThread = flowThreadContainingBlock();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001427 bool isInsideMulticolFlowThread = flowThread && !flowThread->isRenderNamedFlowThread();
1428 bool checkColumnBreaks = isInsideMulticolFlowThread || view().layoutState()->isPaginatingColumns();
1429 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 +00001430 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001431 bool checkAfterAlways = (checkColumnBreaks && child.style().columnBreakAfter() == PBALWAYS)
1432 || (checkPageBreaks && child.style().pageBreakAfter() == PBALWAYS)
akling@apple.com827be9c2013-10-29 02:58:43 +00001433 || (checkRegionBreaks && child.style().regionBreakAfter() == PBALWAYS);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001434 if (checkAfterAlways && inNormalFlow(child) && hasNextPage(logicalOffset, IncludePageBoundary)) {
1435 LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
1436
1437 // So our margin doesn't participate in the next collapsing steps.
1438 marginInfo.clearMargin();
1439
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001440 if (checkColumnBreaks) {
1441 if (isInsideMulticolFlowThread)
1442 checkRegionBreaks = true;
1443 else
1444 view().layoutState()->addForcedColumnBreak(&child, logicalOffset);
1445 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001446 if (checkRegionBreaks) {
1447 LayoutUnit offsetBreakAdjustment = 0;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001448 if (flowThread->addForcedRegionBreak(this, offsetFromLogicalTopOfFirstPage() + logicalOffset + marginOffset, &child, false, &offsetBreakAdjustment))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001449 return logicalOffset + marginOffset + offsetBreakAdjustment;
1450 }
1451 return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
1452 }
1453 return logicalOffset;
1454}
1455
weinig@apple.com12840dc2013-10-22 23:59:08 +00001456LayoutUnit RenderBlockFlow::adjustBlockChildForPagination(LayoutUnit logicalTopAfterClear, LayoutUnit estimateWithoutPagination, RenderBox& child, bool atBeforeSideOfBlock)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001457{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001458 RenderBlock* childRenderBlock = child.isRenderBlock() ? toRenderBlock(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001459
1460 if (estimateWithoutPagination != logicalTopAfterClear) {
1461 // Our guess prior to pagination movement was wrong. Before we attempt to paginate, let's try again at the new
1462 // position.
1463 setLogicalHeight(logicalTopAfterClear);
1464 setLogicalTopForChild(child, logicalTopAfterClear, ApplyLayoutDelta);
1465
weinig@apple.com12840dc2013-10-22 23:59:08 +00001466 if (child.shrinkToAvoidFloats()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001467 // The child's width depends on the line width.
1468 // When the child shifts to clear an item, its width can
1469 // change (because it has more available line width).
1470 // So go ahead and mark the item as dirty.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001471 child.setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001472 }
1473
1474 if (childRenderBlock) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001475 if (!child.avoidsFloats() && childRenderBlock->containsFloats())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001476 toRenderBlockFlow(childRenderBlock)->markAllDescendantsWithFloatsForLayout();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001477 if (!child.needsLayout())
1478 child.markForPaginationRelayoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001479 }
1480
1481 // Our guess was wrong. Make the child lay itself out again.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001482 child.layoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001483 }
1484
1485 LayoutUnit oldTop = logicalTopAfterClear;
1486
1487 // If the object has a page or column break value of "before", then we should shift to the top of the next page.
1488 LayoutUnit result = applyBeforeBreak(child, logicalTopAfterClear);
1489
1490 if (pageLogicalHeightForOffset(result)) {
1491 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(result, ExcludePageBoundary);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001492 LayoutUnit spaceShortage = child.logicalHeight() - remainingLogicalHeight;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001493 if (spaceShortage > 0) {
1494 // If the child crosses a column boundary, report a break, in case nothing inside it has already
1495 // done so. The column balancer needs to know how much it has to stretch the columns to make more
1496 // content fit. If no breaks are reported (but do occur), the balancer will have no clue. FIXME:
1497 // This should be improved, though, because here we just pretend that the child is
1498 // unsplittable. A splittable child, on the other hand, has break opportunities at every position
1499 // where there's no child content, border or padding. In other words, we risk stretching more
1500 // than necessary.
1501 setPageBreak(result, spaceShortage);
1502 }
1503 }
1504
1505 // For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
1506 LayoutUnit logicalTopBeforeUnsplittableAdjustment = result;
1507 LayoutUnit logicalTopAfterUnsplittableAdjustment = adjustForUnsplittableChild(child, result);
1508
1509 LayoutUnit paginationStrut = 0;
1510 LayoutUnit unsplittableAdjustmentDelta = logicalTopAfterUnsplittableAdjustment - logicalTopBeforeUnsplittableAdjustment;
1511 if (unsplittableAdjustmentDelta)
1512 paginationStrut = unsplittableAdjustmentDelta;
1513 else if (childRenderBlock && childRenderBlock->paginationStrut())
1514 paginationStrut = childRenderBlock->paginationStrut();
1515
1516 if (paginationStrut) {
1517 // We are willing to propagate out to our parent block as long as we were at the top of the block prior
1518 // to collapsing our margins, and as long as we didn't clear or move as a result of other pagination.
1519 if (atBeforeSideOfBlock && oldTop == result && !isOutOfFlowPositioned() && !isTableCell()) {
1520 // FIXME: Should really check if we're exceeding the page height before propagating the strut, but we don't
1521 // have all the information to do so (the strut only has the remaining amount to push). Gecko gets this wrong too
1522 // and pushes to the next page anyway, so not too concerned about it.
1523 setPaginationStrut(result + paginationStrut);
1524 if (childRenderBlock)
1525 childRenderBlock->setPaginationStrut(0);
1526 } else
1527 result += paginationStrut;
1528 }
1529
1530 // Similar to how we apply clearance. Go ahead and boost height() to be the place where we're going to position the child.
1531 setLogicalHeight(logicalHeight() + (result - oldTop));
1532
1533 // Return the final adjusted logical top.
1534 return result;
1535}
1536
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001537static inline LayoutUnit calculateMinimumPageHeight(RenderStyle* renderStyle, RootInlineBox* lastLine, LayoutUnit lineTop, LayoutUnit lineBottom)
1538{
1539 // We may require a certain minimum number of lines per page in order to satisfy
1540 // orphans and widows, and that may affect the minimum page height.
andersca@apple.com86298632013-11-10 19:32:33 +00001541 unsigned lineCount = std::max<unsigned>(renderStyle->hasAutoOrphans() ? 1 : renderStyle->orphans(), renderStyle->hasAutoWidows() ? 1 : renderStyle->widows());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001542 if (lineCount > 1) {
1543 RootInlineBox* line = lastLine;
1544 for (unsigned i = 1; i < lineCount && line->prevRootBox(); i++)
1545 line = line->prevRootBox();
1546
1547 // FIXME: Paginating using line overflow isn't all fine. See FIXME in
1548 // adjustLinePositionForPagination() for more details.
1549 LayoutRect overflow = line->logicalVisualOverflowRect(line->lineTop(), line->lineBottom());
andersca@apple.com86298632013-11-10 19:32:33 +00001550 lineTop = std::min(line->lineTopWithLeading(), overflow.y());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001551 }
1552 return lineBottom - lineTop;
1553}
1554
1555void RenderBlockFlow::adjustLinePositionForPagination(RootInlineBox* lineBox, LayoutUnit& delta, RenderFlowThread* flowThread)
1556{
1557 // FIXME: For now we paginate using line overflow. This ensures that lines don't overlap at all when we
1558 // put a strut between them for pagination purposes. However, this really isn't the desired rendering, since
1559 // the line on the top of the next page will appear too far down relative to the same kind of line at the top
1560 // of the first column.
1561 //
1562 // The rendering we would like to see is one where the lineTopWithLeading is at the top of the column, and any line overflow
1563 // simply spills out above the top of the column. This effect would match what happens at the top of the first column.
1564 // We can't achieve this rendering, however, until we stop columns from clipping to the column bounds (thus allowing
1565 // for overflow to occur), and then cache visible overflow for each column rect.
1566 //
1567 // Furthermore, the paint we have to do when a column has overflow has to be special. We need to exclude
1568 // content that paints in a previous column (and content that paints in the following column).
1569 //
1570 // For now we'll at least honor the lineTopWithLeading when paginating if it is above the logical top overflow. This will
1571 // at least make positive leading work in typical cases.
1572 //
1573 // FIXME: Another problem with simply moving lines is that the available line width may change (because of floats).
1574 // Technically if the location we move the line to has a different line width than our old position, then we need to dirty the
1575 // line and all following lines.
1576 LayoutRect logicalVisualOverflow = lineBox->logicalVisualOverflowRect(lineBox->lineTop(), lineBox->lineBottom());
andersca@apple.com86298632013-11-10 19:32:33 +00001577 LayoutUnit logicalOffset = std::min(lineBox->lineTopWithLeading(), logicalVisualOverflow.y());
1578 LayoutUnit logicalBottom = std::max(lineBox->lineBottomWithLeading(), logicalVisualOverflow.maxY());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001579 LayoutUnit lineHeight = logicalBottom - logicalOffset;
akling@apple.com827be9c2013-10-29 02:58:43 +00001580 updateMinimumPageHeight(logicalOffset, calculateMinimumPageHeight(&style(), lineBox, logicalOffset, logicalBottom));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001581 logicalOffset += delta;
1582 lineBox->setPaginationStrut(0);
1583 lineBox->setIsFirstAfterPageBreak(false);
1584 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1585 bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
1586 // If lineHeight is greater than pageLogicalHeight, but logicalVisualOverflow.height() still fits, we are
1587 // still going to add a strut, so that the visible overflow fits on a single page.
1588 if (!pageLogicalHeight || (hasUniformPageLogicalHeight && logicalVisualOverflow.height() > pageLogicalHeight)
1589 || !hasNextPage(logicalOffset))
abucur@adobe.comd40287b2013-10-08 17:33:05 +00001590 // 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.
1591 // 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 +00001592 return;
1593 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
1594
1595 int lineIndex = lineCount(lineBox);
1596 if (remainingLogicalHeight < lineHeight || (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex)) {
abucur@adobe.comfc497132013-10-04 08:49:21 +00001597 if (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex) {
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001598 clearShouldBreakAtLineToAvoidWidow();
abucur@adobe.comfc497132013-10-04 08:49:21 +00001599 setDidBreakAtLineToAvoidWidow();
1600 }
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001601 // If we have a non-uniform page height, then we have to shift further possibly.
1602 if (!hasUniformPageLogicalHeight && !pushToNextPageWithMinimumLogicalHeight(remainingLogicalHeight, logicalOffset, lineHeight))
1603 return;
1604 if (lineHeight > pageLogicalHeight) {
1605 // Split the top margin in order to avoid splitting the visible part of the line.
andersca@apple.com86298632013-11-10 19:32:33 +00001606 remainingLogicalHeight -= std::min(lineHeight - pageLogicalHeight, std::max<LayoutUnit>(0, logicalVisualOverflow.y() - lineBox->lineTopWithLeading()));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001607 }
andersca@apple.com86298632013-11-10 19:32:33 +00001608 LayoutUnit totalLogicalHeight = lineHeight + std::max<LayoutUnit>(0, logicalOffset);
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001609 LayoutUnit pageLogicalHeightAtNewOffset = hasUniformPageLogicalHeight ? pageLogicalHeight : pageLogicalHeightForOffset(logicalOffset + remainingLogicalHeight);
1610 setPageBreak(logicalOffset, lineHeight - remainingLogicalHeight);
akling@apple.com827be9c2013-10-29 02:58:43 +00001611 if (((lineBox == firstRootBox() && totalLogicalHeight < pageLogicalHeightAtNewOffset) || (!style().hasAutoOrphans() && style().orphans() >= lineIndex))
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001612 && !isOutOfFlowPositioned() && !isTableCell())
andersca@apple.com86298632013-11-10 19:32:33 +00001613 setPaginationStrut(remainingLogicalHeight + std::max<LayoutUnit>(0, logicalOffset));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001614 else {
1615 delta += remainingLogicalHeight;
1616 lineBox->setPaginationStrut(remainingLogicalHeight);
1617 lineBox->setIsFirstAfterPageBreak(true);
1618 }
commit-queue@webkit.org883b01c2014-01-20 08:58:36 +00001619 } else if (remainingLogicalHeight == pageLogicalHeight) {
1620 // We're at the very top of a page or column.
1621 if (lineBox != firstRootBox())
1622 lineBox->setIsFirstAfterPageBreak(true);
1623 if (lineBox != firstRootBox() || offsetFromLogicalTopOfFirstPage())
1624 setPageBreak(logicalOffset, lineHeight);
1625 }
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001626}
1627
1628void RenderBlockFlow::setBreakAtLineToAvoidWidow(int lineToBreak)
1629{
abucur@adobe.comfc497132013-10-04 08:49:21 +00001630 ASSERT(lineToBreak >= 0);
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001631 ASSERT(!ensureRareBlockFlowData().m_didBreakAtLineToAvoidWidow);
1632 ensureRareBlockFlowData().m_lineBreakToAvoidWidow = lineToBreak;
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001633}
1634
abucur@adobe.comfc497132013-10-04 08:49:21 +00001635void RenderBlockFlow::setDidBreakAtLineToAvoidWidow()
1636{
1637 ASSERT(!shouldBreakAtLineToAvoidWidow());
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001638 if (!hasRareBlockFlowData())
abucur@adobe.comfc497132013-10-04 08:49:21 +00001639 return;
1640
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001641 rareBlockFlowData()->m_didBreakAtLineToAvoidWidow = true;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001642}
1643
1644void RenderBlockFlow::clearDidBreakAtLineToAvoidWidow()
1645{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001646 if (!hasRareBlockFlowData())
abucur@adobe.comfc497132013-10-04 08:49:21 +00001647 return;
1648
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001649 rareBlockFlowData()->m_didBreakAtLineToAvoidWidow = false;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001650}
1651
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001652void RenderBlockFlow::clearShouldBreakAtLineToAvoidWidow() const
1653{
abucur@adobe.comfc497132013-10-04 08:49:21 +00001654 ASSERT(shouldBreakAtLineToAvoidWidow());
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001655 if (!hasRareBlockFlowData())
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001656 return;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001657
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001658 rareBlockFlowData()->m_lineBreakToAvoidWidow = -1;
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001659}
1660
1661bool RenderBlockFlow::relayoutToAvoidWidows(LayoutStateMaintainer& statePusher)
1662{
1663 if (!shouldBreakAtLineToAvoidWidow())
1664 return false;
1665
1666 statePusher.pop();
1667 setEverHadLayout(true);
1668 layoutBlock(false);
1669 return true;
1670}
1671
weinig@apple.com31324fd2013-10-28 19:22:51 +00001672bool RenderBlockFlow::hasNextPage(LayoutUnit logicalOffset, PageBoundaryRule pageBoundaryRule) const
1673{
1674 ASSERT(view().layoutState() && view().layoutState()->isPaginated());
1675
1676 RenderFlowThread* flowThread = flowThreadContainingBlock();
1677 if (!flowThread)
1678 return true; // Printing and multi-column both make new pages to accommodate content.
1679
1680 // See if we're in the last region.
1681 LayoutUnit pageOffset = offsetFromLogicalTopOfFirstPage() + logicalOffset;
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00001682 RenderRegion* region = flowThread->regionAtBlockOffset(this, pageOffset, true);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001683 if (!region)
1684 return false;
1685 if (region->isLastRegion())
akling@apple.com827be9c2013-10-29 02:58:43 +00001686 return region->isRenderRegionSet() || region->style().regionFragment() == BreakRegionFragment
weinig@apple.com31324fd2013-10-28 19:22:51 +00001687 || (pageBoundaryRule == IncludePageBoundary && pageOffset == region->logicalTopForFlowThreadContent());
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00001688
1689 RenderRegion* startRegion = 0;
1690 RenderRegion* endRegion = 0;
1691 flowThread->getRegionRangeForBox(this, startRegion, endRegion);
1692
1693 if (region == endRegion)
1694 return false;
weinig@apple.com31324fd2013-10-28 19:22:51 +00001695 return true;
1696}
1697
1698LayoutUnit RenderBlockFlow::adjustForUnsplittableChild(RenderBox& child, LayoutUnit logicalOffset, bool includeMargins)
1699{
1700 bool checkColumnBreaks = view().layoutState()->isPaginatingColumns();
1701 bool checkPageBreaks = !checkColumnBreaks && view().layoutState()->m_pageLogicalHeight;
1702 RenderFlowThread* flowThread = flowThreadContainingBlock();
1703 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
akling@apple.com827be9c2013-10-29 02:58:43 +00001704 bool isUnsplittable = child.isUnsplittableForPagination() || (checkColumnBreaks && child.style().columnBreakInside() == PBAVOID)
1705 || (checkPageBreaks && child.style().pageBreakInside() == PBAVOID)
1706 || (checkRegionBreaks && child.style().regionBreakInside() == PBAVOID);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001707 if (!isUnsplittable)
1708 return logicalOffset;
1709 LayoutUnit childLogicalHeight = logicalHeightForChild(child) + (includeMargins ? marginBeforeForChild(child) + marginAfterForChild(child) : LayoutUnit());
1710 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1711 bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
1712 updateMinimumPageHeight(logicalOffset, childLogicalHeight);
1713 if (!pageLogicalHeight || (hasUniformPageLogicalHeight && childLogicalHeight > pageLogicalHeight)
1714 || !hasNextPage(logicalOffset))
1715 return logicalOffset;
1716 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
1717 if (remainingLogicalHeight < childLogicalHeight) {
1718 if (!hasUniformPageLogicalHeight && !pushToNextPageWithMinimumLogicalHeight(remainingLogicalHeight, logicalOffset, childLogicalHeight))
1719 return logicalOffset;
1720 return logicalOffset + remainingLogicalHeight;
1721 }
1722 return logicalOffset;
1723}
1724
1725bool RenderBlockFlow::pushToNextPageWithMinimumLogicalHeight(LayoutUnit& adjustment, LayoutUnit logicalOffset, LayoutUnit minimumLogicalHeight) const
1726{
1727 bool checkRegion = false;
1728 for (LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset + adjustment); pageLogicalHeight;
1729 pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset + adjustment)) {
1730 if (minimumLogicalHeight <= pageLogicalHeight)
1731 return true;
1732 if (!hasNextPage(logicalOffset + adjustment))
1733 return false;
1734 adjustment += pageLogicalHeight;
1735 checkRegion = true;
1736 }
1737 return !checkRegion;
1738}
1739
1740void RenderBlockFlow::setPageBreak(LayoutUnit offset, LayoutUnit spaceShortage)
1741{
1742 if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1743 flowThread->setPageBreak(this, offsetFromLogicalTopOfFirstPage() + offset, spaceShortage);
1744}
1745
1746void RenderBlockFlow::updateMinimumPageHeight(LayoutUnit offset, LayoutUnit minHeight)
1747{
1748 if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1749 flowThread->updateMinimumPageHeight(this, offsetFromLogicalTopOfFirstPage() + offset, minHeight);
1750 else if (ColumnInfo* colInfo = view().layoutState()->m_columnInfo)
1751 colInfo->updateMinimumColumnHeight(minHeight);
1752}
1753
1754LayoutUnit RenderBlockFlow::nextPageLogicalTop(LayoutUnit logicalOffset, PageBoundaryRule pageBoundaryRule) const
1755{
1756 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1757 if (!pageLogicalHeight)
1758 return logicalOffset;
1759
1760 // The logicalOffset is in our coordinate space. We can add in our pushed offset.
1761 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset);
1762 if (pageBoundaryRule == ExcludePageBoundary)
1763 return logicalOffset + (remainingLogicalHeight ? remainingLogicalHeight : pageLogicalHeight);
1764 return logicalOffset + remainingLogicalHeight;
1765}
1766
1767LayoutUnit RenderBlockFlow::pageLogicalTopForOffset(LayoutUnit offset) const
1768{
1769 LayoutUnit firstPageLogicalTop = isHorizontalWritingMode() ? view().layoutState()->m_pageOffset.height() : view().layoutState()->m_pageOffset.width();
1770 LayoutUnit blockLogicalTop = isHorizontalWritingMode() ? view().layoutState()->m_layoutOffset.height() : view().layoutState()->m_layoutOffset.width();
1771
1772 LayoutUnit cumulativeOffset = offset + blockLogicalTop;
1773 RenderFlowThread* flowThread = flowThreadContainingBlock();
1774 if (!flowThread) {
1775 LayoutUnit pageLogicalHeight = view().layoutState()->pageLogicalHeight();
1776 if (!pageLogicalHeight)
1777 return 0;
1778 return cumulativeOffset - roundToInt(cumulativeOffset - firstPageLogicalTop) % roundToInt(pageLogicalHeight);
1779 }
1780 return flowThread->pageLogicalTopForOffset(cumulativeOffset);
1781}
1782
1783LayoutUnit RenderBlockFlow::pageLogicalHeightForOffset(LayoutUnit offset) const
1784{
1785 RenderFlowThread* flowThread = flowThreadContainingBlock();
1786 if (!flowThread)
1787 return view().layoutState()->m_pageLogicalHeight;
1788 return flowThread->pageLogicalHeightForOffset(offset + offsetFromLogicalTopOfFirstPage());
1789}
1790
1791LayoutUnit RenderBlockFlow::pageRemainingLogicalHeightForOffset(LayoutUnit offset, PageBoundaryRule pageBoundaryRule) const
1792{
1793 offset += offsetFromLogicalTopOfFirstPage();
1794
1795 RenderFlowThread* flowThread = flowThreadContainingBlock();
1796 if (!flowThread) {
1797 LayoutUnit pageLogicalHeight = view().layoutState()->m_pageLogicalHeight;
1798 LayoutUnit remainingHeight = pageLogicalHeight - intMod(offset, pageLogicalHeight);
1799 if (pageBoundaryRule == IncludePageBoundary) {
1800 // If includeBoundaryPoint is true the line exactly on the top edge of a
1801 // column will act as being part of the previous column.
1802 remainingHeight = intMod(remainingHeight, pageLogicalHeight);
1803 }
1804 return remainingHeight;
1805 }
1806
1807 return flowThread->pageRemainingLogicalHeightForOffset(offset, pageBoundaryRule);
1808}
1809
1810
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001811void RenderBlockFlow::layoutLineGridBox()
1812{
akling@apple.com827be9c2013-10-29 02:58:43 +00001813 if (style().lineGrid() == RenderStyle::initialLineGrid()) {
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001814 setLineGridBox(0);
1815 return;
1816 }
1817
1818 setLineGridBox(0);
1819
akling@apple.com1aa97b02013-10-31 21:59:49 +00001820 auto lineGridBox = std::make_unique<RootInlineBox>(*this);
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001821 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
1822 lineGridBox->setConstructed();
1823 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1824 VerticalPositionCache verticalPositionCache;
1825 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
1826
akling@apple.com1aa97b02013-10-31 21:59:49 +00001827 setLineGridBox(std::move(lineGridBox));
1828
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001829 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
1830 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
1831 // to this grid.
1832}
1833
weinig@apple.com12840dc2013-10-22 23:59:08 +00001834bool RenderBlockFlow::containsFloat(RenderBox& renderer) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001835{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001836 return m_floatingObjects && m_floatingObjects->set().contains<RenderBox&, FloatingObjectHashTranslator>(renderer);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001837}
1838
1839void RenderBlockFlow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
1840{
1841 RenderBlock::styleDidChange(diff, oldStyle);
1842
1843 // After our style changed, if we lose our ability to propagate floats into next sibling
1844 // blocks, then we need to find the top most parent containing that overhanging float and
1845 // then mark its descendants with floats for layout and clear all floats from its next
1846 // sibling blocks that exist in our floating objects list. See bug 56299 and 62875.
1847 bool canPropagateFloatIntoSibling = !isFloatingOrOutOfFlowPositioned() && !avoidsFloats();
1848 if (diff == StyleDifferenceLayout && s_canPropagateFloatIntoSibling && !canPropagateFloatIntoSibling && hasOverhangingFloats()) {
1849 RenderBlockFlow* parentBlock = this;
1850 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001851
weinig@apple.comc77041e2013-12-14 18:05:45 +00001852 for (auto& ancestor : ancestorsOfType<RenderBlockFlow>(*this)) {
1853 if (ancestor.isRenderView())
akling@apple.comf3028052013-11-04 08:46:06 +00001854 break;
weinig@apple.comc77041e2013-12-14 18:05:45 +00001855 if (ancestor.hasOverhangingFloats()) {
akling@apple.comf3028052013-11-04 08:46:06 +00001856 for (auto it = floatingObjectSet.begin(), end = floatingObjectSet.end(); it != end; ++it) {
1857 RenderBox& renderer = (*it)->renderer();
weinig@apple.comc77041e2013-12-14 18:05:45 +00001858 if (ancestor.hasOverhangingFloat(renderer)) {
1859 parentBlock = &ancestor;
akling@apple.comf3028052013-11-04 08:46:06 +00001860 break;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001861 }
1862 }
1863 }
1864 }
1865
1866 parentBlock->markAllDescendantsWithFloatsForLayout();
1867 parentBlock->markSiblingsWithFloatsForLayout();
1868 }
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00001869
akling@apple.com8f40c5b2013-10-27 22:54:07 +00001870 if (auto fragment = renderNamedFlowFragment())
akling@apple.com827be9c2013-10-29 02:58:43 +00001871 fragment->setStyle(RenderNamedFlowFragment::createStyle(style()));
antti@apple.com42fb53d2013-10-25 02:33:11 +00001872
1873 if (diff >= StyleDifferenceRepaint)
1874 invalidateLineLayoutPath();
hyatt@apple.comd4be3772014-01-24 19:55:33 +00001875
1876 if (multiColumnFlowThread()) {
1877 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox())
1878 child->setStyle(RenderStyle::createAnonymousStyleWithDisplay(&style(), BLOCK));
1879 }
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001880}
1881
akling@apple.combdae43242013-10-25 12:00:20 +00001882void RenderBlockFlow::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001883{
akling@apple.com827be9c2013-10-29 02:58:43 +00001884 const RenderStyle* oldStyle = hasInitializedStyle() ? &style() : nullptr;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001885 s_canPropagateFloatIntoSibling = oldStyle ? !isFloatingOrOutOfFlowPositioned() && !avoidsFloats() : false;
1886
akling@apple.combdae43242013-10-25 12:00:20 +00001887 if (oldStyle && parent() && diff == StyleDifferenceLayout && oldStyle->position() != newStyle.position()) {
1888 if (containsFloats() && !isFloating() && !isOutOfFlowPositioned() && newStyle.hasOutOfFlowPosition())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001889 markAllDescendantsWithFloatsForLayout();
1890 }
1891
1892 RenderBlock::styleWillChange(diff, newStyle);
1893}
1894
antti@apple.coma2c7f242013-10-22 22:37:25 +00001895void RenderBlockFlow::deleteLines()
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001896{
1897 if (containsFloats())
1898 m_floatingObjects->clearLineBoxTreePointers();
weinig@apple.com611b9292013-10-20 22:57:54 +00001899
antti@apple.comfea51992013-10-28 13:39:23 +00001900 if (m_simpleLineLayout) {
antti@apple.com940f5872013-10-24 20:31:11 +00001901 ASSERT(!m_lineBoxes.firstLineBox());
antti@apple.comfea51992013-10-28 13:39:23 +00001902 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +00001903 } else
akling@apple.com31dd4f42013-10-30 22:27:59 +00001904 m_lineBoxes.deleteLineBoxTree();
weinig@apple.com611b9292013-10-20 22:57:54 +00001905
antti@apple.coma2c7f242013-10-22 22:37:25 +00001906 RenderBlock::deleteLines();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001907}
1908
1909void RenderBlockFlow::moveAllChildrenIncludingFloatsTo(RenderBlock* toBlock, bool fullRemoveInsert)
1910{
1911 RenderBlockFlow* toBlockFlow = toRenderBlockFlow(toBlock);
1912 moveAllChildrenTo(toBlockFlow, fullRemoveInsert);
1913
1914 // When a portion of the render tree is being detached, anonymous blocks
1915 // will be combined as their children are deleted. In this process, the
1916 // anonymous block later in the tree is merged into the one preceeding it.
1917 // It can happen that the later block (this) contains floats that the
1918 // previous block (toBlockFlow) did not contain, and thus are not in the
1919 // floating objects list for toBlockFlow. This can result in toBlockFlow
1920 // containing floats that are not in it's floating objects list, but are in
1921 // the floating objects lists of siblings and parents. This can cause
1922 // problems when the float itself is deleted, since the deletion code
1923 // assumes that if a float is not in it's containing block's floating
1924 // objects list, it isn't in any floating objects list. In order to
1925 // preserve this condition (removing it has serious performance
1926 // implications), we need to copy the floating objects from the old block
1927 // (this) to the new block (toBlockFlow). The float's metrics will likely
1928 // all be wrong, but since toBlockFlow is already marked for layout, this
1929 // will get fixed before anything gets displayed.
1930 // See bug https://bugs.webkit.org/show_bug.cgi?id=115566
1931 if (m_floatingObjects) {
1932 if (!toBlockFlow->m_floatingObjects)
1933 toBlockFlow->createFloatingObjects();
1934
1935 const FloatingObjectSet& fromFloatingObjectSet = m_floatingObjects->set();
1936 auto end = fromFloatingObjectSet.end();
1937
1938 for (auto it = fromFloatingObjectSet.begin(); it != end; ++it) {
1939 FloatingObject* floatingObject = it->get();
1940
1941 // Don't insert the object again if it's already in the list
weinig@apple.com12840dc2013-10-22 23:59:08 +00001942 if (toBlockFlow->containsFloat(floatingObject->renderer()))
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001943 continue;
1944
1945 toBlockFlow->m_floatingObjects->add(floatingObject->unsafeClone());
1946 }
1947 }
1948}
1949
1950void RenderBlockFlow::addOverflowFromFloats()
1951{
1952 if (!m_floatingObjects)
1953 return;
1954
1955 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1956 auto end = floatingObjectSet.end();
1957 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
1958 FloatingObject* r = it->get();
1959 if (r->isDescendant())
1960 addOverflowFromChild(&r->renderer(), IntSize(xPositionForFloatIncludingMargin(r), yPositionForFloatIncludingMargin(r)));
1961 }
1962}
1963
1964void RenderBlockFlow::computeOverflow(LayoutUnit oldClientAfterEdge, bool recomputeFloats)
1965{
1966 RenderBlock::computeOverflow(oldClientAfterEdge, recomputeFloats);
1967
1968 if (!hasColumns() && (recomputeFloats || isRoot() || expandsToEncloseOverhangingFloats() || hasSelfPaintingLayer()))
1969 addOverflowFromFloats();
1970}
1971
1972void RenderBlockFlow::repaintOverhangingFloats(bool paintAllDescendants)
1973{
1974 // Repaint any overhanging floats (if we know we're the one to paint them).
1975 // Otherwise, bail out.
1976 if (!hasOverhangingFloats())
1977 return;
1978
1979 // FIXME: Avoid disabling LayoutState. At the very least, don't disable it for floats originating
1980 // in this block. Better yet would be to push extra state for the containers of other floats.
1981 LayoutStateDisabler layoutStateDisabler(&view());
1982 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1983 auto end = floatingObjectSet.end();
1984 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
1985 FloatingObject* floatingObject = it->get();
1986 // Only repaint the object if it is overhanging, is not in its own layer, and
1987 // is our responsibility to paint (m_shouldPaint is set). When paintAllDescendants is true, the latter
1988 // condition is replaced with being a descendant of us.
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00001989 if (logicalBottomForFloat(floatingObject) > logicalHeight()
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001990 && !floatingObject->renderer().hasSelfPaintingLayer()
1991 && (floatingObject->shouldPaint() || (paintAllDescendants && floatingObject->renderer().isDescendantOf(this)))) {
1992 floatingObject->renderer().repaint();
1993 floatingObject->renderer().repaintOverhangingFloats(false);
1994 }
1995 }
1996}
1997
1998void RenderBlockFlow::paintFloats(PaintInfo& paintInfo, const LayoutPoint& paintOffset, bool preservePhase)
1999{
2000 if (!m_floatingObjects)
2001 return;
2002
2003 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2004 auto end = floatingObjectSet.end();
2005 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2006 FloatingObject* r = it->get();
2007 // Only paint the object if our m_shouldPaint flag is set.
2008 if (r->shouldPaint() && !r->renderer().hasSelfPaintingLayer()) {
2009 PaintInfo currentPaintInfo(paintInfo);
2010 currentPaintInfo.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
2011 // FIXME: LayoutPoint version of xPositionForFloatIncludingMargin would make this much cleaner.
2012 LayoutPoint childPoint = flipFloatForWritingModeForChild(r, LayoutPoint(paintOffset.x() + xPositionForFloatIncludingMargin(r) - r->renderer().x(), paintOffset.y() + yPositionForFloatIncludingMargin(r) - r->renderer().y()));
2013 r->renderer().paint(currentPaintInfo, childPoint);
2014 if (!preservePhase) {
2015 currentPaintInfo.phase = PaintPhaseChildBlockBackgrounds;
2016 r->renderer().paint(currentPaintInfo, childPoint);
2017 currentPaintInfo.phase = PaintPhaseFloat;
2018 r->renderer().paint(currentPaintInfo, childPoint);
2019 currentPaintInfo.phase = PaintPhaseForeground;
2020 r->renderer().paint(currentPaintInfo, childPoint);
2021 currentPaintInfo.phase = PaintPhaseOutline;
2022 r->renderer().paint(currentPaintInfo, childPoint);
2023 }
2024 }
2025 }
2026}
2027
weinig@apple.com12840dc2013-10-22 23:59:08 +00002028void RenderBlockFlow::clipOutFloatingObjects(RenderBlock& rootBlock, const PaintInfo* paintInfo, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002029{
2030 if (m_floatingObjects) {
2031 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2032 auto end = floatingObjectSet.end();
2033 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2034 FloatingObject* floatingObject = it->get();
2035 LayoutRect floatBox(offsetFromRootBlock.width() + xPositionForFloatIncludingMargin(floatingObject),
2036 offsetFromRootBlock.height() + yPositionForFloatIncludingMargin(floatingObject),
2037 floatingObject->renderer().width(), floatingObject->renderer().height());
weinig@apple.com12840dc2013-10-22 23:59:08 +00002038 rootBlock.flipForWritingMode(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002039 floatBox.move(rootBlockPhysicalPosition.x(), rootBlockPhysicalPosition.y());
2040 paintInfo->context->clipOut(pixelSnappedIntRect(floatBox));
2041 }
2042 }
2043}
2044
2045void RenderBlockFlow::createFloatingObjects()
2046{
bjonesbe@adobe.com2e94fff2013-11-08 23:24:40 +00002047 m_floatingObjects = adoptPtr(new FloatingObjects(*this));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002048}
2049
2050void RenderBlockFlow::removeFloatingObjects()
2051{
2052 if (!m_floatingObjects)
2053 return;
2054
2055 m_floatingObjects->clear();
2056}
2057
weinig@apple.com12840dc2013-10-22 23:59:08 +00002058FloatingObject* RenderBlockFlow::insertFloatingObject(RenderBox& floatBox)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002059{
weinig@apple.com12840dc2013-10-22 23:59:08 +00002060 ASSERT(floatBox.isFloating());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002061
2062 // Create the list of special objects if we don't aleady have one
2063 if (!m_floatingObjects)
2064 createFloatingObjects();
2065 else {
2066 // Don't insert the floatingObject again if it's already in the list
2067 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002068 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002069 if (it != floatingObjectSet.end())
2070 return it->get();
2071 }
2072
2073 // Create the special floatingObject entry & append it to the list
2074
weinig@apple.com12840dc2013-10-22 23:59:08 +00002075 std::unique_ptr<FloatingObject> floatingObject = FloatingObject::create(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002076
2077 // Our location is irrelevant if we're unsplittable or no pagination is in effect.
2078 // Just go ahead and lay out the float.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002079 bool isChildRenderBlock = floatBox.isRenderBlock();
2080 if (isChildRenderBlock && !floatBox.needsLayout() && view().layoutState()->pageLogicalHeightChanged())
2081 floatBox.setChildNeedsLayout(MarkOnlyThis);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002082
2083 bool needsBlockDirectionLocationSetBeforeLayout = isChildRenderBlock && view().layoutState()->needsBlockDirectionLocationSetBeforeLayout();
2084 if (!needsBlockDirectionLocationSetBeforeLayout || isWritingModeRoot()) // We are unsplittable if we're a block flow root.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002085 floatBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002086 else {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002087 floatBox.updateLogicalWidth();
2088 floatBox.computeAndSetBlockDirectionMargins(this);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002089 }
2090
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002091 setLogicalWidthForFloat(floatingObject.get(), logicalWidthForChild(floatBox) + marginStartForChild(floatBox) + marginEndForChild(floatBox));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002092
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002093 return m_floatingObjects->add(std::move(floatingObject));
2094}
2095
weinig@apple.com12840dc2013-10-22 23:59:08 +00002096void RenderBlockFlow::removeFloatingObject(RenderBox& floatBox)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002097{
2098 if (m_floatingObjects) {
2099 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002100 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002101 if (it != floatingObjectSet.end()) {
2102 FloatingObject* floatingObject = it->get();
2103 if (childrenInline()) {
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002104 LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
2105 LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002106
2107 // Fix for https://bugs.webkit.org/show_bug.cgi?id=54995.
2108 if (logicalBottom < 0 || logicalBottom < logicalTop || logicalTop == LayoutUnit::max())
2109 logicalBottom = LayoutUnit::max();
2110 else {
2111 // Special-case zero- and less-than-zero-height floats: those don't touch
2112 // the line that they're on, but it still needs to be dirtied. This is
2113 // accomplished by pretending they have a height of 1.
andersca@apple.com86298632013-11-10 19:32:33 +00002114 logicalBottom = std::max(logicalBottom, logicalTop + 1);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002115 }
2116 if (floatingObject->originatingLine()) {
2117 if (!selfNeedsLayout()) {
2118 ASSERT(&floatingObject->originatingLine()->renderer() == this);
2119 floatingObject->originatingLine()->markDirty();
2120 }
2121#if !ASSERT_DISABLED
2122 floatingObject->setOriginatingLine(0);
2123#endif
2124 }
2125 markLinesDirtyInBlockRange(0, logicalBottom);
2126 }
2127 m_floatingObjects->remove(floatingObject);
2128 }
2129 }
2130}
2131
2132void RenderBlockFlow::removeFloatingObjectsBelow(FloatingObject* lastFloat, int logicalOffset)
2133{
2134 if (!containsFloats())
2135 return;
2136
2137 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2138 FloatingObject* curr = floatingObjectSet.last().get();
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002139 while (curr != lastFloat && (!curr->isPlaced() || logicalTopForFloat(curr) >= logicalOffset)) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002140 m_floatingObjects->remove(curr);
2141 if (floatingObjectSet.isEmpty())
2142 break;
2143 curr = floatingObjectSet.last().get();
2144 }
2145}
2146
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002147LayoutUnit RenderBlockFlow::logicalLeftOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
2148{
2149 LayoutUnit offset = fixedOffset;
2150 if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
2151 offset = m_floatingObjects->logicalLeftOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
2152 return adjustLogicalLeftOffsetForLine(offset, applyTextIndent);
2153}
2154
2155LayoutUnit RenderBlockFlow::logicalRightOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
2156{
2157 LayoutUnit offset = fixedOffset;
2158 if (m_floatingObjects && m_floatingObjects->hasRightObjects())
2159 offset = m_floatingObjects->logicalRightOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
2160 return adjustLogicalRightOffsetForLine(offset, applyTextIndent);
2161}
2162
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002163LayoutPoint RenderBlockFlow::computeLogicalLocationForFloat(const FloatingObject* floatingObject, LayoutUnit logicalTopOffset) const
2164{
weinig@apple.com12840dc2013-10-22 23:59:08 +00002165 RenderBox& childBox = floatingObject->renderer();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002166 LayoutUnit logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
2167 LayoutUnit logicalRightOffset; // Constant part of right offset.
2168#if ENABLE(CSS_SHAPES)
2169 // FIXME Bug 102948: This only works for shape outside directly set on this block.
2170 ShapeInsideInfo* shapeInsideInfo = this->layoutShapeInsideInfo();
2171 // FIXME: Implement behavior for right floats.
2172 if (shapeInsideInfo) {
zoltan@webkit.orgb64932a2013-10-11 21:01:40 +00002173 LayoutSize floatLogicalSize = logicalSizeForFloat(floatingObject);
2174 // floatingObject's logicalSize doesn't contain the actual height at this point, so we need to calculate it
2175 floatLogicalSize.setHeight(logicalHeightForChild(childBox) + marginBeforeForChild(childBox) + marginAfterForChild(childBox));
2176
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002177 // FIXME: If the float doesn't fit in the shape we should push it under the content box
2178 logicalTopOffset = shapeInsideInfo->computeFirstFitPositionForFloat(floatLogicalSize);
2179 if (logicalHeight() > logicalTopOffset)
2180 logicalTopOffset = logicalHeight();
2181
zoltan@webkit.orgb64932a2013-10-11 21:01:40 +00002182 SegmentList segments = shapeInsideInfo->computeSegmentsForLine(logicalTopOffset, floatLogicalSize.height());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002183 // FIXME Bug 102949: Add support for shapes with multiple segments.
2184 if (segments.size() == 1) {
2185 // The segment offsets are relative to the content box.
2186 logicalRightOffset = logicalLeftOffset + segments[0].logicalRight;
2187 logicalLeftOffset += segments[0].logicalLeft;
2188 }
2189 } else
2190#endif
2191 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset);
2192
andersca@apple.com86298632013-11-10 19:32:33 +00002193 LayoutUnit floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset); // The width we look for.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002194
2195 LayoutUnit floatLogicalLeft;
2196
2197 bool insideFlowThread = flowThreadContainingBlock();
2198
akling@apple.com827be9c2013-10-29 02:58:43 +00002199 if (childBox.style().floating() == LeftFloat) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002200 LayoutUnit heightRemainingLeft = 1;
2201 LayoutUnit heightRemainingRight = 1;
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002202 floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
2203 while (logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight) - floatLogicalLeft < floatLogicalWidth) {
andersca@apple.com86298632013-11-10 19:32:33 +00002204 logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002205 floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002206 if (insideFlowThread) {
2207 // Have to re-evaluate all of our offsets, since they may have changed.
2208 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
2209 logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
andersca@apple.com86298632013-11-10 19:32:33 +00002210 floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002211 }
2212 }
andersca@apple.com86298632013-11-10 19:32:33 +00002213 floatLogicalLeft = std::max(logicalLeftOffset - borderAndPaddingLogicalLeft(), floatLogicalLeft);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002214 } else {
2215 LayoutUnit heightRemainingLeft = 1;
2216 LayoutUnit heightRemainingRight = 1;
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002217 floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
2218 while (floatLogicalLeft - logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft) < floatLogicalWidth) {
andersca@apple.com86298632013-11-10 19:32:33 +00002219 logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002220 floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002221 if (insideFlowThread) {
2222 // Have to re-evaluate all of our offsets, since they may have changed.
2223 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
2224 logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
andersca@apple.com86298632013-11-10 19:32:33 +00002225 floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002226 }
2227 }
2228 // Use the original width of the float here, since the local variable
2229 // |floatLogicalWidth| was capped to the available line width. See
2230 // fast/block/float/clamped-right-float.html.
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002231 floatLogicalLeft -= logicalWidthForFloat(floatingObject);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002232 }
2233
2234 return LayoutPoint(floatLogicalLeft, logicalTopOffset);
2235}
2236
2237bool RenderBlockFlow::positionNewFloats()
2238{
2239 if (!m_floatingObjects)
2240 return false;
2241
2242 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2243 if (floatingObjectSet.isEmpty())
2244 return false;
2245
2246 // If all floats have already been positioned, then we have no work to do.
2247 if (floatingObjectSet.last()->isPlaced())
2248 return false;
2249
2250 // Move backwards through our floating object list until we find a float that has
2251 // already been positioned. Then we'll be able to move forward, positioning all of
2252 // the new floats that need it.
2253 auto it = floatingObjectSet.end();
2254 --it; // Go to last item.
2255 auto begin = floatingObjectSet.begin();
2256 FloatingObject* lastPlacedFloatingObject = 0;
2257 while (it != begin) {
2258 --it;
2259 if ((*it)->isPlaced()) {
2260 lastPlacedFloatingObject = it->get();
2261 ++it;
2262 break;
2263 }
2264 }
2265
2266 LayoutUnit logicalTop = logicalHeight();
2267
2268 // The float cannot start above the top position of the last positioned float.
2269 if (lastPlacedFloatingObject)
andersca@apple.com86298632013-11-10 19:32:33 +00002270 logicalTop = std::max(logicalTopForFloat(lastPlacedFloatingObject), logicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002271
2272 auto end = floatingObjectSet.end();
2273 // Now walk through the set of unpositioned floats and place them.
2274 for (; it != end; ++it) {
2275 FloatingObject* floatingObject = it->get();
2276 // The containing block is responsible for positioning floats, so if we have floats in our
2277 // list that come from somewhere else, do not attempt to position them.
2278 if (floatingObject->renderer().containingBlock() != this)
2279 continue;
2280
weinig@apple.com12840dc2013-10-22 23:59:08 +00002281 RenderBox& childBox = floatingObject->renderer();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002282
akling@apple.com827be9c2013-10-29 02:58:43 +00002283 LayoutUnit childLogicalLeftMargin = style().isLeftToRightDirection() ? marginStartForChild(childBox) : marginEndForChild(childBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002284
weinig@apple.com12840dc2013-10-22 23:59:08 +00002285 LayoutRect oldRect = childBox.frameRect();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002286
akling@apple.com827be9c2013-10-29 02:58:43 +00002287 if (childBox.style().clear() & CLEFT)
andersca@apple.com86298632013-11-10 19:32:33 +00002288 logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatLeft), logicalTop);
akling@apple.com827be9c2013-10-29 02:58:43 +00002289 if (childBox.style().clear() & CRIGHT)
andersca@apple.com86298632013-11-10 19:32:33 +00002290 logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatRight), logicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002291
2292 LayoutPoint floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, logicalTop);
2293
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002294 setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002295
2296 setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
2297 setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
2298
2299 estimateRegionRangeForBoxChild(childBox);
2300
2301 LayoutState* layoutState = view().layoutState();
2302 bool isPaginated = layoutState->isPaginated();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002303 if (isPaginated && !childBox.needsLayout())
2304 childBox.markForPaginationRelayoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002305
weinig@apple.com12840dc2013-10-22 23:59:08 +00002306 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002307
2308 if (isPaginated) {
2309 // If we are unsplittable and don't fit, then we need to move down.
2310 // We include our margins as part of the unsplittable area.
2311 LayoutUnit newLogicalTop = adjustForUnsplittableChild(childBox, floatLogicalLocation.y(), true);
2312
2313 // See if we have a pagination strut that is making us move down further.
2314 // Note that an unsplittable child can't also have a pagination strut, so this is
2315 // exclusive with the case above.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002316 RenderBlock* childBlock = childBox.isRenderBlock() ? toRenderBlock(&childBox) : nullptr;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002317 if (childBlock && childBlock->paginationStrut()) {
2318 newLogicalTop += childBlock->paginationStrut();
2319 childBlock->setPaginationStrut(0);
2320 }
2321
2322 if (newLogicalTop != floatLogicalLocation.y()) {
2323 floatingObject->setPaginationStrut(newLogicalTop - floatLogicalLocation.y());
2324
2325 floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, newLogicalTop);
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002326 setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002327
2328 setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
2329 setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
2330
2331 if (childBlock)
2332 childBlock->setChildNeedsLayout(MarkOnlyThis);
weinig@apple.com12840dc2013-10-22 23:59:08 +00002333 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002334 }
2335
2336 if (updateRegionRangeForBoxChild(childBox)) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002337 childBox.setNeedsLayout(MarkOnlyThis);
2338 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002339 }
2340 }
2341
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002342 setLogicalTopForFloat(floatingObject, floatLogicalLocation.y());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002343
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002344 setLogicalHeightForFloat(floatingObject, logicalHeightForChild(childBox) + marginBeforeForChild(childBox) + marginAfterForChild(childBox));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002345
2346 m_floatingObjects->addPlacedObject(floatingObject);
2347
zoltan@webkit.org0faf5722013-11-05 02:34:16 +00002348#if ENABLE(CSS_SHAPES)
2349 if (ShapeOutsideInfo* shapeOutside = childBox.shapeOutsideInfo())
2350 shapeOutside->setShapeSize(logicalWidthForChild(childBox), logicalHeightForChild(childBox));
2351#endif
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002352 // If the child moved, we have to repaint it.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002353 if (childBox.checkForRepaintDuringLayout())
2354 childBox.repaintDuringLayoutIfMoved(oldRect);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002355 }
2356 return true;
2357}
2358
2359void RenderBlockFlow::newLine(EClear clear)
2360{
2361 positionNewFloats();
2362 // set y position
2363 LayoutUnit newY = 0;
2364 switch (clear) {
2365 case CLEFT:
2366 newY = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
2367 break;
2368 case CRIGHT:
2369 newY = lowestFloatLogicalBottom(FloatingObject::FloatRight);
2370 break;
2371 case CBOTH:
2372 newY = lowestFloatLogicalBottom();
joepeck@webkit.orgaa676ee52014-01-28 04:04:52 +00002373 break;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002374 default:
2375 break;
2376 }
2377 if (height() < newY)
2378 setLogicalHeight(newY);
2379}
2380
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002381LayoutUnit RenderBlockFlow::logicalLeftFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002382{
2383 if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002384 return m_floatingObjects->logicalLeftOffset(fixedOffset, logicalTop, logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002385
2386 return fixedOffset;
2387}
2388
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002389LayoutUnit RenderBlockFlow::logicalRightFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002390{
2391 if (m_floatingObjects && m_floatingObjects->hasRightObjects())
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002392 return m_floatingObjects->logicalRightOffset(fixedOffset, logicalTop, logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002393
2394 return fixedOffset;
2395}
2396
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002397LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelow(LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002398{
2399 if (!m_floatingObjects)
2400 return logicalHeight;
2401
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002402 return m_floatingObjects->findNextFloatLogicalBottomBelow(logicalHeight);
2403}
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002404
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002405LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelowForBlock(LayoutUnit logicalHeight) const
2406{
2407 if (!m_floatingObjects)
2408 return logicalHeight;
2409
2410 return m_floatingObjects->findNextFloatLogicalBottomBelowForBlock(logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002411}
2412
2413LayoutUnit RenderBlockFlow::lowestFloatLogicalBottom(FloatingObject::Type floatType) const
2414{
2415 if (!m_floatingObjects)
2416 return 0;
2417 LayoutUnit lowestFloatBottom = 0;
2418 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2419 auto end = floatingObjectSet.end();
2420 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2421 FloatingObject* floatingObject = it->get();
2422 if (floatingObject->isPlaced() && floatingObject->type() & floatType)
andersca@apple.com86298632013-11-10 19:32:33 +00002423 lowestFloatBottom = std::max(lowestFloatBottom, logicalBottomForFloat(floatingObject));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002424 }
2425 return lowestFloatBottom;
2426}
2427
weinig@apple.com12840dc2013-10-22 23:59:08 +00002428LayoutUnit RenderBlockFlow::addOverhangingFloats(RenderBlockFlow& child, bool makeChildPaintOtherFloats)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002429{
2430 // Prevent floats from being added to the canvas by the root element, e.g., <html>.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002431 if (child.hasOverflowClip() || !child.containsFloats() || child.isRoot() || child.hasColumns() || child.isWritingModeRoot())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002432 return 0;
2433
weinig@apple.com12840dc2013-10-22 23:59:08 +00002434 LayoutUnit childLogicalTop = child.logicalTop();
2435 LayoutUnit childLogicalLeft = child.logicalLeft();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002436 LayoutUnit lowestFloatLogicalBottom = 0;
2437
2438 // Floats that will remain the child's responsibility to paint should factor into its
2439 // overflow.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002440 auto childEnd = child.m_floatingObjects->set().end();
2441 for (auto childIt = child.m_floatingObjects->set().begin(); childIt != childEnd; ++childIt) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002442 FloatingObject* floatingObject = childIt->get();
andersca@apple.com86298632013-11-10 19:32:33 +00002443 LayoutUnit floatLogicalBottom = std::min(logicalBottomForFloat(floatingObject), LayoutUnit::max() - childLogicalTop);
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002444 LayoutUnit logicalBottom = childLogicalTop + floatLogicalBottom;
andersca@apple.com86298632013-11-10 19:32:33 +00002445 lowestFloatLogicalBottom = std::max(lowestFloatLogicalBottom, logicalBottom);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002446
2447 if (logicalBottom > logicalHeight()) {
2448 // If the object is not in the list, we add it now.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002449 if (!containsFloat(floatingObject->renderer())) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002450 LayoutSize offset = isHorizontalWritingMode() ? LayoutSize(-childLogicalLeft, -childLogicalTop) : LayoutSize(-childLogicalTop, -childLogicalLeft);
2451 bool shouldPaint = false;
2452
2453 // The nearest enclosing layer always paints the float (so that zindex and stacking
2454 // behaves properly). We always want to propagate the desire to paint the float as
2455 // far out as we can, to the outermost block that overlaps the float, stopping only
2456 // if we hit a self-painting layer boundary.
2457 if (floatingObject->renderer().enclosingFloatPaintingLayer() == enclosingFloatPaintingLayer()) {
2458 floatingObject->setShouldPaint(false);
2459 shouldPaint = true;
2460 }
2461 // We create the floating object list lazily.
2462 if (!m_floatingObjects)
2463 createFloatingObjects();
2464
2465 m_floatingObjects->add(floatingObject->copyToNewContainer(offset, shouldPaint, true));
2466 }
2467 } else {
2468 if (makeChildPaintOtherFloats && !floatingObject->shouldPaint() && !floatingObject->renderer().hasSelfPaintingLayer()
weinig@apple.com12840dc2013-10-22 23:59:08 +00002469 && floatingObject->renderer().isDescendantOf(&child) && floatingObject->renderer().enclosingFloatPaintingLayer() == child.enclosingFloatPaintingLayer()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002470 // The float is not overhanging from this block, so if it is a descendant of the child, the child should
2471 // paint it (the other case is that it is intruding into the child), unless it has its own layer or enclosing
2472 // layer.
2473 // If makeChildPaintOtherFloats is false, it means that the child must already know about all the floats
2474 // it should paint.
2475 floatingObject->setShouldPaint(true);
2476 }
2477
2478 // 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
2479 // child now.
2480 if (floatingObject->isDescendant())
weinig@apple.com12840dc2013-10-22 23:59:08 +00002481 child.addOverflowFromChild(&floatingObject->renderer(), LayoutSize(xPositionForFloatIncludingMargin(floatingObject), yPositionForFloatIncludingMargin(floatingObject)));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002482 }
2483 }
2484 return lowestFloatLogicalBottom;
2485}
2486
weinig@apple.com12840dc2013-10-22 23:59:08 +00002487bool RenderBlockFlow::hasOverhangingFloat(RenderBox& renderer)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002488{
2489 if (!m_floatingObjects || hasColumns() || !parent())
2490 return false;
2491
2492 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002493 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(renderer);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002494 if (it == floatingObjectSet.end())
2495 return false;
2496
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002497 return logicalBottomForFloat(it->get()) > logicalHeight();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002498}
2499
2500void RenderBlockFlow::addIntrudingFloats(RenderBlockFlow* prev, LayoutUnit logicalLeftOffset, LayoutUnit logicalTopOffset)
2501{
2502 ASSERT(!avoidsFloats());
2503
2504 // If the parent or previous sibling doesn't have any floats to add, don't bother.
2505 if (!prev->m_floatingObjects)
2506 return;
2507
2508 logicalLeftOffset += marginLogicalLeft();
2509
2510 const FloatingObjectSet& prevSet = prev->m_floatingObjects->set();
2511 auto prevEnd = prevSet.end();
2512 for (auto prevIt = prevSet.begin(); prevIt != prevEnd; ++prevIt) {
2513 FloatingObject* floatingObject = prevIt->get();
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002514 if (logicalBottomForFloat(floatingObject) > logicalTopOffset) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002515 if (!m_floatingObjects || !m_floatingObjects->set().contains<FloatingObject&, FloatingObjectHashTranslator>(*floatingObject)) {
2516 // We create the floating object list lazily.
2517 if (!m_floatingObjects)
2518 createFloatingObjects();
2519
2520 // Applying the child's margin makes no sense in the case where the child was passed in.
2521 // since this margin was added already through the modification of the |logicalLeftOffset| variable
2522 // above. |logicalLeftOffset| will equal the margin in this case, so it's already been taken
2523 // into account. Only apply this code if prev is the parent, since otherwise the left margin
2524 // will get applied twice.
2525 LayoutSize offset = isHorizontalWritingMode()
2526 ? LayoutSize(logicalLeftOffset - (prev != parent() ? prev->marginLeft() : LayoutUnit()), logicalTopOffset)
2527 : LayoutSize(logicalTopOffset, logicalLeftOffset - (prev != parent() ? prev->marginTop() : LayoutUnit()));
2528
2529 m_floatingObjects->add(floatingObject->copyToNewContainer(offset));
2530 }
2531 }
2532 }
2533}
2534
2535void RenderBlockFlow::markAllDescendantsWithFloatsForLayout(RenderBox* floatToRemove, bool inLayout)
2536{
2537 if (!everHadLayout() && !containsFloats())
2538 return;
2539
2540 MarkingBehavior markParents = inLayout ? MarkOnlyThis : MarkContainingBlockChain;
2541 setChildNeedsLayout(markParents);
2542
2543 if (floatToRemove)
weinig@apple.com12840dc2013-10-22 23:59:08 +00002544 removeFloatingObject(*floatToRemove);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002545
akling@apple.com525dae62014-01-03 20:22:09 +00002546 if (childrenInline())
2547 return;
2548
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002549 // Iterate over our children and mark them as needed.
akling@apple.com525dae62014-01-03 20:22:09 +00002550 for (auto& block : childrenOfType<RenderBlock>(*this)) {
2551 if (!floatToRemove && block.isFloatingOrOutOfFlowPositioned())
2552 continue;
2553 if (!block.isRenderBlockFlow()) {
2554 if (block.shrinkToAvoidFloats() && block.everHadLayout())
2555 block.setChildNeedsLayout(markParents);
2556 continue;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002557 }
akling@apple.com525dae62014-01-03 20:22:09 +00002558 auto& blockFlow = toRenderBlockFlow(block);
2559 if ((floatToRemove ? blockFlow.containsFloat(*floatToRemove) : blockFlow.containsFloats()) || blockFlow.shrinkToAvoidFloats())
2560 blockFlow.markAllDescendantsWithFloatsForLayout(floatToRemove, inLayout);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002561 }
2562}
2563
2564void RenderBlockFlow::markSiblingsWithFloatsForLayout(RenderBox* floatToRemove)
2565{
2566 if (!m_floatingObjects)
2567 return;
2568
2569 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2570 auto end = floatingObjectSet.end();
2571
2572 for (RenderObject* next = nextSibling(); next; next = next->nextSibling()) {
2573 if (!next->isRenderBlockFlow() || next->isFloatingOrOutOfFlowPositioned() || toRenderBlock(next)->avoidsFloats())
2574 continue;
2575
2576 RenderBlockFlow* nextBlock = toRenderBlockFlow(next);
2577 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002578 RenderBox& floatingBox = (*it)->renderer();
2579 if (floatToRemove && &floatingBox != floatToRemove)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002580 continue;
2581 if (nextBlock->containsFloat(floatingBox))
weinig@apple.com12840dc2013-10-22 23:59:08 +00002582 nextBlock->markAllDescendantsWithFloatsForLayout(&floatingBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002583 }
2584 }
2585}
2586
weinig@apple.com31324fd2013-10-28 19:22:51 +00002587LayoutPoint RenderBlockFlow::flipFloatForWritingModeForChild(const FloatingObject* child, const LayoutPoint& point) const
2588{
akling@apple.com827be9c2013-10-29 02:58:43 +00002589 if (!style().isFlippedBlocksWritingMode())
weinig@apple.com31324fd2013-10-28 19:22:51 +00002590 return point;
2591
2592 // This is similar to RenderBox::flipForWritingModeForChild. We have to subtract out our left/top offsets twice, since
2593 // it's going to get added back in. We hide this complication here so that the calling code looks normal for the unflipped
2594 // case.
2595 if (isHorizontalWritingMode())
2596 return LayoutPoint(point.x(), point.y() + height() - child->renderer().height() - 2 * yPositionForFloatIncludingMargin(child));
2597 return LayoutPoint(point.x() + width() - child->renderer().width() - 2 * xPositionForFloatIncludingMargin(child), point.y());
2598}
2599
weinig@apple.com12840dc2013-10-22 23:59:08 +00002600LayoutUnit RenderBlockFlow::getClearDelta(RenderBox& child, LayoutUnit logicalTop)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002601{
2602 // There is no need to compute clearance if we have no floats.
2603 if (!containsFloats())
2604 return 0;
2605
2606 // At least one float is present. We need to perform the clearance computation.
akling@apple.com827be9c2013-10-29 02:58:43 +00002607 bool clearSet = child.style().clear() != CNONE;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002608 LayoutUnit logicalBottom = 0;
akling@apple.com827be9c2013-10-29 02:58:43 +00002609 switch (child.style().clear()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002610 case CNONE:
2611 break;
2612 case CLEFT:
2613 logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
2614 break;
2615 case CRIGHT:
2616 logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatRight);
2617 break;
2618 case CBOTH:
2619 logicalBottom = lowestFloatLogicalBottom();
2620 break;
2621 }
2622
2623 // 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 +00002624 LayoutUnit result = clearSet ? std::max<LayoutUnit>(0, logicalBottom - logicalTop) : LayoutUnit();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002625 if (!result && child.avoidsFloats()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002626 LayoutUnit newLogicalTop = logicalTop;
2627 while (true) {
2628 LayoutUnit availableLogicalWidthAtNewLogicalTopOffset = availableLogicalWidthForLine(newLogicalTop, false, logicalHeightForChild(child));
2629 if (availableLogicalWidthAtNewLogicalTopOffset == availableLogicalWidthForContent(newLogicalTop))
2630 return newLogicalTop - logicalTop;
2631
2632 RenderRegion* region = regionAtBlockOffset(logicalTopForChild(child));
weinig@apple.com12840dc2013-10-22 23:59:08 +00002633 LayoutRect borderBox = child.borderBoxRectInRegion(region, DoNotCacheRenderBoxRegionInfo);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002634 LayoutUnit childLogicalWidthAtOldLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
2635
2636 // FIXME: None of this is right for perpendicular writing-mode children.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002637 LayoutUnit childOldLogicalWidth = child.logicalWidth();
2638 LayoutUnit childOldMarginLeft = child.marginLeft();
2639 LayoutUnit childOldMarginRight = child.marginRight();
2640 LayoutUnit childOldLogicalTop = child.logicalTop();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002641
weinig@apple.com12840dc2013-10-22 23:59:08 +00002642 child.setLogicalTop(newLogicalTop);
2643 child.updateLogicalWidth();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002644 region = regionAtBlockOffset(logicalTopForChild(child));
weinig@apple.com12840dc2013-10-22 23:59:08 +00002645 borderBox = child.borderBoxRectInRegion(region, DoNotCacheRenderBoxRegionInfo);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002646 LayoutUnit childLogicalWidthAtNewLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
2647
weinig@apple.com12840dc2013-10-22 23:59:08 +00002648 child.setLogicalTop(childOldLogicalTop);
2649 child.setLogicalWidth(childOldLogicalWidth);
2650 child.setMarginLeft(childOldMarginLeft);
2651 child.setMarginRight(childOldMarginRight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002652
2653 if (childLogicalWidthAtNewLogicalTopOffset <= availableLogicalWidthAtNewLogicalTopOffset) {
2654 // Even though we may not be moving, if the logical width did shrink because of the presence of new floats, then
2655 // we need to force a relayout as though we shifted. This happens because of the dynamic addition of overhanging floats
2656 // from previous siblings when negative margins exist on a child (see the addOverhangingFloats call at the end of collapseMargins).
2657 if (childLogicalWidthAtOldLogicalTopOffset != childLogicalWidthAtNewLogicalTopOffset)
weinig@apple.com12840dc2013-10-22 23:59:08 +00002658 child.setChildNeedsLayout(MarkOnlyThis);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002659 return newLogicalTop - logicalTop;
2660 }
2661
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002662 newLogicalTop = nextFloatLogicalBottomBelowForBlock(newLogicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002663 ASSERT(newLogicalTop >= logicalTop);
2664 if (newLogicalTop < logicalTop)
2665 break;
2666 }
2667 ASSERT_NOT_REACHED();
2668 }
2669 return result;
2670}
2671
2672bool RenderBlockFlow::hitTestFloats(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset)
2673{
2674 if (!m_floatingObjects)
2675 return false;
2676
2677 LayoutPoint adjustedLocation = accumulatedOffset;
2678 if (isRenderView())
2679 adjustedLocation += toLayoutSize(toRenderView(*this).frameView().scrollPosition());
2680
2681 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2682 auto begin = floatingObjectSet.begin();
2683 for (auto it = floatingObjectSet.end(); it != begin;) {
2684 --it;
2685 FloatingObject* floatingObject = it->get();
2686 if (floatingObject->shouldPaint() && !floatingObject->renderer().hasSelfPaintingLayer()) {
2687 LayoutUnit xOffset = xPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer().x();
2688 LayoutUnit yOffset = yPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer().y();
2689 LayoutPoint childPoint = flipFloatForWritingModeForChild(floatingObject, adjustedLocation + LayoutSize(xOffset, yOffset));
2690 if (floatingObject->renderer().hitTest(request, result, locationInContainer, childPoint)) {
2691 updateHitTestResult(result, locationInContainer.point() - toLayoutSize(childPoint));
2692 return true;
2693 }
2694 }
2695 }
2696
2697 return false;
2698}
2699
weinig@apple.com611b9292013-10-20 22:57:54 +00002700bool RenderBlockFlow::hitTestInlineChildren(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
2701{
2702 ASSERT(childrenInline());
antti@apple.com940f5872013-10-24 20:31:11 +00002703
antti@apple.comfea51992013-10-28 13:39:23 +00002704 if (m_simpleLineLayout)
2705 return SimpleLineLayout::hitTestFlow(*this, *m_simpleLineLayout, request, result, locationInContainer, accumulatedOffset, hitTestAction);
antti@apple.com940f5872013-10-24 20:31:11 +00002706
weinig@apple.com611b9292013-10-20 22:57:54 +00002707 return m_lineBoxes.hitTest(this, request, result, locationInContainer, accumulatedOffset, hitTestAction);
2708}
2709
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002710void RenderBlockFlow::adjustForBorderFit(LayoutUnit x, LayoutUnit& left, LayoutUnit& right) const
2711{
akling@apple.com827be9c2013-10-29 02:58:43 +00002712 if (style().visibility() != VISIBLE)
weinig@apple.com611b9292013-10-20 22:57:54 +00002713 return;
2714
2715 // We don't deal with relative positioning. Our assumption is that you shrink to fit the lines without accounting
2716 // for either overflow or translations via relative positioning.
2717 if (childrenInline()) {
antti@apple.com940f5872013-10-24 20:31:11 +00002718 const_cast<RenderBlockFlow&>(*this).ensureLineBoxes();
2719
weinig@apple.com611b9292013-10-20 22:57:54 +00002720 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
2721 if (box->firstChild())
andersca@apple.com86298632013-11-10 19:32:33 +00002722 left = std::min(left, x + static_cast<LayoutUnit>(box->firstChild()->x()));
weinig@apple.com611b9292013-10-20 22:57:54 +00002723 if (box->lastChild())
andersca@apple.com86298632013-11-10 19:32:33 +00002724 right = std::max(right, x + static_cast<LayoutUnit>(ceilf(box->lastChild()->logicalRight())));
weinig@apple.com611b9292013-10-20 22:57:54 +00002725 }
2726 } else {
2727 for (RenderBox* obj = firstChildBox(); obj; obj = obj->nextSiblingBox()) {
2728 if (!obj->isFloatingOrOutOfFlowPositioned()) {
2729 if (obj->isRenderBlockFlow() && !obj->hasOverflowClip())
2730 toRenderBlockFlow(obj)->adjustForBorderFit(x + obj->x(), left, right);
akling@apple.com827be9c2013-10-29 02:58:43 +00002731 else if (obj->style().visibility() == VISIBLE) {
weinig@apple.com611b9292013-10-20 22:57:54 +00002732 // We are a replaced element or some kind of non-block-flow object.
andersca@apple.com86298632013-11-10 19:32:33 +00002733 left = std::min(left, x + obj->x());
2734 right = std::max(right, x + obj->x() + obj->width());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002735 }
2736 }
2737 }
2738 }
weinig@apple.com611b9292013-10-20 22:57:54 +00002739
2740 if (m_floatingObjects) {
2741 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2742 auto end = floatingObjectSet.end();
2743 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2744 FloatingObject* r = it->get();
2745 // Only examine the object if our m_shouldPaint flag is set.
2746 if (r->shouldPaint()) {
2747 LayoutUnit floatLeft = xPositionForFloatIncludingMargin(r) - r->renderer().x();
2748 LayoutUnit floatRight = floatLeft + r->renderer().width();
andersca@apple.com86298632013-11-10 19:32:33 +00002749 left = std::min(left, floatLeft);
2750 right = std::max(right, floatRight);
weinig@apple.com611b9292013-10-20 22:57:54 +00002751 }
2752 }
2753 }
2754}
2755
2756void RenderBlockFlow::fitBorderToLinesIfNeeded()
2757{
akling@apple.com827be9c2013-10-29 02:58:43 +00002758 if (style().borderFit() == BorderFitBorder || hasOverrideWidth())
weinig@apple.com611b9292013-10-20 22:57:54 +00002759 return;
2760
2761 // Walk any normal flow lines to snugly fit.
2762 LayoutUnit left = LayoutUnit::max();
2763 LayoutUnit right = LayoutUnit::min();
2764 LayoutUnit oldWidth = contentWidth();
2765 adjustForBorderFit(0, left, right);
2766
2767 // Clamp to our existing edges. We can never grow. We only shrink.
2768 LayoutUnit leftEdge = borderLeft() + paddingLeft();
2769 LayoutUnit rightEdge = leftEdge + oldWidth;
andersca@apple.com86298632013-11-10 19:32:33 +00002770 left = std::min(rightEdge, std::max(leftEdge, left));
2771 right = std::max(leftEdge, std::min(rightEdge, right));
weinig@apple.com611b9292013-10-20 22:57:54 +00002772
2773 LayoutUnit newContentWidth = right - left;
2774 if (newContentWidth == oldWidth)
2775 return;
2776
2777 setOverrideLogicalContentWidth(newContentWidth);
2778 layoutBlock(false);
2779 clearOverrideLogicalContentWidth();
2780}
2781
2782void RenderBlockFlow::markLinesDirtyInBlockRange(LayoutUnit logicalTop, LayoutUnit logicalBottom, RootInlineBox* highest)
2783{
2784 if (logicalTop >= logicalBottom)
2785 return;
2786
2787 RootInlineBox* lowestDirtyLine = lastRootBox();
2788 RootInlineBox* afterLowest = lowestDirtyLine;
2789 while (lowestDirtyLine && lowestDirtyLine->lineBottomWithLeading() >= logicalBottom && logicalBottom < LayoutUnit::max()) {
2790 afterLowest = lowestDirtyLine;
2791 lowestDirtyLine = lowestDirtyLine->prevRootBox();
2792 }
2793
2794 while (afterLowest && afterLowest != highest && (afterLowest->lineBottomWithLeading() >= logicalTop || afterLowest->lineBottomWithLeading() < 0)) {
2795 afterLowest->markDirty();
2796 afterLowest = afterLowest->prevRootBox();
2797 }
2798}
2799
antti@apple.com0e632aa2013-10-22 21:03:38 +00002800int RenderBlockFlow::firstLineBaseline() const
weinig@apple.com611b9292013-10-20 22:57:54 +00002801{
2802 if (isWritingModeRoot() && !isRubyRun())
2803 return -1;
2804
2805 if (!childrenInline())
antti@apple.com0e632aa2013-10-22 21:03:38 +00002806 return RenderBlock::firstLineBaseline();
weinig@apple.com611b9292013-10-20 22:57:54 +00002807
antti@apple.com940f5872013-10-24 20:31:11 +00002808 if (!hasLines())
2809 return -1;
weinig@apple.com611b9292013-10-20 22:57:54 +00002810
antti@apple.comfea51992013-10-28 13:39:23 +00002811 if (m_simpleLineLayout)
2812 return SimpleLineLayout::computeFlowFirstLineBaseline(*this, *m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00002813
akling@apple.comee3c8df2013-11-06 08:09:44 +00002814 ASSERT(firstRootBox());
2815 return firstRootBox()->logicalTop() + firstLineStyle().fontMetrics().ascent(firstRootBox()->baselineType());
weinig@apple.com611b9292013-10-20 22:57:54 +00002816}
2817
2818int RenderBlockFlow::inlineBlockBaseline(LineDirectionMode lineDirection) const
2819{
2820 if (isWritingModeRoot() && !isRubyRun())
2821 return -1;
2822
2823 if (!childrenInline())
2824 return RenderBlock::inlineBlockBaseline(lineDirection);
2825
antti@apple.com0e632aa2013-10-22 21:03:38 +00002826 if (!hasLines()) {
2827 if (!hasLineIfEmpty())
2828 return -1;
akling@apple.com827be9c2013-10-29 02:58:43 +00002829 const FontMetrics& fontMetrics = firstLineStyle().fontMetrics();
weinig@apple.com611b9292013-10-20 22:57:54 +00002830 return fontMetrics.ascent()
2831 + (lineHeight(true, lineDirection, PositionOfInteriorLineBoxes) - fontMetrics.height()) / 2
2832 + (lineDirection == HorizontalLine ? borderTop() + paddingTop() : borderRight() + paddingRight());
2833 }
2834
antti@apple.comfea51992013-10-28 13:39:23 +00002835 if (m_simpleLineLayout)
2836 return SimpleLineLayout::computeFlowLastLineBaseline(*this, *m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00002837
akling@apple.comee3c8df2013-11-06 08:09:44 +00002838 bool isFirstLine = lastRootBox() == firstRootBox();
akling@apple.com827be9c2013-10-29 02:58:43 +00002839 const RenderStyle& style = isFirstLine ? firstLineStyle() : this->style();
akling@apple.comee3c8df2013-11-06 08:09:44 +00002840 return lastRootBox()->logicalTop() + style.fontMetrics().ascent(lastRootBox()->baselineType());
weinig@apple.com611b9292013-10-20 22:57:54 +00002841}
2842
weinig@apple.com12840dc2013-10-22 23:59:08 +00002843GapRects RenderBlockFlow::inlineSelectionGaps(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
weinig@apple.com611b9292013-10-20 22:57:54 +00002844 LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
2845{
antti@apple.comfea51992013-10-28 13:39:23 +00002846 ASSERT(!m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00002847
weinig@apple.com611b9292013-10-20 22:57:54 +00002848 GapRects result;
2849
2850 bool containsStart = selectionState() == SelectionStart || selectionState() == SelectionBoth;
2851
antti@apple.com0e632aa2013-10-22 21:03:38 +00002852 if (!hasLines()) {
weinig@apple.com611b9292013-10-20 22:57:54 +00002853 if (containsStart) {
2854 // Go ahead and update our lastLogicalTop to be the bottom of the block. <hr>s or empty blocks with height can trip this
2855 // case.
2856 lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalHeight();
2857 lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, logicalHeight(), cache);
2858 lastLogicalRight = logicalRightSelectionOffset(rootBlock, logicalHeight(), cache);
2859 }
2860 return result;
2861 }
2862
2863 RootInlineBox* lastSelectedLine = 0;
2864 RootInlineBox* curr;
2865 for (curr = firstRootBox(); curr && !curr->hasSelectedChildren(); curr = curr->nextRootBox()) { }
2866
2867 // Now paint the gaps for the lines.
2868 for (; curr && curr->hasSelectedChildren(); curr = curr->nextRootBox()) {
2869 LayoutUnit selTop = curr->selectionTopAdjustedForPrecedingBlock();
2870 LayoutUnit selHeight = curr->selectionHeightAdjustedForPrecedingBlock();
2871
2872 if (!containsStart && !lastSelectedLine &&
2873 selectionState() != SelectionStart && selectionState() != SelectionBoth)
2874 result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop, lastLogicalLeft, lastLogicalRight, selTop, cache, paintInfo));
2875
2876 LayoutRect logicalRect(curr->logicalLeft(), selTop, curr->logicalWidth(), selTop + selHeight);
2877 logicalRect.move(isHorizontalWritingMode() ? offsetFromRootBlock : offsetFromRootBlock.transposedSize());
weinig@apple.com12840dc2013-10-22 23:59:08 +00002878 LayoutRect physicalRect = rootBlock.logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
weinig@apple.com611b9292013-10-20 22:57:54 +00002879 if (!paintInfo || (isHorizontalWritingMode() && physicalRect.y() < paintInfo->rect.maxY() && physicalRect.maxY() > paintInfo->rect.y())
2880 || (!isHorizontalWritingMode() && physicalRect.x() < paintInfo->rect.maxX() && physicalRect.maxX() > paintInfo->rect.x()))
2881 result.unite(curr->lineSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, selTop, selHeight, cache, paintInfo));
2882
2883 lastSelectedLine = curr;
2884 }
2885
2886 if (containsStart && !lastSelectedLine)
2887 // VisibleSelection must start just after our last line.
2888 lastSelectedLine = lastRootBox();
2889
2890 if (lastSelectedLine && selectionState() != SelectionEnd && selectionState() != SelectionBoth) {
2891 // Go ahead and update our lastY to be the bottom of the last selected line.
2892 lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + lastSelectedLine->selectionBottom();
2893 lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
2894 lastLogicalRight = logicalRightSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
2895 }
2896 return result;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002897}
2898
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002899void RenderBlockFlow::createRenderNamedFlowFragmentIfNeeded()
2900{
abucur@adobe.com0e81bc72013-10-22 14:50:37 +00002901 if (!document().cssRegionsEnabled() || renderNamedFlowFragment() || isRenderNamedFlowFragment())
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002902 return;
2903
akling@apple.com827be9c2013-10-29 02:58:43 +00002904 if (style().isDisplayRegionType() && style().hasFlowFrom()) {
2905 RenderNamedFlowFragment* flowFragment = new RenderNamedFlowFragment(document(), RenderNamedFlowFragment::createStyle(style()));
akling@apple.com8f40c5b2013-10-27 22:54:07 +00002906 flowFragment->initializeStyle();
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002907 setRenderNamedFlowFragment(flowFragment);
2908 addChild(renderNamedFlowFragment());
2909 }
2910}
2911
2912bool RenderBlockFlow::canHaveChildren() const
2913{
2914 return !renderNamedFlowFragment() ? RenderBlock::canHaveChildren() : renderNamedFlowFragment()->canHaveChildren();
2915}
2916
2917bool RenderBlockFlow::canHaveGeneratedChildren() const
2918{
2919 return !renderNamedFlowFragment() ? RenderBlock::canHaveGeneratedChildren() : renderNamedFlowFragment()->canHaveGeneratedChildren();
2920}
2921
2922bool RenderBlockFlow::namedFlowFragmentNeedsUpdate() const
2923{
2924 if (!isRenderNamedFlowFragmentContainer())
2925 return false;
2926
2927 return hasRelativeLogicalHeight() && !isRenderView();
2928}
2929
2930void RenderBlockFlow::updateLogicalHeight()
2931{
2932 RenderBlock::updateLogicalHeight();
2933
2934 if (renderNamedFlowFragment())
andersca@apple.com86298632013-11-10 19:32:33 +00002935 renderNamedFlowFragment()->setLogicalHeight(std::max<LayoutUnit>(0, logicalHeight() - borderAndPaddingLogicalHeight()));
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002936}
2937
2938void RenderBlockFlow::setRenderNamedFlowFragment(RenderNamedFlowFragment* flowFragment)
2939{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00002940 RenderBlockFlowRareData& rareData = ensureRareBlockFlowData();
abucur@adobe.com0e81bc72013-10-22 14:50:37 +00002941 if (rareData.m_renderNamedFlowFragment)
2942 rareData.m_renderNamedFlowFragment->destroy();
2943 rareData.m_renderNamedFlowFragment = flowFragment;
2944}
2945
hyatt@apple.come9fe3d32014-01-24 17:14:22 +00002946void RenderBlockFlow::setMultiColumnFlowThread(RenderMultiColumnFlowThread* flowThread)
2947{
2948 RenderBlockFlowRareData& rareData = ensureRareBlockFlowData();
2949 rareData.m_multiColumnFlowThread = flowThread;
2950}
2951
akling@apple.com525dae62014-01-03 20:22:09 +00002952static bool shouldCheckLines(const RenderBlockFlow& blockFlow)
weinig@apple.com17140912013-10-19 19:55:40 +00002953{
akling@apple.com525dae62014-01-03 20:22:09 +00002954 return !blockFlow.isFloatingOrOutOfFlowPositioned() && !blockFlow.isRunIn() && blockFlow.style().height().isAuto();
weinig@apple.com17140912013-10-19 19:55:40 +00002955}
2956
2957RootInlineBox* RenderBlockFlow::lineAtIndex(int i) const
2958{
2959 ASSERT(i >= 0);
2960
akling@apple.com827be9c2013-10-29 02:58:43 +00002961 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00002962 return nullptr;
2963
2964 if (childrenInline()) {
2965 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
2966 if (!i--)
2967 return box;
2968 }
akling@apple.com525dae62014-01-03 20:22:09 +00002969 return nullptr;
2970 }
2971
2972 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
2973 if (!shouldCheckLines(blockFlow))
2974 continue;
2975 if (RootInlineBox* box = blockFlow.lineAtIndex(i))
2976 return box;
weinig@apple.com17140912013-10-19 19:55:40 +00002977 }
2978
2979 return nullptr;
2980}
2981
2982int RenderBlockFlow::lineCount(const RootInlineBox* stopRootInlineBox, bool* found) const
2983{
akling@apple.com827be9c2013-10-29 02:58:43 +00002984 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00002985 return 0;
2986
2987 int count = 0;
2988
2989 if (childrenInline()) {
2990 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
2991 count++;
2992 if (box == stopRootInlineBox) {
2993 if (found)
2994 *found = true;
2995 break;
2996 }
2997 }
akling@apple.com525dae62014-01-03 20:22:09 +00002998 return count;
2999 }
3000
3001 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
3002 if (!shouldCheckLines(blockFlow))
3003 continue;
3004 bool recursiveFound = false;
3005 count += blockFlow.lineCount(stopRootInlineBox, &recursiveFound);
3006 if (recursiveFound) {
3007 if (found)
3008 *found = true;
3009 break;
weinig@apple.com17140912013-10-19 19:55:40 +00003010 }
3011 }
3012
3013 return count;
3014}
3015
3016static int getHeightForLineCount(const RenderBlockFlow& block, int lineCount, bool includeBottom, int& count)
3017{
akling@apple.com827be9c2013-10-29 02:58:43 +00003018 if (block.style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00003019 return -1;
3020
3021 if (block.childrenInline()) {
3022 for (auto box = block.firstRootBox(); box; box = box->nextRootBox()) {
3023 if (++count == lineCount)
3024 return box->lineBottom() + (includeBottom ? (block.borderBottom() + block.paddingBottom()) : LayoutUnit());
3025 }
3026 } else {
3027 RenderBox* normalFlowChildWithoutLines = 0;
3028 for (auto obj = block.firstChildBox(); obj; obj = obj->nextSiblingBox()) {
akling@apple.com525dae62014-01-03 20:22:09 +00003029 if (obj->isRenderBlockFlow() && shouldCheckLines(toRenderBlockFlow(*obj))) {
weinig@apple.com17140912013-10-19 19:55:40 +00003030 int result = getHeightForLineCount(toRenderBlockFlow(*obj), lineCount, false, count);
3031 if (result != -1)
3032 return result + obj->y() + (includeBottom ? (block.borderBottom() + block.paddingBottom()) : LayoutUnit());
3033 } else if (!obj->isFloatingOrOutOfFlowPositioned() && !obj->isRunIn())
3034 normalFlowChildWithoutLines = obj;
3035 }
3036 if (normalFlowChildWithoutLines && !lineCount)
3037 return normalFlowChildWithoutLines->y() + normalFlowChildWithoutLines->height();
3038 }
3039
3040 return -1;
3041}
3042
3043int RenderBlockFlow::heightForLineCount(int lineCount)
3044{
3045 int count = 0;
3046 return getHeightForLineCount(*this, lineCount, true, count);
3047}
3048
3049void RenderBlockFlow::clearTruncation()
3050{
akling@apple.com827be9c2013-10-29 02:58:43 +00003051 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00003052 return;
3053
3054 if (childrenInline() && hasMarkupTruncation()) {
antti@apple.com940f5872013-10-24 20:31:11 +00003055 ensureLineBoxes();
3056
weinig@apple.com17140912013-10-19 19:55:40 +00003057 setHasMarkupTruncation(false);
3058 for (auto box = firstRootBox(); box; box = box->nextRootBox())
3059 box->clearTruncation();
akling@apple.com525dae62014-01-03 20:22:09 +00003060 return;
3061 }
3062
3063 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
3064 if (shouldCheckLines(blockFlow))
3065 blockFlow.clearTruncation();
weinig@apple.com17140912013-10-19 19:55:40 +00003066 }
3067}
3068
weinig@apple.com3f23b382013-10-19 20:26:58 +00003069bool RenderBlockFlow::containsNonZeroBidiLevel() const
3070{
3071 for (auto root = firstRootBox(); root; root = root->nextRootBox()) {
3072 for (auto box = root->firstLeafChild(); box; box = box->nextLeafChild()) {
3073 if (box->bidiLevel())
3074 return true;
3075 }
3076 }
3077 return false;
3078}
3079
weinig@apple.com611b9292013-10-20 22:57:54 +00003080Position RenderBlockFlow::positionForBox(InlineBox *box, bool start) const
3081{
3082 if (!box)
3083 return Position();
3084
3085 if (!box->renderer().nonPseudoNode())
3086 return createLegacyEditingPosition(nonPseudoElement(), start ? caretMinOffset() : caretMaxOffset());
3087
3088 if (!box->isInlineTextBox())
3089 return createLegacyEditingPosition(box->renderer().nonPseudoNode(), start ? box->renderer().caretMinOffset() : box->renderer().caretMaxOffset());
3090
3091 InlineTextBox* textBox = toInlineTextBox(box);
3092 return createLegacyEditingPosition(box->renderer().nonPseudoNode(), start ? textBox->start() : textBox->start() + textBox->len());
3093}
3094
3095VisiblePosition RenderBlockFlow::positionForPointWithInlineChildren(const LayoutPoint& pointInLogicalContents)
3096{
3097 ASSERT(childrenInline());
3098
antti@apple.com940f5872013-10-24 20:31:11 +00003099 ensureLineBoxes();
3100
weinig@apple.com611b9292013-10-20 22:57:54 +00003101 if (!firstRootBox())
3102 return createVisiblePosition(0, DOWNSTREAM);
3103
akling@apple.com827be9c2013-10-29 02:58:43 +00003104 bool linesAreFlipped = style().isFlippedLinesWritingMode();
3105 bool blocksAreFlipped = style().isFlippedBlocksWritingMode();
weinig@apple.com611b9292013-10-20 22:57:54 +00003106
3107 // look for the closest line box in the root box which is at the passed-in y coordinate
3108 InlineBox* closestBox = 0;
3109 RootInlineBox* firstRootBoxWithChildren = 0;
3110 RootInlineBox* lastRootBoxWithChildren = 0;
3111 for (RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox()) {
3112 if (!root->firstLeafChild())
3113 continue;
3114 if (!firstRootBoxWithChildren)
3115 firstRootBoxWithChildren = root;
3116
3117 if (!linesAreFlipped && root->isFirstAfterPageBreak() && (pointInLogicalContents.y() < root->lineTopWithLeading()
3118 || (blocksAreFlipped && pointInLogicalContents.y() == root->lineTopWithLeading())))
3119 break;
3120
3121 lastRootBoxWithChildren = root;
3122
3123 // check if this root line box is located at this y coordinate
3124 if (pointInLogicalContents.y() < root->selectionBottom() || (blocksAreFlipped && pointInLogicalContents.y() == root->selectionBottom())) {
3125 if (linesAreFlipped) {
3126 RootInlineBox* nextRootBoxWithChildren = root->nextRootBox();
3127 while (nextRootBoxWithChildren && !nextRootBoxWithChildren->firstLeafChild())
3128 nextRootBoxWithChildren = nextRootBoxWithChildren->nextRootBox();
3129
3130 if (nextRootBoxWithChildren && nextRootBoxWithChildren->isFirstAfterPageBreak() && (pointInLogicalContents.y() > nextRootBoxWithChildren->lineTopWithLeading()
3131 || (!blocksAreFlipped && pointInLogicalContents.y() == nextRootBoxWithChildren->lineTopWithLeading())))
3132 continue;
3133 }
3134 closestBox = root->closestLeafChildForLogicalLeftPosition(pointInLogicalContents.x());
3135 if (closestBox)
3136 break;
3137 }
3138 }
3139
3140 bool moveCaretToBoundary = frame().editor().behavior().shouldMoveCaretToHorizontalBoundaryWhenPastTopOrBottom();
3141
3142 if (!moveCaretToBoundary && !closestBox && lastRootBoxWithChildren) {
3143 // y coordinate is below last root line box, pretend we hit it
3144 closestBox = lastRootBoxWithChildren->closestLeafChildForLogicalLeftPosition(pointInLogicalContents.x());
3145 }
3146
3147 if (closestBox) {
3148 if (moveCaretToBoundary) {
andersca@apple.com86298632013-11-10 19:32:33 +00003149 LayoutUnit firstRootBoxWithChildrenTop = std::min<LayoutUnit>(firstRootBoxWithChildren->selectionTop(), firstRootBoxWithChildren->logicalTop());
weinig@apple.com611b9292013-10-20 22:57:54 +00003150 if (pointInLogicalContents.y() < firstRootBoxWithChildrenTop
3151 || (blocksAreFlipped && pointInLogicalContents.y() == firstRootBoxWithChildrenTop)) {
3152 InlineBox* box = firstRootBoxWithChildren->firstLeafChild();
3153 if (box->isLineBreak()) {
3154 if (InlineBox* newBox = box->nextLeafChildIgnoringLineBreak())
3155 box = newBox;
3156 }
3157 // y coordinate is above first root line box, so return the start of the first
3158 return VisiblePosition(positionForBox(box, true), DOWNSTREAM);
3159 }
3160 }
3161
3162 // pass the box a top position that is inside it
3163 LayoutPoint point(pointInLogicalContents.x(), closestBox->root().blockDirectionPointInLine());
3164 if (!isHorizontalWritingMode())
3165 point = point.transposedPoint();
3166 if (closestBox->renderer().isReplaced())
weinig@apple.com12840dc2013-10-22 23:59:08 +00003167 return positionForPointRespectingEditingBoundaries(*this, toRenderBox(closestBox->renderer()), point);
weinig@apple.com611b9292013-10-20 22:57:54 +00003168 return closestBox->renderer().positionForPoint(point);
3169 }
3170
3171 if (lastRootBoxWithChildren) {
3172 // We hit this case for Mac behavior when the Y coordinate is below the last box.
3173 ASSERT(moveCaretToBoundary);
3174 InlineBox* logicallyLastBox;
3175 if (lastRootBoxWithChildren->getLogicalEndBoxWithNode(logicallyLastBox))
3176 return VisiblePosition(positionForBox(logicallyLastBox, false), DOWNSTREAM);
3177 }
3178
3179 // Can't reach this. We have a root line box, but it has no kids.
3180 // FIXME: This should ASSERT_NOT_REACHED(), but clicking on placeholder text
3181 // seems to hit this code path.
3182 return createVisiblePosition(0, DOWNSTREAM);
3183}
3184
commit-queue@webkit.org5ce6c902013-11-11 18:21:05 +00003185VisiblePosition RenderBlockFlow::positionForPoint(const LayoutPoint& point)
3186{
3187 if (auto fragment = renderNamedFlowFragment())
3188 return fragment->positionForPoint(point);
3189 return RenderBlock::positionForPoint(point);
3190}
3191
3192
weinig@apple.com611b9292013-10-20 22:57:54 +00003193void RenderBlockFlow::addFocusRingRectsForInlineChildren(Vector<IntRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject*)
3194{
antti@apple.com940f5872013-10-24 20:31:11 +00003195 ASSERT(childrenInline());
3196
3197 ensureLineBoxes();
3198
weinig@apple.com611b9292013-10-20 22:57:54 +00003199 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
andersca@apple.com86298632013-11-10 19:32:33 +00003200 LayoutUnit top = std::max<LayoutUnit>(curr->lineTop(), curr->top());
3201 LayoutUnit bottom = std::min<LayoutUnit>(curr->lineBottom(), curr->top() + curr->height());
weinig@apple.com611b9292013-10-20 22:57:54 +00003202 LayoutRect rect(additionalOffset.x() + curr->x(), additionalOffset.y() + top, curr->width(), bottom - top);
3203 if (!rect.isEmpty())
3204 rects.append(pixelSnappedIntRect(rect));
3205 }
3206}
3207
3208void RenderBlockFlow::paintInlineChildren(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
3209{
3210 ASSERT(childrenInline());
antti@apple.com940f5872013-10-24 20:31:11 +00003211
antti@apple.comfea51992013-10-28 13:39:23 +00003212 if (m_simpleLineLayout) {
3213 SimpleLineLayout::paintFlow(*this, *m_simpleLineLayout, paintInfo, paintOffset);
antti@apple.com940f5872013-10-24 20:31:11 +00003214 return;
3215 }
weinig@apple.com611b9292013-10-20 22:57:54 +00003216 m_lineBoxes.paint(this, paintInfo, paintOffset);
3217}
3218
3219bool RenderBlockFlow::relayoutForPagination(bool hasSpecifiedPageLogicalHeight, LayoutUnit pageLogicalHeight, LayoutStateMaintainer& statePusher)
3220{
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003221 if (!hasColumns() && !multiColumnFlowThread())
weinig@apple.com611b9292013-10-20 22:57:54 +00003222 return false;
3223
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003224 if (hasColumns()) {
3225 RefPtr<RenderOverflow> savedOverflow = m_overflow.release();
3226 if (childrenInline())
3227 addOverflowFromInlineChildren();
3228 else
3229 addOverflowFromBlockChildren();
3230 LayoutUnit layoutOverflowLogicalBottom = (isHorizontalWritingMode() ? layoutOverflowRect().maxY() : layoutOverflowRect().maxX()) - borderAndPaddingBefore();
weinig@apple.com611b9292013-10-20 22:57:54 +00003231
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003232 // FIXME: We don't balance properly at all in the presence of forced page breaks. We need to understand what
3233 // the distance between forced page breaks is so that we can avoid making the minimum column height too tall.
3234 ColumnInfo* colInfo = columnInfo();
3235 if (!hasSpecifiedPageLogicalHeight) {
3236 LayoutUnit columnHeight = pageLogicalHeight;
3237 int minColumnCount = colInfo->forcedBreaks() + 1;
3238 int desiredColumnCount = colInfo->desiredColumnCount();
3239 if (minColumnCount >= desiredColumnCount) {
3240 // The forced page breaks are in control of the balancing. Just set the column height to the
3241 // maximum page break distance.
3242 if (!pageLogicalHeight) {
3243 LayoutUnit distanceBetweenBreaks = std::max<LayoutUnit>(colInfo->maximumDistanceBetweenForcedBreaks(),
3244 view().layoutState()->pageLogicalOffset(this, borderAndPaddingBefore() + layoutOverflowLogicalBottom) - colInfo->forcedBreakOffset());
3245 columnHeight = std::max(colInfo->minimumColumnHeight(), distanceBetweenBreaks);
3246 }
3247 } else if (layoutOverflowLogicalBottom > boundedMultiply(pageLogicalHeight, desiredColumnCount)) {
3248 // Now that we know the intrinsic height of the columns, we have to rebalance them.
3249 columnHeight = std::max<LayoutUnit>(colInfo->minimumColumnHeight(), ceilf((float)layoutOverflowLogicalBottom / desiredColumnCount));
weinig@apple.com611b9292013-10-20 22:57:54 +00003250 }
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003251
3252 if (columnHeight && columnHeight != pageLogicalHeight) {
3253 statePusher.pop();
3254 setEverHadLayout(true);
3255 layoutBlock(false, columnHeight);
3256 return true;
3257 }
3258 }
weinig@apple.com611b9292013-10-20 22:57:54 +00003259
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003260 if (pageLogicalHeight)
3261 colInfo->setColumnCountAndHeight(ceilf((float)layoutOverflowLogicalBottom / pageLogicalHeight), pageLogicalHeight);
weinig@apple.com611b9292013-10-20 22:57:54 +00003262
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003263 if (columnCount(colInfo)) {
3264 setLogicalHeight(borderAndPaddingBefore() + colInfo->columnHeight() + borderAndPaddingAfter() + scrollbarLogicalHeight());
3265 clearOverflow();
3266 } else
3267 m_overflow = savedOverflow.release();
3268 return false;
3269 }
weinig@apple.com611b9292013-10-20 22:57:54 +00003270
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003271 if (!multiColumnFlowThread()->shouldRelayoutForPagination())
3272 return false;
3273
3274 multiColumnFlowThread()->setNeedsRebalancing(false);
3275 multiColumnFlowThread()->setInBalancingPass(true); // Prevent re-entering this method (and recursion into layout).
3276
3277 bool needsRelayout;
3278 bool neededRelayout = false;
3279 bool firstPass = true;
3280 do {
3281 // Column heights may change here because of balancing. We may have to do multiple layout
3282 // passes, depending on how the contents is fitted to the changed column heights. In most
3283 // cases, laying out again twice or even just once will suffice. Sometimes we need more
3284 // passes than that, though, but the number of retries should not exceed the number of
3285 // columns, unless we have a bug.
3286 needsRelayout = false;
3287 for (RenderBox* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox())
3288 if (childBox != multiColumnFlowThread() && childBox->isRenderMultiColumnSet()) {
3289 RenderMultiColumnSet* multicolSet = toRenderMultiColumnSet(childBox);
3290 if (multicolSet->recalculateBalancedHeight(firstPass)) {
3291 multicolSet->setChildNeedsLayout(MarkOnlyThis);
3292 needsRelayout = true;
3293 }
3294 }
3295
3296 if (needsRelayout) {
3297 // Layout again. Column balancing resulted in a new height.
3298 neededRelayout = true;
3299 multiColumnFlowThread()->setChildNeedsLayout(MarkOnlyThis);
3300 setChildNeedsLayout(MarkOnlyThis);
3301 if (firstPass)
3302 statePusher.pop();
3303 layoutBlock(false);
3304 }
3305 firstPass = false;
3306 } while (needsRelayout);
3307
3308 multiColumnFlowThread()->setInBalancingPass(false);
3309
3310 return neededRelayout;
weinig@apple.com611b9292013-10-20 22:57:54 +00003311}
3312
antti@apple.com940f5872013-10-24 20:31:11 +00003313bool RenderBlockFlow::hasLines() const
3314{
3315 ASSERT(childrenInline());
3316
antti@apple.comfea51992013-10-28 13:39:23 +00003317 if (m_simpleLineLayout)
antti@apple.com09c83512013-10-30 14:12:32 +00003318 return m_simpleLineLayout->lineCount();
antti@apple.com940f5872013-10-24 20:31:11 +00003319
3320 return lineBoxes().firstLineBox();
3321}
3322
3323void RenderBlockFlow::layoutSimpleLines(LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
3324{
3325 ASSERT(!m_lineBoxes.firstLineBox());
3326
antti@apple.comfea51992013-10-28 13:39:23 +00003327 m_simpleLineLayout = SimpleLineLayout::create(*this);
antti@apple.com940f5872013-10-24 20:31:11 +00003328
antti@apple.comfea51992013-10-28 13:39:23 +00003329 LayoutUnit lineLayoutHeight = SimpleLineLayout::computeFlowHeight(*this, *m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00003330 LayoutUnit lineLayoutTop = borderAndPaddingBefore();
3331
3332 repaintLogicalTop = lineLayoutTop;
3333 repaintLogicalBottom = lineLayoutTop + lineLayoutHeight;
3334
3335 setLogicalHeight(lineLayoutTop + lineLayoutHeight + borderAndPaddingAfter());
3336}
3337
3338void RenderBlockFlow::deleteLineBoxesBeforeSimpleLineLayout()
3339{
antti@apple.com42fb53d2013-10-25 02:33:11 +00003340 ASSERT(m_lineLayoutPath == SimpleLinesPath);
akling@apple.com31dd4f42013-10-30 22:27:59 +00003341 lineBoxes().deleteLineBoxes();
antti@apple.com940f5872013-10-24 20:31:11 +00003342 toRenderText(firstChild())->deleteLineBoxesBeforeSimpleLineLayout();
3343}
3344
3345void RenderBlockFlow::ensureLineBoxes()
3346{
antti@apple.com42fb53d2013-10-25 02:33:11 +00003347 m_lineLayoutPath = ForceLineBoxesPath;
antti@apple.com940f5872013-10-24 20:31:11 +00003348
antti@apple.comfea51992013-10-28 13:39:23 +00003349 if (!m_simpleLineLayout)
antti@apple.com940f5872013-10-24 20:31:11 +00003350 return;
antti@apple.comfea51992013-10-28 13:39:23 +00003351 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +00003352
3353#if !ASSERT_DISABLED
3354 LayoutUnit oldHeight = logicalHeight();
3355#endif
3356 bool didNeedLayout = needsLayout();
3357
3358 bool relayoutChildren = false;
3359 LayoutUnit repaintLogicalTop;
3360 LayoutUnit repaintLogicalBottom;
3361 layoutLineBoxes(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
3362
3363 updateLogicalHeight();
3364 ASSERT(didNeedLayout || logicalHeight() == oldHeight);
3365
3366 if (!didNeedLayout)
3367 clearNeedsLayout();
3368}
3369
weinig@apple.com611b9292013-10-20 22:57:54 +00003370#ifndef NDEBUG
3371void RenderBlockFlow::showLineTreeAndMark(const InlineBox* markedBox1, const char* markedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const RenderObject* obj) const
3372{
3373 RenderBlock::showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLabel2, obj);
3374 for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox())
3375 root->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLabel2, obj, 1);
3376}
3377#endif
3378
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00003379RenderBlockFlow::RenderBlockFlowRareData& RenderBlockFlow::ensureRareBlockFlowData()
3380{
3381 if (hasRareBlockFlowData())
3382 return *m_rareBlockFlowData;
3383 materializeRareBlockFlowData();
3384 return *m_rareBlockFlowData;
3385}
3386
3387void RenderBlockFlow::materializeRareBlockFlowData()
3388{
3389 ASSERT(!hasRareBlockFlowData());
3390 m_rareBlockFlowData = std::make_unique<RenderBlockFlow::RenderBlockFlowRareData>(*this);
3391}
3392
aestes@apple.com6751d842014-01-12 02:51:25 +00003393#if ENABLE(IOS_TEXT_AUTOSIZING)
3394inline static bool isVisibleRenderText(RenderObject* renderer)
3395{
3396 if (!renderer->isText())
3397 return false;
3398 RenderText* renderText = toRenderText(renderer);
3399 return !renderText->linesBoundingBox().isEmpty() && !renderText->text()->containsOnlyWhitespace();
3400}
3401
3402inline static bool resizeTextPermitted(RenderObject* render)
3403{
3404 // We disallow resizing for text input fields and textarea to address <rdar://problem/5792987> and <rdar://problem/8021123>
3405 auto renderer = render->parent();
3406 while (renderer) {
3407 // Get the first non-shadow HTMLElement and see if it's an input.
3408 if (renderer->element() && renderer->element()->isHTMLElement() && !renderer->element()->isInShadowTree()) {
3409 const HTMLElement& element = toHTMLElement(*renderer->element());
3410 return !isHTMLInputElement(element) && !isHTMLTextAreaElement(element);
3411 }
3412 renderer = renderer->parent();
3413 }
3414 return true;
3415}
3416
3417int RenderBlockFlow::immediateLineCount()
3418{
3419 // Copied and modified from RenderBlock::lineCount.
3420 // Only descend into list items.
3421 int count = 0;
3422 if (style().visibility() == VISIBLE) {
3423 if (childrenInline()) {
3424 for (RootInlineBox* box = firstRootBox(); box; box = box->nextRootBox())
3425 count++;
3426 } else {
3427 for (RenderObject* obj = firstChild(); obj; obj = obj->nextSibling()) {
3428 if (obj->isListItem())
3429 count += toRenderBlockFlow(obj)->lineCount();
3430 }
3431 }
3432 }
3433 return count;
3434}
3435
3436static bool isNonBlocksOrNonFixedHeightListItems(const RenderObject* render)
3437{
3438 if (!render->isRenderBlock())
3439 return true;
3440 if (render->isListItem())
3441 return render->style().height().type() != Fixed;
3442 return false;
3443}
3444
3445// For now, we auto size single lines of text the same as multiple lines.
3446// We've been experimenting with low values for single lines of text.
3447static inline float oneLineTextMultiplier(float specifiedSize)
3448{
3449 return std::max((1.0f / log10f(specifiedSize) * 1.7f), 1.0f);
3450}
3451
3452static inline float textMultiplier(float specifiedSize)
3453{
3454 return std::max((1.0f / log10f(specifiedSize) * 1.95f), 1.0f);
3455}
3456
3457void RenderBlockFlow::adjustComputedFontSizes(float size, float visibleWidth)
3458{
3459 // Don't do any work if the block is smaller than the visible area.
3460 if (visibleWidth >= width())
3461 return;
3462
3463 unsigned lineCount;
3464 if (m_lineCountForTextAutosizing == NOT_SET) {
3465 int count = immediateLineCount();
3466 if (!count)
3467 lineCount = NO_LINE;
3468 else if (count == 1)
3469 lineCount = ONE_LINE;
3470 else
3471 lineCount = MULTI_LINE;
3472 } else
3473 lineCount = m_lineCountForTextAutosizing;
3474
3475 ASSERT(lineCount != NOT_SET);
3476 if (lineCount == NO_LINE)
3477 return;
3478
3479 float actualWidth = m_widthForTextAutosizing != -1 ? static_cast<float>(m_widthForTextAutosizing) : static_cast<float>(width());
3480 float scale = visibleWidth / actualWidth;
3481 float minFontSize = roundf(size / scale);
3482
3483 for (RenderObject* descendent = traverseNext(this, isNonBlocksOrNonFixedHeightListItems); descendent; descendent = descendent->traverseNext(this, isNonBlocksOrNonFixedHeightListItems)) {
3484 if (isVisibleRenderText(descendent) && resizeTextPermitted(descendent)) {
3485 RenderText* text = toRenderText(descendent);
3486 RenderStyle& oldStyle = text->style();
3487 FontDescription fontDescription = oldStyle.fontDescription();
3488 float specifiedSize = fontDescription.specifiedSize();
3489 float scaledSize = roundf(specifiedSize * scale);
3490 if (scaledSize > 0 && scaledSize < minFontSize) {
3491 // 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.
3492 // This makes text resizing consistent even if the block's width or line count changes (which can be caused by text resizing itself 5159915).
3493 if (m_lineCountForTextAutosizing == NOT_SET)
3494 m_lineCountForTextAutosizing = lineCount;
3495 if (m_widthForTextAutosizing == -1)
3496 m_widthForTextAutosizing = actualWidth;
3497
3498 float candidateNewSize = 0;
3499 float lineTextMultiplier = lineCount == ONE_LINE ? oneLineTextMultiplier(specifiedSize) : textMultiplier(specifiedSize);
3500 candidateNewSize = roundf(std::min(minFontSize, specifiedSize * lineTextMultiplier));
3501 if (candidateNewSize > specifiedSize && candidateNewSize != fontDescription.computedSize() && text->textNode() && oldStyle.textSizeAdjust().isAuto())
3502 document().addAutoSizingNode(text->textNode(), candidateNewSize);
3503 }
3504 }
3505 }
3506}
3507#endif // ENABLE(IOS_TEXT_AUTOSIZING)
3508
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003509RenderObject* RenderBlockFlow::layoutSpecialExcludedChild(bool relayoutChildren)
3510{
3511 if (!multiColumnFlowThread())
3512 return 0;
3513
3514 // Update the dimensions of our regions before we lay out the flow thread.
3515 // FIXME: Eventually this is going to get way more complicated, and we will be destroying regions
3516 // instead of trying to keep them around.
3517 bool shouldInvalidateRegions = false;
3518 for (RenderBox* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox()) {
3519 if (childBox == multiColumnFlowThread())
3520 continue;
3521
3522 if (relayoutChildren || childBox->needsLayout()) {
3523 if (!multiColumnFlowThread()->inBalancingPass() && childBox->isRenderMultiColumnSet())
3524 toRenderMultiColumnSet(childBox)->prepareForLayout();
3525 shouldInvalidateRegions = true;
3526 }
3527 }
3528
3529 if (shouldInvalidateRegions)
3530 multiColumnFlowThread()->invalidateRegions();
3531
3532 if (relayoutChildren)
3533 multiColumnFlowThread()->setChildNeedsLayout(MarkOnlyThis);
3534
3535 if (multiColumnFlowThread()->requiresBalancing()) {
3536 // At the end of multicol layout, relayoutForPagination() is called unconditionally, but if
3537 // no children are to be laid out (e.g. fixed width with layout already being up-to-date),
3538 // we want to prevent it from doing any work, so that the column balancing machinery doesn't
3539 // kick in and trigger additional unnecessary layout passes. Actually, it's not just a good
3540 // idea in general to not waste time on balancing content that hasn't been re-laid out; we
3541 // are actually required to guarantee this. The calculation of implicit breaks needs to be
3542 // preceded by a proper layout pass, since it's layout that sets up content runs, and the
3543 // runs get deleted right after every pass.
3544 multiColumnFlowThread()->setNeedsRebalancing(shouldInvalidateRegions || multiColumnFlowThread()->needsLayout());
3545 }
3546
3547 setLogicalTopForChild(*multiColumnFlowThread(), borderAndPaddingBefore());
3548 multiColumnFlowThread()->layoutIfNeeded();
3549 determineLogicalLeftPositionForChild(*multiColumnFlowThread());
3550
3551 return multiColumnFlowThread();
3552}
3553
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003554void RenderBlockFlow::addChild(RenderObject* newChild, RenderObject* beforeChild)
3555{
3556 if (multiColumnFlowThread())
3557 return multiColumnFlowThread()->addChild(newChild, beforeChild);
3558 RenderBlock::addChild(newChild, beforeChild);
3559}
3560
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003561void RenderBlockFlow::checkForPaginationLogicalHeightChange(LayoutUnit& pageLogicalHeight, bool& pageLogicalHeightChanged, bool& hasSpecifiedPageLogicalHeight)
3562{
3563 // If we don't use either of the two column implementations or a flow thread, then bail.
3564 if (!isRenderFlowThread() && !multiColumnFlowThread() && !hasColumns())
3565 return;
3566
3567 // We don't actually update any of the variables. We just subclassed to adjust our column height.
3568 if (multiColumnFlowThread()) {
3569 updateLogicalHeight();
3570 multiColumnFlowThread()->setColumnHeightAvailable(std::max<LayoutUnit>(contentLogicalHeight(), 0));
3571 setLogicalHeight(0);
3572 } else if (hasColumns()) {
3573 ColumnInfo* colInfo = columnInfo();
3574
3575 if (!pageLogicalHeight) {
3576 // We need to go ahead and set our explicit page height if one exists, so that we can
3577 // avoid doing two layout passes.
3578 updateLogicalHeight();
3579 LayoutUnit columnHeight = isRenderView() ? view().pageOrViewLogicalHeight() : contentLogicalHeight();
3580 if (columnHeight > 0) {
3581 pageLogicalHeight = columnHeight;
3582 hasSpecifiedPageLogicalHeight = true;
3583 }
3584 setLogicalHeight(0);
3585 }
3586
3587 if (colInfo->columnHeight() != pageLogicalHeight && everHadLayout())
3588 pageLogicalHeightChanged = true;
3589
3590 colInfo->setColumnHeight(pageLogicalHeight);
3591
3592 if (!hasSpecifiedPageLogicalHeight && !pageLogicalHeight)
3593 colInfo->clearForcedBreaks();
3594
3595 colInfo->setPaginationUnit(paginationUnit());
3596 } else if (isRenderFlowThread()) {
3597 pageLogicalHeight = 1; // This is just a hack to always make sure we have a page logical height.
3598 pageLogicalHeightChanged = toRenderFlowThread(this)->pageLogicalSizeChanged();
3599 }
3600}
3601
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003602void RenderBlockFlow::setComputedColumnCountAndWidth(int count, LayoutUnit width)
3603{
3604 if (!document().regionBasedColumnsEnabled())
3605 return RenderBlock::setComputedColumnCountAndWidth(count, width);
3606
3607 bool destroyColumns = !requiresColumns(count);
3608 if (destroyColumns) {
3609 if (multiColumnFlowThread())
3610 destroyMultiColumnFlowThread();
3611 } else {
3612 if (!multiColumnFlowThread())
3613 createMultiColumnFlowThread();
3614 multiColumnFlowThread()->setColumnCountAndWidth(count, width);
hyatt@apple.com86919862014-01-27 16:27:45 +00003615 multiColumnFlowThread()->setProgressionIsInline(style().hasInlineColumnAxis());
3616 multiColumnFlowThread()->setProgressionIsReversed(style().columnProgression() == ReverseColumnProgression);
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003617 }
3618}
3619
hyatt@apple.com86919862014-01-27 16:27:45 +00003620void RenderBlockFlow::updateColumnProgressionFromStyle(RenderStyle* style)
3621{
3622 if (!document().regionBasedColumnsEnabled())
3623 return RenderBlock::updateColumnProgressionFromStyle(style);
3624
3625 if (!multiColumnFlowThread())
3626 return;
3627
3628 bool needsLayout = false;
3629 bool oldProgressionIsInline = multiColumnFlowThread()->progressionIsInline();
3630 bool newProgressionIsInline = style->hasInlineColumnAxis();
3631 if (oldProgressionIsInline != newProgressionIsInline) {
3632 multiColumnFlowThread()->setProgressionIsInline(newProgressionIsInline);
3633 needsLayout = true;
3634 }
3635
3636 bool oldProgressionIsReversed = multiColumnFlowThread()->progressionIsReversed();
3637 bool newProgressionIsReversed = style->columnProgression() == ReverseColumnProgression;
3638 if (oldProgressionIsReversed != newProgressionIsReversed) {
3639 multiColumnFlowThread()->setProgressionIsReversed(newProgressionIsReversed);
3640 needsLayout = true;
3641 }
3642
3643 if (needsLayout)
3644 setNeedsLayoutAndPrefWidthsRecalc();
3645}
3646
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003647LayoutUnit RenderBlockFlow::computedColumnWidth() const
3648{
3649 if (!document().regionBasedColumnsEnabled())
3650 return RenderBlock::computedColumnWidth();
3651
3652 if (multiColumnFlowThread())
3653 return multiColumnFlowThread()->computedColumnWidth();
3654 return contentLogicalWidth();
3655}
3656
3657unsigned RenderBlockFlow::computedColumnCount() const
3658{
3659 if (!document().regionBasedColumnsEnabled())
3660 return RenderBlock::computedColumnCount();
3661
3662 if (multiColumnFlowThread())
3663 return multiColumnFlowThread()->computedColumnCount();
3664
3665 return 1;
3666}
3667
hyatt@apple.com31a5daa2014-01-28 01:26:37 +00003668bool RenderBlockFlow::isTopLayoutOverflowAllowed() const
3669{
3670 bool hasTopOverflow = RenderBlock::isTopLayoutOverflowAllowed();
3671 if (!multiColumnFlowThread() || style().columnProgression() == NormalColumnProgression)
3672 return hasTopOverflow;
3673
3674 if (!(isHorizontalWritingMode() ^ !style().hasInlineColumnAxis()))
3675 hasTopOverflow = !hasTopOverflow;
3676
3677 return hasTopOverflow;
3678}
3679
3680bool RenderBlockFlow::isLeftLayoutOverflowAllowed() const
3681{
3682 bool hasLeftOverflow = RenderBlock::isLeftLayoutOverflowAllowed();
3683 if (!multiColumnFlowThread() || style().columnProgression() == NormalColumnProgression)
3684 return hasLeftOverflow;
3685
3686 if (isHorizontalWritingMode() ^ !style().hasInlineColumnAxis())
3687 hasLeftOverflow = !hasLeftOverflow;
3688
3689 return hasLeftOverflow;
3690}
3691
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00003692}
3693// namespace WebCore