1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * rendering object for the point that anchors out-of-flow rendering
8 * objects such as floats and absolutely positioned elements
11 #include "nsPlaceholderFrame.h"
13 #include "nsDisplayList.h"
14 #include "nsFrameManager.h"
15 #include "nsLayoutUtils.h"
16 #include "nsPresContext.h"
17 #include "nsRenderingContext.h"
18 #include "nsIFrameInlines.h"
21 NS_NewPlaceholderFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
,
22 nsFrameState aTypeBit
)
24 return new (aPresShell
) nsPlaceholderFrame(aContext
, aTypeBit
);
27 NS_IMPL_FRAMEARENA_HELPERS(nsPlaceholderFrame
)
30 NS_QUERYFRAME_HEAD(nsPlaceholderFrame
)
31 NS_QUERYFRAME_ENTRY(nsPlaceholderFrame
)
32 NS_QUERYFRAME_TAIL_INHERITING(nsFrame
)
36 nsPlaceholderFrame::GetMinSize(nsBoxLayoutState
& aBoxLayoutState
)
39 DISPLAY_MIN_SIZE(this, size
);
44 nsPlaceholderFrame::GetPrefSize(nsBoxLayoutState
& aBoxLayoutState
)
47 DISPLAY_PREF_SIZE(this, size
);
52 nsPlaceholderFrame::GetMaxSize(nsBoxLayoutState
& aBoxLayoutState
)
54 nsSize
size(NS_INTRINSICSIZE
, NS_INTRINSICSIZE
);
55 DISPLAY_MAX_SIZE(this, size
);
60 nsPlaceholderFrame::AddInlineMinISize(nsRenderingContext
* aRenderingContext
,
61 nsIFrame::InlineMinISizeData
* aData
)
63 // Override AddInlineMinWith so that *nothing* happens. In
64 // particular, we don't want to zero out |aData->trailingWhitespace|,
65 // since nsLineLayout skips placeholders when trimming trailing
66 // whitespace, and we don't want to set aData->skipWhitespace to
69 // ...but push floats onto the list
70 if (mOutOfFlowFrame
->IsFloating()) {
72 nsLayoutUtils::IntrinsicForContainer(aRenderingContext
,
74 nsLayoutUtils::MIN_ISIZE
);
75 aData
->floats
.AppendElement(
76 InlineIntrinsicISizeData::FloatInfo(mOutOfFlowFrame
, floatWidth
));
81 nsPlaceholderFrame::AddInlinePrefISize(nsRenderingContext
* aRenderingContext
,
82 nsIFrame::InlinePrefISizeData
* aData
)
84 // Override AddInlinePrefWith so that *nothing* happens. In
85 // particular, we don't want to zero out |aData->trailingWhitespace|,
86 // since nsLineLayout skips placeholders when trimming trailing
87 // whitespace, and we don't want to set aData->skipWhitespace to
90 // ...but push floats onto the list
91 if (mOutOfFlowFrame
->IsFloating()) {
93 nsLayoutUtils::IntrinsicForContainer(aRenderingContext
,
95 nsLayoutUtils::PREF_ISIZE
);
96 aData
->floats
.AppendElement(
97 InlineIntrinsicISizeData::FloatInfo(mOutOfFlowFrame
, floatWidth
));
102 nsPlaceholderFrame::Reflow(nsPresContext
* aPresContext
,
103 nsHTMLReflowMetrics
& aDesiredSize
,
104 const nsHTMLReflowState
& aReflowState
,
105 nsReflowStatus
& aStatus
)
108 // We should be getting reflowed before our out-of-flow.
109 // If this is our first reflow, and our out-of-flow has already received its
110 // first reflow (before us), complain.
111 // XXXdholbert This "look for a previous continuation or IB-split sibling"
112 // code could use nsLayoutUtils::GetPrevContinuationOrIBSplitSibling(), if
113 // we ever add a function like that. (We currently have a "Next" version.)
114 if ((GetStateBits() & NS_FRAME_FIRST_REFLOW
) &&
115 !(mOutOfFlowFrame
->GetStateBits() & NS_FRAME_FIRST_REFLOW
)) {
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 (ancestor
->GetPrevContinuation() ||
125 ancestor
->Properties().Get(IBSplitPrevSibling())) {
126 isInContinuationOrIBSplit
= true;
131 if (isInContinuationOrIBSplit
) {
132 NS_WARNING("Out-of-flow frame got reflowed before its placeholder");
134 NS_ERROR("Out-of-flow frame got reflowed before its placeholder");
139 DO_GLOBAL_REFLOW_COUNT("nsPlaceholderFrame");
140 DISPLAY_REFLOW(aPresContext
, this, aReflowState
, aDesiredSize
, aStatus
);
141 aDesiredSize
.ClearSize();
143 aStatus
= NS_FRAME_COMPLETE
;
144 NS_FRAME_SET_TRUNCATION(aStatus
, aReflowState
, aDesiredSize
);
148 nsPlaceholderFrame::DestroyFrom(nsIFrame
* aDestructRoot
)
150 nsIFrame
* oof
= mOutOfFlowFrame
;
152 // Unregister out-of-flow frame
153 nsFrameManager
* fm
= PresContext()->GetPresShell()->FrameManager();
154 fm
->UnregisterPlaceholderFrame(this);
155 mOutOfFlowFrame
= nullptr;
156 // If aDestructRoot is not an ancestor of the out-of-flow frame,
157 // then call RemoveFrame on it here.
158 // Also destroy it here if it's a popup frame. (Bug 96291)
159 if ((GetStateBits() & PLACEHOLDER_FOR_POPUP
) ||
160 !nsLayoutUtils::IsProperAncestorFrame(aDestructRoot
, oof
)) {
161 ChildListID listId
= nsLayoutUtils::GetChildListNameFor(oof
);
162 fm
->RemoveFrame(listId
, oof
);
164 // else oof will be destroyed by its parent
167 nsFrame::DestroyFrom(aDestructRoot
);
171 nsPlaceholderFrame::GetType() const
173 return nsGkAtoms::placeholderFrame
;
177 nsPlaceholderFrame::CanContinueTextRun() const
179 if (!mOutOfFlowFrame
) {
182 // first-letter frames can continue text runs, and placeholders for floated
183 // first-letter frames can too
184 return mOutOfFlowFrame
->CanContinueTextRun();
188 nsPlaceholderFrame::GetParentStyleContextFrame() const
190 NS_PRECONDITION(GetParent(), "How can we not have a parent here?");
192 // Lie about our pseudo so we can step out of all anon boxes and
193 // pseudo-elements. The other option would be to reimplement the
194 // {ib} split gunk here.
195 return CorrectStyleParentFrame(GetParent(), nsGkAtoms::placeholderFrame
);
201 PaintDebugPlaceholder(nsIFrame
* aFrame
, nsRenderingContext
* aCtx
,
202 const nsRect
& aDirtyRect
, nsPoint aPt
)
204 aCtx
->SetColor(NS_RGB(0, 255, 255));
205 nscoord x
= nsPresContext::CSSPixelsToAppUnits(-5);
206 aCtx
->FillRect(aPt
.x
+ x
, aPt
.y
,
207 nsPresContext::CSSPixelsToAppUnits(13),
208 nsPresContext::CSSPixelsToAppUnits(3));
209 nscoord y
= nsPresContext::CSSPixelsToAppUnits(-10);
210 aCtx
->FillRect(aPt
.x
, aPt
.y
+ y
,
211 nsPresContext::CSSPixelsToAppUnits(3),
212 nsPresContext::CSSPixelsToAppUnits(10));
216 #if defined(DEBUG) || (defined(MOZ_REFLOW_PERF_DSP) && defined(MOZ_REFLOW_PERF))
219 nsPlaceholderFrame::BuildDisplayList(nsDisplayListBuilder
* aBuilder
,
220 const nsRect
& aDirtyRect
,
221 const nsDisplayListSet
& aLists
)
223 DO_GLOBAL_REFLOW_COUNT_DSP("nsPlaceholderFrame");
226 if (GetShowFrameBorders()) {
227 aLists
.Outlines()->AppendNewToTop(
228 new (aBuilder
) nsDisplayGeneric(aBuilder
, this, PaintDebugPlaceholder
,
230 nsDisplayItem::TYPE_DEBUG_PLACEHOLDER
));
234 #endif // DEBUG || (MOZ_REFLOW_PERF_DSP && MOZ_REFLOW_PERF)
236 #ifdef DEBUG_FRAME_DUMP
238 nsPlaceholderFrame::GetFrameName(nsAString
& aResult
) const
240 return MakeFrameName(NS_LITERAL_STRING("Placeholder"), aResult
);
244 nsPlaceholderFrame::List(FILE* out
, const char* aPrefix
, uint32_t aFlags
) const
247 ListGeneric(str
, aPrefix
, aFlags
);
249 if (mOutOfFlowFrame
) {
250 str
+= " outOfFlowFrame=";
251 nsFrame::ListTag(str
, mOutOfFlowFrame
);
253 fprintf_stderr(out
, "%s\n", str
.get());