backing out bug 347743 due to major crasher in 386332
[mozilla-central.git] / layout / generic / nsInlineFrame.cpp
blobba04b288fff2c0c0fa3e2822b7fa68242557c4d4
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* rendering object for CSS display:inline objects */
40 #include "nsCOMPtr.h"
41 #include "nsInlineFrame.h"
42 #include "nsBlockFrame.h"
43 #include "nsGkAtoms.h"
44 #include "nsHTMLParts.h"
45 #include "nsStyleContext.h"
46 #include "nsIPresShell.h"
47 #include "nsPresContext.h"
48 #include "nsIRenderingContext.h"
49 #include "nsIFontMetrics.h"
50 #include "nsAbsoluteContainingBlock.h"
51 #include "nsCSSAnonBoxes.h"
52 #include "nsAutoPtr.h"
53 #include "nsFrameManager.h"
54 #ifdef ACCESSIBILITY
55 #include "nsIServiceManager.h"
56 #include "nsIAccessibilityService.h"
57 #endif
58 #include "nsDisplayList.h"
60 #ifdef DEBUG
61 #undef NOISY_PUSHING
62 #endif
65 NS_DEFINE_IID(kInlineFrameCID, NS_INLINE_FRAME_CID);
68 //////////////////////////////////////////////////////////////////////
70 // Basic nsInlineFrame methods
72 nsIFrame*
73 NS_NewInlineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
75 return new (aPresShell) nsInlineFrame(aContext);
78 NS_IMETHODIMP
79 nsInlineFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr)
81 if (nsnull == aInstancePtr) {
82 return NS_ERROR_NULL_POINTER;
84 if (aIID.Equals(kInlineFrameCID)) {
85 nsInlineFrame* tmp = this;
86 *aInstancePtr = (void*) tmp;
87 return NS_OK;
89 return nsInlineFrameSuper::QueryInterface(aIID, aInstancePtr);
92 void
93 nsInlineFrame::Destroy()
95 if (mState & NS_FRAME_GENERATED_CONTENT) {
96 // Make sure all the content nodes for the generated content inside
97 // this frame know it's going away.
98 // This is duplicated in nsBlockFrame::Destroy.
99 // See also nsCSSFrameConstructor::CreateGeneratedContentFrame which
100 // created this frame.
102 // XXXbz would this be better done via a global structure in
103 // nsCSSFrameConstructor that could key off of
104 // GeneratedContentFrameRemoved or something? The problem is that
105 // our kids are gone by the time that's called.
106 nsContainerFrame::CleanupGeneratedContentIn(mContent, this);
108 nsInlineFrameSuper::Destroy();
111 #ifdef DEBUG
112 NS_IMETHODIMP
113 nsInlineFrame::GetFrameName(nsAString& aResult) const
115 return MakeFrameName(NS_LITERAL_STRING("Inline"), aResult);
117 #endif
119 nsIAtom*
120 nsInlineFrame::GetType() const
122 return nsGkAtoms::inlineFrame;
125 inline PRBool
126 IsPaddingZero(nsStyleUnit aUnit, nsStyleCoord &aCoord)
128 return ((aUnit == eStyleUnit_Coord && aCoord.GetCoordValue() == 0) ||
129 (aUnit == eStyleUnit_Percent && aCoord.GetPercentValue() == 0.0));
132 inline PRBool
133 IsMarginZero(nsStyleUnit aUnit, nsStyleCoord &aCoord)
135 return (aUnit == eStyleUnit_Auto ||
136 (aUnit == eStyleUnit_Coord && aCoord.GetCoordValue() == 0) ||
137 (aUnit == eStyleUnit_Percent && aCoord.GetPercentValue() == 0.0));
140 /* virtual */ PRBool
141 nsInlineFrame::IsSelfEmpty()
143 #if 0
144 // I used to think inline frames worked this way, but it seems they
145 // don't. At least not in our codebase.
146 if (GetPresContext()->CompatibilityMode() == eCompatibility_FullStandards) {
147 return PR_FALSE;
149 #endif
150 const nsStyleMargin* margin = GetStyleMargin();
151 const nsStyleBorder* border = GetStyleBorder();
152 const nsStylePadding* padding = GetStylePadding();
153 nsStyleCoord coord;
154 // XXX Top and bottom removed, since they shouldn't affect things, but this
155 // doesn't really match with nsLineLayout.cpp's setting of
156 // ZeroEffectiveSpanBox, anymore, so what should this really be?
157 if (border->GetBorderWidth(NS_SIDE_RIGHT) != 0 ||
158 border->GetBorderWidth(NS_SIDE_LEFT) != 0 ||
159 !IsPaddingZero(padding->mPadding.GetRightUnit(),
160 padding->mPadding.GetRight(coord)) ||
161 !IsPaddingZero(padding->mPadding.GetLeftUnit(),
162 padding->mPadding.GetLeft(coord)) ||
163 !IsMarginZero(margin->mMargin.GetRightUnit(),
164 margin->mMargin.GetRight(coord)) ||
165 !IsMarginZero(margin->mMargin.GetLeftUnit(),
166 margin->mMargin.GetLeft(coord))) {
167 return PR_FALSE;
169 return PR_TRUE;
172 PRBool
173 nsInlineFrame::IsEmpty()
175 if (!IsSelfEmpty()) {
176 return PR_FALSE;
179 for (nsIFrame *kid = mFrames.FirstChild(); kid; kid = kid->GetNextSibling()) {
180 if (!kid->IsEmpty())
181 return PR_FALSE;
184 return PR_TRUE;
187 PRBool
188 nsInlineFrame::PeekOffsetCharacter(PRBool aForward, PRInt32* aOffset)
190 // Override the implementation in nsFrame, to skip empty inline frames
191 NS_ASSERTION (aOffset && *aOffset <= 1, "aOffset out of range");
192 PRInt32 startOffset = *aOffset;
193 if (startOffset < 0)
194 startOffset = 1;
195 if (aForward == (startOffset == 0)) {
196 // We're before the frame and moving forward, or after it and moving backwards:
197 // skip to the other side, but keep going.
198 *aOffset = 1 - startOffset;
200 return PR_FALSE;
203 NS_IMETHODIMP
204 nsInlineFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
205 const nsRect& aDirtyRect,
206 const nsDisplayListSet& aLists)
208 nsresult rv = nsHTMLContainerFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
209 NS_ENSURE_SUCCESS(rv, rv);
211 // The sole purpose of this is to trigger display of the selection
212 // window for Named Anchors, which don't have any children and
213 // normally don't have any size, but in Editor we use CSS to display
214 // an image to represent this "hidden" element.
215 if (!mFrames.FirstChild()) {
216 rv = DisplaySelectionOverlay(aBuilder, aLists);
218 return rv;
221 //////////////////////////////////////////////////////////////////////
222 // Reflow methods
224 /* virtual */ void
225 nsInlineFrame::AddInlineMinWidth(nsIRenderingContext *aRenderingContext,
226 nsIFrame::InlineMinWidthData *aData)
228 DoInlineIntrinsicWidth(aRenderingContext, aData, nsLayoutUtils::MIN_WIDTH);
231 /* virtual */ void
232 nsInlineFrame::AddInlinePrefWidth(nsIRenderingContext *aRenderingContext,
233 nsIFrame::InlinePrefWidthData *aData)
235 DoInlineIntrinsicWidth(aRenderingContext, aData, nsLayoutUtils::PREF_WIDTH);
238 /* virtual */ nsSize
239 nsInlineFrame::ComputeSize(nsIRenderingContext *aRenderingContext,
240 nsSize aCBSize, nscoord aAvailableWidth,
241 nsSize aMargin, nsSize aBorder, nsSize aPadding,
242 PRBool aShrinkWrap)
244 // Inlines and text don't compute size before reflow.
245 return nsSize(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
248 void
249 nsInlineFrame::ReparentFloatsForInlineChild(nsIFrame* aOurLineContainer,
250 nsIFrame* aFrame,
251 PRBool aReparentSiblings)
253 NS_ASSERTION(aOurLineContainer->GetNextContinuation() ||
254 aOurLineContainer->GetPrevContinuation(),
255 "Don't call this when we have no continuation, it's a waste");
257 nsIFrame* ancestor = aFrame;
258 nsIFrame* ancestorBlockChild;
259 do {
260 ancestorBlockChild = ancestor;
261 ancestor = ancestor->GetParent();
262 if (!ancestor)
263 return;
264 } while (!ancestor->IsFloatContainingBlock());
266 if (ancestor == aOurLineContainer)
267 return;
269 nsBlockFrame* ourBlock;
270 nsresult rv = aOurLineContainer->QueryInterface(kBlockFrameCID, (void**)&ourBlock);
271 NS_ASSERTION(NS_SUCCEEDED(rv), "Not a block, but broke vertically?");
272 nsBlockFrame* frameBlock;
273 rv = ancestor->QueryInterface(kBlockFrameCID, (void**)&frameBlock);
274 NS_ASSERTION(NS_SUCCEEDED(rv), "ancestor not a block");
276 nsFrameList blockChildren(ancestor->GetFirstChild(nsnull));
277 PRBool isOverflow = !blockChildren.ContainsFrame(ancestorBlockChild);
279 while (PR_TRUE) {
280 ourBlock->ReparentFloats(aFrame, frameBlock, isOverflow, PR_FALSE);
282 if (!aReparentSiblings)
283 return;
284 nsIFrame* next = aFrame->GetNextSibling();
285 if (!next)
286 return;
287 if (next->GetParent() == aFrame->GetParent()) {
288 aFrame = next;
289 continue;
291 // This is paranoid and will hardly ever get hit ... but we can't actually
292 // trust that the frames in the sibling chain all have the same parent,
293 // because lazy reparenting may be going on. If we find a different
294 // parent we need to redo our analysis.
295 ReparentFloatsForInlineChild(aOurLineContainer, next, aReparentSiblings);
296 return;
300 NS_IMETHODIMP
301 nsInlineFrame::Reflow(nsPresContext* aPresContext,
302 nsHTMLReflowMetrics& aMetrics,
303 const nsHTMLReflowState& aReflowState,
304 nsReflowStatus& aStatus)
306 DO_GLOBAL_REFLOW_COUNT("nsInlineFrame");
307 DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus);
308 if (nsnull == aReflowState.mLineLayout) {
309 return NS_ERROR_INVALID_ARG;
312 PRBool lazilySetParentPointer = PR_FALSE;
314 nsIFrame* lineContainer = aReflowState.mLineLayout->GetLineContainerFrame();
316 // Check for an overflow list with our prev-in-flow
317 nsInlineFrame* prevInFlow = (nsInlineFrame*)GetPrevInFlow();
318 if (nsnull != prevInFlow) {
319 nsIFrame* prevOverflowFrames = prevInFlow->GetOverflowFrames(aPresContext, PR_TRUE);
321 if (prevOverflowFrames) {
322 // When pushing and pulling frames we need to check for whether any
323 // views need to be reparented.
324 nsHTMLContainerFrame::ReparentFrameViewList(aPresContext, prevOverflowFrames,
325 prevInFlow, this);
327 if (GetStateBits() & NS_FRAME_FIRST_REFLOW) {
328 // If it's the initial reflow, then our child list must be empty, so
329 // just set the child list rather than calling InsertFrame(). This avoids
330 // having to get the last child frame in the list.
331 // Note that we don't set the parent pointer for the new frames. Instead wait
332 // to do this until we actually reflow the frame. If the overflow list contains
333 // thousands of frames this is a big performance issue (see bug #5588)
334 NS_ASSERTION(mFrames.IsEmpty(), "child list is not empty for initial reflow");
335 mFrames.SetFrames(prevOverflowFrames);
336 lazilySetParentPointer = PR_TRUE;
337 } else {
338 // Assign all floats to our block if necessary
339 if (lineContainer && lineContainer->GetPrevContinuation()) {
340 ReparentFloatsForInlineChild(lineContainer, prevOverflowFrames, PR_TRUE);
342 // Insert the new frames at the beginning of the child list
343 // and set their parent pointer
344 mFrames.InsertFrames(this, nsnull, prevOverflowFrames);
349 // It's also possible that we have an overflow list for ourselves
350 #ifdef DEBUG
351 if (GetStateBits() & NS_FRAME_FIRST_REFLOW) {
352 // If it's our initial reflow, then we should not have an overflow list.
353 // However, add an assertion in case we get reflowed more than once with
354 // the initial reflow reason
355 nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_FALSE);
356 NS_ASSERTION(!overflowFrames, "overflow list is not empty for initial reflow");
358 #endif
359 if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
360 nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_TRUE);
361 if (overflowFrames) {
362 NS_ASSERTION(mFrames.NotEmpty(), "overflow list w/o frames");
364 // Because we lazily set the parent pointer of child frames we get from
365 // our prev-in-flow's overflow list, it's possible that we have not set
366 // the parent pointer for these frames.
367 mFrames.AppendFrames(this, overflowFrames);
371 if (IsFrameTreeTooDeep(aReflowState, aMetrics)) {
372 #ifdef DEBUG_kipp
374 extern char* nsPresShell_ReflowStackPointerTop;
375 char marker;
376 char* newsp = (char*) &marker;
377 printf("XXX: frame tree is too deep; approx stack size = %d\n",
378 nsPresShell_ReflowStackPointerTop - newsp);
380 #endif
381 aStatus = NS_FRAME_COMPLETE;
382 return NS_OK;
385 // Set our own reflow state (additional state above and beyond
386 // aReflowState)
387 InlineReflowState irs;
388 irs.mPrevFrame = nsnull;
389 irs.mLineContainer = lineContainer;
390 irs.mNextInFlow = (nsInlineFrame*) GetNextInFlow();
391 irs.mSetParentPointer = lazilySetParentPointer;
393 nsresult rv;
394 if (mFrames.IsEmpty()) {
395 // Try to pull over one frame before starting so that we know
396 // whether we have an anonymous block or not.
397 PRBool complete;
398 (void) PullOneFrame(aPresContext, irs, &complete);
401 rv = ReflowFrames(aPresContext, aReflowState, irs, aMetrics, aStatus);
403 // Note: the line layout code will properly compute our
404 // NS_FRAME_OUTSIDE_CHILDREN state for us.
406 NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics);
407 return rv;
410 /* virtual */ PRBool
411 nsInlineFrame::CanContinueTextRun() const
413 // We can continue a text run through an inline frame
414 return PR_TRUE;
417 nsresult
418 nsInlineFrame::ReflowFrames(nsPresContext* aPresContext,
419 const nsHTMLReflowState& aReflowState,
420 InlineReflowState& irs,
421 nsHTMLReflowMetrics& aMetrics,
422 nsReflowStatus& aStatus)
424 nsresult rv = NS_OK;
425 aStatus = NS_FRAME_COMPLETE;
427 nsLineLayout* lineLayout = aReflowState.mLineLayout;
428 PRBool ltr = (NS_STYLE_DIRECTION_LTR == aReflowState.mStyleVisibility->mDirection);
429 nscoord leftEdge = 0;
430 if (nsnull == GetPrevContinuation()) {
431 leftEdge = ltr ? aReflowState.mComputedBorderPadding.left
432 : aReflowState.mComputedBorderPadding.right;
434 nscoord availableWidth = aReflowState.availableWidth;
435 if (NS_UNCONSTRAINEDSIZE != availableWidth) {
436 // Subtract off left and right border+padding from availableWidth
437 availableWidth -= leftEdge;
438 availableWidth -= ltr ? aReflowState.mComputedBorderPadding.right
439 : aReflowState.mComputedBorderPadding.left;
440 availableWidth = PR_MAX(0, availableWidth);
442 lineLayout->BeginSpan(this, &aReflowState, leftEdge, leftEdge + availableWidth);
444 // First reflow our current children
445 nsIFrame* frame = mFrames.FirstChild();
446 PRBool done = PR_FALSE;
447 while (nsnull != frame) {
448 PRBool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
450 // Check if we should lazily set the child frame's parent pointer
451 if (irs.mSetParentPointer) {
452 PRBool havePrevBlock =
453 irs.mLineContainer && irs.mLineContainer->GetPrevContinuation();
454 // If our block is the first in flow, then any floats under the pulled
455 // frame must already belong to our block.
456 if (havePrevBlock) {
457 // This has to happen before we update frame's parent; we need to
458 // know frame's ancestry under its old block.
459 // The blockChildren.ContainsFrame check performed by
460 // ReparentFloatsForInlineChild here may be slow, but we can't
461 // easily avoid it because we don't know where 'frame' originally
462 // came from. If we really really have to optimize this we could
463 // cache whether frame->GetParent() is under its containing blocks
464 // overflowList or not.
465 ReparentFloatsForInlineChild(irs.mLineContainer, frame, PR_FALSE);
467 frame->SetParent(this);
468 // We also need to check if frame has a next-in-flow. If it does, then set
469 // its parent frame pointer, too. Otherwise, if we reflow frame and it's
470 // complete we'll fail when deleting its next-in-flow which is no longer
471 // needed. This scenario doesn't happen often, but it can happen
472 nsIFrame* nextInFlow = frame->GetNextInFlow();
473 while (nextInFlow) {
474 // Since we only do lazy setting of parent pointers for the frame's
475 // initial reflow, this frame can't have a next-in-flow. That means
476 // the continuing child frame must be in our child list as well. If
477 // not, then something is wrong
478 NS_ASSERTION(mFrames.ContainsFrame(nextInFlow), "unexpected flow");
479 if (havePrevBlock) {
480 ReparentFloatsForInlineChild(irs.mLineContainer, nextInFlow, PR_FALSE);
482 nextInFlow->SetParent(this);
483 nextInFlow = nextInFlow->GetNextInFlow();
486 rv = ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
487 if (NS_FAILED(rv)) {
488 done = PR_TRUE;
489 break;
491 if (NS_INLINE_IS_BREAK(aStatus) ||
492 (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus))) {
493 done = PR_TRUE;
494 break;
496 irs.mPrevFrame = frame;
497 frame = frame->GetNextSibling();
500 // Attempt to pull frames from our next-in-flow until we can't
501 if (!done && (nsnull != GetNextInFlow())) {
502 while (!done) {
503 PRBool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
504 PRBool isComplete;
505 if (!frame) { // Could be non-null if we pulled a first-letter frame and
506 // it created a continuation, since we don't push those.
507 frame = PullOneFrame(aPresContext, irs, &isComplete);
509 #ifdef NOISY_PUSHING
510 printf("%p pulled up %p\n", this, frame);
511 #endif
512 if (nsnull == frame) {
513 if (!isComplete) {
514 aStatus = NS_FRAME_NOT_COMPLETE;
516 break;
518 rv = ReflowInlineFrame(aPresContext, aReflowState, irs, frame, aStatus);
519 if (NS_FAILED(rv)) {
520 done = PR_TRUE;
521 break;
523 if (NS_INLINE_IS_BREAK(aStatus) ||
524 (!reflowingFirstLetter && NS_FRAME_IS_NOT_COMPLETE(aStatus))) {
525 done = PR_TRUE;
526 break;
528 irs.mPrevFrame = frame;
529 frame = frame->GetNextSibling();
532 #ifdef DEBUG
533 if (NS_FRAME_IS_COMPLETE(aStatus)) {
534 // We can't be complete AND have overflow frames!
535 nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_FALSE);
536 NS_ASSERTION(!overflowFrames, "whoops");
538 #endif
540 // If after reflowing our children they take up no area then make
541 // sure that we don't either.
543 // Note: CSS demands that empty inline elements still affect the
544 // line-height calculations. However, continuations of an inline
545 // that are empty we force to empty so that things like collapsed
546 // whitespace in an inline element don't affect the line-height.
547 nsSize size;
548 lineLayout->EndSpan(this, size);
550 // Compute final width
551 aMetrics.width = size.width;
552 if (nsnull == GetPrevContinuation()) {
553 aMetrics.width += ltr ? aReflowState.mComputedBorderPadding.left
554 : aReflowState.mComputedBorderPadding.right;
556 if (NS_FRAME_IS_COMPLETE(aStatus) && (!GetNextContinuation() || GetNextInFlow())) {
557 aMetrics.width += ltr ? aReflowState.mComputedBorderPadding.right
558 : aReflowState.mComputedBorderPadding.left;
561 nsLayoutUtils::SetFontFromStyle(aReflowState.rendContext, mStyleContext);
562 nsCOMPtr<nsIFontMetrics> fm;
563 aReflowState.rendContext->GetFontMetrics(*getter_AddRefs(fm));
565 if (fm) {
566 // Compute final height of the frame.
568 // Do things the standard css2 way -- though it's hard to find it
569 // in the css2 spec! It's actually found in the css1 spec section
570 // 4.4 (you will have to read between the lines to really see
571 // it).
573 // The height of our box is the sum of our font size plus the top
574 // and bottom border and padding. The height of children do not
575 // affect our height.
576 fm->GetMaxAscent(aMetrics.ascent);
577 fm->GetHeight(aMetrics.height);
578 } else {
579 NS_WARNING("Cannot get font metrics - defaulting sizes to 0");
580 aMetrics.ascent = aMetrics.height = 0;
582 aMetrics.ascent += aReflowState.mComputedBorderPadding.top;
583 aMetrics.height += aReflowState.mComputedBorderPadding.top +
584 aReflowState.mComputedBorderPadding.bottom;
586 // For now our overflow area is zero. The real value will be
587 // computed during vertical alignment of the line we are on.
588 aMetrics.mOverflowArea.SetRect(0, 0, 0, 0);
590 #ifdef NOISY_FINAL_SIZE
591 ListTag(stdout);
592 printf(": metrics=%d,%d ascent=%d\n",
593 aMetrics.width, aMetrics.height, aMetrics.ascent);
594 #endif
596 return rv;
599 nsresult
600 nsInlineFrame::ReflowInlineFrame(nsPresContext* aPresContext,
601 const nsHTMLReflowState& aReflowState,
602 InlineReflowState& irs,
603 nsIFrame* aFrame,
604 nsReflowStatus& aStatus)
606 nsLineLayout* lineLayout = aReflowState.mLineLayout;
607 PRBool reflowingFirstLetter = lineLayout->GetFirstLetterStyleOK();
608 PRBool pushedFrame;
609 nsresult rv =
610 lineLayout->ReflowFrame(aFrame, aStatus, nsnull, pushedFrame);
612 if (NS_FAILED(rv)) {
613 return rv;
615 if (NS_INLINE_IS_BREAK(aStatus)) {
616 if (NS_INLINE_IS_BREAK_BEFORE(aStatus)) {
617 if (aFrame != mFrames.FirstChild()) {
618 // Change break-before status into break-after since we have
619 // already placed at least one child frame. This preserves the
620 // break-type so that it can be propagated upward.
621 aStatus = NS_FRAME_NOT_COMPLETE |
622 NS_INLINE_BREAK | NS_INLINE_BREAK_AFTER |
623 (aStatus & NS_INLINE_BREAK_TYPE_MASK);
624 PushFrames(aPresContext, aFrame, irs.mPrevFrame);
626 else {
627 // Preserve reflow status when breaking-before our first child
628 // and propagate it upward without modification.
629 // Note: if we're lazily setting the frame pointer for our child
630 // frames, then we need to set it now. Don't return and leave the
631 // remaining child frames in our child list with the wrong parent
632 // frame pointer...
633 if (irs.mSetParentPointer) {
634 if (irs.mLineContainer && irs.mLineContainer->GetPrevContinuation()) {
635 ReparentFloatsForInlineChild(irs.mLineContainer, aFrame->GetNextSibling(),
636 PR_TRUE);
638 for (nsIFrame* f = aFrame->GetNextSibling(); f; f = f->GetNextSibling()) {
639 f->SetParent(this);
644 else {
645 // Break-after
646 if (NS_FRAME_IS_NOT_COMPLETE(aStatus)) {
647 nsIFrame* newFrame;
648 rv = CreateNextInFlow(aPresContext, this, aFrame, newFrame);
649 if (NS_FAILED(rv)) {
650 return rv;
653 nsIFrame* nextFrame = aFrame->GetNextSibling();
654 if (nextFrame) {
655 aStatus |= NS_FRAME_NOT_COMPLETE;
656 PushFrames(aPresContext, nextFrame, aFrame);
658 else if (nsnull != GetNextInFlow()) {
659 // We must return an incomplete status if there are more child
660 // frames remaining in a next-in-flow that follows this frame.
661 nsInlineFrame* nextInFlow = (nsInlineFrame*) GetNextInFlow();
662 while (nsnull != nextInFlow) {
663 if (nextInFlow->mFrames.NotEmpty()) {
664 aStatus |= NS_FRAME_NOT_COMPLETE;
665 break;
667 nextInFlow = (nsInlineFrame*) nextInFlow->GetNextInFlow();
672 else if (NS_FRAME_IS_NOT_COMPLETE(aStatus)) {
673 if (nsGkAtoms::placeholderFrame == aFrame->GetType()) {
674 nsBlockReflowState* blockRS = lineLayout->mBlockRS;
675 blockRS->mBlock->SplitPlaceholder(*blockRS, aFrame);
676 // Allow the parent to continue reflowing
677 aStatus = NS_FRAME_COMPLETE;
679 else {
680 nsIFrame* newFrame;
681 rv = CreateNextInFlow(aPresContext, this, aFrame, newFrame);
682 if (NS_FAILED(rv)) {
683 return rv;
685 if (!reflowingFirstLetter) {
686 nsIFrame* nextFrame = aFrame->GetNextSibling();
687 if (nextFrame) {
688 PushFrames(aPresContext, nextFrame, aFrame);
693 return rv;
696 nsIFrame*
697 nsInlineFrame::PullOneFrame(nsPresContext* aPresContext,
698 InlineReflowState& irs,
699 PRBool* aIsComplete)
701 PRBool isComplete = PR_TRUE;
703 nsIFrame* frame = nsnull;
704 nsInlineFrame* nextInFlow = irs.mNextInFlow;
705 while (nsnull != nextInFlow) {
706 frame = nextInFlow->mFrames.FirstChild();
707 if (nsnull != frame) {
708 // If our block has no next continuation, then any floats belonging to
709 // the pulled frame must belong to our block already. This check ensures
710 // we do no extra work in the common non-vertical-breaking case.
711 if (irs.mLineContainer && irs.mLineContainer->GetNextContinuation()) {
712 // The blockChildren.ContainsFrame check performed by
713 // ReparentFloatsForInlineChild will be fast because frame's ancestor
714 // will be the first child of its containing block.
715 ReparentFloatsForInlineChild(irs.mLineContainer, frame, PR_FALSE);
717 nextInFlow->mFrames.RemoveFirstChild();
718 mFrames.InsertFrame(this, irs.mPrevFrame, frame);
719 isComplete = PR_FALSE;
720 nsHTMLContainerFrame::ReparentFrameView(aPresContext, frame, nextInFlow, this);
721 break;
723 nextInFlow = (nsInlineFrame*) nextInFlow->GetNextInFlow();
724 irs.mNextInFlow = nextInFlow;
727 *aIsComplete = isComplete;
728 return frame;
731 void
732 nsInlineFrame::PushFrames(nsPresContext* aPresContext,
733 nsIFrame* aFromChild,
734 nsIFrame* aPrevSibling)
736 NS_PRECONDITION(nsnull != aFromChild, "null pointer");
737 NS_PRECONDITION(nsnull != aPrevSibling, "pushing first child");
738 NS_PRECONDITION(aPrevSibling->GetNextSibling() == aFromChild, "bad prev sibling");
740 #ifdef NOISY_PUSHING
741 printf("%p pushing aFromChild %p, disconnecting from prev sib %p\n",
742 this, aFromChild, aPrevSibling);
743 #endif
744 // Disconnect aFromChild from its previous sibling
745 aPrevSibling->SetNextSibling(nsnull);
747 // Add the frames to our overflow list (let our next in flow drain
748 // our overflow list when it is ready)
749 SetOverflowFrames(aPresContext, aFromChild);
753 //////////////////////////////////////////////////////////////////////
755 PRIntn
756 nsInlineFrame::GetSkipSides() const
758 PRIntn skip = 0;
759 if (!IsLeftMost()) {
760 nsInlineFrame* prev = (nsInlineFrame*) GetPrevContinuation();
761 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
762 (prev && (prev->mRect.height || prev->mRect.width))) {
763 // Prev continuation is not empty therefore we don't render our left
764 // border edge.
765 skip |= 1 << NS_SIDE_LEFT;
767 else {
768 // If the prev continuation is empty, then go ahead and let our left
769 // edge border render.
772 if (!IsRightMost()) {
773 nsInlineFrame* next = (nsInlineFrame*) GetNextContinuation();
774 if ((GetStateBits() & NS_INLINE_FRAME_BIDI_VISUAL_STATE_IS_SET) ||
775 (next && (next->mRect.height || next->mRect.width))) {
776 // Next continuation is not empty therefore we don't render our right
777 // border edge.
778 skip |= 1 << NS_SIDE_RIGHT;
780 else {
781 // If the next continuation is empty, then go ahead and let our right
782 // edge border render.
785 return skip;
788 #ifdef ACCESSIBILITY
789 NS_IMETHODIMP nsInlineFrame::GetAccessible(nsIAccessible** aAccessible)
791 // Broken image accessibles are created here, because layout
792 // replaces the image or image control frame with an inline frame
793 *aAccessible = nsnull;
794 nsIAtom *tagAtom = mContent->Tag();
795 if ((tagAtom == nsGkAtoms::img || tagAtom == nsGkAtoms::input ||
796 tagAtom == nsGkAtoms::label) && mContent->IsNodeOfType(nsINode::eHTML)) {
797 // Only get accessibility service if we're going to use it
798 nsCOMPtr<nsIAccessibilityService> accService(do_GetService("@mozilla.org/accessibilityService;1"));
799 if (!accService)
800 return NS_ERROR_FAILURE;
801 if (tagAtom == nsGkAtoms::input) // Broken <input type=image ... />
802 return accService->CreateHTMLButtonAccessible(NS_STATIC_CAST(nsIFrame*, this), aAccessible);
803 else if (tagAtom == nsGkAtoms::img) // Create accessible for broken <img>
804 return accService->CreateHTMLImageAccessible(NS_STATIC_CAST(nsIFrame*, this), aAccessible);
805 else if (tagAtom == nsGkAtoms::label) // Creat accessible for <label>
806 return accService->CreateHTMLLabelAccessible(NS_STATIC_CAST(nsIFrame*, this), aAccessible);
809 return NS_ERROR_FAILURE;
811 #endif
813 //////////////////////////////////////////////////////////////////////
815 // nsLineFrame implementation
817 static void
818 ReParentChildListStyle(nsPresContext* aPresContext,
819 nsFrameList& aFrameList,
820 nsIFrame* aParentFrame)
822 nsFrameManager *frameManager = aPresContext->FrameManager();
824 for (nsIFrame* kid = aFrameList.FirstChild(); kid;
825 kid = kid->GetNextSibling()) {
826 NS_ASSERTION(kid->GetParent() == aParentFrame, "Bogus parentage");
827 frameManager->ReParentStyleContext(kid);
831 nsIFrame*
832 NS_NewFirstLineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
834 return new (aPresShell) nsFirstLineFrame(aContext);
837 #ifdef DEBUG
838 NS_IMETHODIMP
839 nsFirstLineFrame::GetFrameName(nsAString& aResult) const
841 return MakeFrameName(NS_LITERAL_STRING("Line"), aResult);
843 #endif
845 nsIAtom*
846 nsFirstLineFrame::GetType() const
848 return nsGkAtoms::lineFrame;
851 void
852 nsFirstLineFrame::StealFramesFrom(nsIFrame* aFrame)
854 nsIFrame* prevFrame = mFrames.GetPrevSiblingFor(aFrame);
855 if (prevFrame) {
856 prevFrame->SetNextSibling(nsnull);
858 else {
859 mFrames.SetFrames(nsnull);
863 nsIFrame*
864 nsFirstLineFrame::PullOneFrame(nsPresContext* aPresContext, InlineReflowState& irs,
865 PRBool* aIsComplete)
867 nsIFrame* frame = nsInlineFrame::PullOneFrame(aPresContext, irs, aIsComplete);
868 if (frame && !GetPrevInFlow()) {
869 // We are a first-line frame. Fixup the child frames
870 // style-context that we just pulled.
871 NS_ASSERTION(frame->GetParent() == this, "Incorrect parent?");
872 aPresContext->FrameManager()->ReParentStyleContext(frame);
874 return frame;
877 NS_IMETHODIMP
878 nsFirstLineFrame::Reflow(nsPresContext* aPresContext,
879 nsHTMLReflowMetrics& aMetrics,
880 const nsHTMLReflowState& aReflowState,
881 nsReflowStatus& aStatus)
883 if (nsnull == aReflowState.mLineLayout) {
884 return NS_ERROR_INVALID_ARG;
887 nsIFrame* lineContainer = aReflowState.mLineLayout->GetLineContainerFrame();
889 // Check for an overflow list with our prev-in-flow
890 nsFirstLineFrame* prevInFlow = (nsFirstLineFrame*)GetPrevInFlow();
891 if (nsnull != prevInFlow) {
892 nsIFrame* prevOverflowFrames = prevInFlow->GetOverflowFrames(aPresContext, PR_TRUE);
893 if (prevOverflowFrames) {
894 nsFrameList frames(prevOverflowFrames);
896 // Assign all floats to our block if necessary
897 if (lineContainer && lineContainer->GetPrevContinuation()) {
898 ReparentFloatsForInlineChild(lineContainer, prevOverflowFrames, PR_TRUE);
900 mFrames.InsertFrames(this, nsnull, prevOverflowFrames);
901 ReParentChildListStyle(aPresContext, frames, this);
905 // It's also possible that we have an overflow list for ourselves
906 nsIFrame* overflowFrames = GetOverflowFrames(aPresContext, PR_TRUE);
907 if (overflowFrames) {
908 NS_ASSERTION(mFrames.NotEmpty(), "overflow list w/o frames");
909 nsFrameList frames(overflowFrames);
911 mFrames.AppendFrames(nsnull, overflowFrames);
912 ReParentChildListStyle(aPresContext, frames, this);
915 // Set our own reflow state (additional state above and beyond
916 // aReflowState)
917 InlineReflowState irs;
918 irs.mPrevFrame = nsnull;
919 irs.mLineContainer = lineContainer;
920 irs.mNextInFlow = (nsInlineFrame*) GetNextInFlow();
922 nsresult rv;
923 PRBool wasEmpty = mFrames.IsEmpty();
924 if (wasEmpty) {
925 // Try to pull over one frame before starting so that we know
926 // whether we have an anonymous block or not.
927 PRBool complete;
928 PullOneFrame(aPresContext, irs, &complete);
931 if (nsnull == GetPrevInFlow()) {
932 // XXX This is pretty sick, but what we do here is to pull-up, in
933 // advance, all of the next-in-flows children. We re-resolve their
934 // style while we are at at it so that when we reflow they have
935 // the right style.
937 // All of this is so that text-runs reflow properly.
938 irs.mPrevFrame = mFrames.LastChild();
939 for (;;) {
940 PRBool complete;
941 nsIFrame* frame = PullOneFrame(aPresContext, irs, &complete);
942 if (!frame) {
943 break;
945 irs.mPrevFrame = frame;
947 irs.mPrevFrame = nsnull;
949 else {
950 // XXX do this in the Init method instead
951 // For continuations, we need to check and see if our style
952 // context is right. If its the same as the first-in-flow, then
953 // we need to fix it up (that way :first-line style doesn't leak
954 // into this continuation since we aren't the first line).
955 nsFirstLineFrame* first = (nsFirstLineFrame*) GetFirstInFlow();
956 if (mStyleContext == first->mStyleContext) {
957 // Fixup our style context and our children. First get the
958 // proper parent context.
959 nsStyleContext* parentContext = first->GetParent()->GetStyleContext();
960 if (parentContext) {
961 // Create a new style context that is a child of the parent
962 // style context thus removing the :first-line style. This way
963 // we behave as if an anonymous (unstyled) span was the child
964 // of the parent frame.
965 nsRefPtr<nsStyleContext> newSC;
966 newSC = aPresContext->StyleSet()->
967 ResolvePseudoStyleFor(nsnull,
968 nsCSSAnonBoxes::mozLineFrame, parentContext);
969 if (newSC) {
970 // Switch to the new style context.
971 SetStyleContext(newSC);
973 // Re-resolve all children
974 ReParentChildListStyle(aPresContext, mFrames, this);
980 NS_ASSERTION(!aReflowState.mLineLayout->GetInFirstLine(),
981 "Nested first-line frames? BOGUS");
982 aReflowState.mLineLayout->SetInFirstLine(PR_TRUE);
983 rv = ReflowFrames(aPresContext, aReflowState, irs, aMetrics, aStatus);
984 aReflowState.mLineLayout->SetInFirstLine(PR_FALSE);
986 // Note: the line layout code will properly compute our overflow state for us
988 return rv;
991 //////////////////////////////////////////////////////////////////////
993 nsIFrame*
994 NS_NewPositionedInlineFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
996 return new (aPresShell) nsPositionedInlineFrame(aContext);
999 void
1000 nsPositionedInlineFrame::Destroy()
1002 mAbsoluteContainer.DestroyFrames(this);
1003 nsInlineFrame::Destroy();
1006 NS_IMETHODIMP
1007 nsPositionedInlineFrame::SetInitialChildList(nsIAtom* aListName,
1008 nsIFrame* aChildList)
1010 nsresult rv;
1012 if (mAbsoluteContainer.GetChildListName() == aListName) {
1013 rv = mAbsoluteContainer.SetInitialChildList(this, aListName, aChildList);
1014 } else {
1015 rv = nsInlineFrame::SetInitialChildList(aListName, aChildList);
1018 return rv;
1021 NS_IMETHODIMP
1022 nsPositionedInlineFrame::AppendFrames(nsIAtom* aListName,
1023 nsIFrame* aFrameList)
1025 nsresult rv;
1027 if (mAbsoluteContainer.GetChildListName() == aListName) {
1028 rv = mAbsoluteContainer.AppendFrames(this, aListName, aFrameList);
1029 } else {
1030 rv = nsInlineFrame::AppendFrames(aListName, aFrameList);
1033 return rv;
1036 NS_IMETHODIMP
1037 nsPositionedInlineFrame::InsertFrames(nsIAtom* aListName,
1038 nsIFrame* aPrevFrame,
1039 nsIFrame* aFrameList)
1041 nsresult rv;
1043 if (mAbsoluteContainer.GetChildListName() == aListName) {
1044 rv = mAbsoluteContainer.InsertFrames(this, aListName, aPrevFrame,
1045 aFrameList);
1046 } else {
1047 rv = nsInlineFrame::InsertFrames(aListName, aPrevFrame, aFrameList);
1050 return rv;
1053 NS_IMETHODIMP
1054 nsPositionedInlineFrame::RemoveFrame(nsIAtom* aListName,
1055 nsIFrame* aOldFrame)
1057 nsresult rv;
1059 if (mAbsoluteContainer.GetChildListName() == aListName) {
1060 rv = mAbsoluteContainer.RemoveFrame(this, aListName, aOldFrame);
1061 } else {
1062 rv = nsInlineFrame::RemoveFrame(aListName, aOldFrame);
1065 return rv;
1068 NS_IMETHODIMP
1069 nsPositionedInlineFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
1070 const nsRect& aDirtyRect,
1071 const nsDisplayListSet& aLists)
1073 aBuilder->MarkFramesForDisplayList(this, mAbsoluteContainer.GetFirstChild(), aDirtyRect);
1074 return nsHTMLContainerFrame::BuildDisplayList(aBuilder, aDirtyRect, aLists);
1077 nsIAtom*
1078 nsPositionedInlineFrame::GetAdditionalChildListName(PRInt32 aIndex) const
1080 if (0 == aIndex) {
1081 return mAbsoluteContainer.GetChildListName();
1083 return nsnull;
1086 nsIFrame*
1087 nsPositionedInlineFrame::GetFirstChild(nsIAtom* aListName) const
1089 if (mAbsoluteContainer.GetChildListName() == aListName) {
1090 nsIFrame* result = nsnull;
1091 mAbsoluteContainer.FirstChild(this, aListName, &result);
1092 return result;
1095 return nsInlineFrame::GetFirstChild(aListName);
1098 nsIAtom*
1099 nsPositionedInlineFrame::GetType() const
1101 return nsGkAtoms::positionedInlineFrame;
1104 NS_IMETHODIMP
1105 nsPositionedInlineFrame::Reflow(nsPresContext* aPresContext,
1106 nsHTMLReflowMetrics& aDesiredSize,
1107 const nsHTMLReflowState& aReflowState,
1108 nsReflowStatus& aStatus)
1110 nsresult rv = NS_OK;
1112 // Don't bother optimizing for fast incremental reflow of absolute
1113 // children of an inline
1115 // Let the inline frame do its reflow first
1116 rv = nsInlineFrame::Reflow(aPresContext, aDesiredSize, aReflowState, aStatus);
1118 // Let the absolutely positioned container reflow any absolutely positioned
1119 // child frames that need to be reflowed
1120 // We want to do this under either of two conditions:
1121 // 1. If we didn't do the incremental reflow above.
1122 // 2. If our size changed.
1123 // Even though it's the padding edge that's the containing block, we
1124 // can use our rect (the border edge) since if the border style
1125 // changed, the reflow would have been targeted at us so we'd satisfy
1126 // condition 1.
1127 if (NS_SUCCEEDED(rv) &&
1128 mAbsoluteContainer.HasAbsoluteFrames()) {
1129 // The containing block for the abs pos kids is formed by our padding edge.
1130 nsMargin computedBorder =
1131 aReflowState.mComputedBorderPadding - aReflowState.mComputedPadding;
1132 nscoord containingBlockWidth =
1133 aDesiredSize.width - computedBorder.LeftRight();
1134 nscoord containingBlockHeight =
1135 aDesiredSize.height - computedBorder.TopBottom();
1137 // Factor the absolutely positioned child bounds into the overflow area
1138 // Don't include this frame's bounds, nor its inline descendants' bounds,
1139 // and don't store the overflow property.
1140 // That will all be done by nsLineLayout::RelativePositionFrames.
1141 rv = mAbsoluteContainer.Reflow(this, aPresContext, aReflowState,
1142 containingBlockWidth, containingBlockHeight,
1143 PR_TRUE, PR_TRUE, // XXX could be optimized
1144 &aDesiredSize.mOverflowArea);
1147 return rv;