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