blob: 36503f0fb63ccff336724f66033767cd74bc7682 [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
dino@apple.comc133a8c2014-02-03 23:35:34 +000045#if ENABLE(CSS_SHAPES) && ENABLE(CSS_SHAPE_INSIDE)
zoltan@webkit.org7b4eb622013-12-06 19:31:50 +000046#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
weinig@apple.com611b9292013-10-20 22:57:54 +0000163 // Make sure to destroy anonymous children first while they are still connected to the rest of the tree, so that they will
164 // properly dirty line boxes that they are removed from. Effects that do :before/:after only on hover could crash otherwise.
165 destroyLeftoverChildren();
166
167 // Destroy our continuation before anything other than anonymous children.
168 // The reason we don't destroy it before anonymous children is that they may
169 // have continuations of their own that are anonymous children of our continuation.
170 RenderBoxModelObject* continuation = this->continuation();
171 if (continuation) {
172 continuation->destroy();
173 setContinuation(0);
174 }
175
176 if (!documentBeingDestroyed()) {
akling@apple.comee3c8df2013-11-06 08:09:44 +0000177 if (firstRootBox()) {
weinig@apple.com611b9292013-10-20 22:57:54 +0000178 // We can't wait for RenderBox::destroy to clear the selection,
179 // because by then we will have nuked the line boxes.
180 // FIXME: The FrameSelection should be responsible for this when it
181 // is notified of DOM mutations.
182 if (isSelectionBorder())
183 view().clearSelection();
184
185 // If we are an anonymous block, then our line boxes might have children
186 // that will outlast this block. In the non-anonymous block case those
187 // children will be destroyed by the time we return from this function.
188 if (isAnonymousBlock()) {
akling@apple.comee3c8df2013-11-06 08:09:44 +0000189 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
weinig@apple.com611b9292013-10-20 22:57:54 +0000190 while (auto childBox = box->firstChild())
191 childBox->removeFromParent();
192 }
193 }
194 } else if (parent())
195 parent()->dirtyLinesFromChangedChild(this);
196 }
197
akling@apple.com31dd4f42013-10-30 22:27:59 +0000198 m_lineBoxes.deleteLineBoxes();
weinig@apple.com611b9292013-10-20 22:57:54 +0000199
200 removeFromDelayedUpdateScrollInfoSet();
201
202 // NOTE: This jumps down to RenderBox, bypassing RenderBlock since it would do duplicate work.
203 RenderBox::willBeDestroyed();
hyatt@apple.com3cd5c772013-09-27 18:22:50 +0000204}
205
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000206void RenderBlockFlow::clearFloats()
207{
208 if (m_floatingObjects)
209 m_floatingObjects->setHorizontalWritingMode(isHorizontalWritingMode());
210
211 HashSet<RenderBox*> oldIntrudingFloatSet;
212 if (!childrenInline() && m_floatingObjects) {
213 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +0000214 auto end = floatingObjectSet.end();
215 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
216 FloatingObject* floatingObject = it->get();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000217 if (!floatingObject->isDescendant())
darin@apple.com7cad7042013-09-24 05:53:55 +0000218 oldIntrudingFloatSet.add(&floatingObject->renderer());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000219 }
220 }
221
222 // Inline blocks are covered by the isReplaced() check in the avoidFloats method.
223 if (avoidsFloats() || isRoot() || isRenderView() || isFloatingOrOutOfFlowPositioned() || isTableCell()) {
mihnea@adobe.combe79cf12013-10-17 09:02:19 +0000224 if (m_floatingObjects)
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000225 m_floatingObjects->clear();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000226 if (!oldIntrudingFloatSet.isEmpty())
227 markAllDescendantsWithFloatsForLayout();
228 return;
229 }
230
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000231 RendererToFloatInfoMap floatMap;
232
233 if (m_floatingObjects) {
bjonesbe@adobe.com0434768a2013-09-16 22:01:38 +0000234 if (childrenInline())
235 m_floatingObjects->moveAllToFloatInfoMap(floatMap);
236 else
237 m_floatingObjects->clear();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000238 }
239
240 // We should not process floats if the parent node is not a RenderBlock. Otherwise, we will add
241 // floats in an invalid context. This will cause a crash arising from a bad cast on the parent.
242 // See <rdar://problem/8049753>, where float property is applied on a text node in a SVG.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000243 if (!parent() || !parent()->isRenderBlockFlow())
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000244 return;
245
246 // Attempt to locate a previous sibling with overhanging floats. We skip any elements that are
247 // out of flow (like floating/positioned elements), and we also skip over any objects that may have shifted
248 // to avoid floats.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000249 RenderBlockFlow* parentBlock = toRenderBlockFlow(parent());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000250 bool parentHasFloats = false;
251 RenderObject* prev = previousSibling();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +0000252 while (prev && (prev->isFloatingOrOutOfFlowPositioned() || !prev->isBox() || !prev->isRenderBlockFlow() || toRenderBlockFlow(prev)->avoidsFloats())) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000253 if (prev->isFloating())
254 parentHasFloats = true;
255 prev = prev->previousSibling();
256 }
257
robert@webkit.org97037ef2013-11-20 19:26:10 +0000258 // First add in floats from the parent. Self-collapsing blocks let their parent track any floats that intrude into
259 // them (as opposed to floats they contain themselves) so check for those here too.
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000260 LayoutUnit logicalTopOffset = logicalTop();
robert@webkit.org97037ef2013-11-20 19:26:10 +0000261 if (parentHasFloats || (parentBlock->lowestFloatLogicalBottom() > logicalTopOffset && prev && toRenderBlockFlow(prev)->isSelfCollapsingBlock()))
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000262 addIntrudingFloats(parentBlock, parentBlock->logicalLeftOffsetForContent(), logicalTopOffset);
263
264 LayoutUnit logicalLeftOffset = 0;
265 if (prev)
266 logicalTopOffset -= toRenderBox(prev)->logicalTop();
267 else {
268 prev = parentBlock;
269 logicalLeftOffset += parentBlock->logicalLeftOffsetForContent();
270 }
271
272 // 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 +0000273 RenderBlockFlow* block = toRenderBlockFlow(prev);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000274 if (block->m_floatingObjects && block->lowestFloatLogicalBottom() > logicalTopOffset)
275 addIntrudingFloats(block, logicalLeftOffset, logicalTopOffset);
276
277 if (childrenInline()) {
278 LayoutUnit changeLogicalTop = LayoutUnit::max();
279 LayoutUnit changeLogicalBottom = LayoutUnit::min();
280 if (m_floatingObjects) {
281 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +0000282 auto end = floatingObjectSet.end();
283 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +0000284 FloatingObject* floatingObject = it->get();
285 std::unique_ptr<FloatingObject> oldFloatingObject = floatMap.take(&floatingObject->renderer());
286 LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000287 if (oldFloatingObject) {
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +0000288 LayoutUnit oldLogicalBottom = logicalBottomForFloat(oldFloatingObject.get());
289 if (logicalWidthForFloat(floatingObject) != logicalWidthForFloat(oldFloatingObject.get()) || logicalLeftForFloat(floatingObject) != logicalLeftForFloat(oldFloatingObject.get())) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000290 changeLogicalTop = 0;
andersca@apple.com86298632013-11-10 19:32:33 +0000291 changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalBottom, oldLogicalBottom));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000292 } else {
293 if (logicalBottom != oldLogicalBottom) {
andersca@apple.com86298632013-11-10 19:32:33 +0000294 changeLogicalTop = std::min(changeLogicalTop, std::min(logicalBottom, oldLogicalBottom));
295 changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalBottom, oldLogicalBottom));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000296 }
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +0000297 LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
298 LayoutUnit oldLogicalTop = logicalTopForFloat(oldFloatingObject.get());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000299 if (logicalTop != oldLogicalTop) {
andersca@apple.com86298632013-11-10 19:32:33 +0000300 changeLogicalTop = std::min(changeLogicalTop, std::min(logicalTop, oldLogicalTop));
301 changeLogicalBottom = std::max(changeLogicalBottom, std::max(logicalTop, oldLogicalTop));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000302 }
303 }
304
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000305 if (oldFloatingObject->originatingLine() && !selfNeedsLayout()) {
306 ASSERT(&oldFloatingObject->originatingLine()->renderer() == this);
307 oldFloatingObject->originatingLine()->markDirty();
308 }
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000309 } else {
310 changeLogicalTop = 0;
andersca@apple.com86298632013-11-10 19:32:33 +0000311 changeLogicalBottom = std::max(changeLogicalBottom, logicalBottom);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000312 }
313 }
314 }
315
darin@apple.com7cad7042013-09-24 05:53:55 +0000316 auto end = floatMap.end();
317 for (auto it = floatMap.begin(); it != end; ++it) {
318 FloatingObject* floatingObject = it->value.get();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000319 if (!floatingObject->isDescendant()) {
320 changeLogicalTop = 0;
andersca@apple.com86298632013-11-10 19:32:33 +0000321 changeLogicalBottom = std::max(changeLogicalBottom, logicalBottomForFloat(floatingObject));
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000322 }
323 }
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000324
325 markLinesDirtyInBlockRange(changeLogicalTop, changeLogicalBottom);
326 } else if (!oldIntrudingFloatSet.isEmpty()) {
327 // If there are previously intruding floats that no longer intrude, then children with floats
328 // should also get layout because they might need their floating object lists cleared.
329 if (m_floatingObjects->set().size() < oldIntrudingFloatSet.size())
330 markAllDescendantsWithFloatsForLayout();
331 else {
332 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
darin@apple.com7cad7042013-09-24 05:53:55 +0000333 auto end = floatingObjectSet.end();
334 for (auto it = floatingObjectSet.begin(); it != end && !oldIntrudingFloatSet.isEmpty(); ++it)
335 oldIntrudingFloatSet.remove(&(*it)->renderer());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000336 if (!oldIntrudingFloatSet.isEmpty())
337 markAllDescendantsWithFloatsForLayout();
338 }
339 }
340}
341
342void RenderBlockFlow::layoutBlock(bool relayoutChildren, LayoutUnit pageLogicalHeight)
343{
344 ASSERT(needsLayout());
345
346 if (!relayoutChildren && simplifiedLayout())
347 return;
348
349 LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
350
351 if (updateLogicalWidthAndColumnWidth())
352 relayoutChildren = true;
353
354 clearFloats();
355
356 LayoutUnit previousHeight = logicalHeight();
357 // FIXME: should this start out as borderAndPaddingLogicalHeight() + scrollbarLogicalHeight(),
358 // for consistency with other render classes?
359 setLogicalHeight(0);
360
361 bool pageLogicalHeightChanged = false;
362 bool hasSpecifiedPageLogicalHeight = false;
363 checkForPaginationLogicalHeightChange(pageLogicalHeight, pageLogicalHeightChanged, hasSpecifiedPageLogicalHeight);
364
akling@apple.com827be9c2013-10-29 02:58:43 +0000365 const RenderStyle& styleToUse = style();
weinig@apple.comf8d77f32013-11-18 01:12:17 +0000366 LayoutStateMaintainer statePusher(view(), *this, locationOffset(), hasColumns() || hasTransform() || hasReflection() || styleToUse.isFlippedBlocksWritingMode(), pageLogicalHeight, pageLogicalHeightChanged, columnInfo());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000367
abucur@adobe.com0e81bc72013-10-22 14:50:37 +0000368 prepareShapesAndPaginationBeforeBlockLayout(relayoutChildren);
369 if (!relayoutChildren)
370 relayoutChildren = namedFlowFragmentNeedsUpdate();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000371
372 // We use four values, maxTopPos, maxTopNeg, maxBottomPos, and maxBottomNeg, to track
373 // our current maximal positive and negative margins. These values are used when we
374 // are collapsed with adjacent blocks, so for example, if you have block A and B
375 // collapsing together, then you'd take the maximal positive margin from both A and B
376 // and subtract it from the maximal negative margin from both A and B to get the
377 // true collapsed margin. This algorithm is recursive, so when we finish layout()
378 // our block knows its current maximal positive/negative values.
379 //
380 // Start out by setting our margin values to our current margins. Table cells have
381 // no margins, so we don't fill in the values for table cells.
382 bool isCell = isTableCell();
383 if (!isCell) {
384 initMaxMarginValues();
385
akling@apple.com827be9c2013-10-29 02:58:43 +0000386 setHasMarginBeforeQuirk(styleToUse.hasMarginBeforeQuirk());
387 setHasMarginAfterQuirk(styleToUse.hasMarginAfterQuirk());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000388 setPaginationStrut(0);
389 }
390
391 LayoutUnit repaintLogicalTop = 0;
392 LayoutUnit repaintLogicalBottom = 0;
393 LayoutUnit maxFloatLogicalBottom = 0;
394 if (!firstChild() && !isAnonymousBlock())
395 setChildrenInline(true);
396 if (childrenInline())
397 layoutInlineChildren(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
398 else
399 layoutBlockChildren(relayoutChildren, maxFloatLogicalBottom);
400
401 // Expand our intrinsic height to encompass floats.
402 LayoutUnit toAdd = borderAndPaddingAfter() + scrollbarLogicalHeight();
403 if (lowestFloatLogicalBottom() > (logicalHeight() - toAdd) && expandsToEncloseOverhangingFloats())
404 setLogicalHeight(lowestFloatLogicalBottom() + toAdd);
405
406 if (relayoutForPagination(hasSpecifiedPageLogicalHeight, pageLogicalHeight, statePusher) || relayoutToAvoidWidows(statePusher)) {
407 ASSERT(!shouldBreakAtLineToAvoidWidow());
408 return;
409 }
410
411 // Calculate our new height.
412 LayoutUnit oldHeight = logicalHeight();
413 LayoutUnit oldClientAfterEdge = clientLogicalBottom();
414
415 // Before updating the final size of the flow thread make sure a forced break is applied after the content.
416 // This ensures the size information is correctly computed for the last auto-height region receiving content.
417 if (isRenderFlowThread())
418 toRenderFlowThread(this)->applyBreakAfterContent(oldClientAfterEdge);
419
420 updateLogicalHeight();
421 LayoutUnit newHeight = logicalHeight();
422 if (oldHeight != newHeight) {
423 if (oldHeight > newHeight && maxFloatLogicalBottom > newHeight && !childrenInline()) {
424 // 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 +0000425 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
426 if (blockFlow.isFloatingOrOutOfFlowPositioned())
427 continue;
428 if (blockFlow.lowestFloatLogicalBottom() + blockFlow.logicalTop() > newHeight)
429 addOverhangingFloats(blockFlow, false);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000430 }
431 }
432 }
433
434 bool heightChanged = (previousHeight != newHeight);
435 if (heightChanged)
436 relayoutChildren = true;
437
438 layoutPositionedObjects(relayoutChildren || isRoot());
439
440 updateShapesAfterBlockLayout(heightChanged);
441
442 // Add overflow from children (unless we're multi-column, since in that case all our child overflow is clipped anyway).
443 computeOverflow(oldClientAfterEdge);
444
445 statePusher.pop();
446
447 fitBorderToLinesIfNeeded();
448
449 if (view().layoutState()->m_pageLogicalHeight)
450 setPageLogicalOffset(view().layoutState()->pageLogicalOffset(this, logicalTop()));
451
452 updateLayerTransform();
453
454 // Update our scroll information if we're overflow:auto/scroll/hidden now that we know if
455 // we overflow or not.
456 updateScrollInfoAfterLayout();
457
458 // FIXME: This repaint logic should be moved into a separate helper function!
459 // Repaint with our new bounds if they are different from our old bounds.
460 bool didFullRepaint = repainter.repaintAfterLayout();
akling@apple.com827be9c2013-10-29 02:58:43 +0000461 if (!didFullRepaint && repaintLogicalTop != repaintLogicalBottom && (styleToUse.visibility() == VISIBLE || enclosingLayer()->hasVisibleContent())) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000462 // FIXME: We could tighten up the left and right invalidation points if we let layoutInlineChildren fill them in based off the particular lines
463 // it had to lay out. We wouldn't need the hasOverflowClip() hack in that case either.
464 LayoutUnit repaintLogicalLeft = logicalLeftVisualOverflow();
465 LayoutUnit repaintLogicalRight = logicalRightVisualOverflow();
466 if (hasOverflowClip()) {
467 // 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.
468 // 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.
469 // layoutInlineChildren should be patched to compute the entire repaint rect.
andersca@apple.com86298632013-11-10 19:32:33 +0000470 repaintLogicalLeft = std::min(repaintLogicalLeft, logicalLeftLayoutOverflow());
471 repaintLogicalRight = std::max(repaintLogicalRight, logicalRightLayoutOverflow());
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000472 }
473
474 LayoutRect repaintRect;
475 if (isHorizontalWritingMode())
476 repaintRect = LayoutRect(repaintLogicalLeft, repaintLogicalTop, repaintLogicalRight - repaintLogicalLeft, repaintLogicalBottom - repaintLogicalTop);
477 else
478 repaintRect = LayoutRect(repaintLogicalTop, repaintLogicalLeft, repaintLogicalBottom - repaintLogicalTop, repaintLogicalRight - repaintLogicalLeft);
479
480 // The repaint rect may be split across columns, in which case adjustRectForColumns() will return the union.
481 adjustRectForColumns(repaintRect);
482
483 repaintRect.inflate(maximalOutlineSize(PaintPhaseOutline));
484
485 if (hasOverflowClip()) {
486 // Adjust repaint rect for scroll offset
487 repaintRect.move(-scrolledContentOffset());
488
489 // Don't allow this rect to spill out of our overflow box.
490 repaintRect.intersect(LayoutRect(LayoutPoint(), size()));
491 }
492
493 // Make sure the rect is still non-empty after intersecting for overflow above
494 if (!repaintRect.isEmpty()) {
495 repaintRectangle(repaintRect); // We need to do a partial repaint of our content.
496 if (hasReflection())
497 repaintRectangle(reflectedRect(repaintRect));
498 }
499 }
500
antti@apple.comca2a8ff2013-10-04 04:04:35 +0000501 clearNeedsLayout();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000502}
503
504void RenderBlockFlow::layoutBlockChildren(bool relayoutChildren, LayoutUnit& maxFloatLogicalBottom)
505{
506 dirtyForLayoutFromPercentageHeightDescendants();
507
508 LayoutUnit beforeEdge = borderAndPaddingBefore();
509 LayoutUnit afterEdge = borderAndPaddingAfter() + scrollbarLogicalHeight();
510
511 setLogicalHeight(beforeEdge);
512
513 // Lay out our hypothetical grid line as though it occurs at the top of the block.
514 if (view().layoutState()->lineGrid() == this)
515 layoutLineGridBox();
516
517 // The margin struct caches all our current margin collapsing state.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000518 MarginInfo marginInfo(*this, beforeEdge, afterEdge);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000519
520 // Fieldsets need to find their legend and position it inside the border of the object.
521 // The legend then gets skipped during normal layout. The same is true for ruby text.
522 // It doesn't get included in the normal layout process but is instead skipped.
523 RenderObject* childToExclude = layoutSpecialExcludedChild(relayoutChildren);
524
525 LayoutUnit previousFloatLogicalBottom = 0;
526 maxFloatLogicalBottom = 0;
527
528 RenderBox* next = firstChildBox();
529
530 while (next) {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000531 RenderBox& child = *next;
532 next = child.nextSiblingBox();
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000533
weinig@apple.com12840dc2013-10-22 23:59:08 +0000534 if (childToExclude == &child)
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000535 continue; // Skip this child, since it will be positioned by the specialized subclass (fieldsets and ruby runs).
536
537 updateBlockChildDirtyBitsBeforeLayout(relayoutChildren, child);
538
weinig@apple.com12840dc2013-10-22 23:59:08 +0000539 if (child.isOutOfFlowPositioned()) {
540 child.containingBlock()->insertPositionedObject(child);
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000541 adjustPositionedBlock(child, marginInfo);
542 continue;
543 }
weinig@apple.com12840dc2013-10-22 23:59:08 +0000544 if (child.isFloating()) {
hyatt@apple.comc9b1aef2013-09-09 20:23:21 +0000545 insertFloatingObject(child);
546 adjustFloatingBlock(marginInfo);
547 continue;
548 }
549
550 // Lay out the child.
551 layoutBlockChild(child, marginInfo, previousFloatLogicalBottom, maxFloatLogicalBottom);
552 }
553
554 // Now do the handling of the bottom of the block, adding in our bottom border/padding and
555 // determining the correct collapsed bottom margin information.
556 handleAfterSideOfBlock(beforeEdge, afterEdge, marginInfo);
557}
558
antti@apple.com940f5872013-10-24 20:31:11 +0000559void RenderBlockFlow::layoutInlineChildren(bool relayoutChildren, LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
560{
antti@apple.com42fb53d2013-10-25 02:33:11 +0000561 if (m_lineLayoutPath == UndeterminedPath)
562 m_lineLayoutPath = SimpleLineLayout::canUseFor(*this) ? SimpleLinesPath : LineBoxesPath;
563
564 if (m_lineLayoutPath == SimpleLinesPath) {
antti@apple.com940f5872013-10-24 20:31:11 +0000565 deleteLineBoxesBeforeSimpleLineLayout();
566 layoutSimpleLines(repaintLogicalTop, repaintLogicalBottom);
567 return;
568 }
569
antti@apple.comfea51992013-10-28 13:39:23 +0000570 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +0000571 layoutLineBoxes(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
572}
573
weinig@apple.com12840dc2013-10-22 23:59:08 +0000574void RenderBlockFlow::layoutBlockChild(RenderBox& child, MarginInfo& marginInfo, LayoutUnit& previousFloatLogicalBottom, LayoutUnit& maxFloatLogicalBottom)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000575{
576 LayoutUnit oldPosMarginBefore = maxPositiveMarginBefore();
577 LayoutUnit oldNegMarginBefore = maxNegativeMarginBefore();
578
579 // The child is a normal flow object. Compute the margins we will use for collapsing now.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000580 child.computeAndSetBlockDirectionMargins(this);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000581
582 // Try to guess our correct logical top position. In most cases this guess will
583 // be correct. Only if we're wrong (when we compute the real logical top position)
584 // will we have to potentially relayout.
585 LayoutUnit estimateWithoutPagination;
586 LayoutUnit logicalTopEstimate = estimateLogicalTopPosition(child, marginInfo, estimateWithoutPagination);
587
588 // 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 +0000589 LayoutRect oldRect = child.frameRect();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000590 LayoutUnit oldLogicalTop = logicalTopForChild(child);
591
592#if !ASSERT_DISABLED
593 LayoutSize oldLayoutDelta = view().layoutDelta();
594#endif
595 // Go ahead and position the child as though it didn't collapse with the top.
596 setLogicalTopForChild(child, logicalTopEstimate, ApplyLayoutDelta);
597 estimateRegionRangeForBoxChild(child);
598
weinig@apple.com12840dc2013-10-22 23:59:08 +0000599 RenderBlockFlow* childBlockFlow = child.isRenderBlockFlow() ? toRenderBlockFlow(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000600 bool markDescendantsWithFloats = false;
weinig@apple.com12840dc2013-10-22 23:59:08 +0000601 if (logicalTopEstimate != oldLogicalTop && !child.avoidsFloats() && childBlockFlow && childBlockFlow->containsFloats())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000602 markDescendantsWithFloats = true;
603#if ENABLE(SUBPIXEL_LAYOUT)
604 else if (UNLIKELY(logicalTopEstimate.mightBeSaturated()))
605 // logicalTopEstimate, returned by estimateLogicalTopPosition, might be saturated for
606 // very large elements. If it does the comparison with oldLogicalTop might yield a
607 // false negative as adding and removing margins, borders etc from a saturated number
608 // might yield incorrect results. If this is the case always mark for layout.
609 markDescendantsWithFloats = true;
610#endif
weinig@apple.com12840dc2013-10-22 23:59:08 +0000611 else if (!child.avoidsFloats() || child.shrinkToAvoidFloats()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000612 // If an element might be affected by the presence of floats, then always mark it for
613 // layout.
andersca@apple.com86298632013-11-10 19:32:33 +0000614 LayoutUnit fb = std::max(previousFloatLogicalBottom, lowestFloatLogicalBottom());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000615 if (fb > logicalTopEstimate)
616 markDescendantsWithFloats = true;
617 }
618
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000619 if (childBlockFlow) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000620 if (markDescendantsWithFloats)
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000621 childBlockFlow->markAllDescendantsWithFloatsForLayout();
weinig@apple.com12840dc2013-10-22 23:59:08 +0000622 if (!child.isWritingModeRoot())
andersca@apple.com86298632013-11-10 19:32:33 +0000623 previousFloatLogicalBottom = std::max(previousFloatLogicalBottom, oldLogicalTop + childBlockFlow->lowestFloatLogicalBottom());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000624 }
625
weinig@apple.com12840dc2013-10-22 23:59:08 +0000626 if (!child.needsLayout())
627 child.markForPaginationRelayoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000628
weinig@apple.com12840dc2013-10-22 23:59:08 +0000629 bool childHadLayout = child.everHadLayout();
630 bool childNeededLayout = child.needsLayout();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000631 if (childNeededLayout)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000632 child.layout();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000633
634 // Cache if we are at the top of the block right now.
635 bool atBeforeSideOfBlock = marginInfo.atBeforeSideOfBlock();
636
637 // Now determine the correct ypos based off examination of collapsing margin
638 // values.
639 LayoutUnit logicalTopBeforeClear = collapseMargins(child, marginInfo);
640
641 // Now check for clear.
642 LayoutUnit logicalTopAfterClear = clearFloatsIfNeeded(child, marginInfo, oldPosMarginBefore, oldNegMarginBefore, logicalTopBeforeClear);
643
644 bool paginated = view().layoutState()->isPaginated();
645 if (paginated)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000646 logicalTopAfterClear = adjustBlockChildForPagination(logicalTopAfterClear, estimateWithoutPagination, child, atBeforeSideOfBlock && logicalTopBeforeClear == logicalTopAfterClear);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000647
648 setLogicalTopForChild(child, logicalTopAfterClear, ApplyLayoutDelta);
649
650 // Now we have a final top position. See if it really does end up being different from our estimate.
651 // clearFloatsIfNeeded can also mark the child as needing a layout even though we didn't move. This happens
652 // when collapseMargins dynamically adds overhanging floats because of a child with negative margins.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000653 if (logicalTopAfterClear != logicalTopEstimate || child.needsLayout() || (paginated && childBlockFlow && childBlockFlow->shouldBreakAtLineToAvoidWidow())) {
654 if (child.shrinkToAvoidFloats()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000655 // The child's width depends on the line width.
656 // When the child shifts to clear an item, its width can
657 // change (because it has more available line width).
658 // So go ahead and mark the item as dirty.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000659 child.setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000660 }
661
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000662 if (childBlockFlow) {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000663 if (!child.avoidsFloats() && childBlockFlow->containsFloats())
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000664 childBlockFlow->markAllDescendantsWithFloatsForLayout();
weinig@apple.com12840dc2013-10-22 23:59:08 +0000665 if (!child.needsLayout())
666 child.markForPaginationRelayoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000667 }
668
669 // Our guess was wrong. Make the child lay itself out again.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000670 child.layoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000671 }
672
673 if (updateRegionRangeForBoxChild(child)) {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000674 child.setNeedsLayout(MarkOnlyThis);
675 child.layoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000676 }
677
678 // We are no longer at the top of the block if we encounter a non-empty child.
679 // 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 +0000680 if (marginInfo.atBeforeSideOfBlock() && !child.isSelfCollapsingBlock())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000681 marginInfo.setAtBeforeSideOfBlock(false);
682
683 // Now place the child in the correct left position
684 determineLogicalLeftPositionForChild(child, ApplyLayoutDelta);
685
weinig@apple.com12840dc2013-10-22 23:59:08 +0000686 LayoutSize childOffset = child.location() - oldRect.location();
dino@apple.comc133a8c2014-02-03 23:35:34 +0000687#if ENABLE(CSS_SHAPES) && ENABLE(CSS_SHAPE_INSIDE)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000688 relayoutShapeDescendantIfMoved(child.isRenderBlock() ? toRenderBlock(&child) : nullptr, childOffset);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000689#endif
690
691 // Update our height now that the child has been placed in the correct position.
692 setLogicalHeight(logicalHeight() + logicalHeightForChild(child));
693 if (mustSeparateMarginAfterForChild(child)) {
694 setLogicalHeight(logicalHeight() + marginAfterForChild(child));
695 marginInfo.clearMargin();
696 }
697 // If the child has overhanging floats that intrude into following siblings (or possibly out
698 // of this block), then the parent gets notified of the floats now.
hyatt@apple.com2ea59882013-09-17 16:41:42 +0000699 if (childBlockFlow && childBlockFlow->containsFloats())
andersca@apple.com86298632013-11-10 19:32:33 +0000700 maxFloatLogicalBottom = std::max(maxFloatLogicalBottom, addOverhangingFloats(*childBlockFlow, !childNeededLayout));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000701
702 if (childOffset.width() || childOffset.height()) {
703 view().addLayoutDelta(childOffset);
704
705 // If the child moved, we have to repaint it as well as any floating/positioned
706 // descendants. An exception is if we need a layout. In this case, we know we're going to
707 // repaint ourselves (and the child) anyway.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000708 if (childHadLayout && !selfNeedsLayout() && child.checkForRepaintDuringLayout())
709 child.repaintDuringLayoutIfMoved(oldRect);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000710 }
711
weinig@apple.com12840dc2013-10-22 23:59:08 +0000712 if (!childHadLayout && child.checkForRepaintDuringLayout()) {
713 child.repaint();
714 child.repaintOverhangingFloats(true);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000715 }
716
717 if (paginated) {
718 // Check for an after page/column break.
719 LayoutUnit newHeight = applyAfterBreak(child, logicalHeight(), marginInfo);
720 if (newHeight != height())
721 setLogicalHeight(newHeight);
722 }
723
724 ASSERT(view().layoutDeltaMatches(oldLayoutDelta));
725}
726
weinig@apple.com12840dc2013-10-22 23:59:08 +0000727void RenderBlockFlow::adjustPositionedBlock(RenderBox& child, const MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000728{
729 bool isHorizontal = isHorizontalWritingMode();
akling@apple.com827be9c2013-10-29 02:58:43 +0000730 bool hasStaticBlockPosition = child.style().hasStaticBlockPosition(isHorizontal);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000731
732 LayoutUnit logicalTop = logicalHeight();
733 updateStaticInlinePositionForChild(child, logicalTop);
734
735 if (!marginInfo.canCollapseWithMarginBefore()) {
736 // Positioned blocks don't collapse margins, so add the margin provided by
737 // the container now. The child's own margin is added later when calculating its logical top.
738 LayoutUnit collapsedBeforePos = marginInfo.positiveMargin();
739 LayoutUnit collapsedBeforeNeg = marginInfo.negativeMargin();
740 logicalTop += collapsedBeforePos - collapsedBeforeNeg;
741 }
742
weinig@apple.com12840dc2013-10-22 23:59:08 +0000743 RenderLayer* childLayer = child.layer();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000744 if (childLayer->staticBlockPosition() != logicalTop) {
745 childLayer->setStaticBlockPosition(logicalTop);
746 if (hasStaticBlockPosition)
weinig@apple.com12840dc2013-10-22 23:59:08 +0000747 child.setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000748 }
749}
750
robert@webkit.org97037ef2013-11-20 19:26:10 +0000751LayoutUnit RenderBlockFlow::marginOffsetForSelfCollapsingBlock()
752{
753 ASSERT(isSelfCollapsingBlock());
754 RenderBlockFlow* parentBlock = toRenderBlockFlow(parent());
755 if (parentBlock && style().clear() && parentBlock->getClearDelta(*this, logicalHeight()))
756 return marginValuesForChild(*this).positiveMarginBefore();
757 return LayoutUnit();
758}
759
hyatt@apple.com31a5daa2014-01-28 01:26:37 +0000760void RenderBlockFlow::determineLogicalLeftPositionForChild(RenderBox& child, ApplyLayoutDeltaMode applyDelta)
761{
762 LayoutUnit startPosition = borderStart() + paddingStart();
763 if (style().shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
764 startPosition -= verticalScrollbarWidth();
765 LayoutUnit totalAvailableLogicalWidth = borderAndPaddingLogicalWidth() + availableLogicalWidth();
766
767 // Add in our start margin.
768 LayoutUnit childMarginStart = marginStartForChild(child);
769 LayoutUnit newPosition = startPosition + childMarginStart;
770
771 // Some objects (e.g., tables, horizontal rules, overflow:auto blocks) avoid floats. They need
772 // to shift over as necessary to dodge any floats that might get in the way.
773 if (child.avoidsFloats() && containsFloats() && !flowThreadContainingBlock())
774 newPosition += computeStartPositionDeltaForChildAvoidingFloats(child, marginStartForChild(child));
775
776 setLogicalLeftForChild(child, style().isLeftToRightDirection() ? newPosition : totalAvailableLogicalWidth - newPosition - logicalWidthForChild(child), applyDelta);
777}
robert@webkit.org97037ef2013-11-20 19:26:10 +0000778
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000779void RenderBlockFlow::adjustFloatingBlock(const MarginInfo& marginInfo)
780{
781 // The float should be positioned taking into account the bottom margin
782 // of the previous flow. We add that margin into the height, get the
783 // float positioned properly, and then subtract the margin out of the
784 // height again. In the case of self-collapsing blocks, we always just
785 // use the top margins, since the self-collapsing block collapsed its
786 // own bottom margin into its top margin.
787 //
788 // Note also that the previous flow may collapse its margin into the top of
789 // our block. If this is the case, then we do not add the margin in to our
790 // height when computing the position of the float. This condition can be tested
791 // for by simply calling canCollapseWithMarginBefore. See
792 // http://www.hixie.ch/tests/adhoc/css/box/block/margin-collapse/046.html for
793 // an example of this scenario.
794 LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
795 setLogicalHeight(logicalHeight() + marginOffset);
796 positionNewFloats();
797 setLogicalHeight(logicalHeight() - marginOffset);
798}
799
weinig@apple.com12840dc2013-10-22 23:59:08 +0000800void RenderBlockFlow::updateStaticInlinePositionForChild(RenderBox& child, LayoutUnit logicalTop)
801{
akling@apple.com827be9c2013-10-29 02:58:43 +0000802 if (child.style().isOriginalDisplayInlineType())
weinig@apple.com12840dc2013-10-22 23:59:08 +0000803 setStaticInlinePositionForChild(child, logicalTop, startAlignedOffsetForLine(logicalTop, false));
804 else
805 setStaticInlinePositionForChild(child, logicalTop, startOffsetForContent(logicalTop));
806}
807
808void RenderBlockFlow::setStaticInlinePositionForChild(RenderBox& child, LayoutUnit blockOffset, LayoutUnit inlinePosition)
809{
810 if (flowThreadContainingBlock()) {
811 // Shift the inline position to exclude the region offset.
812 inlinePosition += startOffsetForContent() - startOffsetForContent(blockOffset);
813 }
814 child.layer()->setStaticInlinePosition(inlinePosition);
815}
816
817RenderBlockFlow::MarginValues RenderBlockFlow::marginValuesForChild(RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000818{
819 LayoutUnit childBeforePositive = 0;
820 LayoutUnit childBeforeNegative = 0;
821 LayoutUnit childAfterPositive = 0;
822 LayoutUnit childAfterNegative = 0;
823
824 LayoutUnit beforeMargin = 0;
825 LayoutUnit afterMargin = 0;
826
weinig@apple.com12840dc2013-10-22 23:59:08 +0000827 RenderBlockFlow* childRenderBlock = child.isRenderBlockFlow() ? toRenderBlockFlow(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000828
829 // If the child has the same directionality as we do, then we can just return its
830 // margins in the same direction.
weinig@apple.com12840dc2013-10-22 23:59:08 +0000831 if (!child.isWritingModeRoot()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000832 if (childRenderBlock) {
833 childBeforePositive = childRenderBlock->maxPositiveMarginBefore();
834 childBeforeNegative = childRenderBlock->maxNegativeMarginBefore();
835 childAfterPositive = childRenderBlock->maxPositiveMarginAfter();
836 childAfterNegative = childRenderBlock->maxNegativeMarginAfter();
837 } else {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000838 beforeMargin = child.marginBefore();
839 afterMargin = child.marginAfter();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000840 }
weinig@apple.com12840dc2013-10-22 23:59:08 +0000841 } else if (child.isHorizontalWritingMode() == isHorizontalWritingMode()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000842 // The child has a different directionality. If the child is parallel, then it's just
843 // flipped relative to us. We can use the margins for the opposite edges.
844 if (childRenderBlock) {
845 childBeforePositive = childRenderBlock->maxPositiveMarginAfter();
846 childBeforeNegative = childRenderBlock->maxNegativeMarginAfter();
847 childAfterPositive = childRenderBlock->maxPositiveMarginBefore();
848 childAfterNegative = childRenderBlock->maxNegativeMarginBefore();
849 } else {
weinig@apple.com12840dc2013-10-22 23:59:08 +0000850 beforeMargin = child.marginAfter();
851 afterMargin = child.marginBefore();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000852 }
853 } else {
854 // The child is perpendicular to us, which means its margins don't collapse but are on the
855 // "logical left/right" sides of the child box. We can just return the raw margin in this case.
856 beforeMargin = marginBeforeForChild(child);
857 afterMargin = marginAfterForChild(child);
858 }
859
860 // Resolve uncollapsing margins into their positive/negative buckets.
861 if (beforeMargin) {
862 if (beforeMargin > 0)
863 childBeforePositive = beforeMargin;
864 else
865 childBeforeNegative = -beforeMargin;
866 }
867 if (afterMargin) {
868 if (afterMargin > 0)
869 childAfterPositive = afterMargin;
870 else
871 childAfterNegative = -afterMargin;
872 }
873
874 return MarginValues(childBeforePositive, childBeforeNegative, childAfterPositive, childAfterNegative);
875}
876
weinig@apple.com12840dc2013-10-22 23:59:08 +0000877LayoutUnit RenderBlockFlow::collapseMargins(RenderBox& child, MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000878{
879 bool childDiscardMarginBefore = mustDiscardMarginBeforeForChild(child);
880 bool childDiscardMarginAfter = mustDiscardMarginAfterForChild(child);
weinig@apple.com12840dc2013-10-22 23:59:08 +0000881 bool childIsSelfCollapsing = child.isSelfCollapsingBlock();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000882
883 // The child discards the before margin when the the after margin has discard in the case of a self collapsing block.
884 childDiscardMarginBefore = childDiscardMarginBefore || (childDiscardMarginAfter && childIsSelfCollapsing);
885
886 // Get the four margin values for the child and cache them.
887 const MarginValues childMargins = marginValuesForChild(child);
888
889 // Get our max pos and neg top margins.
890 LayoutUnit posTop = childMargins.positiveMarginBefore();
891 LayoutUnit negTop = childMargins.negativeMarginBefore();
892
893 // For self-collapsing blocks, collapse our bottom margins into our
894 // top to get new posTop and negTop values.
895 if (childIsSelfCollapsing) {
andersca@apple.com86298632013-11-10 19:32:33 +0000896 posTop = std::max(posTop, childMargins.positiveMarginAfter());
897 negTop = std::max(negTop, childMargins.negativeMarginAfter());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000898 }
899
900 // See if the top margin is quirky. We only care if this child has
901 // margins that will collapse with us.
902 bool topQuirk = hasMarginBeforeQuirk(child);
903
904 if (marginInfo.canCollapseWithMarginBefore()) {
905 if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
906 // This child is collapsing with the top of the
907 // block. If it has larger margin values, then we need to update
908 // our own maximal values.
909 if (!document().inQuirksMode() || !marginInfo.quirkContainer() || !topQuirk)
andersca@apple.com86298632013-11-10 19:32:33 +0000910 setMaxMarginBeforeValues(std::max(posTop, maxPositiveMarginBefore()), std::max(negTop, maxNegativeMarginBefore()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000911
912 // The minute any of the margins involved isn't a quirk, don't
913 // collapse it away, even if the margin is smaller (www.webreference.com
914 // has an example of this, a <dt> with 0.8em author-specified inside
915 // a <dl> inside a <td>.
916 if (!marginInfo.determinedMarginBeforeQuirk() && !topQuirk && (posTop - negTop)) {
917 setHasMarginBeforeQuirk(false);
918 marginInfo.setDeterminedMarginBeforeQuirk(true);
919 }
920
921 if (!marginInfo.determinedMarginBeforeQuirk() && topQuirk && !marginBefore())
922 // We have no top margin and our top child has a quirky margin.
923 // We will pick up this quirky margin and pass it through.
924 // This deals with the <td><div><p> case.
925 // Don't do this for a block that split two inlines though. You do
926 // still apply margins in this case.
927 setHasMarginBeforeQuirk(true);
928 } else
929 // The before margin of the container will also discard all the margins it is collapsing with.
930 setMustDiscardMarginBefore();
931 }
932
933 // Once we find a child with discardMarginBefore all the margins collapsing with us must also discard.
934 if (childDiscardMarginBefore) {
935 marginInfo.setDiscardMargin(true);
936 marginInfo.clearMargin();
937 }
938
939 if (marginInfo.quirkContainer() && marginInfo.atBeforeSideOfBlock() && (posTop - negTop))
940 marginInfo.setHasMarginBeforeQuirk(topQuirk);
941
942 LayoutUnit beforeCollapseLogicalTop = logicalHeight();
943 LayoutUnit logicalTop = beforeCollapseLogicalTop;
robert@webkit.org97037ef2013-11-20 19:26:10 +0000944
945 LayoutUnit clearanceForSelfCollapsingBlock;
946 RenderObject* prev = child.previousSibling();
947 // 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
948 // 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
949 // 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.
950 if (!marginInfo.canCollapseWithMarginBefore() && prev && prev->isRenderBlockFlow() && toRenderBlockFlow(prev)->isSelfCollapsingBlock()) {
951 clearanceForSelfCollapsingBlock = toRenderBlockFlow(prev)->marginOffsetForSelfCollapsingBlock();
952 setLogicalHeight(logicalHeight() - clearanceForSelfCollapsingBlock);
953 }
954
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000955 if (childIsSelfCollapsing) {
956 // 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.
957 // Also, the child's top position equals the logical height of the container.
958 if (!childDiscardMarginBefore && !marginInfo.discardMargin()) {
959 // This child has no height. We need to compute our
960 // position before we collapse the child's margins together,
961 // so that we can get an accurate position for the zero-height block.
andersca@apple.com86298632013-11-10 19:32:33 +0000962 LayoutUnit collapsedBeforePos = std::max(marginInfo.positiveMargin(), childMargins.positiveMarginBefore());
963 LayoutUnit collapsedBeforeNeg = std::max(marginInfo.negativeMargin(), childMargins.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000964 marginInfo.setMargin(collapsedBeforePos, collapsedBeforeNeg);
965
966 // Now collapse the child's margins together, which means examining our
967 // bottom margin values as well.
968 marginInfo.setPositiveMarginIfLarger(childMargins.positiveMarginAfter());
969 marginInfo.setNegativeMarginIfLarger(childMargins.negativeMarginAfter());
970
971 if (!marginInfo.canCollapseWithMarginBefore())
972 // We need to make sure that the position of the self-collapsing block
973 // is correct, since it could have overflowing content
974 // that needs to be positioned correctly (e.g., a block that
975 // had a specified height of 0 but that actually had subcontent).
976 logicalTop = logicalHeight() + collapsedBeforePos - collapsedBeforeNeg;
977 }
978 } else {
979 if (mustSeparateMarginBeforeForChild(child)) {
980 ASSERT(!marginInfo.discardMargin() || (marginInfo.discardMargin() && !marginInfo.margin()));
981 // If we are at the before side of the block and we collapse, ignore the computed margin
982 // and just add the child margin to the container height. This will correctly position
983 // the child inside the container.
zalan@apple.coma4d00552014-01-25 00:21:59 +0000984 LayoutUnit separateMargin = !marginInfo.canCollapseWithMarginBefore() ? marginInfo.margin() : LayoutUnit::fromPixel(0);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000985 setLogicalHeight(logicalHeight() + separateMargin + marginBeforeForChild(child));
986 logicalTop = logicalHeight();
987 } else if (!marginInfo.discardMargin() && (!marginInfo.atBeforeSideOfBlock()
988 || (!marginInfo.canCollapseMarginBeforeWithChildren()
989 && (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginBeforeQuirk())))) {
990 // We're collapsing with a previous sibling's margins and not
991 // with the top of the block.
andersca@apple.com86298632013-11-10 19:32:33 +0000992 setLogicalHeight(logicalHeight() + std::max(marginInfo.positiveMargin(), posTop) - std::max(marginInfo.negativeMargin(), negTop));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +0000993 logicalTop = logicalHeight();
994 }
995
996 marginInfo.setDiscardMargin(childDiscardMarginAfter);
997
998 if (!marginInfo.discardMargin()) {
999 marginInfo.setPositiveMargin(childMargins.positiveMarginAfter());
1000 marginInfo.setNegativeMargin(childMargins.negativeMarginAfter());
1001 } else
1002 marginInfo.clearMargin();
1003
1004 if (marginInfo.margin())
1005 marginInfo.setHasMarginAfterQuirk(hasMarginAfterQuirk(child));
1006 }
1007
1008 // If margins would pull us past the top of the next page, then we need to pull back and pretend like the margins
1009 // collapsed into the page edge.
1010 LayoutState* layoutState = view().layoutState();
1011 if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTop > beforeCollapseLogicalTop
1012 && hasNextPage(beforeCollapseLogicalTop)) {
1013 LayoutUnit oldLogicalTop = logicalTop;
andersca@apple.com86298632013-11-10 19:32:33 +00001014 logicalTop = std::min(logicalTop, nextPageLogicalTop(beforeCollapseLogicalTop));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001015 setLogicalHeight(logicalHeight() + (logicalTop - oldLogicalTop));
1016 }
1017
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001018 if (prev && prev->isRenderBlockFlow() && !prev->isFloatingOrOutOfFlowPositioned()) {
robert@webkit.org97037ef2013-11-20 19:26:10 +00001019 // 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
1020 // any floats from the parent will now overhang.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001021 RenderBlockFlow& block = toRenderBlockFlow(*prev);
robert@webkit.org97037ef2013-11-20 19:26:10 +00001022 LayoutUnit oldLogicalHeight = logicalHeight();
1023 setLogicalHeight(logicalTop);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001024 if (block.containsFloats() && !block.avoidsFloats() && (block.logicalTop() + block.lowestFloatLogicalBottom()) > logicalTop)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001025 addOverhangingFloats(block, false);
robert@webkit.org97037ef2013-11-20 19:26:10 +00001026 setLogicalHeight(oldLogicalHeight);
1027
1028 // If |child|'s previous sibling is a self-collapsing block that cleared a float and margin collapsing resulted in |child| moving up
1029 // 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
1030 // floats in the parent that overhang |child|'s new logical top.
1031 bool logicalTopIntrudesIntoFloat = clearanceForSelfCollapsingBlock > 0 && logicalTop < beforeCollapseLogicalTop;
1032 if (logicalTopIntrudesIntoFloat && containsFloats() && !child.avoidsFloats() && lowestFloatLogicalBottom() > logicalTop)
1033 child.setNeedsLayout();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001034 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001035
1036 return logicalTop;
1037}
1038
weinig@apple.com12840dc2013-10-22 23:59:08 +00001039LayoutUnit RenderBlockFlow::clearFloatsIfNeeded(RenderBox& child, MarginInfo& marginInfo, LayoutUnit oldTopPosMargin, LayoutUnit oldTopNegMargin, LayoutUnit yPos)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001040{
1041 LayoutUnit heightIncrease = getClearDelta(child, yPos);
1042 if (!heightIncrease)
1043 return yPos;
1044
weinig@apple.com12840dc2013-10-22 23:59:08 +00001045 if (child.isSelfCollapsingBlock()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001046 bool childDiscardMargin = mustDiscardMarginBeforeForChild(child) || mustDiscardMarginAfterForChild(child);
1047
1048 // For self-collapsing blocks that clear, they can still collapse their
1049 // margins with following siblings. Reset the current margins to represent
1050 // the self-collapsing block's margins only.
1051 // If DISCARD is specified for -webkit-margin-collapse, reset the margin values.
robert@webkit.org97037ef2013-11-20 19:26:10 +00001052 MarginValues childMargins = marginValuesForChild(child);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001053 if (!childDiscardMargin) {
andersca@apple.com86298632013-11-10 19:32:33 +00001054 marginInfo.setPositiveMargin(std::max(childMargins.positiveMarginBefore(), childMargins.positiveMarginAfter()));
1055 marginInfo.setNegativeMargin(std::max(childMargins.negativeMarginBefore(), childMargins.negativeMarginAfter()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001056 } else
1057 marginInfo.clearMargin();
1058 marginInfo.setDiscardMargin(childDiscardMargin);
1059
1060 // CSS2.1 states:
1061 // "If the top and bottom margins of an element with clearance are adjoining, its margins collapse with
1062 // the adjoining margins of following siblings but that resulting margin does not collapse with the bottom margin of the parent block."
1063 // So the parent's bottom margin cannot collapse through this block or any subsequent self-collapsing blocks. Check subsequent siblings
1064 // for a block with height - if none is found then don't allow the margins to collapse with the parent.
1065 bool wouldCollapseMarginsWithParent = marginInfo.canCollapseMarginAfterWithChildren();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001066 for (RenderBox* curr = child.nextSiblingBox(); curr && wouldCollapseMarginsWithParent; curr = curr->nextSiblingBox()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001067 if (!curr->isFloatingOrOutOfFlowPositioned() && !curr->isSelfCollapsingBlock())
1068 wouldCollapseMarginsWithParent = false;
1069 }
1070 if (wouldCollapseMarginsWithParent)
1071 marginInfo.setCanCollapseMarginAfterWithChildren(false);
1072
robert@webkit.org97037ef2013-11-20 19:26:10 +00001073 // 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
1074 // its own at the correct vertical position. If subsequent siblings attempt to collapse with |child|'s margins in |collapseMargins| we will
1075 // 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
1076 // margins can collapse at the correct vertical position.
1077 // 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
1078 // (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],
1079 // i.e., clearance = [height of float] - margin-top".
1080 setLogicalHeight(child.logicalTop() + childMargins.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001081 } else
1082 // Increase our height by the amount we had to clear.
1083 setLogicalHeight(logicalHeight() + heightIncrease);
1084
1085 if (marginInfo.canCollapseWithMarginBefore()) {
1086 // We can no longer collapse with the top of the block since a clear
1087 // occurred. The empty blocks collapse into the cleared block.
1088 // FIXME: This isn't quite correct. Need clarification for what to do
1089 // if the height the cleared block is offset by is smaller than the
1090 // margins involved.
1091 setMaxMarginBeforeValues(oldTopPosMargin, oldTopNegMargin);
1092 marginInfo.setAtBeforeSideOfBlock(false);
1093
1094 // 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 +00001095 setMustDiscardMarginBefore(style().marginBeforeCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001096 }
1097
robert@webkit.org97037ef2013-11-20 19:26:10 +00001098 return yPos + heightIncrease;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001099}
1100
weinig@apple.com12840dc2013-10-22 23:59:08 +00001101void RenderBlockFlow::marginBeforeEstimateForChild(RenderBox& child, LayoutUnit& positiveMarginBefore, LayoutUnit& negativeMarginBefore, bool& discardMarginBefore) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001102{
1103 // Give up if in quirks mode and we're a body/table cell and the top margin of the child box is quirky.
1104 // Give up if the child specified -webkit-margin-collapse: separate that prevents collapsing.
1105 // FIXME: Use writing mode independent accessor for marginBeforeCollapse.
akling@apple.com827be9c2013-10-29 02:58:43 +00001106 if ((document().inQuirksMode() && hasMarginAfterQuirk(child) && (isTableCell() || isBody())) || child.style().marginBeforeCollapse() == MSEPARATE)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001107 return;
1108
1109 // The margins are discarded by a child that specified -webkit-margin-collapse: discard.
1110 // FIXME: Use writing mode independent accessor for marginBeforeCollapse.
akling@apple.com827be9c2013-10-29 02:58:43 +00001111 if (child.style().marginBeforeCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001112 positiveMarginBefore = 0;
1113 negativeMarginBefore = 0;
1114 discardMarginBefore = true;
1115 return;
1116 }
1117
1118 LayoutUnit beforeChildMargin = marginBeforeForChild(child);
andersca@apple.com86298632013-11-10 19:32:33 +00001119 positiveMarginBefore = std::max(positiveMarginBefore, beforeChildMargin);
1120 negativeMarginBefore = std::max(negativeMarginBefore, -beforeChildMargin);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001121
weinig@apple.com12840dc2013-10-22 23:59:08 +00001122 if (!child.isRenderBlockFlow())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001123 return;
1124
weinig@apple.com12840dc2013-10-22 23:59:08 +00001125 RenderBlockFlow& childBlock = toRenderBlockFlow(child);
1126 if (childBlock.childrenInline() || childBlock.isWritingModeRoot())
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001127 return;
1128
weinig@apple.com12840dc2013-10-22 23:59:08 +00001129 MarginInfo childMarginInfo(childBlock, childBlock.borderAndPaddingBefore(), childBlock.borderAndPaddingAfter());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001130 if (!childMarginInfo.canCollapseMarginBeforeWithChildren())
1131 return;
1132
weinig@apple.com12840dc2013-10-22 23:59:08 +00001133 RenderBox* grandchildBox = childBlock.firstChildBox();
1134 for (; grandchildBox; grandchildBox = grandchildBox->nextSiblingBox()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001135 if (!grandchildBox->isFloatingOrOutOfFlowPositioned())
1136 break;
1137 }
1138
1139 // 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 +00001140 if (!grandchildBox || grandchildBox->style().clear() != CNONE)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001141 return;
1142
1143 // Make sure to update the block margins now for the grandchild box so that we're looking at current values.
1144 if (grandchildBox->needsLayout()) {
1145 grandchildBox->computeAndSetBlockDirectionMargins(this);
1146 if (grandchildBox->isRenderBlock()) {
1147 RenderBlock* grandchildBlock = toRenderBlock(grandchildBox);
akling@apple.com827be9c2013-10-29 02:58:43 +00001148 grandchildBlock->setHasMarginBeforeQuirk(grandchildBox->style().hasMarginBeforeQuirk());
1149 grandchildBlock->setHasMarginAfterQuirk(grandchildBox->style().hasMarginAfterQuirk());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001150 }
1151 }
1152
1153 // Collapse the margin of the grandchild box with our own to produce an estimate.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001154 childBlock.marginBeforeEstimateForChild(*grandchildBox, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001155}
1156
weinig@apple.com12840dc2013-10-22 23:59:08 +00001157LayoutUnit RenderBlockFlow::estimateLogicalTopPosition(RenderBox& child, const MarginInfo& marginInfo, LayoutUnit& estimateWithoutPagination)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001158{
1159 // FIXME: We need to eliminate the estimation of vertical position, because when it's wrong we sometimes trigger a pathological
1160 // relayout if there are intruding floats.
1161 LayoutUnit logicalTopEstimate = logicalHeight();
1162 if (!marginInfo.canCollapseWithMarginBefore()) {
1163 LayoutUnit positiveMarginBefore = 0;
1164 LayoutUnit negativeMarginBefore = 0;
1165 bool discardMarginBefore = false;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001166 if (child.selfNeedsLayout()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001167 // Try to do a basic estimation of how the collapse is going to go.
1168 marginBeforeEstimateForChild(child, positiveMarginBefore, negativeMarginBefore, discardMarginBefore);
1169 } else {
1170 // Use the cached collapsed margin values from a previous layout. Most of the time they
1171 // will be right.
1172 MarginValues marginValues = marginValuesForChild(child);
andersca@apple.com86298632013-11-10 19:32:33 +00001173 positiveMarginBefore = std::max(positiveMarginBefore, marginValues.positiveMarginBefore());
1174 negativeMarginBefore = std::max(negativeMarginBefore, marginValues.negativeMarginBefore());
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001175 discardMarginBefore = mustDiscardMarginBeforeForChild(child);
1176 }
1177
1178 // Collapse the result with our current margins.
1179 if (!discardMarginBefore)
andersca@apple.com86298632013-11-10 19:32:33 +00001180 logicalTopEstimate += std::max(marginInfo.positiveMargin(), positiveMarginBefore) - std::max(marginInfo.negativeMargin(), negativeMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001181 }
1182
1183 // Adjust logicalTopEstimate down to the next page if the margins are so large that we don't fit on the current
1184 // page.
1185 LayoutState* layoutState = view().layoutState();
1186 if (layoutState->isPaginated() && layoutState->pageLogicalHeight() && logicalTopEstimate > logicalHeight()
1187 && hasNextPage(logicalHeight()))
andersca@apple.com86298632013-11-10 19:32:33 +00001188 logicalTopEstimate = std::min(logicalTopEstimate, nextPageLogicalTop(logicalHeight()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001189
1190 logicalTopEstimate += getClearDelta(child, logicalTopEstimate);
1191
1192 estimateWithoutPagination = logicalTopEstimate;
1193
1194 if (layoutState->isPaginated()) {
1195 // If the object has a page or column break value of "before", then we should shift to the top of the next page.
1196 logicalTopEstimate = applyBeforeBreak(child, logicalTopEstimate);
1197
1198 // For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
1199 logicalTopEstimate = adjustForUnsplittableChild(child, logicalTopEstimate);
1200
weinig@apple.com12840dc2013-10-22 23:59:08 +00001201 if (!child.selfNeedsLayout() && child.isRenderBlock())
1202 logicalTopEstimate += toRenderBlock(child).paginationStrut();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001203 }
1204
1205 return logicalTopEstimate;
1206}
1207
1208void RenderBlockFlow::setCollapsedBottomMargin(const MarginInfo& marginInfo)
1209{
1210 if (marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()) {
1211 // 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.
1212 // Don't update the max margin values because we won't need them anyway.
1213 if (marginInfo.discardMargin()) {
1214 setMustDiscardMarginAfter();
1215 return;
1216 }
1217
1218 // Update our max pos/neg bottom margins, since we collapsed our bottom margins
1219 // with our children.
andersca@apple.com86298632013-11-10 19:32:33 +00001220 setMaxMarginAfterValues(std::max(maxPositiveMarginAfter(), marginInfo.positiveMargin()), std::max(maxNegativeMarginAfter(), marginInfo.negativeMargin()));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001221
1222 if (!marginInfo.hasMarginAfterQuirk())
1223 setHasMarginAfterQuirk(false);
1224
1225 if (marginInfo.hasMarginAfterQuirk() && !marginAfter())
1226 // We have no bottom margin and our last child has a quirky margin.
1227 // We will pick up this quirky margin and pass it through.
1228 // This deals with the <td><div><p> case.
1229 setHasMarginAfterQuirk(true);
1230 }
1231}
1232
1233void RenderBlockFlow::handleAfterSideOfBlock(LayoutUnit beforeSide, LayoutUnit afterSide, MarginInfo& marginInfo)
1234{
1235 marginInfo.setAtAfterSideOfBlock(true);
1236
robert@webkit.org97037ef2013-11-20 19:26:10 +00001237 // If our last child was a self-collapsing block with clearance then our logical height is flush with the
1238 // bottom edge of the float that the child clears. The correct vertical position for the margin-collapsing we want
1239 // to perform now is at the child's margin-top - so adjust our height to that position.
1240 RenderObject* lastBlock = lastChild();
1241 if (lastBlock && lastBlock->isRenderBlockFlow() && toRenderBlockFlow(lastBlock)->isSelfCollapsingBlock())
1242 setLogicalHeight(logicalHeight() - toRenderBlockFlow(lastBlock)->marginOffsetForSelfCollapsingBlock());
1243
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001244 // If we can't collapse with children then go ahead and add in the bottom margin.
1245 if (!marginInfo.discardMargin() && (!marginInfo.canCollapseWithMarginAfter() && !marginInfo.canCollapseWithMarginBefore()
1246 && (!document().inQuirksMode() || !marginInfo.quirkContainer() || !marginInfo.hasMarginAfterQuirk())))
1247 setLogicalHeight(logicalHeight() + marginInfo.margin());
1248
1249 // Now add in our bottom border/padding.
1250 setLogicalHeight(logicalHeight() + afterSide);
1251
1252 // Negative margins can cause our height to shrink below our minimal height (border/padding).
1253 // If this happens, ensure that the computed height is increased to the minimal height.
andersca@apple.com86298632013-11-10 19:32:33 +00001254 setLogicalHeight(std::max(logicalHeight(), beforeSide + afterSide));
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001255
1256 // Update our bottom collapsed margin info.
1257 setCollapsedBottomMargin(marginInfo);
1258}
1259
1260void RenderBlockFlow::setMaxMarginBeforeValues(LayoutUnit pos, LayoutUnit neg)
1261{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001262 if (!hasRareBlockFlowData()) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001263 if (pos == RenderBlockFlowRareData::positiveMarginBeforeDefault(*this) && neg == RenderBlockFlowRareData::negativeMarginBeforeDefault(*this))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001264 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001265 materializeRareBlockFlowData();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001266 }
weinig@apple.com924a77a2013-11-11 00:22:38 +00001267
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001268 rareBlockFlowData()->m_margins.setPositiveMarginBefore(pos);
1269 rareBlockFlowData()->m_margins.setNegativeMarginBefore(neg);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001270}
1271
1272void RenderBlockFlow::setMaxMarginAfterValues(LayoutUnit pos, LayoutUnit neg)
1273{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001274 if (!hasRareBlockFlowData()) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001275 if (pos == RenderBlockFlowRareData::positiveMarginAfterDefault(*this) && neg == RenderBlockFlowRareData::negativeMarginAfterDefault(*this))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001276 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001277 materializeRareBlockFlowData();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001278 }
weinig@apple.com924a77a2013-11-11 00:22:38 +00001279
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001280 rareBlockFlowData()->m_margins.setPositiveMarginAfter(pos);
1281 rareBlockFlowData()->m_margins.setNegativeMarginAfter(neg);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001282}
1283
1284void RenderBlockFlow::setMustDiscardMarginBefore(bool value)
1285{
akling@apple.com827be9c2013-10-29 02:58:43 +00001286 if (style().marginBeforeCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001287 ASSERT(value);
1288 return;
1289 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001290
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001291 if (!hasRareBlockFlowData()) {
weinig@apple.com924a77a2013-11-11 00:22:38 +00001292 if (!value)
1293 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001294 materializeRareBlockFlowData();
weinig@apple.com924a77a2013-11-11 00:22:38 +00001295 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001296
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001297 rareBlockFlowData()->m_discardMarginBefore = value;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001298}
1299
1300void RenderBlockFlow::setMustDiscardMarginAfter(bool value)
1301{
akling@apple.com827be9c2013-10-29 02:58:43 +00001302 if (style().marginAfterCollapse() == MDISCARD) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001303 ASSERT(value);
1304 return;
1305 }
1306
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001307 if (!hasRareBlockFlowData()) {
weinig@apple.com924a77a2013-11-11 00:22:38 +00001308 if (!value)
1309 return;
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001310 materializeRareBlockFlowData();
weinig@apple.com924a77a2013-11-11 00:22:38 +00001311 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001312
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001313 rareBlockFlowData()->m_discardMarginAfter = value;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001314}
1315
1316bool RenderBlockFlow::mustDiscardMarginBefore() const
1317{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001318 return style().marginBeforeCollapse() == MDISCARD || (hasRareBlockFlowData() && rareBlockFlowData()->m_discardMarginBefore);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001319}
1320
1321bool RenderBlockFlow::mustDiscardMarginAfter() const
1322{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001323 return style().marginAfterCollapse() == MDISCARD || (hasRareBlockFlowData() && rareBlockFlowData()->m_discardMarginAfter);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001324}
1325
weinig@apple.com12840dc2013-10-22 23:59:08 +00001326bool RenderBlockFlow::mustDiscardMarginBeforeForChild(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).mustDiscardMarginBefore() : (child.style().marginBeforeCollapse() == 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).mustDiscardMarginAfter() : (child.style().marginAfterCollapse() == MDISCARD);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001333
1334 // FIXME: We return false here because the implementation is not geometrically complete. We have values only for before/after, not start/end.
1335 // In case the boxes are perpendicular we assume the property is not specified.
1336 return false;
1337}
1338
weinig@apple.com12840dc2013-10-22 23:59:08 +00001339bool RenderBlockFlow::mustDiscardMarginAfterForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001340{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001341 ASSERT(!child.selfNeedsLayout());
1342 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001343 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginAfter() : (child.style().marginAfterCollapse() == MDISCARD);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001344 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001345 return child.isRenderBlockFlow() ? toRenderBlockFlow(child).mustDiscardMarginBefore() : (child.style().marginBeforeCollapse() == MDISCARD);
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::mustSeparateMarginBeforeForChild(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.marginBeforeCollapse() == 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.marginAfterCollapse() == 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 +00001364bool RenderBlockFlow::mustSeparateMarginAfterForChild(const RenderBox& child) const
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001365{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001366 ASSERT(!child.selfNeedsLayout());
akling@apple.com827be9c2013-10-29 02:58:43 +00001367 const RenderStyle& childStyle = child.style();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001368 if (!child.isWritingModeRoot())
akling@apple.com827be9c2013-10-29 02:58:43 +00001369 return childStyle.marginAfterCollapse() == MSEPARATE;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001370 if (child.isHorizontalWritingMode() == isHorizontalWritingMode())
akling@apple.com827be9c2013-10-29 02:58:43 +00001371 return childStyle.marginBeforeCollapse() == MSEPARATE;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001372
1373 // FIXME: See |mustDiscardMarginBeforeForChild| above.
1374 return false;
1375}
1376
weinig@apple.com12840dc2013-10-22 23:59:08 +00001377static bool inNormalFlow(RenderBox& child)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001378{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001379 RenderBlock* curr = child.containingBlock();
1380 while (curr && curr != &child.view()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001381 if (curr->hasColumns() || curr->isRenderFlowThread())
1382 return true;
1383 if (curr->isFloatingOrOutOfFlowPositioned())
1384 return false;
1385 curr = curr->containingBlock();
1386 }
1387 return true;
1388}
1389
weinig@apple.com12840dc2013-10-22 23:59:08 +00001390LayoutUnit RenderBlockFlow::applyBeforeBreak(RenderBox& child, LayoutUnit logicalOffset)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001391{
1392 // FIXME: Add page break checking here when we support printing.
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001393 RenderFlowThread* flowThread = flowThreadContainingBlock();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001394 bool isInsideMulticolFlowThread = flowThread && !flowThread->isRenderNamedFlowThread();
1395 bool checkColumnBreaks = isInsideMulticolFlowThread || view().layoutState()->isPaginatingColumns();
1396 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 +00001397 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001398 bool checkBeforeAlways = (checkColumnBreaks && child.style().columnBreakBefore() == PBALWAYS)
1399 || (checkPageBreaks && child.style().pageBreakBefore() == PBALWAYS)
akling@apple.com827be9c2013-10-29 02:58:43 +00001400 || (checkRegionBreaks && child.style().regionBreakBefore() == PBALWAYS);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001401 if (checkBeforeAlways && inNormalFlow(child) && hasNextPage(logicalOffset, IncludePageBoundary)) {
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001402 if (checkColumnBreaks) {
1403 if (isInsideMulticolFlowThread)
1404 checkRegionBreaks = true;
1405 else
1406 view().layoutState()->addForcedColumnBreak(&child, logicalOffset);
1407 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001408 if (checkRegionBreaks) {
1409 LayoutUnit offsetBreakAdjustment = 0;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001410 if (flowThread->addForcedRegionBreak(this, offsetFromLogicalTopOfFirstPage() + logicalOffset, &child, true, &offsetBreakAdjustment))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001411 return logicalOffset + offsetBreakAdjustment;
1412 }
1413 return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
1414 }
1415 return logicalOffset;
1416}
1417
weinig@apple.com12840dc2013-10-22 23:59:08 +00001418LayoutUnit RenderBlockFlow::applyAfterBreak(RenderBox& child, LayoutUnit logicalOffset, MarginInfo& marginInfo)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001419{
1420 // FIXME: Add page break checking here when we support printing.
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001421 RenderFlowThread* flowThread = flowThreadContainingBlock();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001422 bool isInsideMulticolFlowThread = flowThread && !flowThread->isRenderNamedFlowThread();
1423 bool checkColumnBreaks = isInsideMulticolFlowThread || view().layoutState()->isPaginatingColumns();
1424 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 +00001425 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001426 bool checkAfterAlways = (checkColumnBreaks && child.style().columnBreakAfter() == PBALWAYS)
1427 || (checkPageBreaks && child.style().pageBreakAfter() == PBALWAYS)
akling@apple.com827be9c2013-10-29 02:58:43 +00001428 || (checkRegionBreaks && child.style().regionBreakAfter() == PBALWAYS);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001429 if (checkAfterAlways && inNormalFlow(child) && hasNextPage(logicalOffset, IncludePageBoundary)) {
1430 LayoutUnit marginOffset = marginInfo.canCollapseWithMarginBefore() ? LayoutUnit() : marginInfo.margin();
1431
1432 // So our margin doesn't participate in the next collapsing steps.
1433 marginInfo.clearMargin();
1434
commit-queue@webkit.orgfd8f1a22014-01-20 19:55:59 +00001435 if (checkColumnBreaks) {
1436 if (isInsideMulticolFlowThread)
1437 checkRegionBreaks = true;
1438 else
1439 view().layoutState()->addForcedColumnBreak(&child, logicalOffset);
1440 }
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001441 if (checkRegionBreaks) {
1442 LayoutUnit offsetBreakAdjustment = 0;
weinig@apple.com12840dc2013-10-22 23:59:08 +00001443 if (flowThread->addForcedRegionBreak(this, offsetFromLogicalTopOfFirstPage() + logicalOffset + marginOffset, &child, false, &offsetBreakAdjustment))
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001444 return logicalOffset + marginOffset + offsetBreakAdjustment;
1445 }
1446 return nextPageLogicalTop(logicalOffset, IncludePageBoundary);
1447 }
1448 return logicalOffset;
1449}
1450
weinig@apple.com12840dc2013-10-22 23:59:08 +00001451LayoutUnit RenderBlockFlow::adjustBlockChildForPagination(LayoutUnit logicalTopAfterClear, LayoutUnit estimateWithoutPagination, RenderBox& child, bool atBeforeSideOfBlock)
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001452{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001453 RenderBlock* childRenderBlock = child.isRenderBlock() ? toRenderBlock(&child) : nullptr;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001454
1455 if (estimateWithoutPagination != logicalTopAfterClear) {
1456 // Our guess prior to pagination movement was wrong. Before we attempt to paginate, let's try again at the new
1457 // position.
1458 setLogicalHeight(logicalTopAfterClear);
1459 setLogicalTopForChild(child, logicalTopAfterClear, ApplyLayoutDelta);
1460
weinig@apple.com12840dc2013-10-22 23:59:08 +00001461 if (child.shrinkToAvoidFloats()) {
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001462 // The child's width depends on the line width.
1463 // When the child shifts to clear an item, its width can
1464 // change (because it has more available line width).
1465 // So go ahead and mark the item as dirty.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001466 child.setChildNeedsLayout(MarkOnlyThis);
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001467 }
1468
1469 if (childRenderBlock) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00001470 if (!child.avoidsFloats() && childRenderBlock->containsFloats())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001471 toRenderBlockFlow(childRenderBlock)->markAllDescendantsWithFloatsForLayout();
weinig@apple.com12840dc2013-10-22 23:59:08 +00001472 if (!child.needsLayout())
1473 child.markForPaginationRelayoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001474 }
1475
1476 // Our guess was wrong. Make the child lay itself out again.
weinig@apple.com12840dc2013-10-22 23:59:08 +00001477 child.layoutIfNeeded();
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001478 }
1479
1480 LayoutUnit oldTop = logicalTopAfterClear;
1481
1482 // If the object has a page or column break value of "before", then we should shift to the top of the next page.
1483 LayoutUnit result = applyBeforeBreak(child, logicalTopAfterClear);
1484
1485 if (pageLogicalHeightForOffset(result)) {
1486 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(result, ExcludePageBoundary);
weinig@apple.com12840dc2013-10-22 23:59:08 +00001487 LayoutUnit spaceShortage = child.logicalHeight() - remainingLogicalHeight;
hyatt@apple.com1807b5b2013-09-11 19:50:03 +00001488 if (spaceShortage > 0) {
1489 // If the child crosses a column boundary, report a break, in case nothing inside it has already
1490 // done so. The column balancer needs to know how much it has to stretch the columns to make more
1491 // content fit. If no breaks are reported (but do occur), the balancer will have no clue. FIXME:
1492 // This should be improved, though, because here we just pretend that the child is
1493 // unsplittable. A splittable child, on the other hand, has break opportunities at every position
1494 // where there's no child content, border or padding. In other words, we risk stretching more
1495 // than necessary.
1496 setPageBreak(result, spaceShortage);
1497 }
1498 }
1499
1500 // For replaced elements and scrolled elements, we want to shift them to the next page if they don't fit on the current one.
1501 LayoutUnit logicalTopBeforeUnsplittableAdjustment = result;
1502 LayoutUnit logicalTopAfterUnsplittableAdjustment = adjustForUnsplittableChild(child, result);
1503
1504 LayoutUnit paginationStrut = 0;
1505 LayoutUnit unsplittableAdjustmentDelta = logicalTopAfterUnsplittableAdjustment - logicalTopBeforeUnsplittableAdjustment;
1506 if (unsplittableAdjustmentDelta)
1507 paginationStrut = unsplittableAdjustmentDelta;
1508 else if (childRenderBlock && childRenderBlock->paginationStrut())
1509 paginationStrut = childRenderBlock->paginationStrut();
1510
1511 if (paginationStrut) {
1512 // We are willing to propagate out to our parent block as long as we were at the top of the block prior
1513 // to collapsing our margins, and as long as we didn't clear or move as a result of other pagination.
1514 if (atBeforeSideOfBlock && oldTop == result && !isOutOfFlowPositioned() && !isTableCell()) {
1515 // FIXME: Should really check if we're exceeding the page height before propagating the strut, but we don't
1516 // have all the information to do so (the strut only has the remaining amount to push). Gecko gets this wrong too
1517 // and pushes to the next page anyway, so not too concerned about it.
1518 setPaginationStrut(result + paginationStrut);
1519 if (childRenderBlock)
1520 childRenderBlock->setPaginationStrut(0);
1521 } else
1522 result += paginationStrut;
1523 }
1524
1525 // Similar to how we apply clearance. Go ahead and boost height() to be the place where we're going to position the child.
1526 setLogicalHeight(logicalHeight() + (result - oldTop));
1527
1528 // Return the final adjusted logical top.
1529 return result;
1530}
1531
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001532static inline LayoutUnit calculateMinimumPageHeight(RenderStyle* renderStyle, RootInlineBox* lastLine, LayoutUnit lineTop, LayoutUnit lineBottom)
1533{
1534 // We may require a certain minimum number of lines per page in order to satisfy
1535 // orphans and widows, and that may affect the minimum page height.
andersca@apple.com86298632013-11-10 19:32:33 +00001536 unsigned lineCount = std::max<unsigned>(renderStyle->hasAutoOrphans() ? 1 : renderStyle->orphans(), renderStyle->hasAutoWidows() ? 1 : renderStyle->widows());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001537 if (lineCount > 1) {
1538 RootInlineBox* line = lastLine;
1539 for (unsigned i = 1; i < lineCount && line->prevRootBox(); i++)
1540 line = line->prevRootBox();
1541
1542 // FIXME: Paginating using line overflow isn't all fine. See FIXME in
1543 // adjustLinePositionForPagination() for more details.
1544 LayoutRect overflow = line->logicalVisualOverflowRect(line->lineTop(), line->lineBottom());
andersca@apple.com86298632013-11-10 19:32:33 +00001545 lineTop = std::min(line->lineTopWithLeading(), overflow.y());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001546 }
1547 return lineBottom - lineTop;
1548}
1549
1550void RenderBlockFlow::adjustLinePositionForPagination(RootInlineBox* lineBox, LayoutUnit& delta, RenderFlowThread* flowThread)
1551{
1552 // FIXME: For now we paginate using line overflow. This ensures that lines don't overlap at all when we
1553 // put a strut between them for pagination purposes. However, this really isn't the desired rendering, since
1554 // the line on the top of the next page will appear too far down relative to the same kind of line at the top
1555 // of the first column.
1556 //
1557 // The rendering we would like to see is one where the lineTopWithLeading is at the top of the column, and any line overflow
1558 // simply spills out above the top of the column. This effect would match what happens at the top of the first column.
1559 // We can't achieve this rendering, however, until we stop columns from clipping to the column bounds (thus allowing
1560 // for overflow to occur), and then cache visible overflow for each column rect.
1561 //
1562 // Furthermore, the paint we have to do when a column has overflow has to be special. We need to exclude
1563 // content that paints in a previous column (and content that paints in the following column).
1564 //
1565 // For now we'll at least honor the lineTopWithLeading when paginating if it is above the logical top overflow. This will
1566 // at least make positive leading work in typical cases.
1567 //
1568 // FIXME: Another problem with simply moving lines is that the available line width may change (because of floats).
1569 // Technically if the location we move the line to has a different line width than our old position, then we need to dirty the
1570 // line and all following lines.
1571 LayoutRect logicalVisualOverflow = lineBox->logicalVisualOverflowRect(lineBox->lineTop(), lineBox->lineBottom());
andersca@apple.com86298632013-11-10 19:32:33 +00001572 LayoutUnit logicalOffset = std::min(lineBox->lineTopWithLeading(), logicalVisualOverflow.y());
1573 LayoutUnit logicalBottom = std::max(lineBox->lineBottomWithLeading(), logicalVisualOverflow.maxY());
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001574 LayoutUnit lineHeight = logicalBottom - logicalOffset;
akling@apple.com827be9c2013-10-29 02:58:43 +00001575 updateMinimumPageHeight(logicalOffset, calculateMinimumPageHeight(&style(), lineBox, logicalOffset, logicalBottom));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001576 logicalOffset += delta;
1577 lineBox->setPaginationStrut(0);
1578 lineBox->setIsFirstAfterPageBreak(false);
1579 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1580 bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
1581 // If lineHeight is greater than pageLogicalHeight, but logicalVisualOverflow.height() still fits, we are
1582 // still going to add a strut, so that the visible overflow fits on a single page.
1583 if (!pageLogicalHeight || (hasUniformPageLogicalHeight && logicalVisualOverflow.height() > pageLogicalHeight)
1584 || !hasNextPage(logicalOffset))
abucur@adobe.comd40287b2013-10-08 17:33:05 +00001585 // 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.
1586 // 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 +00001587 return;
1588 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
1589
1590 int lineIndex = lineCount(lineBox);
1591 if (remainingLogicalHeight < lineHeight || (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex)) {
abucur@adobe.comfc497132013-10-04 08:49:21 +00001592 if (shouldBreakAtLineToAvoidWidow() && lineBreakToAvoidWidow() == lineIndex) {
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001593 clearShouldBreakAtLineToAvoidWidow();
abucur@adobe.comfc497132013-10-04 08:49:21 +00001594 setDidBreakAtLineToAvoidWidow();
1595 }
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001596 // If we have a non-uniform page height, then we have to shift further possibly.
1597 if (!hasUniformPageLogicalHeight && !pushToNextPageWithMinimumLogicalHeight(remainingLogicalHeight, logicalOffset, lineHeight))
1598 return;
1599 if (lineHeight > pageLogicalHeight) {
1600 // Split the top margin in order to avoid splitting the visible part of the line.
andersca@apple.com86298632013-11-10 19:32:33 +00001601 remainingLogicalHeight -= std::min(lineHeight - pageLogicalHeight, std::max<LayoutUnit>(0, logicalVisualOverflow.y() - lineBox->lineTopWithLeading()));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001602 }
andersca@apple.com86298632013-11-10 19:32:33 +00001603 LayoutUnit totalLogicalHeight = lineHeight + std::max<LayoutUnit>(0, logicalOffset);
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001604 LayoutUnit pageLogicalHeightAtNewOffset = hasUniformPageLogicalHeight ? pageLogicalHeight : pageLogicalHeightForOffset(logicalOffset + remainingLogicalHeight);
1605 setPageBreak(logicalOffset, lineHeight - remainingLogicalHeight);
akling@apple.com827be9c2013-10-29 02:58:43 +00001606 if (((lineBox == firstRootBox() && totalLogicalHeight < pageLogicalHeightAtNewOffset) || (!style().hasAutoOrphans() && style().orphans() >= lineIndex))
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001607 && !isOutOfFlowPositioned() && !isTableCell())
andersca@apple.com86298632013-11-10 19:32:33 +00001608 setPaginationStrut(remainingLogicalHeight + std::max<LayoutUnit>(0, logicalOffset));
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001609 else {
1610 delta += remainingLogicalHeight;
1611 lineBox->setPaginationStrut(remainingLogicalHeight);
1612 lineBox->setIsFirstAfterPageBreak(true);
1613 }
commit-queue@webkit.org883b01c2014-01-20 08:58:36 +00001614 } else if (remainingLogicalHeight == pageLogicalHeight) {
1615 // We're at the very top of a page or column.
1616 if (lineBox != firstRootBox())
1617 lineBox->setIsFirstAfterPageBreak(true);
1618 if (lineBox != firstRootBox() || offsetFromLogicalTopOfFirstPage())
1619 setPageBreak(logicalOffset, lineHeight);
1620 }
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001621}
1622
1623void RenderBlockFlow::setBreakAtLineToAvoidWidow(int lineToBreak)
1624{
abucur@adobe.comfc497132013-10-04 08:49:21 +00001625 ASSERT(lineToBreak >= 0);
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001626 ASSERT(!ensureRareBlockFlowData().m_didBreakAtLineToAvoidWidow);
1627 ensureRareBlockFlowData().m_lineBreakToAvoidWidow = lineToBreak;
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001628}
1629
abucur@adobe.comfc497132013-10-04 08:49:21 +00001630void RenderBlockFlow::setDidBreakAtLineToAvoidWidow()
1631{
1632 ASSERT(!shouldBreakAtLineToAvoidWidow());
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001633 if (!hasRareBlockFlowData())
abucur@adobe.comfc497132013-10-04 08:49:21 +00001634 return;
1635
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001636 rareBlockFlowData()->m_didBreakAtLineToAvoidWidow = true;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001637}
1638
1639void RenderBlockFlow::clearDidBreakAtLineToAvoidWidow()
1640{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001641 if (!hasRareBlockFlowData())
abucur@adobe.comfc497132013-10-04 08:49:21 +00001642 return;
1643
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001644 rareBlockFlowData()->m_didBreakAtLineToAvoidWidow = false;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001645}
1646
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001647void RenderBlockFlow::clearShouldBreakAtLineToAvoidWidow() const
1648{
abucur@adobe.comfc497132013-10-04 08:49:21 +00001649 ASSERT(shouldBreakAtLineToAvoidWidow());
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001650 if (!hasRareBlockFlowData())
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001651 return;
abucur@adobe.comfc497132013-10-04 08:49:21 +00001652
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00001653 rareBlockFlowData()->m_lineBreakToAvoidWidow = -1;
hyatt@apple.com2ea59882013-09-17 16:41:42 +00001654}
1655
1656bool RenderBlockFlow::relayoutToAvoidWidows(LayoutStateMaintainer& statePusher)
1657{
1658 if (!shouldBreakAtLineToAvoidWidow())
1659 return false;
1660
1661 statePusher.pop();
1662 setEverHadLayout(true);
1663 layoutBlock(false);
1664 return true;
1665}
1666
weinig@apple.com31324fd2013-10-28 19:22:51 +00001667bool RenderBlockFlow::hasNextPage(LayoutUnit logicalOffset, PageBoundaryRule pageBoundaryRule) const
1668{
1669 ASSERT(view().layoutState() && view().layoutState()->isPaginated());
1670
1671 RenderFlowThread* flowThread = flowThreadContainingBlock();
1672 if (!flowThread)
1673 return true; // Printing and multi-column both make new pages to accommodate content.
1674
1675 // See if we're in the last region.
1676 LayoutUnit pageOffset = offsetFromLogicalTopOfFirstPage() + logicalOffset;
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00001677 RenderRegion* region = flowThread->regionAtBlockOffset(this, pageOffset, true);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001678 if (!region)
1679 return false;
1680 if (region->isLastRegion())
akling@apple.com827be9c2013-10-29 02:58:43 +00001681 return region->isRenderRegionSet() || region->style().regionFragment() == BreakRegionFragment
weinig@apple.com31324fd2013-10-28 19:22:51 +00001682 || (pageBoundaryRule == IncludePageBoundary && pageOffset == region->logicalTopForFlowThreadContent());
stavila@adobe.com6cb976d2013-11-21 06:57:19 +00001683
1684 RenderRegion* startRegion = 0;
1685 RenderRegion* endRegion = 0;
1686 flowThread->getRegionRangeForBox(this, startRegion, endRegion);
1687
1688 if (region == endRegion)
1689 return false;
weinig@apple.com31324fd2013-10-28 19:22:51 +00001690 return true;
1691}
1692
1693LayoutUnit RenderBlockFlow::adjustForUnsplittableChild(RenderBox& child, LayoutUnit logicalOffset, bool includeMargins)
1694{
1695 bool checkColumnBreaks = view().layoutState()->isPaginatingColumns();
1696 bool checkPageBreaks = !checkColumnBreaks && view().layoutState()->m_pageLogicalHeight;
1697 RenderFlowThread* flowThread = flowThreadContainingBlock();
1698 bool checkRegionBreaks = flowThread && flowThread->isRenderNamedFlowThread();
akling@apple.com827be9c2013-10-29 02:58:43 +00001699 bool isUnsplittable = child.isUnsplittableForPagination() || (checkColumnBreaks && child.style().columnBreakInside() == PBAVOID)
1700 || (checkPageBreaks && child.style().pageBreakInside() == PBAVOID)
1701 || (checkRegionBreaks && child.style().regionBreakInside() == PBAVOID);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001702 if (!isUnsplittable)
1703 return logicalOffset;
1704 LayoutUnit childLogicalHeight = logicalHeightForChild(child) + (includeMargins ? marginBeforeForChild(child) + marginAfterForChild(child) : LayoutUnit());
1705 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1706 bool hasUniformPageLogicalHeight = !flowThread || flowThread->regionsHaveUniformLogicalHeight();
1707 updateMinimumPageHeight(logicalOffset, childLogicalHeight);
1708 if (!pageLogicalHeight || (hasUniformPageLogicalHeight && childLogicalHeight > pageLogicalHeight)
1709 || !hasNextPage(logicalOffset))
1710 return logicalOffset;
1711 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset, ExcludePageBoundary);
1712 if (remainingLogicalHeight < childLogicalHeight) {
1713 if (!hasUniformPageLogicalHeight && !pushToNextPageWithMinimumLogicalHeight(remainingLogicalHeight, logicalOffset, childLogicalHeight))
1714 return logicalOffset;
1715 return logicalOffset + remainingLogicalHeight;
1716 }
1717 return logicalOffset;
1718}
1719
1720bool RenderBlockFlow::pushToNextPageWithMinimumLogicalHeight(LayoutUnit& adjustment, LayoutUnit logicalOffset, LayoutUnit minimumLogicalHeight) const
1721{
1722 bool checkRegion = false;
1723 for (LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset + adjustment); pageLogicalHeight;
1724 pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset + adjustment)) {
1725 if (minimumLogicalHeight <= pageLogicalHeight)
1726 return true;
1727 if (!hasNextPage(logicalOffset + adjustment))
1728 return false;
1729 adjustment += pageLogicalHeight;
1730 checkRegion = true;
1731 }
1732 return !checkRegion;
1733}
1734
1735void RenderBlockFlow::setPageBreak(LayoutUnit offset, LayoutUnit spaceShortage)
1736{
1737 if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1738 flowThread->setPageBreak(this, offsetFromLogicalTopOfFirstPage() + offset, spaceShortage);
1739}
1740
1741void RenderBlockFlow::updateMinimumPageHeight(LayoutUnit offset, LayoutUnit minHeight)
1742{
1743 if (RenderFlowThread* flowThread = flowThreadContainingBlock())
1744 flowThread->updateMinimumPageHeight(this, offsetFromLogicalTopOfFirstPage() + offset, minHeight);
1745 else if (ColumnInfo* colInfo = view().layoutState()->m_columnInfo)
1746 colInfo->updateMinimumColumnHeight(minHeight);
1747}
1748
1749LayoutUnit RenderBlockFlow::nextPageLogicalTop(LayoutUnit logicalOffset, PageBoundaryRule pageBoundaryRule) const
1750{
1751 LayoutUnit pageLogicalHeight = pageLogicalHeightForOffset(logicalOffset);
1752 if (!pageLogicalHeight)
1753 return logicalOffset;
1754
1755 // The logicalOffset is in our coordinate space. We can add in our pushed offset.
1756 LayoutUnit remainingLogicalHeight = pageRemainingLogicalHeightForOffset(logicalOffset);
1757 if (pageBoundaryRule == ExcludePageBoundary)
1758 return logicalOffset + (remainingLogicalHeight ? remainingLogicalHeight : pageLogicalHeight);
1759 return logicalOffset + remainingLogicalHeight;
1760}
1761
1762LayoutUnit RenderBlockFlow::pageLogicalTopForOffset(LayoutUnit offset) const
1763{
1764 LayoutUnit firstPageLogicalTop = isHorizontalWritingMode() ? view().layoutState()->m_pageOffset.height() : view().layoutState()->m_pageOffset.width();
1765 LayoutUnit blockLogicalTop = isHorizontalWritingMode() ? view().layoutState()->m_layoutOffset.height() : view().layoutState()->m_layoutOffset.width();
1766
1767 LayoutUnit cumulativeOffset = offset + blockLogicalTop;
1768 RenderFlowThread* flowThread = flowThreadContainingBlock();
1769 if (!flowThread) {
1770 LayoutUnit pageLogicalHeight = view().layoutState()->pageLogicalHeight();
1771 if (!pageLogicalHeight)
1772 return 0;
1773 return cumulativeOffset - roundToInt(cumulativeOffset - firstPageLogicalTop) % roundToInt(pageLogicalHeight);
1774 }
hyatt@apple.com150e7f22014-02-11 16:51:45 +00001775 return firstPageLogicalTop + flowThread->pageLogicalTopForOffset(cumulativeOffset - firstPageLogicalTop);
weinig@apple.com31324fd2013-10-28 19:22:51 +00001776}
1777
1778LayoutUnit RenderBlockFlow::pageLogicalHeightForOffset(LayoutUnit offset) const
1779{
1780 RenderFlowThread* flowThread = flowThreadContainingBlock();
1781 if (!flowThread)
1782 return view().layoutState()->m_pageLogicalHeight;
1783 return flowThread->pageLogicalHeightForOffset(offset + offsetFromLogicalTopOfFirstPage());
1784}
1785
1786LayoutUnit RenderBlockFlow::pageRemainingLogicalHeightForOffset(LayoutUnit offset, PageBoundaryRule pageBoundaryRule) const
1787{
1788 offset += offsetFromLogicalTopOfFirstPage();
1789
1790 RenderFlowThread* flowThread = flowThreadContainingBlock();
1791 if (!flowThread) {
1792 LayoutUnit pageLogicalHeight = view().layoutState()->m_pageLogicalHeight;
1793 LayoutUnit remainingHeight = pageLogicalHeight - intMod(offset, pageLogicalHeight);
1794 if (pageBoundaryRule == IncludePageBoundary) {
1795 // If includeBoundaryPoint is true the line exactly on the top edge of a
1796 // column will act as being part of the previous column.
1797 remainingHeight = intMod(remainingHeight, pageLogicalHeight);
1798 }
1799 return remainingHeight;
1800 }
1801
1802 return flowThread->pageRemainingLogicalHeightForOffset(offset, pageBoundaryRule);
1803}
1804
1805
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001806void RenderBlockFlow::layoutLineGridBox()
1807{
akling@apple.com827be9c2013-10-29 02:58:43 +00001808 if (style().lineGrid() == RenderStyle::initialLineGrid()) {
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001809 setLineGridBox(0);
1810 return;
1811 }
1812
1813 setLineGridBox(0);
1814
akling@apple.com1aa97b02013-10-31 21:59:49 +00001815 auto lineGridBox = std::make_unique<RootInlineBox>(*this);
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001816 lineGridBox->setHasTextChildren(); // Needed to make the line ascent/descent actually be honored in quirks mode.
1817 lineGridBox->setConstructed();
1818 GlyphOverflowAndFallbackFontsMap textBoxDataMap;
1819 VerticalPositionCache verticalPositionCache;
1820 lineGridBox->alignBoxesInBlockDirection(logicalHeight(), textBoxDataMap, verticalPositionCache);
1821
akling@apple.com1aa97b02013-10-31 21:59:49 +00001822 setLineGridBox(std::move(lineGridBox));
1823
hyatt@apple.com3cd5c772013-09-27 18:22:50 +00001824 // FIXME: If any of the characteristics of the box change compared to the old one, then we need to do a deep dirtying
1825 // (similar to what happens when the page height changes). Ideally, though, we only do this if someone is actually snapping
1826 // to this grid.
1827}
1828
weinig@apple.com12840dc2013-10-22 23:59:08 +00001829bool RenderBlockFlow::containsFloat(RenderBox& renderer) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001830{
weinig@apple.com12840dc2013-10-22 23:59:08 +00001831 return m_floatingObjects && m_floatingObjects->set().contains<RenderBox&, FloatingObjectHashTranslator>(renderer);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001832}
1833
1834void RenderBlockFlow::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
1835{
1836 RenderBlock::styleDidChange(diff, oldStyle);
1837
1838 // After our style changed, if we lose our ability to propagate floats into next sibling
1839 // blocks, then we need to find the top most parent containing that overhanging float and
1840 // then mark its descendants with floats for layout and clear all floats from its next
1841 // sibling blocks that exist in our floating objects list. See bug 56299 and 62875.
1842 bool canPropagateFloatIntoSibling = !isFloatingOrOutOfFlowPositioned() && !avoidsFloats();
1843 if (diff == StyleDifferenceLayout && s_canPropagateFloatIntoSibling && !canPropagateFloatIntoSibling && hasOverhangingFloats()) {
1844 RenderBlockFlow* parentBlock = this;
1845 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001846
weinig@apple.comc77041e2013-12-14 18:05:45 +00001847 for (auto& ancestor : ancestorsOfType<RenderBlockFlow>(*this)) {
1848 if (ancestor.isRenderView())
akling@apple.comf3028052013-11-04 08:46:06 +00001849 break;
weinig@apple.comc77041e2013-12-14 18:05:45 +00001850 if (ancestor.hasOverhangingFloats()) {
akling@apple.comf3028052013-11-04 08:46:06 +00001851 for (auto it = floatingObjectSet.begin(), end = floatingObjectSet.end(); it != end; ++it) {
1852 RenderBox& renderer = (*it)->renderer();
weinig@apple.comc77041e2013-12-14 18:05:45 +00001853 if (ancestor.hasOverhangingFloat(renderer)) {
1854 parentBlock = &ancestor;
akling@apple.comf3028052013-11-04 08:46:06 +00001855 break;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001856 }
1857 }
1858 }
1859 }
1860
1861 parentBlock->markAllDescendantsWithFloatsForLayout();
1862 parentBlock->markSiblingsWithFloatsForLayout();
1863 }
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00001864
akling@apple.com8f40c5b2013-10-27 22:54:07 +00001865 if (auto fragment = renderNamedFlowFragment())
akling@apple.com827be9c2013-10-29 02:58:43 +00001866 fragment->setStyle(RenderNamedFlowFragment::createStyle(style()));
antti@apple.com42fb53d2013-10-25 02:33:11 +00001867
1868 if (diff >= StyleDifferenceRepaint)
1869 invalidateLineLayoutPath();
hyatt@apple.comd4be3772014-01-24 19:55:33 +00001870
1871 if (multiColumnFlowThread()) {
1872 for (RenderBox* child = firstChildBox(); child; child = child->nextSiblingBox())
1873 child->setStyle(RenderStyle::createAnonymousStyleWithDisplay(&style(), BLOCK));
1874 }
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001875}
1876
akling@apple.combdae43242013-10-25 12:00:20 +00001877void RenderBlockFlow::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001878{
akling@apple.com827be9c2013-10-29 02:58:43 +00001879 const RenderStyle* oldStyle = hasInitializedStyle() ? &style() : nullptr;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001880 s_canPropagateFloatIntoSibling = oldStyle ? !isFloatingOrOutOfFlowPositioned() && !avoidsFloats() : false;
1881
akling@apple.combdae43242013-10-25 12:00:20 +00001882 if (oldStyle && parent() && diff == StyleDifferenceLayout && oldStyle->position() != newStyle.position()) {
1883 if (containsFloats() && !isFloating() && !isOutOfFlowPositioned() && newStyle.hasOutOfFlowPosition())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001884 markAllDescendantsWithFloatsForLayout();
1885 }
1886
1887 RenderBlock::styleWillChange(diff, newStyle);
1888}
1889
antti@apple.coma2c7f242013-10-22 22:37:25 +00001890void RenderBlockFlow::deleteLines()
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001891{
1892 if (containsFloats())
1893 m_floatingObjects->clearLineBoxTreePointers();
weinig@apple.com611b9292013-10-20 22:57:54 +00001894
antti@apple.comfea51992013-10-28 13:39:23 +00001895 if (m_simpleLineLayout) {
antti@apple.com940f5872013-10-24 20:31:11 +00001896 ASSERT(!m_lineBoxes.firstLineBox());
antti@apple.comfea51992013-10-28 13:39:23 +00001897 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +00001898 } else
akling@apple.com31dd4f42013-10-30 22:27:59 +00001899 m_lineBoxes.deleteLineBoxTree();
weinig@apple.com611b9292013-10-20 22:57:54 +00001900
antti@apple.coma2c7f242013-10-22 22:37:25 +00001901 RenderBlock::deleteLines();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001902}
1903
1904void RenderBlockFlow::moveAllChildrenIncludingFloatsTo(RenderBlock* toBlock, bool fullRemoveInsert)
1905{
1906 RenderBlockFlow* toBlockFlow = toRenderBlockFlow(toBlock);
1907 moveAllChildrenTo(toBlockFlow, fullRemoveInsert);
1908
1909 // When a portion of the render tree is being detached, anonymous blocks
1910 // will be combined as their children are deleted. In this process, the
1911 // anonymous block later in the tree is merged into the one preceeding it.
1912 // It can happen that the later block (this) contains floats that the
1913 // previous block (toBlockFlow) did not contain, and thus are not in the
1914 // floating objects list for toBlockFlow. This can result in toBlockFlow
1915 // containing floats that are not in it's floating objects list, but are in
1916 // the floating objects lists of siblings and parents. This can cause
1917 // problems when the float itself is deleted, since the deletion code
1918 // assumes that if a float is not in it's containing block's floating
1919 // objects list, it isn't in any floating objects list. In order to
1920 // preserve this condition (removing it has serious performance
1921 // implications), we need to copy the floating objects from the old block
1922 // (this) to the new block (toBlockFlow). The float's metrics will likely
1923 // all be wrong, but since toBlockFlow is already marked for layout, this
1924 // will get fixed before anything gets displayed.
1925 // See bug https://bugs.webkit.org/show_bug.cgi?id=115566
1926 if (m_floatingObjects) {
1927 if (!toBlockFlow->m_floatingObjects)
1928 toBlockFlow->createFloatingObjects();
1929
1930 const FloatingObjectSet& fromFloatingObjectSet = m_floatingObjects->set();
1931 auto end = fromFloatingObjectSet.end();
1932
1933 for (auto it = fromFloatingObjectSet.begin(); it != end; ++it) {
1934 FloatingObject* floatingObject = it->get();
1935
1936 // Don't insert the object again if it's already in the list
weinig@apple.com12840dc2013-10-22 23:59:08 +00001937 if (toBlockFlow->containsFloat(floatingObject->renderer()))
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001938 continue;
1939
1940 toBlockFlow->m_floatingObjects->add(floatingObject->unsafeClone());
1941 }
1942 }
1943}
1944
1945void RenderBlockFlow::addOverflowFromFloats()
1946{
1947 if (!m_floatingObjects)
1948 return;
1949
1950 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1951 auto end = floatingObjectSet.end();
1952 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
1953 FloatingObject* r = it->get();
1954 if (r->isDescendant())
1955 addOverflowFromChild(&r->renderer(), IntSize(xPositionForFloatIncludingMargin(r), yPositionForFloatIncludingMargin(r)));
1956 }
1957}
1958
1959void RenderBlockFlow::computeOverflow(LayoutUnit oldClientAfterEdge, bool recomputeFloats)
1960{
1961 RenderBlock::computeOverflow(oldClientAfterEdge, recomputeFloats);
1962
1963 if (!hasColumns() && (recomputeFloats || isRoot() || expandsToEncloseOverhangingFloats() || hasSelfPaintingLayer()))
1964 addOverflowFromFloats();
1965}
1966
1967void RenderBlockFlow::repaintOverhangingFloats(bool paintAllDescendants)
1968{
1969 // Repaint any overhanging floats (if we know we're the one to paint them).
1970 // Otherwise, bail out.
1971 if (!hasOverhangingFloats())
1972 return;
1973
1974 // FIXME: Avoid disabling LayoutState. At the very least, don't disable it for floats originating
1975 // in this block. Better yet would be to push extra state for the containers of other floats.
1976 LayoutStateDisabler layoutStateDisabler(&view());
1977 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1978 auto end = floatingObjectSet.end();
1979 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
1980 FloatingObject* floatingObject = it->get();
1981 // Only repaint the object if it is overhanging, is not in its own layer, and
1982 // is our responsibility to paint (m_shouldPaint is set). When paintAllDescendants is true, the latter
1983 // condition is replaced with being a descendant of us.
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00001984 if (logicalBottomForFloat(floatingObject) > logicalHeight()
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00001985 && !floatingObject->renderer().hasSelfPaintingLayer()
1986 && (floatingObject->shouldPaint() || (paintAllDescendants && floatingObject->renderer().isDescendantOf(this)))) {
1987 floatingObject->renderer().repaint();
1988 floatingObject->renderer().repaintOverhangingFloats(false);
1989 }
1990 }
1991}
1992
1993void RenderBlockFlow::paintFloats(PaintInfo& paintInfo, const LayoutPoint& paintOffset, bool preservePhase)
1994{
1995 if (!m_floatingObjects)
1996 return;
1997
1998 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
1999 auto end = floatingObjectSet.end();
2000 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2001 FloatingObject* r = it->get();
2002 // Only paint the object if our m_shouldPaint flag is set.
2003 if (r->shouldPaint() && !r->renderer().hasSelfPaintingLayer()) {
2004 PaintInfo currentPaintInfo(paintInfo);
2005 currentPaintInfo.phase = preservePhase ? paintInfo.phase : PaintPhaseBlockBackground;
2006 // FIXME: LayoutPoint version of xPositionForFloatIncludingMargin would make this much cleaner.
2007 LayoutPoint childPoint = flipFloatForWritingModeForChild(r, LayoutPoint(paintOffset.x() + xPositionForFloatIncludingMargin(r) - r->renderer().x(), paintOffset.y() + yPositionForFloatIncludingMargin(r) - r->renderer().y()));
2008 r->renderer().paint(currentPaintInfo, childPoint);
2009 if (!preservePhase) {
2010 currentPaintInfo.phase = PaintPhaseChildBlockBackgrounds;
2011 r->renderer().paint(currentPaintInfo, childPoint);
2012 currentPaintInfo.phase = PaintPhaseFloat;
2013 r->renderer().paint(currentPaintInfo, childPoint);
2014 currentPaintInfo.phase = PaintPhaseForeground;
2015 r->renderer().paint(currentPaintInfo, childPoint);
2016 currentPaintInfo.phase = PaintPhaseOutline;
2017 r->renderer().paint(currentPaintInfo, childPoint);
2018 }
2019 }
2020 }
2021}
2022
weinig@apple.com12840dc2013-10-22 23:59:08 +00002023void RenderBlockFlow::clipOutFloatingObjects(RenderBlock& rootBlock, const PaintInfo* paintInfo, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002024{
2025 if (m_floatingObjects) {
2026 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2027 auto end = floatingObjectSet.end();
2028 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2029 FloatingObject* floatingObject = it->get();
2030 LayoutRect floatBox(offsetFromRootBlock.width() + xPositionForFloatIncludingMargin(floatingObject),
2031 offsetFromRootBlock.height() + yPositionForFloatIncludingMargin(floatingObject),
2032 floatingObject->renderer().width(), floatingObject->renderer().height());
weinig@apple.com12840dc2013-10-22 23:59:08 +00002033 rootBlock.flipForWritingMode(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002034 floatBox.move(rootBlockPhysicalPosition.x(), rootBlockPhysicalPosition.y());
2035 paintInfo->context->clipOut(pixelSnappedIntRect(floatBox));
2036 }
2037 }
2038}
2039
2040void RenderBlockFlow::createFloatingObjects()
2041{
bjonesbe@adobe.com2e94fff2013-11-08 23:24:40 +00002042 m_floatingObjects = adoptPtr(new FloatingObjects(*this));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002043}
2044
2045void RenderBlockFlow::removeFloatingObjects()
2046{
2047 if (!m_floatingObjects)
2048 return;
2049
2050 m_floatingObjects->clear();
2051}
2052
weinig@apple.com12840dc2013-10-22 23:59:08 +00002053FloatingObject* RenderBlockFlow::insertFloatingObject(RenderBox& floatBox)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002054{
weinig@apple.com12840dc2013-10-22 23:59:08 +00002055 ASSERT(floatBox.isFloating());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002056
2057 // Create the list of special objects if we don't aleady have one
2058 if (!m_floatingObjects)
2059 createFloatingObjects();
2060 else {
2061 // Don't insert the floatingObject again if it's already in the list
2062 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002063 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002064 if (it != floatingObjectSet.end())
2065 return it->get();
2066 }
2067
2068 // Create the special floatingObject entry & append it to the list
2069
weinig@apple.com12840dc2013-10-22 23:59:08 +00002070 std::unique_ptr<FloatingObject> floatingObject = FloatingObject::create(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002071
2072 // Our location is irrelevant if we're unsplittable or no pagination is in effect.
2073 // Just go ahead and lay out the float.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002074 bool isChildRenderBlock = floatBox.isRenderBlock();
2075 if (isChildRenderBlock && !floatBox.needsLayout() && view().layoutState()->pageLogicalHeightChanged())
2076 floatBox.setChildNeedsLayout(MarkOnlyThis);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002077
2078 bool needsBlockDirectionLocationSetBeforeLayout = isChildRenderBlock && view().layoutState()->needsBlockDirectionLocationSetBeforeLayout();
2079 if (!needsBlockDirectionLocationSetBeforeLayout || isWritingModeRoot()) // We are unsplittable if we're a block flow root.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002080 floatBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002081 else {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002082 floatBox.updateLogicalWidth();
2083 floatBox.computeAndSetBlockDirectionMargins(this);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002084 }
2085
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002086 setLogicalWidthForFloat(floatingObject.get(), logicalWidthForChild(floatBox) + marginStartForChild(floatBox) + marginEndForChild(floatBox));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002087
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002088 return m_floatingObjects->add(std::move(floatingObject));
2089}
2090
weinig@apple.com12840dc2013-10-22 23:59:08 +00002091void RenderBlockFlow::removeFloatingObject(RenderBox& floatBox)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002092{
2093 if (m_floatingObjects) {
2094 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002095 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(floatBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002096 if (it != floatingObjectSet.end()) {
2097 FloatingObject* floatingObject = it->get();
2098 if (childrenInline()) {
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002099 LayoutUnit logicalTop = logicalTopForFloat(floatingObject);
2100 LayoutUnit logicalBottom = logicalBottomForFloat(floatingObject);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002101
2102 // Fix for https://bugs.webkit.org/show_bug.cgi?id=54995.
2103 if (logicalBottom < 0 || logicalBottom < logicalTop || logicalTop == LayoutUnit::max())
2104 logicalBottom = LayoutUnit::max();
2105 else {
2106 // Special-case zero- and less-than-zero-height floats: those don't touch
2107 // the line that they're on, but it still needs to be dirtied. This is
2108 // accomplished by pretending they have a height of 1.
andersca@apple.com86298632013-11-10 19:32:33 +00002109 logicalBottom = std::max(logicalBottom, logicalTop + 1);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002110 }
2111 if (floatingObject->originatingLine()) {
2112 if (!selfNeedsLayout()) {
2113 ASSERT(&floatingObject->originatingLine()->renderer() == this);
2114 floatingObject->originatingLine()->markDirty();
2115 }
2116#if !ASSERT_DISABLED
2117 floatingObject->setOriginatingLine(0);
2118#endif
2119 }
2120 markLinesDirtyInBlockRange(0, logicalBottom);
2121 }
2122 m_floatingObjects->remove(floatingObject);
2123 }
2124 }
2125}
2126
2127void RenderBlockFlow::removeFloatingObjectsBelow(FloatingObject* lastFloat, int logicalOffset)
2128{
2129 if (!containsFloats())
2130 return;
2131
2132 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2133 FloatingObject* curr = floatingObjectSet.last().get();
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002134 while (curr != lastFloat && (!curr->isPlaced() || logicalTopForFloat(curr) >= logicalOffset)) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002135 m_floatingObjects->remove(curr);
2136 if (floatingObjectSet.isEmpty())
2137 break;
2138 curr = floatingObjectSet.last().get();
2139 }
2140}
2141
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002142LayoutUnit RenderBlockFlow::logicalLeftOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
2143{
2144 LayoutUnit offset = fixedOffset;
2145 if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
2146 offset = m_floatingObjects->logicalLeftOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
2147 return adjustLogicalLeftOffsetForLine(offset, applyTextIndent);
2148}
2149
2150LayoutUnit RenderBlockFlow::logicalRightOffsetForPositioningFloat(LayoutUnit logicalTop, LayoutUnit fixedOffset, bool applyTextIndent, LayoutUnit* heightRemaining) const
2151{
2152 LayoutUnit offset = fixedOffset;
2153 if (m_floatingObjects && m_floatingObjects->hasRightObjects())
2154 offset = m_floatingObjects->logicalRightOffsetForPositioningFloat(fixedOffset, logicalTop, heightRemaining);
2155 return adjustLogicalRightOffsetForLine(offset, applyTextIndent);
2156}
2157
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002158LayoutPoint RenderBlockFlow::computeLogicalLocationForFloat(const FloatingObject* floatingObject, LayoutUnit logicalTopOffset) const
2159{
weinig@apple.com12840dc2013-10-22 23:59:08 +00002160 RenderBox& childBox = floatingObject->renderer();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002161 LayoutUnit logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
2162 LayoutUnit logicalRightOffset; // Constant part of right offset.
dino@apple.comc133a8c2014-02-03 23:35:34 +00002163#if ENABLE(CSS_SHAPES) && ENABLE(CSS_SHAPE_INSIDE)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002164 // FIXME Bug 102948: This only works for shape outside directly set on this block.
2165 ShapeInsideInfo* shapeInsideInfo = this->layoutShapeInsideInfo();
2166 // FIXME: Implement behavior for right floats.
2167 if (shapeInsideInfo) {
zoltan@webkit.orgb64932a2013-10-11 21:01:40 +00002168 LayoutSize floatLogicalSize = logicalSizeForFloat(floatingObject);
2169 // floatingObject's logicalSize doesn't contain the actual height at this point, so we need to calculate it
2170 floatLogicalSize.setHeight(logicalHeightForChild(childBox) + marginBeforeForChild(childBox) + marginAfterForChild(childBox));
2171
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002172 // FIXME: If the float doesn't fit in the shape we should push it under the content box
2173 logicalTopOffset = shapeInsideInfo->computeFirstFitPositionForFloat(floatLogicalSize);
2174 if (logicalHeight() > logicalTopOffset)
2175 logicalTopOffset = logicalHeight();
2176
zoltan@webkit.orgb64932a2013-10-11 21:01:40 +00002177 SegmentList segments = shapeInsideInfo->computeSegmentsForLine(logicalTopOffset, floatLogicalSize.height());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002178 // FIXME Bug 102949: Add support for shapes with multiple segments.
2179 if (segments.size() == 1) {
2180 // The segment offsets are relative to the content box.
2181 logicalRightOffset = logicalLeftOffset + segments[0].logicalRight;
2182 logicalLeftOffset += segments[0].logicalLeft;
2183 }
2184 } else
2185#endif
2186 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset);
2187
andersca@apple.com86298632013-11-10 19:32:33 +00002188 LayoutUnit floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset); // The width we look for.
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002189
2190 LayoutUnit floatLogicalLeft;
2191
2192 bool insideFlowThread = flowThreadContainingBlock();
2193
akling@apple.com827be9c2013-10-29 02:58:43 +00002194 if (childBox.style().floating() == LeftFloat) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002195 LayoutUnit heightRemainingLeft = 1;
2196 LayoutUnit heightRemainingRight = 1;
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002197 floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
2198 while (logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight) - floatLogicalLeft < floatLogicalWidth) {
andersca@apple.com86298632013-11-10 19:32:33 +00002199 logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002200 floatLogicalLeft = logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002201 if (insideFlowThread) {
2202 // Have to re-evaluate all of our offsets, since they may have changed.
2203 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
2204 logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
andersca@apple.com86298632013-11-10 19:32:33 +00002205 floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002206 }
2207 }
andersca@apple.com86298632013-11-10 19:32:33 +00002208 floatLogicalLeft = std::max(logicalLeftOffset - borderAndPaddingLogicalLeft(), floatLogicalLeft);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002209 } else {
2210 LayoutUnit heightRemainingLeft = 1;
2211 LayoutUnit heightRemainingRight = 1;
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002212 floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
2213 while (floatLogicalLeft - logicalLeftOffsetForPositioningFloat(logicalTopOffset, logicalLeftOffset, false, &heightRemainingLeft) < floatLogicalWidth) {
andersca@apple.com86298632013-11-10 19:32:33 +00002214 logicalTopOffset += std::min(heightRemainingLeft, heightRemainingRight);
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002215 floatLogicalLeft = logicalRightOffsetForPositioningFloat(logicalTopOffset, logicalRightOffset, false, &heightRemainingRight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002216 if (insideFlowThread) {
2217 // Have to re-evaluate all of our offsets, since they may have changed.
2218 logicalRightOffset = logicalRightOffsetForContent(logicalTopOffset); // Constant part of right offset.
2219 logicalLeftOffset = logicalLeftOffsetForContent(logicalTopOffset); // Constant part of left offset.
andersca@apple.com86298632013-11-10 19:32:33 +00002220 floatLogicalWidth = std::min(logicalWidthForFloat(floatingObject), logicalRightOffset - logicalLeftOffset);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002221 }
2222 }
2223 // Use the original width of the float here, since the local variable
2224 // |floatLogicalWidth| was capped to the available line width. See
2225 // fast/block/float/clamped-right-float.html.
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002226 floatLogicalLeft -= logicalWidthForFloat(floatingObject);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002227 }
2228
2229 return LayoutPoint(floatLogicalLeft, logicalTopOffset);
2230}
2231
2232bool RenderBlockFlow::positionNewFloats()
2233{
2234 if (!m_floatingObjects)
2235 return false;
2236
2237 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2238 if (floatingObjectSet.isEmpty())
2239 return false;
2240
2241 // If all floats have already been positioned, then we have no work to do.
2242 if (floatingObjectSet.last()->isPlaced())
2243 return false;
2244
2245 // Move backwards through our floating object list until we find a float that has
2246 // already been positioned. Then we'll be able to move forward, positioning all of
2247 // the new floats that need it.
2248 auto it = floatingObjectSet.end();
2249 --it; // Go to last item.
2250 auto begin = floatingObjectSet.begin();
2251 FloatingObject* lastPlacedFloatingObject = 0;
2252 while (it != begin) {
2253 --it;
2254 if ((*it)->isPlaced()) {
2255 lastPlacedFloatingObject = it->get();
2256 ++it;
2257 break;
2258 }
2259 }
2260
2261 LayoutUnit logicalTop = logicalHeight();
2262
2263 // The float cannot start above the top position of the last positioned float.
2264 if (lastPlacedFloatingObject)
andersca@apple.com86298632013-11-10 19:32:33 +00002265 logicalTop = std::max(logicalTopForFloat(lastPlacedFloatingObject), logicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002266
2267 auto end = floatingObjectSet.end();
2268 // Now walk through the set of unpositioned floats and place them.
2269 for (; it != end; ++it) {
2270 FloatingObject* floatingObject = it->get();
2271 // The containing block is responsible for positioning floats, so if we have floats in our
2272 // list that come from somewhere else, do not attempt to position them.
2273 if (floatingObject->renderer().containingBlock() != this)
2274 continue;
2275
weinig@apple.com12840dc2013-10-22 23:59:08 +00002276 RenderBox& childBox = floatingObject->renderer();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002277
akling@apple.com827be9c2013-10-29 02:58:43 +00002278 LayoutUnit childLogicalLeftMargin = style().isLeftToRightDirection() ? marginStartForChild(childBox) : marginEndForChild(childBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002279
weinig@apple.com12840dc2013-10-22 23:59:08 +00002280 LayoutRect oldRect = childBox.frameRect();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002281
akling@apple.com827be9c2013-10-29 02:58:43 +00002282 if (childBox.style().clear() & CLEFT)
andersca@apple.com86298632013-11-10 19:32:33 +00002283 logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatLeft), logicalTop);
akling@apple.com827be9c2013-10-29 02:58:43 +00002284 if (childBox.style().clear() & CRIGHT)
andersca@apple.com86298632013-11-10 19:32:33 +00002285 logicalTop = std::max(lowestFloatLogicalBottom(FloatingObject::FloatRight), logicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002286
2287 LayoutPoint floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, logicalTop);
2288
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002289 setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002290
2291 setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
2292 setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
2293
2294 estimateRegionRangeForBoxChild(childBox);
2295
2296 LayoutState* layoutState = view().layoutState();
2297 bool isPaginated = layoutState->isPaginated();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002298 if (isPaginated && !childBox.needsLayout())
2299 childBox.markForPaginationRelayoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002300
weinig@apple.com12840dc2013-10-22 23:59:08 +00002301 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002302
2303 if (isPaginated) {
2304 // If we are unsplittable and don't fit, then we need to move down.
2305 // We include our margins as part of the unsplittable area.
2306 LayoutUnit newLogicalTop = adjustForUnsplittableChild(childBox, floatLogicalLocation.y(), true);
2307
2308 // See if we have a pagination strut that is making us move down further.
2309 // Note that an unsplittable child can't also have a pagination strut, so this is
2310 // exclusive with the case above.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002311 RenderBlock* childBlock = childBox.isRenderBlock() ? toRenderBlock(&childBox) : nullptr;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002312 if (childBlock && childBlock->paginationStrut()) {
2313 newLogicalTop += childBlock->paginationStrut();
2314 childBlock->setPaginationStrut(0);
2315 }
2316
2317 if (newLogicalTop != floatLogicalLocation.y()) {
2318 floatingObject->setPaginationStrut(newLogicalTop - floatLogicalLocation.y());
2319
2320 floatLogicalLocation = computeLogicalLocationForFloat(floatingObject, newLogicalTop);
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002321 setLogicalLeftForFloat(floatingObject, floatLogicalLocation.x());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002322
2323 setLogicalLeftForChild(childBox, floatLogicalLocation.x() + childLogicalLeftMargin);
2324 setLogicalTopForChild(childBox, floatLogicalLocation.y() + marginBeforeForChild(childBox));
2325
2326 if (childBlock)
2327 childBlock->setChildNeedsLayout(MarkOnlyThis);
weinig@apple.com12840dc2013-10-22 23:59:08 +00002328 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002329 }
2330
2331 if (updateRegionRangeForBoxChild(childBox)) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002332 childBox.setNeedsLayout(MarkOnlyThis);
2333 childBox.layoutIfNeeded();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002334 }
2335 }
2336
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002337 setLogicalTopForFloat(floatingObject, floatLogicalLocation.y());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002338
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002339 setLogicalHeightForFloat(floatingObject, logicalHeightForChild(childBox) + marginBeforeForChild(childBox) + marginAfterForChild(childBox));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002340
2341 m_floatingObjects->addPlacedObject(floatingObject);
2342
zoltan@webkit.org0faf5722013-11-05 02:34:16 +00002343#if ENABLE(CSS_SHAPES)
2344 if (ShapeOutsideInfo* shapeOutside = childBox.shapeOutsideInfo())
2345 shapeOutside->setShapeSize(logicalWidthForChild(childBox), logicalHeightForChild(childBox));
2346#endif
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002347 // If the child moved, we have to repaint it.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002348 if (childBox.checkForRepaintDuringLayout())
2349 childBox.repaintDuringLayoutIfMoved(oldRect);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002350 }
2351 return true;
2352}
2353
2354void RenderBlockFlow::newLine(EClear clear)
2355{
2356 positionNewFloats();
2357 // set y position
2358 LayoutUnit newY = 0;
2359 switch (clear) {
2360 case CLEFT:
2361 newY = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
2362 break;
2363 case CRIGHT:
2364 newY = lowestFloatLogicalBottom(FloatingObject::FloatRight);
2365 break;
2366 case CBOTH:
2367 newY = lowestFloatLogicalBottom();
joepeck@webkit.orgaa676ee52014-01-28 04:04:52 +00002368 break;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002369 default:
2370 break;
2371 }
2372 if (height() < newY)
2373 setLogicalHeight(newY);
2374}
2375
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002376LayoutUnit RenderBlockFlow::logicalLeftFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002377{
2378 if (m_floatingObjects && m_floatingObjects->hasLeftObjects())
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002379 return m_floatingObjects->logicalLeftOffset(fixedOffset, logicalTop, logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002380
2381 return fixedOffset;
2382}
2383
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002384LayoutUnit RenderBlockFlow::logicalRightFloatOffsetForLine(LayoutUnit logicalTop, LayoutUnit fixedOffset, LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002385{
2386 if (m_floatingObjects && m_floatingObjects->hasRightObjects())
bjonesbe@adobe.com98b899b2013-11-07 18:11:43 +00002387 return m_floatingObjects->logicalRightOffset(fixedOffset, logicalTop, logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002388
2389 return fixedOffset;
2390}
2391
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002392LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelow(LayoutUnit logicalHeight) const
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002393{
2394 if (!m_floatingObjects)
2395 return logicalHeight;
2396
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002397 return m_floatingObjects->findNextFloatLogicalBottomBelow(logicalHeight);
2398}
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002399
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002400LayoutUnit RenderBlockFlow::nextFloatLogicalBottomBelowForBlock(LayoutUnit logicalHeight) const
2401{
2402 if (!m_floatingObjects)
2403 return logicalHeight;
2404
2405 return m_floatingObjects->findNextFloatLogicalBottomBelowForBlock(logicalHeight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002406}
2407
2408LayoutUnit RenderBlockFlow::lowestFloatLogicalBottom(FloatingObject::Type floatType) const
2409{
2410 if (!m_floatingObjects)
2411 return 0;
2412 LayoutUnit lowestFloatBottom = 0;
2413 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2414 auto end = floatingObjectSet.end();
2415 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2416 FloatingObject* floatingObject = it->get();
2417 if (floatingObject->isPlaced() && floatingObject->type() & floatType)
andersca@apple.com86298632013-11-10 19:32:33 +00002418 lowestFloatBottom = std::max(lowestFloatBottom, logicalBottomForFloat(floatingObject));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002419 }
2420 return lowestFloatBottom;
2421}
2422
weinig@apple.com12840dc2013-10-22 23:59:08 +00002423LayoutUnit RenderBlockFlow::addOverhangingFloats(RenderBlockFlow& child, bool makeChildPaintOtherFloats)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002424{
2425 // Prevent floats from being added to the canvas by the root element, e.g., <html>.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002426 if (child.hasOverflowClip() || !child.containsFloats() || child.isRoot() || child.hasColumns() || child.isWritingModeRoot())
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002427 return 0;
2428
weinig@apple.com12840dc2013-10-22 23:59:08 +00002429 LayoutUnit childLogicalTop = child.logicalTop();
2430 LayoutUnit childLogicalLeft = child.logicalLeft();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002431 LayoutUnit lowestFloatLogicalBottom = 0;
2432
2433 // Floats that will remain the child's responsibility to paint should factor into its
2434 // overflow.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002435 auto childEnd = child.m_floatingObjects->set().end();
2436 for (auto childIt = child.m_floatingObjects->set().begin(); childIt != childEnd; ++childIt) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002437 FloatingObject* floatingObject = childIt->get();
andersca@apple.com86298632013-11-10 19:32:33 +00002438 LayoutUnit floatLogicalBottom = std::min(logicalBottomForFloat(floatingObject), LayoutUnit::max() - childLogicalTop);
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002439 LayoutUnit logicalBottom = childLogicalTop + floatLogicalBottom;
andersca@apple.com86298632013-11-10 19:32:33 +00002440 lowestFloatLogicalBottom = std::max(lowestFloatLogicalBottom, logicalBottom);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002441
2442 if (logicalBottom > logicalHeight()) {
2443 // If the object is not in the list, we add it now.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002444 if (!containsFloat(floatingObject->renderer())) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002445 LayoutSize offset = isHorizontalWritingMode() ? LayoutSize(-childLogicalLeft, -childLogicalTop) : LayoutSize(-childLogicalTop, -childLogicalLeft);
2446 bool shouldPaint = false;
2447
2448 // The nearest enclosing layer always paints the float (so that zindex and stacking
2449 // behaves properly). We always want to propagate the desire to paint the float as
2450 // far out as we can, to the outermost block that overlaps the float, stopping only
2451 // if we hit a self-painting layer boundary.
2452 if (floatingObject->renderer().enclosingFloatPaintingLayer() == enclosingFloatPaintingLayer()) {
2453 floatingObject->setShouldPaint(false);
2454 shouldPaint = true;
2455 }
2456 // We create the floating object list lazily.
2457 if (!m_floatingObjects)
2458 createFloatingObjects();
2459
2460 m_floatingObjects->add(floatingObject->copyToNewContainer(offset, shouldPaint, true));
2461 }
2462 } else {
2463 if (makeChildPaintOtherFloats && !floatingObject->shouldPaint() && !floatingObject->renderer().hasSelfPaintingLayer()
weinig@apple.com12840dc2013-10-22 23:59:08 +00002464 && floatingObject->renderer().isDescendantOf(&child) && floatingObject->renderer().enclosingFloatPaintingLayer() == child.enclosingFloatPaintingLayer()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002465 // The float is not overhanging from this block, so if it is a descendant of the child, the child should
2466 // paint it (the other case is that it is intruding into the child), unless it has its own layer or enclosing
2467 // layer.
2468 // If makeChildPaintOtherFloats is false, it means that the child must already know about all the floats
2469 // it should paint.
2470 floatingObject->setShouldPaint(true);
2471 }
2472
2473 // 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
2474 // child now.
2475 if (floatingObject->isDescendant())
weinig@apple.com12840dc2013-10-22 23:59:08 +00002476 child.addOverflowFromChild(&floatingObject->renderer(), LayoutSize(xPositionForFloatIncludingMargin(floatingObject), yPositionForFloatIncludingMargin(floatingObject)));
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002477 }
2478 }
2479 return lowestFloatLogicalBottom;
2480}
2481
weinig@apple.com12840dc2013-10-22 23:59:08 +00002482bool RenderBlockFlow::hasOverhangingFloat(RenderBox& renderer)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002483{
2484 if (!m_floatingObjects || hasColumns() || !parent())
2485 return false;
2486
2487 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002488 auto it = floatingObjectSet.find<RenderBox&, FloatingObjectHashTranslator>(renderer);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002489 if (it == floatingObjectSet.end())
2490 return false;
2491
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002492 return logicalBottomForFloat(it->get()) > logicalHeight();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002493}
2494
2495void RenderBlockFlow::addIntrudingFloats(RenderBlockFlow* prev, LayoutUnit logicalLeftOffset, LayoutUnit logicalTopOffset)
2496{
2497 ASSERT(!avoidsFloats());
2498
2499 // If the parent or previous sibling doesn't have any floats to add, don't bother.
2500 if (!prev->m_floatingObjects)
2501 return;
2502
2503 logicalLeftOffset += marginLogicalLeft();
2504
2505 const FloatingObjectSet& prevSet = prev->m_floatingObjects->set();
2506 auto prevEnd = prevSet.end();
2507 for (auto prevIt = prevSet.begin(); prevIt != prevEnd; ++prevIt) {
2508 FloatingObject* floatingObject = prevIt->get();
bjonesbe@adobe.com1ccd3a12013-10-10 00:35:38 +00002509 if (logicalBottomForFloat(floatingObject) > logicalTopOffset) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002510 if (!m_floatingObjects || !m_floatingObjects->set().contains<FloatingObject&, FloatingObjectHashTranslator>(*floatingObject)) {
2511 // We create the floating object list lazily.
2512 if (!m_floatingObjects)
2513 createFloatingObjects();
2514
2515 // Applying the child's margin makes no sense in the case where the child was passed in.
2516 // since this margin was added already through the modification of the |logicalLeftOffset| variable
2517 // above. |logicalLeftOffset| will equal the margin in this case, so it's already been taken
2518 // into account. Only apply this code if prev is the parent, since otherwise the left margin
2519 // will get applied twice.
2520 LayoutSize offset = isHorizontalWritingMode()
2521 ? LayoutSize(logicalLeftOffset - (prev != parent() ? prev->marginLeft() : LayoutUnit()), logicalTopOffset)
2522 : LayoutSize(logicalTopOffset, logicalLeftOffset - (prev != parent() ? prev->marginTop() : LayoutUnit()));
2523
2524 m_floatingObjects->add(floatingObject->copyToNewContainer(offset));
2525 }
2526 }
2527 }
2528}
2529
2530void RenderBlockFlow::markAllDescendantsWithFloatsForLayout(RenderBox* floatToRemove, bool inLayout)
2531{
2532 if (!everHadLayout() && !containsFloats())
2533 return;
2534
2535 MarkingBehavior markParents = inLayout ? MarkOnlyThis : MarkContainingBlockChain;
2536 setChildNeedsLayout(markParents);
2537
2538 if (floatToRemove)
weinig@apple.com12840dc2013-10-22 23:59:08 +00002539 removeFloatingObject(*floatToRemove);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002540
akling@apple.com525dae62014-01-03 20:22:09 +00002541 if (childrenInline())
2542 return;
2543
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002544 // Iterate over our children and mark them as needed.
akling@apple.com525dae62014-01-03 20:22:09 +00002545 for (auto& block : childrenOfType<RenderBlock>(*this)) {
2546 if (!floatToRemove && block.isFloatingOrOutOfFlowPositioned())
2547 continue;
2548 if (!block.isRenderBlockFlow()) {
2549 if (block.shrinkToAvoidFloats() && block.everHadLayout())
2550 block.setChildNeedsLayout(markParents);
2551 continue;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002552 }
akling@apple.com525dae62014-01-03 20:22:09 +00002553 auto& blockFlow = toRenderBlockFlow(block);
2554 if ((floatToRemove ? blockFlow.containsFloat(*floatToRemove) : blockFlow.containsFloats()) || blockFlow.shrinkToAvoidFloats())
2555 blockFlow.markAllDescendantsWithFloatsForLayout(floatToRemove, inLayout);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002556 }
2557}
2558
2559void RenderBlockFlow::markSiblingsWithFloatsForLayout(RenderBox* floatToRemove)
2560{
2561 if (!m_floatingObjects)
2562 return;
2563
2564 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2565 auto end = floatingObjectSet.end();
2566
2567 for (RenderObject* next = nextSibling(); next; next = next->nextSibling()) {
2568 if (!next->isRenderBlockFlow() || next->isFloatingOrOutOfFlowPositioned() || toRenderBlock(next)->avoidsFloats())
2569 continue;
2570
2571 RenderBlockFlow* nextBlock = toRenderBlockFlow(next);
2572 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
weinig@apple.com12840dc2013-10-22 23:59:08 +00002573 RenderBox& floatingBox = (*it)->renderer();
2574 if (floatToRemove && &floatingBox != floatToRemove)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002575 continue;
2576 if (nextBlock->containsFloat(floatingBox))
weinig@apple.com12840dc2013-10-22 23:59:08 +00002577 nextBlock->markAllDescendantsWithFloatsForLayout(&floatingBox);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002578 }
2579 }
2580}
2581
weinig@apple.com31324fd2013-10-28 19:22:51 +00002582LayoutPoint RenderBlockFlow::flipFloatForWritingModeForChild(const FloatingObject* child, const LayoutPoint& point) const
2583{
akling@apple.com827be9c2013-10-29 02:58:43 +00002584 if (!style().isFlippedBlocksWritingMode())
weinig@apple.com31324fd2013-10-28 19:22:51 +00002585 return point;
2586
2587 // This is similar to RenderBox::flipForWritingModeForChild. We have to subtract out our left/top offsets twice, since
2588 // it's going to get added back in. We hide this complication here so that the calling code looks normal for the unflipped
2589 // case.
2590 if (isHorizontalWritingMode())
2591 return LayoutPoint(point.x(), point.y() + height() - child->renderer().height() - 2 * yPositionForFloatIncludingMargin(child));
2592 return LayoutPoint(point.x() + width() - child->renderer().width() - 2 * xPositionForFloatIncludingMargin(child), point.y());
2593}
2594
weinig@apple.com12840dc2013-10-22 23:59:08 +00002595LayoutUnit RenderBlockFlow::getClearDelta(RenderBox& child, LayoutUnit logicalTop)
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002596{
2597 // There is no need to compute clearance if we have no floats.
2598 if (!containsFloats())
2599 return 0;
2600
2601 // At least one float is present. We need to perform the clearance computation.
akling@apple.com827be9c2013-10-29 02:58:43 +00002602 bool clearSet = child.style().clear() != CNONE;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002603 LayoutUnit logicalBottom = 0;
akling@apple.com827be9c2013-10-29 02:58:43 +00002604 switch (child.style().clear()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002605 case CNONE:
2606 break;
2607 case CLEFT:
2608 logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatLeft);
2609 break;
2610 case CRIGHT:
2611 logicalBottom = lowestFloatLogicalBottom(FloatingObject::FloatRight);
2612 break;
2613 case CBOTH:
2614 logicalBottom = lowestFloatLogicalBottom();
2615 break;
2616 }
2617
2618 // 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 +00002619 LayoutUnit result = clearSet ? std::max<LayoutUnit>(0, logicalBottom - logicalTop) : LayoutUnit();
weinig@apple.com12840dc2013-10-22 23:59:08 +00002620 if (!result && child.avoidsFloats()) {
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002621 LayoutUnit newLogicalTop = logicalTop;
2622 while (true) {
2623 LayoutUnit availableLogicalWidthAtNewLogicalTopOffset = availableLogicalWidthForLine(newLogicalTop, false, logicalHeightForChild(child));
2624 if (availableLogicalWidthAtNewLogicalTopOffset == availableLogicalWidthForContent(newLogicalTop))
2625 return newLogicalTop - logicalTop;
2626
2627 RenderRegion* region = regionAtBlockOffset(logicalTopForChild(child));
weinig@apple.com12840dc2013-10-22 23:59:08 +00002628 LayoutRect borderBox = child.borderBoxRectInRegion(region, DoNotCacheRenderBoxRegionInfo);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002629 LayoutUnit childLogicalWidthAtOldLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
2630
2631 // FIXME: None of this is right for perpendicular writing-mode children.
weinig@apple.com12840dc2013-10-22 23:59:08 +00002632 LayoutUnit childOldLogicalWidth = child.logicalWidth();
2633 LayoutUnit childOldMarginLeft = child.marginLeft();
2634 LayoutUnit childOldMarginRight = child.marginRight();
2635 LayoutUnit childOldLogicalTop = child.logicalTop();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002636
weinig@apple.com12840dc2013-10-22 23:59:08 +00002637 child.setLogicalTop(newLogicalTop);
2638 child.updateLogicalWidth();
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002639 region = regionAtBlockOffset(logicalTopForChild(child));
weinig@apple.com12840dc2013-10-22 23:59:08 +00002640 borderBox = child.borderBoxRectInRegion(region, DoNotCacheRenderBoxRegionInfo);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002641 LayoutUnit childLogicalWidthAtNewLogicalTopOffset = isHorizontalWritingMode() ? borderBox.width() : borderBox.height();
2642
weinig@apple.com12840dc2013-10-22 23:59:08 +00002643 child.setLogicalTop(childOldLogicalTop);
2644 child.setLogicalWidth(childOldLogicalWidth);
2645 child.setMarginLeft(childOldMarginLeft);
2646 child.setMarginRight(childOldMarginRight);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002647
2648 if (childLogicalWidthAtNewLogicalTopOffset <= availableLogicalWidthAtNewLogicalTopOffset) {
2649 // Even though we may not be moving, if the logical width did shrink because of the presence of new floats, then
2650 // we need to force a relayout as though we shifted. This happens because of the dynamic addition of overhanging floats
2651 // from previous siblings when negative margins exist on a child (see the addOverhangingFloats call at the end of collapseMargins).
2652 if (childLogicalWidthAtOldLogicalTopOffset != childLogicalWidthAtNewLogicalTopOffset)
weinig@apple.com12840dc2013-10-22 23:59:08 +00002653 child.setChildNeedsLayout(MarkOnlyThis);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002654 return newLogicalTop - logicalTop;
2655 }
2656
bjonesbe@adobe.comedea3422013-11-08 22:01:33 +00002657 newLogicalTop = nextFloatLogicalBottomBelowForBlock(newLogicalTop);
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002658 ASSERT(newLogicalTop >= logicalTop);
2659 if (newLogicalTop < logicalTop)
2660 break;
2661 }
2662 ASSERT_NOT_REACHED();
2663 }
2664 return result;
2665}
2666
2667bool RenderBlockFlow::hitTestFloats(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset)
2668{
2669 if (!m_floatingObjects)
2670 return false;
2671
2672 LayoutPoint adjustedLocation = accumulatedOffset;
2673 if (isRenderView())
2674 adjustedLocation += toLayoutSize(toRenderView(*this).frameView().scrollPosition());
2675
2676 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2677 auto begin = floatingObjectSet.begin();
2678 for (auto it = floatingObjectSet.end(); it != begin;) {
2679 --it;
2680 FloatingObject* floatingObject = it->get();
2681 if (floatingObject->shouldPaint() && !floatingObject->renderer().hasSelfPaintingLayer()) {
2682 LayoutUnit xOffset = xPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer().x();
2683 LayoutUnit yOffset = yPositionForFloatIncludingMargin(floatingObject) - floatingObject->renderer().y();
2684 LayoutPoint childPoint = flipFloatForWritingModeForChild(floatingObject, adjustedLocation + LayoutSize(xOffset, yOffset));
2685 if (floatingObject->renderer().hitTest(request, result, locationInContainer, childPoint)) {
2686 updateHitTestResult(result, locationInContainer.point() - toLayoutSize(childPoint));
2687 return true;
2688 }
2689 }
2690 }
2691
2692 return false;
2693}
2694
weinig@apple.com611b9292013-10-20 22:57:54 +00002695bool RenderBlockFlow::hitTestInlineChildren(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
2696{
2697 ASSERT(childrenInline());
antti@apple.com940f5872013-10-24 20:31:11 +00002698
antti@apple.comfea51992013-10-28 13:39:23 +00002699 if (m_simpleLineLayout)
2700 return SimpleLineLayout::hitTestFlow(*this, *m_simpleLineLayout, request, result, locationInContainer, accumulatedOffset, hitTestAction);
antti@apple.com940f5872013-10-24 20:31:11 +00002701
weinig@apple.com611b9292013-10-20 22:57:54 +00002702 return m_lineBoxes.hitTest(this, request, result, locationInContainer, accumulatedOffset, hitTestAction);
2703}
2704
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002705void RenderBlockFlow::adjustForBorderFit(LayoutUnit x, LayoutUnit& left, LayoutUnit& right) const
2706{
akling@apple.com827be9c2013-10-29 02:58:43 +00002707 if (style().visibility() != VISIBLE)
weinig@apple.com611b9292013-10-20 22:57:54 +00002708 return;
2709
2710 // We don't deal with relative positioning. Our assumption is that you shrink to fit the lines without accounting
2711 // for either overflow or translations via relative positioning.
2712 if (childrenInline()) {
antti@apple.com940f5872013-10-24 20:31:11 +00002713 const_cast<RenderBlockFlow&>(*this).ensureLineBoxes();
2714
weinig@apple.com611b9292013-10-20 22:57:54 +00002715 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
2716 if (box->firstChild())
andersca@apple.com86298632013-11-10 19:32:33 +00002717 left = std::min(left, x + static_cast<LayoutUnit>(box->firstChild()->x()));
weinig@apple.com611b9292013-10-20 22:57:54 +00002718 if (box->lastChild())
andersca@apple.com86298632013-11-10 19:32:33 +00002719 right = std::max(right, x + static_cast<LayoutUnit>(ceilf(box->lastChild()->logicalRight())));
weinig@apple.com611b9292013-10-20 22:57:54 +00002720 }
2721 } else {
2722 for (RenderBox* obj = firstChildBox(); obj; obj = obj->nextSiblingBox()) {
2723 if (!obj->isFloatingOrOutOfFlowPositioned()) {
2724 if (obj->isRenderBlockFlow() && !obj->hasOverflowClip())
2725 toRenderBlockFlow(obj)->adjustForBorderFit(x + obj->x(), left, right);
akling@apple.com827be9c2013-10-29 02:58:43 +00002726 else if (obj->style().visibility() == VISIBLE) {
weinig@apple.com611b9292013-10-20 22:57:54 +00002727 // We are a replaced element or some kind of non-block-flow object.
andersca@apple.com86298632013-11-10 19:32:33 +00002728 left = std::min(left, x + obj->x());
2729 right = std::max(right, x + obj->x() + obj->width());
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002730 }
2731 }
2732 }
2733 }
weinig@apple.com611b9292013-10-20 22:57:54 +00002734
2735 if (m_floatingObjects) {
2736 const FloatingObjectSet& floatingObjectSet = m_floatingObjects->set();
2737 auto end = floatingObjectSet.end();
2738 for (auto it = floatingObjectSet.begin(); it != end; ++it) {
2739 FloatingObject* r = it->get();
2740 // Only examine the object if our m_shouldPaint flag is set.
2741 if (r->shouldPaint()) {
2742 LayoutUnit floatLeft = xPositionForFloatIncludingMargin(r) - r->renderer().x();
2743 LayoutUnit floatRight = floatLeft + r->renderer().width();
andersca@apple.com86298632013-11-10 19:32:33 +00002744 left = std::min(left, floatLeft);
2745 right = std::max(right, floatRight);
weinig@apple.com611b9292013-10-20 22:57:54 +00002746 }
2747 }
2748 }
2749}
2750
2751void RenderBlockFlow::fitBorderToLinesIfNeeded()
2752{
akling@apple.com827be9c2013-10-29 02:58:43 +00002753 if (style().borderFit() == BorderFitBorder || hasOverrideWidth())
weinig@apple.com611b9292013-10-20 22:57:54 +00002754 return;
2755
2756 // Walk any normal flow lines to snugly fit.
2757 LayoutUnit left = LayoutUnit::max();
2758 LayoutUnit right = LayoutUnit::min();
2759 LayoutUnit oldWidth = contentWidth();
2760 adjustForBorderFit(0, left, right);
2761
2762 // Clamp to our existing edges. We can never grow. We only shrink.
2763 LayoutUnit leftEdge = borderLeft() + paddingLeft();
2764 LayoutUnit rightEdge = leftEdge + oldWidth;
andersca@apple.com86298632013-11-10 19:32:33 +00002765 left = std::min(rightEdge, std::max(leftEdge, left));
2766 right = std::max(leftEdge, std::min(rightEdge, right));
weinig@apple.com611b9292013-10-20 22:57:54 +00002767
2768 LayoutUnit newContentWidth = right - left;
2769 if (newContentWidth == oldWidth)
2770 return;
2771
2772 setOverrideLogicalContentWidth(newContentWidth);
2773 layoutBlock(false);
2774 clearOverrideLogicalContentWidth();
2775}
2776
2777void RenderBlockFlow::markLinesDirtyInBlockRange(LayoutUnit logicalTop, LayoutUnit logicalBottom, RootInlineBox* highest)
2778{
2779 if (logicalTop >= logicalBottom)
2780 return;
2781
2782 RootInlineBox* lowestDirtyLine = lastRootBox();
2783 RootInlineBox* afterLowest = lowestDirtyLine;
2784 while (lowestDirtyLine && lowestDirtyLine->lineBottomWithLeading() >= logicalBottom && logicalBottom < LayoutUnit::max()) {
2785 afterLowest = lowestDirtyLine;
2786 lowestDirtyLine = lowestDirtyLine->prevRootBox();
2787 }
2788
2789 while (afterLowest && afterLowest != highest && (afterLowest->lineBottomWithLeading() >= logicalTop || afterLowest->lineBottomWithLeading() < 0)) {
2790 afterLowest->markDirty();
2791 afterLowest = afterLowest->prevRootBox();
2792 }
2793}
2794
antti@apple.com0e632aa2013-10-22 21:03:38 +00002795int RenderBlockFlow::firstLineBaseline() const
weinig@apple.com611b9292013-10-20 22:57:54 +00002796{
2797 if (isWritingModeRoot() && !isRubyRun())
2798 return -1;
2799
2800 if (!childrenInline())
antti@apple.com0e632aa2013-10-22 21:03:38 +00002801 return RenderBlock::firstLineBaseline();
weinig@apple.com611b9292013-10-20 22:57:54 +00002802
antti@apple.com940f5872013-10-24 20:31:11 +00002803 if (!hasLines())
2804 return -1;
weinig@apple.com611b9292013-10-20 22:57:54 +00002805
antti@apple.comfea51992013-10-28 13:39:23 +00002806 if (m_simpleLineLayout)
2807 return SimpleLineLayout::computeFlowFirstLineBaseline(*this, *m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00002808
akling@apple.comee3c8df2013-11-06 08:09:44 +00002809 ASSERT(firstRootBox());
2810 return firstRootBox()->logicalTop() + firstLineStyle().fontMetrics().ascent(firstRootBox()->baselineType());
weinig@apple.com611b9292013-10-20 22:57:54 +00002811}
2812
2813int RenderBlockFlow::inlineBlockBaseline(LineDirectionMode lineDirection) const
2814{
2815 if (isWritingModeRoot() && !isRubyRun())
2816 return -1;
2817
2818 if (!childrenInline())
2819 return RenderBlock::inlineBlockBaseline(lineDirection);
2820
antti@apple.com0e632aa2013-10-22 21:03:38 +00002821 if (!hasLines()) {
2822 if (!hasLineIfEmpty())
2823 return -1;
akling@apple.com827be9c2013-10-29 02:58:43 +00002824 const FontMetrics& fontMetrics = firstLineStyle().fontMetrics();
weinig@apple.com611b9292013-10-20 22:57:54 +00002825 return fontMetrics.ascent()
2826 + (lineHeight(true, lineDirection, PositionOfInteriorLineBoxes) - fontMetrics.height()) / 2
2827 + (lineDirection == HorizontalLine ? borderTop() + paddingTop() : borderRight() + paddingRight());
2828 }
2829
antti@apple.comfea51992013-10-28 13:39:23 +00002830 if (m_simpleLineLayout)
2831 return SimpleLineLayout::computeFlowLastLineBaseline(*this, *m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00002832
akling@apple.comee3c8df2013-11-06 08:09:44 +00002833 bool isFirstLine = lastRootBox() == firstRootBox();
akling@apple.com827be9c2013-10-29 02:58:43 +00002834 const RenderStyle& style = isFirstLine ? firstLineStyle() : this->style();
akling@apple.comee3c8df2013-11-06 08:09:44 +00002835 return lastRootBox()->logicalTop() + style.fontMetrics().ascent(lastRootBox()->baselineType());
weinig@apple.com611b9292013-10-20 22:57:54 +00002836}
2837
weinig@apple.com12840dc2013-10-22 23:59:08 +00002838GapRects RenderBlockFlow::inlineSelectionGaps(RenderBlock& rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
weinig@apple.com611b9292013-10-20 22:57:54 +00002839 LayoutUnit& lastLogicalTop, LayoutUnit& lastLogicalLeft, LayoutUnit& lastLogicalRight, const LogicalSelectionOffsetCaches& cache, const PaintInfo* paintInfo)
2840{
antti@apple.comfea51992013-10-28 13:39:23 +00002841 ASSERT(!m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00002842
weinig@apple.com611b9292013-10-20 22:57:54 +00002843 GapRects result;
2844
2845 bool containsStart = selectionState() == SelectionStart || selectionState() == SelectionBoth;
2846
antti@apple.com0e632aa2013-10-22 21:03:38 +00002847 if (!hasLines()) {
weinig@apple.com611b9292013-10-20 22:57:54 +00002848 if (containsStart) {
2849 // Go ahead and update our lastLogicalTop to be the bottom of the block. <hr>s or empty blocks with height can trip this
2850 // case.
2851 lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + logicalHeight();
2852 lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, logicalHeight(), cache);
2853 lastLogicalRight = logicalRightSelectionOffset(rootBlock, logicalHeight(), cache);
2854 }
2855 return result;
2856 }
2857
2858 RootInlineBox* lastSelectedLine = 0;
2859 RootInlineBox* curr;
2860 for (curr = firstRootBox(); curr && !curr->hasSelectedChildren(); curr = curr->nextRootBox()) { }
2861
2862 // Now paint the gaps for the lines.
2863 for (; curr && curr->hasSelectedChildren(); curr = curr->nextRootBox()) {
2864 LayoutUnit selTop = curr->selectionTopAdjustedForPrecedingBlock();
2865 LayoutUnit selHeight = curr->selectionHeightAdjustedForPrecedingBlock();
2866
2867 if (!containsStart && !lastSelectedLine &&
2868 selectionState() != SelectionStart && selectionState() != SelectionBoth)
2869 result.uniteCenter(blockSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, lastLogicalTop, lastLogicalLeft, lastLogicalRight, selTop, cache, paintInfo));
2870
2871 LayoutRect logicalRect(curr->logicalLeft(), selTop, curr->logicalWidth(), selTop + selHeight);
2872 logicalRect.move(isHorizontalWritingMode() ? offsetFromRootBlock : offsetFromRootBlock.transposedSize());
weinig@apple.com12840dc2013-10-22 23:59:08 +00002873 LayoutRect physicalRect = rootBlock.logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
weinig@apple.com611b9292013-10-20 22:57:54 +00002874 if (!paintInfo || (isHorizontalWritingMode() && physicalRect.y() < paintInfo->rect.maxY() && physicalRect.maxY() > paintInfo->rect.y())
2875 || (!isHorizontalWritingMode() && physicalRect.x() < paintInfo->rect.maxX() && physicalRect.maxX() > paintInfo->rect.x()))
2876 result.unite(curr->lineSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock, selTop, selHeight, cache, paintInfo));
2877
2878 lastSelectedLine = curr;
2879 }
2880
2881 if (containsStart && !lastSelectedLine)
2882 // VisibleSelection must start just after our last line.
2883 lastSelectedLine = lastRootBox();
2884
2885 if (lastSelectedLine && selectionState() != SelectionEnd && selectionState() != SelectionBoth) {
2886 // Go ahead and update our lastY to be the bottom of the last selected line.
2887 lastLogicalTop = blockDirectionOffset(rootBlock, offsetFromRootBlock) + lastSelectedLine->selectionBottom();
2888 lastLogicalLeft = logicalLeftSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
2889 lastLogicalRight = logicalRightSelectionOffset(rootBlock, lastSelectedLine->selectionBottom(), cache);
2890 }
2891 return result;
bjonesbe@adobe.com24199752013-10-08 23:20:42 +00002892}
2893
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002894void RenderBlockFlow::createRenderNamedFlowFragmentIfNeeded()
2895{
abucur@adobe.com0e81bc72013-10-22 14:50:37 +00002896 if (!document().cssRegionsEnabled() || renderNamedFlowFragment() || isRenderNamedFlowFragment())
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002897 return;
2898
akling@apple.com827be9c2013-10-29 02:58:43 +00002899 if (style().isDisplayRegionType() && style().hasFlowFrom()) {
2900 RenderNamedFlowFragment* flowFragment = new RenderNamedFlowFragment(document(), RenderNamedFlowFragment::createStyle(style()));
akling@apple.com8f40c5b2013-10-27 22:54:07 +00002901 flowFragment->initializeStyle();
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002902 setRenderNamedFlowFragment(flowFragment);
2903 addChild(renderNamedFlowFragment());
2904 }
2905}
2906
2907bool RenderBlockFlow::canHaveChildren() const
2908{
2909 return !renderNamedFlowFragment() ? RenderBlock::canHaveChildren() : renderNamedFlowFragment()->canHaveChildren();
2910}
2911
2912bool RenderBlockFlow::canHaveGeneratedChildren() const
2913{
2914 return !renderNamedFlowFragment() ? RenderBlock::canHaveGeneratedChildren() : renderNamedFlowFragment()->canHaveGeneratedChildren();
2915}
2916
2917bool RenderBlockFlow::namedFlowFragmentNeedsUpdate() const
2918{
2919 if (!isRenderNamedFlowFragmentContainer())
2920 return false;
2921
2922 return hasRelativeLogicalHeight() && !isRenderView();
2923}
2924
2925void RenderBlockFlow::updateLogicalHeight()
2926{
2927 RenderBlock::updateLogicalHeight();
2928
2929 if (renderNamedFlowFragment())
andersca@apple.com86298632013-11-10 19:32:33 +00002930 renderNamedFlowFragment()->setLogicalHeight(std::max<LayoutUnit>(0, logicalHeight() - borderAndPaddingLogicalHeight()));
mihnea@adobe.combe79cf12013-10-17 09:02:19 +00002931}
2932
2933void RenderBlockFlow::setRenderNamedFlowFragment(RenderNamedFlowFragment* flowFragment)
2934{
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00002935 RenderBlockFlowRareData& rareData = ensureRareBlockFlowData();
abucur@adobe.com0e81bc72013-10-22 14:50:37 +00002936 if (rareData.m_renderNamedFlowFragment)
2937 rareData.m_renderNamedFlowFragment->destroy();
2938 rareData.m_renderNamedFlowFragment = flowFragment;
2939}
2940
hyatt@apple.come9fe3d32014-01-24 17:14:22 +00002941void RenderBlockFlow::setMultiColumnFlowThread(RenderMultiColumnFlowThread* flowThread)
2942{
2943 RenderBlockFlowRareData& rareData = ensureRareBlockFlowData();
2944 rareData.m_multiColumnFlowThread = flowThread;
2945}
2946
akling@apple.com525dae62014-01-03 20:22:09 +00002947static bool shouldCheckLines(const RenderBlockFlow& blockFlow)
weinig@apple.com17140912013-10-19 19:55:40 +00002948{
akling@apple.com38f0a652014-02-06 21:24:17 +00002949 return !blockFlow.isFloatingOrOutOfFlowPositioned() && blockFlow.style().height().isAuto();
weinig@apple.com17140912013-10-19 19:55:40 +00002950}
2951
2952RootInlineBox* RenderBlockFlow::lineAtIndex(int i) const
2953{
2954 ASSERT(i >= 0);
2955
akling@apple.com827be9c2013-10-29 02:58:43 +00002956 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00002957 return nullptr;
2958
2959 if (childrenInline()) {
2960 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
2961 if (!i--)
2962 return box;
2963 }
akling@apple.com525dae62014-01-03 20:22:09 +00002964 return nullptr;
2965 }
2966
2967 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
2968 if (!shouldCheckLines(blockFlow))
2969 continue;
2970 if (RootInlineBox* box = blockFlow.lineAtIndex(i))
2971 return box;
weinig@apple.com17140912013-10-19 19:55:40 +00002972 }
2973
2974 return nullptr;
2975}
2976
2977int RenderBlockFlow::lineCount(const RootInlineBox* stopRootInlineBox, bool* found) const
2978{
akling@apple.com827be9c2013-10-29 02:58:43 +00002979 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00002980 return 0;
2981
2982 int count = 0;
2983
2984 if (childrenInline()) {
2985 for (auto box = firstRootBox(); box; box = box->nextRootBox()) {
2986 count++;
2987 if (box == stopRootInlineBox) {
2988 if (found)
2989 *found = true;
2990 break;
2991 }
2992 }
akling@apple.com525dae62014-01-03 20:22:09 +00002993 return count;
2994 }
2995
2996 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
2997 if (!shouldCheckLines(blockFlow))
2998 continue;
2999 bool recursiveFound = false;
3000 count += blockFlow.lineCount(stopRootInlineBox, &recursiveFound);
3001 if (recursiveFound) {
3002 if (found)
3003 *found = true;
3004 break;
weinig@apple.com17140912013-10-19 19:55:40 +00003005 }
3006 }
3007
3008 return count;
3009}
3010
3011static int getHeightForLineCount(const RenderBlockFlow& block, int lineCount, bool includeBottom, int& count)
3012{
akling@apple.com827be9c2013-10-29 02:58:43 +00003013 if (block.style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00003014 return -1;
3015
3016 if (block.childrenInline()) {
3017 for (auto box = block.firstRootBox(); box; box = box->nextRootBox()) {
3018 if (++count == lineCount)
3019 return box->lineBottom() + (includeBottom ? (block.borderBottom() + block.paddingBottom()) : LayoutUnit());
3020 }
3021 } else {
3022 RenderBox* normalFlowChildWithoutLines = 0;
3023 for (auto obj = block.firstChildBox(); obj; obj = obj->nextSiblingBox()) {
akling@apple.com525dae62014-01-03 20:22:09 +00003024 if (obj->isRenderBlockFlow() && shouldCheckLines(toRenderBlockFlow(*obj))) {
weinig@apple.com17140912013-10-19 19:55:40 +00003025 int result = getHeightForLineCount(toRenderBlockFlow(*obj), lineCount, false, count);
3026 if (result != -1)
3027 return result + obj->y() + (includeBottom ? (block.borderBottom() + block.paddingBottom()) : LayoutUnit());
akling@apple.com38f0a652014-02-06 21:24:17 +00003028 } else if (!obj->isFloatingOrOutOfFlowPositioned())
weinig@apple.com17140912013-10-19 19:55:40 +00003029 normalFlowChildWithoutLines = obj;
3030 }
3031 if (normalFlowChildWithoutLines && !lineCount)
3032 return normalFlowChildWithoutLines->y() + normalFlowChildWithoutLines->height();
3033 }
3034
3035 return -1;
3036}
3037
3038int RenderBlockFlow::heightForLineCount(int lineCount)
3039{
3040 int count = 0;
3041 return getHeightForLineCount(*this, lineCount, true, count);
3042}
3043
3044void RenderBlockFlow::clearTruncation()
3045{
akling@apple.com827be9c2013-10-29 02:58:43 +00003046 if (style().visibility() != VISIBLE)
weinig@apple.com17140912013-10-19 19:55:40 +00003047 return;
3048
3049 if (childrenInline() && hasMarkupTruncation()) {
antti@apple.com940f5872013-10-24 20:31:11 +00003050 ensureLineBoxes();
3051
weinig@apple.com17140912013-10-19 19:55:40 +00003052 setHasMarkupTruncation(false);
3053 for (auto box = firstRootBox(); box; box = box->nextRootBox())
3054 box->clearTruncation();
akling@apple.com525dae62014-01-03 20:22:09 +00003055 return;
3056 }
3057
3058 for (auto& blockFlow : childrenOfType<RenderBlockFlow>(*this)) {
3059 if (shouldCheckLines(blockFlow))
3060 blockFlow.clearTruncation();
weinig@apple.com17140912013-10-19 19:55:40 +00003061 }
3062}
3063
weinig@apple.com3f23b382013-10-19 20:26:58 +00003064bool RenderBlockFlow::containsNonZeroBidiLevel() const
3065{
3066 for (auto root = firstRootBox(); root; root = root->nextRootBox()) {
3067 for (auto box = root->firstLeafChild(); box; box = box->nextLeafChild()) {
3068 if (box->bidiLevel())
3069 return true;
3070 }
3071 }
3072 return false;
3073}
3074
weinig@apple.com611b9292013-10-20 22:57:54 +00003075Position RenderBlockFlow::positionForBox(InlineBox *box, bool start) const
3076{
3077 if (!box)
3078 return Position();
3079
3080 if (!box->renderer().nonPseudoNode())
3081 return createLegacyEditingPosition(nonPseudoElement(), start ? caretMinOffset() : caretMaxOffset());
3082
3083 if (!box->isInlineTextBox())
3084 return createLegacyEditingPosition(box->renderer().nonPseudoNode(), start ? box->renderer().caretMinOffset() : box->renderer().caretMaxOffset());
3085
3086 InlineTextBox* textBox = toInlineTextBox(box);
3087 return createLegacyEditingPosition(box->renderer().nonPseudoNode(), start ? textBox->start() : textBox->start() + textBox->len());
3088}
3089
3090VisiblePosition RenderBlockFlow::positionForPointWithInlineChildren(const LayoutPoint& pointInLogicalContents)
3091{
3092 ASSERT(childrenInline());
3093
antti@apple.com940f5872013-10-24 20:31:11 +00003094 ensureLineBoxes();
3095
weinig@apple.com611b9292013-10-20 22:57:54 +00003096 if (!firstRootBox())
3097 return createVisiblePosition(0, DOWNSTREAM);
3098
akling@apple.com827be9c2013-10-29 02:58:43 +00003099 bool linesAreFlipped = style().isFlippedLinesWritingMode();
3100 bool blocksAreFlipped = style().isFlippedBlocksWritingMode();
weinig@apple.com611b9292013-10-20 22:57:54 +00003101
3102 // look for the closest line box in the root box which is at the passed-in y coordinate
3103 InlineBox* closestBox = 0;
3104 RootInlineBox* firstRootBoxWithChildren = 0;
3105 RootInlineBox* lastRootBoxWithChildren = 0;
3106 for (RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox()) {
3107 if (!root->firstLeafChild())
3108 continue;
3109 if (!firstRootBoxWithChildren)
3110 firstRootBoxWithChildren = root;
3111
3112 if (!linesAreFlipped && root->isFirstAfterPageBreak() && (pointInLogicalContents.y() < root->lineTopWithLeading()
3113 || (blocksAreFlipped && pointInLogicalContents.y() == root->lineTopWithLeading())))
3114 break;
3115
3116 lastRootBoxWithChildren = root;
3117
3118 // check if this root line box is located at this y coordinate
3119 if (pointInLogicalContents.y() < root->selectionBottom() || (blocksAreFlipped && pointInLogicalContents.y() == root->selectionBottom())) {
3120 if (linesAreFlipped) {
3121 RootInlineBox* nextRootBoxWithChildren = root->nextRootBox();
3122 while (nextRootBoxWithChildren && !nextRootBoxWithChildren->firstLeafChild())
3123 nextRootBoxWithChildren = nextRootBoxWithChildren->nextRootBox();
3124
3125 if (nextRootBoxWithChildren && nextRootBoxWithChildren->isFirstAfterPageBreak() && (pointInLogicalContents.y() > nextRootBoxWithChildren->lineTopWithLeading()
3126 || (!blocksAreFlipped && pointInLogicalContents.y() == nextRootBoxWithChildren->lineTopWithLeading())))
3127 continue;
3128 }
3129 closestBox = root->closestLeafChildForLogicalLeftPosition(pointInLogicalContents.x());
3130 if (closestBox)
3131 break;
3132 }
3133 }
3134
3135 bool moveCaretToBoundary = frame().editor().behavior().shouldMoveCaretToHorizontalBoundaryWhenPastTopOrBottom();
3136
3137 if (!moveCaretToBoundary && !closestBox && lastRootBoxWithChildren) {
3138 // y coordinate is below last root line box, pretend we hit it
3139 closestBox = lastRootBoxWithChildren->closestLeafChildForLogicalLeftPosition(pointInLogicalContents.x());
3140 }
3141
3142 if (closestBox) {
3143 if (moveCaretToBoundary) {
andersca@apple.com86298632013-11-10 19:32:33 +00003144 LayoutUnit firstRootBoxWithChildrenTop = std::min<LayoutUnit>(firstRootBoxWithChildren->selectionTop(), firstRootBoxWithChildren->logicalTop());
weinig@apple.com611b9292013-10-20 22:57:54 +00003145 if (pointInLogicalContents.y() < firstRootBoxWithChildrenTop
3146 || (blocksAreFlipped && pointInLogicalContents.y() == firstRootBoxWithChildrenTop)) {
3147 InlineBox* box = firstRootBoxWithChildren->firstLeafChild();
3148 if (box->isLineBreak()) {
3149 if (InlineBox* newBox = box->nextLeafChildIgnoringLineBreak())
3150 box = newBox;
3151 }
3152 // y coordinate is above first root line box, so return the start of the first
3153 return VisiblePosition(positionForBox(box, true), DOWNSTREAM);
3154 }
3155 }
3156
3157 // pass the box a top position that is inside it
3158 LayoutPoint point(pointInLogicalContents.x(), closestBox->root().blockDirectionPointInLine());
3159 if (!isHorizontalWritingMode())
3160 point = point.transposedPoint();
3161 if (closestBox->renderer().isReplaced())
weinig@apple.com12840dc2013-10-22 23:59:08 +00003162 return positionForPointRespectingEditingBoundaries(*this, toRenderBox(closestBox->renderer()), point);
weinig@apple.com611b9292013-10-20 22:57:54 +00003163 return closestBox->renderer().positionForPoint(point);
3164 }
3165
3166 if (lastRootBoxWithChildren) {
3167 // We hit this case for Mac behavior when the Y coordinate is below the last box.
3168 ASSERT(moveCaretToBoundary);
3169 InlineBox* logicallyLastBox;
3170 if (lastRootBoxWithChildren->getLogicalEndBoxWithNode(logicallyLastBox))
3171 return VisiblePosition(positionForBox(logicallyLastBox, false), DOWNSTREAM);
3172 }
3173
3174 // Can't reach this. We have a root line box, but it has no kids.
3175 // FIXME: This should ASSERT_NOT_REACHED(), but clicking on placeholder text
3176 // seems to hit this code path.
3177 return createVisiblePosition(0, DOWNSTREAM);
3178}
3179
commit-queue@webkit.org5ce6c902013-11-11 18:21:05 +00003180VisiblePosition RenderBlockFlow::positionForPoint(const LayoutPoint& point)
3181{
3182 if (auto fragment = renderNamedFlowFragment())
3183 return fragment->positionForPoint(point);
3184 return RenderBlock::positionForPoint(point);
3185}
3186
3187
weinig@apple.com611b9292013-10-20 22:57:54 +00003188void RenderBlockFlow::addFocusRingRectsForInlineChildren(Vector<IntRect>& rects, const LayoutPoint& additionalOffset, const RenderLayerModelObject*)
3189{
antti@apple.com940f5872013-10-24 20:31:11 +00003190 ASSERT(childrenInline());
3191
3192 ensureLineBoxes();
3193
weinig@apple.com611b9292013-10-20 22:57:54 +00003194 for (RootInlineBox* curr = firstRootBox(); curr; curr = curr->nextRootBox()) {
andersca@apple.com86298632013-11-10 19:32:33 +00003195 LayoutUnit top = std::max<LayoutUnit>(curr->lineTop(), curr->top());
3196 LayoutUnit bottom = std::min<LayoutUnit>(curr->lineBottom(), curr->top() + curr->height());
weinig@apple.com611b9292013-10-20 22:57:54 +00003197 LayoutRect rect(additionalOffset.x() + curr->x(), additionalOffset.y() + top, curr->width(), bottom - top);
3198 if (!rect.isEmpty())
3199 rects.append(pixelSnappedIntRect(rect));
3200 }
3201}
3202
3203void RenderBlockFlow::paintInlineChildren(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
3204{
3205 ASSERT(childrenInline());
antti@apple.com940f5872013-10-24 20:31:11 +00003206
antti@apple.comfea51992013-10-28 13:39:23 +00003207 if (m_simpleLineLayout) {
3208 SimpleLineLayout::paintFlow(*this, *m_simpleLineLayout, paintInfo, paintOffset);
antti@apple.com940f5872013-10-24 20:31:11 +00003209 return;
3210 }
weinig@apple.com611b9292013-10-20 22:57:54 +00003211 m_lineBoxes.paint(this, paintInfo, paintOffset);
3212}
3213
3214bool RenderBlockFlow::relayoutForPagination(bool hasSpecifiedPageLogicalHeight, LayoutUnit pageLogicalHeight, LayoutStateMaintainer& statePusher)
3215{
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003216 if (!hasColumns() && !multiColumnFlowThread())
weinig@apple.com611b9292013-10-20 22:57:54 +00003217 return false;
3218
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003219 if (hasColumns()) {
3220 RefPtr<RenderOverflow> savedOverflow = m_overflow.release();
3221 if (childrenInline())
3222 addOverflowFromInlineChildren();
3223 else
3224 addOverflowFromBlockChildren();
3225 LayoutUnit layoutOverflowLogicalBottom = (isHorizontalWritingMode() ? layoutOverflowRect().maxY() : layoutOverflowRect().maxX()) - borderAndPaddingBefore();
weinig@apple.com611b9292013-10-20 22:57:54 +00003226
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003227 // FIXME: We don't balance properly at all in the presence of forced page breaks. We need to understand what
3228 // the distance between forced page breaks is so that we can avoid making the minimum column height too tall.
3229 ColumnInfo* colInfo = columnInfo();
3230 if (!hasSpecifiedPageLogicalHeight) {
3231 LayoutUnit columnHeight = pageLogicalHeight;
3232 int minColumnCount = colInfo->forcedBreaks() + 1;
3233 int desiredColumnCount = colInfo->desiredColumnCount();
3234 if (minColumnCount >= desiredColumnCount) {
3235 // The forced page breaks are in control of the balancing. Just set the column height to the
3236 // maximum page break distance.
3237 if (!pageLogicalHeight) {
3238 LayoutUnit distanceBetweenBreaks = std::max<LayoutUnit>(colInfo->maximumDistanceBetweenForcedBreaks(),
3239 view().layoutState()->pageLogicalOffset(this, borderAndPaddingBefore() + layoutOverflowLogicalBottom) - colInfo->forcedBreakOffset());
3240 columnHeight = std::max(colInfo->minimumColumnHeight(), distanceBetweenBreaks);
3241 }
3242 } else if (layoutOverflowLogicalBottom > boundedMultiply(pageLogicalHeight, desiredColumnCount)) {
3243 // Now that we know the intrinsic height of the columns, we have to rebalance them.
3244 columnHeight = std::max<LayoutUnit>(colInfo->minimumColumnHeight(), ceilf((float)layoutOverflowLogicalBottom / desiredColumnCount));
weinig@apple.com611b9292013-10-20 22:57:54 +00003245 }
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003246
3247 if (columnHeight && columnHeight != pageLogicalHeight) {
3248 statePusher.pop();
3249 setEverHadLayout(true);
3250 layoutBlock(false, columnHeight);
3251 return true;
3252 }
3253 }
weinig@apple.com611b9292013-10-20 22:57:54 +00003254
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003255 if (pageLogicalHeight)
3256 colInfo->setColumnCountAndHeight(ceilf((float)layoutOverflowLogicalBottom / pageLogicalHeight), pageLogicalHeight);
weinig@apple.com611b9292013-10-20 22:57:54 +00003257
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003258 if (columnCount(colInfo)) {
3259 setLogicalHeight(borderAndPaddingBefore() + colInfo->columnHeight() + borderAndPaddingAfter() + scrollbarLogicalHeight());
3260 clearOverflow();
3261 } else
3262 m_overflow = savedOverflow.release();
3263 return false;
3264 }
weinig@apple.com611b9292013-10-20 22:57:54 +00003265
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003266 if (!multiColumnFlowThread()->shouldRelayoutForPagination())
3267 return false;
3268
3269 multiColumnFlowThread()->setNeedsRebalancing(false);
3270 multiColumnFlowThread()->setInBalancingPass(true); // Prevent re-entering this method (and recursion into layout).
3271
3272 bool needsRelayout;
3273 bool neededRelayout = false;
3274 bool firstPass = true;
3275 do {
3276 // Column heights may change here because of balancing. We may have to do multiple layout
3277 // passes, depending on how the contents is fitted to the changed column heights. In most
3278 // cases, laying out again twice or even just once will suffice. Sometimes we need more
3279 // passes than that, though, but the number of retries should not exceed the number of
3280 // columns, unless we have a bug.
3281 needsRelayout = false;
3282 for (RenderBox* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox())
3283 if (childBox != multiColumnFlowThread() && childBox->isRenderMultiColumnSet()) {
3284 RenderMultiColumnSet* multicolSet = toRenderMultiColumnSet(childBox);
3285 if (multicolSet->recalculateBalancedHeight(firstPass)) {
3286 multicolSet->setChildNeedsLayout(MarkOnlyThis);
3287 needsRelayout = true;
3288 }
3289 }
3290
3291 if (needsRelayout) {
3292 // Layout again. Column balancing resulted in a new height.
3293 neededRelayout = true;
3294 multiColumnFlowThread()->setChildNeedsLayout(MarkOnlyThis);
3295 setChildNeedsLayout(MarkOnlyThis);
3296 if (firstPass)
3297 statePusher.pop();
3298 layoutBlock(false);
3299 }
3300 firstPass = false;
3301 } while (needsRelayout);
3302
3303 multiColumnFlowThread()->setInBalancingPass(false);
3304
3305 return neededRelayout;
weinig@apple.com611b9292013-10-20 22:57:54 +00003306}
3307
antti@apple.com940f5872013-10-24 20:31:11 +00003308bool RenderBlockFlow::hasLines() const
3309{
3310 ASSERT(childrenInline());
3311
antti@apple.comfea51992013-10-28 13:39:23 +00003312 if (m_simpleLineLayout)
antti@apple.com09c83512013-10-30 14:12:32 +00003313 return m_simpleLineLayout->lineCount();
antti@apple.com940f5872013-10-24 20:31:11 +00003314
3315 return lineBoxes().firstLineBox();
3316}
3317
3318void RenderBlockFlow::layoutSimpleLines(LayoutUnit& repaintLogicalTop, LayoutUnit& repaintLogicalBottom)
3319{
3320 ASSERT(!m_lineBoxes.firstLineBox());
3321
antti@apple.comfea51992013-10-28 13:39:23 +00003322 m_simpleLineLayout = SimpleLineLayout::create(*this);
antti@apple.com940f5872013-10-24 20:31:11 +00003323
antti@apple.comfea51992013-10-28 13:39:23 +00003324 LayoutUnit lineLayoutHeight = SimpleLineLayout::computeFlowHeight(*this, *m_simpleLineLayout);
antti@apple.com940f5872013-10-24 20:31:11 +00003325 LayoutUnit lineLayoutTop = borderAndPaddingBefore();
3326
3327 repaintLogicalTop = lineLayoutTop;
3328 repaintLogicalBottom = lineLayoutTop + lineLayoutHeight;
3329
3330 setLogicalHeight(lineLayoutTop + lineLayoutHeight + borderAndPaddingAfter());
3331}
3332
3333void RenderBlockFlow::deleteLineBoxesBeforeSimpleLineLayout()
3334{
antti@apple.com42fb53d2013-10-25 02:33:11 +00003335 ASSERT(m_lineLayoutPath == SimpleLinesPath);
akling@apple.com31dd4f42013-10-30 22:27:59 +00003336 lineBoxes().deleteLineBoxes();
antti@apple.com940f5872013-10-24 20:31:11 +00003337 toRenderText(firstChild())->deleteLineBoxesBeforeSimpleLineLayout();
3338}
3339
3340void RenderBlockFlow::ensureLineBoxes()
3341{
antti@apple.com42fb53d2013-10-25 02:33:11 +00003342 m_lineLayoutPath = ForceLineBoxesPath;
antti@apple.com940f5872013-10-24 20:31:11 +00003343
antti@apple.comfea51992013-10-28 13:39:23 +00003344 if (!m_simpleLineLayout)
antti@apple.com940f5872013-10-24 20:31:11 +00003345 return;
antti@apple.comfea51992013-10-28 13:39:23 +00003346 m_simpleLineLayout = nullptr;
antti@apple.com940f5872013-10-24 20:31:11 +00003347
3348#if !ASSERT_DISABLED
3349 LayoutUnit oldHeight = logicalHeight();
3350#endif
3351 bool didNeedLayout = needsLayout();
3352
3353 bool relayoutChildren = false;
3354 LayoutUnit repaintLogicalTop;
3355 LayoutUnit repaintLogicalBottom;
3356 layoutLineBoxes(relayoutChildren, repaintLogicalTop, repaintLogicalBottom);
3357
3358 updateLogicalHeight();
3359 ASSERT(didNeedLayout || logicalHeight() == oldHeight);
3360
3361 if (!didNeedLayout)
3362 clearNeedsLayout();
3363}
3364
weinig@apple.com611b9292013-10-20 22:57:54 +00003365#ifndef NDEBUG
3366void RenderBlockFlow::showLineTreeAndMark(const InlineBox* markedBox1, const char* markedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const RenderObject* obj) const
3367{
3368 RenderBlock::showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLabel2, obj);
3369 for (const RootInlineBox* root = firstRootBox(); root; root = root->nextRootBox())
3370 root->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLabel2, obj, 1);
3371}
3372#endif
3373
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00003374RenderBlockFlow::RenderBlockFlowRareData& RenderBlockFlow::ensureRareBlockFlowData()
3375{
3376 if (hasRareBlockFlowData())
3377 return *m_rareBlockFlowData;
3378 materializeRareBlockFlowData();
3379 return *m_rareBlockFlowData;
3380}
3381
3382void RenderBlockFlow::materializeRareBlockFlowData()
3383{
3384 ASSERT(!hasRareBlockFlowData());
3385 m_rareBlockFlowData = std::make_unique<RenderBlockFlow::RenderBlockFlowRareData>(*this);
3386}
3387
aestes@apple.com6751d842014-01-12 02:51:25 +00003388#if ENABLE(IOS_TEXT_AUTOSIZING)
3389inline static bool isVisibleRenderText(RenderObject* renderer)
3390{
3391 if (!renderer->isText())
3392 return false;
3393 RenderText* renderText = toRenderText(renderer);
3394 return !renderText->linesBoundingBox().isEmpty() && !renderText->text()->containsOnlyWhitespace();
3395}
3396
3397inline static bool resizeTextPermitted(RenderObject* render)
3398{
3399 // We disallow resizing for text input fields and textarea to address <rdar://problem/5792987> and <rdar://problem/8021123>
3400 auto renderer = render->parent();
3401 while (renderer) {
3402 // Get the first non-shadow HTMLElement and see if it's an input.
3403 if (renderer->element() && renderer->element()->isHTMLElement() && !renderer->element()->isInShadowTree()) {
3404 const HTMLElement& element = toHTMLElement(*renderer->element());
3405 return !isHTMLInputElement(element) && !isHTMLTextAreaElement(element);
3406 }
3407 renderer = renderer->parent();
3408 }
3409 return true;
3410}
3411
3412int RenderBlockFlow::immediateLineCount()
3413{
3414 // Copied and modified from RenderBlock::lineCount.
3415 // Only descend into list items.
3416 int count = 0;
3417 if (style().visibility() == VISIBLE) {
3418 if (childrenInline()) {
3419 for (RootInlineBox* box = firstRootBox(); box; box = box->nextRootBox())
3420 count++;
3421 } else {
3422 for (RenderObject* obj = firstChild(); obj; obj = obj->nextSibling()) {
3423 if (obj->isListItem())
3424 count += toRenderBlockFlow(obj)->lineCount();
3425 }
3426 }
3427 }
3428 return count;
3429}
3430
3431static bool isNonBlocksOrNonFixedHeightListItems(const RenderObject* render)
3432{
3433 if (!render->isRenderBlock())
3434 return true;
3435 if (render->isListItem())
3436 return render->style().height().type() != Fixed;
3437 return false;
3438}
3439
3440// For now, we auto size single lines of text the same as multiple lines.
3441// We've been experimenting with low values for single lines of text.
3442static inline float oneLineTextMultiplier(float specifiedSize)
3443{
3444 return std::max((1.0f / log10f(specifiedSize) * 1.7f), 1.0f);
3445}
3446
3447static inline float textMultiplier(float specifiedSize)
3448{
3449 return std::max((1.0f / log10f(specifiedSize) * 1.95f), 1.0f);
3450}
3451
3452void RenderBlockFlow::adjustComputedFontSizes(float size, float visibleWidth)
3453{
3454 // Don't do any work if the block is smaller than the visible area.
3455 if (visibleWidth >= width())
3456 return;
3457
3458 unsigned lineCount;
3459 if (m_lineCountForTextAutosizing == NOT_SET) {
3460 int count = immediateLineCount();
3461 if (!count)
3462 lineCount = NO_LINE;
3463 else if (count == 1)
3464 lineCount = ONE_LINE;
3465 else
3466 lineCount = MULTI_LINE;
3467 } else
3468 lineCount = m_lineCountForTextAutosizing;
3469
3470 ASSERT(lineCount != NOT_SET);
3471 if (lineCount == NO_LINE)
3472 return;
3473
3474 float actualWidth = m_widthForTextAutosizing != -1 ? static_cast<float>(m_widthForTextAutosizing) : static_cast<float>(width());
3475 float scale = visibleWidth / actualWidth;
3476 float minFontSize = roundf(size / scale);
3477
3478 for (RenderObject* descendent = traverseNext(this, isNonBlocksOrNonFixedHeightListItems); descendent; descendent = descendent->traverseNext(this, isNonBlocksOrNonFixedHeightListItems)) {
3479 if (isVisibleRenderText(descendent) && resizeTextPermitted(descendent)) {
3480 RenderText* text = toRenderText(descendent);
3481 RenderStyle& oldStyle = text->style();
3482 FontDescription fontDescription = oldStyle.fontDescription();
3483 float specifiedSize = fontDescription.specifiedSize();
3484 float scaledSize = roundf(specifiedSize * scale);
3485 if (scaledSize > 0 && scaledSize < minFontSize) {
3486 // 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.
3487 // This makes text resizing consistent even if the block's width or line count changes (which can be caused by text resizing itself 5159915).
3488 if (m_lineCountForTextAutosizing == NOT_SET)
3489 m_lineCountForTextAutosizing = lineCount;
3490 if (m_widthForTextAutosizing == -1)
3491 m_widthForTextAutosizing = actualWidth;
3492
3493 float candidateNewSize = 0;
3494 float lineTextMultiplier = lineCount == ONE_LINE ? oneLineTextMultiplier(specifiedSize) : textMultiplier(specifiedSize);
3495 candidateNewSize = roundf(std::min(minFontSize, specifiedSize * lineTextMultiplier));
3496 if (candidateNewSize > specifiedSize && candidateNewSize != fontDescription.computedSize() && text->textNode() && oldStyle.textSizeAdjust().isAuto())
3497 document().addAutoSizingNode(text->textNode(), candidateNewSize);
3498 }
3499 }
3500 }
3501}
3502#endif // ENABLE(IOS_TEXT_AUTOSIZING)
3503
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003504RenderObject* RenderBlockFlow::layoutSpecialExcludedChild(bool relayoutChildren)
3505{
3506 if (!multiColumnFlowThread())
3507 return 0;
3508
3509 // Update the dimensions of our regions before we lay out the flow thread.
3510 // FIXME: Eventually this is going to get way more complicated, and we will be destroying regions
3511 // instead of trying to keep them around.
3512 bool shouldInvalidateRegions = false;
3513 for (RenderBox* childBox = firstChildBox(); childBox; childBox = childBox->nextSiblingBox()) {
3514 if (childBox == multiColumnFlowThread())
3515 continue;
3516
3517 if (relayoutChildren || childBox->needsLayout()) {
3518 if (!multiColumnFlowThread()->inBalancingPass() && childBox->isRenderMultiColumnSet())
3519 toRenderMultiColumnSet(childBox)->prepareForLayout();
3520 shouldInvalidateRegions = true;
3521 }
3522 }
3523
3524 if (shouldInvalidateRegions)
3525 multiColumnFlowThread()->invalidateRegions();
3526
3527 if (relayoutChildren)
3528 multiColumnFlowThread()->setChildNeedsLayout(MarkOnlyThis);
3529
3530 if (multiColumnFlowThread()->requiresBalancing()) {
3531 // At the end of multicol layout, relayoutForPagination() is called unconditionally, but if
3532 // no children are to be laid out (e.g. fixed width with layout already being up-to-date),
3533 // we want to prevent it from doing any work, so that the column balancing machinery doesn't
3534 // kick in and trigger additional unnecessary layout passes. Actually, it's not just a good
3535 // idea in general to not waste time on balancing content that hasn't been re-laid out; we
3536 // are actually required to guarantee this. The calculation of implicit breaks needs to be
3537 // preceded by a proper layout pass, since it's layout that sets up content runs, and the
3538 // runs get deleted right after every pass.
3539 multiColumnFlowThread()->setNeedsRebalancing(shouldInvalidateRegions || multiColumnFlowThread()->needsLayout());
3540 }
3541
3542 setLogicalTopForChild(*multiColumnFlowThread(), borderAndPaddingBefore());
3543 multiColumnFlowThread()->layoutIfNeeded();
3544 determineLogicalLeftPositionForChild(*multiColumnFlowThread());
3545
3546 return multiColumnFlowThread();
3547}
3548
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003549void RenderBlockFlow::addChild(RenderObject* newChild, RenderObject* beforeChild)
3550{
3551 if (multiColumnFlowThread())
3552 return multiColumnFlowThread()->addChild(newChild, beforeChild);
3553 RenderBlock::addChild(newChild, beforeChild);
3554}
3555
hyatt@apple.comd4be3772014-01-24 19:55:33 +00003556void RenderBlockFlow::checkForPaginationLogicalHeightChange(LayoutUnit& pageLogicalHeight, bool& pageLogicalHeightChanged, bool& hasSpecifiedPageLogicalHeight)
3557{
3558 // If we don't use either of the two column implementations or a flow thread, then bail.
3559 if (!isRenderFlowThread() && !multiColumnFlowThread() && !hasColumns())
3560 return;
3561
3562 // We don't actually update any of the variables. We just subclassed to adjust our column height.
3563 if (multiColumnFlowThread()) {
3564 updateLogicalHeight();
3565 multiColumnFlowThread()->setColumnHeightAvailable(std::max<LayoutUnit>(contentLogicalHeight(), 0));
3566 setLogicalHeight(0);
3567 } else if (hasColumns()) {
3568 ColumnInfo* colInfo = columnInfo();
3569
3570 if (!pageLogicalHeight) {
3571 // We need to go ahead and set our explicit page height if one exists, so that we can
3572 // avoid doing two layout passes.
3573 updateLogicalHeight();
3574 LayoutUnit columnHeight = isRenderView() ? view().pageOrViewLogicalHeight() : contentLogicalHeight();
3575 if (columnHeight > 0) {
3576 pageLogicalHeight = columnHeight;
3577 hasSpecifiedPageLogicalHeight = true;
3578 }
3579 setLogicalHeight(0);
3580 }
3581
3582 if (colInfo->columnHeight() != pageLogicalHeight && everHadLayout())
3583 pageLogicalHeightChanged = true;
3584
3585 colInfo->setColumnHeight(pageLogicalHeight);
3586
3587 if (!hasSpecifiedPageLogicalHeight && !pageLogicalHeight)
3588 colInfo->clearForcedBreaks();
3589
3590 colInfo->setPaginationUnit(paginationUnit());
3591 } else if (isRenderFlowThread()) {
3592 pageLogicalHeight = 1; // This is just a hack to always make sure we have a page logical height.
3593 pageLogicalHeightChanged = toRenderFlowThread(this)->pageLogicalSizeChanged();
3594 }
3595}
3596
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003597void RenderBlockFlow::setComputedColumnCountAndWidth(int count, LayoutUnit width)
3598{
3599 if (!document().regionBasedColumnsEnabled())
3600 return RenderBlock::setComputedColumnCountAndWidth(count, width);
3601
3602 bool destroyColumns = !requiresColumns(count);
3603 if (destroyColumns) {
3604 if (multiColumnFlowThread())
3605 destroyMultiColumnFlowThread();
3606 } else {
3607 if (!multiColumnFlowThread())
3608 createMultiColumnFlowThread();
3609 multiColumnFlowThread()->setColumnCountAndWidth(count, width);
hyatt@apple.com86919862014-01-27 16:27:45 +00003610 multiColumnFlowThread()->setProgressionIsInline(style().hasInlineColumnAxis());
3611 multiColumnFlowThread()->setProgressionIsReversed(style().columnProgression() == ReverseColumnProgression);
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003612 }
3613}
3614
hyatt@apple.com86919862014-01-27 16:27:45 +00003615void RenderBlockFlow::updateColumnProgressionFromStyle(RenderStyle* style)
3616{
3617 if (!document().regionBasedColumnsEnabled())
3618 return RenderBlock::updateColumnProgressionFromStyle(style);
3619
3620 if (!multiColumnFlowThread())
3621 return;
3622
3623 bool needsLayout = false;
3624 bool oldProgressionIsInline = multiColumnFlowThread()->progressionIsInline();
3625 bool newProgressionIsInline = style->hasInlineColumnAxis();
3626 if (oldProgressionIsInline != newProgressionIsInline) {
3627 multiColumnFlowThread()->setProgressionIsInline(newProgressionIsInline);
3628 needsLayout = true;
3629 }
3630
3631 bool oldProgressionIsReversed = multiColumnFlowThread()->progressionIsReversed();
3632 bool newProgressionIsReversed = style->columnProgression() == ReverseColumnProgression;
3633 if (oldProgressionIsReversed != newProgressionIsReversed) {
3634 multiColumnFlowThread()->setProgressionIsReversed(newProgressionIsReversed);
3635 needsLayout = true;
3636 }
3637
3638 if (needsLayout)
3639 setNeedsLayoutAndPrefWidthsRecalc();
3640}
3641
hyatt@apple.com39746fd2014-01-24 22:52:41 +00003642LayoutUnit RenderBlockFlow::computedColumnWidth() const
3643{
3644 if (!document().regionBasedColumnsEnabled())
3645 return RenderBlock::computedColumnWidth();
3646
3647 if (multiColumnFlowThread())
3648 return multiColumnFlowThread()->computedColumnWidth();
3649 return contentLogicalWidth();
3650}
3651
3652unsigned RenderBlockFlow::computedColumnCount() const
3653{
3654 if (!document().regionBasedColumnsEnabled())
3655 return RenderBlock::computedColumnCount();
3656
3657 if (multiColumnFlowThread())
3658 return multiColumnFlowThread()->computedColumnCount();
3659
3660 return 1;
3661}
3662
hyatt@apple.com31a5daa2014-01-28 01:26:37 +00003663bool RenderBlockFlow::isTopLayoutOverflowAllowed() const
3664{
3665 bool hasTopOverflow = RenderBlock::isTopLayoutOverflowAllowed();
3666 if (!multiColumnFlowThread() || style().columnProgression() == NormalColumnProgression)
3667 return hasTopOverflow;
3668
3669 if (!(isHorizontalWritingMode() ^ !style().hasInlineColumnAxis()))
3670 hasTopOverflow = !hasTopOverflow;
3671
3672 return hasTopOverflow;
3673}
3674
3675bool RenderBlockFlow::isLeftLayoutOverflowAllowed() const
3676{
3677 bool hasLeftOverflow = RenderBlock::isLeftLayoutOverflowAllowed();
3678 if (!multiColumnFlowThread() || style().columnProgression() == NormalColumnProgression)
3679 return hasLeftOverflow;
3680
3681 if (isHorizontalWritingMode() ^ !style().hasInlineColumnAxis())
3682 hasLeftOverflow = !hasLeftOverflow;
3683
3684 return hasLeftOverflow;
3685}
3686
hyatt@apple.come96ebaf2013-11-12 22:51:12 +00003687}
3688// namespace WebCore