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/. */
6 
7 /* base class #1 for rendering objects that have child lists */
8 
9 #include "nsContainerFrame.h"
10 #include "nsContainerFrameInlines.h"
11 
12 #include "mozilla/ComputedStyle.h"
13 #include "mozilla/PresShell.h"
14 #include "mozilla/dom/HTMLSummaryElement.h"
15 #include "mozilla/gfx/2D.h"
16 #include "mozilla/gfx/Types.h"
17 #include "nsAbsoluteContainingBlock.h"
18 #include "nsAttrValue.h"
19 #include "nsAttrValueInlines.h"
20 #include "nsFlexContainerFrame.h"
21 #include "nsFrameSelection.h"
22 #include "mozilla/dom/Document.h"
23 #include "nsPresContext.h"
24 #include "nsRect.h"
25 #include "nsPoint.h"
26 #include "nsStyleConsts.h"
27 #include "nsView.h"
28 #include "nsCOMPtr.h"
29 #include "nsGkAtoms.h"
30 #include "nsViewManager.h"
31 #include "nsIWidget.h"
32 #include "nsCSSRendering.h"
33 #include "nsError.h"
34 #include "nsDisplayList.h"
35 #include "nsIBaseWindow.h"
36 #include "nsBoxLayoutState.h"
37 #include "nsCSSFrameConstructor.h"
38 #include "nsBlockFrame.h"
39 #include "nsPlaceholderFrame.h"
40 #include "mozilla/AutoRestore.h"
41 #include "nsIFrameInlines.h"
42 #include "nsPrintfCString.h"
43 #include "mozilla/webrender/WebRenderAPI.h"
44 #include <algorithm>
45 
46 using namespace mozilla;
47 using namespace mozilla::dom;
48 using namespace mozilla::layout;
49 
50 using mozilla::gfx::ColorPattern;
51 using mozilla::gfx::DeviceColor;
52 using mozilla::gfx::DrawTarget;
53 using mozilla::gfx::Rect;
54 using mozilla::gfx::sRGBColor;
55 using mozilla::gfx::ToDeviceColor;
56 
57 nsContainerFrame::~nsContainerFrame() = default;
58 
59 NS_QUERYFRAME_HEAD(nsContainerFrame)
NS_QUERYFRAME_ENTRY(nsContainerFrame)60   NS_QUERYFRAME_ENTRY(nsContainerFrame)
61 NS_QUERYFRAME_TAIL_INHERITING(nsSplittableFrame)
62 
63 void nsContainerFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
64                             nsIFrame* aPrevInFlow) {
65   nsSplittableFrame::Init(aContent, aParent, aPrevInFlow);
66   if (aPrevInFlow) {
67     // Make sure we copy bits from our prev-in-flow that will affect
68     // us. A continuation for a container frame needs to know if it
69     // has a child with a view so that we'll properly reposition it.
70     if (aPrevInFlow->HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW)) {
71       AddStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW);
72     }
73   }
74 }
75 
SetInitialChildList(ChildListID aListID,nsFrameList & aChildList)76 void nsContainerFrame::SetInitialChildList(ChildListID aListID,
77                                            nsFrameList& aChildList) {
78 #ifdef DEBUG
79   nsIFrame::VerifyDirtyBitSet(aChildList);
80   for (nsIFrame* f : aChildList) {
81     MOZ_ASSERT(f->GetParent() == this, "Unexpected parent");
82   }
83 #endif
84   if (aListID == kPrincipalList) {
85     MOZ_ASSERT(mFrames.IsEmpty(),
86                "unexpected second call to SetInitialChildList");
87     mFrames.SetFrames(aChildList);
88   } else if (aListID == kBackdropList) {
89     MOZ_ASSERT(StyleDisplay()->mTopLayer != StyleTopLayer::None,
90                "Only top layer frames should have backdrop");
91     MOZ_ASSERT(HasAnyStateBits(NS_FRAME_OUT_OF_FLOW),
92                "Top layer frames should be out-of-flow");
93     MOZ_ASSERT(!GetProperty(BackdropProperty()),
94                "We shouldn't have setup backdrop frame list before");
95 #ifdef DEBUG
96     {
97       nsIFrame* placeholder = aChildList.FirstChild();
98       MOZ_ASSERT(aChildList.OnlyChild(), "Should have only one backdrop");
99       MOZ_ASSERT(placeholder->IsPlaceholderFrame(),
100                  "The frame to be stored should be a placeholder");
101       MOZ_ASSERT(static_cast<nsPlaceholderFrame*>(placeholder)
102                      ->GetOutOfFlowFrame()
103                      ->IsBackdropFrame(),
104                  "The placeholder should points to a backdrop frame");
105     }
106 #endif
107     nsFrameList* list = new (PresShell()) nsFrameList(aChildList);
108     SetProperty(BackdropProperty(), list);
109   } else {
110     MOZ_ASSERT_UNREACHABLE("Unexpected child list");
111   }
112 }
113 
AppendFrames(ChildListID aListID,nsFrameList & aFrameList)114 void nsContainerFrame::AppendFrames(ChildListID aListID,
115                                     nsFrameList& aFrameList) {
116   MOZ_ASSERT(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
117              "unexpected child list");
118 
119   if (MOZ_UNLIKELY(aFrameList.IsEmpty())) {
120     return;
121   }
122 
123   DrainSelfOverflowList();  // ensure the last frame is in mFrames
124   mFrames.AppendFrames(this, aFrameList);
125 
126   if (aListID != kNoReflowPrincipalList) {
127     PresShell()->FrameNeedsReflow(this, IntrinsicDirty::TreeChange,
128                                   NS_FRAME_HAS_DIRTY_CHILDREN);
129   }
130 }
131 
InsertFrames(ChildListID aListID,nsIFrame * aPrevFrame,const nsLineList::iterator * aPrevFrameLine,nsFrameList & aFrameList)132 void nsContainerFrame::InsertFrames(ChildListID aListID, nsIFrame* aPrevFrame,
133                                     const nsLineList::iterator* aPrevFrameLine,
134                                     nsFrameList& aFrameList) {
135   MOZ_ASSERT(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
136              "unexpected child list");
137   NS_ASSERTION(!aPrevFrame || aPrevFrame->GetParent() == this,
138                "inserting after sibling frame with different parent");
139 
140   if (MOZ_UNLIKELY(aFrameList.IsEmpty())) {
141     return;
142   }
143 
144   DrainSelfOverflowList();  // ensure aPrevFrame is in mFrames
145   mFrames.InsertFrames(this, aPrevFrame, aFrameList);
146 
147   if (aListID != kNoReflowPrincipalList) {
148     PresShell()->FrameNeedsReflow(this, IntrinsicDirty::TreeChange,
149                                   NS_FRAME_HAS_DIRTY_CHILDREN);
150   }
151 }
152 
RemoveFrame(ChildListID aListID,nsIFrame * aOldFrame)153 void nsContainerFrame::RemoveFrame(ChildListID aListID, nsIFrame* aOldFrame) {
154   MOZ_ASSERT(aListID == kPrincipalList || aListID == kNoReflowPrincipalList,
155              "unexpected child list");
156 
157   AutoTArray<nsIFrame*, 10> continuations;
158   {
159     nsIFrame* continuation = aOldFrame;
160     while (continuation) {
161       continuations.AppendElement(continuation);
162       continuation = continuation->GetNextContinuation();
163     }
164   }
165 
166   mozilla::PresShell* presShell = PresShell();
167   nsContainerFrame* lastParent = nullptr;
168 
169   // Loop and destroy aOldFrame and all of its continuations.
170   //
171   // Request a reflow on the parent frames involved unless we were explicitly
172   // told not to (kNoReflowPrincipalList).
173   const bool generateReflowCommand = (kNoReflowPrincipalList != aListID);
174   for (nsIFrame* continuation : Reversed(continuations)) {
175     nsContainerFrame* parent = continuation->GetParent();
176 
177     // Please note that 'parent' may not actually be where 'continuation' lives.
178     // We really MUST use StealFrame() and nothing else here.
179     // @see nsInlineFrame::StealFrame for details.
180     parent->StealFrame(continuation);
181     continuation->Destroy();
182     if (generateReflowCommand && parent != lastParent) {
183       presShell->FrameNeedsReflow(parent, IntrinsicDirty::TreeChange,
184                                   NS_FRAME_HAS_DIRTY_CHILDREN);
185       lastParent = parent;
186     }
187   }
188 }
189 
DestroyAbsoluteFrames(nsIFrame * aDestructRoot,PostDestroyData & aPostDestroyData)190 void nsContainerFrame::DestroyAbsoluteFrames(
191     nsIFrame* aDestructRoot, PostDestroyData& aPostDestroyData) {
192   if (IsAbsoluteContainer()) {
193     GetAbsoluteContainingBlock()->DestroyFrames(this, aDestructRoot,
194                                                 aPostDestroyData);
195     MarkAsNotAbsoluteContainingBlock();
196   }
197 }
198 
SafelyDestroyFrameListProp(nsIFrame * aDestructRoot,PostDestroyData & aPostDestroyData,mozilla::PresShell * aPresShell,FrameListPropertyDescriptor aProp)199 void nsContainerFrame::SafelyDestroyFrameListProp(
200     nsIFrame* aDestructRoot, PostDestroyData& aPostDestroyData,
201     mozilla::PresShell* aPresShell, FrameListPropertyDescriptor aProp) {
202   // Note that the last frame can be removed through another route and thus
203   // delete the property -- that's why we fetch the property again before
204   // removing each frame rather than fetching it once and iterating the list.
205   while (nsFrameList* frameList = GetProperty(aProp)) {
206     nsIFrame* frame = frameList->RemoveFirstChild();
207     if (MOZ_LIKELY(frame)) {
208       frame->DestroyFrom(aDestructRoot, aPostDestroyData);
209     } else {
210       Unused << TakeProperty(aProp);
211       frameList->Delete(aPresShell);
212       return;
213     }
214   }
215 }
216 
DestroyFrom(nsIFrame * aDestructRoot,PostDestroyData & aPostDestroyData)217 void nsContainerFrame::DestroyFrom(nsIFrame* aDestructRoot,
218                                    PostDestroyData& aPostDestroyData) {
219   // Prevent event dispatch during destruction.
220   if (HasView()) {
221     GetView()->SetFrame(nullptr);
222   }
223 
224   DestroyAbsoluteFrames(aDestructRoot, aPostDestroyData);
225 
226   // Destroy frames on the principal child list.
227   mFrames.DestroyFramesFrom(aDestructRoot, aPostDestroyData);
228 
229   // If we have any IB split siblings, clear their references to us.
230   if (HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT)) {
231     // Delete previous sibling's reference to me.
232     if (nsIFrame* prevSib = GetProperty(nsIFrame::IBSplitPrevSibling())) {
233       NS_WARNING_ASSERTION(
234           this == prevSib->GetProperty(nsIFrame::IBSplitSibling()),
235           "IB sibling chain is inconsistent");
236       prevSib->RemoveProperty(nsIFrame::IBSplitSibling());
237     }
238 
239     // Delete next sibling's reference to me.
240     if (nsIFrame* nextSib = GetProperty(nsIFrame::IBSplitSibling())) {
241       NS_WARNING_ASSERTION(
242           this == nextSib->GetProperty(nsIFrame::IBSplitPrevSibling()),
243           "IB sibling chain is inconsistent");
244       nextSib->RemoveProperty(nsIFrame::IBSplitPrevSibling());
245     }
246 
247 #ifdef DEBUG
248     // This is just so we can assert it's not set in nsIFrame::DestroyFrom.
249     RemoveStateBits(NS_FRAME_PART_OF_IBSPLIT);
250 #endif
251   }
252 
253   if (MOZ_UNLIKELY(!mProperties.IsEmpty())) {
254     using T = mozilla::FrameProperties::UntypedDescriptor;
255     bool hasO = false, hasOC = false, hasEOC = false, hasBackdrop = false;
256     mProperties.ForEach([&](const T& aProp, uint64_t) {
257       if (aProp == OverflowProperty()) {
258         hasO = true;
259       } else if (aProp == OverflowContainersProperty()) {
260         hasOC = true;
261       } else if (aProp == ExcessOverflowContainersProperty()) {
262         hasEOC = true;
263       } else if (aProp == BackdropProperty()) {
264         hasBackdrop = true;
265       }
266       return true;
267     });
268 
269     // Destroy frames on the auxiliary frame lists and delete the lists.
270     nsPresContext* pc = PresContext();
271     mozilla::PresShell* presShell = pc->PresShell();
272     if (hasO) {
273       SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
274                                  OverflowProperty());
275     }
276 
277     MOZ_ASSERT(
278         IsFrameOfType(eCanContainOverflowContainers) || !(hasOC || hasEOC),
279         "this type of frame shouldn't have overflow containers");
280     if (hasOC) {
281       SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
282                                  OverflowContainersProperty());
283     }
284     if (hasEOC) {
285       SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
286                                  ExcessOverflowContainersProperty());
287     }
288 
289     MOZ_ASSERT(!GetProperty(BackdropProperty()) ||
290                    StyleDisplay()->mTopLayer != StyleTopLayer::None,
291                "only top layer frame may have backdrop");
292     if (hasBackdrop) {
293       SafelyDestroyFrameListProp(aDestructRoot, aPostDestroyData, presShell,
294                                  BackdropProperty());
295     }
296   }
297 
298   nsSplittableFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
299 }
300 
301 /////////////////////////////////////////////////////////////////////////////
302 // Child frame enumeration
303 
GetChildList(ChildListID aListID) const304 const nsFrameList& nsContainerFrame::GetChildList(ChildListID aListID) const {
305   // We only know about the principal child list, the overflow lists,
306   // and the backdrop list.
307   switch (aListID) {
308     case kPrincipalList:
309       return mFrames;
310     case kOverflowList: {
311       nsFrameList* list = GetOverflowFrames();
312       return list ? *list : nsFrameList::EmptyList();
313     }
314     case kOverflowContainersList: {
315       nsFrameList* list = GetOverflowContainers();
316       return list ? *list : nsFrameList::EmptyList();
317     }
318     case kExcessOverflowContainersList: {
319       nsFrameList* list = GetExcessOverflowContainers();
320       return list ? *list : nsFrameList::EmptyList();
321     }
322     case kBackdropList: {
323       nsFrameList* list = GetProperty(BackdropProperty());
324       return list ? *list : nsFrameList::EmptyList();
325     }
326     default:
327       return nsSplittableFrame::GetChildList(aListID);
328   }
329 }
330 
GetChildLists(nsTArray<ChildList> * aLists) const331 void nsContainerFrame::GetChildLists(nsTArray<ChildList>* aLists) const {
332   mFrames.AppendIfNonempty(aLists, kPrincipalList);
333 
334   using T = mozilla::FrameProperties::UntypedDescriptor;
335   mProperties.ForEach([this, aLists](const T& aProp, uint64_t aValue) {
336     typedef const nsFrameList* L;
337     if (aProp == OverflowProperty()) {
338       reinterpret_cast<L>(aValue)->AppendIfNonempty(aLists, kOverflowList);
339     } else if (aProp == OverflowContainersProperty()) {
340       MOZ_ASSERT(IsFrameOfType(nsIFrame::eCanContainOverflowContainers),
341                  "found unexpected OverflowContainersProperty");
342       Unused << this;  // silence clang -Wunused-lambda-capture in opt builds
343       reinterpret_cast<L>(aValue)->AppendIfNonempty(aLists,
344                                                     kOverflowContainersList);
345     } else if (aProp == ExcessOverflowContainersProperty()) {
346       MOZ_ASSERT(IsFrameOfType(nsIFrame::eCanContainOverflowContainers),
347                  "found unexpected ExcessOverflowContainersProperty");
348       Unused << this;  // silence clang -Wunused-lambda-capture in opt builds
349       reinterpret_cast<L>(aValue)->AppendIfNonempty(
350           aLists, kExcessOverflowContainersList);
351     } else if (aProp == BackdropProperty()) {
352       reinterpret_cast<L>(aValue)->AppendIfNonempty(aLists, kBackdropList);
353     }
354     return true;
355   });
356 
357   nsSplittableFrame::GetChildLists(aLists);
358 }
359 
360 /////////////////////////////////////////////////////////////////////////////
361 // Painting/Events
362 
BuildDisplayList(nsDisplayListBuilder * aBuilder,const nsDisplayListSet & aLists)363 void nsContainerFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
364                                         const nsDisplayListSet& aLists) {
365   DisplayBorderBackgroundOutline(aBuilder, aLists);
366   BuildDisplayListForNonBlockChildren(aBuilder, aLists);
367 }
368 
BuildDisplayListForNonBlockChildren(nsDisplayListBuilder * aBuilder,const nsDisplayListSet & aLists,DisplayChildFlags aFlags)369 void nsContainerFrame::BuildDisplayListForNonBlockChildren(
370     nsDisplayListBuilder* aBuilder, const nsDisplayListSet& aLists,
371     DisplayChildFlags aFlags) {
372   nsIFrame* kid = mFrames.FirstChild();
373   // Put each child's background directly onto the content list
374   nsDisplayListSet set(aLists, aLists.Content());
375   // The children should be in content order
376   while (kid) {
377     BuildDisplayListForChild(aBuilder, kid, set, aFlags);
378     kid = kid->GetNextSibling();
379   }
380 }
381 
382 class nsDisplaySelectionOverlay : public nsPaintedDisplayItem {
383  public:
384   /**
385    * @param aSelectionValue nsISelectionController::getDisplaySelection.
386    */
nsDisplaySelectionOverlay(nsDisplayListBuilder * aBuilder,nsIFrame * aFrame,int16_t aSelectionValue)387   nsDisplaySelectionOverlay(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame,
388                             int16_t aSelectionValue)
389       : nsPaintedDisplayItem(aBuilder, aFrame),
390         mSelectionValue(aSelectionValue) {
391     MOZ_COUNT_CTOR(nsDisplaySelectionOverlay);
392   }
393   MOZ_COUNTED_DTOR_OVERRIDE(nsDisplaySelectionOverlay)
394 
395   virtual void Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) override;
396   bool CreateWebRenderCommands(
397       mozilla::wr::DisplayListBuilder& aBuilder,
398       mozilla::wr::IpcResourceUpdateQueue& aResources,
399       const StackingContextHelper& aSc,
400       mozilla::layers::RenderRootStateManager* aManager,
401       nsDisplayListBuilder* aDisplayListBuilder) override;
402   NS_DISPLAY_DECL_NAME("SelectionOverlay", TYPE_SELECTION_OVERLAY)
403  private:
404   DeviceColor ComputeColor() const;
405 
406   static DeviceColor ComputeColorFromSelectionStyle(ComputedStyle&);
407   static DeviceColor ApplyTransparencyIfNecessary(nscolor);
408 
409   // nsISelectionController::getDisplaySelection.
410   int16_t mSelectionValue;
411 };
412 
ApplyTransparencyIfNecessary(nscolor aColor)413 DeviceColor nsDisplaySelectionOverlay::ApplyTransparencyIfNecessary(
414     nscolor aColor) {
415   // If it has already alpha, leave it like that.
416   if (NS_GET_A(aColor) != 255) {
417     return ToDeviceColor(aColor);
418   }
419 
420   // NOTE(emilio): Blink and WebKit do something slightly different here, and
421   // blend the color with white instead, both for overlays and text backgrounds.
422   auto color = sRGBColor::FromABGR(aColor);
423   color.a = 0.5;
424   return ToDeviceColor(color);
425 }
426 
ComputeColorFromSelectionStyle(ComputedStyle & aStyle)427 DeviceColor nsDisplaySelectionOverlay::ComputeColorFromSelectionStyle(
428     ComputedStyle& aStyle) {
429   return ApplyTransparencyIfNecessary(
430       aStyle.GetVisitedDependentColor(&nsStyleBackground::mBackgroundColor));
431 }
432 
ComputeColor() const433 DeviceColor nsDisplaySelectionOverlay::ComputeColor() const {
434   LookAndFeel::ColorID colorID;
435   if (RefPtr<ComputedStyle> style =
436           mFrame->ComputeSelectionStyle(mSelectionValue)) {
437     return ComputeColorFromSelectionStyle(*style);
438   }
439   if (mSelectionValue == nsISelectionController::SELECTION_ON) {
440     colorID = LookAndFeel::ColorID::Highlight;
441   } else if (mSelectionValue == nsISelectionController::SELECTION_ATTENTION) {
442     colorID = LookAndFeel::ColorID::TextSelectAttentionBackground;
443   } else {
444     colorID = LookAndFeel::ColorID::TextSelectDisabledBackground;
445   }
446 
447   return ApplyTransparencyIfNecessary(
448       LookAndFeel::Color(colorID, mFrame, NS_RGB(255, 255, 255)));
449 }
450 
Paint(nsDisplayListBuilder * aBuilder,gfxContext * aCtx)451 void nsDisplaySelectionOverlay::Paint(nsDisplayListBuilder* aBuilder,
452                                       gfxContext* aCtx) {
453   DrawTarget& aDrawTarget = *aCtx->GetDrawTarget();
454   ColorPattern color(ComputeColor());
455 
456   nsIntRect pxRect =
457       GetPaintRect(aBuilder, aCtx)
458           .ToOutsidePixels(mFrame->PresContext()->AppUnitsPerDevPixel());
459   Rect rect(pxRect.x, pxRect.y, pxRect.width, pxRect.height);
460   MaybeSnapToDevicePixels(rect, aDrawTarget, true);
461 
462   aDrawTarget.FillRect(rect, color);
463 }
464 
CreateWebRenderCommands(mozilla::wr::DisplayListBuilder & aBuilder,mozilla::wr::IpcResourceUpdateQueue & aResources,const StackingContextHelper & aSc,mozilla::layers::RenderRootStateManager * aManager,nsDisplayListBuilder * aDisplayListBuilder)465 bool nsDisplaySelectionOverlay::CreateWebRenderCommands(
466     mozilla::wr::DisplayListBuilder& aBuilder,
467     mozilla::wr::IpcResourceUpdateQueue& aResources,
468     const StackingContextHelper& aSc,
469     mozilla::layers::RenderRootStateManager* aManager,
470     nsDisplayListBuilder* aDisplayListBuilder) {
471   wr::LayoutRect bounds = wr::ToLayoutRect(LayoutDeviceRect::FromAppUnits(
472       nsRect(ToReferenceFrame(), Frame()->GetSize()),
473       mFrame->PresContext()->AppUnitsPerDevPixel()));
474   aBuilder.PushRect(bounds, bounds, !BackfaceIsHidden(), false,
475                     wr::ToColorF(ComputeColor()));
476   return true;
477 }
478 
DisplaySelectionOverlay(nsDisplayListBuilder * aBuilder,nsDisplayList * aList,uint16_t aContentType)479 void nsContainerFrame::DisplaySelectionOverlay(nsDisplayListBuilder* aBuilder,
480                                                nsDisplayList* aList,
481                                                uint16_t aContentType) {
482   if (!IsSelected() || !IsVisibleForPainting()) {
483     return;
484   }
485 
486   int16_t displaySelection = PresShell()->GetSelectionFlags();
487   if (!(displaySelection & aContentType)) {
488     return;
489   }
490 
491   const nsFrameSelection* frameSelection = GetConstFrameSelection();
492   int16_t selectionValue = frameSelection->GetDisplaySelection();
493 
494   if (selectionValue <= nsISelectionController::SELECTION_HIDDEN) {
495     return;  // selection is hidden or off
496   }
497 
498   nsIContent* newContent = mContent->GetParent();
499 
500   // check to see if we are anonymous content
501   // XXXbz there has GOT to be a better way of determining this!
502   int32_t offset =
503       newContent ? newContent->ComputeIndexOf_Deprecated(mContent) : 0;
504 
505   // look up to see what selection(s) are on this frame
506   UniquePtr<SelectionDetails> details =
507       frameSelection->LookUpSelection(newContent, offset, 1, false);
508   if (!details) {
509     return;
510   }
511 
512   bool normal = false;
513   for (SelectionDetails* sd = details.get(); sd; sd = sd->mNext.get()) {
514     if (sd->mSelectionType == SelectionType::eNormal) {
515       normal = true;
516     }
517   }
518 
519   if (!normal && aContentType == nsISelectionDisplay::DISPLAY_IMAGES) {
520     // Don't overlay an image if it's not in the primary selection.
521     return;
522   }
523 
524   aList->AppendNewToTop<nsDisplaySelectionOverlay>(aBuilder, this,
525                                                    selectionValue);
526 }
527 
528 /* virtual */
ChildIsDirty(nsIFrame * aChild)529 void nsContainerFrame::ChildIsDirty(nsIFrame* aChild) {
530   NS_ASSERTION(aChild->IsSubtreeDirty(), "child isn't actually dirty");
531 
532   AddStateBits(NS_FRAME_HAS_DIRTY_CHILDREN);
533 }
534 
PeekOffsetNoAmount(bool aForward,int32_t * aOffset)535 nsIFrame::FrameSearchResult nsContainerFrame::PeekOffsetNoAmount(
536     bool aForward, int32_t* aOffset) {
537   NS_ASSERTION(aOffset && *aOffset <= 1, "aOffset out of range");
538   // Don't allow the caret to stay in an empty (leaf) container frame.
539   return CONTINUE_EMPTY;
540 }
541 
PeekOffsetCharacter(bool aForward,int32_t * aOffset,PeekOffsetCharacterOptions aOptions)542 nsIFrame::FrameSearchResult nsContainerFrame::PeekOffsetCharacter(
543     bool aForward, int32_t* aOffset, PeekOffsetCharacterOptions aOptions) {
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;
547 }
548 
549 /////////////////////////////////////////////////////////////////////////////
550 // Helper member functions
551 
552 /**
553  * Position the view associated with |aKidFrame|, if there is one. A
554  * container frame should call this method after positioning a frame,
555  * but before |Reflow|.
556  */
PositionFrameView(nsIFrame * aKidFrame)557 void nsContainerFrame::PositionFrameView(nsIFrame* aKidFrame) {
558   nsIFrame* parentFrame = aKidFrame->GetParent();
559   if (!aKidFrame->HasView() || !parentFrame) return;
560 
561   nsView* view = aKidFrame->GetView();
562   nsViewManager* vm = view->GetViewManager();
563   nsPoint pt;
564   nsView* ancestorView = parentFrame->GetClosestView(&pt);
565 
566   if (ancestorView != view->GetParent()) {
567     NS_ASSERTION(ancestorView == view->GetParent()->GetParent(),
568                  "Allowed only one anonymous view between frames");
569     // parentFrame is responsible for positioning aKidFrame's view
570     // explicitly
571     return;
572   }
573 
574   pt += aKidFrame->GetPosition();
575   vm->MoveViewTo(view, pt.x, pt.y);
576 }
577 
ReparentFrameView(nsIFrame * aChildFrame,nsIFrame * aOldParentFrame,nsIFrame * aNewParentFrame)578 nsresult nsContainerFrame::ReparentFrameView(nsIFrame* aChildFrame,
579                                              nsIFrame* aOldParentFrame,
580                                              nsIFrame* aNewParentFrame) {
581   MOZ_ASSERT(aChildFrame, "null child frame pointer");
582   MOZ_ASSERT(aOldParentFrame, "null old parent frame pointer");
583   MOZ_ASSERT(aNewParentFrame, "null new parent frame pointer");
584   MOZ_ASSERT(aOldParentFrame != aNewParentFrame,
585              "same old and new parent frame");
586 
587   // See if either the old parent frame or the new parent frame have a view
588   while (!aOldParentFrame->HasView() && !aNewParentFrame->HasView()) {
589     // Walk up both the old parent frame and the new parent frame nodes
590     // stopping when we either find a common parent or views for one
591     // or both of the frames.
592     //
593     // This works well in the common case where we push/pull and the old parent
594     // frame and the new parent frame are part of the same flow. They will
595     // typically be the same distance (height wise) from the
596     aOldParentFrame = aOldParentFrame->GetParent();
597     aNewParentFrame = aNewParentFrame->GetParent();
598 
599     // We should never walk all the way to the root frame without finding
600     // a view
601     NS_ASSERTION(aOldParentFrame && aNewParentFrame, "didn't find view");
602 
603     // See if we reached a common ancestor
604     if (aOldParentFrame == aNewParentFrame) {
605       break;
606     }
607   }
608 
609   // See if we found a common parent frame
610   if (aOldParentFrame == aNewParentFrame) {
611     // We found a common parent and there are no views between the old parent
612     // and the common parent or the new parent frame and the common parent.
613     // Because neither the old parent frame nor the new parent frame have views,
614     // then any child views don't need reparenting
615     return NS_OK;
616   }
617 
618   // We found views for one or both of the ancestor frames before we
619   // found a common ancestor.
620   nsView* oldParentView = aOldParentFrame->GetClosestView();
621   nsView* newParentView = aNewParentFrame->GetClosestView();
622 
623   // See if the old parent frame and the new parent frame are in the
624   // same view sub-hierarchy. If they are then we don't have to do
625   // anything
626   if (oldParentView != newParentView) {
627     // They're not so we need to reparent any child views
628     aChildFrame->ReparentFrameViewTo(oldParentView->GetViewManager(),
629                                      newParentView, oldParentView);
630   }
631 
632   return NS_OK;
633 }
634 
ReparentFrameViewList(const nsFrameList & aChildFrameList,nsIFrame * aOldParentFrame,nsIFrame * aNewParentFrame)635 void nsContainerFrame::ReparentFrameViewList(const nsFrameList& aChildFrameList,
636                                              nsIFrame* aOldParentFrame,
637                                              nsIFrame* aNewParentFrame) {
638   MOZ_ASSERT(aChildFrameList.NotEmpty(), "empty child frame list");
639   MOZ_ASSERT(aOldParentFrame, "null old parent frame pointer");
640   MOZ_ASSERT(aNewParentFrame, "null new parent frame pointer");
641   MOZ_ASSERT(aOldParentFrame != aNewParentFrame,
642              "same old and new parent frame");
643 
644   // See if either the old parent frame or the new parent frame have a view
645   while (!aOldParentFrame->HasView() && !aNewParentFrame->HasView()) {
646     // Walk up both the old parent frame and the new parent frame nodes
647     // stopping when we either find a common parent or views for one
648     // or both of the frames.
649     //
650     // This works well in the common case where we push/pull and the old parent
651     // frame and the new parent frame are part of the same flow. They will
652     // typically be the same distance (height wise) from the
653     aOldParentFrame = aOldParentFrame->GetParent();
654     aNewParentFrame = aNewParentFrame->GetParent();
655 
656     // We should never walk all the way to the root frame without finding
657     // a view
658     NS_ASSERTION(aOldParentFrame && aNewParentFrame, "didn't find view");
659 
660     // See if we reached a common ancestor
661     if (aOldParentFrame == aNewParentFrame) {
662       break;
663     }
664   }
665 
666   // See if we found a common parent frame
667   if (aOldParentFrame == aNewParentFrame) {
668     // We found a common parent and there are no views between the old parent
669     // and the common parent or the new parent frame and the common parent.
670     // Because neither the old parent frame nor the new parent frame have views,
671     // then any child views don't need reparenting
672     return;
673   }
674 
675   // We found views for one or both of the ancestor frames before we
676   // found a common ancestor.
677   nsView* oldParentView = aOldParentFrame->GetClosestView();
678   nsView* newParentView = aNewParentFrame->GetClosestView();
679 
680   // See if the old parent frame and the new parent frame are in the
681   // same view sub-hierarchy. If they are then we don't have to do
682   // anything
683   if (oldParentView != newParentView) {
684     nsViewManager* viewManager = oldParentView->GetViewManager();
685 
686     // They're not so we need to reparent any child views
687     for (nsFrameList::Enumerator e(aChildFrameList); !e.AtEnd(); e.Next()) {
688       e.get()->ReparentFrameViewTo(viewManager, newParentView, oldParentView);
689     }
690   }
691 }
692 
ReparentFrame(nsIFrame * aFrame,nsContainerFrame * aOldParent,nsContainerFrame * aNewParent)693 void nsContainerFrame::ReparentFrame(nsIFrame* aFrame,
694                                      nsContainerFrame* aOldParent,
695                                      nsContainerFrame* aNewParent) {
696   NS_ASSERTION(aOldParent == aFrame->GetParent(),
697                "Parent not consistent with expectations");
698 
699   aFrame->SetParent(aNewParent);
700 
701   // When pushing and pulling frames we need to check for whether any
702   // views need to be reparented
703   ReparentFrameView(aFrame, aOldParent, aNewParent);
704 }
705 
ReparentFrames(nsFrameList & aFrameList,nsContainerFrame * aOldParent,nsContainerFrame * aNewParent)706 void nsContainerFrame::ReparentFrames(nsFrameList& aFrameList,
707                                       nsContainerFrame* aOldParent,
708                                       nsContainerFrame* aNewParent) {
709   for (auto* f : aFrameList) {
710     ReparentFrame(f, aOldParent, aNewParent);
711   }
712 }
713 
GetPresContextContainerWidget(nsPresContext * aPresContext)714 static nsIWidget* GetPresContextContainerWidget(nsPresContext* aPresContext) {
715   nsCOMPtr<nsISupports> container = aPresContext->Document()->GetContainer();
716   nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(container);
717   if (!baseWindow) return nullptr;
718 
719   nsCOMPtr<nsIWidget> mainWidget;
720   baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
721   return mainWidget;
722 }
723 
IsTopLevelWidget(nsIWidget * aWidget)724 static bool IsTopLevelWidget(nsIWidget* aWidget) {
725   nsWindowType windowType = aWidget->WindowType();
726   return windowType == eWindowType_toplevel ||
727          windowType == eWindowType_dialog || windowType == eWindowType_popup ||
728          windowType == eWindowType_sheet;
729 }
730 
SyncWindowProperties(nsPresContext * aPresContext,nsIFrame * aFrame,nsView * aView,gfxContext * aRC,uint32_t aFlags)731 void nsContainerFrame::SyncWindowProperties(nsPresContext* aPresContext,
732                                             nsIFrame* aFrame, nsView* aView,
733                                             gfxContext* aRC, uint32_t aFlags) {
734   if (!aView || !nsCSSRendering::IsCanvasFrame(aFrame) || !aView->HasWidget())
735     return;
736 
737   nsCOMPtr<nsIWidget> windowWidget =
738       GetPresContextContainerWidget(aPresContext);
739   if (!windowWidget || !IsTopLevelWidget(windowWidget)) return;
740 
741   nsViewManager* vm = aView->GetViewManager();
742   nsView* rootView = vm->GetRootView();
743 
744   if (aView != rootView) return;
745 
746   Element* rootElement = aPresContext->Document()->GetRootElement();
747   if (!rootElement) {
748     return;
749   }
750 
751   nsIFrame* rootFrame =
752       aPresContext->PresShell()->FrameConstructor()->GetRootElementStyleFrame();
753   if (!rootFrame) return;
754 
755   if (aFlags & SET_ASYNC) {
756     aView->SetNeedsWindowPropertiesSync();
757     return;
758   }
759 
760   RefPtr<nsPresContext> kungFuDeathGrip(aPresContext);
761   AutoWeakFrame weak(rootFrame);
762 
763   if (!aPresContext->PresShell()->GetRootScrollFrame()) {
764     // Scrollframes use native widgets which don't work well with
765     // translucent windows, at least in Windows XP. So if the document
766     // has a root scrollrame it's useless to try to make it transparent,
767     // we'll just get something broken.
768     // We can change this to allow translucent toplevel HTML documents
769     // (e.g. to do something like Dashboard widgets), once we
770     // have broad support for translucent scrolled documents, but be
771     // careful because apparently some Firefox extensions expect
772     // openDialog("something.html") to produce an opaque window
773     // even if the HTML doesn't have a background-color set.
774     nsTransparencyMode mode =
775         nsLayoutUtils::GetFrameTransparency(aFrame, rootFrame);
776     StyleWindowShadow shadow = rootFrame->StyleUIReset()->mWindowShadow;
777     nsCOMPtr<nsIWidget> viewWidget = aView->GetWidget();
778     viewWidget->SetTransparencyMode(mode);
779     windowWidget->SetWindowShadowStyle(shadow);
780 
781     // For macOS, apply color scheme overrides to the top level window widget.
782     if (auto scheme = aPresContext->GetOverriddenColorScheme()) {
783       windowWidget->SetColorScheme(scheme);
784     }
785   }
786 
787   if (!aRC) return;
788 
789   if (!weak.IsAlive()) {
790     return;
791   }
792 
793   nsSize minSize(0, 0);
794   nsSize maxSize(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
795   if (rootElement->IsXULElement()) {
796     nsBoxLayoutState aState(aPresContext, aRC);
797     minSize = rootFrame->GetXULMinSize(aState);
798     maxSize = rootFrame->GetXULMaxSize(aState);
799   } else {
800     auto* pos = rootFrame->StylePosition();
801     if (pos->mMinWidth.ConvertsToLength()) {
802       minSize.width = pos->mMinWidth.ToLength();
803     }
804     if (pos->mMinHeight.ConvertsToLength()) {
805       minSize.height = pos->mMinHeight.ToLength();
806     }
807     if (pos->mMaxWidth.ConvertsToLength()) {
808       maxSize.width = pos->mMaxWidth.ToLength();
809     }
810     if (pos->mMaxHeight.ConvertsToLength()) {
811       maxSize.height = pos->mMaxHeight.ToLength();
812     }
813   }
814   SetSizeConstraints(aPresContext, windowWidget, minSize, maxSize);
815 }
816 
SetSizeConstraints(nsPresContext * aPresContext,nsIWidget * aWidget,const nsSize & aMinSize,const nsSize & aMaxSize)817 void nsContainerFrame::SetSizeConstraints(nsPresContext* aPresContext,
818                                           nsIWidget* aWidget,
819                                           const nsSize& aMinSize,
820                                           const nsSize& aMaxSize) {
821   LayoutDeviceIntSize devMinSize(
822       aPresContext->AppUnitsToDevPixels(aMinSize.width),
823       aPresContext->AppUnitsToDevPixels(aMinSize.height));
824   LayoutDeviceIntSize devMaxSize(
825       aMaxSize.width == NS_UNCONSTRAINEDSIZE
826           ? NS_MAXSIZE
827           : aPresContext->AppUnitsToDevPixels(aMaxSize.width),
828       aMaxSize.height == NS_UNCONSTRAINEDSIZE
829           ? NS_MAXSIZE
830           : aPresContext->AppUnitsToDevPixels(aMaxSize.height));
831 
832   // MinSize has a priority over MaxSize
833   if (devMinSize.width > devMaxSize.width) devMaxSize.width = devMinSize.width;
834   if (devMinSize.height > devMaxSize.height)
835     devMaxSize.height = devMinSize.height;
836 
837   nsIWidget* rootWidget = aPresContext->GetNearestWidget();
838   DesktopToLayoutDeviceScale constraintsScale(MOZ_WIDGET_INVALID_SCALE);
839   if (rootWidget) {
840     constraintsScale = rootWidget->GetDesktopToDeviceScale();
841   }
842 
843   widget::SizeConstraints constraints(devMinSize, devMaxSize, constraintsScale);
844 
845   // The sizes are in inner window sizes, so convert them into outer window
846   // sizes. Use a size of (200, 200) as only the difference between the inner
847   // and outer size is needed.
848   LayoutDeviceIntSize windowSize =
849       aWidget->ClientToWindowSize(LayoutDeviceIntSize(200, 200));
850   if (constraints.mMinSize.width)
851     constraints.mMinSize.width += windowSize.width - 200;
852   if (constraints.mMinSize.height)
853     constraints.mMinSize.height += windowSize.height - 200;
854   if (constraints.mMaxSize.width != NS_MAXSIZE)
855     constraints.mMaxSize.width += windowSize.width - 200;
856   if (constraints.mMaxSize.height != NS_MAXSIZE)
857     constraints.mMaxSize.height += windowSize.height - 200;
858 
859   aWidget->SetSizeConstraints(constraints);
860 }
861 
SyncFrameViewAfterReflow(nsPresContext * aPresContext,nsIFrame * aFrame,nsView * aView,const nsRect & aInkOverflowArea,ReflowChildFlags aFlags)862 void nsContainerFrame::SyncFrameViewAfterReflow(nsPresContext* aPresContext,
863                                                 nsIFrame* aFrame, nsView* aView,
864                                                 const nsRect& aInkOverflowArea,
865                                                 ReflowChildFlags aFlags) {
866   if (!aView) {
867     return;
868   }
869 
870   // Make sure the view is sized and positioned correctly
871   if (!(aFlags & ReflowChildFlags::NoMoveView)) {
872     PositionFrameView(aFrame);
873   }
874 
875   if (!(aFlags & ReflowChildFlags::NoSizeView)) {
876     nsViewManager* vm = aView->GetViewManager();
877 
878     vm->ResizeView(aView, aInkOverflowArea, true);
879   }
880 }
881 
DoInlineMinISize(gfxContext * aRenderingContext,InlineMinISizeData * aData)882 void nsContainerFrame::DoInlineMinISize(gfxContext* aRenderingContext,
883                                         InlineMinISizeData* aData) {
884   auto handleChildren = [aRenderingContext](auto frame, auto data) {
885     for (nsIFrame* kid : frame->mFrames) {
886       kid->AddInlineMinISize(aRenderingContext, data);
887     }
888   };
889   DoInlineIntrinsicISize(aData, handleChildren);
890 }
891 
DoInlinePrefISize(gfxContext * aRenderingContext,InlinePrefISizeData * aData)892 void nsContainerFrame::DoInlinePrefISize(gfxContext* aRenderingContext,
893                                          InlinePrefISizeData* aData) {
894   auto handleChildren = [aRenderingContext](auto frame, auto data) {
895     for (nsIFrame* kid : frame->mFrames) {
896       kid->AddInlinePrefISize(aRenderingContext, data);
897     }
898   };
899   DoInlineIntrinsicISize(aData, handleChildren);
900   aData->mLineIsEmpty = false;
901 }
902 
903 /* virtual */
ComputeAutoSize(gfxContext * aRenderingContext,WritingMode aWM,const LogicalSize & aCBSize,nscoord aAvailableISize,const LogicalSize & aMargin,const mozilla::LogicalSize & aBorderPadding,const StyleSizeOverrides & aSizeOverrides,ComputeSizeFlags aFlags)904 LogicalSize nsContainerFrame::ComputeAutoSize(
905     gfxContext* aRenderingContext, WritingMode aWM, const LogicalSize& aCBSize,
906     nscoord aAvailableISize, const LogicalSize& aMargin,
907     const mozilla::LogicalSize& aBorderPadding,
908     const StyleSizeOverrides& aSizeOverrides, ComputeSizeFlags aFlags) {
909   LogicalSize result(aWM, 0xdeadbeef, NS_UNCONSTRAINEDSIZE);
910   nscoord availBased =
911       aAvailableISize - aMargin.ISize(aWM) - aBorderPadding.ISize(aWM);
912   // replaced elements always shrink-wrap
913   if (aFlags.contains(ComputeSizeFlag::ShrinkWrap) ||
914       IsFrameOfType(eReplaced)) {
915     // Only bother computing our 'auto' ISize if the result will be used.
916     const auto& styleISize = aSizeOverrides.mStyleISize
917                                  ? *aSizeOverrides.mStyleISize
918                                  : StylePosition()->ISize(aWM);
919     if (styleISize.IsAuto()) {
920       result.ISize(aWM) =
921           ShrinkWidthToFit(aRenderingContext, availBased, aFlags);
922     }
923   } else {
924     result.ISize(aWM) = availBased;
925   }
926 
927   if (IsTableCaption()) {
928     // If we're a container for font size inflation, then shrink
929     // wrapping inside of us should not apply font size inflation.
930     AutoMaybeDisableFontInflation an(this);
931 
932     WritingMode tableWM = GetParent()->GetWritingMode();
933     StyleCaptionSide captionSide = StyleTableBorder()->mCaptionSide;
934 
935     if (aWM.IsOrthogonalTo(tableWM)) {
936       if (captionSide == StyleCaptionSide::Top ||
937           captionSide == StyleCaptionSide::TopOutside ||
938           captionSide == StyleCaptionSide::Bottom ||
939           captionSide == StyleCaptionSide::BottomOutside) {
940         // For an orthogonal caption on a block-dir side of the table,
941         // shrink-wrap to min-isize.
942         result.ISize(aWM) = GetMinISize(aRenderingContext);
943       } else {
944         // An orthogonal caption on an inline-dir side of the table
945         // is constrained to the containing block.
946         nscoord pref = GetPrefISize(aRenderingContext);
947         if (pref > aCBSize.ISize(aWM)) {
948           pref = aCBSize.ISize(aWM);
949         }
950         if (pref < result.ISize(aWM)) {
951           result.ISize(aWM) = pref;
952         }
953       }
954     } else {
955       if (captionSide == StyleCaptionSide::Left ||
956           captionSide == StyleCaptionSide::Right) {
957         result.ISize(aWM) = GetMinISize(aRenderingContext);
958       } else if (captionSide == StyleCaptionSide::Top ||
959                  captionSide == StyleCaptionSide::Bottom) {
960         // The outer frame constrains our available isize to the isize of
961         // the table.  Grow if our min-isize is bigger than that, but not
962         // larger than the containing block isize.  (It would really be nice
963         // to transmit that information another way, so we could grow up to
964         // the table's available isize, but that's harder.)
965         nscoord min = GetMinISize(aRenderingContext);
966         if (min > aCBSize.ISize(aWM)) {
967           min = aCBSize.ISize(aWM);
968         }
969         if (min > result.ISize(aWM)) {
970           result.ISize(aWM) = min;
971         }
972       }
973     }
974   }
975   return result;
976 }
977 
ReflowChild(nsIFrame * aKidFrame,nsPresContext * aPresContext,ReflowOutput & aDesiredSize,const ReflowInput & aReflowInput,const WritingMode & aWM,const LogicalPoint & aPos,const nsSize & aContainerSize,ReflowChildFlags aFlags,nsReflowStatus & aStatus,nsOverflowContinuationTracker * aTracker)978 void nsContainerFrame::ReflowChild(
979     nsIFrame* aKidFrame, nsPresContext* aPresContext,
980     ReflowOutput& aDesiredSize, const ReflowInput& aReflowInput,
981     const WritingMode& aWM, const LogicalPoint& aPos,
982     const nsSize& aContainerSize, ReflowChildFlags aFlags,
983     nsReflowStatus& aStatus, nsOverflowContinuationTracker* aTracker) {
984   MOZ_ASSERT(aReflowInput.mFrame == aKidFrame, "bad reflow input");
985   if (aWM.IsPhysicalRTL()) {
986     NS_ASSERTION(aContainerSize.width != NS_UNCONSTRAINEDSIZE,
987                  "ReflowChild with unconstrained container width!");
988   }
989   MOZ_ASSERT(aDesiredSize.InkOverflow() == nsRect(0, 0, 0, 0) &&
990                  aDesiredSize.ScrollableOverflow() == nsRect(0, 0, 0, 0),
991              "please reset the overflow areas before calling ReflowChild");
992 
993   // Position the child frame and its view if requested.
994   if (ReflowChildFlags::NoMoveFrame !=
995       (aFlags & ReflowChildFlags::NoMoveFrame)) {
996     aKidFrame->SetPosition(aWM, aPos, aContainerSize);
997   }
998 
999   if (!(aFlags & ReflowChildFlags::NoMoveView)) {
1000     PositionFrameView(aKidFrame);
1001     PositionChildViews(aKidFrame);
1002   }
1003 
1004   // Reflow the child frame
1005   aKidFrame->Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
1006 
1007   // If the child frame is complete, delete any next-in-flows,
1008   // but only if the NoDeleteNextInFlowChild flag isn't set.
1009   if (!aStatus.IsInlineBreakBefore() && aStatus.IsFullyComplete() &&
1010       !(aFlags & ReflowChildFlags::NoDeleteNextInFlowChild)) {
1011     nsIFrame* kidNextInFlow = aKidFrame->GetNextInFlow();
1012     if (kidNextInFlow) {
1013       // Remove all of the childs next-in-flows. Make sure that we ask
1014       // the right parent to do the removal (it's possible that the
1015       // parent is not this because we are executing pullup code)
1016       nsOverflowContinuationTracker::AutoFinish fini(aTracker, aKidFrame);
1017       kidNextInFlow->GetParent()->DeleteNextInFlowChild(kidNextInFlow, true);
1018     }
1019   }
1020 }
1021 
1022 // XXX temporary: hold on to a copy of the old physical version of
1023 //    ReflowChild so that we can convert callers incrementally.
ReflowChild(nsIFrame * aKidFrame,nsPresContext * aPresContext,ReflowOutput & aDesiredSize,const ReflowInput & aReflowInput,nscoord aX,nscoord aY,ReflowChildFlags aFlags,nsReflowStatus & aStatus,nsOverflowContinuationTracker * aTracker)1024 void nsContainerFrame::ReflowChild(nsIFrame* aKidFrame,
1025                                    nsPresContext* aPresContext,
1026                                    ReflowOutput& aDesiredSize,
1027                                    const ReflowInput& aReflowInput, nscoord aX,
1028                                    nscoord aY, ReflowChildFlags aFlags,
1029                                    nsReflowStatus& aStatus,
1030                                    nsOverflowContinuationTracker* aTracker) {
1031   MOZ_ASSERT(aReflowInput.mFrame == aKidFrame, "bad reflow input");
1032 
1033   // Position the child frame and its view if requested.
1034   if (ReflowChildFlags::NoMoveFrame !=
1035       (aFlags & ReflowChildFlags::NoMoveFrame)) {
1036     aKidFrame->SetPosition(nsPoint(aX, aY));
1037   }
1038 
1039   if (!(aFlags & ReflowChildFlags::NoMoveView)) {
1040     PositionFrameView(aKidFrame);
1041     PositionChildViews(aKidFrame);
1042   }
1043 
1044   // Reflow the child frame
1045   aKidFrame->Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
1046 
1047   // If the child frame is complete, delete any next-in-flows,
1048   // but only if the NoDeleteNextInFlowChild flag isn't set.
1049   if (aStatus.IsFullyComplete() &&
1050       !(aFlags & ReflowChildFlags::NoDeleteNextInFlowChild)) {
1051     nsIFrame* kidNextInFlow = aKidFrame->GetNextInFlow();
1052     if (kidNextInFlow) {
1053       // Remove all of the childs next-in-flows. Make sure that we ask
1054       // the right parent to do the removal (it's possible that the
1055       // parent is not this because we are executing pullup code)
1056       nsOverflowContinuationTracker::AutoFinish fini(aTracker, aKidFrame);
1057       kidNextInFlow->GetParent()->DeleteNextInFlowChild(kidNextInFlow, true);
1058     }
1059   }
1060 }
1061 
1062 /**
1063  * Position the views of |aFrame|'s descendants. A container frame
1064  * should call this method if it moves a frame after |Reflow|.
1065  */
PositionChildViews(nsIFrame * aFrame)1066 void nsContainerFrame::PositionChildViews(nsIFrame* aFrame) {
1067   if (!aFrame->HasAnyStateBits(NS_FRAME_HAS_CHILD_WITH_VIEW)) {
1068     return;
1069   }
1070 
1071   // Recursively walk aFrame's child frames.
1072   // Process the additional child lists, but skip the popup list as the
1073   // view for popups is managed by the parent. Currently only nsMenuFrame
1074   // and nsPopupSetFrame have a popupList and during layout will adjust the
1075   // view manually to position the popup.
1076   for (const auto& [list, listID] : aFrame->ChildLists()) {
1077     if (listID == kPopupList) {
1078       continue;
1079     }
1080     for (nsIFrame* childFrame : list) {
1081       // Position the frame's view (if it has one) otherwise recursively
1082       // process its children
1083       if (childFrame->HasView()) {
1084         PositionFrameView(childFrame);
1085       } else {
1086         PositionChildViews(childFrame);
1087       }
1088     }
1089   }
1090 }
1091 
1092 /**
1093  * De-optimize function to work around a VC2017 15.5+ compiler bug:
1094  * https://bugzil.la/1424281#c12
1095  */
1096 #if defined(_MSC_VER) && !defined(__clang__) && defined(_M_AMD64)
1097 #  pragma optimize("g", off)
1098 #endif
FinishReflowChild(nsIFrame * aKidFrame,nsPresContext * aPresContext,const ReflowOutput & aDesiredSize,const ReflowInput * aReflowInput,const WritingMode & aWM,const LogicalPoint & aPos,const nsSize & aContainerSize,nsIFrame::ReflowChildFlags aFlags)1099 void nsContainerFrame::FinishReflowChild(
1100     nsIFrame* aKidFrame, nsPresContext* aPresContext,
1101     const ReflowOutput& aDesiredSize, const ReflowInput* aReflowInput,
1102     const WritingMode& aWM, const LogicalPoint& aPos,
1103     const nsSize& aContainerSize, nsIFrame::ReflowChildFlags aFlags) {
1104   MOZ_ASSERT(!aReflowInput || aReflowInput->mFrame == aKidFrame);
1105   MOZ_ASSERT(aReflowInput || aKidFrame->IsFrameOfType(eMathML) ||
1106                  aKidFrame->IsTableCellFrame(),
1107              "aReflowInput should be passed in almost all cases");
1108 
1109   if (aWM.IsPhysicalRTL()) {
1110     NS_ASSERTION(aContainerSize.width != NS_UNCONSTRAINEDSIZE,
1111                  "FinishReflowChild with unconstrained container width!");
1112   }
1113 
1114   nsPoint curOrigin = aKidFrame->GetPosition();
1115   const LogicalSize convertedSize = aDesiredSize.Size(aWM);
1116   LogicalPoint pos(aPos);
1117 
1118   if (aFlags & ReflowChildFlags::ApplyRelativePositioning) {
1119     MOZ_ASSERT(aReflowInput, "caller must have passed reflow input");
1120     // ApplyRelativePositioning in right-to-left writing modes needs to know
1121     // the updated frame width to set the normal position correctly.
1122     aKidFrame->SetSize(aWM, convertedSize);
1123 
1124     const LogicalMargin offsets = aReflowInput->ComputedLogicalOffsets(aWM);
1125     ReflowInput::ApplyRelativePositioning(aKidFrame, aWM, offsets, &pos,
1126                                           aContainerSize);
1127   }
1128 
1129   if (ReflowChildFlags::NoMoveFrame !=
1130       (aFlags & ReflowChildFlags::NoMoveFrame)) {
1131     aKidFrame->SetRect(aWM, LogicalRect(aWM, pos, convertedSize),
1132                        aContainerSize);
1133   } else {
1134     aKidFrame->SetSize(aWM, convertedSize);
1135   }
1136 
1137   if (aKidFrame->HasView()) {
1138     nsView* view = aKidFrame->GetView();
1139     // Make sure the frame's view is properly sized and positioned and has
1140     // things like opacity correct
1141     SyncFrameViewAfterReflow(aPresContext, aKidFrame, view,
1142                              aDesiredSize.InkOverflow(), aFlags);
1143   }
1144 
1145   nsPoint newOrigin = aKidFrame->GetPosition();
1146   if (!(aFlags & ReflowChildFlags::NoMoveView) && curOrigin != newOrigin) {
1147     if (!aKidFrame->HasView()) {
1148       // If the frame has moved, then we need to make sure any child views are
1149       // correctly positioned
1150       PositionChildViews(aKidFrame);
1151     }
1152   }
1153 
1154   aKidFrame->DidReflow(aPresContext, aReflowInput);
1155 }
1156 #if defined(_MSC_VER) && !defined(__clang__) && defined(_M_AMD64)
1157 #  pragma optimize("", on)
1158 #endif
1159 
1160 // XXX temporary: hold on to a copy of the old physical version of
1161 //    FinishReflowChild so that we can convert callers incrementally.
FinishReflowChild(nsIFrame * aKidFrame,nsPresContext * aPresContext,const ReflowOutput & aDesiredSize,const ReflowInput * aReflowInput,nscoord aX,nscoord aY,ReflowChildFlags aFlags)1162 void nsContainerFrame::FinishReflowChild(nsIFrame* aKidFrame,
1163                                          nsPresContext* aPresContext,
1164                                          const ReflowOutput& aDesiredSize,
1165                                          const ReflowInput* aReflowInput,
1166                                          nscoord aX, nscoord aY,
1167                                          ReflowChildFlags aFlags) {
1168   MOZ_ASSERT(!(aFlags & ReflowChildFlags::ApplyRelativePositioning),
1169              "only the logical version supports ApplyRelativePositioning "
1170              "since ApplyRelativePositioning requires the container size");
1171 
1172   nsPoint curOrigin = aKidFrame->GetPosition();
1173   nsPoint pos(aX, aY);
1174   nsSize size(aDesiredSize.PhysicalSize());
1175 
1176   if (ReflowChildFlags::NoMoveFrame !=
1177       (aFlags & ReflowChildFlags::NoMoveFrame)) {
1178     aKidFrame->SetRect(nsRect(pos, size));
1179   } else {
1180     aKidFrame->SetSize(size);
1181   }
1182 
1183   if (aKidFrame->HasView()) {
1184     nsView* view = aKidFrame->GetView();
1185     // Make sure the frame's view is properly sized and positioned and has
1186     // things like opacity correct
1187     SyncFrameViewAfterReflow(aPresContext, aKidFrame, view,
1188                              aDesiredSize.InkOverflow(), aFlags);
1189   }
1190 
1191   if (!(aFlags & ReflowChildFlags::NoMoveView) && curOrigin != pos) {
1192     if (!aKidFrame->HasView()) {
1193       // If the frame has moved, then we need to make sure any child views are
1194       // correctly positioned
1195       PositionChildViews(aKidFrame);
1196     }
1197   }
1198 
1199   aKidFrame->DidReflow(aPresContext, aReflowInput);
1200 }
1201 
ReflowOverflowContainerChildren(nsPresContext * aPresContext,const ReflowInput & aReflowInput,OverflowAreas & aOverflowRects,ReflowChildFlags aFlags,nsReflowStatus & aStatus,ChildFrameMerger aMergeFunc,Maybe<nsSize> aContainerSize)1202 void nsContainerFrame::ReflowOverflowContainerChildren(
1203     nsPresContext* aPresContext, const ReflowInput& aReflowInput,
1204     OverflowAreas& aOverflowRects, ReflowChildFlags aFlags,
1205     nsReflowStatus& aStatus, ChildFrameMerger aMergeFunc,
1206     Maybe<nsSize> aContainerSize) {
1207   MOZ_ASSERT(aPresContext, "null pointer");
1208 
1209   nsFrameList* overflowContainers =
1210       DrainExcessOverflowContainersList(aMergeFunc);
1211   if (!overflowContainers) {
1212     return;  // nothing to reflow
1213   }
1214 
1215   nsOverflowContinuationTracker tracker(this, false, false);
1216   bool shouldReflowAllKids = aReflowInput.ShouldReflowAllKids();
1217 
1218   for (nsIFrame* frame : *overflowContainers) {
1219     if (frame->GetPrevInFlow()->GetParent() != GetPrevInFlow()) {
1220       // frame's prevInFlow has moved, skip reflowing this frame;
1221       // it will get reflowed once it's been placed
1222       if (GetNextInFlow()) {
1223         // We report OverflowIncomplete status in this case to avoid our parent
1224         // deleting our next-in-flows which might destroy non-empty frames.
1225         nsReflowStatus status;
1226         status.SetOverflowIncomplete();
1227         aStatus.MergeCompletionStatusFrom(status);
1228       }
1229       continue;
1230     }
1231 
1232     auto ScrollableOverflowExceedsAvailableBSize =
1233         [this, &aReflowInput](nsIFrame* aFrame) {
1234           if (aReflowInput.AvailableBSize() == NS_UNCONSTRAINEDSIZE) {
1235             return false;
1236           }
1237           const auto parentWM = GetWritingMode();
1238           const nscoord scrollableOverflowRectBEnd =
1239               LogicalRect(parentWM,
1240                           aFrame->ScrollableOverflowRectRelativeToParent(),
1241                           GetSize())
1242                   .BEnd(parentWM);
1243           return scrollableOverflowRectBEnd > aReflowInput.AvailableBSize();
1244         };
1245 
1246     // If the available block-size has changed, or the existing scrollable
1247     // overflow's block-end exceeds it, we need to reflow even if the frame
1248     // isn't dirty.
1249     if (shouldReflowAllKids || frame->IsSubtreeDirty() ||
1250         ScrollableOverflowExceedsAvailableBSize(frame)) {
1251       // Get prev-in-flow
1252       nsIFrame* prevInFlow = frame->GetPrevInFlow();
1253       NS_ASSERTION(prevInFlow,
1254                    "overflow container frame must have a prev-in-flow");
1255       NS_ASSERTION(
1256           frame->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1257           "overflow container frame must have overflow container bit set");
1258       WritingMode wm = frame->GetWritingMode();
1259       nsSize containerSize = aContainerSize.valueOr(
1260           aReflowInput.AvailableSize(wm).GetPhysicalSize(wm));
1261       LogicalRect prevRect = prevInFlow->GetLogicalRect(wm, containerSize);
1262 
1263       // Initialize reflow params
1264       LogicalSize availSpace(wm, prevRect.ISize(wm),
1265                              aReflowInput.AvailableSize(wm).BSize(wm));
1266       ReflowOutput desiredSize(aReflowInput);
1267       ReflowInput frameState(aPresContext, aReflowInput, frame, availSpace);
1268       nsReflowStatus frameStatus;
1269 
1270       // Reflow
1271       LogicalPoint pos(wm, prevRect.IStart(wm), 0);
1272       ReflowChild(frame, aPresContext, desiredSize, frameState, wm, pos,
1273                   containerSize, aFlags, frameStatus, &tracker);
1274       // XXXfr Do we need to override any shrinkwrap effects here?
1275       // e.g. desiredSize.Width() = prevRect.width;
1276       FinishReflowChild(frame, aPresContext, desiredSize, &frameState, wm, pos,
1277                         containerSize, aFlags);
1278 
1279       // Handle continuations
1280       if (!frameStatus.IsFullyComplete()) {
1281         if (frame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) {
1282           // Abspos frames can't cause their parent to be incomplete,
1283           // only overflow incomplete.
1284           frameStatus.SetOverflowIncomplete();
1285         } else {
1286           NS_ASSERTION(frameStatus.IsComplete(),
1287                        "overflow container frames can't be incomplete, only "
1288                        "overflow-incomplete");
1289         }
1290 
1291         // Acquire a next-in-flow, creating it if necessary
1292         nsIFrame* nif = frame->GetNextInFlow();
1293         if (!nif) {
1294           NS_ASSERTION(frameStatus.NextInFlowNeedsReflow(),
1295                        "Someone forgot a NextInFlowNeedsReflow flag");
1296           nif = PresShell()->FrameConstructor()->CreateContinuingFrame(frame,
1297                                                                        this);
1298         } else if (!nif->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1299           // used to be a normal next-in-flow; steal it from the child list
1300           nif->GetParent()->StealFrame(nif);
1301         }
1302 
1303         tracker.Insert(nif, frameStatus);
1304       }
1305       aStatus.MergeCompletionStatusFrom(frameStatus);
1306       // At this point it would be nice to assert
1307       // !frame->GetOverflowRect().IsEmpty(), but we have some unsplittable
1308       // frames that, when taller than availableHeight will push zero-height
1309       // content into a next-in-flow.
1310     } else {
1311       tracker.Skip(frame, aStatus);
1312       if (aReflowInput.mFloatManager) {
1313         nsBlockFrame::RecoverFloatsFor(frame, *aReflowInput.mFloatManager,
1314                                        aReflowInput.GetWritingMode(),
1315                                        aReflowInput.ComputedPhysicalSize());
1316       }
1317     }
1318     ConsiderChildOverflow(aOverflowRects, frame);
1319   }
1320 }
1321 
DisplayOverflowContainers(nsDisplayListBuilder * aBuilder,const nsDisplayListSet & aLists)1322 void nsContainerFrame::DisplayOverflowContainers(
1323     nsDisplayListBuilder* aBuilder, const nsDisplayListSet& aLists) {
1324   nsFrameList* overflowconts = GetOverflowContainers();
1325   if (overflowconts) {
1326     for (nsIFrame* frame : *overflowconts) {
1327       BuildDisplayListForChild(aBuilder, frame, aLists);
1328     }
1329   }
1330 }
1331 
TryRemoveFrame(FrameListPropertyDescriptor aProp,nsIFrame * aChildToRemove)1332 bool nsContainerFrame::TryRemoveFrame(FrameListPropertyDescriptor aProp,
1333                                       nsIFrame* aChildToRemove) {
1334   nsFrameList* list = GetProperty(aProp);
1335   if (list && list->StartRemoveFrame(aChildToRemove)) {
1336     // aChildToRemove *may* have been removed from this list.
1337     if (list->IsEmpty()) {
1338       Unused << TakeProperty(aProp);
1339       list->Delete(PresShell());
1340     }
1341     return true;
1342   }
1343   return false;
1344 }
1345 
MaybeStealOverflowContainerFrame(nsIFrame * aChild)1346 bool nsContainerFrame::MaybeStealOverflowContainerFrame(nsIFrame* aChild) {
1347   bool removed = false;
1348   if (MOZ_UNLIKELY(aChild->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER))) {
1349     // Try removing from the overflow container list.
1350     removed = TryRemoveFrame(OverflowContainersProperty(), aChild);
1351     if (!removed) {
1352       // It might be in the excess overflow container list.
1353       removed = TryRemoveFrame(ExcessOverflowContainersProperty(), aChild);
1354     }
1355   }
1356   return removed;
1357 }
1358 
StealFrame(nsIFrame * aChild)1359 void nsContainerFrame::StealFrame(nsIFrame* aChild) {
1360 #ifdef DEBUG
1361   if (!mFrames.ContainsFrame(aChild)) {
1362     nsFrameList* list = GetOverflowFrames();
1363     if (!list || !list->ContainsFrame(aChild)) {
1364       list = GetOverflowContainers();
1365       if (!list || !list->ContainsFrame(aChild)) {
1366         list = GetExcessOverflowContainers();
1367         MOZ_ASSERT(list && list->ContainsFrame(aChild),
1368                    "aChild isn't our child"
1369                    " or on a frame list not supported by StealFrame");
1370       }
1371     }
1372   }
1373 #endif
1374 
1375   if (MaybeStealOverflowContainerFrame(aChild)) {
1376     return;
1377   }
1378 
1379   // NOTE nsColumnSetFrame and nsCanvasFrame have their overflow containers
1380   // on the normal lists so we might get here also if the frame bit
1381   // NS_FRAME_IS_OVERFLOW_CONTAINER is set.
1382   if (mFrames.StartRemoveFrame(aChild)) {
1383     return;
1384   }
1385 
1386   // We didn't find the child in our principal child list.
1387   // Maybe it's on the overflow list?
1388   nsFrameList* frameList = GetOverflowFrames();
1389   if (frameList && frameList->ContinueRemoveFrame(aChild)) {
1390     if (frameList->IsEmpty()) {
1391       DestroyOverflowList();
1392     }
1393     return;
1394   }
1395 
1396   MOZ_ASSERT_UNREACHABLE("StealFrame: can't find aChild");
1397 }
1398 
StealFramesAfter(nsIFrame * aChild)1399 nsFrameList nsContainerFrame::StealFramesAfter(nsIFrame* aChild) {
1400   NS_ASSERTION(
1401       !aChild || !aChild->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1402       "StealFramesAfter doesn't handle overflow containers");
1403   NS_ASSERTION(!IsBlockFrame(), "unexpected call");
1404 
1405   if (!aChild) {
1406     nsFrameList copy(mFrames);
1407     mFrames.Clear();
1408     return copy;
1409   }
1410 
1411   for (nsFrameList::FrameLinkEnumerator iter(mFrames); !iter.AtEnd();
1412        iter.Next()) {
1413     if (iter.PrevFrame() == aChild) {
1414       return mFrames.ExtractTail(iter);
1415     }
1416   }
1417 
1418   // We didn't find the child in the principal child list.
1419   // Maybe it's on the overflow list?
1420   nsFrameList* overflowFrames = GetOverflowFrames();
1421   if (overflowFrames) {
1422     for (nsFrameList::FrameLinkEnumerator iter(*overflowFrames); !iter.AtEnd();
1423          iter.Next()) {
1424       if (iter.PrevFrame() == aChild) {
1425         return overflowFrames->ExtractTail(iter);
1426       }
1427     }
1428   }
1429 
1430   NS_ERROR("StealFramesAfter: can't find aChild");
1431   return nsFrameList::EmptyList();
1432 }
1433 
1434 /*
1435  * Create a next-in-flow for aFrame. Will return the newly created
1436  * frame <b>if and only if</b> a new frame is created; otherwise
1437  * nullptr is returned.
1438  */
CreateNextInFlow(nsIFrame * aFrame)1439 nsIFrame* nsContainerFrame::CreateNextInFlow(nsIFrame* aFrame) {
1440   MOZ_ASSERT(
1441       !IsBlockFrame(),
1442       "you should have called nsBlockFrame::CreateContinuationFor instead");
1443   MOZ_ASSERT(mFrames.ContainsFrame(aFrame), "expected an in-flow child frame");
1444 
1445   nsIFrame* nextInFlow = aFrame->GetNextInFlow();
1446   if (nullptr == nextInFlow) {
1447     // Create a continuation frame for the child frame and insert it
1448     // into our child list.
1449     nextInFlow =
1450         PresShell()->FrameConstructor()->CreateContinuingFrame(aFrame, this);
1451     mFrames.InsertFrame(nullptr, aFrame, nextInFlow);
1452 
1453     NS_FRAME_LOG(NS_FRAME_TRACE_NEW_FRAMES,
1454                  ("nsContainerFrame::CreateNextInFlow: frame=%p nextInFlow=%p",
1455                   aFrame, nextInFlow));
1456 
1457     return nextInFlow;
1458   }
1459   return nullptr;
1460 }
1461 
1462 /**
1463  * Remove and delete aNextInFlow and its next-in-flows. Updates the sibling and
1464  * flow pointers
1465  */
DeleteNextInFlowChild(nsIFrame * aNextInFlow,bool aDeletingEmptyFrames)1466 void nsContainerFrame::DeleteNextInFlowChild(nsIFrame* aNextInFlow,
1467                                              bool aDeletingEmptyFrames) {
1468 #ifdef DEBUG
1469   nsIFrame* prevInFlow = aNextInFlow->GetPrevInFlow();
1470 #endif
1471   MOZ_ASSERT(prevInFlow, "bad prev-in-flow");
1472 
1473   // If the next-in-flow has a next-in-flow then delete it, too (and
1474   // delete it first).
1475   // Do this in a loop so we don't overflow the stack for frames
1476   // with very many next-in-flows
1477   nsIFrame* nextNextInFlow = aNextInFlow->GetNextInFlow();
1478   if (nextNextInFlow) {
1479     AutoTArray<nsIFrame*, 8> frames;
1480     for (nsIFrame* f = nextNextInFlow; f; f = f->GetNextInFlow()) {
1481       frames.AppendElement(f);
1482     }
1483     for (nsIFrame* delFrame : Reversed(frames)) {
1484       delFrame->GetParent()->DeleteNextInFlowChild(delFrame,
1485                                                    aDeletingEmptyFrames);
1486     }
1487   }
1488 
1489   // Take the next-in-flow out of the parent's child list
1490   StealFrame(aNextInFlow);
1491 
1492 #ifdef DEBUG
1493   if (aDeletingEmptyFrames) {
1494     nsLayoutUtils::AssertTreeOnlyEmptyNextInFlows(aNextInFlow);
1495   }
1496 #endif
1497 
1498   // Delete the next-in-flow frame and its descendants. This will also
1499   // remove it from its next-in-flow/prev-in-flow chain.
1500   aNextInFlow->Destroy();
1501 
1502   MOZ_ASSERT(!prevInFlow->GetNextInFlow(), "non null next-in-flow");
1503 }
1504 
PushChildrenToOverflow(nsIFrame * aFromChild,nsIFrame * aPrevSibling)1505 void nsContainerFrame::PushChildrenToOverflow(nsIFrame* aFromChild,
1506                                               nsIFrame* aPrevSibling) {
1507   MOZ_ASSERT(aFromChild, "null pointer");
1508   MOZ_ASSERT(aPrevSibling, "pushing first child");
1509   MOZ_ASSERT(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
1510 
1511   // Add the frames to our overflow list (let our next in flow drain
1512   // our overflow list when it is ready)
1513   SetOverflowFrames(mFrames.RemoveFramesAfter(aPrevSibling));
1514 }
1515 
PushChildren(nsIFrame * aFromChild,nsIFrame * aPrevSibling)1516 void nsContainerFrame::PushChildren(nsIFrame* aFromChild,
1517                                     nsIFrame* aPrevSibling) {
1518   MOZ_ASSERT(aFromChild, "null pointer");
1519   MOZ_ASSERT(aPrevSibling, "pushing first child");
1520   MOZ_ASSERT(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
1521 
1522   // Disconnect aFromChild from its previous sibling
1523   nsFrameList tail = mFrames.RemoveFramesAfter(aPrevSibling);
1524 
1525   nsContainerFrame* nextInFlow =
1526       static_cast<nsContainerFrame*>(GetNextInFlow());
1527   if (nextInFlow) {
1528     // XXX This is not a very good thing to do. If it gets removed
1529     // then remove the copy of this routine that doesn't do this from
1530     // nsInlineFrame.
1531     // When pushing and pulling frames we need to check for whether any
1532     // views need to be reparented.
1533     for (nsIFrame* f = aFromChild; f; f = f->GetNextSibling()) {
1534       nsContainerFrame::ReparentFrameView(f, this, nextInFlow);
1535     }
1536     nextInFlow->mFrames.InsertFrames(nextInFlow, nullptr, tail);
1537   } else {
1538     // Add the frames to our overflow list
1539     SetOverflowFrames(std::move(tail));
1540   }
1541 }
1542 
PushIncompleteChildren(const FrameHashtable & aPushedItems,const FrameHashtable & aIncompleteItems,const FrameHashtable & aOverflowIncompleteItems)1543 bool nsContainerFrame::PushIncompleteChildren(
1544     const FrameHashtable& aPushedItems, const FrameHashtable& aIncompleteItems,
1545     const FrameHashtable& aOverflowIncompleteItems) {
1546   MOZ_ASSERT(IsFlexOrGridContainer(),
1547              "Only Grid / Flex containers can call this!");
1548 
1549   if (aPushedItems.IsEmpty() && aIncompleteItems.IsEmpty() &&
1550       aOverflowIncompleteItems.IsEmpty()) {
1551     return false;
1552   }
1553 
1554   // Iterate the children in normal document order and append them (or a NIF)
1555   // to one of the following frame lists according to their status.
1556   nsFrameList pushedList;
1557   nsFrameList incompleteList;
1558   nsFrameList overflowIncompleteList;
1559   auto* fc = PresShell()->FrameConstructor();
1560   for (nsIFrame* child = GetChildList(kPrincipalList).FirstChild(); child;) {
1561     MOZ_ASSERT((aPushedItems.Contains(child) ? 1 : 0) +
1562                        (aIncompleteItems.Contains(child) ? 1 : 0) +
1563                        (aOverflowIncompleteItems.Contains(child) ? 1 : 0) <=
1564                    1,
1565                "child should only be in one of these sets");
1566     // Save the next-sibling so we can continue the loop if |child| is moved.
1567     nsIFrame* next = child->GetNextSibling();
1568     if (aPushedItems.Contains(child)) {
1569       MOZ_ASSERT(child->GetParent() == this);
1570       StealFrame(child);
1571       pushedList.AppendFrame(nullptr, child);
1572     } else if (aIncompleteItems.Contains(child)) {
1573       nsIFrame* childNIF = child->GetNextInFlow();
1574       if (!childNIF) {
1575         childNIF = fc->CreateContinuingFrame(child, this);
1576         incompleteList.AppendFrame(nullptr, childNIF);
1577       } else {
1578         auto* parent = childNIF->GetParent();
1579         MOZ_ASSERT(parent != this || !mFrames.ContainsFrame(childNIF),
1580                    "child's NIF shouldn't be in the same principal list");
1581         // If child's existing NIF is an overflow container, convert it to an
1582         // actual NIF, since now |child| has non-overflow stuff to give it.
1583         // Or, if it's further away then our next-in-flow, then pull it up.
1584         if (childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) ||
1585             (parent != this && parent != GetNextInFlow())) {
1586           parent->StealFrame(childNIF);
1587           childNIF->RemoveStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1588           if (parent == this) {
1589             incompleteList.AppendFrame(nullptr, childNIF);
1590           } else {
1591             // If childNIF already lives on the next fragment, then we
1592             // don't need to reparent it, since we know it's destined to end
1593             // up there anyway.  Just move it to its parent's overflow list.
1594             if (parent == GetNextInFlow()) {
1595               nsFrameList toMove(childNIF, childNIF);
1596               parent->MergeSortedOverflow(toMove);
1597             } else {
1598               ReparentFrame(childNIF, parent, this);
1599               incompleteList.AppendFrame(nullptr, childNIF);
1600             }
1601           }
1602         }
1603       }
1604     } else if (aOverflowIncompleteItems.Contains(child)) {
1605       nsIFrame* childNIF = child->GetNextInFlow();
1606       if (!childNIF) {
1607         childNIF = fc->CreateContinuingFrame(child, this);
1608         childNIF->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1609         overflowIncompleteList.AppendFrame(nullptr, childNIF);
1610       } else {
1611         DebugOnly<nsContainerFrame*> lastParent = this;
1612         auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1613         // If child has any non-overflow-container NIFs, convert them to
1614         // overflow containers, since that's all |child| needs now.
1615         while (childNIF &&
1616                !childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1617           auto* parent = childNIF->GetParent();
1618           parent->StealFrame(childNIF);
1619           childNIF->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
1620           if (parent == this) {
1621             overflowIncompleteList.AppendFrame(nullptr, childNIF);
1622           } else {
1623             if (!nif || parent == nif) {
1624               nsFrameList toMove(childNIF, childNIF);
1625               parent->MergeSortedExcessOverflowContainers(toMove);
1626             } else {
1627               ReparentFrame(childNIF, parent, nif);
1628               nsFrameList toMove(childNIF, childNIF);
1629               nif->MergeSortedExcessOverflowContainers(toMove);
1630             }
1631             // We only need to reparent the first childNIF (or not at all if
1632             // its parent is our NIF).
1633             nif = nullptr;
1634           }
1635           lastParent = parent;
1636           childNIF = childNIF->GetNextInFlow();
1637         }
1638       }
1639     }
1640     child = next;
1641   }
1642 
1643   // Merge the results into our respective overflow child lists.
1644   if (!pushedList.IsEmpty()) {
1645     MergeSortedOverflow(pushedList);
1646   }
1647   if (!incompleteList.IsEmpty()) {
1648     MergeSortedOverflow(incompleteList);
1649   }
1650   if (!overflowIncompleteList.IsEmpty()) {
1651     // If our next-in-flow already has overflow containers list, merge the
1652     // overflowIncompleteList into that list. Otherwise, merge it into our
1653     // excess overflow containers list, to be drained by our next-in-flow.
1654     auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1655     nsFrameList* oc = nif ? nif->GetOverflowContainers() : nullptr;
1656     if (oc) {
1657       ReparentFrames(overflowIncompleteList, this, nif);
1658       MergeSortedFrameLists(*oc, overflowIncompleteList, GetContent());
1659     } else {
1660       MergeSortedExcessOverflowContainers(overflowIncompleteList);
1661     }
1662   }
1663   return true;
1664 }
1665 
NormalizeChildLists()1666 void nsContainerFrame::NormalizeChildLists() {
1667   MOZ_ASSERT(IsFlexOrGridContainer(),
1668              "Only Flex / Grid containers can call this!");
1669 
1670   // Note: the following description uses grid container as an example. Flex
1671   // container is similar.
1672   //
1673   // First we gather child frames we should include in our reflow/placement,
1674   // i.e. overflowed children from our prev-in-flow, and pushed first-in-flow
1675   // children (that might now fit). It's important to note that these children
1676   // can be in arbitrary order vis-a-vis the current children in our lists.
1677   // E.g. grid items in the document order: A, B, C may be placed in the rows
1678   // 3, 2, 1.  Assume each row goes in a separate grid container fragment,
1679   // and we reflow the second fragment.  Now if C (in fragment 1) overflows,
1680   // we can't just prepend it to our mFrames like we usually do because that
1681   // would violate the document order invariant that other code depends on.
1682   // Similarly if we pull up child A (from fragment 3) we can't just append
1683   // that for the same reason.  Instead, we must sort these children into
1684   // our child lists.  (The sorting is trivial given that both lists are
1685   // already fully sorted individually - it's just a merge.)
1686   //
1687   // The invariants that we maintain are that each grid container child list
1688   // is sorted in the normal document order at all times, but that children
1689   // in different grid container continuations may be in arbitrary order.
1690 
1691   const auto didPushItemsBit = IsFlexContainerFrame()
1692                                    ? NS_STATE_FLEX_DID_PUSH_ITEMS
1693                                    : NS_STATE_GRID_DID_PUSH_ITEMS;
1694   const auto hasChildNifBit = IsFlexContainerFrame()
1695                                   ? NS_STATE_FLEX_HAS_CHILD_NIFS
1696                                   : NS_STATE_GRID_HAS_CHILD_NIFS;
1697 
1698   auto* prevInFlow = static_cast<nsContainerFrame*>(GetPrevInFlow());
1699   // Merge overflow frames from our prev-in-flow into our principal child list.
1700   if (prevInFlow) {
1701     AutoFrameListPtr overflow(PresContext(), prevInFlow->StealOverflowFrames());
1702     if (overflow) {
1703       ReparentFrames(*overflow, prevInFlow, this);
1704       MergeSortedFrameLists(mFrames, *overflow, GetContent());
1705 
1706       // Move trailing next-in-flows into our overflow list.
1707       nsFrameList continuations;
1708       for (nsIFrame* f = mFrames.FirstChild(); f;) {
1709         nsIFrame* next = f->GetNextSibling();
1710         nsIFrame* pif = f->GetPrevInFlow();
1711         if (pif && pif->GetParent() == this) {
1712           mFrames.RemoveFrame(f);
1713           continuations.AppendFrame(nullptr, f);
1714         }
1715         f = next;
1716       }
1717       MergeSortedOverflow(continuations);
1718 
1719       // Move prev-in-flow's excess overflow containers list into our own
1720       // overflow containers list. If we already have an excess overflow
1721       // containers list, any child in that list which doesn't have a
1722       // prev-in-flow in this frame is also merged into our overflow container
1723       // list.
1724       nsFrameList* overflowContainers =
1725           DrainExcessOverflowContainersList(MergeSortedFrameListsFor);
1726 
1727       // Move trailing OC next-in-flows into our excess overflow containers
1728       // list.
1729       if (overflowContainers) {
1730         nsFrameList moveToEOC;
1731         for (nsIFrame* f = overflowContainers->FirstChild(); f;) {
1732           nsIFrame* next = f->GetNextSibling();
1733           nsIFrame* pif = f->GetPrevInFlow();
1734           if (pif && pif->GetParent() == this) {
1735             overflowContainers->RemoveFrame(f);
1736             moveToEOC.AppendFrame(nullptr, f);
1737           }
1738           f = next;
1739         }
1740         if (overflowContainers->IsEmpty()) {
1741           DestroyOverflowContainers();
1742         }
1743         MergeSortedExcessOverflowContainers(moveToEOC);
1744       }
1745     }
1746   }
1747 
1748   // For each item in aItems, pull up its next-in-flow (if any), and reparent it
1749   // to our next-in-flow, unless its parent is already ourselves or our
1750   // next-in-flow (to avoid leaving a hole there).
1751   auto PullItemsNextInFlow = [this](const nsFrameList& aItems) {
1752     auto* firstNIF = static_cast<nsContainerFrame*>(GetNextInFlow());
1753     if (!firstNIF) {
1754       return;
1755     }
1756     nsFrameList childNIFs;
1757     nsFrameList childOCNIFs;
1758     for (auto* child : aItems) {
1759       if (auto* childNIF = child->GetNextInFlow()) {
1760         if (auto* parent = childNIF->GetParent();
1761             parent != this && parent != firstNIF) {
1762           parent->StealFrame(childNIF);
1763           ReparentFrame(childNIF, parent, firstNIF);
1764           if (childNIF->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
1765             childOCNIFs.AppendFrame(nullptr, childNIF);
1766           } else {
1767             childNIFs.AppendFrame(nullptr, childNIF);
1768           }
1769         }
1770       }
1771     }
1772     // Merge aItems' NIFs into our NIF's respective overflow child lists.
1773     firstNIF->MergeSortedOverflow(childNIFs);
1774     firstNIF->MergeSortedExcessOverflowContainers(childOCNIFs);
1775   };
1776 
1777   // Merge our own overflow frames into our principal child list,
1778   // except those that are a next-in-flow for one of our items.
1779   DebugOnly<bool> foundOwnPushedChild = false;
1780   {
1781     nsFrameList* ourOverflow = GetOverflowFrames();
1782     if (ourOverflow) {
1783       nsFrameList items;
1784       for (nsIFrame* f = ourOverflow->FirstChild(); f;) {
1785         nsIFrame* next = f->GetNextSibling();
1786         nsIFrame* pif = f->GetPrevInFlow();
1787         if (!pif || pif->GetParent() != this) {
1788           MOZ_ASSERT(f->GetParent() == this);
1789           ourOverflow->RemoveFrame(f);
1790           items.AppendFrame(nullptr, f);
1791           if (!pif) {
1792             foundOwnPushedChild = true;
1793           }
1794         }
1795         f = next;
1796       }
1797 
1798       if (ourOverflow->IsEmpty()) {
1799         DestroyOverflowList();
1800         ourOverflow = nullptr;
1801       }
1802       if (items.NotEmpty()) {
1803         PullItemsNextInFlow(items);
1804       }
1805       MergeSortedFrameLists(mFrames, items, GetContent());
1806     }
1807   }
1808 
1809   // Push any child next-in-flows in our principal list to OverflowList.
1810   if (HasAnyStateBits(hasChildNifBit)) {
1811     nsFrameList framesToPush;
1812     nsIFrame* firstChild = mFrames.FirstChild();
1813     // Note that we potentially modify our mFrames list as we go.
1814     for (auto* child = firstChild; child; child = child->GetNextSibling()) {
1815       if (auto* childNIF = child->GetNextInFlow()) {
1816         if (childNIF->GetParent() == this) {
1817           for (auto* c = child->GetNextSibling(); c; c = c->GetNextSibling()) {
1818             if (c == childNIF) {
1819               // child's next-in-flow is in our principal child list, push it.
1820               mFrames.RemoveFrame(childNIF);
1821               framesToPush.AppendFrame(nullptr, childNIF);
1822               break;
1823             }
1824           }
1825         }
1826       }
1827     }
1828     if (!framesToPush.IsEmpty()) {
1829       MergeSortedOverflow(framesToPush);
1830     }
1831     RemoveStateBits(hasChildNifBit);
1832   }
1833 
1834   // Pull up any first-in-flow children we might have pushed.
1835   if (HasAnyStateBits(didPushItemsBit)) {
1836     RemoveStateBits(didPushItemsBit);
1837     nsFrameList items;
1838     auto* nif = static_cast<nsContainerFrame*>(GetNextInFlow());
1839     DebugOnly<bool> nifNeedPushedItem = false;
1840     while (nif) {
1841       nsFrameList nifItems;
1842       for (nsIFrame* nifChild = nif->GetChildList(kPrincipalList).FirstChild();
1843            nifChild;) {
1844         nsIFrame* next = nifChild->GetNextSibling();
1845         if (!nifChild->GetPrevInFlow()) {
1846           nif->StealFrame(nifChild);
1847           ReparentFrame(nifChild, nif, this);
1848           nifItems.AppendFrame(nullptr, nifChild);
1849           nifNeedPushedItem = false;
1850         }
1851         nifChild = next;
1852       }
1853       MergeSortedFrameLists(items, nifItems, GetContent());
1854 
1855       if (!nif->HasAnyStateBits(didPushItemsBit)) {
1856         MOZ_ASSERT(!nifNeedPushedItem || mDidPushItemsBitMayLie,
1857                    "The state bit stored in didPushItemsBit lied!");
1858         break;
1859       }
1860       nifNeedPushedItem = true;
1861 
1862       for (nsIFrame* nifChild = nif->GetChildList(kOverflowList).FirstChild();
1863            nifChild;) {
1864         nsIFrame* next = nifChild->GetNextSibling();
1865         if (!nifChild->GetPrevInFlow()) {
1866           nif->StealFrame(nifChild);
1867           ReparentFrame(nifChild, nif, this);
1868           nifItems.AppendFrame(nullptr, nifChild);
1869           nifNeedPushedItem = false;
1870         }
1871         nifChild = next;
1872       }
1873       MergeSortedFrameLists(items, nifItems, GetContent());
1874 
1875       nif->RemoveStateBits(didPushItemsBit);
1876       nif = static_cast<nsContainerFrame*>(nif->GetNextInFlow());
1877       MOZ_ASSERT(nif || !nifNeedPushedItem || mDidPushItemsBitMayLie,
1878                  "The state bit stored in didPushItemsBit lied!");
1879     }
1880 
1881     if (!items.IsEmpty()) {
1882       PullItemsNextInFlow(items);
1883     }
1884 
1885     MOZ_ASSERT(
1886         foundOwnPushedChild || !items.IsEmpty() || mDidPushItemsBitMayLie,
1887         "The state bit stored in didPushItemsBit lied!");
1888     MergeSortedFrameLists(mFrames, items, GetContent());
1889   }
1890 }
1891 
NoteNewChildren(ChildListID aListID,const nsFrameList & aFrameList)1892 void nsContainerFrame::NoteNewChildren(ChildListID aListID,
1893                                        const nsFrameList& aFrameList) {
1894   MOZ_ASSERT(aListID == kPrincipalList, "unexpected child list");
1895   MOZ_ASSERT(IsFlexOrGridContainer(),
1896              "Only Flex / Grid containers can call this!");
1897 
1898   mozilla::PresShell* presShell = PresShell();
1899   const auto didPushItemsBit = IsFlexContainerFrame()
1900                                    ? NS_STATE_FLEX_DID_PUSH_ITEMS
1901                                    : NS_STATE_GRID_DID_PUSH_ITEMS;
1902   for (auto* pif = GetPrevInFlow(); pif; pif = pif->GetPrevInFlow()) {
1903     pif->AddStateBits(didPushItemsBit);
1904     presShell->FrameNeedsReflow(pif, IntrinsicDirty::TreeChange,
1905                                 NS_FRAME_IS_DIRTY);
1906   }
1907 }
1908 
MoveOverflowToChildList()1909 bool nsContainerFrame::MoveOverflowToChildList() {
1910   bool result = false;
1911 
1912   // Check for an overflow list with our prev-in-flow
1913   nsContainerFrame* prevInFlow = (nsContainerFrame*)GetPrevInFlow();
1914   if (nullptr != prevInFlow) {
1915     AutoFrameListPtr prevOverflowFrames(PresContext(),
1916                                         prevInFlow->StealOverflowFrames());
1917     if (prevOverflowFrames) {
1918       // Tables are special; they can have repeated header/footer
1919       // frames on mFrames at this point.
1920       NS_ASSERTION(mFrames.IsEmpty() || IsTableFrame(), "bad overflow list");
1921       // When pushing and pulling frames we need to check for whether any
1922       // views need to be reparented.
1923       nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
1924                                               this);
1925       mFrames.AppendFrames(this, *prevOverflowFrames);
1926       result = true;
1927     }
1928   }
1929 
1930   // It's also possible that we have an overflow list for ourselves.
1931   return DrainSelfOverflowList() || result;
1932 }
1933 
MergeSortedOverflow(nsFrameList & aList)1934 void nsContainerFrame::MergeSortedOverflow(nsFrameList& aList) {
1935   if (aList.IsEmpty()) {
1936     return;
1937   }
1938   MOZ_ASSERT(
1939       !aList.FirstChild()->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1940       "this is the wrong list to put this child frame");
1941   MOZ_ASSERT(aList.FirstChild()->GetParent() == this);
1942   nsFrameList* overflow = GetOverflowFrames();
1943   if (overflow) {
1944     MergeSortedFrameLists(*overflow, aList, GetContent());
1945   } else {
1946     SetOverflowFrames(std::move(aList));
1947   }
1948 }
1949 
MergeSortedExcessOverflowContainers(nsFrameList & aList)1950 void nsContainerFrame::MergeSortedExcessOverflowContainers(nsFrameList& aList) {
1951   if (aList.IsEmpty()) {
1952     return;
1953   }
1954   MOZ_ASSERT(
1955       aList.FirstChild()->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER),
1956       "this is the wrong list to put this child frame");
1957   MOZ_ASSERT(aList.FirstChild()->GetParent() == this);
1958   if (nsFrameList* eoc = GetExcessOverflowContainers()) {
1959     MergeSortedFrameLists(*eoc, aList, GetContent());
1960   } else {
1961     SetExcessOverflowContainers(std::move(aList));
1962   }
1963 }
1964 
GetFirstNonAnonBoxInSubtree(nsIFrame * aFrame)1965 nsIFrame* nsContainerFrame::GetFirstNonAnonBoxInSubtree(nsIFrame* aFrame) {
1966   while (aFrame) {
1967     // If aFrame isn't an anonymous container, or it's text or such, then it'll
1968     // do.
1969     if (!aFrame->Style()->IsAnonBox() ||
1970         nsCSSAnonBoxes::IsNonElement(aFrame->Style()->GetPseudoType())) {
1971       break;
1972     }
1973 
1974     // Otherwise, descend to its first child and repeat.
1975 
1976     // SPECIAL CASE: if we're dealing with an anonymous table, then it might
1977     // be wrapping something non-anonymous in its caption or col-group lists
1978     // (instead of its principal child list), so we have to look there.
1979     // (Note: For anonymous tables that have a non-anon cell *and* a non-anon
1980     // column, we'll always return the column. This is fine; we're really just
1981     // looking for a handle to *anything* with a meaningful content node inside
1982     // the table, for use in DOM comparisons to things outside of the table.)
1983     if (MOZ_UNLIKELY(aFrame->IsTableWrapperFrame())) {
1984       nsIFrame* captionDescendant = GetFirstNonAnonBoxInSubtree(
1985           aFrame->GetChildList(kCaptionList).FirstChild());
1986       if (captionDescendant) {
1987         return captionDescendant;
1988       }
1989     } else if (MOZ_UNLIKELY(aFrame->IsTableFrame())) {
1990       nsIFrame* colgroupDescendant = GetFirstNonAnonBoxInSubtree(
1991           aFrame->GetChildList(kColGroupList).FirstChild());
1992       if (colgroupDescendant) {
1993         return colgroupDescendant;
1994       }
1995     }
1996 
1997     // USUAL CASE: Descend to the first child in principal list.
1998     aFrame = aFrame->PrincipalChildList().FirstChild();
1999   }
2000   return aFrame;
2001 }
2002 
2003 /**
2004  * Is aFrame1 a prev-continuation of aFrame2?
2005  */
IsPrevContinuationOf(nsIFrame * aFrame1,nsIFrame * aFrame2)2006 static bool IsPrevContinuationOf(nsIFrame* aFrame1, nsIFrame* aFrame2) {
2007   nsIFrame* prev = aFrame2;
2008   while ((prev = prev->GetPrevContinuation())) {
2009     if (prev == aFrame1) {
2010       return true;
2011     }
2012   }
2013   return false;
2014 }
2015 
MergeSortedFrameLists(nsFrameList & aDest,nsFrameList & aSrc,nsIContent * aCommonAncestor)2016 void nsContainerFrame::MergeSortedFrameLists(nsFrameList& aDest,
2017                                              nsFrameList& aSrc,
2018                                              nsIContent* aCommonAncestor) {
2019   // Returns a frame whose DOM node can be used for the purpose of ordering
2020   // aFrame among its sibling frames by DOM position. If aFrame is
2021   // non-anonymous, this just returns aFrame itself. Otherwise, this returns the
2022   // first non-anonymous descendant in aFrame's continuation chain.
2023   auto FrameForDOMPositionComparison = [](nsIFrame* aFrame) {
2024     if (!aFrame->Style()->IsAnonBox()) {
2025       // The usual case.
2026       return aFrame;
2027     }
2028 
2029     // Walk the continuation chain from the start, and return the first
2030     // non-anonymous descendant that we find.
2031     for (nsIFrame* f = aFrame->FirstContinuation(); f;
2032          f = f->GetNextContinuation()) {
2033       if (nsIFrame* nonAnonBox = GetFirstNonAnonBoxInSubtree(f)) {
2034         return nonAnonBox;
2035       }
2036     }
2037 
2038     MOZ_ASSERT_UNREACHABLE(
2039         "Why is there no non-anonymous descendants in the continuation chain?");
2040     return aFrame;
2041   };
2042 
2043   nsIFrame* dest = aDest.FirstChild();
2044   for (nsIFrame* src = aSrc.FirstChild(); src;) {
2045     if (!dest) {
2046       aDest.AppendFrames(nullptr, aSrc);
2047       break;
2048     }
2049     nsIContent* srcContent = FrameForDOMPositionComparison(src)->GetContent();
2050     nsIContent* destContent = FrameForDOMPositionComparison(dest)->GetContent();
2051     int32_t result = nsLayoutUtils::CompareTreePosition(srcContent, destContent,
2052                                                         aCommonAncestor);
2053     if (MOZ_UNLIKELY(result == 0)) {
2054       // NOTE: we get here when comparing ::before/::after for the same element.
2055       if (MOZ_UNLIKELY(srcContent->IsGeneratedContentContainerForBefore())) {
2056         if (MOZ_LIKELY(!destContent->IsGeneratedContentContainerForBefore()) ||
2057             ::IsPrevContinuationOf(src, dest)) {
2058           result = -1;
2059         }
2060       } else if (MOZ_UNLIKELY(
2061                      srcContent->IsGeneratedContentContainerForAfter())) {
2062         if (MOZ_UNLIKELY(destContent->IsGeneratedContentContainerForAfter()) &&
2063             ::IsPrevContinuationOf(src, dest)) {
2064           result = -1;
2065         }
2066       } else if (::IsPrevContinuationOf(src, dest)) {
2067         result = -1;
2068       }
2069     }
2070     if (result < 0) {
2071       // src should come before dest
2072       nsIFrame* next = src->GetNextSibling();
2073       aSrc.RemoveFrame(src);
2074       aDest.InsertFrame(nullptr, dest->GetPrevSibling(), src);
2075       src = next;
2076     } else {
2077       dest = dest->GetNextSibling();
2078     }
2079   }
2080   MOZ_ASSERT(aSrc.IsEmpty());
2081 }
2082 
MoveInlineOverflowToChildList(nsIFrame * aLineContainer)2083 bool nsContainerFrame::MoveInlineOverflowToChildList(nsIFrame* aLineContainer) {
2084   MOZ_ASSERT(aLineContainer,
2085              "Must have line container for moving inline overflows");
2086 
2087   bool result = false;
2088 
2089   // Check for an overflow list with our prev-in-flow
2090   if (auto prevInFlow = static_cast<nsContainerFrame*>(GetPrevInFlow())) {
2091     AutoFrameListPtr prevOverflowFrames(PresContext(),
2092                                         prevInFlow->StealOverflowFrames());
2093     if (prevOverflowFrames) {
2094       // We may need to reparent floats from prev-in-flow to our line
2095       // container if the container has prev continuation.
2096       if (aLineContainer->GetPrevContinuation()) {
2097         ReparentFloatsForInlineChild(aLineContainer,
2098                                      prevOverflowFrames->FirstChild(), true);
2099       }
2100       // When pushing and pulling frames we need to check for whether
2101       // any views need to be reparented.
2102       nsContainerFrame::ReparentFrameViewList(*prevOverflowFrames, prevInFlow,
2103                                               this);
2104       // Prepend overflow frames to the list.
2105       mFrames.InsertFrames(this, nullptr, *prevOverflowFrames);
2106       result = true;
2107     }
2108   }
2109 
2110   // It's also possible that we have overflow list for ourselves.
2111   return DrainSelfOverflowList() || result;
2112 }
2113 
DrainSelfOverflowList()2114 bool nsContainerFrame::DrainSelfOverflowList() {
2115   AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
2116   if (overflowFrames) {
2117     mFrames.AppendFrames(nullptr, *overflowFrames);
2118     return true;
2119   }
2120   return false;
2121 }
2122 
DrainAndMergeSelfOverflowList()2123 bool nsContainerFrame::DrainAndMergeSelfOverflowList() {
2124   MOZ_ASSERT(IsFlexOrGridContainer(),
2125              "Only Flex / Grid containers can call this!");
2126 
2127   // Unlike nsContainerFrame::DrainSelfOverflowList, flex or grid containers
2128   // need to merge these lists so that the resulting mFrames is in document
2129   // content order.
2130   // NOTE: nsContainerFrame::AppendFrames/InsertFrames calls this method and
2131   // there are also direct calls from the fctor (FindAppendPrevSibling).
2132   AutoFrameListPtr overflowFrames(PresContext(), StealOverflowFrames());
2133   if (overflowFrames) {
2134     MergeSortedFrameLists(mFrames, *overflowFrames, GetContent());
2135     // We set a frame bit to push them again in Reflow() to avoid creating
2136     // multiple flex / grid items per flex / grid container fragment for the
2137     // same content.
2138     AddStateBits(IsFlexContainerFrame() ? NS_STATE_FLEX_HAS_CHILD_NIFS
2139                                         : NS_STATE_GRID_HAS_CHILD_NIFS);
2140     return true;
2141   }
2142   return false;
2143 }
2144 
DrainExcessOverflowContainersList(ChildFrameMerger aMergeFunc)2145 nsFrameList* nsContainerFrame::DrainExcessOverflowContainersList(
2146     ChildFrameMerger aMergeFunc) {
2147   nsFrameList* overflowContainers = GetOverflowContainers();
2148 
2149   // Drain excess overflow containers from our prev-in-flow.
2150   if (auto* prev = static_cast<nsContainerFrame*>(GetPrevInFlow())) {
2151     AutoFrameListPtr excessFrames(PresContext(),
2152                                   prev->StealExcessOverflowContainers());
2153     if (excessFrames) {
2154       excessFrames->ApplySetParent(this);
2155       nsContainerFrame::ReparentFrameViewList(*excessFrames, prev, this);
2156       if (overflowContainers) {
2157         // The default merge function is AppendFrames, so we use excessFrames as
2158         // the destination and then assign the result to overflowContainers.
2159         aMergeFunc(*excessFrames, *overflowContainers, this);
2160         *overflowContainers = std::move(*excessFrames);
2161       } else {
2162         overflowContainers = SetOverflowContainers(std::move(*excessFrames));
2163       }
2164     }
2165   }
2166 
2167   // Our own excess overflow containers from a previous reflow can still be
2168   // present if our next-in-flow hasn't been reflown yet.  Move any children
2169   // from it that don't have a continuation in this frame to the
2170   // OverflowContainers list.
2171   AutoFrameListPtr selfExcessOCFrames(PresContext(),
2172                                       StealExcessOverflowContainers());
2173   if (selfExcessOCFrames) {
2174     nsFrameList toMove;
2175     auto child = selfExcessOCFrames->FirstChild();
2176     while (child) {
2177       auto next = child->GetNextSibling();
2178       MOZ_ASSERT(child->GetPrevInFlow(),
2179                  "ExcessOverflowContainers frames must be continuations");
2180       if (child->GetPrevInFlow()->GetParent() != this) {
2181         selfExcessOCFrames->RemoveFrame(child);
2182         toMove.AppendFrame(nullptr, child);
2183       }
2184       child = next;
2185     }
2186 
2187     // If there's any remaining excess overflow containers, put them back.
2188     if (selfExcessOCFrames->NotEmpty()) {
2189       SetExcessOverflowContainers(std::move(*selfExcessOCFrames));
2190     }
2191 
2192     if (toMove.NotEmpty()) {
2193       if (overflowContainers) {
2194         aMergeFunc(*overflowContainers, toMove, this);
2195       } else {
2196         overflowContainers = SetOverflowContainers(std::move(toMove));
2197       }
2198     }
2199   }
2200 
2201   return overflowContainers;
2202 }
2203 
GetNextInFlowChild(ContinuationTraversingState & aState,bool * aIsInOverflow)2204 nsIFrame* nsContainerFrame::GetNextInFlowChild(
2205     ContinuationTraversingState& aState, bool* aIsInOverflow) {
2206   nsContainerFrame*& nextInFlow = aState.mNextInFlow;
2207   while (nextInFlow) {
2208     // See if there is any frame in the container
2209     nsIFrame* frame = nextInFlow->mFrames.FirstChild();
2210     if (frame) {
2211       if (aIsInOverflow) {
2212         *aIsInOverflow = false;
2213       }
2214       return frame;
2215     }
2216     // No frames in the principal list, try its overflow list
2217     nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
2218     if (overflowFrames) {
2219       if (aIsInOverflow) {
2220         *aIsInOverflow = true;
2221       }
2222       return overflowFrames->FirstChild();
2223     }
2224     nextInFlow = static_cast<nsContainerFrame*>(nextInFlow->GetNextInFlow());
2225   }
2226   return nullptr;
2227 }
2228 
PullNextInFlowChild(ContinuationTraversingState & aState)2229 nsIFrame* nsContainerFrame::PullNextInFlowChild(
2230     ContinuationTraversingState& aState) {
2231   bool isInOverflow;
2232   nsIFrame* frame = GetNextInFlowChild(aState, &isInOverflow);
2233   if (frame) {
2234     nsContainerFrame* nextInFlow = aState.mNextInFlow;
2235     if (isInOverflow) {
2236       nsFrameList* overflowFrames = nextInFlow->GetOverflowFrames();
2237       overflowFrames->RemoveFirstChild();
2238       if (overflowFrames->IsEmpty()) {
2239         nextInFlow->DestroyOverflowList();
2240       }
2241     } else {
2242       nextInFlow->mFrames.RemoveFirstChild();
2243     }
2244 
2245     // Move the frame to the principal frame list of this container
2246     mFrames.AppendFrame(this, frame);
2247     // AppendFrame has reparented the frame, we need
2248     // to reparent the frame view then.
2249     nsContainerFrame::ReparentFrameView(frame, nextInFlow, this);
2250   }
2251   return frame;
2252 }
2253 
2254 /* static */
ReparentFloatsForInlineChild(nsIFrame * aOurLineContainer,nsIFrame * aFrame,bool aReparentSiblings)2255 void nsContainerFrame::ReparentFloatsForInlineChild(nsIFrame* aOurLineContainer,
2256                                                     nsIFrame* aFrame,
2257                                                     bool aReparentSiblings) {
2258   // XXXbz this would be better if it took a nsFrameList or a frame
2259   // list slice....
2260   NS_ASSERTION(aOurLineContainer->GetNextContinuation() ||
2261                    aOurLineContainer->GetPrevContinuation(),
2262                "Don't call this when we have no continuation, it's a waste");
2263   if (!aFrame) {
2264     NS_ASSERTION(aReparentSiblings, "Why did we get called?");
2265     return;
2266   }
2267 
2268   nsBlockFrame* frameBlock = nsLayoutUtils::GetFloatContainingBlock(aFrame);
2269   if (!frameBlock || frameBlock == aOurLineContainer) {
2270     return;
2271   }
2272 
2273   nsBlockFrame* ourBlock = do_QueryFrame(aOurLineContainer);
2274   NS_ASSERTION(ourBlock, "Not a block, but broke vertically?");
2275 
2276   while (true) {
2277     ourBlock->ReparentFloats(aFrame, frameBlock, false);
2278 
2279     if (!aReparentSiblings) return;
2280     nsIFrame* next = aFrame->GetNextSibling();
2281     if (!next) return;
2282     if (next->GetParent() == aFrame->GetParent()) {
2283       aFrame = next;
2284       continue;
2285     }
2286     // This is paranoid and will hardly ever get hit ... but we can't actually
2287     // trust that the frames in the sibling chain all have the same parent,
2288     // because lazy reparenting may be going on. If we find a different
2289     // parent we need to redo our analysis.
2290     ReparentFloatsForInlineChild(aOurLineContainer, next, aReparentSiblings);
2291     return;
2292   }
2293 }
2294 
ResolvedOrientationIsVertical()2295 bool nsContainerFrame::ResolvedOrientationIsVertical() {
2296   StyleOrient orient = StyleDisplay()->mOrient;
2297   switch (orient) {
2298     case StyleOrient::Horizontal:
2299       return false;
2300     case StyleOrient::Vertical:
2301       return true;
2302     case StyleOrient::Inline:
2303       return GetWritingMode().IsVertical();
2304     case StyleOrient::Block:
2305       return !GetWritingMode().IsVertical();
2306   }
2307   MOZ_ASSERT_UNREACHABLE("unexpected -moz-orient value");
2308   return false;
2309 }
2310 
ComputeSizeWithIntrinsicDimensions(gfxContext * aRenderingContext,WritingMode aWM,const IntrinsicSize & aIntrinsicSize,const AspectRatio & aAspectRatio,const LogicalSize & aCBSize,const LogicalSize & aMargin,const LogicalSize & aBorderPadding,const StyleSizeOverrides & aSizeOverrides,ComputeSizeFlags aFlags)2311 LogicalSize nsContainerFrame::ComputeSizeWithIntrinsicDimensions(
2312     gfxContext* aRenderingContext, WritingMode aWM,
2313     const IntrinsicSize& aIntrinsicSize, const AspectRatio& aAspectRatio,
2314     const LogicalSize& aCBSize, const LogicalSize& aMargin,
2315     const LogicalSize& aBorderPadding, const StyleSizeOverrides& aSizeOverrides,
2316     ComputeSizeFlags aFlags) {
2317   const nsStylePosition* stylePos = StylePosition();
2318   const auto& styleISize = aSizeOverrides.mStyleISize
2319                                ? *aSizeOverrides.mStyleISize
2320                                : stylePos->ISize(aWM);
2321   const auto& styleBSize = aSizeOverrides.mStyleBSize
2322                                ? *aSizeOverrides.mStyleBSize
2323                                : stylePos->BSize(aWM);
2324   const auto& aspectRatio =
2325       aSizeOverrides.mAspectRatio ? *aSizeOverrides.mAspectRatio : aAspectRatio;
2326 
2327   auto* parentFrame = GetParent();
2328   const bool isGridItem = IsGridItem();
2329   const bool isFlexItem =
2330       IsFlexItem() &&
2331       !parentFrame->HasAnyStateBits(NS_STATE_FLEX_IS_EMULATING_LEGACY_BOX);
2332   // This variable only gets set (and used) if isFlexItem is true.  It
2333   // indicates which axis (in this frame's own WM) corresponds to its
2334   // flex container's main axis.
2335   LogicalAxis flexMainAxis =
2336       eLogicalAxisInline;  // (init to make valgrind happy)
2337 
2338   if (isFlexItem) {
2339     flexMainAxis = nsFlexContainerFrame::IsItemInlineAxisMainAxis(this)
2340                        ? eLogicalAxisInline
2341                        : eLogicalAxisBlock;
2342   }
2343 
2344   // Handle intrinsic sizes and their interaction with
2345   // {min-,max-,}{width,height} according to the rules in
2346   // http://www.w3.org/TR/CSS21/visudet.html#min-max-widths
2347 
2348   // Note: throughout the following section of the function, I avoid
2349   // a * (b / c) because of its reduced accuracy relative to a * b / c
2350   // or (a * b) / c (which are equivalent).
2351 
2352   const bool isAutoISize = styleISize.IsAuto();
2353   const bool isAutoBSize =
2354       nsLayoutUtils::IsAutoBSize(styleBSize, aCBSize.BSize(aWM)) ||
2355       aFlags.contains(ComputeSizeFlag::UseAutoBSize);
2356 
2357   const auto boxSizingAdjust = stylePos->mBoxSizing == StyleBoxSizing::Border
2358                                    ? aBorderPadding
2359                                    : LogicalSize(aWM);
2360   const nscoord boxSizingToMarginEdgeISize = aMargin.ISize(aWM) +
2361                                              aBorderPadding.ISize(aWM) -
2362                                              boxSizingAdjust.ISize(aWM);
2363 
2364   nscoord iSize, minISize, maxISize, bSize, minBSize, maxBSize;
2365   enum class Stretch {
2366     // stretch to fill the CB (preserving intrinsic ratio) in the relevant axis
2367     StretchPreservingRatio,  // XXX not used yet
2368     // stretch to fill the CB in the relevant axis
2369     Stretch,
2370     // no stretching in the relevant axis
2371     NoStretch,
2372   };
2373   // just to avoid having to type these out everywhere:
2374   const auto eStretchPreservingRatio = Stretch::StretchPreservingRatio;
2375   const auto eStretch = Stretch::Stretch;
2376   const auto eNoStretch = Stretch::NoStretch;
2377 
2378   Stretch stretchI = eNoStretch;  // stretch behavior in the inline axis
2379   Stretch stretchB = eNoStretch;  // stretch behavior in the block axis
2380 
2381   const bool isOrthogonal = aWM.IsOrthogonalTo(parentFrame->GetWritingMode());
2382   const bool isVertical = aWM.IsVertical();
2383   const LogicalSize fallbackIntrinsicSize(aWM, kFallbackIntrinsicSize);
2384   const auto& isizeCoord =
2385       isVertical ? aIntrinsicSize.height : aIntrinsicSize.width;
2386   const bool hasIntrinsicISize = isizeCoord.isSome();
2387   nscoord intrinsicISize = std::max(0, isizeCoord.valueOr(0));
2388 
2389   const auto& bsizeCoord =
2390       isVertical ? aIntrinsicSize.width : aIntrinsicSize.height;
2391   const bool hasIntrinsicBSize = bsizeCoord.isSome();
2392   nscoord intrinsicBSize = std::max(0, bsizeCoord.valueOr(0));
2393 
2394   if (!isAutoISize) {
2395     iSize = ComputeISizeValue(aRenderingContext, aWM, aCBSize, boxSizingAdjust,
2396                               boxSizingToMarginEdgeISize, styleISize,
2397                               aSizeOverrides, aFlags)
2398                 .mISize;
2399   } else if (MOZ_UNLIKELY(isGridItem) &&
2400              !parentFrame->IsMasonry(isOrthogonal ? eLogicalAxisBlock
2401                                                   : eLogicalAxisInline)) {
2402     MOZ_ASSERT(!IsTrueOverflowContainer());
2403     // 'auto' inline-size for grid-level box - apply 'stretch' as needed:
2404     auto cbSize = aCBSize.ISize(aWM);
2405     if (cbSize != NS_UNCONSTRAINEDSIZE) {
2406       if (!StyleMargin()->HasInlineAxisAuto(aWM)) {
2407         auto inlineAxisAlignment =
2408             isOrthogonal ? stylePos->UsedAlignSelf(GetParent()->Style())._0
2409                          : stylePos->UsedJustifySelf(GetParent()->Style())._0;
2410         if (inlineAxisAlignment == StyleAlignFlags::STRETCH) {
2411           stretchI = eStretch;
2412         }
2413       }
2414       if (stretchI != eNoStretch ||
2415           aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize)) {
2416         iSize = std::max(nscoord(0), cbSize - aBorderPadding.ISize(aWM) -
2417                                          aMargin.ISize(aWM));
2418       }
2419     } else {
2420       // Reset this flag to avoid applying the clamping below.
2421       aFlags -= ComputeSizeFlag::IClampMarginBoxMinSize;
2422     }
2423   }
2424 
2425   const auto& maxISizeCoord = stylePos->MaxISize(aWM);
2426 
2427   if (!maxISizeCoord.IsNone() &&
2428       !(isFlexItem && flexMainAxis == eLogicalAxisInline)) {
2429     maxISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
2430                                  boxSizingAdjust, boxSizingToMarginEdgeISize,
2431                                  maxISizeCoord, aSizeOverrides, aFlags)
2432                    .mISize;
2433   } else {
2434     maxISize = nscoord_MAX;
2435   }
2436 
2437   // NOTE: Flex items ignore their min & max sizing properties in their
2438   // flex container's main-axis.  (Those properties get applied later in
2439   // the flexbox algorithm.)
2440 
2441   const auto& minISizeCoord = stylePos->MinISize(aWM);
2442 
2443   if (!minISizeCoord.IsAuto() &&
2444       !(isFlexItem && flexMainAxis == eLogicalAxisInline)) {
2445     minISize = ComputeISizeValue(aRenderingContext, aWM, aCBSize,
2446                                  boxSizingAdjust, boxSizingToMarginEdgeISize,
2447                                  minISizeCoord, aSizeOverrides, aFlags)
2448                    .mISize;
2449   } else {
2450     // Treat "min-width: auto" as 0.
2451     // NOTE: Technically, "auto" is supposed to behave like "min-content" on
2452     // flex items. However, we don't need to worry about that here, because
2453     // flex items' min-sizes are intentionally ignored until the flex
2454     // container explicitly considers them during space distribution.
2455     minISize = 0;
2456   }
2457 
2458   if (!isAutoBSize) {
2459     bSize = nsLayoutUtils::ComputeBSizeValue(aCBSize.BSize(aWM),
2460                                              boxSizingAdjust.BSize(aWM),
2461                                              styleBSize.AsLengthPercentage());
2462   } else if (MOZ_UNLIKELY(isGridItem) &&
2463              !parentFrame->IsMasonry(isOrthogonal ? eLogicalAxisInline
2464                                                   : eLogicalAxisBlock)) {
2465     MOZ_ASSERT(!IsTrueOverflowContainer());
2466     // 'auto' block-size for grid-level box - apply 'stretch' as needed:
2467     auto cbSize = aCBSize.BSize(aWM);
2468     if (cbSize != NS_UNCONSTRAINEDSIZE) {
2469       if (!StyleMargin()->HasBlockAxisAuto(aWM)) {
2470         auto blockAxisAlignment =
2471             !isOrthogonal ? stylePos->UsedAlignSelf(GetParent()->Style())._0
2472                           : stylePos->UsedJustifySelf(GetParent()->Style())._0;
2473         if (blockAxisAlignment == StyleAlignFlags::STRETCH) {
2474           stretchB = eStretch;
2475         }
2476       }
2477       if (stretchB != eNoStretch ||
2478           aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize)) {
2479         bSize = std::max(nscoord(0), cbSize - aBorderPadding.BSize(aWM) -
2480                                          aMargin.BSize(aWM));
2481       }
2482     } else {
2483       // Reset this flag to avoid applying the clamping below.
2484       aFlags -= ComputeSizeFlag::BClampMarginBoxMinSize;
2485     }
2486   }
2487 
2488   const auto& maxBSizeCoord = stylePos->MaxBSize(aWM);
2489 
2490   if (!nsLayoutUtils::IsAutoBSize(maxBSizeCoord, aCBSize.BSize(aWM)) &&
2491       !(isFlexItem && flexMainAxis == eLogicalAxisBlock)) {
2492     maxBSize = nsLayoutUtils::ComputeBSizeValue(
2493         aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
2494         maxBSizeCoord.AsLengthPercentage());
2495   } else {
2496     maxBSize = nscoord_MAX;
2497   }
2498 
2499   const auto& minBSizeCoord = stylePos->MinBSize(aWM);
2500 
2501   if (!nsLayoutUtils::IsAutoBSize(minBSizeCoord, aCBSize.BSize(aWM)) &&
2502       !(isFlexItem && flexMainAxis == eLogicalAxisBlock)) {
2503     minBSize = nsLayoutUtils::ComputeBSizeValue(
2504         aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
2505         minBSizeCoord.AsLengthPercentage());
2506   } else {
2507     minBSize = 0;
2508   }
2509 
2510   NS_ASSERTION(aCBSize.ISize(aWM) != NS_UNCONSTRAINEDSIZE,
2511                "Our containing block must not have unconstrained inline-size!");
2512 
2513   // Now calculate the used values for iSize and bSize:
2514   if (isAutoISize) {
2515     if (isAutoBSize) {
2516       // 'auto' iSize, 'auto' bSize
2517 
2518       // Get tentative values - CSS 2.1 sections 10.3.2 and 10.6.2:
2519 
2520       nscoord tentISize, tentBSize;
2521 
2522       if (hasIntrinsicISize) {
2523         tentISize = intrinsicISize;
2524       } else if (hasIntrinsicBSize && aspectRatio) {
2525         tentISize = aspectRatio.ComputeRatioDependentSize(
2526             LogicalAxis::eLogicalAxisInline, aWM, intrinsicBSize,
2527             boxSizingAdjust);
2528       } else if (aspectRatio) {
2529         tentISize =
2530             aCBSize.ISize(aWM) - boxSizingToMarginEdgeISize;  // XXX scrollbar?
2531         if (tentISize < 0) {
2532           tentISize = 0;
2533         }
2534       } else {
2535         tentISize = fallbackIntrinsicSize.ISize(aWM);
2536       }
2537 
2538       // If we need to clamp the inline size to fit the CB, we use the 'stretch'
2539       // or 'normal' codepath.  We use the ratio-preserving 'normal' codepath
2540       // unless we have 'stretch' in the other axis.
2541       if (aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize) &&
2542           stretchI != eStretch && tentISize > iSize) {
2543         stretchI = (stretchB == eStretch ? eStretch : eStretchPreservingRatio);
2544       }
2545 
2546       if (hasIntrinsicBSize) {
2547         tentBSize = intrinsicBSize;
2548       } else if (aspectRatio) {
2549         tentBSize = aspectRatio.ComputeRatioDependentSize(
2550             LogicalAxis::eLogicalAxisBlock, aWM, tentISize, boxSizingAdjust);
2551       } else {
2552         tentBSize = fallbackIntrinsicSize.BSize(aWM);
2553       }
2554 
2555       // (ditto the comment about clamping the inline size above)
2556       if (aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize) &&
2557           stretchB != eStretch && tentBSize > bSize) {
2558         stretchB = (stretchI == eStretch ? eStretch : eStretchPreservingRatio);
2559       }
2560 
2561       if (stretchI == eStretch) {
2562         tentISize = iSize;  // * / 'stretch'
2563         if (stretchB == eStretch) {
2564           tentBSize = bSize;  // 'stretch' / 'stretch'
2565         } else if (stretchB == eStretchPreservingRatio && aspectRatio) {
2566           // 'normal' / 'stretch'
2567           tentBSize = aspectRatio.ComputeRatioDependentSize(
2568               LogicalAxis::eLogicalAxisBlock, aWM, iSize, boxSizingAdjust);
2569         }
2570       } else if (stretchB == eStretch) {
2571         tentBSize = bSize;  // 'stretch' / * (except 'stretch')
2572         if (stretchI == eStretchPreservingRatio && aspectRatio) {
2573           // 'stretch' / 'normal'
2574           tentISize = aspectRatio.ComputeRatioDependentSize(
2575               LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2576         }
2577       } else if (stretchI == eStretchPreservingRatio && aspectRatio) {
2578         tentISize = iSize;  // * (except 'stretch') / 'normal'
2579         tentBSize = aspectRatio.ComputeRatioDependentSize(
2580             LogicalAxis::eLogicalAxisBlock, aWM, iSize, boxSizingAdjust);
2581         if (stretchB == eStretchPreservingRatio && tentBSize > bSize) {
2582           // Stretch within the CB size with preserved intrinsic ratio.
2583           tentBSize = bSize;  // 'normal' / 'normal'
2584           tentISize = aspectRatio.ComputeRatioDependentSize(
2585               LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2586         }
2587       } else if (stretchB == eStretchPreservingRatio && aspectRatio) {
2588         tentBSize = bSize;  // 'normal' / * (except 'normal' and 'stretch')
2589         tentISize = aspectRatio.ComputeRatioDependentSize(
2590             LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2591       }
2592 
2593       // ComputeAutoSizeWithIntrinsicDimensions preserves the ratio when
2594       // applying the min/max-size.  We don't want that when we have 'stretch'
2595       // in either axis because tentISize/tentBSize is likely not according to
2596       // ratio now.
2597       if (aspectRatio && stretchI != eStretch && stretchB != eStretch) {
2598         nsSize autoSize = nsLayoutUtils::ComputeAutoSizeWithIntrinsicDimensions(
2599             minISize, minBSize, maxISize, maxBSize, tentISize, tentBSize);
2600         // The nsSize that ComputeAutoSizeWithIntrinsicDimensions returns will
2601         // actually contain logical values if the parameters passed to it were
2602         // logical coordinates, so we do NOT perform a physical-to-logical
2603         // conversion here, but just assign the fields directly to our result.
2604         iSize = autoSize.width;
2605         bSize = autoSize.height;
2606       } else {
2607         // Not honoring an intrinsic ratio: clamp the dimensions independently.
2608         iSize = NS_CSS_MINMAX(tentISize, minISize, maxISize);
2609         bSize = NS_CSS_MINMAX(tentBSize, minBSize, maxBSize);
2610       }
2611     } else {
2612       // 'auto' iSize, non-'auto' bSize
2613       bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2614       if (stretchI != eStretch) {
2615         if (aspectRatio) {
2616           iSize = aspectRatio.ComputeRatioDependentSize(
2617               LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
2618         } else if (hasIntrinsicISize) {
2619           if (!(aFlags.contains(ComputeSizeFlag::IClampMarginBoxMinSize) &&
2620                 intrinsicISize > iSize)) {
2621             iSize = intrinsicISize;
2622           }  // else - leave iSize as is to fill the CB
2623         } else {
2624           iSize = fallbackIntrinsicSize.ISize(aWM);
2625         }
2626       }  // else - leave iSize as is to fill the CB
2627       iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2628     }
2629   } else {
2630     if (isAutoBSize) {
2631       // non-'auto' iSize, 'auto' bSize
2632       iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2633       if (stretchB != eStretch) {
2634         if (aspectRatio) {
2635           bSize = aspectRatio.ComputeRatioDependentSize(
2636               LogicalAxis::eLogicalAxisBlock, aWM, iSize, boxSizingAdjust);
2637         } else if (hasIntrinsicBSize) {
2638           if (!(aFlags.contains(ComputeSizeFlag::BClampMarginBoxMinSize) &&
2639                 intrinsicBSize > bSize)) {
2640             bSize = intrinsicBSize;
2641           }  // else - leave bSize as is to fill the CB
2642         } else {
2643           bSize = fallbackIntrinsicSize.BSize(aWM);
2644         }
2645       }  // else - leave bSize as is to fill the CB
2646       bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2647 
2648     } else {
2649       // non-'auto' iSize, non-'auto' bSize
2650       iSize = NS_CSS_MINMAX(iSize, minISize, maxISize);
2651       bSize = NS_CSS_MINMAX(bSize, minBSize, maxBSize);
2652     }
2653   }
2654 
2655   return LogicalSize(aWM, iSize, bSize);
2656 }
2657 
ComputeSimpleTightBounds(DrawTarget * aDrawTarget) const2658 nsRect nsContainerFrame::ComputeSimpleTightBounds(
2659     DrawTarget* aDrawTarget) const {
2660   if (StyleOutline()->ShouldPaintOutline() || StyleBorder()->HasBorder() ||
2661       !StyleBackground()->IsTransparent(this) ||
2662       StyleDisplay()->HasAppearance()) {
2663     // Not necessarily tight, due to clipping, negative
2664     // outline-offset, and lots of other issues, but that's OK
2665     return InkOverflowRect();
2666   }
2667 
2668   nsRect r(0, 0, 0, 0);
2669   for (const auto& childLists : ChildLists()) {
2670     for (nsIFrame* child : childLists.mList) {
2671       r.UnionRect(
2672           r, child->ComputeTightBounds(aDrawTarget) + child->GetPosition());
2673     }
2674   }
2675   return r;
2676 }
2677 
PushDirtyBitToAbsoluteFrames()2678 void nsContainerFrame::PushDirtyBitToAbsoluteFrames() {
2679   if (!HasAnyStateBits(NS_FRAME_IS_DIRTY)) {
2680     return;  // No dirty bit to push.
2681   }
2682   if (!HasAbsolutelyPositionedChildren()) {
2683     return;  // No absolute children to push to.
2684   }
2685   GetAbsoluteContainingBlock()->MarkAllFramesDirty();
2686 }
2687 
2688 // Define the MAX_FRAME_DEPTH to be the ContentSink's MAX_REFLOW_DEPTH plus
2689 // 4 for the frames above the document's frames:
2690 //  the Viewport, GFXScroll, ScrollPort, and Canvas
2691 #define MAX_FRAME_DEPTH (MAX_REFLOW_DEPTH + 4)
2692 
IsFrameTreeTooDeep(const ReflowInput & aReflowInput,ReflowOutput & aMetrics,nsReflowStatus & aStatus)2693 bool nsContainerFrame::IsFrameTreeTooDeep(const ReflowInput& aReflowInput,
2694                                           ReflowOutput& aMetrics,
2695                                           nsReflowStatus& aStatus) {
2696   if (aReflowInput.mReflowDepth > MAX_FRAME_DEPTH) {
2697     NS_WARNING("frame tree too deep; setting zero size and returning");
2698     AddStateBits(NS_FRAME_TOO_DEEP_IN_FRAME_TREE);
2699     ClearOverflowRects();
2700     aMetrics.ClearSize();
2701     aMetrics.SetBlockStartAscent(0);
2702     aMetrics.mCarriedOutBEndMargin.Zero();
2703     aMetrics.mOverflowAreas.Clear();
2704 
2705     aStatus.Reset();
2706     if (GetNextInFlow()) {
2707       // Reflow depth might vary between reflows, so we might have
2708       // successfully reflowed and split this frame before.  If so, we
2709       // shouldn't delete its continuations.
2710       aStatus.SetIncomplete();
2711     }
2712 
2713     return true;
2714   }
2715   RemoveStateBits(NS_FRAME_TOO_DEEP_IN_FRAME_TREE);
2716   return false;
2717 }
2718 
ShouldAvoidBreakInside(const ReflowInput & aReflowInput) const2719 bool nsContainerFrame::ShouldAvoidBreakInside(
2720     const ReflowInput& aReflowInput) const {
2721   const auto* disp = StyleDisplay();
2722   const bool mayAvoidBreak = [&] {
2723     switch (disp->mBreakInside) {
2724       case StyleBreakWithin::Auto:
2725         return false;
2726       case StyleBreakWithin::Avoid:
2727         return true;
2728       case StyleBreakWithin::AvoidPage:
2729         return aReflowInput.mBreakType == ReflowInput::BreakType::Page;
2730       case StyleBreakWithin::AvoidColumn:
2731         return aReflowInput.mBreakType == ReflowInput::BreakType::Column;
2732     }
2733     MOZ_ASSERT_UNREACHABLE("Unknown break-inside value");
2734     return false;
2735   }();
2736 
2737   if (!mayAvoidBreak) {
2738     return false;
2739   }
2740   if (aReflowInput.mFlags.mIsTopOfPage) {
2741     return false;
2742   }
2743   if (IsAbsolutelyPositioned(disp)) {
2744     return false;
2745   }
2746   if (GetPrevInFlow()) {
2747     return false;
2748   }
2749   return true;
2750 }
2751 
ConsiderChildOverflow(OverflowAreas & aOverflowAreas,nsIFrame * aChildFrame)2752 void nsContainerFrame::ConsiderChildOverflow(OverflowAreas& aOverflowAreas,
2753                                              nsIFrame* aChildFrame) {
2754   if (StyleDisplay()->IsContainLayout() &&
2755       IsFrameOfType(eSupportsContainLayoutAndPaint)) {
2756     // If we have layout containment and are not a non-atomic, inline-level
2757     // principal box, we should only consider our child's ink overflow,
2758     // leaving the scrollable regions of the parent unaffected.
2759     // Note: scrollable overflow is a subset of ink overflow,
2760     // so this has the same affect as unioning the child's ink and
2761     // scrollable overflow with the parent's ink overflow.
2762     const OverflowAreas childOverflows(aChildFrame->InkOverflowRect(),
2763                                        nsRect());
2764     aOverflowAreas.UnionWith(childOverflows + aChildFrame->GetPosition());
2765   } else {
2766     aOverflowAreas.UnionWith(
2767         aChildFrame->GetActualAndNormalOverflowAreasRelativeToParent());
2768   }
2769 }
2770 
CSSAlignmentForAbsPosChild(const ReflowInput & aChildRI,LogicalAxis aLogicalAxis) const2771 StyleAlignFlags nsContainerFrame::CSSAlignmentForAbsPosChild(
2772     const ReflowInput& aChildRI, LogicalAxis aLogicalAxis) const {
2773   MOZ_ASSERT(aChildRI.mFrame->IsAbsolutelyPositioned(),
2774              "This method should only be called for abspos children");
2775   NS_ERROR(
2776       "Child classes that use css box alignment for abspos children "
2777       "should provide their own implementation of this method!");
2778 
2779   // In the unexpected/unlikely event that this implementation gets invoked,
2780   // just use "start" alignment.
2781   return StyleAlignFlags::START;
2782 }
2783 
nsOverflowContinuationTracker(nsContainerFrame * aFrame,bool aWalkOOFFrames,bool aSkipOverflowContainerChildren)2784 nsOverflowContinuationTracker::nsOverflowContinuationTracker(
2785     nsContainerFrame* aFrame, bool aWalkOOFFrames,
2786     bool aSkipOverflowContainerChildren)
2787     : mOverflowContList(nullptr),
2788       mPrevOverflowCont(nullptr),
2789       mSentry(nullptr),
2790       mParent(aFrame),
2791       mSkipOverflowContainerChildren(aSkipOverflowContainerChildren),
2792       mWalkOOFFrames(aWalkOOFFrames) {
2793   MOZ_ASSERT(aFrame, "null frame pointer");
2794   SetupOverflowContList();
2795 }
2796 
SetupOverflowContList()2797 void nsOverflowContinuationTracker::SetupOverflowContList() {
2798   MOZ_ASSERT(mParent, "null frame pointer");
2799   MOZ_ASSERT(!mOverflowContList, "already have list");
2800   nsContainerFrame* nif =
2801       static_cast<nsContainerFrame*>(mParent->GetNextInFlow());
2802   if (nif) {
2803     mOverflowContList = nif->GetOverflowContainers();
2804     if (mOverflowContList) {
2805       mParent = nif;
2806       SetUpListWalker();
2807     }
2808   }
2809   if (!mOverflowContList) {
2810     mOverflowContList = mParent->GetExcessOverflowContainers();
2811     if (mOverflowContList) {
2812       SetUpListWalker();
2813     }
2814   }
2815 }
2816 
2817 /**
2818  * Helper function to walk past overflow continuations whose prev-in-flow
2819  * isn't a normal child and to set mSentry and mPrevOverflowCont correctly.
2820  */
SetUpListWalker()2821 void nsOverflowContinuationTracker::SetUpListWalker() {
2822   NS_ASSERTION(!mSentry && !mPrevOverflowCont,
2823                "forgot to reset mSentry or mPrevOverflowCont");
2824   if (mOverflowContList) {
2825     nsIFrame* cur = mOverflowContList->FirstChild();
2826     if (mSkipOverflowContainerChildren) {
2827       while (cur && cur->GetPrevInFlow()->HasAnyStateBits(
2828                         NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2829         mPrevOverflowCont = cur;
2830         cur = cur->GetNextSibling();
2831       }
2832       while (cur &&
2833              (cur->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) != mWalkOOFFrames)) {
2834         mPrevOverflowCont = cur;
2835         cur = cur->GetNextSibling();
2836       }
2837     }
2838     if (cur) {
2839       mSentry = cur->GetPrevInFlow();
2840     }
2841   }
2842 }
2843 
2844 /**
2845  * Helper function to step forward through the overflow continuations list.
2846  * Sets mSentry and mPrevOverflowCont, skipping over OOF or non-OOF frames
2847  * as appropriate. May only be called when we have already set up an
2848  * mOverflowContList; mOverflowContList cannot be null.
2849  */
StepForward()2850 void nsOverflowContinuationTracker::StepForward() {
2851   MOZ_ASSERT(mOverflowContList, "null list");
2852 
2853   // Step forward
2854   if (mPrevOverflowCont) {
2855     mPrevOverflowCont = mPrevOverflowCont->GetNextSibling();
2856   } else {
2857     mPrevOverflowCont = mOverflowContList->FirstChild();
2858   }
2859 
2860   // Skip over oof or non-oof frames as appropriate
2861   if (mSkipOverflowContainerChildren) {
2862     nsIFrame* cur = mPrevOverflowCont->GetNextSibling();
2863     while (cur &&
2864            (cur->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) != mWalkOOFFrames)) {
2865       mPrevOverflowCont = cur;
2866       cur = cur->GetNextSibling();
2867     }
2868   }
2869 
2870   // Set up the sentry
2871   mSentry = (mPrevOverflowCont->GetNextSibling())
2872                 ? mPrevOverflowCont->GetNextSibling()->GetPrevInFlow()
2873                 : nullptr;
2874 }
2875 
Insert(nsIFrame * aOverflowCont,nsReflowStatus & aReflowStatus)2876 nsresult nsOverflowContinuationTracker::Insert(nsIFrame* aOverflowCont,
2877                                                nsReflowStatus& aReflowStatus) {
2878   MOZ_ASSERT(aOverflowCont, "null frame pointer");
2879   MOZ_ASSERT(!mSkipOverflowContainerChildren ||
2880                  mWalkOOFFrames ==
2881                      aOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW),
2882              "shouldn't insert frame that doesn't match walker type");
2883   MOZ_ASSERT(aOverflowCont->GetPrevInFlow(),
2884              "overflow containers must have a prev-in-flow");
2885 
2886   nsresult rv = NS_OK;
2887   bool reparented = false;
2888   nsPresContext* presContext = aOverflowCont->PresContext();
2889   bool addToList = !mSentry || aOverflowCont != mSentry->GetNextInFlow();
2890 
2891   // If we have a list and aOverflowCont is already in it then don't try to
2892   // add it again.
2893   if (addToList && aOverflowCont->GetParent() == mParent &&
2894       aOverflowCont->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) &&
2895       mOverflowContList && mOverflowContList->ContainsFrame(aOverflowCont)) {
2896     addToList = false;
2897     mPrevOverflowCont = aOverflowCont->GetPrevSibling();
2898   }
2899 
2900   if (addToList) {
2901     if (aOverflowCont->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2902       // aOverflowCont is in some other overflow container list,
2903       // steal it first
2904       NS_ASSERTION(!(mOverflowContList &&
2905                      mOverflowContList->ContainsFrame(aOverflowCont)),
2906                    "overflow containers out of order");
2907       aOverflowCont->GetParent()->StealFrame(aOverflowCont);
2908     } else {
2909       aOverflowCont->AddStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER);
2910     }
2911     if (!mOverflowContList) {
2912       // Note: We don't use SetExcessOverflowContainers() since it requires
2913       // setting a non-empty list. It's OK to manually set an empty list to
2914       // ExcessOverflowContainersProperty() because we are going to insert
2915       // aOverflowCont to mOverflowContList below, which guarantees an nonempty
2916       // list in ExcessOverflowContainersProperty().
2917       mOverflowContList = new (presContext->PresShell()) nsFrameList();
2918       mParent->SetProperty(nsContainerFrame::ExcessOverflowContainersProperty(),
2919                            mOverflowContList);
2920       SetUpListWalker();
2921     }
2922     if (aOverflowCont->GetParent() != mParent) {
2923       nsContainerFrame::ReparentFrameView(aOverflowCont,
2924                                           aOverflowCont->GetParent(), mParent);
2925       reparented = true;
2926     }
2927 
2928     // If aOverflowCont has a prev/next-in-flow that might be in
2929     // mOverflowContList we need to find it and insert after/before it to
2930     // maintain the order amongst next-in-flows in this list.
2931     nsIFrame* pif = aOverflowCont->GetPrevInFlow();
2932     nsIFrame* nif = aOverflowCont->GetNextInFlow();
2933     if ((pif && pif->GetParent() == mParent && pif != mPrevOverflowCont) ||
2934         (nif && nif->GetParent() == mParent && mPrevOverflowCont)) {
2935       for (nsFrameList::Enumerator e(*mOverflowContList); !e.AtEnd();
2936            e.Next()) {
2937         nsIFrame* f = e.get();
2938         if (f == pif) {
2939           mPrevOverflowCont = pif;
2940           break;
2941         }
2942         if (f == nif) {
2943           mPrevOverflowCont = f->GetPrevSibling();
2944           break;
2945         }
2946       }
2947     }
2948 
2949     mOverflowContList->InsertFrame(mParent, mPrevOverflowCont, aOverflowCont);
2950     aReflowStatus.SetNextInFlowNeedsReflow();
2951   }
2952 
2953   // If we need to reflow it, mark it dirty
2954   if (aReflowStatus.NextInFlowNeedsReflow()) {
2955     aOverflowCont->MarkSubtreeDirty();
2956   }
2957 
2958   // It's in our list, just step forward
2959   StepForward();
2960   NS_ASSERTION(mPrevOverflowCont == aOverflowCont ||
2961                    (mSkipOverflowContainerChildren &&
2962                     mPrevOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) !=
2963                         aOverflowCont->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)),
2964                "OverflowContTracker in unexpected state");
2965 
2966   if (addToList) {
2967     // Convert all non-overflow-container next-in-flows of aOverflowCont
2968     // into overflow containers and move them to our overflow
2969     // tracker. This preserves the invariant that the next-in-flows
2970     // of an overflow container are also overflow containers.
2971     nsIFrame* f = aOverflowCont->GetNextInFlow();
2972     if (f && (!f->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER) ||
2973               (!reparented && f->GetParent() == mParent) ||
2974               (reparented && f->GetParent() != mParent))) {
2975       if (!f->HasAnyStateBits(NS_FRAME_IS_OVERFLOW_CONTAINER)) {
2976         f->GetParent()->StealFrame(f);
2977       }
2978       Insert(f, aReflowStatus);
2979     }
2980   }
2981   return rv;
2982 }
2983 
BeginFinish(nsIFrame * aChild)2984 void nsOverflowContinuationTracker::BeginFinish(nsIFrame* aChild) {
2985   MOZ_ASSERT(aChild, "null ptr");
2986   MOZ_ASSERT(aChild->GetNextInFlow(),
2987              "supposed to call Finish *before* deleting next-in-flow!");
2988 
2989   for (nsIFrame* f = aChild; f; f = f->GetNextInFlow()) {
2990     // We'll update these in EndFinish after the next-in-flows are gone.
2991     if (f == mPrevOverflowCont) {
2992       mSentry = nullptr;
2993       mPrevOverflowCont = nullptr;
2994       break;
2995     }
2996     if (f == mSentry) {
2997       mSentry = nullptr;
2998       break;
2999     }
3000   }
3001 }
3002 
EndFinish(nsIFrame * aChild)3003 void nsOverflowContinuationTracker::EndFinish(nsIFrame* aChild) {
3004   if (!mOverflowContList) {
3005     return;
3006   }
3007   // Forget mOverflowContList if it was deleted.
3008   nsFrameList* eoc = mParent->GetExcessOverflowContainers();
3009   if (eoc != mOverflowContList) {
3010     nsFrameList* oc = mParent->GetOverflowContainers();
3011     if (oc != mOverflowContList) {
3012       // mOverflowContList was deleted
3013       mPrevOverflowCont = nullptr;
3014       mSentry = nullptr;
3015       mParent = aChild->GetParent();
3016       mOverflowContList = nullptr;
3017       SetupOverflowContList();
3018       return;
3019     }
3020   }
3021   // The list survived, update mSentry if needed.
3022   if (!mSentry) {
3023     if (!mPrevOverflowCont) {
3024       SetUpListWalker();
3025     } else {
3026       mozilla::AutoRestore<nsIFrame*> saved(mPrevOverflowCont);
3027       // step backward to make StepForward() use our current mPrevOverflowCont
3028       mPrevOverflowCont = mPrevOverflowCont->GetPrevSibling();
3029       StepForward();
3030     }
3031   }
3032 }
3033 
3034 /////////////////////////////////////////////////////////////////////////////
3035 // Debugging
3036 
3037 #ifdef DEBUG
SanityCheckChildListsBeforeReflow() const3038 void nsContainerFrame::SanityCheckChildListsBeforeReflow() const {
3039   MOZ_ASSERT(IsFlexOrGridContainer(),
3040              "Only Flex / Grid containers can call this!");
3041 
3042   const auto didPushItemsBit = IsFlexContainerFrame()
3043                                    ? NS_STATE_FLEX_DID_PUSH_ITEMS
3044                                    : NS_STATE_GRID_DID_PUSH_ITEMS;
3045   ChildListIDs absLists = {kAbsoluteList, kFixedList, kOverflowContainersList,
3046                            kExcessOverflowContainersList};
3047   ChildListIDs itemLists = {kPrincipalList, kOverflowList};
3048   for (const nsIFrame* f = this; f; f = f->GetNextInFlow()) {
3049     MOZ_ASSERT(!f->HasAnyStateBits(didPushItemsBit),
3050                "At start of reflow, we should've pulled items back from all "
3051                "NIFs and cleared the state bit stored in didPushItemsBit in "
3052                "the process.");
3053     for (const auto& [list, listID] : f->ChildLists()) {
3054       if (!itemLists.contains(listID)) {
3055         MOZ_ASSERT(absLists.contains(listID) || listID == kBackdropList,
3056                    "unexpected non-empty child list");
3057         continue;
3058       }
3059       for (const auto* child : list) {
3060         MOZ_ASSERT(f == this || child->GetPrevInFlow(),
3061                    "all pushed items must be pulled up before reflow");
3062       }
3063     }
3064   }
3065   // If we have a prev-in-flow, each of its children's next-in-flow
3066   // should be one of our children or be null.
3067   const auto* pif = static_cast<nsContainerFrame*>(GetPrevInFlow());
3068   if (pif) {
3069     const nsFrameList* oc = GetOverflowContainers();
3070     const nsFrameList* eoc = GetExcessOverflowContainers();
3071     const nsFrameList* pifEOC = pif->GetExcessOverflowContainers();
3072     for (const nsIFrame* child : pif->GetChildList(kPrincipalList)) {
3073       const nsIFrame* childNIF = child->GetNextInFlow();
3074       MOZ_ASSERT(!childNIF || mFrames.ContainsFrame(childNIF) ||
3075                  (pifEOC && pifEOC->ContainsFrame(childNIF)) ||
3076                  (oc && oc->ContainsFrame(childNIF)) ||
3077                  (eoc && eoc->ContainsFrame(childNIF)));
3078     }
3079   }
3080 }
3081 
SetDidPushItemsBitIfNeeded(ChildListID aListID,nsIFrame * aOldFrame)3082 void nsContainerFrame::SetDidPushItemsBitIfNeeded(ChildListID aListID,
3083                                                   nsIFrame* aOldFrame) {
3084   MOZ_ASSERT(IsFlexOrGridContainer(),
3085              "Only Flex / Grid containers can call this!");
3086 
3087   // Note that kPrincipalList doesn't mean aOldFrame must be on that list.
3088   // It can also be on kOverflowList, in which case it might be a pushed
3089   // item, and if it's the only pushed item our DID_PUSH_ITEMS bit will lie.
3090   if (aListID == kPrincipalList && !aOldFrame->GetPrevInFlow()) {
3091     // Since the bit may lie, set the mDidPushItemsBitMayLie value to true for
3092     // ourself and for all our prev-in-flows.
3093     nsContainerFrame* frameThatMayLie = this;
3094     do {
3095       frameThatMayLie->mDidPushItemsBitMayLie = true;
3096       frameThatMayLie =
3097           static_cast<nsContainerFrame*>(frameThatMayLie->GetPrevInFlow());
3098     } while (frameThatMayLie);
3099   }
3100 }
3101 #endif
3102 
3103 #ifdef DEBUG_FRAME_DUMP
List(FILE * out,const char * aPrefix,ListFlags aFlags) const3104 void nsContainerFrame::List(FILE* out, const char* aPrefix,
3105                             ListFlags aFlags) const {
3106   nsCString str;
3107   ListGeneric(str, aPrefix, aFlags);
3108   ExtraContainerFrameInfo(str);
3109 
3110   // Output the frame name and various fields.
3111   fprintf_stderr(out, "%s <\n", str.get());
3112 
3113   const nsCString pfx = nsCString(aPrefix) + "  "_ns;
3114 
3115   // Output principal child list separately since we want to omit its
3116   // name and address.
3117   for (nsIFrame* kid : PrincipalChildList()) {
3118     kid->List(out, pfx.get(), aFlags);
3119   }
3120 
3121   // Output rest of the child lists.
3122   const ChildListIDs skippedListIDs = {kPrincipalList};
3123   ListChildLists(out, pfx.get(), aFlags, skippedListIDs);
3124 
3125   fprintf_stderr(out, "%s>\n", aPrefix);
3126 }
3127 
ListWithMatchedRules(FILE * out,const char * aPrefix) const3128 void nsContainerFrame::ListWithMatchedRules(FILE* out,
3129                                             const char* aPrefix) const {
3130   fprintf_stderr(out, "%s%s\n", aPrefix, ListTag().get());
3131 
3132   nsCString rulePrefix;
3133   rulePrefix += aPrefix;
3134   rulePrefix += "    ";
3135   ListMatchedRules(out, rulePrefix.get());
3136 
3137   nsCString childPrefix;
3138   childPrefix += aPrefix;
3139   childPrefix += "  ";
3140 
3141   for (const auto& childList : ChildLists()) {
3142     for (const nsIFrame* kid : childList.mList) {
3143       kid->ListWithMatchedRules(out, childPrefix.get());
3144     }
3145   }
3146 }
3147 
ListChildLists(FILE * aOut,const char * aPrefix,ListFlags aFlags,ChildListIDs aSkippedListIDs) const3148 void nsContainerFrame::ListChildLists(FILE* aOut, const char* aPrefix,
3149                                       ListFlags aFlags,
3150                                       ChildListIDs aSkippedListIDs) const {
3151   const nsCString nestedPfx = nsCString(aPrefix) + "  "_ns;
3152 
3153   for (const auto& [list, listID] : ChildLists()) {
3154     if (aSkippedListIDs.contains(listID)) {
3155       continue;
3156     }
3157 
3158     // Use nsPrintfCString so that %p don't output prefix "0x". This is
3159     // consistent with nsIFrame::ListTag().
3160     const nsPrintfCString str("%s%s@%p <\n", aPrefix, ChildListName(listID),
3161                               &GetChildList(listID));
3162     fprintf_stderr(aOut, "%s", str.get());
3163 
3164     for (nsIFrame* kid : list) {
3165       // Verify the child frame's parent frame pointer is correct.
3166       NS_ASSERTION(kid->GetParent() == this, "Bad parent frame pointer!");
3167       kid->List(aOut, nestedPfx.get(), aFlags);
3168     }
3169     fprintf_stderr(aOut, "%s>\n", aPrefix);
3170   }
3171 }
3172 
ExtraContainerFrameInfo(nsACString & aTo) const3173 void nsContainerFrame::ExtraContainerFrameInfo(nsACString& aTo) const {
3174   (void)aTo;
3175 }
3176 
3177 #endif
3178