Bumping manifests a=b2g-bump
[gecko.git] / layout / generic / nsPlaceholderFrame.cpp
blob6188dd9ec0ddc08f60e5aac144ba3b5459425efd
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/. */
6 /*
7 * rendering object for the point that anchors out-of-flow rendering
8 * objects such as floats and absolutely positioned elements
9 */
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"
20 nsIFrame*
21 NS_NewPlaceholderFrame(nsIPresShell* aPresShell, nsStyleContext* aContext,
22 nsFrameState aTypeBit)
24 return new (aPresShell) nsPlaceholderFrame(aContext, aTypeBit);
27 NS_IMPL_FRAMEARENA_HELPERS(nsPlaceholderFrame)
29 #ifdef DEBUG
30 NS_QUERYFRAME_HEAD(nsPlaceholderFrame)
31 NS_QUERYFRAME_ENTRY(nsPlaceholderFrame)
32 NS_QUERYFRAME_TAIL_INHERITING(nsFrame)
33 #endif
35 /* virtual */ nsSize
36 nsPlaceholderFrame::GetMinSize(nsBoxLayoutState& aBoxLayoutState)
38 nsSize size(0, 0);
39 DISPLAY_MIN_SIZE(this, size);
40 return size;
43 /* virtual */ nsSize
44 nsPlaceholderFrame::GetPrefSize(nsBoxLayoutState& aBoxLayoutState)
46 nsSize size(0, 0);
47 DISPLAY_PREF_SIZE(this, size);
48 return size;
51 /* virtual */ nsSize
52 nsPlaceholderFrame::GetMaxSize(nsBoxLayoutState& aBoxLayoutState)
54 nsSize size(NS_INTRINSICSIZE, NS_INTRINSICSIZE);
55 DISPLAY_MAX_SIZE(this, size);
56 return size;
59 /* virtual */ void
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
67 // false.
69 // ...but push floats onto the list
70 if (mOutOfFlowFrame->IsFloating()) {
71 nscoord floatWidth =
72 nsLayoutUtils::IntrinsicForContainer(aRenderingContext,
73 mOutOfFlowFrame,
74 nsLayoutUtils::MIN_ISIZE);
75 aData->floats.AppendElement(
76 InlineIntrinsicISizeData::FloatInfo(mOutOfFlowFrame, floatWidth));
80 /* virtual */ void
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
88 // false.
90 // ...but push floats onto the list
91 if (mOutOfFlowFrame->IsFloating()) {
92 nscoord floatWidth =
93 nsLayoutUtils::IntrinsicForContainer(aRenderingContext,
94 mOutOfFlowFrame,
95 nsLayoutUtils::PREF_ISIZE);
96 aData->floats.AppendElement(
97 InlineIntrinsicISizeData::FloatInfo(mOutOfFlowFrame, floatWidth));
101 void
102 nsPlaceholderFrame::Reflow(nsPresContext* aPresContext,
103 nsHTMLReflowMetrics& aDesiredSize,
104 const nsHTMLReflowState& aReflowState,
105 nsReflowStatus& aStatus)
107 #ifdef DEBUG
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;
127 break;
131 if (isInContinuationOrIBSplit) {
132 NS_WARNING("Out-of-flow frame got reflowed before its placeholder");
133 } else {
134 NS_ERROR("Out-of-flow frame got reflowed before its placeholder");
137 #endif
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);
147 void
148 nsPlaceholderFrame::DestroyFrom(nsIFrame* aDestructRoot)
150 nsIFrame* oof = mOutOfFlowFrame;
151 if (oof) {
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);
170 nsIAtom*
171 nsPlaceholderFrame::GetType() const
173 return nsGkAtoms::placeholderFrame;
176 /* virtual */ bool
177 nsPlaceholderFrame::CanContinueTextRun() const
179 if (!mOutOfFlowFrame) {
180 return false;
182 // first-letter frames can continue text runs, and placeholders for floated
183 // first-letter frames can too
184 return mOutOfFlowFrame->CanContinueTextRun();
187 nsIFrame*
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);
199 #ifdef DEBUG
200 static void
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));
214 #endif // DEBUG
216 #if defined(DEBUG) || (defined(MOZ_REFLOW_PERF_DSP) && defined(MOZ_REFLOW_PERF))
218 void
219 nsPlaceholderFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
220 const nsRect& aDirtyRect,
221 const nsDisplayListSet& aLists)
223 DO_GLOBAL_REFLOW_COUNT_DSP("nsPlaceholderFrame");
225 #ifdef DEBUG
226 if (GetShowFrameBorders()) {
227 aLists.Outlines()->AppendNewToTop(
228 new (aBuilder) nsDisplayGeneric(aBuilder, this, PaintDebugPlaceholder,
229 "DebugPlaceholder",
230 nsDisplayItem::TYPE_DEBUG_PLACEHOLDER));
232 #endif
234 #endif // DEBUG || (MOZ_REFLOW_PERF_DSP && MOZ_REFLOW_PERF)
236 #ifdef DEBUG_FRAME_DUMP
237 nsresult
238 nsPlaceholderFrame::GetFrameName(nsAString& aResult) const
240 return MakeFrameName(NS_LITERAL_STRING("Placeholder"), aResult);
243 void
244 nsPlaceholderFrame::List(FILE* out, const char* aPrefix, uint32_t aFlags) const
246 nsCString str;
247 ListGeneric(str, aPrefix, aFlags);
249 if (mOutOfFlowFrame) {
250 str += " outOfFlowFrame=";
251 nsFrame::ListTag(str, mOutOfFlowFrame);
253 fprintf_stderr(out, "%s\n", str.get());
255 #endif