Backed out changeset f53842753805 (bug 1804872) for causing reftest failures on 15535...
[gecko.git] / layout / generic / nsContainerFrame.cpp
bloba76e1fee801b3854771973e4362dc0e5c3deeb1b
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(ChildListID aListID, nsIFrame* aOldFrame) {
157 MOZ_ASSERT(aListID == FrameChildListID::Principal ||
158 aListID == FrameChildListID::NoReflowPrincipal,
159 "unexpected child list");
161 AutoTArray<nsIFrame*, 10> continuations;
163 nsIFrame* continuation = aOldFrame;
164 while (continuation) {
165 continuations.AppendElement(continuation);
166 continuation = continuation->GetNextContinuation();
170 mozilla::PresShell* presShell = PresShell();
171 nsContainerFrame* lastParent = nullptr;
173 // Loop and destroy aOldFrame and all of its continuations.
175 // Request a reflow on the parent frames involved unless we were explicitly
176 // told not to (FrameChildListID::NoReflowPrincipal).
177 const bool generateReflowCommand =
178 (FrameChildListID::NoReflowPrincipal != aListID);
179 for (nsIFrame* continuation : Reversed(continuations)) {
180 nsContainerFrame* parent = continuation->GetParent();
182 // Please note that 'parent' may not actually be where 'continuation' lives.
183 // We really MUST use StealFrame() and nothing else here.
184 // @see nsInlineFrame::StealFrame for details.
185 parent->StealFrame(continuation);
186 continuation->Destroy();
187 if (generateReflowCommand && parent != lastParent) {
188 presShell->FrameNeedsReflow(parent, IntrinsicDirty::FrameAndAncestors,
189 NS_FRAME_HAS_DIRTY_CHILDREN);
190 lastParent = parent;
195 void nsContainerFrame::DestroyAbsoluteFrames(
196 nsIFrame* aDestructRoot, PostDestroyData& aPostDestroyData) {
197 if (IsAbsoluteContainer()) {
198 GetAbsoluteContainingBlock()->DestroyFrames(this, aDestructRoot,
199 aPostDestroyData);
200 MarkAsNotAbsoluteContainingBlock();
204 void nsContainerFrame::SafelyDestroyFrameListProp(
205 nsIFrame* aDestructRoot, PostDestroyData& aPostDestroyData,
206 mozilla::PresShell* aPresShell, FrameListPropertyDescriptor aProp) {
207 // Note that the last frame can be removed through another route and thus
208 // delete the property -- that's why we fetch the property again before
209 // removing each frame rather than fetching it once and iterating the list.
210 while (nsFrameList* frameList = GetProperty(aProp)) {
211 nsIFrame* frame = frameList->RemoveFirstChild();
212 if (MOZ_LIKELY(frame)) {
213 frame->DestroyFrom(aDestructRoot, aPostDestroyData);
214 } else {
215 Unused << TakeProperty(aProp);
216 frameList->Delete(aPresShell);
217 return;
222 void nsContainerFrame::DestroyFrom(nsIFrame* aDestructRoot,
223 PostDestroyData& aPostDestroyData) {
224 // Prevent event dispatch during destruction.
225 if (HasView()) {
226 GetView()->SetFrame(nullptr);
229 DestroyAbsoluteFrames(aDestructRoot, aPostDestroyData);
231 // Destroy frames on the principal child list.
232 mFrames.DestroyFramesFrom(aDestructRoot, aPostDestroyData);
234 // If we have any IB split siblings, clear their references to us.
235 if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
236 // Delete previous sibling's reference to me.
237 if (nsIFrame* prevSib = GetProperty(nsIFrame::IBSplitPrevSibling())) {
238 NS_WARNING_ASSERTION(
239 this == prevSib->GetProperty(nsIFrame::IBSplitSibling()),
240 "IB sibling chain is inconsistent");
241 prevSib->RemoveProperty(nsIFrame::IBSplitSibling());
244 // Delete next sibling's reference to me.
245 if (nsIFrame* nextSib = GetProperty(nsIFrame::IBSplitSibling())) {
246 NS_WARNING_ASSERTION(
247 this == nextSib->GetProperty(nsIFrame::IBSplitPrevSibling()),
248 "IB sibling chain is inconsistent");
249 nextSib->RemoveProperty(nsIFrame::IBSplitPrevSibling());
252 #ifdef DEBUG
253 // This is just so we can assert it's not set in nsIFrame::DestroyFrom.
254 RemoveStateBits(NS_FRAME_PART_OF_IBSPLIT);
255 #endif
258 if (MOZ_UNLIKELY(!mProperties.IsEmpty())) {
259 using T = mozilla::FrameProperties::UntypedDescriptor;
260 bool hasO = false, hasOC = false, hasEOC = false, hasBackdrop = false;
261 mProperties.ForEach([&](const T& aProp, uint64_t) {
262 if (aProp == OverflowProperty()) {
263 hasO = true;
264 } else if (aProp == OverflowContainersProperty()) {
265 hasOC = true;
266 } else if (aProp == ExcessOverflowContainersProperty()) {
267 hasEOC = true;
268 } else if (aProp == BackdropProperty()) {
269 hasBackdrop = true;
271 return true;
274 // Destroy frames on the auxiliary frame lists and delete the lists.
275 nsPresContext* pc = PresContext();
276 mozilla::PresShell* presShell = pc->PresShell();
277 if (hasO) {
278 SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
279 OverflowProperty());
282 MOZ_ASSERT(
283 IsFrameOfType(eCanContainOverflowContainers) || !(hasOC || hasEOC),
284 "this type of frame shouldn't have overflow containers");
285 if (hasOC) {
286 SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
287 OverflowContainersProperty());
289 if (hasEOC) {
290 SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
291 ExcessOverflowContainersProperty());
294 MOZ_ASSERT(!GetProperty(BackdropProperty()) ||
295 StyleDisplay()->mTopLayer != StyleTopLayer::None,
296 "only top layer frame may have backdrop");
297 if (hasBackdrop) {
298 SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
299 BackdropProperty());
303 nsSplittableFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
306 /////////////////////////////////////////////////////////////////////////////
307 // Child frame enumeration
309 const nsFrameList& nsContainerFrame::GetChildList(ChildListID aListID) const {
310 // We only know about the principal child list, the overflow lists,
311 // and the backdrop list.
312 switch (aListID) {
313 case FrameChildListID::Principal:
314 return mFrames;
315 case FrameChildListID::Overflow: {
316 nsFrameList* list = GetOverflowFrames();
317 return list ? *list : nsFrameList::EmptyList();
319 case FrameChildListID::OverflowContainers: {
320 nsFrameList* list = GetOverflowContainers();
321 return list ? *list : nsFrameList::EmptyList();
323 case FrameChildListID::ExcessOverflowContainers: {
324 nsFrameList* list = GetExcessOverflowContainers();
325 return list ? *list : nsFrameList::EmptyList();
327 case FrameChildListID::Backdrop: {
328 nsFrameList* list = GetProperty(BackdropProperty());
329 return list ? *list : nsFrameList::EmptyList();
331 default:
332 return nsSplittableFrame::GetChildList(aListID);
336 void nsContainerFrame::GetChildLists(nsTArray<ChildList>* aLists) const {
337 mFrames.AppendIfNonempty(aLists, FrameChildListID::Principal);
339 using T = mozilla::FrameProperties::UntypedDescriptor;
340 mProperties.ForEach([this, aLists](const T& aProp, uint64_t aValue) {
341 typedef const nsFrameList* L;
342 if (aProp == OverflowProperty()) {
343 reinterpret_cast<L>(aValue)->AppendIfNonempty(aLists,
344 FrameChildListID::Overflow);
345 } else if (aProp == OverflowContainersProperty()) {
346 MOZ_ASSERT(IsFrameOfType(nsIFrame::eCanContainOverflowContainers),
347 "found unexpected OverflowContainersProperty");
348 Unused << this; // silence clang -Wunused-lambda-capture in opt builds
349 reinterpret_cast<L>(aValue)->AppendIfNonempty(
350 aLists, FrameChildListID::OverflowContainers);
351 } else if (aProp == ExcessOverflowContainersProperty()) {
352 MOZ_ASSERT(IsFrameOfType(nsIFrame::eCanContainOverflowContainers),
353 "found unexpected ExcessOverflowContainersProperty");
354 Unused << this; // silence clang -Wunused-lambda-capture in opt builds
355 reinterpret_cast<L>(aValue)->AppendIfNonempty(
356 aLists, FrameChildListID::ExcessOverflowContainers);
357 } else if (aProp == BackdropProperty()) {
358 reinterpret_cast<L>(aValue)->AppendIfNonempty(aLists,
359 FrameChildListID::Backdrop);
361 return true;
364 nsSplittableFrame::GetChildLists(aLists);
367 /////////////////////////////////////////////////////////////////////////////
368 // Painting/Events
370 void nsContainerFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
371 const nsDisplayListSet& aLists) {
372 DisplayBorderBackgroundOutline(aBuilder, aLists);
373 BuildDisplayListForNonBlockChildren(aBuilder, aLists);
376 void nsContainerFrame::BuildDisplayListForNonBlockChildren(
377 nsDisplayListBuilder* aBuilder, const nsDisplayListSet& aLists,
378 DisplayChildFlags aFlags) {
379 nsIFrame* kid = mFrames.FirstChild();
380 // Put each child's background directly onto the content list
381 nsDisplayListSet set(aLists, aLists.Content());
382 // The children should be in content order
383 while (kid) {
384 BuildDisplayListForChild(aBuilder, kid, set, aFlags);
385 kid = kid->GetNextSibling();
389 class nsDisplaySelectionOverlay : public nsPaintedDisplayItem {
390 public:
392 * @param aSelectionValue nsISelectionController::getDisplaySelection.
394 nsDisplaySelectionOverlay(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame,
395 int16_t aSelectionValue)
396 : nsPaintedDisplayItem(aBuilder, aFrame),
397 mSelectionValue(aSelectionValue) {
398 MOZ_COUNT_CTOR(nsDisplaySelectionOverlay);
400 MOZ_COUNTED_DTOR_OVERRIDE(nsDisplaySelectionOverlay)
402 virtual void Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) override;
403 bool CreateWebRenderCommands(
404 mozilla::wr::DisplayListBuilder& aBuilder,
405 mozilla::wr::IpcResourceUpdateQueue& aResources,
406 const StackingContextHelper& aSc,
407 mozilla::layers::RenderRootStateManager* aManager,
408 nsDisplayListBuilder* aDisplayListBuilder) override;
409 NS_DISPLAY_DECL_NAME("SelectionOverlay", TYPE_SELECTION_OVERLAY)
410 private:
411 DeviceColor ComputeColor() const;
413 static DeviceColor ComputeColorFromSelectionStyle(ComputedStyle&);
414 static DeviceColor ApplyTransparencyIfNecessary(nscolor);
416 // nsISelectionController::getDisplaySelection.
417 int16_t mSelectionValue;
420 DeviceColor nsDisplaySelectionOverlay::ApplyTransparencyIfNecessary(
421 nscolor aColor) {
422 // If it has already alpha, leave it like that.
423 if (NS_GET_A(aColor) != 255) {
424 return ToDeviceColor(aColor);
427 // NOTE(emilio): Blink and WebKit do something slightly different here, and
428 // blend the color with white instead, both for overlays and text backgrounds.
429 auto color = sRGBColor::FromABGR(aColor);
430 color.a = 0.5;
431 return ToDeviceColor(color);
434 DeviceColor nsDisplaySelectionOverlay::ComputeColorFromSelectionStyle(
435 ComputedStyle& aStyle) {
436 return ApplyTransparencyIfNecessary(
437 aStyle.GetVisitedDependentColor(&nsStyleBackground::mBackgroundColor));
440 DeviceColor nsDisplaySelectionOverlay::ComputeColor() const {
441 LookAndFeel::ColorID colorID;
442 if (RefPtr<ComputedStyle> style =
443 mFrame->ComputeSelectionStyle(mSelectionValue)) {
444 return ComputeColorFromSelectionStyle(*style);
446 if (mSelectionValue == nsISelectionController::SELECTION_ON) {
447 colorID = LookAndFeel::ColorID::Highlight;
448 } else if (mSelectionValue == nsISelectionController::SELECTION_ATTENTION) {
449 colorID = LookAndFeel::ColorID::TextSelectAttentionBackground;
450 } else {
451 colorID = LookAndFeel::ColorID::TextSelectDisabledBackground;
454 return ApplyTransparencyIfNecessary(
455 LookAndFeel::Color(colorID, mFrame, NS_RGB(255, 255, 255)));
458 void nsDisplaySelectionOverlay::Paint(nsDisplayListBuilder* aBuilder,
459 gfxContext* aCtx) {
460 DrawTarget& aDrawTarget = *aCtx->GetDrawTarget();
461 ColorPattern color(ComputeColor());
463 nsIntRect pxRect =
464 GetPaintRect(aBuilder, aCtx)
465 .ToOutsidePixels(mFrame->PresContext()->AppUnitsPerDevPixel());
466 Rect rect(pxRect.x, pxRect.y, pxRect.width, pxRect.height);
467 MaybeSnapToDevicePixels(rect, aDrawTarget, true);
469 aDrawTarget.FillRect(rect, color);
472 bool nsDisplaySelectionOverlay::CreateWebRenderCommands(
473 mozilla::wr::DisplayListBuilder& aBuilder,
474 mozilla::wr::IpcResourceUpdateQueue& aResources,
475 const StackingContextHelper& aSc,
476 mozilla::layers::RenderRootStateManager* aManager,
477 nsDisplayListBuilder* aDisplayListBuilder) {
478 wr::LayoutRect bounds = wr::ToLayoutRect(LayoutDeviceRect::FromAppUnits(
479 nsRect(ToReferenceFrame(), Frame()->GetSize()),
480 mFrame->PresContext()->AppUnitsPerDevPixel()));
481 aBuilder.PushRect(bounds, bounds, !BackfaceIsHidden(), false, false,
482 wr::ToColorF(ComputeColor()));
483 return true;
486 void nsContainerFrame::DisplaySelectionOverlay(nsDisplayListBuilder* aBuilder,
487 nsDisplayList* aList,
488 uint16_t aContentType) {
489 if (!IsSelected() || !IsVisibleForPainting()) {
490 return;
493 int16_t displaySelection = PresShell()->GetSelectionFlags();
494 if (!(displaySelection & aContentType)) {
495 return;
498 const nsFrameSelection* frameSelection = GetConstFrameSelection();
499 int16_t selectionValue = frameSelection->GetDisplaySelection();
501 if (selectionValue <= nsISelectionController::SELECTION_HIDDEN) {
502 return; // selection is hidden or off
505 nsIContent* newContent = mContent->GetParent();
507 // check to see if we are anonymous content
508 // XXXbz there has GOT to be a better way of determining this!
509 int32_t offset =
510 newContent ? newContent->ComputeIndexOf_Deprecated(mContent) : 0;
512 // look up to see what selection(s) are on this frame
513 UniquePtr<SelectionDetails> details =
514 frameSelection->LookUpSelection(newContent, offset, 1, false);
515 if (!details) {
516 return;
519 bool normal = false;
520 for (SelectionDetails* sd = details.get(); sd; sd = sd->mNext.get()) {
521 if (sd->mSelectionType == SelectionType::eNormal) {
522 normal = true;
526 if (!normal && aContentType == nsISelectionDisplay::DISPLAY_IMAGES) {
527 // Don't overlay an image if it's not in the primary selection.
528 return;
531 aList->AppendNewToTop<nsDisplaySelectionOverlay>(aBuilder, this,
532 selectionValue);
535 /* virtual */
536 void nsContainerFrame::ChildIsDirty(nsIFrame* aChild) {
537 NS_ASSERTION(aChild->IsSubtreeDirty(), "child isn't actually dirty");
539 AddStateBits(NS_FRAME_HAS_DIRTY_CHILDREN);
542 nsIFrame::FrameSearchResult nsContainerFrame::PeekOffsetNoAmount(
543 bool aForward, int32_t* aOffset) {
544 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
545 // Don't allow the caret to stay in an empty (leaf) container frame.
546 return CONTINUE_EMPTY;
549 nsIFrame::FrameSearchResult nsContainerFrame::PeekOffsetCharacter(
550 bool aForward, int32_t* aOffset, PeekOffsetCharacterOptions aOptions) {
551 NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
552 // Don't allow the caret to stay in an empty (leaf) container frame.
553 return CONTINUE_EMPTY;
556 /////////////////////////////////////////////////////////////////////////////
557 // Helper member functions
560 * Position the view associated with |aKidFrame|, if there is one. A
561 * container frame should call this method after positioning a frame,
562 * but before |Reflow|.
564 void nsContainerFrame::PositionFrameView(nsIFrame* aKidFrame) {
565 nsIFrame* parentFrame = aKidFrame->GetParent();
566 if (!aKidFrame->HasView() || !parentFrame) return;
568 nsView* view = aKidFrame->GetView();
569 nsViewManager* vm = view->GetViewManager();
570 nsPoint pt;
571 nsView* ancestorView = parentFrame->GetClosestView(&pt);
573 if (ancestorView != view->GetParent()) {
574 NS_ASSERTION(ancestorView == view->GetParent()->GetParent(),
575 "Allowed only one anonymous view between frames");
576 // parentFrame is responsible for positioning aKidFrame's view
577 // explicitly
578 return;
581 pt += aKidFrame->GetPosition();
582 vm->MoveViewTo(view, pt.x, pt.y);
585 nsresult nsContainerFrame::ReparentFrameView(nsIFrame* aChildFrame,
586 nsIFrame* aOldParentFrame,
587 nsIFrame* aNewParentFrame) {
588 MOZ_ASSERT(aChildFrame, "null child frame pointer");
589 MOZ_ASSERT(aOldParentFrame, "null old parent frame pointer");
590 MOZ_ASSERT(aNewParentFrame, "null new parent frame pointer");
591 MOZ_ASSERT(aOldParentFrame != aNewParentFrame,
592 "same old and new parent frame");
594 // See if either the old parent frame or the new parent frame have a view
595 while (!aOldParentFrame->HasView() && !aNewParentFrame->HasView()) {
596 // Walk up both the old parent frame and the new parent frame nodes
597 // stopping when we either find a common parent or views for one
598 // or both of the frames.
600 // This works well in the common case where we push/pull and the old parent
601 // frame and the new parent frame are part of the same flow. They will
602 // typically be the same distance (height wise) from the
603 aOldParentFrame = aOldParentFrame->GetParent();
604 aNewParentFrame = aNewParentFrame->GetParent();
606 // We should never walk all the way to the root frame without finding
607 // a view
608 NS_ASSERTION(aOldParentFrame && aNewParentFrame, "didn't find view");
610 // See if we reached a common ancestor
611 if (aOldParentFrame == aNewParentFrame) {
612 break;
616 // See if we found a common parent frame
617 if (aOldParentFrame == aNewParentFrame) {
618 // We found a common parent and there are no views between the old parent
619 // and the common parent or the new parent frame and the common parent.
620 // Because neither the old parent frame nor the new parent frame have views,
621 // then any child views don't need reparenting
622 return NS_OK;
625 // We found views for one or both of the ancestor frames before we
626 // found a common ancestor.
627 nsView* oldParentView = aOldParentFrame->GetClosestView();
628 nsView* newParentView = aNewParentFrame->GetClosestView();
630 // See if the old parent frame and the new parent frame are in the
631 // same view sub-hierarchy. If they are then we don't have to do
632 // anything
633 if (oldParentView != newParentView) {
634 // They're not so we need to reparent any child views
635 aChildFrame->ReparentFrameViewTo(oldParentView->GetViewManager(),
636 newParentView, oldParentView);
639 return NS_OK;
642 void nsContainerFrame::ReparentFrameViewList(const nsFrameList& aChildFrameList,
643 nsIFrame* aOldParentFrame,
644 nsIFrame* aNewParentFrame) {
645 MOZ_ASSERT(aChildFrameList.NotEmpty(), "empty child frame list");
646 MOZ_ASSERT(aOldParentFrame, "null old parent frame pointer");
647 MOZ_ASSERT(aNewParentFrame, "null new parent frame pointer");
648 MOZ_ASSERT(aOldParentFrame != aNewParentFrame,
649 "same old and new parent frame");
651 // See if either the old parent frame or the new parent frame have a view
652 while (!aOldParentFrame->HasView() && !aNewParentFrame->HasView()) {
653 // Walk up both the old parent frame and the new parent frame nodes
654 // stopping when we either find a common parent or views for one
655 // or both of the frames.
657 // This works well in the common case where we push/pull and the old parent
658 // frame and the new parent frame are part of the same flow. They will
659 // typically be the same distance (height wise) from the
660 aOldParentFrame = aOldParentFrame->GetParent();
661 aNewParentFrame = aNewParentFrame->GetParent();
663 // We should never walk all the way to the root frame without finding
664 // a view
665 NS_ASSERTION(aOldParentFrame && aNewParentFrame, "didn't find view");
667 // See if we reached a common ancestor
668 if (aOldParentFrame == aNewParentFrame) {
669 break;
673 // See if we found a common parent frame
674 if (aOldParentFrame == aNewParentFrame) {
675 // We found a common parent and there are no views between the old parent
676 // and the common parent or the new parent frame and the common parent.
677 // Because neither the old parent frame nor the new parent frame have views,
678 // then any child views don't need reparenting
679 return;
682 // We found views for one or both of the ancestor frames before we
683 // found a common ancestor.
684 nsView* oldParentView = aOldParentFrame->GetClosestView();
685 nsView* newParentView = aNewParentFrame->GetClosestView();
687 // See if the old parent frame and the new parent frame are in the
688 // same view sub-hierarchy. If they are then we don't have to do
689 // anything
690 if (oldParentView != newParentView) {
691 nsViewManager* viewManager = oldParentView->GetViewManager();
693 // They're not so we need to reparent any child views
694 for (nsIFrame* f : aChildFrameList) {
695 f->ReparentFrameViewTo(viewManager, newParentView, oldParentView);
700 void nsContainerFrame::ReparentFrame(nsIFrame* aFrame,
701 nsContainerFrame* aOldParent,
702 nsContainerFrame* aNewParent) {
703 NS_ASSERTION(aOldParent == aFrame->GetParent(),
704 "Parent not consistent with expectations");
706 aFrame->SetParent(aNewParent);
708 // When pushing and pulling frames we need to check for whether any
709 // views need to be reparented
710 ReparentFrameView(aFrame, aOldParent, aNewParent);
713 void nsContainerFrame::ReparentFrames(nsFrameList& aFrameList,
714 nsContainerFrame* aOldParent,
715 nsContainerFrame* aNewParent) {
716 for (auto* f : aFrameList) {
717 ReparentFrame(f, aOldParent, aNewParent);
721 void nsContainerFrame::SetSizeConstraints(nsPresContext* aPresContext,
722 nsIWidget* aWidget,
723 const nsSize& aMinSize,
724 const nsSize& aMaxSize) {
725 LayoutDeviceIntSize devMinSize(
726 aPresContext->AppUnitsToDevPixels(aMinSize.width),
727 aPresContext->AppUnitsToDevPixels(aMinSize.height));
728 LayoutDeviceIntSize devMaxSize(
729 aMaxSize.width == NS_UNCONSTRAINEDSIZE
730 ? NS_MAXSIZE
731 : aPresContext->AppUnitsToDevPixels(aMaxSize.width),
732 aMaxSize.height == NS_UNCONSTRAINEDSIZE
733 ? NS_MAXSIZE
734 : aPresContext->AppUnitsToDevPixels(aMaxSize.height));
736 // MinSize has a priority over MaxSize
737 if (devMinSize.width > devMaxSize.width) devMaxSize.width = devMinSize.width;
738 if (devMinSize.height > devMaxSize.height)
739 devMaxSize.height = devMinSize.height;
741 nsIWidget* rootWidget = aPresContext->GetNearestWidget();
742 DesktopToLayoutDeviceScale constraintsScale(MOZ_WIDGET_INVALID_SCALE);
743 if (rootWidget) {
744 constraintsScale = rootWidget->GetDesktopToDeviceScale();
747 widget::SizeConstraints constraints(devMinSize, devMaxSize, constraintsScale);
749 // The sizes are in inner window sizes, so convert them into outer window
750 // sizes. Use a size of (200, 200) as only the difference between the inner
751 // and outer size is needed.
752 const LayoutDeviceIntSize sizeDiff = aWidget->ClientToWindowSizeDifference();
753 if (constraints.mMinSize.width) {
754 constraints.mMinSize.width += sizeDiff.width;
756 if (constraints.mMinSize.height) {
757 constraints.mMinSize.height += sizeDiff.height;
759 if (constraints.mMaxSize.width != NS_MAXSIZE) {
760 constraints.mMaxSize.width += sizeDiff.width;
762 if (constraints.mMaxSize.height != NS_MAXSIZE) {
763 constraints.mMaxSize.height += sizeDiff.height;
766 aWidget->SetSizeConstraints(constraints);
769 void nsContainerFrame::SyncFrameViewAfterReflow(nsPresContext* aPresContext,
770 nsIFrame* aFrame, nsView* aView,
771 const nsRect& aInkOverflowArea,
772 ReflowChildFlags aFlags) {
773 if (!aView) {
774 return;
777 // Make sure the view is sized and positioned correctly
778 if (!(aFlags & ReflowChildFlags::NoMoveView)) {
779 PositionFrameView(aFrame);
782 if (!(aFlags & ReflowChildFlags::NoSizeView)) {
783 nsViewManager* vm = aView->GetViewManager();
785 vm->ResizeView(aView, aInkOverflowArea, true);
789 void nsContainerFrame::DoInlineMinISize(gfxContext* aRenderingContext,
790 InlineMinISizeData* aData) {
791 auto handleChildren = [aRenderingContext](auto frame, auto data) {
792 for (nsIFrame* kid : frame->mFrames) {
793 kid->AddInlineMinISize(aRenderingContext, data);
796 DoInlineIntrinsicISize(aData, handleChildren);
799 void nsContainerFrame::DoInlinePrefISize(gfxContext* aRenderingContext,
800 InlinePrefISizeData* aData) {
801 auto handleChildren = [aRenderingContext](auto frame, auto data) {
802 for (nsIFrame* kid : frame->mFrames) {
803 kid->AddInlinePrefISize(aRenderingContext, data);
806 DoInlineIntrinsicISize(aData, handleChildren);
807 aData->mLineIsEmpty = false;
810 /* virtual */
811 LogicalSize nsContainerFrame::ComputeAutoSize(
812 gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
813 nscoord aAvailableISize, const LogicalSize& aMargin,
814 const mozilla::LogicalSize& aBorderPadding,
815 const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) {
816 LogicalSize result(aWM, 0xdeadbeef, NS_UNCONSTRAINEDSIZE);
817 nscoord availBased =
818 aAvailableISize - aMargin.ISize(aWM) - aBorderPadding.ISize(aWM);
819 // replaced elements always shrink-wrap
820 if (aFlags.contains(ComputeSizeFlag::ShrinkWrap) ||
821 IsFrameOfType(eReplaced)) {
822 // Only bother computing our 'auto' ISize if the result will be used.
823 const auto& styleISize = aSizeOverrides.mStyleISize
824 ? *aSizeOverrides.mStyleISize
825 : StylePosition()->ISize(aWM);
826 if (styleISize.IsAuto()) {
827 result.ISize(aWM) =
828 ShrinkISizeToFit(aRenderingContext, availBased, aFlags);
830 } else {
831 result.ISize(aWM) = availBased;
834 if (IsTableCaption()) {
835 // If we're a container for font size inflation, then shrink
836 // wrapping inside of us should not apply font size inflation.
837 AutoMaybeDisableFontInflation an(this);
839 WritingMode tableWM = GetParent()->GetWritingMode();
840 if (aWM.IsOrthogonalTo(tableWM)) {
841 // For an orthogonal caption on a block-dir side of the table, shrink-wrap
842 // to min-isize.
843 result.ISize(aWM) = GetMinISize(aRenderingContext);
844 } else {
845 // The outer frame constrains our available isize to the isize of
846 // the table. Grow if our min-isize is bigger than that, but not
847 // larger than the containing block isize. (It would really be nice
848 // to transmit that information another way, so we could grow up to
849 // the table's available isize, but that's harder.)
850 nscoord min = GetMinISize(aRenderingContext);
851 if (min > aCBSize.ISize(aWM)) {
852 min = aCBSize.ISize(aWM);
854 if (min > result.ISize(aWM)) {
855 result.ISize(aWM) = min;
859 return result;
862 void nsContainerFrame::ReflowChild(
863 nsIFrame* aKidFrame, nsPresContext* aPresContext,
864 ReflowOutput& aDesiredSize, const ReflowInput& aReflowInput,
865 const WritingMode& aWM, const LogicalPoint& aPos,
866 const nsSize& aContainerSize, ReflowChildFlags aFlags,
867 nsReflowStatus& aStatus, nsOverflowContinuationTracker* aTracker) {
868 MOZ_ASSERT(aReflowInput.mFrame == aKidFrame, "bad reflow input");
869 if (aWM.IsPhysicalRTL()) {
870 NS_ASSERTION(aContainerSize.width != NS_UNCONSTRAINEDSIZE,
871 "ReflowChild with unconstrained container width!");
873 MOZ_ASSERT(aDesiredSize.InkOverflow() == nsRect(0, 0, 0, 0) &&
874 aDesiredSize.ScrollableOverflow() == nsRect(0, 0, 0, 0),
875 "please reset the overflow areas before calling ReflowChild");
877 // Position the child frame and its view if requested.
878 if (ReflowChildFlags::NoMoveFrame !=
879 (aFlags & ReflowChildFlags::NoMoveFrame)) {
880 aKidFrame->SetPosition(aWM, aPos, aContainerSize);
883 if (!(aFlags & ReflowChildFlags::NoMoveView)) {
884 PositionFrameView(aKidFrame);
885 PositionChildViews(aKidFrame);
888 // Reflow the child frame
889 aKidFrame->Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
891 // If the child frame is complete, delete any next-in-flows,
892 // but only if the NoDeleteNextInFlowChild flag isn't set.
893 if (!aStatus.IsInlineBreakBefore() && aStatus.IsFullyComplete() &&
894 !(aFlags & ReflowChildFlags::NoDeleteNextInFlowChild)) {
895 nsIFrame* kidNextInFlow = aKidFrame->GetNextInFlow();
896 if (kidNextInFlow) {
897 // Remove all of the childs next-in-flows. Make sure that we ask
898 // the right parent to do the removal (it's possible that the
899 // parent is not this because we are executing pullup code)
900 nsOverflowContinuationTracker::AutoFinish fini(aTracker, aKidFrame);
901 kidNextInFlow->GetParent()->DeleteNextInFlowChild(kidNextInFlow, true);
906 // XXX temporary: hold on to a copy of the old physical version of
907 // ReflowChild so that we can convert callers incrementally.
908 void nsContainerFrame::ReflowChild(nsIFrame* aKidFrame,
909 nsPresContext* aPresContext,
910 ReflowOutput& aDesiredSize,
911 const ReflowInput& aReflowInput, nscoord aX,
912 nscoord aY, ReflowChildFlags aFlags,
913 nsReflowStatus& aStatus,
914 nsOverflowContinuationTracker* aTracker) {
915 MOZ_ASSERT(aReflowInput.mFrame == aKidFrame, "bad reflow input");
917 // Position the child frame and its view if requested.
918 if (ReflowChildFlags::NoMoveFrame !=
919 (aFlags & ReflowChildFlags::NoMoveFrame)) {
920 aKidFrame->SetPosition(nsPoint(aX, aY));
923 if (!(aFlags & ReflowChildFlags::NoMoveView)) {
924 PositionFrameView(aKidFrame);
925 PositionChildViews(aKidFrame);
928 // Reflow the child frame
929 aKidFrame->Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
931 // If the child frame is complete, delete any next-in-flows,
932 // but only if the NoDeleteNextInFlowChild flag isn't set.
933 if (aStatus.IsFullyComplete() &&
934 !(aFlags & ReflowChildFlags::NoDeleteNextInFlowChild)) {
935 nsIFrame* kidNextInFlow = aKidFrame->GetNextInFlow();
936 if (kidNextInFlow) {
937 // Remove all of the childs next-in-flows. Make sure that we ask
938 // the right parent to do the removal (it's possible that the
939 // parent is not this because we are executing pullup code)
940 nsOverflowContinuationTracker::AutoFinish fini(aTracker, aKidFrame);
941 kidNextInFlow->GetParent()->DeleteNextInFlowChild(kidNextInFlow, true);
947 * Position the views of |aFrame|'s descendants. A container frame
948 * should call this method if it moves a frame after |Reflow|.
950 void nsContainerFrame::PositionChildViews(nsIFrame* aFrame) {
951 if (!aFrame->HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW)) {
952 return;
955 // Recursively walk aFrame's child frames.
956 // Process the additional child lists, but skip the popup list as the view for
957 // popups is managed by the parent.
958 // Currently only nsMenuFrame has a popupList and during layout will adjust
959 // the view manually to position the popup.
960 for (const auto& [list, listID] : aFrame->ChildLists()) {
961 if (listID == FrameChildListID::Popup) {
962 continue;
964 for (nsIFrame* childFrame : list) {
965 // Position the frame's view (if it has one) otherwise recursively
966 // process its children
967 if (childFrame->HasView()) {
968 PositionFrameView(childFrame);
969 } else {
970 PositionChildViews(childFrame);
977 * De-optimize function to work around a VC2017 15.5+ compiler bug:
978 * https://bugzil.la/1424281#c12
980 #if defined(_MSC_VER) && !defined(__clang__) && defined(_M_AMD64)
981 # pragma optimize("g", off)
982 #endif
983 void nsContainerFrame::FinishReflowChild(
984 nsIFrame* aKidFrame, nsPresContext* aPresContext,
985 const ReflowOutput& aDesiredSize, const ReflowInput* aReflowInput,
986 const WritingMode& aWM, const LogicalPoint& aPos,
987 const nsSize& aContainerSize, nsIFrame::ReflowChildFlags aFlags) {
988 MOZ_ASSERT(!aReflowInput || aReflowInput->mFrame == aKidFrame);
989 MOZ_ASSERT(aReflowInput || aKidFrame->IsFrameOfType(eMathML) ||
990 aKidFrame->IsTableCellFrame(),
991 "aReflowInput should be passed in almost all cases");
993 if (aWM.IsPhysicalRTL()) {
994 NS_ASSERTION(aContainerSize.width != NS_UNCONSTRAINEDSIZE,
995 "FinishReflowChild with unconstrained container width!");
998 nsPoint curOrigin = aKidFrame->GetPosition();
999 const LogicalSize convertedSize = aDesiredSize.Size(aWM);
1000 LogicalPoint pos(aPos);
1002 if (aFlags & ReflowChildFlags::ApplyRelativePositioning) {
1003 MOZ_ASSERT(aReflowInput, "caller must have passed reflow input");
1004 // ApplyRelativePositioning in right-to-left writing modes needs to know
1005 // the updated frame width to set the normal position correctly.
1006 aKidFrame->SetSize(aWM, convertedSize);
1008 const LogicalMargin offsets = aReflowInput->ComputedLogicalOffsets(aWM);
1009 ReflowInput::ApplyRelativePositioning(aKidFrame, aWM, offsets, &pos,
1010 aContainerSize);
1013 if (ReflowChildFlags::NoMoveFrame !=
1014 (aFlags & ReflowChildFlags::NoMoveFrame)) {
1015 aKidFrame->SetRect(aWM, LogicalRect(aWM, pos, convertedSize),
1016 aContainerSize);
1017 } else {
1018 aKidFrame->SetSize(aWM, convertedSize);
1021 if (aKidFrame->HasView()) {
1022 nsView* view = aKidFrame->GetView();
1023 // Make sure the frame's view is properly sized and positioned and has
1024 // things like opacity correct
1025 SyncFrameViewAfterReflow(aPresContext, aKidFrame, view,
1026 aDesiredSize.InkOverflow(), aFlags);
1029 nsPoint newOrigin = aKidFrame->GetPosition();
1030 if (!(aFlags & ReflowChildFlags::NoMoveView) && curOrigin != newOrigin) {
1031 if (!aKidFrame->HasView()) {
1032 // If the frame has moved, then we need to make sure any child views are
1033 // correctly positioned
1034 PositionChildViews(aKidFrame);
1038 aKidFrame->DidReflow(aPresContext, aReflowInput);
1040 #if defined(_MSC_VER) && !defined(__clang__) && defined(_M_AMD64)
1041 # pragma optimize("", on)
1042 #endif
1044 // XXX temporary: hold on to a copy of the old physical version of
1045 // FinishReflowChild so that we can convert callers incrementally.
1046 void nsContainerFrame::FinishReflowChild(nsIFrame* aKidFrame,
1047 nsPresContext* aPresContext,
1048 const ReflowOutput& aDesiredSize,
1049 const ReflowInput* aReflowInput,
1050 nscoord aX, nscoord aY,
1051 ReflowChildFlags aFlags) {
1052 MOZ_ASSERT(!(aFlags & ReflowChildFlags::ApplyRelativePositioning),
1053 "only the logical version supports ApplyRelativePositioning "
1054 "since ApplyRelativePositioning requires the container size");
1056 nsPoint curOrigin = aKidFrame->GetPosition();
1057 nsPoint pos(aX, aY);
1058 nsSize size(aDesiredSize.PhysicalSize());
1060 if (ReflowChildFlags::NoMoveFrame !=
1061 (aFlags & ReflowChildFlags::NoMoveFrame)) {
1062 aKidFrame->SetRect(nsRect(pos, size));
1063 } else {
1064 aKidFrame->SetSize(size);
1067 if (aKidFrame->HasView()) {
1068 nsView* view = aKidFrame->GetView();
1069 // Make sure the frame's view is properly sized and positioned and has
1070 // things like opacity correct
1071 SyncFrameViewAfterReflow(aPresContext, aKidFrame, view,
1072 aDesiredSize.InkOverflow(), aFlags);
1075 if (!(aFlags & ReflowChildFlags::NoMoveView) && curOrigin != pos) {
1076 if (!aKidFrame->HasView()) {
1077 // If the frame has moved, then we need to make sure any child views are
1078 // correctly positioned
1079 PositionChildViews(aKidFrame);
1083 aKidFrame->DidReflow(aPresContext, aReflowInput);
1086 void nsContainerFrame::ReflowOverflowContainerChildren(
1087 nsPresContext* aPresContext, const ReflowInput& aReflowInput,
1088 OverflowAreas& aOverflowRects, ReflowChildFlags aFlags,
1089 nsReflowStatus& aStatus, ChildFrameMerger aMergeFunc,
1090 Maybe<nsSize> aContainerSize) {
1091 MOZ_ASSERT(aPresContext, "null pointer");
1093 nsFrameList* overflowContainers =
1094 DrainExcessOverflowContainersList(aMergeFunc);
1095 if (!overflowContainers) {
1096 return; // nothing to reflow
1099 nsOverflowContinuationTracker tracker(this, false, false);
1100 bool shouldReflowAllKids = aReflowInput.ShouldReflowAllKids();
1102 for (nsIFrame* frame : *overflowContainers) {
1103 if (frame->GetPrevInFlow()->GetParent() != GetPrevInFlow()) {
1104 // frame's prevInFlow has moved, skip reflowing this frame;
1105 // it will get reflowed once it's been placed
1106 if (GetNextInFlow()) {
1107 // We report OverflowIncomplete status in this case to avoid our parent
1108 // deleting our next-in-flows which might destroy non-empty frames.
1109 nsReflowStatus status;
1110 status.SetOverflowIncomplete();
1111 aStatus.MergeCompletionStatusFrom(status);
1113 continue;
1116 auto ScrollableOverflowExceedsAvailableBSize =
1117 [this, &aReflowInput](nsIFrame* aFrame) {
1118 if (aReflowInput.AvailableBSize() == NS_UNCONSTRAINEDSIZE) {
1119 return false;
1121 const auto parentWM = GetWritingMode();
1122 const nscoord scrollableOverflowRectBEnd =
1123 LogicalRect(parentWM,
1124 aFrame->ScrollableOverflowRectRelativeToParent(),
1125 GetSize())
1126 .BEnd(parentWM);
1127 return scrollableOverflowRectBEnd > aReflowInput.AvailableBSize();
1130 // If the available block-size has changed, or the existing scrollable
1131 // overflow's block-end exceeds it, we need to reflow even if the frame
1132 // isn't dirty.
1133 if (shouldReflowAllKids || frame->IsSubtreeDirty() ||
1134 ScrollableOverflowExceedsAvailableBSize(frame)) {
1135 // Get prev-in-flow
1136 nsIFrame* prevInFlow = frame->GetPrevInFlow();
1137 NS_ASSERTION(prevInFlow,
1138 "overflow container frame must have a prev-in-flow");
1139 NS_ASSERTION(
1140 frame->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1141 "overflow container frame must have overflow container bit set");
1142 WritingMode wm = frame->GetWritingMode();
1143 nsSize containerSize =
1144 aContainerSize ? *aContainerSize
1145 : aReflowInput.AvailableSize(wm).GetPhysicalSize(wm);
1146 LogicalRect prevRect = prevInFlow->GetLogicalRect(wm, containerSize);
1148 // Initialize reflow params
1149 LogicalSize availSpace(wm, prevRect.ISize(wm),
1150 aReflowInput.AvailableSize(wm).BSize(wm));
1151 ReflowOutput desiredSize(aReflowInput);
1153 StyleSizeOverrides sizeOverride;
1154 if (frame->IsFlexItem()) {
1155 // A flex item's size is determined by the flex algorithm, not solely by
1156 // its style. Thus, the following overrides are necessary.
1158 // Use the overflow container flex item's prev-in-flow inline-size since
1159 // this continuation's inline-size is the same.
1160 sizeOverride.mStyleISize.emplace(
1161 StyleSize::LengthPercentage(LengthPercentage::FromAppUnits(
1162 frame->StylePosition()->mBoxSizing == StyleBoxSizing::Border
1163 ? prevRect.ISize(wm)
1164 : prevInFlow->ContentSize(wm).ISize(wm))));
1166 // An overflow container's block-size must be 0.
1167 sizeOverride.mStyleBSize.emplace(
1168 StyleSize::LengthPercentage(LengthPercentage::FromAppUnits(0)));
1170 ReflowInput reflowInput(aPresContext, aReflowInput, frame, availSpace,
1171 Nothing(), {}, sizeOverride);
1173 LogicalPoint pos(wm, prevRect.IStart(wm), 0);
1174 nsReflowStatus frameStatus;
1175 ReflowChild(frame, aPresContext, desiredSize, reflowInput, wm, pos,
1176 containerSize, aFlags, frameStatus, &tracker);
1177 FinishReflowChild(frame, aPresContext, desiredSize, &reflowInput, wm, pos,
1178 containerSize, aFlags);
1180 // Handle continuations
1181 if (!frameStatus.IsFullyComplete()) {
1182 if (frame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
1183 // Abspos frames can't cause their parent to be incomplete,
1184 // only overflow incomplete.
1185 frameStatus.SetOverflowIncomplete();
1186 } else {
1187 NS_ASSERTION(frameStatus.IsComplete(),
1188 "overflow container frames can't be incomplete, only "
1189 "overflow-incomplete");
1192 // Acquire a next-in-flow, creating it if necessary
1193 nsIFrame* nif = frame->GetNextInFlow();
1194 if (!nif) {
1195 NS_ASSERTION(frameStatus.NextInFlowNeedsReflow(),
1196 "Someone forgot a NextInFlowNeedsReflow flag");
1197 nif = PresShell()->FrameConstructor()->CreateContinuingFrame(frame,
1198 this);
1199 } else if (!nif->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1200 // used to be a normal next-in-flow; steal it from the child list
1201 nif->GetParent()->StealFrame(nif);
1204 tracker.Insert(nif, frameStatus);
1206 aStatus.MergeCompletionStatusFrom(frameStatus);
1207 // At this point it would be nice to assert
1208 // !frame->GetOverflowRect().IsEmpty(), but we have some unsplittable
1209 // frames that, when taller than availableHeight will push zero-height
1210 // content into a next-in-flow.
1211 } else {
1212 tracker.Skip(frame, aStatus);
1213 if (aReflowInput.mFloatManager) {
1214 nsBlockFrame::RecoverFloatsFor(frame, *aReflowInput.mFloatManager,
1215 aReflowInput.GetWritingMode(),
1216 aReflowInput.ComputedPhysicalSize());
1219 ConsiderChildOverflow(aOverflowRects, frame);
1223 void nsContainerFrame::DisplayOverflowContainers(
1224 nsDisplayListBuilder* aBuilder, const nsDisplayListSet& aLists) {
1225 nsFrameList* overflowconts = GetOverflowContainers();
1226 if (overflowconts) {
1227 for (nsIFrame* frame : *overflowconts) {
1228 BuildDisplayListForChild(aBuilder, frame, aLists);
1233 bool nsContainerFrame::TryRemoveFrame(FrameListPropertyDescriptor aProp,
1234 nsIFrame* aChildToRemove) {
1235 nsFrameList* list = GetProperty(aProp);
1236 if (list && list->StartRemoveFrame(aChildToRemove)) {
1237 // aChildToRemove *may* have been removed from this list.
1238 if (list->IsEmpty()) {
1239 Unused << TakeProperty(aProp);
1240 list->Delete(PresShell());
1242 return true;
1244 return false;
1247 bool nsContainerFrame::MaybeStealOverflowContainerFrame(nsIFrame* aChild) {
1248 bool removed = false;
1249 if (MOZ_UNLIKELY(aChild->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER))) {
1250 // Try removing from the overflow container list.
1251 removed = TryRemoveFrame(OverflowContainersProperty(), aChild);
1252 if (!removed) {
1253 // It might be in the excess overflow container list.
1254 removed = TryRemoveFrame(ExcessOverflowContainersProperty(), aChild);
1257 return removed;
1260 void nsContainerFrame::StealFrame(nsIFrame* aChild) {
1261 #ifdef DEBUG
1262 if (!mFrames.ContainsFrame(aChild)) {
1263 nsFrameList* list = GetOverflowFrames();
1264 if (!list || !list->ContainsFrame(aChild)) {
1265 list = GetOverflowContainers();
1266 if (!list || !list->ContainsFrame(aChild)) {
1267 list = GetExcessOverflowContainers();
1268 MOZ_ASSERT(list && list->ContainsFrame(aChild),
1269 "aChild isn't our child"
1270 " or on a frame list not supported by StealFrame");
1274 #endif
1276 if (MaybeStealOverflowContainerFrame(aChild)) {
1277 return;
1280 // NOTE nsColumnSetFrame and nsCanvasFrame have their overflow containers
1281 // on the normal lists so we might get here also if the frame bit
1282 // NS_FRAME_IS_OVERFLOW_CONTAINER is set.
1283 if (mFrames.StartRemoveFrame(aChild)) {
1284 return;
1287 // We didn't find the child in our principal child list.
1288 // Maybe it's on the overflow list?
1289 nsFrameList* frameList = GetOverflowFrames();
1290 if (frameList && frameList->ContinueRemoveFrame(aChild)) {
1291 if (frameList->IsEmpty()) {
1292 DestroyOverflowList();
1294 return;
1297 MOZ_ASSERT_UNREACHABLE("StealFrame: can't find aChild");
1300 nsFrameList nsContainerFrame::StealFramesAfter(nsIFrame* aChild) {
1301 NS_ASSERTION(
1302 !aChild || !aChild->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1303 "StealFramesAfter doesn't handle overflow containers");
1304 NS_ASSERTION(!IsBlockFrame(), "unexpected call");
1306 if (!aChild) {
1307 return std::move(mFrames);
1310 for (nsIFrame* f : mFrames) {
1311 if (f == aChild) {
1312 return mFrames.TakeFramesAfter(f);
1316 // We didn't find the child in the principal child list.
1317 // Maybe it's on the overflow list?
1318 if (nsFrameList* overflowFrames = GetOverflowFrames()) {
1319 for (nsIFrame* f : *overflowFrames) {
1320 if (f == aChild) {
1321 return mFrames.TakeFramesAfter(f);
1326 NS_ERROR("StealFramesAfter: can't find aChild");
1327 return nsFrameList();
1331 * Create a next-in-flow for aFrame. Will return the newly created
1332 * frame <b>if and only if</b> a new frame is created; otherwise
1333 * nullptr is returned.
1335 nsIFrame* nsContainerFrame::CreateNextInFlow(nsIFrame* aFrame) {
1336 MOZ_ASSERT(
1337 !IsBlockFrame(),
1338 "you should have called nsBlockFrame::CreateContinuationFor instead");
1339 MOZ_ASSERT(mFrames.ContainsFrame(aFrame), "expected an in-flow child frame");
1341 nsIFrame* nextInFlow = aFrame->GetNextInFlow();
1342 if (nullptr == nextInFlow) {
1343 // Create a continuation frame for the child frame and insert it
1344 // into our child list.
1345 nextInFlow =
1346 PresShell()->FrameConstructor()->CreateContinuingFrame(aFrame, this);
1347 mFrames.InsertFrame(nullptr, aFrame, nextInFlow);
1349 NS_FRAME_LOG(NS_FRAME_TRACE_NEW_FRAMES,
1350 ("nsContainerFrame::CreateNextInFlow: frame=%p nextInFlow=%p",
1351 aFrame, nextInFlow));
1353 return nextInFlow;
1355 return nullptr;
1359 * Remove and delete aNextInFlow and its next-in-flows. Updates the sibling and
1360 * flow pointers
1362 void nsContainerFrame::DeleteNextInFlowChild(nsIFrame* aNextInFlow,
1363 bool aDeletingEmptyFrames) {
1364 #ifdef DEBUG
1365 nsIFrame* prevInFlow = aNextInFlow->GetPrevInFlow();
1366 #endif
1367 MOZ_ASSERT(prevInFlow, "bad prev-in-flow");
1369 // If the next-in-flow has a next-in-flow then delete it, too (and
1370 // delete it first).
1371 // Do this in a loop so we don't overflow the stack for frames
1372 // with very many next-in-flows
1373 nsIFrame* nextNextInFlow = aNextInFlow->GetNextInFlow();
1374 if (nextNextInFlow) {
1375 AutoTArray<nsIFrame*, 8> frames;
1376 for (nsIFrame* f = nextNextInFlow; f; f = f->GetNextInFlow()) {
1377 frames.AppendElement(f);
1379 for (nsIFrame* delFrame : Reversed(frames)) {
1380 delFrame->GetParent()->DeleteNextInFlowChild(delFrame,
1381 aDeletingEmptyFrames);
1385 // Take the next-in-flow out of the parent's child list
1386 StealFrame(aNextInFlow);
1388 #ifdef DEBUG
1389 if (aDeletingEmptyFrames) {
1390 nsLayoutUtils::AssertTreeOnlyEmptyNextInFlows(aNextInFlow);
1392 #endif
1394 // Delete the next-in-flow frame and its descendants. This will also
1395 // remove it from its next-in-flow/prev-in-flow chain.
1396 aNextInFlow->Destroy();
1398 MOZ_ASSERT(!prevInFlow->GetNextInFlow(), "non null next-in-flow");
1401 void nsContainerFrame::PushChildrenToOverflow(nsIFrame* aFromChild,
1402 nsIFrame* aPrevSibling) {
1403 MOZ_ASSERT(aFromChild, "null pointer");
1404 MOZ_ASSERT(aPrevSibling, "pushing first child");
1405 MOZ_ASSERT(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
1407 // Add the frames to our overflow list (let our next in flow drain
1408 // our overflow list when it is ready)
1409 SetOverflowFrames(mFrames.TakeFramesAfter(aPrevSibling));
1412 void nsContainerFrame::PushChildren(nsIFrame* aFromChild,
1413 nsIFrame* aPrevSibling) {
1414 MOZ_ASSERT(aFromChild, "null pointer");
1415 MOZ_ASSERT(aPrevSibling, "pushing first child");
1416 MOZ_ASSERT(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
1418 // Disconnect aFromChild from its previous sibling
1419 nsFrameList tail = mFrames.TakeFramesAfter(aPrevSibling);
1421 nsContainerFrame* nextInFlow =
1422 static_cast<nsContainerFrame*>(GetNextInFlow());
1423 if (nextInFlow) {
1424 // XXX This is not a very good thing to do. If it gets removed
1425 // then remove the copy of this routine that doesn't do this from
1426 // nsInlineFrame.
1427 // When pushing and pulling frames we need to check for whether any
1428 // views need to be reparented.
1429 for (nsIFrame* f = aFromChild; f; f = f->GetNextSibling()) {
1430 nsContainerFrame::ReparentFrameView(f, this, nextInFlow);
1432 nextInFlow->mFrames.InsertFrames(nextInFlow, nullptr, std::move(tail));
1433 } else {
1434 // Add the frames to our overflow list
1435 SetOverflowFrames(std::move(tail));
1439 bool nsContainerFrame::PushIncompleteChildren(
1440 const FrameHashtable& aPushedItems, const FrameHashtable& aIncompleteItems,
1441 const FrameHashtable& aOverflowIncompleteItems) {
1442 MOZ_ASSERT(IsFlexOrGridContainer(),
1443 "Only Grid / Flex containers can call this!");
1445 if (aPushedItems.IsEmpty() && aIncompleteItems.IsEmpty() &&
1446 aOverflowIncompleteItems.IsEmpty()) {
1447 return false;
1450 // Iterate the children in normal document order and append them (or a NIF)
1451 // to one of the following frame lists according to their status.
1452 nsFrameList pushedList;
1453 nsFrameList incompleteList;
1454 nsFrameList overflowIncompleteList;
1455 auto* fc = PresShell()->FrameConstructor();
1456 for (nsIFrame* child = PrincipalChildList().FirstChild(); child;) {
1457 MOZ_ASSERT((aPushedItems.Contains(child) ? 1 : 0) +
1458 (aIncompleteItems.Contains(child) ? 1 : 0) +
1459 (aOverflowIncompleteItems.Contains(child) ? 1 : 0) <=
1461 "child should only be in one of these sets");
1462 // Save the next-sibling so we can continue the loop if |child| is moved.
1463 nsIFrame* next = child->GetNextSibling();
1464 if (aPushedItems.Contains(child)) {
1465 MOZ_ASSERT(child->GetParent() == this);
1466 StealFrame(child);
1467 pushedList.AppendFrame(nullptr, child);
1468 } else if (aIncompleteItems.Contains(child)) {
1469 nsIFrame* childNIF = child->GetNextInFlow();
1470 if (!childNIF) {
1471 childNIF = fc->CreateContinuingFrame(child, this);
1472 incompleteList.AppendFrame(nullptr, childNIF);
1473 } else {
1474 auto* parent = childNIF->GetParent();
1475 MOZ_ASSERT(parent != this || !mFrames.ContainsFrame(childNIF),
1476 "child's NIF shouldn't be in the same principal list");
1477 // If child's existing NIF is an overflow container, convert it to an
1478 // actual NIF, since now |child| has non-overflow stuff to give it.
1479 // Or, if it's further away then our next-in-flow, then pull it up.
1480 if (childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) ||
1481 (parent != this && parent != GetNextInFlow())) {
1482 parent->StealFrame(childNIF);
1483 childNIF->RemoveStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1484 if (parent == this) {
1485 incompleteList.AppendFrame(nullptr, childNIF);
1486 } else {
1487 // If childNIF already lives on the next fragment, then we
1488 // don't need to reparent it, since we know it's destined to end
1489 // up there anyway. Just move it to its parent's overflow list.
1490 if (parent == GetNextInFlow()) {
1491 nsFrameList toMove(childNIF, childNIF);
1492 parent->MergeSortedOverflow(toMove);
1493 } else {
1494 ReparentFrame(childNIF, parent, this);
1495 incompleteList.AppendFrame(nullptr, childNIF);
1500 } else if (aOverflowIncompleteItems.Contains(child)) {
1501 nsIFrame* childNIF = child->GetNextInFlow();
1502 if (!childNIF) {
1503 childNIF = fc->CreateContinuingFrame(child, this);
1504 childNIF->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1505 overflowIncompleteList.AppendFrame(nullptr, childNIF);
1506 } else {
1507 DebugOnly<nsContainerFrame*> lastParent = this;
1508 auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1509 // If child has any non-overflow-container NIFs, convert them to
1510 // overflow containers, since that's all |child| needs now.
1511 while (childNIF &&
1512 !childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1513 auto* parent = childNIF->GetParent();
1514 parent->StealFrame(childNIF);
1515 childNIF->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1516 if (parent == this) {
1517 overflowIncompleteList.AppendFrame(nullptr, childNIF);
1518 } else {
1519 if (!nif || parent == nif) {
1520 nsFrameList toMove(childNIF, childNIF);
1521 parent->MergeSortedExcessOverflowContainers(toMove);
1522 } else {
1523 ReparentFrame(childNIF, parent, nif);
1524 nsFrameList toMove(childNIF, childNIF);
1525 nif->MergeSortedExcessOverflowContainers(toMove);
1527 // We only need to reparent the first childNIF (or not at all if
1528 // its parent is our NIF).
1529 nif = nullptr;
1531 lastParent = parent;
1532 childNIF = childNIF->GetNextInFlow();
1536 child = next;
1539 // Merge the results into our respective overflow child lists.
1540 if (!pushedList.IsEmpty()) {
1541 MergeSortedOverflow(pushedList);
1543 if (!incompleteList.IsEmpty()) {
1544 MergeSortedOverflow(incompleteList);
1546 if (!overflowIncompleteList.IsEmpty()) {
1547 // If our next-in-flow already has overflow containers list, merge the
1548 // overflowIncompleteList into that list. Otherwise, merge it into our
1549 // excess overflow containers list, to be drained by our next-in-flow.
1550 auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1551 nsFrameList* oc = nif ? nif->GetOverflowContainers() : nullptr;
1552 if (oc) {
1553 ReparentFrames(overflowIncompleteList, this, nif);
1554 MergeSortedFrameLists(*oc, overflowIncompleteList, GetContent());
1555 } else {
1556 MergeSortedExcessOverflowContainers(overflowIncompleteList);
1559 return true;
1562 void nsContainerFrame::NormalizeChildLists() {
1563 MOZ_ASSERT(IsFlexOrGridContainer(),
1564 "Only Flex / Grid containers can call this!");
1566 // Note: the following description uses grid container as an example. Flex
1567 // container is similar.
1569 // First we gather child frames we should include in our reflow/placement,
1570 // i.e. overflowed children from our prev-in-flow, and pushed first-in-flow
1571 // children (that might now fit). It's important to note that these children
1572 // can be in arbitrary order vis-a-vis the current children in our lists.
1573 // E.g. grid items in the document order: A, B, C may be placed in the rows
1574 // 3, 2, 1. Assume each row goes in a separate grid container fragment,
1575 // and we reflow the second fragment. Now if C (in fragment 1) overflows,
1576 // we can't just prepend it to our mFrames like we usually do because that
1577 // would violate the document order invariant that other code depends on.
1578 // Similarly if we pull up child A (from fragment 3) we can't just append
1579 // that for the same reason. Instead, we must sort these children into
1580 // our child lists. (The sorting is trivial given that both lists are
1581 // already fully sorted individually - it's just a merge.)
1583 // The invariants that we maintain are that each grid container child list
1584 // is sorted in the normal document order at all times, but that children
1585 // in different grid container continuations may be in arbitrary order.
1587 const auto didPushItemsBit = IsFlexContainerFrame()
1588 ? NS_STATE_FLEX_DID_PUSH_ITEMS
1589 : NS_STATE_GRID_DID_PUSH_ITEMS;
1590 const auto hasChildNifBit = IsFlexContainerFrame()
1591 ? NS_STATE_FLEX_HAS_CHILD_NIFS
1592 : NS_STATE_GRID_HAS_CHILD_NIFS;
1594 auto* prevInFlow = static_cast<nsContainerFrame*>(GetPrevInFlow());
1595 // Merge overflow frames from our prev-in-flow into our principal child list.
1596 if (prevInFlow) {
1597 AutoFrameListPtr overflow(PresContext(), prevInFlow->StealOverflowFrames());
1598 if (overflow) {
1599 ReparentFrames(*overflow, prevInFlow, this);
1600 MergeSortedFrameLists(mFrames, *overflow, GetContent());
1602 // Move trailing next-in-flows into our overflow list.
1603 nsFrameList continuations;
1604 for (nsIFrame* f = mFrames.FirstChild(); f;) {
1605 nsIFrame* next = f->GetNextSibling();
1606 nsIFrame* pif = f->GetPrevInFlow();
1607 if (pif && pif->GetParent() == this) {
1608 mFrames.RemoveFrame(f);
1609 continuations.AppendFrame(nullptr, f);
1611 f = next;
1613 MergeSortedOverflow(continuations);
1615 // Move prev-in-flow's excess overflow containers list into our own
1616 // overflow containers list. If we already have an excess overflow
1617 // containers list, any child in that list which doesn't have a
1618 // prev-in-flow in this frame is also merged into our overflow container
1619 // list.
1620 nsFrameList* overflowContainers =
1621 DrainExcessOverflowContainersList(MergeSortedFrameListsFor);
1623 // Move trailing OC next-in-flows into our excess overflow containers
1624 // list.
1625 if (overflowContainers) {
1626 nsFrameList moveToEOC;
1627 for (nsIFrame* f = overflowContainers->FirstChild(); f;) {
1628 nsIFrame* next = f->GetNextSibling();
1629 nsIFrame* pif = f->GetPrevInFlow();
1630 if (pif && pif->GetParent() == this) {
1631 overflowContainers->RemoveFrame(f);
1632 moveToEOC.AppendFrame(nullptr, f);
1634 f = next;
1636 if (overflowContainers->IsEmpty()) {
1637 DestroyOverflowContainers();
1639 MergeSortedExcessOverflowContainers(moveToEOC);
1644 // For each item in aItems, pull up its next-in-flow (if any), and reparent it
1645 // to our next-in-flow, unless its parent is already ourselves or our
1646 // next-in-flow (to avoid leaving a hole there).
1647 auto PullItemsNextInFlow = [this](const nsFrameList& aItems) {
1648 auto* firstNIF = static_cast<nsContainerFrame*>(GetNextInFlow());
1649 if (!firstNIF) {
1650 return;
1652 nsFrameList childNIFs;
1653 nsFrameList childOCNIFs;
1654 for (auto* child : aItems) {
1655 if (auto* childNIF = child->GetNextInFlow()) {
1656 if (auto* parent = childNIF->GetParent();
1657 parent != this && parent != firstNIF) {
1658 parent->StealFrame(childNIF);
1659 ReparentFrame(childNIF, parent, firstNIF);
1660 if (childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1661 childOCNIFs.AppendFrame(nullptr, childNIF);
1662 } else {
1663 childNIFs.AppendFrame(nullptr, childNIF);
1668 // Merge aItems' NIFs into our NIF's respective overflow child lists.
1669 firstNIF->MergeSortedOverflow(childNIFs);
1670 firstNIF->MergeSortedExcessOverflowContainers(childOCNIFs);
1673 // Merge our own overflow frames into our principal child list,
1674 // except those that are a next-in-flow for one of our items.
1675 DebugOnly<bool> foundOwnPushedChild = false;
1677 nsFrameList* ourOverflow = GetOverflowFrames();
1678 if (ourOverflow) {
1679 nsFrameList items;
1680 for (nsIFrame* f = ourOverflow->FirstChild(); f;) {
1681 nsIFrame* next = f->GetNextSibling();
1682 nsIFrame* pif = f->GetPrevInFlow();
1683 if (!pif || pif->GetParent() != this) {
1684 MOZ_ASSERT(f->GetParent() == this);
1685 ourOverflow->RemoveFrame(f);
1686 items.AppendFrame(nullptr, f);
1687 if (!pif) {
1688 foundOwnPushedChild = true;
1691 f = next;
1694 if (ourOverflow->IsEmpty()) {
1695 DestroyOverflowList();
1696 ourOverflow = nullptr;
1698 if (items.NotEmpty()) {
1699 PullItemsNextInFlow(items);
1701 MergeSortedFrameLists(mFrames, items, GetContent());
1705 // Push any child next-in-flows in our principal list to OverflowList.
1706 if (HasAnyStateBits(hasChildNifBit)) {
1707 nsFrameList framesToPush;
1708 nsIFrame* firstChild = mFrames.FirstChild();
1709 // Note that we potentially modify our mFrames list as we go.
1710 for (auto* child = firstChild; child; child = child->GetNextSibling()) {
1711 if (auto* childNIF = child->GetNextInFlow()) {
1712 if (childNIF->GetParent() == this) {
1713 for (auto* c = child->GetNextSibling(); c; c = c->GetNextSibling()) {
1714 if (c == childNIF) {
1715 // child's next-in-flow is in our principal child list, push it.
1716 mFrames.RemoveFrame(childNIF);
1717 framesToPush.AppendFrame(nullptr, childNIF);
1718 break;
1724 if (!framesToPush.IsEmpty()) {
1725 MergeSortedOverflow(framesToPush);
1727 RemoveStateBits(hasChildNifBit);
1730 // Pull up any first-in-flow children we might have pushed.
1731 if (HasAnyStateBits(didPushItemsBit)) {
1732 RemoveStateBits(didPushItemsBit);
1733 nsFrameList items;
1734 auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1735 DebugOnly<bool> nifNeedPushedItem = false;
1736 while (nif) {
1737 nsFrameList nifItems;
1738 for (nsIFrame* nifChild = nif->PrincipalChildList().FirstChild();
1739 nifChild;) {
1740 nsIFrame* next = nifChild->GetNextSibling();
1741 if (!nifChild->GetPrevInFlow()) {
1742 nif->StealFrame(nifChild);
1743 ReparentFrame(nifChild, nif, this);
1744 nifItems.AppendFrame(nullptr, nifChild);
1745 nifNeedPushedItem = false;
1747 nifChild = next;
1749 MergeSortedFrameLists(items, nifItems, GetContent());
1751 if (!nif->HasAnyStateBits(didPushItemsBit)) {
1752 MOZ_ASSERT(!nifNeedPushedItem || mDidPushItemsBitMayLie,
1753 "The state bit stored in didPushItemsBit lied!");
1754 break;
1756 nifNeedPushedItem = true;
1758 for (nsIFrame* nifChild =
1759 nif->GetChildList(FrameChildListID::Overflow).FirstChild();
1760 nifChild;) {
1761 nsIFrame* next = nifChild->GetNextSibling();
1762 if (!nifChild->GetPrevInFlow()) {
1763 nif->StealFrame(nifChild);
1764 ReparentFrame(nifChild, nif, this);
1765 nifItems.AppendFrame(nullptr, nifChild);
1766 nifNeedPushedItem = false;
1768 nifChild = next;
1770 MergeSortedFrameLists(items, nifItems, GetContent());
1772 nif->RemoveStateBits(didPushItemsBit);
1773 nif = static_cast<nsContainerFrame*>(nif->GetNextInFlow());
1774 MOZ_ASSERT(nif || !nifNeedPushedItem || mDidPushItemsBitMayLie,
1775 "The state bit stored in didPushItemsBit lied!");
1778 if (!items.IsEmpty()) {
1779 PullItemsNextInFlow(items);
1782 MOZ_ASSERT(
1783 foundOwnPushedChild || !items.IsEmpty() || mDidPushItemsBitMayLie,
1784 "The state bit stored in didPushItemsBit lied!");
1785 MergeSortedFrameLists(mFrames, items, GetContent());
1789 void nsContainerFrame::NoteNewChildren(ChildListID aListID,
1790 const nsFrameList& aFrameList) {
1791 MOZ_ASSERT(aListID == FrameChildListID::Principal, "unexpected child list");
1792 MOZ_ASSERT(IsFlexOrGridContainer(),
1793 "Only Flex / Grid containers can call this!");
1795 mozilla::PresShell* presShell = PresShell();
1796 const auto didPushItemsBit = IsFlexContainerFrame()
1797 ? NS_STATE_FLEX_DID_PUSH_ITEMS
1798 : NS_STATE_GRID_DID_PUSH_ITEMS;
1799 for (auto* pif = GetPrevInFlow(); pif; pif = pif->GetPrevInFlow()) {
1800 pif->AddStateBits(didPushItemsBit);
1801 presShell->FrameNeedsReflow(pif, IntrinsicDirty::FrameAndAncestors,
1802 NS_FRAME_IS_DIRTY);
1806 bool nsContainerFrame::MoveOverflowToChildList() {
1807 bool result = false;
1809 // Check for an overflow list with our prev-in-flow
1810 nsContainerFrame* prevInFlow = (nsContainerFrame*)GetPrevInFlow();
1811 if (nullptr != prevInFlow) {
1812 AutoFrameListPtr prevOverflowFrames(PresContext(),
1813 prevInFlow->StealOverflowFrames());
1814 if (prevOverflowFrames) {
1815 // Tables are special; they can have repeated header/footer
1816 // frames on mFrames at this point.
1817 NS_ASSERTION(mFrames.IsEmpty() || IsTableFrame(), "bad overflow list");
1818 // When pushing and pulling frames we need to check for whether any
1819 // views need to be reparented.
1820 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
1821 this);
1822 mFrames.AppendFrames(this, std::move(*prevOverflowFrames));
1823 result = true;
1827 // It's also possible that we have an overflow list for ourselves.
1828 return DrainSelfOverflowList() || result;
1831 void nsContainerFrame::MergeSortedOverflow(nsFrameList& aList) {
1832 if (aList.IsEmpty()) {
1833 return;
1835 MOZ_ASSERT(
1836 !aList.FirstChild()->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1837 "this is the wrong list to put this child frame");
1838 MOZ_ASSERT(aList.FirstChild()->GetParent() == this);
1839 nsFrameList* overflow = GetOverflowFrames();
1840 if (overflow) {
1841 MergeSortedFrameLists(*overflow, aList, GetContent());
1842 } else {
1843 SetOverflowFrames(std::move(aList));
1847 void nsContainerFrame::MergeSortedExcessOverflowContainers(nsFrameList& aList) {
1848 if (aList.IsEmpty()) {
1849 return;
1851 MOZ_ASSERT(
1852 aList.FirstChild()->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1853 "this is the wrong list to put this child frame");
1854 MOZ_ASSERT(aList.FirstChild()->GetParent() == this);
1855 if (nsFrameList* eoc = GetExcessOverflowContainers()) {
1856 MergeSortedFrameLists(*eoc, aList, GetContent());
1857 } else {
1858 SetExcessOverflowContainers(std::move(aList));
1862 nsIFrame* nsContainerFrame::GetFirstNonAnonBoxInSubtree(nsIFrame* aFrame) {
1863 while (aFrame) {
1864 // If aFrame isn't an anonymous container, or it's text or such, then it'll
1865 // do.
1866 if (!aFrame->Style()->IsAnonBox() ||
1867 nsCSSAnonBoxes::IsNonElement(aFrame->Style()->GetPseudoType())) {
1868 break;
1871 // Otherwise, descend to its first child and repeat.
1873 // SPECIAL CASE: if we're dealing with an anonymous table, then it might
1874 // be wrapping something non-anonymous in its caption or col-group lists
1875 // (instead of its principal child list), so we have to look there.
1876 // (Note: For anonymous tables that have a non-anon cell *and* a non-anon
1877 // column, we'll always return the column. This is fine; we're really just
1878 // looking for a handle to *anything* with a meaningful content node inside
1879 // the table, for use in DOM comparisons to things outside of the table.)
1880 if (MOZ_UNLIKELY(aFrame->IsTableWrapperFrame())) {
1881 nsIFrame* captionDescendant = GetFirstNonAnonBoxInSubtree(
1882 aFrame->GetChildList(FrameChildListID::Caption).FirstChild());
1883 if (captionDescendant) {
1884 return captionDescendant;
1886 } else if (MOZ_UNLIKELY(aFrame->IsTableFrame())) {
1887 nsIFrame* colgroupDescendant = GetFirstNonAnonBoxInSubtree(
1888 aFrame->GetChildList(FrameChildListID::ColGroup).FirstChild());
1889 if (colgroupDescendant) {
1890 return colgroupDescendant;
1894 // USUAL CASE: Descend to the first child in principal list.
1895 aFrame = aFrame->PrincipalChildList().FirstChild();
1897 return aFrame;
1901 * Is aFrame1 a prev-continuation of aFrame2?
1903 static bool IsPrevContinuationOf(nsIFrame* aFrame1, nsIFrame* aFrame2) {
1904 nsIFrame* prev = aFrame2;
1905 while ((prev = prev->GetPrevContinuation())) {
1906 if (prev == aFrame1) {
1907 return true;
1910 return false;
1913 void nsContainerFrame::MergeSortedFrameLists(nsFrameList& aDest,
1914 nsFrameList& aSrc,
1915 nsIContent* aCommonAncestor) {
1916 // Returns a frame whose DOM node can be used for the purpose of ordering
1917 // aFrame among its sibling frames by DOM position. If aFrame is
1918 // non-anonymous, this just returns aFrame itself. Otherwise, this returns the
1919 // first non-anonymous descendant in aFrame's continuation chain.
1920 auto FrameForDOMPositionComparison = [](nsIFrame* aFrame) {
1921 if (!aFrame->Style()->IsAnonBox()) {
1922 // The usual case.
1923 return aFrame;
1926 // Walk the continuation chain from the start, and return the first
1927 // non-anonymous descendant that we find.
1928 for (nsIFrame* f = aFrame->FirstContinuation(); f;
1929 f = f->GetNextContinuation()) {
1930 if (nsIFrame* nonAnonBox = GetFirstNonAnonBoxInSubtree(f)) {
1931 return nonAnonBox;
1935 MOZ_ASSERT_UNREACHABLE(
1936 "Why is there no non-anonymous descendants in the continuation chain?");
1937 return aFrame;
1940 nsIFrame* dest = aDest.FirstChild();
1941 for (nsIFrame* src = aSrc.FirstChild(); src;) {
1942 if (!dest) {
1943 aDest.AppendFrames(nullptr, std::move(aSrc));
1944 break;
1946 nsIContent* srcContent = FrameForDOMPositionComparison(src)->GetContent();
1947 nsIContent* destContent = FrameForDOMPositionComparison(dest)->GetContent();
1948 int32_t result = nsLayoutUtils::CompareTreePosition(srcContent, destContent,
1949 aCommonAncestor);
1950 if (MOZ_UNLIKELY(result == 0)) {
1951 // NOTE: we get here when comparing ::before/::after for the same element.
1952 if (MOZ_UNLIKELY(srcContent->IsGeneratedContentContainerForBefore())) {
1953 if (MOZ_LIKELY(!destContent->IsGeneratedContentContainerForBefore()) ||
1954 ::IsPrevContinuationOf(src, dest)) {
1955 result = -1;
1957 } else if (MOZ_UNLIKELY(
1958 srcContent->IsGeneratedContentContainerForAfter())) {
1959 if (MOZ_UNLIKELY(destContent->IsGeneratedContentContainerForAfter()) &&
1960 ::IsPrevContinuationOf(src, dest)) {
1961 result = -1;
1963 } else if (::IsPrevContinuationOf(src, dest)) {
1964 result = -1;
1967 if (result < 0) {
1968 // src should come before dest
1969 nsIFrame* next = src->GetNextSibling();
1970 aSrc.RemoveFrame(src);
1971 aDest.InsertFrame(nullptr, dest->GetPrevSibling(), src);
1972 src = next;
1973 } else {
1974 dest = dest->GetNextSibling();
1977 MOZ_ASSERT(aSrc.IsEmpty());
1980 bool nsContainerFrame::MoveInlineOverflowToChildList(nsIFrame* aLineContainer) {
1981 MOZ_ASSERT(aLineContainer,
1982 "Must have line container for moving inline overflows");
1984 bool result = false;
1986 // Check for an overflow list with our prev-in-flow
1987 if (auto prevInFlow = static_cast<nsContainerFrame*>(GetPrevInFlow())) {
1988 AutoFrameListPtr prevOverflowFrames(PresContext(),
1989 prevInFlow->StealOverflowFrames());
1990 if (prevOverflowFrames) {
1991 // We may need to reparent floats from prev-in-flow to our line
1992 // container if the container has prev continuation.
1993 if (aLineContainer->GetPrevContinuation()) {
1994 ReparentFloatsForInlineChild(aLineContainer,
1995 prevOverflowFrames->FirstChild(), true);
1997 // When pushing and pulling frames we need to check for whether
1998 // any views need to be reparented.
1999 nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
2000 this);
2001 // Prepend overflow frames to the list.
2002 mFrames.InsertFrames(this, nullptr, std::move(*prevOverflowFrames));
2003 result = true;
2007 // It's also possible that we have overflow list for ourselves.
2008 return DrainSelfOverflowList() || result;
2011 bool nsContainerFrame::DrainSelfOverflowList() {
2012 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
2013 if (overflowFrames) {
2014 mFrames.AppendFrames(nullptr, std::move(*overflowFrames));
2015 return true;
2017 return false;
2020 bool nsContainerFrame::DrainAndMergeSelfOverflowList() {
2021 MOZ_ASSERT(IsFlexOrGridContainer(),
2022 "Only Flex / Grid containers can call this!");
2024 // Unlike nsContainerFrame::DrainSelfOverflowList, flex or grid containers
2025 // need to merge these lists so that the resulting mFrames is in document
2026 // content order.
2027 // NOTE: nsContainerFrame::AppendFrames/InsertFrames calls this method and
2028 // there are also direct calls from the fctor (FindAppendPrevSibling).
2029 AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
2030 if (overflowFrames) {
2031 MergeSortedFrameLists(mFrames, *overflowFrames, GetContent());
2032 // We set a frame bit to push them again in Reflow() to avoid creating
2033 // multiple flex / grid items per flex / grid container fragment for the
2034 // same content.
2035 AddStateBits(IsFlexContainerFrame() ? NS_STATE_FLEX_HAS_CHILD_NIFS
2036 : NS_STATE_GRID_HAS_CHILD_NIFS);
2037 return true;
2039 return false;
2042 nsFrameList* nsContainerFrame::DrainExcessOverflowContainersList(
2043 ChildFrameMerger aMergeFunc) {
2044 nsFrameList* overflowContainers = GetOverflowContainers();
2046 // Drain excess overflow containers from our prev-in-flow.
2047 if (auto* prev = static_cast<nsContainerFrame*>(GetPrevInFlow())) {
2048 AutoFrameListPtr excessFrames(PresContext(),
2049 prev->StealExcessOverflowContainers());
2050 if (excessFrames) {
2051 excessFrames->ApplySetParent(this);
2052 nsContainerFrame::ReparentFrameViewList(*excessFrames, prev, this);
2053 if (overflowContainers) {
2054 // The default merge function is AppendFrames, so we use excessFrames as
2055 // the destination and then assign the result to overflowContainers.
2056 aMergeFunc(*excessFrames, *overflowContainers, this);
2057 *overflowContainers = std::move(*excessFrames);
2058 } else {
2059 overflowContainers = SetOverflowContainers(std::move(*excessFrames));
2064 // Our own excess overflow containers from a previous reflow can still be
2065 // present if our next-in-flow hasn't been reflown yet. Move any children
2066 // from it that don't have a continuation in this frame to the
2067 // OverflowContainers list.
2068 AutoFrameListPtr selfExcessOCFrames(PresContext(),
2069 StealExcessOverflowContainers());
2070 if (selfExcessOCFrames) {
2071 nsFrameList toMove;
2072 auto child = selfExcessOCFrames->FirstChild();
2073 while (child) {
2074 auto next = child->GetNextSibling();
2075 MOZ_ASSERT(child->GetPrevInFlow(),
2076 "ExcessOverflowContainers frames must be continuations");
2077 if (child->GetPrevInFlow()->GetParent() != this) {
2078 selfExcessOCFrames->RemoveFrame(child);
2079 toMove.AppendFrame(nullptr, child);
2081 child = next;
2084 // If there's any remaining excess overflow containers, put them back.
2085 if (selfExcessOCFrames->NotEmpty()) {
2086 SetExcessOverflowContainers(std::move(*selfExcessOCFrames));
2089 if (toMove.NotEmpty()) {
2090 if (overflowContainers) {
2091 aMergeFunc(*overflowContainers, toMove, this);
2092 } else {
2093 overflowContainers = SetOverflowContainers(std::move(toMove));
2098 return overflowContainers;
2101 nsIFrame* nsContainerFrame::GetNextInFlowChild(
2102 ContinuationTraversingState& aState, bool* aIsInOverflow) {
2103 nsContainerFrame*& nextInFlow = aState.mNextInFlow;
2104 while (nextInFlow) {
2105 // See if there is any frame in the container
2106 nsIFrame* frame = nextInFlow->mFrames.FirstChild();
2107 if (frame) {
2108 if (aIsInOverflow) {
2109 *aIsInOverflow = false;
2111 return frame;
2113 // No frames in the principal list, try its overflow list
2114 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
2115 if (overflowFrames) {
2116 if (aIsInOverflow) {
2117 *aIsInOverflow = true;
2119 return overflowFrames->FirstChild();
2121 nextInFlow = static_cast<nsContainerFrame*>(nextInFlow->GetNextInFlow());
2123 return nullptr;
2126 nsIFrame* nsContainerFrame::PullNextInFlowChild(
2127 ContinuationTraversingState& aState) {
2128 bool isInOverflow;
2129 nsIFrame* frame = GetNextInFlowChild(aState, &isInOverflow);
2130 if (frame) {
2131 nsContainerFrame* nextInFlow = aState.mNextInFlow;
2132 if (isInOverflow) {
2133 nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
2134 overflowFrames->RemoveFirstChild();
2135 if (overflowFrames->IsEmpty()) {
2136 nextInFlow->DestroyOverflowList();
2138 } else {
2139 nextInFlow->mFrames.RemoveFirstChild();
2142 // Move the frame to the principal frame list of this container
2143 mFrames.AppendFrame(this, frame);
2144 // AppendFrame has reparented the frame, we need
2145 // to reparent the frame view then.
2146 nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
2148 return frame;
2151 /* static */
2152 void nsContainerFrame::ReparentFloatsForInlineChild(nsIFrame* aOurLineContainer,
2153 nsIFrame* aFrame,
2154 bool aReparentSiblings) {
2155 // XXXbz this would be better if it took a nsFrameList or a frame
2156 // list slice....
2157 NS_ASSERTION(aOurLineContainer->GetNextContinuation() ||
2158 aOurLineContainer->GetPrevContinuation(),
2159 "Don't call this when we have no continuation, it's a waste");
2160 if (!aFrame) {
2161 NS_ASSERTION(aReparentSiblings, "Why did we get called?");
2162 return;
2165 nsBlockFrame* frameBlock = nsLayoutUtils::GetFloatContainingBlock(aFrame);
2166 if (!frameBlock || frameBlock == aOurLineContainer) {
2167 return;
2170 nsBlockFrame* ourBlock = do_QueryFrame(aOurLineContainer);
2171 NS_ASSERTION(ourBlock, "Not a block, but broke vertically?");
2173 while (true) {
2174 ourBlock->ReparentFloats(aFrame, frameBlock, false);
2176 if (!aReparentSiblings) return;
2177 nsIFrame* next = aFrame->GetNextSibling();
2178 if (!next) return;
2179 if (next->GetParent() == aFrame->GetParent()) {
2180 aFrame = next;
2181 continue;
2183 // This is paranoid and will hardly ever get hit ... but we can't actually
2184 // trust that the frames in the sibling chain all have the same parent,
2185 // because lazy reparenting may be going on. If we find a different
2186 // parent we need to redo our analysis.
2187 ReparentFloatsForInlineChild(aOurLineContainer, next, aReparentSiblings);
2188 return;
2192 bool nsContainerFrame::ResolvedOrientationIsVertical() {
2193 StyleOrient orient = StyleDisplay()->mOrient;
2194 switch (orient) {
2195 case StyleOrient::Horizontal:
2196 return false;
2197 case StyleOrient::Vertical:
2198 return true;
2199 case StyleOrient::Inline:
2200 return GetWritingMode().IsVertical();
2201 case StyleOrient::Block:
2202 return !GetWritingMode().IsVertical();
2204 MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
2205 return false;
2208 LogicalSize nsContainerFrame::ComputeSizeWithIntrinsicDimensions(
2209 gfxContext* aRenderingContext, WritingMode aWM,
2210 const IntrinsicSize& aIntrinsicSize, const AspectRatio& aAspectRatio,
2211 const LogicalSize& aCBSize, const LogicalSize& aMargin,
2212 const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
2213 ComputeSizeFlags aFlags) {
2214 const nsStylePosition* stylePos = StylePosition();
2215 const auto& styleISize = aSizeOverrides.mStyleISize
2216 ? *aSizeOverrides.mStyleISize
2217 : stylePos->ISize(aWM);
2218 const auto& styleBSize = aSizeOverrides.mStyleBSize
2219 ? *aSizeOverrides.mStyleBSize
2220 : stylePos->BSize(aWM);
2221 const auto& aspectRatio =
2222 aSizeOverrides.mAspectRatio ? *aSizeOverrides.mAspectRatio : aAspectRatio;
2224 auto* parentFrame = GetParent();
2225 const bool isGridItem = IsGridItem();
2226 const bool isFlexItem =
2227 IsFlexItem() && !parentFrame->HasAnyStateBits(
2228 NS_STATE_FLEX_IS_EMULATING_LEGACY_WEBKIT_BOX);
2229 // This variable only gets meaningfully set if isFlexItem is true. It
2230 // indicates which axis (in this frame's own WM) corresponds to its
2231 // flex container's main axis.
2232 LogicalAxis flexMainAxis = eLogicalAxisBlock;
2233 if (isFlexItem && nsFlexContainerFrame::IsItemInlineAxisMainAxis(this)) {
2234 flexMainAxis = eLogicalAxisInline;
2237 // Handle intrinsic sizes and their interaction with
2238 // {min-,max-,}{width,height} according to the rules in
2239 // https://www.w3.org/TR/CSS22/visudet.html#min-max-widths and
2240 // https://drafts.csswg.org/css-sizing-3/#intrinsic-sizes
2242 // Note: throughout the following section of the function, I avoid
2243 // a * (b / c) because of its reduced accuracy relative to a * b / c
2244 // or (a * b) / c (which are equivalent).
2246 const bool isAutoOrMaxContentISize =
2247 styleISize.IsAuto() || styleISize.IsMaxContent();
2248 const bool isAutoBSize =
2249 nsLayoutUtils::IsAutoBSize(styleBSize, aCBSize.BSize(aWM));
2251 const auto boxSizingAdjust = stylePos->mBoxSizing == StyleBoxSizing::Border
2252 ? aBorderPadding
2253 : LogicalSize(aWM);
2254 const nscoord boxSizingToMarginEdgeISize = aMargin.ISize(aWM) +
2255 aBorderPadding.ISize(aWM) -
2256 boxSizingAdjust.ISize(aWM);
2258 nscoord iSize, minISize, maxISize, bSize, minBSize, maxBSize;
2259 enum class Stretch {
2260 // stretch to fill the CB (preserving intrinsic ratio) in the relevant axis
2261 StretchPreservingRatio,
2262 // stretch to fill the CB in the relevant axis
2263 Stretch,
2264 // no stretching in the relevant axis
2265 NoStretch,
2267 // just to avoid having to type these out everywhere:
2268 const auto eStretchPreservingRatio = Stretch::StretchPreservingRatio;
2269 const auto eStretch = Stretch::Stretch;
2270 const auto eNoStretch = Stretch::NoStretch;
2272 Stretch stretchI = eNoStretch; // stretch behavior in the inline axis
2273 Stretch stretchB = eNoStretch; // stretch behavior in the block axis
2275 const bool isOrthogonal = aWM.IsOrthogonalTo(parentFrame->GetWritingMode());
2276 const bool isVertical = aWM.IsVertical();
2277 const LogicalSize fallbackIntrinsicSize(aWM, kFallbackIntrinsicSize);
2278 const auto& isizeCoord =
2279 isVertical ? aIntrinsicSize.height : aIntrinsicSize.width;
2280 const bool hasIntrinsicISize = isizeCoord.isSome();
2281 nscoord intrinsicISize = std::max(0, isizeCoord.valueOr(0));
2283 const auto& bsizeCoord =
2284 isVertical ? aIntrinsicSize.width : aIntrinsicSize.height;
2285 const bool hasIntrinsicBSize = bsizeCoord.isSome();
2286 nscoord intrinsicBSize = std::max(0, bsizeCoord.valueOr(0));
2288 if (!isAutoOrMaxContentISize) {
2289 iSize = ComputeISizeValue(aRenderingContext, aWM, aCBSize, boxSizingAdjust,
2290 boxSizingToMarginEdgeISize, styleISize,
2291 aSizeOverrides, aFlags)
2292 .mISize;
2293 } else if (MOZ_UNLIKELY(isGridItem) &&
2294 !parentFrame->IsMasonry(isOrthogonal ? eLogicalAxisBlock
2295 : eLogicalAxisInline)) {
2296 MOZ_ASSERT(!IsTrueOverflowContainer());
2297 // 'auto' inline-size for grid-level box - apply 'stretch' as needed:
2298 auto cbSize = aCBSize.ISize(aWM);
2299 if (cbSize != NS_UNCONSTRAINEDSIZE) {
2300 if (!StyleMargin()->HasInlineAxisAuto(aWM)) {
2301 auto inlineAxisAlignment =
2302 isOrthogonal ? stylePos->UsedAlignSelf(GetParent()->Style())._0
2303 : stylePos->UsedJustifySelf(GetParent()->Style())._0;
2304 if (inlineAxisAlignment == StyleAlignFlags::STRETCH) {
2305 stretchI = eStretch;
2308 if (stretchI != eNoStretch ||
2309 aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize)) {
2310 iSize = std::max(nscoord(0), cbSize - aBorderPadding.ISize(aWM) -
2311 aMargin.ISize(aWM));
2313 } else {
2314 // Reset this flag to avoid applying the clamping below.
2315 aFlags -= ComputeSizeFlag::IClampMarginBoxMinSize;
2319 const auto& maxISizeCoord = stylePos->MaxISize(aWM);
2321 if (!maxISizeCoord.IsNone() &&
2322 !(isFlexItem && flexMainAxis == eLogicalAxisInline)) {
2323 maxISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
2324 boxSizingAdjust, boxSizingToMarginEdgeISize,
2325 maxISizeCoord, aSizeOverrides, aFlags)
2326 .mISize;
2327 } else {
2328 maxISize = nscoord_MAX;
2331 // NOTE: Flex items ignore their min & max sizing properties in their
2332 // flex container's main-axis. (Those properties get applied later in
2333 // the flexbox algorithm.)
2335 const auto& minISizeCoord = stylePos->MinISize(aWM);
2337 if (!minISizeCoord.IsAuto() &&
2338 !(isFlexItem && flexMainAxis == eLogicalAxisInline)) {
2339 minISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
2340 boxSizingAdjust, boxSizingToMarginEdgeISize,
2341 minISizeCoord, aSizeOverrides, aFlags)
2342 .mISize;
2343 } else {
2344 // Treat "min-width: auto" as 0.
2345 // NOTE: Technically, "auto" is supposed to behave like "min-content" on
2346 // flex items. However, we don't need to worry about that here, because
2347 // flex items' min-sizes are intentionally ignored until the flex
2348 // container explicitly considers them during space distribution.
2349 minISize = 0;
2352 if (!isAutoBSize) {
2353 bSize = nsLayoutUtils::ComputeBSizeValue(aCBSize.BSize(aWM),
2354 boxSizingAdjust.BSize(aWM),
2355 styleBSize.AsLengthPercentage());
2356 } else if (MOZ_UNLIKELY(isGridItem) &&
2357 !parentFrame->IsMasonry(isOrthogonal ? eLogicalAxisInline
2358 : eLogicalAxisBlock)) {
2359 MOZ_ASSERT(!IsTrueOverflowContainer());
2360 // 'auto' block-size for grid-level box - apply 'stretch' as needed:
2361 auto cbSize = aCBSize.BSize(aWM);
2362 if (cbSize != NS_UNCONSTRAINEDSIZE) {
2363 if (!StyleMargin()->HasBlockAxisAuto(aWM)) {
2364 auto blockAxisAlignment =
2365 !isOrthogonal ? stylePos->UsedAlignSelf(GetParent()->Style())._0
2366 : stylePos->UsedJustifySelf(GetParent()->Style())._0;
2367 if (blockAxisAlignment == StyleAlignFlags::STRETCH) {
2368 stretchB = eStretch;
2371 if (stretchB != eNoStretch ||
2372 aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize)) {
2373 bSize = std::max(nscoord(0), cbSize - aBorderPadding.BSize(aWM) -
2374 aMargin.BSize(aWM));
2376 } else {
2377 // Reset this flag to avoid applying the clamping below.
2378 aFlags -= ComputeSizeFlag::BClampMarginBoxMinSize;
2382 const auto& maxBSizeCoord = stylePos->MaxBSize(aWM);
2384 if (!nsLayoutUtils::IsAutoBSize(maxBSizeCoord, aCBSize.BSize(aWM)) &&
2385 !(isFlexItem && flexMainAxis == eLogicalAxisBlock)) {
2386 maxBSize = nsLayoutUtils::ComputeBSizeValue(
2387 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
2388 maxBSizeCoord.AsLengthPercentage());
2389 } else {
2390 maxBSize = nscoord_MAX;
2393 const auto& minBSizeCoord = stylePos->MinBSize(aWM);
2395 if (!nsLayoutUtils::IsAutoBSize(minBSizeCoord, aCBSize.BSize(aWM)) &&
2396 !(isFlexItem && flexMainAxis == eLogicalAxisBlock)) {
2397 minBSize = nsLayoutUtils::ComputeBSizeValue(
2398 aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
2399 minBSizeCoord.AsLengthPercentage());
2400 } else {
2401 minBSize = 0;
2404 NS_ASSERTION(aCBSize.ISize(aWM) != NS_UNCONSTRAINEDSIZE,
2405 "Our containing block must not have unconstrained inline-size!");
2407 // Now calculate the used values for iSize and bSize:
2408 if (isAutoOrMaxContentISize) {
2409 if (isAutoBSize) {
2410 // 'auto' iSize, 'auto' bSize
2412 // Get tentative values - CSS 2.1 sections 10.3.2 and 10.6.2:
2414 nscoord tentISize, tentBSize;
2416 if (hasIntrinsicISize) {
2417 tentISize = intrinsicISize;
2418 } else if (hasIntrinsicBSize && aspectRatio) {
2419 tentISize = aspectRatio.ComputeRatioDependentSize(
2420 LogicalAxis::eLogicalAxisInline, aWM, intrinsicBSize,
2421 boxSizingAdjust);
2422 } else if (aspectRatio) {
2423 tentISize =
2424 aCBSize.ISize(aWM) - boxSizingToMarginEdgeISize; // XXX scrollbar?
2425 if (tentISize < 0) {
2426 tentISize = 0;
2428 } else {
2429 tentISize = fallbackIntrinsicSize.ISize(aWM);
2432 // If we need to clamp the inline size to fit the CB, we use the 'stretch'
2433 // or 'normal' codepath. We use the ratio-preserving 'normal' codepath
2434 // unless we have 'stretch' in the other axis.
2435 if (aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize) &&
2436 stretchI != eStretch && tentISize > iSize) {
2437 stretchI = (stretchB == eStretch ? eStretch : eStretchPreservingRatio);
2440 if (hasIntrinsicBSize) {
2441 tentBSize = intrinsicBSize;
2442 } else if (aspectRatio) {
2443 tentBSize = aspectRatio.ComputeRatioDependentSize(
2444 LogicalAxis::eLogicalAxisBlock, aWM, tentISize, boxSizingAdjust);
2445 } else {
2446 tentBSize = fallbackIntrinsicSize.BSize(aWM);
2449 // (ditto the comment about clamping the inline size above)
2450 if (aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize) &&
2451 stretchB != eStretch && tentBSize > bSize) {
2452 stretchB = (stretchI == eStretch ? eStretch : eStretchPreservingRatio);
2455 if (stretchI == eStretch) {
2456 tentISize = iSize; // * / 'stretch'
2457 if (stretchB == eStretch) {
2458 tentBSize = bSize; // 'stretch' / 'stretch'
2459 } else if (stretchB == eStretchPreservingRatio && aspectRatio) {
2460 // 'normal' / 'stretch'
2461 tentBSize = aspectRatio.ComputeRatioDependentSize(
2462 LogicalAxis::eLogicalAxisBlock, aWM, iSize, boxSizingAdjust);
2464 } else if (stretchB == eStretch) {
2465 tentBSize = bSize; // 'stretch' / * (except 'stretch')
2466 if (stretchI == eStretchPreservingRatio && aspectRatio) {
2467 // 'stretch' / 'normal'
2468 tentISize = aspectRatio.ComputeRatioDependentSize(
2469 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2471 } else if (stretchI == eStretchPreservingRatio && aspectRatio) {
2472 tentISize = iSize; // * (except 'stretch') / 'normal'
2473 tentBSize = aspectRatio.ComputeRatioDependentSize(
2474 LogicalAxis::eLogicalAxisBlock, aWM, iSize, boxSizingAdjust);
2475 if (stretchB == eStretchPreservingRatio && tentBSize > bSize) {
2476 // Stretch within the CB size with preserved intrinsic ratio.
2477 tentBSize = bSize; // 'normal' / 'normal'
2478 tentISize = aspectRatio.ComputeRatioDependentSize(
2479 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2481 } else if (stretchB == eStretchPreservingRatio && aspectRatio) {
2482 tentBSize = bSize; // 'normal' / * (except 'normal' and 'stretch')
2483 tentISize = aspectRatio.ComputeRatioDependentSize(
2484 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2487 // ComputeAutoSizeWithIntrinsicDimensions preserves the ratio when
2488 // applying the min/max-size. We don't want that when we have 'stretch'
2489 // in either axis because tentISize/tentBSize is likely not according to
2490 // ratio now.
2491 if (aspectRatio && stretchI != eStretch && stretchB != eStretch) {
2492 nsSize autoSize = nsLayoutUtils::ComputeAutoSizeWithIntrinsicDimensions(
2493 minISize, minBSize, maxISize, maxBSize, tentISize, tentBSize);
2494 // The nsSize that ComputeAutoSizeWithIntrinsicDimensions returns will
2495 // actually contain logical values if the parameters passed to it were
2496 // logical coordinates, so we do NOT perform a physical-to-logical
2497 // conversion here, but just assign the fields directly to our result.
2498 iSize = autoSize.width;
2499 bSize = autoSize.height;
2500 } else {
2501 // Not honoring an intrinsic ratio: clamp the dimensions independently.
2502 iSize = NS_CSS_MINMAX(tentISize, minISize, maxISize);
2503 bSize = NS_CSS_MINMAX(tentBSize, minBSize, maxBSize);
2505 } else {
2506 // 'auto' iSize, non-'auto' bSize
2507 bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2508 if (stretchI != eStretch) {
2509 if (aspectRatio) {
2510 iSize = aspectRatio.ComputeRatioDependentSize(
2511 LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2512 } else if (hasIntrinsicISize) {
2513 if (!(aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize) &&
2514 intrinsicISize > iSize)) {
2515 iSize = intrinsicISize;
2516 } // else - leave iSize as is to fill the CB
2517 } else {
2518 iSize = fallbackIntrinsicSize.ISize(aWM);
2520 } // else - leave iSize as is to fill the CB
2521 iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2523 } else {
2524 if (isAutoBSize) {
2525 // non-'auto' iSize, 'auto' bSize
2526 iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2527 if (stretchB != eStretch) {
2528 if (aspectRatio) {
2529 bSize = aspectRatio.ComputeRatioDependentSize(
2530 LogicalAxis::eLogicalAxisBlock, aWM, iSize, boxSizingAdjust);
2531 } else if (hasIntrinsicBSize) {
2532 if (!(aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize) &&
2533 intrinsicBSize > bSize)) {
2534 bSize = intrinsicBSize;
2535 } // else - leave bSize as is to fill the CB
2536 } else {
2537 bSize = fallbackIntrinsicSize.BSize(aWM);
2539 } // else - leave bSize as is to fill the CB
2540 bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2542 } else {
2543 // non-'auto' iSize, non-'auto' bSize
2544 iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2545 bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2549 return LogicalSize(aWM, iSize, bSize);
2552 nsRect nsContainerFrame::ComputeSimpleTightBounds(
2553 DrawTarget* aDrawTarget) const {
2554 if (StyleOutline()->ShouldPaintOutline() || StyleBorder()->HasBorder() ||
2555 !StyleBackground()->IsTransparent(this) ||
2556 StyleDisplay()->HasAppearance()) {
2557 // Not necessarily tight, due to clipping, negative
2558 // outline-offset, and lots of other issues, but that's OK
2559 return InkOverflowRect();
2562 nsRect r(0, 0, 0, 0);
2563 for (const auto& childLists : ChildLists()) {
2564 for (nsIFrame* child : childLists.mList) {
2565 r.UnionRect(
2566 r, child->ComputeTightBounds(aDrawTarget) + child->GetPosition());
2569 return r;
2572 void nsContainerFrame::PushDirtyBitToAbsoluteFrames() {
2573 if (!HasAnyStateBits(NS_FRAME_IS_DIRTY)) {
2574 return; // No dirty bit to push.
2576 if (!HasAbsolutelyPositionedChildren()) {
2577 return; // No absolute children to push to.
2579 GetAbsoluteContainingBlock()->MarkAllFramesDirty();
2582 // Define the MAX_FRAME_DEPTH to be the ContentSink's MAX_REFLOW_DEPTH plus
2583 // 4 for the frames above the document's frames:
2584 // the Viewport, GFXScroll, ScrollPort, and Canvas
2585 #define MAX_FRAME_DEPTH (MAX_REFLOW_DEPTH + 4)
2587 bool nsContainerFrame::IsFrameTreeTooDeep(const ReflowInput& aReflowInput,
2588 ReflowOutput& aMetrics,
2589 nsReflowStatus& aStatus) {
2590 if (aReflowInput.mReflowDepth > MAX_FRAME_DEPTH) {
2591 NS_WARNING("frame tree too deep; setting zero size and returning");
2592 AddStateBits(NS_FRAME_TOO_DEEP_IN_FRAME_TREE);
2593 ClearOverflowRects();
2594 aMetrics.ClearSize();
2595 aMetrics.SetBlockStartAscent(0);
2596 aMetrics.mCarriedOutBEndMargin.Zero();
2597 aMetrics.mOverflowAreas.Clear();
2599 aStatus.Reset();
2600 if (GetNextInFlow()) {
2601 // Reflow depth might vary between reflows, so we might have
2602 // successfully reflowed and split this frame before. If so, we
2603 // shouldn't delete its continuations.
2604 aStatus.SetIncomplete();
2607 return true;
2609 RemoveStateBits(NS_FRAME_TOO_DEEP_IN_FRAME_TREE);
2610 return false;
2613 bool nsContainerFrame::ShouldAvoidBreakInside(
2614 const ReflowInput& aReflowInput) const {
2615 MOZ_ASSERT(this == aReflowInput.mFrame,
2616 "Caller should pass a ReflowInput for this frame!");
2618 const auto* disp = StyleDisplay();
2619 const bool mayAvoidBreak = [&] {
2620 switch (disp->mBreakInside) {
2621 case StyleBreakWithin::Auto:
2622 return false;
2623 case StyleBreakWithin::Avoid:
2624 return true;
2625 case StyleBreakWithin::AvoidPage:
2626 return aReflowInput.mBreakType == ReflowInput::BreakType::Page;
2627 case StyleBreakWithin::AvoidColumn:
2628 return aReflowInput.mBreakType == ReflowInput::BreakType::Column;
2630 MOZ_ASSERT_UNREACHABLE("Unknown break-inside value");
2631 return false;
2632 }();
2634 if (!mayAvoidBreak) {
2635 return false;
2637 if (aReflowInput.mFlags.mIsTopOfPage) {
2638 return false;
2640 if (IsAbsolutelyPositioned(disp)) {
2641 return false;
2643 if (GetPrevInFlow()) {
2644 return false;
2646 return true;
2649 void nsContainerFrame::ConsiderChildOverflow(OverflowAreas& aOverflowAreas,
2650 nsIFrame* aChildFrame) {
2651 if (StyleDisplay()->IsContainLayout() &&
2652 IsFrameOfType(eSupportsContainLayoutAndPaint)) {
2653 // If we have layout containment and are not a non-atomic, inline-level
2654 // principal box, we should only consider our child's ink overflow,
2655 // leaving the scrollable regions of the parent unaffected.
2656 // Note: scrollable overflow is a subset of ink overflow,
2657 // so this has the same affect as unioning the child's ink and
2658 // scrollable overflow with the parent's ink overflow.
2659 const OverflowAreas childOverflows(aChildFrame->InkOverflowRect(),
2660 nsRect());
2661 aOverflowAreas.UnionWith(childOverflows + aChildFrame->GetPosition());
2662 } else {
2663 aOverflowAreas.UnionWith(
2664 aChildFrame->GetActualAndNormalOverflowAreasRelativeToParent());
2668 StyleAlignFlags nsContainerFrame::CSSAlignmentForAbsPosChild(
2669 const ReflowInput& aChildRI, LogicalAxis aLogicalAxis) const {
2670 MOZ_ASSERT(aChildRI.mFrame->IsAbsolutelyPositioned(),
2671 "This method should only be called for abspos children");
2672 NS_ERROR(
2673 "Child classes that use css box alignment for abspos children "
2674 "should provide their own implementation of this method!");
2676 // In the unexpected/unlikely event that this implementation gets invoked,
2677 // just use "start" alignment.
2678 return StyleAlignFlags::START;
2681 nsOverflowContinuationTracker::nsOverflowContinuationTracker(
2682 nsContainerFrame* aFrame, bool aWalkOOFFrames,
2683 bool aSkipOverflowContainerChildren)
2684 : mOverflowContList(nullptr),
2685 mPrevOverflowCont(nullptr),
2686 mSentry(nullptr),
2687 mParent(aFrame),
2688 mSkipOverflowContainerChildren(aSkipOverflowContainerChildren),
2689 mWalkOOFFrames(aWalkOOFFrames) {
2690 MOZ_ASSERT(aFrame, "null frame pointer");
2691 SetupOverflowContList();
2694 void nsOverflowContinuationTracker::SetupOverflowContList() {
2695 MOZ_ASSERT(mParent, "null frame pointer");
2696 MOZ_ASSERT(!mOverflowContList, "already have list");
2697 nsContainerFrame* nif =
2698 static_cast<nsContainerFrame*>(mParent->GetNextInFlow());
2699 if (nif) {
2700 mOverflowContList = nif->GetOverflowContainers();
2701 if (mOverflowContList) {
2702 mParent = nif;
2703 SetUpListWalker();
2706 if (!mOverflowContList) {
2707 mOverflowContList = mParent->GetExcessOverflowContainers();
2708 if (mOverflowContList) {
2709 SetUpListWalker();
2715 * Helper function to walk past overflow continuations whose prev-in-flow
2716 * isn't a normal child and to set mSentry and mPrevOverflowCont correctly.
2718 void nsOverflowContinuationTracker::SetUpListWalker() {
2719 NS_ASSERTION(!mSentry && !mPrevOverflowCont,
2720 "forgot to reset mSentry or mPrevOverflowCont");
2721 if (mOverflowContList) {
2722 nsIFrame* cur = mOverflowContList->FirstChild();
2723 if (mSkipOverflowContainerChildren) {
2724 while (cur && cur->GetPrevInFlow()->HasAnyStateBits(
2725 NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2726 mPrevOverflowCont = cur;
2727 cur = cur->GetNextSibling();
2729 while (cur &&
2730 (cur->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) != mWalkOOFFrames)) {
2731 mPrevOverflowCont = cur;
2732 cur = cur->GetNextSibling();
2735 if (cur) {
2736 mSentry = cur->GetPrevInFlow();
2742 * Helper function to step forward through the overflow continuations list.
2743 * Sets mSentry and mPrevOverflowCont, skipping over OOF or non-OOF frames
2744 * as appropriate. May only be called when we have already set up an
2745 * mOverflowContList; mOverflowContList cannot be null.
2747 void nsOverflowContinuationTracker::StepForward() {
2748 MOZ_ASSERT(mOverflowContList, "null list");
2750 // Step forward
2751 if (mPrevOverflowCont) {
2752 mPrevOverflowCont = mPrevOverflowCont->GetNextSibling();
2753 } else {
2754 mPrevOverflowCont = mOverflowContList->FirstChild();
2757 // Skip over oof or non-oof frames as appropriate
2758 if (mSkipOverflowContainerChildren) {
2759 nsIFrame* cur = mPrevOverflowCont->GetNextSibling();
2760 while (cur &&
2761 (cur->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) != mWalkOOFFrames)) {
2762 mPrevOverflowCont = cur;
2763 cur = cur->GetNextSibling();
2767 // Set up the sentry
2768 mSentry = (mPrevOverflowCont->GetNextSibling())
2769 ? mPrevOverflowCont->GetNextSibling()->GetPrevInFlow()
2770 : nullptr;
2773 nsresult nsOverflowContinuationTracker::Insert(nsIFrame* aOverflowCont,
2774 nsReflowStatus& aReflowStatus) {
2775 MOZ_ASSERT(aOverflowCont, "null frame pointer");
2776 MOZ_ASSERT(!mSkipOverflowContainerChildren ||
2777 mWalkOOFFrames ==
2778 aOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW),
2779 "shouldn't insert frame that doesn't match walker type");
2780 MOZ_ASSERT(aOverflowCont->GetPrevInFlow(),
2781 "overflow containers must have a prev-in-flow");
2783 nsresult rv = NS_OK;
2784 bool reparented = false;
2785 nsPresContext* presContext = aOverflowCont->PresContext();
2786 bool addToList = !mSentry || aOverflowCont != mSentry->GetNextInFlow();
2788 // If we have a list and aOverflowCont is already in it then don't try to
2789 // add it again.
2790 if (addToList && aOverflowCont->GetParent() == mParent &&
2791 aOverflowCont->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) &&
2792 mOverflowContList && mOverflowContList->ContainsFrame(aOverflowCont)) {
2793 addToList = false;
2794 mPrevOverflowCont = aOverflowCont->GetPrevSibling();
2797 if (addToList) {
2798 if (aOverflowCont->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2799 // aOverflowCont is in some other overflow container list,
2800 // steal it first
2801 NS_ASSERTION(!(mOverflowContList &&
2802 mOverflowContList->ContainsFrame(aOverflowCont)),
2803 "overflow containers out of order");
2804 aOverflowCont->GetParent()->StealFrame(aOverflowCont);
2805 } else {
2806 aOverflowCont->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
2808 if (!mOverflowContList) {
2809 // Note: We don't use SetExcessOverflowContainers() since it requires
2810 // setting a non-empty list. It's OK to manually set an empty list to
2811 // ExcessOverflowContainersProperty() because we are going to insert
2812 // aOverflowCont to mOverflowContList below, which guarantees an nonempty
2813 // list in ExcessOverflowContainersProperty().
2814 mOverflowContList = new (presContext->PresShell()) nsFrameList();
2815 mParent->SetProperty(nsContainerFrame::ExcessOverflowContainersProperty(),
2816 mOverflowContList);
2817 SetUpListWalker();
2819 if (aOverflowCont->GetParent() != mParent) {
2820 nsContainerFrame::ReparentFrameView(aOverflowCont,
2821 aOverflowCont->GetParent(), mParent);
2822 reparented = true;
2825 // If aOverflowCont has a prev/next-in-flow that might be in
2826 // mOverflowContList we need to find it and insert after/before it to
2827 // maintain the order amongst next-in-flows in this list.
2828 nsIFrame* pif = aOverflowCont->GetPrevInFlow();
2829 nsIFrame* nif = aOverflowCont->GetNextInFlow();
2830 if ((pif && pif->GetParent() == mParent && pif != mPrevOverflowCont) ||
2831 (nif && nif->GetParent() == mParent && mPrevOverflowCont)) {
2832 for (nsIFrame* f : *mOverflowContList) {
2833 if (f == pif) {
2834 mPrevOverflowCont = pif;
2835 break;
2837 if (f == nif) {
2838 mPrevOverflowCont = f->GetPrevSibling();
2839 break;
2844 mOverflowContList->InsertFrame(mParent, mPrevOverflowCont, aOverflowCont);
2845 aReflowStatus.SetNextInFlowNeedsReflow();
2848 // If we need to reflow it, mark it dirty
2849 if (aReflowStatus.NextInFlowNeedsReflow()) {
2850 aOverflowCont->MarkSubtreeDirty();
2853 // It's in our list, just step forward
2854 StepForward();
2855 NS_ASSERTION(mPrevOverflowCont == aOverflowCont ||
2856 (mSkipOverflowContainerChildren &&
2857 mPrevOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) !=
2858 aOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)),
2859 "OverflowContTracker in unexpected state");
2861 if (addToList) {
2862 // Convert all non-overflow-container next-in-flows of aOverflowCont
2863 // into overflow containers and move them to our overflow
2864 // tracker. This preserves the invariant that the next-in-flows
2865 // of an overflow container are also overflow containers.
2866 nsIFrame* f = aOverflowCont->GetNextInFlow();
2867 if (f && (!f->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) ||
2868 (!reparented && f->GetParent() == mParent) ||
2869 (reparented && f->GetParent() != mParent))) {
2870 if (!f->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2871 f->GetParent()->StealFrame(f);
2873 Insert(f, aReflowStatus);
2876 return rv;
2879 void nsOverflowContinuationTracker::BeginFinish(nsIFrame* aChild) {
2880 MOZ_ASSERT(aChild, "null ptr");
2881 MOZ_ASSERT(aChild->GetNextInFlow(),
2882 "supposed to call Finish *before* deleting next-in-flow!");
2884 for (nsIFrame* f = aChild; f; f = f->GetNextInFlow()) {
2885 // We'll update these in EndFinish after the next-in-flows are gone.
2886 if (f == mPrevOverflowCont) {
2887 mSentry = nullptr;
2888 mPrevOverflowCont = nullptr;
2889 break;
2891 if (f == mSentry) {
2892 mSentry = nullptr;
2893 break;
2898 void nsOverflowContinuationTracker::EndFinish(nsIFrame* aChild) {
2899 if (!mOverflowContList) {
2900 return;
2902 // Forget mOverflowContList if it was deleted.
2903 nsFrameList* eoc = mParent->GetExcessOverflowContainers();
2904 if (eoc != mOverflowContList) {
2905 nsFrameList* oc = mParent->GetOverflowContainers();
2906 if (oc != mOverflowContList) {
2907 // mOverflowContList was deleted
2908 mPrevOverflowCont = nullptr;
2909 mSentry = nullptr;
2910 mParent = aChild->GetParent();
2911 mOverflowContList = nullptr;
2912 SetupOverflowContList();
2913 return;
2916 // The list survived, update mSentry if needed.
2917 if (!mSentry) {
2918 if (!mPrevOverflowCont) {
2919 SetUpListWalker();
2920 } else {
2921 mozilla::AutoRestore<nsIFrame*> saved(mPrevOverflowCont);
2922 // step backward to make StepForward() use our current mPrevOverflowCont
2923 mPrevOverflowCont = mPrevOverflowCont->GetPrevSibling();
2924 StepForward();
2929 /////////////////////////////////////////////////////////////////////////////
2930 // Debugging
2932 #ifdef DEBUG
2933 void nsContainerFrame::SanityCheckChildListsBeforeReflow() const {
2934 MOZ_ASSERT(IsFlexOrGridContainer(),
2935 "Only Flex / Grid containers can call this!");
2937 const auto didPushItemsBit = IsFlexContainerFrame()
2938 ? NS_STATE_FLEX_DID_PUSH_ITEMS
2939 : NS_STATE_GRID_DID_PUSH_ITEMS;
2940 ChildListIDs absLists = {FrameChildListID::Absolute, FrameChildListID::Fixed,
2941 FrameChildListID::OverflowContainers,
2942 FrameChildListID::ExcessOverflowContainers};
2943 ChildListIDs itemLists = {FrameChildListID::Principal,
2944 FrameChildListID::Overflow};
2945 for (const nsIFrame* f = this; f; f = f->GetNextInFlow()) {
2946 MOZ_ASSERT(!f->HasAnyStateBits(didPushItemsBit),
2947 "At start of reflow, we should've pulled items back from all "
2948 "NIFs and cleared the state bit stored in didPushItemsBit in "
2949 "the process.");
2950 for (const auto& [list, listID] : f->ChildLists()) {
2951 if (!itemLists.contains(listID)) {
2952 MOZ_ASSERT(
2953 absLists.contains(listID) || listID == FrameChildListID::Backdrop,
2954 "unexpected non-empty child list");
2955 continue;
2957 for (const auto* child : list) {
2958 MOZ_ASSERT(f == this || child->GetPrevInFlow(),
2959 "all pushed items must be pulled up before reflow");
2963 // If we have a prev-in-flow, each of its children's next-in-flow
2964 // should be one of our children or be null.
2965 const auto* pif = static_cast<nsContainerFrame*>(GetPrevInFlow());
2966 if (pif) {
2967 const nsFrameList* oc = GetOverflowContainers();
2968 const nsFrameList* eoc = GetExcessOverflowContainers();
2969 const nsFrameList* pifEOC = pif->GetExcessOverflowContainers();
2970 for (const nsIFrame* child : pif->PrincipalChildList()) {
2971 const nsIFrame* childNIF = child->GetNextInFlow();
2972 MOZ_ASSERT(!childNIF || mFrames.ContainsFrame(childNIF) ||
2973 (pifEOC && pifEOC->ContainsFrame(childNIF)) ||
2974 (oc && oc->ContainsFrame(childNIF)) ||
2975 (eoc && eoc->ContainsFrame(childNIF)));
2980 void nsContainerFrame::SetDidPushItemsBitIfNeeded(ChildListID aListID,
2981 nsIFrame* aOldFrame) {
2982 MOZ_ASSERT(IsFlexOrGridContainer(),
2983 "Only Flex / Grid containers can call this!");
2985 // Note that FrameChildListID::Principal doesn't mean aOldFrame must be on
2986 // that list. It can also be on FrameChildListID::Overflow, in which case it
2987 // might be a pushed item, and if it's the only pushed item our DID_PUSH_ITEMS
2988 // bit will lie.
2989 if (aListID == FrameChildListID::Principal && !aOldFrame->GetPrevInFlow()) {
2990 // Since the bit may lie, set the mDidPushItemsBitMayLie value to true for
2991 // ourself and for all our prev-in-flows.
2992 nsContainerFrame* frameThatMayLie = this;
2993 do {
2994 frameThatMayLie->mDidPushItemsBitMayLie = true;
2995 frameThatMayLie =
2996 static_cast<nsContainerFrame*>(frameThatMayLie->GetPrevInFlow());
2997 } while (frameThatMayLie);
3000 #endif
3002 #ifdef DEBUG_FRAME_DUMP
3003 void nsContainerFrame::List(FILE* out, const char* aPrefix,
3004 ListFlags aFlags) const {
3005 nsCString str;
3006 ListGeneric(str, aPrefix, aFlags);
3007 ExtraContainerFrameInfo(str);
3009 // Output the frame name and various fields.
3010 fprintf_stderr(out, "%s <\n", str.get());
3012 const nsCString pfx = nsCString(aPrefix) + " "_ns;
3014 // Output principal child list separately since we want to omit its
3015 // name and address.
3016 for (nsIFrame* kid : PrincipalChildList()) {
3017 kid->List(out, pfx.get(), aFlags);
3020 // Output rest of the child lists.
3021 const ChildListIDs skippedListIDs = {FrameChildListID::Principal};
3022 ListChildLists(out, pfx.get(), aFlags, skippedListIDs);
3024 fprintf_stderr(out, "%s>\n", aPrefix);
3027 void nsContainerFrame::ListWithMatchedRules(FILE* out,
3028 const char* aPrefix) const {
3029 fprintf_stderr(out, "%s%s\n", aPrefix, ListTag().get());
3031 nsCString rulePrefix;
3032 rulePrefix += aPrefix;
3033 rulePrefix += " ";
3034 ListMatchedRules(out, rulePrefix.get());
3036 nsCString childPrefix;
3037 childPrefix += aPrefix;
3038 childPrefix += " ";
3040 for (const auto& childList : ChildLists()) {
3041 for (const nsIFrame* kid : childList.mList) {
3042 kid->ListWithMatchedRules(out, childPrefix.get());
3047 void nsContainerFrame::ListChildLists(FILE* aOut, const char* aPrefix,
3048 ListFlags aFlags,
3049 ChildListIDs aSkippedListIDs) const {
3050 const nsCString nestedPfx = nsCString(aPrefix) + " "_ns;
3052 for (const auto& [list, listID] : ChildLists()) {
3053 if (aSkippedListIDs.contains(listID)) {
3054 continue;
3057 // Use nsPrintfCString so that %p don't output prefix "0x". This is
3058 // consistent with nsIFrame::ListTag().
3059 const nsPrintfCString str("%s%s@%p <\n", aPrefix, ChildListName(listID),
3060 &GetChildList(listID));
3061 fprintf_stderr(aOut, "%s", str.get());
3063 for (nsIFrame* kid : list) {
3064 // Verify the child frame's parent frame pointer is correct.
3065 NS_ASSERTION(kid->GetParent() == this, "Bad parent frame pointer!");
3066 kid->List(aOut, nestedPfx.get(), aFlags);
3068 fprintf_stderr(aOut, "%s>\n", aPrefix);
3072 void nsContainerFrame::ExtraContainerFrameInfo(nsACString& aTo) const {
3073 (void)aTo;
3076 #endif