Bug 1876335 - use GRADLE_MAVEN_REPOSITORIES in more places. r=owlish,geckoview-review...
[gecko.git] / layout / generic / nsContainerFrame.cpp
blob2577a7a00d47c2ee22cbdfc467693ca10f58f2b5
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 // replaced elements always shrink-wrap
818 if (aFlags.contains(ComputeSizeFlag::ShrinkWrap) || IsReplaced()) {
819 // Only bother computing our 'auto' ISize if the result will be used.
820 const auto& styleISize = aSizeOverrides.mStyleISize
821 ? *aSizeOverrides.mStyleISize
822 : StylePosition()->ISize(aWM);
823 if (styleISize.IsAuto()) {
824 result.ISize(aWM) =
825 ShrinkISizeToFit(aRenderingContext, availBased, aFlags);
827 } else {
828 result.ISize(aWM) = availBased;
831 if (IsTableCaption()) {
832 // If we're a container for font size inflation, then shrink
833 // wrapping inside of us should not apply font size inflation.
834 AutoMaybeDisableFontInflation an(this);
836 WritingMode tableWM = GetParent()->GetWritingMode();
837 if (aWM.IsOrthogonalTo(tableWM)) {
838 // For an orthogonal caption on a block-dir side of the table, shrink-wrap
839 // to min-isize.
840 result.ISize(aWM) = GetMinISize(aRenderingContext);
841 } else {
842 // The outer frame constrains our available isize to the isize of
843 // the table. Grow if our min-isize is bigger than that, but not
844 // larger than the containing block isize. (It would really be nice
845 // to transmit that information another way, so we could grow up to
846 // the table's available isize, but that's harder.)
847 nscoord min = GetMinISize(aRenderingContext);
848 if (min > aCBSize.ISize(aWM)) {
849 min = aCBSize.ISize(aWM);
851 if (min > result.ISize(aWM)) {
852 result.ISize(aWM) = min;
856 return result;
859 void nsContainerFrame::ReflowChild(
860 nsIFrame* aKidFrame, nsPresContext* aPresContext,
861 ReflowOutput& aDesiredSize, const ReflowInput& aReflowInput,
862 const WritingMode& aWM, const LogicalPoint& aPos,
863 const nsSize& aContainerSize, ReflowChildFlags aFlags,
864 nsReflowStatus& aStatus, nsOverflowContinuationTracker* aTracker) {
865 MOZ_ASSERT(aReflowInput.mFrame == aKidFrame, "bad reflow input");
866 if (aWM.IsPhysicalRTL()) {
867 NS_ASSERTION(aContainerSize.width != NS_UNCONSTRAINEDSIZE,
868 "ReflowChild with unconstrained container width!");
870 MOZ_ASSERT(aDesiredSize.InkOverflow() == nsRect(0, 0, 0, 0) &&
871 aDesiredSize.ScrollableOverflow() == nsRect(0, 0, 0, 0),
872 "please reset the overflow areas before calling ReflowChild");
874 // Position the child frame and its view if requested.
875 if (ReflowChildFlags::NoMoveFrame !=
876 (aFlags & ReflowChildFlags::NoMoveFrame)) {
877 aKidFrame->SetPosition(aWM, aPos, aContainerSize);
880 if (!(aFlags & ReflowChildFlags::NoMoveView)) {
881 PositionFrameView(aKidFrame);
882 PositionChildViews(aKidFrame);
885 // Reflow the child frame
886 aKidFrame->Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
888 // If the child frame is complete, delete any next-in-flows,
889 // but only if the NoDeleteNextInFlowChild flag isn't set.
890 if (!aStatus.IsInlineBreakBefore() && aStatus.IsFullyComplete() &&
891 !(aFlags & ReflowChildFlags::NoDeleteNextInFlowChild)) {
892 if (nsIFrame* kidNextInFlow = aKidFrame->GetNextInFlow()) {
893 // Remove all of the childs next-in-flows. Make sure that we ask
894 // the right parent to do the removal (it's possible that the
895 // parent is not this because we are executing pullup code)
896 nsOverflowContinuationTracker::AutoFinish fini(aTracker, aKidFrame);
897 DestroyContext context(PresShell());
898 kidNextInFlow->GetParent()->DeleteNextInFlowChild(context, kidNextInFlow,
899 true);
904 // XXX temporary: hold on to a copy of the old physical version of
905 // ReflowChild so that we can convert callers incrementally.
906 void nsContainerFrame::ReflowChild(nsIFrame* aKidFrame,
907 nsPresContext* aPresContext,
908 ReflowOutput& aDesiredSize,
909 const ReflowInput& aReflowInput, nscoord aX,
910 nscoord aY, ReflowChildFlags aFlags,
911 nsReflowStatus& aStatus,
912 nsOverflowContinuationTracker* aTracker) {
913 MOZ_ASSERT(aReflowInput.mFrame == aKidFrame, "bad reflow input");
915 // Position the child frame and its view if requested.
916 if (ReflowChildFlags::NoMoveFrame !=
917 (aFlags & ReflowChildFlags::NoMoveFrame)) {
918 aKidFrame->SetPosition(nsPoint(aX, aY));
921 if (!(aFlags & ReflowChildFlags::NoMoveView)) {
922 PositionFrameView(aKidFrame);
923 PositionChildViews(aKidFrame);
926 // Reflow the child frame
927 aKidFrame->Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
929 // If the child frame is complete, delete any next-in-flows,
930 // but only if the NoDeleteNextInFlowChild flag isn't set.
931 if (aStatus.IsFullyComplete() &&
932 !(aFlags & ReflowChildFlags::NoDeleteNextInFlowChild)) {
933 if (nsIFrame* kidNextInFlow = aKidFrame->GetNextInFlow()) {
934 // Remove all of the childs next-in-flows. Make sure that we ask
935 // the right parent to do the removal (it's possible that the
936 // parent is not this because we are executing pullup code)
937 nsOverflowContinuationTracker::AutoFinish fini(aTracker, aKidFrame);
938 DestroyContext context(PresShell());
939 kidNextInFlow->GetParent()->DeleteNextInFlowChild(context, kidNextInFlow,
940 true);
946 * Position the views of |aFrame|'s descendants. A container frame
947 * should call this method if it moves a frame after |Reflow|.
949 void nsContainerFrame::PositionChildViews(nsIFrame* aFrame) {
950 if (!aFrame->HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW)) {
951 return;
954 // Recursively walk aFrame's child frames.
955 // Process the additional child lists, but skip the popup list as the view for
956 // popups is managed by the parent.
957 // Currently only nsMenuFrame has a popupList and during layout will adjust
958 // the view manually to position the popup.
959 for (const auto& [list, listID] : aFrame->ChildLists()) {
960 if (listID == FrameChildListID::Popup) {
961 continue;
963 for (nsIFrame* childFrame : list) {
964 // Position the frame's view (if it has one) otherwise recursively
965 // process its children
966 if (childFrame->HasView()) {
967 PositionFrameView(childFrame);
968 } else {
969 PositionChildViews(childFrame);
975 void nsContainerFrame::FinishReflowChild(
976 nsIFrame* aKidFrame, nsPresContext* aPresContext,
977 const ReflowOutput& aDesiredSize, const ReflowInput* aReflowInput,
978 const WritingMode& aWM, const LogicalPoint& aPos,
979 const nsSize& aContainerSize, nsIFrame::ReflowChildFlags aFlags) {
980 MOZ_ASSERT(!aReflowInput || aReflowInput->mFrame == aKidFrame);
981 MOZ_ASSERT(aReflowInput || aKidFrame->IsMathMLFrame() ||
982 aKidFrame->IsTableCellFrame(),
983 "aReflowInput should be passed in almost all cases");
985 if (aWM.IsPhysicalRTL()) {
986 NS_ASSERTION(aContainerSize.width != NS_UNCONSTRAINEDSIZE,
987 "FinishReflowChild with unconstrained container width!");
990 nsPoint curOrigin = aKidFrame->GetPosition();
991 const LogicalSize convertedSize = aDesiredSize.Size(aWM);
992 LogicalPoint pos(aPos);
994 if (aFlags & ReflowChildFlags::ApplyRelativePositioning) {
995 MOZ_ASSERT(aReflowInput, "caller must have passed reflow input");
996 // ApplyRelativePositioning in right-to-left writing modes needs to know
997 // the updated frame width to set the normal position correctly.
998 aKidFrame->SetSize(aWM, convertedSize);
1000 const LogicalMargin offsets = aReflowInput->ComputedLogicalOffsets(aWM);
1001 ReflowInput::ApplyRelativePositioning(aKidFrame, aWM, offsets, &pos,
1002 aContainerSize);
1005 if (ReflowChildFlags::NoMoveFrame !=
1006 (aFlags & ReflowChildFlags::NoMoveFrame)) {
1007 aKidFrame->SetRect(aWM, LogicalRect(aWM, pos, convertedSize),
1008 aContainerSize);
1009 } else {
1010 aKidFrame->SetSize(aWM, convertedSize);
1013 if (aKidFrame->HasView()) {
1014 nsView* view = aKidFrame->GetView();
1015 // Make sure the frame's view is properly sized and positioned and has
1016 // things like opacity correct
1017 SyncFrameViewAfterReflow(aPresContext, aKidFrame, view,
1018 aDesiredSize.InkOverflow(), aFlags);
1021 nsPoint newOrigin = aKidFrame->GetPosition();
1022 if (!(aFlags & ReflowChildFlags::NoMoveView) && curOrigin != newOrigin) {
1023 if (!aKidFrame->HasView()) {
1024 // If the frame has moved, then we need to make sure any child views are
1025 // correctly positioned
1026 PositionChildViews(aKidFrame);
1030 aKidFrame->DidReflow(aPresContext, aReflowInput);
1032 #if defined(_MSC_VER) && !defined(__clang__) && defined(_M_AMD64)
1033 # pragma optimize("", on)
1034 #endif
1036 // XXX temporary: hold on to a copy of the old physical version of
1037 // FinishReflowChild so that we can convert callers incrementally.
1038 void nsContainerFrame::FinishReflowChild(nsIFrame* aKidFrame,
1039 nsPresContext* aPresContext,
1040 const ReflowOutput& aDesiredSize,
1041 const ReflowInput* aReflowInput,
1042 nscoord aX, nscoord aY,
1043 ReflowChildFlags aFlags) {
1044 MOZ_ASSERT(!(aFlags & ReflowChildFlags::ApplyRelativePositioning),
1045 "only the logical version supports ApplyRelativePositioning "
1046 "since ApplyRelativePositioning requires the container size");
1048 nsPoint curOrigin = aKidFrame->GetPosition();
1049 nsPoint pos(aX, aY);
1050 nsSize size(aDesiredSize.PhysicalSize());
1052 if (ReflowChildFlags::NoMoveFrame !=
1053 (aFlags & ReflowChildFlags::NoMoveFrame)) {
1054 aKidFrame->SetRect(nsRect(pos, size));
1055 } else {
1056 aKidFrame->SetSize(size);
1059 if (aKidFrame->HasView()) {
1060 nsView* view = aKidFrame->GetView();
1061 // Make sure the frame's view is properly sized and positioned and has
1062 // things like opacity correct
1063 SyncFrameViewAfterReflow(aPresContext, aKidFrame, view,
1064 aDesiredSize.InkOverflow(), aFlags);
1067 if (!(aFlags & ReflowChildFlags::NoMoveView) && curOrigin != pos) {
1068 if (!aKidFrame->HasView()) {
1069 // If the frame has moved, then we need to make sure any child views are
1070 // correctly positioned
1071 PositionChildViews(aKidFrame);
1075 aKidFrame->DidReflow(aPresContext, aReflowInput);
1078 void nsContainerFrame::ReflowOverflowContainerChildren(
1079 nsPresContext* aPresContext, const ReflowInput& aReflowInput,
1080 OverflowAreas& aOverflowRects, ReflowChildFlags aFlags,
1081 nsReflowStatus& aStatus, ChildFrameMerger aMergeFunc,
1082 Maybe<nsSize> aContainerSize) {
1083 MOZ_ASSERT(aPresContext, "null pointer");
1085 nsFrameList* overflowContainers =
1086 DrainExcessOverflowContainersList(aMergeFunc);
1087 if (!overflowContainers) {
1088 return; // nothing to reflow
1091 nsOverflowContinuationTracker tracker(this, false, false);
1092 bool shouldReflowAllKids = aReflowInput.ShouldReflowAllKids();
1094 for (nsIFrame* frame : *overflowContainers) {
1095 if (frame->GetPrevInFlow()->GetParent() != GetPrevInFlow()) {
1096 // frame's prevInFlow has moved, skip reflowing this frame;
1097 // it will get reflowed once it's been placed
1098 if (GetNextInFlow()) {
1099 // We report OverflowIncomplete status in this case to avoid our parent
1100 // deleting our next-in-flows which might destroy non-empty frames.
1101 nsReflowStatus status;
1102 status.SetOverflowIncomplete();
1103 aStatus.MergeCompletionStatusFrom(status);
1105 continue;
1108 auto ScrollableOverflowExceedsAvailableBSize =
1109 [this, &aReflowInput](nsIFrame* aFrame) {
1110 if (aReflowInput.AvailableBSize() == NS_UNCONSTRAINEDSIZE) {
1111 return false;
1113 const auto parentWM = GetWritingMode();
1114 const nscoord scrollableOverflowRectBEnd =
1115 LogicalRect(parentWM,
1116 aFrame->ScrollableOverflowRectRelativeToParent(),
1117 GetSize())
1118 .BEnd(parentWM);
1119 return scrollableOverflowRectBEnd > aReflowInput.AvailableBSize();
1122 // If the available block-size has changed, or the existing scrollable
1123 // overflow's block-end exceeds it, we need to reflow even if the frame
1124 // isn't dirty.
1125 if (shouldReflowAllKids || frame->IsSubtreeDirty() ||
1126 ScrollableOverflowExceedsAvailableBSize(frame)) {
1127 nsIFrame* prevInFlow = frame->GetPrevInFlow();
1128 NS_ASSERTION(prevInFlow,
1129 "overflow container frame must have a prev-in-flow");
1130 NS_ASSERTION(
1131 frame->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1132 "overflow container frame must have overflow container bit set");
1133 WritingMode wm = frame->GetWritingMode();
1134 // Note: aReflowInput's available inline-size is technically wrong for us
1135 // to hand off to children here, because it doesn't account for the space
1136 // that's been used for the container's margin/border/padding (and some
1137 // other space that a concrete container type, e.g. fieldset and grid [1],
1138 // might reserve before setting up the available space for their
1139 // children). Since we don't have a way to query the specific available
1140 // inline-size each container type used, nor do we know how the container
1141 // computes its non-overflow-container children's inline-size, we just
1142 // unconditionally override the frame's inline-size, so that the available
1143 // inline-size for the children doesn't really matter anyway.
1145 // [1] For example, fieldset uses its computed inline-size with padding as
1146 // the available inline-size to reflow its inner child frame.
1147 // https://searchfox.org/mozilla-central/rev/04f7743d94691fa24212fb43099f9d84c3bfc890/layout/forms/nsFieldSetFrame.cpp#535-536
1148 const LogicalSize availSpace = aReflowInput.AvailableSize(wm);
1150 StyleSizeOverrides sizeOverride;
1151 // We override current continuation's inline-size by using the
1152 // prev-in-flow's inline-size since both should be the same.
1153 sizeOverride.mStyleISize.emplace(
1154 StyleSize::LengthPercentage(LengthPercentage::FromAppUnits(
1155 frame->StylePosition()->mBoxSizing == StyleBoxSizing::Border
1156 ? prevInFlow->ISize(wm)
1157 : prevInFlow->ContentISize(wm))));
1159 if (frame->IsFlexItem()) {
1160 // An overflow container's block-size must be 0.
1161 sizeOverride.mStyleBSize.emplace(
1162 StyleSize::LengthPercentage(LengthPercentage::FromAppUnits(0)));
1164 ReflowOutput desiredSize(wm);
1165 ReflowInput reflowInput(aPresContext, aReflowInput, frame, availSpace,
1166 Nothing(), {}, sizeOverride);
1167 const nsSize containerSize =
1168 aContainerSize ? *aContainerSize
1169 : aReflowInput.AvailableSize(wm).GetPhysicalSize(wm);
1170 const LogicalPoint pos(wm, prevInFlow->IStart(wm, containerSize), 0);
1171 nsReflowStatus frameStatus;
1173 ReflowChild(frame, aPresContext, desiredSize, reflowInput, wm, pos,
1174 containerSize, aFlags, frameStatus, &tracker);
1175 FinishReflowChild(frame, aPresContext, desiredSize, &reflowInput, wm, pos,
1176 containerSize, aFlags);
1178 // Handle continuations
1179 if (!frameStatus.IsFullyComplete()) {
1180 if (frame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
1181 // Abspos frames can't cause their parent to be incomplete,
1182 // only overflow incomplete.
1183 frameStatus.SetOverflowIncomplete();
1184 } else {
1185 NS_ASSERTION(frameStatus.IsComplete(),
1186 "overflow container frames can't be incomplete, only "
1187 "overflow-incomplete");
1190 // Acquire a next-in-flow, creating it if necessary
1191 nsIFrame* nif = frame->GetNextInFlow();
1192 if (!nif) {
1193 NS_ASSERTION(frameStatus.NextInFlowNeedsReflow(),
1194 "Someone forgot a NextInFlowNeedsReflow flag");
1195 nif = PresShell()->FrameConstructor()->CreateContinuingFrame(frame,
1196 this);
1197 } else if (!nif->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1198 // used to be a normal next-in-flow; steal it from the child list
1199 nif->GetParent()->StealFrame(nif);
1202 tracker.Insert(nif, frameStatus);
1204 aStatus.MergeCompletionStatusFrom(frameStatus);
1205 // At this point it would be nice to assert
1206 // !frame->GetOverflowRect().IsEmpty(), but we have some unsplittable
1207 // frames that, when taller than availableHeight will push zero-height
1208 // content into a next-in-flow.
1209 } else {
1210 tracker.Skip(frame, aStatus);
1211 if (aReflowInput.mFloatManager) {
1212 nsBlockFrame::RecoverFloatsFor(frame, *aReflowInput.mFloatManager,
1213 aReflowInput.GetWritingMode(),
1214 aReflowInput.ComputedPhysicalSize());
1217 ConsiderChildOverflow(aOverflowRects, frame);
1221 void nsContainerFrame::DisplayOverflowContainers(
1222 nsDisplayListBuilder* aBuilder, const nsDisplayListSet& aLists) {
1223 nsFrameList* overflowconts = GetOverflowContainers();
1224 if (overflowconts) {
1225 for (nsIFrame* frame : *overflowconts) {
1226 BuildDisplayListForChild(aBuilder, frame, aLists);
1231 bool nsContainerFrame::TryRemoveFrame(FrameListPropertyDescriptor aProp,
1232 nsIFrame* aChildToRemove) {
1233 nsFrameList* list = GetProperty(aProp);
1234 if (list && list->StartRemoveFrame(aChildToRemove)) {
1235 // aChildToRemove *may* have been removed from this list.
1236 if (list->IsEmpty()) {
1237 Unused << TakeProperty(aProp);
1238 list->Delete(PresShell());
1240 return true;
1242 return false;
1245 bool nsContainerFrame::MaybeStealOverflowContainerFrame(nsIFrame* aChild) {
1246 bool removed = false;
1247 if (MOZ_UNLIKELY(aChild->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER))) {
1248 // Try removing from the overflow container list.
1249 removed = TryRemoveFrame(OverflowContainersProperty(), aChild);
1250 if (!removed) {
1251 // It might be in the excess overflow container list.
1252 removed = TryRemoveFrame(ExcessOverflowContainersProperty(), aChild);
1255 return removed;
1258 void nsContainerFrame::StealFrame(nsIFrame* aChild) {
1259 #ifdef DEBUG
1260 if (!mFrames.ContainsFrame(aChild)) {
1261 nsFrameList* list = GetOverflowFrames();
1262 if (!list || !list->ContainsFrame(aChild)) {
1263 list = GetOverflowContainers();
1264 if (!list || !list->ContainsFrame(aChild)) {
1265 list = GetExcessOverflowContainers();
1266 MOZ_ASSERT(list && list->ContainsFrame(aChild),
1267 "aChild isn't our child"
1268 " or on a frame list not supported by StealFrame");
1272 #endif
1274 if (MaybeStealOverflowContainerFrame(aChild)) {
1275 return;
1278 // NOTE nsColumnSetFrame and nsCanvasFrame have their overflow containers
1279 // on the normal lists so we might get here also if the frame bit
1280 // NS_FRAME_IS_OVERFLOW_CONTAINER is set.
1281 if (mFrames.StartRemoveFrame(aChild)) {
1282 return;
1285 // We didn't find the child in our principal child list.
1286 // Maybe it's on the overflow list?
1287 nsFrameList* frameList = GetOverflowFrames();
1288 if (frameList && frameList->ContinueRemoveFrame(aChild)) {
1289 if (frameList->IsEmpty()) {
1290 DestroyOverflowList();
1292 return;
1295 MOZ_ASSERT_UNREACHABLE("StealFrame: can't find aChild");
1298 nsFrameList nsContainerFrame::StealFramesAfter(nsIFrame* aChild) {
1299 NS_ASSERTION(
1300 !aChild || !aChild->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1301 "StealFramesAfter doesn't handle overflow containers");
1302 NS_ASSERTION(!IsBlockFrame(), "unexpected call");
1304 if (!aChild) {
1305 return std::move(mFrames);
1308 for (nsIFrame* f : mFrames) {
1309 if (f == aChild) {
1310 return mFrames.TakeFramesAfter(f);
1314 // We didn't find the child in the principal child list.
1315 // Maybe it's on the overflow list?
1316 if (nsFrameList* overflowFrames = GetOverflowFrames()) {
1317 for (nsIFrame* f : *overflowFrames) {
1318 if (f == aChild) {
1319 return mFrames.TakeFramesAfter(f);
1324 NS_ERROR("StealFramesAfter: can't find aChild");
1325 return nsFrameList();
1329 * Create a next-in-flow for aFrame. Will return the newly created
1330 * frame <b>if and only if</b> a new frame is created; otherwise
1331 * nullptr is returned.
1333 nsIFrame* nsContainerFrame::CreateNextInFlow(nsIFrame* aFrame) {
1334 MOZ_ASSERT(
1335 !IsBlockFrame(),
1336 "you should have called nsBlockFrame::CreateContinuationFor instead");
1337 MOZ_ASSERT(mFrames.ContainsFrame(aFrame), "expected an in-flow child frame");
1339 nsIFrame* nextInFlow = aFrame->GetNextInFlow();
1340 if (nullptr == nextInFlow) {
1341 // Create a continuation frame for the child frame and insert it
1342 // into our child list.
1343 nextInFlow =
1344 PresShell()->FrameConstructor()->CreateContinuingFrame(aFrame, this);
1345 mFrames.InsertFrame(nullptr, aFrame, nextInFlow);
1347 NS_FRAME_LOG(NS_FRAME_TRACE_NEW_FRAMES,
1348 ("nsContainerFrame::CreateNextInFlow: frame=%p nextInFlow=%p",
1349 aFrame, nextInFlow));
1351 return nextInFlow;
1353 return nullptr;
1357 * Remove and delete aNextInFlow and its next-in-flows. Updates the sibling and
1358 * flow pointers
1360 void nsContainerFrame::DeleteNextInFlowChild(DestroyContext& aContext,
1361 nsIFrame* aNextInFlow,
1362 bool aDeletingEmptyFrames) {
1363 #ifdef DEBUG
1364 nsIFrame* prevInFlow = aNextInFlow->GetPrevInFlow();
1365 #endif
1366 MOZ_ASSERT(prevInFlow, "bad prev-in-flow");
1368 // If the next-in-flow has a next-in-flow then delete it, too (and
1369 // delete it first).
1370 // Do this in a loop so we don't overflow the stack for frames
1371 // with very many next-in-flows
1372 nsIFrame* nextNextInFlow = aNextInFlow->GetNextInFlow();
1373 if (nextNextInFlow) {
1374 AutoTArray<nsIFrame*, 8> frames;
1375 for (nsIFrame* f = nextNextInFlow; f; f = f->GetNextInFlow()) {
1376 frames.AppendElement(f);
1378 for (nsIFrame* delFrame : Reversed(frames)) {
1379 nsContainerFrame* parent = delFrame->GetParent();
1380 parent->DeleteNextInFlowChild(aContext, delFrame, aDeletingEmptyFrames);
1384 // Take the next-in-flow out of the parent's child list
1385 StealFrame(aNextInFlow);
1387 #ifdef DEBUG
1388 if (aDeletingEmptyFrames) {
1389 nsLayoutUtils::AssertTreeOnlyEmptyNextInFlows(aNextInFlow);
1391 #endif
1393 // Delete the next-in-flow frame and its descendants. This will also
1394 // remove it from its next-in-flow/prev-in-flow chain.
1395 aNextInFlow->Destroy(aContext);
1397 MOZ_ASSERT(!prevInFlow->GetNextInFlow(), "non null next-in-flow");
1400 void nsContainerFrame::PushChildrenToOverflow(nsIFrame* aFromChild,
1401 nsIFrame* aPrevSibling) {
1402 MOZ_ASSERT(aFromChild, "null pointer");
1403 MOZ_ASSERT(aPrevSibling, "pushing first child");
1404 MOZ_ASSERT(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
1406 // Add the frames to our overflow list (let our next in flow drain
1407 // our overflow list when it is ready)
1408 SetOverflowFrames(mFrames.TakeFramesAfter(aPrevSibling));
1411 bool nsContainerFrame::PushIncompleteChildren(
1412 const FrameHashtable& aPushedItems, const FrameHashtable& aIncompleteItems,
1413 const FrameHashtable& aOverflowIncompleteItems) {
1414 MOZ_ASSERT(IsFlexOrGridContainer(),
1415 "Only Grid / Flex containers can call this!");
1417 if (aPushedItems.IsEmpty() && aIncompleteItems.IsEmpty() &&
1418 aOverflowIncompleteItems.IsEmpty()) {
1419 return false;
1422 // Iterate the children in normal document order and append them (or a NIF)
1423 // to one of the following frame lists according to their status.
1424 nsFrameList pushedList;
1425 nsFrameList incompleteList;
1426 nsFrameList overflowIncompleteList;
1427 auto* fc = PresShell()->FrameConstructor();
1428 for (nsIFrame* child = PrincipalChildList().FirstChild(); child;) {
1429 MOZ_ASSERT((aPushedItems.Contains(child) ? 1 : 0) +
1430 (aIncompleteItems.Contains(child) ? 1 : 0) +
1431 (aOverflowIncompleteItems.Contains(child) ? 1 : 0) <=
1433 "child should only be in one of these sets");
1434 // Save the next-sibling so we can continue the loop if |child| is moved.
1435 nsIFrame* next = child->GetNextSibling();
1436 if (aPushedItems.Contains(child)) {
1437 MOZ_ASSERT(child->GetParent() == this);
1438 StealFrame(child);
1439 pushedList.AppendFrame(nullptr, child);
1440 } else if (aIncompleteItems.Contains(child)) {
1441 nsIFrame* childNIF = child->GetNextInFlow();
1442 if (!childNIF) {
1443 childNIF = fc->CreateContinuingFrame(child, this);
1444 incompleteList.AppendFrame(nullptr, childNIF);
1445 } else {
1446 auto* parent = childNIF->GetParent();
1447 MOZ_ASSERT(parent != this || !mFrames.ContainsFrame(childNIF),
1448 "child's NIF shouldn't be in the same principal list");
1449 // If child's existing NIF is an overflow container, convert it to an
1450 // actual NIF, since now |child| has non-overflow stuff to give it.
1451 // Or, if it's further away then our next-in-flow, then pull it up.
1452 if (childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) ||
1453 (parent != this && parent != GetNextInFlow())) {
1454 parent->StealFrame(childNIF);
1455 childNIF->RemoveStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1456 if (parent == this) {
1457 incompleteList.AppendFrame(nullptr, childNIF);
1458 } else {
1459 // If childNIF already lives on the next fragment, then we
1460 // don't need to reparent it, since we know it's destined to end
1461 // up there anyway. Just move it to its parent's overflow list.
1462 if (parent == GetNextInFlow()) {
1463 nsFrameList toMove(childNIF, childNIF);
1464 parent->MergeSortedOverflow(toMove);
1465 } else {
1466 ReparentFrame(childNIF, parent, this);
1467 incompleteList.AppendFrame(nullptr, childNIF);
1472 } else if (aOverflowIncompleteItems.Contains(child)) {
1473 nsIFrame* childNIF = child->GetNextInFlow();
1474 if (!childNIF) {
1475 childNIF = fc->CreateContinuingFrame(child, this);
1476 childNIF->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1477 overflowIncompleteList.AppendFrame(nullptr, childNIF);
1478 } else {
1479 DebugOnly<nsContainerFrame*> lastParent = this;
1480 auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1481 // If child has any non-overflow-container NIFs, convert them to
1482 // overflow containers, since that's all |child| needs now.
1483 while (childNIF &&
1484 !childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1485 auto* parent = childNIF->GetParent();
1486 parent->StealFrame(childNIF);
1487 childNIF->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1488 if (parent == this) {
1489 overflowIncompleteList.AppendFrame(nullptr, childNIF);
1490 } else {
1491 if (!nif || parent == nif) {
1492 nsFrameList toMove(childNIF, childNIF);
1493 parent->MergeSortedExcessOverflowContainers(toMove);
1494 } else {
1495 ReparentFrame(childNIF, parent, nif);
1496 nsFrameList toMove(childNIF, childNIF);
1497 nif->MergeSortedExcessOverflowContainers(toMove);
1499 // We only need to reparent the first childNIF (or not at all if
1500 // its parent is our NIF).
1501 nif = nullptr;
1503 lastParent = parent;
1504 childNIF = childNIF->GetNextInFlow();
1508 child = next;
1511 // Merge the results into our respective overflow child lists.
1512 if (!pushedList.IsEmpty()) {
1513 MergeSortedOverflow(pushedList);
1515 if (!incompleteList.IsEmpty()) {
1516 MergeSortedOverflow(incompleteList);
1518 if (!overflowIncompleteList.IsEmpty()) {
1519 // If our next-in-flow already has overflow containers list, merge the
1520 // overflowIncompleteList into that list. Otherwise, merge it into our
1521 // excess overflow containers list, to be drained by our next-in-flow.
1522 auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1523 nsFrameList* oc = nif ? nif->GetOverflowContainers() : nullptr;
1524 if (oc) {
1525 ReparentFrames(overflowIncompleteList, this, nif);
1526 MergeSortedFrameLists(*oc, overflowIncompleteList, GetContent());
1527 } else {
1528 MergeSortedExcessOverflowContainers(overflowIncompleteList);
1531 return true;
1534 void nsContainerFrame::NormalizeChildLists() {
1535 MOZ_ASSERT(IsFlexOrGridContainer(),
1536 "Only Flex / Grid containers can call this!");
1538 // Note: the following description uses grid container as an example. Flex
1539 // container is similar.
1541 // First we gather child frames we should include in our reflow/placement,
1542 // i.e. overflowed children from our prev-in-flow, and pushed first-in-flow
1543 // children (that might now fit). It's important to note that these children
1544 // can be in arbitrary order vis-a-vis the current children in our lists.
1545 // E.g. grid items in the document order: A, B, C may be placed in the rows
1546 // 3, 2, 1. Assume each row goes in a separate grid container fragment,
1547 // and we reflow the second fragment. Now if C (in fragment 1) overflows,
1548 // we can't just prepend it to our mFrames like we usually do because that
1549 // would violate the document order invariant that other code depends on.
1550 // Similarly if we pull up child A (from fragment 3) we can't just append
1551 // that for the same reason. Instead, we must sort these children into
1552 // our child lists. (The sorting is trivial given that both lists are
1553 // already fully sorted individually - it's just a merge.)
1555 // The invariants that we maintain are that each grid container child list
1556 // is sorted in the normal document order at all times, but that children
1557 // in different grid container continuations may be in arbitrary order.
1559 const auto didPushItemsBit = IsFlexContainerFrame()
1560 ? NS_STATE_FLEX_DID_PUSH_ITEMS
1561 : NS_STATE_GRID_DID_PUSH_ITEMS;
1562 const auto hasChildNifBit = IsFlexContainerFrame()
1563 ? NS_STATE_FLEX_HAS_CHILD_NIFS
1564 : NS_STATE_GRID_HAS_CHILD_NIFS;
1566 auto* prevInFlow = static_cast<nsContainerFrame*>(GetPrevInFlow());
1567 // Merge overflow frames from our prev-in-flow into our principal child list.
1568 if (prevInFlow) {
1569 AutoFrameListPtr overflow(PresContext(), prevInFlow->StealOverflowFrames());
1570 if (overflow) {
1571 ReparentFrames(*overflow, prevInFlow, this);
1572 MergeSortedFrameLists(mFrames, *overflow, GetContent());
1574 // Move trailing next-in-flows into our overflow list.
1575 nsFrameList continuations;
1576 for (nsIFrame* f = mFrames.FirstChild(); f;) {
1577 nsIFrame* next = f->GetNextSibling();
1578 nsIFrame* pif = f->GetPrevInFlow();
1579 if (pif && pif->GetParent() == this) {
1580 mFrames.RemoveFrame(f);
1581 continuations.AppendFrame(nullptr, f);
1583 f = next;
1585 MergeSortedOverflow(continuations);
1587 // Move prev-in-flow's excess overflow containers list into our own
1588 // overflow containers list. If we already have an excess overflow
1589 // containers list, any child in that list which doesn't have a
1590 // prev-in-flow in this frame is also merged into our overflow container
1591 // list.
1592 nsFrameList* overflowContainers =
1593 DrainExcessOverflowContainersList(MergeSortedFrameListsFor);
1595 // Move trailing OC next-in-flows into our excess overflow containers
1596 // list.
1597 if (overflowContainers) {
1598 nsFrameList moveToEOC;
1599 for (nsIFrame* f = overflowContainers->FirstChild(); f;) {
1600 nsIFrame* next = f->GetNextSibling();
1601 nsIFrame* pif = f->GetPrevInFlow();
1602 if (pif && pif->GetParent() == this) {
1603 overflowContainers->RemoveFrame(f);
1604 moveToEOC.AppendFrame(nullptr, f);
1606 f = next;
1608 if (overflowContainers->IsEmpty()) {
1609 DestroyOverflowContainers();
1611 MergeSortedExcessOverflowContainers(moveToEOC);
1616 // For each item in aItems, pull up its next-in-flow (if any), and reparent it
1617 // to our next-in-flow, unless its parent is already ourselves or our
1618 // next-in-flow (to avoid leaving a hole there).
1619 auto PullItemsNextInFlow = [this](const nsFrameList& aItems) {
1620 auto* firstNIF = static_cast<nsContainerFrame*>(GetNextInFlow());
1621 if (!firstNIF) {
1622 return;
1624 nsFrameList childNIFs;
1625 nsFrameList childOCNIFs;
1626 for (auto* child : aItems) {
1627 if (auto* childNIF = child->GetNextInFlow()) {
1628 if (auto* parent = childNIF->GetParent();
1629 parent != this && parent != firstNIF) {
1630 parent->StealFrame(childNIF);
1631 ReparentFrame(childNIF, parent, firstNIF);
1632 if (childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1633 childOCNIFs.AppendFrame(nullptr, childNIF);
1634 } else {
1635 childNIFs.AppendFrame(nullptr, childNIF);
1640 // Merge aItems' NIFs into our NIF's respective overflow child lists.
1641 firstNIF->MergeSortedOverflow(childNIFs);
1642 firstNIF->MergeSortedExcessOverflowContainers(childOCNIFs);
1645 // Merge our own overflow frames into our principal child list,
1646 // except those that are a next-in-flow for one of our items.
1647 DebugOnly<bool> foundOwnPushedChild = false;
1649 nsFrameList* ourOverflow = GetOverflowFrames();
1650 if (ourOverflow) {
1651 nsFrameList items;
1652 for (nsIFrame* f = ourOverflow->FirstChild(); f;) {
1653 nsIFrame* next = f->GetNextSibling();
1654 nsIFrame* pif = f->GetPrevInFlow();
1655 if (!pif || pif->GetParent() != this) {
1656 MOZ_ASSERT(f->GetParent() == this);
1657 ourOverflow->RemoveFrame(f);
1658 items.AppendFrame(nullptr, f);
1659 if (!pif) {
1660 foundOwnPushedChild = true;
1663 f = next;
1666 if (ourOverflow->IsEmpty()) {
1667 DestroyOverflowList();
1668 ourOverflow = nullptr;
1670 if (items.NotEmpty()) {
1671 PullItemsNextInFlow(items);
1673 MergeSortedFrameLists(mFrames, items, GetContent());
1677 // Push any child next-in-flows in our principal list to OverflowList.
1678 if (HasAnyStateBits(hasChildNifBit)) {
1679 nsFrameList framesToPush;
1680 nsIFrame* firstChild = mFrames.FirstChild();
1681 // Note that we potentially modify our mFrames list as we go.
1682 for (auto* child = firstChild; child; child = child->GetNextSibling()) {
1683 if (auto* childNIF = child->GetNextInFlow()) {
1684 if (childNIF->GetParent() == this) {
1685 for (auto* c = child->GetNextSibling(); c; c = c->GetNextSibling()) {
1686 if (c == childNIF) {
1687 // child's next-in-flow is in our principal child list, push it.
1688 mFrames.RemoveFrame(childNIF);
1689 framesToPush.AppendFrame(nullptr, childNIF);
1690 break;
1696 if (!framesToPush.IsEmpty()) {
1697 MergeSortedOverflow(framesToPush);
1699 RemoveStateBits(hasChildNifBit);
1702 // Pull up any first-in-flow children we might have pushed.
1703 if (HasAnyStateBits(didPushItemsBit)) {
1704 RemoveStateBits(didPushItemsBit);
1705 nsFrameList items;
1706 auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1707 DebugOnly<bool> nifNeedPushedItem = false;
1708 while (nif) {
1709 nsFrameList nifItems;
1710 for (nsIFrame* nifChild = nif->PrincipalChildList().FirstChild();
1711 nifChild;) {
1712 nsIFrame* next = nifChild->GetNextSibling();
1713 if (!nifChild->GetPrevInFlow()) {
1714 nif->StealFrame(nifChild);
1715 ReparentFrame(nifChild, nif, this);
1716 nifItems.AppendFrame(nullptr, nifChild);
1717 nifNeedPushedItem = false;
1719 nifChild = next;
1721 MergeSortedFrameLists(items, nifItems, GetContent());
1723 if (!nif->HasAnyStateBits(didPushItemsBit)) {
1724 MOZ_ASSERT(!nifNeedPushedItem || mDidPushItemsBitMayLie,
1725 "The state bit stored in didPushItemsBit lied!");
1726 break;
1728 nifNeedPushedItem = true;
1730 for (nsIFrame* nifChild =
1731 nif->GetChildList(FrameChildListID::Overflow).FirstChild();
1732 nifChild;) {
1733 nsIFrame* next = nifChild->GetNextSibling();
1734 if (!nifChild->GetPrevInFlow()) {
1735 nif->StealFrame(nifChild);
1736 ReparentFrame(nifChild, nif, this);
1737 nifItems.AppendFrame(nullptr, nifChild);
1738 nifNeedPushedItem = false;
1740 nifChild = next;
1742 MergeSortedFrameLists(items, nifItems, GetContent());
1744 nif->RemoveStateBits(didPushItemsBit);
1745 nif = static_cast<nsContainerFrame*>(nif->GetNextInFlow());
1746 MOZ_ASSERT(nif || !nifNeedPushedItem || mDidPushItemsBitMayLie,
1747 "The state bit stored in didPushItemsBit lied!");
1750 if (!items.IsEmpty()) {
1751 PullItemsNextInFlow(items);
1754 MOZ_ASSERT(
1755 foundOwnPushedChild || !items.IsEmpty() || mDidPushItemsBitMayLie,
1756 "The state bit stored in didPushItemsBit lied!");
1757 MergeSortedFrameLists(mFrames, items, GetContent());
1761 void nsContainerFrame::NoteNewChildren(ChildListID aListID,
1762 const nsFrameList& aFrameList) {
1763 MOZ_ASSERT(aListID == FrameChildListID::Principal, "unexpected child list");
1764 MOZ_ASSERT(IsFlexOrGridContainer(),
1765 "Only Flex / Grid containers can call this!");
1767 mozilla::PresShell* presShell = PresShell();
1768 const auto didPushItemsBit = IsFlexContainerFrame()
1769 ? NS_STATE_FLEX_DID_PUSH_ITEMS
1770 : NS_STATE_GRID_DID_PUSH_ITEMS;
1771 for (auto* pif = GetPrevInFlow(); pif; pif = pif->GetPrevInFlow()) {
1772 pif->AddStateBits(didPushItemsBit);
1773 presShell->FrameNeedsReflow(pif, IntrinsicDirty::FrameAndAncestors,
1774 NS_FRAME_IS_DIRTY);
1778 bool nsContainerFrame::MoveOverflowToChildList() {
1779 bool result = false;
1781 // Check for an overflow list with our prev-in-flow
1782 nsContainerFrame* prevInFlow = (nsContainerFrame*)GetPrevInFlow();
1783 if (nullptr != prevInFlow) {
1784 AutoFrameListPtr prevOverflowFrames(PresContext(),
1785 prevInFlow->StealOverflowFrames());
1786 if (prevOverflowFrames) {
1787 // Tables are special; they can have repeated header/footer
1788 // frames on mFrames at this point.
1789 NS_ASSERTION(mFrames.IsEmpty() || IsTableFrame(), "bad overflow list");
1790 // When pushing and pulling frames we need to check for whether any
1791 // views need to be reparented.
1792 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
1793 this);
1794 mFrames.AppendFrames(this, std::move(*prevOverflowFrames));
1795 result = true;
1799 // It's also possible that we have an overflow list for ourselves.
1800 return DrainSelfOverflowList() || result;
1803 void nsContainerFrame::MergeSortedOverflow(nsFrameList& aList) {
1804 if (aList.IsEmpty()) {
1805 return;
1807 MOZ_ASSERT(
1808 !aList.FirstChild()->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1809 "this is the wrong list to put this child frame");
1810 MOZ_ASSERT(aList.FirstChild()->GetParent() == this);
1811 nsFrameList* overflow = GetOverflowFrames();
1812 if (overflow) {
1813 MergeSortedFrameLists(*overflow, aList, GetContent());
1814 } else {
1815 SetOverflowFrames(std::move(aList));
1819 void nsContainerFrame::MergeSortedExcessOverflowContainers(nsFrameList& aList) {
1820 if (aList.IsEmpty()) {
1821 return;
1823 MOZ_ASSERT(
1824 aList.FirstChild()->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1825 "this is the wrong list to put this child frame");
1826 MOZ_ASSERT(aList.FirstChild()->GetParent() == this);
1827 if (nsFrameList* eoc = GetExcessOverflowContainers()) {
1828 MergeSortedFrameLists(*eoc, aList, GetContent());
1829 } else {
1830 SetExcessOverflowContainers(std::move(aList));
1834 nsIFrame* nsContainerFrame::GetFirstNonAnonBoxInSubtree(nsIFrame* aFrame) {
1835 while (aFrame) {
1836 // If aFrame isn't an anonymous container, or it's text or such, then it'll
1837 // do.
1838 if (!aFrame->Style()->IsAnonBox() ||
1839 nsCSSAnonBoxes::IsNonElement(aFrame->Style()->GetPseudoType())) {
1840 break;
1843 // Otherwise, descend to its first child and repeat.
1845 // SPECIAL CASE: if we're dealing with an anonymous table, then it might
1846 // be wrapping something non-anonymous in its caption or col-group lists
1847 // (instead of its principal child list), so we have to look there.
1848 // (Note: For anonymous tables that have a non-anon cell *and* a non-anon
1849 // column, we'll always return the column. This is fine; we're really just
1850 // looking for a handle to *anything* with a meaningful content node inside
1851 // the table, for use in DOM comparisons to things outside of the table.)
1852 if (MOZ_UNLIKELY(aFrame->IsTableWrapperFrame())) {
1853 nsIFrame* captionDescendant = GetFirstNonAnonBoxInSubtree(
1854 aFrame->GetChildList(FrameChildListID::Caption).FirstChild());
1855 if (captionDescendant) {
1856 return captionDescendant;
1858 } else if (MOZ_UNLIKELY(aFrame->IsTableFrame())) {
1859 nsIFrame* colgroupDescendant = GetFirstNonAnonBoxInSubtree(
1860 aFrame->GetChildList(FrameChildListID::ColGroup).FirstChild());
1861 if (colgroupDescendant) {
1862 return colgroupDescendant;
1866 // USUAL CASE: Descend to the first child in principal list.
1867 aFrame = aFrame->PrincipalChildList().FirstChild();
1869 return aFrame;
1873 * Is aFrame1 a prev-continuation of aFrame2?
1875 static bool IsPrevContinuationOf(nsIFrame* aFrame1, nsIFrame* aFrame2) {
1876 nsIFrame* prev = aFrame2;
1877 while ((prev = prev->GetPrevContinuation())) {
1878 if (prev == aFrame1) {
1879 return true;
1882 return false;
1885 void nsContainerFrame::MergeSortedFrameLists(nsFrameList& aDest,
1886 nsFrameList& aSrc,
1887 nsIContent* aCommonAncestor) {
1888 // Returns a frame whose DOM node can be used for the purpose of ordering
1889 // aFrame among its sibling frames by DOM position. If aFrame is
1890 // non-anonymous, this just returns aFrame itself. Otherwise, this returns the
1891 // first non-anonymous descendant in aFrame's continuation chain.
1892 auto FrameForDOMPositionComparison = [](nsIFrame* aFrame) {
1893 if (!aFrame->Style()->IsAnonBox()) {
1894 // The usual case.
1895 return aFrame;
1898 // Walk the continuation chain from the start, and return the first
1899 // non-anonymous descendant that we find.
1900 for (nsIFrame* f = aFrame->FirstContinuation(); f;
1901 f = f->GetNextContinuation()) {
1902 if (nsIFrame* nonAnonBox = GetFirstNonAnonBoxInSubtree(f)) {
1903 return nonAnonBox;
1907 MOZ_ASSERT_UNREACHABLE(
1908 "Why is there no non-anonymous descendants in the continuation chain?");
1909 return aFrame;
1912 nsIFrame* dest = aDest.FirstChild();
1913 for (nsIFrame* src = aSrc.FirstChild(); src;) {
1914 if (!dest) {
1915 aDest.AppendFrames(nullptr, std::move(aSrc));
1916 break;
1918 nsIContent* srcContent = FrameForDOMPositionComparison(src)->GetContent();
1919 nsIContent* destContent = FrameForDOMPositionComparison(dest)->GetContent();
1920 int32_t result = nsContentUtils::CompareTreePosition<TreeKind::Flat>(
1921 srcContent, destContent, aCommonAncestor);
1922 if (MOZ_UNLIKELY(result == 0)) {
1923 // NOTE: we get here when comparing ::before/::after for the same element.
1924 if (MOZ_UNLIKELY(srcContent->IsGeneratedContentContainerForBefore())) {
1925 if (MOZ_LIKELY(!destContent->IsGeneratedContentContainerForBefore()) ||
1926 ::IsPrevContinuationOf(src, dest)) {
1927 result = -1;
1929 } else if (MOZ_UNLIKELY(
1930 srcContent->IsGeneratedContentContainerForAfter())) {
1931 if (MOZ_UNLIKELY(destContent->IsGeneratedContentContainerForAfter()) &&
1932 ::IsPrevContinuationOf(src, dest)) {
1933 result = -1;
1935 } else if (::IsPrevContinuationOf(src, dest)) {
1936 result = -1;
1939 if (result < 0) {
1940 // src should come before dest
1941 nsIFrame* next = src->GetNextSibling();
1942 aSrc.RemoveFrame(src);
1943 aDest.InsertFrame(nullptr, dest->GetPrevSibling(), src);
1944 src = next;
1945 } else {
1946 dest = dest->GetNextSibling();
1949 MOZ_ASSERT(aSrc.IsEmpty());
1952 bool nsContainerFrame::MoveInlineOverflowToChildList(nsIFrame* aLineContainer) {
1953 MOZ_ASSERT(aLineContainer,
1954 "Must have line container for moving inline overflows");
1956 bool result = false;
1958 // Check for an overflow list with our prev-in-flow
1959 if (auto prevInFlow = static_cast<nsContainerFrame*>(GetPrevInFlow())) {
1960 AutoFrameListPtr prevOverflowFrames(PresContext(),
1961 prevInFlow->StealOverflowFrames());
1962 if (prevOverflowFrames) {
1963 // We may need to reparent floats from prev-in-flow to our line
1964 // container if the container has prev continuation.
1965 if (aLineContainer->GetPrevContinuation()) {
1966 ReparentFloatsForInlineChild(aLineContainer,
1967 prevOverflowFrames->FirstChild(), true);
1969 // When pushing and pulling frames we need to check for whether
1970 // any views need to be reparented.
1971 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
1972 this);
1973 // Prepend overflow frames to the list.
1974 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
1975 result = true;
1979 // It's also possible that we have overflow list for ourselves.
1980 return DrainSelfOverflowList() || result;
1983 bool nsContainerFrame::DrainSelfOverflowList() {
1984 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
1985 if (overflowFrames) {
1986 mFrames.AppendFrames(nullptr, std::move(*overflowFrames));
1987 return true;
1989 return false;
1992 bool nsContainerFrame::DrainAndMergeSelfOverflowList() {
1993 MOZ_ASSERT(IsFlexOrGridContainer(),
1994 "Only Flex / Grid containers can call this!");
1996 // Unlike nsContainerFrame::DrainSelfOverflowList, flex or grid containers
1997 // need to merge these lists so that the resulting mFrames is in document
1998 // content order.
1999 // NOTE: nsContainerFrame::AppendFrames/InsertFrames calls this method and
2000 // there are also direct calls from the fctor (FindAppendPrevSibling).
2001 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
2002 if (overflowFrames) {
2003 MergeSortedFrameLists(mFrames, *overflowFrames, GetContent());
2004 // We set a frame bit to push them again in Reflow() to avoid creating
2005 // multiple flex / grid items per flex / grid container fragment for the
2006 // same content.
2007 AddStateBits(IsFlexContainerFrame() ? NS_STATE_FLEX_HAS_CHILD_NIFS
2008 : NS_STATE_GRID_HAS_CHILD_NIFS);
2009 return true;
2011 return false;
2014 nsFrameList* nsContainerFrame::DrainExcessOverflowContainersList(
2015 ChildFrameMerger aMergeFunc) {
2016 nsFrameList* overflowContainers = GetOverflowContainers();
2018 // Drain excess overflow containers from our prev-in-flow.
2019 if (auto* prev = static_cast<nsContainerFrame*>(GetPrevInFlow())) {
2020 AutoFrameListPtr excessFrames(PresContext(),
2021 prev->StealExcessOverflowContainers());
2022 if (excessFrames) {
2023 excessFrames->ApplySetParent(this);
2024 nsContainerFrame::ReparentFrameViewList(*excessFrames, prev, this);
2025 if (overflowContainers) {
2026 // The default merge function is AppendFrames, so we use excessFrames as
2027 // the destination and then assign the result to overflowContainers.
2028 aMergeFunc(*excessFrames, *overflowContainers, this);
2029 *overflowContainers = std::move(*excessFrames);
2030 } else {
2031 overflowContainers = SetOverflowContainers(std::move(*excessFrames));
2036 // Our own excess overflow containers from a previous reflow can still be
2037 // present if our next-in-flow hasn't been reflown yet. Move any children
2038 // from it that don't have a continuation in this frame to the
2039 // OverflowContainers list.
2040 AutoFrameListPtr selfExcessOCFrames(PresContext(),
2041 StealExcessOverflowContainers());
2042 if (selfExcessOCFrames) {
2043 nsFrameList toMove;
2044 auto child = selfExcessOCFrames->FirstChild();
2045 while (child) {
2046 auto next = child->GetNextSibling();
2047 MOZ_ASSERT(child->GetPrevInFlow(),
2048 "ExcessOverflowContainers frames must be continuations");
2049 if (child->GetPrevInFlow()->GetParent() != this) {
2050 selfExcessOCFrames->RemoveFrame(child);
2051 toMove.AppendFrame(nullptr, child);
2053 child = next;
2056 // If there's any remaining excess overflow containers, put them back.
2057 if (selfExcessOCFrames->NotEmpty()) {
2058 SetExcessOverflowContainers(std::move(*selfExcessOCFrames));
2061 if (toMove.NotEmpty()) {
2062 if (overflowContainers) {
2063 aMergeFunc(*overflowContainers, toMove, this);
2064 } else {
2065 overflowContainers = SetOverflowContainers(std::move(toMove));
2070 return overflowContainers;
2073 nsIFrame* nsContainerFrame::GetNextInFlowChild(
2074 ContinuationTraversingState& aState, bool* aIsInOverflow) {
2075 nsContainerFrame*& nextInFlow = aState.mNextInFlow;
2076 while (nextInFlow) {
2077 // See if there is any frame in the container
2078 nsIFrame* frame = nextInFlow->mFrames.FirstChild();
2079 if (frame) {
2080 if (aIsInOverflow) {
2081 *aIsInOverflow = false;
2083 return frame;
2085 // No frames in the principal list, try its overflow list
2086 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
2087 if (overflowFrames) {
2088 if (aIsInOverflow) {
2089 *aIsInOverflow = true;
2091 return overflowFrames->FirstChild();
2093 nextInFlow = static_cast<nsContainerFrame*>(nextInFlow->GetNextInFlow());
2095 return nullptr;
2098 nsIFrame* nsContainerFrame::PullNextInFlowChild(
2099 ContinuationTraversingState& aState) {
2100 bool isInOverflow;
2101 nsIFrame* frame = GetNextInFlowChild(aState, &isInOverflow);
2102 if (frame) {
2103 nsContainerFrame* nextInFlow = aState.mNextInFlow;
2104 if (isInOverflow) {
2105 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
2106 overflowFrames->RemoveFirstChild();
2107 if (overflowFrames->IsEmpty()) {
2108 nextInFlow->DestroyOverflowList();
2110 } else {
2111 nextInFlow->mFrames.RemoveFirstChild();
2114 // Move the frame to the principal frame list of this container
2115 mFrames.AppendFrame(this, frame);
2116 // AppendFrame has reparented the frame, we need
2117 // to reparent the frame view then.
2118 nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
2120 return frame;
2123 /* static */
2124 void nsContainerFrame::ReparentFloatsForInlineChild(nsIFrame* aOurLineContainer,
2125 nsIFrame* aFrame,
2126 bool aReparentSiblings) {
2127 // XXXbz this would be better if it took a nsFrameList or a frame
2128 // list slice....
2129 NS_ASSERTION(aOurLineContainer->GetNextContinuation() ||
2130 aOurLineContainer->GetPrevContinuation(),
2131 "Don't call this when we have no continuation, it's a waste");
2132 if (!aFrame) {
2133 NS_ASSERTION(aReparentSiblings, "Why did we get called?");
2134 return;
2137 nsBlockFrame* frameBlock = nsLayoutUtils::GetFloatContainingBlock(aFrame);
2138 if (!frameBlock || frameBlock == aOurLineContainer) {
2139 return;
2142 nsBlockFrame* ourBlock = do_QueryFrame(aOurLineContainer);
2143 NS_ASSERTION(ourBlock, "Not a block, but broke vertically?");
2145 while (true) {
2146 ourBlock->ReparentFloats(aFrame, frameBlock, false);
2148 if (!aReparentSiblings) return;
2149 nsIFrame* next = aFrame->GetNextSibling();
2150 if (!next) return;
2151 if (next->GetParent() == aFrame->GetParent()) {
2152 aFrame = next;
2153 continue;
2155 // This is paranoid and will hardly ever get hit ... but we can't actually
2156 // trust that the frames in the sibling chain all have the same parent,
2157 // because lazy reparenting may be going on. If we find a different
2158 // parent we need to redo our analysis.
2159 ReparentFloatsForInlineChild(aOurLineContainer, next, aReparentSiblings);
2160 return;
2164 bool nsContainerFrame::ResolvedOrientationIsVertical() {
2165 StyleOrient orient = StyleDisplay()->mOrient;
2166 switch (orient) {
2167 case StyleOrient::Horizontal:
2168 return false;
2169 case StyleOrient::Vertical:
2170 return true;
2171 case StyleOrient::Inline:
2172 return GetWritingMode().IsVertical();
2173 case StyleOrient::Block:
2174 return !GetWritingMode().IsVertical();
2176 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
2177 return false;
2180 LogicalSize nsContainerFrame::ComputeSizeWithIntrinsicDimensions(
2181 gfxContext* aRenderingContext, WritingMode aWM,
2182 const IntrinsicSize& aIntrinsicSize, const AspectRatio& aAspectRatio,
2183 const LogicalSize& aCBSize, const LogicalSize& aMargin,
2184 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
2185 ComputeSizeFlags aFlags) {
2186 const nsStylePosition* stylePos = StylePosition();
2187 const auto& styleISize = aSizeOverrides.mStyleISize
2188 ? *aSizeOverrides.mStyleISize
2189 : stylePos->ISize(aWM);
2190 const auto& styleBSize = aSizeOverrides.mStyleBSize
2191 ? *aSizeOverrides.mStyleBSize
2192 : stylePos->BSize(aWM);
2193 const auto& aspectRatio =
2194 aSizeOverrides.mAspectRatio ? *aSizeOverrides.mAspectRatio : aAspectRatio;
2196 auto* parentFrame = GetParent();
2197 const bool isGridItem = IsGridItem();
2198 const bool isFlexItem =
2199 IsFlexItem() && !parentFrame->HasAnyStateBits(
2200 NS_STATE_FLEX_IS_EMULATING_LEGACY_WEBKIT_BOX);
2201 // This variable only gets meaningfully set if isFlexItem is true. It
2202 // indicates which axis (in this frame's own WM) corresponds to its
2203 // flex container's main axis.
2204 LogicalAxis flexMainAxis = eLogicalAxisBlock;
2205 if (isFlexItem && nsFlexContainerFrame::IsItemInlineAxisMainAxis(this)) {
2206 flexMainAxis = eLogicalAxisInline;
2209 // Handle intrinsic sizes and their interaction with
2210 // {min-,max-,}{width,height} according to the rules in
2211 // https://www.w3.org/TR/CSS22/visudet.html#min-max-widths and
2212 // https://drafts.csswg.org/css-sizing-3/#intrinsic-sizes
2214 // Note: throughout the following section of the function, I avoid
2215 // a * (b / c) because of its reduced accuracy relative to a * b / c
2216 // or (a * b) / c (which are equivalent).
2218 const bool isAutoOrMaxContentISize =
2219 styleISize.IsAuto() || styleISize.IsMaxContent();
2220 const bool isAutoBSize =
2221 nsLayoutUtils::IsAutoBSize(styleBSize, aCBSize.BSize(aWM));
2223 const auto boxSizingAdjust = stylePos->mBoxSizing == StyleBoxSizing::Border
2224 ? aBorderPadding
2225 : LogicalSize(aWM);
2226 const nscoord boxSizingToMarginEdgeISize = aMargin.ISize(aWM) +
2227 aBorderPadding.ISize(aWM) -
2228 boxSizingAdjust.ISize(aWM);
2230 nscoord iSize, minISize, maxISize, bSize, minBSize, maxBSize;
2231 enum class Stretch {
2232 // stretch to fill the CB (preserving intrinsic ratio) in the relevant axis
2233 StretchPreservingRatio,
2234 // stretch to fill the CB in the relevant axis
2235 Stretch,
2236 // no stretching in the relevant axis
2237 NoStretch,
2239 // just to avoid having to type these out everywhere:
2240 const auto eStretchPreservingRatio = Stretch::StretchPreservingRatio;
2241 const auto eStretch = Stretch::Stretch;
2242 const auto eNoStretch = Stretch::NoStretch;
2244 Stretch stretchI = eNoStretch; // stretch behavior in the inline axis
2245 Stretch stretchB = eNoStretch; // stretch behavior in the block axis
2247 const bool isOrthogonal = aWM.IsOrthogonalTo(parentFrame->GetWritingMode());
2248 const bool isVertical = aWM.IsVertical();
2249 const LogicalSize fallbackIntrinsicSize(aWM, kFallbackIntrinsicSize);
2250 const auto& isizeCoord =
2251 isVertical ? aIntrinsicSize.height : aIntrinsicSize.width;
2252 const bool hasIntrinsicISize = isizeCoord.isSome();
2253 nscoord intrinsicISize = std::max(0, isizeCoord.valueOr(0));
2255 const auto& bsizeCoord =
2256 isVertical ? aIntrinsicSize.width : aIntrinsicSize.height;
2257 const bool hasIntrinsicBSize = bsizeCoord.isSome();
2258 nscoord intrinsicBSize = std::max(0, bsizeCoord.valueOr(0));
2260 if (!isAutoOrMaxContentISize) {
2261 iSize = ComputeISizeValue(aRenderingContext, aWM, aCBSize, boxSizingAdjust,
2262 boxSizingToMarginEdgeISize, styleISize,
2263 aSizeOverrides, aFlags)
2264 .mISize;
2265 } else if (MOZ_UNLIKELY(isGridItem) &&
2266 !parentFrame->IsMasonry(isOrthogonal ? eLogicalAxisBlock
2267 : eLogicalAxisInline)) {
2268 MOZ_ASSERT(!IsTrueOverflowContainer());
2269 // 'auto' inline-size for grid-level box - apply 'stretch' as needed:
2270 auto cbSize = aCBSize.ISize(aWM);
2271 if (cbSize != NS_UNCONSTRAINEDSIZE) {
2272 if (!StyleMargin()->HasInlineAxisAuto(aWM)) {
2273 auto inlineAxisAlignment =
2274 isOrthogonal ? stylePos->UsedAlignSelf(GetParent()->Style())._0
2275 : stylePos->UsedJustifySelf(GetParent()->Style())._0;
2276 if (inlineAxisAlignment == StyleAlignFlags::STRETCH) {
2277 stretchI = eStretch;
2280 if (stretchI != eNoStretch ||
2281 aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize)) {
2282 iSize = std::max(nscoord(0), cbSize - aBorderPadding.ISize(aWM) -
2283 aMargin.ISize(aWM));
2285 } else {
2286 // Reset this flag to avoid applying the clamping below.
2287 aFlags -= ComputeSizeFlag::IClampMarginBoxMinSize;
2291 const auto& maxISizeCoord = stylePos->MaxISize(aWM);
2293 if (!maxISizeCoord.IsNone() &&
2294 !(isFlexItem && flexMainAxis == eLogicalAxisInline)) {
2295 maxISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
2296 boxSizingAdjust, boxSizingToMarginEdgeISize,
2297 maxISizeCoord, aSizeOverrides, aFlags)
2298 .mISize;
2299 } else {
2300 maxISize = nscoord_MAX;
2303 // NOTE: Flex items ignore their min & max sizing properties in their
2304 // flex container's main-axis. (Those properties get applied later in
2305 // the flexbox algorithm.)
2307 const auto& minISizeCoord = stylePos->MinISize(aWM);
2309 if (!minISizeCoord.IsAuto() &&
2310 !(isFlexItem && flexMainAxis == eLogicalAxisInline)) {
2311 minISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
2312 boxSizingAdjust, boxSizingToMarginEdgeISize,
2313 minISizeCoord, aSizeOverrides, aFlags)
2314 .mISize;
2315 } else {
2316 // Treat "min-width: auto" as 0.
2317 // NOTE: Technically, "auto" is supposed to behave like "min-content" on
2318 // flex items. However, we don't need to worry about that here, because
2319 // flex items' min-sizes are intentionally ignored until the flex
2320 // container explicitly considers them during space distribution.
2321 minISize = 0;
2324 if (!isAutoBSize) {
2325 bSize = nsLayoutUtils::ComputeBSizeValue(aCBSize.BSize(aWM),
2326 boxSizingAdjust.BSize(aWM),
2327 styleBSize.AsLengthPercentage());
2328 } else if (MOZ_UNLIKELY(isGridItem) &&
2329 !parentFrame->IsMasonry(isOrthogonal ? eLogicalAxisInline
2330 : eLogicalAxisBlock)) {
2331 MOZ_ASSERT(!IsTrueOverflowContainer());
2332 // 'auto' block-size for grid-level box - apply 'stretch' as needed:
2333 auto cbSize = aCBSize.BSize(aWM);
2334 if (cbSize != NS_UNCONSTRAINEDSIZE) {
2335 if (!StyleMargin()->HasBlockAxisAuto(aWM)) {
2336 auto blockAxisAlignment =
2337 !isOrthogonal ? stylePos->UsedAlignSelf(GetParent()->Style())._0
2338 : stylePos->UsedJustifySelf(GetParent()->Style())._0;
2339 if (blockAxisAlignment == StyleAlignFlags::STRETCH) {
2340 stretchB = eStretch;
2343 if (stretchB != eNoStretch ||
2344 aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize)) {
2345 bSize = std::max(nscoord(0), cbSize - aBorderPadding.BSize(aWM) -
2346 aMargin.BSize(aWM));
2348 } else {
2349 // Reset this flag to avoid applying the clamping below.
2350 aFlags -= ComputeSizeFlag::BClampMarginBoxMinSize;
2354 const auto& maxBSizeCoord = stylePos->MaxBSize(aWM);
2356 if (!nsLayoutUtils::IsAutoBSize(maxBSizeCoord, aCBSize.BSize(aWM)) &&
2357 !(isFlexItem && flexMainAxis == eLogicalAxisBlock)) {
2358 maxBSize = nsLayoutUtils::ComputeBSizeValue(
2359 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
2360 maxBSizeCoord.AsLengthPercentage());
2361 } else {
2362 maxBSize = nscoord_MAX;
2365 const auto& minBSizeCoord = stylePos->MinBSize(aWM);
2367 if (!nsLayoutUtils::IsAutoBSize(minBSizeCoord, aCBSize.BSize(aWM)) &&
2368 !(isFlexItem && flexMainAxis == eLogicalAxisBlock)) {
2369 minBSize = nsLayoutUtils::ComputeBSizeValue(
2370 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
2371 minBSizeCoord.AsLengthPercentage());
2372 } else {
2373 minBSize = 0;
2376 NS_ASSERTION(aCBSize.ISize(aWM) != NS_UNCONSTRAINEDSIZE,
2377 "Our containing block must not have unconstrained inline-size!");
2379 // Now calculate the used values for iSize and bSize:
2380 if (isAutoOrMaxContentISize) {
2381 if (isAutoBSize) {
2382 // 'auto' iSize, 'auto' bSize
2384 // Get tentative values - CSS 2.1 sections 10.3.2 and 10.6.2:
2386 nscoord tentISize, tentBSize;
2388 if (hasIntrinsicISize) {
2389 tentISize = intrinsicISize;
2390 } else if (hasIntrinsicBSize && aspectRatio) {
2391 tentISize = aspectRatio.ComputeRatioDependentSize(
2392 LogicalAxis::eLogicalAxisInline, aWM, intrinsicBSize,
2393 boxSizingAdjust);
2394 } else if (aspectRatio) {
2395 tentISize =
2396 aCBSize.ISize(aWM) - boxSizingToMarginEdgeISize; // XXX scrollbar?
2397 if (tentISize < 0) {
2398 tentISize = 0;
2400 } else {
2401 tentISize = fallbackIntrinsicSize.ISize(aWM);
2404 // If we need to clamp the inline size to fit the CB, we use the 'stretch'
2405 // or 'normal' codepath. We use the ratio-preserving 'normal' codepath
2406 // unless we have 'stretch' in the other axis.
2407 if (aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize) &&
2408 stretchI != eStretch && tentISize > iSize) {
2409 stretchI = (stretchB == eStretch ? eStretch : eStretchPreservingRatio);
2412 if (hasIntrinsicBSize) {
2413 tentBSize = intrinsicBSize;
2414 } else if (aspectRatio) {
2415 tentBSize = aspectRatio.ComputeRatioDependentSize(
2416 LogicalAxis::eLogicalAxisBlock, aWM, tentISize, boxSizingAdjust);
2417 } else {
2418 tentBSize = fallbackIntrinsicSize.BSize(aWM);
2421 // (ditto the comment about clamping the inline size above)
2422 if (aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize) &&
2423 stretchB != eStretch && tentBSize > bSize) {
2424 stretchB = (stretchI == eStretch ? eStretch : eStretchPreservingRatio);
2427 if (stretchI == eStretch) {
2428 tentISize = iSize; // * / 'stretch'
2429 if (stretchB == eStretch) {
2430 tentBSize = bSize; // 'stretch' / 'stretch'
2431 } else if (stretchB == eStretchPreservingRatio && aspectRatio) {
2432 // 'normal' / 'stretch'
2433 tentBSize = aspectRatio.ComputeRatioDependentSize(
2434 LogicalAxis::eLogicalAxisBlock, aWM, iSize, boxSizingAdjust);
2436 } else if (stretchB == eStretch) {
2437 tentBSize = bSize; // 'stretch' / * (except 'stretch')
2438 if (stretchI == eStretchPreservingRatio && aspectRatio) {
2439 // 'stretch' / 'normal'
2440 tentISize = aspectRatio.ComputeRatioDependentSize(
2441 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2443 } else if (stretchI == eStretchPreservingRatio && aspectRatio) {
2444 tentISize = iSize; // * (except 'stretch') / 'normal'
2445 tentBSize = aspectRatio.ComputeRatioDependentSize(
2446 LogicalAxis::eLogicalAxisBlock, aWM, iSize, boxSizingAdjust);
2447 if (stretchB == eStretchPreservingRatio && tentBSize > bSize) {
2448 // Stretch within the CB size with preserved intrinsic ratio.
2449 tentBSize = bSize; // 'normal' / 'normal'
2450 tentISize = aspectRatio.ComputeRatioDependentSize(
2451 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2453 } else if (stretchB == eStretchPreservingRatio && aspectRatio) {
2454 tentBSize = bSize; // 'normal' / * (except 'normal' and 'stretch')
2455 tentISize = aspectRatio.ComputeRatioDependentSize(
2456 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2459 // ComputeAutoSizeWithIntrinsicDimensions preserves the ratio when
2460 // applying the min/max-size. We don't want that when we have 'stretch'
2461 // in either axis because tentISize/tentBSize is likely not according to
2462 // ratio now.
2463 if (aspectRatio && stretchI != eStretch && stretchB != eStretch) {
2464 nsSize autoSize = nsLayoutUtils::ComputeAutoSizeWithIntrinsicDimensions(
2465 minISize, minBSize, maxISize, maxBSize, tentISize, tentBSize);
2466 // The nsSize that ComputeAutoSizeWithIntrinsicDimensions returns will
2467 // actually contain logical values if the parameters passed to it were
2468 // logical coordinates, so we do NOT perform a physical-to-logical
2469 // conversion here, but just assign the fields directly to our result.
2470 iSize = autoSize.width;
2471 bSize = autoSize.height;
2472 } else {
2473 // Not honoring an intrinsic ratio: clamp the dimensions independently.
2474 iSize = NS_CSS_MINMAX(tentISize, minISize, maxISize);
2475 bSize = NS_CSS_MINMAX(tentBSize, minBSize, maxBSize);
2477 } else {
2478 // 'auto' iSize, non-'auto' bSize
2479 bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2480 if (stretchI != eStretch) {
2481 if (aspectRatio) {
2482 iSize = aspectRatio.ComputeRatioDependentSize(
2483 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2484 } else if (hasIntrinsicISize) {
2485 if (!(aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize) &&
2486 intrinsicISize > iSize)) {
2487 iSize = intrinsicISize;
2488 } // else - leave iSize as is to fill the CB
2489 } else {
2490 iSize = fallbackIntrinsicSize.ISize(aWM);
2492 } // else - leave iSize as is to fill the CB
2493 iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2495 } else {
2496 if (isAutoBSize) {
2497 // non-'auto' iSize, 'auto' bSize
2498 iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2499 if (stretchB != eStretch) {
2500 if (aspectRatio) {
2501 bSize = aspectRatio.ComputeRatioDependentSize(
2502 LogicalAxis::eLogicalAxisBlock, aWM, iSize, boxSizingAdjust);
2503 } else if (hasIntrinsicBSize) {
2504 if (!(aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize) &&
2505 intrinsicBSize > bSize)) {
2506 bSize = intrinsicBSize;
2507 } // else - leave bSize as is to fill the CB
2508 } else {
2509 bSize = fallbackIntrinsicSize.BSize(aWM);
2511 } // else - leave bSize as is to fill the CB
2512 bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2514 } else {
2515 // non-'auto' iSize, non-'auto' bSize
2516 iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2517 bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2521 return LogicalSize(aWM, iSize, bSize);
2524 nsRect nsContainerFrame::ComputeSimpleTightBounds(
2525 DrawTarget* aDrawTarget) const {
2526 if (StyleOutline()->ShouldPaintOutline() || StyleBorder()->HasBorder() ||
2527 !StyleBackground()->IsTransparent(this) ||
2528 StyleDisplay()->HasAppearance()) {
2529 // Not necessarily tight, due to clipping, negative
2530 // outline-offset, and lots of other issues, but that's OK
2531 return InkOverflowRect();
2534 nsRect r(0, 0, 0, 0);
2535 for (const auto& childLists : ChildLists()) {
2536 for (nsIFrame* child : childLists.mList) {
2537 r.UnionRect(
2538 r, child->ComputeTightBounds(aDrawTarget) + child->GetPosition());
2541 return r;
2544 void nsContainerFrame::PushDirtyBitToAbsoluteFrames() {
2545 if (!HasAnyStateBits(NS_FRAME_IS_DIRTY)) {
2546 return; // No dirty bit to push.
2548 if (!HasAbsolutelyPositionedChildren()) {
2549 return; // No absolute children to push to.
2551 GetAbsoluteContainingBlock()->MarkAllFramesDirty();
2554 // Define the MAX_FRAME_DEPTH to be the ContentSink's MAX_REFLOW_DEPTH plus
2555 // 4 for the frames above the document's frames:
2556 // the Viewport, GFXScroll, ScrollPort, and Canvas
2557 #define MAX_FRAME_DEPTH (MAX_REFLOW_DEPTH + 4)
2559 bool nsContainerFrame::IsFrameTreeTooDeep(const ReflowInput& aReflowInput,
2560 ReflowOutput& aMetrics,
2561 nsReflowStatus& aStatus) {
2562 if (aReflowInput.mReflowDepth > MAX_FRAME_DEPTH) {
2563 NS_WARNING("frame tree too deep; setting zero size and returning");
2564 AddStateBits(NS_FRAME_TOO_DEEP_IN_FRAME_TREE);
2565 ClearOverflowRects();
2566 aMetrics.ClearSize();
2567 aMetrics.SetBlockStartAscent(0);
2568 aMetrics.mCarriedOutBEndMargin.Zero();
2569 aMetrics.mOverflowAreas.Clear();
2571 aStatus.Reset();
2572 if (GetNextInFlow()) {
2573 // Reflow depth might vary between reflows, so we might have
2574 // successfully reflowed and split this frame before. If so, we
2575 // shouldn't delete its continuations.
2576 aStatus.SetIncomplete();
2579 return true;
2581 RemoveStateBits(NS_FRAME_TOO_DEEP_IN_FRAME_TREE);
2582 return false;
2585 bool nsContainerFrame::ShouldAvoidBreakInside(
2586 const ReflowInput& aReflowInput) const {
2587 MOZ_ASSERT(this == aReflowInput.mFrame,
2588 "Caller should pass a ReflowInput for this frame!");
2590 const auto* disp = StyleDisplay();
2591 const bool mayAvoidBreak = [&] {
2592 switch (disp->mBreakInside) {
2593 case StyleBreakWithin::Auto:
2594 return false;
2595 case StyleBreakWithin::Avoid:
2596 return true;
2597 case StyleBreakWithin::AvoidPage:
2598 return aReflowInput.mBreakType == ReflowInput::BreakType::Page;
2599 case StyleBreakWithin::AvoidColumn:
2600 return aReflowInput.mBreakType == ReflowInput::BreakType::Column;
2602 MOZ_ASSERT_UNREACHABLE("Unknown break-inside value");
2603 return false;
2604 }();
2606 if (!mayAvoidBreak) {
2607 return false;
2609 if (aReflowInput.mFlags.mIsTopOfPage) {
2610 return false;
2612 if (IsAbsolutelyPositioned(disp)) {
2613 return false;
2615 if (GetPrevInFlow()) {
2616 return false;
2618 return true;
2621 void nsContainerFrame::ConsiderChildOverflow(OverflowAreas& aOverflowAreas,
2622 nsIFrame* aChildFrame) {
2623 if (StyleDisplay()->IsContainLayout() && SupportsContainLayoutAndPaint()) {
2624 // If we have layout containment and are not a non-atomic, inline-level
2625 // principal box, we should only consider our child's ink overflow,
2626 // leaving the scrollable regions of the parent unaffected.
2627 // Note: scrollable overflow is a subset of ink overflow,
2628 // so this has the same affect as unioning the child's ink and
2629 // scrollable overflow with the parent's ink overflow.
2630 const OverflowAreas childOverflows(aChildFrame->InkOverflowRect(),
2631 nsRect());
2632 aOverflowAreas.UnionWith(childOverflows + aChildFrame->GetPosition());
2633 } else {
2634 aOverflowAreas.UnionWith(
2635 aChildFrame->GetActualAndNormalOverflowAreasRelativeToParent());
2639 StyleAlignFlags nsContainerFrame::CSSAlignmentForAbsPosChild(
2640 const ReflowInput& aChildRI, LogicalAxis aLogicalAxis) const {
2641 MOZ_ASSERT(aChildRI.mFrame->IsAbsolutelyPositioned(),
2642 "This method should only be called for abspos children");
2643 NS_ERROR(
2644 "Child classes that use css box alignment for abspos children "
2645 "should provide their own implementation of this method!");
2647 // In the unexpected/unlikely event that this implementation gets invoked,
2648 // just use "start" alignment.
2649 return StyleAlignFlags::START;
2652 nsOverflowContinuationTracker::nsOverflowContinuationTracker(
2653 nsContainerFrame* aFrame, bool aWalkOOFFrames,
2654 bool aSkipOverflowContainerChildren)
2655 : mOverflowContList(nullptr),
2656 mPrevOverflowCont(nullptr),
2657 mSentry(nullptr),
2658 mParent(aFrame),
2659 mSkipOverflowContainerChildren(aSkipOverflowContainerChildren),
2660 mWalkOOFFrames(aWalkOOFFrames) {
2661 MOZ_ASSERT(aFrame, "null frame pointer");
2662 SetupOverflowContList();
2665 void nsOverflowContinuationTracker::SetupOverflowContList() {
2666 MOZ_ASSERT(mParent, "null frame pointer");
2667 MOZ_ASSERT(!mOverflowContList, "already have list");
2668 nsContainerFrame* nif =
2669 static_cast<nsContainerFrame*>(mParent->GetNextInFlow());
2670 if (nif) {
2671 mOverflowContList = nif->GetOverflowContainers();
2672 if (mOverflowContList) {
2673 mParent = nif;
2674 SetUpListWalker();
2677 if (!mOverflowContList) {
2678 mOverflowContList = mParent->GetExcessOverflowContainers();
2679 if (mOverflowContList) {
2680 SetUpListWalker();
2686 * Helper function to walk past overflow continuations whose prev-in-flow
2687 * isn't a normal child and to set mSentry and mPrevOverflowCont correctly.
2689 void nsOverflowContinuationTracker::SetUpListWalker() {
2690 NS_ASSERTION(!mSentry && !mPrevOverflowCont,
2691 "forgot to reset mSentry or mPrevOverflowCont");
2692 if (mOverflowContList) {
2693 nsIFrame* cur = mOverflowContList->FirstChild();
2694 if (mSkipOverflowContainerChildren) {
2695 while (cur && cur->GetPrevInFlow()->HasAnyStateBits(
2696 NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2697 mPrevOverflowCont = cur;
2698 cur = cur->GetNextSibling();
2700 while (cur &&
2701 (cur->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) != mWalkOOFFrames)) {
2702 mPrevOverflowCont = cur;
2703 cur = cur->GetNextSibling();
2706 if (cur) {
2707 mSentry = cur->GetPrevInFlow();
2713 * Helper function to step forward through the overflow continuations list.
2714 * Sets mSentry and mPrevOverflowCont, skipping over OOF or non-OOF frames
2715 * as appropriate. May only be called when we have already set up an
2716 * mOverflowContList; mOverflowContList cannot be null.
2718 void nsOverflowContinuationTracker::StepForward() {
2719 MOZ_ASSERT(mOverflowContList, "null list");
2721 // Step forward
2722 if (mPrevOverflowCont) {
2723 mPrevOverflowCont = mPrevOverflowCont->GetNextSibling();
2724 } else {
2725 mPrevOverflowCont = mOverflowContList->FirstChild();
2728 // Skip over oof or non-oof frames as appropriate
2729 if (mSkipOverflowContainerChildren) {
2730 nsIFrame* cur = mPrevOverflowCont->GetNextSibling();
2731 while (cur &&
2732 (cur->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) != mWalkOOFFrames)) {
2733 mPrevOverflowCont = cur;
2734 cur = cur->GetNextSibling();
2738 // Set up the sentry
2739 mSentry = (mPrevOverflowCont->GetNextSibling())
2740 ? mPrevOverflowCont->GetNextSibling()->GetPrevInFlow()
2741 : nullptr;
2744 nsresult nsOverflowContinuationTracker::Insert(nsIFrame* aOverflowCont,
2745 nsReflowStatus& aReflowStatus) {
2746 MOZ_ASSERT(aOverflowCont, "null frame pointer");
2747 MOZ_ASSERT(!mSkipOverflowContainerChildren ||
2748 mWalkOOFFrames ==
2749 aOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW),
2750 "shouldn't insert frame that doesn't match walker type");
2751 MOZ_ASSERT(aOverflowCont->GetPrevInFlow(),
2752 "overflow containers must have a prev-in-flow");
2754 nsresult rv = NS_OK;
2755 bool reparented = false;
2756 nsPresContext* presContext = aOverflowCont->PresContext();
2757 bool addToList = !mSentry || aOverflowCont != mSentry->GetNextInFlow();
2759 // If we have a list and aOverflowCont is already in it then don't try to
2760 // add it again.
2761 if (addToList && aOverflowCont->GetParent() == mParent &&
2762 aOverflowCont->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) &&
2763 mOverflowContList && mOverflowContList->ContainsFrame(aOverflowCont)) {
2764 addToList = false;
2765 mPrevOverflowCont = aOverflowCont->GetPrevSibling();
2768 if (addToList) {
2769 if (aOverflowCont->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2770 // aOverflowCont is in some other overflow container list,
2771 // steal it first
2772 NS_ASSERTION(!(mOverflowContList &&
2773 mOverflowContList->ContainsFrame(aOverflowCont)),
2774 "overflow containers out of order");
2775 aOverflowCont->GetParent()->StealFrame(aOverflowCont);
2776 } else {
2777 aOverflowCont->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
2779 if (!mOverflowContList) {
2780 // Note: We don't use SetExcessOverflowContainers() since it requires
2781 // setting a non-empty list. It's OK to manually set an empty list to
2782 // ExcessOverflowContainersProperty() because we are going to insert
2783 // aOverflowCont to mOverflowContList below, which guarantees an nonempty
2784 // list in ExcessOverflowContainersProperty().
2785 mOverflowContList = new (presContext->PresShell()) nsFrameList();
2786 mParent->SetProperty(nsContainerFrame::ExcessOverflowContainersProperty(),
2787 mOverflowContList);
2788 SetUpListWalker();
2790 if (aOverflowCont->GetParent() != mParent) {
2791 nsContainerFrame::ReparentFrameView(aOverflowCont,
2792 aOverflowCont->GetParent(), mParent);
2793 reparented = true;
2796 // If aOverflowCont has a prev/next-in-flow that might be in
2797 // mOverflowContList we need to find it and insert after/before it to
2798 // maintain the order amongst next-in-flows in this list.
2799 nsIFrame* pif = aOverflowCont->GetPrevInFlow();
2800 nsIFrame* nif = aOverflowCont->GetNextInFlow();
2801 if ((pif && pif->GetParent() == mParent && pif != mPrevOverflowCont) ||
2802 (nif && nif->GetParent() == mParent && mPrevOverflowCont)) {
2803 for (nsIFrame* f : *mOverflowContList) {
2804 if (f == pif) {
2805 mPrevOverflowCont = pif;
2806 break;
2808 if (f == nif) {
2809 mPrevOverflowCont = f->GetPrevSibling();
2810 break;
2815 mOverflowContList->InsertFrame(mParent, mPrevOverflowCont, aOverflowCont);
2816 aReflowStatus.SetNextInFlowNeedsReflow();
2819 // If we need to reflow it, mark it dirty
2820 if (aReflowStatus.NextInFlowNeedsReflow()) {
2821 aOverflowCont->MarkSubtreeDirty();
2824 // It's in our list, just step forward
2825 StepForward();
2826 NS_ASSERTION(mPrevOverflowCont == aOverflowCont ||
2827 (mSkipOverflowContainerChildren &&
2828 mPrevOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) !=
2829 aOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)),
2830 "OverflowContTracker in unexpected state");
2832 if (addToList) {
2833 // Convert all non-overflow-container next-in-flows of aOverflowCont
2834 // into overflow containers and move them to our overflow
2835 // tracker. This preserves the invariant that the next-in-flows
2836 // of an overflow container are also overflow containers.
2837 nsIFrame* f = aOverflowCont->GetNextInFlow();
2838 if (f && (!f->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) ||
2839 (!reparented && f->GetParent() == mParent) ||
2840 (reparented && f->GetParent() != mParent))) {
2841 if (!f->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2842 f->GetParent()->StealFrame(f);
2844 Insert(f, aReflowStatus);
2847 return rv;
2850 void nsOverflowContinuationTracker::BeginFinish(nsIFrame* aChild) {
2851 MOZ_ASSERT(aChild, "null ptr");
2852 MOZ_ASSERT(aChild->GetNextInFlow(),
2853 "supposed to call Finish *before* deleting next-in-flow!");
2855 for (nsIFrame* f = aChild; f; f = f->GetNextInFlow()) {
2856 // We'll update these in EndFinish after the next-in-flows are gone.
2857 if (f == mPrevOverflowCont) {
2858 mSentry = nullptr;
2859 mPrevOverflowCont = nullptr;
2860 break;
2862 if (f == mSentry) {
2863 mSentry = nullptr;
2864 break;
2869 void nsOverflowContinuationTracker::EndFinish(nsIFrame* aChild) {
2870 if (!mOverflowContList) {
2871 return;
2873 // Forget mOverflowContList if it was deleted.
2874 nsFrameList* eoc = mParent->GetExcessOverflowContainers();
2875 if (eoc != mOverflowContList) {
2876 nsFrameList* oc = mParent->GetOverflowContainers();
2877 if (oc != mOverflowContList) {
2878 // mOverflowContList was deleted
2879 mPrevOverflowCont = nullptr;
2880 mSentry = nullptr;
2881 mParent = aChild->GetParent();
2882 mOverflowContList = nullptr;
2883 SetupOverflowContList();
2884 return;
2887 // The list survived, update mSentry if needed.
2888 if (!mSentry) {
2889 if (!mPrevOverflowCont) {
2890 SetUpListWalker();
2891 } else {
2892 mozilla::AutoRestore<nsIFrame*> saved(mPrevOverflowCont);
2893 // step backward to make StepForward() use our current mPrevOverflowCont
2894 mPrevOverflowCont = mPrevOverflowCont->GetPrevSibling();
2895 StepForward();
2900 /////////////////////////////////////////////////////////////////////////////
2901 // Debugging
2903 #ifdef DEBUG
2904 void nsContainerFrame::SanityCheckChildListsBeforeReflow() const {
2905 MOZ_ASSERT(IsFlexOrGridContainer(),
2906 "Only Flex / Grid containers can call this!");
2908 const auto didPushItemsBit = IsFlexContainerFrame()
2909 ? NS_STATE_FLEX_DID_PUSH_ITEMS
2910 : NS_STATE_GRID_DID_PUSH_ITEMS;
2911 ChildListIDs absLists = {FrameChildListID::Absolute, FrameChildListID::Fixed,
2912 FrameChildListID::OverflowContainers,
2913 FrameChildListID::ExcessOverflowContainers};
2914 ChildListIDs itemLists = {FrameChildListID::Principal,
2915 FrameChildListID::Overflow};
2916 for (const nsIFrame* f = this; f; f = f->GetNextInFlow()) {
2917 MOZ_ASSERT(!f->HasAnyStateBits(didPushItemsBit),
2918 "At start of reflow, we should've pulled items back from all "
2919 "NIFs and cleared the state bit stored in didPushItemsBit in "
2920 "the process.");
2921 for (const auto& [list, listID] : f->ChildLists()) {
2922 if (!itemLists.contains(listID)) {
2923 MOZ_ASSERT(
2924 absLists.contains(listID) || listID == FrameChildListID::Backdrop,
2925 "unexpected non-empty child list");
2926 continue;
2928 for (const auto* child : list) {
2929 MOZ_ASSERT(f == this || child->GetPrevInFlow(),
2930 "all pushed items must be pulled up before reflow");
2934 // If we have a prev-in-flow, each of its children's next-in-flow
2935 // should be one of our children or be null.
2936 const auto* pif = static_cast<nsContainerFrame*>(GetPrevInFlow());
2937 if (pif) {
2938 const nsFrameList* oc = GetOverflowContainers();
2939 const nsFrameList* eoc = GetExcessOverflowContainers();
2940 const nsFrameList* pifEOC = pif->GetExcessOverflowContainers();
2941 for (const nsIFrame* child : pif->PrincipalChildList()) {
2942 const nsIFrame* childNIF = child->GetNextInFlow();
2943 MOZ_ASSERT(!childNIF || mFrames.ContainsFrame(childNIF) ||
2944 (pifEOC && pifEOC->ContainsFrame(childNIF)) ||
2945 (oc && oc->ContainsFrame(childNIF)) ||
2946 (eoc && eoc->ContainsFrame(childNIF)));
2951 void nsContainerFrame::SetDidPushItemsBitIfNeeded(ChildListID aListID,
2952 nsIFrame* aOldFrame) {
2953 MOZ_ASSERT(IsFlexOrGridContainer(),
2954 "Only Flex / Grid containers can call this!");
2956 // Note that FrameChildListID::Principal doesn't mean aOldFrame must be on
2957 // that list. It can also be on FrameChildListID::Overflow, in which case it
2958 // might be a pushed item, and if it's the only pushed item our DID_PUSH_ITEMS
2959 // bit will lie.
2960 if (aListID == FrameChildListID::Principal && !aOldFrame->GetPrevInFlow()) {
2961 // Since the bit may lie, set the mDidPushItemsBitMayLie value to true for
2962 // ourself and for all our prev-in-flows.
2963 nsContainerFrame* frameThatMayLie = this;
2964 do {
2965 frameThatMayLie->mDidPushItemsBitMayLie = true;
2966 frameThatMayLie =
2967 static_cast<nsContainerFrame*>(frameThatMayLie->GetPrevInFlow());
2968 } while (frameThatMayLie);
2971 #endif
2973 #ifdef DEBUG_FRAME_DUMP
2974 void nsContainerFrame::List(FILE* out, const char* aPrefix,
2975 ListFlags aFlags) const {
2976 nsCString str;
2977 ListGeneric(str, aPrefix, aFlags);
2978 ExtraContainerFrameInfo(str);
2980 // Output the frame name and various fields.
2981 fprintf_stderr(out, "%s <\n", str.get());
2983 const nsCString pfx = nsCString(aPrefix) + " "_ns;
2985 // Output principal child list separately since we want to omit its
2986 // name and address.
2987 for (nsIFrame* kid : PrincipalChildList()) {
2988 kid->List(out, pfx.get(), aFlags);
2991 // Output rest of the child lists.
2992 const ChildListIDs skippedListIDs = {FrameChildListID::Principal};
2993 ListChildLists(out, pfx.get(), aFlags, skippedListIDs);
2995 fprintf_stderr(out, "%s>\n", aPrefix);
2998 void nsContainerFrame::ListWithMatchedRules(FILE* out,
2999 const char* aPrefix) const {
3000 fprintf_stderr(out, "%s%s\n", aPrefix, ListTag().get());
3002 nsCString rulePrefix;
3003 rulePrefix += aPrefix;
3004 rulePrefix += " ";
3005 ListMatchedRules(out, rulePrefix.get());
3007 nsCString childPrefix;
3008 childPrefix += aPrefix;
3009 childPrefix += " ";
3011 for (const auto& childList : ChildLists()) {
3012 for (const nsIFrame* kid : childList.mList) {
3013 kid->ListWithMatchedRules(out, childPrefix.get());
3018 void nsContainerFrame::ListChildLists(FILE* aOut, const char* aPrefix,
3019 ListFlags aFlags,
3020 ChildListIDs aSkippedListIDs) const {
3021 const nsCString nestedPfx = nsCString(aPrefix) + " "_ns;
3023 for (const auto& [list, listID] : ChildLists()) {
3024 if (aSkippedListIDs.contains(listID)) {
3025 continue;
3028 // Use nsPrintfCString so that %p don't output prefix "0x". This is
3029 // consistent with nsIFrame::ListTag().
3030 const nsPrintfCString str("%s%s@%p <\n", aPrefix, ChildListName(listID),
3031 &GetChildList(listID));
3032 fprintf_stderr(aOut, "%s", str.get());
3034 for (nsIFrame* kid : list) {
3035 // Verify the child frame's parent frame pointer is correct.
3036 NS_ASSERTION(kid->GetParent() == this, "Bad parent frame pointer!");
3037 kid->List(aOut, nestedPfx.get(), aFlags);
3039 fprintf_stderr(aOut, "%s>\n", aPrefix);
3043 void nsContainerFrame::ExtraContainerFrameInfo(nsACString& aTo) const {
3044 (void)aTo;
3047 #endif