blob: cc82e21fbceed89618e2b1740f262c4a2985d89c [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
765
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000766void RenderBlockFlow::adjustFloatingBlock(const MarginInfo& marginInfo)
767{
768 // The float should be positioned taking into account the bottom margin
769 // of the previous flow. We add that margin into the height, get the
770 // float positioned properly, and then subtract the margin out of the
771 // height again. In the case of self-collapsing blocks, we always just
772 // use the top margins, since the self-collapsing block collapsed its
773 // own bottom margin into its top margin.
774 //
775 // Note also that the previous flow may collapse its margin into the top of
776 // our block. If this is the case, then we do not add the margin in to our
777 // height when computing the position of the float. This condition can be tested
778 // for by simply calling canCollapseWithMarginBefore. See
779 // http://www.hixie.ch/tests/adhoc/css/box/block/margin-collapse/046.html for
780 // an example of this scenario.
781 LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
782 setLogicalHeight(logicalHeight() + marginOffset);
783 positionNewFloats();
784 setLogicalHeight(logicalHeight() - marginOffset);
785}
786
weinig@apple.com12840dc2013-10-22 23:59:08 +0000787void RenderBlockFlow::updateStaticInlinePositionForChild(RenderBox& child, LayoutUnit logicalTop)
788{
akling@apple.com827be9c2013-10-29 02:58:43 +0000789 if (child.style().isOriginalDisplayInlineType())
weinig@apple.com12840dc2013-10-22 23:59:08 +0000790 setStaticInlinePositionForChild(child, logicalTop, startAlignedOffsetForLine(logicalTop, false));
791 else
792 setStaticInlinePositionForChild(child, logicalTop, startOffsetForContent(logicalTop));
793}
794
795void RenderBlockFlow::setStaticInlinePositionForChild(RenderBox& child, LayoutUnit blockOffset, LayoutUnit inlinePosition)
796{
797 if (flowThreadContainingBlock()) {
798 // Shift the inline position to exclude the region offset.
799 inlinePosition += startOffsetForContent() - startOffsetForContent(blockOffset);
800 }
801 child.layer()->setStaticInlinePosition(inlinePosition);
802}
803
804RenderBlockFlow::MarginValues RenderBlockFlow::marginValuesForChild(RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000805{
806 LayoutUnit childBeforePositive = 0;
807 LayoutUnit childBeforeNegative = 0;
808 LayoutUnit childAfterPositive = 0;
809 LayoutUnit childAfterNegative = 0;
810
811 LayoutUnit beforeMargin = 0;
812 LayoutUnit afterMargin = 0;
813
weinig@apple.com12840dc2013-10-22 23:59:08 +0000814 RenderBlockFlow* childRenderBlock = child.isRenderBlockFlow() ? toRenderBlockFlow(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000815
816 // If the child has the same directionality as we do, then we can just return its
817 // margins in the same direction.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000818 if (!child.isWritingModeRoot()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000819 if (childRenderBlock) {
820 childBeforePositive = childRenderBlock->maxPositiveMarginBefore();
821 childBeforeNegative = childRenderBlock->maxNegativeMarginBefore();
822 childAfterPositive = childRenderBlock->maxPositiveMarginAfter();
823 childAfterNegative = childRenderBlock->maxNegativeMarginAfter();
824 } else {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000825 beforeMargin = child.marginBefore();
826 afterMargin = child.marginAfter();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000827 }
weinig@apple.com12840dc2013-10-22 23:59:08 +0000828 } else if (child.isHorizontalWritingMode() == isHorizontalWritingMode()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000829 // The child has a different directionality. If the child is parallel, then it's just
830 // flipped relative to us. We can use the margins for the opposite edges.
831 if (childRenderBlock) {
832 childBeforePositive = childRenderBlock->maxPositiveMarginAfter();
833 childBeforeNegative = childRenderBlock->maxNegativeMarginAfter();
834 childAfterPositive = childRenderBlock->maxPositiveMarginBefore();
835 childAfterNegative = childRenderBlock->maxNegativeMarginBefore();
836 } else {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000837 beforeMargin = child.marginAfter();
838 afterMargin = child.marginBefore();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000839 }
840 } else {
841 // The child is perpendicular to us, which means its margins don't collapse but are on the
842 // "logical left/right" sides of the child box. We can just return the raw margin in this case.
843 beforeMargin = marginBeforeForChild(child);
844 afterMargin = marginAfterForChild(child);
845 }
846
847 // Resolve uncollapsing margins into their positive/negative buckets.
848 if (beforeMargin) {
849 if (beforeMargin > 0)
850 childBeforePositive = beforeMargin;
851 else
852 childBeforeNegative = -beforeMargin;
853 }
854 if (afterMargin) {
855 if (afterMargin > 0)
856 childAfterPositive = afterMargin;
857 else
858 childAfterNegative = -afterMargin;
859 }
860
861 return MarginValues(childBeforePositive, childBeforeNegative, childAfterPositive, childAfterNegative);
862}
863
weinig@apple.com12840dc2013-10-22 23:59:08 +0000864LayoutUnit RenderBlockFlow::collapseMargins(RenderBox& child, MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000865{
866 bool childDiscardMarginBefore = mustDiscardMarginBeforeForChild(child);
867 bool childDiscardMarginAfter = mustDiscardMarginAfterForChild(child);
weinig@apple.com12840dc2013-10-22 23:59:08 +0000868 bool childIsSelfCollapsing = child.isSelfCollapsingBlock();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000869
870 // The child discards the before margin when the the after margin has discard in the case of a self collapsing block.
871 childDiscardMarginBefore = childDiscardMarginBefore || (childDiscardMarginAfter && childIsSelfCollapsing);
872
873 // Get the four margin values for the child and cache them.
874 const MarginValues childMargins = marginValuesForChild(child);
875
876 // Get our max pos and neg top margins.
877 LayoutUnit posTop = childMargins.positiveMarginBefore();
878 LayoutUnit negTop = childMargins.negativeMarginBefore();
879
880 // For self-collapsing blocks, collapse our bottom margins into our
881 // top to get new posTop and negTop values.
882 if (childIsSelfCollapsing) {
andersca@apple.com86298632013-11-10 19:32:33 +0000883 posTop = std::max(posTop, childMargins.positiveMarginAfter());
884 negTop = std::max(negTop, childMargins.negativeMarginAfter());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000885 }
886
887 // See if the top margin is quirky. We only care if this child has
888 // margins that will collapse with us.
889 bool topQuirk = hasMarginBeforeQuirk(child);
890
891 if (marginInfo.canCollapseWithMarginBefore()) {
892 if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
893 // This child is collapsing with the top of the
894 // block. If it has larger margin values, then we need to update
895 // our own maximal values.
896 if (!document().inQuirksMode() || !marginInfo.quirkContainer() || !topQuirk)
andersca@apple.com86298632013-11-10 19:32:33 +0000897 setMaxMarginBeforeValues(std::max(posTop, maxPositiveMarginBefore()), std::max(negTop, maxNegativeMarginBefore()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000898
899 // The minute any of the margins involved isn't a quirk, don't
900 // collapse it away, even if the margin is smaller (www.webreference.com
901 // has an example of this, a <dt> with 0.8em author-specified inside
902 // a <dl> inside a <td>.
903 if (!marginInfo.determinedMarginBeforeQuirk() && !topQuirk && (posTop - negTop)) {
904 setHasMarginBeforeQuirk(false);
905 marginInfo.setDeterminedMarginBeforeQuirk(true);
906 }
907
908 if (!marginInfo.determinedMarginBeforeQuirk() && topQuirk && !marginBefore())
909 // We have no top margin and our top child has a quirky margin.
910 // We will pick up this quirky margin and pass it through.
911 // This deals with the <td><div><p> case.
912 // Don't do this for a block that split two inlines though. You do
913 // still apply margins in this case.
914 setHasMarginBeforeQuirk(true);
915 } else
916 // The before margin of the container will also discard all the margins it is collapsing with.
917 setMustDiscardMarginBefore();
918 }
919
920 // Once we find a child with discardMarginBefore all the margins collapsing with us must also discard.
921 if (childDiscardMarginBefore) {
922 marginInfo.setDiscardMargin(true);
923 marginInfo.clearMargin();
924 }
925
926 if (marginInfo.quirkContainer() && marginInfo.atBeforeSideOfBlock() && (posTop - negTop))
927 marginInfo.setHasMarginBeforeQuirk(topQuirk);
928
929 LayoutUnit beforeCollapseLogicalTop = logicalHeight();
930 LayoutUnit logicalTop = beforeCollapseLogicalTop;
robert@webkit.org97037ef2013-11-20 19:26:10 +0000931
932 LayoutUnit clearanceForSelfCollapsingBlock;
933 RenderObject* prev = child.previousSibling();
934 // 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
935 // 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
936 // 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.
937 if (!marginInfo.canCollapseWithMarginBefore() && prev && prev->isRenderBlockFlow() && toRenderBlockFlow(prev)->isSelfCollapsingBlock()) {
938 clearanceForSelfCollapsingBlock = toRenderBlockFlow(prev)->marginOffsetForSelfCollapsingBlock();
939 setLogicalHeight(logicalHeight() - clearanceForSelfCollapsingBlock);
940 }
941
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000942 if (childIsSelfCollapsing) {
943 // 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.
944 // Also, the child's top position equals the logical height of the container.
945 if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
946 // This child has no height. We need to compute our
947 // position before we collapse the child's margins together,
948 // so that we can get an accurate position for the zero-height block.
andersca@apple.com86298632013-11-10 19:32:33 +0000949 LayoutUnit collapsedBeforePos = std::max(marginInfo.positiveMargin(), childMargins.positiveMarginBefore());
950 LayoutUnit collapsedBeforeNeg = std::max(marginInfo.negativeMargin(), childMargins.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000951 marginInfo.setMargin(collapsedBeforePos, collapsedBeforeNeg);
952
953 // Now collapse the child's margins together, which means examining our
954 // bottom margin values as well.
955 marginInfo.setPositiveMarginIfLarger(childMargins.positiveMarginAfter());
956 marginInfo.setNegativeMarginIfLarger(childMargins.negativeMarginAfter());
957
958 if (!marginInfo.canCollapseWithMarginBefore())
959 // We need to make sure that the position of the self-collapsing block
960 // is correct, since it could have overflowing content
961 // that needs to be positioned correctly (e.g., a block that
962 // had a specified height of 0 but that actually had subcontent).
963 logicalTop = logicalHeight() + collapsedBeforePos - collapsedBeforeNeg;
964 }
965 } else {
966 if (mustSeparateMarginBeforeForChild(child)) {
967 ASSERT(!marginInfo.discardMargin() || (marginInfo.discardMargin() && !marginInfo.margin()));
968 // If we are at the before side of the block and we collapse, ignore the computed margin
969 // and just add the child margin to the container height. This will correctly position
970 // the child inside the container.
zalan@apple.coma4d00552014-01-25 00:21:59 +0000971 LayoutUnit separateMargin = !marginInfo.canCollapseWithMarginBefore() ? marginInfo.margin() : LayoutUnit::fromPixel(0);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000972 setLogicalHeight(logicalHeight() + separateMargin + marginBeforeForChild(child));
973 logicalTop = logicalHeight();
974 } else if (!marginInfo.discardMargin() && (!marginInfo.atBeforeSideOfBlock()
975 || (!marginInfo.canCollapseMarginBeforeWithChildren()
976 && (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginBeforeQuirk())))) {
977 // We're collapsing with a previous sibling's margins and not
978 // with the top of the block.
andersca@apple.com86298632013-11-10 19:32:33 +0000979 setLogicalHeight(logicalHeight() + std::max(marginInfo.positiveMargin(), posTop) - std::max(marginInfo.negativeMargin(), negTop));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000980 logicalTop = logicalHeight();
981 }
982
983 marginInfo.setDiscardMargin(childDiscardMarginAfter);
984
985 if (!marginInfo.discardMargin()) {
986 marginInfo.setPositiveMargin(childMargins.positiveMarginAfter());
987 marginInfo.setNegativeMargin(childMargins.negativeMarginAfter());
988 } else
989 marginInfo.clearMargin();
990
991 if (marginInfo.margin())
992 marginInfo.setHasMarginAfterQuirk(hasMarginAfterQuirk(child));
993 }
994
995 // If margins would pull us past the top of the next page, then we need to pull back and pretend like the margins
996 // collapsed into the page edge.
997 LayoutState* layoutState = view().layoutState();
998 if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTop > beforeCollapseLogicalTop
999 && hasNextPage(beforeCollapseLogicalTop)) {
1000 LayoutUnit oldLogicalTop = logicalTop;
andersca@apple.com86298632013-11-10 19:32:33 +00001001 logicalTop = std::min(logicalTop, nextPageLogicalTop(beforeCollapseLogicalTop));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001002 setLogicalHeight(logicalHeight() + (logicalTop - oldLogicalTop));
1003 }
1004
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001005 if (prev && prev->isRenderBlockFlow() && !prev->isFloatingOrOutOfFlowPositioned()) {
robert@webkit.org97037ef2013-11-20 19:26:10 +00001006 // 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
1007 // any floats from the parent will now overhang.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001008 RenderBlockFlow& block = toRenderBlockFlow(*prev);
robert@webkit.org97037ef2013-11-20 19:26:10 +00001009 LayoutUnit oldLogicalHeight = logicalHeight();
1010 setLogicalHeight(logicalTop);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001011 if (block.containsFloats() && !block.avoidsFloats() && (block.logicalTop() + block.lowestFloatLogicalBottom()) > logicalTop)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001012 addOverhangingFloats(block, false);
robert@webkit.org97037ef2013-11-20 19:26:10 +00001013 setLogicalHeight(oldLogicalHeight);
1014
1015 // If |child|'s previous sibling is a self-collapsing block that cleared a float and margin collapsing resulted in |child| moving up
1016 // 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
1017 // floats in the parent that overhang |child|'s new logical top.
1018 bool logicalTopIntrudesIntoFloat = clearanceForSelfCollapsingBlock > 0 && logicalTop < beforeCollapseLogicalTop;
1019 if (logicalTopIntrudesIntoFloat && containsFloats() && !child.avoidsFloats() && lowestFloatLogicalBottom() > logicalTop)
1020 child.setNeedsLayout();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001021 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001022
1023 return logicalTop;
1024}
1025
weinig@apple.com12840dc2013-10-22 23:59:08 +00001026LayoutUnit RenderBlockFlow::clearFloatsIfNeeded(RenderBox& child, MarginInfo& marginInfo, LayoutUnit oldTopPosMargin, LayoutUnit oldTopNegMargin, LayoutUnit yPos)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001027{
1028 LayoutUnit heightIncrease = getClearDelta(child, yPos);
1029 if (!heightIncrease)
1030 return yPos;
1031
weinig@apple.com12840dc2013-10-22 23:59:08 +00001032 if (child.isSelfCollapsingBlock()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001033 bool childDiscardMargin = mustDiscardMarginBeforeForChild(child) || mustDiscardMarginAfterForChild(child);
1034
1035 // For self-collapsing blocks that clear, they can still collapse their
1036 // margins with following siblings. Reset the current margins to represent
1037 // the self-collapsing block's margins only.
1038 // If DISCARD is specified for -webkit-margin-collapse, reset the margin values.
robert@webkit.org97037ef2013-11-20 19:26:10 +00001039 MarginValues childMargins = marginValuesForChild(child);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001040 if (!childDiscardMargin) {
andersca@apple.com86298632013-11-10 19:32:33 +00001041 marginInfo.setPositiveMargin(std::max(childMargins.positiveMarginBefore(), childMargins.positiveMarginAfter()));
1042 marginInfo.setNegativeMargin(std::max(childMargins.negativeMarginBefore(), childMargins.negativeMarginAfter()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001043 } else
1044 marginInfo.clearMargin();
1045 marginInfo.setDiscardMargin(childDiscardMargin);
1046
1047 // CSS2.1 states:
1048 // "If the top and bottom margins of an element with clearance are adjoining, its margins collapse with
1049 // the adjoining margins of following siblings but that resulting margin does not collapse with the bottom margin of the parent block."
1050 // So the parent's bottom margin cannot collapse through this block or any subsequent self-collapsing blocks. Check subsequent siblings
1051 // for a block with height - if none is found then don't allow the margins to collapse with the parent.
1052 bool wouldCollapseMarginsWithParent = marginInfo.canCollapseMarginAfterWithChildren();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001053 for (RenderBox* curr = child.nextSiblingBox(); curr && wouldCollapseMarginsWithParent; curr = curr->nextSiblingBox()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001054 if (!curr->isFloatingOrOutOfFlowPositioned() && !curr->isSelfCollapsingBlock())
1055 wouldCollapseMarginsWithParent = false;
1056 }
1057 if (wouldCollapseMarginsWithParent)
1058 marginInfo.setCanCollapseMarginAfterWithChildren(false);
1059
robert@webkit.org97037ef2013-11-20 19:26:10 +00001060 // 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
1061 // its own at the correct vertical position. If subsequent siblings attempt to collapse with |child|'s margins in |collapseMargins| we will
1062 // 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
1063 // margins can collapse at the correct vertical position.
1064 // 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
1065 // (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],
1066 // i.e., clearance = [height of float] - margin-top".
1067 setLogicalHeight(child.logicalTop() + childMargins.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001068 } else
1069 // Increase our height by the amount we had to clear.
1070 setLogicalHeight(logicalHeight() + heightIncrease);
1071
1072 if (marginInfo.canCollapseWithMarginBefore()) {
1073 // We can no longer collapse with the top of the block since a clear
1074 // occurred. The empty blocks collapse into the cleared block.
1075 // FIXME: This isn't quite correct. Need clarification for what to do
1076 // if the height the cleared block is offset by is smaller than the
1077 // margins involved.
1078 setMaxMarginBeforeValues(oldTopPosMargin, oldTopNegMargin);
1079 marginInfo.setAtBeforeSideOfBlock(false);
1080
1081 // 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 +00001082 setMustDiscardMarginBefore(style().marginBeforeCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001083 }
1084
robert@webkit.org97037ef2013-11-20 19:26:10 +00001085 return yPos + heightIncrease;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001086}
1087
weinig@apple.com12840dc2013-10-22 23:59:08 +00001088void RenderBlockFlow::marginBeforeEstimateForChild(RenderBox& child, LayoutUnit& positiveMarginBefore, LayoutUnit& negativeMarginBefore, bool& discardMarginBefore) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001089{
1090 // Give up if in quirks mode and we're a body/table cell and the top margin of the child box is quirky.
1091 // Give up if the child specified -webkit-margin-collapse: separate that prevents collapsing.
1092 // FIXME: Use writing mode independent accessor for marginBeforeCollapse.
akling@apple.com827be9c2013-10-29 02:58:43 +00001093 if ((document().inQuirksMode() && hasMarginAfterQuirk(child) && (isTableCell() || isBody())) || child.style().marginBeforeCollapse() == MSEPARATE)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001094 return;
1095
1096 // The margins are discarded by a child that specified -webkit-margin-collapse: discard.
1097 // FIXME: Use writing mode independent accessor for marginBeforeCollapse.
akling@apple.com827be9c2013-10-29 02:58:43 +00001098 if (child.style().marginBeforeCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001099 positiveMarginBefore = 0;
1100 negativeMarginBefore = 0;
1101 discardMarginBefore = true;
1102 return;
1103 }
1104
1105 LayoutUnit beforeChildMargin = marginBeforeForChild(child);
andersca@apple.com86298632013-11-10 19:32:33 +00001106 positiveMarginBefore = std::max(positiveMarginBefore, beforeChildMargin);
1107 negativeMarginBefore = std::max(negativeMarginBefore, -beforeChildMargin);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001108
weinig@apple.com12840dc2013-10-22 23:59:08 +00001109 if (!child.isRenderBlockFlow())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001110 return;
1111
weinig@apple.com12840dc2013-10-22 23:59:08 +00001112 RenderBlockFlow& childBlock = toRenderBlockFlow(child);
1113 if (childBlock.childrenInline() || childBlock.isWritingModeRoot())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001114 return;
1115
weinig@apple.com12840dc2013-10-22 23:59:08 +00001116 MarginInfo childMarginInfo(childBlock, childBlock.borderAndPaddingBefore(), childBlock.borderAndPaddingAfter());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001117 if (!childMarginInfo.canCollapseMarginBeforeWithChildren())
1118 return;
1119
weinig@apple.com12840dc2013-10-22 23:59:08 +00001120 RenderBox* grandchildBox = childBlock.firstChildBox();
1121 for (; grandchildBox; grandchildBox = grandchildBox->nextSiblingBox()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001122 if (!grandchildBox->isFloatingOrOutOfFlowPositioned())
1123 break;
1124 }
1125
1126 // 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 +00001127 if (!grandchildBox || grandchildBox->style().clear() != CNONE)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001128 return;
1129
1130 // Make sure to update the block margins now for the grandchild box so that we're looking at current values.
1131 if (grandchildBox->needsLayout()) {
1132 grandchildBox->computeAndSetBlockDirectionMargins(this);
1133 if (grandchildBox->isRenderBlock()) {
1134 RenderBlock* grandchildBlock = toRenderBlock(grandchildBox);
akling@apple.com827be9c2013-10-29 02:58:43 +00001135 grandchildBlock->setHasMarginBeforeQuirk(grandchildBox->style().hasMarginBeforeQuirk());
1136 grandchildBlock->setHasMarginAfterQuirk(grandchildBox->style().hasMarginAfterQuirk());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001137 }
1138 }
1139
1140 // Collapse the margin of the grandchild box with our own to produce an estimate.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001141 childBlock.marginBeforeEstimateForChild(*grandchildBox, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001142}
1143
weinig@apple.com12840dc2013-10-22 23:59:08 +00001144LayoutUnit RenderBlockFlow::estimateLogicalTopPosition(RenderBox& child, const MarginInfo& marginInfo, LayoutUnit& estimateWithoutPagination)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001145{
1146 // FIXME: We need to eliminate the estimation of vertical position, because when it's wrong we sometimes trigger a pathological
1147 // relayout if there are intruding floats.
1148 LayoutUnit logicalTopEstimate = logicalHeight();
1149 if (!marginInfo.canCollapseWithMarginBefore()) {
1150 LayoutUnit positiveMarginBefore = 0;
1151 LayoutUnit negativeMarginBefore = 0;
1152 bool discardMarginBefore = false;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001153 if (child.selfNeedsLayout()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001154 // Try to do a basic estimation of how the collapse is going to go.
1155 marginBeforeEstimateForChild(child, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
1156 } else {
1157 // Use the cached collapsed margin values from a previous layout. Most of the time they
1158 // will be right.
1159 MarginValues marginValues = marginValuesForChild(child);
andersca@apple.com86298632013-11-10 19:32:33 +00001160 positiveMarginBefore = std::max(positiveMarginBefore, marginValues.positiveMarginBefore());
1161 negativeMarginBefore = std::max(negativeMarginBefore, marginValues.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001162 discardMarginBefore = mustDiscardMarginBeforeForChild(child);
1163 }
1164
1165 // Collapse the result with our current margins.
1166 if (!discardMarginBefore)
andersca@apple.com86298632013-11-10 19:32:33 +00001167 logicalTopEstimate += std::max(marginInfo.positiveMargin(), positiveMarginBefore) - std::max(marginInfo.negativeMargin(), negativeMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001168 }
1169
1170 // Adjust logicalTopEstimate down to the next page if the margins are so large that we don't fit on the current
1171 // page.
1172 LayoutState* layoutState = view().layoutState();
1173 if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTopEstimate > logicalHeight()
1174 && hasNextPage(logicalHeight()))
andersca@apple.com86298632013-11-10 19:32:33 +00001175 logicalTopEstimate = std::min(logicalTopEstimate, nextPageLogicalTop(logicalHeight()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001176
1177 logicalTopEstimate += getClearDelta(child, logicalTopEstimate);
1178
1179 estimateWithoutPagination = logicalTopEstimate;
1180
1181 if (layoutState->isPaginated()) {
1182 // If the object has a page or column break value of "before", then we should shift to the top of the next page.
1183 logicalTopEstimate = applyBeforeBreak(child, logicalTopEstimate);
1184
1185 // For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
1186 logicalTopEstimate = adjustForUnsplittableChild(child, logicalTopEstimate);
1187
weinig@apple.com12840dc2013-10-22 23:59:08 +00001188 if (!child.selfNeedsLayout() && child.isRenderBlock())
1189 logicalTopEstimate += toRenderBlock(child).paginationStrut();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001190 }
1191
1192 return logicalTopEstimate;
1193}
1194
1195void RenderBlockFlow::setCollapsedBottomMargin(const MarginInfo& marginInfo)
1196{
1197 if (marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()) {
1198 // 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.
1199 // Don't update the max margin values because we won't need them anyway.
1200 if (marginInfo.discardMargin()) {
1201 setMustDiscardMarginAfter();
1202 return;
1203 }
1204
1205 // Update our max pos/neg bottom margins, since we collapsed our bottom margins
1206 // with our children.
andersca@apple.com86298632013-11-10 19:32:33 +00001207 setMaxMarginAfterValues(std::max(maxPositiveMarginAfter(), marginInfo.positiveMargin()), std::max(maxNegativeMarginAfter(), marginInfo.negativeMargin()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001208
1209 if (!marginInfo.hasMarginAfterQuirk())
1210 setHasMarginAfterQuirk(false);
1211
1212 if (marginInfo.hasMarginAfterQuirk() && !marginAfter())
1213 // We have no bottom margin and our last child has a quirky margin.
1214 // We will pick up this quirky margin and pass it through.
1215 // This deals with the <td><div><p> case.
1216 setHasMarginAfterQuirk(true);
1217 }
1218}
1219
1220void RenderBlockFlow::handleAfterSideOfBlock(LayoutUnit beforeSide, LayoutUnit afterSide, MarginInfo& marginInfo)
1221{
1222 marginInfo.setAtAfterSideOfBlock(true);
1223
robert@webkit.org97037ef2013-11-20 19:26:10 +00001224 // If our last child was a self-collapsing block with clearance then our logical height is flush with the
1225 // bottom edge of the float that the child clears. The correct vertical position for the margin-collapsing we want
1226 // to perform now is at the child's margin-top - so adjust our height to that position.
1227 RenderObject* lastBlock = lastChild();
1228 if (lastBlock && lastBlock->isRenderBlockFlow() && toRenderBlockFlow(lastBlock)->isSelfCollapsingBlock())
1229 setLogicalHeight(logicalHeight() - toRenderBlockFlow(lastBlock)->marginOffsetForSelfCollapsingBlock());
1230
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001231 // If we can't collapse with children then go ahead and add in the bottom margin.
1232 if (!marginInfo.discardMargin() && (!marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()
1233 && (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginAfterQuirk())))
1234 setLogicalHeight(logicalHeight() + marginInfo.margin());
1235
1236 // Now add in our bottom border/padding.
1237 setLogicalHeight(logicalHeight() + afterSide);
1238
1239 // Negative margins can cause our height to shrink below our minimal height (border/padding).
1240 // If this happens, ensure that the computed height is increased to the minimal height.
andersca@apple.com86298632013-11-10 19:32:33 +00001241 setLogicalHeight(std::max(logicalHeight(), beforeSide + afterSide));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001242
1243 // Update our bottom collapsed margin info.
1244 setCollapsedBottomMargin(marginInfo);
1245}
1246
1247void RenderBlockFlow::setMaxMarginBeforeValues(LayoutUnit pos, LayoutUnit neg)
1248{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001249 if (!hasRareBlockFlowData()) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001250 if (pos == RenderBlockFlowRareData::positiveMarginBeforeDefault(*this) && neg == RenderBlockFlowRareData::negativeMarginBeforeDefault(*this))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001251 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001252 materializeRareBlockFlowData();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001253 }
weinig@apple.com924a77a2013-11-11 00:22:38 +00001254
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001255 rareBlockFlowData()->m_margins.setPositiveMarginBefore(pos);
1256 rareBlockFlowData()->m_margins.setNegativeMarginBefore(neg);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001257}
1258
1259void RenderBlockFlow::setMaxMarginAfterValues(LayoutUnit pos, LayoutUnit neg)
1260{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001261 if (!hasRareBlockFlowData()) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001262 if (pos == RenderBlockFlowRareData::positiveMarginAfterDefault(*this) && neg == RenderBlockFlowRareData::negativeMarginAfterDefault(*this))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001263 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001264 materializeRareBlockFlowData();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001265 }
weinig@apple.com924a77a2013-11-11 00:22:38 +00001266
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001267 rareBlockFlowData()->m_margins.setPositiveMarginAfter(pos);
1268 rareBlockFlowData()->m_margins.setNegativeMarginAfter(neg);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001269}
1270
1271void RenderBlockFlow::setMustDiscardMarginBefore(bool value)
1272{
akling@apple.com827be9c2013-10-29 02:58:43 +00001273 if (style().marginBeforeCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001274 ASSERT(value);
1275 return;
1276 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001277
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001278 if (!hasRareBlockFlowData()) {
weinig@apple.com924a77a2013-11-11 00:22:38 +00001279 if (!value)
1280 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001281 materializeRareBlockFlowData();
weinig@apple.com924a77a2013-11-11 00:22:38 +00001282 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001283
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001284 rareBlockFlowData()->m_discardMarginBefore = value;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001285}
1286
1287void RenderBlockFlow::setMustDiscardMarginAfter(bool value)
1288{
akling@apple.com827be9c2013-10-29 02:58:43 +00001289 if (style().marginAfterCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001290 ASSERT(value);
1291 return;
1292 }
1293
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001294 if (!hasRareBlockFlowData()) {
weinig@apple.com924a77a2013-11-11 00:22:38 +00001295 if (!value)
1296 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001297 materializeRareBlockFlowData();
weinig@apple.com924a77a2013-11-11 00:22:38 +00001298 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001299
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001300 rareBlockFlowData()->m_discardMarginAfter = value;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001301}
1302
1303bool RenderBlockFlow::mustDiscardMarginBefore() const
1304{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001305 return style().marginBeforeCollapse() == MDISCARD || (hasRareBlockFlowData() && rareBlockFlowData()->m_discardMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001306}
1307
1308bool RenderBlockFlow::mustDiscardMarginAfter() const
1309{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001310 return style().marginAfterCollapse() == MDISCARD || (hasRareBlockFlowData() && rareBlockFlowData()->m_discardMarginAfter);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001311}
1312
weinig@apple.com12840dc2013-10-22 23:59:08 +00001313bool RenderBlockFlow::mustDiscardMarginBeforeForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001314{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001315 ASSERT(!child.selfNeedsLayout());
1316 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001317 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginBefore() : (child.style().marginBeforeCollapse() == MDISCARD);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001318 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001319 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginAfter() : (child.style().marginAfterCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001320
1321 // FIXME: We return false here because the implementation is not geometrically complete. We have values only for before/after, not start/end.
1322 // In case the boxes are perpendicular we assume the property is not specified.
1323 return false;
1324}
1325
weinig@apple.com12840dc2013-10-22 23:59:08 +00001326bool RenderBlockFlow::mustDiscardMarginAfterForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001327{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001328 ASSERT(!child.selfNeedsLayout());
1329 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001330 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginAfter() : (child.style().marginAfterCollapse() == MDISCARD);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001331 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001332 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginBefore() : (child.style().marginBeforeCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001333
1334 // FIXME: See |mustDiscardMarginBeforeForChild| above.
1335 return false;
1336}
1337
weinig@apple.com12840dc2013-10-22 23:59:08 +00001338bool RenderBlockFlow::mustSeparateMarginBeforeForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001339{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001340 ASSERT(!child.selfNeedsLayout());
akling@apple.com827be9c2013-10-29 02:58:43 +00001341 const RenderStyle& childStyle = child.style();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001342 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001343 return childStyle.marginBeforeCollapse() == MSEPARATE;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001344 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001345 return childStyle.marginAfterCollapse() == MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001346
1347 // FIXME: See |mustDiscardMarginBeforeForChild| above.
1348 return false;
1349}
1350
weinig@apple.com12840dc2013-10-22 23:59:08 +00001351bool RenderBlockFlow::mustSeparateMarginAfterForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001352{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001353 ASSERT(!child.selfNeedsLayout());
akling@apple.com827be9c2013-10-29 02:58:43 +00001354 const RenderStyle& childStyle = child.style();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001355 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001356 return childStyle.marginAfterCollapse() == MSEPARATE;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001357 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001358 return childStyle.marginBeforeCollapse() == MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001359
1360 // FIXME: See |mustDiscardMarginBeforeForChild| above.
1361 return false;
1362}
1363
weinig@apple.com12840dc2013-10-22 23:59:08 +00001364static bool inNormalFlow(RenderBox& child)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001365{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001366 RenderBlock* curr = child.containingBlock();
1367 while (curr && curr != &child.view()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001368 if (curr->hasColumns() || curr->isRenderFlowThread())
1369 return true;
1370 if (curr->isFloatingOrOutOfFlowPositioned())
1371 return false;
1372 curr = curr->containingBlock();
1373 }
1374 return true;
1375}
1376
weinig@apple.com12840dc2013-10-22 23:59:08 +00001377LayoutUnit RenderBlockFlow::applyBeforeBreak(RenderBox& child, LayoutUnit logicalOffset)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001378{
1379 // FIXME: Add page break checking here when we support printing.
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001380 RenderFlowThread* flowThread = flowThreadContainingBlock();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001381 bool isInsideMulticolFlowThread = flowThread && !flowThread->isRenderNamedFlowThread();
1382 bool checkColumnBreaks = isInsideMulticolFlowThread || view().layoutState()->isPaginatingColumns();
1383 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 +00001384 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001385 bool checkBeforeAlways = (checkColumnBreaks && child.style().columnBreakBefore() == PBALWAYS)
1386 || (checkPageBreaks && child.style().pageBreakBefore() == PBALWAYS)
akling@apple.com827be9c2013-10-29 02:58:43 +00001387 || (checkRegionBreaks && child.style().regionBreakBefore() == PBALWAYS);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001388 if (checkBeforeAlways && inNormalFlow(child) && hasNextPage(logicalOffset, IncludePageBoundary)) {
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001389 if (checkColumnBreaks) {
1390 if (isInsideMulticolFlowThread)
1391 checkRegionBreaks = true;
1392 else
1393 view().layoutState()->addForcedColumnBreak(&child, logicalOffset);
1394 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001395 if (checkRegionBreaks) {
1396 LayoutUnit offsetBreakAdjustment = 0;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001397 if (flowThread->addForcedRegionBreak(this, offsetFromLogicalTopOfFirstPage() + logicalOffset, &child, true, &offsetBreakAdjustment))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001398 return logicalOffset + offsetBreakAdjustment;
1399 }
1400 return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
1401 }
1402 return logicalOffset;
1403}
1404
weinig@apple.com12840dc2013-10-22 23:59:08 +00001405LayoutUnit RenderBlockFlow::applyAfterBreak(RenderBox& child, LayoutUnit logicalOffset, MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001406{
1407 // FIXME: Add page break checking here when we support printing.
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001408 RenderFlowThread* flowThread = flowThreadContainingBlock();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001409 bool isInsideMulticolFlowThread = flowThread && !flowThread->isRenderNamedFlowThread();
1410 bool checkColumnBreaks = isInsideMulticolFlowThread || view().layoutState()->isPaginatingColumns();
1411 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 +00001412 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001413 bool checkAfterAlways = (checkColumnBreaks && child.style().columnBreakAfter() == PBALWAYS)
1414 || (checkPageBreaks && child.style().pageBreakAfter() == PBALWAYS)
akling@apple.com827be9c2013-10-29 02:58:43 +00001415 || (checkRegionBreaks && child.style().regionBreakAfter() == PBALWAYS);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001416 if (checkAfterAlways && inNormalFlow(child) && hasNextPage(logicalOffset, IncludePageBoundary)) {
1417 LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
1418
1419 // So our margin doesn't participate in the next collapsing steps.
1420 marginInfo.clearMargin();
1421
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001422 if (checkColumnBreaks) {
1423 if (isInsideMulticolFlowThread)
1424 checkRegionBreaks = true;
1425 else
1426 view().layoutState()->addForcedColumnBreak(&child, logicalOffset);
1427 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001428 if (checkRegionBreaks) {
1429 LayoutUnit offsetBreakAdjustment = 0;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001430 if (flowThread->addForcedRegionBreak(this, offsetFromLogicalTopOfFirstPage() + logicalOffset + marginOffset, &child, false, &offsetBreakAdjustment))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001431 return logicalOffset + marginOffset + offsetBreakAdjustment;
1432 }
1433 return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
1434 }
1435 return logicalOffset;
1436}
1437
weinig@apple.com12840dc2013-10-22 23:59:08 +00001438LayoutUnit RenderBlockFlow::adjustBlockChildForPagination(LayoutUnit logicalTopAfterClear, LayoutUnit estimateWithoutPagination, RenderBox& child, bool atBeforeSideOfBlock)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001439{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001440 RenderBlock* childRenderBlock = child.isRenderBlock() ? toRenderBlock(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001441
1442 if (estimateWithoutPagination != logicalTopAfterClear) {
1443 // Our guess prior to pagination movement was wrong. Before we attempt to paginate, let's try again at the new
1444 // position.
1445 setLogicalHeight(logicalTopAfterClear);
1446 setLogicalTopForChild(child, logicalTopAfterClear, ApplyLayoutDelta);
1447
weinig@apple.com12840dc2013-10-22 23:59:08 +00001448 if (child.shrinkToAvoidFloats()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001449 // The child's width depends on the line width.
1450 // When the child shifts to clear an item, its width can
1451 // change (because it has more available line width).
1452 // So go ahead and mark the item as dirty.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001453 child.setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001454 }
1455
1456 if (childRenderBlock) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001457 if (!child.avoidsFloats() && childRenderBlock->containsFloats())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001458 toRenderBlockFlow(childRenderBlock)->markAllDescendantsWithFloatsForLayout();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001459 if (!child.needsLayout())
1460 child.markForPaginationRelayoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001461 }
1462
1463 // Our guess was wrong. Make the child lay itself out again.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001464 child.layoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001465 }
1466
1467 LayoutUnit oldTop = logicalTopAfterClear;
1468
1469 // If the object has a page or column break value of "before", then we should shift to the top of the next page.
1470 LayoutUnit result = applyBeforeBreak(child, logicalTopAfterClear);
1471
1472 if (pageLogicalHeightForOffset(result)) {
1473 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(result, ExcludePageBoundary);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001474 LayoutUnit spaceShortage = child.logicalHeight() - remainingLogicalHeight;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001475 if (spaceShortage > 0) {
1476 // If the child crosses a column boundary, report a break, in case nothing inside it has already
1477 // done so. The column balancer needs to know how much it has to stretch the columns to make more
1478 // content fit. If no breaks are reported (but do occur), the balancer will have no clue. FIXME:
1479 // This should be improved, though, because here we just pretend that the child is
1480 // unsplittable. A splittable child, on the other hand, has break opportunities at every position
1481 // where there's no child content, border or padding. In other words, we risk stretching more
1482 // than necessary.
1483 setPageBreak(result, spaceShortage);
1484 }
1485 }
1486
1487 // For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
1488 LayoutUnit logicalTopBeforeUnsplittableAdjustment = result;
1489 LayoutUnit logicalTopAfterUnsplittableAdjustment = adjustForUnsplittableChild(child, result);
1490
1491 LayoutUnit paginationStrut = 0;
1492 LayoutUnit unsplittableAdjustmentDelta = logicalTopAfterUnsplittableAdjustment - logicalTopBeforeUnsplittableAdjustment;
1493 if (unsplittableAdjustmentDelta)
1494 paginationStrut = unsplittableAdjustmentDelta;
1495 else if (childRenderBlock && childRenderBlock->paginationStrut())
1496 paginationStrut = childRenderBlock->paginationStrut();
1497
1498 if (paginationStrut) {
1499 // We are willing to propagate out to our parent block as long as we were at the top of the block prior
1500 // to collapsing our margins, and as long as we didn't clear or move as a result of other pagination.
1501 if (atBeforeSideOfBlock && oldTop == result && !isOutOfFlowPositioned() && !isTableCell()) {
1502 // FIXME: Should really check if we're exceeding the page height before propagating the strut, but we don't
1503 // have all the information to do so (the strut only has the remaining amount to push). Gecko gets this wrong too
1504 // and pushes to the next page anyway, so not too concerned about it.
1505 setPaginationStrut(result + paginationStrut);
1506 if (childRenderBlock)
1507 childRenderBlock->setPaginationStrut(0);
1508 } else
1509 result += paginationStrut;
1510 }
1511
1512 // Similar to how we apply clearance. Go ahead and boost height() to be the place where we're going to position the child.
1513 setLogicalHeight(logicalHeight() + (result - oldTop));
1514
1515 // Return the final adjusted logical top.
1516 return result;
1517}
1518
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001519static inline LayoutUnit calculateMinimumPageHeight(RenderStyle* renderStyle, RootInlineBox* lastLine, LayoutUnit lineTop, LayoutUnit lineBottom)
1520{
1521 // We may require a certain minimum number of lines per page in order to satisfy
1522 // orphans and widows, and that may affect the minimum page height.
andersca@apple.com86298632013-11-10 19:32:33 +00001523 unsigned lineCount = std::max<unsigned>(renderStyle->hasAutoOrphans() ? 1 : renderStyle->orphans(), renderStyle->hasAutoWidows() ? 1 : renderStyle->widows());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001524 if (lineCount > 1) {
1525 RootInlineBox* line = lastLine;
1526 for (unsigned i = 1; i < lineCount && line->prevRootBox(); i++)
1527 line = line->prevRootBox();
1528
1529 // FIXME: Paginating using line overflow isn't all fine. See FIXME in
1530 // adjustLinePositionForPagination() for more details.
1531 LayoutRect overflow = line->logicalVisualOverflowRect(line->lineTop(), line->lineBottom());
andersca@apple.com86298632013-11-10 19:32:33 +00001532 lineTop = std::min(line->lineTopWithLeading(), overflow.y());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001533 }
1534 return lineBottom - lineTop;
1535}
1536
1537void RenderBlockFlow::adjustLinePositionForPagination(RootInlineBox* lineBox, LayoutUnit& delta, RenderFlowThread* flowThread)
1538{
1539 // FIXME: For now we paginate using line overflow. This ensures that lines don't overlap at all when we
1540 // put a strut between them for pagination purposes. However, this really isn't the desired rendering, since
1541 // the line on the top of the next page will appear too far down relative to the same kind of line at the top
1542 // of the first column.
1543 //
1544 // The rendering we would like to see is one where the lineTopWithLeading is at the top of the column, and any line overflow
1545 // simply spills out above the top of the column. This effect would match what happens at the top of the first column.
1546 // We can't achieve this rendering, however, until we stop columns from clipping to the column bounds (thus allowing
1547 // for overflow to occur), and then cache visible overflow for each column rect.
1548 //
1549 // Furthermore, the paint we have to do when a column has overflow has to be special. We need to exclude
1550 // content that paints in a previous column (and content that paints in the following column).
1551 //
1552 // For now we'll at least honor the lineTopWithLeading when paginating if it is above the logical top overflow. This will
1553 // at least make positive leading work in typical cases.
1554 //
1555 // FIXME: Another problem with simply moving lines is that the available line width may change (because of floats).
1556 // Technically if the location we move the line to has a different line width than our old position, then we need to dirty the
1557 // line and all following lines.
1558 LayoutRect logicalVisualOverflow = lineBox->logicalVisualOverflowRect(lineBox->lineTop(), lineBox->lineBottom());
andersca@apple.com86298632013-11-10 19:32:33 +00001559 LayoutUnit logicalOffset = std::min(lineBox->lineTopWithLeading(), logicalVisualOverflow.y());
1560 LayoutUnit logicalBottom = std::max(lineBox->lineBottomWithLeading(), logicalVisualOverflow.maxY());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001561 LayoutUnit lineHeight = logicalBottom - logicalOffset;
akling@apple.com827be9c2013-10-29 02:58:43 +00001562 updateMinimumPageHeight(logicalOffset, calculateMinimumPageHeight(&style(), lineBox, logicalOffset, logicalBottom));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001563 logicalOffset += delta;
1564 lineBox->setPaginationStrut(0);
1565 lineBox->setIsFirstAfterPageBreak(false);
1566 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1567 bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
1568 // If lineHeight is greater than pageLogicalHeight, but logicalVisualOverflow.height() still fits, we are
1569 // still going to add a strut, so that the visible overflow fits on a single page.
1570 if (!pageLogicalHeight || (hasUniformPageLogicalHeight && logicalVisualOverflow.height() > pageLogicalHeight)
1571 || !hasNextPage(logicalOffset))
abucur@adobe.comd40287b2013-10-08 17:33:05 +00001572 // 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.
1573 // 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 +00001574 return;
1575 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
1576
1577 int lineIndex = lineCount(lineBox);
1578 if (remainingLogicalHeight < lineHeight || (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex)) {
abucur@adobe.comfc497132013-10-04 08:49:21 +00001579 if (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex) {
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001580 clearShouldBreakAtLineToAvoidWidow();
abucur@adobe.comfc497132013-10-04 08:49:21 +00001581 setDidBreakAtLineToAvoidWidow();
1582 }
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001583 // If we have a non-uniform page height, then we have to shift further possibly.
1584 if (!hasUniformPageLogicalHeight && !pushToNextPageWithMinimumLogicalHeight(remainingLogicalHeight, logicalOffset, lineHeight))
1585 return;
1586 if (lineHeight > pageLogicalHeight) {
1587 // Split the top margin in order to avoid splitting the visible part of the line.
andersca@apple.com86298632013-11-10 19:32:33 +00001588 remainingLogicalHeight -= std::min(lineHeight - pageLogicalHeight, std::max<LayoutUnit>(0, logicalVisualOverflow.y() - lineBox->lineTopWithLeading()));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001589 }
andersca@apple.com86298632013-11-10 19:32:33 +00001590 LayoutUnit totalLogicalHeight = lineHeight + std::max<LayoutUnit>(0, logicalOffset);
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001591 LayoutUnit pageLogicalHeightAtNewOffset = hasUniformPageLogicalHeight ? pageLogicalHeight : pageLogicalHeightForOffset(logicalOffset + remainingLogicalHeight);
1592 setPageBreak(logicalOffset, lineHeight - remainingLogicalHeight);
akling@apple.com827be9c2013-10-29 02:58:43 +00001593 if (((lineBox == firstRootBox() && totalLogicalHeight < pageLogicalHeightAtNewOffset) || (!style().hasAutoOrphans() && style().orphans() >= lineIndex))
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001594 && !isOutOfFlowPositioned() && !isTableCell())
andersca@apple.com86298632013-11-10 19:32:33 +00001595 setPaginationStrut(remainingLogicalHeight + std::max<LayoutUnit>(0, logicalOffset));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001596 else {
1597 delta += remainingLogicalHeight;
1598 lineBox->setPaginationStrut(remainingLogicalHeight);
1599 lineBox->setIsFirstAfterPageBreak(true);
1600 }
commit-queue@webkit.org883b01c2014-01-20 08:58:36 +00001601 } else if (remainingLogicalHeight == pageLogicalHeight) {
1602 // We're at the very top of a page or column.
1603 if (lineBox != firstRootBox())
1604 lineBox->setIsFirstAfterPageBreak(true);
1605 if (lineBox != firstRootBox() || offsetFromLogicalTopOfFirstPage())
1606 setPageBreak(logicalOffset, lineHeight);
1607 }
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001608}
1609
1610void RenderBlockFlow::setBreakAtLineToAvoidWidow(int lineToBreak)
1611{
abucur@adobe.comfc497132013-10-04 08:49:21 +00001612 ASSERT(lineToBreak >= 0);
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001613 ASSERT(!ensureRareBlockFlowData().m_didBreakAtLineToAvoidWidow);
1614 ensureRareBlockFlowData().m_lineBreakToAvoidWidow = lineToBreak;
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001615}
1616
abucur@adobe.comfc497132013-10-04 08:49:21 +00001617void RenderBlockFlow::setDidBreakAtLineToAvoidWidow()
1618{
1619 ASSERT(!shouldBreakAtLineToAvoidWidow());
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001620 if (!hasRareBlockFlowData())
abucur@adobe.comfc497132013-10-04 08:49:21 +00001621 return;
1622
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001623 rareBlockFlowData()->m_didBreakAtLineToAvoidWidow = true;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001624}
1625
1626void RenderBlockFlow::clearDidBreakAtLineToAvoidWidow()
1627{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001628 if (!hasRareBlockFlowData())
abucur@adobe.comfc497132013-10-04 08:49:21 +00001629 return;
1630
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001631 rareBlockFlowData()->m_didBreakAtLineToAvoidWidow = false;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001632}
1633
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001634void RenderBlockFlow::clearShouldBreakAtLineToAvoidWidow() const
1635{
abucur@adobe.comfc497132013-10-04 08:49:21 +00001636 ASSERT(shouldBreakAtLineToAvoidWidow());
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001637 if (!hasRareBlockFlowData())
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001638 return;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001639
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001640 rareBlockFlowData()->m_lineBreakToAvoidWidow = -1;
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001641}
1642
1643bool RenderBlockFlow::relayoutToAvoidWidows(LayoutStateMaintainer& statePusher)
1644{
1645 if (!shouldBreakAtLineToAvoidWidow())
1646 return false;
1647
1648 statePusher.pop();
1649 setEverHadLayout(true);
1650 layoutBlock(false);
1651 return true;
1652}
1653
weinig@apple.com31324fd2013-10-28 19:22:51 +00001654bool RenderBlockFlow::hasNextPage(LayoutUnit logicalOffset, PageBoundaryRule pageBoundaryRule) const
1655{
1656 ASSERT(view().layoutState() && view().layoutState()->isPaginated());
1657
1658 RenderFlowThread* flowThread = flowThreadContainingBlock();
1659 if (!flowThread)
1660 return true; // Printing and multi-column both make new pages to accommodate content.
1661
1662 // See if we're in the last region.
1663 LayoutUnit pageOffset = offsetFromLogicalTopOfFirstPage() + logicalOffset;
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00001664 RenderRegion* region = flowThread->regionAtBlockOffset(this, pageOffset, true);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001665 if (!region)
1666 return false;
1667 if (region->isLastRegion())
akling@apple.com827be9c2013-10-29 02:58:43 +00001668 return region->isRenderRegionSet() || region->style().regionFragment() == BreakRegionFragment
weinig@apple.com31324fd2013-10-28 19:22:51 +00001669 || (pageBoundaryRule == IncludePageBoundary && pageOffset == region->logicalTopForFlowThreadContent());
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00001670
1671 RenderRegion* startRegion = 0;
1672 RenderRegion* endRegion = 0;
1673 flowThread->getRegionRangeForBox(this, startRegion, endRegion);
1674
1675 if (region == endRegion)
1676 return false;
weinig@apple.com31324fd2013-10-28 19:22:51 +00001677 return true;
1678}
1679
1680LayoutUnit RenderBlockFlow::adjustForUnsplittableChild(RenderBox& child, LayoutUnit logicalOffset, bool includeMargins)
1681{
1682 bool checkColumnBreaks = view().layoutState()->isPaginatingColumns();
1683 bool checkPageBreaks = !checkColumnBreaks && view().layoutState()->m_pageLogicalHeight;
1684 RenderFlowThread* flowThread = flowThreadContainingBlock();
1685 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
akling@apple.com827be9c2013-10-29 02:58:43 +00001686 bool isUnsplittable = child.isUnsplittableForPagination() || (checkColumnBreaks && child.style().columnBreakInside() == PBAVOID)
1687 || (checkPageBreaks && child.style().pageBreakInside() == PBAVOID)
1688 || (checkRegionBreaks && child.style().regionBreakInside() == PBAVOID);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001689 if (!isUnsplittable)
1690 return logicalOffset;
1691 LayoutUnit childLogicalHeight = logicalHeightForChild(child) + (includeMargins ? marginBeforeForChild(child) + marginAfterForChild(child) : LayoutUnit());
1692 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1693 bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
1694 updateMinimumPageHeight(logicalOffset, childLogicalHeight);
1695 if (!pageLogicalHeight || (hasUniformPageLogicalHeight && childLogicalHeight > pageLogicalHeight)
1696 || !hasNextPage(logicalOffset))
1697 return logicalOffset;
1698 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
1699 if (remainingLogicalHeight < childLogicalHeight) {
1700 if (!hasUniformPageLogicalHeight && !pushToNextPageWithMinimumLogicalHeight(remainingLogicalHeight, logicalOffset, childLogicalHeight))
1701 return logicalOffset;
1702 return logicalOffset + remainingLogicalHeight;
1703 }
1704 return logicalOffset;
1705}
1706
1707bool RenderBlockFlow::pushToNextPageWithMinimumLogicalHeight(LayoutUnit& adjustment, LayoutUnit logicalOffset, LayoutUnit minimumLogicalHeight) const
1708{
1709 bool checkRegion = false;
1710 for (LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset + adjustment); pageLogicalHeight;
1711 pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset + adjustment)) {
1712 if (minimumLogicalHeight <= pageLogicalHeight)
1713 return true;
1714 if (!hasNextPage(logicalOffset + adjustment))
1715 return false;
1716 adjustment += pageLogicalHeight;
1717 checkRegion = true;
1718 }
1719 return !checkRegion;
1720}
1721
1722void RenderBlockFlow::setPageBreak(LayoutUnit offset, LayoutUnit spaceShortage)
1723{
1724 if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1725 flowThread->setPageBreak(this, offsetFromLogicalTopOfFirstPage() + offset, spaceShortage);
1726}
1727
1728void RenderBlockFlow::updateMinimumPageHeight(LayoutUnit offset, LayoutUnit minHeight)
1729{
1730 if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1731 flowThread->updateMinimumPageHeight(this, offsetFromLogicalTopOfFirstPage() + offset, minHeight);
1732 else if (ColumnInfo* colInfo = view().layoutState()->m_columnInfo)
1733 colInfo->updateMinimumColumnHeight(minHeight);
1734}
1735
1736LayoutUnit RenderBlockFlow::nextPageLogicalTop(LayoutUnit logicalOffset, PageBoundaryRule pageBoundaryRule) const
1737{
1738 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1739 if (!pageLogicalHeight)
1740 return logicalOffset;
1741
1742 // The logicalOffset is in our coordinate space. We can add in our pushed offset.
1743 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset);
1744 if (pageBoundaryRule == ExcludePageBoundary)
1745 return logicalOffset + (remainingLogicalHeight ? remainingLogicalHeight : pageLogicalHeight);
1746 return logicalOffset + remainingLogicalHeight;
1747}
1748
1749LayoutUnit RenderBlockFlow::pageLogicalTopForOffset(LayoutUnit offset) const
1750{
1751 LayoutUnit firstPageLogicalTop = isHorizontalWritingMode() ? view().layoutState()->m_pageOffset.height() : view().layoutState()->m_pageOffset.width();
1752 LayoutUnit blockLogicalTop = isHorizontalWritingMode() ? view().layoutState()->m_layoutOffset.height() : view().layoutState()->m_layoutOffset.width();
1753
1754 LayoutUnit cumulativeOffset = offset + blockLogicalTop;
1755 RenderFlowThread* flowThread = flowThreadContainingBlock();
1756 if (!flowThread) {
1757 LayoutUnit pageLogicalHeight = view().layoutState()->pageLogicalHeight();
1758 if (!pageLogicalHeight)
1759 return 0;
1760 return cumulativeOffset - roundToInt(cumulativeOffset - firstPageLogicalTop) % roundToInt(pageLogicalHeight);
1761 }
1762 return flowThread->pageLogicalTopForOffset(cumulativeOffset);
1763}
1764
1765LayoutUnit RenderBlockFlow::pageLogicalHeightForOffset(LayoutUnit offset) const
1766{
1767 RenderFlowThread* flowThread = flowThreadContainingBlock();
1768 if (!flowThread)
1769 return view().layoutState()->m_pageLogicalHeight;
1770 return flowThread->pageLogicalHeightForOffset(offset + offsetFromLogicalTopOfFirstPage());
1771}
1772
1773LayoutUnit RenderBlockFlow::pageRemainingLogicalHeightForOffset(LayoutUnit offset, PageBoundaryRule pageBoundaryRule) const
1774{
1775 offset += offsetFromLogicalTopOfFirstPage();
1776
1777 RenderFlowThread* flowThread = flowThreadContainingBlock();
1778 if (!flowThread) {
1779 LayoutUnit pageLogicalHeight = view().layoutState()->m_pageLogicalHeight;
1780 LayoutUnit remainingHeight = pageLogicalHeight - intMod(offset, pageLogicalHeight);
1781 if (pageBoundaryRule == IncludePageBoundary) {
1782 // If includeBoundaryPoint is true the line exactly on the top edge of a
1783 // column will act as being part of the previous column.
1784 remainingHeight = intMod(remainingHeight, pageLogicalHeight);
1785 }
1786 return remainingHeight;
1787 }
1788
1789 return flowThread->pageRemainingLogicalHeightForOffset(offset, pageBoundaryRule);
1790}
1791
1792
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001793void RenderBlockFlow::layoutLineGridBox()
1794{
akling@apple.com827be9c2013-10-29 02:58:43 +00001795 if (style().lineGrid() == RenderStyle::initialLineGrid()) {
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001796 setLineGridBox(0);
1797 return;
1798 }
1799
1800 setLineGridBox(0);
1801
akling@apple.com1aa97b02013-10-31 21:59:49 +00001802 auto lineGridBox = std::make_unique<RootInlineBox>(*this);
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001803 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
1804 lineGridBox->setConstructed();
1805 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1806 VerticalPositionCache verticalPositionCache;
1807 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
1808
akling@apple.com1aa97b02013-10-31 21:59:49 +00001809 setLineGridBox(std::move(lineGridBox));
1810
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001811 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
1812 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
1813 // to this grid.
1814}
1815
weinig@apple.com12840dc2013-10-22 23:59:08 +00001816bool RenderBlockFlow::containsFloat(RenderBox& renderer) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001817{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001818 return m_floatingObjects && m_floatingObjects->set().contains<RenderBox&, FloatingObjectHashTranslator>(renderer);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001819}
1820
1821void RenderBlockFlow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
1822{
1823 RenderBlock::styleDidChange(diff, oldStyle);
1824
1825 // After our style changed, if we lose our ability to propagate floats into next sibling
1826 // blocks, then we need to find the top most parent containing that overhanging float and
1827 // then mark its descendants with floats for layout and clear all floats from its next
1828 // sibling blocks that exist in our floating objects list. See bug 56299 and 62875.
1829 bool canPropagateFloatIntoSibling = !isFloatingOrOutOfFlowPositioned() && !avoidsFloats();
1830 if (diff == StyleDifferenceLayout && s_canPropagateFloatIntoSibling && !canPropagateFloatIntoSibling && hasOverhangingFloats()) {
1831 RenderBlockFlow* parentBlock = this;
1832 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001833
weinig@apple.comc77041e2013-12-14 18:05:45 +00001834 for (auto& ancestor : ancestorsOfType<RenderBlockFlow>(*this)) {
1835 if (ancestor.isRenderView())
akling@apple.comf3028052013-11-04 08:46:06 +00001836 break;
weinig@apple.comc77041e2013-12-14 18:05:45 +00001837 if (ancestor.hasOverhangingFloats()) {
akling@apple.comf3028052013-11-04 08:46:06 +00001838 for (auto it = floatingObjectSet.begin(), end = floatingObjectSet.end(); it != end; ++it) {
1839 RenderBox& renderer = (*it)->renderer();
weinig@apple.comc77041e2013-12-14 18:05:45 +00001840 if (ancestor.hasOverhangingFloat(renderer)) {
1841 parentBlock = &ancestor;
akling@apple.comf3028052013-11-04 08:46:06 +00001842 break;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001843 }
1844 }
1845 }
1846 }
1847
1848 parentBlock->markAllDescendantsWithFloatsForLayout();
1849 parentBlock->markSiblingsWithFloatsForLayout();
1850 }
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00001851
akling@apple.com8f40c5b2013-10-27 22:54:07 +00001852 if (auto fragment = renderNamedFlowFragment())
akling@apple.com827be9c2013-10-29 02:58:43 +00001853 fragment->setStyle(RenderNamedFlowFragment::createStyle(style()));
antti@apple.com42fb53d2013-10-25 02:33:11 +00001854
1855 if (diff >= StyleDifferenceRepaint)
1856 invalidateLineLayoutPath();
hyatt@apple.comd4be3772014-01-24 19:55:33 +00001857
1858 if (multiColumnFlowThread()) {
1859 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox())
1860 child->setStyle(RenderStyle::createAnonymousStyleWithDisplay(&style(), BLOCK));
1861 }
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001862}
1863
akling@apple.combdae43242013-10-25 12:00:20 +00001864void RenderBlockFlow::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001865{
akling@apple.com827be9c2013-10-29 02:58:43 +00001866 const RenderStyle* oldStyle = hasInitializedStyle() ? &style() : nullptr;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001867 s_canPropagateFloatIntoSibling = oldStyle ? !isFloatingOrOutOfFlowPositioned() && !avoidsFloats() : false;
1868
akling@apple.combdae43242013-10-25 12:00:20 +00001869 if (oldStyle && parent() && diff == StyleDifferenceLayout && oldStyle->position() != newStyle.position()) {
1870 if (containsFloats() && !isFloating() && !isOutOfFlowPositioned() && newStyle.hasOutOfFlowPosition())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001871 markAllDescendantsWithFloatsForLayout();
1872 }
1873
1874 RenderBlock::styleWillChange(diff, newStyle);
1875}
1876
antti@apple.coma2c7f242013-10-22 22:37:25 +00001877void RenderBlockFlow::deleteLines()
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001878{
1879 if (containsFloats())
1880 m_floatingObjects->clearLineBoxTreePointers();
weinig@apple.com611b9292013-10-20 22:57:54 +00001881
antti@apple.comfea51992013-10-28 13:39:23 +00001882 if (m_simpleLineLayout) {
antti@apple.com940f5872013-10-24 20:31:11 +00001883 ASSERT(!m_lineBoxes.firstLineBox());
antti@apple.comfea51992013-10-28 13:39:23 +00001884 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +00001885 } else
akling@apple.com31dd4f42013-10-30 22:27:59 +00001886 m_lineBoxes.deleteLineBoxTree();
weinig@apple.com611b9292013-10-20 22:57:54 +00001887
antti@apple.coma2c7f242013-10-22 22:37:25 +00001888 RenderBlock::deleteLines();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001889}
1890
1891void RenderBlockFlow::moveAllChildrenIncludingFloatsTo(RenderBlock* toBlock, bool fullRemoveInsert)
1892{
1893 RenderBlockFlow* toBlockFlow = toRenderBlockFlow(toBlock);
1894 moveAllChildrenTo(toBlockFlow, fullRemoveInsert);
1895
1896 // When a portion of the render tree is being detached, anonymous blocks
1897 // will be combined as their children are deleted. In this process, the
1898 // anonymous block later in the tree is merged into the one preceeding it.
1899 // It can happen that the later block (this) contains floats that the
1900 // previous block (toBlockFlow) did not contain, and thus are not in the
1901 // floating objects list for toBlockFlow. This can result in toBlockFlow
1902 // containing floats that are not in it's floating objects list, but are in
1903 // the floating objects lists of siblings and parents. This can cause
1904 // problems when the float itself is deleted, since the deletion code
1905 // assumes that if a float is not in it's containing block's floating
1906 // objects list, it isn't in any floating objects list. In order to
1907 // preserve this condition (removing it has serious performance
1908 // implications), we need to copy the floating objects from the old block
1909 // (this) to the new block (toBlockFlow). The float's metrics will likely
1910 // all be wrong, but since toBlockFlow is already marked for layout, this
1911 // will get fixed before anything gets displayed.
1912 // See bug https://bugs.webkit.org/show_bug.cgi?id=115566
1913 if (m_floatingObjects) {
1914 if (!toBlockFlow->m_floatingObjects)
1915 toBlockFlow->createFloatingObjects();
1916
1917 const FloatingObjectSet& fromFloatingObjectSet = m_floatingObjects->set();
1918 auto end = fromFloatingObjectSet.end();
1919
1920 for (auto it = fromFloatingObjectSet.begin(); it != end; ++it) {
1921 FloatingObject* floatingObject = it->get();
1922
1923 // Don't insert the object again if it's already in the list
weinig@apple.com12840dc2013-10-22 23:59:08 +00001924 if (toBlockFlow->containsFloat(floatingObject->renderer()))
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001925 continue;
1926
1927 toBlockFlow->m_floatingObjects->add(floatingObject->unsafeClone());
1928 }
1929 }
1930}
1931
1932void RenderBlockFlow::addOverflowFromFloats()
1933{
1934 if (!m_floatingObjects)
1935 return;
1936
1937 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1938 auto end = floatingObjectSet.end();
1939 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
1940 FloatingObject* r = it->get();
1941 if (r->isDescendant())
1942 addOverflowFromChild(&r->renderer(), IntSize(xPositionForFloatIncludingMargin(r), yPositionForFloatIncludingMargin(r)));
1943 }
1944}
1945
1946void RenderBlockFlow::computeOverflow(LayoutUnit oldClientAfterEdge, bool recomputeFloats)
1947{
1948 RenderBlock::computeOverflow(oldClientAfterEdge, recomputeFloats);
1949
1950 if (!hasColumns() && (recomputeFloats || isRoot() || expandsToEncloseOverhangingFloats() || hasSelfPaintingLayer()))
1951 addOverflowFromFloats();
1952}
1953
1954void RenderBlockFlow::repaintOverhangingFloats(bool paintAllDescendants)
1955{
1956 // Repaint any overhanging floats (if we know we're the one to paint them).
1957 // Otherwise, bail out.
1958 if (!hasOverhangingFloats())
1959 return;
1960
1961 // FIXME: Avoid disabling LayoutState. At the very least, don't disable it for floats originating
1962 // in this block. Better yet would be to push extra state for the containers of other floats.
1963 LayoutStateDisabler layoutStateDisabler(&view());
1964 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1965 auto end = floatingObjectSet.end();
1966 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
1967 FloatingObject* floatingObject = it->get();
1968 // Only repaint the object if it is overhanging, is not in its own layer, and
1969 // is our responsibility to paint (m_shouldPaint is set). When paintAllDescendants is true, the latter
1970 // condition is replaced with being a descendant of us.
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00001971 if (logicalBottomForFloat(floatingObject) > logicalHeight()
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001972 && !floatingObject->renderer().hasSelfPaintingLayer()
1973 && (floatingObject->shouldPaint() || (paintAllDescendants && floatingObject->renderer().isDescendantOf(this)))) {
1974 floatingObject->renderer().repaint();
1975 floatingObject->renderer().repaintOverhangingFloats(false);
1976 }
1977 }
1978}
1979
1980void RenderBlockFlow::paintFloats(PaintInfo& paintInfo, const LayoutPoint& paintOffset, bool preservePhase)
1981{
1982 if (!m_floatingObjects)
1983 return;
1984
1985 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1986 auto end = floatingObjectSet.end();
1987 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
1988 FloatingObject* r = it->get();
1989 // Only paint the object if our m_shouldPaint flag is set.
1990 if (r->shouldPaint() && !r->renderer().hasSelfPaintingLayer()) {
1991 PaintInfo currentPaintInfo(paintInfo);
1992 currentPaintInfo.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
1993 // FIXME: LayoutPoint version of xPositionForFloatIncludingMargin would make this much cleaner.
1994 LayoutPoint childPoint = flipFloatForWritingModeForChild(r, LayoutPoint(paintOffset.x() + xPositionForFloatIncludingMargin(r) - r->renderer().x(), paintOffset.y() + yPositionForFloatIncludingMargin(r) - r->renderer().y()));
1995 r->renderer().paint(currentPaintInfo, childPoint);
1996 if (!preservePhase) {
1997 currentPaintInfo.phase = PaintPhaseChildBlockBackgrounds;
1998 r->renderer().paint(currentPaintInfo, childPoint);
1999 currentPaintInfo.phase = PaintPhaseFloat;
2000 r->renderer().paint(currentPaintInfo, childPoint);
2001 currentPaintInfo.phase = PaintPhaseForeground;
2002 r->renderer().paint(currentPaintInfo, childPoint);
2003 currentPaintInfo.phase = PaintPhaseOutline;
2004 r->renderer().paint(currentPaintInfo, childPoint);
2005 }
2006 }
2007 }
2008}
2009
weinig@apple.com12840dc2013-10-22 23:59:08 +00002010void RenderBlockFlow::clipOutFloatingObjects(RenderBlock& rootBlock, const PaintInfo* paintInfo, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002011{
2012 if (m_floatingObjects) {
2013 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2014 auto end = floatingObjectSet.end();
2015 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2016 FloatingObject* floatingObject = it->get();
2017 LayoutRect floatBox(offsetFromRootBlock.width() + xPositionForFloatIncludingMargin(floatingObject),
2018 offsetFromRootBlock.height() + yPositionForFloatIncludingMargin(floatingObject),
2019 floatingObject->renderer().width(), floatingObject->renderer().height());
weinig@apple.com12840dc2013-10-22 23:59:08 +00002020 rootBlock.flipForWritingMode(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002021 floatBox.move(rootBlockPhysicalPosition.x(), rootBlockPhysicalPosition.y());
2022 paintInfo->context->clipOut(pixelSnappedIntRect(floatBox));
2023 }
2024 }
2025}
2026
2027void RenderBlockFlow::createFloatingObjects()
2028{
bjonesbe@adobe.com2e94fff2013-11-08 23:24:40 +00002029 m_floatingObjects = adoptPtr(new FloatingObjects(*this));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002030}
2031
2032void RenderBlockFlow::removeFloatingObjects()
2033{
2034 if (!m_floatingObjects)
2035 return;
2036
2037 m_floatingObjects->clear();
2038}
2039
weinig@apple.com12840dc2013-10-22 23:59:08 +00002040FloatingObject* RenderBlockFlow::insertFloatingObject(RenderBox& floatBox)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002041{
weinig@apple.com12840dc2013-10-22 23:59:08 +00002042 ASSERT(floatBox.isFloating());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002043
2044 // Create the list of special objects if we don't aleady have one
2045 if (!m_floatingObjects)
2046 createFloatingObjects();
2047 else {
2048 // Don't insert the floatingObject again if it's already in the list
2049 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002050 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002051 if (it != floatingObjectSet.end())
2052 return it->get();
2053 }
2054
2055 // Create the special floatingObject entry & append it to the list
2056
weinig@apple.com12840dc2013-10-22 23:59:08 +00002057 std::unique_ptr<FloatingObject> floatingObject = FloatingObject::create(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002058
2059 // Our location is irrelevant if we're unsplittable or no pagination is in effect.
2060 // Just go ahead and lay out the float.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002061 bool isChildRenderBlock = floatBox.isRenderBlock();
2062 if (isChildRenderBlock && !floatBox.needsLayout() && view().layoutState()->pageLogicalHeightChanged())
2063 floatBox.setChildNeedsLayout(MarkOnlyThis);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002064
2065 bool needsBlockDirectionLocationSetBeforeLayout = isChildRenderBlock && view().layoutState()->needsBlockDirectionLocationSetBeforeLayout();
2066 if (!needsBlockDirectionLocationSetBeforeLayout || isWritingModeRoot()) // We are unsplittable if we're a block flow root.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002067 floatBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002068 else {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002069 floatBox.updateLogicalWidth();
2070 floatBox.computeAndSetBlockDirectionMargins(this);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002071 }
2072
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002073 setLogicalWidthForFloat(floatingObject.get(), logicalWidthForChild(floatBox) + marginStartForChild(floatBox) + marginEndForChild(floatBox));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002074
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002075 return m_floatingObjects->add(std::move(floatingObject));
2076}
2077
weinig@apple.com12840dc2013-10-22 23:59:08 +00002078void RenderBlockFlow::removeFloatingObject(RenderBox& floatBox)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002079{
2080 if (m_floatingObjects) {
2081 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002082 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002083 if (it != floatingObjectSet.end()) {
2084 FloatingObject* floatingObject = it->get();
2085 if (childrenInline()) {
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002086 LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
2087 LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002088
2089 // Fix for https://bugs.webkit.org/show_bug.cgi?id=54995.
2090 if (logicalBottom < 0 || logicalBottom < logicalTop || logicalTop == LayoutUnit::max())
2091 logicalBottom = LayoutUnit::max();
2092 else {
2093 // Special-case zero- and less-than-zero-height floats: those don't touch
2094 // the line that they're on, but it still needs to be dirtied. This is
2095 // accomplished by pretending they have a height of 1.
andersca@apple.com86298632013-11-10 19:32:33 +00002096 logicalBottom = std::max(logicalBottom, logicalTop + 1);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002097 }
2098 if (floatingObject->originatingLine()) {
2099 if (!selfNeedsLayout()) {
2100 ASSERT(&floatingObject->originatingLine()->renderer() == this);
2101 floatingObject->originatingLine()->markDirty();
2102 }
2103#if !ASSERT_DISABLED
2104 floatingObject->setOriginatingLine(0);
2105#endif
2106 }
2107 markLinesDirtyInBlockRange(0, logicalBottom);
2108 }
2109 m_floatingObjects->remove(floatingObject);
2110 }
2111 }
2112}
2113
2114void RenderBlockFlow::removeFloatingObjectsBelow(FloatingObject* lastFloat, int logicalOffset)
2115{
2116 if (!containsFloats())
2117 return;
2118
2119 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2120 FloatingObject* curr = floatingObjectSet.last().get();
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002121 while (curr != lastFloat && (!curr->isPlaced() || logicalTopForFloat(curr) >= logicalOffset)) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002122 m_floatingObjects->remove(curr);
2123 if (floatingObjectSet.isEmpty())
2124 break;
2125 curr = floatingObjectSet.last().get();
2126 }
2127}
2128
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002129LayoutUnit RenderBlockFlow::logicalLeftOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
2130{
2131 LayoutUnit offset = fixedOffset;
2132 if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
2133 offset = m_floatingObjects->logicalLeftOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
2134 return adjustLogicalLeftOffsetForLine(offset, applyTextIndent);
2135}
2136
2137LayoutUnit RenderBlockFlow::logicalRightOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
2138{
2139 LayoutUnit offset = fixedOffset;
2140 if (m_floatingObjects && m_floatingObjects->hasRightObjects())
2141 offset = m_floatingObjects->logicalRightOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
2142 return adjustLogicalRightOffsetForLine(offset, applyTextIndent);
2143}
2144
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002145LayoutPoint RenderBlockFlow::computeLogicalLocationForFloat(const FloatingObject* floatingObject, LayoutUnit logicalTopOffset) const
2146{
weinig@apple.com12840dc2013-10-22 23:59:08 +00002147 RenderBox& childBox = floatingObject->renderer();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002148 LayoutUnit logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
2149 LayoutUnit logicalRightOffset; // Constant part of right offset.
2150#if ENABLE(CSS_SHAPES)
2151 // FIXME Bug 102948: This only works for shape outside directly set on this block.
2152 ShapeInsideInfo* shapeInsideInfo = this->layoutShapeInsideInfo();
2153 // FIXME: Implement behavior for right floats.
2154 if (shapeInsideInfo) {
zoltan@webkit.orgb64932a2013-10-11 21:01:40 +00002155 LayoutSize floatLogicalSize = logicalSizeForFloat(floatingObject);
2156 // floatingObject's logicalSize doesn't contain the actual height at this point, so we need to calculate it
2157 floatLogicalSize.setHeight(logicalHeightForChild(childBox) + marginBeforeForChild(childBox) + marginAfterForChild(childBox));
2158
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002159 // FIXME: If the float doesn't fit in the shape we should push it under the content box
2160 logicalTopOffset = shapeInsideInfo->computeFirstFitPositionForFloat(floatLogicalSize);
2161 if (logicalHeight() > logicalTopOffset)
2162 logicalTopOffset = logicalHeight();
2163
zoltan@webkit.orgb64932a2013-10-11 21:01:40 +00002164 SegmentList segments = shapeInsideInfo->computeSegmentsForLine(logicalTopOffset, floatLogicalSize.height());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002165 // FIXME Bug 102949: Add support for shapes with multiple segments.
2166 if (segments.size() == 1) {
2167 // The segment offsets are relative to the content box.
2168 logicalRightOffset = logicalLeftOffset + segments[0].logicalRight;
2169 logicalLeftOffset += segments[0].logicalLeft;
2170 }
2171 } else
2172#endif
2173 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset);
2174
andersca@apple.com86298632013-11-10 19:32:33 +00002175 LayoutUnit floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset); // The width we look for.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002176
2177 LayoutUnit floatLogicalLeft;
2178
2179 bool insideFlowThread = flowThreadContainingBlock();
2180
akling@apple.com827be9c2013-10-29 02:58:43 +00002181 if (childBox.style().floating() == LeftFloat) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002182 LayoutUnit heightRemainingLeft = 1;
2183 LayoutUnit heightRemainingRight = 1;
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002184 floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
2185 while (logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight) - floatLogicalLeft < floatLogicalWidth) {
andersca@apple.com86298632013-11-10 19:32:33 +00002186 logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002187 floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002188 if (insideFlowThread) {
2189 // Have to re-evaluate all of our offsets, since they may have changed.
2190 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
2191 logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
andersca@apple.com86298632013-11-10 19:32:33 +00002192 floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002193 }
2194 }
andersca@apple.com86298632013-11-10 19:32:33 +00002195 floatLogicalLeft = std::max(logicalLeftOffset - borderAndPaddingLogicalLeft(), floatLogicalLeft);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002196 } else {
2197 LayoutUnit heightRemainingLeft = 1;
2198 LayoutUnit heightRemainingRight = 1;
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002199 floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
2200 while (floatLogicalLeft - logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft) < floatLogicalWidth) {
andersca@apple.com86298632013-11-10 19:32:33 +00002201 logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002202 floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002203 if (insideFlowThread) {
2204 // Have to re-evaluate all of our offsets, since they may have changed.
2205 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
2206 logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
andersca@apple.com86298632013-11-10 19:32:33 +00002207 floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002208 }
2209 }
2210 // Use the original width of the float here, since the local variable
2211 // |floatLogicalWidth| was capped to the available line width. See
2212 // fast/block/float/clamped-right-float.html.
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002213 floatLogicalLeft -= logicalWidthForFloat(floatingObject);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002214 }
2215
2216 return LayoutPoint(floatLogicalLeft, logicalTopOffset);
2217}
2218
2219bool RenderBlockFlow::positionNewFloats()
2220{
2221 if (!m_floatingObjects)
2222 return false;
2223
2224 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2225 if (floatingObjectSet.isEmpty())
2226 return false;
2227
2228 // If all floats have already been positioned, then we have no work to do.
2229 if (floatingObjectSet.last()->isPlaced())
2230 return false;
2231
2232 // Move backwards through our floating object list until we find a float that has
2233 // already been positioned. Then we'll be able to move forward, positioning all of
2234 // the new floats that need it.
2235 auto it = floatingObjectSet.end();
2236 --it; // Go to last item.
2237 auto begin = floatingObjectSet.begin();
2238 FloatingObject* lastPlacedFloatingObject = 0;
2239 while (it != begin) {
2240 --it;
2241 if ((*it)->isPlaced()) {
2242 lastPlacedFloatingObject = it->get();
2243 ++it;
2244 break;
2245 }
2246 }
2247
2248 LayoutUnit logicalTop = logicalHeight();
2249
2250 // The float cannot start above the top position of the last positioned float.
2251 if (lastPlacedFloatingObject)
andersca@apple.com86298632013-11-10 19:32:33 +00002252 logicalTop = std::max(logicalTopForFloat(lastPlacedFloatingObject), logicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002253
2254 auto end = floatingObjectSet.end();
2255 // Now walk through the set of unpositioned floats and place them.
2256 for (; it != end; ++it) {
2257 FloatingObject* floatingObject = it->get();
2258 // The containing block is responsible for positioning floats, so if we have floats in our
2259 // list that come from somewhere else, do not attempt to position them.
2260 if (floatingObject->renderer().containingBlock() != this)
2261 continue;
2262
weinig@apple.com12840dc2013-10-22 23:59:08 +00002263 RenderBox& childBox = floatingObject->renderer();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002264
akling@apple.com827be9c2013-10-29 02:58:43 +00002265 LayoutUnit childLogicalLeftMargin = style().isLeftToRightDirection() ? marginStartForChild(childBox) : marginEndForChild(childBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002266
weinig@apple.com12840dc2013-10-22 23:59:08 +00002267 LayoutRect oldRect = childBox.frameRect();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002268
akling@apple.com827be9c2013-10-29 02:58:43 +00002269 if (childBox.style().clear() & CLEFT)
andersca@apple.com86298632013-11-10 19:32:33 +00002270 logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatLeft), logicalTop);
akling@apple.com827be9c2013-10-29 02:58:43 +00002271 if (childBox.style().clear() & CRIGHT)
andersca@apple.com86298632013-11-10 19:32:33 +00002272 logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatRight), logicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002273
2274 LayoutPoint floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, logicalTop);
2275
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002276 setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002277
2278 setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
2279 setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
2280
2281 estimateRegionRangeForBoxChild(childBox);
2282
2283 LayoutState* layoutState = view().layoutState();
2284 bool isPaginated = layoutState->isPaginated();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002285 if (isPaginated && !childBox.needsLayout())
2286 childBox.markForPaginationRelayoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002287
weinig@apple.com12840dc2013-10-22 23:59:08 +00002288 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002289
2290 if (isPaginated) {
2291 // If we are unsplittable and don't fit, then we need to move down.
2292 // We include our margins as part of the unsplittable area.
2293 LayoutUnit newLogicalTop = adjustForUnsplittableChild(childBox, floatLogicalLocation.y(), true);
2294
2295 // See if we have a pagination strut that is making us move down further.
2296 // Note that an unsplittable child can't also have a pagination strut, so this is
2297 // exclusive with the case above.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002298 RenderBlock* childBlock = childBox.isRenderBlock() ? toRenderBlock(&childBox) : nullptr;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002299 if (childBlock && childBlock->paginationStrut()) {
2300 newLogicalTop += childBlock->paginationStrut();
2301 childBlock->setPaginationStrut(0);
2302 }
2303
2304 if (newLogicalTop != floatLogicalLocation.y()) {
2305 floatingObject->setPaginationStrut(newLogicalTop - floatLogicalLocation.y());
2306
2307 floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, newLogicalTop);
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002308 setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002309
2310 setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
2311 setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
2312
2313 if (childBlock)
2314 childBlock->setChildNeedsLayout(MarkOnlyThis);
weinig@apple.com12840dc2013-10-22 23:59:08 +00002315 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002316 }
2317
2318 if (updateRegionRangeForBoxChild(childBox)) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002319 childBox.setNeedsLayout(MarkOnlyThis);
2320 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002321 }
2322 }
2323
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002324 setLogicalTopForFloat(floatingObject, floatLogicalLocation.y());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002325
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002326 setLogicalHeightForFloat(floatingObject, logicalHeightForChild(childBox) + marginBeforeForChild(childBox) + marginAfterForChild(childBox));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002327
2328 m_floatingObjects->addPlacedObject(floatingObject);
2329
zoltan@webkit.org0faf5722013-11-05 02:34:16 +00002330#if ENABLE(CSS_SHAPES)
2331 if (ShapeOutsideInfo* shapeOutside = childBox.shapeOutsideInfo())
2332 shapeOutside->setShapeSize(logicalWidthForChild(childBox), logicalHeightForChild(childBox));
2333#endif
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002334 // If the child moved, we have to repaint it.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002335 if (childBox.checkForRepaintDuringLayout())
2336 childBox.repaintDuringLayoutIfMoved(oldRect);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002337 }
2338 return true;
2339}
2340
2341void RenderBlockFlow::newLine(EClear clear)
2342{
2343 positionNewFloats();
2344 // set y position
2345 LayoutUnit newY = 0;
2346 switch (clear) {
2347 case CLEFT:
2348 newY = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
2349 break;
2350 case CRIGHT:
2351 newY = lowestFloatLogicalBottom(FloatingObject::FloatRight);
2352 break;
2353 case CBOTH:
2354 newY = lowestFloatLogicalBottom();
2355 default:
2356 break;
2357 }
2358 if (height() < newY)
2359 setLogicalHeight(newY);
2360}
2361
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002362LayoutUnit RenderBlockFlow::logicalLeftFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002363{
2364 if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002365 return m_floatingObjects->logicalLeftOffset(fixedOffset, logicalTop, logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002366
2367 return fixedOffset;
2368}
2369
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002370LayoutUnit RenderBlockFlow::logicalRightFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002371{
2372 if (m_floatingObjects && m_floatingObjects->hasRightObjects())
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002373 return m_floatingObjects->logicalRightOffset(fixedOffset, logicalTop, logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002374
2375 return fixedOffset;
2376}
2377
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002378LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelow(LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002379{
2380 if (!m_floatingObjects)
2381 return logicalHeight;
2382
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002383 return m_floatingObjects->findNextFloatLogicalBottomBelow(logicalHeight);
2384}
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002385
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002386LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelowForBlock(LayoutUnit logicalHeight) const
2387{
2388 if (!m_floatingObjects)
2389 return logicalHeight;
2390
2391 return m_floatingObjects->findNextFloatLogicalBottomBelowForBlock(logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002392}
2393
2394LayoutUnit RenderBlockFlow::lowestFloatLogicalBottom(FloatingObject::Type floatType) const
2395{
2396 if (!m_floatingObjects)
2397 return 0;
2398 LayoutUnit lowestFloatBottom = 0;
2399 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2400 auto end = floatingObjectSet.end();
2401 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2402 FloatingObject* floatingObject = it->get();
2403 if (floatingObject->isPlaced() && floatingObject->type() & floatType)
andersca@apple.com86298632013-11-10 19:32:33 +00002404 lowestFloatBottom = std::max(lowestFloatBottom, logicalBottomForFloat(floatingObject));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002405 }
2406 return lowestFloatBottom;
2407}
2408
weinig@apple.com12840dc2013-10-22 23:59:08 +00002409LayoutUnit RenderBlockFlow::addOverhangingFloats(RenderBlockFlow& child, bool makeChildPaintOtherFloats)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002410{
2411 // Prevent floats from being added to the canvas by the root element, e.g., <html>.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002412 if (child.hasOverflowClip() || !child.containsFloats() || child.isRoot() || child.hasColumns() || child.isWritingModeRoot())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002413 return 0;
2414
weinig@apple.com12840dc2013-10-22 23:59:08 +00002415 LayoutUnit childLogicalTop = child.logicalTop();
2416 LayoutUnit childLogicalLeft = child.logicalLeft();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002417 LayoutUnit lowestFloatLogicalBottom = 0;
2418
2419 // Floats that will remain the child's responsibility to paint should factor into its
2420 // overflow.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002421 auto childEnd = child.m_floatingObjects->set().end();
2422 for (auto childIt = child.m_floatingObjects->set().begin(); childIt != childEnd; ++childIt) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002423 FloatingObject* floatingObject = childIt->get();
andersca@apple.com86298632013-11-10 19:32:33 +00002424 LayoutUnit floatLogicalBottom = std::min(logicalBottomForFloat(floatingObject), LayoutUnit::max() - childLogicalTop);
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002425 LayoutUnit logicalBottom = childLogicalTop + floatLogicalBottom;
andersca@apple.com86298632013-11-10 19:32:33 +00002426 lowestFloatLogicalBottom = std::max(lowestFloatLogicalBottom, logicalBottom);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002427
2428 if (logicalBottom > logicalHeight()) {
2429 // If the object is not in the list, we add it now.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002430 if (!containsFloat(floatingObject->renderer())) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002431 LayoutSize offset = isHorizontalWritingMode() ? LayoutSize(-childLogicalLeft, -childLogicalTop) : LayoutSize(-childLogicalTop, -childLogicalLeft);
2432 bool shouldPaint = false;
2433
2434 // The nearest enclosing layer always paints the float (so that zindex and stacking
2435 // behaves properly). We always want to propagate the desire to paint the float as
2436 // far out as we can, to the outermost block that overlaps the float, stopping only
2437 // if we hit a self-painting layer boundary.
2438 if (floatingObject->renderer().enclosingFloatPaintingLayer() == enclosingFloatPaintingLayer()) {
2439 floatingObject->setShouldPaint(false);
2440 shouldPaint = true;
2441 }
2442 // We create the floating object list lazily.
2443 if (!m_floatingObjects)
2444 createFloatingObjects();
2445
2446 m_floatingObjects->add(floatingObject->copyToNewContainer(offset, shouldPaint, true));
2447 }
2448 } else {
2449 if (makeChildPaintOtherFloats && !floatingObject->shouldPaint() && !floatingObject->renderer().hasSelfPaintingLayer()
weinig@apple.com12840dc2013-10-22 23:59:08 +00002450 && floatingObject->renderer().isDescendantOf(&child) && floatingObject->renderer().enclosingFloatPaintingLayer() == child.enclosingFloatPaintingLayer()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002451 // The float is not overhanging from this block, so if it is a descendant of the child, the child should
2452 // paint it (the other case is that it is intruding into the child), unless it has its own layer or enclosing
2453 // layer.
2454 // If makeChildPaintOtherFloats is false, it means that the child must already know about all the floats
2455 // it should paint.
2456 floatingObject->setShouldPaint(true);
2457 }
2458
2459 // 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
2460 // child now.
2461 if (floatingObject->isDescendant())
weinig@apple.com12840dc2013-10-22 23:59:08 +00002462 child.addOverflowFromChild(&floatingObject->renderer(), LayoutSize(xPositionForFloatIncludingMargin(floatingObject), yPositionForFloatIncludingMargin(floatingObject)));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002463 }
2464 }
2465 return lowestFloatLogicalBottom;
2466}
2467
weinig@apple.com12840dc2013-10-22 23:59:08 +00002468bool RenderBlockFlow::hasOverhangingFloat(RenderBox& renderer)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002469{
2470 if (!m_floatingObjects || hasColumns() || !parent())
2471 return false;
2472
2473 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002474 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(renderer);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002475 if (it == floatingObjectSet.end())
2476 return false;
2477
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002478 return logicalBottomForFloat(it->get()) > logicalHeight();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002479}
2480
2481void RenderBlockFlow::addIntrudingFloats(RenderBlockFlow* prev, LayoutUnit logicalLeftOffset, LayoutUnit logicalTopOffset)
2482{
2483 ASSERT(!avoidsFloats());
2484
2485 // If the parent or previous sibling doesn't have any floats to add, don't bother.
2486 if (!prev->m_floatingObjects)
2487 return;
2488
2489 logicalLeftOffset += marginLogicalLeft();
2490
2491 const FloatingObjectSet& prevSet = prev->m_floatingObjects->set();
2492 auto prevEnd = prevSet.end();
2493 for (auto prevIt = prevSet.begin(); prevIt != prevEnd; ++prevIt) {
2494 FloatingObject* floatingObject = prevIt->get();
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002495 if (logicalBottomForFloat(floatingObject) > logicalTopOffset) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002496 if (!m_floatingObjects || !m_floatingObjects->set().contains<FloatingObject&, FloatingObjectHashTranslator>(*floatingObject)) {
2497 // We create the floating object list lazily.
2498 if (!m_floatingObjects)
2499 createFloatingObjects();
2500
2501 // Applying the child's margin makes no sense in the case where the child was passed in.
2502 // since this margin was added already through the modification of the |logicalLeftOffset| variable
2503 // above. |logicalLeftOffset| will equal the margin in this case, so it's already been taken
2504 // into account. Only apply this code if prev is the parent, since otherwise the left margin
2505 // will get applied twice.
2506 LayoutSize offset = isHorizontalWritingMode()
2507 ? LayoutSize(logicalLeftOffset - (prev != parent() ? prev->marginLeft() : LayoutUnit()), logicalTopOffset)
2508 : LayoutSize(logicalTopOffset, logicalLeftOffset - (prev != parent() ? prev->marginTop() : LayoutUnit()));
2509
2510 m_floatingObjects->add(floatingObject->copyToNewContainer(offset));
2511 }
2512 }
2513 }
2514}
2515
2516void RenderBlockFlow::markAllDescendantsWithFloatsForLayout(RenderBox* floatToRemove, bool inLayout)
2517{
2518 if (!everHadLayout() && !containsFloats())
2519 return;
2520
2521 MarkingBehavior markParents = inLayout ? MarkOnlyThis : MarkContainingBlockChain;
2522 setChildNeedsLayout(markParents);
2523
2524 if (floatToRemove)
weinig@apple.com12840dc2013-10-22 23:59:08 +00002525 removeFloatingObject(*floatToRemove);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002526
akling@apple.com525dae62014-01-03 20:22:09 +00002527 if (childrenInline())
2528 return;
2529
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002530 // Iterate over our children and mark them as needed.
akling@apple.com525dae62014-01-03 20:22:09 +00002531 for (auto& block : childrenOfType<RenderBlock>(*this)) {
2532 if (!floatToRemove && block.isFloatingOrOutOfFlowPositioned())
2533 continue;
2534 if (!block.isRenderBlockFlow()) {
2535 if (block.shrinkToAvoidFloats() && block.everHadLayout())
2536 block.setChildNeedsLayout(markParents);
2537 continue;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002538 }
akling@apple.com525dae62014-01-03 20:22:09 +00002539 auto& blockFlow = toRenderBlockFlow(block);
2540 if ((floatToRemove ? blockFlow.containsFloat(*floatToRemove) : blockFlow.containsFloats()) || blockFlow.shrinkToAvoidFloats())
2541 blockFlow.markAllDescendantsWithFloatsForLayout(floatToRemove, inLayout);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002542 }
2543}
2544
2545void RenderBlockFlow::markSiblingsWithFloatsForLayout(RenderBox* floatToRemove)
2546{
2547 if (!m_floatingObjects)
2548 return;
2549
2550 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2551 auto end = floatingObjectSet.end();
2552
2553 for (RenderObject* next = nextSibling(); next; next = next->nextSibling()) {
2554 if (!next->isRenderBlockFlow() || next->isFloatingOrOutOfFlowPositioned() || toRenderBlock(next)->avoidsFloats())
2555 continue;
2556
2557 RenderBlockFlow* nextBlock = toRenderBlockFlow(next);
2558 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002559 RenderBox& floatingBox = (*it)->renderer();
2560 if (floatToRemove && &floatingBox != floatToRemove)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002561 continue;
2562 if (nextBlock->containsFloat(floatingBox))
weinig@apple.com12840dc2013-10-22 23:59:08 +00002563 nextBlock->markAllDescendantsWithFloatsForLayout(&floatingBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002564 }
2565 }
2566}
2567
weinig@apple.com31324fd2013-10-28 19:22:51 +00002568LayoutPoint RenderBlockFlow::flipFloatForWritingModeForChild(const FloatingObject* child, const LayoutPoint& point) const
2569{
akling@apple.com827be9c2013-10-29 02:58:43 +00002570 if (!style().isFlippedBlocksWritingMode())
weinig@apple.com31324fd2013-10-28 19:22:51 +00002571 return point;
2572
2573 // This is similar to RenderBox::flipForWritingModeForChild. We have to subtract out our left/top offsets twice, since
2574 // it's going to get added back in. We hide this complication here so that the calling code looks normal for the unflipped
2575 // case.
2576 if (isHorizontalWritingMode())
2577 return LayoutPoint(point.x(), point.y() + height() - child->renderer().height() - 2 * yPositionForFloatIncludingMargin(child));
2578 return LayoutPoint(point.x() + width() - child->renderer().width() - 2 * xPositionForFloatIncludingMargin(child), point.y());
2579}
2580
weinig@apple.com12840dc2013-10-22 23:59:08 +00002581LayoutUnit RenderBlockFlow::getClearDelta(RenderBox& child, LayoutUnit logicalTop)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002582{
2583 // There is no need to compute clearance if we have no floats.
2584 if (!containsFloats())
2585 return 0;
2586
2587 // At least one float is present. We need to perform the clearance computation.
akling@apple.com827be9c2013-10-29 02:58:43 +00002588 bool clearSet = child.style().clear() != CNONE;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002589 LayoutUnit logicalBottom = 0;
akling@apple.com827be9c2013-10-29 02:58:43 +00002590 switch (child.style().clear()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002591 case CNONE:
2592 break;
2593 case CLEFT:
2594 logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
2595 break;
2596 case CRIGHT:
2597 logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatRight);
2598 break;
2599 case CBOTH:
2600 logicalBottom = lowestFloatLogicalBottom();
2601 break;
2602 }
2603
2604 // 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 +00002605 LayoutUnit result = clearSet ? std::max<LayoutUnit>(0, logicalBottom - logicalTop) : LayoutUnit();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002606 if (!result && child.avoidsFloats()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002607 LayoutUnit newLogicalTop = logicalTop;
2608 while (true) {
2609 LayoutUnit availableLogicalWidthAtNewLogicalTopOffset = availableLogicalWidthForLine(newLogicalTop, false, logicalHeightForChild(child));
2610 if (availableLogicalWidthAtNewLogicalTopOffset == availableLogicalWidthForContent(newLogicalTop))
2611 return newLogicalTop - logicalTop;
2612
2613 RenderRegion* region = regionAtBlockOffset(logicalTopForChild(child));
weinig@apple.com12840dc2013-10-22 23:59:08 +00002614 LayoutRect borderBox = child.borderBoxRectInRegion(region, DoNotCacheRenderBoxRegionInfo);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002615 LayoutUnit childLogicalWidthAtOldLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
2616
2617 // FIXME: None of this is right for perpendicular writing-mode children.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002618 LayoutUnit childOldLogicalWidth = child.logicalWidth();
2619 LayoutUnit childOldMarginLeft = child.marginLeft();
2620 LayoutUnit childOldMarginRight = child.marginRight();
2621 LayoutUnit childOldLogicalTop = child.logicalTop();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002622
weinig@apple.com12840dc2013-10-22 23:59:08 +00002623 child.setLogicalTop(newLogicalTop);
2624 child.updateLogicalWidth();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002625 region = regionAtBlockOffset(logicalTopForChild(child));
weinig@apple.com12840dc2013-10-22 23:59:08 +00002626 borderBox = child.borderBoxRectInRegion(region, DoNotCacheRenderBoxRegionInfo);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002627 LayoutUnit childLogicalWidthAtNewLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
2628
weinig@apple.com12840dc2013-10-22 23:59:08 +00002629 child.setLogicalTop(childOldLogicalTop);
2630 child.setLogicalWidth(childOldLogicalWidth);
2631 child.setMarginLeft(childOldMarginLeft);
2632 child.setMarginRight(childOldMarginRight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002633
2634 if (childLogicalWidthAtNewLogicalTopOffset <= availableLogicalWidthAtNewLogicalTopOffset) {
2635 // Even though we may not be moving, if the logical width did shrink because of the presence of new floats, then
2636 // we need to force a relayout as though we shifted. This happens because of the dynamic addition of overhanging floats
2637 // from previous siblings when negative margins exist on a child (see the addOverhangingFloats call at the end of collapseMargins).
2638 if (childLogicalWidthAtOldLogicalTopOffset != childLogicalWidthAtNewLogicalTopOffset)
weinig@apple.com12840dc2013-10-22 23:59:08 +00002639 child.setChildNeedsLayout(MarkOnlyThis);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002640 return newLogicalTop - logicalTop;
2641 }
2642
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002643 newLogicalTop = nextFloatLogicalBottomBelowForBlock(newLogicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002644 ASSERT(newLogicalTop >= logicalTop);
2645 if (newLogicalTop < logicalTop)
2646 break;
2647 }
2648 ASSERT_NOT_REACHED();
2649 }
2650 return result;
2651}
2652
2653bool RenderBlockFlow::hitTestFloats(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset)
2654{
2655 if (!m_floatingObjects)
2656 return false;
2657
2658 LayoutPoint adjustedLocation = accumulatedOffset;
2659 if (isRenderView())
2660 adjustedLocation += toLayoutSize(toRenderView(*this).frameView().scrollPosition());
2661
2662 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2663 auto begin = floatingObjectSet.begin();
2664 for (auto it = floatingObjectSet.end(); it != begin;) {
2665 --it;
2666 FloatingObject* floatingObject = it->get();
2667 if (floatingObject->shouldPaint() && !floatingObject->renderer().hasSelfPaintingLayer()) {
2668 LayoutUnit xOffset = xPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer().x();
2669 LayoutUnit yOffset = yPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer().y();
2670 LayoutPoint childPoint = flipFloatForWritingModeForChild(floatingObject, adjustedLocation + LayoutSize(xOffset, yOffset));
2671 if (floatingObject->renderer().hitTest(request, result, locationInContainer, childPoint)) {
2672 updateHitTestResult(result, locationInContainer.point() - toLayoutSize(childPoint));
2673 return true;
2674 }
2675 }
2676 }
2677
2678 return false;
2679}
2680
weinig@apple.com611b9292013-10-20 22:57:54 +00002681bool RenderBlockFlow::hitTestInlineChildren(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
2682{
2683 ASSERT(childrenInline());
antti@apple.com940f5872013-10-24 20:31:11 +00002684
antti@apple.comfea51992013-10-28 13:39:23 +00002685 if (m_simpleLineLayout)
2686 return SimpleLineLayout::hitTestFlow(*this, *m_simpleLineLayout, request, result, locationInContainer, accumulatedOffset, hitTestAction);
antti@apple.com940f5872013-10-24 20:31:11 +00002687
weinig@apple.com611b9292013-10-20 22:57:54 +00002688 return m_lineBoxes.hitTest(this, request, result, locationInContainer, accumulatedOffset, hitTestAction);
2689}
2690
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002691void RenderBlockFlow::adjustForBorderFit(LayoutUnit x, LayoutUnit& left, LayoutUnit& right) const
2692{
akling@apple.com827be9c2013-10-29 02:58:43 +00002693 if (style().visibility() != VISIBLE)
weinig@apple.com611b9292013-10-20 22:57:54 +00002694 return;
2695
2696 // We don't deal with relative positioning. Our assumption is that you shrink to fit the lines without accounting
2697 // for either overflow or translations via relative positioning.
2698 if (childrenInline()) {
antti@apple.com940f5872013-10-24 20:31:11 +00002699 const_cast<RenderBlockFlow&>(*this).ensureLineBoxes();
2700
weinig@apple.com611b9292013-10-20 22:57:54 +00002701 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
2702 if (box->firstChild())
andersca@apple.com86298632013-11-10 19:32:33 +00002703 left = std::min(left, x + static_cast<LayoutUnit>(box->firstChild()->x()));
weinig@apple.com611b9292013-10-20 22:57:54 +00002704 if (box->lastChild())
andersca@apple.com86298632013-11-10 19:32:33 +00002705 right = std::max(right, x + static_cast<LayoutUnit>(ceilf(box->lastChild()->logicalRight())));
weinig@apple.com611b9292013-10-20 22:57:54 +00002706 }
2707 } else {
2708 for (RenderBox* obj = firstChildBox(); obj; obj = obj->nextSiblingBox()) {
2709 if (!obj->isFloatingOrOutOfFlowPositioned()) {
2710 if (obj->isRenderBlockFlow() && !obj->hasOverflowClip())
2711 toRenderBlockFlow(obj)->adjustForBorderFit(x + obj->x(), left, right);
akling@apple.com827be9c2013-10-29 02:58:43 +00002712 else if (obj->style().visibility() == VISIBLE) {
weinig@apple.com611b9292013-10-20 22:57:54 +00002713 // We are a replaced element or some kind of non-block-flow object.
andersca@apple.com86298632013-11-10 19:32:33 +00002714 left = std::min(left, x + obj->x());
2715 right = std::max(right, x + obj->x() + obj->width());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002716 }
2717 }
2718 }
2719 }
weinig@apple.com611b9292013-10-20 22:57:54 +00002720
2721 if (m_floatingObjects) {
2722 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2723 auto end = floatingObjectSet.end();
2724 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2725 FloatingObject* r = it->get();
2726 // Only examine the object if our m_shouldPaint flag is set.
2727 if (r->shouldPaint()) {
2728 LayoutUnit floatLeft = xPositionForFloatIncludingMargin(r) - r->renderer().x();
2729 LayoutUnit floatRight = floatLeft + r->renderer().width();
andersca@apple.com86298632013-11-10 19:32:33 +00002730 left = std::min(left, floatLeft);
2731 right = std::max(right, floatRight);
weinig@apple.com611b9292013-10-20 22:57:54 +00002732 }
2733 }
2734 }
2735}
2736
2737void RenderBlockFlow::fitBorderToLinesIfNeeded()
2738{
akling@apple.com827be9c2013-10-29 02:58:43 +00002739 if (style().borderFit() == BorderFitBorder || hasOverrideWidth())
weinig@apple.com611b9292013-10-20 22:57:54 +00002740 return;
2741
2742 // Walk any normal flow lines to snugly fit.
2743 LayoutUnit left = LayoutUnit::max();
2744 LayoutUnit right = LayoutUnit::min();
2745 LayoutUnit oldWidth = contentWidth();
2746 adjustForBorderFit(0, left, right);
2747
2748 // Clamp to our existing edges. We can never grow. We only shrink.
2749 LayoutUnit leftEdge = borderLeft() + paddingLeft();
2750 LayoutUnit rightEdge = leftEdge + oldWidth;
andersca@apple.com86298632013-11-10 19:32:33 +00002751 left = std::min(rightEdge, std::max(leftEdge, left));
2752 right = std::max(leftEdge, std::min(rightEdge, right));
weinig@apple.com611b9292013-10-20 22:57:54 +00002753
2754 LayoutUnit newContentWidth = right - left;
2755 if (newContentWidth == oldWidth)
2756 return;
2757
2758 setOverrideLogicalContentWidth(newContentWidth);
2759 layoutBlock(false);
2760 clearOverrideLogicalContentWidth();
2761}
2762
2763void RenderBlockFlow::markLinesDirtyInBlockRange(LayoutUnit logicalTop, LayoutUnit logicalBottom, RootInlineBox* highest)
2764{
2765 if (logicalTop >= logicalBottom)
2766 return;
2767
2768 RootInlineBox* lowestDirtyLine = lastRootBox();
2769 RootInlineBox* afterLowest = lowestDirtyLine;
2770 while (lowestDirtyLine && lowestDirtyLine->lineBottomWithLeading() >= logicalBottom && logicalBottom < LayoutUnit::max()) {
2771 afterLowest = lowestDirtyLine;
2772 lowestDirtyLine = lowestDirtyLine->prevRootBox();
2773 }
2774
2775 while (afterLowest && afterLowest != highest && (afterLowest->lineBottomWithLeading() >= logicalTop || afterLowest->lineBottomWithLeading() < 0)) {
2776 afterLowest->markDirty();
2777 afterLowest = afterLowest->prevRootBox();
2778 }
2779}
2780
antti@apple.com0e632aa2013-10-22 21:03:38 +00002781int RenderBlockFlow::firstLineBaseline() const
weinig@apple.com611b9292013-10-20 22:57:54 +00002782{
2783 if (isWritingModeRoot() && !isRubyRun())
2784 return -1;
2785
2786 if (!childrenInline())
antti@apple.com0e632aa2013-10-22 21:03:38 +00002787 return RenderBlock::firstLineBaseline();
weinig@apple.com611b9292013-10-20 22:57:54 +00002788
antti@apple.com940f5872013-10-24 20:31:11 +00002789 if (!hasLines())
2790 return -1;
weinig@apple.com611b9292013-10-20 22:57:54 +00002791
antti@apple.comfea51992013-10-28 13:39:23 +00002792 if (m_simpleLineLayout)
2793 return SimpleLineLayout::computeFlowFirstLineBaseline(*this, *m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00002794
akling@apple.comee3c8df2013-11-06 08:09:44 +00002795 ASSERT(firstRootBox());
2796 return firstRootBox()->logicalTop() + firstLineStyle().fontMetrics().ascent(firstRootBox()->baselineType());
weinig@apple.com611b9292013-10-20 22:57:54 +00002797}
2798
2799int RenderBlockFlow::inlineBlockBaseline(LineDirectionMode lineDirection) const
2800{
2801 if (isWritingModeRoot() && !isRubyRun())
2802 return -1;
2803
2804 if (!childrenInline())
2805 return RenderBlock::inlineBlockBaseline(lineDirection);
2806
antti@apple.com0e632aa2013-10-22 21:03:38 +00002807 if (!hasLines()) {
2808 if (!hasLineIfEmpty())
2809 return -1;
akling@apple.com827be9c2013-10-29 02:58:43 +00002810 const FontMetrics& fontMetrics = firstLineStyle().fontMetrics();
weinig@apple.com611b9292013-10-20 22:57:54 +00002811 return fontMetrics.ascent()
2812 + (lineHeight(true, lineDirection, PositionOfInteriorLineBoxes) - fontMetrics.height()) / 2
2813 + (lineDirection == HorizontalLine ? borderTop() + paddingTop() : borderRight() + paddingRight());
2814 }
2815
antti@apple.comfea51992013-10-28 13:39:23 +00002816 if (m_simpleLineLayout)
2817 return SimpleLineLayout::computeFlowLastLineBaseline(*this, *m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00002818
akling@apple.comee3c8df2013-11-06 08:09:44 +00002819 bool isFirstLine = lastRootBox() == firstRootBox();
akling@apple.com827be9c2013-10-29 02:58:43 +00002820 const RenderStyle& style = isFirstLine ? firstLineStyle() : this->style();
akling@apple.comee3c8df2013-11-06 08:09:44 +00002821 return lastRootBox()->logicalTop() + style.fontMetrics().ascent(lastRootBox()->baselineType());
weinig@apple.com611b9292013-10-20 22:57:54 +00002822}
2823
weinig@apple.com12840dc2013-10-22 23:59:08 +00002824GapRects RenderBlockFlow::inlineSelectionGaps(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
weinig@apple.com611b9292013-10-20 22:57:54 +00002825 LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
2826{
antti@apple.comfea51992013-10-28 13:39:23 +00002827 ASSERT(!m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00002828
weinig@apple.com611b9292013-10-20 22:57:54 +00002829 GapRects result;
2830
2831 bool containsStart = selectionState() == SelectionStart || selectionState() == SelectionBoth;
2832
antti@apple.com0e632aa2013-10-22 21:03:38 +00002833 if (!hasLines()) {
weinig@apple.com611b9292013-10-20 22:57:54 +00002834 if (containsStart) {
2835 // Go ahead and update our lastLogicalTop to be the bottom of the block. <hr>s or empty blocks with height can trip this
2836 // case.
2837 lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalHeight();
2838 lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, logicalHeight(), cache);
2839 lastLogicalRight = logicalRightSelectionOffset(rootBlock, logicalHeight(), cache);
2840 }
2841 return result;
2842 }
2843
2844 RootInlineBox* lastSelectedLine = 0;
2845 RootInlineBox* curr;
2846 for (curr = firstRootBox(); curr && !curr->hasSelectedChildren(); curr = curr->nextRootBox()) { }
2847
2848 // Now paint the gaps for the lines.
2849 for (; curr && curr->hasSelectedChildren(); curr = curr->nextRootBox()) {
2850 LayoutUnit selTop = curr->selectionTopAdjustedForPrecedingBlock();
2851 LayoutUnit selHeight = curr->selectionHeightAdjustedForPrecedingBlock();
2852
2853 if (!containsStart && !lastSelectedLine &&
2854 selectionState() != SelectionStart && selectionState() != SelectionBoth)
2855 result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop, lastLogicalLeft, lastLogicalRight, selTop, cache, paintInfo));
2856
2857 LayoutRect logicalRect(curr->logicalLeft(), selTop, curr->logicalWidth(), selTop + selHeight);
2858 logicalRect.move(isHorizontalWritingMode() ? offsetFromRootBlock : offsetFromRootBlock.transposedSize());
weinig@apple.com12840dc2013-10-22 23:59:08 +00002859 LayoutRect physicalRect = rootBlock.logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
weinig@apple.com611b9292013-10-20 22:57:54 +00002860 if (!paintInfo || (isHorizontalWritingMode() && physicalRect.y() < paintInfo->rect.maxY() && physicalRect.maxY() > paintInfo->rect.y())
2861 || (!isHorizontalWritingMode() && physicalRect.x() < paintInfo->rect.maxX() && physicalRect.maxX() > paintInfo->rect.x()))
2862 result.unite(curr->lineSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, selTop, selHeight, cache, paintInfo));
2863
2864 lastSelectedLine = curr;
2865 }
2866
2867 if (containsStart && !lastSelectedLine)
2868 // VisibleSelection must start just after our last line.
2869 lastSelectedLine = lastRootBox();
2870
2871 if (lastSelectedLine && selectionState() != SelectionEnd && selectionState() != SelectionBoth) {
2872 // Go ahead and update our lastY to be the bottom of the last selected line.
2873 lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + lastSelectedLine->selectionBottom();
2874 lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
2875 lastLogicalRight = logicalRightSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
2876 }
2877 return result;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002878}
2879
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002880void RenderBlockFlow::createRenderNamedFlowFragmentIfNeeded()
2881{
abucur@adobe.com0e81bc72013-10-22 14:50:37 +00002882 if (!document().cssRegionsEnabled() || renderNamedFlowFragment() || isRenderNamedFlowFragment())
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002883 return;
2884
akling@apple.com827be9c2013-10-29 02:58:43 +00002885 if (style().isDisplayRegionType() && style().hasFlowFrom()) {
2886 RenderNamedFlowFragment* flowFragment = new RenderNamedFlowFragment(document(), RenderNamedFlowFragment::createStyle(style()));
akling@apple.com8f40c5b2013-10-27 22:54:07 +00002887 flowFragment->initializeStyle();
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002888 setRenderNamedFlowFragment(flowFragment);
2889 addChild(renderNamedFlowFragment());
2890 }
2891}
2892
2893bool RenderBlockFlow::canHaveChildren() const
2894{
2895 return !renderNamedFlowFragment() ? RenderBlock::canHaveChildren() : renderNamedFlowFragment()->canHaveChildren();
2896}
2897
2898bool RenderBlockFlow::canHaveGeneratedChildren() const
2899{
2900 return !renderNamedFlowFragment() ? RenderBlock::canHaveGeneratedChildren() : renderNamedFlowFragment()->canHaveGeneratedChildren();
2901}
2902
2903bool RenderBlockFlow::namedFlowFragmentNeedsUpdate() const
2904{
2905 if (!isRenderNamedFlowFragmentContainer())
2906 return false;
2907
2908 return hasRelativeLogicalHeight() && !isRenderView();
2909}
2910
2911void RenderBlockFlow::updateLogicalHeight()
2912{
2913 RenderBlock::updateLogicalHeight();
2914
2915 if (renderNamedFlowFragment())
andersca@apple.com86298632013-11-10 19:32:33 +00002916 renderNamedFlowFragment()->setLogicalHeight(std::max<LayoutUnit>(0, logicalHeight() - borderAndPaddingLogicalHeight()));
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002917}
2918
2919void RenderBlockFlow::setRenderNamedFlowFragment(RenderNamedFlowFragment* flowFragment)
2920{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00002921 RenderBlockFlowRareData& rareData = ensureRareBlockFlowData();
abucur@adobe.com0e81bc72013-10-22 14:50:37 +00002922 if (rareData.m_renderNamedFlowFragment)
2923 rareData.m_renderNamedFlowFragment->destroy();
2924 rareData.m_renderNamedFlowFragment = flowFragment;
2925}
2926
hyatt@apple.come9fe3d32014-01-24 17:14:22 +00002927void RenderBlockFlow::setMultiColumnFlowThread(RenderMultiColumnFlowThread* flowThread)
2928{
2929 RenderBlockFlowRareData& rareData = ensureRareBlockFlowData();
2930 rareData.m_multiColumnFlowThread = flowThread;
2931}
2932
akling@apple.com525dae62014-01-03 20:22:09 +00002933static bool shouldCheckLines(const RenderBlockFlow& blockFlow)
weinig@apple.com17140912013-10-19 19:55:40 +00002934{
akling@apple.com525dae62014-01-03 20:22:09 +00002935 return !blockFlow.isFloatingOrOutOfFlowPositioned() && !blockFlow.isRunIn() && blockFlow.style().height().isAuto();
weinig@apple.com17140912013-10-19 19:55:40 +00002936}
2937
2938RootInlineBox* RenderBlockFlow::lineAtIndex(int i) const
2939{
2940 ASSERT(i >= 0);
2941
akling@apple.com827be9c2013-10-29 02:58:43 +00002942 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00002943 return nullptr;
2944
2945 if (childrenInline()) {
2946 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
2947 if (!i--)
2948 return box;
2949 }
akling@apple.com525dae62014-01-03 20:22:09 +00002950 return nullptr;
2951 }
2952
2953 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
2954 if (!shouldCheckLines(blockFlow))
2955 continue;
2956 if (RootInlineBox* box = blockFlow.lineAtIndex(i))
2957 return box;
weinig@apple.com17140912013-10-19 19:55:40 +00002958 }
2959
2960 return nullptr;
2961}
2962
2963int RenderBlockFlow::lineCount(const RootInlineBox* stopRootInlineBox, bool* found) const
2964{
akling@apple.com827be9c2013-10-29 02:58:43 +00002965 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00002966 return 0;
2967
2968 int count = 0;
2969
2970 if (childrenInline()) {
2971 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
2972 count++;
2973 if (box == stopRootInlineBox) {
2974 if (found)
2975 *found = true;
2976 break;
2977 }
2978 }
akling@apple.com525dae62014-01-03 20:22:09 +00002979 return count;
2980 }
2981
2982 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
2983 if (!shouldCheckLines(blockFlow))
2984 continue;
2985 bool recursiveFound = false;
2986 count += blockFlow.lineCount(stopRootInlineBox, &recursiveFound);
2987 if (recursiveFound) {
2988 if (found)
2989 *found = true;
2990 break;
weinig@apple.com17140912013-10-19 19:55:40 +00002991 }
2992 }
2993
2994 return count;
2995}
2996
2997static int getHeightForLineCount(const RenderBlockFlow& block, int lineCount, bool includeBottom, int& count)
2998{
akling@apple.com827be9c2013-10-29 02:58:43 +00002999 if (block.style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00003000 return -1;
3001
3002 if (block.childrenInline()) {
3003 for (auto box = block.firstRootBox(); box; box = box->nextRootBox()) {
3004 if (++count == lineCount)
3005 return box->lineBottom() + (includeBottom ? (block.borderBottom() + block.paddingBottom()) : LayoutUnit());
3006 }
3007 } else {
3008 RenderBox* normalFlowChildWithoutLines = 0;
3009 for (auto obj = block.firstChildBox(); obj; obj = obj->nextSiblingBox()) {
akling@apple.com525dae62014-01-03 20:22:09 +00003010 if (obj->isRenderBlockFlow() && shouldCheckLines(toRenderBlockFlow(*obj))) {
weinig@apple.com17140912013-10-19 19:55:40 +00003011 int result = getHeightForLineCount(toRenderBlockFlow(*obj), lineCount, false, count);
3012 if (result != -1)
3013 return result + obj->y() + (includeBottom ? (block.borderBottom() + block.paddingBottom()) : LayoutUnit());
3014 } else if (!obj->isFloatingOrOutOfFlowPositioned() && !obj->isRunIn())
3015 normalFlowChildWithoutLines = obj;
3016 }
3017 if (normalFlowChildWithoutLines && !lineCount)
3018 return normalFlowChildWithoutLines->y() + normalFlowChildWithoutLines->height();
3019 }
3020
3021 return -1;
3022}
3023
3024int RenderBlockFlow::heightForLineCount(int lineCount)
3025{
3026 int count = 0;
3027 return getHeightForLineCount(*this, lineCount, true, count);
3028}
3029
3030void RenderBlockFlow::clearTruncation()
3031{
akling@apple.com827be9c2013-10-29 02:58:43 +00003032 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00003033 return;
3034
3035 if (childrenInline() && hasMarkupTruncation()) {
antti@apple.com940f5872013-10-24 20:31:11 +00003036 ensureLineBoxes();
3037
weinig@apple.com17140912013-10-19 19:55:40 +00003038 setHasMarkupTruncation(false);
3039 for (auto box = firstRootBox(); box; box = box->nextRootBox())
3040 box->clearTruncation();
akling@apple.com525dae62014-01-03 20:22:09 +00003041 return;
3042 }
3043
3044 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
3045 if (shouldCheckLines(blockFlow))
3046 blockFlow.clearTruncation();
weinig@apple.com17140912013-10-19 19:55:40 +00003047 }
3048}
3049
weinig@apple.com3f23b382013-10-19 20:26:58 +00003050bool RenderBlockFlow::containsNonZeroBidiLevel() const
3051{
3052 for (auto root = firstRootBox(); root; root = root->nextRootBox()) {
3053 for (auto box = root->firstLeafChild(); box; box = box->nextLeafChild()) {
3054 if (box->bidiLevel())
3055 return true;
3056 }
3057 }
3058 return false;
3059}
3060
weinig@apple.com611b9292013-10-20 22:57:54 +00003061Position RenderBlockFlow::positionForBox(InlineBox *box, bool start) const
3062{
3063 if (!box)
3064 return Position();
3065
3066 if (!box->renderer().nonPseudoNode())
3067 return createLegacyEditingPosition(nonPseudoElement(), start ? caretMinOffset() : caretMaxOffset());
3068
3069 if (!box->isInlineTextBox())
3070 return createLegacyEditingPosition(box->renderer().nonPseudoNode(), start ? box->renderer().caretMinOffset() : box->renderer().caretMaxOffset());
3071
3072 InlineTextBox* textBox = toInlineTextBox(box);
3073 return createLegacyEditingPosition(box->renderer().nonPseudoNode(), start ? textBox->start() : textBox->start() + textBox->len());
3074}
3075
3076VisiblePosition RenderBlockFlow::positionForPointWithInlineChildren(const LayoutPoint& pointInLogicalContents)
3077{
3078 ASSERT(childrenInline());
3079
antti@apple.com940f5872013-10-24 20:31:11 +00003080 ensureLineBoxes();
3081
weinig@apple.com611b9292013-10-20 22:57:54 +00003082 if (!firstRootBox())
3083 return createVisiblePosition(0, DOWNSTREAM);
3084
akling@apple.com827be9c2013-10-29 02:58:43 +00003085 bool linesAreFlipped = style().isFlippedLinesWritingMode();
3086 bool blocksAreFlipped = style().isFlippedBlocksWritingMode();
weinig@apple.com611b9292013-10-20 22:57:54 +00003087
3088 // look for the closest line box in the root box which is at the passed-in y coordinate
3089 InlineBox* closestBox = 0;
3090 RootInlineBox* firstRootBoxWithChildren = 0;
3091 RootInlineBox* lastRootBoxWithChildren = 0;
3092 for (RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox()) {
3093 if (!root->firstLeafChild())
3094 continue;
3095 if (!firstRootBoxWithChildren)
3096 firstRootBoxWithChildren = root;
3097
3098 if (!linesAreFlipped && root->isFirstAfterPageBreak() && (pointInLogicalContents.y() < root->lineTopWithLeading()
3099 || (blocksAreFlipped && pointInLogicalContents.y() == root->lineTopWithLeading())))
3100 break;
3101
3102 lastRootBoxWithChildren = root;
3103
3104 // check if this root line box is located at this y coordinate
3105 if (pointInLogicalContents.y() < root->selectionBottom() || (blocksAreFlipped && pointInLogicalContents.y() == root->selectionBottom())) {
3106 if (linesAreFlipped) {
3107 RootInlineBox* nextRootBoxWithChildren = root->nextRootBox();
3108 while (nextRootBoxWithChildren && !nextRootBoxWithChildren->firstLeafChild())
3109 nextRootBoxWithChildren = nextRootBoxWithChildren->nextRootBox();
3110
3111 if (nextRootBoxWithChildren && nextRootBoxWithChildren->isFirstAfterPageBreak() && (pointInLogicalContents.y() > nextRootBoxWithChildren->lineTopWithLeading()
3112 || (!blocksAreFlipped && pointInLogicalContents.y() == nextRootBoxWithChildren->lineTopWithLeading())))
3113 continue;
3114 }
3115 closestBox = root->closestLeafChildForLogicalLeftPosition(pointInLogicalContents.x());
3116 if (closestBox)
3117 break;
3118 }
3119 }
3120
3121 bool moveCaretToBoundary = frame().editor().behavior().shouldMoveCaretToHorizontalBoundaryWhenPastTopOrBottom();
3122
3123 if (!moveCaretToBoundary && !closestBox && lastRootBoxWithChildren) {
3124 // y coordinate is below last root line box, pretend we hit it
3125 closestBox = lastRootBoxWithChildren->closestLeafChildForLogicalLeftPosition(pointInLogicalContents.x());
3126 }
3127
3128 if (closestBox) {
3129 if (moveCaretToBoundary) {
andersca@apple.com86298632013-11-10 19:32:33 +00003130 LayoutUnit firstRootBoxWithChildrenTop = std::min<LayoutUnit>(firstRootBoxWithChildren->selectionTop(), firstRootBoxWithChildren->logicalTop());
weinig@apple.com611b9292013-10-20 22:57:54 +00003131 if (pointInLogicalContents.y() < firstRootBoxWithChildrenTop
3132 || (blocksAreFlipped && pointInLogicalContents.y() == firstRootBoxWithChildrenTop)) {
3133 InlineBox* box = firstRootBoxWithChildren->firstLeafChild();
3134 if (box->isLineBreak()) {
3135 if (InlineBox* newBox = box->nextLeafChildIgnoringLineBreak())
3136 box = newBox;
3137 }
3138 // y coordinate is above first root line box, so return the start of the first
3139 return VisiblePosition(positionForBox(box, true), DOWNSTREAM);
3140 }
3141 }
3142
3143 // pass the box a top position that is inside it
3144 LayoutPoint point(pointInLogicalContents.x(), closestBox->root().blockDirectionPointInLine());
3145 if (!isHorizontalWritingMode())
3146 point = point.transposedPoint();
3147 if (closestBox->renderer().isReplaced())
weinig@apple.com12840dc2013-10-22 23:59:08 +00003148 return positionForPointRespectingEditingBoundaries(*this, toRenderBox(closestBox->renderer()), point);
weinig@apple.com611b9292013-10-20 22:57:54 +00003149 return closestBox->renderer().positionForPoint(point);
3150 }
3151
3152 if (lastRootBoxWithChildren) {
3153 // We hit this case for Mac behavior when the Y coordinate is below the last box.
3154 ASSERT(moveCaretToBoundary);
3155 InlineBox* logicallyLastBox;
3156 if (lastRootBoxWithChildren->getLogicalEndBoxWithNode(logicallyLastBox))
3157 return VisiblePosition(positionForBox(logicallyLastBox, false), DOWNSTREAM);
3158 }
3159
3160 // Can't reach this. We have a root line box, but it has no kids.
3161 // FIXME: This should ASSERT_NOT_REACHED(), but clicking on placeholder text
3162 // seems to hit this code path.
3163 return createVisiblePosition(0, DOWNSTREAM);
3164}
3165
commit-queue@webkit.org5ce6c902013-11-11 18:21:05 +00003166VisiblePosition RenderBlockFlow::positionForPoint(const LayoutPoint& point)
3167{
3168 if (auto fragment = renderNamedFlowFragment())
3169 return fragment->positionForPoint(point);
3170 return RenderBlock::positionForPoint(point);
3171}
3172
3173
weinig@apple.com611b9292013-10-20 22:57:54 +00003174void RenderBlockFlow::addFocusRingRectsForInlineChildren(Vector<IntRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject*)
3175{
antti@apple.com940f5872013-10-24 20:31:11 +00003176 ASSERT(childrenInline());
3177
3178 ensureLineBoxes();
3179
weinig@apple.com611b9292013-10-20 22:57:54 +00003180 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
andersca@apple.com86298632013-11-10 19:32:33 +00003181 LayoutUnit top = std::max<LayoutUnit>(curr->lineTop(), curr->top());
3182 LayoutUnit bottom = std::min<LayoutUnit>(curr->lineBottom(), curr->top() + curr->height());
weinig@apple.com611b9292013-10-20 22:57:54 +00003183 LayoutRect rect(additionalOffset.x() + curr->x(), additionalOffset.y() + top, curr->width(), bottom - top);
3184 if (!rect.isEmpty())
3185 rects.append(pixelSnappedIntRect(rect));
3186 }
3187}
3188
3189void RenderBlockFlow::paintInlineChildren(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
3190{
3191 ASSERT(childrenInline());
antti@apple.com940f5872013-10-24 20:31:11 +00003192
antti@apple.comfea51992013-10-28 13:39:23 +00003193 if (m_simpleLineLayout) {
3194 SimpleLineLayout::paintFlow(*this, *m_simpleLineLayout, paintInfo, paintOffset);
antti@apple.com940f5872013-10-24 20:31:11 +00003195 return;
3196 }
weinig@apple.com611b9292013-10-20 22:57:54 +00003197 m_lineBoxes.paint(this, paintInfo, paintOffset);
3198}
3199
3200bool RenderBlockFlow::relayoutForPagination(bool hasSpecifiedPageLogicalHeight, LayoutUnit pageLogicalHeight, LayoutStateMaintainer& statePusher)
3201{
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003202 if (!hasColumns() && !multiColumnFlowThread())
weinig@apple.com611b9292013-10-20 22:57:54 +00003203 return false;
3204
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003205 if (hasColumns()) {
3206 RefPtr<RenderOverflow> savedOverflow = m_overflow.release();
3207 if (childrenInline())
3208 addOverflowFromInlineChildren();
3209 else
3210 addOverflowFromBlockChildren();
3211 LayoutUnit layoutOverflowLogicalBottom = (isHorizontalWritingMode() ? layoutOverflowRect().maxY() : layoutOverflowRect().maxX()) - borderAndPaddingBefore();
weinig@apple.com611b9292013-10-20 22:57:54 +00003212
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003213 // FIXME: We don't balance properly at all in the presence of forced page breaks. We need to understand what
3214 // the distance between forced page breaks is so that we can avoid making the minimum column height too tall.
3215 ColumnInfo* colInfo = columnInfo();
3216 if (!hasSpecifiedPageLogicalHeight) {
3217 LayoutUnit columnHeight = pageLogicalHeight;
3218 int minColumnCount = colInfo->forcedBreaks() + 1;
3219 int desiredColumnCount = colInfo->desiredColumnCount();
3220 if (minColumnCount >= desiredColumnCount) {
3221 // The forced page breaks are in control of the balancing. Just set the column height to the
3222 // maximum page break distance.
3223 if (!pageLogicalHeight) {
3224 LayoutUnit distanceBetweenBreaks = std::max<LayoutUnit>(colInfo->maximumDistanceBetweenForcedBreaks(),
3225 view().layoutState()->pageLogicalOffset(this, borderAndPaddingBefore() + layoutOverflowLogicalBottom) - colInfo->forcedBreakOffset());
3226 columnHeight = std::max(colInfo->minimumColumnHeight(), distanceBetweenBreaks);
3227 }
3228 } else if (layoutOverflowLogicalBottom > boundedMultiply(pageLogicalHeight, desiredColumnCount)) {
3229 // Now that we know the intrinsic height of the columns, we have to rebalance them.
3230 columnHeight = std::max<LayoutUnit>(colInfo->minimumColumnHeight(), ceilf((float)layoutOverflowLogicalBottom / desiredColumnCount));
weinig@apple.com611b9292013-10-20 22:57:54 +00003231 }
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003232
3233 if (columnHeight && columnHeight != pageLogicalHeight) {
3234 statePusher.pop();
3235 setEverHadLayout(true);
3236 layoutBlock(false, columnHeight);
3237 return true;
3238 }
3239 }
weinig@apple.com611b9292013-10-20 22:57:54 +00003240
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003241 if (pageLogicalHeight)
3242 colInfo->setColumnCountAndHeight(ceilf((float)layoutOverflowLogicalBottom / pageLogicalHeight), pageLogicalHeight);
weinig@apple.com611b9292013-10-20 22:57:54 +00003243
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003244 if (columnCount(colInfo)) {
3245 setLogicalHeight(borderAndPaddingBefore() + colInfo->columnHeight() + borderAndPaddingAfter() + scrollbarLogicalHeight());
3246 clearOverflow();
3247 } else
3248 m_overflow = savedOverflow.release();
3249 return false;
3250 }
weinig@apple.com611b9292013-10-20 22:57:54 +00003251
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003252 if (!multiColumnFlowThread()->shouldRelayoutForPagination())
3253 return false;
3254
3255 multiColumnFlowThread()->setNeedsRebalancing(false);
3256 multiColumnFlowThread()->setInBalancingPass(true); // Prevent re-entering this method (and recursion into layout).
3257
3258 bool needsRelayout;
3259 bool neededRelayout = false;
3260 bool firstPass = true;
3261 do {
3262 // Column heights may change here because of balancing. We may have to do multiple layout
3263 // passes, depending on how the contents is fitted to the changed column heights. In most
3264 // cases, laying out again twice or even just once will suffice. Sometimes we need more
3265 // passes than that, though, but the number of retries should not exceed the number of
3266 // columns, unless we have a bug.
3267 needsRelayout = false;
3268 for (RenderBox* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox())
3269 if (childBox != multiColumnFlowThread() && childBox->isRenderMultiColumnSet()) {
3270 RenderMultiColumnSet* multicolSet = toRenderMultiColumnSet(childBox);
3271 if (multicolSet->recalculateBalancedHeight(firstPass)) {
3272 multicolSet->setChildNeedsLayout(MarkOnlyThis);
3273 needsRelayout = true;
3274 }
3275 }
3276
3277 if (needsRelayout) {
3278 // Layout again. Column balancing resulted in a new height.
3279 neededRelayout = true;
3280 multiColumnFlowThread()->setChildNeedsLayout(MarkOnlyThis);
3281 setChildNeedsLayout(MarkOnlyThis);
3282 if (firstPass)
3283 statePusher.pop();
3284 layoutBlock(false);
3285 }
3286 firstPass = false;
3287 } while (needsRelayout);
3288
3289 multiColumnFlowThread()->setInBalancingPass(false);
3290
3291 return neededRelayout;
weinig@apple.com611b9292013-10-20 22:57:54 +00003292}
3293
antti@apple.com940f5872013-10-24 20:31:11 +00003294bool RenderBlockFlow::hasLines() const
3295{
3296 ASSERT(childrenInline());
3297
antti@apple.comfea51992013-10-28 13:39:23 +00003298 if (m_simpleLineLayout)
antti@apple.com09c83512013-10-30 14:12:32 +00003299 return m_simpleLineLayout->lineCount();
antti@apple.com940f5872013-10-24 20:31:11 +00003300
3301 return lineBoxes().firstLineBox();
3302}
3303
3304void RenderBlockFlow::layoutSimpleLines(LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
3305{
3306 ASSERT(!m_lineBoxes.firstLineBox());
3307
antti@apple.comfea51992013-10-28 13:39:23 +00003308 m_simpleLineLayout = SimpleLineLayout::create(*this);
antti@apple.com940f5872013-10-24 20:31:11 +00003309
antti@apple.comfea51992013-10-28 13:39:23 +00003310 LayoutUnit lineLayoutHeight = SimpleLineLayout::computeFlowHeight(*this, *m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00003311 LayoutUnit lineLayoutTop = borderAndPaddingBefore();
3312
3313 repaintLogicalTop = lineLayoutTop;
3314 repaintLogicalBottom = lineLayoutTop + lineLayoutHeight;
3315
3316 setLogicalHeight(lineLayoutTop + lineLayoutHeight + borderAndPaddingAfter());
3317}
3318
3319void RenderBlockFlow::deleteLineBoxesBeforeSimpleLineLayout()
3320{
antti@apple.com42fb53d2013-10-25 02:33:11 +00003321 ASSERT(m_lineLayoutPath == SimpleLinesPath);
akling@apple.com31dd4f42013-10-30 22:27:59 +00003322 lineBoxes().deleteLineBoxes();
antti@apple.com940f5872013-10-24 20:31:11 +00003323 toRenderText(firstChild())->deleteLineBoxesBeforeSimpleLineLayout();
3324}
3325
3326void RenderBlockFlow::ensureLineBoxes()
3327{
antti@apple.com42fb53d2013-10-25 02:33:11 +00003328 m_lineLayoutPath = ForceLineBoxesPath;
antti@apple.com940f5872013-10-24 20:31:11 +00003329
antti@apple.comfea51992013-10-28 13:39:23 +00003330 if (!m_simpleLineLayout)
antti@apple.com940f5872013-10-24 20:31:11 +00003331 return;
antti@apple.comfea51992013-10-28 13:39:23 +00003332 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +00003333
3334#if !ASSERT_DISABLED
3335 LayoutUnit oldHeight = logicalHeight();
3336#endif
3337 bool didNeedLayout = needsLayout();
3338
3339 bool relayoutChildren = false;
3340 LayoutUnit repaintLogicalTop;
3341 LayoutUnit repaintLogicalBottom;
3342 layoutLineBoxes(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
3343
3344 updateLogicalHeight();
3345 ASSERT(didNeedLayout || logicalHeight() == oldHeight);
3346
3347 if (!didNeedLayout)
3348 clearNeedsLayout();
3349}
3350
weinig@apple.com611b9292013-10-20 22:57:54 +00003351#ifndef NDEBUG
3352void RenderBlockFlow::showLineTreeAndMark(const InlineBox* markedBox1, const char* markedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const RenderObject* obj) const
3353{
3354 RenderBlock::showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLabel2, obj);
3355 for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox())
3356 root->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLabel2, obj, 1);
3357}
3358#endif
3359
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00003360RenderBlockFlow::RenderBlockFlowRareData& RenderBlockFlow::ensureRareBlockFlowData()
3361{
3362 if (hasRareBlockFlowData())
3363 return *m_rareBlockFlowData;
3364 materializeRareBlockFlowData();
3365 return *m_rareBlockFlowData;
3366}
3367
3368void RenderBlockFlow::materializeRareBlockFlowData()
3369{
3370 ASSERT(!hasRareBlockFlowData());
3371 m_rareBlockFlowData = std::make_unique<RenderBlockFlow::RenderBlockFlowRareData>(*this);
3372}
3373
aestes@apple.com6751d842014-01-12 02:51:25 +00003374#if ENABLE(IOS_TEXT_AUTOSIZING)
3375inline static bool isVisibleRenderText(RenderObject* renderer)
3376{
3377 if (!renderer->isText())
3378 return false;
3379 RenderText* renderText = toRenderText(renderer);
3380 return !renderText->linesBoundingBox().isEmpty() && !renderText->text()->containsOnlyWhitespace();
3381}
3382
3383inline static bool resizeTextPermitted(RenderObject* render)
3384{
3385 // We disallow resizing for text input fields and textarea to address <rdar://problem/5792987> and <rdar://problem/8021123>
3386 auto renderer = render->parent();
3387 while (renderer) {
3388 // Get the first non-shadow HTMLElement and see if it's an input.
3389 if (renderer->element() && renderer->element()->isHTMLElement() && !renderer->element()->isInShadowTree()) {
3390 const HTMLElement& element = toHTMLElement(*renderer->element());
3391 return !isHTMLInputElement(element) && !isHTMLTextAreaElement(element);
3392 }
3393 renderer = renderer->parent();
3394 }
3395 return true;
3396}
3397
3398int RenderBlockFlow::immediateLineCount()
3399{
3400 // Copied and modified from RenderBlock::lineCount.
3401 // Only descend into list items.
3402 int count = 0;
3403 if (style().visibility() == VISIBLE) {
3404 if (childrenInline()) {
3405 for (RootInlineBox* box = firstRootBox(); box; box = box->nextRootBox())
3406 count++;
3407 } else {
3408 for (RenderObject* obj = firstChild(); obj; obj = obj->nextSibling()) {
3409 if (obj->isListItem())
3410 count += toRenderBlockFlow(obj)->lineCount();
3411 }
3412 }
3413 }
3414 return count;
3415}
3416
3417static bool isNonBlocksOrNonFixedHeightListItems(const RenderObject* render)
3418{
3419 if (!render->isRenderBlock())
3420 return true;
3421 if (render->isListItem())
3422 return render->style().height().type() != Fixed;
3423 return false;
3424}
3425
3426// For now, we auto size single lines of text the same as multiple lines.
3427// We've been experimenting with low values for single lines of text.
3428static inline float oneLineTextMultiplier(float specifiedSize)
3429{
3430 return std::max((1.0f / log10f(specifiedSize) * 1.7f), 1.0f);
3431}
3432
3433static inline float textMultiplier(float specifiedSize)
3434{
3435 return std::max((1.0f / log10f(specifiedSize) * 1.95f), 1.0f);
3436}
3437
3438void RenderBlockFlow::adjustComputedFontSizes(float size, float visibleWidth)
3439{
3440 // Don't do any work if the block is smaller than the visible area.
3441 if (visibleWidth >= width())
3442 return;
3443
3444 unsigned lineCount;
3445 if (m_lineCountForTextAutosizing == NOT_SET) {
3446 int count = immediateLineCount();
3447 if (!count)
3448 lineCount = NO_LINE;
3449 else if (count == 1)
3450 lineCount = ONE_LINE;
3451 else
3452 lineCount = MULTI_LINE;
3453 } else
3454 lineCount = m_lineCountForTextAutosizing;
3455
3456 ASSERT(lineCount != NOT_SET);
3457 if (lineCount == NO_LINE)
3458 return;
3459
3460 float actualWidth = m_widthForTextAutosizing != -1 ? static_cast<float>(m_widthForTextAutosizing) : static_cast<float>(width());
3461 float scale = visibleWidth / actualWidth;
3462 float minFontSize = roundf(size / scale);
3463
3464 for (RenderObject* descendent = traverseNext(this, isNonBlocksOrNonFixedHeightListItems); descendent; descendent = descendent->traverseNext(this, isNonBlocksOrNonFixedHeightListItems)) {
3465 if (isVisibleRenderText(descendent) && resizeTextPermitted(descendent)) {
3466 RenderText* text = toRenderText(descendent);
3467 RenderStyle& oldStyle = text->style();
3468 FontDescription fontDescription = oldStyle.fontDescription();
3469 float specifiedSize = fontDescription.specifiedSize();
3470 float scaledSize = roundf(specifiedSize * scale);
3471 if (scaledSize > 0 && scaledSize < minFontSize) {
3472 // 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.
3473 // This makes text resizing consistent even if the block's width or line count changes (which can be caused by text resizing itself 5159915).
3474 if (m_lineCountForTextAutosizing == NOT_SET)
3475 m_lineCountForTextAutosizing = lineCount;
3476 if (m_widthForTextAutosizing == -1)
3477 m_widthForTextAutosizing = actualWidth;
3478
3479 float candidateNewSize = 0;
3480 float lineTextMultiplier = lineCount == ONE_LINE ? oneLineTextMultiplier(specifiedSize) : textMultiplier(specifiedSize);
3481 candidateNewSize = roundf(std::min(minFontSize, specifiedSize * lineTextMultiplier));
3482 if (candidateNewSize > specifiedSize && candidateNewSize != fontDescription.computedSize() && text->textNode() && oldStyle.textSizeAdjust().isAuto())
3483 document().addAutoSizingNode(text->textNode(), candidateNewSize);
3484 }
3485 }
3486 }
3487}
3488#endif // ENABLE(IOS_TEXT_AUTOSIZING)
3489
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003490RenderObject* RenderBlockFlow::layoutSpecialExcludedChild(bool relayoutChildren)
3491{
3492 if (!multiColumnFlowThread())
3493 return 0;
3494
3495 // Update the dimensions of our regions before we lay out the flow thread.
3496 // FIXME: Eventually this is going to get way more complicated, and we will be destroying regions
3497 // instead of trying to keep them around.
3498 bool shouldInvalidateRegions = false;
3499 for (RenderBox* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox()) {
3500 if (childBox == multiColumnFlowThread())
3501 continue;
3502
3503 if (relayoutChildren || childBox->needsLayout()) {
3504 if (!multiColumnFlowThread()->inBalancingPass() && childBox->isRenderMultiColumnSet())
3505 toRenderMultiColumnSet(childBox)->prepareForLayout();
3506 shouldInvalidateRegions = true;
3507 }
3508 }
3509
3510 if (shouldInvalidateRegions)
3511 multiColumnFlowThread()->invalidateRegions();
3512
3513 if (relayoutChildren)
3514 multiColumnFlowThread()->setChildNeedsLayout(MarkOnlyThis);
3515
3516 if (multiColumnFlowThread()->requiresBalancing()) {
3517 // At the end of multicol layout, relayoutForPagination() is called unconditionally, but if
3518 // no children are to be laid out (e.g. fixed width with layout already being up-to-date),
3519 // we want to prevent it from doing any work, so that the column balancing machinery doesn't
3520 // kick in and trigger additional unnecessary layout passes. Actually, it's not just a good
3521 // idea in general to not waste time on balancing content that hasn't been re-laid out; we
3522 // are actually required to guarantee this. The calculation of implicit breaks needs to be
3523 // preceded by a proper layout pass, since it's layout that sets up content runs, and the
3524 // runs get deleted right after every pass.
3525 multiColumnFlowThread()->setNeedsRebalancing(shouldInvalidateRegions || multiColumnFlowThread()->needsLayout());
3526 }
3527
3528 setLogicalTopForChild(*multiColumnFlowThread(), borderAndPaddingBefore());
3529 multiColumnFlowThread()->layoutIfNeeded();
3530 determineLogicalLeftPositionForChild(*multiColumnFlowThread());
3531
3532 return multiColumnFlowThread();
3533}
3534
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003535void RenderBlockFlow::addChild(RenderObject* newChild, RenderObject* beforeChild)
3536{
3537 if (multiColumnFlowThread())
3538 return multiColumnFlowThread()->addChild(newChild, beforeChild);
3539 RenderBlock::addChild(newChild, beforeChild);
3540}
3541
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003542void RenderBlockFlow::checkForPaginationLogicalHeightChange(LayoutUnit& pageLogicalHeight, bool& pageLogicalHeightChanged, bool& hasSpecifiedPageLogicalHeight)
3543{
3544 // If we don't use either of the two column implementations or a flow thread, then bail.
3545 if (!isRenderFlowThread() && !multiColumnFlowThread() && !hasColumns())
3546 return;
3547
3548 // We don't actually update any of the variables. We just subclassed to adjust our column height.
3549 if (multiColumnFlowThread()) {
3550 updateLogicalHeight();
3551 multiColumnFlowThread()->setColumnHeightAvailable(std::max<LayoutUnit>(contentLogicalHeight(), 0));
3552 setLogicalHeight(0);
3553 } else if (hasColumns()) {
3554 ColumnInfo* colInfo = columnInfo();
3555
3556 if (!pageLogicalHeight) {
3557 // We need to go ahead and set our explicit page height if one exists, so that we can
3558 // avoid doing two layout passes.
3559 updateLogicalHeight();
3560 LayoutUnit columnHeight = isRenderView() ? view().pageOrViewLogicalHeight() : contentLogicalHeight();
3561 if (columnHeight > 0) {
3562 pageLogicalHeight = columnHeight;
3563 hasSpecifiedPageLogicalHeight = true;
3564 }
3565 setLogicalHeight(0);
3566 }
3567
3568 if (colInfo->columnHeight() != pageLogicalHeight && everHadLayout())
3569 pageLogicalHeightChanged = true;
3570
3571 colInfo->setColumnHeight(pageLogicalHeight);
3572
3573 if (!hasSpecifiedPageLogicalHeight && !pageLogicalHeight)
3574 colInfo->clearForcedBreaks();
3575
3576 colInfo->setPaginationUnit(paginationUnit());
3577 } else if (isRenderFlowThread()) {
3578 pageLogicalHeight = 1; // This is just a hack to always make sure we have a page logical height.
3579 pageLogicalHeightChanged = toRenderFlowThread(this)->pageLogicalSizeChanged();
3580 }
3581}
3582
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003583void RenderBlockFlow::setComputedColumnCountAndWidth(int count, LayoutUnit width)
3584{
3585 if (!document().regionBasedColumnsEnabled())
3586 return RenderBlock::setComputedColumnCountAndWidth(count, width);
3587
3588 bool destroyColumns = !requiresColumns(count);
3589 if (destroyColumns) {
3590 if (multiColumnFlowThread())
3591 destroyMultiColumnFlowThread();
3592 } else {
3593 if (!multiColumnFlowThread())
3594 createMultiColumnFlowThread();
3595 multiColumnFlowThread()->setColumnCountAndWidth(count, width);
3596 }
3597}
3598
3599LayoutUnit RenderBlockFlow::computedColumnWidth() const
3600{
3601 if (!document().regionBasedColumnsEnabled())
3602 return RenderBlock::computedColumnWidth();
3603
3604 if (multiColumnFlowThread())
3605 return multiColumnFlowThread()->computedColumnWidth();
3606 return contentLogicalWidth();
3607}
3608
3609unsigned RenderBlockFlow::computedColumnCount() const
3610{
3611 if (!document().regionBasedColumnsEnabled())
3612 return RenderBlock::computedColumnCount();
3613
3614 if (multiColumnFlowThread())
3615 return multiColumnFlowThread()->computedColumnCount();
3616
3617 return 1;
3618}
3619
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00003620}
3621// namespace WebCore