/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /* * construction of a frame tree that is nearly isomorphic to the content * tree and updating of that tree in response to dynamic changes */ #include "nsCSSFrameConstructor.h" #include "mozilla/AutoRestore.h" #include "mozilla/ComputedStyleInlines.h" #include "mozilla/DebugOnly.h" #include "mozilla/ErrorResult.h" #include "mozilla/ManualNAC.h" #include "mozilla/dom/BindContext.h" #include "mozilla/dom/BrowsingContext.h" #include "mozilla/dom/GeneratedImageContent.h" #include "mozilla/dom/HTMLSelectElement.h" #include "mozilla/dom/HTMLSharedListElement.h" #include "mozilla/dom/HTMLSummaryElement.h" #include "mozilla/Likely.h" #include "mozilla/LinkedList.h" #include "mozilla/MemoryReporting.h" #include "mozilla/PresShell.h" #include "mozilla/PresShellInlines.h" #include "mozilla/PrintedSheetFrame.h" #include "mozilla/ProfilerLabels.h" #include "mozilla/ProfilerMarkers.h" #include "mozilla/ScopeExit.h" #include "mozilla/ServoBindings.h" #include "mozilla/ServoStyleSetInlines.h" #include "mozilla/StaticPrefs_browser.h" #include "mozilla/StaticPrefs_layout.h" #include "mozilla/StaticPrefs_mathml.h" #include "mozilla/Unused.h" #include "RetainedDisplayListBuilder.h" #include "nsAbsoluteContainingBlock.h" #include "nsMenuBarListener.h" #include "nsCSSPseudoElements.h" #include "nsCheckboxRadioFrame.h" #include "nsCRT.h" #include "nsAtom.h" #include "nsIFrameInlines.h" #include "nsGkAtoms.h" #include "nsPresContext.h" #include "mozilla/dom/Document.h" #include "mozilla/dom/DocumentInlines.h" #include "nsTableFrame.h" #include "nsTableColFrame.h" #include "nsTableRowFrame.h" #include "nsTableCellFrame.h" #include "nsHTMLParts.h" #include "nsUnicharUtils.h" #include "nsViewManager.h" #include "nsStyleConsts.h" #include "nsXULElement.h" #include "nsContainerFrame.h" #include "nsNameSpaceManager.h" #include "nsComboboxControlFrame.h" #include "nsListControlFrame.h" #include "nsPlaceholderFrame.h" #include "nsTableRowGroupFrame.h" #include "nsIFormControl.h" #include "nsCSSAnonBoxes.h" #include "nsTextFragment.h" #include "nsTextBoxFrame.h" #include "nsIAnonymousContentCreator.h" #include "nsContentUtils.h" #include "nsIScriptError.h" #ifdef XP_MACOSX # include "nsIDocShell.h" #endif #include "ChildIterator.h" #include "nsError.h" #include "nsLayoutUtils.h" #include "nsBoxFrame.h" #include "nsBoxLayout.h" #include "nsFlexContainerFrame.h" #include "nsGridContainerFrame.h" #include "RubyUtils.h" #include "nsRubyFrame.h" #include "nsRubyBaseFrame.h" #include "nsRubyBaseContainerFrame.h" #include "nsRubyTextFrame.h" #include "nsRubyTextContainerFrame.h" #include "nsImageFrame.h" #include "nsIObjectLoadingContent.h" #include "nsTArray.h" #include "mozilla/dom/CharacterData.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/ElementInlines.h" #include "mozilla/dom/HTMLInputElement.h" #include "nsAutoLayoutPhase.h" #include "nsStyleStructInlines.h" #include "nsPageContentFrame.h" #include "mozilla/RestyleManager.h" #include "StickyScrollContainer.h" #include "nsFieldSetFrame.h" #include "nsInlineFrame.h" #include "nsBlockFrame.h" #include "nsCanvasFrame.h" #include "nsFirstLetterFrame.h" #include "nsGfxScrollFrame.h" #include "nsPageFrame.h" #include "nsPageSequenceFrame.h" #include "nsTableWrapperFrame.h" #include "nsIScrollableFrame.h" #include "nsBackdropFrame.h" #include "nsTransitionManager.h" #include "nsIPopupContainer.h" #ifdef ACCESSIBILITY # include "nsAccessibilityService.h" #endif #undef NOISY_FIRST_LETTER #include "nsMathMLParts.h" #include "mozilla/dom/SVGAnimationElement.h" #include "mozilla/dom/SVGFilters.h" #include "mozilla/dom/SVGTests.h" #include "mozilla/SVGGradientFrame.h" #include "mozilla/SVGUtils.h" #include "nsRefreshDriver.h" #include "nsTextNode.h" #include "ActiveLayerTracker.h" using namespace mozilla; using namespace mozilla::dom; nsIFrame* NS_NewHTMLCanvasFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewHTMLVideoFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsContainerFrame* NS_NewSVGOuterSVGFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsContainerFrame* NS_NewSVGOuterSVGAnonChildFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGInnerSVGFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGGeometryFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGGFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsContainerFrame* NS_NewSVGForeignObjectFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGAFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGSwitchFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGSymbolFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGTextFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGContainerFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGUseFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGViewFrame(PresShell* aPresShell, ComputedStyle* aStyle); extern nsIFrame* NS_NewSVGLinearGradientFrame(PresShell* aPresShell, ComputedStyle* aStyle); extern nsIFrame* NS_NewSVGRadialGradientFrame(PresShell* aPresShell, ComputedStyle* aStyle); extern nsIFrame* NS_NewSVGStopFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsContainerFrame* NS_NewSVGMarkerFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsContainerFrame* NS_NewSVGMarkerAnonChildFrame(PresShell* aPresShell, ComputedStyle* aStyle); extern nsIFrame* NS_NewSVGImageFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGClipPathFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGFilterFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGPatternFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGMaskFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGFEContainerFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGFELeafFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGFEImageFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSVGFEUnstyledLeafFrame(PresShell* aPresShell, ComputedStyle* aStyle); #include "mozilla/dom/NodeInfo.h" #include "prenv.h" #include "nsNodeInfoManager.h" #include "nsContentCreatorFunctions.h" #ifdef DEBUG // Set the environment variable GECKO_FRAMECTOR_DEBUG_FLAGS to one or // more of the following flags (comma separated) for handy debug // output. static bool gNoisyContentUpdates = false; static bool gReallyNoisyContentUpdates = false; static bool gNoisyInlineConstruction = false; struct FrameCtorDebugFlags { const char* name; bool* on; }; static FrameCtorDebugFlags gFlags[] = { {"content-updates", &gNoisyContentUpdates}, {"really-noisy-content-updates", &gReallyNoisyContentUpdates}, {"noisy-inline", &gNoisyInlineConstruction}}; # define NUM_DEBUG_FLAGS (sizeof(gFlags) / sizeof(gFlags[0])) #endif #include "nsTreeColFrame.h" //------------------------------------------------------------------ nsIFrame* NS_NewLeafBoxFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewRangeFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewImageBoxFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewTextBoxFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewSplitterFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewMenuPopupFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewMenuFrame(PresShell* aPresShell, ComputedStyle* aStyle, uint32_t aFlags); nsIFrame* NS_NewMenuBarFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewTreeBodyFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsHTMLScrollFrame* NS_NewHTMLScrollFrame(PresShell* aPresShell, ComputedStyle* aStyle, bool aIsRoot); nsXULScrollFrame* NS_NewXULScrollFrame(PresShell* aPresShell, ComputedStyle* aStyle, bool aIsRoot); nsIFrame* NS_NewSliderFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewScrollbarFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewScrollbarButtonFrame(PresShell* aPresShell, ComputedStyle* aStyle); nsIFrame* NS_NewImageFrameForContentProperty(PresShell*, ComputedStyle*); nsIFrame* NS_NewImageFrameForGeneratedContentIndex(PresShell*, ComputedStyle*); nsIFrame* NS_NewImageFrameForListStyleImage(PresShell*, ComputedStyle*); // Returns true if aFrame is an anonymous flex/grid item. static inline bool IsAnonymousItem(const nsIFrame* aFrame) { return aFrame->Style()->GetPseudoType() == PseudoStyleType::anonymousItem; } // Returns true IFF the given nsIFrame is a nsFlexContainerFrame and represents // a -webkit-{inline-}box container. static inline bool IsFlexContainerForLegacyWebKitBox(const nsIFrame* aFrame) { return aFrame->IsFlexContainerFrame() && aFrame->HasAnyStateBits(NS_STATE_FLEX_IS_EMULATING_LEGACY_WEBKIT_BOX); } #if DEBUG static void AssertAnonymousFlexOrGridItemParent(const nsIFrame* aChild, const nsIFrame* aParent) { MOZ_ASSERT(IsAnonymousItem(aChild), "expected an anonymous item child frame"); MOZ_ASSERT(aParent, "expected a parent frame"); MOZ_ASSERT(aParent->IsFlexContainerFrame() || aParent->IsGridContainerFrame() || aParent->IsXULBoxFrame(), "anonymous items should only exist as children of " "flex/grid/-moz-box container frames"); } #else # define AssertAnonymousFlexOrGridItemParent(x, y) PR_BEGIN_MACRO PR_END_MACRO #endif #define ToCreationFunc(_func) \ [](PresShell* aPs, ComputedStyle* aStyle) -> nsIFrame* { \ return _func(aPs, aStyle); \ } /** * True if aFrame is an actual inline frame in the sense of non-replaced * display:inline CSS boxes. In other words, it can be affected by {ib} * splitting and can contain first-letter frames. Basically, this is either an * inline frame (positioned or otherwise) or an line frame (this last because * it can contain first-letter and because inserting blocks in the middle of it * needs to terminate it). */ static bool IsInlineFrame(const nsIFrame* aFrame) { return aFrame->IsFrameOfType(nsIFrame::eLineParticipant); } /** * True for display: contents elements. */ static inline bool IsDisplayContents(const Element* aElement) { return aElement->IsDisplayContents(); } static inline bool IsDisplayContents(const nsIContent* aContent) { return aContent->IsElement() && IsDisplayContents(aContent->AsElement()); } /** * True if aFrame is an instance of an SVG frame class or is an inline/block * frame being used for SVG text. */ static bool IsFrameForSVG(const nsIFrame* aFrame) { return aFrame->IsFrameOfType(nsIFrame::eSVG) || SVGUtils::IsInSVGTextSubtree(aFrame); } static bool IsLastContinuationForColumnContent(const nsIFrame* aFrame) { MOZ_ASSERT(aFrame); return aFrame->Style()->GetPseudoType() == PseudoStyleType::columnContent && !aFrame->GetNextContinuation(); } /** * Returns true iff aFrame explicitly prevents its descendants from floating * (at least, down to the level of descendants which themselves are * float-containing blocks -- those will manage the floating status of any * lower-level descendents inside them, of course). */ static bool ShouldSuppressFloatingOfDescendants(nsIFrame* aFrame) { return aFrame->IsFlexOrGridContainer() || aFrame->IsXULBoxFrame() || aFrame->IsFrameOfType(nsIFrame::eMathML); } // Return true if column-span descendants should be suppressed under aFrame's // subtree (until a multi-column container re-establishing a block formatting // context). Basically, this is testing whether aFrame establishes a new block // formatting context or not. static bool ShouldSuppressColumnSpanDescendants(nsIFrame* aFrame) { if (aFrame->Style()->GetPseudoType() == PseudoStyleType::columnContent) { // Never suppress column-span under ::-moz-column-content frames. return false; } if (aFrame->IsInlineFrame()) { // Allow inline frames to have column-span block children. return false; } if (!aFrame->IsBlockFrameOrSubclass() || aFrame->HasAnyStateBits(NS_BLOCK_FLOAT_MGR | NS_FRAME_OUT_OF_FLOW) || aFrame->IsFixedPosContainingBlock()) { // Need to suppress column-span if we: // - Are a different block formatting context, // - Are an out-of-flow frame, OR // - Establish a containing block for fixed-position descendants // // For example, the children of a column-span never need to be further // processed even if there is a nested column-span child. Because a // column-span always creates its own block formatting context, a nested // column-span child won't be in the same block formatting context with the // nearest multi-column ancestor. This is the same case as if the // column-span is outside of a multi-column hierarchy. return true; } return false; } // Reparent a frame into a wrapper frame that is a child of its old parent. static void ReparentFrame(RestyleManager* aRestyleManager, nsContainerFrame* aNewParentFrame, nsIFrame* aFrame, bool aForceStyleReparent) { aFrame->SetParent(aNewParentFrame); // We reparent frames for two reasons: to put them inside ::first-line, and to // put them inside some wrapper anonymous boxes. if (aForceStyleReparent) { aRestyleManager->ReparentComputedStyleForFirstLine(aFrame); } } static void ReparentFrames(nsCSSFrameConstructor* aFrameConstructor, nsContainerFrame* aNewParentFrame, const nsFrameList& aFrameList, bool aForceStyleReparent) { RestyleManager* restyleManager = aFrameConstructor->RestyleManager(); for (nsIFrame* f : aFrameList) { ReparentFrame(restyleManager, aNewParentFrame, f, aForceStyleReparent); } } //---------------------------------------------------------------------- // // When inline frames get weird and have block frames in them, we // annotate them to help us respond to incremental content changes // more easily. static inline bool IsFramePartOfIBSplit(nsIFrame* aFrame) { bool result = aFrame->HasAnyStateBits(NS_FRAME_PART_OF_IBSPLIT); MOZ_ASSERT(!result || static_cast(do_QueryFrame(aFrame)) || static_cast(do_QueryFrame(aFrame)), "only block/inline frames can have NS_FRAME_PART_OF_IBSPLIT"); return result; } static nsContainerFrame* GetIBSplitSibling(nsIFrame* aFrame) { MOZ_ASSERT(IsFramePartOfIBSplit(aFrame), "Shouldn't call this"); // We only store the "ib-split sibling" annotation with the first // frame in the continuation chain. Walk back to find that frame now. return aFrame->FirstContinuation()->GetProperty(nsIFrame::IBSplitSibling()); } static nsContainerFrame* GetIBSplitPrevSibling(nsIFrame* aFrame) { MOZ_ASSERT(IsFramePartOfIBSplit(aFrame), "Shouldn't call this"); // We only store the ib-split sibling annotation with the first // frame in the continuation chain. Walk back to find that frame now. return aFrame->FirstContinuation()->GetProperty( nsIFrame::IBSplitPrevSibling()); } static nsContainerFrame* GetLastIBSplitSibling(nsIFrame* aFrame) { for (nsIFrame *frame = aFrame, *next;; frame = next) { next = GetIBSplitSibling(frame); if (!next) { return static_cast(frame); } } MOZ_ASSERT_UNREACHABLE("unreachable code"); return nullptr; } static void SetFrameIsIBSplit(nsContainerFrame* aFrame, nsContainerFrame* aIBSplitSibling) { MOZ_ASSERT(aFrame, "bad args!"); // We should be the only continuation NS_ASSERTION(!aFrame->GetPrevContinuation(), "assigning ib-split sibling to other than first continuation!"); NS_ASSERTION(!aFrame->GetNextContinuation() || IsFramePartOfIBSplit(aFrame->GetNextContinuation()), "should have no non-ib-split continuations here"); // Mark the frame as ib-split. aFrame->AddStateBits(NS_FRAME_PART_OF_IBSPLIT); if (aIBSplitSibling) { NS_ASSERTION(!aIBSplitSibling->GetPrevContinuation(), "assigning something other than the first continuation as the " "ib-split sibling"); // Store the ib-split sibling (if we were given one) with the // first frame in the flow. aFrame->SetProperty(nsIFrame::IBSplitSibling(), aIBSplitSibling); aIBSplitSibling->SetProperty(nsIFrame::IBSplitPrevSibling(), aFrame); } } static nsIFrame* GetIBContainingBlockFor(nsIFrame* aFrame) { MOZ_ASSERT( IsFramePartOfIBSplit(aFrame), "GetIBContainingBlockFor() should only be called on known IB frames"); // Get the first "normal" ancestor of the target frame. nsIFrame* parentFrame; do { parentFrame = aFrame->GetParent(); if (!parentFrame) { NS_ERROR("no unsplit block frame in IB hierarchy"); return aFrame; } // Note that we ignore non-ib-split frames which have a pseudo on their // ComputedStyle -- they're not the frames we're looking for! In // particular, they may be hiding a real parent that _is_ in an ib-split. if (!IsFramePartOfIBSplit(parentFrame) && !parentFrame->Style()->IsPseudoOrAnonBox()) break; aFrame = parentFrame; } while (1); // post-conditions NS_ASSERTION(parentFrame, "no normal ancestor found for ib-split frame " "in GetIBContainingBlockFor"); NS_ASSERTION(parentFrame != aFrame, "parentFrame is actually the child frame - bogus reslt"); return parentFrame; } // Find the multicol containing block suitable for reframing. // // Note: this function may not return a ColumnSetWrapperFrame. For example, if // the multicol containing block has "overflow:scroll" style, HTMLScrollFrame is // returned because ColumnSetWrapperFrame is the scrolled frame which has the // -moz-scrolled-content pseudo style. We may walk up "too far", but in terms of // correctness of reframing, it's OK. static nsContainerFrame* GetMultiColumnContainingBlockFor(nsIFrame* aFrame) { MOZ_ASSERT(aFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR), "Should only be called if the frame has a multi-column ancestor!"); nsContainerFrame* current = aFrame->GetParent(); while (current && (current->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR) || current->Style()->IsPseudoOrAnonBox())) { current = current->GetParent(); } MOZ_ASSERT(current, "No multicol containing block in a valid column hierarchy?"); return current; } // This is a bit slow, but sometimes we need it. static bool ParentIsWrapperAnonBox(nsIFrame* aParent) { nsIFrame* maybeAnonBox = aParent; if (maybeAnonBox->Style()->GetPseudoType() == PseudoStyleType::cellContent) { // The thing that would maybe be a wrapper anon box is the cell. maybeAnonBox = maybeAnonBox->GetParent(); } return maybeAnonBox->Style()->IsWrapperAnonBox(); } //---------------------------------------------------------------------- // Block/inline frame construction logic. We maintain a few invariants here: // // 1. Block frames contain block and inline frames. // // 2. Inline frames only contain inline frames. If an inline parent has a block // child then the block child is migrated upward until it lands in a block // parent (the inline frames containing block is where it will end up). inline void SetInitialSingleChild(nsContainerFrame* aParent, nsIFrame* aFrame) { MOZ_ASSERT(!aFrame->GetNextSibling(), "Should be using a frame list"); aParent->SetInitialChildList(FrameChildListID::Principal, nsFrameList(aFrame, aFrame)); } // ----------------------------------------------------------- // Structure used when constructing formatting object trees. Contains // state information needed for absolutely positioned elements namespace mozilla { struct AbsoluteFrameList final : public nsFrameList { // Containing block for absolutely positioned elements. nsContainerFrame* mContainingBlock; explicit AbsoluteFrameList(nsContainerFrame* aContainingBlock = nullptr) : mContainingBlock(aContainingBlock) {} // Transfer frames in aOther to this list. aOther becomes empty after this // operation. AbsoluteFrameList(AbsoluteFrameList&& aOther) = default; AbsoluteFrameList& operator=(AbsoluteFrameList&& aOther) = default; #ifdef DEBUG // XXXbz Does this need a debug-only assignment operator that nulls out the // childList in the AbsoluteFrameList we're copying? Introducing a difference // between debug and non-debug behavior seems bad, so I guess not... ~AbsoluteFrameList() { NS_ASSERTION(!FirstChild(), "Dangling child list. Someone forgot to insert it?"); } #endif }; } // namespace mozilla // ----------------------------------------------------------- // Structure for saving the existing state when pushing/poping containing // blocks. The destructor restores the state to its previous state class MOZ_STACK_CLASS nsFrameConstructorSaveState { public: ~nsFrameConstructorSaveState(); private: // Pointer to struct whose data we save/restore. AbsoluteFrameList* mList = nullptr; // Copy of original frame list. This can be the original absolute list or a // float list. If we're saving the abs-pos state for a transformed element, // i.e. when mSavedFixedPosIsAbsPos is true, this is the original fixed list. AbsoluteFrameList mSavedList; // The name of the child list in which our frames would belong. mozilla::FrameChildListID mChildListID = FrameChildListID::Principal; nsFrameConstructorState* mState = nullptr; // Only used when saving an absolute list. See the description of // nsFrameConstructorState::mFixedPosIsAbsPos for its meaning. bool mSavedFixedPosIsAbsPos = false; friend class nsFrameConstructorState; }; // Structure used for maintaining state information during the // frame construction process class MOZ_STACK_CLASS nsFrameConstructorState { public: nsPresContext* mPresContext; PresShell* mPresShell; nsFrameManager* mFrameManager; // Containing block information for out-of-flow frames. AbsoluteFrameList mFixedList; AbsoluteFrameList mAbsoluteList; AbsoluteFrameList mFloatedList; // The containing block of a frame in the top layer is defined by the // spec: fixed-positioned frames are children of the viewport frame, // and absolutely-positioned frames are children of the initial // containing block. They would not be caught by any other containing // block, e.g. frames with transform or filter. AbsoluteFrameList mTopLayerFixedList; AbsoluteFrameList mTopLayerAbsoluteList; // What `page: auto` resolves to. This is the used page-name of the parent // frame. Updated by AutoFrameConstructionPageName. const nsAtom* mAutoPageNameValue; nsCOMPtr mFrameState; // These bits will be added to the state bits of any frame we construct // using this state. nsFrameState mAdditionalStateBits; // When working with transform / filter properties, we want to hook the // abs-pos and fixed-pos lists together, since such elements are fixed-pos // containing blocks. // // Similarly when restricting absolute positioning (for e.g. mathml). // // This flag determines whether or not we want to wire the fixed-pos and // abs-pos lists together. bool mFixedPosIsAbsPos; // If false (which is the default) then call SetPrimaryFrame() as needed // during frame construction. If true, don't make any SetPrimaryFrame() // calls, except for generated content which doesn't have a primary frame // yet. The mCreatingExtraFrames == true mode is meant to be used for // construction of random "extra" frames for elements via normal frame // construction APIs (e.g. replication of things across pages in paginated // mode). bool mCreatingExtraFrames; // This keeps track of whether we have found a "rendered legend" for // the current FieldSetFrame. bool mHasRenderedLegend; nsTArray> mGeneratedContentWithInitializer; #ifdef DEBUG // Record the float containing block candidate passed into // MaybePushFloatContainingBlock() to keep track that we've call the method to // handle the float CB scope before processing the CB's children. It is reset // in ConstructFramesFromItemList(). nsContainerFrame* mFloatCBCandidate = nullptr; #endif // Constructor // Use the passed-in history state. nsFrameConstructorState( PresShell* aPresShell, nsContainerFrame* aFixedContainingBlock, nsContainerFrame* aAbsoluteContainingBlock, nsContainerFrame* aFloatContainingBlock, already_AddRefed aHistoryState); // Get the history state from the pres context's pres shell. nsFrameConstructorState(PresShell* aPresShell, nsContainerFrame* aFixedContainingBlock, nsContainerFrame* aAbsoluteContainingBlock, nsContainerFrame* aFloatContainingBlock); ~nsFrameConstructorState(); // Process the frame insertions for all the out-of-flow nsAbsoluteItems. void ProcessFrameInsertionsForAllLists(); // Function to push the existing absolute containing block state and // create a new scope. Code that uses this function should get matching // logic in GetAbsoluteContainingBlock. // Also makes aNewAbsoluteContainingBlock the containing block for // fixed-pos elements if necessary. // aPositionedFrame is the frame whose style actually makes // aNewAbsoluteContainingBlock a containing block. E.g. for a scrollable // element aPositionedFrame is the element's primary frame and // aNewAbsoluteContainingBlock is the scrolled frame. void PushAbsoluteContainingBlock( nsContainerFrame* aNewAbsoluteContainingBlock, nsIFrame* aPositionedFrame, nsFrameConstructorSaveState& aSaveState); // Function to forbid floats descendants under aFloatCBCandidate, or open a // new float containing block scope for aFloatCBCandidate. The current // state is saved in aSaveState if a new scope is pushed. void MaybePushFloatContainingBlock(nsContainerFrame* aFloatCBCandidate, nsFrameConstructorSaveState& aSaveState); // Helper function for MaybePushFloatContainingBlock(). void PushFloatContainingBlock(nsContainerFrame* aNewFloatContainingBlock, nsFrameConstructorSaveState& aSaveState); // Function to return the proper geometric parent for a frame with display // struct given by aStyleDisplay and parent's frame given by // aContentParentFrame. nsContainerFrame* GetGeometricParent( const nsStyleDisplay& aStyleDisplay, nsContainerFrame* aContentParentFrame) const; // Collect absolute frames in mAbsoluteList which are proper descendants // of aNewParent, and reparent them to aNewParent. // // Note: This function does something unusual that moves absolute items // after their frames are constructed under a column hierarchy which has // column-span elements. Do not use this if you're not dealing with // columns. void ReparentAbsoluteItems(nsContainerFrame* aNewParent); // Collect floats in mFloatedList which are proper descendants of aNewParent, // and reparent them to aNewParent. // // Note: This function does something unusual that moves floats after their // frames are constructed under a column hierarchy which has column-span // elements. Do not use this if you're not dealing with columns. void ReparentFloats(nsContainerFrame* aNewParent); /** * Function to add a new frame to the right frame list. This MUST be called * on frames before their children have been processed if the frames might * conceivably be out-of-flow; otherwise cleanup in error cases won't work * right. Also, this MUST be called on frames after they have been * initialized. * @param aNewFrame the frame to add * @param aFrameList the list to add in-flow frames to * @param aContent the content pointer for aNewFrame * @param aParentFrame the parent frame for the content if it were in-flow * @param aCanBePositioned pass false if the frame isn't allowed to be * positioned * @param aCanBeFloated pass false if the frame isn't allowed to be * floated */ void AddChild(nsIFrame* aNewFrame, nsFrameList& aFrameList, nsIContent* aContent, nsContainerFrame* aParentFrame, bool aCanBePositioned = true, bool aCanBeFloated = true, bool aInsertAfter = false, nsIFrame* aInsertAfterFrame = nullptr); /** * Function to return the fixed-pos element list. Normally this will just * hand back the fixed-pos element list, but in case we're dealing with a * transformed element that's acting as an abs-pos and fixed-pos container, * we'll hand back the abs-pos list. Callers should use this function if they * want to get the list acting as the fixed-pos item parent. */ AbsoluteFrameList& GetFixedList() { return mFixedPosIsAbsPos ? mAbsoluteList : mFixedList; } const AbsoluteFrameList& GetFixedList() const { return mFixedPosIsAbsPos ? mAbsoluteList : mFixedList; } protected: friend class nsFrameConstructorSaveState; /** * ProcessFrameInsertions takes the frames in aFrameList and adds them as * kids to the aChildListID child list of |aFrameList.containingBlock|. */ void ProcessFrameInsertions(AbsoluteFrameList& aFrameList, mozilla::FrameChildListID aChildListID); /** * GetOutOfFlowFrameList selects the out-of-flow frame list the new * frame should be added to. If the frame shouldn't be added to any * out-of-flow list, it returns nullptr. The corresponding type of * placeholder is also returned via the aPlaceholderType parameter * if this method doesn't return nullptr. The caller should check * whether the returned list really has a containing block. */ AbsoluteFrameList* GetOutOfFlowFrameList(nsIFrame* aNewFrame, bool aCanBePositioned, bool aCanBeFloated, nsFrameState* aPlaceholderType); void ConstructBackdropFrameFor(nsIContent* aContent, nsIFrame* aFrame); }; nsFrameConstructorState::nsFrameConstructorState( PresShell* aPresShell, nsContainerFrame* aFixedContainingBlock, nsContainerFrame* aAbsoluteContainingBlock, nsContainerFrame* aFloatContainingBlock, already_AddRefed aHistoryState) : mPresContext(aPresShell->GetPresContext()), mPresShell(aPresShell), mFrameManager(aPresShell->FrameConstructor()), mFixedList(aFixedContainingBlock), mAbsoluteList(aAbsoluteContainingBlock), mFloatedList(aFloatContainingBlock), mTopLayerFixedList( static_cast(mFrameManager->GetRootFrame())), mTopLayerAbsoluteList( aPresShell->FrameConstructor()->GetDocElementContainingBlock()), // Will be set by AutoFrameConstructionPageName mAutoPageNameValue(nullptr), // See PushAbsoluteContaningBlock below mFrameState(aHistoryState), mAdditionalStateBits(nsFrameState(0)), // If the fixed-pos containing block is equal to the abs-pos containing // block, use the abs-pos containing block's abs-pos list for fixed-pos // frames. mFixedPosIsAbsPos(aFixedContainingBlock == aAbsoluteContainingBlock), mCreatingExtraFrames(false), mHasRenderedLegend(false) { MOZ_COUNT_CTOR(nsFrameConstructorState); } nsFrameConstructorState::nsFrameConstructorState( PresShell* aPresShell, nsContainerFrame* aFixedContainingBlock, nsContainerFrame* aAbsoluteContainingBlock, nsContainerFrame* aFloatContainingBlock) : nsFrameConstructorState( aPresShell, aFixedContainingBlock, aAbsoluteContainingBlock, aFloatContainingBlock, aPresShell->GetDocument()->GetLayoutHistoryState()) {} nsFrameConstructorState::~nsFrameConstructorState() { MOZ_COUNT_DTOR(nsFrameConstructorState); ProcessFrameInsertionsForAllLists(); for (auto& content : Reversed(mGeneratedContentWithInitializer)) { content->RemoveProperty(nsGkAtoms::genConInitializerProperty); } } void nsFrameConstructorState::ProcessFrameInsertionsForAllLists() { ProcessFrameInsertions(mTopLayerFixedList, FrameChildListID::Fixed); ProcessFrameInsertions(mTopLayerAbsoluteList, FrameChildListID::Absolute); ProcessFrameInsertions(mFloatedList, FrameChildListID::Float); ProcessFrameInsertions(mAbsoluteList, FrameChildListID::Absolute); ProcessFrameInsertions(mFixedList, FrameChildListID::Fixed); } void nsFrameConstructorState::PushAbsoluteContainingBlock( nsContainerFrame* aNewAbsoluteContainingBlock, nsIFrame* aPositionedFrame, nsFrameConstructorSaveState& aSaveState) { MOZ_ASSERT(!!aNewAbsoluteContainingBlock == !!aPositionedFrame, "We should have both or none"); aSaveState.mList = &mAbsoluteList; aSaveState.mChildListID = FrameChildListID::Absolute; aSaveState.mState = this; aSaveState.mSavedFixedPosIsAbsPos = mFixedPosIsAbsPos; if (mFixedPosIsAbsPos) { // Since we're going to replace mAbsoluteList, we need to save it into // mFixedList now (and save the current value of mFixedList). aSaveState.mSavedList = std::move(mFixedList); mFixedList = std::move(mAbsoluteList); } else { // Otherwise, we just save mAbsoluteList. aSaveState.mSavedList = std::move(mAbsoluteList); } mAbsoluteList = AbsoluteFrameList(aNewAbsoluteContainingBlock); /* See if we're wiring the fixed-pos and abs-pos lists together. This happens * if we're a transformed/filtered/etc element, or if we force a null abspos * containing block (for mathml for example). */ mFixedPosIsAbsPos = !aPositionedFrame || aPositionedFrame->IsFixedPosContainingBlock(); if (aNewAbsoluteContainingBlock) { aNewAbsoluteContainingBlock->MarkAsAbsoluteContainingBlock(); } } void nsFrameConstructorState::MaybePushFloatContainingBlock( nsContainerFrame* aFloatCBCandidate, nsFrameConstructorSaveState& aSaveState) { // The logic here needs to match the logic in GetFloatContainingBlock(). if (ShouldSuppressFloatingOfDescendants(aFloatCBCandidate)) { // Pushing a null float containing block forbids any frames from being // floated until a new float containing block is pushed. See implementation // of nsFrameConstructorState::AddChild(). // // XXX we should get rid of null float containing blocks and teach the // various frame classes to deal with floats instead. PushFloatContainingBlock(nullptr, aSaveState); } else if (aFloatCBCandidate->IsFloatContainingBlock()) { PushFloatContainingBlock(aFloatCBCandidate, aSaveState); } #ifdef DEBUG mFloatCBCandidate = aFloatCBCandidate; #endif } void nsFrameConstructorState::PushFloatContainingBlock( nsContainerFrame* aNewFloatContainingBlock, nsFrameConstructorSaveState& aSaveState) { MOZ_ASSERT(!aNewFloatContainingBlock || aNewFloatContainingBlock->IsFloatContainingBlock(), "Please push a real float containing block!"); NS_ASSERTION( !aNewFloatContainingBlock || !ShouldSuppressFloatingOfDescendants(aNewFloatContainingBlock), "We should not push a frame that is supposed to _suppress_ " "floats as a float containing block!"); aSaveState.mList = &mFloatedList; aSaveState.mSavedList = std::move(mFloatedList); aSaveState.mChildListID = FrameChildListID::Float; aSaveState.mState = this; mFloatedList = AbsoluteFrameList(aNewFloatContainingBlock); } nsContainerFrame* nsFrameConstructorState::GetGeometricParent( const nsStyleDisplay& aStyleDisplay, nsContainerFrame* aContentParentFrame) const { // If there is no container for a fixed, absolute, or floating root // frame, we will ignore the positioning. This hack is originally // brought to you by the letter T: tables, since other roots don't // even call into this code. See bug 178855. // // XXX Disabling positioning in this case is a hack. If one was so inclined, // one could support this either by (1) inserting a dummy block between the // table and the canvas or (2) teaching the canvas how to reflow positioned // elements. (1) has the usual problems when multiple frames share the same // content (notice all the special cases in this file dealing with inner // tables and table wrappers which share the same content). (2) requires some // work and possible factoring. // // XXXbz couldn't we just force position to "static" on roots and // float to "none"? That's OK per CSS 2.1, as far as I can tell. if (aContentParentFrame && SVGUtils::IsInSVGTextSubtree(aContentParentFrame)) { return aContentParentFrame; } if (aStyleDisplay.IsFloatingStyle() && mFloatedList.mContainingBlock) { NS_ASSERTION(!aStyleDisplay.IsAbsolutelyPositionedStyle(), "Absolutely positioned _and_ floating?"); return mFloatedList.mContainingBlock; } if (aStyleDisplay.mTopLayer != StyleTopLayer::None) { MOZ_ASSERT(aStyleDisplay.mTopLayer == StyleTopLayer::Top, "-moz-top-layer should be either none or top"); MOZ_ASSERT(aStyleDisplay.IsAbsolutelyPositionedStyle(), "Top layer items should always be absolutely positioned"); if (aStyleDisplay.mPosition == StylePositionProperty::Fixed) { MOZ_ASSERT(mTopLayerFixedList.mContainingBlock, "No root frame?"); return mTopLayerFixedList.mContainingBlock; } MOZ_ASSERT(aStyleDisplay.mPosition == StylePositionProperty::Absolute); MOZ_ASSERT(mTopLayerAbsoluteList.mContainingBlock); return mTopLayerAbsoluteList.mContainingBlock; } if (aStyleDisplay.mPosition == StylePositionProperty::Absolute && mAbsoluteList.mContainingBlock) { return mAbsoluteList.mContainingBlock; } if (aStyleDisplay.mPosition == StylePositionProperty::Fixed && GetFixedList().mContainingBlock) { return GetFixedList().mContainingBlock; } return aContentParentFrame; } void nsFrameConstructorState::ReparentAbsoluteItems( nsContainerFrame* aNewParent) { // Bug 1491727: This function might not conform to the spec. See // https://github.com/w3c/csswg-drafts/issues/1894. MOZ_ASSERT(aNewParent->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR), "Restrict the usage under column hierarchy."); AbsoluteFrameList newAbsoluteItems(aNewParent); nsIFrame* current = mAbsoluteList.FirstChild(); while (current) { nsIFrame* placeholder = current->GetPlaceholderFrame(); if (nsLayoutUtils::IsProperAncestorFrame(aNewParent, placeholder)) { nsIFrame* next = current->GetNextSibling(); mAbsoluteList.RemoveFrame(current); newAbsoluteItems.AppendFrame(aNewParent, current); current = next; } else { current = current->GetNextSibling(); } } if (newAbsoluteItems.NotEmpty()) { // ~nsFrameConstructorSaveState() will move newAbsoluteItems to // aNewParent's absolute child list. nsFrameConstructorSaveState absoluteSaveState; // It doesn't matter whether aNewParent has position style or not. Caller // won't call us if we can't have absolute children. PushAbsoluteContainingBlock(aNewParent, aNewParent, absoluteSaveState); mAbsoluteList = std::move(newAbsoluteItems); } } void nsFrameConstructorState::ReparentFloats(nsContainerFrame* aNewParent) { MOZ_ASSERT(aNewParent->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR), "Restrict the usage under column hierarchy."); MOZ_ASSERT( aNewParent->IsFloatContainingBlock(), "Why calling this method if aNewParent is not a float containing block?"); // Gather floats that should reparent under aNewParent. AbsoluteFrameList floats(aNewParent); nsIFrame* current = mFloatedList.FirstChild(); while (current) { nsIFrame* placeholder = current->GetPlaceholderFrame(); nsIFrame* next = current->GetNextSibling(); if (nsLayoutUtils::IsProperAncestorFrame(aNewParent, placeholder)) { mFloatedList.RemoveFrame(current); floats.AppendFrame(aNewParent, current); } current = next; } if (floats.NotEmpty()) { // Make floats move into aNewParent's float child list in // ~nsFrameConstructorSaveState() when destructing floatSaveState. nsFrameConstructorSaveState floatSaveState; PushFloatContainingBlock(aNewParent, floatSaveState); mFloatedList = std::move(floats); } } AbsoluteFrameList* nsFrameConstructorState::GetOutOfFlowFrameList( nsIFrame* aNewFrame, bool aCanBePositioned, bool aCanBeFloated, nsFrameState* aPlaceholderType) { const nsStyleDisplay* disp = aNewFrame->StyleDisplay(); if (aCanBeFloated && disp->IsFloatingStyle()) { *aPlaceholderType = PLACEHOLDER_FOR_FLOAT; return &mFloatedList; } if (aCanBePositioned) { if (disp->mTopLayer != StyleTopLayer::None) { *aPlaceholderType = PLACEHOLDER_FOR_TOPLAYER; if (disp->mPosition == StylePositionProperty::Fixed) { *aPlaceholderType |= PLACEHOLDER_FOR_FIXEDPOS; return &mTopLayerFixedList; } *aPlaceholderType |= PLACEHOLDER_FOR_ABSPOS; return &mTopLayerAbsoluteList; } if (disp->mPosition == StylePositionProperty::Absolute) { *aPlaceholderType = PLACEHOLDER_FOR_ABSPOS; return &mAbsoluteList; } if (disp->mPosition == StylePositionProperty::Fixed) { *aPlaceholderType = PLACEHOLDER_FOR_FIXEDPOS; return &GetFixedList(); } } return nullptr; } void nsFrameConstructorState::ConstructBackdropFrameFor(nsIContent* aContent, nsIFrame* aFrame) { MOZ_ASSERT(aFrame->StyleDisplay()->mTopLayer == StyleTopLayer::Top); nsContainerFrame* frame = do_QueryFrame(aFrame); if (!frame) { NS_WARNING("Cannot create backdrop frame for non-container frame"); return; } RefPtr style = mPresShell->StyleSet()->ResolvePseudoElementStyle( *aContent->AsElement(), PseudoStyleType::backdrop, /* aParentStyle */ nullptr); MOZ_ASSERT(style->StyleDisplay()->mTopLayer == StyleTopLayer::Top); nsContainerFrame* parentFrame = GetGeometricParent(*style->StyleDisplay(), nullptr); nsBackdropFrame* backdropFrame = new (mPresShell) nsBackdropFrame(style, mPresShell->GetPresContext()); backdropFrame->Init(aContent, parentFrame, nullptr); nsFrameState placeholderType; AbsoluteFrameList* frameList = GetOutOfFlowFrameList(backdropFrame, true, true, &placeholderType); MOZ_ASSERT(placeholderType & PLACEHOLDER_FOR_TOPLAYER); nsIFrame* placeholder = nsCSSFrameConstructor::CreatePlaceholderFrameFor( mPresShell, aContent, backdropFrame, frame, nullptr, placeholderType); frame->SetInitialChildList(FrameChildListID::Backdrop, nsFrameList(placeholder, placeholder)); frameList->AppendFrame(nullptr, backdropFrame); } void nsFrameConstructorState::AddChild( nsIFrame* aNewFrame, nsFrameList& aFrameList, nsIContent* aContent, nsContainerFrame* aParentFrame, bool aCanBePositioned, bool aCanBeFloated, bool aInsertAfter, nsIFrame* aInsertAfterFrame) { MOZ_ASSERT(!aNewFrame->GetNextSibling(), "Shouldn't happen"); nsFrameState placeholderType; AbsoluteFrameList* outOfFlowFrameList = GetOutOfFlowFrameList( aNewFrame, aCanBePositioned, aCanBeFloated, &placeholderType); // The comments in GetGeometricParent regarding root table frames // all apply here, unfortunately. Thus, we need to check whether // the returned frame items really has containing block. nsFrameList* frameList; if (outOfFlowFrameList && outOfFlowFrameList->mContainingBlock) { MOZ_ASSERT(aNewFrame->GetParent() == outOfFlowFrameList->mContainingBlock, "Parent of the frame is not the containing block?"); frameList = outOfFlowFrameList; } else { frameList = &aFrameList; placeholderType = nsFrameState(0); } if (placeholderType) { NS_ASSERTION(frameList != &aFrameList, "Putting frame in-flow _and_ want a placeholder?"); nsIFrame* placeholderFrame = nsCSSFrameConstructor::CreatePlaceholderFrameFor( mPresShell, aContent, aNewFrame, aParentFrame, nullptr, placeholderType); placeholderFrame->AddStateBits(mAdditionalStateBits); // Add the placeholder frame to the flow aFrameList.AppendFrame(nullptr, placeholderFrame); if (placeholderType & PLACEHOLDER_FOR_TOPLAYER) { ConstructBackdropFrameFor(aContent, aNewFrame); } } #ifdef DEBUG else { NS_ASSERTION(aNewFrame->GetParent() == aParentFrame, "In-flow frame has wrong parent"); } #endif if (aInsertAfter) { frameList->InsertFrame(nullptr, aInsertAfterFrame, aNewFrame); } else { frameList->AppendFrame(nullptr, aNewFrame); } } // Some of this function's callers recurse 1000 levels deep in crashtests. On // platforms where stack limits are low, we can't afford to incorporate this // function's `AutoTArray`s into its callers' stack frames, so disable inlining. MOZ_NEVER_INLINE void nsFrameConstructorState::ProcessFrameInsertions( AbsoluteFrameList& aFrameList, FrameChildListID aChildListID) { #define NS_NONXUL_LIST_TEST \ (&aFrameList == &mFloatedList && aChildListID == FrameChildListID::Float) || \ ((&aFrameList == &mAbsoluteList || \ &aFrameList == &mTopLayerAbsoluteList) && \ aChildListID == FrameChildListID::Absolute) || \ ((&aFrameList == &mFixedList || &aFrameList == &mTopLayerFixedList) && \ aChildListID == FrameChildListID::Fixed) MOZ_ASSERT(NS_NONXUL_LIST_TEST, "Unexpected aFrameList/aChildListID combination"); if (aFrameList.IsEmpty()) { return; } nsContainerFrame* containingBlock = aFrameList.mContainingBlock; NS_ASSERTION(containingBlock, "Child list without containing block?"); if (aChildListID == FrameChildListID::Fixed) { // Put this frame on the transformed-frame's abs-pos list instead, if // it has abs-pos children instead of fixed-pos children. aChildListID = containingBlock->GetAbsoluteListID(); } // Insert the frames hanging out in aItems. We can use SetInitialChildList() // if the containing block hasn't been reflowed yet (so NS_FRAME_FIRST_REFLOW // is set) and doesn't have any frames in the aChildListID child list yet. const nsFrameList& childList = containingBlock->GetChildList(aChildListID); if (childList.IsEmpty() && containingBlock->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) { // If we're injecting absolutely positioned frames, inject them on the // absolute containing block if (aChildListID == containingBlock->GetAbsoluteListID()) { containingBlock->GetAbsoluteContainingBlock()->SetInitialChildList( containingBlock, aChildListID, std::move(aFrameList)); } else { containingBlock->SetInitialChildList(aChildListID, std::move(aFrameList)); } } else if (aChildListID == FrameChildListID::Fixed || aChildListID == FrameChildListID::Absolute) { // The order is not important for abs-pos/fixed-pos frame list, just // append the frame items to the list directly. mFrameManager->AppendFrames(containingBlock, aChildListID, std::move(aFrameList)); } else { // Note that whether the frame construction context is doing an append or // not is not helpful here, since it could be appending to some frame in // the middle of the document, which means we're not necessarily // appending to the children of the containing block. // // We need to make sure the 'append to the end of document' case is fast. // So first test the last child of the containing block nsIFrame* lastChild = childList.LastChild(); // CompareTreePosition uses placeholder hierarchy for out of flow frames, // so this will make out-of-flows respect the ordering of placeholders, // which is great because it takes care of anonymous content. nsIFrame* firstNewFrame = aFrameList.FirstChild(); // Cache the ancestor chain so that we can reuse it if needed. AutoTArray firstNewFrameAncestors; nsIFrame* notCommonAncestor = nullptr; if (lastChild) { notCommonAncestor = nsLayoutUtils::FillAncestors( firstNewFrame, containingBlock, &firstNewFrameAncestors); } if (!lastChild || nsLayoutUtils::CompareTreePosition( lastChild, firstNewFrame, firstNewFrameAncestors, notCommonAncestor ? containingBlock : nullptr) < 0) { // no lastChild, or lastChild comes before the new children, so just // append mFrameManager->AppendFrames(containingBlock, aChildListID, std::move(aFrameList)); } else { // Try the other children. First collect them to an array so that a // reasonable fast binary search can be used to find the insertion point. AutoTArray children; for (nsIFrame* f = childList.FirstChild(); f != lastChild; f = f->GetNextSibling()) { children.AppendElement(f); } nsIFrame* insertionPoint = nullptr; int32_t imin = 0; int32_t max = children.Length(); while (max > imin) { int32_t imid = imin + ((max - imin) / 2); nsIFrame* f = children[imid]; int32_t compare = nsLayoutUtils::CompareTreePosition( f, firstNewFrame, firstNewFrameAncestors, notCommonAncestor ? containingBlock : nullptr); if (compare > 0) { // f is after the new frame. max = imid; insertionPoint = imid > 0 ? children[imid - 1] : nullptr; } else if (compare < 0) { // f is before the new frame. imin = imid + 1; insertionPoint = f; } else { // This is for the old behavior. Should be removed once it is // guaranteed that CompareTreePosition can't return 0! // See bug 928645. NS_WARNING("Something odd happening???"); insertionPoint = nullptr; for (uint32_t i = 0; i < children.Length(); ++i) { nsIFrame* f = children[i]; if (nsLayoutUtils::CompareTreePosition( f, firstNewFrame, firstNewFrameAncestors, notCommonAncestor ? containingBlock : nullptr) > 0) { break; } insertionPoint = f; } break; } } mFrameManager->InsertFrames(containingBlock, aChildListID, insertionPoint, std::move(aFrameList)); } } MOZ_ASSERT(aFrameList.IsEmpty(), "How did that happen?"); } nsFrameConstructorSaveState::~nsFrameConstructorSaveState() { // Restore the state if (mList) { MOZ_ASSERT(mState, "Can't have mList set without having a state!"); mState->ProcessFrameInsertions(*mList, mChildListID); if (mList == &mState->mAbsoluteList) { mState->mFixedPosIsAbsPos = mSavedFixedPosIsAbsPos; // mAbsoluteList was moved to mFixedList, so move mFixedList back // and repair the old mFixedList now. if (mSavedFixedPosIsAbsPos) { mState->mAbsoluteList = std::move(mState->mFixedList); mState->mFixedList = std::move(mSavedList); } else { mState->mAbsoluteList = std::move(mSavedList); } } else { mState->mFloatedList = std::move(mSavedList); } MOZ_ASSERT(mSavedList.IsEmpty(), "Frames in mSavedList should've moved back into mState!"); MOZ_ASSERT(!mList->LastChild() || !mList->LastChild()->GetNextSibling(), "Something corrupted our list!"); } } /** * Moves aFrameList from aOldParent to aNewParent. This updates the parent * pointer of the frames in the list, and reparents their views as needed. * nsIFrame::SetParent sets the NS_FRAME_HAS_VIEW bit on aNewParent and its * ancestors as needed. Then it sets the list as the initial child list * on aNewParent, unless aNewParent either already has kids or has been * reflowed; in that case it appends the new frames. Note that this * method differs from ReparentFrames in that it doesn't change the kids' * style. */ // XXXbz Since this is only used for {ib} splits, could we just copy the view // bits from aOldParent to aNewParent and then use the // nsFrameList::ApplySetParent? That would still leave us doing two passes // over the list, of course; if we really wanted to we could factor out the // relevant part of ReparentFrameViewList, I suppose... Or just get rid of // views, which would make most of this function go away. static void MoveChildrenTo(nsIFrame* aOldParent, nsContainerFrame* aNewParent, nsFrameList& aFrameList) { bool sameGrandParent = aOldParent->GetParent() == aNewParent->GetParent(); if (aNewParent->HasView() || aOldParent->HasView() || !sameGrandParent) { // Move the frames into the new view nsContainerFrame::ReparentFrameViewList(aFrameList, aOldParent, aNewParent); } aFrameList.ApplySetParent(aNewParent); if (aNewParent->PrincipalChildList().IsEmpty() && aNewParent->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) { aNewParent->SetInitialChildList(FrameChildListID::Principal, std::move(aFrameList)); } else { aNewParent->AppendFrames(FrameChildListID::Principal, std::move(aFrameList)); } } static bool MaybeApplyPageName(nsFrameConstructorState& aState, const StylePageName& aPageName) { if (aPageName.IsPageName()) { aState.mAutoPageNameValue = aPageName.AsPageName().AsAtom(); return true; } MOZ_ASSERT(aPageName.IsAuto(), "Impossible page name"); return false; } static void EnsureAutoPageName(nsFrameConstructorState& aState, const nsContainerFrame* const aFrame) { // Check if we need to figure out our used page name. // When building the entire document, this should only happen for the // root, which will mean the loop will immediately end. Either way, this will // only happen once for each time the frame constructor is run. if (aState.mAutoPageNameValue) { return; } for (const nsContainerFrame* frame = aFrame; frame; frame = frame->GetParent()) { const StylePageName& pageName = frame->StylePage()->mPage; if (MaybeApplyPageName(aState, pageName)) { return; } } // Ensure that a root with `page: auto` gets an empty page name // https://drafts.csswg.org/css-page-3/#using-named-pages aState.mAutoPageNameValue = nsGkAtoms::_empty; } nsCSSFrameConstructor::AutoFrameConstructionPageName:: AutoFrameConstructionPageName(nsFrameConstructorState& aState, nsIFrame* const aFrame) : mState(aState), mNameToRestore(nullptr) { if (!aState.mPresContext->IsPaginated() || !StaticPrefs::layout_css_named_pages_enabled()) { MOZ_ASSERT(!aState.mAutoPageNameValue, "Page name should not have been set"); return; } #ifdef DEBUG MOZ_ASSERT(!aFrame->mWasVisitedByAutoFrameConstructionPageName, "Frame should only have been visited once"); aFrame->mWasVisitedByAutoFrameConstructionPageName = true; #endif EnsureAutoPageName(aState, aFrame->GetParent()); mNameToRestore = aState.mAutoPageNameValue; MOZ_ASSERT(mNameToRestore, "Page name should have been found by EnsureAutoPageName"); MaybeApplyPageName(aState, aFrame->StylePage()->mPage); aFrame->SetAutoPageValue(aState.mAutoPageNameValue); } nsCSSFrameConstructor::AutoFrameConstructionPageName:: ~AutoFrameConstructionPageName() { // This isn't actually useful when not in paginated layout or when // layout.css.named-pages.enabled is false, but it's very likely cheaper to // unconditionally write this pointer than to test for paginated layout and // the value of the pref and then branch on the result. mState.mAutoPageNameValue = mNameToRestore; } //---------------------------------------------------------------------- nsCSSFrameConstructor::nsCSSFrameConstructor(Document* aDocument, PresShell* aPresShell) : nsFrameManager(aPresShell), mDocument(aDocument), mRootElementFrame(nullptr), mRootElementStyleFrame(nullptr), mDocElementContainingBlock(nullptr), mPageSequenceFrame(nullptr), mFirstFreeFCItem(nullptr), mFCItemsInUse(0), mCurrentDepth(0), mQuotesDirty(false), mCountersDirty(false), mAlwaysCreateFramesForIgnorableWhitespace(false) { #ifdef DEBUG static bool gFirstTime = true; if (gFirstTime) { gFirstTime = false; char* flags = PR_GetEnv("GECKO_FRAMECTOR_DEBUG_FLAGS"); if (flags) { bool error = false; for (;;) { char* comma = strchr(flags, ','); if (comma) *comma = '\0'; bool found = false; FrameCtorDebugFlags* flag = gFlags; FrameCtorDebugFlags* limit = gFlags + NUM_DEBUG_FLAGS; while (flag < limit) { if (nsCRT::strcasecmp(flag->name, flags) == 0) { *(flag->on) = true; printf("nsCSSFrameConstructor: setting %s debug flag on\n", flag->name); found = true; break; } ++flag; } if (!found) error = true; if (!comma) break; *comma = ','; flags = comma + 1; } if (error) { printf("Here are the available GECKO_FRAMECTOR_DEBUG_FLAGS:\n"); FrameCtorDebugFlags* flag = gFlags; FrameCtorDebugFlags* limit = gFlags + NUM_DEBUG_FLAGS; while (flag < limit) { printf(" %s\n", flag->name); ++flag; } printf( "Note: GECKO_FRAMECTOR_DEBUG_FLAGS is a comma separated list of " "flag\n"); printf("names (no whitespace)\n"); } } } #endif } void nsCSSFrameConstructor::NotifyDestroyingFrame(nsIFrame* aFrame) { if (aFrame->HasAnyStateBits(NS_FRAME_GENERATED_CONTENT) && mContainStyleScopeManager.DestroyQuoteNodesFor(aFrame)) { QuotesDirty(); } if (aFrame->HasAnyStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE) && mContainStyleScopeManager.DestroyCounterNodesFor(aFrame)) { // Technically we don't need to update anything if we destroyed only // USE nodes. However, this is unlikely to happen in the real world // since USE nodes generally go along with INCREMENT nodes. CountersDirty(); } if (aFrame->StyleDisplay()->IsContainStyle()) { mContainStyleScopeManager.DestroyScopesFor(aFrame); } RestyleManager()->NotifyDestroyingFrame(aFrame); } struct nsGenConInitializer { UniquePtr mNode; nsGenConList* mList; void (nsCSSFrameConstructor::*mDirtyAll)(); nsGenConInitializer(UniquePtr aNode, nsGenConList* aList, void (nsCSSFrameConstructor::*aDirtyAll)()) : mNode(std::move(aNode)), mList(aList), mDirtyAll(aDirtyAll) {} }; already_AddRefed nsCSSFrameConstructor::CreateGenConTextNode( nsFrameConstructorState& aState, const nsAString& aString, UniquePtr aInitializer) { RefPtr content = new (mDocument->NodeInfoManager()) nsTextNode(mDocument->NodeInfoManager()); content->SetText(aString, false); if (aInitializer) { aInitializer->mNode->mText = content; content->SetProperty(nsGkAtoms::genConInitializerProperty, aInitializer.release(), nsINode::DeleteProperty); aState.mGeneratedContentWithInitializer.AppendElement(content); } return content.forget(); } void nsCSSFrameConstructor::CreateGeneratedContent( nsFrameConstructorState& aState, Element& aOriginatingElement, ComputedStyle& aPseudoStyle, uint32_t aContentIndex, const FunctionRef aAddChild) { using Type = StyleContentItem::Tag; // Get the content value const auto& item = aPseudoStyle.StyleContent()->ContentAt(aContentIndex); const Type type = item.tag; switch (type) { case Type::Image: { RefPtr c = GeneratedImageContent::Create(*mDocument, aContentIndex); aAddChild(c); return; } case Type::String: { RefPtr text = CreateGenConTextNode( aState, NS_ConvertUTF8toUTF16(item.AsString().AsString()), nullptr); aAddChild(text); return; } case Type::Attr: { const auto& attr = item.AsAttr(); RefPtr attrName = attr.attribute.AsAtom(); int32_t attrNameSpace = kNameSpaceID_None; RefPtr ns = attr.namespace_url.AsAtom(); if (!ns->IsEmpty()) { nsresult rv = nsNameSpaceManager::GetInstance()->RegisterNameSpace( ns.forget(), attrNameSpace); NS_ENSURE_SUCCESS_VOID(rv); } if (mDocument->IsHTMLDocument() && aOriginatingElement.IsHTMLElement()) { ToLowerCaseASCII(attrName); } nsCOMPtr content; NS_NewAttributeContent(mDocument->NodeInfoManager(), attrNameSpace, attrName, getter_AddRefs(content)); aAddChild(content); return; } case Type::Counter: case Type::Counters: { RefPtr name; CounterStylePtr ptr; nsString separator; if (type == Type::Counter) { auto& counter = item.AsCounter(); name = counter._0.AsAtom(); ptr = CounterStylePtr::FromStyle(counter._1); } else { auto& counters = item.AsCounters(); name = counters._0.AsAtom(); CopyUTF8toUTF16(counters._1.AsString(), separator); ptr = CounterStylePtr::FromStyle(counters._2); } auto* counterList = mContainStyleScopeManager.GetOrCreateCounterList( aOriginatingElement, name); auto node = MakeUnique( std::move(ptr), std::move(separator), aContentIndex, /* aAllCounters = */ type == Type::Counters); auto initializer = MakeUnique( std::move(node), counterList, &nsCSSFrameConstructor::CountersDirty); RefPtr c = CreateGenConTextNode(aState, u""_ns, std::move(initializer)); aAddChild(c); return; } case Type::OpenQuote: case Type::CloseQuote: case Type::NoOpenQuote: case Type::NoCloseQuote: { auto node = MakeUnique(type, aContentIndex); auto* quoteList = mContainStyleScopeManager.QuoteListFor(aOriginatingElement); auto initializer = MakeUnique( std::move(node), quoteList, &nsCSSFrameConstructor::QuotesDirty); RefPtr c = CreateGenConTextNode(aState, u""_ns, std::move(initializer)); aAddChild(c); return; } case Type::MozLabelContent: { nsAutoString accesskey; if (!aOriginatingElement.GetAttr(nsGkAtoms::accesskey, accesskey) || accesskey.IsEmpty() || !nsMenuBarListener::GetMenuAccessKey()) { // Easy path: just return a regular value attribute content. nsCOMPtr content; NS_NewAttributeContent(mDocument->NodeInfoManager(), kNameSpaceID_None, nsGkAtoms::value, getter_AddRefs(content)); aAddChild(content); return; } nsAutoString value; aOriginatingElement.GetAttr(nsGkAtoms::value, value); auto AppendAccessKeyLabel = [&] { // Always append accesskey text in uppercase, see bug 1806167. ToUpperCase(accesskey); nsAutoString accessKeyLabel = u"("_ns + accesskey + u")"_ns; if (!StringEndsWith(value, accessKeyLabel)) { if (nsTextBoxFrame::InsertSeparatorBeforeAccessKey() && !value.IsEmpty() && !NS_IS_SPACE(value.Last())) { value.Append(' '); } value.Append(accessKeyLabel); } }; if (nsTextBoxFrame::AlwaysAppendAccessKey()) { AppendAccessKeyLabel(); RefPtr c = CreateGenConTextNode(aState, value, nullptr); aAddChild(c); return; } const auto accessKeyStart = [&]() -> Maybe { nsAString::const_iterator start, end; value.BeginReading(start); value.EndReading(end); const auto originalStart = start; // not appending access key - do case-sensitive search // first bool found = true; if (!FindInReadable(accesskey, start, end)) { start = originalStart; // didn't find it - perform a case-insensitive search found = FindInReadable(accesskey, start, end, nsCaseInsensitiveStringComparator); } if (!found) { return Nothing(); } return Some(Distance(originalStart, start)); }(); if (accessKeyStart.isNothing()) { AppendAccessKeyLabel(); RefPtr c = CreateGenConTextNode(aState, value, nullptr); aAddChild(c); return; } if (*accessKeyStart != 0) { RefPtr beginning = CreateGenConTextNode( aState, Substring(value, 0, *accessKeyStart), nullptr); aAddChild(beginning); } { RefPtr accessKeyText = CreateGenConTextNode( aState, Substring(value, *accessKeyStart, accesskey.Length()), nullptr); RefPtr underline = mDocument->CreateHTMLElement(nsGkAtoms::u); underline->AppendChildTo(accessKeyText, /* aNotify = */ false, IgnoreErrors()); aAddChild(underline); } size_t accessKeyEnd = *accessKeyStart + accesskey.Length(); if (accessKeyEnd != value.Length()) { RefPtr valueEnd = CreateGenConTextNode( aState, Substring(value, *accessKeyStart + accesskey.Length()), nullptr); aAddChild(valueEnd); } break; } case Type::MozAltContent: { // Use the "alt" attribute; if that fails and the node is an HTML // , try the value attribute and then fall back to some default // localized text we have. // XXX what if the 'alt' attribute is added later, how will we // detect that and do the right thing here? if (aOriginatingElement.HasAttr(nsGkAtoms::alt)) { nsCOMPtr content; NS_NewAttributeContent(mDocument->NodeInfoManager(), kNameSpaceID_None, nsGkAtoms::alt, getter_AddRefs(content)); aAddChild(content); return; } if (aOriginatingElement.IsHTMLElement(nsGkAtoms::input)) { if (aOriginatingElement.HasAttr(nsGkAtoms::value)) { nsCOMPtr content; NS_NewAttributeContent(mDocument->NodeInfoManager(), kNameSpaceID_None, nsGkAtoms::value, getter_AddRefs(content)); aAddChild(content); return; } nsAutoString temp; nsContentUtils::GetMaybeLocalizedString( nsContentUtils::eFORMS_PROPERTIES, "Submit", mDocument, temp); RefPtr c = CreateGenConTextNode(aState, temp, nullptr); aAddChild(c); return; } break; } } return; } void nsCSSFrameConstructor::CreateGeneratedContentFromListStyle( nsFrameConstructorState& aState, Element& aOriginatingElement, const ComputedStyle& aPseudoStyle, const FunctionRef aAddChild) { const nsStyleList* styleList = aPseudoStyle.StyleList(); if (!styleList->mListStyleImage.IsNone()) { RefPtr child = GeneratedImageContent::CreateForListStyleImage(*mDocument); aAddChild(child); child = CreateGenConTextNode(aState, u" "_ns, nullptr); aAddChild(child); return; } CreateGeneratedContentFromListStyleType(aState, aOriginatingElement, aPseudoStyle, aAddChild); } void nsCSSFrameConstructor::CreateGeneratedContentFromListStyleType( nsFrameConstructorState& aState, Element& aOriginatingElement, const ComputedStyle& aPseudoStyle, const FunctionRef aAddChild) { const nsStyleList* styleList = aPseudoStyle.StyleList(); CounterStyle* counterStyle = mPresShell->GetPresContext()->CounterStyleManager()->ResolveCounterStyle( styleList->mCounterStyle); bool needUseNode = false; switch (counterStyle->GetStyle()) { case ListStyle::None: return; case ListStyle::Disc: case ListStyle::Circle: case ListStyle::Square: case ListStyle::DisclosureClosed: case ListStyle::DisclosureOpen: break; default: const auto* anonStyle = counterStyle->AsAnonymous(); if (!anonStyle || !anonStyle->IsSingleString()) { needUseNode = true; } } auto node = MakeUnique(nsCounterUseNode::ForLegacyBullet, styleList->mCounterStyle); if (!needUseNode) { nsAutoString text; node->GetText(WritingMode(&aPseudoStyle), counterStyle, text); // Note that we're done with 'node' in this case. It's not inserted into // any list so it's deleted when we return. RefPtr child = CreateGenConTextNode(aState, text, nullptr); aAddChild(child); return; } auto* counterList = mContainStyleScopeManager.GetOrCreateCounterList( aOriginatingElement, nsGkAtoms::list_item); auto initializer = MakeUnique( std::move(node), counterList, &nsCSSFrameConstructor::CountersDirty); RefPtr child = CreateGenConTextNode(aState, EmptyString(), std::move(initializer)); aAddChild(child); } // Frames for these may not be leaves in the proper sense, but we still don't // want to expose generated content on them. For the purposes of the page they // should be leaves. static bool HasUAWidget(const Element& aOriginatingElement) { const ShadowRoot* sr = aOriginatingElement.GetShadowRoot(); return sr && sr->IsUAWidget(); } /* * aParentFrame - the frame that should be the parent of the generated * content. This is the frame for the corresponding content node, * which must not be a leaf frame. * * Any items created are added to aItems. * * We create an XML element (tag _moz_generated_content_before/after/marker) * representing the pseudoelement. We create a DOM node for each 'content' * item and make those nodes the children of the XML element. Then we create * a frame subtree for the XML element as if it were a regular child of * aParentFrame/aParentContent, giving the XML element the ::before, ::after * or ::marker style. */ void nsCSSFrameConstructor::CreateGeneratedContentItem( nsFrameConstructorState& aState, nsContainerFrame* aParentFrame, Element& aOriginatingElement, ComputedStyle& aStyle, PseudoStyleType aPseudoElement, FrameConstructionItemList& aItems, ItemFlags aExtraFlags) { MOZ_ASSERT(aPseudoElement == PseudoStyleType::before || aPseudoElement == PseudoStyleType::after || aPseudoElement == PseudoStyleType::marker, "unexpected aPseudoElement"); if (HasUAWidget(aOriginatingElement) && !aOriginatingElement.IsHTMLElement(nsGkAtoms::details)) { return; } ServoStyleSet* styleSet = mPresShell->StyleSet(); // Probe for the existence of the pseudo-element. // |ProbePseudoElementStyle| checks the relevant properties for the pseudo. // It only returns a non-null value if the pseudo should exist. RefPtr pseudoStyle = styleSet->ProbePseudoElementStyle( aOriginatingElement, aPseudoElement, &aStyle); if (!pseudoStyle) { return; } nsAtom* elemName = nullptr; nsAtom* property = nullptr; switch (aPseudoElement) { case PseudoStyleType::before: elemName = nsGkAtoms::mozgeneratedcontentbefore; property = nsGkAtoms::beforePseudoProperty; break; case PseudoStyleType::after: elemName = nsGkAtoms::mozgeneratedcontentafter; property = nsGkAtoms::afterPseudoProperty; break; case PseudoStyleType::marker: // We want to get a marker style even if we match no rules, but we still // want to check the result of GeneratedContentPseudoExists. elemName = nsGkAtoms::mozgeneratedcontentmarker; property = nsGkAtoms::markerPseudoProperty; break; default: MOZ_ASSERT_UNREACHABLE("unexpected aPseudoElement"); } RefPtr nodeInfo = mDocument->NodeInfoManager()->GetNodeInfo( elemName, nullptr, kNameSpaceID_None, nsINode::ELEMENT_NODE); RefPtr container; nsresult rv = NS_NewXMLElement(getter_AddRefs(container), nodeInfo.forget()); if (NS_FAILED(rv)) { return; } // Cleared when the pseudo is unbound from the tree, so no need to store a // strong reference, nor a destructor. aOriginatingElement.SetProperty(property, container.get()); container->SetIsNativeAnonymousRoot(); container->SetPseudoElementType(aPseudoElement); BindContext context(aOriginatingElement, BindContext::ForNativeAnonymous); rv = container->BindToTree(context, aOriginatingElement); if (NS_FAILED(rv)) { container->UnbindFromTree(); return; } // Servo has already eagerly computed the style for the container, so we can // just stick the style on the element and avoid an additional traversal. // // We don't do this for pseudos that may trigger animations or transitions, // since those need to be kicked off by the traversal machinery. // // Note that when a pseudo-element animates, we flag the originating element, // so we check that flag, but we could also a more expensive (but exhaustive) // check using EffectSet::GetEffectSet, for example. if (!Servo_ComputedValues_SpecifiesAnimationsOrTransitions(pseudoStyle) && !aOriginatingElement.MayHaveAnimations()) { Servo_SetExplicitStyle(container, pseudoStyle); } else { // If animations are involved, we avoid the SetExplicitStyle optimization // above. We need to grab style with animations from the pseudo element and // replace old one. mPresShell->StyleSet()->StyleNewSubtree(container); pseudoStyle = ServoStyleSet::ResolveServoStyle(*container); } auto AppendChild = [&container, this](nsIContent* aChild) { // We don't strictly have to set NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE // here; it would get set under AppendChildTo. But AppendChildTo might // think that we're going from not being anonymous to being anonymous and // do some extra work; setting the flag here avoids that. aChild->SetFlags(NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE); container->AppendChildTo(aChild, false, IgnoreErrors()); if (auto* childElement = Element::FromNode(aChild)) { // If we created any children elements, Servo needs to traverse them, but // the root is already set up. mPresShell->StyleSet()->StyleNewSubtree(childElement); } }; const uint32_t contentCount = pseudoStyle->StyleContent()->ContentCount(); for (uint32_t contentIndex = 0; contentIndex < contentCount; contentIndex++) { CreateGeneratedContent(aState, aOriginatingElement, *pseudoStyle, contentIndex, AppendChild); } // If a ::marker has no 'content' then generate it from its 'list-style-*'. if (contentCount == 0 && aPseudoElement == PseudoStyleType::marker) { CreateGeneratedContentFromListStyle(aState, aOriginatingElement, *pseudoStyle, AppendChild); } auto flags = ItemFlags{ItemFlag::IsGeneratedContent} + aExtraFlags; AddFrameConstructionItemsInternal(aState, container, aParentFrame, true, pseudoStyle, flags, aItems); } /**************************************************** ** BEGIN TABLE SECTION ****************************************************/ // The term pseudo frame is being used instead of anonymous frame, since // anonymous frame has been used elsewhere to refer to frames that have // generated content // Return whether the given frame is a table pseudo-frame. Note that // cell-content and table-outer frames have pseudo-types, but are always // created, even for non-anonymous cells and tables respectively. So for those // we have to examine the cell or table frame to see whether it's a pseudo // frame. In particular, a lone table caption will have a table wrapper as its // parent, but will also trigger construction of an empty inner table, which // will be the one we can examine to see whether the wrapper was a pseudo-frame. static bool IsTablePseudo(nsIFrame* aFrame) { auto pseudoType = aFrame->Style()->GetPseudoType(); return pseudoType != PseudoStyleType::NotPseudo && (pseudoType == PseudoStyleType::table || pseudoType == PseudoStyleType::inlineTable || pseudoType == PseudoStyleType::tableColGroup || pseudoType == PseudoStyleType::tableRowGroup || pseudoType == PseudoStyleType::tableRow || pseudoType == PseudoStyleType::tableCell || (pseudoType == PseudoStyleType::cellContent && aFrame->GetParent()->Style()->GetPseudoType() == PseudoStyleType::tableCell) || (pseudoType == PseudoStyleType::tableWrapper && (aFrame->PrincipalChildList() .FirstChild() ->Style() ->GetPseudoType() == PseudoStyleType::table || aFrame->PrincipalChildList() .FirstChild() ->Style() ->GetPseudoType() == PseudoStyleType::inlineTable))); } static bool IsRubyPseudo(nsIFrame* aFrame) { return RubyUtils::IsRubyPseudo(aFrame->Style()->GetPseudoType()); } static bool IsTableOrRubyPseudo(nsIFrame* aFrame) { return IsTablePseudo(aFrame) || IsRubyPseudo(aFrame); } /* static */ nsCSSFrameConstructor::ParentType nsCSSFrameConstructor::GetParentType( LayoutFrameType aFrameType) { if (aFrameType == LayoutFrameType::Table) { return eTypeTable; } if (aFrameType == LayoutFrameType::TableRowGroup) { return eTypeRowGroup; } if (aFrameType == LayoutFrameType::TableRow) { return eTypeRow; } if (aFrameType == LayoutFrameType::TableColGroup) { return eTypeColGroup; } if (aFrameType == LayoutFrameType::RubyBaseContainer) { return eTypeRubyBaseContainer; } if (aFrameType == LayoutFrameType::RubyTextContainer) { return eTypeRubyTextContainer; } if (aFrameType == LayoutFrameType::Ruby) { return eTypeRuby; } return eTypeBlock; } // Pull all the captions present in aItems out into aCaptions. static void PullOutCaptionFrames(nsFrameList& aList, nsFrameList& aCaptions) { nsIFrame* child = aList.FirstChild(); while (child) { nsIFrame* nextSibling = child->GetNextSibling(); if (child->StyleDisplay()->mDisplay == StyleDisplay::TableCaption) { aList.RemoveFrame(child); aCaptions.AppendFrame(nullptr, child); } child = nextSibling; } } // Construct the outer, inner table frames and the children frames for the // table. // XXX Page break frames for pseudo table frames are not constructed to avoid // the risk associated with revising the pseudo frame mechanism. The long term // solution of having frames handle page-break-before/after will solve the // problem. nsIFrame* nsCSSFrameConstructor::ConstructTable(nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay, nsFrameList& aFrameList) { MOZ_ASSERT(aDisplay->mDisplay == StyleDisplay::Table || aDisplay->mDisplay == StyleDisplay::InlineTable, "Unexpected call"); nsIContent* const content = aItem.mContent; ComputedStyle* const computedStyle = aItem.mComputedStyle; const bool isMathMLContent = content->IsMathMLElement(); // create the pseudo SC for the table wrapper as a child of the inner SC RefPtr outerComputedStyle = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle( PseudoStyleType::tableWrapper, computedStyle); // Create the table wrapper frame which holds the caption and inner table // frame nsContainerFrame* newFrame; if (isMathMLContent) newFrame = NS_NewMathMLmtableOuterFrame(mPresShell, outerComputedStyle); else newFrame = NS_NewTableWrapperFrame(mPresShell, outerComputedStyle); nsContainerFrame* geometricParent = aState.GetGeometricParent( *outerComputedStyle->StyleDisplay(), aParentFrame); // Init the table wrapper frame InitAndRestoreFrame(aState, content, geometricParent, newFrame); // Create the inner table frame nsContainerFrame* innerFrame; if (isMathMLContent) innerFrame = NS_NewMathMLmtableFrame(mPresShell, computedStyle); else innerFrame = NS_NewTableFrame(mPresShell, computedStyle); InitAndRestoreFrame(aState, content, newFrame, innerFrame); innerFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); // Put the newly created frames into the right child list SetInitialSingleChild(newFrame, innerFrame); aState.AddChild(newFrame, aFrameList, content, aParentFrame); if (!mRootElementFrame) { // The frame we're constructing will be the root element frame. SetRootElementFrameAndConstructCanvasAnonContent(newFrame, aState, aFrameList); } nsFrameList childList; // Process children nsFrameConstructorSaveState absoluteSaveState; const nsStyleDisplay* display = outerComputedStyle->StyleDisplay(); // Mark the table frame as an absolute container if needed newFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN); if (display->IsAbsPosContainingBlock(newFrame)) { aState.PushAbsoluteContainingBlock(newFrame, newFrame, absoluteSaveState); } nsFrameConstructorSaveState floatSaveState; aState.MaybePushFloatContainingBlock(innerFrame, floatSaveState); if (aItem.mFCData->mBits & FCDATA_USE_CHILD_ITEMS) { ConstructFramesFromItemList( aState, aItem.mChildItems, innerFrame, aItem.mFCData->mBits & FCDATA_IS_WRAPPER_ANON_BOX, childList); } else { ProcessChildren(aState, content, computedStyle, innerFrame, true, childList, false); } nsFrameList captionList; PullOutCaptionFrames(childList, captionList); // Set the inner table frame's initial primary list innerFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); // Set the table wrapper frame's secondary childlist lists if (captionList.NotEmpty()) { captionList.ApplySetParent(newFrame); newFrame->SetInitialChildList(FrameChildListID::Caption, std::move(captionList)); } return newFrame; } static void MakeTablePartAbsoluteContainingBlockIfNeeded( nsFrameConstructorState& aState, const nsStyleDisplay* aDisplay, nsFrameConstructorSaveState& aAbsSaveState, nsContainerFrame* aFrame) { // If we're positioned, then we need to become an absolute containing block // for any absolutely positioned children and register for post-reflow fixup. // // Note that usually if a frame type can be an absolute containing block, we // always set NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN, whether it actually is or // not. However, in this case flag serves the additional purpose of indicating // that the frame was registered with its table frame. This allows us to avoid // the overhead of unregistering the frame in most cases. if (aDisplay->IsAbsPosContainingBlock(aFrame)) { aFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN); aState.PushAbsoluteContainingBlock(aFrame, aFrame, aAbsSaveState); nsTableFrame::RegisterPositionedTablePart(aFrame); } } nsIFrame* nsCSSFrameConstructor::ConstructTableRowOrRowGroup( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay, nsFrameList& aFrameList) { MOZ_ASSERT(aDisplay->mDisplay == StyleDisplay::TableRow || aDisplay->mDisplay == StyleDisplay::TableRowGroup || aDisplay->mDisplay == StyleDisplay::TableFooterGroup || aDisplay->mDisplay == StyleDisplay::TableHeaderGroup, "Not a row or row group"); MOZ_ASSERT(aItem.mComputedStyle->StyleDisplay() == aDisplay, "Display style doesn't match style"); nsIContent* const content = aItem.mContent; ComputedStyle* const computedStyle = aItem.mComputedStyle; nsContainerFrame* newFrame; if (aDisplay->mDisplay == StyleDisplay::TableRow) { if (content->IsMathMLElement()) newFrame = NS_NewMathMLmtrFrame(mPresShell, computedStyle); else newFrame = NS_NewTableRowFrame(mPresShell, computedStyle); } else { newFrame = NS_NewTableRowGroupFrame(mPresShell, computedStyle); } InitAndRestoreFrame(aState, content, aParentFrame, newFrame); nsFrameConstructorSaveState absoluteSaveState; MakeTablePartAbsoluteContainingBlockIfNeeded(aState, aDisplay, absoluteSaveState, newFrame); nsFrameConstructorSaveState floatSaveState; aState.MaybePushFloatContainingBlock(newFrame, floatSaveState); nsFrameList childList; if (aItem.mFCData->mBits & FCDATA_USE_CHILD_ITEMS) { ConstructFramesFromItemList( aState, aItem.mChildItems, newFrame, aItem.mFCData->mBits & FCDATA_IS_WRAPPER_ANON_BOX, childList); } else { ProcessChildren(aState, content, computedStyle, newFrame, true, childList, false); } newFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); aFrameList.AppendFrame(nullptr, newFrame); return newFrame; } nsIFrame* nsCSSFrameConstructor::ConstructTableCol( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aStyleDisplay, nsFrameList& aFrameList) { nsIContent* const content = aItem.mContent; ComputedStyle* const computedStyle = aItem.mComputedStyle; nsTableColFrame* colFrame = NS_NewTableColFrame(mPresShell, computedStyle); InitAndRestoreFrame(aState, content, aParentFrame, colFrame); NS_ASSERTION(colFrame->Style() == computedStyle, "Unexpected style"); aFrameList.AppendFrame(nullptr, colFrame); // construct additional col frames if the col frame has a span > 1 int32_t span = colFrame->GetSpan(); for (int32_t spanX = 1; spanX < span; spanX++) { nsTableColFrame* newCol = NS_NewTableColFrame(mPresShell, computedStyle); InitAndRestoreFrame(aState, content, aParentFrame, newCol, false); aFrameList.LastChild()->SetNextContinuation(newCol); newCol->SetPrevContinuation(aFrameList.LastChild()); aFrameList.AppendFrame(nullptr, newCol); newCol->SetColType(eColAnonymousCol); } return colFrame; } nsIFrame* nsCSSFrameConstructor::ConstructTableCell( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay, nsFrameList& aFrameList) { MOZ_ASSERT(aDisplay->mDisplay == StyleDisplay::TableCell, "Unexpected call"); nsIContent* const content = aItem.mContent; ComputedStyle* const computedStyle = aItem.mComputedStyle; const bool isMathMLContent = content->IsMathMLElement(); nsTableFrame* tableFrame = static_cast(aParentFrame)->GetTableFrame(); nsContainerFrame* newFrame; // is border separate in mathml.css and the MathML code doesn't // implement border collapse. For those users who style with border // collapse, give them the default non-MathML table frames that understand // border collapse. This won't break us because MathML table frames are all // subclasses of the default table code, and so we can freely mix // with or , or . What will happen is just that non-MathML // frames won't understand MathML attributes and will therefore miss the // special handling that the MathML code does. if (isMathMLContent && !tableFrame->IsBorderCollapse()) { newFrame = NS_NewMathMLmtdFrame(mPresShell, computedStyle, tableFrame); } else { // Warning: If you change this and add a wrapper frame around table cell // frames, make sure Bug 368554 doesn't regress! // See IsInAutoWidthTableCellForQuirk() in nsImageFrame.cpp. newFrame = NS_NewTableCellFrame(mPresShell, computedStyle, tableFrame); } // Initialize the table cell frame InitAndRestoreFrame(aState, content, aParentFrame, newFrame); newFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); // Resolve pseudo style and initialize the body cell frame RefPtr innerPseudoStyle = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle( PseudoStyleType::cellContent, computedStyle); // Create a block frame that will format the cell's content nsContainerFrame* cellInnerFrame; if (isMathMLContent) { cellInnerFrame = NS_NewMathMLmtdInnerFrame(mPresShell, innerPseudoStyle); } else { cellInnerFrame = NS_NewBlockFormattingContext(mPresShell, innerPseudoStyle); } InitAndRestoreFrame(aState, content, newFrame, cellInnerFrame); nsFrameConstructorSaveState absoluteSaveState; MakeTablePartAbsoluteContainingBlockIfNeeded(aState, aDisplay, absoluteSaveState, newFrame); nsFrameConstructorSaveState floatSaveState; aState.MaybePushFloatContainingBlock(cellInnerFrame, floatSaveState); nsFrameList childList; if (aItem.mFCData->mBits & FCDATA_USE_CHILD_ITEMS) { AutoFrameConstructionPageName pageNameTracker(aState, cellInnerFrame); ConstructFramesFromItemList( aState, aItem.mChildItems, cellInnerFrame, aItem.mFCData->mBits & FCDATA_IS_WRAPPER_ANON_BOX, childList); } else { // Process the child content ProcessChildren(aState, content, computedStyle, cellInnerFrame, true, childList, !isMathMLContent); } cellInnerFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); SetInitialSingleChild(newFrame, cellInnerFrame); aFrameList.AppendFrame(nullptr, newFrame); return newFrame; } static inline bool NeedFrameFor(const nsFrameConstructorState& aState, nsContainerFrame* aParentFrame, nsIContent* aChildContent) { // XXX the GetContent() != aChildContent check is needed due to bug 135040. // Remove it once that's fixed. MOZ_ASSERT( !aChildContent->GetPrimaryFrame() || aState.mCreatingExtraFrames || aChildContent->GetPrimaryFrame()->GetContent() != aChildContent, "Why did we get called?"); // don't create a whitespace frame if aParentFrame doesn't want it. // always create frames for children in generated content. counter(), // quotes, and attr() content can easily change dynamically and we don't // want to be reconstructing frames. It's not even clear that these // should be considered ignorable just because they evaluate to // whitespace. // We could handle all this in CreateNeededPseudoContainers or some other // place after we build our frame construction items, but that would involve // creating frame construction items for whitespace kids that ignores // white-space, where we know we'll be dropping them all anyway, and involve // an extra walk down the frame construction item list. auto excludesIgnorableWhitespace = [](nsIFrame* aParentFrame) { return aParentFrame->IsFrameOfType(nsIFrame::eXULBox) || aParentFrame->IsFrameOfType(nsIFrame::eMathML); }; if (!aParentFrame || !excludesIgnorableWhitespace(aParentFrame) || aParentFrame->IsGeneratedContentFrame() || !aChildContent->IsText()) { return true; } aChildContent->SetFlags(NS_CREATE_FRAME_IF_NON_WHITESPACE | NS_REFRAME_IF_WHITESPACE); return !aChildContent->TextIsOnlyWhitespace(); } /*********************************************** * END TABLE SECTION ***********************************************/ void nsCSSFrameConstructor::SetRootElementFrameAndConstructCanvasAnonContent( nsContainerFrame* aRootElementFrame, nsFrameConstructorState& aState, nsFrameList& aFrameList) { MOZ_DIAGNOSTIC_ASSERT(!mRootElementFrame); mRootElementFrame = aRootElementFrame; if (mDocElementContainingBlock->IsCanvasFrame()) { // NOTE(emilio): This is in the reverse order compared to normal anonymous // children. We usually generate anonymous kids first, then non-anonymous, // but we generate the doc element frame the other way around. This is fine // either way, but generating anonymous children in a different order // requires changing nsCanvasFrame (and a whole lot of other potentially // unknown code) to look at the last child to find the root frame rather // than the first child. ConstructAnonymousContentForCanvas(aState, mDocElementContainingBlock, aRootElementFrame->GetContent(), aFrameList); } } nsIFrame* nsCSSFrameConstructor::ConstructDocElementFrame( Element* aDocElement) { MOZ_ASSERT(GetRootFrame(), "No viewport? Someone forgot to call ConstructRootFrame!"); MOZ_ASSERT(!mDocElementContainingBlock, "Shouldn't have a doc element containing block here"); // Resolve a new style for the viewport since it may be affected by a new root // element style (e.g. a propagated 'direction'). // // @see ComputedStyle::ApplyStyleFixups { RefPtr sc = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle( PseudoStyleType::viewport, nullptr); GetRootFrame()->SetComputedStyleWithoutNotification(sc); } // Ensure the document element is styled at this point. if (!aDocElement->HasServoData()) { mPresShell->StyleSet()->StyleNewSubtree(aDocElement); } // Make sure to call UpdateViewportScrollStylesOverride before // SetUpDocElementContainingBlock, since it sets up our scrollbar state // properly. DebugOnly propagatedScrollFrom; if (nsPresContext* presContext = mPresShell->GetPresContext()) { propagatedScrollFrom = presContext->UpdateViewportScrollStylesOverride(); } SetUpDocElementContainingBlock(aDocElement); // This has the side-effect of getting `mFrameTreeState` from our docshell. // // FIXME(emilio): There may be a more sensible time to do this. if (!mFrameTreeState) { mPresShell->CaptureHistoryState(getter_AddRefs(mFrameTreeState)); } NS_ASSERTION(mDocElementContainingBlock, "Should have parent by now"); nsFrameConstructorState state( mPresShell, GetAbsoluteContainingBlock(mDocElementContainingBlock, FIXED_POS), nullptr, nullptr, do_AddRef(mFrameTreeState)); RefPtr computedStyle = ServoStyleSet::ResolveServoStyle(*aDocElement); const nsStyleDisplay* display = computedStyle->StyleDisplay(); // --------- IF SCROLLABLE WRAP IN SCROLLFRAME -------- NS_ASSERTION(!display->IsScrollableOverflow() || state.mPresContext->IsPaginated() || propagatedScrollFrom == aDocElement, "Scrollbars should have been propagated to the viewport"); if (MOZ_UNLIKELY(display->mDisplay == StyleDisplay::None)) { return nullptr; } if (mDocElementContainingBlock->IsCanvasFrame()) { // This implements "The Principal Writing Mode". // https://drafts.csswg.org/css-writing-modes-3/#principal-flow // // If there's a element in an HTML document, its writing-mode, // direction, and text-orientation override the root element's used value. // // We need to copy 's WritingMode to mDocElementContainingBlock before // construct mRootElementFrame so that anonymous internal frames such as // with table style can copy their parent frame's mWritingMode in // nsIFrame::Init(). MOZ_ASSERT(!mRootElementFrame, "We need to copy 's principal writing-mode before " "constructing mRootElementFrame."); const WritingMode propagatedWM = [&] { const WritingMode rootWM(computedStyle); if (computedStyle->StyleDisplay()->IsContainAny()) { return rootWM; } Element* body = mDocument->GetBodyElement(); if (!body) { return rootWM; } RefPtr bodyStyle = ResolveComputedStyle(body); if (bodyStyle->StyleDisplay()->IsContainAny()) { return rootWM; } const WritingMode bodyWM(bodyStyle); if (bodyWM != rootWM) { nsContentUtils::ReportToConsole( nsIScriptError::warningFlag, "Layout"_ns, mDocument, nsContentUtils::eLAYOUT_PROPERTIES, "PrincipalWritingModePropagationWarning"); } return bodyWM; }(); mDocElementContainingBlock->PropagateWritingModeToSelfAndAncestors( propagatedWM); } nsFrameConstructorSaveState docElementContainingBlockAbsoluteSaveState; // Push the absolute containing block now so we can absolutely position // the root element mDocElementContainingBlock->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN); state.PushAbsoluteContainingBlock(mDocElementContainingBlock, mDocElementContainingBlock, docElementContainingBlockAbsoluteSaveState); // The rules from CSS 2.1, section 9.2.4, have already been applied // by the style system, so we can assume that display->mDisplay is // either NONE, BLOCK, or TABLE. // contentFrame is the primary frame for the root element. frameList contains // the children of the initial containing block. // // The first of those frames is usually `contentFrame`, but it can be // different, in particular if the root frame is positioned, in which case // contentFrame is the out-of-flow frame and frameList.FirstChild() is the // placeholder. // // The rest of the frames in frameList are the anonymous content of the canvas // frame. nsContainerFrame* contentFrame; nsFrameList frameList; bool processChildren = false; nsFrameConstructorSaveState absoluteSaveState; if (aDocElement->IsSVGElement()) { if (!aDocElement->IsSVGElement(nsGkAtoms::svg)) { return nullptr; } // We're going to call the right function ourselves, so no need to give a // function to this FrameConstructionData. // XXXbz on the other hand, if we converted this whole function to // FrameConstructionData/Item, then we'd need the right function // here... but would probably be able to get away with less code in this // function in general. static constexpr FrameConstructionData rootSVGData; AutoFrameConstructionItem item(this, &rootSVGData, aDocElement, do_AddRef(computedStyle), true); contentFrame = static_cast(ConstructOuterSVG( state, item, mDocElementContainingBlock, display, frameList)); } else if (display->mDisplay == StyleDisplay::Flex || display->mDisplay == StyleDisplay::WebkitBox || display->mDisplay == StyleDisplay::Grid || display->mDisplay == StyleDisplay::MozBox) { auto func = [&] { if (display->mDisplay == StyleDisplay::Grid) { return NS_NewGridContainerFrame; } if (display->mDisplay == StyleDisplay::MozBox && !computedStyle->StyleVisibility()->EmulateMozBoxWithFlex()) { return NS_NewBoxFrame; } return NS_NewFlexContainerFrame; }(); contentFrame = func(mPresShell, computedStyle); InitAndRestoreFrame( state, aDocElement, state.GetGeometricParent(*display, mDocElementContainingBlock), contentFrame); state.AddChild(contentFrame, frameList, aDocElement, mDocElementContainingBlock); processChildren = true; contentFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN); if (display->IsAbsPosContainingBlock(contentFrame)) { state.PushAbsoluteContainingBlock(contentFrame, contentFrame, absoluteSaveState); } } else if (display->mDisplay == StyleDisplay::Table) { // We're going to call the right function ourselves, so no need to give a // function to this FrameConstructionData. // XXXbz on the other hand, if we converted this whole function to // FrameConstructionData/Item, then we'd need the right function // here... but would probably be able to get away with less code in this // function in general. static constexpr FrameConstructionData rootTableData; AutoFrameConstructionItem item(this, &rootTableData, aDocElement, do_AddRef(computedStyle), true); // if the document is a table then just populate it. contentFrame = static_cast(ConstructTable( state, item, mDocElementContainingBlock, display, frameList)); } else if (display->DisplayInside() == StyleDisplayInside::Ruby) { static constexpr FrameConstructionData data( &nsCSSFrameConstructor::ConstructBlockRubyFrame); AutoFrameConstructionItem item(this, &data, aDocElement, do_AddRef(computedStyle), true); contentFrame = static_cast(ConstructBlockRubyFrame( state, item, state.GetGeometricParent(*display, mDocElementContainingBlock), display, frameList)); } else { MOZ_ASSERT(display->mDisplay == StyleDisplay::Block || display->mDisplay == StyleDisplay::FlowRoot, "Unhandled display type for root element"); contentFrame = NS_NewBlockFormattingContext(mPresShell, computedStyle); ConstructBlock( state, aDocElement, state.GetGeometricParent(*display, mDocElementContainingBlock), mDocElementContainingBlock, computedStyle, &contentFrame, frameList, display->IsAbsPosContainingBlock(contentFrame) ? contentFrame : nullptr); } MOZ_ASSERT(frameList.FirstChild()); MOZ_ASSERT(frameList.FirstChild()->GetContent() == aDocElement); MOZ_ASSERT(contentFrame); MOZ_ASSERT( processChildren ? !mRootElementFrame : mRootElementFrame == contentFrame, "unexpected mRootElementFrame"); if (processChildren) { SetRootElementFrameAndConstructCanvasAnonContent(contentFrame, state, frameList); } // Figure out which frame has the main style for the document element, // assigning it to mRootElementStyleFrame. // Backgrounds should be propagated from that frame to the viewport. contentFrame->GetParentComputedStyle(&mRootElementStyleFrame); bool isChild = mRootElementStyleFrame && mRootElementStyleFrame->GetParent() == contentFrame; if (!isChild) { mRootElementStyleFrame = mRootElementFrame; } if (processChildren) { // Still need to process the child content nsFrameList childList; NS_ASSERTION(!contentFrame->IsBlockFrameOrSubclass() && !contentFrame->IsFrameOfType(nsIFrame::eSVG), "Only XUL frames should reach here"); nsFrameConstructorSaveState floatSaveState; state.MaybePushFloatContainingBlock(contentFrame, floatSaveState); ProcessChildren(state, aDocElement, computedStyle, contentFrame, true, childList, false); // Set the initial child lists contentFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); } nsIFrame* newFrame = frameList.FirstChild(); // set the primary frame aDocElement->SetPrimaryFrame(contentFrame); mDocElementContainingBlock->AppendFrames(FrameChildListID::Principal, std::move(frameList)); return newFrame; } nsIFrame* nsCSSFrameConstructor::ConstructRootFrame() { AUTO_PROFILER_LABEL("nsCSSFrameConstructor::ConstructRootFrame", LAYOUT_FrameConstruction); AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC); ServoStyleSet* styleSet = mPresShell->StyleSet(); // --------- BUILD VIEWPORT ----------- RefPtr viewportPseudoStyle = styleSet->ResolveInheritingAnonymousBoxStyle(PseudoStyleType::viewport, nullptr); ViewportFrame* viewportFrame = NS_NewViewportFrame(mPresShell, viewportPseudoStyle); // XXXbz do we _have_ to pass a null content pointer to that frame? // Would it really kill us to pass in the root element or something? // What would that break? viewportFrame->Init(nullptr, nullptr, nullptr); viewportFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); // Bind the viewport frame to the root view nsView* rootView = mPresShell->GetViewManager()->GetRootView(); viewportFrame->SetView(rootView); viewportFrame->SyncFrameViewProperties(rootView); nsContainerFrame::SyncWindowProperties(mPresShell->GetPresContext(), viewportFrame, rootView, nullptr, nsContainerFrame::SET_ASYNC); // Make it an absolute container for fixed-pos elements viewportFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN); viewportFrame->MarkAsAbsoluteContainingBlock(); return viewportFrame; } void nsCSSFrameConstructor::SetUpDocElementContainingBlock( nsIContent* aDocElement) { MOZ_ASSERT(aDocElement, "No element?"); MOZ_ASSERT(!aDocElement->GetParent(), "Not root content?"); MOZ_ASSERT(aDocElement->GetUncomposedDoc(), "Not in a document?"); MOZ_ASSERT(aDocElement->GetUncomposedDoc()->GetRootElement() == aDocElement, "Not the root of the document?"); /* how the root frame hierarchy should look Galley presentation, with scrolling: ViewportFrame [fixed-cb] nsHTMLScrollFrame (if needed) nsCanvasFrame [abs-cb] root element frame (nsBlockFrame, SVGOuterSVGFrame, nsTableWrapperFrame, nsPlaceholderFrame, nsBoxFrame) Print presentation, non-XUL ViewportFrame nsCanvasFrame nsPageSequenceFrame PrintedSheetFrame nsPageFrame nsPageContentFrame [fixed-cb] nsCanvasFrame [abs-cb] root element frame (nsBlockFrame, SVGOuterSVGFrame, nsTableWrapperFrame, nsPlaceholderFrame) Print-preview presentation, non-XUL ViewportFrame nsHTMLScrollFrame nsCanvasFrame nsPageSequenceFrame PrintedSheetFrame nsPageFrame nsPageContentFrame [fixed-cb] nsCanvasFrame [abs-cb] root element frame (nsBlockFrame, SVGOuterSVGFrame, nsTableWrapperFrame, nsPlaceholderFrame) Print/print preview of XUL is not supported. [fixed-cb]: the default containing block for fixed-pos content [abs-cb]: the default containing block for abs-pos content Meaning of nsCSSFrameConstructor fields: mRootElementFrame is "root element frame". This is the primary frame for the root element. mDocElementContainingBlock is the parent of mRootElementFrame (i.e. nsCanvasFrame) mPageSequenceFrame is the nsPageSequenceFrame, or null if there isn't one */ // --------- CREATE ROOT FRAME ------- // Create the root frame. The document element's frame is a child of the // root frame. // // The root frame serves two purposes: // - reserves space for any margins needed for the document element's frame // - renders the document element's background. This ensures the background // covers the entire canvas as specified by the CSS2 spec nsPresContext* presContext = mPresShell->GetPresContext(); const bool isPaginated = presContext->IsRootPaginatedDocument(); const bool isHTML = aDocElement->IsHTMLElement(); const bool isXUL = !isHTML && aDocElement->IsXULElement(); const bool isScrollable = [&] { if (isPaginated) { return presContext->HasPaginatedScrolling(); } // Never create scrollbars for XUL documents or top level XHTML documents // that disable scrolling. if (isXUL) { return false; } if (aDocElement->OwnerDoc()->IsDocumentURISchemeChrome() && aDocElement->AsElement()->AttrValueIs( kNameSpaceID_None, nsGkAtoms::scrolling, nsGkAtoms::_false, eCaseMatters)) { return false; } return true; }(); nsContainerFrame* viewportFrame = static_cast(GetRootFrame()); ComputedStyle* viewportPseudoStyle = viewportFrame->Style(); nsContainerFrame* rootFrame = NS_NewCanvasFrame(mPresShell, viewportPseudoStyle); PseudoStyleType rootPseudo = PseudoStyleType::canvas; mDocElementContainingBlock = rootFrame; // --------- IF SCROLLABLE WRAP IN SCROLLFRAME -------- // If the device supports scrolling (e.g., in galley mode on the screen and // for print-preview, but not when printing), then create a scroll frame that // will act as the scrolling mechanism for the viewport. // XXX Do we even need a viewport when printing to a printer? // We no longer need to do overflow propagation here. It's taken care of // when we construct frames for the element whose overflow might be // propagated NS_ASSERTION(!isScrollable || !isXUL, "XUL documents should never be scrollable - see above"); nsContainerFrame* newFrame = rootFrame; RefPtr rootPseudoStyle; // we must create a state because if the scrollbars are GFX it needs the // state to build the scrollbar frames. nsFrameConstructorState state(mPresShell, nullptr, nullptr, nullptr); // Start off with the viewport as parent; we'll adjust it as needed. nsContainerFrame* parentFrame = viewportFrame; ServoStyleSet* styleSet = mPresShell->StyleSet(); // If paginated, make sure we don't put scrollbars in if (!isScrollable) { rootPseudoStyle = styleSet->ResolveInheritingAnonymousBoxStyle( rootPseudo, viewportPseudoStyle); } else { rootPseudo = PseudoStyleType::scrolledCanvas; // Build the frame. We give it the content we are wrapping which is the // document element, the root frame, the parent view port frame, and we // should get back the new frame and the scrollable view if one was // created. // resolve a context for the scrollframe RefPtr computedStyle = styleSet->ResolveInheritingAnonymousBoxStyle( PseudoStyleType::viewportScroll, viewportPseudoStyle); // Note that the viewport scrollframe is always built with // overflow:auto style. This forces the scroll frame to create // anonymous content for both scrollbars. This is necessary even // if the HTML or BODY elements are overriding the viewport // scroll style to 'hidden' --- dynamic style changes might put // scrollbars back on the viewport and we don't want to have to // reframe the viewport to create the scrollbar content. newFrame = nullptr; rootPseudoStyle = BeginBuildingScrollFrame(state, aDocElement, computedStyle, viewportFrame, rootPseudo, true, newFrame); parentFrame = newFrame; } rootFrame->SetComputedStyleWithoutNotification(rootPseudoStyle); rootFrame->Init(aDocElement, parentFrame, nullptr); if (isScrollable) { FinishBuildingScrollFrame(parentFrame, rootFrame); } if (isPaginated) { // Create a page sequence frame { RefPtr pageSequenceStyle = styleSet->ResolveInheritingAnonymousBoxStyle( PseudoStyleType::pageSequence, viewportPseudoStyle); mPageSequenceFrame = NS_NewPageSequenceFrame(mPresShell, pageSequenceStyle); mPageSequenceFrame->Init(aDocElement, rootFrame, nullptr); SetInitialSingleChild(rootFrame, mPageSequenceFrame); } // Create the first printed sheet frame, as the sole child (for now) of our // page sequence frame (mPageSequenceFrame). auto* printedSheetFrame = ConstructPrintedSheetFrame(mPresShell, mPageSequenceFrame, nullptr); SetInitialSingleChild(mPageSequenceFrame, printedSheetFrame); MOZ_ASSERT(!mNextPageContentFramePageName, "Next page name should not have been set."); // Create the first page, as the sole child (for now) of the printed sheet // frame that we just created. nsContainerFrame* canvasFrame; nsContainerFrame* pageFrame = ConstructPageFrame(mPresShell, printedSheetFrame, nullptr, canvasFrame); pageFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); SetInitialSingleChild(printedSheetFrame, pageFrame); // The eventual parent of the document element frame. // XXX should this be set for every new page (in ConstructPageFrame)? mDocElementContainingBlock = canvasFrame; } if (viewportFrame->HasAnyStateBits(NS_FRAME_FIRST_REFLOW)) { SetInitialSingleChild(viewportFrame, newFrame); } else { viewportFrame->AppendFrames(FrameChildListID::Principal, nsFrameList(newFrame, newFrame)); } } void nsCSSFrameConstructor::ConstructAnonymousContentForCanvas( nsFrameConstructorState& aState, nsContainerFrame* aFrame, nsIContent* aDocElement, nsFrameList& aFrameList) { NS_ASSERTION(aFrame->IsCanvasFrame(), "aFrame should be canvas frame!"); MOZ_ASSERT(mRootElementFrame->GetContent() == aDocElement); AutoTArray anonymousItems; GetAnonymousContent(aDocElement, aFrame, anonymousItems); if (anonymousItems.IsEmpty()) { return; } AutoFrameConstructionItemList itemsToConstruct(this); AutoFrameConstructionPageName pageNameTracker(aState, aFrame); AddFCItemsForAnonymousContent(aState, aFrame, anonymousItems, itemsToConstruct, pageNameTracker); ConstructFramesFromItemList(aState, itemsToConstruct, aFrame, /* aParentIsWrapperAnonBox = */ false, aFrameList); } PrintedSheetFrame* nsCSSFrameConstructor::ConstructPrintedSheetFrame( PresShell* aPresShell, nsContainerFrame* aParentFrame, nsIFrame* aPrevSheetFrame) { RefPtr printedSheetPseudoStyle = aPresShell->StyleSet()->ResolveNonInheritingAnonymousBoxStyle( PseudoStyleType::printedSheet); auto* printedSheetFrame = NS_NewPrintedSheetFrame(aPresShell, printedSheetPseudoStyle); printedSheetFrame->Init(nullptr, aParentFrame, aPrevSheetFrame); return printedSheetFrame; } nsContainerFrame* nsCSSFrameConstructor::ConstructPageFrame( PresShell* aPresShell, nsContainerFrame* aParentFrame, nsIFrame* aPrevPageFrame, nsContainerFrame*& aCanvasFrame) { ServoStyleSet* styleSet = aPresShell->StyleSet(); RefPtr pagePseudoStyle = styleSet->ResolveNonInheritingAnonymousBoxStyle(PseudoStyleType::page); nsContainerFrame* pageFrame = NS_NewPageFrame(aPresShell, pagePseudoStyle); // Initialize the page frame and force it to have a view. This makes printing // of the pages easier and faster. pageFrame->Init(nullptr, aParentFrame, aPrevPageFrame); RefPtr pageName; if (mNextPageContentFramePageName) { pageName = mNextPageContentFramePageName.forget(); } else if (aPrevPageFrame) { pageName = aPrevPageFrame->ComputePageValue(); MOZ_ASSERT(pageName, "Page name from prev-in-flow should not have been null"); } RefPtr pageContentPseudoStyle = styleSet->ResolvePageContentStyle(pageName); nsContainerFrame* pageContentFrame = NS_NewPageContentFrame( aPresShell, pageContentPseudoStyle, pageName.forget()); // Initialize the page content frame and force it to have a view. Also make it // the containing block for fixed elements which are repeated on every page. nsIFrame* prevPageContentFrame = nullptr; if (aPrevPageFrame) { prevPageContentFrame = aPrevPageFrame->PrincipalChildList().FirstChild(); NS_ASSERTION(prevPageContentFrame, "missing page content frame"); } pageContentFrame->Init(nullptr, pageFrame, prevPageContentFrame); if (!prevPageContentFrame) { // The canvas is an inheriting anon box, so needs to be "owned" by the page // content. pageContentFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); } SetInitialSingleChild(pageFrame, pageContentFrame); // Make it an absolute container for fixed-pos elements pageContentFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN); pageContentFrame->MarkAsAbsoluteContainingBlock(); RefPtr canvasPseudoStyle = styleSet->ResolveInheritingAnonymousBoxStyle(PseudoStyleType::canvas, pageContentPseudoStyle); aCanvasFrame = NS_NewCanvasFrame(aPresShell, canvasPseudoStyle); nsIFrame* prevCanvasFrame = nullptr; if (prevPageContentFrame) { prevCanvasFrame = prevPageContentFrame->PrincipalChildList().FirstChild(); NS_ASSERTION(prevCanvasFrame, "missing canvas frame"); } aCanvasFrame->Init(nullptr, pageContentFrame, prevCanvasFrame); SetInitialSingleChild(pageContentFrame, aCanvasFrame); return pageFrame; } /* static */ nsIFrame* nsCSSFrameConstructor::CreatePlaceholderFrameFor( PresShell* aPresShell, nsIContent* aContent, nsIFrame* aFrame, nsContainerFrame* aParentFrame, nsIFrame* aPrevInFlow, nsFrameState aTypeBit) { RefPtr placeholderStyle = aPresShell->StyleSet()->ResolveStyleForPlaceholder(); // The placeholder frame gets a pseudo style. nsPlaceholderFrame* placeholderFrame = NS_NewPlaceholderFrame(aPresShell, placeholderStyle, aTypeBit); placeholderFrame->Init(aContent, aParentFrame, aPrevInFlow); // Associate the placeholder/out-of-flow with each other. placeholderFrame->SetOutOfFlowFrame(aFrame); aFrame->SetProperty(nsIFrame::PlaceholderFrameProperty(), placeholderFrame); aFrame->AddStateBits(NS_FRAME_OUT_OF_FLOW); return placeholderFrame; } // Clears any lazy bits set in the range [aStartContent, aEndContent). If // aEndContent is null, that means to clear bits in all siblings starting with // aStartContent. aStartContent must not be null unless aEndContent is also // null. We do this so that when new children are inserted under elements whose // frame is a leaf the new children don't cause us to try to construct frames // for the existing children again. static inline void ClearLazyBits(nsIContent* aStartContent, nsIContent* aEndContent) { MOZ_ASSERT(aStartContent || !aEndContent, "Must have start child if we have an end child"); for (nsIContent* cur = aStartContent; cur != aEndContent; cur = cur->GetNextSibling()) { cur->UnsetFlags(NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME); } } nsIFrame* nsCSSFrameConstructor::ConstructSelectFrame( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aStyleDisplay, nsFrameList& aFrameList) { nsIContent* const content = aItem.mContent; ComputedStyle* const computedStyle = aItem.mComputedStyle; // Construct a frame-based listbox or combobox dom::HTMLSelectElement* sel = dom::HTMLSelectElement::FromNode(content); MOZ_ASSERT(sel); if (sel->IsCombobox()) { // Construct a frame-based combo box. // The frame-based combo box is built out of three parts. A display area, a // button and a dropdown list. The display area and button are created // through anonymous content. The drop-down list's frame is created // explicitly. The combobox frame shares its content with the drop-down // list. nsFrameState flags = NS_BLOCK_FLOAT_MGR; nsComboboxControlFrame* comboboxFrame = NS_NewComboboxControlFrame(mPresShell, computedStyle, flags); // Save the history state so we don't restore during construction // since the complete tree is required before we restore. nsILayoutHistoryState* historyState = aState.mFrameState; aState.mFrameState = nullptr; // Initialize the combobox frame InitAndRestoreFrame(aState, content, aState.GetGeometricParent(*aStyleDisplay, aParentFrame), comboboxFrame); comboboxFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); aState.AddChild(comboboxFrame, aFrameList, content, aParentFrame); // Resolve pseudo element style for the dropdown list RefPtr listStyle = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle( PseudoStyleType::dropDownList, computedStyle); // child frames of combobox frame nsFrameList childList; // Create display and button frames from the combobox's anonymous content. // The anonymous content is appended to existing anonymous content for this // element (the scrollbars). // // nsComboboxControlFrame needs special frame creation behavior for its // first piece of anonymous content, which means that we can't take the // normal ProcessChildren path. AutoTArray newAnonymousItems; DebugOnly rv = GetAnonymousContent(content, comboboxFrame, newAnonymousItems); MOZ_ASSERT(NS_SUCCEEDED(rv)); MOZ_ASSERT(!newAnonymousItems.IsEmpty()); // Manually create a frame for the special NAC. MOZ_ASSERT(newAnonymousItems[0].mContent == comboboxFrame->GetDisplayNode()); newAnonymousItems.RemoveElementAt(0); nsIFrame* customFrame = comboboxFrame->CreateFrameForDisplayNode(); MOZ_ASSERT(customFrame); childList.AppendFrame(nullptr, customFrame); nsFrameConstructorSaveState floatSaveState; aState.MaybePushFloatContainingBlock(comboboxFrame, floatSaveState); // The other piece of NAC can take the normal path. AutoFrameConstructionItemList fcItems(this); AutoFrameConstructionPageName pageNameTracker(aState, comboboxFrame); AddFCItemsForAnonymousContent(aState, comboboxFrame, newAnonymousItems, fcItems, pageNameTracker); ConstructFramesFromItemList(aState, fcItems, comboboxFrame, /* aParentIsWrapperAnonBox = */ false, childList); comboboxFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); aState.mFrameState = historyState; if (aState.mFrameState) { // Restore frame state for the entire subtree of |comboboxFrame|. RestoreFrameState(comboboxFrame, aState.mFrameState); } return comboboxFrame; } // Listbox, not combobox nsContainerFrame* listFrame = NS_NewListControlFrame(mPresShell, computedStyle); nsContainerFrame* scrolledFrame = NS_NewSelectsAreaFrame(mPresShell, computedStyle, NS_BLOCK_FLOAT_MGR); // ******* this code stolen from Initialze ScrollFrame ******** // please adjust this code to use BuildScrollFrame. InitializeListboxSelect(aState, listFrame, scrolledFrame, content, aParentFrame, computedStyle, aFrameList); return listFrame; } void nsCSSFrameConstructor::InitializeListboxSelect( nsFrameConstructorState& aState, nsContainerFrame* scrollFrame, nsContainerFrame* scrolledFrame, nsIContent* aContent, nsContainerFrame* aParentFrame, ComputedStyle* aComputedStyle, nsFrameList& aFrameList) { // Initialize it nsContainerFrame* geometricParent = aState.GetGeometricParent(*aComputedStyle->StyleDisplay(), aParentFrame); // We don't call InitAndRestoreFrame for scrollFrame because we can only // restore the frame state after its parts have been created (in particular, // the scrollable view). So we have to split Init and Restore. scrollFrame->Init(aContent, geometricParent, nullptr); aState.AddChild(scrollFrame, aFrameList, aContent, aParentFrame); BuildScrollFrame(aState, aContent, aComputedStyle, scrolledFrame, geometricParent, scrollFrame); if (aState.mFrameState) { // Restore frame state for the scroll frame RestoreFrameStateFor(scrollFrame, aState.mFrameState); } nsFrameConstructorSaveState floatSaveState; aState.MaybePushFloatContainingBlock(scrolledFrame, floatSaveState); // Process children nsFrameList childList; ProcessChildren(aState, aContent, aComputedStyle, scrolledFrame, false, childList, false); // Set the scrolled frame's initial child lists scrolledFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); } nsIFrame* nsCSSFrameConstructor::ConstructFieldSetFrame( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aStyleDisplay, nsFrameList& aFrameList) { AutoRestore savedHasRenderedLegend(aState.mHasRenderedLegend); aState.mHasRenderedLegend = false; nsIContent* const content = aItem.mContent; ComputedStyle* const computedStyle = aItem.mComputedStyle; nsContainerFrame* fieldsetFrame = NS_NewFieldSetFrame(mPresShell, computedStyle); // Initialize it InitAndRestoreFrame(aState, content, aState.GetGeometricParent(*aStyleDisplay, aParentFrame), fieldsetFrame); fieldsetFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); // Resolve style and initialize the frame RefPtr fieldsetContentStyle = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle( PseudoStyleType::fieldsetContent, computedStyle); const nsStyleDisplay* fieldsetContentDisplay = fieldsetContentStyle->StyleDisplay(); bool isScrollable = fieldsetContentDisplay->IsScrollableOverflow(); nsContainerFrame* scrollFrame = nullptr; if (isScrollable) { fieldsetContentStyle = BeginBuildingScrollFrame( aState, content, fieldsetContentStyle, fieldsetFrame, PseudoStyleType::scrolledContent, false, scrollFrame); } // Create the inner ::-moz-fieldset-content frame. nsContainerFrame* contentFrameTop; nsContainerFrame* contentFrame; auto parent = scrollFrame ? scrollFrame : fieldsetFrame; MOZ_ASSERT(fieldsetContentDisplay->DisplayOutside() == StyleDisplayOutside::Block); switch (fieldsetContentDisplay->DisplayInside()) { case StyleDisplayInside::Flex: contentFrame = NS_NewFlexContainerFrame(mPresShell, fieldsetContentStyle); InitAndRestoreFrame(aState, content, parent, contentFrame); contentFrameTop = contentFrame; break; case StyleDisplayInside::Grid: contentFrame = NS_NewGridContainerFrame(mPresShell, fieldsetContentStyle); InitAndRestoreFrame(aState, content, parent, contentFrame); contentFrameTop = contentFrame; break; default: { MOZ_ASSERT(fieldsetContentDisplay->mDisplay == StyleDisplay::Block, "bug in StyleAdjuster::adjust_for_fieldset_content?"); contentFrame = NS_NewBlockFormattingContext(mPresShell, fieldsetContentStyle); if (fieldsetContentStyle->StyleColumn()->IsColumnContainerStyle()) { contentFrameTop = BeginBuildingColumns( aState, content, parent, contentFrame, fieldsetContentStyle); } else { // No need to create column container. Initialize content frame. InitAndRestoreFrame(aState, content, parent, contentFrame); contentFrameTop = contentFrame; } break; } } aState.AddChild(fieldsetFrame, aFrameList, content, aParentFrame); // Process children nsFrameConstructorSaveState absoluteSaveState; nsFrameList childList; contentFrameTop->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN); if (fieldsetFrame->IsAbsPosContainingBlock()) { aState.PushAbsoluteContainingBlock(contentFrameTop, fieldsetFrame, absoluteSaveState); } nsFrameConstructorSaveState floatSaveState; aState.MaybePushFloatContainingBlock(contentFrame, floatSaveState); ProcessChildren(aState, content, computedStyle, contentFrame, true, childList, true); nsFrameList fieldsetKids; fieldsetKids.AppendFrame(nullptr, scrollFrame ? scrollFrame : contentFrameTop); if (!MayNeedToCreateColumnSpanSiblings(contentFrame, childList)) { // Set the inner frame's initial child lists. contentFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); } else { // Extract any initial non-column-span kids, and put them in inner frame's // child list. nsFrameList initialNonColumnSpanKids = childList.Split([](nsIFrame* f) { return f->IsColumnSpan(); }); contentFrame->SetInitialChildList(FrameChildListID::Principal, std::move(initialNonColumnSpanKids)); if (childList.NotEmpty()) { nsFrameList columnSpanSiblings = CreateColumnSpanSiblings( aState, contentFrame, childList, // Column content should never be a absolute/fixed positioned // containing block. Pass nullptr as aPositionedFrame. nullptr); FinishBuildingColumns(aState, contentFrameTop, contentFrame, columnSpanSiblings); } } if (isScrollable) { FinishBuildingScrollFrame(scrollFrame, contentFrameTop); } // We use AppendFrames here because the rendered legend will already // be present in the principal child list if it exists. fieldsetFrame->AppendFrames(FrameChildListID::NoReflowPrincipal, std::move(fieldsetKids)); return fieldsetFrame; } nsIFrame* nsCSSFrameConstructor::ConstructDetails( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aStyleDisplay, nsFrameList& aFrameList) { if (!aStyleDisplay->IsScrollableOverflow()) { return ConstructNonScrollableBlock(aState, aItem, aParentFrame, aStyleDisplay, aFrameList); } // Build a scroll frame if necessary. return ConstructScrollableBlock(aState, aItem, aParentFrame, aStyleDisplay, aFrameList); } nsIFrame* nsCSSFrameConstructor::ConstructBlockRubyFrame( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aStyleDisplay, nsFrameList& aFrameList) { nsIContent* const content = aItem.mContent; ComputedStyle* const computedStyle = aItem.mComputedStyle; nsBlockFrame* blockFrame = NS_NewBlockFrame(mPresShell, computedStyle); nsContainerFrame* newFrame = blockFrame; nsContainerFrame* geometricParent = aState.GetGeometricParent(*aStyleDisplay, aParentFrame); AutoFrameConstructionPageName pageNameTracker(aState, blockFrame); if ((aItem.mFCData->mBits & FCDATA_MAY_NEED_SCROLLFRAME) && aStyleDisplay->IsScrollableOverflow()) { nsContainerFrame* scrollframe = nullptr; BuildScrollFrame(aState, content, computedStyle, blockFrame, geometricParent, scrollframe); newFrame = scrollframe; } else { InitAndRestoreFrame(aState, content, geometricParent, blockFrame); } RefPtr rubyStyle = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle( PseudoStyleType::blockRubyContent, computedStyle); nsContainerFrame* rubyFrame = NS_NewRubyFrame(mPresShell, rubyStyle); InitAndRestoreFrame(aState, content, blockFrame, rubyFrame); SetInitialSingleChild(blockFrame, rubyFrame); blockFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); aState.AddChild(newFrame, aFrameList, content, aParentFrame); if (!mRootElementFrame) { // The frame we're constructing will be the root element frame. SetRootElementFrameAndConstructCanvasAnonContent(newFrame, aState, aFrameList); } nsFrameConstructorSaveState absoluteSaveState; blockFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN); if (aStyleDisplay->IsAbsPosContainingBlock(newFrame)) { aState.PushAbsoluteContainingBlock(blockFrame, blockFrame, absoluteSaveState); } nsFrameConstructorSaveState floatSaveState; aState.MaybePushFloatContainingBlock(blockFrame, floatSaveState); nsFrameList childList; ProcessChildren(aState, content, rubyStyle, rubyFrame, true, childList, false, nullptr); rubyFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); return newFrame; } static nsIFrame* FindAncestorWithGeneratedContentPseudo(nsIFrame* aFrame) { for (nsIFrame* f = aFrame->GetParent(); f; f = f->GetParent()) { NS_ASSERTION(f->IsGeneratedContentFrame(), "should not have exited generated content"); auto pseudo = f->Style()->GetPseudoType(); if (pseudo == PseudoStyleType::before || pseudo == PseudoStyleType::after || pseudo == PseudoStyleType::marker) return f; } return nullptr; } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindTextData(const Text& aTextContent, nsIFrame* aParentFrame) { if (aParentFrame && IsFrameForSVG(aParentFrame)) { nsIFrame* ancestorFrame = SVGUtils::GetFirstNonAAncestorFrame(aParentFrame); if (!ancestorFrame || !SVGUtils::IsInSVGTextSubtree(ancestorFrame)) { return nullptr; } // FIXME(bug 1588477) Don't render stuff in display: contents / Shadow DOM // subtrees, because TextCorrespondenceRecorder in the SVG text code doesn't // really know how to deal with it. This kinda sucks. :( if (aParentFrame->GetContent() != aTextContent.GetParent()) { return nullptr; } static constexpr FrameConstructionData sSVGTextData( NS_NewTextFrame, FCDATA_IS_LINE_PARTICIPANT | FCDATA_IS_SVG_TEXT); return &sSVGTextData; } static constexpr FrameConstructionData sTextData(NS_NewTextFrame, FCDATA_IS_LINE_PARTICIPANT); return &sTextData; } void nsCSSFrameConstructor::ConstructTextFrame( const FrameConstructionData* aData, nsFrameConstructorState& aState, nsIContent* aContent, nsContainerFrame* aParentFrame, ComputedStyle* aComputedStyle, nsFrameList& aFrameList) { MOZ_ASSERT(aData, "Must have frame construction data"); nsIFrame* newFrame = (*aData->mFunc.mCreationFunc)(mPresShell, aComputedStyle); InitAndRestoreFrame(aState, aContent, aParentFrame, newFrame); // We never need to create a view for a text frame. if (newFrame->IsGeneratedContentFrame()) { UniquePtr initializer( static_cast( aContent->TakeProperty(nsGkAtoms::genConInitializerProperty))); if (initializer) { if (initializer->mNode.release()->InitTextFrame( initializer->mList, FindAncestorWithGeneratedContentPseudo(newFrame), newFrame)) { (this->*(initializer->mDirtyAll))(); } } } // Add the newly constructed frame to the flow aFrameList.AppendFrame(nullptr, newFrame); if (!aState.mCreatingExtraFrames || (aContent->IsInNativeAnonymousSubtree() && !aContent->GetPrimaryFrame())) { aContent->SetPrimaryFrame(newFrame); } } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindDataByInt(int32_t aInt, const Element& aElement, ComputedStyle& aComputedStyle, const FrameConstructionDataByInt* aDataPtr, uint32_t aDataLength) { for (const FrameConstructionDataByInt *curData = aDataPtr, *endData = aDataPtr + aDataLength; curData != endData; ++curData) { if (curData->mInt == aInt) { const FrameConstructionData* data = &curData->mData; if (data->mBits & FCDATA_FUNC_IS_DATA_GETTER) { return data->mFunc.mDataGetter(aElement, aComputedStyle); } return data; } } return nullptr; } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindDataByTag(const Element& aElement, ComputedStyle& aStyle, const FrameConstructionDataByTag* aDataPtr, uint32_t aDataLength) { const nsAtom* tag = aElement.NodeInfo()->NameAtom(); for (const FrameConstructionDataByTag *curData = aDataPtr, *endData = aDataPtr + aDataLength; curData != endData; ++curData) { if (curData->mTag == tag) { const FrameConstructionData* data = &curData->mData; if (data->mBits & FCDATA_FUNC_IS_DATA_GETTER) { return data->mFunc.mDataGetter(aElement, aStyle); } return data; } } return nullptr; } #define SUPPRESS_FCDATA() FrameConstructionData(nullptr, FCDATA_SUPPRESS_FRAME) #define SIMPLE_INT_CREATE(_int, _func) \ { int32_t(_int), FrameConstructionData(_func) } #define SIMPLE_INT_CHAIN(_int, _func) \ { int32_t(_int), FrameConstructionData(_func) } #define COMPLEX_INT_CREATE(_int, _func) \ { int32_t(_int), FrameConstructionData(_func) } #define SIMPLE_TAG_CREATE(_tag, _func) \ { nsGkAtoms::_tag, FrameConstructionData(_func) } #define SIMPLE_TAG_CHAIN(_tag, _func) \ { nsGkAtoms::_tag, FrameConstructionData(_func) } #define COMPLEX_TAG_CREATE(_tag, _func) \ { nsGkAtoms::_tag, FrameConstructionData(_func) } static nsFieldSetFrame* GetFieldSetFrameFor(nsIFrame* aFrame) { auto pseudo = aFrame->Style()->GetPseudoType(); if (pseudo == PseudoStyleType::fieldsetContent || pseudo == PseudoStyleType::scrolledContent || pseudo == PseudoStyleType::columnSet || pseudo == PseudoStyleType::columnContent) { return GetFieldSetFrameFor(aFrame->GetParent()); } return do_QueryFrame(aFrame); } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindHTMLData(const Element& aElement, nsIFrame* aParentFrame, ComputedStyle& aStyle) { MOZ_ASSERT(aElement.IsHTMLElement()); NS_ASSERTION(!aParentFrame || aParentFrame->Style()->GetPseudoType() != PseudoStyleType::fieldsetContent || aParentFrame->GetParent()->IsFieldSetFrame(), "Unexpected parent for fieldset content anon box"); static constexpr FrameConstructionDataByTag sHTMLData[] = { SIMPLE_TAG_CHAIN(img, nsCSSFrameConstructor::FindImgData), SIMPLE_TAG_CHAIN(mozgeneratedcontentimage, nsCSSFrameConstructor::FindGeneratedImageData), {nsGkAtoms::br, {NS_NewBRFrame, FCDATA_IS_LINE_PARTICIPANT | FCDATA_IS_LINE_BREAK}}, SIMPLE_TAG_CREATE(wbr, NS_NewWBRFrame), SIMPLE_TAG_CHAIN(input, nsCSSFrameConstructor::FindInputData), SIMPLE_TAG_CREATE(textarea, NS_NewTextControlFrame), COMPLEX_TAG_CREATE(select, &nsCSSFrameConstructor::ConstructSelectFrame), SIMPLE_TAG_CHAIN(object, nsCSSFrameConstructor::FindObjectData), SIMPLE_TAG_CHAIN(embed, nsCSSFrameConstructor::FindObjectData), COMPLEX_TAG_CREATE(fieldset, &nsCSSFrameConstructor::ConstructFieldSetFrame), SIMPLE_TAG_CREATE(frameset, NS_NewHTMLFramesetFrame), SIMPLE_TAG_CREATE(iframe, NS_NewSubDocumentFrame), {nsGkAtoms::button, {ToCreationFunc(NS_NewHTMLButtonControlFrame), FCDATA_ALLOW_BLOCK_STYLES | FCDATA_ALLOW_GRID_FLEX_COLUMN, PseudoStyleType::buttonContent}}, SIMPLE_TAG_CHAIN(canvas, nsCSSFrameConstructor::FindCanvasData), SIMPLE_TAG_CREATE(video, NS_NewHTMLVideoFrame), SIMPLE_TAG_CREATE(audio, NS_NewHTMLVideoFrame), SIMPLE_TAG_CREATE(progress, NS_NewProgressFrame), SIMPLE_TAG_CREATE(meter, NS_NewMeterFrame), COMPLEX_TAG_CREATE(details, &nsCSSFrameConstructor::ConstructDetails)}; return FindDataByTag(aElement, aStyle, sHTMLData, ArrayLength(sHTMLData)); } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindGeneratedImageData(const Element& aElement, ComputedStyle&) { if (!aElement.IsInNativeAnonymousSubtree()) { return nullptr; } auto& generatedContent = static_cast(aElement); if (generatedContent.IsForListStyleImageMarker()) { static constexpr FrameConstructionData sImgData( NS_NewImageFrameForListStyleImage); return &sImgData; } static constexpr FrameConstructionData sImgData( NS_NewImageFrameForGeneratedContentIndex); return &sImgData; } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindImgData(const Element& aElement, ComputedStyle& aStyle) { if (nsImageFrame::ImageFrameTypeFor(aElement, aStyle) != nsImageFrame::ImageFrameType::ForElementRequest) { // content: url gets handled by the generic code-path. return nullptr; } static constexpr FrameConstructionData sImgData(NS_NewImageFrame); return &sImgData; } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindImgControlData(const Element& aElement, ComputedStyle& aStyle) { if (nsImageFrame::ImageFrameTypeFor(aElement, aStyle) != nsImageFrame::ImageFrameType::ForElementRequest) { return nullptr; } static constexpr FrameConstructionData sImgControlData( NS_NewImageControlFrame); return &sImgControlData; } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindSearchControlData(const Element& aElement, ComputedStyle& aStyle) { if (StaticPrefs::layout_forms_input_type_search_enabled()) { static constexpr FrameConstructionData sSearchControlData( NS_NewSearchControlFrame); return &sSearchControlData; } static constexpr FrameConstructionData sTextControlData( NS_NewTextControlFrame); return &sTextControlData; } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindInputData(const Element& aElement, ComputedStyle& aStyle) { static constexpr FrameConstructionDataByInt sInputData[] = { SIMPLE_INT_CREATE(FormControlType::InputCheckbox, ToCreationFunc(NS_NewCheckboxRadioFrame)), SIMPLE_INT_CREATE(FormControlType::InputRadio, ToCreationFunc(NS_NewCheckboxRadioFrame)), SIMPLE_INT_CREATE(FormControlType::InputFile, NS_NewFileControlFrame), SIMPLE_INT_CHAIN(FormControlType::InputImage, nsCSSFrameConstructor::FindImgControlData), SIMPLE_INT_CREATE(FormControlType::InputEmail, NS_NewTextControlFrame), SIMPLE_INT_CREATE(FormControlType::InputText, NS_NewTextControlFrame), SIMPLE_INT_CREATE(FormControlType::InputTel, NS_NewTextControlFrame), SIMPLE_INT_CREATE(FormControlType::InputUrl, NS_NewTextControlFrame), SIMPLE_INT_CREATE(FormControlType::InputRange, NS_NewRangeFrame), SIMPLE_INT_CREATE(FormControlType::InputPassword, NS_NewTextControlFrame), {int32_t(FormControlType::InputColor), {NS_NewColorControlFrame, 0, PseudoStyleType::buttonContent}}, SIMPLE_INT_CHAIN(FormControlType::InputSearch, nsCSSFrameConstructor::FindSearchControlData), SIMPLE_INT_CREATE(FormControlType::InputNumber, NS_NewNumberControlFrame), SIMPLE_INT_CREATE(FormControlType::InputTime, NS_NewDateTimeControlFrame), SIMPLE_INT_CREATE(FormControlType::InputDate, NS_NewDateTimeControlFrame), SIMPLE_INT_CREATE(FormControlType::InputDatetimeLocal, NS_NewDateTimeControlFrame), // TODO: this is temporary until a frame is written: bug 888320 SIMPLE_INT_CREATE(FormControlType::InputMonth, NS_NewTextControlFrame), // TODO: this is temporary until a frame is written: bug 888320 SIMPLE_INT_CREATE(FormControlType::InputWeek, NS_NewTextControlFrame), {int32_t(FormControlType::InputSubmit), {ToCreationFunc(NS_NewGfxButtonControlFrame), 0, PseudoStyleType::buttonContent}}, {int32_t(FormControlType::InputReset), {ToCreationFunc(NS_NewGfxButtonControlFrame), 0, PseudoStyleType::buttonContent}}, {int32_t(FormControlType::InputButton), {ToCreationFunc(NS_NewGfxButtonControlFrame), 0, PseudoStyleType::buttonContent}} // Keeping hidden inputs out of here on purpose for so they get frames by // display (in practice, none). }; auto controlType = HTMLInputElement::FromNode(aElement)->ControlType(); // radio and checkbox inputs with appearance:none should be constructed // by display type. (Note that we're not checking that appearance is // not (respectively) StyleAppearance::Radio and StyleAppearance::Checkbox.) if ((controlType == FormControlType::InputCheckbox || controlType == FormControlType::InputRadio) && !aStyle.StyleDisplay()->HasAppearance()) { return nullptr; } return FindDataByInt(int32_t(controlType), aElement, aStyle, sInputData, ArrayLength(sInputData)); } static nsIFrame* NS_NewSubDocumentOrImageFrame(mozilla::PresShell* aPresShell, mozilla::ComputedStyle* aStyle) { return StaticPrefs:: browser_opaqueResponseBlocking_syntheticBrowsingContext_AtStartup() ? NS_NewSubDocumentFrame(aPresShell, aStyle) : NS_NewImageFrame(aPresShell, aStyle); } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindObjectData(const Element& aElement, ComputedStyle& aStyle) { // GetDisplayedType isn't necessarily nsIObjectLoadingContent::TYPE_NULL for // cases when the object is broken/suppressed/etc (e.g. a broken image), but // we want to treat those cases as TYPE_NULL uint32_t type; if (aElement.State().HasState(ElementState::BROKEN)) { type = nsIObjectLoadingContent::TYPE_NULL; } else { nsCOMPtr objContent = do_QueryInterface(const_cast(&aElement)); NS_ASSERTION(objContent, "embed and object must implement " "nsIObjectLoadingContent!"); objContent->GetDisplayedType(&type); } if (type == nsIObjectLoadingContent::TYPE_FALLBACK && !StaticPrefs::layout_use_plugin_fallback()) { type = nsIObjectLoadingContent::TYPE_NULL; } static constexpr FrameConstructionDataByInt sObjectData[] = { SIMPLE_INT_CREATE(nsIObjectLoadingContent::TYPE_LOADING, NS_NewEmptyFrame), SIMPLE_INT_CREATE(nsIObjectLoadingContent::TYPE_FALLBACK, ToCreationFunc(NS_NewBlockFrame)), SIMPLE_INT_CREATE(nsIObjectLoadingContent::TYPE_IMAGE, NS_NewSubDocumentOrImageFrame), SIMPLE_INT_CREATE(nsIObjectLoadingContent::TYPE_DOCUMENT, NS_NewSubDocumentFrame), // Fake plugin handlers load as documents // XXXmats is TYPE_FAKE_PLUGIN something we need? SIMPLE_INT_CREATE(nsIObjectLoadingContent::TYPE_FAKE_PLUGIN, NS_NewSubDocumentFrame) // Nothing for TYPE_NULL so we'll construct frames by display there }; return FindDataByInt((int32_t)type, aElement, aStyle, sObjectData, ArrayLength(sObjectData)); } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindCanvasData(const Element& aElement, ComputedStyle& aStyle) { // We want to check whether script is enabled on the document that // could be painting to the canvas. That's the owner document of // the canvas, except when the owner document is a static document, // in which case it's the original document it was cloned from. Document* doc = aElement.OwnerDoc(); if (doc->IsStaticDocument()) { doc = doc->GetOriginalDocument(); } if (!doc->IsScriptEnabled()) { return nullptr; } static constexpr FrameConstructionData sCanvasData( NS_NewHTMLCanvasFrame, 0, PseudoStyleType::htmlCanvasContent); return &sCanvasData; } void nsCSSFrameConstructor::ConstructFrameFromItemInternal( FrameConstructionItem& aItem, nsFrameConstructorState& aState, nsContainerFrame* aParentFrame, nsFrameList& aFrameList) { const FrameConstructionData* data = aItem.mFCData; NS_ASSERTION(data, "Must have frame construction data"); uint32_t bits = data->mBits; NS_ASSERTION(!(bits & FCDATA_FUNC_IS_DATA_GETTER), "Should have dealt with this inside the data finder"); // Some sets of bits are not compatible with each other #define CHECK_ONLY_ONE_BIT(_bit1, _bit2) \ NS_ASSERTION(!(bits & _bit1) || !(bits & _bit2), \ "Only one of these bits should be set") CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_FORCE_NULL_ABSPOS_CONTAINER); CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_WRAP_KIDS_IN_BLOCKS); CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_IS_POPUP); CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_SKIP_ABSPOS_PUSH); CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_DISALLOW_GENERATED_CONTENT); CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_ALLOW_BLOCK_STYLES); CHECK_ONLY_ONE_BIT(FCDATA_FUNC_IS_FULL_CTOR, FCDATA_CREATE_BLOCK_WRAPPER_FOR_ALL_KIDS); CHECK_ONLY_ONE_BIT(FCDATA_WRAP_KIDS_IN_BLOCKS, FCDATA_CREATE_BLOCK_WRAPPER_FOR_ALL_KIDS); #undef CHECK_ONLY_ONE_BIT NS_ASSERTION(!(bits & FCDATA_FORCED_NON_SCROLLABLE_BLOCK) || ((bits & FCDATA_FUNC_IS_FULL_CTOR) && data->mFunc.mFullConstructor == &nsCSSFrameConstructor::ConstructNonScrollableBlock), "Unexpected FCDATA_FORCED_NON_SCROLLABLE_BLOCK flag"); MOZ_ASSERT( !(bits & FCDATA_IS_WRAPPER_ANON_BOX) || (bits & FCDATA_USE_CHILD_ITEMS), "Wrapper anon boxes should always have FCDATA_USE_CHILD_ITEMS"); MOZ_ASSERT(!(bits & FCDATA_ALLOW_GRID_FLEX_COLUMN) || (bits & FCDATA_CREATE_BLOCK_WRAPPER_FOR_ALL_KIDS), "Need the block wrapper bit to create grid/flex/column."); // Don't create a subdocument frame for iframes if we're creating extra frames if (aState.mCreatingExtraFrames && aItem.mContent->IsHTMLElement(nsGkAtoms::iframe)) { return; } nsIContent* const content = aItem.mContent; nsIFrame* newFrame; nsIFrame* primaryFrame; ComputedStyle* const computedStyle = aItem.mComputedStyle; const nsStyleDisplay* display = computedStyle->StyleDisplay(); if (bits & FCDATA_FUNC_IS_FULL_CTOR) { newFrame = (this->*(data->mFunc.mFullConstructor))( aState, aItem, aParentFrame, display, aFrameList); MOZ_ASSERT(newFrame, "Full constructor failed"); primaryFrame = newFrame; } else { newFrame = (*data->mFunc.mCreationFunc)(mPresShell, computedStyle); const bool allowOutOfFlow = !(bits & FCDATA_DISALLOW_OUT_OF_FLOW); const bool isPopup = aItem.mIsPopup; nsContainerFrame* geometricParent = (isPopup || allowOutOfFlow) ? aState.GetGeometricParent(*display, aParentFrame) : aParentFrame; // In the non-scrollframe case, primaryFrame and newFrame are equal; in the // scrollframe case, newFrame is the scrolled frame while primaryFrame is // the scrollframe. if ((bits & FCDATA_MAY_NEED_SCROLLFRAME) && display->IsScrollableOverflow()) { nsContainerFrame* scrollframe = nullptr; BuildScrollFrame(aState, content, computedStyle, newFrame, geometricParent, scrollframe); primaryFrame = scrollframe; } else { InitAndRestoreFrame(aState, content, geometricParent, newFrame); primaryFrame = newFrame; } // If we need to create a block formatting context to wrap our // kids, do it now. nsIFrame* maybeAbsoluteContainingBlockStyleFrame = primaryFrame; nsIFrame* maybeAbsoluteContainingBlock = newFrame; nsIFrame* possiblyLeafFrame = newFrame; nsContainerFrame* outerFrame = nullptr; if (bits & FCDATA_CREATE_BLOCK_WRAPPER_FOR_ALL_KIDS) { RefPtr outerStyle = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle( data->mAnonBoxPseudo, computedStyle); #ifdef DEBUG nsContainerFrame* containerFrame = do_QueryFrame(newFrame); MOZ_ASSERT(containerFrame); #endif nsContainerFrame* container = static_cast(newFrame); nsContainerFrame* innerFrame; if (bits & FCDATA_ALLOW_GRID_FLEX_COLUMN) { switch (display->DisplayInside()) { case StyleDisplayInside::Flex: outerFrame = NS_NewFlexContainerFrame(mPresShell, outerStyle); InitAndRestoreFrame(aState, content, container, outerFrame); innerFrame = outerFrame; break; case StyleDisplayInside::Grid: outerFrame = NS_NewGridContainerFrame(mPresShell, outerStyle); InitAndRestoreFrame(aState, content, container, outerFrame); innerFrame = outerFrame; break; default: { innerFrame = NS_NewBlockFormattingContext(mPresShell, outerStyle); if (outerStyle->StyleColumn()->IsColumnContainerStyle()) { outerFrame = BeginBuildingColumns(aState, content, container, innerFrame, outerStyle); } else { // No need to create column container. Initialize innerFrame. InitAndRestoreFrame(aState, content, container, innerFrame); outerFrame = innerFrame; } break; } } } else { innerFrame = NS_NewBlockFormattingContext(mPresShell, outerStyle); InitAndRestoreFrame(aState, content, container, innerFrame); outerFrame = innerFrame; } SetInitialSingleChild(container, outerFrame); container->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); // Now figure out whether newFrame or outerFrame should be the // absolute container. auto outerDisplay = outerStyle->StyleDisplay(); if (outerDisplay->IsAbsPosContainingBlock(outerFrame)) { maybeAbsoluteContainingBlock = outerFrame; maybeAbsoluteContainingBlockStyleFrame = outerFrame; innerFrame->AddStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN); } // Our kids should go into the innerFrame. newFrame = innerFrame; } aState.AddChild(primaryFrame, aFrameList, content, aParentFrame, allowOutOfFlow, allowOutOfFlow); nsContainerFrame* newFrameAsContainer = do_QueryFrame(newFrame); if (newFrameAsContainer) { // Process the child content if requested nsFrameList childList; nsFrameConstructorSaveState absoluteSaveState; if (bits & FCDATA_FORCE_NULL_ABSPOS_CONTAINER) { aState.PushAbsoluteContainingBlock(nullptr, nullptr, absoluteSaveState); } else if (!(bits & FCDATA_SKIP_ABSPOS_PUSH)) { maybeAbsoluteContainingBlock->AddStateBits( NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN); if (maybeAbsoluteContainingBlockStyleFrame->IsAbsPosContainingBlock()) { auto* cf = static_cast(maybeAbsoluteContainingBlock); aState.PushAbsoluteContainingBlock( cf, maybeAbsoluteContainingBlockStyleFrame, absoluteSaveState); } } nsFrameConstructorSaveState floatSaveState; aState.MaybePushFloatContainingBlock(newFrameAsContainer, floatSaveState); if (bits & FCDATA_USE_CHILD_ITEMS) { // At this point, we have not set up the auto value for this frame, and // no caller will have set it so it is not redundant and therefor will // not assert. AutoFrameConstructionPageName pageNameTracker(aState, newFrameAsContainer); ConstructFramesFromItemList( aState, aItem.mChildItems, newFrameAsContainer, bits & FCDATA_IS_WRAPPER_ANON_BOX, childList); } else { // Process the child frames. ProcessChildren(aState, content, computedStyle, newFrameAsContainer, !(bits & FCDATA_DISALLOW_GENERATED_CONTENT), childList, (bits & FCDATA_ALLOW_BLOCK_STYLES) != 0, possiblyLeafFrame); } if (bits & FCDATA_WRAP_KIDS_IN_BLOCKS) { nsFrameList newList; nsFrameList currentBlockList; nsIFrame* f; while ((f = childList.FirstChild()) != nullptr) { bool wrapFrame = IsInlineFrame(f) || IsFramePartOfIBSplit(f); if (!wrapFrame) { FlushAccumulatedBlock(aState, content, newFrameAsContainer, currentBlockList, newList); } childList.RemoveFrame(f); if (wrapFrame) { currentBlockList.AppendFrame(nullptr, f); } else { newList.AppendFrame(nullptr, f); } } FlushAccumulatedBlock(aState, content, newFrameAsContainer, currentBlockList, newList); if (childList.NotEmpty()) { // an error must have occurred, delete unprocessed frames childList.DestroyFrames(); } childList = std::move(newList); } if (!(bits & FCDATA_ALLOW_GRID_FLEX_COLUMN) || !MayNeedToCreateColumnSpanSiblings(newFrameAsContainer, childList)) { // Set the frame's initial child list. Note that MathML depends on this // being called even if childList is empty! newFrameAsContainer->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); } else { // Extract any initial non-column-span kids, and put them in inner // frame's child list. nsFrameList initialNonColumnSpanKids = childList.Split([](nsIFrame* f) { return f->IsColumnSpan(); }); newFrameAsContainer->SetInitialChildList( FrameChildListID::Principal, std::move(initialNonColumnSpanKids)); if (childList.NotEmpty()) { nsFrameList columnSpanSiblings = CreateColumnSpanSiblings( aState, newFrameAsContainer, childList, // Column content should never be a absolute/fixed positioned // containing block. Pass nullptr as aPositionedFrame. nullptr); MOZ_ASSERT(outerFrame, "outerFrame should be non-null if multi-column container " "is created."); FinishBuildingColumns(aState, outerFrame, newFrameAsContainer, columnSpanSiblings); } } } } NS_ASSERTION(newFrame->IsFrameOfType(nsIFrame::eLineParticipant) == ((bits & FCDATA_IS_LINE_PARTICIPANT) != 0), "Incorrectly set FCDATA_IS_LINE_PARTICIPANT bits"); // Even if mCreatingExtraFrames is set, we may need to SetPrimaryFrame for // generated content that doesn't have one yet. Note that we have to examine // the frame bit, because by this point mIsGeneratedContent has been cleared // on aItem. if ((!aState.mCreatingExtraFrames || (aItem.mContent->IsRootOfNativeAnonymousSubtree() && !aItem.mContent->GetPrimaryFrame())) && !(bits & FCDATA_SKIP_FRAMESET)) { aItem.mContent->SetPrimaryFrame(primaryFrame); ActiveLayerTracker::TransferActivityToFrame(aItem.mContent, primaryFrame); } } static void GatherSubtreeElements(Element* aElement, nsTArray& aElements) { aElements.AppendElement(aElement); StyleChildrenIterator iter(aElement); for (nsIContent* c = iter.GetNextChild(); c; c = iter.GetNextChild()) { if (!c->IsElement()) { continue; } GatherSubtreeElements(c->AsElement(), aElements); } } nsresult nsCSSFrameConstructor::GetAnonymousContent( nsIContent* aParent, nsIFrame* aParentFrame, nsTArray& aContent) { nsIAnonymousContentCreator* creator = do_QueryFrame(aParentFrame); if (!creator) { return NS_OK; } nsresult rv = creator->CreateAnonymousContent(aContent); if (NS_FAILED(rv)) { // CreateAnonymousContent failed, e.g. because the page has a loop. return rv; } MOZ_ASSERT(aParent->IsElement()); for (const auto& info : aContent) { // get our child's content and set its parent to our content nsIContent* content = info.mContent; content->SetIsNativeAnonymousRoot(); BindContext context(*aParent->AsElement(), BindContext::ForNativeAnonymous); rv = content->BindToTree(context, *aParent); if (NS_FAILED(rv)) { content->UnbindFromTree(); return rv; } } // Some situations where we don't cache anonymous content styles: // // * when visibility or pointer-events is anything other than the initial // value; we rely on visibility and pointer-events inheriting into anonymous // content, but don't bother adding this state to the AnonymousContentKey, // since it's not so common. Note that with overlay scrollbars, scrollbars // always start off with pointer-events: none so we don't need to check for // that in that case. // // * when the medium is anything other than screen; some UA style sheet rules // apply in e.g. print medium, and will give different results from the // cached styles Maybe computedAllowStyleCaching; auto ComputeAllowStyleCaching = [&] { if (!StaticPrefs::layout_css_cached_scrollbar_styles_enabled()) { return false; } if (aParentFrame->StyleVisibility()->mVisible != StyleVisibility::Visible) { return false; } nsPresContext* pc = mPresShell->GetPresContext(); if (!pc->UseOverlayScrollbars() && aParentFrame->StyleUI()->ComputedPointerEvents() != StylePointerEvents::Auto) { return false; } if (pc->Medium() != nsGkAtoms::screen) { return false; } return true; }; auto AllowStyleCaching = [&] { if (computedAllowStyleCaching.isNothing()) { computedAllowStyleCaching.emplace(ComputeAllowStyleCaching()); } return computedAllowStyleCaching.value(); }; // Compute styles for the anonymous content tree. ServoStyleSet* styleSet = mPresShell->StyleSet(); for (auto& info : aContent) { Element* e = Element::FromNode(info.mContent); if (!e) { continue; } if (info.mKey == AnonymousContentKey::None || !AllowStyleCaching()) { // Most NAC subtrees do not use caching of computed styles. Just go // ahead and eagerly style the subtree. styleSet->StyleNewSubtree(e); continue; } // We have a NAC subtree for which we can use cached styles. AutoTArray, 2> cachedStyles; AutoTArray elements; GatherSubtreeElements(e, elements); styleSet->GetCachedAnonymousContentStyles(info.mKey, cachedStyles); if (cachedStyles.IsEmpty()) { // We haven't stored cached styles for this kind of NAC subtree yet. // Eagerly compute those styles, then cache them for later. styleSet->StyleNewSubtree(e); for (Element* e : elements) { if (e->HasServoData()) { cachedStyles.AppendElement(ServoStyleSet::ResolveServoStyle(*e)); } else { cachedStyles.AppendElement(nullptr); } } styleSet->PutCachedAnonymousContentStyles(info.mKey, std::move(cachedStyles)); continue; } // We previously stored cached styles for this kind of NAC subtree. // Iterate over them and set them on the subtree's elements. MOZ_ASSERT(cachedStyles.Length() == elements.Length(), "should always produce the same size NAC subtree"); for (size_t i = 0, len = cachedStyles.Length(); i != len; ++i) { if (cachedStyles[i]) { #ifdef DEBUG // Assert that our cached style is the same as one we could compute. RefPtr cs = styleSet->ResolveStyleLazily(*elements[i]); MOZ_ASSERT( cachedStyles[i]->EqualForCachedAnonymousContentStyle(*cs), "cached anonymous content styles should be identical to those we " "would compute normally"); // All overlay scrollbars start off as inactive, so we can rely on their // pointer-events value being always none. MOZ_ASSERT(!mPresShell->GetPresContext()->UseOverlayScrollbars() || cs->StyleUI()->ComputedPointerEvents() == StylePointerEvents::None); #endif Servo_SetExplicitStyle(elements[i], cachedStyles[i]); } } } return NS_OK; } // XUL frames are not allowed to be out of flow. #define SIMPLE_XUL_FCDATA(_func) \ FrameConstructionData(_func, \ FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_SKIP_ABSPOS_PUSH) #define SCROLLABLE_XUL_FCDATA(_func) \ FrameConstructionData(_func, FCDATA_DISALLOW_OUT_OF_FLOW | \ FCDATA_SKIP_ABSPOS_PUSH | \ FCDATA_MAY_NEED_SCROLLFRAME) // .. but we allow some XUL frames to be _containers_ for out-of-flow content // (This is the same as SCROLLABLE_XUL_FCDATA, but w/o FCDATA_SKIP_ABSPOS_PUSH) #define SCROLLABLE_ABSPOS_CONTAINER_XUL_FCDATA(_func) \ FrameConstructionData( \ _func, FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_MAY_NEED_SCROLLFRAME) #define SIMPLE_XUL_CREATE(_tag, _func) \ { nsGkAtoms::_tag, SIMPLE_XUL_FCDATA(_func) } #define SCROLLABLE_XUL_CREATE(_tag, _func) \ { nsGkAtoms::_tag, SCROLLABLE_XUL_FCDATA(_func) } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindXULTagData(const Element& aElement, ComputedStyle& aStyle) { MOZ_ASSERT(aElement.IsXULElement()); static constexpr FrameConstructionData kPopupData( NS_NewMenuPopupFrame, FCDATA_IS_POPUP | FCDATA_SKIP_ABSPOS_PUSH); static constexpr FrameConstructionDataByTag sXULTagData[] = { SIMPLE_XUL_CREATE(image, NS_NewImageBoxFrame), SIMPLE_XUL_CREATE(treechildren, NS_NewTreeBodyFrame), SIMPLE_XUL_CREATE(treecol, NS_NewTreeColFrame), SIMPLE_TAG_CHAIN(label, nsCSSFrameConstructor::FindXULLabelOrDescriptionData), SIMPLE_TAG_CHAIN(description, nsCSSFrameConstructor::FindXULLabelOrDescriptionData), #ifdef XP_MACOSX SIMPLE_TAG_CHAIN(menubar, nsCSSFrameConstructor::FindXULMenubarData), #else SIMPLE_XUL_CREATE(menubar, NS_NewMenuBarFrame), #endif /* XP_MACOSX */ SIMPLE_XUL_CREATE(iframe, NS_NewSubDocumentFrame), SIMPLE_XUL_CREATE(editor, NS_NewSubDocumentFrame), SIMPLE_XUL_CREATE(browser, NS_NewSubDocumentFrame), SIMPLE_XUL_CREATE(splitter, NS_NewSplitterFrame), SIMPLE_XUL_CREATE(slider, NS_NewSliderFrame), SIMPLE_XUL_CREATE(scrollbar, NS_NewScrollbarFrame), SIMPLE_XUL_CREATE(scrollbarbutton, NS_NewScrollbarButtonFrame), {nsGkAtoms::panel, kPopupData}, {nsGkAtoms::menupopup, kPopupData}, {nsGkAtoms::tooltip, kPopupData}, }; return FindDataByTag(aElement, aStyle, sXULTagData, ArrayLength(sXULTagData)); } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindXULLabelOrDescriptionData(const Element& aElement, ComputedStyle&) { // Follow CSS display value if no value attribute if (!aElement.HasAttr(nsGkAtoms::value)) { return nullptr; } // Follow CSS display if there's no crop="center". if (!aElement.AttrValueIs(kNameSpaceID_None, nsGkAtoms::crop, nsGkAtoms::center, eCaseMatters)) { return nullptr; } static constexpr FrameConstructionData sXULTextBoxData = SIMPLE_XUL_FCDATA(NS_NewTextBoxFrame); return &sXULTextBoxData; } #ifdef XP_MACOSX /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindXULMenubarData(const Element& aElement, ComputedStyle&) { if (aElement.OwnerDoc()->IsInChromeDocShell()) { BrowsingContext* bc = aElement.OwnerDoc()->GetBrowsingContext(); bool isRoot = bc && !bc->GetParent(); if (isRoot) { // This is the root. Suppress the menubar, since on Mac // window menus are not attached to the window. static constexpr FrameConstructionData sSuppressData = SUPPRESS_FCDATA(); return &sSuppressData; } } static constexpr FrameConstructionData sMenubarData = SIMPLE_XUL_FCDATA(NS_NewMenuBarFrame); return &sMenubarData; } #endif /* XP_MACOSX */ already_AddRefed nsCSSFrameConstructor::BeginBuildingScrollFrame( nsFrameConstructorState& aState, nsIContent* aContent, ComputedStyle* aContentStyle, nsContainerFrame* aParentFrame, PseudoStyleType aScrolledPseudo, bool aIsRoot, nsContainerFrame*& aNewFrame) { nsContainerFrame* gfxScrollFrame = aNewFrame; nsFrameList anonymousList; if (!gfxScrollFrame) { const bool useXULScrollFrame = [&] { const auto& disp = *aContentStyle->StyleDisplay(); if (disp.DisplayOutside() == StyleDisplayOutside::XUL) { // XXX Should this be emulated? return true; } if (disp.DisplayInside() == StyleDisplayInside::MozBox) { return !aContentStyle->StyleVisibility()->EmulateMozBoxWithFlex(); } return false; }(); // Build a XULScrollFrame when the child is a box, otherwise an // HTMLScrollFrame if (useXULScrollFrame) { gfxScrollFrame = NS_NewXULScrollFrame(mPresShell, aContentStyle, aIsRoot); } else { gfxScrollFrame = NS_NewHTMLScrollFrame(mPresShell, aContentStyle, aIsRoot); } InitAndRestoreFrame(aState, aContent, aParentFrame, gfxScrollFrame); } MOZ_ASSERT(gfxScrollFrame); // if there are any anonymous children for the scroll frame, create // frames for them. // // We can't take the normal ProcessChildren path, because the NAC needs to // be parented to the scrollframe, and everything else needs to be parented // to the scrolledframe. AutoTArray scrollNAC; DebugOnly rv = GetAnonymousContent(aContent, gfxScrollFrame, scrollNAC); MOZ_ASSERT(NS_SUCCEEDED(rv)); if (scrollNAC.Length() > 0) { nsFrameConstructorSaveState floatSaveState; aState.MaybePushFloatContainingBlock(gfxScrollFrame, floatSaveState); AutoFrameConstructionItemList items(this); AutoFrameConstructionPageName pageNameTracker(aState, gfxScrollFrame); AddFCItemsForAnonymousContent(aState, gfxScrollFrame, scrollNAC, items, pageNameTracker); ConstructFramesFromItemList(aState, items, gfxScrollFrame, /* aParentIsWrapperAnonBox = */ false, anonymousList); } aNewFrame = gfxScrollFrame; gfxScrollFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); // we used the style that was passed in. So resolve another one. ServoStyleSet* styleSet = mPresShell->StyleSet(); RefPtr scrolledChildStyle = styleSet->ResolveInheritingAnonymousBoxStyle(aScrolledPseudo, aContentStyle); gfxScrollFrame->SetInitialChildList(FrameChildListID::Principal, std::move(anonymousList)); return scrolledChildStyle.forget(); } void nsCSSFrameConstructor::FinishBuildingScrollFrame( nsContainerFrame* aScrollFrame, nsIFrame* aScrolledFrame) { aScrollFrame->AppendFrames(FrameChildListID::Principal, nsFrameList(aScrolledFrame, aScrolledFrame)); } /** * Called to wrap a gfx scrollframe around a frame. The hierarchy will look like * this * * ------- for gfx scrollbars ------ * * * ScrollFrame * ^ * | * Frame (scrolled frame you passed in) * * * ----------------------------------- * LEGEND: * * ScrollFrame: This is a frame that manages gfx cross platform frame based * scrollbars. * * @param aContent the content node of the child to wrap. * * @param aScrolledFrame The frame of the content to wrap. This should not be * Initialized. This method will initialize it with a scrolled pseudo and no * nsIContent. The content will be attached to the scrollframe returned. * * @param aContentStyle the style that has already been resolved for the content * being passed in. * * @param aParentFrame The parent to attach the scroll frame to * * @param aNewFrame The new scrollframe or gfx scrollframe that we create. It * will contain the scrolled frame you passed in. (returned) If this is not * null, we'll just use it * * @param aScrolledContentStyle the style that was resolved for the scrolled * frame. (returned) */ void nsCSSFrameConstructor::BuildScrollFrame(nsFrameConstructorState& aState, nsIContent* aContent, ComputedStyle* aContentStyle, nsIFrame* aScrolledFrame, nsContainerFrame* aParentFrame, nsContainerFrame*& aNewFrame) { RefPtr scrolledContentStyle = BeginBuildingScrollFrame( aState, aContent, aContentStyle, aParentFrame, PseudoStyleType::scrolledContent, false, aNewFrame); aScrolledFrame->SetComputedStyleWithoutNotification(scrolledContentStyle); InitAndRestoreFrame(aState, aContent, aNewFrame, aScrolledFrame); FinishBuildingScrollFrame(aNewFrame, aScrolledFrame); } const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindDisplayData(const nsStyleDisplay& aDisplay, const StyleMozBoxLayout aMozBoxLayout, const Element& aElement) { static_assert(eParentTypeCount < (1 << (32 - FCDATA_PARENT_TYPE_OFFSET)), "Check eParentTypeCount should not overflow"); // The style system ensures that floated and positioned frames are // block-level. NS_ASSERTION( !(aDisplay.IsFloatingStyle() || aDisplay.IsAbsolutelyPositionedStyle()) || aDisplay.IsBlockOutsideStyle() || aDisplay.DisplayOutside() == StyleDisplayOutside::XUL, "Style system did not apply CSS2.1 section 9.7 fixups"); // If this is "body", try propagating its scroll style to the viewport // Note that we need to do this even if the body is NOT scrollable; // it might have dynamically changed from scrollable to not scrollable, // and that might need to be propagated. // XXXbz is this the right place to do this? If this code moves, // make this function static. bool propagatedScrollToViewport = false; if (aElement.IsHTMLElement(nsGkAtoms::body)) { if (nsPresContext* presContext = mPresShell->GetPresContext()) { propagatedScrollToViewport = presContext->UpdateViewportScrollStylesOverride() == &aElement; MOZ_ASSERT(!propagatedScrollToViewport || !mPresShell->GetPresContext()->IsPaginated(), "Shouldn't propagate scroll in paginated contexts"); } } switch (aDisplay.DisplayInside()) { case StyleDisplayInside::Flow: case StyleDisplayInside::FlowRoot: { if (aDisplay.IsInlineFlow()) { static constexpr FrameConstructionData data( &nsCSSFrameConstructor::ConstructInline, FCDATA_IS_INLINE | FCDATA_IS_LINE_PARTICIPANT); return &data; } // If the frame is a block-level frame and is scrollable, then wrap it in // a scroll frame. Except we don't want to do that for paginated contexts // for frames that are block-outside and aren't frames for native // anonymous stuff. // XXX Ignore tables for the time being (except caption) const uint32_t kCaptionCtorFlags = FCDATA_IS_TABLE_PART | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable); bool caption = aDisplay.mDisplay == StyleDisplay::TableCaption; bool suppressScrollFrame = false; bool needScrollFrame = aDisplay.IsScrollableOverflow() && !propagatedScrollToViewport; if (needScrollFrame) { suppressScrollFrame = mPresShell->GetPresContext()->IsPaginated() && aDisplay.IsBlockOutsideStyle() && !aElement.IsInNativeAnonymousSubtree(); if (!suppressScrollFrame) { static constexpr FrameConstructionData sScrollableBlockData[2] = { {&nsCSSFrameConstructor::ConstructScrollableBlock}, {&nsCSSFrameConstructor::ConstructScrollableBlock, kCaptionCtorFlags}}; return &sScrollableBlockData[caption]; } // If the scrollable frame would have propagated its scrolling to the // viewport, we still want to construct a regular block rather than a // scrollframe so that it paginates correctly, but we don't want to set // the bit on the block that tells it to clip at paint time. if (mPresShell->GetPresContext()->ElementWouldPropagateScrollStyles( aElement)) { suppressScrollFrame = false; } } // Handle various non-scrollable blocks. static constexpr FrameConstructionData sNonScrollableBlockData[2][2] = { {{&nsCSSFrameConstructor::ConstructNonScrollableBlock}, {&nsCSSFrameConstructor::ConstructNonScrollableBlock, kCaptionCtorFlags}}, {{&nsCSSFrameConstructor::ConstructNonScrollableBlock, FCDATA_FORCED_NON_SCROLLABLE_BLOCK}, {&nsCSSFrameConstructor::ConstructNonScrollableBlock, FCDATA_FORCED_NON_SCROLLABLE_BLOCK | kCaptionCtorFlags}}}; return &sNonScrollableBlockData[suppressScrollFrame][caption]; } case StyleDisplayInside::Table: { static constexpr FrameConstructionData data( &nsCSSFrameConstructor::ConstructTable); return &data; } // NOTE: In the unlikely event that we add another table-part here that // has a desired-parent-type (& hence triggers table fixup), we'll need to // also update the flexbox chunk in ComputedStyle::ApplyStyleFixups(). case StyleDisplayInside::TableRowGroup: { static constexpr FrameConstructionData data( &nsCSSFrameConstructor::ConstructTableRowOrRowGroup, FCDATA_IS_TABLE_PART | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable)); return &data; } case StyleDisplayInside::TableColumn: { static constexpr FrameConstructionData data( &nsCSSFrameConstructor::ConstructTableCol, FCDATA_IS_TABLE_PART | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeColGroup)); return &data; } case StyleDisplayInside::TableColumnGroup: { static constexpr FrameConstructionData data( ToCreationFunc(NS_NewTableColGroupFrame), FCDATA_IS_TABLE_PART | FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_SKIP_ABSPOS_PUSH | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable)); return &data; } case StyleDisplayInside::TableHeaderGroup: { static constexpr FrameConstructionData data( &nsCSSFrameConstructor::ConstructTableRowOrRowGroup, FCDATA_IS_TABLE_PART | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable)); return &data; } case StyleDisplayInside::TableFooterGroup: { static constexpr FrameConstructionData data( &nsCSSFrameConstructor::ConstructTableRowOrRowGroup, FCDATA_IS_TABLE_PART | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable)); return &data; } case StyleDisplayInside::TableRow: { static constexpr FrameConstructionData data( &nsCSSFrameConstructor::ConstructTableRowOrRowGroup, FCDATA_IS_TABLE_PART | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRowGroup)); return &data; } case StyleDisplayInside::TableCell: { static constexpr FrameConstructionData data( &nsCSSFrameConstructor::ConstructTableCell, FCDATA_IS_TABLE_PART | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRow)); return &data; } case StyleDisplayInside::MozBox: { if (!aElement.IsInNativeAnonymousSubtree() && aElement.OwnerDoc()->IsContentDocument()) { aElement.OwnerDoc()->WarnOnceAbout( DeprecatedOperations::eMozBoxOrInlineBoxDisplay); } // If we're emulating -moz-box with flexbox, then treat it as non-XUL and // fall through. if (aMozBoxLayout == StyleMozBoxLayout::Legacy) { static constexpr FrameConstructionData data = SCROLLABLE_ABSPOS_CONTAINER_XUL_FCDATA( ToCreationFunc(NS_NewBoxFrame)); return &data; } [[fallthrough]]; } case StyleDisplayInside::Flex: case StyleDisplayInside::WebkitBox: { static constexpr FrameConstructionData nonScrollableData( ToCreationFunc(NS_NewFlexContainerFrame)); static constexpr FrameConstructionData data( ToCreationFunc(NS_NewFlexContainerFrame), FCDATA_MAY_NEED_SCROLLFRAME); return MOZ_UNLIKELY(propagatedScrollToViewport) ? &nonScrollableData : &data; } case StyleDisplayInside::Grid: { static constexpr FrameConstructionData nonScrollableData( ToCreationFunc(NS_NewGridContainerFrame)); static constexpr FrameConstructionData data( ToCreationFunc(NS_NewGridContainerFrame), FCDATA_MAY_NEED_SCROLLFRAME); return MOZ_UNLIKELY(propagatedScrollToViewport) ? &nonScrollableData : &data; } case StyleDisplayInside::Ruby: { static constexpr FrameConstructionData data[] = { {&nsCSSFrameConstructor::ConstructBlockRubyFrame, FCDATA_MAY_NEED_SCROLLFRAME}, {ToCreationFunc(NS_NewRubyFrame), FCDATA_IS_LINE_PARTICIPANT}}; bool isInline = aDisplay.DisplayOutside() == StyleDisplayOutside::Inline; return &data[isInline]; } case StyleDisplayInside::RubyBase: { static constexpr FrameConstructionData data( ToCreationFunc(NS_NewRubyBaseFrame), FCDATA_IS_LINE_PARTICIPANT | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRubyBaseContainer)); return &data; } case StyleDisplayInside::RubyBaseContainer: { static constexpr FrameConstructionData data( ToCreationFunc(NS_NewRubyBaseContainerFrame), FCDATA_IS_LINE_PARTICIPANT | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRuby)); return &data; } case StyleDisplayInside::RubyText: { static constexpr FrameConstructionData data( ToCreationFunc(NS_NewRubyTextFrame), FCDATA_IS_LINE_PARTICIPANT | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRubyTextContainer)); return &data; } case StyleDisplayInside::RubyTextContainer: { static constexpr FrameConstructionData data( ToCreationFunc(NS_NewRubyTextContainerFrame), FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRuby)); return &data; } default: MOZ_ASSERT_UNREACHABLE("unknown 'display' value"); return nullptr; } } nsIFrame* nsCSSFrameConstructor::ConstructScrollableBlock( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay, nsFrameList& aFrameList) { nsIContent* const content = aItem.mContent; ComputedStyle* const computedStyle = aItem.mComputedStyle; nsContainerFrame* newFrame = nullptr; RefPtr scrolledContentStyle = BeginBuildingScrollFrame( aState, content, computedStyle, aState.GetGeometricParent(*aDisplay, aParentFrame), PseudoStyleType::scrolledContent, false, newFrame); // Create our block frame // pass a temporary stylecontext, the correct one will be set later nsContainerFrame* scrolledFrame = NS_NewBlockFormattingContext(mPresShell, computedStyle); // Make sure to AddChild before we call ConstructBlock so that we // end up before our descendants in fixed-pos lists as needed. aState.AddChild(newFrame, aFrameList, content, aParentFrame); nsFrameList blockList; ConstructBlock( aState, content, newFrame, newFrame, scrolledContentStyle, &scrolledFrame, blockList, aDisplay->IsAbsPosContainingBlock(newFrame) ? newFrame : nullptr); MOZ_ASSERT(blockList.OnlyChild() == scrolledFrame, "Scrollframe's frameList should be exactly the scrolled frame!"); FinishBuildingScrollFrame(newFrame, scrolledFrame); return newFrame; } nsIFrame* nsCSSFrameConstructor::ConstructNonScrollableBlock( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay, nsFrameList& aFrameList) { ComputedStyle* const computedStyle = aItem.mComputedStyle; // We want a block formatting context root in paginated contexts for // every block that would be scrollable in a non-paginated context. // We mark our blocks with a bit here if this condition is true, so // we can check it later in nsIFrame::ApplyPaginatedOverflowClipping. bool clipPaginatedOverflow = (aItem.mFCData->mBits & FCDATA_FORCED_NON_SCROLLABLE_BLOCK) != 0; nsFrameState flags = nsFrameState(0); if ((aDisplay->IsAbsolutelyPositionedStyle() || aDisplay->IsFloatingStyle() || aDisplay->DisplayInside() == StyleDisplayInside::FlowRoot || clipPaginatedOverflow) && !SVGUtils::IsInSVGTextSubtree(aParentFrame)) { flags = NS_BLOCK_FORMATTING_CONTEXT_STATE_BITS; if (clipPaginatedOverflow) { flags |= NS_BLOCK_CLIP_PAGINATED_OVERFLOW; } } nsContainerFrame* newFrame = NS_NewBlockFrame(mPresShell, computedStyle); newFrame->AddStateBits(flags); ConstructBlock( aState, aItem.mContent, aState.GetGeometricParent(*aDisplay, aParentFrame), aParentFrame, computedStyle, &newFrame, aFrameList, aDisplay->IsAbsPosContainingBlock(newFrame) ? newFrame : nullptr); return newFrame; } void nsCSSFrameConstructor::InitAndRestoreFrame( const nsFrameConstructorState& aState, nsIContent* aContent, nsContainerFrame* aParentFrame, nsIFrame* aNewFrame, bool aAllowCounters) { MOZ_ASSERT(aNewFrame, "Null frame cannot be initialized"); // Initialize the frame aNewFrame->Init(aContent, aParentFrame, nullptr); aNewFrame->AddStateBits(aState.mAdditionalStateBits); if (aState.mFrameState) { // Restore frame state for just the newly created frame. RestoreFrameStateFor(aNewFrame, aState.mFrameState); } if (aAllowCounters && mContainStyleScopeManager.AddCounterChanges(aNewFrame)) { CountersDirty(); } } already_AddRefed nsCSSFrameConstructor::ResolveComputedStyle( nsIContent* aContent) { if (auto* element = Element::FromNode(aContent)) { return ServoStyleSet::ResolveServoStyle(*element); } MOZ_ASSERT(aContent->IsText(), "shouldn't waste time creating ComputedStyles for " "comments and processing instructions"); Element* parent = aContent->GetFlattenedTreeParentElement(); MOZ_ASSERT(parent, "Text out of the flattened tree?"); // FIXME(emilio): The const_cast is unfortunate, but it's not worse than what // we did before. // // We could use ResolveServoStyle, but that would involve extra unnecessary // refcount traffic... auto* parentStyle = const_cast(Servo_Element_GetMaybeOutOfDateStyle(parent)); MOZ_ASSERT(parentStyle, "How are we inserting text frames in an unstyled element?"); return mPresShell->StyleSet()->ResolveStyleForText(aContent, parentStyle); } // MathML Mod - RBS void nsCSSFrameConstructor::FlushAccumulatedBlock( nsFrameConstructorState& aState, nsIContent* aContent, nsContainerFrame* aParentFrame, nsFrameList& aBlockList, nsFrameList& aNewList) { if (aBlockList.IsEmpty()) { // Nothing to do return; } auto anonPseudo = PseudoStyleType::mozMathMLAnonymousBlock; ComputedStyle* parentContext = nsIFrame::CorrectStyleParentFrame(aParentFrame, anonPseudo)->Style(); ServoStyleSet* styleSet = mPresShell->StyleSet(); RefPtr blockContext = styleSet->ResolveInheritingAnonymousBoxStyle(anonPseudo, parentContext); // then, create a block frame that will wrap the child frames. Make it a // MathML frame so that Get(Absolute/Float)ContainingBlockFor know that this // is not a suitable block. nsContainerFrame* blockFrame = NS_NewMathMLmathBlockFrame(mPresShell, blockContext); InitAndRestoreFrame(aState, aContent, aParentFrame, blockFrame); ReparentFrames(this, blockFrame, aBlockList, false); // We have to walk over aBlockList before we hand it over to blockFrame. for (nsIFrame* f : aBlockList) { f->SetParentIsWrapperAnonBox(); } // abs-pos and floats are disabled in MathML children so we don't have to // worry about messing up those. blockFrame->SetInitialChildList(FrameChildListID::Principal, std::move(aBlockList)); aNewList.AppendFrame(nullptr, blockFrame); } // Only elements can be floated or positioned. All other MathML // should be in-flow. #define SIMPLE_MATHML_CREATE(_tag, _func) \ { \ nsGkAtoms::_tag, { \ _func, FCDATA_DISALLOW_OUT_OF_FLOW | \ FCDATA_FORCE_NULL_ABSPOS_CONTAINER | \ FCDATA_WRAP_KIDS_IN_BLOCKS \ } \ } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindMathMLData(const Element& aElement, ComputedStyle& aStyle) { MOZ_ASSERT(aElement.IsMathMLElement()); nsAtom* tag = aElement.NodeInfo()->NameAtom(); // Handle specially, because it sometimes produces inlines if (tag == nsGkAtoms::math) { // The IsBlockOutsideStyle() check must match what // specified::Display::equivalent_block_display is checking for // already-block-outside things. Though the behavior here for the // display:table case is pretty weird... if (aStyle.StyleDisplay()->IsBlockOutsideStyle()) { static constexpr FrameConstructionData sBlockMathData( ToCreationFunc(NS_NewMathMLmathBlockFrame), FCDATA_FORCE_NULL_ABSPOS_CONTAINER | FCDATA_WRAP_KIDS_IN_BLOCKS); return &sBlockMathData; } static constexpr FrameConstructionData sInlineMathData( ToCreationFunc(NS_NewMathMLmathInlineFrame), FCDATA_FORCE_NULL_ABSPOS_CONTAINER | FCDATA_IS_LINE_PARTICIPANT | FCDATA_WRAP_KIDS_IN_BLOCKS); return &sInlineMathData; } if (!StaticPrefs:: mathml_legacy_maction_and_semantics_implementations_disabled()) { static constexpr FrameConstructionDataByTag sMactionAndSemanticsData[] = { SIMPLE_MATHML_CREATE(maction_, NS_NewMathMLmactionFrame), SIMPLE_MATHML_CREATE(semantics_, NS_NewMathMLsemanticsFrame)}; const FrameConstructionData* data = FindDataByTag(aElement, aStyle, sMactionAndSemanticsData, ArrayLength(sMactionAndSemanticsData)); if (data) { return data; } } static constexpr FrameConstructionDataByTag sMathMLData[] = { SIMPLE_MATHML_CREATE(annotation_, NS_NewMathMLTokenFrame), SIMPLE_MATHML_CREATE(annotation_xml_, NS_NewMathMLmrowFrame), SIMPLE_MATHML_CREATE(mi_, NS_NewMathMLTokenFrame), SIMPLE_MATHML_CREATE(mn_, NS_NewMathMLTokenFrame), SIMPLE_MATHML_CREATE(ms_, NS_NewMathMLTokenFrame), SIMPLE_MATHML_CREATE(mtext_, NS_NewMathMLTokenFrame), SIMPLE_MATHML_CREATE(mo_, NS_NewMathMLmoFrame), SIMPLE_MATHML_CREATE(mfrac_, NS_NewMathMLmfracFrame), SIMPLE_MATHML_CREATE(msup_, NS_NewMathMLmmultiscriptsFrame), SIMPLE_MATHML_CREATE(msub_, NS_NewMathMLmmultiscriptsFrame), SIMPLE_MATHML_CREATE(msubsup_, NS_NewMathMLmmultiscriptsFrame), SIMPLE_MATHML_CREATE(munder_, NS_NewMathMLmunderoverFrame), SIMPLE_MATHML_CREATE(mover_, NS_NewMathMLmunderoverFrame), SIMPLE_MATHML_CREATE(munderover_, NS_NewMathMLmunderoverFrame), SIMPLE_MATHML_CREATE(mphantom_, NS_NewMathMLmrowFrame), SIMPLE_MATHML_CREATE(mpadded_, NS_NewMathMLmpaddedFrame), SIMPLE_MATHML_CREATE(mspace_, NS_NewMathMLmspaceFrame), SIMPLE_MATHML_CREATE(none, NS_NewMathMLmrowFrame), SIMPLE_MATHML_CREATE(mprescripts_, NS_NewMathMLmrowFrame), SIMPLE_MATHML_CREATE(mfenced_, NS_NewMathMLmrowFrame), SIMPLE_MATHML_CREATE(mmultiscripts_, NS_NewMathMLmmultiscriptsFrame), SIMPLE_MATHML_CREATE(mstyle_, NS_NewMathMLmrowFrame), SIMPLE_MATHML_CREATE(msqrt_, NS_NewMathMLmsqrtFrame), SIMPLE_MATHML_CREATE(mroot_, NS_NewMathMLmrootFrame), SIMPLE_MATHML_CREATE(maction_, NS_NewMathMLmrowFrame), SIMPLE_MATHML_CREATE(mrow_, NS_NewMathMLmrowFrame), SIMPLE_MATHML_CREATE(merror_, NS_NewMathMLmrowFrame), SIMPLE_MATHML_CREATE(menclose_, NS_NewMathMLmencloseFrame), SIMPLE_MATHML_CREATE(semantics_, NS_NewMathMLmrowFrame)}; return FindDataByTag(aElement, aStyle, sMathMLData, ArrayLength(sMathMLData)); } nsContainerFrame* nsCSSFrameConstructor::ConstructFrameWithAnonymousChild( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, nsFrameList& aFrameList, ContainerFrameCreationFunc aConstructor, ContainerFrameCreationFunc aInnerConstructor, PseudoStyleType aInnerPseudo, bool aCandidateRootFrame) { nsIContent* const content = aItem.mContent; ComputedStyle* const computedStyle = aItem.mComputedStyle; // Create the outer frame: nsContainerFrame* newFrame = aConstructor(mPresShell, computedStyle); InitAndRestoreFrame(aState, content, aCandidateRootFrame ? aState.GetGeometricParent( *computedStyle->StyleDisplay(), aParentFrame) : aParentFrame, newFrame); newFrame->AddStateBits(NS_FRAME_OWNS_ANON_BOXES); // Create the pseudo SC for the anonymous wrapper child as a child of the SC: RefPtr scForAnon = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle(aInnerPseudo, computedStyle); // Create the anonymous inner wrapper frame nsContainerFrame* innerFrame = aInnerConstructor(mPresShell, scForAnon); InitAndRestoreFrame(aState, content, newFrame, innerFrame); // Put the newly created frames into the right child list SetInitialSingleChild(newFrame, innerFrame); aState.AddChild(newFrame, aFrameList, content, aParentFrame, aCandidateRootFrame, aCandidateRootFrame); if (!mRootElementFrame && aCandidateRootFrame) { // The frame we're constructing will be the root element frame. SetRootElementFrameAndConstructCanvasAnonContent(newFrame, aState, aFrameList); } nsFrameConstructorSaveState floatSaveState; aState.MaybePushFloatContainingBlock(innerFrame, floatSaveState); nsFrameList childList; // Process children if (aItem.mFCData->mBits & FCDATA_USE_CHILD_ITEMS) { ConstructFramesFromItemList( aState, aItem.mChildItems, innerFrame, aItem.mFCData->mBits & FCDATA_IS_WRAPPER_ANON_BOX, childList); } else { ProcessChildren(aState, content, computedStyle, innerFrame, true, childList, false); } // Set the inner wrapper frame's initial primary list innerFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); return newFrame; } nsIFrame* nsCSSFrameConstructor::ConstructOuterSVG( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay, nsFrameList& aFrameList) { return ConstructFrameWithAnonymousChild( aState, aItem, aParentFrame, aFrameList, NS_NewSVGOuterSVGFrame, NS_NewSVGOuterSVGAnonChildFrame, PseudoStyleType::mozSVGOuterSVGAnonChild, true); } nsIFrame* nsCSSFrameConstructor::ConstructMarker( nsFrameConstructorState& aState, FrameConstructionItem& aItem, nsContainerFrame* aParentFrame, const nsStyleDisplay* aDisplay, nsFrameList& aFrameList) { return ConstructFrameWithAnonymousChild( aState, aItem, aParentFrame, aFrameList, NS_NewSVGMarkerFrame, NS_NewSVGMarkerAnonChildFrame, PseudoStyleType::mozSVGMarkerAnonChild, false); } // Only outer elements can be floated or positioned. All other SVG // should be in-flow. #define SIMPLE_SVG_FCDATA(_func) \ FrameConstructionData(ToCreationFunc(_func), \ FCDATA_DISALLOW_OUT_OF_FLOW | \ FCDATA_SKIP_ABSPOS_PUSH | \ FCDATA_DISALLOW_GENERATED_CONTENT) #define SIMPLE_SVG_CREATE(_tag, _func) \ { nsGkAtoms::_tag, SIMPLE_SVG_FCDATA(_func) } static bool IsFilterPrimitiveChildTag(const nsAtom* aTag) { return aTag == nsGkAtoms::feDistantLight || aTag == nsGkAtoms::fePointLight || aTag == nsGkAtoms::feSpotLight || aTag == nsGkAtoms::feFuncR || aTag == nsGkAtoms::feFuncG || aTag == nsGkAtoms::feFuncB || aTag == nsGkAtoms::feFuncA || aTag == nsGkAtoms::feMergeNode; } /* static */ const nsCSSFrameConstructor::FrameConstructionData* nsCSSFrameConstructor::FindSVGData(const Element& aElement, nsIFrame* aParentFrame, bool aIsWithinSVGText, bool aAllowsTextPathChild, ComputedStyle& aStyle) { MOZ_ASSERT(aElement.IsSVGElement()); static constexpr FrameConstructionData sSuppressData = SUPPRESS_FCDATA(); static constexpr FrameConstructionData sContainerData = SIMPLE_SVG_FCDATA(NS_NewSVGContainerFrame); bool parentIsSVG = aIsWithinSVGText; nsIContent* parentContent = aParentFrame ? aParentFrame->GetContent() : nullptr; nsAtom* tag = aElement.NodeInfo()->NameAtom(); // XXXbz should this really be based on the tag of the parent frame's content? // Should it not be based on the type of the parent frame (e.g. whether it's // an SVG frame)? if (parentContent) { // It's not clear whether the SVG spec intends to allow any SVG // content within svg:foreignObject at all (SVG 1.1, section // 23.2), but if it does, it better be svg:svg. So given that // we're allowing it, treat it as a non-SVG parent. parentIsSVG = parentContent->IsSVGElement() && parentContent->NodeInfo()->NameAtom() != nsGkAtoms::foreignObject; } if ((tag != nsGkAtoms::svg && !parentIsSVG) || (tag == nsGkAtoms::desc || tag == nsGkAtoms::title || tag == nsGkAtoms::metadata)) { // Sections 5.1 and G.4 of SVG 1.1 say that SVG elements other than // svg:svg not contained within svg:svg are incorrect, although they // don't seem to specify error handling. Ignore them, since many of // our frame classes can't deal. It *may* be that the document // should at that point be considered in error according to F.2, but // it's hard to tell. // // Style mutation can't change this situation, so don't bother // adding to the undisplayed content map. // // We don't currently handle any UI for desc/title/metadata return &sSuppressData; } // We don't need frames for animation elements if (nsCOMPtr animationElement = do_QueryInterface(const_cast(&aElement))) { return &sSuppressData; } if (tag == nsGkAtoms::svg && !parentIsSVG) { // We need outer elements to have an SVGOuterSVGFrame regardless // of whether they fail conditional processing attributes, since various // SVG frames assume that one exists. We handle the non-rendering // of failing outer element contents like statements, // and do the PassesConditionalProcessingTests call in // SVGOuterSVGFrame::Init. static constexpr FrameConstructionData sOuterSVGData( &nsCSSFrameConstructor::ConstructOuterSVG); return &sOuterSVGData; } if (tag == nsGkAtoms::marker) { static constexpr FrameConstructionData sMarkerSVGData( &nsCSSFrameConstructor::ConstructMarker); return &sMarkerSVGData; } nsCOMPtr tests = do_QueryInterface(const_cast(&aElement)); if (tests && !tests->PassesConditionalProcessingTests()) { // Elements with failing conditional processing attributes never get // rendered. Note that this is not where we select which frame in a // to render! That happens in SVGSwitchFrame::PaintSVG. if (aIsWithinSVGText) { // SVGTextFrame doesn't handle conditional processing attributes, // so don't create frames for descendants of with failing // attributes. We need frames not to be created so that text layout // is correct. return &sSuppressData; } // If we're not inside , create an SVGContainerFrame (which is a // frame that doesn't render) so that paint servers can still be referenced, // even if they live inside an element with failing conditional processing // attributes. return &sContainerData; } // Ensure that a stop frame is a child of a gradient and that gradients // can only have stop children. bool parentIsGradient = aParentFrame && static_cast( do_QueryFrame(aParentFrame)); bool stop = (tag == nsGkAtoms::stop); if ((parentIsGradient && !stop) || (!parentIsGradient && stop)) { return &sSuppressData; } // Prevent bad frame types being children of filters or parents of filter // primitives. If aParentFrame is null, we know that the frame that will // be created will be an nsInlineFrame, so it can never be a filter. bool parentIsFilter = aParentFrame && aParentFrame->IsSVGFilterFrame(); nsCOMPtr filterPrimitive = do_QueryInterface(const_cast(&aElement)); if ((parentIsFilter && !filterPrimitive) || (!parentIsFilter && filterPrimitive)) { return &sSuppressData; } // Prevent bad frame types being children of filter primitives or parents of // filter primitive children. If aParentFrame is null, we know that the frame // that will be created will be an nsInlineFrame, so it can never be a filter // primitive. bool parentIsFEContainerFrame = aParentFrame && aParentFrame->IsSVGFEContainerFrame(); if ((parentIsFEContainerFrame && !IsFilterPrimitiveChildTag(tag)) || (!parentIsFEContainerFrame && IsFilterPrimitiveChildTag(tag))) { return &sSuppressData; } // Special cases for text/tspan/textPath, because the kind of frame // they get depends on the parent frame. We ignore 'a' elements when // determining the parent, however. if (aIsWithinSVGText) { // If aIsWithinSVGText is true, then we know that the "SVG text uses // CSS frames" pref was true when this SVG fragment was first constructed. // // FIXME(bug 1588477) Don't render stuff in display: contents / Shadow DOM // subtrees, because TextCorrespondenceRecorder in the SVG text code doesn't // really know how to deal with it. This kinda sucks. :( if (aParentFrame && aParentFrame->GetContent() != aElement.GetParent()) { return &sSuppressData; } // We don't use ConstructInline because we want different behavior // for generated content. static constexpr FrameConstructionData sTSpanData( ToCreationFunc(NS_NewInlineFrame), FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_SKIP_ABSPOS_PUSH | FCDATA_DISALLOW_GENERATED_CONTENT | FCDATA_IS_LINE_PARTICIPANT | FCDATA_IS_INLINE | FCDATA_USE_CHILD_ITEMS); if (tag == nsGkAtoms::textPath) { if (aAllowsTextPathChild) { return &sTSpanData; } } else if (tag == nsGkAtoms::tspan || tag == nsGkAtoms::a) { return &sTSpanData; } return &sSuppressData; } else if (tag == nsGkAtoms::tspan || tag == nsGkAtoms::textPath) { return &sSuppressData; } static constexpr FrameConstructionDataByTag sSVGData[] = { SIMPLE_SVG_CREATE(svg, NS_NewSVGInnerSVGFrame), SIMPLE_SVG_CREATE(g, NS_NewSVGGFrame), SIMPLE_SVG_CREATE(svgSwitch, NS_NewSVGSwitchFrame), SIMPLE_SVG_CREATE(symbol, NS_NewSVGSymbolFrame), SIMPLE_SVG_CREATE(polygon, NS_NewSVGGeometryFrame), SIMPLE_SVG_CREATE(polyline, NS_NewSVGGeometryFrame), SIMPLE_SVG_CREATE(circle, NS_NewSVGGeometryFrame), SIMPLE_SVG_CREATE(ellipse, NS_NewSVGGeometryFrame), SIMPLE_SVG_CREATE(line, NS_NewSVGGeometryFrame), SIMPLE_SVG_CREATE(rect, NS_NewSVGGeometryFrame), SIMPLE_SVG_CREATE(path, NS_NewSVGGeometryFrame), SIMPLE_SVG_CREATE(defs, NS_NewSVGContainerFrame), {nsGkAtoms::text, {NS_NewSVGTextFrame, FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_ALLOW_BLOCK_STYLES, PseudoStyleType::mozSVGText}}, {nsGkAtoms::foreignObject, {ToCreationFunc(NS_NewSVGForeignObjectFrame), FCDATA_DISALLOW_OUT_OF_FLOW, PseudoStyleType::mozSVGForeignContent}}, SIMPLE_SVG_CREATE(a, NS_NewSVGAFrame), SIMPLE_SVG_CREATE(linearGradient, NS_NewSVGLinearGradientFrame), SIMPLE_SVG_CREATE(radialGradient, NS_NewSVGRadialGradientFrame), SIMPLE_SVG_CREATE(stop, NS_NewSVGStopFrame), SIMPLE_SVG_CREATE(use, NS_NewSVGUseFrame), SIMPLE_SVG_CREATE(view, NS_NewSVGViewFrame), SIMPLE_SVG_CREATE(image, NS_NewSVGImageFrame), SIMPLE_SVG_CREATE(clipPath, NS_NewSVGClipPathFrame), SIMPLE_SVG_CREATE(filter, NS_NewSVGFilterFrame), SIMPLE_SVG_CREATE(pattern, NS_NewSVGPatternFrame), SIMPLE_SVG_CREATE(mask, NS_NewSVGMaskFrame), SIMPLE_SVG_CREATE(feDistantLight, NS_NewSVGFEUnstyledLeafFrame), SIMPLE_SVG_CREATE(fePointLight, NS_NewSVGFEUnstyledLeafFrame), SIMPLE_SVG_CREATE(feSpotLight, NS_NewSVGFEUnstyledLeafFrame), SIMPLE_SVG_CREATE(feBlend, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feColorMatrix, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feFuncR, NS_NewSVGFEUnstyledLeafFrame), SIMPLE_SVG_CREATE(feFuncG, NS_NewSVGFEUnstyledLeafFrame), SIMPLE_SVG_CREATE(feFuncB, NS_NewSVGFEUnstyledLeafFrame), SIMPLE_SVG_CREATE(feFuncA, NS_NewSVGFEUnstyledLeafFrame), SIMPLE_SVG_CREATE(feComposite, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feComponentTransfer, NS_NewSVGFEContainerFrame), SIMPLE_SVG_CREATE(feConvolveMatrix, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feDiffuseLighting, NS_NewSVGFEContainerFrame), SIMPLE_SVG_CREATE(feDisplacementMap, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feDropShadow, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feFlood, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feGaussianBlur, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feImage, NS_NewSVGFEImageFrame), SIMPLE_SVG_CREATE(feMerge, NS_NewSVGFEContainerFrame), SIMPLE_SVG_CREATE(feMergeNode, NS_NewSVGFEUnstyledLeafFrame), SIMPLE_SVG_CREATE(feMorphology, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feOffset, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feSpecularLighting, NS_NewSVGFEContainerFrame), SIMPLE_SVG_CREATE(feTile, NS_NewSVGFELeafFrame), SIMPLE_SVG_CREATE(feTurbulence, NS_NewSVGFELeafFrame)}; const FrameConstructionData* data = FindDataByTag(aElement, aStyle, sSVGData, ArrayLength(sSVGData)); if (!data) { data = &sContainerData; } return data; } void nsCSSFrameConstructor::InsertPageBreakItem( nsIContent* aContent, FrameConstructionItemList& aItems, InsertPageBreakLocation location) { RefPtr pseudoStyle = mPresShell->StyleSet()->ResolveNonInheritingAnonymousBoxStyle( PseudoStyleType::pageBreak); MOZ_ASSERT(pseudoStyle->StyleDisplay()->mDisplay == StyleDisplay::Block, "Unexpected display"); static constexpr FrameConstructionData sPageBreakData(NS_NewPageBreakFrame, FCDATA_SKIP_FRAMESET); if (location == InsertPageBreakLocation::eBefore) { aItems.PrependItem(this, &sPageBreakData, aContent, pseudoStyle.forget(), true); } else { aItems.AppendItem(this, &sPageBreakData, aContent, pseudoStyle.forget(), true); } } bool nsCSSFrameConstructor::ShouldCreateItemsForChild( nsFrameConstructorState& aState, nsIContent* aContent, nsContainerFrame* aParentFrame) { aContent->UnsetFlags(NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME); // XXX the GetContent() != aContent check is needed due to bug 135040. // Remove it once that's fixed. if (aContent->GetPrimaryFrame() && aContent->GetPrimaryFrame()->GetContent() == aContent && !aState.mCreatingExtraFrames) { MOZ_ASSERT(false, "asked to create frame construction item for a node that " "already has a frame"); return false; } // don't create a whitespace frame if aParent doesn't want it if (!NeedFrameFor(aState, aParentFrame, aContent)) { return false; } // never create frames for comments or PIs if (aContent->IsComment() || aContent->IsProcessingInstruction()) { return false; } return true; } void nsCSSFrameConstructor::AddFrameConstructionItems( nsFrameConstructorState& aState, nsIContent* aContent, bool aSuppressWhiteSpaceOptimizations, const ComputedStyle& aParentStyle, const InsertionPoint& aInsertion, FrameConstructionItemList& aItems, ItemFlags aFlags) { nsContainerFrame* parentFrame = aInsertion.mParentFrame; if (!ShouldCreateItemsForChild(aState, aContent, parentFrame)) { return; } if (MOZ_UNLIKELY(aParentStyle.StyleContent()->mContent.IsNone()) && StaticPrefs::layout_css_element_content_none_enabled()) { return; } RefPtr computedStyle = ResolveComputedStyle(aContent); auto flags = aFlags + ItemFlag::AllowPageBreak; if (parentFrame) { if (SVGUtils::IsInSVGTextSubtree(parentFrame)) { flags += ItemFlag::IsWithinSVGText; } if (parentFrame->IsBlockFrame() && parentFrame->GetParent() && parentFrame->GetParent()->IsSVGTextFrame()) { flags += ItemFlag::AllowTextPathChild; } } AddFrameConstructionItemsInternal(aState, aContent, parentFrame, aSuppressWhiteSpaceOptimizations, computedStyle, flags, aItems); } // Whether we should suppress frames for a child under a
so no need to check it // // FIXME(emilio): A display: contents sibling could! Write a test-case and // fix. items.SetLineBoundaryAtEnd(!nextSibling || !nextSibling->IsInlineOutside()); } // To suppress whitespace-only text frames, we have to verify that // our container's DOM child list matches its flattened tree child list. items.SetParentHasNoShadowDOM(haveNoShadowDOM); nsFrameConstructorSaveState floatSaveState; state.MaybePushFloatContainingBlock(parentFrame, floatSaveState); nsFrameList frameList; ConstructFramesFromItemList(state, items, parentFrame, ParentIsWrapperAnonBox(parentFrame), frameList); for (nsIContent* child = aFirstNewContent; child; child = child->GetNextSibling()) { // Invalidate now instead of before the WipeContainingBlock call, just in // case we do wipe; in that case we don't need to do this walk at all. // XXXbz does that matter? Would it make more sense to save some virtual // GetChildAt_Deprecated calls instead and do this during construction of // our FrameConstructionItemList? InvalidateCanvasIfNeeded(mPresShell, child); } // If the container is a table and a caption was appended, it needs to be put // in the table wrapper frame's additional child list. nsFrameList captionList; if (LayoutFrameType::Table == frameType) { // Pull out the captions. Note that we don't want to do that as we go, // because processing a single caption can add a whole bunch of things to // the frame items due to pseudoframe processing. So we'd have to pull // captions from a list anyway; might as well do that here. // XXXbz this is no longer true; we could pull captions directly out of the // FrameConstructionItemList now. PullOutCaptionFrames(frameList, captionList); } if (haveFirstLineStyle && parentFrame == containingBlock) { // It's possible that some of the new frames go into a // first-line frame. Look at them and see... AppendFirstLineFrames(state, containingBlock->GetContent(), containingBlock, frameList); // That moved things into line frames as needed, reparenting their // styles. Nothing else needs to be done. } else if (parentFrame->Style()->HasPseudoElementData()) { // parentFrame might be inside a ::first-line frame. Check whether it is, // and if so fix up our styles. CheckForFirstLineInsertion(parentFrame, frameList); CheckForFirstLineInsertion(parentFrame, captionList); } // Notify the parent frame passing it the list of new frames // Append the flowed frames to the principal child list; captions // need special treatment if (captionList.NotEmpty()) { // append the caption to the table wrapper NS_ASSERTION(LayoutFrameType::Table == frameType, "how did that happen?"); nsContainerFrame* outerTable = parentFrame->GetParent(); captionList.ApplySetParent(outerTable); AppendFrames(outerTable, FrameChildListID::Caption, std::move(captionList)); } LAYOUT_PHASE_TEMP_EXIT(); if (MaybeRecreateForColumnSpan(state, parentFrame, frameList, prevSibling)) { LAYOUT_PHASE_TEMP_REENTER(); return; } LAYOUT_PHASE_TEMP_REENTER(); if (frameList.NotEmpty()) { // append the in-flow kids AppendFramesToParent(state, parentFrame, frameList, prevSibling); } // Recover first-letter frames if (haveFirstLetterStyle) { RecoverLetterFrames(containingBlock); } #ifdef DEBUG if (gReallyNoisyContentUpdates) { printf("nsCSSFrameConstructor::ContentAppended: resulting frame model:\n"); parentFrame->List(stdout); } #endif #ifdef ACCESSIBILITY if (nsAccessibilityService* accService = GetAccService()) { accService->ContentRangeInserted(mPresShell, aFirstNewContent, nullptr); } #endif } void nsCSSFrameConstructor::ContentInserted(nsIContent* aChild, InsertionKind aInsertionKind) { ContentRangeInserted(aChild, aChild->GetNextSibling(), aInsertionKind); } // ContentRangeInserted handles creating frames for a range of nodes that // aren't at the end of their childlist. ContentRangeInserted isn't a real // content notification, but rather it handles regular ContentInserted calls // for a single node as well as the lazy construction of frames for a range of // nodes when called from CreateNeededFrames. For a range of nodes to be // suitable to have its frames constructed all at once they must meet the same // conditions that ContentAppended imposes (GetRangeInsertionPoint checks // these), plus more. Namely when finding the insertion prevsibling we must not // need to consult something specific to any one node in the range, so that the // insertion prevsibling would be the same for each node in the range. So we // pass the first node in the range to GetInsertionPrevSibling, and if // IsValidSibling (the only place GetInsertionPrevSibling might look at the // passed in node itself) needs to resolve style on the node we record this and // return that this range needs to be split up and inserted separately. Table // captions need extra attention as we need to determine where to insert them // in the caption list, while skipping any nodes in the range being inserted // (because when we treat the caption frames the other nodes have had their // frames constructed but not yet inserted into the frame tree). void nsCSSFrameConstructor::ContentRangeInserted(nsIContent* aStartChild, nsIContent* aEndChild, InsertionKind aInsertionKind) { MOZ_ASSERT(aInsertionKind == InsertionKind::Sync || !RestyleManager()->IsInStyleRefresh()); AUTO_PROFILER_LABEL("nsCSSFrameConstructor::ContentRangeInserted", LAYOUT_FrameConstruction); AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC); MOZ_ASSERT(aStartChild, "must always pass a child"); #ifdef DEBUG if (gNoisyContentUpdates) { printf( "nsCSSFrameConstructor::ContentRangeInserted container=%p " "start-child=%p end-child=%p lazy=%d\n", aStartChild->GetParent(), aStartChild, aEndChild, aInsertionKind == InsertionKind::Async); if (gReallyNoisyContentUpdates) { if (aStartChild->GetParent()) { aStartChild->GetParent()->List(stdout, 0); } else { aStartChild->List(stdout, 0); } } } for (nsIContent* child = aStartChild; child != aEndChild; child = child->GetNextSibling()) { // XXX the GetContent() != child check is needed due to bug 135040. // Remove it once that's fixed. NS_ASSERTION( !child->GetPrimaryFrame() || child->GetPrimaryFrame()->GetContent() != child, "asked to construct a frame for a node that already has a frame"); } #endif bool isSingleInsert = (aStartChild->GetNextSibling() == aEndChild); NS_ASSERTION(isSingleInsert || aInsertionKind == InsertionKind::Sync, "range insert shouldn't be lazy"); NS_ASSERTION(isSingleInsert || aEndChild, "range should not include all nodes after aStartChild"); // If we have a null parent, then this must be the document element being // inserted, or some other child of the document in the DOM (might be a PI, // say). if (!aStartChild->GetParent()) { MOZ_ASSERT(isSingleInsert, "root node insertion should be a single insertion"); Element* docElement = mDocument->GetRootElement(); if (aStartChild != docElement) { // Not the root element; just bail out return; } MOZ_ASSERT(!mRootElementFrame, "root element frame already created"); // Create frames for the document element and its child elements if (ConstructDocElementFrame(docElement)) { InvalidateCanvasIfNeeded(mPresShell, aStartChild); #ifdef DEBUG if (gReallyNoisyContentUpdates) { printf( "nsCSSFrameConstructor::ContentRangeInserted: resulting frame " "model:\n"); mRootElementFrame->List(stdout); } #endif } #ifdef ACCESSIBILITY if (nsAccessibilityService* accService = GetAccService()) { accService->ContentRangeInserted(mPresShell, aStartChild, aEndChild); } #endif return; } InsertionPoint insertion; if (isSingleInsert) { // See if we have a Shadow DOM insertion point. If so, then that's our real // parent frame; if not, then the frame hasn't been built yet and we just // bail. insertion = GetInsertionPoint(aStartChild); } else { // Get our insertion point. If we need to issue single ContentInserteds // GetRangeInsertionPoint will take care of that for us. LAYOUT_PHASE_TEMP_EXIT(); insertion = GetRangeInsertionPoint(aStartChild, aEndChild, aInsertionKind); LAYOUT_PHASE_TEMP_REENTER(); } if (!insertion.mParentFrame) { // We're punting on frame construction because there's no container frame. // The Servo-backed style system handles this case like the lazy frame // construction case, except when we're already constructing frames, in // which case we shouldn't need to do anything else. if (aInsertionKind == InsertionKind::Async) { LazilyStyleNewChildRange(aStartChild, aEndChild); } return; } if (aInsertionKind == InsertionKind::Async) { ConstructLazily(CONTENTINSERT, aStartChild); LazilyStyleNewChildRange(aStartChild, aEndChild); return; } bool isAppend, isRangeInsertSafe; nsIFrame* prevSibling = GetInsertionPrevSibling( &insertion, aStartChild, &isAppend, &isRangeInsertSafe); // check if range insert is safe if (!isSingleInsert && !isRangeInsertSafe) { // must fall back to a single ContertInserted for each child in the range LAYOUT_PHASE_TEMP_EXIT(); IssueSingleInsertNofications(aStartChild, aEndChild, InsertionKind::Sync); LAYOUT_PHASE_TEMP_REENTER(); return; } LayoutFrameType frameType = insertion.mParentFrame->Type(); LAYOUT_PHASE_TEMP_EXIT(); if (MaybeRecreateForFrameset(insertion.mParentFrame, aStartChild, aEndChild)) { LAYOUT_PHASE_TEMP_REENTER(); return; } LAYOUT_PHASE_TEMP_REENTER(); // We should only get here with fieldsets when doing a single insert, because // fieldsets have multiple insertion points. NS_ASSERTION(isSingleInsert || frameType != LayoutFrameType::FieldSet, "Unexpected parent"); // Note that this check is insufficient if aStartChild is not a legend with // display::contents that contains a legend. We'll catch that case in // WipeContainingBlock. (That code would also catch this case, but handling // this early is slightly faster.) // XXXmats we should be able to optimize this when the fieldset doesn't // currently have a rendered legend. ContentRangeInserted needs to be fixed // to use the inner frame as the content insertion frame in that case. if (GetFieldSetFrameFor(insertion.mParentFrame) && aStartChild->NodeInfo()->NameAtom() == nsGkAtoms::legend) { // Just reframe the parent, since figuring out whether this // should be the new legend and then handling it is too complex. // We could do a little better here --- check if the fieldset already // has a legend which occurs earlier in its child list than this node, // and if so, proceed. But we'd have to extend nsFieldSetFrame // to locate this legend in the inserted frames and extract it. LAYOUT_PHASE_TEMP_EXIT(); RecreateFramesForContent(insertion.mParentFrame->GetContent(), InsertionKind::Async); LAYOUT_PHASE_TEMP_REENTER(); return; } // Don't construct kids of leaves if (insertion.mParentFrame->IsLeaf()) { // Clear lazy bits so we don't try to construct again. ClearLazyBits(aStartChild, aEndChild); return; } LAYOUT_PHASE_TEMP_EXIT(); if (WipeInsertionParent(insertion.mParentFrame)) { LAYOUT_PHASE_TEMP_REENTER(); return; } LAYOUT_PHASE_TEMP_REENTER(); nsFrameConstructorState state( mPresShell, GetAbsoluteContainingBlock(insertion.mParentFrame, FIXED_POS), GetAbsoluteContainingBlock(insertion.mParentFrame, ABS_POS), GetFloatContainingBlock(insertion.mParentFrame), do_AddRef(mFrameTreeState)); // Recover state for the containing block - we need to know if // it has :first-letter or :first-line style applied to it. The // reason we care is that the internal structure in these cases // is not the normal structure and requires custom updating // logic. nsContainerFrame* containingBlock = state.mFloatedList.mContainingBlock; bool haveFirstLetterStyle = false; bool haveFirstLineStyle = false; // In order to shave off some cycles, we only dig up the // containing block haveFirst* flags if the parent frame where // the insertion/append is occurring is an inline or block // container. For other types of containers this isn't relevant. StyleDisplayInside parentDisplayInside = insertion.mParentFrame->StyleDisplay()->DisplayInside(); // Examine the insertion.mParentFrame where the insertion is taking // place. If it's a certain kind of container then some special // processing is done. if (StyleDisplayInside::Flow == parentDisplayInside) { // Recover the special style flags for the containing block if (containingBlock) { haveFirstLetterStyle = HasFirstLetterStyle(containingBlock); haveFirstLineStyle = ShouldHaveFirstLineStyle( containingBlock->GetContent(), containingBlock->Style()); } if (haveFirstLetterStyle) { // If our current insertion.mParentFrame is a Letter frame, use its parent // as our new parent hint if (insertion.mParentFrame->IsLetterFrame()) { // If insertion.mParentFrame is out of flow, then we actually want the // parent of the placeholder frame. if (insertion.mParentFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) { nsPlaceholderFrame* placeholderFrame = insertion.mParentFrame->GetPlaceholderFrame(); NS_ASSERTION(placeholderFrame, "No placeholder for out-of-flow?"); insertion.mParentFrame = placeholderFrame->GetParent(); } else { insertion.mParentFrame = insertion.mParentFrame->GetParent(); } } // Remove the old letter frames before doing the insertion RemoveLetterFrames(mPresShell, state.mFloatedList.mContainingBlock); // Removing the letterframes messes around with the frame tree, removing // and creating frames. We need to reget our prevsibling, parent frame, // etc. prevSibling = GetInsertionPrevSibling(&insertion, aStartChild, &isAppend, &isRangeInsertSafe); // Need check whether a range insert is still safe. if (!isSingleInsert && !isRangeInsertSafe) { // Need to recover the letter frames first. RecoverLetterFrames(state.mFloatedList.mContainingBlock); // must fall back to a single ContertInserted for each child in the // range LAYOUT_PHASE_TEMP_EXIT(); IssueSingleInsertNofications(aStartChild, aEndChild, InsertionKind::Sync); LAYOUT_PHASE_TEMP_REENTER(); return; } frameType = insertion.mParentFrame->Type(); } } // This handles fallback to 'list-style-type' when a 'list-style-image' fails // to load. if (aStartChild->IsInNativeAnonymousSubtree() && aStartChild->IsHTMLElement(nsGkAtoms::mozgeneratedcontentimage)) { MOZ_ASSERT(isSingleInsert); MOZ_ASSERT(insertion.mParentFrame->Style()->GetPseudoType() == PseudoStyleType::marker, "we can only handle ::marker fallback for now"); nsIContent* const nextSibling = aStartChild->GetNextSibling(); MOZ_ASSERT(nextSibling && nextSibling->IsText(), "expected a text node after the list-style-image image"); RemoveFrame(FrameChildListID::Principal, nextSibling->GetPrimaryFrame()); auto* const container = aStartChild->GetParent()->AsElement(); nsIContent* firstNewChild = nullptr; auto InsertChild = [this, container, nextSibling, &firstNewChild](RefPtr&& aChild) { // We don't strictly have to set NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE // here; it would get set under AppendChildTo. But AppendChildTo might // think that we're going from not being anonymous to being anonymous and // do some extra work; setting the flag here avoids that. aChild->SetFlags(NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE); container->InsertChildBefore(aChild, nextSibling, false, IgnoreErrors()); if (auto* childElement = Element::FromNode(aChild)) { // If we created any children elements, Servo needs to traverse them, // but the root is already set up. mPresShell->StyleSet()->StyleNewSubtree(childElement); } if (!firstNewChild) { firstNewChild = aChild; } }; CreateGeneratedContentFromListStyleType( state, *insertion.mContainer->AsElement(), *insertion.mParentFrame->Style(), InsertChild); if (!firstNewChild) { // No fallback content - we're done. return; } aStartChild = firstNewChild; MOZ_ASSERT(firstNewChild->GetNextSibling() == nextSibling, "list-style-type should only create one child"); } AutoFrameConstructionItemList items(this); RefPtr parentStyle = ResolveComputedStyle(insertion.mContainer); ParentType parentType = GetParentType(frameType); FlattenedChildIterator iter(insertion.mContainer); const bool haveNoShadowDOM = !iter.ShadowDOMInvolved() || !iter.GetNextChild(); if (aStartChild->GetPreviousSibling() && parentType == eTypeBlock && haveNoShadowDOM) { // If there's a text node in the normal content list just before the // new nodes, and it has no frame, make a frame construction item for // it, because it might need a frame now. No need to do this if our // parent type is not block, though, since WipeContainingBlock // already handles that situation. AddTextItemIfNeeded(state, *parentStyle, insertion, aStartChild->GetPreviousSibling(), items); } if (isSingleInsert) { AddFrameConstructionItems(state, aStartChild, aStartChild->IsRootOfNativeAnonymousSubtree(), *parentStyle, insertion, items); } else { for (nsIContent* child = aStartChild; child != aEndChild; child = child->GetNextSibling()) { AddFrameConstructionItems(state, child, false, *parentStyle, insertion, items); } } if (aEndChild && parentType == eTypeBlock && haveNoShadowDOM) { // If there's a text node in the normal content list just after the // new nodes, and it has no frame, make a frame construction item for // it, because it might need a frame now. No need to do this if our // parent type is not block, though, since WipeContainingBlock // already handles that situation. AddTextItemIfNeeded(state, *parentStyle, insertion, aEndChild, items); } // Perform special check for diddling around with the frames in // a special inline frame. // If we're appending before :after content, then we're not really // appending, so let WipeContainingBlock know that. LAYOUT_PHASE_TEMP_EXIT(); if (WipeContainingBlock(state, containingBlock, insertion.mParentFrame, items, isAppend, prevSibling)) { LAYOUT_PHASE_TEMP_REENTER(); return; } LAYOUT_PHASE_TEMP_REENTER(); nsFrameConstructorSaveState floatSaveState; state.MaybePushFloatContainingBlock(insertion.mParentFrame, floatSaveState); if (state.mPresContext->IsPaginated() && StaticPrefs::layout_css_named_pages_enabled()) { // Because this function can be called outside frame construction, we need // to set state.mAutoPageNameValue based on what the parent frame's auto // value is. // Calling this from outside the frame constructor can violate many of the // expectations in AutoFrameConstructionPageName, and unlike during frame // construction we already have an auto value from parentFrame, so we do // not use AutoFrameConstructionPageName here. state.mAutoPageNameValue = insertion.mParentFrame->GetAutoPageValue(); #ifdef DEBUG insertion.mParentFrame->mWasVisitedByAutoFrameConstructionPageName = true; #endif } // If the container is a table and a caption will be appended, it needs to be // put in the table wrapper frame's additional child list. // We make no attempt here to set flags to indicate whether the list // will be at the start or end of a block. It doesn't seem worthwhile. nsFrameList frameList, captionList; ConstructFramesFromItemList(state, items, insertion.mParentFrame, ParentIsWrapperAnonBox(insertion.mParentFrame), frameList); if (frameList.NotEmpty()) { for (nsIContent* child = aStartChild; child != aEndChild; child = child->GetNextSibling()) { InvalidateCanvasIfNeeded(mPresShell, child); } if (LayoutFrameType::Table == frameType || LayoutFrameType::TableWrapper == frameType) { PullOutCaptionFrames(frameList, captionList); } } if (haveFirstLineStyle && insertion.mParentFrame == containingBlock && isAppend) { // It's possible that the new frame goes into a first-line // frame. Look at it and see... AppendFirstLineFrames(state, containingBlock->GetContent(), containingBlock, frameList); } else if (insertion.mParentFrame->Style()->HasPseudoElementData()) { CheckForFirstLineInsertion(insertion.mParentFrame, frameList); CheckForFirstLineInsertion(insertion.mParentFrame, captionList); } // We might have captions; put them into the caption list of the // table wrapper frame. if (captionList.NotEmpty()) { NS_ASSERTION(LayoutFrameType::Table == frameType || LayoutFrameType::TableWrapper == frameType, "parent for caption is not table?"); // We need to determine where to put the caption items; start with the // the parent frame that has already been determined and get the insertion // prevsibling of the first caption item. bool captionIsAppend; nsIFrame* captionPrevSibling = nullptr; // aIsRangeInsertSafe is ignored on purpose because it is irrelevant here. bool ignored; InsertionPoint captionInsertion = insertion; if (isSingleInsert) { captionPrevSibling = GetInsertionPrevSibling( &captionInsertion, aStartChild, &captionIsAppend, &ignored); } else { nsIContent* firstCaption = captionList.FirstChild()->GetContent(); // It is very important here that we skip the children in // [aStartChild,aEndChild) when looking for a // prevsibling. captionPrevSibling = GetInsertionPrevSibling( &captionInsertion, firstCaption, &captionIsAppend, &ignored, aStartChild, aEndChild); } nsContainerFrame* outerTable = captionInsertion.mParentFrame->IsTableFrame() ? captionInsertion.mParentFrame->GetParent() : captionInsertion.mParentFrame; // If the parent is not a table wrapper frame we will try to add frames // to a named child list that the parent does not honor and the frames // will get lost. MOZ_ASSERT(outerTable->IsTableWrapperFrame(), "Pseudo frame construction failure; " "a caption can be only a child of a table wrapper frame"); // If the parent of our current prevSibling is different from the frame // we'll actually use as the parent, then the calculated insertion // point is now invalid (bug 341382). if (captionPrevSibling && captionPrevSibling->GetParent() != outerTable) { captionPrevSibling = nullptr; } captionList.ApplySetParent(outerTable); if (captionIsAppend) { AppendFrames(outerTable, FrameChildListID::Caption, std::move(captionList)); } else { InsertFrames(outerTable, FrameChildListID::Caption, captionPrevSibling, std::move(captionList)); } } LAYOUT_PHASE_TEMP_EXIT(); if (MaybeRecreateForColumnSpan(state, insertion.mParentFrame, frameList, prevSibling)) { LAYOUT_PHASE_TEMP_REENTER(); return; } LAYOUT_PHASE_TEMP_REENTER(); if (frameList.NotEmpty()) { // Notify the parent frame if (isAppend) { AppendFramesToParent(state, insertion.mParentFrame, frameList, prevSibling); } else { InsertFrames(insertion.mParentFrame, FrameChildListID::Principal, prevSibling, std::move(frameList)); } } if (haveFirstLetterStyle) { // Recover the letter frames for the containing block when // it has first-letter style. RecoverLetterFrames(state.mFloatedList.mContainingBlock); } #ifdef DEBUG if (gReallyNoisyContentUpdates && insertion.mParentFrame) { printf( "nsCSSFrameConstructor::ContentRangeInserted: resulting frame " "model:\n"); insertion.mParentFrame->List(stdout); } #endif #ifdef ACCESSIBILITY if (nsAccessibilityService* accService = GetAccService()) { accService->ContentRangeInserted(mPresShell, aStartChild, aEndChild); } #endif } bool nsCSSFrameConstructor::ContentRemoved(nsIContent* aChild, nsIContent* aOldNextSibling, RemoveFlags aFlags) { MOZ_ASSERT(aChild); MOZ_ASSERT(!aChild->IsRootOfNativeAnonymousSubtree() || !aOldNextSibling, "Anonymous roots don't have siblings"); AUTO_PROFILER_LABEL("nsCSSFrameConstructor::ContentRemoved", LAYOUT_FrameConstruction); AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC); nsPresContext* presContext = mPresShell->GetPresContext(); MOZ_ASSERT(presContext, "Our presShell should have a valid presContext"); // We want to detect when the viewport override element stored in the // prescontext is in the subtree being removed. Except in fullscreen cases // (which are handled in Element::UnbindFromTree and do not get stored on the // prescontext), the override element is always either the root element or a // child of the root element. So we can only be removing the stored // override element if the thing being removed is either the override element // itself or the root element (which can be a parent of the override element). if (aChild == presContext->GetViewportScrollStylesOverrideElement() || (aChild->IsElement() && !aChild->GetParent())) { // We might be removing the element that we propagated viewport scrollbar // styles from. Recompute those. (This clause covers two of the three // possible scrollbar-propagation sources: the [as aChild or a // descendant] and the root node. The other possible scrollbar-propagation // source is a fullscreen element, and we have code elsewhere to update // scrollbars after fullscreen elements are removed -- specifically, it's // part of the fullscreen cleanup code called by Element::UnbindFromTree. // We don't handle the fullscreen case here, because it doesn't change the // scrollbar styles override element stored on the prescontext.) Element* newOverrideElement = presContext->UpdateViewportScrollStylesOverride(); // If aChild is the root, then we don't need to do any reframing of // newOverrideElement, because we're about to tear down the whole frame tree // anyway. And we need to make sure we don't do any such reframing, because // reframing the can trigger a reframe of the and then reenter // here. // // But if aChild is not the root, and if newOverrideElement is not // the root and isn't aChild (which it could be if all we're doing // here is reframing the current override element), it needs // reframing. In particular, it used to have a scrollframe // (because its overflow was not "visible"), but now it will // propagate its overflow to the viewport, so it should not need a // scrollframe anymore. if (aChild->GetParent() && newOverrideElement && newOverrideElement->GetParent() && newOverrideElement != aChild) { LAYOUT_PHASE_TEMP_EXIT(); RecreateFramesForContent(newOverrideElement, InsertionKind::Async); LAYOUT_PHASE_TEMP_REENTER(); } } #ifdef DEBUG if (gNoisyContentUpdates) { printf( "nsCSSFrameConstructor::ContentRemoved container=%p child=%p " "old-next-sibling=%p\n", aChild->GetParent(), aChild, aOldNextSibling); if (gReallyNoisyContentUpdates) { aChild->GetParent()->List(stdout, 0); } } #endif nsIFrame* childFrame = aChild->GetPrimaryFrame(); if (!childFrame || childFrame->GetContent() != aChild) { // XXXbz the GetContent() != aChild check is needed due to bug 135040. // Remove it once that's fixed. childFrame = nullptr; } // If we're removing the root, then make sure to remove things starting at // the viewport's child instead of the primary frame (which might even be // null if the root was display:none, even though the frames above it got // created). Detecting removal of a root is a little exciting; in particular, // having no parent is necessary but NOT sufficient. // // Due to how we process reframes, the content node might not even be in our // document by now. So explicitly check whether the viewport's first kid's // content node is aChild. // // FIXME(emilio): I think the "might not be in our document" bit is impossible // now. bool isRoot = false; if (!aChild->GetParent()) { if (nsIFrame* viewport = GetRootFrame()) { nsIFrame* firstChild = viewport->PrincipalChildList().FirstChild(); if (firstChild && firstChild->GetContent() == aChild) { isRoot = true; childFrame = firstChild; NS_ASSERTION(!childFrame->GetNextSibling(), "How did that happen?"); } } } // We need to be conservative about when to determine whether something has // display: contents or not because at this point our actual display may be // different. // // Consider the case of: // //
// // If we reconstruct A because its display changed to "none", we still need to // cleanup the frame on B, but A's display is now "none", so we can't poke at // the style of it. // // FIXME(emilio, bug 1450366): We can make this faster without adding much // complexity for the display: none -> other case, which right now // unnecessarily walks the content tree down. auto CouldHaveBeenDisplayContents = [aFlags](nsIContent* aContent) -> bool { return aFlags == REMOVE_FOR_RECONSTRUCTION || IsDisplayContents(aContent); }; if (!childFrame && CouldHaveBeenDisplayContents(aChild)) { // NOTE(emilio): We may iterate through ::before and ::after here and they // may be gone after the respective ContentRemoved call. Right now // StyleChildrenIterator handles that properly, so it's not an issue. StyleChildrenIterator iter(aChild); for (nsIContent* c = iter.GetNextChild(); c; c = iter.GetNextChild()) { if (c->GetPrimaryFrame() || CouldHaveBeenDisplayContents(c)) { LAYOUT_PHASE_TEMP_EXIT(); bool didReconstruct = ContentRemoved(c, nullptr, aFlags); LAYOUT_PHASE_TEMP_REENTER(); if (didReconstruct) { return true; } } } return false; } if (childFrame) { if (aFlags == REMOVE_FOR_RECONSTRUCTION) { // Before removing the frames associated with the content object, // ask them to save their state onto our state object. CaptureStateForFramesOf(aChild, mFrameTreeState); } InvalidateCanvasIfNeeded(mPresShell, aChild); // See whether we need to remove more than just childFrame LAYOUT_PHASE_TEMP_EXIT(); if (MaybeRecreateContainerForFrameRemoval(childFrame)) { LAYOUT_PHASE_TEMP_REENTER(); return true; } LAYOUT_PHASE_TEMP_REENTER(); // Get the childFrame's parent frame nsIFrame* parentFrame = childFrame->GetParent(); LayoutFrameType parentType = parentFrame->Type(); if (parentType == LayoutFrameType::FrameSet && IsSpecialFramesetChild(aChild)) { // Just reframe the parent, since framesets are weird like that. LAYOUT_PHASE_TEMP_EXIT(); RecreateFramesForContent(parentFrame->GetContent(), InsertionKind::Async); LAYOUT_PHASE_TEMP_REENTER(); return true; } // If we're a child of MathML, then we should reframe the MathML content. // If we're non-MathML, then we would be wrapped in a block so we need to // check our grandparent in that case. nsIFrame* possibleMathMLAncestor = parentType == LayoutFrameType::Block ? parentFrame->GetParent() : parentFrame; if (possibleMathMLAncestor->IsFrameOfType(nsIFrame::eMathML)) { LAYOUT_PHASE_TEMP_EXIT(); RecreateFramesForContent(parentFrame->GetContent(), InsertionKind::Async); LAYOUT_PHASE_TEMP_REENTER(); return true; } #ifdef ACCESSIBILITY if (aFlags != REMOVE_FOR_RECONSTRUCTION) { if (nsAccessibilityService* accService = GetAccService()) { accService->ContentRemoved(mPresShell, aChild); } } #endif // Examine the containing-block for the removed content and see if // :first-letter style applies. nsIFrame* inflowChild = childFrame; if (childFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) { inflowChild = childFrame->GetPlaceholderFrame(); NS_ASSERTION(inflowChild, "No placeholder for out-of-flow?"); } nsContainerFrame* containingBlock = GetFloatContainingBlock(inflowChild->GetParent()); bool haveFLS = containingBlock && HasFirstLetterStyle(containingBlock); if (haveFLS) { // Trap out to special routine that handles adjusting a blocks // frame tree when first-letter style is present. #ifdef NOISY_FIRST_LETTER printf("ContentRemoved: containingBlock="); containingBlock->ListTag(stdout); printf(" parentFrame="); parentFrame->ListTag(stdout); printf(" childFrame="); childFrame->ListTag(stdout); printf("\n"); #endif // First update the containing blocks structure by removing the // existing letter frames. This makes the subsequent logic // simpler. RemoveLetterFrames(mPresShell, containingBlock); // Recover childFrame and parentFrame childFrame = aChild->GetPrimaryFrame(); if (!childFrame || childFrame->GetContent() != aChild) { // XXXbz the GetContent() != aChild check is needed due to bug 135040. // Remove it once that's fixed. return false; } parentFrame = childFrame->GetParent(); parentType = parentFrame->Type(); #ifdef NOISY_FIRST_LETTER printf(" ==> revised parentFrame="); parentFrame->ListTag(stdout); printf(" childFrame="); childFrame->ListTag(stdout); printf("\n"); #endif } #ifdef DEBUG if (gReallyNoisyContentUpdates) { printf("nsCSSFrameConstructor::ContentRemoved: childFrame="); childFrame->ListTag(stdout); putchar('\n'); parentFrame->List(stdout); } #endif // Notify the parent frame that it should delete the frame if (childFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW)) { childFrame = childFrame->GetPlaceholderFrame(); NS_ASSERTION(childFrame, "Missing placeholder frame for out of flow."); parentFrame = childFrame->GetParent(); } RemoveFrame(nsLayoutUtils::GetChildListNameFor(childFrame), childFrame); // NOTE(emilio): aChild could be dead here already if it is a ::before or // ::after pseudo-element (since in that case it was owned by childFrame, // which we just destroyed). if (isRoot) { mRootElementFrame = nullptr; mRootElementStyleFrame = nullptr; mDocElementContainingBlock = nullptr; mPageSequenceFrame = nullptr; } if (haveFLS && mRootElementFrame) { RecoverLetterFrames(containingBlock); } // If we're just reconstructing frames for the element, then the // following ContentInserted notification on the element will // take care of fixing up any adjacent text nodes. We don't need // to do this if the table parent type of our parent type is not // eTypeBlock, though, because in that case the whitespace isn't // being suppressed due to us anyway. if (aOldNextSibling && aFlags == REMOVE_CONTENT && GetParentType(parentType) == eTypeBlock) { MOZ_ASSERT(aChild->GetParentNode(), "How did we have a sibling without a parent?"); // Adjacent whitespace-only text nodes might have been suppressed if // this node does not have inline ends. Create frames for them now // if necessary. // Reframe any text node just before the node being removed, if there is // one, and if it's not the last child or the first child. If a whitespace // textframe was being suppressed and it's now the last child or first // child then it can stay suppressed since the parent must be a block // and hence it's adjacent to a block end. // If aOldNextSibling is null, then the text node before the node being // removed is the last node, and we don't need to worry about it. // // FIXME(emilio): This should probably use the lazy frame construction // bits if possible instead of reframing it in place. nsIContent* prevSibling = aOldNextSibling->GetPreviousSibling(); if (prevSibling && prevSibling->GetPreviousSibling()) { LAYOUT_PHASE_TEMP_EXIT(); ReframeTextIfNeeded(prevSibling); LAYOUT_PHASE_TEMP_REENTER(); } // Reframe any text node just after the node being removed, if there is // one, and if it's not the last child or the first child. if (aOldNextSibling->GetNextSibling() && aOldNextSibling->GetPreviousSibling()) { LAYOUT_PHASE_TEMP_EXIT(); ReframeTextIfNeeded(aOldNextSibling); LAYOUT_PHASE_TEMP_REENTER(); } } #ifdef DEBUG if (gReallyNoisyContentUpdates && parentFrame) { printf("nsCSSFrameConstructor::ContentRemoved: resulting frame model:\n"); parentFrame->List(stdout); } #endif } return false; } /** * This method invalidates the canvas when frames are removed or added for a * node that might have its background propagated to the canvas, i.e., a * document root node or an HTML BODY which is a child of the root node. * * @param aFrame a frame for a content node about to be removed or a frame that * was just created for a content node that was inserted. */ static void InvalidateCanvasIfNeeded(PresShell* aPresShell, nsIContent* aNode) { MOZ_ASSERT(aPresShell->GetRootFrame(), "What happened here?"); MOZ_ASSERT(aPresShell->GetPresContext(), "Say what?"); // Note that both in ContentRemoved and ContentInserted the content node // will still have the right parent pointer, so looking at that is ok. nsIContent* parent = aNode->GetParent(); if (parent) { // Has a parent; might not be what we want nsIContent* grandParent = parent->GetParent(); if (grandParent) { // Has a grandparent, so not what we want return; } // Check whether it's an HTML body if (!aNode->IsHTMLElement(nsGkAtoms::body)) { return; } } // At this point the node has no parent or it's an HTML child of the // root. We might not need to invalidate in this case (eg we might be in // XHTML or something), but chances are we want to. Play it safe. // Invalidate the viewport. nsIFrame* rootFrame = aPresShell->GetRootFrame(); rootFrame->InvalidateFrameSubtree(); } bool nsCSSFrameConstructor::EnsureFrameForTextNodeIsCreatedAfterFlush( CharacterData* aContent) { if (!aContent->HasFlag(NS_CREATE_FRAME_IF_NON_WHITESPACE)) { return false; } if (mAlwaysCreateFramesForIgnorableWhitespace) { return false; } // Text frame may have been suppressed. Disable suppression and signal that a // flush should be performed. We do this on a document-wide basis so that // pages that repeatedly query metrics for collapsed-whitespace text nodes // don't trigger pathological behavior. mAlwaysCreateFramesForIgnorableWhitespace = true; Element* root = mDocument->GetRootElement(); if (!root) { return false; } RestyleManager()->PostRestyleEvent(root, RestyleHint{0}, nsChangeHint_ReconstructFrame); return true; } void nsCSSFrameConstructor::CharacterDataChanged( nsIContent* aContent, const CharacterDataChangeInfo& aInfo) { AUTO_PROFILER_LABEL("nsCSSFrameConstructor::CharacterDataChanged", LAYOUT_FrameConstruction); AUTO_LAYOUT_PHASE_ENTRY_POINT(mPresShell->GetPresContext(), FrameC); if ((aContent->HasFlag(NS_CREATE_FRAME_IF_NON_WHITESPACE) && !aContent->TextIsOnlyWhitespace()) || (aContent->HasFlag(NS_REFRAME_IF_WHITESPACE) && aContent->TextIsOnlyWhitespace())) { #ifdef DEBUG nsIFrame* frame = aContent->GetPrimaryFrame(); NS_ASSERTION(!frame || !frame->IsGeneratedContentFrame(), "Bit should never be set on generated content"); #endif LAYOUT_PHASE_TEMP_EXIT(); RecreateFramesForContent(aContent, InsertionKind::Async); LAYOUT_PHASE_TEMP_REENTER(); return; } // It's possible the frame whose content changed isn't inserted into the // frame hierarchy yet, or that there is no frame that maps the content if (nsIFrame* frame = aContent->GetPrimaryFrame()) { #if 0 NS_FRAME_LOG(NS_FRAME_TRACE_CALLS, ("nsCSSFrameConstructor::CharacterDataChanged: content=%p[%s] subcontent=%p frame=%p", aContent, ContentTag(aContent, 0), aSubContent, frame)); #endif // Special check for text content that is a child of a letter frame. If // this happens, we should remove the letter frame, do whatever we're // planning to do with this notification, then put the letter frame back. // Note that this is basically what RecreateFramesForContent ends up doing; // the reason we dont' want to call that here is that our text content // could be native anonymous, in which case RecreateFramesForContent would // completely barf on it. And recreating the non-anonymous ancestor would // just lead us to come back into this notification (e.g. if quotes or // counters are involved), leading to a loop. nsContainerFrame* block = GetFloatContainingBlock(frame); bool haveFirstLetterStyle = false; if (block) { // See if the block has first-letter style applied to it. haveFirstLetterStyle = HasFirstLetterStyle(block); if (haveFirstLetterStyle) { RemoveLetterFrames(mPresShell, block); // Reget |frame|, since we might have killed it. // Do we really need to call CharacterDataChanged in this case, though? frame = aContent->GetPrimaryFrame(); NS_ASSERTION(frame, "Should have frame here!"); } } // Notify the first frame that maps the content. It will generate a reflow // command frame->CharacterDataChanged(aInfo); if (haveFirstLetterStyle) { RecoverLetterFrames(block); } } } void nsCSSFrameConstructor::RecalcQuotesAndCounters() { nsAutoScriptBlocker scriptBlocker; if (mQuotesDirty) { mQuotesDirty = false; mContainStyleScopeManager.RecalcAllQuotes(); } if (mCountersDirty) { mCountersDirty = false; mContainStyleScopeManager.RecalcAllCounters(); } NS_ASSERTION(!mQuotesDirty, "Quotes updates will be lost"); NS_ASSERTION(!mCountersDirty, "Counter updates will be lost"); } void nsCSSFrameConstructor::NotifyCounterStylesAreDirty() { mContainStyleScopeManager.SetAllCountersDirty(); CountersDirty(); } void nsCSSFrameConstructor::WillDestroyFrameTree() { #if defined(DEBUG_dbaron_off) mContainStyleScopeManager.DumpCounters(); #endif // Prevent frame tree destruction from being O(N^2) mContainStyleScopeManager.Clear(); nsFrameManager::Destroy(); } // STATIC // XXXbz I'd really like this method to go away. Once we have inline-block and // I can just use that for sized broken images, that can happen, maybe. // // NOTE(emilio): This needs to match MozAltContent handling. void nsCSSFrameConstructor::GetAlternateTextFor(const Element& aElement, nsAString& aAltText) { // The "alt" attribute specifies alternate text that is rendered // when the image can not be displayed. if (aElement.GetAttr(nsGkAtoms::alt, aAltText)) { return; } if (aElement.IsHTMLElement(nsGkAtoms::input)) { // If there's no "alt" attribute, and aElement is an input element, then use // the value of the "value" attribute. if (aElement.GetAttr(nsGkAtoms::value, aAltText)) { return; } // If there's no "value" attribute either, then use the localized string for // "Submit" as the alternate text. nsContentUtils::GetMaybeLocalizedString(nsContentUtils::eFORMS_PROPERTIES, "Submit", aElement.OwnerDoc(), aAltText); } } nsIFrame* nsCSSFrameConstructor::CreateContinuingOuterTableFrame( nsIFrame* aFrame, nsContainerFrame* aParentFrame, nsIContent* aContent, ComputedStyle* aComputedStyle) { nsTableWrapperFrame* newFrame = NS_NewTableWrapperFrame(mPresShell, aComputedStyle); newFrame->Init(aContent, aParentFrame, aFrame); // Create a continuing inner table frame, and if there's a caption then // replicate the caption nsFrameList newChildFrames; nsIFrame* childFrame = aFrame->PrincipalChildList().FirstChild(); if (childFrame) { nsIFrame* continuingTableFrame = CreateContinuingFrame(childFrame, newFrame); newChildFrames.AppendFrame(nullptr, continuingTableFrame); NS_ASSERTION(!childFrame->GetNextSibling(), "there can be only one inner table frame"); } // Set the table wrapper's initial child list newFrame->SetInitialChildList(FrameChildListID::Principal, std::move(newChildFrames)); return newFrame; } nsIFrame* nsCSSFrameConstructor::CreateContinuingTableFrame( nsIFrame* aFrame, nsContainerFrame* aParentFrame, nsIContent* aContent, ComputedStyle* aComputedStyle) { nsTableFrame* newFrame = NS_NewTableFrame(mPresShell, aComputedStyle); newFrame->Init(aContent, aParentFrame, aFrame); // Replicate any header/footer frames nsFrameList childFrames; for (nsIFrame* childFrame : aFrame->PrincipalChildList()) { // See if it's a header/footer, possibly wrapped in a scroll frame. nsTableRowGroupFrame* rowGroupFrame = static_cast(childFrame); // If the row group was continued, then don't replicate it. nsIFrame* rgNextInFlow = rowGroupFrame->GetNextInFlow(); if (rgNextInFlow) { rowGroupFrame->SetRepeatable(false); } else if (rowGroupFrame->IsRepeatable()) { // Replicate the header/footer frame. nsTableRowGroupFrame* headerFooterFrame; nsFrameList childList; nsFrameConstructorState state( mPresShell, GetAbsoluteContainingBlock(newFrame, FIXED_POS), GetAbsoluteContainingBlock(newFrame, ABS_POS), nullptr); state.mCreatingExtraFrames = true; ComputedStyle* const headerFooterComputedStyle = rowGroupFrame->Style(); headerFooterFrame = static_cast( NS_NewTableRowGroupFrame(mPresShell, headerFooterComputedStyle)); nsIContent* headerFooter = rowGroupFrame->GetContent(); headerFooterFrame->Init(headerFooter, newFrame, nullptr); nsFrameConstructorSaveState absoluteSaveState; MakeTablePartAbsoluteContainingBlockIfNeeded( state, headerFooterComputedStyle->StyleDisplay(), absoluteSaveState, headerFooterFrame); nsFrameConstructorSaveState floatSaveState; state.MaybePushFloatContainingBlock(headerFooterFrame, floatSaveState); ProcessChildren(state, headerFooter, rowGroupFrame->Style(), headerFooterFrame, true, childList, false, nullptr); NS_ASSERTION(state.mFloatedList.IsEmpty(), "unexpected floated element"); headerFooterFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childList)); headerFooterFrame->SetRepeatable(true); // Table specific initialization headerFooterFrame->InitRepeatedFrame(rowGroupFrame); // XXX Deal with absolute and fixed frames... childFrames.AppendFrame(nullptr, headerFooterFrame); } } // Set the table frame's initial child list newFrame->SetInitialChildList(FrameChildListID::Principal, std::move(childFrames)); return newFrame; } nsIFrame* nsCSSFrameConstructor::CreateContinuingFrame( nsIFrame* aFrame, nsContainerFrame* aParentFrame, bool aIsFluid) { ComputedStyle* computedStyle = aFrame->Style(); nsIFrame* newFrame = nullptr; nsIFrame* nextContinuation = aFrame->GetNextContinuation(); nsIFrame* nextInFlow = aFrame->GetNextInFlow(); // Use the frame type to determine what type of frame to create LayoutFrameType frameType = aFrame->Type(); nsIContent* content = aFrame->GetContent(); if (LayoutFrameType::Text == frameType) { newFrame = NS_NewContinuingTextFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::Inline == frameType) { newFrame = NS_NewInlineFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::Block == frameType) { MOZ_ASSERT(!aFrame->IsTableCaption(), "no support for fragmenting table captions yet"); newFrame = NS_NewBlockFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::ColumnSetWrapper == frameType) { newFrame = NS_NewColumnSetWrapperFrame(mPresShell, computedStyle, nsFrameState(0)); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::ColumnSet == frameType) { MOZ_ASSERT(!aFrame->IsTableCaption(), "no support for fragmenting table captions yet"); newFrame = NS_NewColumnSetFrame(mPresShell, computedStyle, nsFrameState(0)); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::PrintedSheet == frameType) { newFrame = ConstructPrintedSheetFrame(mPresShell, aParentFrame, aFrame); } else if (LayoutFrameType::Page == frameType) { nsContainerFrame* canvasFrame; // (unused outparam for ConstructPageFrame) newFrame = ConstructPageFrame(mPresShell, aParentFrame, aFrame, canvasFrame); } else if (LayoutFrameType::TableWrapper == frameType) { newFrame = CreateContinuingOuterTableFrame(aFrame, aParentFrame, content, computedStyle); } else if (LayoutFrameType::Table == frameType) { newFrame = CreateContinuingTableFrame(aFrame, aParentFrame, content, computedStyle); } else if (LayoutFrameType::TableRowGroup == frameType) { newFrame = NS_NewTableRowGroupFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); if (newFrame->HasAnyStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN)) { nsTableFrame::RegisterPositionedTablePart(newFrame); } } else if (LayoutFrameType::TableRow == frameType) { nsTableRowFrame* rowFrame = NS_NewTableRowFrame(mPresShell, computedStyle); rowFrame->Init(content, aParentFrame, aFrame); if (rowFrame->HasAnyStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN)) { nsTableFrame::RegisterPositionedTablePart(rowFrame); } // Create a continuing frame for each table cell frame nsFrameList newChildList; nsIFrame* cellFrame = aFrame->PrincipalChildList().FirstChild(); while (cellFrame) { // See if it's a table cell frame if (cellFrame->IsTableCellFrame()) { nsIFrame* continuingCellFrame = CreateContinuingFrame(cellFrame, rowFrame); newChildList.AppendFrame(nullptr, continuingCellFrame); } cellFrame = cellFrame->GetNextSibling(); } rowFrame->SetInitialChildList(FrameChildListID::Principal, std::move(newChildList)); newFrame = rowFrame; } else if (LayoutFrameType::TableCell == frameType) { // Warning: If you change this and add a wrapper frame around table cell // frames, make sure Bug 368554 doesn't regress! // See IsInAutoWidthTableCellForQuirk() in nsImageFrame.cpp. nsTableFrame* tableFrame = static_cast(aParentFrame)->GetTableFrame(); nsTableCellFrame* cellFrame = NS_NewTableCellFrame(mPresShell, computedStyle, tableFrame); cellFrame->Init(content, aParentFrame, aFrame); if (cellFrame->HasAnyStateBits(NS_FRAME_CAN_HAVE_ABSPOS_CHILDREN)) { nsTableFrame::RegisterPositionedTablePart(cellFrame); } // Create a continuing area frame nsIFrame* blockFrame = aFrame->PrincipalChildList().FirstChild(); nsIFrame* continuingBlockFrame = CreateContinuingFrame(blockFrame, cellFrame); SetInitialSingleChild(cellFrame, continuingBlockFrame); newFrame = cellFrame; } else if (LayoutFrameType::Line == frameType) { newFrame = NS_NewFirstLineFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::Letter == frameType) { newFrame = NS_NewFirstLetterFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::Image == frameType) { auto* imageFrame = static_cast(aFrame); newFrame = imageFrame->CreateContinuingFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::ImageControl == frameType) { newFrame = NS_NewImageControlFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::FieldSet == frameType) { newFrame = NS_NewFieldSetFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::FlexContainer == frameType) { newFrame = NS_NewFlexContainerFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::GridContainer == frameType) { newFrame = NS_NewGridContainerFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::Ruby == frameType) { newFrame = NS_NewRubyFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::RubyBaseContainer == frameType) { newFrame = NS_NewRubyBaseContainerFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else if (LayoutFrameType::RubyTextContainer == frameType) { newFrame = NS_NewRubyTextContainerFrame(mPresShell, computedStyle); newFrame->Init(content, aParentFrame, aFrame); } else { MOZ_CRASH("unexpected frame type"); } // Init() set newFrame to be a fluid continuation of aFrame. // If we want a non-fluid continuation, we need to call SetPrevContinuation() // to reset NS_FRAME_IS_FLUID_CONTINUATION. if (!aIsFluid) { newFrame->SetPrevContinuation(aFrame); } // If a continuing frame needs to carry frame state bits from its previous // continuation or parent, set them in nsIFrame::Init(), or in any derived // frame class's Init() if the bits are belong to specific group. if (nextInFlow) { nextInFlow->SetPrevInFlow(newFrame); newFrame->SetNextInFlow(nextInFlow); } else if (nextContinuation) { nextContinuation->SetPrevContinuation(newFrame); newFrame->SetNextContinuation(nextContinuation); } // aFrame cannot be a dynamic reflow root because it has a continuation now. aFrame->RemoveStateBits(NS_FRAME_DYNAMIC_REFLOW_ROOT); // XXXalaskanemily: This avoids linear-time FirstContinuation lookups during // paginated reflow, but there are a lot of smarter ways to manage this. We // might also want to share the struct (refcount or some guarantee the struct // will remain valid during reflow). if (nsIFrame::PageValues* pageValues = aFrame->GetProperty(nsIFrame::PageValuesProperty())) { // It is possible that both values of a PageValues struct can be // overwritten with null. If that's the case, then as a minor optimization // we don't need to create a copy of the struct since this property being // missing is equivalent to having null start/end values. if (pageValues->mStartPageValue || pageValues->mEndPageValue) { nsIFrame::PageValues* const newPageValues = new nsIFrame::PageValues(*pageValues); newFrame->SetProperty(nsIFrame::PageValuesProperty(), newPageValues); } } MOZ_ASSERT(!newFrame->GetNextSibling(), "unexpected sibling"); return newFrame; } nsresult nsCSSFrameConstructor::ReplicateFixedFrames( nsPageContentFrame* aParentFrame) { // Now deal with fixed-pos things.... They should appear on all pages, // so we want to move over the placeholders when processing the child // of the pageContentFrame. nsIFrame* prevPageContentFrame = aParentFrame->GetPrevInFlow(); if (!prevPageContentFrame) { return NS_OK; } nsContainerFrame* canvasFrame = do_QueryFrame(aParentFrame->PrincipalChildList().FirstChild()); nsIFrame* prevCanvasFrame = prevPageContentFrame->PrincipalChildList().FirstChild(); if (!canvasFrame || !prevCanvasFrame) { // document's root element frame missing return NS_ERROR_UNEXPECTED; } nsFrameList fixedPlaceholders; nsIFrame* firstFixed = prevPageContentFrame->GetChildList(FrameChildListID::Fixed).FirstChild(); if (!firstFixed) { return NS_OK; } // Don't allow abs-pos descendants of the fixed content to escape the content. // This should not normally be possible (because fixed-pos elements should // be absolute containers) but fixed-pos tables currently aren't abs-pos // containers. nsFrameConstructorState state(mPresShell, aParentFrame, nullptr, mRootElementFrame); state.mCreatingExtraFrames = true; // We can't use an ancestor filter here, because we're not going to // be usefully recurring down the tree. This means that other // places in frame construction can't assume a filter is // initialized! // Iterate across fixed frames and replicate each whose placeholder is a // descendant of aFrame. (We don't want to explicitly copy placeholders that // are within fixed frames, because that would cause duplicates on the new // page - bug 389619) for (nsIFrame* fixed = firstFixed; fixed; fixed = fixed->GetNextSibling()) { nsIFrame* prevPlaceholder = fixed->GetPlaceholderFrame(); if (prevPlaceholder && nsLayoutUtils::IsProperAncestorFrame( prevCanvasFrame, prevPlaceholder)) { // We want to use the same style as the primary style frame for // our content nsIContent* content = fixed->GetContent(); ComputedStyle* computedStyle = nsLayoutUtils::GetStyleFrame(content)->Style(); AutoFrameConstructionItemList items(this); AddFrameConstructionItemsInternal(state, content, canvasFrame, true, computedStyle, {ItemFlag::AllowPageBreak}, items); ConstructFramesFromItemList(state, items, canvasFrame, /* aParentIsWrapperAnonBox = */ false, fixedPlaceholders); } } // Add the placeholders to our primary child list. // XXXbz this is a little screwed up, since the fixed frames will have // broken auto-positioning. Oh, well. NS_ASSERTION(!canvasFrame->PrincipalChildList().FirstChild(), "leaking frames; doc root continuation must be empty"); canvasFrame->SetInitialChildList(FrameChildListID::Principal, std::move(fixedPlaceholders)); return NS_OK; } nsCSSFrameConstructor::InsertionPoint nsCSSFrameConstructor::GetInsertionPoint( nsIContent* aChild) { MOZ_ASSERT(aChild); nsIContent* insertionElement = aChild->GetFlattenedTreeParent(); if (!insertionElement) { // The element doesn't belong in the flattened tree, and thus we don't want // to render it. return {}; } return {GetContentInsertionFrameFor(insertionElement), insertionElement}; } // Capture state for the frame tree rooted at the frame associated with the // content object, aContent void nsCSSFrameConstructor::CaptureStateForFramesOf( nsIContent* aContent, nsILayoutHistoryState* aHistoryState) { if (!aHistoryState) { return; } nsIFrame* frame = aContent->GetPrimaryFrame(); if (frame == mRootElementFrame) { frame = mRootElementFrame ? GetAbsoluteContainingBlock(mRootElementFrame, FIXED_POS) : GetRootFrame(); } for (; frame; frame = nsLayoutUtils::GetNextContinuationOrIBSplitSibling(frame)) { CaptureFrameState(frame, aHistoryState); } } static bool IsWhitespaceFrame(nsIFrame* aFrame) { MOZ_ASSERT(aFrame, "invalid argument"); return aFrame->IsTextFrame() && aFrame->GetContent()->TextIsOnlyWhitespace(); } static nsIFrame* FindFirstNonWhitespaceChild(nsIFrame* aParentFrame) { nsIFrame* f = aParentFrame->PrincipalChildList().FirstChild(); while (f && IsWhitespaceFrame(f)) { f = f->GetNextSibling(); } return f; } static nsIFrame* FindNextNonWhitespaceSibling(nsIFrame* aFrame) { nsIFrame* f = aFrame; do { f = f->GetNextSibling(); } while (f && IsWhitespaceFrame(f)); return f; } static nsIFrame* FindPreviousNonWhitespaceSibling(nsIFrame* aFrame) { nsIFrame* f = aFrame; do { f = f->GetPrevSibling(); } while (f && IsWhitespaceFrame(f)); return f; } bool nsCSSFrameConstructor::MaybeRecreateContainerForFrameRemoval( nsIFrame* aFrame) { #define TRACE(reason) \ PROFILER_MARKER("MaybeRecreateContainerForFrameRemoval: " reason, LAYOUT, \ {}, Tracing, "Layout") MOZ_ASSERT(aFrame, "Must have a frame"); MOZ_ASSERT(aFrame->GetParent(), "Frame shouldn't be root"); MOZ_ASSERT(aFrame == aFrame->FirstContinuation(), "aFrame not the result of GetPrimaryFrame()?"); nsIFrame* inFlowFrame = aFrame->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW) ? aFrame->GetPlaceholderFrame() : aFrame; MOZ_ASSERT(inFlowFrame, "How did that happen?"); MOZ_ASSERT(inFlowFrame == inFlowFrame->FirstContinuation(), "placeholder for primary frame has previous continuations?"); nsIFrame* parent = inFlowFrame->GetParent(); if (inFlowFrame->HasAnyStateBits(NS_FRAME_HAS_MULTI_COLUMN_ANCESTOR)) { nsIFrame* grandparent = parent->GetParent(); MOZ_ASSERT(grandparent); bool needsReframe = // 1. Removing a column-span may lead to an empty // ::-moz-column-span-wrapper. inFlowFrame->IsColumnSpan() || // 2. Removing a frame which has any column-span siblings may also // lead to an empty ::-moz-column-span-wrapper subtree. The // column-span siblings were the frame's children, but later become // the frame's siblings after CreateColumnSpanSiblings(). inFlowFrame->HasColumnSpanSiblings() || // 3. Removing the only child of a ::-moz-column-content, whose // ColumnSet grandparent has a previous column-span sibling, requires // reframing since we might connect the ColumnSet's next column-span // sibling (if there's one). Note that this isn't actually needed if // the ColumnSet is at the end of ColumnSetWrapper since we create // empty ones at the end anyway, but we're not worried about // optimizing that case. (parent->Style()->GetPseudoType() == PseudoStyleType::columnContent && // The only child in ::-moz-column-content (might be tall enough to // split across columns) !inFlowFrame->GetPrevSibling() && !inFlowFrame->GetNextSibling() && // That ::-moz-column-content is the first column. !parent->GetPrevInFlow() && // The ColumnSet grandparent has a previous sibling that is a // column-span. grandparent->GetPrevSibling()); if (needsReframe) { nsContainerFrame* containingBlock = GetMultiColumnContainingBlockFor(inFlowFrame); #ifdef DEBUG if (IsFramePartOfIBSplit(inFlowFrame)) { nsIFrame* ibContainingBlock = GetIBContainingBlockFor(inFlowFrame); MOZ_ASSERT(containingBlock == ibContainingBlock || nsLayoutUtils::IsProperAncestorFrame(containingBlock, ibContainingBlock), "Multi-column containing block should be equal to or be the " "ancestor of the IB containing block!"); } #endif TRACE("Multi-column"); RecreateFramesForContent(containingBlock->GetContent(), InsertionKind::Async); return true; } } if (IsFramePartOfIBSplit(aFrame)) { // The removal functions can't handle removal of an {ib} split directly; we // need to rebuild the containing block. TRACE("IB split removal"); ReframeContainingBlock(aFrame); return true; } if (inFlowFrame->IsRenderedLegend()) { TRACE("Fieldset / Legend"); RecreateFramesForContent(parent->GetContent(), InsertionKind::Async); return true; } // Now check for possibly needing to reconstruct due to a pseudo parent // For the case of ruby pseudo parent, effectively, only pseudo rb/rt frame // need to be checked here, since all other types of parent will be catched // by "Check ruby containers" section below. if (IsTableOrRubyPseudo(parent)) { if (FindFirstNonWhitespaceChild(parent) == inFlowFrame || !FindNextNonWhitespaceSibling(inFlowFrame->LastContinuation()) || // If it is a whitespace, and is the only child of the parent, the // pseudo parent was created for the space, and should now be removed. (IsWhitespaceFrame(aFrame) && parent->PrincipalChildList().OnlyChild()) || // If we're a table-column-group, then the OnlyChild check above is // not going to catch cases when we're the first child. (inFlowFrame->IsTableColGroupFrame() && parent->GetChildList(FrameChildListID::ColGroup).FirstChild() == inFlowFrame) || // Similar if we're a table-caption. (inFlowFrame->IsTableCaption() && parent->GetChildList(FrameChildListID::Caption).FirstChild() == inFlowFrame)) { TRACE("Table or ruby pseudo parent"); RecreateFramesForContent(parent->GetContent(), InsertionKind::Async); return true; } } // Might need to reconstruct things if this frame's nextSibling is a table // or ruby pseudo, since removal of this frame might mean that this pseudo // needs to get merged with the frame's prevSibling if that's also a table // or ruby pseudo. nsIFrame* nextSibling = FindNextNonWhitespaceSibling(inFlowFrame->LastContinuation()); NS_ASSERTION(!IsTableOrRubyPseudo(inFlowFrame), "Shouldn't happen here"); // Effectively, for the ruby pseudo sibling case, only pseudo frame // need to be checked here, since all other types of such frames will have // a ruby container parent, and be catched by "Check ruby containers" below. if (nextSibling && IsTableOrRubyPseudo(nextSibling)) { nsIFrame* prevSibling = FindPreviousNonWhitespaceSibling(inFlowFrame); if (prevSibling && IsTableOrRubyPseudo(prevSibling)) { TRACE("Table or ruby pseudo sibling"); // Good enough to recreate frames for aFrame's parent's content; even if // aFrame's parent is a pseudo, that'll be the right content node. RecreateFramesForContent(parent->GetContent(), InsertionKind::Async); return true; } } // Check ruby containers LayoutFrameType parentType = parent->Type(); if (parentType == LayoutFrameType::Ruby || RubyUtils::IsRubyContainerBox(parentType)) { // In ruby containers, pseudo frames may be created from // whitespaces or even nothing. There are two cases we actually // need to handle here, but hard to check exactly: // 1. Status of spaces beside the frame may vary, and related // frames may be constructed or destroyed accordingly. // 2. The type of the first child of a ruby frame determines // whether a pseudo ruby base container should exist. TRACE("Ruby container"); RecreateFramesForContent(parent->GetContent(), InsertionKind::Async); return true; } // Might need to reconstruct things if the removed frame's nextSibling is an // anonymous flex item. The removed frame might've been what divided two // runs of inline content into two anonymous flex items, which would now // need to be merged. // NOTE: It's fine that we've advanced nextSibling past whitespace (up above); // we're only interested in anonymous flex items here, and those can never // be adjacent to whitespace, since they absorb contiguous runs of inline // non-replaced content (including whitespace). if (nextSibling && IsAnonymousItem(nextSibling)) { AssertAnonymousFlexOrGridItemParent(nextSibling, parent); TRACE("Anon flex or grid item next sibling"); // Recreate frames for the flex container (the removed frame's parent) RecreateFramesForContent(parent->GetContent(), InsertionKind::Async); return true; } // Might need to reconstruct things if the removed frame's nextSibling is // null and its parent is an anonymous flex item. (This might be the last // remaining child of that anonymous flex item, which can then go away.) if (!nextSibling && IsAnonymousItem(parent)) { AssertAnonymousFlexOrGridItemParent(parent, parent->GetParent()); TRACE("Anon flex or grid item parent"); // Recreate frames for the flex container (the removed frame's grandparent) RecreateFramesForContent(parent->GetParent()->GetContent(), InsertionKind::Async); return true; } // Reconstruct if inflowFrame is parent's only child, and parent is, or has, // a non-fluid continuation, i.e. it was split by bidi resolution if (!inFlowFrame->GetPrevSibling() && !inFlowFrame->GetNextSibling() && ((parent->GetPrevContinuation() && !parent->GetPrevInFlow()) || (parent->GetNextContinuation() && !parent->GetNextInFlow()))) { TRACE("Removing last child of non-fluid split parent"); RecreateFramesForContent(parent->GetContent(), InsertionKind::Async); return true; } // We might still need to reconstruct things if the parent of inFlowFrame is // ib-split, since in that case the removal of aFrame might affect the // splitting of its parent. if (!IsFramePartOfIBSplit(parent)) { return false; } // If inFlowFrame is not the only in-flow child of |parent|, then removing // it will change nothing about the {ib} split. if (inFlowFrame != parent->PrincipalChildList().FirstChild() || inFlowFrame->LastContinuation()->GetNextSibling()) { return false; } // If the parent is the first or last part of the {ib} split, then // removing one of its kids will have no effect on the splitting. // Get the first continuation up front so we don't have to do it twice. nsIFrame* parentFirstContinuation = parent->FirstContinuation(); if (!GetIBSplitSibling(parentFirstContinuation) || !GetIBSplitPrevSibling(parentFirstContinuation)) { return false; } TRACE("IB split parent"); ReframeContainingBlock(parent); return true; #undef TRACE } void nsCSSFrameConstructor::UpdateTableCellSpans(nsIContent* aContent) { nsTableCellFrame* cellFrame = do_QueryFrame(aContent->GetPrimaryFrame()); // It's possible that this warning could fire if some other style change // simultaneously changes the 'display' of the element and makes it no // longer be a table cell. NS_WARNING_ASSERTION(cellFrame, "Hint should only be posted on table cells!"); if (cellFrame) { cellFrame->GetTableFrame()->RowOrColSpanChanged(cellFrame); } } static nsIContent* GetTopmostMathMLElement(nsIContent* aMathMLContent) { MOZ_ASSERT(aMathMLContent->IsMathMLElement()); MOZ_ASSERT(aMathMLContent->GetPrimaryFrame()); MOZ_ASSERT( aMathMLContent->GetPrimaryFrame()->IsFrameOfType(nsIFrame::eMathML)); nsIContent* root = aMathMLContent; for (nsIContent* parent = aMathMLContent->GetFlattenedTreeParent(); parent; parent = parent->GetFlattenedTreeParent()) { nsIFrame* frame = parent->GetPrimaryFrame(); if (!frame || !frame->IsFrameOfType(nsIFrame::eMathML)) { break; } root = parent; } return root; } // We don't know how to re-insert an anonymous subtree root, so recreate the // closest non-generated ancestor instead, except for a few special cases... static bool ShouldRecreateContainerForNativeAnonymousContentRoot( nsIContent* aContent) { if (!aContent->IsRootOfNativeAnonymousSubtree()) { return false; } if (ManualNACPtr::IsManualNAC(aContent)) { // Editor NAC, would enter an infinite loop, and we sorta get away with it // because it's all abspos. return false; } if (auto* el = Element::FromNode(aContent)) { if (auto* classes = el->GetClasses()) { if (classes->Contains(nsGkAtoms::mozCustomContentContainer, eCaseMatters)) { // Canvas anonymous content (like the custom content container) is also // fine, because its only sibling is a tooltip which is also abspos, so // relative insertion order doesn't really matter. // // This is important because the inspector uses it, and we don't want // inspecting the page to change behavior heavily (and reframing // unfortunately has side-effects sometimes, even though they're bugs). return false; } } } return true; } void nsCSSFrameConstructor::RecreateFramesForContent( nsIContent* aContent, InsertionKind aInsertionKind) { MOZ_ASSERT(aContent); // If there is no document, we don't want to recreate frames for it. (You // shouldn't generally be giving this method content without a document // anyway). // Rebuilding the frame tree can have bad effects, especially if it's the // frame tree for chrome (see bug 157322). if (NS_WARN_IF(!aContent->GetComposedDoc())) { return; } // TODO(emilio): We technically can find the right insertion point nowadays // using StyleChildrenIterator rather than FlattenedTreeIterator. But we'd // need to tweak the setup to insert into replaced elements to filter which // anonymous roots can be allowed, and which can't. // // TODO(emilio, 2022): Is this true? If we have a replaced element we wouldn't // have generated e.g., a ::before/::after pseudo-element to begin with (which // is what this code is about, so maybe we can just remove this piece of code // altogether). if (ShouldRecreateContainerForNativeAnonymousContentRoot(aContent)) { do { aContent = aContent->GetParent(); } while (ShouldRecreateContainerForNativeAnonymousContentRoot(aContent)); return RecreateFramesForContent(aContent, InsertionKind::Async); } nsIFrame* frame = aContent->GetPrimaryFrame(); if (frame && frame->IsFrameOfType(nsIFrame::eMathML)) { // Reframe the topmost MathML element to prevent exponential blowup // (see bug 397518). aContent = GetTopmostMathMLElement(aContent); frame = aContent->GetPrimaryFrame(); } if (frame) { nsIFrame* parent = frame->GetParent(); nsIContent* parentContent = parent ? parent->GetContent() : nullptr; // If the parent frame is a leaf then the subsequent insert will fail to // create a frame, so we need to recreate the parent content. This happens // with native anonymous content from the editor. if (parent && parent->IsLeaf() && parentContent && parentContent != aContent) { return RecreateFramesForContent(parentContent, InsertionKind::Async); } } if (frame && MaybeRecreateContainerForFrameRemoval(frame)) { return; } MOZ_ASSERT(aContent->GetParentNode()); // Remove the frames associated with the content object. nsIContent* nextSibling = aContent->IsRootOfNativeAnonymousSubtree() ? nullptr : aContent->GetNextSibling(); bool didReconstruct = ContentRemoved(aContent, nextSibling, REMOVE_FOR_RECONSTRUCTION); if (!didReconstruct) { if (aInsertionKind == InsertionKind::Async && aContent->IsElement()) { // FIXME(emilio, bug 1397239): There's nothing removing the frame state // for elements that go away before we come back to the frame // constructor. // // Also, it'd be nice to just use the `ContentRangeInserted` path for // both elements and non-elements, but we need to make lazy frame // construction to apply to all elements first. RestyleManager()->PostRestyleEvent(aContent->AsElement(), RestyleHint{0}, nsChangeHint_ReconstructFrame); } else { // Now, recreate the frames associated with this content object. If // ContentRemoved triggered reconstruction, then we don't need to do this // because the frames will already have been built. ContentRangeInserted(aContent, aContent->GetNextSibling(), aInsertionKind); } } } bool nsCSSFrameConstructor::DestroyFramesFor(nsIContent* aContent) { MOZ_ASSERT(aContent && aContent->GetParentNode()); nsIContent* nextSibling = aContent->IsRootOfNativeAnonymousSubtree() ? nullptr : aContent->GetNextSibling(); return ContentRemoved(aContent, nextSibling, REMOVE_FOR_RECONSTRUCTION); } ////////////////////////////////////////////////////////////////////// // Block frame construction code already_AddRefed nsCSSFrameConstructor::GetFirstLetterStyle( nsIContent* aContent, ComputedStyle* aComputedStyle) { if (aContent) { return mPresShell->StyleSet()->ResolvePseudoElementStyle( *aContent->AsElement(), PseudoStyleType::firstLetter, aComputedStyle); } return nullptr; } already_AddRefed nsCSSFrameConstructor::GetFirstLineStyle( nsIContent* aContent, ComputedStyle* aComputedStyle) { if (aContent) { return mPresShell->StyleSet()->ResolvePseudoElementStyle( *aContent->AsElement(), PseudoStyleType::firstLine, aComputedStyle); } return nullptr; } // Predicate to see if a given content (block element) has // first-letter style applied to it. bool nsCSSFrameConstructor::ShouldHaveFirstLetterStyle( nsIContent* aContent, ComputedStyle* aComputedStyle) { return nsLayoutUtils::HasPseudoStyle(aContent, aComputedStyle, PseudoStyleType::firstLetter, mPresShell->GetPresContext()); } bool nsCSSFrameConstructor::HasFirstLetterStyle(nsIFrame* aBlockFrame) { MOZ_ASSERT(aBlockFrame, "Need a frame"); NS_ASSERTION(aBlockFrame->IsBlockFrameOrSubclass(), "Not a block frame?"); return aBlockFrame->HasAnyStateBits(NS_BLOCK_HAS_FIRST_LETTER_STYLE); } bool nsCSSFrameConstructor::ShouldHaveFirstLineStyle( nsIContent* aContent, ComputedStyle* aComputedStyle) { bool hasFirstLine = nsLayoutUtils::HasPseudoStyle( aContent, aComputedStyle, PseudoStyleType::firstLine, mPresShell->GetPresContext()); return hasFirstLine && !aContent->IsHTMLElement(nsGkAtoms::fieldset); } void nsCSSFrameConstructor::ShouldHaveSpecialBlockStyle( nsIContent* aContent, ComputedStyle* aComputedStyle, bool* aHaveFirstLetterStyle, bool* aHaveFirstLineStyle) { *aHaveFirstLetterStyle = ShouldHaveFirstLetterStyle(aContent, aComputedStyle); *aHaveFirstLineStyle = ShouldHaveFirstLineStyle(aContent, aComputedStyle); } /* static */ const nsCSSFrameConstructor::PseudoParentData nsCSSFrameConstructor::sPseudoParentData[eParentTypeCount] = { // Cell {{&nsCSSFrameConstructor::ConstructTableCell, FCDATA_IS_TABLE_PART | FCDATA_SKIP_FRAMESET | FCDATA_USE_CHILD_ITEMS | FCDATA_IS_WRAPPER_ANON_BOX | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRow)}, PseudoStyleType::tableCell}, // Row {{&nsCSSFrameConstructor::ConstructTableRowOrRowGroup, FCDATA_IS_TABLE_PART | FCDATA_SKIP_FRAMESET | FCDATA_USE_CHILD_ITEMS | FCDATA_IS_WRAPPER_ANON_BOX | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRowGroup)}, PseudoStyleType::tableRow}, // Row group {{&nsCSSFrameConstructor::ConstructTableRowOrRowGroup, FCDATA_IS_TABLE_PART | FCDATA_SKIP_FRAMESET | FCDATA_USE_CHILD_ITEMS | FCDATA_IS_WRAPPER_ANON_BOX | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable)}, PseudoStyleType::tableRowGroup}, // Column group {{ToCreationFunc(NS_NewTableColGroupFrame), FCDATA_IS_TABLE_PART | FCDATA_SKIP_FRAMESET | FCDATA_DISALLOW_OUT_OF_FLOW | FCDATA_USE_CHILD_ITEMS | FCDATA_SKIP_ABSPOS_PUSH | // Not FCDATA_IS_WRAPPER_ANON_BOX, because we don't need to // restyle these: they have non-inheriting styles. FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeTable)}, PseudoStyleType::tableColGroup}, // Table {{&nsCSSFrameConstructor::ConstructTable, FCDATA_SKIP_FRAMESET | FCDATA_USE_CHILD_ITEMS | FCDATA_IS_WRAPPER_ANON_BOX}, PseudoStyleType::table}, // Ruby {{ToCreationFunc(NS_NewRubyFrame), FCDATA_IS_LINE_PARTICIPANT | FCDATA_USE_CHILD_ITEMS | FCDATA_IS_WRAPPER_ANON_BOX | FCDATA_SKIP_FRAMESET}, PseudoStyleType::ruby}, // Ruby Base {{ToCreationFunc(NS_NewRubyBaseFrame), FCDATA_USE_CHILD_ITEMS | FCDATA_IS_LINE_PARTICIPANT | FCDATA_IS_WRAPPER_ANON_BOX | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRubyBaseContainer) | FCDATA_SKIP_FRAMESET}, PseudoStyleType::rubyBase}, // Ruby Base Container {{ToCreationFunc(NS_NewRubyBaseContainerFrame), FCDATA_USE_CHILD_ITEMS | FCDATA_IS_LINE_PARTICIPANT | FCDATA_IS_WRAPPER_ANON_BOX | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRuby) | FCDATA_SKIP_FRAMESET}, PseudoStyleType::rubyBaseContainer}, // Ruby Text {{ToCreationFunc(NS_NewRubyTextFrame), FCDATA_USE_CHILD_ITEMS | FCDATA_IS_LINE_PARTICIPANT | FCDATA_IS_WRAPPER_ANON_BOX | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRubyTextContainer) | FCDATA_SKIP_FRAMESET}, PseudoStyleType::rubyText}, // Ruby Text Container {{ToCreationFunc(NS_NewRubyTextContainerFrame), FCDATA_USE_CHILD_ITEMS | FCDATA_IS_WRAPPER_ANON_BOX | FCDATA_DESIRED_PARENT_TYPE_TO_BITS(eTypeRuby) | FCDATA_SKIP_FRAMESET}, PseudoStyleType::rubyTextContainer}}; void nsCSSFrameConstructor::CreateNeededAnonFlexOrGridItems( nsFrameConstructorState& aState, FrameConstructionItemList& aItems, nsIFrame* aParentFrame) { if (aItems.IsEmpty()) { return; } if (!aParentFrame->IsFlexOrGridContainer() && !aParentFrame->IsXULBoxFrame()) { return; } const bool isLegacyWebKitBox = IsFlexContainerForLegacyWebKitBox(aParentFrame); FCItemIterator iter(aItems); do { // Advance iter past children that don't want to be wrapped if (iter.SkipItemsThatDontNeedAnonFlexOrGridItem(aState, isLegacyWebKitBox)) { // Hit the end of the items without finding any remaining children that // need to be wrapped. We're finished! return; } // If our next potentially-wrappable child is whitespace, then see if // there's anything wrappable immediately after it. If not, we just drop // the whitespace and move on. (We're not supposed to create any anonymous // flex/grid items that _only_ contain whitespace). // (BUT if this is generated content, then we don't give whitespace nodes // any special treatment, because they're probably not really whitespace -- // they're just temporarily empty, waiting for their generated text.) // XXXdholbert If this node's generated text will *actually end up being // entirely whitespace*, then we technically should still skip over it, per // the CSS grid & flexbox specs. I'm not bothering with that at this point, // since it's a pretty extreme edge case. if (!aParentFrame->IsGeneratedContentFrame() && iter.item().IsWhitespace(aState)) { FCItemIterator afterWhitespaceIter(iter); bool hitEnd = afterWhitespaceIter.SkipWhitespace(aState); bool nextChildNeedsAnonItem = !hitEnd && afterWhitespaceIter.item().NeedsAnonFlexOrGridItem( aState, isLegacyWebKitBox); if (!nextChildNeedsAnonItem) { // There's nothing after the whitespace that we need to wrap, so we // just drop this run of whitespace. iter.DeleteItemsTo(this, afterWhitespaceIter); if (hitEnd) { // Nothing left to do -- we're finished! return; } // else, we have a next child and it does not want to be wrapped. So, // we jump back to the beginning of the loop to skip over that child // (and anything else non-wrappable after it) MOZ_ASSERT(!iter.IsDone() && !iter.item().NeedsAnonFlexOrGridItem( aState, isLegacyWebKitBox), "hitEnd and/or nextChildNeedsAnonItem lied"); continue; } } // Now |iter| points to the first child that needs to be wrapped in an // anonymous flex/grid item. Now we see how many children after it also want // to be wrapped in an anonymous flex/grid item. FCItemIterator endIter(iter); // iterator to find the end of the group endIter.SkipItemsThatNeedAnonFlexOrGridItem(aState, isLegacyWebKitBox); NS_ASSERTION(iter != endIter, "Should've had at least one wrappable child to seek past"); // Now, we create the anonymous flex or grid item to contain the children // between |iter| and |endIter|. nsIContent* parentContent = aParentFrame->GetContent(); RefPtr wrapperStyle = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle( PseudoStyleType::anonymousItem, aParentFrame->Style()); static constexpr FrameConstructionData sBlockFormattingContextFCData( ToCreationFunc(NS_NewBlockFormattingContext), FCDATA_SKIP_FRAMESET | FCDATA_USE_CHILD_ITEMS | FCDATA_IS_WRAPPER_ANON_BOX); FrameConstructionItem* newItem = new (this) FrameConstructionItem(&sBlockFormattingContextFCData, // Use the content of our parent frame parentContent, wrapperStyle.forget(), true); newItem->mIsAllInline = newItem->mComputedStyle->StyleDisplay()->IsInlineOutsideStyle(); newItem->mIsBlock = !newItem->mIsAllInline; MOZ_ASSERT(!newItem->mIsAllInline && newItem->mIsBlock, "expecting anonymous flex/grid items to be block-level " "(this will make a difference when we encounter " "'align-items: baseline')"); // Anonymous flex and grid items induce line boundaries around their // contents. newItem->mChildItems.SetLineBoundaryAtStart(true); newItem->mChildItems.SetLineBoundaryAtEnd(true); // The parent of the items in aItems is also the parent of the items // in mChildItems newItem->mChildItems.SetParentHasNoShadowDOM(aItems.ParentHasNoShadowDOM()); // Eat up all items between |iter| and |endIter| and put them in our // wrapper. This advances |iter| to point to |endIter|. iter.AppendItemsToList(this, endIter, newItem->mChildItems); iter.InsertItem(newItem); } while (!iter.IsDone()); } /* static */ nsCSSFrameConstructor::RubyWhitespaceType nsCSSFrameConstructor::ComputeRubyWhitespaceType(StyleDisplay aPrevDisplay, StyleDisplay aNextDisplay) { MOZ_ASSERT(nsStyleDisplay::IsRubyDisplayType(aPrevDisplay) && nsStyleDisplay::IsRubyDisplayType(aNextDisplay)); if (aPrevDisplay == aNextDisplay && (aPrevDisplay == StyleDisplay::RubyBase || aPrevDisplay == StyleDisplay::RubyText)) { return eRubyInterLeafWhitespace; } if (aNextDisplay == StyleDisplay::RubyText || aNextDisplay == StyleDisplay::RubyTextContainer) { return eRubyInterLevelWhitespace; } return eRubyInterSegmentWhitespace; } /** * This function checks the content from |aStartIter| to |aEndIter|, * determines whether it contains only whitespace, and if yes, * interprets the type of whitespace. This method does not change * any of the iters. */ /* static */ nsCSSFrameConstructor::RubyWhitespaceType nsCSSFrameConstructor::InterpretRubyWhitespace(nsFrameConstructorState& aState, const FCItemIterator& aStartIter, const FCItemIterator& aEndIter) { if (!aStartIter.item().IsWhitespace(aState)) { return eRubyNotWhitespace; } FCItemIterator spaceEndIter(aStartIter); spaceEndIter.SkipWhitespace(aState); if (spaceEndIter != aEndIter) { return eRubyNotWhitespace; } // Any leading or trailing whitespace in non-pseudo ruby box // should have been trimmed, hence there should not be any // whitespace at the start or the end. MOZ_ASSERT(!aStartIter.AtStart() && !aEndIter.IsDone()); FCItemIterator prevIter(aStartIter); prevIter.Prev(); return ComputeRubyWhitespaceType( prevIter.item().mComputedStyle->StyleDisplay()->mDisplay, aEndIter.item().mComputedStyle->StyleDisplay()->mDisplay); } /** * This function eats up consecutive items which do not want the current * parent into either a ruby base box or a ruby text box. When it * returns, |aIter| points to the first item it doesn't wrap. */ void nsCSSFrameConstructor::WrapItemsInPseudoRubyLeafBox( FCItemIterator& aIter, ComputedStyle* aParentStyle, nsIContent* aParentContent) { StyleDisplay parentDisplay = aParentStyle->StyleDisplay()->mDisplay; ParentType parentType, wrapperType; if (parentDisplay == StyleDisplay::RubyTextContainer) { parentType = eTypeRubyTextContainer; wrapperType = eTypeRubyText; } else { MOZ_ASSERT(parentDisplay == StyleDisplay::RubyBaseContainer); parentType = eTypeRubyBaseContainer; wrapperType = eTypeRubyBase; } MOZ_ASSERT(aIter.item().DesiredParentType() != parentType, "Should point to something needs to be wrapped."); FCItemIterator endIter(aIter); endIter.SkipItemsNotWantingParentType(parentType); WrapItemsInPseudoParent(aParentContent, aParentStyle, wrapperType, aIter, endIter); } /** * This function eats up consecutive items into a ruby level container. * It may create zero or one level container. When it returns, |aIter| * points to the first item it doesn't wrap. */ void nsCSSFrameConstructor::WrapItemsInPseudoRubyLevelContainer( nsFrameConstructorState& aState, FCItemIterator& aIter, ComputedStyle* aParentStyle, nsIContent* aParentContent) { MOZ_ASSERT(aIter.item().DesiredParentType() != eTypeRuby, "Pointing to a level container?"); FrameConstructionItem& firstItem = aIter.item(); ParentType wrapperType = firstItem.DesiredParentType(); if (wrapperType != eTypeRubyTextContainer) { // If the first item is not ruby text, // it should be in a base container. wrapperType = eTypeRubyBaseContainer; } FCItemIterator endIter(aIter); do { if (endIter.SkipItemsWantingParentType(wrapperType) || // If the skipping above stops at some item which wants a // different ruby parent, then we have finished. IsRubyParentType(endIter.item().DesiredParentType())) { // No more items need to be wrapped in this level container. break; } FCItemIterator contentEndIter(endIter); contentEndIter.SkipItemsNotWantingRubyParent(); // endIter must be on something doesn't want a ruby parent. MOZ_ASSERT(contentEndIter != endIter); // InterpretRubyWhitespace depends on the fact that any leading or // trailing whitespace described in the spec have been trimmed at // this point. With this precondition, it is safe not to check // whether contentEndIter has been done. RubyWhitespaceType whitespaceType = InterpretRubyWhitespace(aState, endIter, contentEndIter); if (whitespaceType == eRubyInterLevelWhitespace) { // Remove inter-level whitespace. bool atStart = (aIter == endIter); endIter.DeleteItemsTo(this, contentEndIter); if (atStart) { aIter = endIter; } } else if (whitespaceType == eRubyInterSegmentWhitespace) { // If this level container starts with inter-segment whitespaces, // wrap them. Break at contentEndIter. Otherwise, leave it here. // Break at endIter. They will be wrapped when we are here again. if (aIter == endIter) { MOZ_ASSERT(wrapperType == eTypeRubyBaseContainer, "Inter-segment whitespace should be wrapped in rbc"); endIter = contentEndIter; } break; } else if (wrapperType == eTypeRubyTextContainer && whitespaceType != eRubyInterLeafWhitespace) { // Misparented inline content that's not inter-annotation // whitespace doesn't belong in a pseudo ruby text container. // Break at endIter. break; } else { endIter = contentEndIter; } } while (!endIter.IsDone()); // It is possible that everything our parent wants us to wrap is // simply an inter-level whitespace, which has been trimmed, or // an inter-segment whitespace, which will be wrapped later. // In those cases, don't create anything. if (aIter != endIter) { WrapItemsInPseudoParent(aParentContent, aParentStyle, wrapperType, aIter, endIter); } } /** * This function trims leading and trailing whitespaces * in the given item list. */ void nsCSSFrameConstructor::TrimLeadingAndTrailingWhitespaces( nsFrameConstructorState& aState, FrameConstructionItemList& aItems) { FCItemIterator iter(aItems); if (!iter.IsDone() && iter.item().IsWhitespace(aState)) { FCItemIterator spaceEndIter(iter); spaceEndIter.SkipWhitespace(aState); iter.DeleteItemsTo(this, spaceEndIter); } iter.SetToEnd(); if (!iter.AtStart()) { FCItemIterator spaceEndIter(iter); do { iter.Prev(); if (iter.AtStart()) { // It's fine to not check the first item, because we // should have trimmed leading whitespaces above. break; } } while (iter.item().IsWhitespace(aState)); iter.Next(); if (iter != spaceEndIter) { iter.DeleteItemsTo(this, spaceEndIter); } } } /** * This function walks through the child list (aItems) and creates * needed pseudo ruby boxes to wrap misparented children. */ void nsCSSFrameConstructor::CreateNeededPseudoInternalRubyBoxes( nsFrameConstructorState& aState, FrameConstructionItemList& aItems, nsIFrame* aParentFrame) { const ParentType ourParentType = GetParentType(aParentFrame); if (!IsRubyParentType(ourParentType) || aItems.AllWantParentType(ourParentType)) { return; } if (!IsRubyPseudo(aParentFrame) || ourParentType == eTypeRuby /* for 'display:block ruby' */) { // Normally, ruby pseudo frames start from and end at some elements, // which means they don't have leading and trailing whitespaces at // all. But there are two cases where they do actually have leading // or trailing whitespaces: // 1. It is an inter-segment whitespace which in an individual ruby // base container. // 2. The pseudo frame starts from or ends at consecutive inline // content, which is not pure whitespace, but includes some. // In either case, the whitespaces are not the leading or trailing // whitespaces defined in the spec, and thus should not be trimmed. TrimLeadingAndTrailingWhitespaces(aState, aItems); } FCItemIterator iter(aItems); nsIContent* parentContent = aParentFrame->GetContent(); ComputedStyle* parentStyle = aParentFrame->Style(); while (!iter.IsDone()) { if (!iter.SkipItemsWantingParentType(ourParentType)) { if (ourParentType == eTypeRuby) { WrapItemsInPseudoRubyLevelContainer(aState, iter, parentStyle, parentContent); } else { WrapItemsInPseudoRubyLeafBox(iter, parentStyle, parentContent); } } } } /* * This function works as follows: we walk through the child list (aItems) and * find items that cannot have aParentFrame as their parent. We wrap * continuous runs of such items into a FrameConstructionItem for a frame that * gets them closer to their desired parents. For example, a run of non-row * children of a row-group will get wrapped in a row. When we later construct * the frame for this wrapper (in this case for the row), it'll be the correct * parent for the cells in the set of items we wrapped or we'll wrap cells * around everything else. At the end of this method, aItems is guaranteed to * contain only items for frames that can be direct kids of aParentFrame. */ void nsCSSFrameConstructor::CreateNeededPseudoContainers( nsFrameConstructorState& aState, FrameConstructionItemList& aItems, nsIFrame* aParentFrame) { ParentType ourParentType = GetParentType(aParentFrame); if (IsRubyParentType(ourParentType) || aItems.AllWantParentType(ourParentType)) { // Nothing to do here return; } FCItemIterator iter(aItems); do { if (iter.SkipItemsWantingParentType(ourParentType)) { // Nothing else to do here; we're finished return; } // Now we're pointing to the first child that wants a different parent // type. // Now try to figure out what kids we can group together. We can generally // group everything that has a different desired parent type from us. Two // exceptions to this: // 1) If our parent type is table, we can't group columns with anything // else other than whitespace. // 2) Whitespace that lies between two things we can group which both want // a non-block parent should be dropped, even if we can't group them // with each other and even if the whitespace wants a parent of // ourParentType. Ends of the list count as things that don't want a // block parent (so that for example we'll drop a whitespace-only list). FCItemIterator endIter(iter); /* iterator to find the end of the group */ ParentType groupingParentType = endIter.item().DesiredParentType(); if (aItems.AllWantParentType(groupingParentType) && groupingParentType != eTypeBlock) { // Just group them all and be done with it. We need the check for // eTypeBlock here to catch the "all the items are whitespace" case // described above. endIter.SetToEnd(); } else { // Locate the end of the group. // Keep track of the type the previous item wanted, in case we have to // deal with whitespace. Start it off with ourParentType, since that's // the last thing |iter| would have skipped over. ParentType prevParentType = ourParentType; do { // Walk an iterator past any whitespace that we might be able to drop // from the list FCItemIterator spaceEndIter(endIter); if (prevParentType != eTypeBlock && !aParentFrame->IsGeneratedContentFrame() && spaceEndIter.item().IsWhitespace(aState)) { bool trailingSpaces = spaceEndIter.SkipWhitespace(aState); // We drop the whitespace in the following cases: // 1) If these are not trailing spaces and the next item wants a table // or table-part parent // 2) If these are trailing spaces and aParentFrame is a // tabular container according to rule 1.3 of CSS 2.1 Sec 17.2.1. // (Being a tabular container pretty much means ourParentType is // not eTypeBlock besides the eTypeColGroup case, which won't // reach here.) if ((!trailingSpaces && IsTableParentType(spaceEndIter.item().DesiredParentType())) || (trailingSpaces && ourParentType != eTypeBlock)) { bool updateStart = (iter == endIter); endIter.DeleteItemsTo(this, spaceEndIter); NS_ASSERTION(trailingSpaces == endIter.IsDone(), "These should match"); if (updateStart) { iter = endIter; } if (trailingSpaces) { break; /* Found group end */ } if (updateStart) { // Update groupingParentType, since it might have been eTypeBlock // just because of the whitespace. groupingParentType = iter.item().DesiredParentType(); } } } // Now endIter points to a non-whitespace item or a non-droppable // whitespace item. In the latter case, if this is the end of the group // we'll traverse this whitespace again. But it'll all just be quick // DesiredParentType() checks which will match ourParentType (that's // what it means that this is the group end), so it's OK. // However, when we are grouping a ruby parent, and endIter points to // a non-droppable whitespace, if the next non-whitespace item also // wants a ruby parent, the whitespace should also be included into // the current ruby container. prevParentType = endIter.item().DesiredParentType(); if (prevParentType == ourParentType && (endIter == spaceEndIter || spaceEndIter.IsDone() || !IsRubyParentType(groupingParentType) || !IsRubyParentType(spaceEndIter.item().DesiredParentType()))) { // End the group at endIter. break; } if (ourParentType == eTypeTable && (prevParentType == eTypeColGroup) != (groupingParentType == eTypeColGroup)) { // Either we started with columns and now found something else, or // vice versa. In any case, end the grouping. break; } // If we have some whitespace that we were not able to drop and there is // an item after the whitespace that is already properly parented, then // make sure to include the spaces in our group but stop the group after // that. if (spaceEndIter != endIter && !spaceEndIter.IsDone() && ourParentType == spaceEndIter.item().DesiredParentType()) { endIter = spaceEndIter; break; } // Include the whitespace we didn't drop (if any) in the group. endIter = spaceEndIter; prevParentType = endIter.item().DesiredParentType(); endIter.Next(); } while (!endIter.IsDone()); } if (iter == endIter) { // Nothing to wrap here; just skipped some whitespace continue; } // Now group together all the items between iter and endIter. The right // parent type to use depends on ourParentType. ParentType wrapperType; switch (ourParentType) { case eTypeRow: // The parent type for a cell is eTypeBlock, since that's what a cell // looks like to its kids. wrapperType = eTypeBlock; break; case eTypeRowGroup: wrapperType = eTypeRow; break; case eTypeTable: // Either colgroup or rowgroup, depending on what we're grouping. wrapperType = groupingParentType == eTypeColGroup ? eTypeColGroup : eTypeRowGroup; break; case eTypeColGroup: MOZ_CRASH("Colgroups should be suppresing non-col child items"); default: NS_ASSERTION(ourParentType == eTypeBlock, "Unrecognized parent type"); if (IsRubyParentType(groupingParentType)) { wrapperType = eTypeRuby; } else { NS_ASSERTION(IsTableParentType(groupingParentType), "groupingParentType should be either Ruby or table"); wrapperType = eTypeTable; } } ComputedStyle* parentStyle = aParentFrame->Style(); WrapItemsInPseudoParent(aParentFrame->GetContent(), parentStyle, wrapperType, iter, endIter); // Now |iter| points to the item that was the first one we didn't wrap; // loop and see whether we need to skip it or wrap it in something // different. } while (!iter.IsDone()); } /** * This method wraps frame construction item from |aIter| to * |aEndIter|. After it returns, aIter points to the first item * after the wrapper. */ void nsCSSFrameConstructor::WrapItemsInPseudoParent( nsIContent* aParentContent, ComputedStyle* aParentStyle, ParentType aWrapperType, FCItemIterator& aIter, const FCItemIterator& aEndIter) { const PseudoParentData& pseudoData = sPseudoParentData[aWrapperType]; PseudoStyleType pseudoType = pseudoData.mPseudoType; auto& parentDisplay = *aParentStyle->StyleDisplay(); auto parentDisplayInside = parentDisplay.DisplayInside(); // XXXmats should we use IsInlineInsideStyle() here instead? seems odd to // exclude RubyBaseContainer/RubyTextContainer... if (pseudoType == PseudoStyleType::table && (parentDisplay.IsInlineFlow() || parentDisplayInside == StyleDisplayInside::RubyBase || parentDisplayInside == StyleDisplayInside::RubyText)) { pseudoType = PseudoStyleType::inlineTable; } RefPtr wrapperStyle; if (pseudoData.mFCData.mBits & FCDATA_IS_WRAPPER_ANON_BOX) { wrapperStyle = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle( pseudoType, aParentStyle); } else { wrapperStyle = mPresShell->StyleSet()->ResolveNonInheritingAnonymousBoxStyle( pseudoType); } FrameConstructionItem* newItem = new (this) FrameConstructionItem(&pseudoData.mFCData, // Use the content of our parent frame aParentContent, wrapperStyle.forget(), true); const nsStyleDisplay* disp = newItem->mComputedStyle->StyleDisplay(); // Here we're cheating a tad... technically, table-internal items should be // inline if aParentFrame is inline, but they'll get wrapped in an // inline-table in the end, so it'll all work out. In any case, arguably // we don't need to maintain this state at this point... but it's better // to, I guess. newItem->mIsAllInline = disp->IsInlineOutsideStyle(); bool isRuby = disp->IsRubyDisplayType(); if (!isRuby) { // Table pseudo frames always induce line boundaries around their // contents. newItem->mChildItems.SetLineBoundaryAtStart(true); newItem->mChildItems.SetLineBoundaryAtEnd(true); } // The parent of the items in aItems is also the parent of the items // in mChildItems newItem->mChildItems.SetParentHasNoShadowDOM( aIter.List()->ParentHasNoShadowDOM()); // Eat up all items between |aIter| and |aEndIter| and put them in our // wrapper Advances |aIter| to point to |aEndIter|. aIter.AppendItemsToList(this, aEndIter, newItem->mChildItems); aIter.InsertItem(newItem); } void nsCSSFrameConstructor::CreateNeededPseudoSiblings( nsFrameConstructorState& aState, FrameConstructionItemList& aItems, nsIFrame* aParentFrame) { if (aItems.IsEmpty() || GetParentType(aParentFrame) != eTypeRuby) { return; } FCItemIterator iter(aItems); StyleDisplay firstDisplay = iter.item().mComputedStyle->StyleDisplay()->mDisplay; if (firstDisplay == StyleDisplay::RubyBaseContainer) { return; } NS_ASSERTION(firstDisplay == StyleDisplay::RubyTextContainer, "Child of ruby frame should either a rbc or a rtc"); const PseudoParentData& pseudoData = sPseudoParentData[eTypeRubyBaseContainer]; RefPtr pseudoStyle = mPresShell->StyleSet()->ResolveInheritingAnonymousBoxStyle( pseudoData.mPseudoType, aParentFrame->Style()); FrameConstructionItem* newItem = new (this) FrameConstructionItem( &pseudoData.mFCData, // Use the content of the parent frame aParentFrame->GetContent(), pseudoStyle.forget(), true); newItem->mIsAllInline = true; newItem->mChildItems.SetParentHasNoShadowDOM(true); iter.InsertItem(newItem); } #ifdef DEBUG /** * Returns true iff aFrame should be wrapped in an anonymous flex/grid item, * rather than being a direct child of aContainerFrame. * * NOTE: aContainerFrame must be a flex or grid container - this function is * purely for sanity-checking the children of these container types. * NOTE: See also NeedsAnonFlexOrGridItem(), for the non-debug version of this * logic (which operates a bit earlier, on FCData instead of frames). */ static bool FrameWantsToBeInAnonymousItem(const nsIFrame* aContainerFrame, const nsIFrame* aFrame) { MOZ_ASSERT(aContainerFrame->IsFlexOrGridContainer() || aContainerFrame->IsXULBoxFrame()); // Any line-participant frames (e.g. text) definitely want to be wrapped in // an anonymous flex/grid item. if (aFrame->IsFrameOfType(nsIFrame::eLineParticipant)) { return true; } // If the container is a -webkit-{inline-}box container, then placeholders // also need to be wrapped, for compatibility. if (IsFlexContainerForLegacyWebKitBox(aContainerFrame) && aFrame->IsPlaceholderFrame()) { return true; } return false; } #endif static void VerifyGridFlexContainerChildren(nsIFrame* aParentFrame, const nsFrameList& aChildren) { #ifdef DEBUG if (!aParentFrame->IsFlexOrGridContainer() && !aParentFrame->IsXULBoxFrame()) { return; } bool prevChildWasAnonItem = false; for (const nsIFrame* child : aChildren) { MOZ_ASSERT(!FrameWantsToBeInAnonymousItem(aParentFrame, child), "frame wants to be inside an anonymous item, but it isn't"); if (IsAnonymousItem(child)) { AssertAnonymousFlexOrGridItemParent(child, aParentFrame); MOZ_ASSERT(!prevChildWasAnonItem, "two anon items in a row"); nsIFrame* firstWrappedChild = child->PrincipalChildList().FirstChild(); MOZ_ASSERT(firstWrappedChild, "anonymous item shouldn't be empty"); prevChildWasAnonItem = true; } else { prevChildWasAnonItem = false; } } #endif } static bool FrameHasOnlyPlaceholderPrevSiblings(const nsIFrame* aFrame) { // Check for prev siblings, ignoring placeholder frames. MOZ_ASSERT(aFrame, "frame must not be null"); const nsIFrame* prevSibling = aFrame; do { prevSibling = prevSibling->GetPrevSibling(); } while (prevSibling && prevSibling->IsPlaceholderFrame()); return !prevSibling; } static bool FrameHasOnlyPlaceholderNextSiblings(const nsIFrame* aFrame) { // Check for next siblings, ignoring placeholder frames. MOZ_ASSERT(aFrame, "frame must not be null"); const nsIFrame* nextSibling = aFrame; do { nextSibling = nextSibling->GetNextSibling(); } while (nextSibling && nextSibling->IsPlaceholderFrame()); return !nextSibling; } static void SetPageValues(nsIFrame* const aFrame, const nsAtom* const aAutoValue, const nsAtom* const aStartValue, const nsAtom* const aEndValue) { MOZ_ASSERT(aAutoValue, "Auto page value should never be null"); MOZ_ASSERT(aStartValue || aEndValue, "Should not have called with no values"); nsIFrame::PageValues* pageValues = aFrame->GetProperty(nsIFrame::PageValuesProperty()); if (aStartValue) { if (aStartValue == aAutoValue) { // If the page value struct already exists, set the start value to null // to indicate the auto value. if (pageValues) { pageValues->mStartPageValue = nullptr; } } else { // The start value is not auto, so we need to store it, creating the // page values struct if it does not already exist. if (!pageValues) { pageValues = new nsIFrame::PageValues(); aFrame->SetProperty(nsIFrame::PageValuesProperty(), pageValues); } pageValues->mStartPageValue = aStartValue; } } if (aEndValue) { if (aEndValue == aAutoValue) { // If the page value struct already exists, set the end value to null // to indicate the auto value. if (pageValues) { pageValues->mEndPageValue = nullptr; } } else { // The end value is not auto, so we need to store it, creating the // page values struct if it does not already exist. if (!pageValues) { pageValues = new nsIFrame::PageValues(); aFrame->SetProperty(nsIFrame::PageValuesProperty(), pageValues); } pageValues->mEndPageValue = aEndValue; } } } inline void nsCSSFrameConstructor::ConstructFramesFromItemList( nsFrameConstructorState& aState, FrameConstructionItemList& aItems, nsContainerFrame* aParentFrame, bool aParentIsWrapperAnonBox, nsFrameList& aFrameList) { #ifdef DEBUG if (aParentFrame->StyleContent()->mContent.IsNone() && StaticPrefs::layout_css_element_content_none_enabled()) { for (FCItemIterator iter(aItems); !iter.IsDone(); iter.Next()) { MOZ_ASSERT(iter.item().mContent->IsInNativeAnonymousSubtree() || iter.item().mComputedStyle->IsPseudoOrAnonBox()); } } // The assertion condition should match the logic in // MaybePushFloatContainingBlock(). MOZ_ASSERT(!(ShouldSuppressFloatingOfDescendants(aParentFrame) || aParentFrame->IsFloatContainingBlock()) || aState.mFloatCBCandidate == aParentFrame, "Our caller or ProcessChildren()'s caller should call " "MaybePushFloatContainingBlock() to handle the float containing " "block candidate!"); aState.mFloatCBCandidate = nullptr; #endif // Ensure aParentIsWrapperAnonBox is correct. We _could_ compute it directly, // but it would be a bit slow, which is why we pass it from callers, who have // that information offhand in many cases. MOZ_ASSERT(ParentIsWrapperAnonBox(aParentFrame) == aParentIsWrapperAnonBox); // Note: we explicitly exclude TableColGroupFrame because it doesn't // have the FCDATA_IS_WRAPPER_ANON_BOX on pseudos so aParentIsWrapperAnonBox // is false for such pseudos (see sPseudoParentData below). if (!aParentIsWrapperAnonBox && aState.mHasRenderedLegend && aParentFrame->GetContent()->IsHTMLElement(nsGkAtoms::fieldset) && !aParentFrame->IsTableColGroupFrame()) { DebugOnly found = false; for (FCItemIterator iter(aItems); !iter.IsDone(); iter.Next()) { if (iter.item().mIsRenderedLegend) { // This makes the rendered legend the first frame in the fieldset child // list which makes keyboard traversal follow the visual order. nsFieldSetFrame* fieldSetFrame = GetFieldSetFrameFor(aParentFrame); nsFrameList renderedLegend; ConstructFramesFromItem(aState, iter, fieldSetFrame, renderedLegend); MOZ_ASSERT(renderedLegend.OnlyChild(), "a rendered legend should have exactly one frame"); fieldSetFrame->InsertFrames(FrameChildListID::Principal, nullptr, nullptr, std::move(renderedLegend)); FCItemIterator next = iter; next.Next(); iter.DeleteItemsTo(this, next); found = true; break; } } MOZ_ASSERT(found, "should have found our rendered legend"); } CreateNeededPseudoContainers(aState, aItems, aParentFrame); CreateNeededAnonFlexOrGridItems(aState, aItems, aParentFrame); CreateNeededPseudoInternalRubyBoxes(aState, aItems, aParentFrame); CreateNeededPseudoSiblings(aState, aItems, aParentFrame); for (FCItemIterator iter(aItems); !iter.IsDone(); iter.Next()) { MOZ_ASSERT(!iter.item().mIsRenderedLegend, "Only one item can be the rendered legend, " "and it should've been handled above"); NS_ASSERTION(iter.item().DesiredParentType() == GetParentType(aParentFrame), "Needed pseudos didn't get created; expect bad things"); ConstructFramesFromItem(aState, iter, aParentFrame, aFrameList); } VerifyGridFlexContainerChildren(aParentFrame, aFrameList); // Calculate and propagate page-name values for each frame in the frame list. // This will be affected by https://bugzilla.mozilla.org/1782597 if (aState.mPresContext->IsPaginated() && StaticPrefs::layout_css_named_pages_enabled() && aParentFrame->IsBlockFrameOrSubclass()) { // Set the start/end page values while iterating the frame list, to walk // up the frame tree only once after iterating the frame list. // This also avoids extra property lookups on these frames. MOZ_ASSERT(aState.mAutoPageNameValue == aParentFrame->GetAutoPageValue(), "aState.mAutoPageNameValue should have been equivalent to " "the auto value stored on our parent frame."); // Even though we store null for page values that equal the "auto" resolved // value on frames, we always want startPageValue/endPageValue to be the // actual atoms reflecting the start/end values. This is because when we // propagate the values up the frame tree, we will need to compare them to // the auto value for each ancestor. This value might be different than the // auto value for this frame. const nsAtom* startPageValue = nullptr; const nsAtom* endPageValue = nullptr; for (nsIFrame* f : aFrameList) { if (f->IsPlaceholderFrame()) { continue; } // Resolve auto against the parent frame's used page name, which has been // determined and set on aState.mAutoPageNameValue. If this item is not // block-level then we use the value that auto resolves to. // // This is to achieve the propagation behavior described in the spec: // // "A start page value and end page value is determined for each box as // the value (if any) propagated from its first or last child box // (respectively), else the used value on the box itself." // // "A child propagates its own start or end page value if and only if the // page property applies to it." // // The page property only applies to "boxes that create class A break // points". When taken together, this means that non block-level children // do not propagate start/end page values, and instead we use "the used // value on the box itself", the "box itself" being aParentFrame. This // value has been determined and saved as aState.mAutoPageNameValue // // https://www.w3.org/TR/css-page-3/#using-named-pages // https://www.w3.org/TR/css-break-3/#btw-blocks const StylePageName& pageName = f->StylePage()->mPage; const nsAtom* const pageNameAtom = (pageName.IsPageName() && f->IsBlockOutside()) ? pageName.AsPageName().AsAtom() : aState.mAutoPageNameValue; nsIFrame::PageValues* pageValues = f->GetProperty(nsIFrame::PageValuesProperty()); // If this frame has any children, it will already have had its page // values set at this point. However, if no page values have been set, // we must ensure that the appropriate PageValuesProperty value has been // set. // If the page name is equal to the auto value, then PageValuesProperty // should remain null to indicate that the start/end values are both // equal to the auto value. if (pageNameAtom != aState.mAutoPageNameValue && !pageValues) { pageValues = new nsIFrame::PageValues{pageNameAtom, pageNameAtom}; f->SetProperty(nsIFrame::PageValuesProperty(), pageValues); } // We don't want to use GetStartPageValue() or GetEndPageValue(), as each // requires a property lookup which we can avoid here. if (!startPageValue) { startPageValue = (pageValues && pageValues->mStartPageValue) ? pageValues->mStartPageValue.get() : aState.mAutoPageNameValue; } endPageValue = (pageValues && pageValues->mEndPageValue) ? pageValues->mEndPageValue.get() : aState.mAutoPageNameValue; MOZ_ASSERT(startPageValue && endPageValue, "Should have found start/end page value"); } MOZ_ASSERT(!startPageValue == !endPageValue, "Should have set both or neither page values"); if (startPageValue) { // Walk up the frame tree from our parent frame, propagating start and // end page values. // As we go, if we find that, for a frame, we are not contributing one of // the start/end page values, then our subtree will not contribute this // value from that frame onward. startPageValue/endPageValue are set to // null to indicate this. // Stop iterating when we are not contributing either start or end // values, when we hit the root frame (no parent), or when we find a // frame that is not a block frame. for (nsContainerFrame* ancestorFrame = aParentFrame; (startPageValue || endPageValue) && ancestorFrame && ancestorFrame->IsBlockFrameOrSubclass(); ancestorFrame = ancestorFrame->GetParent()) { MOZ_ASSERT(!ancestorFrame->GetPrevInFlow(), "Should not have fragmentation yet"); MOZ_ASSERT(ancestorFrame->mWasVisitedByAutoFrameConstructionPageName, "Frame should have been visited by " "AutoFrameConstructionPageName"); { // Get what the auto value is, based on this frame's parent. // For the root frame, `auto` resolves to the empty atom. const nsContainerFrame* const parent = ancestorFrame->GetParent(); const nsAtom* const parentAuto = MOZ_LIKELY(parent) ? parent->GetAutoPageValue() : nsGkAtoms::_empty; SetPageValues(ancestorFrame, parentAuto, startPageValue, endPageValue); } // Once we stop contributing start/end values, we know there is a // sibling subtree that contributed that value to our shared parent // instead of our starting frame's subtree. This means once // startPageValue/endPageValue becomes null, indicating that we are no // longer contributing that page value, it should stay null and we no // longer need to check for siblings in that direction. if (startPageValue && !FrameHasOnlyPlaceholderPrevSiblings(ancestorFrame)) { startPageValue = nullptr; } if (endPageValue && !FrameHasOnlyPlaceholderNextSiblings(ancestorFrame)) { endPageValue = nullptr; } } } } if (aParentIsWrapperAnonBox) { for (nsIFrame* f : aFrameList) { f->SetParentIsWrapperAnonBox(); } } } void nsCSSFrameConstructor::AddFCItemsForAnonymousContent( nsFrameConstructorState& aState, nsContainerFrame* aFrame, const nsTArray& aAnonymousItems, FrameConstructionItemList& aItemsToConstruct, const AutoFrameConstructionPageName&) { for (const auto& info : aAnonymousItems) { nsIContent* content = info.mContent; // Gecko-styled nodes should have no pending restyle flags. // Assert some things about this content MOZ_ASSERT(!(content->GetFlags() & (NODE_DESCENDANTS_NEED_FRAMES | NODE_NEEDS_FRAME)), "Should not be marked as needing frames"); MOZ_ASSERT(!content->GetPrimaryFrame(), "Should have no existing frame"); MOZ_ASSERT(!content->IsComment() && !content->IsProcessingInstruction(), "Why is someone creating garbage anonymous content"); // Make sure we eagerly performed the servo cascade when the anonymous // nodes were created. MOZ_ASSERT(!content->IsElement() || content->AsElement()->HasServoData()); RefPtr computedStyle = ResolveComputedStyle(content); AddFrameConstructionItemsInternal(aState, content, aFrame, true, computedStyle, {ItemFlag::AllowPageBreak}, aItemsToConstruct); } } void nsCSSFrameConstructor::ProcessChildren( nsFrameConstructorState& aState, nsIContent* aContent, ComputedStyle* aComputedStyle, nsContainerFrame* aFrame, const bool aCanHaveGeneratedContent, nsFrameList& aFrameList, const bool aAllowBlockStyles, nsIFrame* aPossiblyLeafFrame) { MOZ_ASSERT(aFrame, "Must have parent frame here"); MOZ_ASSERT(aFrame->GetContentInsertionFrame() == aFrame, "Parent frame in ProcessChildren should be its own " "content insertion frame"); const uint32_t kMaxDepth = 2 * MAX_REFLOW_DEPTH; static_assert(kMaxDepth <= UINT16_MAX, "mCurrentDepth type is too narrow"); AutoRestore savedDepth(mCurrentDepth); if (mCurrentDepth != UINT16_MAX) { ++mCurrentDepth; } if (!aPossiblyLeafFrame) { aPossiblyLeafFrame = aFrame; } // XXXbz ideally, this would do all the pushing of various // containing blocks as needed, so callers don't have to do it... // Check that our parent frame is a block before allowing ::first-letter/line. // E.g.