Bug 1874684 - Part 20: Tag stack classes with MOZ_STACK_CLASS. r=allstarschh
[gecko.git] / layout / generic / nsContainerFrame.cpp
blob2c9e707830b0c12ef8afc11d9e988d5877ce6e8c
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 /* base class #1 for rendering objects that have child lists */
9 #include "nsContainerFrame.h"
10 #include "mozilla/widget/InitData.h"
11 #include "nsContainerFrameInlines.h"
13 #include "mozilla/ComputedStyle.h"
14 #include "mozilla/PresShell.h"
15 #include "mozilla/dom/HTMLSummaryElement.h"
16 #include "mozilla/gfx/2D.h"
17 #include "mozilla/gfx/Types.h"
18 #include "nsAbsoluteContainingBlock.h"
19 #include "nsAttrValue.h"
20 #include "nsAttrValueInlines.h"
21 #include "nsFlexContainerFrame.h"
22 #include "nsFrameSelection.h"
23 #include "mozilla/dom/Document.h"
24 #include "nsPresContext.h"
25 #include "nsRect.h"
26 #include "nsPoint.h"
27 #include "nsStyleConsts.h"
28 #include "nsView.h"
29 #include "nsCOMPtr.h"
30 #include "nsGkAtoms.h"
31 #include "nsViewManager.h"
32 #include "nsIWidget.h"
33 #include "nsCanvasFrame.h"
34 #include "nsCSSRendering.h"
35 #include "nsError.h"
36 #include "nsDisplayList.h"
37 #include "nsIBaseWindow.h"
38 #include "nsCSSFrameConstructor.h"
39 #include "nsBlockFrame.h"
40 #include "nsPlaceholderFrame.h"
41 #include "mozilla/AutoRestore.h"
42 #include "nsIFrameInlines.h"
43 #include "nsPrintfCString.h"
44 #include "mozilla/webrender/WebRenderAPI.h"
45 #include <algorithm>
47 using namespace mozilla;
48 using namespace mozilla::dom;
49 using namespace mozilla::layout;
51 using mozilla::gfx::ColorPattern;
52 using mozilla::gfx::DeviceColor;
53 using mozilla::gfx::DrawTarget;
54 using mozilla::gfx::Rect;
55 using mozilla::gfx::sRGBColor;
56 using mozilla::gfx::ToDeviceColor;
58 nsContainerFrame::~nsContainerFrame() = default;
60 NS_QUERYFRAME_HEAD(nsContainerFrame)
61 NS_QUERYFRAME_ENTRY(nsContainerFrame)
62 NS_QUERYFRAME_TAIL_INHERITING(nsSplittableFrame)
64 void nsContainerFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
65 nsIFrame* aPrevInFlow) {
66 nsSplittableFrame::Init(aContent, aParent, aPrevInFlow);
67 if (aPrevInFlow) {
68 // Make sure we copy bits from our prev-in-flow that will affect
69 // us. A continuation for a container frame needs to know if it
70 // has a child with a view so that we'll properly reposition it.
71 if (aPrevInFlow->HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW)) {
72 AddStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
77 void nsContainerFrame::SetInitialChildList(ChildListID aListID,
78 nsFrameList&& aChildList) {
79 #ifdef DEBUG
80 nsIFrame::VerifyDirtyBitSet(aChildList);
81 for (nsIFrame* f : aChildList) {
82 MOZ_ASSERT(f->GetParent() == this, "Unexpected parent");
84 #endif
85 if (aListID == FrameChildListID::Principal) {
86 MOZ_ASSERT(mFrames.IsEmpty(),
87 "unexpected second call to SetInitialChildList");
88 mFrames = std::move(aChildList);
89 } else if (aListID == FrameChildListID::Backdrop) {
90 MOZ_ASSERT(StyleDisplay()->mTopLayer != StyleTopLayer::None,
91 "Only top layer frames should have backdrop");
92 MOZ_ASSERT(HasAnyStateBits(NS_FRAME_OUT_OF_FLOW),
93 "Top layer frames should be out-of-flow");
94 MOZ_ASSERT(!GetProperty(BackdropProperty()),
95 "We shouldn't have setup backdrop frame list before");
96 #ifdef DEBUG
98 nsIFrame* placeholder = aChildList.FirstChild();
99 MOZ_ASSERT(aChildList.OnlyChild(), "Should have only one backdrop");
100 MOZ_ASSERT(placeholder->IsPlaceholderFrame(),
101 "The frame to be stored should be a placeholder");
102 MOZ_ASSERT(static_cast<nsPlaceholderFrame*>(placeholder)
103 ->GetOutOfFlowFrame()
104 ->IsBackdropFrame(),
105 "The placeholder should points to a backdrop frame");
107 #endif
108 nsFrameList* list = new (PresShell()) nsFrameList(std::move(aChildList));
109 SetProperty(BackdropProperty(), list);
110 } else {
111 MOZ_ASSERT_UNREACHABLE("Unexpected child list");
115 void nsContainerFrame::AppendFrames(ChildListID aListID,
116 nsFrameList&& aFrameList) {
117 MOZ_ASSERT(aListID == FrameChildListID::Principal ||
118 aListID == FrameChildListID::NoReflowPrincipal,
119 "unexpected child list");
121 if (MOZ_UNLIKELY(aFrameList.IsEmpty())) {
122 return;
125 DrainSelfOverflowList(); // ensure the last frame is in mFrames
126 mFrames.AppendFrames(this, std::move(aFrameList));
128 if (aListID != FrameChildListID::NoReflowPrincipal) {
129 PresShell()->FrameNeedsReflow(this, IntrinsicDirty::FrameAndAncestors,
130 NS_FRAME_HAS_DIRTY_CHILDREN);
134 void nsContainerFrame::InsertFrames(ChildListID aListID, nsIFrame* aPrevFrame,
135 const nsLineList::iterator* aPrevFrameLine,
136 nsFrameList&& aFrameList) {
137 MOZ_ASSERT(aListID == FrameChildListID::Principal ||
138 aListID == FrameChildListID::NoReflowPrincipal,
139 "unexpected child list");
140 NS_ASSERTION(!aPrevFrame || aPrevFrame->GetParent() == this,
141 "inserting after sibling frame with different parent");
143 if (MOZ_UNLIKELY(aFrameList.IsEmpty())) {
144 return;
147 DrainSelfOverflowList(); // ensure aPrevFrame is in mFrames
148 mFrames.InsertFrames(this, aPrevFrame, std::move(aFrameList));
150 if (aListID != FrameChildListID::NoReflowPrincipal) {
151 PresShell()->FrameNeedsReflow(this, IntrinsicDirty::FrameAndAncestors,
152 NS_FRAME_HAS_DIRTY_CHILDREN);
156 void nsContainerFrame::RemoveFrame(DestroyContext& aContext,
157 ChildListID aListID, nsIFrame* aOldFrame) {
158 MOZ_ASSERT(aListID == FrameChildListID::Principal ||
159 aListID == FrameChildListID::NoReflowPrincipal,
160 "unexpected child list");
162 AutoTArray<nsIFrame*, 10> continuations;
164 nsIFrame* continuation = aOldFrame;
165 while (continuation) {
166 continuations.AppendElement(continuation);
167 continuation = continuation->GetNextContinuation();
171 mozilla::PresShell* presShell = PresShell();
172 nsContainerFrame* lastParent = nullptr;
174 // Loop and destroy aOldFrame and all of its continuations.
176 // Request a reflow on the parent frames involved unless we were explicitly
177 // told not to (FrameChildListID::NoReflowPrincipal).
178 const bool generateReflowCommand =
179 aListID != FrameChildListID::NoReflowPrincipal;
180 for (nsIFrame* continuation : Reversed(continuations)) {
181 nsContainerFrame* parent = continuation->GetParent();
183 // Please note that 'parent' may not actually be where 'continuation' lives.
184 // We really MUST use StealFrame() and nothing else here.
185 // @see nsInlineFrame::StealFrame for details.
186 parent->StealFrame(continuation);
187 continuation->Destroy(aContext);
188 if (generateReflowCommand && parent != lastParent) {
189 presShell->FrameNeedsReflow(parent, IntrinsicDirty::FrameAndAncestors,
190 NS_FRAME_HAS_DIRTY_CHILDREN);
191 lastParent = parent;
196 void nsContainerFrame::DestroyAbsoluteFrames(DestroyContext& aContext) {
197 if (IsAbsoluteContainer()) {
198 GetAbsoluteContainingBlock()->DestroyFrames(aContext);
199 MarkAsNotAbsoluteContainingBlock();
203 void nsContainerFrame::SafelyDestroyFrameListProp(
204 DestroyContext& aContext, mozilla::PresShell* aPresShell,
205 FrameListPropertyDescriptor aProp) {
206 // Note that the last frame can be removed through another route and thus
207 // delete the property -- that's why we fetch the property again before
208 // removing each frame rather than fetching it once and iterating the list.
209 while (nsFrameList* frameList = GetProperty(aProp)) {
210 nsIFrame* frame = frameList->RemoveFirstChild();
211 if (MOZ_LIKELY(frame)) {
212 frame->Destroy(aContext);
213 } else {
214 Unused << TakeProperty(aProp);
215 frameList->Delete(aPresShell);
216 return;
221 void nsContainerFrame::Destroy(DestroyContext& aContext) {
222 // Prevent event dispatch during destruction.
223 if (HasView()) {
224 GetView()->SetFrame(nullptr);
227 DestroyAbsoluteFrames(aContext);
229 // Destroy frames on the principal child list.
230 mFrames.DestroyFrames(aContext);
232 // If we have any IB split siblings, clear their references to us.
233 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
234 // Delete previous sibling's reference to me.
235 if (nsIFrame* prevSib = GetProperty(nsIFrame::IBSplitPrevSibling())) {
236 NS_WARNING_ASSERTION(
237 this == prevSib->GetProperty(nsIFrame::IBSplitSibling()),
238 "IB sibling chain is inconsistent");
239 prevSib->RemoveProperty(nsIFrame::IBSplitSibling());
242 // Delete next sibling's reference to me.
243 if (nsIFrame* nextSib = GetProperty(nsIFrame::IBSplitSibling())) {
244 NS_WARNING_ASSERTION(
245 this == nextSib->GetProperty(nsIFrame::IBSplitPrevSibling()),
246 "IB sibling chain is inconsistent");
247 nextSib->RemoveProperty(nsIFrame::IBSplitPrevSibling());
250 #ifdef DEBUG
251 // This is just so we can assert it's not set in nsIFrame::DestroyFrom.
252 RemoveStateBits(NS_FRAME_PART_OF_IBSPLIT);
253 #endif
256 if (MOZ_UNLIKELY(!mProperties.IsEmpty())) {
257 using T = mozilla::FrameProperties::UntypedDescriptor;
258 bool hasO = false, hasOC = false, hasEOC = false, hasBackdrop = false;
259 mProperties.ForEach([&](const T& aProp, uint64_t) {
260 if (aProp == OverflowProperty()) {
261 hasO = true;
262 } else if (aProp == OverflowContainersProperty()) {
263 hasOC = true;
264 } else if (aProp == ExcessOverflowContainersProperty()) {
265 hasEOC = true;
266 } else if (aProp == BackdropProperty()) {
267 hasBackdrop = true;
269 return true;
272 // Destroy frames on the auxiliary frame lists and delete the lists.
273 mozilla::PresShell* presShell = PresShell();
274 if (hasO) {
275 SafelyDestroyFrameListProp(aContext, presShell, OverflowProperty());
278 MOZ_ASSERT(CanContainOverflowContainers() || !(hasOC || hasEOC),
279 "this type of frame shouldn't have overflow containers");
280 if (hasOC) {
281 SafelyDestroyFrameListProp(aContext, presShell,
282 OverflowContainersProperty());
284 if (hasEOC) {
285 SafelyDestroyFrameListProp(aContext, presShell,
286 ExcessOverflowContainersProperty());
289 MOZ_ASSERT(!GetProperty(BackdropProperty()) ||
290 StyleDisplay()->mTopLayer != StyleTopLayer::None,
291 "only top layer frame may have backdrop");
292 if (hasBackdrop) {
293 SafelyDestroyFrameListProp(aContext, presShell, BackdropProperty());
297 nsSplittableFrame::Destroy(aContext);
300 /////////////////////////////////////////////////////////////////////////////
301 // Child frame enumeration
303 const nsFrameList& nsContainerFrame::GetChildList(ChildListID aListID) const {
304 // We only know about the principal child list, the overflow lists,
305 // and the backdrop list.
306 switch (aListID) {
307 case FrameChildListID::Principal:
308 return mFrames;
309 case FrameChildListID::Overflow: {
310 nsFrameList* list = GetOverflowFrames();
311 return list ? *list : nsFrameList::EmptyList();
313 case FrameChildListID::OverflowContainers: {
314 nsFrameList* list = GetOverflowContainers();
315 return list ? *list : nsFrameList::EmptyList();
317 case FrameChildListID::ExcessOverflowContainers: {
318 nsFrameList* list = GetExcessOverflowContainers();
319 return list ? *list : nsFrameList::EmptyList();
321 case FrameChildListID::Backdrop: {
322 nsFrameList* list = GetProperty(BackdropProperty());
323 return list ? *list : nsFrameList::EmptyList();
325 default:
326 return nsSplittableFrame::GetChildList(aListID);
330 void nsContainerFrame::GetChildLists(nsTArray<ChildList>* aLists) const {
331 mFrames.AppendIfNonempty(aLists, FrameChildListID::Principal);
333 using T = mozilla::FrameProperties::UntypedDescriptor;
334 mProperties.ForEach([this, aLists](const T& aProp, uint64_t aValue) {
335 typedef const nsFrameList* L;
336 if (aProp == OverflowProperty()) {
337 reinterpret_cast<L>(aValue)->AppendIfNonempty(aLists,
338 FrameChildListID::Overflow);
339 } else if (aProp == OverflowContainersProperty()) {
340 MOZ_ASSERT(CanContainOverflowContainers(),
341 "found unexpected OverflowContainersProperty");
342 Unused << this; // silence clang -Wunused-lambda-capture in opt builds
343 reinterpret_cast<L>(aValue)->AppendIfNonempty(
344 aLists, FrameChildListID::OverflowContainers);
345 } else if (aProp == ExcessOverflowContainersProperty()) {
346 MOZ_ASSERT(CanContainOverflowContainers(),
347 "found unexpected ExcessOverflowContainersProperty");
348 Unused << this; // silence clang -Wunused-lambda-capture in opt builds
349 reinterpret_cast<L>(aValue)->AppendIfNonempty(
350 aLists, FrameChildListID::ExcessOverflowContainers);
351 } else if (aProp == BackdropProperty()) {
352 reinterpret_cast<L>(aValue)->AppendIfNonempty(aLists,
353 FrameChildListID::Backdrop);
355 return true;
358 nsSplittableFrame::GetChildLists(aLists);
361 /////////////////////////////////////////////////////////////////////////////
362 // Painting/Events
364 void nsContainerFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
365 const nsDisplayListSet& aLists) {
366 DisplayBorderBackgroundOutline(aBuilder, aLists);
367 BuildDisplayListForNonBlockChildren(aBuilder, aLists);
370 void nsContainerFrame::BuildDisplayListForNonBlockChildren(
371 nsDisplayListBuilder* aBuilder, const nsDisplayListSet& aLists,
372 DisplayChildFlags aFlags) {
373 nsIFrame* kid = mFrames.FirstChild();
374 // Put each child's background directly onto the content list
375 nsDisplayListSet set(aLists, aLists.Content());
376 // The children should be in content order
377 while (kid) {
378 BuildDisplayListForChild(aBuilder, kid, set, aFlags);
379 kid = kid->GetNextSibling();
383 class nsDisplaySelectionOverlay : public nsPaintedDisplayItem {
384 public:
386 * @param aSelectionValue nsISelectionController::getDisplaySelection.
388 nsDisplaySelectionOverlay(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame,
389 int16_t aSelectionValue)
390 : nsPaintedDisplayItem(aBuilder, aFrame),
391 mSelectionValue(aSelectionValue) {
392 MOZ_COUNT_CTOR(nsDisplaySelectionOverlay);
394 MOZ_COUNTED_DTOR_OVERRIDE(nsDisplaySelectionOverlay)
396 virtual void Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) override;
397 bool CreateWebRenderCommands(
398 mozilla::wr::DisplayListBuilder& aBuilder,
399 mozilla::wr::IpcResourceUpdateQueue& aResources,
400 const StackingContextHelper& aSc,
401 mozilla::layers::RenderRootStateManager* aManager,
402 nsDisplayListBuilder* aDisplayListBuilder) override;
403 NS_DISPLAY_DECL_NAME("SelectionOverlay", TYPE_SELECTION_OVERLAY)
404 private:
405 DeviceColor ComputeColor() const;
407 static DeviceColor ComputeColorFromSelectionStyle(ComputedStyle&);
408 static DeviceColor ApplyTransparencyIfNecessary(nscolor);
410 // nsISelectionController::getDisplaySelection.
411 int16_t mSelectionValue;
414 DeviceColor nsDisplaySelectionOverlay::ApplyTransparencyIfNecessary(
415 nscolor aColor) {
416 // If it has already alpha, leave it like that.
417 if (NS_GET_A(aColor) != 255) {
418 return ToDeviceColor(aColor);
421 // NOTE(emilio): Blink and WebKit do something slightly different here, and
422 // blend the color with white instead, both for overlays and text backgrounds.
423 auto color = sRGBColor::FromABGR(aColor);
424 color.a = 0.5;
425 return ToDeviceColor(color);
428 DeviceColor nsDisplaySelectionOverlay::ComputeColorFromSelectionStyle(
429 ComputedStyle& aStyle) {
430 return ApplyTransparencyIfNecessary(
431 aStyle.GetVisitedDependentColor(&nsStyleBackground::mBackgroundColor));
434 DeviceColor nsDisplaySelectionOverlay::ComputeColor() const {
435 LookAndFeel::ColorID colorID;
436 if (RefPtr<ComputedStyle> style =
437 mFrame->ComputeSelectionStyle(mSelectionValue)) {
438 return ComputeColorFromSelectionStyle(*style);
440 if (mSelectionValue == nsISelectionController::SELECTION_ON) {
441 colorID = LookAndFeel::ColorID::Highlight;
442 } else if (mSelectionValue == nsISelectionController::SELECTION_ATTENTION) {
443 colorID = LookAndFeel::ColorID::TextSelectAttentionBackground;
444 } else {
445 colorID = LookAndFeel::ColorID::TextSelectDisabledBackground;
448 return ApplyTransparencyIfNecessary(
449 LookAndFeel::Color(colorID, mFrame, NS_RGB(255, 255, 255)));
452 void nsDisplaySelectionOverlay::Paint(nsDisplayListBuilder* aBuilder,
453 gfxContext* aCtx) {
454 DrawTarget& aDrawTarget = *aCtx->GetDrawTarget();
455 ColorPattern color(ComputeColor());
457 nsIntRect pxRect =
458 GetPaintRect(aBuilder, aCtx)
459 .ToOutsidePixels(mFrame->PresContext()->AppUnitsPerDevPixel());
460 Rect rect(pxRect.x, pxRect.y, pxRect.width, pxRect.height);
461 MaybeSnapToDevicePixels(rect, aDrawTarget, true);
463 aDrawTarget.FillRect(rect, color);
466 bool nsDisplaySelectionOverlay::CreateWebRenderCommands(
467 mozilla::wr::DisplayListBuilder& aBuilder,
468 mozilla::wr::IpcResourceUpdateQueue& aResources,
469 const StackingContextHelper& aSc,
470 mozilla::layers::RenderRootStateManager* aManager,
471 nsDisplayListBuilder* aDisplayListBuilder) {
472 wr::LayoutRect bounds = wr::ToLayoutRect(LayoutDeviceRect::FromAppUnits(
473 nsRect(ToReferenceFrame(), Frame()->GetSize()),
474 mFrame->PresContext()->AppUnitsPerDevPixel()));
475 aBuilder.PushRect(bounds, bounds, !BackfaceIsHidden(), false, false,
476 wr::ToColorF(ComputeColor()));
477 return true;
480 void nsContainerFrame::DisplaySelectionOverlay(nsDisplayListBuilder* aBuilder,
481 nsDisplayList* aList,
482 uint16_t aContentType) {
483 if (!IsSelected() || !IsVisibleForPainting()) {
484 return;
487 int16_t displaySelection = PresShell()->GetSelectionFlags();
488 if (!(displaySelection & aContentType)) {
489 return;
492 const nsFrameSelection* frameSelection = GetConstFrameSelection();
493 int16_t selectionValue = frameSelection->GetDisplaySelection();
495 if (selectionValue <= nsISelectionController::SELECTION_HIDDEN) {
496 return; // selection is hidden or off
499 nsIContent* newContent = mContent->GetParent();
501 // check to see if we are anonymous content
502 // XXXbz there has GOT to be a better way of determining this!
503 int32_t offset =
504 newContent ? newContent->ComputeIndexOf_Deprecated(mContent) : 0;
506 // look up to see what selection(s) are on this frame
507 UniquePtr<SelectionDetails> details =
508 frameSelection->LookUpSelection(newContent, offset, 1, false);
509 if (!details) {
510 return;
513 bool normal = false;
514 for (SelectionDetails* sd = details.get(); sd; sd = sd->mNext.get()) {
515 if (sd->mSelectionType == SelectionType::eNormal) {
516 normal = true;
520 if (!normal && aContentType == nsISelectionDisplay::DISPLAY_IMAGES) {
521 // Don't overlay an image if it's not in the primary selection.
522 return;
525 aList->AppendNewToTop<nsDisplaySelectionOverlay>(aBuilder, this,
526 selectionValue);
529 /* virtual */
530 void nsContainerFrame::ChildIsDirty(nsIFrame* aChild) {
531 NS_ASSERTION(aChild->IsSubtreeDirty(), "child isn't actually dirty");
533 AddStateBits(NS_FRAME_HAS_DIRTY_CHILDREN);
536 nsIFrame::FrameSearchResult nsContainerFrame::PeekOffsetNoAmount(
537 bool aForward, int32_t* aOffset) {
538 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
539 // Don't allow the caret to stay in an empty (leaf) container frame.
540 return CONTINUE_EMPTY;
543 nsIFrame::FrameSearchResult nsContainerFrame::PeekOffsetCharacter(
544 bool aForward, int32_t* aOffset, PeekOffsetCharacterOptions aOptions) {
545 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
546 // Don't allow the caret to stay in an empty (leaf) container frame.
547 return CONTINUE_EMPTY;
550 /////////////////////////////////////////////////////////////////////////////
551 // Helper member functions
554 * Position the view associated with |aKidFrame|, if there is one. A
555 * container frame should call this method after positioning a frame,
556 * but before |Reflow|.
558 void nsContainerFrame::PositionFrameView(nsIFrame* aKidFrame) {
559 nsIFrame* parentFrame = aKidFrame->GetParent();
560 if (!aKidFrame->HasView() || !parentFrame) return;
562 nsView* view = aKidFrame->GetView();
563 nsViewManager* vm = view->GetViewManager();
564 nsPoint pt;
565 nsView* ancestorView = parentFrame->GetClosestView(&pt);
567 if (ancestorView != view->GetParent()) {
568 NS_ASSERTION(ancestorView == view->GetParent()->GetParent(),
569 "Allowed only one anonymous view between frames");
570 // parentFrame is responsible for positioning aKidFrame's view
571 // explicitly
572 return;
575 pt += aKidFrame->GetPosition();
576 vm->MoveViewTo(view, pt.x, pt.y);
579 void nsContainerFrame::ReparentFrameView(nsIFrame* aChildFrame,
580 nsIFrame* aOldParentFrame,
581 nsIFrame* aNewParentFrame) {
582 #ifdef DEBUG
583 MOZ_ASSERT(aChildFrame, "null child frame pointer");
584 MOZ_ASSERT(aOldParentFrame, "null old parent frame pointer");
585 MOZ_ASSERT(aNewParentFrame, "null new parent frame pointer");
586 MOZ_ASSERT(aOldParentFrame != aNewParentFrame,
587 "same old and new parent frame");
589 // See if either the old parent frame or the new parent frame have a view
590 while (!aOldParentFrame->HasView() && !aNewParentFrame->HasView()) {
591 // Walk up both the old parent frame and the new parent frame nodes
592 // stopping when we either find a common parent or views for one
593 // or both of the frames.
595 // This works well in the common case where we push/pull and the old parent
596 // frame and the new parent frame are part of the same flow. They will
597 // typically be the same distance (height wise) from the
598 aOldParentFrame = aOldParentFrame->GetParent();
599 aNewParentFrame = aNewParentFrame->GetParent();
601 // We should never walk all the way to the root frame without finding
602 // a view
603 NS_ASSERTION(aOldParentFrame && aNewParentFrame, "didn't find view");
605 // See if we reached a common ancestor
606 if (aOldParentFrame == aNewParentFrame) {
607 break;
611 // See if we found a common parent frame
612 if (aOldParentFrame == aNewParentFrame) {
613 // We found a common parent and there are no views between the old parent
614 // and the common parent or the new parent frame and the common parent.
615 // Because neither the old parent frame nor the new parent frame have views,
616 // then any child views don't need reparenting
617 return;
620 // We found views for one or both of the ancestor frames before we
621 // found a common ancestor.
622 nsView* oldParentView = aOldParentFrame->GetClosestView();
623 nsView* newParentView = aNewParentFrame->GetClosestView();
625 // See if the old parent frame and the new parent frame are in the
626 // same view sub-hierarchy. If they are then we don't have to do
627 // anything
628 if (oldParentView != newParentView) {
629 MOZ_ASSERT_UNREACHABLE("can't move frames between views");
630 // They're not so we need to reparent any child views
631 aChildFrame->ReparentFrameViewTo(oldParentView->GetViewManager(),
632 newParentView);
634 #endif
637 void nsContainerFrame::ReparentFrameViewList(const nsFrameList& aChildFrameList,
638 nsIFrame* aOldParentFrame,
639 nsIFrame* aNewParentFrame) {
640 #ifdef DEBUG
641 MOZ_ASSERT(aChildFrameList.NotEmpty(), "empty child frame list");
642 MOZ_ASSERT(aOldParentFrame, "null old parent frame pointer");
643 MOZ_ASSERT(aNewParentFrame, "null new parent frame pointer");
644 MOZ_ASSERT(aOldParentFrame != aNewParentFrame,
645 "same old and new parent frame");
647 // See if either the old parent frame or the new parent frame have a view
648 while (!aOldParentFrame->HasView() && !aNewParentFrame->HasView()) {
649 // Walk up both the old parent frame and the new parent frame nodes
650 // stopping when we either find a common parent or views for one
651 // or both of the frames.
653 // This works well in the common case where we push/pull and the old parent
654 // frame and the new parent frame are part of the same flow. They will
655 // typically be the same distance (height wise) from the
656 aOldParentFrame = aOldParentFrame->GetParent();
657 aNewParentFrame = aNewParentFrame->GetParent();
659 // We should never walk all the way to the root frame without finding
660 // a view
661 NS_ASSERTION(aOldParentFrame && aNewParentFrame, "didn't find view");
663 // See if we reached a common ancestor
664 if (aOldParentFrame == aNewParentFrame) {
665 break;
669 // See if we found a common parent frame
670 if (aOldParentFrame == aNewParentFrame) {
671 // We found a common parent and there are no views between the old parent
672 // and the common parent or the new parent frame and the common parent.
673 // Because neither the old parent frame nor the new parent frame have views,
674 // then any child views don't need reparenting
675 return;
678 // We found views for one or both of the ancestor frames before we
679 // found a common ancestor.
680 nsView* oldParentView = aOldParentFrame->GetClosestView();
681 nsView* newParentView = aNewParentFrame->GetClosestView();
683 // See if the old parent frame and the new parent frame are in the
684 // same view sub-hierarchy. If they are then we don't have to do
685 // anything
686 if (oldParentView != newParentView) {
687 MOZ_ASSERT_UNREACHABLE("can't move frames between views");
688 nsViewManager* viewManager = oldParentView->GetViewManager();
690 // They're not so we need to reparent any child views
691 for (nsIFrame* f : aChildFrameList) {
692 f->ReparentFrameViewTo(viewManager, newParentView);
695 #endif
698 void nsContainerFrame::ReparentFrame(nsIFrame* aFrame,
699 nsContainerFrame* aOldParent,
700 nsContainerFrame* aNewParent) {
701 NS_ASSERTION(aOldParent == aFrame->GetParent(),
702 "Parent not consistent with expectations");
704 aFrame->SetParent(aNewParent);
706 // When pushing and pulling frames we need to check for whether any
707 // views need to be reparented
708 ReparentFrameView(aFrame, aOldParent, aNewParent);
711 void nsContainerFrame::ReparentFrames(nsFrameList& aFrameList,
712 nsContainerFrame* aOldParent,
713 nsContainerFrame* aNewParent) {
714 for (auto* f : aFrameList) {
715 ReparentFrame(f, aOldParent, aNewParent);
719 void nsContainerFrame::SetSizeConstraints(nsPresContext* aPresContext,
720 nsIWidget* aWidget,
721 const nsSize& aMinSize,
722 const nsSize& aMaxSize) {
723 LayoutDeviceIntSize devMinSize(
724 aPresContext->AppUnitsToDevPixels(aMinSize.width),
725 aPresContext->AppUnitsToDevPixels(aMinSize.height));
726 LayoutDeviceIntSize devMaxSize(
727 aMaxSize.width == NS_UNCONSTRAINEDSIZE
728 ? NS_MAXSIZE
729 : aPresContext->AppUnitsToDevPixels(aMaxSize.width),
730 aMaxSize.height == NS_UNCONSTRAINEDSIZE
731 ? NS_MAXSIZE
732 : aPresContext->AppUnitsToDevPixels(aMaxSize.height));
734 // MinSize has a priority over MaxSize
735 if (devMinSize.width > devMaxSize.width) devMaxSize.width = devMinSize.width;
736 if (devMinSize.height > devMaxSize.height)
737 devMaxSize.height = devMinSize.height;
739 nsIWidget* rootWidget = aPresContext->GetNearestWidget();
740 DesktopToLayoutDeviceScale constraintsScale(MOZ_WIDGET_INVALID_SCALE);
741 if (rootWidget) {
742 constraintsScale = rootWidget->GetDesktopToDeviceScale();
745 widget::SizeConstraints constraints(devMinSize, devMaxSize, constraintsScale);
747 // The sizes are in inner window sizes, so convert them into outer window
748 // sizes. Use a size of (200, 200) as only the difference between the inner
749 // and outer size is needed.
750 const LayoutDeviceIntSize sizeDiff = aWidget->ClientToWindowSizeDifference();
751 if (constraints.mMinSize.width) {
752 constraints.mMinSize.width += sizeDiff.width;
754 if (constraints.mMinSize.height) {
755 constraints.mMinSize.height += sizeDiff.height;
757 if (constraints.mMaxSize.width != NS_MAXSIZE) {
758 constraints.mMaxSize.width += sizeDiff.width;
760 if (constraints.mMaxSize.height != NS_MAXSIZE) {
761 constraints.mMaxSize.height += sizeDiff.height;
764 aWidget->SetSizeConstraints(constraints);
767 void nsContainerFrame::SyncFrameViewAfterReflow(nsPresContext* aPresContext,
768 nsIFrame* aFrame, nsView* aView,
769 const nsRect& aInkOverflowArea,
770 ReflowChildFlags aFlags) {
771 if (!aView) {
772 return;
775 // Make sure the view is sized and positioned correctly
776 if (!(aFlags & ReflowChildFlags::NoMoveView)) {
777 PositionFrameView(aFrame);
780 if (!(aFlags & ReflowChildFlags::NoSizeView)) {
781 nsViewManager* vm = aView->GetViewManager();
783 vm->ResizeView(aView, aInkOverflowArea, true);
787 void nsContainerFrame::DoInlineMinISize(gfxContext* aRenderingContext,
788 InlineMinISizeData* aData) {
789 auto handleChildren = [aRenderingContext](auto frame, auto data) {
790 for (nsIFrame* kid : frame->mFrames) {
791 kid->AddInlineMinISize(aRenderingContext, data);
794 DoInlineIntrinsicISize(aData, handleChildren);
797 void nsContainerFrame::DoInlinePrefISize(gfxContext* aRenderingContext,
798 InlinePrefISizeData* aData) {
799 auto handleChildren = [aRenderingContext](auto frame, auto data) {
800 for (nsIFrame* kid : frame->mFrames) {
801 kid->AddInlinePrefISize(aRenderingContext, data);
804 DoInlineIntrinsicISize(aData, handleChildren);
805 aData->mLineIsEmpty = false;
808 /* virtual */
809 LogicalSize nsContainerFrame::ComputeAutoSize(
810 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
811 nscoord aAvailableISize, const LogicalSize& aMargin,
812 const mozilla::LogicalSize& aBorderPadding,
813 const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) {
814 LogicalSize result(aWM, 0xdeadbeef, NS_UNCONSTRAINEDSIZE);
815 nscoord availBased =
816 aAvailableISize - aMargin.ISize(aWM) - aBorderPadding.ISize(aWM);
817 if (aFlags.contains(ComputeSizeFlag::ShrinkWrap)) {
818 // Only bother computing our 'auto' ISize if the result will be used.
819 const auto& styleISize = aSizeOverrides.mStyleISize
820 ? *aSizeOverrides.mStyleISize
821 : StylePosition()->ISize(aWM);
822 if (styleISize.IsAuto()) {
823 result.ISize(aWM) =
824 ShrinkISizeToFit(aRenderingContext, availBased, aFlags);
826 } else {
827 result.ISize(aWM) = availBased;
830 if (IsTableCaption()) {
831 // If we're a container for font size inflation, then shrink
832 // wrapping inside of us should not apply font size inflation.
833 AutoMaybeDisableFontInflation an(this);
835 WritingMode tableWM = GetParent()->GetWritingMode();
836 if (aWM.IsOrthogonalTo(tableWM)) {
837 // For an orthogonal caption on a block-dir side of the table, shrink-wrap
838 // to min-isize.
839 result.ISize(aWM) = GetMinISize(aRenderingContext);
840 } else {
841 // The outer frame constrains our available isize to the isize of
842 // the table. Grow if our min-isize is bigger than that, but not
843 // larger than the containing block isize. (It would really be nice
844 // to transmit that information another way, so we could grow up to
845 // the table's available isize, but that's harder.)
846 nscoord min = GetMinISize(aRenderingContext);
847 if (min > aCBSize.ISize(aWM)) {
848 min = aCBSize.ISize(aWM);
850 if (min > result.ISize(aWM)) {
851 result.ISize(aWM) = min;
855 return result;
858 void nsContainerFrame::ReflowChild(
859 nsIFrame* aKidFrame, nsPresContext* aPresContext,
860 ReflowOutput& aDesiredSize, const ReflowInput& aReflowInput,
861 const WritingMode& aWM, const LogicalPoint& aPos,
862 const nsSize& aContainerSize, ReflowChildFlags aFlags,
863 nsReflowStatus& aStatus, nsOverflowContinuationTracker* aTracker) {
864 MOZ_ASSERT(aReflowInput.mFrame == aKidFrame, "bad reflow input");
865 if (aWM.IsPhysicalRTL()) {
866 NS_ASSERTION(aContainerSize.width != NS_UNCONSTRAINEDSIZE,
867 "ReflowChild with unconstrained container width!");
869 MOZ_ASSERT(aDesiredSize.InkOverflow() == nsRect(0, 0, 0, 0) &&
870 aDesiredSize.ScrollableOverflow() == nsRect(0, 0, 0, 0),
871 "please reset the overflow areas before calling ReflowChild");
873 // Position the child frame and its view if requested.
874 if (ReflowChildFlags::NoMoveFrame !=
875 (aFlags & ReflowChildFlags::NoMoveFrame)) {
876 aKidFrame->SetPosition(aWM, aPos, aContainerSize);
879 if (!(aFlags & ReflowChildFlags::NoMoveView)) {
880 PositionFrameView(aKidFrame);
881 PositionChildViews(aKidFrame);
884 // Reflow the child frame
885 aKidFrame->Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
887 // If the child frame is complete, delete any next-in-flows,
888 // but only if the NoDeleteNextInFlowChild flag isn't set.
889 if (!aStatus.IsInlineBreakBefore() && aStatus.IsFullyComplete() &&
890 !(aFlags & ReflowChildFlags::NoDeleteNextInFlowChild)) {
891 if (nsIFrame* kidNextInFlow = aKidFrame->GetNextInFlow()) {
892 // Remove all of the childs next-in-flows. Make sure that we ask
893 // the right parent to do the removal (it's possible that the
894 // parent is not this because we are executing pullup code)
895 nsOverflowContinuationTracker::AutoFinish fini(aTracker, aKidFrame);
896 DestroyContext context(PresShell());
897 kidNextInFlow->GetParent()->DeleteNextInFlowChild(context, kidNextInFlow,
898 true);
903 // XXX temporary: hold on to a copy of the old physical version of
904 // ReflowChild so that we can convert callers incrementally.
905 void nsContainerFrame::ReflowChild(nsIFrame* aKidFrame,
906 nsPresContext* aPresContext,
907 ReflowOutput& aDesiredSize,
908 const ReflowInput& aReflowInput, nscoord aX,
909 nscoord aY, ReflowChildFlags aFlags,
910 nsReflowStatus& aStatus,
911 nsOverflowContinuationTracker* aTracker) {
912 MOZ_ASSERT(aReflowInput.mFrame == aKidFrame, "bad reflow input");
914 // Position the child frame and its view if requested.
915 if (ReflowChildFlags::NoMoveFrame !=
916 (aFlags & ReflowChildFlags::NoMoveFrame)) {
917 aKidFrame->SetPosition(nsPoint(aX, aY));
920 if (!(aFlags & ReflowChildFlags::NoMoveView)) {
921 PositionFrameView(aKidFrame);
922 PositionChildViews(aKidFrame);
925 // Reflow the child frame
926 aKidFrame->Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
928 // If the child frame is complete, delete any next-in-flows,
929 // but only if the NoDeleteNextInFlowChild flag isn't set.
930 if (aStatus.IsFullyComplete() &&
931 !(aFlags & ReflowChildFlags::NoDeleteNextInFlowChild)) {
932 if (nsIFrame* kidNextInFlow = aKidFrame->GetNextInFlow()) {
933 // Remove all of the childs next-in-flows. Make sure that we ask
934 // the right parent to do the removal (it's possible that the
935 // parent is not this because we are executing pullup code)
936 nsOverflowContinuationTracker::AutoFinish fini(aTracker, aKidFrame);
937 DestroyContext context(PresShell());
938 kidNextInFlow->GetParent()->DeleteNextInFlowChild(context, kidNextInFlow,
939 true);
945 * Position the views of |aFrame|'s descendants. A container frame
946 * should call this method if it moves a frame after |Reflow|.
948 void nsContainerFrame::PositionChildViews(nsIFrame* aFrame) {
949 if (!aFrame->HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW)) {
950 return;
953 // Recursively walk aFrame's child frames.
954 // Process the additional child lists, but skip the popup list as the view for
955 // popups is managed by the parent.
956 // Currently only nsMenuFrame has a popupList and during layout will adjust
957 // the view manually to position the popup.
958 for (const auto& [list, listID] : aFrame->ChildLists()) {
959 if (listID == FrameChildListID::Popup) {
960 continue;
962 for (nsIFrame* childFrame : list) {
963 // Position the frame's view (if it has one) otherwise recursively
964 // process its children
965 if (childFrame->HasView()) {
966 PositionFrameView(childFrame);
967 } else {
968 PositionChildViews(childFrame);
974 void nsContainerFrame::FinishReflowChild(
975 nsIFrame* aKidFrame, nsPresContext* aPresContext,
976 const ReflowOutput& aDesiredSize, const ReflowInput* aReflowInput,
977 const WritingMode& aWM, const LogicalPoint& aPos,
978 const nsSize& aContainerSize, nsIFrame::ReflowChildFlags aFlags) {
979 MOZ_ASSERT(!aReflowInput || aReflowInput->mFrame == aKidFrame);
980 MOZ_ASSERT(aReflowInput || aKidFrame->IsMathMLFrame() ||
981 aKidFrame->IsTableCellFrame(),
982 "aReflowInput should be passed in almost all cases");
984 if (aWM.IsPhysicalRTL()) {
985 NS_ASSERTION(aContainerSize.width != NS_UNCONSTRAINEDSIZE,
986 "FinishReflowChild with unconstrained container width!");
989 nsPoint curOrigin = aKidFrame->GetPosition();
990 const LogicalSize convertedSize = aDesiredSize.Size(aWM);
991 LogicalPoint pos(aPos);
993 if (aFlags & ReflowChildFlags::ApplyRelativePositioning) {
994 MOZ_ASSERT(aReflowInput, "caller must have passed reflow input");
995 // ApplyRelativePositioning in right-to-left writing modes needs to know
996 // the updated frame width to set the normal position correctly.
997 aKidFrame->SetSize(aWM, convertedSize);
999 const LogicalMargin offsets = aReflowInput->ComputedLogicalOffsets(aWM);
1000 ReflowInput::ApplyRelativePositioning(aKidFrame, aWM, offsets, &pos,
1001 aContainerSize);
1004 if (ReflowChildFlags::NoMoveFrame !=
1005 (aFlags & ReflowChildFlags::NoMoveFrame)) {
1006 aKidFrame->SetRect(aWM, LogicalRect(aWM, pos, convertedSize),
1007 aContainerSize);
1008 } else {
1009 aKidFrame->SetSize(aWM, convertedSize);
1012 if (aKidFrame->HasView()) {
1013 nsView* view = aKidFrame->GetView();
1014 // Make sure the frame's view is properly sized and positioned and has
1015 // things like opacity correct
1016 SyncFrameViewAfterReflow(aPresContext, aKidFrame, view,
1017 aDesiredSize.InkOverflow(), aFlags);
1020 nsPoint newOrigin = aKidFrame->GetPosition();
1021 if (!(aFlags & ReflowChildFlags::NoMoveView) && curOrigin != newOrigin) {
1022 if (!aKidFrame->HasView()) {
1023 // If the frame has moved, then we need to make sure any child views are
1024 // correctly positioned
1025 PositionChildViews(aKidFrame);
1029 aKidFrame->DidReflow(aPresContext, aReflowInput);
1031 #if defined(_MSC_VER) && !defined(__clang__) && defined(_M_AMD64)
1032 # pragma optimize("", on)
1033 #endif
1035 // XXX temporary: hold on to a copy of the old physical version of
1036 // FinishReflowChild so that we can convert callers incrementally.
1037 void nsContainerFrame::FinishReflowChild(nsIFrame* aKidFrame,
1038 nsPresContext* aPresContext,
1039 const ReflowOutput& aDesiredSize,
1040 const ReflowInput* aReflowInput,
1041 nscoord aX, nscoord aY,
1042 ReflowChildFlags aFlags) {
1043 MOZ_ASSERT(!(aFlags & ReflowChildFlags::ApplyRelativePositioning),
1044 "only the logical version supports ApplyRelativePositioning "
1045 "since ApplyRelativePositioning requires the container size");
1047 nsPoint curOrigin = aKidFrame->GetPosition();
1048 nsPoint pos(aX, aY);
1049 nsSize size(aDesiredSize.PhysicalSize());
1051 if (ReflowChildFlags::NoMoveFrame !=
1052 (aFlags & ReflowChildFlags::NoMoveFrame)) {
1053 aKidFrame->SetRect(nsRect(pos, size));
1054 } else {
1055 aKidFrame->SetSize(size);
1058 if (aKidFrame->HasView()) {
1059 nsView* view = aKidFrame->GetView();
1060 // Make sure the frame's view is properly sized and positioned and has
1061 // things like opacity correct
1062 SyncFrameViewAfterReflow(aPresContext, aKidFrame, view,
1063 aDesiredSize.InkOverflow(), aFlags);
1066 if (!(aFlags & ReflowChildFlags::NoMoveView) && curOrigin != pos) {
1067 if (!aKidFrame->HasView()) {
1068 // If the frame has moved, then we need to make sure any child views are
1069 // correctly positioned
1070 PositionChildViews(aKidFrame);
1074 aKidFrame->DidReflow(aPresContext, aReflowInput);
1077 void nsContainerFrame::ReflowOverflowContainerChildren(
1078 nsPresContext* aPresContext, const ReflowInput& aReflowInput,
1079 OverflowAreas& aOverflowRects, ReflowChildFlags aFlags,
1080 nsReflowStatus& aStatus, ChildFrameMerger aMergeFunc,
1081 Maybe<nsSize> aContainerSize) {
1082 MOZ_ASSERT(aPresContext, "null pointer");
1084 nsFrameList* overflowContainers =
1085 DrainExcessOverflowContainersList(aMergeFunc);
1086 if (!overflowContainers) {
1087 return; // nothing to reflow
1090 nsOverflowContinuationTracker tracker(this, false, false);
1091 bool shouldReflowAllKids = aReflowInput.ShouldReflowAllKids();
1093 for (nsIFrame* frame : *overflowContainers) {
1094 if (frame->GetPrevInFlow()->GetParent() != GetPrevInFlow()) {
1095 // frame's prevInFlow has moved, skip reflowing this frame;
1096 // it will get reflowed once it's been placed
1097 if (GetNextInFlow()) {
1098 // We report OverflowIncomplete status in this case to avoid our parent
1099 // deleting our next-in-flows which might destroy non-empty frames.
1100 nsReflowStatus status;
1101 status.SetOverflowIncomplete();
1102 aStatus.MergeCompletionStatusFrom(status);
1104 continue;
1107 auto ScrollableOverflowExceedsAvailableBSize =
1108 [this, &aReflowInput](nsIFrame* aFrame) {
1109 if (aReflowInput.AvailableBSize() == NS_UNCONSTRAINEDSIZE) {
1110 return false;
1112 const auto parentWM = GetWritingMode();
1113 const nscoord scrollableOverflowRectBEnd =
1114 LogicalRect(parentWM,
1115 aFrame->ScrollableOverflowRectRelativeToParent(),
1116 GetSize())
1117 .BEnd(parentWM);
1118 return scrollableOverflowRectBEnd > aReflowInput.AvailableBSize();
1121 // If the available block-size has changed, or the existing scrollable
1122 // overflow's block-end exceeds it, we need to reflow even if the frame
1123 // isn't dirty.
1124 if (shouldReflowAllKids || frame->IsSubtreeDirty() ||
1125 ScrollableOverflowExceedsAvailableBSize(frame)) {
1126 nsIFrame* prevInFlow = frame->GetPrevInFlow();
1127 NS_ASSERTION(prevInFlow,
1128 "overflow container frame must have a prev-in-flow");
1129 NS_ASSERTION(
1130 frame->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1131 "overflow container frame must have overflow container bit set");
1132 WritingMode wm = frame->GetWritingMode();
1133 // Note: aReflowInput's available inline-size is technically wrong for us
1134 // to hand off to children here, because it doesn't account for the space
1135 // that's been used for the container's margin/border/padding (and some
1136 // other space that a concrete container type, e.g. fieldset and grid [1],
1137 // might reserve before setting up the available space for their
1138 // children). Since we don't have a way to query the specific available
1139 // inline-size each container type used, nor do we know how the container
1140 // computes its non-overflow-container children's inline-size, we just
1141 // unconditionally override the frame's inline-size, so that the available
1142 // inline-size for the children doesn't really matter anyway.
1144 // [1] For example, fieldset uses its computed inline-size with padding as
1145 // the available inline-size to reflow its inner child frame.
1146 // https://searchfox.org/mozilla-central/rev/04f7743d94691fa24212fb43099f9d84c3bfc890/layout/forms/nsFieldSetFrame.cpp#535-536
1147 const LogicalSize availSpace = aReflowInput.AvailableSize(wm);
1149 StyleSizeOverrides sizeOverride;
1150 // We override current continuation's inline-size by using the
1151 // prev-in-flow's inline-size since both should be the same.
1152 sizeOverride.mStyleISize.emplace(
1153 StyleSize::LengthPercentage(LengthPercentage::FromAppUnits(
1154 frame->StylePosition()->mBoxSizing == StyleBoxSizing::Border
1155 ? prevInFlow->ISize(wm)
1156 : prevInFlow->ContentISize(wm))));
1158 if (frame->IsFlexItem()) {
1159 // An overflow container's block-size must be 0.
1160 sizeOverride.mStyleBSize.emplace(
1161 StyleSize::LengthPercentage(LengthPercentage::FromAppUnits(0)));
1163 ReflowOutput desiredSize(wm);
1164 ReflowInput reflowInput(aPresContext, aReflowInput, frame, availSpace,
1165 Nothing(), {}, sizeOverride);
1166 const nsSize containerSize =
1167 aContainerSize ? *aContainerSize
1168 : aReflowInput.AvailableSize(wm).GetPhysicalSize(wm);
1169 const LogicalPoint pos(wm, prevInFlow->IStart(wm, containerSize), 0);
1170 nsReflowStatus frameStatus;
1172 ReflowChild(frame, aPresContext, desiredSize, reflowInput, wm, pos,
1173 containerSize, aFlags, frameStatus, &tracker);
1174 FinishReflowChild(frame, aPresContext, desiredSize, &reflowInput, wm, pos,
1175 containerSize, aFlags);
1177 // Handle continuations
1178 if (!frameStatus.IsFullyComplete()) {
1179 if (frame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
1180 // Abspos frames can't cause their parent to be incomplete,
1181 // only overflow incomplete.
1182 frameStatus.SetOverflowIncomplete();
1183 } else {
1184 NS_ASSERTION(frameStatus.IsComplete(),
1185 "overflow container frames can't be incomplete, only "
1186 "overflow-incomplete");
1189 // Acquire a next-in-flow, creating it if necessary
1190 nsIFrame* nif = frame->GetNextInFlow();
1191 if (!nif) {
1192 NS_ASSERTION(frameStatus.NextInFlowNeedsReflow(),
1193 "Someone forgot a NextInFlowNeedsReflow flag");
1194 nif = PresShell()->FrameConstructor()->CreateContinuingFrame(frame,
1195 this);
1196 } else if (!nif->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1197 // used to be a normal next-in-flow; steal it from the child list
1198 nif->GetParent()->StealFrame(nif);
1201 tracker.Insert(nif, frameStatus);
1203 aStatus.MergeCompletionStatusFrom(frameStatus);
1204 // At this point it would be nice to assert
1205 // !frame->GetOverflowRect().IsEmpty(), but we have some unsplittable
1206 // frames that, when taller than availableHeight will push zero-height
1207 // content into a next-in-flow.
1208 } else {
1209 tracker.Skip(frame, aStatus);
1210 if (aReflowInput.mFloatManager) {
1211 nsBlockFrame::RecoverFloatsFor(frame, *aReflowInput.mFloatManager,
1212 aReflowInput.GetWritingMode(),
1213 aReflowInput.ComputedPhysicalSize());
1216 ConsiderChildOverflow(aOverflowRects, frame);
1220 void nsContainerFrame::DisplayOverflowContainers(
1221 nsDisplayListBuilder* aBuilder, const nsDisplayListSet& aLists) {
1222 nsFrameList* overflowconts = GetOverflowContainers();
1223 if (overflowconts) {
1224 for (nsIFrame* frame : *overflowconts) {
1225 BuildDisplayListForChild(aBuilder, frame, aLists);
1230 bool nsContainerFrame::TryRemoveFrame(FrameListPropertyDescriptor aProp,
1231 nsIFrame* aChildToRemove) {
1232 nsFrameList* list = GetProperty(aProp);
1233 if (list && list->StartRemoveFrame(aChildToRemove)) {
1234 // aChildToRemove *may* have been removed from this list.
1235 if (list->IsEmpty()) {
1236 Unused << TakeProperty(aProp);
1237 list->Delete(PresShell());
1239 return true;
1241 return false;
1244 bool nsContainerFrame::MaybeStealOverflowContainerFrame(nsIFrame* aChild) {
1245 bool removed = false;
1246 if (MOZ_UNLIKELY(aChild->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER))) {
1247 // Try removing from the overflow container list.
1248 removed = TryRemoveFrame(OverflowContainersProperty(), aChild);
1249 if (!removed) {
1250 // It might be in the excess overflow container list.
1251 removed = TryRemoveFrame(ExcessOverflowContainersProperty(), aChild);
1254 return removed;
1257 void nsContainerFrame::StealFrame(nsIFrame* aChild) {
1258 #ifdef DEBUG
1259 if (!mFrames.ContainsFrame(aChild)) {
1260 nsFrameList* list = GetOverflowFrames();
1261 if (!list || !list->ContainsFrame(aChild)) {
1262 list = GetOverflowContainers();
1263 if (!list || !list->ContainsFrame(aChild)) {
1264 list = GetExcessOverflowContainers();
1265 MOZ_ASSERT(list && list->ContainsFrame(aChild),
1266 "aChild isn't our child"
1267 " or on a frame list not supported by StealFrame");
1271 #endif
1273 if (MaybeStealOverflowContainerFrame(aChild)) {
1274 return;
1277 // NOTE nsColumnSetFrame and nsCanvasFrame have their overflow containers
1278 // on the normal lists so we might get here also if the frame bit
1279 // NS_FRAME_IS_OVERFLOW_CONTAINER is set.
1280 if (mFrames.StartRemoveFrame(aChild)) {
1281 return;
1284 // We didn't find the child in our principal child list.
1285 // Maybe it's on the overflow list?
1286 nsFrameList* frameList = GetOverflowFrames();
1287 if (frameList && frameList->ContinueRemoveFrame(aChild)) {
1288 if (frameList->IsEmpty()) {
1289 DestroyOverflowList();
1291 return;
1294 MOZ_ASSERT_UNREACHABLE("StealFrame: can't find aChild");
1297 nsFrameList nsContainerFrame::StealFramesAfter(nsIFrame* aChild) {
1298 NS_ASSERTION(
1299 !aChild || !aChild->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1300 "StealFramesAfter doesn't handle overflow containers");
1301 NS_ASSERTION(!IsBlockFrame(), "unexpected call");
1303 if (!aChild) {
1304 return std::move(mFrames);
1307 for (nsIFrame* f : mFrames) {
1308 if (f == aChild) {
1309 return mFrames.TakeFramesAfter(f);
1313 // We didn't find the child in the principal child list.
1314 // Maybe it's on the overflow list?
1315 if (nsFrameList* overflowFrames = GetOverflowFrames()) {
1316 for (nsIFrame* f : *overflowFrames) {
1317 if (f == aChild) {
1318 return mFrames.TakeFramesAfter(f);
1323 NS_ERROR("StealFramesAfter: can't find aChild");
1324 return nsFrameList();
1328 * Create a next-in-flow for aFrame. Will return the newly created
1329 * frame <b>if and only if</b> a new frame is created; otherwise
1330 * nullptr is returned.
1332 nsIFrame* nsContainerFrame::CreateNextInFlow(nsIFrame* aFrame) {
1333 MOZ_ASSERT(
1334 !IsBlockFrame(),
1335 "you should have called nsBlockFrame::CreateContinuationFor instead");
1336 MOZ_ASSERT(mFrames.ContainsFrame(aFrame), "expected an in-flow child frame");
1338 nsIFrame* nextInFlow = aFrame->GetNextInFlow();
1339 if (nullptr == nextInFlow) {
1340 // Create a continuation frame for the child frame and insert it
1341 // into our child list.
1342 nextInFlow =
1343 PresShell()->FrameConstructor()->CreateContinuingFrame(aFrame, this);
1344 mFrames.InsertFrame(nullptr, aFrame, nextInFlow);
1346 NS_FRAME_LOG(NS_FRAME_TRACE_NEW_FRAMES,
1347 ("nsContainerFrame::CreateNextInFlow: frame=%p nextInFlow=%p",
1348 aFrame, nextInFlow));
1350 return nextInFlow;
1352 return nullptr;
1356 * Remove and delete aNextInFlow and its next-in-flows. Updates the sibling and
1357 * flow pointers
1359 void nsContainerFrame::DeleteNextInFlowChild(DestroyContext& aContext,
1360 nsIFrame* aNextInFlow,
1361 bool aDeletingEmptyFrames) {
1362 #ifdef DEBUG
1363 nsIFrame* prevInFlow = aNextInFlow->GetPrevInFlow();
1364 #endif
1365 MOZ_ASSERT(prevInFlow, "bad prev-in-flow");
1367 // If the next-in-flow has a next-in-flow then delete it, too (and
1368 // delete it first).
1369 // Do this in a loop so we don't overflow the stack for frames
1370 // with very many next-in-flows
1371 nsIFrame* nextNextInFlow = aNextInFlow->GetNextInFlow();
1372 if (nextNextInFlow) {
1373 AutoTArray<nsIFrame*, 8> frames;
1374 for (nsIFrame* f = nextNextInFlow; f; f = f->GetNextInFlow()) {
1375 frames.AppendElement(f);
1377 for (nsIFrame* delFrame : Reversed(frames)) {
1378 nsContainerFrame* parent = delFrame->GetParent();
1379 parent->DeleteNextInFlowChild(aContext, delFrame, aDeletingEmptyFrames);
1383 // Take the next-in-flow out of the parent's child list
1384 StealFrame(aNextInFlow);
1386 #ifdef DEBUG
1387 if (aDeletingEmptyFrames) {
1388 nsLayoutUtils::AssertTreeOnlyEmptyNextInFlows(aNextInFlow);
1390 #endif
1392 // Delete the next-in-flow frame and its descendants. This will also
1393 // remove it from its next-in-flow/prev-in-flow chain.
1394 aNextInFlow->Destroy(aContext);
1396 MOZ_ASSERT(!prevInFlow->GetNextInFlow(), "non null next-in-flow");
1399 void nsContainerFrame::PushChildrenToOverflow(nsIFrame* aFromChild,
1400 nsIFrame* aPrevSibling) {
1401 MOZ_ASSERT(aFromChild, "null pointer");
1402 MOZ_ASSERT(aPrevSibling, "pushing first child");
1403 MOZ_ASSERT(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
1405 // Add the frames to our overflow list (let our next in flow drain
1406 // our overflow list when it is ready)
1407 SetOverflowFrames(mFrames.TakeFramesAfter(aPrevSibling));
1410 bool nsContainerFrame::PushIncompleteChildren(
1411 const FrameHashtable& aPushedItems, const FrameHashtable& aIncompleteItems,
1412 const FrameHashtable& aOverflowIncompleteItems) {
1413 MOZ_ASSERT(IsFlexOrGridContainer(),
1414 "Only Grid / Flex containers can call this!");
1416 if (aPushedItems.IsEmpty() && aIncompleteItems.IsEmpty() &&
1417 aOverflowIncompleteItems.IsEmpty()) {
1418 return false;
1421 // Iterate the children in normal document order and append them (or a NIF)
1422 // to one of the following frame lists according to their status.
1423 nsFrameList pushedList;
1424 nsFrameList incompleteList;
1425 nsFrameList overflowIncompleteList;
1426 auto* fc = PresShell()->FrameConstructor();
1427 for (nsIFrame* child = PrincipalChildList().FirstChild(); child;) {
1428 MOZ_ASSERT((aPushedItems.Contains(child) ? 1 : 0) +
1429 (aIncompleteItems.Contains(child) ? 1 : 0) +
1430 (aOverflowIncompleteItems.Contains(child) ? 1 : 0) <=
1432 "child should only be in one of these sets");
1433 // Save the next-sibling so we can continue the loop if |child| is moved.
1434 nsIFrame* next = child->GetNextSibling();
1435 if (aPushedItems.Contains(child)) {
1436 MOZ_ASSERT(child->GetParent() == this);
1437 StealFrame(child);
1438 pushedList.AppendFrame(nullptr, child);
1439 } else if (aIncompleteItems.Contains(child)) {
1440 nsIFrame* childNIF = child->GetNextInFlow();
1441 if (!childNIF) {
1442 childNIF = fc->CreateContinuingFrame(child, this);
1443 incompleteList.AppendFrame(nullptr, childNIF);
1444 } else {
1445 auto* parent = childNIF->GetParent();
1446 MOZ_ASSERT(parent != this || !mFrames.ContainsFrame(childNIF),
1447 "child's NIF shouldn't be in the same principal list");
1448 // If child's existing NIF is an overflow container, convert it to an
1449 // actual NIF, since now |child| has non-overflow stuff to give it.
1450 // Or, if it's further away then our next-in-flow, then pull it up.
1451 if (childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) ||
1452 (parent != this && parent != GetNextInFlow())) {
1453 parent->StealFrame(childNIF);
1454 childNIF->RemoveStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1455 if (parent == this) {
1456 incompleteList.AppendFrame(nullptr, childNIF);
1457 } else {
1458 // If childNIF already lives on the next fragment, then we
1459 // don't need to reparent it, since we know it's destined to end
1460 // up there anyway. Just move it to its parent's overflow list.
1461 if (parent == GetNextInFlow()) {
1462 nsFrameList toMove(childNIF, childNIF);
1463 parent->MergeSortedOverflow(toMove);
1464 } else {
1465 ReparentFrame(childNIF, parent, this);
1466 incompleteList.AppendFrame(nullptr, childNIF);
1471 } else if (aOverflowIncompleteItems.Contains(child)) {
1472 nsIFrame* childNIF = child->GetNextInFlow();
1473 if (!childNIF) {
1474 childNIF = fc->CreateContinuingFrame(child, this);
1475 childNIF->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1476 overflowIncompleteList.AppendFrame(nullptr, childNIF);
1477 } else {
1478 DebugOnly<nsContainerFrame*> lastParent = this;
1479 auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1480 // If child has any non-overflow-container NIFs, convert them to
1481 // overflow containers, since that's all |child| needs now.
1482 while (childNIF &&
1483 !childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1484 auto* parent = childNIF->GetParent();
1485 parent->StealFrame(childNIF);
1486 childNIF->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1487 if (parent == this) {
1488 overflowIncompleteList.AppendFrame(nullptr, childNIF);
1489 } else {
1490 if (!nif || parent == nif) {
1491 nsFrameList toMove(childNIF, childNIF);
1492 parent->MergeSortedExcessOverflowContainers(toMove);
1493 } else {
1494 ReparentFrame(childNIF, parent, nif);
1495 nsFrameList toMove(childNIF, childNIF);
1496 nif->MergeSortedExcessOverflowContainers(toMove);
1498 // We only need to reparent the first childNIF (or not at all if
1499 // its parent is our NIF).
1500 nif = nullptr;
1502 lastParent = parent;
1503 childNIF = childNIF->GetNextInFlow();
1507 child = next;
1510 // Merge the results into our respective overflow child lists.
1511 if (!pushedList.IsEmpty()) {
1512 MergeSortedOverflow(pushedList);
1514 if (!incompleteList.IsEmpty()) {
1515 MergeSortedOverflow(incompleteList);
1517 if (!overflowIncompleteList.IsEmpty()) {
1518 // If our next-in-flow already has overflow containers list, merge the
1519 // overflowIncompleteList into that list. Otherwise, merge it into our
1520 // excess overflow containers list, to be drained by our next-in-flow.
1521 auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1522 nsFrameList* oc = nif ? nif->GetOverflowContainers() : nullptr;
1523 if (oc) {
1524 ReparentFrames(overflowIncompleteList, this, nif);
1525 MergeSortedFrameLists(*oc, overflowIncompleteList, GetContent());
1526 } else {
1527 MergeSortedExcessOverflowContainers(overflowIncompleteList);
1530 return true;
1533 void nsContainerFrame::NormalizeChildLists() {
1534 MOZ_ASSERT(IsFlexOrGridContainer(),
1535 "Only Flex / Grid containers can call this!");
1537 // Note: the following description uses grid container as an example. Flex
1538 // container is similar.
1540 // First we gather child frames we should include in our reflow/placement,
1541 // i.e. overflowed children from our prev-in-flow, and pushed first-in-flow
1542 // children (that might now fit). It's important to note that these children
1543 // can be in arbitrary order vis-a-vis the current children in our lists.
1544 // E.g. grid items in the document order: A, B, C may be placed in the rows
1545 // 3, 2, 1. Assume each row goes in a separate grid container fragment,
1546 // and we reflow the second fragment. Now if C (in fragment 1) overflows,
1547 // we can't just prepend it to our mFrames like we usually do because that
1548 // would violate the document order invariant that other code depends on.
1549 // Similarly if we pull up child A (from fragment 3) we can't just append
1550 // that for the same reason. Instead, we must sort these children into
1551 // our child lists. (The sorting is trivial given that both lists are
1552 // already fully sorted individually - it's just a merge.)
1554 // The invariants that we maintain are that each grid container child list
1555 // is sorted in the normal document order at all times, but that children
1556 // in different grid container continuations may be in arbitrary order.
1558 const auto didPushItemsBit = IsFlexContainerFrame()
1559 ? NS_STATE_FLEX_DID_PUSH_ITEMS
1560 : NS_STATE_GRID_DID_PUSH_ITEMS;
1561 const auto hasChildNifBit = IsFlexContainerFrame()
1562 ? NS_STATE_FLEX_HAS_CHILD_NIFS
1563 : NS_STATE_GRID_HAS_CHILD_NIFS;
1565 auto* prevInFlow = static_cast<nsContainerFrame*>(GetPrevInFlow());
1566 // Merge overflow frames from our prev-in-flow into our principal child list.
1567 if (prevInFlow) {
1568 AutoFrameListPtr overflow(PresContext(), prevInFlow->StealOverflowFrames());
1569 if (overflow) {
1570 ReparentFrames(*overflow, prevInFlow, this);
1571 MergeSortedFrameLists(mFrames, *overflow, GetContent());
1573 // Move trailing next-in-flows into our overflow list.
1574 nsFrameList continuations;
1575 for (nsIFrame* f = mFrames.FirstChild(); f;) {
1576 nsIFrame* next = f->GetNextSibling();
1577 nsIFrame* pif = f->GetPrevInFlow();
1578 if (pif && pif->GetParent() == this) {
1579 mFrames.RemoveFrame(f);
1580 continuations.AppendFrame(nullptr, f);
1582 f = next;
1584 MergeSortedOverflow(continuations);
1586 // Move prev-in-flow's excess overflow containers list into our own
1587 // overflow containers list. If we already have an excess overflow
1588 // containers list, any child in that list which doesn't have a
1589 // prev-in-flow in this frame is also merged into our overflow container
1590 // list.
1591 nsFrameList* overflowContainers =
1592 DrainExcessOverflowContainersList(MergeSortedFrameListsFor);
1594 // Move trailing OC next-in-flows into our excess overflow containers
1595 // list.
1596 if (overflowContainers) {
1597 nsFrameList moveToEOC;
1598 for (nsIFrame* f = overflowContainers->FirstChild(); f;) {
1599 nsIFrame* next = f->GetNextSibling();
1600 nsIFrame* pif = f->GetPrevInFlow();
1601 if (pif && pif->GetParent() == this) {
1602 overflowContainers->RemoveFrame(f);
1603 moveToEOC.AppendFrame(nullptr, f);
1605 f = next;
1607 if (overflowContainers->IsEmpty()) {
1608 DestroyOverflowContainers();
1610 MergeSortedExcessOverflowContainers(moveToEOC);
1615 // For each item in aItems, pull up its next-in-flow (if any), and reparent it
1616 // to our next-in-flow, unless its parent is already ourselves or our
1617 // next-in-flow (to avoid leaving a hole there).
1618 auto PullItemsNextInFlow = [this](const nsFrameList& aItems) {
1619 auto* firstNIF = static_cast<nsContainerFrame*>(GetNextInFlow());
1620 if (!firstNIF) {
1621 return;
1623 nsFrameList childNIFs;
1624 nsFrameList childOCNIFs;
1625 for (auto* child : aItems) {
1626 if (auto* childNIF = child->GetNextInFlow()) {
1627 if (auto* parent = childNIF->GetParent();
1628 parent != this && parent != firstNIF) {
1629 parent->StealFrame(childNIF);
1630 ReparentFrame(childNIF, parent, firstNIF);
1631 if (childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1632 childOCNIFs.AppendFrame(nullptr, childNIF);
1633 } else {
1634 childNIFs.AppendFrame(nullptr, childNIF);
1639 // Merge aItems' NIFs into our NIF's respective overflow child lists.
1640 firstNIF->MergeSortedOverflow(childNIFs);
1641 firstNIF->MergeSortedExcessOverflowContainers(childOCNIFs);
1644 // Merge our own overflow frames into our principal child list,
1645 // except those that are a next-in-flow for one of our items.
1646 DebugOnly<bool> foundOwnPushedChild = false;
1648 nsFrameList* ourOverflow = GetOverflowFrames();
1649 if (ourOverflow) {
1650 nsFrameList items;
1651 for (nsIFrame* f = ourOverflow->FirstChild(); f;) {
1652 nsIFrame* next = f->GetNextSibling();
1653 nsIFrame* pif = f->GetPrevInFlow();
1654 if (!pif || pif->GetParent() != this) {
1655 MOZ_ASSERT(f->GetParent() == this);
1656 ourOverflow->RemoveFrame(f);
1657 items.AppendFrame(nullptr, f);
1658 if (!pif) {
1659 foundOwnPushedChild = true;
1662 f = next;
1665 if (ourOverflow->IsEmpty()) {
1666 DestroyOverflowList();
1667 ourOverflow = nullptr;
1669 if (items.NotEmpty()) {
1670 PullItemsNextInFlow(items);
1672 MergeSortedFrameLists(mFrames, items, GetContent());
1676 // Push any child next-in-flows in our principal list to OverflowList.
1677 if (HasAnyStateBits(hasChildNifBit)) {
1678 nsFrameList framesToPush;
1679 nsIFrame* firstChild = mFrames.FirstChild();
1680 // Note that we potentially modify our mFrames list as we go.
1681 for (auto* child = firstChild; child; child = child->GetNextSibling()) {
1682 if (auto* childNIF = child->GetNextInFlow()) {
1683 if (childNIF->GetParent() == this) {
1684 for (auto* c = child->GetNextSibling(); c; c = c->GetNextSibling()) {
1685 if (c == childNIF) {
1686 // child's next-in-flow is in our principal child list, push it.
1687 mFrames.RemoveFrame(childNIF);
1688 framesToPush.AppendFrame(nullptr, childNIF);
1689 break;
1695 if (!framesToPush.IsEmpty()) {
1696 MergeSortedOverflow(framesToPush);
1698 RemoveStateBits(hasChildNifBit);
1701 // Pull up any first-in-flow children we might have pushed.
1702 if (HasAnyStateBits(didPushItemsBit)) {
1703 RemoveStateBits(didPushItemsBit);
1704 nsFrameList items;
1705 auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1706 DebugOnly<bool> nifNeedPushedItem = false;
1707 while (nif) {
1708 nsFrameList nifItems;
1709 for (nsIFrame* nifChild = nif->PrincipalChildList().FirstChild();
1710 nifChild;) {
1711 nsIFrame* next = nifChild->GetNextSibling();
1712 if (!nifChild->GetPrevInFlow()) {
1713 nif->StealFrame(nifChild);
1714 ReparentFrame(nifChild, nif, this);
1715 nifItems.AppendFrame(nullptr, nifChild);
1716 nifNeedPushedItem = false;
1718 nifChild = next;
1720 MergeSortedFrameLists(items, nifItems, GetContent());
1722 if (!nif->HasAnyStateBits(didPushItemsBit)) {
1723 MOZ_ASSERT(!nifNeedPushedItem || mDidPushItemsBitMayLie,
1724 "The state bit stored in didPushItemsBit lied!");
1725 break;
1727 nifNeedPushedItem = true;
1729 for (nsIFrame* nifChild =
1730 nif->GetChildList(FrameChildListID::Overflow).FirstChild();
1731 nifChild;) {
1732 nsIFrame* next = nifChild->GetNextSibling();
1733 if (!nifChild->GetPrevInFlow()) {
1734 nif->StealFrame(nifChild);
1735 ReparentFrame(nifChild, nif, this);
1736 nifItems.AppendFrame(nullptr, nifChild);
1737 nifNeedPushedItem = false;
1739 nifChild = next;
1741 MergeSortedFrameLists(items, nifItems, GetContent());
1743 nif->RemoveStateBits(didPushItemsBit);
1744 nif = static_cast<nsContainerFrame*>(nif->GetNextInFlow());
1745 MOZ_ASSERT(nif || !nifNeedPushedItem || mDidPushItemsBitMayLie,
1746 "The state bit stored in didPushItemsBit lied!");
1749 if (!items.IsEmpty()) {
1750 PullItemsNextInFlow(items);
1753 MOZ_ASSERT(
1754 foundOwnPushedChild || !items.IsEmpty() || mDidPushItemsBitMayLie,
1755 "The state bit stored in didPushItemsBit lied!");
1756 MergeSortedFrameLists(mFrames, items, GetContent());
1760 void nsContainerFrame::NoteNewChildren(ChildListID aListID,
1761 const nsFrameList& aFrameList) {
1762 MOZ_ASSERT(aListID == FrameChildListID::Principal, "unexpected child list");
1763 MOZ_ASSERT(IsFlexOrGridContainer(),
1764 "Only Flex / Grid containers can call this!");
1766 mozilla::PresShell* presShell = PresShell();
1767 const auto didPushItemsBit = IsFlexContainerFrame()
1768 ? NS_STATE_FLEX_DID_PUSH_ITEMS
1769 : NS_STATE_GRID_DID_PUSH_ITEMS;
1770 for (auto* pif = GetPrevInFlow(); pif; pif = pif->GetPrevInFlow()) {
1771 pif->AddStateBits(didPushItemsBit);
1772 presShell->FrameNeedsReflow(pif, IntrinsicDirty::FrameAndAncestors,
1773 NS_FRAME_IS_DIRTY);
1777 bool nsContainerFrame::MoveOverflowToChildList() {
1778 bool result = false;
1780 // Check for an overflow list with our prev-in-flow
1781 nsContainerFrame* prevInFlow = (nsContainerFrame*)GetPrevInFlow();
1782 if (nullptr != prevInFlow) {
1783 AutoFrameListPtr prevOverflowFrames(PresContext(),
1784 prevInFlow->StealOverflowFrames());
1785 if (prevOverflowFrames) {
1786 // Tables are special; they can have repeated header/footer
1787 // frames on mFrames at this point.
1788 NS_ASSERTION(mFrames.IsEmpty() || IsTableFrame(), "bad overflow list");
1789 // When pushing and pulling frames we need to check for whether any
1790 // views need to be reparented.
1791 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
1792 this);
1793 mFrames.AppendFrames(this, std::move(*prevOverflowFrames));
1794 result = true;
1798 // It's also possible that we have an overflow list for ourselves.
1799 return DrainSelfOverflowList() || result;
1802 void nsContainerFrame::MergeSortedOverflow(nsFrameList& aList) {
1803 if (aList.IsEmpty()) {
1804 return;
1806 MOZ_ASSERT(
1807 !aList.FirstChild()->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1808 "this is the wrong list to put this child frame");
1809 MOZ_ASSERT(aList.FirstChild()->GetParent() == this);
1810 nsFrameList* overflow = GetOverflowFrames();
1811 if (overflow) {
1812 MergeSortedFrameLists(*overflow, aList, GetContent());
1813 } else {
1814 SetOverflowFrames(std::move(aList));
1818 void nsContainerFrame::MergeSortedExcessOverflowContainers(nsFrameList& aList) {
1819 if (aList.IsEmpty()) {
1820 return;
1822 MOZ_ASSERT(
1823 aList.FirstChild()->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1824 "this is the wrong list to put this child frame");
1825 MOZ_ASSERT(aList.FirstChild()->GetParent() == this);
1826 if (nsFrameList* eoc = GetExcessOverflowContainers()) {
1827 MergeSortedFrameLists(*eoc, aList, GetContent());
1828 } else {
1829 SetExcessOverflowContainers(std::move(aList));
1833 nsIFrame* nsContainerFrame::GetFirstNonAnonBoxInSubtree(nsIFrame* aFrame) {
1834 while (aFrame) {
1835 // If aFrame isn't an anonymous container, or it's text or such, then it'll
1836 // do.
1837 if (!aFrame->Style()->IsAnonBox() ||
1838 nsCSSAnonBoxes::IsNonElement(aFrame->Style()->GetPseudoType())) {
1839 break;
1842 // Otherwise, descend to its first child and repeat.
1844 // SPECIAL CASE: if we're dealing with an anonymous table, then it might
1845 // be wrapping something non-anonymous in its caption or col-group lists
1846 // (instead of its principal child list), so we have to look there.
1847 // (Note: For anonymous tables that have a non-anon cell *and* a non-anon
1848 // column, we'll always return the column. This is fine; we're really just
1849 // looking for a handle to *anything* with a meaningful content node inside
1850 // the table, for use in DOM comparisons to things outside of the table.)
1851 if (MOZ_UNLIKELY(aFrame->IsTableWrapperFrame())) {
1852 nsIFrame* captionDescendant = GetFirstNonAnonBoxInSubtree(
1853 aFrame->GetChildList(FrameChildListID::Caption).FirstChild());
1854 if (captionDescendant) {
1855 return captionDescendant;
1857 } else if (MOZ_UNLIKELY(aFrame->IsTableFrame())) {
1858 nsIFrame* colgroupDescendant = GetFirstNonAnonBoxInSubtree(
1859 aFrame->GetChildList(FrameChildListID::ColGroup).FirstChild());
1860 if (colgroupDescendant) {
1861 return colgroupDescendant;
1865 // USUAL CASE: Descend to the first child in principal list.
1866 aFrame = aFrame->PrincipalChildList().FirstChild();
1868 return aFrame;
1872 * Is aFrame1 a prev-continuation of aFrame2?
1874 static bool IsPrevContinuationOf(nsIFrame* aFrame1, nsIFrame* aFrame2) {
1875 nsIFrame* prev = aFrame2;
1876 while ((prev = prev->GetPrevContinuation())) {
1877 if (prev == aFrame1) {
1878 return true;
1881 return false;
1884 void nsContainerFrame::MergeSortedFrameLists(nsFrameList& aDest,
1885 nsFrameList& aSrc,
1886 nsIContent* aCommonAncestor) {
1887 // Returns a frame whose DOM node can be used for the purpose of ordering
1888 // aFrame among its sibling frames by DOM position. If aFrame is
1889 // non-anonymous, this just returns aFrame itself. Otherwise, this returns the
1890 // first non-anonymous descendant in aFrame's continuation chain.
1891 auto FrameForDOMPositionComparison = [](nsIFrame* aFrame) {
1892 if (!aFrame->Style()->IsAnonBox()) {
1893 // The usual case.
1894 return aFrame;
1897 // Walk the continuation chain from the start, and return the first
1898 // non-anonymous descendant that we find.
1899 for (nsIFrame* f = aFrame->FirstContinuation(); f;
1900 f = f->GetNextContinuation()) {
1901 if (nsIFrame* nonAnonBox = GetFirstNonAnonBoxInSubtree(f)) {
1902 return nonAnonBox;
1906 MOZ_ASSERT_UNREACHABLE(
1907 "Why is there no non-anonymous descendants in the continuation chain?");
1908 return aFrame;
1911 nsIFrame* dest = aDest.FirstChild();
1912 for (nsIFrame* src = aSrc.FirstChild(); src;) {
1913 if (!dest) {
1914 aDest.AppendFrames(nullptr, std::move(aSrc));
1915 break;
1917 nsIContent* srcContent = FrameForDOMPositionComparison(src)->GetContent();
1918 nsIContent* destContent = FrameForDOMPositionComparison(dest)->GetContent();
1919 int32_t result = nsContentUtils::CompareTreePosition<TreeKind::Flat>(
1920 srcContent, destContent, aCommonAncestor);
1921 if (MOZ_UNLIKELY(result == 0)) {
1922 // NOTE: we get here when comparing ::before/::after for the same element.
1923 if (MOZ_UNLIKELY(srcContent->IsGeneratedContentContainerForBefore())) {
1924 if (MOZ_LIKELY(!destContent->IsGeneratedContentContainerForBefore()) ||
1925 ::IsPrevContinuationOf(src, dest)) {
1926 result = -1;
1928 } else if (MOZ_UNLIKELY(
1929 srcContent->IsGeneratedContentContainerForAfter())) {
1930 if (MOZ_UNLIKELY(destContent->IsGeneratedContentContainerForAfter()) &&
1931 ::IsPrevContinuationOf(src, dest)) {
1932 result = -1;
1934 } else if (::IsPrevContinuationOf(src, dest)) {
1935 result = -1;
1938 if (result < 0) {
1939 // src should come before dest
1940 nsIFrame* next = src->GetNextSibling();
1941 aSrc.RemoveFrame(src);
1942 aDest.InsertFrame(nullptr, dest->GetPrevSibling(), src);
1943 src = next;
1944 } else {
1945 dest = dest->GetNextSibling();
1948 MOZ_ASSERT(aSrc.IsEmpty());
1951 bool nsContainerFrame::MoveInlineOverflowToChildList(nsIFrame* aLineContainer) {
1952 MOZ_ASSERT(aLineContainer,
1953 "Must have line container for moving inline overflows");
1955 bool result = false;
1957 // Check for an overflow list with our prev-in-flow
1958 if (auto prevInFlow = static_cast<nsContainerFrame*>(GetPrevInFlow())) {
1959 AutoFrameListPtr prevOverflowFrames(PresContext(),
1960 prevInFlow->StealOverflowFrames());
1961 if (prevOverflowFrames) {
1962 // We may need to reparent floats from prev-in-flow to our line
1963 // container if the container has prev continuation.
1964 if (aLineContainer->GetPrevContinuation()) {
1965 ReparentFloatsForInlineChild(aLineContainer,
1966 prevOverflowFrames->FirstChild(), true);
1968 // When pushing and pulling frames we need to check for whether
1969 // any views need to be reparented.
1970 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
1971 this);
1972 // Prepend overflow frames to the list.
1973 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
1974 result = true;
1978 // It's also possible that we have overflow list for ourselves.
1979 return DrainSelfOverflowList() || result;
1982 bool nsContainerFrame::DrainSelfOverflowList() {
1983 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
1984 if (overflowFrames) {
1985 mFrames.AppendFrames(nullptr, std::move(*overflowFrames));
1986 return true;
1988 return false;
1991 bool nsContainerFrame::DrainAndMergeSelfOverflowList() {
1992 MOZ_ASSERT(IsFlexOrGridContainer(),
1993 "Only Flex / Grid containers can call this!");
1995 // Unlike nsContainerFrame::DrainSelfOverflowList, flex or grid containers
1996 // need to merge these lists so that the resulting mFrames is in document
1997 // content order.
1998 // NOTE: nsContainerFrame::AppendFrames/InsertFrames calls this method and
1999 // there are also direct calls from the fctor (FindAppendPrevSibling).
2000 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
2001 if (overflowFrames) {
2002 MergeSortedFrameLists(mFrames, *overflowFrames, GetContent());
2003 // We set a frame bit to push them again in Reflow() to avoid creating
2004 // multiple flex / grid items per flex / grid container fragment for the
2005 // same content.
2006 AddStateBits(IsFlexContainerFrame() ? NS_STATE_FLEX_HAS_CHILD_NIFS
2007 : NS_STATE_GRID_HAS_CHILD_NIFS);
2008 return true;
2010 return false;
2013 nsFrameList* nsContainerFrame::DrainExcessOverflowContainersList(
2014 ChildFrameMerger aMergeFunc) {
2015 nsFrameList* overflowContainers = GetOverflowContainers();
2017 // Drain excess overflow containers from our prev-in-flow.
2018 if (auto* prev = static_cast<nsContainerFrame*>(GetPrevInFlow())) {
2019 AutoFrameListPtr excessFrames(PresContext(),
2020 prev->StealExcessOverflowContainers());
2021 if (excessFrames) {
2022 excessFrames->ApplySetParent(this);
2023 nsContainerFrame::ReparentFrameViewList(*excessFrames, prev, this);
2024 if (overflowContainers) {
2025 // The default merge function is AppendFrames, so we use excessFrames as
2026 // the destination and then assign the result to overflowContainers.
2027 aMergeFunc(*excessFrames, *overflowContainers, this);
2028 *overflowContainers = std::move(*excessFrames);
2029 } else {
2030 overflowContainers = SetOverflowContainers(std::move(*excessFrames));
2035 // Our own excess overflow containers from a previous reflow can still be
2036 // present if our next-in-flow hasn't been reflown yet. Move any children
2037 // from it that don't have a continuation in this frame to the
2038 // OverflowContainers list.
2039 AutoFrameListPtr selfExcessOCFrames(PresContext(),
2040 StealExcessOverflowContainers());
2041 if (selfExcessOCFrames) {
2042 nsFrameList toMove;
2043 auto child = selfExcessOCFrames->FirstChild();
2044 while (child) {
2045 auto next = child->GetNextSibling();
2046 MOZ_ASSERT(child->GetPrevInFlow(),
2047 "ExcessOverflowContainers frames must be continuations");
2048 if (child->GetPrevInFlow()->GetParent() != this) {
2049 selfExcessOCFrames->RemoveFrame(child);
2050 toMove.AppendFrame(nullptr, child);
2052 child = next;
2055 // If there's any remaining excess overflow containers, put them back.
2056 if (selfExcessOCFrames->NotEmpty()) {
2057 SetExcessOverflowContainers(std::move(*selfExcessOCFrames));
2060 if (toMove.NotEmpty()) {
2061 if (overflowContainers) {
2062 aMergeFunc(*overflowContainers, toMove, this);
2063 } else {
2064 overflowContainers = SetOverflowContainers(std::move(toMove));
2069 return overflowContainers;
2072 nsIFrame* nsContainerFrame::GetNextInFlowChild(
2073 ContinuationTraversingState& aState, bool* aIsInOverflow) {
2074 nsContainerFrame*& nextInFlow = aState.mNextInFlow;
2075 while (nextInFlow) {
2076 // See if there is any frame in the container
2077 nsIFrame* frame = nextInFlow->mFrames.FirstChild();
2078 if (frame) {
2079 if (aIsInOverflow) {
2080 *aIsInOverflow = false;
2082 return frame;
2084 // No frames in the principal list, try its overflow list
2085 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
2086 if (overflowFrames) {
2087 if (aIsInOverflow) {
2088 *aIsInOverflow = true;
2090 return overflowFrames->FirstChild();
2092 nextInFlow = static_cast<nsContainerFrame*>(nextInFlow->GetNextInFlow());
2094 return nullptr;
2097 nsIFrame* nsContainerFrame::PullNextInFlowChild(
2098 ContinuationTraversingState& aState) {
2099 bool isInOverflow;
2100 nsIFrame* frame = GetNextInFlowChild(aState, &isInOverflow);
2101 if (frame) {
2102 nsContainerFrame* nextInFlow = aState.mNextInFlow;
2103 if (isInOverflow) {
2104 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
2105 overflowFrames->RemoveFirstChild();
2106 if (overflowFrames->IsEmpty()) {
2107 nextInFlow->DestroyOverflowList();
2109 } else {
2110 nextInFlow->mFrames.RemoveFirstChild();
2113 // Move the frame to the principal frame list of this container
2114 mFrames.AppendFrame(this, frame);
2115 // AppendFrame has reparented the frame, we need
2116 // to reparent the frame view then.
2117 nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
2119 return frame;
2122 /* static */
2123 void nsContainerFrame::ReparentFloatsForInlineChild(nsIFrame* aOurLineContainer,
2124 nsIFrame* aFrame,
2125 bool aReparentSiblings) {
2126 // XXXbz this would be better if it took a nsFrameList or a frame
2127 // list slice....
2128 NS_ASSERTION(aOurLineContainer->GetNextContinuation() ||
2129 aOurLineContainer->GetPrevContinuation(),
2130 "Don't call this when we have no continuation, it's a waste");
2131 if (!aFrame) {
2132 NS_ASSERTION(aReparentSiblings, "Why did we get called?");
2133 return;
2136 nsBlockFrame* frameBlock = nsLayoutUtils::GetFloatContainingBlock(aFrame);
2137 if (!frameBlock || frameBlock == aOurLineContainer) {
2138 return;
2141 nsBlockFrame* ourBlock = do_QueryFrame(aOurLineContainer);
2142 NS_ASSERTION(ourBlock, "Not a block, but broke vertically?");
2144 while (true) {
2145 ourBlock->ReparentFloats(aFrame, frameBlock, false);
2147 if (!aReparentSiblings) return;
2148 nsIFrame* next = aFrame->GetNextSibling();
2149 if (!next) return;
2150 if (next->GetParent() == aFrame->GetParent()) {
2151 aFrame = next;
2152 continue;
2154 // This is paranoid and will hardly ever get hit ... but we can't actually
2155 // trust that the frames in the sibling chain all have the same parent,
2156 // because lazy reparenting may be going on. If we find a different
2157 // parent we need to redo our analysis.
2158 ReparentFloatsForInlineChild(aOurLineContainer, next, aReparentSiblings);
2159 return;
2163 bool nsContainerFrame::ResolvedOrientationIsVertical() {
2164 StyleOrient orient = StyleDisplay()->mOrient;
2165 switch (orient) {
2166 case StyleOrient::Horizontal:
2167 return false;
2168 case StyleOrient::Vertical:
2169 return true;
2170 case StyleOrient::Inline:
2171 return GetWritingMode().IsVertical();
2172 case StyleOrient::Block:
2173 return !GetWritingMode().IsVertical();
2175 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
2176 return false;
2179 LogicalSize nsContainerFrame::ComputeSizeWithIntrinsicDimensions(
2180 gfxContext* aRenderingContext, WritingMode aWM,
2181 const IntrinsicSize& aIntrinsicSize, const AspectRatio& aAspectRatio,
2182 const LogicalSize& aCBSize, const LogicalSize& aMargin,
2183 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
2184 ComputeSizeFlags aFlags) {
2185 const nsStylePosition* stylePos = StylePosition();
2186 const auto& styleISize = aSizeOverrides.mStyleISize
2187 ? *aSizeOverrides.mStyleISize
2188 : stylePos->ISize(aWM);
2189 const auto& styleBSize = aSizeOverrides.mStyleBSize
2190 ? *aSizeOverrides.mStyleBSize
2191 : stylePos->BSize(aWM);
2192 const auto& aspectRatio =
2193 aSizeOverrides.mAspectRatio ? *aSizeOverrides.mAspectRatio : aAspectRatio;
2195 auto* parentFrame = GetParent();
2196 const bool isGridItem = IsGridItem();
2197 const bool isFlexItem =
2198 IsFlexItem() && !parentFrame->HasAnyStateBits(
2199 NS_STATE_FLEX_IS_EMULATING_LEGACY_WEBKIT_BOX);
2200 // This variable only gets meaningfully set if isFlexItem is true. It
2201 // indicates which axis (in this frame's own WM) corresponds to its
2202 // flex container's main axis.
2203 LogicalAxis flexMainAxis = LogicalAxis::Block;
2204 if (isFlexItem && nsFlexContainerFrame::IsItemInlineAxisMainAxis(this)) {
2205 flexMainAxis = LogicalAxis::Inline;
2208 // Handle intrinsic sizes and their interaction with
2209 // {min-,max-,}{width,height} according to the rules in
2210 // https://www.w3.org/TR/CSS22/visudet.html#min-max-widths and
2211 // https://drafts.csswg.org/css-sizing-3/#intrinsic-sizes
2213 // Note: throughout the following section of the function, I avoid
2214 // a * (b / c) because of its reduced accuracy relative to a * b / c
2215 // or (a * b) / c (which are equivalent).
2217 const bool isAutoOrMaxContentISize =
2218 styleISize.IsAuto() || styleISize.IsMaxContent();
2219 const bool isAutoBSize =
2220 nsLayoutUtils::IsAutoBSize(styleBSize, aCBSize.BSize(aWM));
2222 const auto boxSizingAdjust = stylePos->mBoxSizing == StyleBoxSizing::Border
2223 ? aBorderPadding
2224 : LogicalSize(aWM);
2225 const nscoord boxSizingToMarginEdgeISize = aMargin.ISize(aWM) +
2226 aBorderPadding.ISize(aWM) -
2227 boxSizingAdjust.ISize(aWM);
2229 nscoord iSize, minISize, maxISize, bSize, minBSize, maxBSize;
2230 enum class Stretch {
2231 // stretch to fill the CB (preserving intrinsic ratio) in the relevant axis
2232 StretchPreservingRatio,
2233 // stretch to fill the CB in the relevant axis
2234 Stretch,
2235 // no stretching in the relevant axis
2236 NoStretch,
2238 // just to avoid having to type these out everywhere:
2239 const auto eStretchPreservingRatio = Stretch::StretchPreservingRatio;
2240 const auto eStretch = Stretch::Stretch;
2241 const auto eNoStretch = Stretch::NoStretch;
2243 Stretch stretchI = eNoStretch; // stretch behavior in the inline axis
2244 Stretch stretchB = eNoStretch; // stretch behavior in the block axis
2246 const bool isOrthogonal = aWM.IsOrthogonalTo(parentFrame->GetWritingMode());
2247 const bool isVertical = aWM.IsVertical();
2248 const LogicalSize fallbackIntrinsicSize(aWM, kFallbackIntrinsicSize);
2249 const auto& isizeCoord =
2250 isVertical ? aIntrinsicSize.height : aIntrinsicSize.width;
2251 const bool hasIntrinsicISize = isizeCoord.isSome();
2252 nscoord intrinsicISize = std::max(0, isizeCoord.valueOr(0));
2254 const auto& bsizeCoord =
2255 isVertical ? aIntrinsicSize.width : aIntrinsicSize.height;
2256 const bool hasIntrinsicBSize = bsizeCoord.isSome();
2257 nscoord intrinsicBSize = std::max(0, bsizeCoord.valueOr(0));
2259 if (!isAutoOrMaxContentISize) {
2260 iSize = ComputeISizeValue(aRenderingContext, aWM, aCBSize, boxSizingAdjust,
2261 boxSizingToMarginEdgeISize, styleISize,
2262 aSizeOverrides, aFlags)
2263 .mISize;
2264 } else if (MOZ_UNLIKELY(isGridItem) &&
2265 !parentFrame->IsMasonry(isOrthogonal ? LogicalAxis::Block
2266 : LogicalAxis::Inline)) {
2267 MOZ_ASSERT(!IsTrueOverflowContainer());
2268 // 'auto' inline-size for grid-level box - apply 'stretch' as needed:
2269 auto cbSize = aCBSize.ISize(aWM);
2270 if (cbSize != NS_UNCONSTRAINEDSIZE) {
2271 if (!StyleMargin()->HasInlineAxisAuto(aWM)) {
2272 auto inlineAxisAlignment =
2273 isOrthogonal ? stylePos->UsedAlignSelf(GetParent()->Style())._0
2274 : stylePos->UsedJustifySelf(GetParent()->Style())._0;
2275 if (inlineAxisAlignment == StyleAlignFlags::STRETCH) {
2276 stretchI = eStretch;
2279 if (stretchI != eNoStretch ||
2280 aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize)) {
2281 iSize = std::max(nscoord(0), cbSize - aBorderPadding.ISize(aWM) -
2282 aMargin.ISize(aWM));
2284 } else {
2285 // Reset this flag to avoid applying the clamping below.
2286 aFlags -= ComputeSizeFlag::IClampMarginBoxMinSize;
2290 const auto& maxISizeCoord = stylePos->MaxISize(aWM);
2292 if (!maxISizeCoord.IsNone() &&
2293 !(isFlexItem && flexMainAxis == LogicalAxis::Inline)) {
2294 maxISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
2295 boxSizingAdjust, boxSizingToMarginEdgeISize,
2296 maxISizeCoord, aSizeOverrides, aFlags)
2297 .mISize;
2298 } else {
2299 maxISize = nscoord_MAX;
2302 // NOTE: Flex items ignore their min & max sizing properties in their
2303 // flex container's main-axis. (Those properties get applied later in
2304 // the flexbox algorithm.)
2306 const auto& minISizeCoord = stylePos->MinISize(aWM);
2308 if (!minISizeCoord.IsAuto() &&
2309 !(isFlexItem && flexMainAxis == LogicalAxis::Inline)) {
2310 minISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
2311 boxSizingAdjust, boxSizingToMarginEdgeISize,
2312 minISizeCoord, aSizeOverrides, aFlags)
2313 .mISize;
2314 } else {
2315 // Treat "min-width: auto" as 0.
2316 // NOTE: Technically, "auto" is supposed to behave like "min-content" on
2317 // flex items. However, we don't need to worry about that here, because
2318 // flex items' min-sizes are intentionally ignored until the flex
2319 // container explicitly considers them during space distribution.
2320 minISize = 0;
2323 if (!isAutoBSize) {
2324 bSize = nsLayoutUtils::ComputeBSizeValue(aCBSize.BSize(aWM),
2325 boxSizingAdjust.BSize(aWM),
2326 styleBSize.AsLengthPercentage());
2327 } else if (MOZ_UNLIKELY(isGridItem) &&
2328 !parentFrame->IsMasonry(isOrthogonal ? LogicalAxis::Inline
2329 : LogicalAxis::Block)) {
2330 MOZ_ASSERT(!IsTrueOverflowContainer());
2331 // 'auto' block-size for grid-level box - apply 'stretch' as needed:
2332 auto cbSize = aCBSize.BSize(aWM);
2333 if (cbSize != NS_UNCONSTRAINEDSIZE) {
2334 if (!StyleMargin()->HasBlockAxisAuto(aWM)) {
2335 auto blockAxisAlignment =
2336 !isOrthogonal ? stylePos->UsedAlignSelf(GetParent()->Style())._0
2337 : stylePos->UsedJustifySelf(GetParent()->Style())._0;
2338 if (blockAxisAlignment == StyleAlignFlags::STRETCH) {
2339 stretchB = eStretch;
2342 if (stretchB != eNoStretch ||
2343 aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize)) {
2344 bSize = std::max(nscoord(0), cbSize - aBorderPadding.BSize(aWM) -
2345 aMargin.BSize(aWM));
2347 } else {
2348 // Reset this flag to avoid applying the clamping below.
2349 aFlags -= ComputeSizeFlag::BClampMarginBoxMinSize;
2353 const auto& maxBSizeCoord = stylePos->MaxBSize(aWM);
2355 if (!nsLayoutUtils::IsAutoBSize(maxBSizeCoord, aCBSize.BSize(aWM)) &&
2356 !(isFlexItem && flexMainAxis == LogicalAxis::Block)) {
2357 maxBSize = nsLayoutUtils::ComputeBSizeValue(
2358 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
2359 maxBSizeCoord.AsLengthPercentage());
2360 } else {
2361 maxBSize = nscoord_MAX;
2364 const auto& minBSizeCoord = stylePos->MinBSize(aWM);
2366 if (!nsLayoutUtils::IsAutoBSize(minBSizeCoord, aCBSize.BSize(aWM)) &&
2367 !(isFlexItem && flexMainAxis == LogicalAxis::Block)) {
2368 minBSize = nsLayoutUtils::ComputeBSizeValue(
2369 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
2370 minBSizeCoord.AsLengthPercentage());
2371 } else {
2372 minBSize = 0;
2375 NS_ASSERTION(aCBSize.ISize(aWM) != NS_UNCONSTRAINEDSIZE,
2376 "Our containing block must not have unconstrained inline-size!");
2378 // Now calculate the used values for iSize and bSize:
2379 if (isAutoOrMaxContentISize) {
2380 if (isAutoBSize) {
2381 // 'auto' iSize, 'auto' bSize
2383 // Get tentative values - CSS 2.1 sections 10.3.2 and 10.6.2:
2385 nscoord tentISize, tentBSize;
2387 if (hasIntrinsicISize) {
2388 tentISize = intrinsicISize;
2389 } else if (hasIntrinsicBSize && aspectRatio) {
2390 tentISize = aspectRatio.ComputeRatioDependentSize(
2391 LogicalAxis::Inline, aWM, intrinsicBSize, boxSizingAdjust);
2392 } else if (aspectRatio) {
2393 tentISize =
2394 aCBSize.ISize(aWM) - boxSizingToMarginEdgeISize; // XXX scrollbar?
2395 if (tentISize < 0) {
2396 tentISize = 0;
2398 } else {
2399 tentISize = fallbackIntrinsicSize.ISize(aWM);
2402 // If we need to clamp the inline size to fit the CB, we use the 'stretch'
2403 // or 'normal' codepath. We use the ratio-preserving 'normal' codepath
2404 // unless we have 'stretch' in the other axis.
2405 if (aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize) &&
2406 stretchI != eStretch && tentISize > iSize) {
2407 stretchI = (stretchB == eStretch ? eStretch : eStretchPreservingRatio);
2410 if (hasIntrinsicBSize) {
2411 tentBSize = intrinsicBSize;
2412 } else if (aspectRatio) {
2413 tentBSize = aspectRatio.ComputeRatioDependentSize(
2414 LogicalAxis::Block, aWM, tentISize, boxSizingAdjust);
2415 } else {
2416 tentBSize = fallbackIntrinsicSize.BSize(aWM);
2419 // (ditto the comment about clamping the inline size above)
2420 if (aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize) &&
2421 stretchB != eStretch && tentBSize > bSize) {
2422 stretchB = (stretchI == eStretch ? eStretch : eStretchPreservingRatio);
2425 if (stretchI == eStretch) {
2426 tentISize = iSize; // * / 'stretch'
2427 if (stretchB == eStretch) {
2428 tentBSize = bSize; // 'stretch' / 'stretch'
2429 } else if (stretchB == eStretchPreservingRatio && aspectRatio) {
2430 // 'normal' / 'stretch'
2431 tentBSize = aspectRatio.ComputeRatioDependentSize(
2432 LogicalAxis::Block, aWM, iSize, boxSizingAdjust);
2434 } else if (stretchB == eStretch) {
2435 tentBSize = bSize; // 'stretch' / * (except 'stretch')
2436 if (stretchI == eStretchPreservingRatio && aspectRatio) {
2437 // 'stretch' / 'normal'
2438 tentISize = aspectRatio.ComputeRatioDependentSize(
2439 LogicalAxis::Inline, aWM, bSize, boxSizingAdjust);
2441 } else if (stretchI == eStretchPreservingRatio && aspectRatio) {
2442 tentISize = iSize; // * (except 'stretch') / 'normal'
2443 tentBSize = aspectRatio.ComputeRatioDependentSize(
2444 LogicalAxis::Block, aWM, iSize, boxSizingAdjust);
2445 if (stretchB == eStretchPreservingRatio && tentBSize > bSize) {
2446 // Stretch within the CB size with preserved intrinsic ratio.
2447 tentBSize = bSize; // 'normal' / 'normal'
2448 tentISize = aspectRatio.ComputeRatioDependentSize(
2449 LogicalAxis::Inline, aWM, bSize, boxSizingAdjust);
2451 } else if (stretchB == eStretchPreservingRatio && aspectRatio) {
2452 tentBSize = bSize; // 'normal' / * (except 'normal' and 'stretch')
2453 tentISize = aspectRatio.ComputeRatioDependentSize(
2454 LogicalAxis::Inline, aWM, bSize, boxSizingAdjust);
2457 // ComputeAutoSizeWithIntrinsicDimensions preserves the ratio when
2458 // applying the min/max-size. We don't want that when we have 'stretch'
2459 // in either axis because tentISize/tentBSize is likely not according to
2460 // ratio now.
2461 if (aspectRatio && stretchI != eStretch && stretchB != eStretch) {
2462 nsSize autoSize = nsLayoutUtils::ComputeAutoSizeWithIntrinsicDimensions(
2463 minISize, minBSize, maxISize, maxBSize, tentISize, tentBSize);
2464 // The nsSize that ComputeAutoSizeWithIntrinsicDimensions returns will
2465 // actually contain logical values if the parameters passed to it were
2466 // logical coordinates, so we do NOT perform a physical-to-logical
2467 // conversion here, but just assign the fields directly to our result.
2468 iSize = autoSize.width;
2469 bSize = autoSize.height;
2470 } else {
2471 // Not honoring an intrinsic ratio: clamp the dimensions independently.
2472 iSize = NS_CSS_MINMAX(tentISize, minISize, maxISize);
2473 bSize = NS_CSS_MINMAX(tentBSize, minBSize, maxBSize);
2475 } else {
2476 // 'auto' iSize, non-'auto' bSize
2477 bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2478 if (stretchI != eStretch) {
2479 if (aspectRatio) {
2480 iSize = aspectRatio.ComputeRatioDependentSize(
2481 LogicalAxis::Inline, aWM, bSize, boxSizingAdjust);
2482 } else if (hasIntrinsicISize) {
2483 if (!(aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize) &&
2484 intrinsicISize > iSize)) {
2485 iSize = intrinsicISize;
2486 } // else - leave iSize as is to fill the CB
2487 } else {
2488 iSize = fallbackIntrinsicSize.ISize(aWM);
2490 } // else - leave iSize as is to fill the CB
2491 iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2493 } else {
2494 if (isAutoBSize) {
2495 // non-'auto' iSize, 'auto' bSize
2496 iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2497 if (stretchB != eStretch) {
2498 if (aspectRatio) {
2499 bSize = aspectRatio.ComputeRatioDependentSize(LogicalAxis::Block, aWM,
2500 iSize, boxSizingAdjust);
2501 } else if (hasIntrinsicBSize) {
2502 if (!(aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize) &&
2503 intrinsicBSize > bSize)) {
2504 bSize = intrinsicBSize;
2505 } // else - leave bSize as is to fill the CB
2506 } else {
2507 bSize = fallbackIntrinsicSize.BSize(aWM);
2509 } // else - leave bSize as is to fill the CB
2510 bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2512 } else {
2513 // non-'auto' iSize, non-'auto' bSize
2514 iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2515 bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2519 return LogicalSize(aWM, iSize, bSize);
2522 nsRect nsContainerFrame::ComputeSimpleTightBounds(
2523 DrawTarget* aDrawTarget) const {
2524 if (StyleOutline()->ShouldPaintOutline() || StyleBorder()->HasBorder() ||
2525 !StyleBackground()->IsTransparent(this) ||
2526 StyleDisplay()->HasAppearance()) {
2527 // Not necessarily tight, due to clipping, negative
2528 // outline-offset, and lots of other issues, but that's OK
2529 return InkOverflowRect();
2532 nsRect r(0, 0, 0, 0);
2533 for (const auto& childLists : ChildLists()) {
2534 for (nsIFrame* child : childLists.mList) {
2535 r.UnionRect(
2536 r, child->ComputeTightBounds(aDrawTarget) + child->GetPosition());
2539 return r;
2542 void nsContainerFrame::PushDirtyBitToAbsoluteFrames() {
2543 if (!HasAnyStateBits(NS_FRAME_IS_DIRTY)) {
2544 return; // No dirty bit to push.
2546 if (!HasAbsolutelyPositionedChildren()) {
2547 return; // No absolute children to push to.
2549 GetAbsoluteContainingBlock()->MarkAllFramesDirty();
2552 // Define the MAX_FRAME_DEPTH to be the ContentSink's MAX_REFLOW_DEPTH plus
2553 // 4 for the frames above the document's frames:
2554 // the Viewport, GFXScroll, ScrollPort, and Canvas
2555 #define MAX_FRAME_DEPTH (MAX_REFLOW_DEPTH + 4)
2557 bool nsContainerFrame::IsFrameTreeTooDeep(const ReflowInput& aReflowInput,
2558 ReflowOutput& aMetrics,
2559 nsReflowStatus& aStatus) {
2560 if (aReflowInput.mReflowDepth > MAX_FRAME_DEPTH) {
2561 NS_WARNING("frame tree too deep; setting zero size and returning");
2562 AddStateBits(NS_FRAME_TOO_DEEP_IN_FRAME_TREE);
2563 ClearOverflowRects();
2564 aMetrics.ClearSize();
2565 aMetrics.SetBlockStartAscent(0);
2566 aMetrics.mCarriedOutBEndMargin.Zero();
2567 aMetrics.mOverflowAreas.Clear();
2569 aStatus.Reset();
2570 if (GetNextInFlow()) {
2571 // Reflow depth might vary between reflows, so we might have
2572 // successfully reflowed and split this frame before. If so, we
2573 // shouldn't delete its continuations.
2574 aStatus.SetIncomplete();
2577 return true;
2579 RemoveStateBits(NS_FRAME_TOO_DEEP_IN_FRAME_TREE);
2580 return false;
2583 bool nsContainerFrame::ShouldAvoidBreakInside(
2584 const ReflowInput& aReflowInput) const {
2585 MOZ_ASSERT(this == aReflowInput.mFrame,
2586 "Caller should pass a ReflowInput for this frame!");
2588 const auto* disp = StyleDisplay();
2589 const bool mayAvoidBreak = [&] {
2590 switch (disp->mBreakInside) {
2591 case StyleBreakWithin::Auto:
2592 return false;
2593 case StyleBreakWithin::Avoid:
2594 return true;
2595 case StyleBreakWithin::AvoidPage:
2596 return aReflowInput.mBreakType == ReflowInput::BreakType::Page;
2597 case StyleBreakWithin::AvoidColumn:
2598 return aReflowInput.mBreakType == ReflowInput::BreakType::Column;
2600 MOZ_ASSERT_UNREACHABLE("Unknown break-inside value");
2601 return false;
2602 }();
2604 if (!mayAvoidBreak) {
2605 return false;
2607 if (aReflowInput.mFlags.mIsTopOfPage) {
2608 return false;
2610 if (IsAbsolutelyPositioned(disp)) {
2611 return false;
2613 if (GetPrevInFlow()) {
2614 return false;
2616 return true;
2619 void nsContainerFrame::ConsiderChildOverflow(OverflowAreas& aOverflowAreas,
2620 nsIFrame* aChildFrame) {
2621 if (StyleDisplay()->IsContainLayout() && SupportsContainLayoutAndPaint()) {
2622 // If we have layout containment and are not a non-atomic, inline-level
2623 // principal box, we should only consider our child's ink overflow,
2624 // leaving the scrollable regions of the parent unaffected.
2625 // Note: scrollable overflow is a subset of ink overflow,
2626 // so this has the same affect as unioning the child's ink and
2627 // scrollable overflow with the parent's ink overflow.
2628 const OverflowAreas childOverflows(aChildFrame->InkOverflowRect(),
2629 nsRect());
2630 aOverflowAreas.UnionWith(childOverflows + aChildFrame->GetPosition());
2631 } else {
2632 aOverflowAreas.UnionWith(
2633 aChildFrame->GetActualAndNormalOverflowAreasRelativeToParent());
2637 StyleAlignFlags nsContainerFrame::CSSAlignmentForAbsPosChild(
2638 const ReflowInput& aChildRI, LogicalAxis aLogicalAxis) const {
2639 MOZ_ASSERT(aChildRI.mFrame->IsAbsolutelyPositioned(),
2640 "This method should only be called for abspos children");
2641 NS_ERROR(
2642 "Child classes that use css box alignment for abspos children "
2643 "should provide their own implementation of this method!");
2645 // In the unexpected/unlikely event that this implementation gets invoked,
2646 // just use "start" alignment.
2647 return StyleAlignFlags::START;
2650 nsOverflowContinuationTracker::nsOverflowContinuationTracker(
2651 nsContainerFrame* aFrame, bool aWalkOOFFrames,
2652 bool aSkipOverflowContainerChildren)
2653 : mOverflowContList(nullptr),
2654 mPrevOverflowCont(nullptr),
2655 mSentry(nullptr),
2656 mParent(aFrame),
2657 mSkipOverflowContainerChildren(aSkipOverflowContainerChildren),
2658 mWalkOOFFrames(aWalkOOFFrames) {
2659 MOZ_ASSERT(aFrame, "null frame pointer");
2660 SetupOverflowContList();
2663 void nsOverflowContinuationTracker::SetupOverflowContList() {
2664 MOZ_ASSERT(mParent, "null frame pointer");
2665 MOZ_ASSERT(!mOverflowContList, "already have list");
2666 nsContainerFrame* nif =
2667 static_cast<nsContainerFrame*>(mParent->GetNextInFlow());
2668 if (nif) {
2669 mOverflowContList = nif->GetOverflowContainers();
2670 if (mOverflowContList) {
2671 mParent = nif;
2672 SetUpListWalker();
2675 if (!mOverflowContList) {
2676 mOverflowContList = mParent->GetExcessOverflowContainers();
2677 if (mOverflowContList) {
2678 SetUpListWalker();
2684 * Helper function to walk past overflow continuations whose prev-in-flow
2685 * isn't a normal child and to set mSentry and mPrevOverflowCont correctly.
2687 void nsOverflowContinuationTracker::SetUpListWalker() {
2688 NS_ASSERTION(!mSentry && !mPrevOverflowCont,
2689 "forgot to reset mSentry or mPrevOverflowCont");
2690 if (mOverflowContList) {
2691 nsIFrame* cur = mOverflowContList->FirstChild();
2692 if (mSkipOverflowContainerChildren) {
2693 while (cur && cur->GetPrevInFlow()->HasAnyStateBits(
2694 NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2695 mPrevOverflowCont = cur;
2696 cur = cur->GetNextSibling();
2698 while (cur &&
2699 (cur->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) != mWalkOOFFrames)) {
2700 mPrevOverflowCont = cur;
2701 cur = cur->GetNextSibling();
2704 if (cur) {
2705 mSentry = cur->GetPrevInFlow();
2711 * Helper function to step forward through the overflow continuations list.
2712 * Sets mSentry and mPrevOverflowCont, skipping over OOF or non-OOF frames
2713 * as appropriate. May only be called when we have already set up an
2714 * mOverflowContList; mOverflowContList cannot be null.
2716 void nsOverflowContinuationTracker::StepForward() {
2717 MOZ_ASSERT(mOverflowContList, "null list");
2719 // Step forward
2720 if (mPrevOverflowCont) {
2721 mPrevOverflowCont = mPrevOverflowCont->GetNextSibling();
2722 } else {
2723 mPrevOverflowCont = mOverflowContList->FirstChild();
2726 // Skip over oof or non-oof frames as appropriate
2727 if (mSkipOverflowContainerChildren) {
2728 nsIFrame* cur = mPrevOverflowCont->GetNextSibling();
2729 while (cur &&
2730 (cur->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) != mWalkOOFFrames)) {
2731 mPrevOverflowCont = cur;
2732 cur = cur->GetNextSibling();
2736 // Set up the sentry
2737 mSentry = (mPrevOverflowCont->GetNextSibling())
2738 ? mPrevOverflowCont->GetNextSibling()->GetPrevInFlow()
2739 : nullptr;
2742 nsresult nsOverflowContinuationTracker::Insert(nsIFrame* aOverflowCont,
2743 nsReflowStatus& aReflowStatus) {
2744 MOZ_ASSERT(aOverflowCont, "null frame pointer");
2745 MOZ_ASSERT(!mSkipOverflowContainerChildren ||
2746 mWalkOOFFrames ==
2747 aOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW),
2748 "shouldn't insert frame that doesn't match walker type");
2749 MOZ_ASSERT(aOverflowCont->GetPrevInFlow(),
2750 "overflow containers must have a prev-in-flow");
2752 nsresult rv = NS_OK;
2753 bool reparented = false;
2754 nsPresContext* presContext = aOverflowCont->PresContext();
2755 bool addToList = !mSentry || aOverflowCont != mSentry->GetNextInFlow();
2757 // If we have a list and aOverflowCont is already in it then don't try to
2758 // add it again.
2759 if (addToList && aOverflowCont->GetParent() == mParent &&
2760 aOverflowCont->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) &&
2761 mOverflowContList && mOverflowContList->ContainsFrame(aOverflowCont)) {
2762 addToList = false;
2763 mPrevOverflowCont = aOverflowCont->GetPrevSibling();
2766 if (addToList) {
2767 if (aOverflowCont->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2768 // aOverflowCont is in some other overflow container list,
2769 // steal it first
2770 NS_ASSERTION(!(mOverflowContList &&
2771 mOverflowContList->ContainsFrame(aOverflowCont)),
2772 "overflow containers out of order");
2773 aOverflowCont->GetParent()->StealFrame(aOverflowCont);
2774 } else {
2775 aOverflowCont->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
2777 if (!mOverflowContList) {
2778 // Note: We don't use SetExcessOverflowContainers() since it requires
2779 // setting a non-empty list. It's OK to manually set an empty list to
2780 // ExcessOverflowContainersProperty() because we are going to insert
2781 // aOverflowCont to mOverflowContList below, which guarantees an nonempty
2782 // list in ExcessOverflowContainersProperty().
2783 mOverflowContList = new (presContext->PresShell()) nsFrameList();
2784 mParent->SetProperty(nsContainerFrame::ExcessOverflowContainersProperty(),
2785 mOverflowContList);
2786 SetUpListWalker();
2788 if (aOverflowCont->GetParent() != mParent) {
2789 nsContainerFrame::ReparentFrameView(aOverflowCont,
2790 aOverflowCont->GetParent(), mParent);
2791 reparented = true;
2794 // If aOverflowCont has a prev/next-in-flow that might be in
2795 // mOverflowContList we need to find it and insert after/before it to
2796 // maintain the order amongst next-in-flows in this list.
2797 nsIFrame* pif = aOverflowCont->GetPrevInFlow();
2798 nsIFrame* nif = aOverflowCont->GetNextInFlow();
2799 if ((pif && pif->GetParent() == mParent && pif != mPrevOverflowCont) ||
2800 (nif && nif->GetParent() == mParent && mPrevOverflowCont)) {
2801 for (nsIFrame* f : *mOverflowContList) {
2802 if (f == pif) {
2803 mPrevOverflowCont = pif;
2804 break;
2806 if (f == nif) {
2807 mPrevOverflowCont = f->GetPrevSibling();
2808 break;
2813 mOverflowContList->InsertFrame(mParent, mPrevOverflowCont, aOverflowCont);
2814 aReflowStatus.SetNextInFlowNeedsReflow();
2817 // If we need to reflow it, mark it dirty
2818 if (aReflowStatus.NextInFlowNeedsReflow()) {
2819 aOverflowCont->MarkSubtreeDirty();
2822 // It's in our list, just step forward
2823 StepForward();
2824 NS_ASSERTION(mPrevOverflowCont == aOverflowCont ||
2825 (mSkipOverflowContainerChildren &&
2826 mPrevOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) !=
2827 aOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)),
2828 "OverflowContTracker in unexpected state");
2830 if (addToList) {
2831 // Convert all non-overflow-container next-in-flows of aOverflowCont
2832 // into overflow containers and move them to our overflow
2833 // tracker. This preserves the invariant that the next-in-flows
2834 // of an overflow container are also overflow containers.
2835 nsIFrame* f = aOverflowCont->GetNextInFlow();
2836 if (f && (!f->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) ||
2837 (!reparented && f->GetParent() == mParent) ||
2838 (reparented && f->GetParent() != mParent))) {
2839 if (!f->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2840 f->GetParent()->StealFrame(f);
2842 Insert(f, aReflowStatus);
2845 return rv;
2848 void nsOverflowContinuationTracker::BeginFinish(nsIFrame* aChild) {
2849 MOZ_ASSERT(aChild, "null ptr");
2850 MOZ_ASSERT(aChild->GetNextInFlow(),
2851 "supposed to call Finish *before* deleting next-in-flow!");
2853 for (nsIFrame* f = aChild; f; f = f->GetNextInFlow()) {
2854 // We'll update these in EndFinish after the next-in-flows are gone.
2855 if (f == mPrevOverflowCont) {
2856 mSentry = nullptr;
2857 mPrevOverflowCont = nullptr;
2858 break;
2860 if (f == mSentry) {
2861 mSentry = nullptr;
2862 break;
2867 void nsOverflowContinuationTracker::EndFinish(nsIFrame* aChild) {
2868 if (!mOverflowContList) {
2869 return;
2871 // Forget mOverflowContList if it was deleted.
2872 nsFrameList* eoc = mParent->GetExcessOverflowContainers();
2873 if (eoc != mOverflowContList) {
2874 nsFrameList* oc = mParent->GetOverflowContainers();
2875 if (oc != mOverflowContList) {
2876 // mOverflowContList was deleted
2877 mPrevOverflowCont = nullptr;
2878 mSentry = nullptr;
2879 mParent = aChild->GetParent();
2880 mOverflowContList = nullptr;
2881 SetupOverflowContList();
2882 return;
2885 // The list survived, update mSentry if needed.
2886 if (!mSentry) {
2887 if (!mPrevOverflowCont) {
2888 SetUpListWalker();
2889 } else {
2890 mozilla::AutoRestore<nsIFrame*> saved(mPrevOverflowCont);
2891 // step backward to make StepForward() use our current mPrevOverflowCont
2892 mPrevOverflowCont = mPrevOverflowCont->GetPrevSibling();
2893 StepForward();
2898 /////////////////////////////////////////////////////////////////////////////
2899 // Debugging
2901 #ifdef DEBUG
2902 void nsContainerFrame::SanityCheckChildListsBeforeReflow() const {
2903 MOZ_ASSERT(IsFlexOrGridContainer(),
2904 "Only Flex / Grid containers can call this!");
2906 const auto didPushItemsBit = IsFlexContainerFrame()
2907 ? NS_STATE_FLEX_DID_PUSH_ITEMS
2908 : NS_STATE_GRID_DID_PUSH_ITEMS;
2909 ChildListIDs absLists = {FrameChildListID::Absolute, FrameChildListID::Fixed,
2910 FrameChildListID::OverflowContainers,
2911 FrameChildListID::ExcessOverflowContainers};
2912 ChildListIDs itemLists = {FrameChildListID::Principal,
2913 FrameChildListID::Overflow};
2914 for (const nsIFrame* f = this; f; f = f->GetNextInFlow()) {
2915 MOZ_ASSERT(!f->HasAnyStateBits(didPushItemsBit),
2916 "At start of reflow, we should've pulled items back from all "
2917 "NIFs and cleared the state bit stored in didPushItemsBit in "
2918 "the process.");
2919 for (const auto& [list, listID] : f->ChildLists()) {
2920 if (!itemLists.contains(listID)) {
2921 MOZ_ASSERT(
2922 absLists.contains(listID) || listID == FrameChildListID::Backdrop,
2923 "unexpected non-empty child list");
2924 continue;
2926 for (const auto* child : list) {
2927 MOZ_ASSERT(f == this || child->GetPrevInFlow(),
2928 "all pushed items must be pulled up before reflow");
2932 // If we have a prev-in-flow, each of its children's next-in-flow
2933 // should be one of our children or be null.
2934 const auto* pif = static_cast<nsContainerFrame*>(GetPrevInFlow());
2935 if (pif) {
2936 const nsFrameList* oc = GetOverflowContainers();
2937 const nsFrameList* eoc = GetExcessOverflowContainers();
2938 const nsFrameList* pifEOC = pif->GetExcessOverflowContainers();
2939 for (const nsIFrame* child : pif->PrincipalChildList()) {
2940 const nsIFrame* childNIF = child->GetNextInFlow();
2941 MOZ_ASSERT(!childNIF || mFrames.ContainsFrame(childNIF) ||
2942 (pifEOC && pifEOC->ContainsFrame(childNIF)) ||
2943 (oc && oc->ContainsFrame(childNIF)) ||
2944 (eoc && eoc->ContainsFrame(childNIF)));
2949 void nsContainerFrame::SetDidPushItemsBitIfNeeded(ChildListID aListID,
2950 nsIFrame* aOldFrame) {
2951 MOZ_ASSERT(IsFlexOrGridContainer(),
2952 "Only Flex / Grid containers can call this!");
2954 // Note that FrameChildListID::Principal doesn't mean aOldFrame must be on
2955 // that list. It can also be on FrameChildListID::Overflow, in which case it
2956 // might be a pushed item, and if it's the only pushed item our DID_PUSH_ITEMS
2957 // bit will lie.
2958 if (aListID == FrameChildListID::Principal && !aOldFrame->GetPrevInFlow()) {
2959 // Since the bit may lie, set the mDidPushItemsBitMayLie value to true for
2960 // ourself and for all our prev-in-flows.
2961 nsContainerFrame* frameThatMayLie = this;
2962 do {
2963 frameThatMayLie->mDidPushItemsBitMayLie = true;
2964 frameThatMayLie =
2965 static_cast<nsContainerFrame*>(frameThatMayLie->GetPrevInFlow());
2966 } while (frameThatMayLie);
2969 #endif
2971 #ifdef DEBUG_FRAME_DUMP
2972 void nsContainerFrame::List(FILE* out, const char* aPrefix,
2973 ListFlags aFlags) const {
2974 nsCString str;
2975 ListGeneric(str, aPrefix, aFlags);
2976 ExtraContainerFrameInfo(str);
2978 // Output the frame name and various fields.
2979 fprintf_stderr(out, "%s <\n", str.get());
2981 const nsCString pfx = nsCString(aPrefix) + " "_ns;
2983 // Output principal child list separately since we want to omit its
2984 // name and address.
2985 for (nsIFrame* kid : PrincipalChildList()) {
2986 kid->List(out, pfx.get(), aFlags);
2989 // Output rest of the child lists.
2990 const ChildListIDs skippedListIDs = {FrameChildListID::Principal};
2991 ListChildLists(out, pfx.get(), aFlags, skippedListIDs);
2993 fprintf_stderr(out, "%s>\n", aPrefix);
2996 void nsContainerFrame::ListWithMatchedRules(FILE* out,
2997 const char* aPrefix) const {
2998 fprintf_stderr(out, "%s%s\n", aPrefix, ListTag().get());
3000 nsCString rulePrefix;
3001 rulePrefix += aPrefix;
3002 rulePrefix += " ";
3003 ListMatchedRules(out, rulePrefix.get());
3005 nsCString childPrefix;
3006 childPrefix += aPrefix;
3007 childPrefix += " ";
3009 for (const auto& childList : ChildLists()) {
3010 for (const nsIFrame* kid : childList.mList) {
3011 kid->ListWithMatchedRules(out, childPrefix.get());
3016 void nsContainerFrame::ListChildLists(FILE* aOut, const char* aPrefix,
3017 ListFlags aFlags,
3018 ChildListIDs aSkippedListIDs) const {
3019 const nsCString nestedPfx = nsCString(aPrefix) + " "_ns;
3021 for (const auto& [list, listID] : ChildLists()) {
3022 if (aSkippedListIDs.contains(listID)) {
3023 continue;
3026 // Use nsPrintfCString so that %p don't output prefix "0x". This is
3027 // consistent with nsIFrame::ListTag().
3028 const nsPrintfCString str("%s%s@%p <\n", aPrefix, ChildListName(listID),
3029 &GetChildList(listID));
3030 fprintf_stderr(aOut, "%s", str.get());
3032 for (nsIFrame* kid : list) {
3033 // Verify the child frame's parent frame pointer is correct.
3034 NS_ASSERTION(kid->GetParent() == this, "Bad parent frame pointer!");
3035 kid->List(aOut, nestedPfx.get(), aFlags);
3037 fprintf_stderr(aOut, "%s>\n", aPrefix);
3041 void nsContainerFrame::ExtraContainerFrameInfo(nsACString& aTo) const {
3042 (void)aTo;
3045 #endif