nsPlaceholderFrame.cpp (9318B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* 8 * rendering object for the point that anchors out-of-flow rendering 9 * objects such as floats and absolutely positioned elements 10 */ 11 12 #include "nsPlaceholderFrame.h" 13 14 #include "gfxContext.h" 15 #include "gfxUtils.h" 16 #include "mozilla/PresShell.h" 17 #include "mozilla/PresShellInlines.h" 18 #include "mozilla/ServoStyleSetInlines.h" 19 #include "mozilla/dom/ElementInlines.h" 20 #include "mozilla/gfx/2D.h" 21 #include "nsCSSFrameConstructor.h" 22 #include "nsDisplayList.h" 23 #include "nsIContentInlines.h" 24 #include "nsIFrameInlines.h" 25 #include "nsLayoutUtils.h" 26 #include "nsPresContext.h" 27 #include "nsPresContextInlines.h" 28 29 using namespace mozilla; 30 using namespace mozilla::dom; 31 using namespace mozilla::gfx; 32 33 nsPlaceholderFrame* NS_NewPlaceholderFrame(PresShell* aPresShell, 34 ComputedStyle* aStyle, 35 nsFrameState aTypeBits) { 36 return new (aPresShell) 37 nsPlaceholderFrame(aStyle, aPresShell->GetPresContext(), aTypeBits); 38 } 39 40 NS_IMPL_FRAMEARENA_HELPERS(nsPlaceholderFrame) 41 42 #ifdef DEBUG 43 NS_QUERYFRAME_HEAD(nsPlaceholderFrame) 44 NS_QUERYFRAME_ENTRY(nsPlaceholderFrame) 45 NS_QUERYFRAME_TAIL_INHERITING(nsIFrame) 46 #endif 47 48 /* virtual */ 49 void nsPlaceholderFrame::AddInlineMinISize(const IntrinsicSizeInput& aInput, 50 InlineMinISizeData* aData) { 51 // Override AddInlineMinISize so that *nothing* happens. In 52 // particular, we don't want to zero out |aData->mTrailingWhitespace|, 53 // since nsLineLayout skips placeholders when trimming trailing 54 // whitespace, and we don't want to set aData->mSkipWhitespace to 55 // false. 56 57 // ...but push floats onto aData's list. 58 AddFloatToIntrinsicISizeData(aInput, IntrinsicISizeType::MinISize, aData); 59 } 60 61 /* virtual */ 62 void nsPlaceholderFrame::AddInlinePrefISize(const IntrinsicSizeInput& aInput, 63 InlinePrefISizeData* aData) { 64 // Override AddInlinePrefISize so that *nothing* happens. In 65 // particular, we don't want to zero out |aData->mTrailingWhitespace|, 66 // since nsLineLayout skips placeholders when trimming trailing 67 // whitespace, and we don't want to set aData->mSkipWhitespace to 68 // false. 69 70 // ...but push floats onto aData's list. 71 AddFloatToIntrinsicISizeData(aInput, IntrinsicISizeType::PrefISize, aData); 72 } 73 74 void nsPlaceholderFrame::AddFloatToIntrinsicISizeData( 75 const IntrinsicSizeInput& aInput, IntrinsicISizeType aType, 76 InlineIntrinsicISizeData* aData) const { 77 if (mOutOfFlowFrame->IsFloating()) { 78 const IntrinsicSizeInput floatInput( 79 aInput, mOutOfFlowFrame->GetWritingMode(), GetWritingMode()); 80 const nscoord floatISize = nsLayoutUtils::IntrinsicForContainer( 81 floatInput.mContext, mOutOfFlowFrame, aType, 82 floatInput.mPercentageBasisForChildren); 83 aData->mFloats.EmplaceBack(mOutOfFlowFrame, floatISize); 84 } 85 } 86 87 void nsPlaceholderFrame::Reflow(nsPresContext* aPresContext, 88 ReflowOutput& aDesiredSize, 89 const ReflowInput& aReflowInput, 90 nsReflowStatus& aStatus) { 91 // NOTE that the ReflowInput passed to this method is not fully initialized, 92 // on the grounds that reflowing a placeholder is a rather trivial operation. 93 // (See bug 1367711.) 94 95 #ifdef DEBUG 96 // We should be getting reflowed before our out-of-flow. If this is our first 97 // reflow, and our out-of-flow has already received its first reflow (before 98 // us), complain. 99 // 100 // Popups are an exception though, because their position doesn't depend on 101 // the placeholder, so they don't have this requirement (and this condition 102 // doesn't hold anyways because the default popupgroup goes before than the 103 // default tooltip, for example). Same for the backdrop. 104 // TODO(emilio): All top layer nodes technically can hit this, but their 105 // static pos is supposed to be 0, 0, see 106 // https://github.com/w3c/csswg-drafts/issues/9939. 107 // 108 // We also have an exception if the out-of-flow created an orthogonal flow, 109 // because in this case we may have needed to do a measuring reflow during 110 // intrinsic size computation. That's OK because it does not depend on the 111 // placeholder being reflowed first. 112 if (HasAnyStateBits(NS_FRAME_FIRST_REFLOW) && 113 !mOutOfFlowFrame->IsMenuPopupFrame() && 114 mOutOfFlowFrame->Style()->GetPseudoType() != PseudoStyleType::backdrop && 115 !mOutOfFlowFrame->HasAnyStateBits(NS_FRAME_FIRST_REFLOW) && 116 !mOutOfFlowFrame->GetWritingMode().IsOrthogonalTo(GetWritingMode())) { 117 // Unfortunately, this can currently happen when the placeholder is in a 118 // later continuation or later IB-split sibling than its out-of-flow (as 119 // is the case in some of our existing unit tests). So for now, in that 120 // case, we'll warn instead of asserting. 121 bool isInContinuationOrIBSplit = false; 122 nsIFrame* ancestor = this; 123 while ((ancestor = ancestor->GetParent())) { 124 if (nsLayoutUtils::GetPrevContinuationOrIBSplitSibling(ancestor)) { 125 isInContinuationOrIBSplit = true; 126 break; 127 } 128 } 129 130 if (isInContinuationOrIBSplit) { 131 NS_WARNING("Out-of-flow frame got reflowed before its placeholder"); 132 } else { 133 NS_ERROR("Out-of-flow frame got reflowed before its placeholder"); 134 } 135 } 136 #endif 137 138 MarkInReflow(); 139 DO_GLOBAL_REFLOW_COUNT("nsPlaceholderFrame"); 140 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!"); 141 aDesiredSize.ClearSize(); 142 } 143 144 static FrameChildListID ChildListIDForOutOfFlow(nsFrameState aPlaceholderState, 145 const nsIFrame* aChild) { 146 if (aPlaceholderState & PLACEHOLDER_FOR_FLOAT) { 147 return FrameChildListID::Float; 148 } 149 if (aPlaceholderState & PLACEHOLDER_FOR_FIXEDPOS) { 150 return nsLayoutUtils::MayBeReallyFixedPos(aChild) 151 ? FrameChildListID::Fixed 152 : FrameChildListID::Absolute; 153 } 154 if (aPlaceholderState & PLACEHOLDER_FOR_ABSPOS) { 155 return FrameChildListID::Absolute; 156 } 157 MOZ_DIAGNOSTIC_CRASH("unknown list"); 158 return FrameChildListID::Float; 159 } 160 161 void nsPlaceholderFrame::Destroy(DestroyContext& aContext) { 162 if (nsIFrame* oof = mOutOfFlowFrame) { 163 mOutOfFlowFrame = nullptr; 164 oof->RemoveProperty(nsIFrame::PlaceholderFrameProperty()); 165 166 // Destroy the out of flow now. 167 ChildListID listId = ChildListIDForOutOfFlow(GetStateBits(), oof); 168 nsFrameManager* fm = PresContext()->FrameConstructor(); 169 fm->RemoveFrame(aContext, listId, oof); 170 } 171 172 nsIFrame::Destroy(aContext); 173 } 174 175 /* virtual */ 176 bool nsPlaceholderFrame::CanContinueTextRun() const { 177 if (!mOutOfFlowFrame) { 178 return false; 179 } 180 // first-letter frames can continue text runs, and placeholders for floated 181 // first-letter frames can too 182 return mOutOfFlowFrame->CanContinueTextRun(); 183 } 184 185 ComputedStyle* nsPlaceholderFrame::GetParentComputedStyleForOutOfFlow( 186 nsIFrame** aProviderFrame) const { 187 MOZ_ASSERT(GetParent(), "How can we not have a parent here?"); 188 189 Element* parentElement = 190 mContent ? mContent->GetFlattenedTreeParentElement() : nullptr; 191 // See the similar code in nsIFrame::DoGetParentComputedStyle. 192 if (parentElement && MOZ_LIKELY(parentElement->HasServoData()) && 193 Servo_Element_IsDisplayContents(parentElement)) { 194 RefPtr<ComputedStyle> style = 195 ServoStyleSet::ResolveServoStyle(*parentElement); 196 *aProviderFrame = nullptr; 197 // See the comment in GetParentComputedStyle to see why returning this as a 198 // weak ref is fine. 199 return style; 200 } 201 202 return GetLayoutParentStyleForOutOfFlow(aProviderFrame); 203 } 204 205 ComputedStyle* nsPlaceholderFrame::GetLayoutParentStyleForOutOfFlow( 206 nsIFrame** aProviderFrame) const { 207 // Lie about our pseudo so we can step out of all anon boxes and 208 // pseudo-elements. The other option would be to reimplement the 209 // {ib} split gunk here. 210 // 211 // See the hack in CorrectStyleParentFrame for why we pass `MAX`. 212 *aProviderFrame = CorrectStyleParentFrame(GetParent(), PseudoStyleType::MAX); 213 return *aProviderFrame ? (*aProviderFrame)->Style() : nullptr; 214 } 215 216 #if defined(DEBUG) || (defined(MOZ_REFLOW_PERF_DSP) && defined(MOZ_REFLOW_PERF)) 217 218 void nsPlaceholderFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder, 219 const nsDisplayListSet& aLists) { 220 DO_GLOBAL_REFLOW_COUNT_DSP("nsPlaceholderFrame"); 221 } 222 #endif // DEBUG || (MOZ_REFLOW_PERF_DSP && MOZ_REFLOW_PERF) 223 224 #ifdef DEBUG_FRAME_DUMP 225 nsresult nsPlaceholderFrame::GetFrameName(nsAString& aResult) const { 226 return MakeFrameName(u"Placeholder"_ns, aResult); 227 } 228 229 void nsPlaceholderFrame::List(FILE* out, const char* aPrefix, 230 ListFlags aFlags) const { 231 nsCString str; 232 ListGeneric(str, aPrefix, aFlags); 233 234 if (mOutOfFlowFrame) { 235 str += " outOfFlowFrame="; 236 str += mOutOfFlowFrame->ListTag( 237 aFlags.contains(ListFlag::OnlyListDeterministicInfo)); 238 } 239 fprintf_stderr(out, "%s\n", str.get()); 240 } 241 #endif