Bug 1763869 [wpt PR 33577] - Fix adb command to find webview package, a=testonly
[gecko.git] / layout / generic / nsBlockReflowContext.cpp
blob3090ab9279814c7d61d9cfcf67616e0b234b789f
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* class that a parent frame uses to reflow a block frame */
9 #include "nsBlockReflowContext.h"
10 #include "BlockReflowState.h"
11 #include "nsFloatManager.h"
12 #include "nsColumnSetFrame.h"
13 #include "nsContainerFrame.h"
14 #include "nsBlockFrame.h"
15 #include "nsLineBox.h"
16 #include "nsLayoutUtils.h"
18 using namespace mozilla;
20 #ifdef DEBUG
21 # include "nsBlockDebugFlags.h" // For NOISY_BLOCK_DIR_MARGINS
22 #endif
24 nsBlockReflowContext::nsBlockReflowContext(nsPresContext* aPresContext,
25 const ReflowInput& aParentRI)
26 : mPresContext(aPresContext),
27 mOuterReflowInput(aParentRI),
28 mFrame(nullptr),
29 mSpace(aParentRI.GetWritingMode()),
30 mICoord(0),
31 mBCoord(0),
32 mMetrics(aParentRI) {}
34 static nsIFrame* DescendIntoBlockLevelFrame(nsIFrame* aFrame) {
35 LayoutFrameType type = aFrame->Type();
36 if (type == LayoutFrameType::ColumnSet) {
37 static_cast<nsColumnSetFrame*>(aFrame)->DrainOverflowColumns();
38 nsIFrame* child = aFrame->PrincipalChildList().FirstChild();
39 if (child) {
40 return DescendIntoBlockLevelFrame(child);
43 return aFrame;
46 bool nsBlockReflowContext::ComputeCollapsedBStartMargin(
47 const ReflowInput& aRI, nsCollapsingMargin* aMargin,
48 nsIFrame* aClearanceFrame, bool* aMayNeedRetry, bool* aBlockIsEmpty) {
49 WritingMode wm = aRI.GetWritingMode();
50 WritingMode parentWM = mMetrics.GetWritingMode();
52 // Include block-start element of frame's margin
53 aMargin->Include(aRI.ComputedLogicalMargin(parentWM).BStart(parentWM));
55 // The inclusion of the block-end margin when empty is done by the caller
56 // since it doesn't need to be done by the top-level (non-recursive)
57 // caller.
59 #ifdef NOISY_BLOCK_DIR_MARGINS
60 aRI.mFrame->ListTag(stdout);
61 printf(": %d => %d\n", aRI.ComputedLogicalMargin(wm).BStart(wm),
62 aMargin->get());
63 #endif
65 bool dirtiedLine = false;
66 bool setBlockIsEmpty = false;
68 // Calculate the frame's generational block-start-margin from its child
69 // blocks. Note that if the frame has a non-zero block-start-border or
70 // block-start-padding then this step is skipped because it will be a margin
71 // root. It is also skipped if the frame is a margin root for other
72 // reasons.
73 nsIFrame* frame = DescendIntoBlockLevelFrame(aRI.mFrame);
74 nsPresContext* prescontext = frame->PresContext();
75 nsBlockFrame* block = nullptr;
76 if (0 == aRI.ComputedLogicalBorderPadding(wm).BStart(wm)) {
77 block = do_QueryFrame(frame);
78 if (block) {
79 bool bStartMarginRoot, unused;
80 block->IsMarginRoot(&bStartMarginRoot, &unused);
81 if (bStartMarginRoot) {
82 block = nullptr;
87 // iterate not just through the lines of 'block' but also its
88 // overflow lines and the normal and overflow lines of its next in
89 // flows. Note that this will traverse some frames more than once:
90 // for example, if A contains B and A->nextinflow contains
91 // B->nextinflow, we'll traverse B->nextinflow twice. But this is
92 // OK because our traversal is idempotent.
93 for (; block; block = static_cast<nsBlockFrame*>(block->GetNextInFlow())) {
94 for (int overflowLines = 0; overflowLines <= 1; ++overflowLines) {
95 nsBlockFrame::LineIterator line;
96 nsBlockFrame::LineIterator line_end;
97 bool anyLines = true;
98 if (overflowLines) {
99 nsBlockFrame::FrameLines* frames = block->GetOverflowLines();
100 nsLineList* lines = frames ? &frames->mLines : nullptr;
101 if (!lines) {
102 anyLines = false;
103 } else {
104 line = lines->begin();
105 line_end = lines->end();
107 } else {
108 line = block->LinesBegin();
109 line_end = block->LinesEnd();
111 for (; anyLines && line != line_end; ++line) {
112 if (!aClearanceFrame && line->HasClearance()) {
113 // If we don't have a clearance frame, then we're computing
114 // the collapsed margin in the first pass, assuming that all
115 // lines have no clearance. So clear their clearance flags.
116 line->ClearHasClearance();
117 line->MarkDirty();
118 dirtiedLine = true;
121 bool isEmpty;
122 if (line->IsInline()) {
123 isEmpty = line->IsEmpty();
124 } else {
125 nsIFrame* kid = line->mFirstChild;
126 if (kid == aClearanceFrame) {
127 line->SetHasClearance();
128 line->MarkDirty();
129 dirtiedLine = true;
130 if (!setBlockIsEmpty && aBlockIsEmpty) {
131 setBlockIsEmpty = true;
132 *aBlockIsEmpty = false;
134 goto done;
136 // Here is where we recur. Now that we have determined that a
137 // generational collapse is required we need to compute the
138 // child blocks margin and so in so that we can look into
139 // it. For its margins to be computed we need to have a reflow
140 // input for it.
142 // We may have to construct an extra reflow input here if
143 // we drilled down through a block wrapper. At the moment
144 // we can only drill down one level so we only have to support
145 // one extra reflow input.
146 const ReflowInput* outerReflowInput = &aRI;
147 if (frame != aRI.mFrame) {
148 NS_ASSERTION(frame->GetParent() == aRI.mFrame,
149 "Can only drill through one level of block wrapper");
150 LogicalSize availSpace = aRI.ComputedSize(frame->GetWritingMode());
151 outerReflowInput =
152 new ReflowInput(prescontext, aRI, frame, availSpace);
155 LogicalSize availSpace =
156 outerReflowInput->ComputedSize(kid->GetWritingMode());
157 ReflowInput innerReflowInput(prescontext, *outerReflowInput, kid,
158 availSpace);
159 // Record that we're being optimistic by assuming the kid
160 // has no clearance
161 if (kid->StyleDisplay()->mBreakType != StyleClear::None ||
162 !nsBlockFrame::BlockCanIntersectFloats(kid)) {
163 *aMayNeedRetry = true;
165 if (ComputeCollapsedBStartMargin(innerReflowInput, aMargin,
166 aClearanceFrame, aMayNeedRetry,
167 &isEmpty)) {
168 line->MarkDirty();
169 dirtiedLine = true;
171 if (isEmpty) {
172 LogicalMargin innerMargin =
173 innerReflowInput.ComputedLogicalMargin(parentWM);
174 aMargin->Include(innerMargin.BEnd(parentWM));
177 if (outerReflowInput != &aRI) {
178 delete const_cast<ReflowInput*>(outerReflowInput);
181 if (!isEmpty) {
182 if (!setBlockIsEmpty && aBlockIsEmpty) {
183 setBlockIsEmpty = true;
184 *aBlockIsEmpty = false;
186 goto done;
189 if (!setBlockIsEmpty && aBlockIsEmpty) {
190 // The first time we reach here is when this is the first block
191 // and we have processed all its normal lines.
192 setBlockIsEmpty = true;
193 // All lines are empty, or we wouldn't be here!
194 *aBlockIsEmpty = aRI.mFrame->IsSelfEmpty();
198 done:
200 if (!setBlockIsEmpty && aBlockIsEmpty) {
201 *aBlockIsEmpty = aRI.mFrame->IsEmpty();
204 #ifdef NOISY_BLOCK_DIR_MARGINS
205 aRI.mFrame->ListTag(stdout);
206 printf(": => %d\n", aMargin->get());
207 #endif
209 return dirtiedLine;
212 void nsBlockReflowContext::ReflowBlock(
213 const LogicalRect& aSpace, bool aApplyBStartMargin,
214 nsCollapsingMargin& aPrevMargin, nscoord aClearance,
215 bool aIsAdjacentWithBStart, nsLineBox* aLine, ReflowInput& aFrameRI,
216 nsReflowStatus& aFrameReflowStatus, BlockReflowState& aState) {
217 mFrame = aFrameRI.mFrame;
218 mWritingMode = aState.mReflowInput.GetWritingMode();
219 mContainerSize = aState.ContainerSize();
220 mSpace = aSpace;
222 if (!aIsAdjacentWithBStart) {
223 aFrameRI.mFlags.mIsTopOfPage = false; // make sure this is cleared
226 if (aApplyBStartMargin) {
227 mBStartMargin = aPrevMargin;
229 #ifdef NOISY_BLOCK_DIR_MARGINS
230 mOuterReflowInput.mFrame->ListTag(stdout);
231 printf(": reflowing ");
232 mFrame->ListTag(stdout);
233 printf(" margin => %d, clearance => %d\n", mBStartMargin.get(), aClearance);
234 #endif
236 // Adjust the available size if it's constrained so that the
237 // child frame doesn't think it can reflow into its margin area.
238 if (mWritingMode.IsOrthogonalTo(mFrame->GetWritingMode())) {
239 if (NS_UNCONSTRAINEDSIZE != aFrameRI.AvailableISize()) {
240 aFrameRI.AvailableISize() -= mBStartMargin.get() + aClearance;
241 aFrameRI.AvailableISize() = std::max(0, aFrameRI.AvailableISize());
243 } else {
244 if (NS_UNCONSTRAINEDSIZE != aFrameRI.AvailableBSize()) {
245 aFrameRI.AvailableBSize() -= mBStartMargin.get() + aClearance;
246 aFrameRI.AvailableBSize() = std::max(0, aFrameRI.AvailableBSize());
249 } else {
250 // nsBlockFrame::ReflowBlock might call us multiple times with
251 // *different* values of aApplyBStartMargin.
252 mBStartMargin.Zero();
255 nscoord tI = 0, tB = 0;
256 // The values of x and y do not matter for floats, so don't bother
257 // calculating them. Floats are guaranteed to have their own float
258 // manager, so tI and tB don't matter. mICoord and mBCoord don't
259 // matter becacuse they are only used in PlaceBlock, which is not used
260 // for floats.
261 if (aLine) {
262 // Compute inline/block coordinate where reflow will begin. Use the
263 // rules from 10.3.3 to determine what to apply. At this point in the
264 // reflow auto inline-start/end margins will have a zero value.
265 LogicalMargin usedMargin = aFrameRI.ComputedLogicalMargin(mWritingMode);
266 mICoord = mSpace.IStart(mWritingMode) + usedMargin.IStart(mWritingMode);
267 mBCoord = mSpace.BStart(mWritingMode) + mBStartMargin.get() + aClearance;
269 LogicalRect space(
270 mWritingMode, mICoord, mBCoord,
271 mSpace.ISize(mWritingMode) - usedMargin.IStartEnd(mWritingMode),
272 mSpace.BSize(mWritingMode) - usedMargin.BStartEnd(mWritingMode));
273 tI = space.LineLeft(mWritingMode, mContainerSize);
274 tB = mBCoord;
276 if (!mFrame->HasAnyStateBits(NS_BLOCK_FLOAT_MGR)) {
277 aFrameRI.mBlockDelta =
278 mOuterReflowInput.mBlockDelta + mBCoord - aLine->BStart();
282 #ifdef DEBUG
283 mMetrics.ISize(mWritingMode) = nscoord(0xdeadbeef);
284 mMetrics.BSize(mWritingMode) = nscoord(0xdeadbeef);
285 #endif
287 mOuterReflowInput.mFloatManager->Translate(tI, tB);
288 mFrame->Reflow(mPresContext, mMetrics, aFrameRI, aFrameReflowStatus);
289 mOuterReflowInput.mFloatManager->Translate(-tI, -tB);
291 #ifdef DEBUG
292 if (!aFrameReflowStatus.IsInlineBreakBefore()) {
293 if ((ABSURD_SIZE(mMetrics.ISize(mWritingMode)) ||
294 ABSURD_SIZE(mMetrics.BSize(mWritingMode))) &&
295 !mFrame->GetParent()->IsAbsurdSizeAssertSuppressed()) {
296 printf("nsBlockReflowContext: ");
297 mFrame->ListTag(stdout);
298 printf(" metrics=%d,%d!\n", mMetrics.ISize(mWritingMode),
299 mMetrics.BSize(mWritingMode));
301 if ((mMetrics.ISize(mWritingMode) == nscoord(0xdeadbeef)) ||
302 (mMetrics.BSize(mWritingMode) == nscoord(0xdeadbeef))) {
303 printf("nsBlockReflowContext: ");
304 mFrame->ListTag(stdout);
305 printf(" didn't set i/b %d,%d!\n", mMetrics.ISize(mWritingMode),
306 mMetrics.BSize(mWritingMode));
309 #endif
311 if (!mFrame->HasOverflowAreas()) {
312 mMetrics.SetOverflowAreasToDesiredBounds();
315 if (!aFrameReflowStatus.IsInlineBreakBefore() &&
316 !aFrameRI.WillReflowAgainForClearance() &&
317 aFrameReflowStatus.IsFullyComplete()) {
318 // If mFrame is fully-complete and has a next-in-flow, we need to delete
319 // them now. Do not do this when a break-before is signaled or when a
320 // clearance frame is discovered in mFrame's subtree because mFrame is going
321 // to get reflowed again (whether the frame is (in)complete is undefined in
322 // that case anyway).
323 if (nsIFrame* kidNextInFlow = mFrame->GetNextInFlow()) {
324 // Remove all of the childs next-in-flows. Make sure that we ask
325 // the right parent to do the removal (it's possible that the
326 // parent is not this because we are executing pullup code).
327 // Floats will eventually be removed via nsBlockFrame::RemoveFloat
328 // which detaches the placeholder from the float.
329 nsOverflowContinuationTracker::AutoFinish fini(aState.mOverflowTracker,
330 mFrame);
331 kidNextInFlow->GetParent()->DeleteNextInFlowChild(kidNextInFlow, true);
337 * Attempt to place the block frame within the available space. If
338 * it fits, apply inline-dir ("horizontal") positioning (CSS 10.3.3),
339 * collapse margins (CSS2 8.3.1). Also apply relative positioning.
341 bool nsBlockReflowContext::PlaceBlock(const ReflowInput& aReflowInput,
342 bool aForceFit, nsLineBox* aLine,
343 nsCollapsingMargin& aBEndMarginResult,
344 OverflowAreas& aOverflowAreas,
345 const nsReflowStatus& aReflowStatus) {
346 // Compute collapsed block-end margin value.
347 WritingMode parentWM = mMetrics.GetWritingMode();
349 // Don't apply the block-end margin if the block has a *later* sibling across
350 // column-span split.
351 if (aReflowStatus.IsComplete() && !mFrame->HasColumnSpanSiblings()) {
352 aBEndMarginResult = mMetrics.mCarriedOutBEndMargin;
353 aBEndMarginResult.Include(
354 aReflowInput.ComputedLogicalMargin(parentWM).BEnd(parentWM));
355 } else {
356 // The used block-end-margin is set to zero before a break.
357 aBEndMarginResult.Zero();
360 nscoord backupContainingBlockAdvance = 0;
362 // Check whether the block's block-end margin collapses with its block-start
363 // margin. See CSS 2.1 section 8.3.1; those rules seem to match
364 // nsBlockFrame::IsEmpty(). Any such block must have zero block-size so
365 // check that first. Note that a block can have clearance and still
366 // have adjoining block-start/end margins, because the clearance goes
367 // above the block-start margin.
368 // Mark the frame as non-dirty; it has been reflowed (or we wouldn't
369 // be here), and we don't want to assert in CachedIsEmpty()
370 mFrame->RemoveStateBits(NS_FRAME_IS_DIRTY);
371 bool empty = 0 == mMetrics.BSize(parentWM) && aLine->CachedIsEmpty();
372 if (empty) {
373 // Collapse the block-end margin with the block-start margin that was
374 // already applied.
375 aBEndMarginResult.Include(mBStartMargin);
377 #ifdef NOISY_BLOCK_DIR_MARGINS
378 printf(" ");
379 mOuterReflowInput.mFrame->ListTag(stdout);
380 printf(": ");
381 mFrame->ListTag(stdout);
382 printf(
383 " -- collapsing block start & end margin together; BStart=%d "
384 "spaceBStart=%d\n",
385 mBCoord, mSpace.BStart(mWritingMode));
386 #endif
387 // Section 8.3.1 of CSS 2.1 says that blocks with adjoining
388 // "top/bottom" (i.e. block-start/end) margins whose top margin collapses
389 // with their parent's top margin should have their top border-edge at the
390 // top border-edge of their parent. We actually don't have to do
391 // anything special to make this happen. In that situation,
392 // nsBlockFrame::ShouldApplyBStartMargin will have returned false,
393 // and mBStartMargin and aClearance will have been zero in
394 // ReflowBlock.
396 // If we did apply our block-start margin, but now we're collapsing it
397 // into the block-end margin, we need to back up the containing
398 // block's bCoord-advance by our block-start margin so that it doesn't get
399 // counted twice. Note that here we're allowing the line's bounds
400 // to become different from the block's position; we do this
401 // because the containing block will place the next line at the
402 // line's BEnd, and it must place the next line at a different
403 // point from where this empty block will be.
404 backupContainingBlockAdvance = mBStartMargin.get();
407 // See if the frame fit. If it's the first frame or empty then it
408 // always fits. If the block-size is unconstrained then it always fits,
409 // even if there's some sort of integer overflow that makes bCoord +
410 // mMetrics.BSize() appear to go beyond the available block size.
411 if (!empty && !aForceFit &&
412 mSpace.BSize(mWritingMode) != NS_UNCONSTRAINEDSIZE) {
413 nscoord bEnd =
414 mBCoord - backupContainingBlockAdvance + mMetrics.BSize(mWritingMode);
415 if (bEnd > mSpace.BEnd(mWritingMode)) {
416 // didn't fit, we must acquit.
417 mFrame->DidReflow(mPresContext, &aReflowInput);
418 return false;
422 aLine->SetBounds(mWritingMode, mICoord,
423 mBCoord - backupContainingBlockAdvance,
424 mMetrics.ISize(mWritingMode), mMetrics.BSize(mWritingMode),
425 mContainerSize);
427 // Now place the frame and complete the reflow process
428 nsContainerFrame::FinishReflowChild(
429 mFrame, mPresContext, mMetrics, &aReflowInput, mWritingMode,
430 LogicalPoint(mWritingMode, mICoord, mBCoord), mContainerSize,
431 nsIFrame::ReflowChildFlags::ApplyRelativePositioning);
433 aOverflowAreas = mMetrics.mOverflowAreas + mFrame->GetPosition();
435 return true;